Skip to content

Commit

Permalink
[tsan] Derive the unmangled SP in longjmp with xor key on loongarch64
Browse files Browse the repository at this point in the history
Introducing xor key to derive unmangled sp is here to follow the way
that the glibc adds support for pointer mangling on loongarch in commit
1c9bc1b6e50293a1b7037a7bfbf835868a55baed.

Reviewed By: SixWeining, wangleiat, xen0n

Differential Revision: https://reviews.llvm.org/D146716
  • Loading branch information
Ami-zhang authored and wangleiat committed Mar 28, 2023
1 parent 488185c commit cfdcdf0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ extern "C" void *__libc_stack_end;
void *__libc_stack_end = 0;
#endif

#if SANITIZER_LINUX && defined(__aarch64__) && !SANITIZER_GO
#if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) && \
!SANITIZER_GO
# define INIT_LONGJMP_XOR_KEY 1
#else
# define INIT_LONGJMP_XOR_KEY 0
Expand Down Expand Up @@ -314,7 +315,7 @@ void InitializePlatform() {
}

#endif
#if SANITIZER_LINUX && defined(__aarch64__)
#if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64))
// Initialize the xor key used in {sig}{set,long}jump.
InitializeLongjmpXorKey();
#endif
Expand Down Expand Up @@ -387,8 +388,8 @@ static uptr UnmangleLongJmpSp(uptr mangled_sp) {
# else
return mangled_sp;
# endif
#elif defined(__loongarch__)
return mangled_sp;
#elif defined(__loongarch_lp64)
return mangled_sp ^ longjmp_xor_key;
#elif defined(__powerpc64__)
// Reverse of:
// ld r4, -28696(r13)
Expand Down Expand Up @@ -452,7 +453,11 @@ static void InitializeLongjmpXorKey() {

// 2. Retrieve vanilla/mangled SP.
uptr sp;
#ifdef __loongarch__
asm("move %0, $sp" : "=r" (sp));
#else
asm("mov %0, sp" : "=r" (sp));
#endif
uptr mangled_sp = ((uptr *)&env)[LONG_JMP_SP_ENV_SLOT];

// 3. xor SPs to obtain key.
Expand Down

0 comments on commit cfdcdf0

Please sign in to comment.