# Segmentation fault at address 0x4 using C library

**URL:** <https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468>\
**Category:** Help\
**Tags:** c, build-system\
**Created:** [May 27, 2024, 8:43pm UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468 "2024-05-27T20:43:53Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 3, 2024, 10:54pm UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/21 "2024-06-03T22:54:27Z")

</div>

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

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 4, 2024, 7:08am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/22 "2024-06-04T07:08:22Z")

</div>

> [@squeek502](#):
>
> ```zig
> 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.

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 4, 2024, 11:27pm UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/23 "2024-06-04T23:27:39Z")

</div>

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:

```zig
const std = @import("std");

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

```

with

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

```

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 5, 2024, 7:00am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/24 "2024-06-05T07:00:43Z")

</div>

I don’t get any output ☹  
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.

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 5, 2024, 10:14am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/25 "2024-06-05T10:14:10Z")

</div>

It attempts to pick the newest version.

You can see the paths it’s detecting by doing

```zig
zig libc

```

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 6, 2024, 8:27am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/26 "2024-06-06T08:27:10Z")

</div>

I think something weird is happening on this laptop…

```zig
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…

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [June 6, 2024, 8:58am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/27 "2024-06-06T08:58:33Z")

</div>

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
❯ 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=

```

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 7, 2024, 6:26am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/28 "2024-06-07T06:26:07Z")

</div>

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:

```zig
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.

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 7, 2024, 7:11am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/29 "2024-06-07T07:11:19Z")

</div>

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:

```zig
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

```

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 7, 2024, 7:29am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/30 "2024-06-07T07:29:50Z")

</div>

Nice, that’s very helpful. This is a bug (and a silly one at that). Here’s a [commit that should fix it](https://github.com/squeek502/zig/commit/7bcb350a4eb259f811217038b1e65674d08533d6).

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](https://www.ryanliptak.com/misc/zig-libc-fix-20240607.zip)

Or, you can apply [this change](https://github.com/squeek502/zig/commit/7bcb350a4eb259f811217038b1e65674d08533d6) 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.

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 7, 2024, 8:46am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/31 "2024-06-07T08:46:03Z")

</div>

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

```zig
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=

```

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 7, 2024, 9:14am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/32 "2024-06-07T09:14:55Z")

</div>

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](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. 🙂

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 7, 2024, 9:57am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/33 "2024-06-07T09:57:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [June 9, 2024, 1:52am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/34 "2024-06-09T01:52:04Z")

</div>

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](https://gist.github.com/squeek502/cba5454291643320b6906a6722138ef6), but it’s working fine for me:

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

```

---

<div class="post-metadata">

**Author:** ![niso](https://ziggit.dev/user_avatar/ziggit.dev/niso/32/1089_2.png) [@niso](https://ziggit.dev/u/niso)\
**Post date:** [June 10, 2024, 8:53am UTC](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468/35 "2024-06-10T08:53:24Z")

</div>

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…

[Previous page](https://ziggit.dev/t/segmentation-fault-at-address-0x4-using-c-library/4468.md?page=1)
