Skip to content

Commit

Permalink
test glibc_runtime_check: add strlcpy() check
Browse files Browse the repository at this point in the history
The strlcpy symbol was added in v2.38, so this is a handy symbol for
creating binaries that won't run on relatively modern systems (e.g., mine,
that has glibc 2.36 installed).
  • Loading branch information
rootbeer committed Dec 4, 2023
1 parent 33d3d3d commit dd18830
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/link/glibc_compat/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ pub fn build(b: *std.Build) void {
} else {
check.checkInDynamicSymtab();
check.checkExact("0 0 UND FUNC GLOBAL DEFAULT reallocarray");
}

// before v2.38 strlcpy is not supported
check.checkStart();
if (glibc_ver.order(.{ .major = 2, .minor = 38, .patch = 0 }) == .lt) {
check.checkInDynamicSymtab();
check.checkNotPresent("strlcpy");
} else {
check.checkInDynamicSymtab();
check.checkNotPresent("reallocarray");
check.checkExact("0 0 UND FUNC GLOBAL DEFAULT strlcpy");
}

// v2.16 introduced getauxval(), so always present
Expand Down
22 changes: 22 additions & 0 deletions test/link/glibc_compat/glibc_runtime_check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const c_stdlib = @cImport(
@cInclude("stdlib.h"), // for atexit
);

const c_string = @cImport(
@cInclude("string.h"), // for strlcpy
);

// Version of glibc this test is being built to run against
const glibc_ver = builtin.target.os.version_range.linux.glibc;

Expand Down Expand Up @@ -73,6 +77,23 @@ fn checkGetAuxVal_v2_16() !void {
assert(pgsz != 0);
}

// strlcpy introduced in v2.38, which is newer than many installed glibcs
fn checkStrlcpy() !void {
if (comptime glibc_ver.order(.{ .major = 2, .minor = 38, .patch = 0 }) == .lt) {
if (@hasDecl(c_string, "strlcpy")) {
@compileError("Before v2.38 glibc does not define 'strlcpy'");
}
} else {
try checkStrlcpy_v2_38();
}
}

fn checkStrlcpy_v2_38() !void {
var buf: [99]u8 = undefined;
const used = c_string.strlcpy(&buf, "strlcpy works!", buf.len);
assert(used == 15);
}

// atexit is part of libc_nonshared, so ensure its linked in correctly
fn forceExit0Callback() callconv(.C) void {
std.c.exit(0); // Override the main() exit code
Expand All @@ -86,6 +107,7 @@ fn checkAtExit() !void {
pub fn main() !u8 {
try checkStat();
try checkReallocarray();
try checkStrlcpy();

try checkGetAuxVal();
try checkAtExit();
Expand Down

0 comments on commit dd18830

Please sign in to comment.