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).
This commit is contained in:
Andrew Kelley 2026-02-12 13:13:48 -08:00
parent ec02571a30
commit 6d52678a6c
3 changed files with 39 additions and 21 deletions

View file

@ -518,7 +518,7 @@ pub fn build(b: *std.Build) !void {
.test_extra_targets = test_extra_targets,
.root_src = "lib/c.zig",
.name = "zigc",
.desc = "Run the zigc tests",
.desc = "Run the zig libc implementation unit tests",
.optimize_modes = optimization_modes,
.include_paths = &.{},
.skip_single_threaded = true,

View file

@ -15,17 +15,32 @@ pub const panic = if (builtin.is_test)
else
std.debug.no_panic;
/// Determines the symbol's visibility to other objects.
/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
/// export it to the host runtime.
pub const visibility: std.builtin.SymbolVisibility = .hidden;
/// It is possible that this libc is being linked into a different test
/// compilation, as opposed to being tested itself. In such case,
/// `builtin.link_libc` will be `true` along with `builtin.is_test`.
///
/// When we don't have a complete libc, `builtin.link_libc` will be `false` and
/// we will be missing externally provided symbols, such as `_errno` from
/// ucrtbase.dll. In such case, we must avoid analyzing otherwise exported
/// functions because it would cause undefined symbol usage.
///
/// Unfortunately such logic cannot be automatically done in this function body
/// since `func` will always be analyzed by the time we get here, so `comptime`
/// blocks will need to each check for `builtin.link_libc` and skip exports
/// when the exported functions have libc dependencies not provided by this
/// compilation unit.
pub inline fn symbol(comptime func: *const anyopaque, comptime name: []const u8) void {
// Normally, libc goes into a static archive, making all symbols
// overridable. However, Zig supports including the libc functions as part
// of the Zig Compilation Unit, so to support this use case we make all
// symbols weak.
@export(func, .{ .name = name, .linkage = .weak, .visibility = visibility });
@export(func, .{
.name = name,
// Normally, libc goes into a static archive, making all symbols
// overridable. However, Zig supports including the libc functions as part
// of the Zig Compilation Unit, so to support this use case we make all
// symbols weak.
.linkage = .weak,
// For WebAssembly, hidden visibility allows the symbol to be resolved to
// other modules, but will not export it to the host runtime.
.visibility = .hidden,
});
}
/// Given a low-level syscall return value, sets errno and returns `-1`, or on

View file

@ -23,17 +23,20 @@ const alignment: Alignment = .fromByteUnits(alignment_bytes);
const symbol = @import("../c.zig").symbol;
comptime {
symbol(&malloc, "malloc");
symbol(&aligned_alloc, "aligned_alloc");
symbol(&posix_memalign, "posix_memalign");
symbol(&calloc, "calloc");
symbol(&realloc, "realloc");
symbol(&reallocarray, "reallocarray");
symbol(&free, "free");
symbol(&malloc_usable_size, "malloc_usable_size");
// Dependency on external errno location.
if (builtin.link_libc) {
symbol(&malloc, "malloc");
symbol(&aligned_alloc, "aligned_alloc");
symbol(&posix_memalign, "posix_memalign");
symbol(&calloc, "calloc");
symbol(&realloc, "realloc");
symbol(&reallocarray, "reallocarray");
symbol(&free, "free");
symbol(&malloc_usable_size, "malloc_usable_size");
symbol(&valloc, "valloc");
symbol(&memalign, "memalign");
symbol(&valloc, "valloc");
symbol(&memalign, "memalign");
}
}
const no_context: *anyopaque = undefined;