Segmentation fault at address 0x4 using C library

Ah, my bad, Zig doesn’t quote the command arguments when printing them. Try this:

C:\Users\USER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\zig.exe build-exe -lNeuralaInspectorLib -ODebug -target x86_64-windows-msvc -mcpu baseline -I D:\Devel\Neurala\zig_test\include -I "C:\Program Files\Neurala\InspectorLib\include" -L "C:\Program Files\Neurala\InspectorLib\lib" -Mroot=D:\Devel\Neurala\zig_test\src\example_api.zig -lc --cache-dir D:\Devel\Neurala\zig_test\.zig-cache --global-cache-dir C:\Users\USER\AppData\Local\zig --name zig_example

I don’t get any output when I run this command nor I see any executable created…
If I remove the msvc target flag then I see the output and the executable is created but with the segfault 0x4.

Possibly a compiler crash. Not sure how to proceed without being able to reproduce the problem or you building a debug version of the compiler.

As a sanity check, could you try compiling this:

const std = @import("std");

pub fn main() !void {
    std.debug.print("hello\n", .{});
}

with

zig build-exe hello.zig -lc -target x86_64-windows-msvc

I don’t get any output :frowning:
On the other hand, is there any possibility to select a specific version of Visual studio compiler? I have 3 different version of VS (to support legacy code) and I wonder if I can select the specific version of the vs compiler I want to use.

It attempts to pick the newest version.

You can see the paths it’s detecting by doing

zig libc

I think something weird is happening on this laptop…

C:\Users\MYUSER>zig libc
►►►►►►►►►►►►►►►►►►►►►►►►►►►►error: the following build command failed with exit code 5:
C:\Users\MYUSER\AppData\Local\zig\o\d755b3787e455d1ecb0e4cbb7760ceb1\libc.exe AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib

OTOH, Can this be related with the original issue? I still don’t understand why the C code does not give any segfault…

Try to install the following “Individual components” using Visual Studio Installer:

  • Windows 10 SDK (10.0.20348.0)
  • MSVC v143 - VS 2022 C++ x64/x86 build tools (latest)

then retry zig libc.
It is fine to have different versions, VS 2019 or Windows 11 SDK.

The expected zig libc output looks like:

❯ zig libc
# The directory that contains `stdlib.h`.
# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
include_dir=C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\ucrt

# The system-specific include directory. May be the same as `include_dir`.
# On Windows it's the directory that includes `vcruntime.h`.
# On POSIX it's the directory that includes `sys/errno.h`.
sys_include_dir=C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.40.33807\include

# The directory that contains `crt1.o` or `crt2.o`.
# On POSIX, can be found with `cc -print-file-name=crt1.o`.
# Not needed when targeting MacOS.
crt_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64

# The directory that contains `vcruntime.lib`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.40.33807\Lib\x64

# The directory that contains `kernel32.lib`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64

# The directory that contains `crtbeginS.o` and `crtendS.o`
# Only needed when targeting Haiku.
gcc_dir=

Ah, very strange. It’s actually the libc subcommand that’s failing, and the stuff looks like the progress bar freaking out.

zig libc compiles a libc.exe on demand and then executes it. By default, it’s built in ReleaseFast, but you can force it to compile in Debug mode by doing:

set ZIG_DEBUG_CMD=1
zig libc

If that still gives you the same result (just an exit code without a stack trace), try taking the failing command and running it directly.


Note though that this whole MSVC wild goose chase is only because I was curious if the original problem is due to something ABI related, which may not even be the case.

I’ve installed that specific version of Windows 10 SDK (I’ve already had that version of MSVC) and it is still failing.
Here is the output when I set the debug flag:

C:\Users\MYUSER>set ZIG_DEBUG_CMD=1

