Skip to content

[safestack] Various 32-bit Linux fixes #99455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions compiler-rt/lib/safestack/safestack_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//
//
// This file implements platform specific parts of SafeStack runtime.
// Don't use equivalent functionality from sanitizer_common to avoid dragging
// a large codebase into security sensitive code.
//
//===----------------------------------------------------------------------===//

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

// Keep in sync with sanitizer_linux.cpp.
//
// Are we using 32-bit or 64-bit Linux syscalls?
// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
// but it still needs to use 64-bit syscalls.
#if SANITIZER_LINUX && \
(defined(__x86_64__) || defined(__powerpc64__) || \
SANITIZER_WORDSIZE == 64 || (defined(__mips__) && _MIPS_SIM == _ABIN32))
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
#else
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
#endif

namespace safestack {

#if SANITIZER_NETBSD
Expand Down Expand Up @@ -117,7 +132,8 @@ inline int TgKill(pid_t pid, ThreadId tid, int sig) {
#elif SANITIZER_FREEBSD
return syscall(SYS_thr_kill2, pid, tid, sig);
#else
return syscall(SYS_tgkill, pid, tid, sig);
// tid is pid_t (int), not ThreadId (uint64_t).
return syscall(SYS_tgkill, pid, (pid_t)tid, sig);
#endif
}

Expand All @@ -129,8 +145,13 @@ inline void *Mmap(void *addr, size_t length, int prot, int flags, int fd,
return (void *)__syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
#elif SANITIZER_SOLARIS
return _REAL64(mmap)(addr, length, prot, flags, fd, offset);
#else
#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
#else
// mmap2 specifies file offset in 4096-byte units.
SFS_CHECK(IsAligned(offset, 4096));
return (void *)syscall(SYS_mmap2, addr, length, prot, flags, fd,
offset / 4096);
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/safestack/safestack_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ inline size_t RoundUpTo(size_t size, size_t boundary) {
return (size + boundary - 1) & ~(boundary - 1);
}

inline constexpr bool IsAligned(size_t a, size_t alignment) {
return (a & (alignment - 1)) == 0;
}

class MutexLock {
public:
explicit MutexLock(pthread_mutex_t &mutex) : mutex_(&mutex) {
Expand Down
Loading