From dd188307b45b562d409da7d308bac100336b2f78 Mon Sep 17 00:00:00 2001 From: Pat Tullmann Date: Tue, 28 Nov 2023 20:26:17 -0800 Subject: [PATCH] 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). --- test/link/glibc_compat/build.zig | 9 +++++++- .../link/glibc_compat/glibc_runtime_check.zig | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index 3506be7dd848..8b7ec1deed90 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -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 diff --git a/test/link/glibc_compat/glibc_runtime_check.zig b/test/link/glibc_compat/glibc_runtime_check.zig index 2866fefc67ad..7344bafc6ed2 100644 --- a/test/link/glibc_compat/glibc_runtime_check.zig +++ b/test/link/glibc_compat/glibc_runtime_check.zig @@ -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();