Thread xxxx panic: applying non-zero offset 65536 to null pointer when calling to a C function

the C code does something like this
PBYTE pbLast = (PBYTE)hModuleLast + MM_ALLOCATION_GRANULARITY;
Src:

the C library is also being compiled by zig
zig build -Dtarget=x86_64-windows-msvc --verbose prints out:

Z:\Home\.zig\zig.exe build-lib --stack 8388608 Z:\Home\projects\zig\Pawned\Detours\creatwth.cpp Z:\Home\projects\zig\Pawned\Detours\detours.cpp Z:\Home\projects\zig\Pawned\Detours\disasm.cpp Z:\Home\projects\zig\Pawned\Detours\disolarm.cpp Z:\Home\projects\zig\Pawned\Detours\disolarm64.cpp Z:\Home\projects\zig\Pawned\Detours\disolia64.cpp Z:\Home\projects\zig\Pawned\Detours\disolx64.cpp Z:\Home\projects\zig\Pawned\Detours\disolx86.cpp Z:\Home\projects\zig\Pawned\Detours\image.cpp Z:\Home\projects\zig\Pawned\Detours\modules.cpp -ODebug -target x86_64-windows-msvc -mcpu baseline 
-Mroot -lc --cache-dir .zig-cache --global-cache-dir C:\Users\User\AppData\Local\zig --name detours -static --zig-lib-dir Z:\Home\.zig\lib\ --listen=-
Z:\Home\.zig\zig.exe build-lib --stack 8388608 .zig-cache\o\0ef3714a13e7d8e7a75c27196944990a\detours.lib -ODebug -target x86_64-windows-msvc -mcpu baseline -I Z:\Home\projects\zig\Pawned\.zig-cache\o\26c55dcd1a21eeb719a1d04e8bce84d1 -I Z:\Home\projects\zig\Pawned\Detours -Mroot=Z:\Home\projects\zig\Pawned\src\dllmain.zig -lc -femit-implib --cache-dir .zig-cache --global-cache-dir C:\Users\User\AppData\Local\zig --name Pawned -dynamic --zig-lib-dir Z:\Home\.zig\lib\ --subsystem console --listen=-

Any suggestions what should I do?

Edit:
After building the Detours lib using msvc and then linking it with the zig seems to fix this issue, probably some runtime checks are being removed, which zig compiler keeps?

These are the commands msvc was executing:

cl /nologo /W4 /WX /we4777 /we4800 /Zi /MT /Gy /Gm- /Zl /Od /DDETOUR_DEBUG=0 /DWIN32_LEAN_AND_MEAN /D_WIN32_WINNT=0x501 /Fd..\lib.X64\detours.pdb /Foobj.X64\ /c detours.cpp modules.cpp disasm.cpp image.cpp creatwth.cpp disolx86.cpp disolx64.cpp disolia64.cpp disolarm.cpp disolarm64.cpp

link /lib /nologo /out:..\lib.X64\detours.lib obj.X64\detours.obj      obj.X64\modules.obj      obj.X64\disasm.obj       obj.X64\image.obj        obj.X64\creatwth.obj     obj.X64\disolx86.obj     obj.X64\disolx64.obj     obj.X64\disolia64.obj    obj.X64\disolarm.obj     obj.X64\disolarm64.obj

Zig enables clang’s undefined behavior sanitizer by default when compiling in Debug mode, so this is potentially finding undefined behavior in Detours.

If you want to ignore it, you can turn off UBSan by passing the flag -fno-sanitize=undefined to the relevant step in your build.zig, or you can build that dependency in ReleaseFast.

If you want to get it fixed, try reproducing it with a C++ compiler to confirm and then report it to the Detours repo (if it hasn’t been reported already).

(this being enabled by default in Zig has led a lot of undefined behavior being uncovered in various C/C++ programs/libraries)

3 Likes

You mean by enabling sanitizer of some sort and when compiling with another C++ compiler?

1 Like

Yes, clang with sanitizer enabled should give you the same error, if this is the cause of the crash.

1 Like