diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp index 2d7a44525c5ff335f0be18dbd0fbca3529a922d0..ebc5726e838db4e56b644b5414363c1fcad89b50 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 fe4f9c18e9a22d4470abc7b41eec0efbc0497134..7ee7883c53de4ab7fed36f509e790272f23801ba 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 b01c74320380231e918010f37475d5667d34be61..46cf80161225f66e11442d9094d67b23818a26cc 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)