mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests/size: Fix SIGSEGV on 32-bit x86 due to uninitialized vDSO pointer
@ 2026-09-29  4:36 Wake Liu
  0 siblings, 0 replies; only message in thread
From: Wake Liu @ 2026-09-29  4:36 UTC (permalink / raw)
  To: Shuah Khan, linux-kselftest
  Cc: Tim Bird, Siddhesh Poyarekar, linux-kernel, Wake Liu

Commit 6b64a650f0b2 ("kselftest: Minimise dependency of get_size on C
library interfaces") replaced direct C library calls with syscall() to
avoid accessing uninitialized TLS on x86_64 when built with
-nostartfiles.

However, on 32-bit x86 (i386), the C library's syscall() wrapper itself
relies on CRT startup initialization to dispatch system calls via the
vDSO entry point:
- In glibc (libc.a), syscall() executes "call *%gs:0x10", reading the
  __kernel_vsyscall pointer from the Thread Control Block (TCB).
- In Android Bionic (libc.a), syscall() calls __kernel_syscall(), which
  returns the global __libc_sysinfo pointer in .bss.

Because get_size is compiled with -static -ffreestanding -nostartfiles
and defines its own _start, CRT and TLS initialization never run. As a
result, calling libc's syscall() on 32-bit x86 dereferences an
uninitialized %gs selector or jumps to NULL (0x0), crashing immediately
with SIGSEGV.

Provide a local syscall() implementation on __i386__ that invokes
"int $0x80" directly, avoiding the C library's syscall() wrapper and
its CRT initialization dependency.

Fixes: 6b64a650f0b2 ("kselftest: Minimise dependency of get_size on C library interfaces")
Signed-off-by: Wake Liu <wakel@google.com>
---
 tools/testing/selftests/size/get_size.c | 29 +++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/tools/testing/selftests/size/get_size.c b/tools/testing/selftests/size/get_size.c
index 2980b1a63366..352c90d5c770 100644
--- a/tools/testing/selftests/size/get_size.c
+++ b/tools/testing/selftests/size/get_size.c
@@ -32,6 +32,35 @@
 
 #define STDOUT_FILENO 1
 
+#ifdef __i386__
+#include <stdarg.h>
+
+/*
+ * On 32-bit x86, libc's syscall() wrapper invokes the vDSO entry point
+ * stored in TLS (%gs:0x10 in glibc) or a global variable (__libc_sysinfo
+ * in Bionic) initialized by CRT startup routines. Because this binary is
+ * built with -nostartfiles, those pointers remain uninitialized and
+ * calling libc's syscall() crashes with SIGSEGV. Use int $0x80 directly.
+ */
+long syscall(long num, ...)
+{
+	va_list ap;
+	long a1, a2, a3, ret;
+
+	va_start(ap, num);
+	a1 = va_arg(ap, long);
+	a2 = va_arg(ap, long);
+	a3 = va_arg(ap, long);
+	va_end(ap);
+
+	__asm__ volatile("int $0x80"
+		: "=a"(ret)
+		: "a"(num), "b"(a1), "c"(a2), "d"(a3)
+		: "memory", "cc");
+	return ret;
+}
+#endif
+
 static int print(const char *s)
 {
 	size_t len = 0;
-- 
2.56.0.rc1.315.gc6ed9934b7-goog


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-29  4:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29  4:36 [PATCH] selftests/size: Fix SIGSEGV on 32-bit x86 due to uninitialized vDSO pointer Wake Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®