Skip to content

Commit 82af008

Browse files
authored
[safestack] Various 32-bit Linux fixes (#99455)
When enabling 32-bit testing on Linux/i386 and Linux/sparc, many tests `FAIL`: - All Linux/i386 tests `FAIL` with ``` safestack CHECK failed: /vol/llvm/src/llvm-project/local/compiler-rt/lib/safestack/safestack.cpp:95 MAP_FAILED != addr ``` because the safestack `mmap` implementation doesn't work there. This patch adjusts it to match the `sanitizer_linux.cpp.c` one. - On 32-bit Linux/sparc, the `pthread*.c` tests `FAIL` because a `tid_t` (`uint64_t`) `tid` arg was passed to `syscall(SYS_tgkill)` while `tid` is actually a `pid_t` (`int`). Fixed by adding a cast. Tested on `x86_64-pc-linux-gnu` (32 and 64-bit) and `sparc64-unknown-linux-gnu` (32 and 64-bit).
1 parent 79a0b66 commit 82af008

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

compiler-rt/lib/safestack/safestack_platform.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file implements platform specific parts of SafeStack runtime.
10+
// Don't use equivalent functionality from sanitizer_common to avoid dragging
11+
// a large codebase into security sensitive code.
1012
//
1113
//===----------------------------------------------------------------------===//
1214

@@ -45,6 +47,19 @@ extern "C" void *__mmap(void *, size_t, int, int, int, int, off_t);
4547
# include <thread.h>
4648
#endif
4749

50+
// Keep in sync with sanitizer_linux.cpp.
51+
//
52+
// Are we using 32-bit or 64-bit Linux syscalls?
53+
// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
54+
// but it still needs to use 64-bit syscalls.
55+
#if SANITIZER_LINUX && \
56+
(defined(__x86_64__) || defined(__powerpc64__) || \
57+
SANITIZER_WORDSIZE == 64 || (defined(__mips__) && _MIPS_SIM == _ABIN32))
58+
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
59+
#else
60+
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
61+
#endif
62+
4863
namespace safestack {
4964

5065
#if SANITIZER_NETBSD
@@ -117,7 +132,8 @@ inline int TgKill(pid_t pid, ThreadId tid, int sig) {
117132
#elif SANITIZER_FREEBSD
118133
return syscall(SYS_thr_kill2, pid, tid, sig);
119134
#else
120-
return syscall(SYS_tgkill, pid, tid, sig);
135+
// tid is pid_t (int), not ThreadId (uint64_t).
136+
return syscall(SYS_tgkill, pid, (pid_t)tid, sig);
121137
#endif
122138
}
123139

@@ -129,8 +145,13 @@ inline void *Mmap(void *addr, size_t length, int prot, int flags, int fd,
129145
return (void *)__syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
130146
#elif SANITIZER_SOLARIS
131147
return _REAL64(mmap)(addr, length, prot, flags, fd, offset);
132-
#else
148+
#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
133149
return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
150+
#else
151+
// mmap2 specifies file offset in 4096-byte units.
152+
SFS_CHECK(IsAligned(offset, 4096));
153+
return (void *)syscall(SYS_mmap2, addr, length, prot, flags, fd,
154+
offset / 4096);
134155
#endif
135156
}
136157

compiler-rt/lib/safestack/safestack_util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ inline size_t RoundUpTo(size_t size, size_t boundary) {
3333
return (size + boundary - 1) & ~(boundary - 1);
3434
}
3535

36+
inline constexpr bool IsAligned(size_t a, size_t alignment) {
37+
return (a & (alignment - 1)) == 0;
38+
}
39+
3640
class MutexLock {
3741
public:
3842
explicit MutexLock(pthread_mutex_t &mutex) : mutex_(&mutex) {

0 commit comments

Comments
 (0)