From 5e07b7c53b901f20970100da5e1193e9f9cdcb25 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 6 May 2025 15:27:55 +0800 Subject: [PATCH] [Compiler-RT][HWASAN] HWASAN support emulated-tls options Feature to support emulated-tls when using HWASAN. Signed-off-by: Eric --- compiler-rt/lib/hwasan/hwasan_linux.cpp | 11 +++++++++-- .../lib/sanitizer_common/sanitizer_linux.h | 13 ++++++++++++- .../Instrumentation/HWAddressSanitizer.cpp | 15 ++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp index 2d7a44525c5f..ebc5726e838d 100644 --- a/compiler-rt/lib/hwasan/hwasan_linux.cpp +++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp @@ -50,7 +50,7 @@ // Tested with check-hwasan on x86_64-linux. // HWASAN_WITH_INTERCEPTORS=ON, SANITIZER_ANDROID=ON // Tested with check-hwasan on aarch64-linux-android. -# if !SANITIZER_ANDROID +# if !SANITIZER_ANDROID && !SANITIZER_OHOS SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL uptr __hwasan_tls; # endif @@ -295,11 +295,18 @@ void HwasanTSDInit() {} void HwasanTSDThreadInit() {} # endif +// OHOS_LOCAL begin # if SANITIZER_ANDROID uptr *GetCurrentThreadLongPtr() { return (uptr *)get_android_tls_ptr(); } +# elif SANITIZER_OHOS +// Musl doesn't support libc using TLS variables now, +// so musl puts hwasan_tls on the pthread, this interface can return the +// corresponding address. +uptr *GetCurrentThreadLongPtr() { return (uptr *)get_ohos_tls_ptr(); } # else uptr *GetCurrentThreadLongPtr() { return &__hwasan_tls; } -# endif +# endif // SANITIZER_ANDROID || SANITIZER_OHOS +// OHOS_LOCAL end # if SANITIZER_ANDROID void AndroidTestTlsSlot() { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h index fe4f9c18e9a2..7ee7883c53de 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h @@ -168,16 +168,27 @@ inline void ReleaseMemoryPagesToOSAndZeroFill(uptr beg, uptr end) { #error "Unsupported architecture." #endif +#if SANITIZER_ANDROID // The Android Bionic team has allocated a TLS slot for sanitizers starting // with Q, given that Android currently doesn't support ELF TLS. It is used to // store sanitizer thread specific data. static const int TLS_SLOT_SANITIZER = 6; +#elif SANITIZER_OHOS +static const int TLS_SLOT_SANITIZER = 18; // OHOS_LOCAL +#endif // SANITIZER_ANDROID ALWAYS_INLINE uptr *get_android_tls_ptr() { return reinterpret_cast(&__get_tls()[TLS_SLOT_SANITIZER]); } -#endif // SANITIZER_ANDROID +// OHOS_LOCAL begin +#if SANITIZER_OHOS +ALWAYS_INLINE uptr *get_ohos_tls_ptr() { + return reinterpret_cast(__get_tls() - TLS_SLOT_SANITIZER); +} +#endif // SANITIZER_OHOS +// OHOS_LOCAL end +#endif // SANITIZER_ANDROID || SANITIZER_OHOS } // namespace __sanitizer diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp index b01c74320380..46cf80161225 100644 --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -613,7 +613,9 @@ void HWAddressSanitizer::initializeModule() { instrumentPersonalityFunctions(); } - if (!TargetTriple.isAndroid()) { + // OHOS_LOCAL begin + if (!(TargetTriple.isAndroid() || TargetTriple.isOHOSFamily())) { + // OHOS_LOCAL end Constant *C = M.getOrInsertGlobal("__hwasan_tls", IntptrTy, [&] { auto *GV = new GlobalVariable(M, IntptrTy, /*isConstant=*/false, GlobalValue::ExternalLinkage, nullptr, @@ -1116,15 +1118,22 @@ Value *HWAddressSanitizer::untagPointer(IRBuilder<> &IRB, Value *PtrLong) { Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) { Module *M = IRB.GetInsertBlock()->getParent()->getParent(); - if (TargetTriple.isAArch64() && TargetTriple.isAndroid()) { + // OHOS_LOCAL begin + if (TargetTriple.isAArch64() && + (TargetTriple.isAndroid() || TargetTriple.isOHOSFamily())) { // Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER // in Bionic's libc/private/bionic_tls.h. + // For OHOS, in order to support hwasan with emulated-tls, we are provides + // a fixed TLS slot for sanitizers. It is 144 byte blow the TP. + // hwasan_tls in the pthread struct in Musl's src/internal/pthread_impl.h Function *ThreadPointerFunc = Intrinsic::getDeclaration(M, Intrinsic::thread_pointer); Value *SlotPtr = IRB.CreatePointerCast( IRB.CreateConstGEP1_32(IRB.getInt8Ty(), - IRB.CreateCall(ThreadPointerFunc), 0x30), + IRB.CreateCall(ThreadPointerFunc), + TargetTriple.isAndroid()? 0x30 : -0x90), Ty->getPointerTo(0)); + // OHOS_LOCAL end return SlotPtr; } if (ThreadPtrGlobal) -- Gitee