aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPat Tullmann <pat.github@tullmann.org>2023-11-28 20:26:17 -0800
committerAndrew Kelley <andrew@ziglang.org>2024-01-04 17:12:07 -0700
commitcbaaf6477fa314257a550515886563d6e7a06fc7 (patch)
tree30b76ec92695a6018f69c65cc7b5912d19432739
parent4a5d73a34d6473a6970d6aaa31703cfa43135729 (diff)
downloadzig-cbaaf6477fa314257a550515886563d6e7a06fc7.tar.gz
zig-cbaaf6477fa314257a550515886563d6e7a06fc7.zip
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).
-rw-r--r--test/link/glibc_compat/build.zig8
-rw-r--r--test/link/glibc_compat/glibc_runtime_check.zig22
2 files changed, 29 insertions, 1 deletions
diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig
index cb2a906c2c..e0cc1a70d8 100644
--- a/test/link/glibc_compat/build.zig
+++ b/test/link/glibc_compat/build.zig
@@ -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
diff --git a/test/link/glibc_compat/glibc_runtime_check.zig b/test/link/glibc_compat/glibc_runtime_check.zig
index 2866fefc67..7344bafc6e 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();