authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2023-11-28 20:26:17-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-04 17:12:07-07:00
logcbaaf6477fa314257a550515886563d6e7a06fc7
tree30b76ec92695a6018f69c65cc7b5912d19432739
parent4a5d73a34d6473a6970d6aaa31703cfa43135729

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).

2 files changed, 29 insertions(+), 1 deletions(-)

test/link/glibc_compat/build.zig+7-1
......@@ -90,9 +90,15 @@ pub fn build(b: *std.Build) void {
9090 } else {
9191 check.checkInDynamicSymtab();
9292 check.checkExact("0 0 UND FUNC GLOBAL DEFAULT reallocarray");
93 }
94
95 // before v2.38 strlcpy is not supported
96 if (glibc_ver.order(.{ .major = 2, .minor = 38, .patch = 0 }) == .lt) {
97 check.checkInDynamicSymtab();
98 check.checkNotPresent("strlcpy");
9399 } else {
94100 check.checkInDynamicSymtab();
95 check.checkNotPresent("reallocarray");
101 check.checkExact("0 0 UND FUNC GLOBAL DEFAULT strlcpy");
96102 }
97103
98104 // v2.16 introduced getauxval(), so always present
test/link/glibc_compat/glibc_runtime_check.zig+22
......@@ -16,6 +16,10 @@ const c_stdlib = @cImport(
1616 @cInclude("stdlib.h"), // for atexit
1717);
1818
19const c_string = @cImport(
20 @cInclude("string.h"), // for strlcpy
21);
22
1923// Version of glibc this test is being built to run against
2024const glibc_ver = builtin.target.os.version_range.linux.glibc;
2125
......@@ -73,6 +77,23 @@ fn checkGetAuxVal_v2_16() !void {
7377 assert(pgsz != 0);
7478}
7579
80// strlcpy introduced in v2.38, which is newer than many installed glibcs
81fn checkStrlcpy() !void {
82 if (comptime glibc_ver.order(.{ .major = 2, .minor = 38, .patch = 0 }) == .lt) {
83 if (@hasDecl(c_string, "strlcpy")) {
84 @compileError("Before v2.38 glibc does not define 'strlcpy'");
85 }
86 } else {
87 try checkStrlcpy_v2_38();
88 }
89}
90
91fn checkStrlcpy_v2_38() !void {
92 var buf: [99]u8 = undefined;
93 const used = c_string.strlcpy(&buf, "strlcpy works!", buf.len);
94 assert(used == 15);
95}
96
7697// atexit is part of libc_nonshared, so ensure its linked in correctly
7798fn forceExit0Callback() callconv(.C) void {
7899 std.c.exit(0); // Override the main() exit code
......@@ -86,6 +107,7 @@ fn checkAtExit() !void {
86107pub fn main() !u8 {
87108 try checkStat();
88109 try checkReallocarray();
110 try checkStrlcpy();
89111
90112 try checkGetAuxVal();
91113 try checkAtExit();