Skip to content

Commit

Permalink
[HWASan] [NFC] pull logic to get sanitizer ptr out of hwasan (llvm#86024
Browse files Browse the repository at this point in the history
)

Also some drive by cleanup removing an unnnecessary argument and a
redundant condition.
  • Loading branch information
fmayer authored Mar 21, 2024
1 parent 536cb1f commit 85ccfb5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ bool isLifetimeIntrinsic(Value *V);
Value *readRegister(IRBuilder<> &IRB, StringRef Name);
Value *getFP(IRBuilder<> &IRB);
Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB);
Value *getAndroidSanitizerSlotPtr(IRBuilder<> &IRB);

} // namespace memtag
} // namespace llvm
Expand Down
22 changes: 6 additions & 16 deletions llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class HWAddressSanitizer {
Value *getAllocaTag(IRBuilder<> &IRB, Value *StackTag, unsigned AllocaNo);
Value *getUARTag(IRBuilder<> &IRB);

Value *getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty);
Value *getHwasanThreadSlotPtr(IRBuilder<> &IRB);
Value *applyTagMask(IRBuilder<> &IRB, Value *OldTag);
unsigned retagMask(unsigned AllocaNo);

Expand Down Expand Up @@ -1219,20 +1219,10 @@ Value *HWAddressSanitizer::untagPointer(IRBuilder<> &IRB, Value *PtrLong) {
return UntaggedPtrLong;
}

Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
if (TargetTriple.isAArch64() && TargetTriple.isAndroid()) {
// Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
// in Bionic's libc/private/bionic_tls.h.
Function *ThreadPointerFunc =
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
return IRB.CreateConstGEP1_32(Int8Ty, IRB.CreateCall(ThreadPointerFunc),
0x30);
}
if (ThreadPtrGlobal)
return ThreadPtrGlobal;

return nullptr;
Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB) {
if (TargetTriple.isAArch64() && TargetTriple.isAndroid())
return memtag::getAndroidSanitizerSlotPtr(IRB);
return ThreadPtrGlobal;
}

Value *HWAddressSanitizer::getCachedFP(IRBuilder<> &IRB) {
Expand Down Expand Up @@ -1271,7 +1261,7 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {

auto getThreadLongMaybeUntagged = [&]() {
if (!SlotPtr)
SlotPtr = getHwasanThreadSlotPtr(IRB, IntptrTy);
SlotPtr = getHwasanThreadSlotPtr(IRB);
if (!ThreadLong)
ThreadLong = IRB.CreateLoad(IntptrTy, SlotPtr);
// Extract the address field from ThreadLong. Unnecessary on AArch64 with
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,15 @@ Value *getFP(IRBuilder<> &IRB) {
IRB.getIntPtrTy(M->getDataLayout()));
}

Value *getAndroidSanitizerSlotPtr(IRBuilder<> &IRB) {
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
// Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
// in Bionic's libc/private/bionic_tls.h.
Function *ThreadPointerFunc =
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
return IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
IRB.CreateCall(ThreadPointerFunc), 0x30);
}

} // namespace memtag
} // namespace llvm

0 comments on commit 85ccfb5

Please sign in to comment.