test glibc_runtime_check: add strlcpy() check

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).
This commit is contained in:
Pat Tullmann 2023-11-28 20:26:17 -08:00 committed by Andrew Kelley
parent 4a5d73a34d
commit cbaaf6477f
2 changed files with 29 additions and 1 deletions

View File

@ -90,9 +90,15 @@ 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
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

View File

@ -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;
@ -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
@ -86,6 +107,7 @@ fn checkAtExit() !void {
pub fn main() !u8 {
try checkStat();
try checkReallocarray();
try checkStrlcpy();
try checkGetAuxVal();
try checkAtExit();