authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-12 13:13:48-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-12 13:14:51-08:00
log6d52678a6cfee1bc05d995bef8d963a5921ed8ea
treeadd8e77992efcfe39504068a95f9da74e59d7df2
parentec02571a30e2975039cef5cb92467082ea5de5c0

zig libc malloc: skip export when unit testing

These functions can only be exported when external libc components are available due to the errno location dependency. Note that even when zig libc is complete, on Windows, errno location will always be external (in ucrtbase.dll).

3 files changed, 40 insertions(+), 22 deletions(-)

build.zig+1-1
......@@ -518,7 +518,7 @@ pub fn build(b: *std.Build) !void {
518518 .test_extra_targets = test_extra_targets,
519519 .root_src = "lib/c.zig",
520520 .name = "zigc",
521 .desc = "Run the zigc tests",
521 .desc = "Run the zig libc implementation unit tests",
522522 .optimize_modes = optimization_modes,
523523 .include_paths = &.{},
524524 .skip_single_threaded = true,
lib/c.zig+25-10
......@@ -15,17 +15,32 @@ pub const panic = if (builtin.is_test)
1515else
1616 std.debug.no_panic;
1717
18/// Determines the symbol's visibility to other objects.
19/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
20/// export it to the host runtime.
21pub const visibility: std.builtin.SymbolVisibility = .hidden;
22
18/// It is possible that this libc is being linked into a different test
19/// compilation, as opposed to being tested itself. In such case,
20/// `builtin.link_libc` will be `true` along with `builtin.is_test`.
21///
22/// When we don't have a complete libc, `builtin.link_libc` will be `false` and
23/// we will be missing externally provided symbols, such as `_errno` from
24/// ucrtbase.dll. In such case, we must avoid analyzing otherwise exported
25/// functions because it would cause undefined symbol usage.
26///
27/// Unfortunately such logic cannot be automatically done in this function body
28/// since `func` will always be analyzed by the time we get here, so `comptime`
29/// blocks will need to each check for `builtin.link_libc` and skip exports
30/// when the exported functions have libc dependencies not provided by this
31/// compilation unit.
2332pub inline fn symbol(comptime func: *const anyopaque, comptime name: []const u8) void {
24 // Normally, libc goes into a static archive, making all symbols
25 // overridable. However, Zig supports including the libc functions as part
26 // of the Zig Compilation Unit, so to support this use case we make all
27 // symbols weak.
28 @export(func, .{ .name = name, .linkage = .weak, .visibility = visibility });
33 @export(func, .{
34 .name = name,
35 // Normally, libc goes into a static archive, making all symbols
36 // overridable. However, Zig supports including the libc functions as part
37 // of the Zig Compilation Unit, so to support this use case we make all
38 // symbols weak.
39 .linkage = .weak,
40 // For WebAssembly, hidden visibility allows the symbol to be resolved to
41 // other modules, but will not export it to the host runtime.
42 .visibility = .hidden,
43 });
2944}
3045
3146/// Given a low-level syscall return value, sets errno and returns `-1`, or on
lib/c/malloc.zig+14-11
......@@ -23,17 +23,20 @@ const alignment: Alignment = .fromByteUnits(alignment_bytes);
2323const symbol = @import("../c.zig").symbol;
2424
2525comptime {
26 symbol(&malloc, "malloc");
27 symbol(&aligned_alloc, "aligned_alloc");
28 symbol(&posix_memalign, "posix_memalign");
29 symbol(&calloc, "calloc");
30 symbol(&realloc, "realloc");
31 symbol(&reallocarray, "reallocarray");
32 symbol(&free, "free");
33 symbol(&malloc_usable_size, "malloc_usable_size");
34
35 symbol(&valloc, "valloc");
36 symbol(&memalign, "memalign");
26 // Dependency on external errno location.
27 if (builtin.link_libc) {
28 symbol(&malloc, "malloc");
29 symbol(&aligned_alloc, "aligned_alloc");
30 symbol(&posix_memalign, "posix_memalign");
31 symbol(&calloc, "calloc");
32 symbol(&realloc, "realloc");
33 symbol(&reallocarray, "reallocarray");
34 symbol(&free, "free");
35 symbol(&malloc_usable_size, "malloc_usable_size");
36
37 symbol(&valloc, "valloc");
38 symbol(&memalign, "memalign");
39 }
3740}
3841
3942const no_context: *anyopaque = undefined;