C:\Users\MYUSER>zig libc
►►►►►►►►►►►►thread 17096 panic: index out of bounds: index 0, len 0
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\zig\WindowsSdk.zig:483:36: 0x26da44 in findFromRoot (libc.exe.obj)
            break :version versions[0];
                                   ^
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\zig\WindowsSdk.zig:420:46: 0x2581da in find (libc.exe.obj)
            const installation = findFromRoot(allocator, roots_key, roots_subkey, prefix) catch
                                             ^
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\zig\WindowsSdk.zig:39:43: 0x21ac73 in find (libc.exe.obj)
    const windows81sdk = Installation.find(allocator, roots_key, "KitsRoot81", "winver", "v8.1") catch |err| switch (err) {
                                          ^
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\zig\LibCInstallation.zig:185:44: 0x20ca86 in findNative (libc.exe.obj)
        const sdk = std.zig.WindowsSdk.find(args.allocator) catch |err| switch (err) {
                                           ^
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\compiler\libc.zig:119:47: 0x20ee43 in main (libc.exe.obj)
        var libc = LibCInstallation.findNative(.{
                                              ^
C:\Users\MYUSER\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\start.zig:363:53: 0x2115fc in WinStartup (libc.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
???:?:?►: 0x7ffed1b47343 in ??? (KERNEL32.DLL)
???:?:?: 0x7ffed1c826b0 in ??? (ntdll.dll)
►►►►►►►►►►►►►►►►►►►►►►►►►error: the following build command failed with exit code 3:
C:\Users\MYUSER\AppData\Local\zig\o\ac4431035e2b64c6e78bfee38ba6d59a\libc.exe AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib
1 Like

Nice, that’s very helpful. This is a bug (and a silly one at that). Here’s a commit that should fix it.

If you’re comfortable with it, here’s a libc.exe I compiled locally with the fix included if you want to test it (run it as libc.exe "<path to your zig lib dir>" which judging from the failing command output would be AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib):

https://www.ryanliptak.com/misc/zig-libc-fix-20240607.zip

Or, you can apply this change locally to the lib/std/zig/WindowsSdk.zig in your zig install and then running zig libc again should recompile your libc.exe with the fix included.

1 Like

Nice, since it was only a line, I applied the change locally. Here is the output:

PS D:\Devel> zig libc
# The directory that contains `stdlib.h`.
# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
include_dir=C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\ucrt

# The system-specific include directory. May be the same as `include_dir`.
# On Windows it's the directory that includes `vcruntime.h`.
# On POSIX it's the directory that includes `sys/errno.h`.
sys_include_dir=C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.40.33807\include

# The directory that contains `crt1.o` or `crt2.o`.
# On POSIX, can be found with `cc -print-file-name=crt1.o`.
# Not needed when targeting MacOS.
crt_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64

# The directory that contains `vcruntime.lib`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.40.33807\Lib\x64   

# The directory that contains `kernel32.lib`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64

# The directory that contains `crtbeginS.o` and `crtendS.o`
# Only needed when targeting Haiku.
gcc_dir=
1 Like

Looks good! Unfortunately you’d need to compile Zig from source with that fix included or wait for https://github.com/ziglang/zig/pull/20222 to be merged before the x86_64-windows-msvc target will work for you, though (the main zig compiler has WindowsSdk.zig compiled into it as well).

Still might be totally unrelated to your problem, but at least it’s led to a real bug fix. :slight_smile:

1 Like

Awesome! I think I’ll wait till the fix is merged.
Do you have any other idea about the original problem?
How different is anyopaque to void* from the compiler point of view?
Maybe zig translate-c is not able to convert the C header file into zig?

For the original problem, I think the most helpful thing would be to try to find a reproduction that doesn’t rely on linking to proprietary libraries (if possible).

Here’s my attempt, but it’s working fine for me:

> zig build
> .\zig-out\bin\neurala.exe
Loading model...Loading was OK

Hmm this is exactly what I was trying to do. Build like a mock that produces the same error and then understand why it gives such segfault. But as you, everything I did was working fine…

1 Like