Skip to content

Commit 315d792

Browse files
pkubajnemanjai
authored andcommitted
[PowerPC] Fix sanitizers build on FreeBSD
1. Add correct pc, sp and bp for FreeBSD. 2. Since there's no personality.h header on FreeBSD, move SANITIZER_PPC64V2 case below FREEBSD case. 3. __ppc_get_timebase_freq() is glibc-specific. Add a shim for FreeBSD that does the same.
1 parent e0ff354 commit 315d792

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,12 +2091,19 @@ static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
20912091
*sp = ucontext->uc_mcontext.gregs[REG_UESP];
20922092
# endif
20932093
#elif defined(__powerpc__) || defined(__powerpc64__)
2094+
# if SANITIZER_FREEBSD
2095+
ucontext_t *ucontext = (ucontext_t *)context;
2096+
*pc = ucontext->uc_mcontext.mc_srr0;
2097+
*sp = ucontext->uc_mcontext.mc_frame[1];
2098+
*bp = ucontext->uc_mcontext.mc_frame[31];
2099+
# else
20942100
ucontext_t *ucontext = (ucontext_t*)context;
20952101
*pc = ucontext->uc_mcontext.regs->nip;
20962102
*sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
20972103
// The powerpc{,64}-linux ABIs do not specify r31 as the frame
20982104
// pointer, but GCC always uses r31 when we need a frame pointer.
20992105
*bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
2106+
# endif
21002107
#elif defined(__sparc__)
21012108
#if defined(__arch64__) || defined(__sparcv9)
21022109
#define STACK_BIAS 2047
@@ -2185,17 +2192,6 @@ void CheckASLR() {
21852192
GetArgv()[0]);
21862193
Die();
21872194
}
2188-
#elif SANITIZER_PPC64V2
2189-
// Disable ASLR for Linux PPC64LE.
2190-
int old_personality = personality(0xffffffff);
2191-
if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2192-
VReport(1, "WARNING: Program is being run with address space layout "
2193-
"randomization (ASLR) enabled which prevents the thread and "
2194-
"memory sanitizers from working on powerpc64le.\n"
2195-
"ASLR will be disabled and the program re-executed.\n");
2196-
CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2197-
ReExec();
2198-
}
21992195
#elif SANITIZER_FREEBSD
22002196
int aslr_status;
22012197
if (UNLIKELY(procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status) == -1)) {
@@ -2209,9 +2205,21 @@ void CheckASLR() {
22092205
"and binaries compiled with PIE\n");
22102206
Die();
22112207
}
2212-
#else
2208+
# elif SANITIZER_PPC64V2
2209+
// Disable ASLR for Linux PPC64LE.
2210+
int old_personality = personality(0xffffffff);
2211+
if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2212+
VReport(1,
2213+
"WARNING: Program is being run with address space layout "
2214+
"randomization (ASLR) enabled which prevents the thread and "
2215+
"memory sanitizers from working on powerpc64le.\n"
2216+
"ASLR will be disabled and the program re-executed.\n");
2217+
CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2218+
ReExec();
2219+
}
2220+
# else
22132221
// Do nothing
2214-
#endif
2222+
# endif
22152223
}
22162224

22172225
void CheckMPROTECT() {

compiler-rt/lib/xray/xray_powerpc64.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212

1313
#include <cstdint>
1414
#include <mutex>
15+
#ifdef __linux__
1516
#include <sys/platform/ppc.h>
17+
#elif defined(__FreeBSD__)
18+
#include <sys/types.h>
19+
#include <sys/sysctl.h>
20+
21+
#define __ppc_get_timebase __builtin_ppc_get_timebase
22+
23+
uint64_t __ppc_get_timebase_freq (void)
24+
{
25+
uint64_t tb_freq = 0;
26+
size_t length = sizeof(tb_freq);
27+
sysctlbyname("kern.timecounter.tc.timebase.frequency", &tb_freq, &length, nullptr, 0);
28+
return tb_freq;
29+
}
30+
#endif
1631

1732
#include "xray_defs.h"
1833

0 commit comments

Comments
 (0)