Skip to content

[NFC] [hwasan] factor get[PC|FP] out of HWASan class #84404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/StackSafetyAnalysis.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/Alignment.h"

namespace llvm {
Expand Down Expand Up @@ -79,6 +80,10 @@ class StackInfoBuilder {
uint64_t getAllocaSizeInBytes(const AllocaInst &AI);
void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align);

Value *readRegister(IRBuilder<> &IRB, StringRef Name);
Value *getFP(IRBuilder<> &IRB);
Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB);

} // namespace memtag
} // namespace llvm

Expand Down
71 changes: 22 additions & 49 deletions llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ class HWAddressSanitizer {
bool instrumentStack(memtag::StackInfo &Info, Value *StackTag, Value *UARTag,
const DominatorTree &DT, const PostDominatorTree &PDT,
const LoopInfo &LI);
Value *readRegister(IRBuilder<> &IRB, StringRef Name);
bool instrumentLandingPads(SmallVectorImpl<Instruction *> &RetVec);
Value *getNextTagWithCall(IRBuilder<> &IRB);
Value *getStackBaseTag(IRBuilder<> &IRB);
Expand All @@ -372,8 +371,7 @@ class HWAddressSanitizer {
void instrumentGlobal(GlobalVariable *GV, uint8_t Tag);
void instrumentGlobals();

Value *getPC(IRBuilder<> &IRB);
Value *getFP(IRBuilder<> &IRB);
Value *getCachedFP(IRBuilder<> &IRB);
Value *getFrameRecordInfo(IRBuilder<> &IRB);

void instrumentPersonalityFunctions();
Expand Down Expand Up @@ -448,7 +446,7 @@ class HWAddressSanitizer {

Value *ShadowBase = nullptr;
Value *StackBaseTag = nullptr;
Value *CachedSP = nullptr;
Value *CachedFP = nullptr;
GlobalValue *ThreadPtrGlobal = nullptr;
};

Expand Down Expand Up @@ -1168,10 +1166,10 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
// Extract some entropy from the stack pointer for the tags.
// Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
// between functions).
Value *StackPointerLong = getFP(IRB);
Value *FramePointerLong = getCachedFP(IRB);
Value *StackTag =
applyTagMask(IRB, IRB.CreateXor(StackPointerLong,
IRB.CreateLShr(StackPointerLong, 20)));
applyTagMask(IRB, IRB.CreateXor(FramePointerLong,
IRB.CreateLShr(FramePointerLong, 20)));
StackTag->setName("hwasan.stack.base.tag");
return StackTag;
}
Expand All @@ -1185,9 +1183,9 @@ Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
}

Value *HWAddressSanitizer::getUARTag(IRBuilder<> &IRB) {
Value *StackPointerLong = getFP(IRB);
Value *FramePointerLong = getCachedFP(IRB);
Value *UARTag =
applyTagMask(IRB, IRB.CreateLShr(StackPointerLong, PointerTagShift));
applyTagMask(IRB, IRB.CreateLShr(FramePointerLong, PointerTagShift));

UARTag->setName("hwasan.uar.tag");
return UARTag;
Expand Down Expand Up @@ -1246,41 +1244,25 @@ Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
return nullptr;
}

Value *HWAddressSanitizer::getPC(IRBuilder<> &IRB) {
if (TargetTriple.getArch() == Triple::aarch64)
return readRegister(IRB, "pc");
return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(), IntptrTy);
}

Value *HWAddressSanitizer::getFP(IRBuilder<> &IRB) {
if (!CachedSP) {
// FIXME: use addressofreturnaddress (but implement it in aarch64 backend
// first).
Function *F = IRB.GetInsertBlock()->getParent();
Module *M = F->getParent();
auto *GetStackPointerFn = Intrinsic::getDeclaration(
M, Intrinsic::frameaddress,
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
CachedSP = IRB.CreatePtrToInt(
IRB.CreateCall(GetStackPointerFn, {Constant::getNullValue(Int32Ty)}),
IntptrTy);
}
return CachedSP;
Value *HWAddressSanitizer::getCachedFP(IRBuilder<> &IRB) {
if (!CachedFP)
CachedFP = memtag::getFP(IRB);
return CachedFP;
}

Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
// Prepare ring buffer data.
Value *PC = getPC(IRB);
Value *SP = getFP(IRB);
Value *PC = memtag::getPC(TargetTriple, IRB);
Value *FP = getCachedFP(IRB);

// Mix SP and PC.
// Mix FP and PC.
// Assumptions:
// PC is 0x0000PPPPPPPPPPPP (48 bits are meaningful, others are zero)
// SP is 0xsssssssssssSSSS0 (4 lower bits are zero)
// We only really need ~20 lower non-zero bits (SSSS), so we mix like this:
// 0xSSSSPPPPPPPPPPPP
SP = IRB.CreateShl(SP, 44);
return IRB.CreateOr(PC, SP);
// FP is 0xfffffffffffFFFF0 (4 lower bits are zero)
// We only really need ~20 lower non-zero bits (FFFF), so we mix like this:
// 0xFFFFPPPPPPPPPPPP
FP = IRB.CreateShl(FP, 44);
return IRB.CreateOr(PC, FP);
}

void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
Expand Down Expand Up @@ -1365,23 +1347,14 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
}
}

Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
Function *ReadRegister =
Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
Value *Args[] = {MetadataAsValue::get(*C, MD)};
return IRB.CreateCall(ReadRegister, Args);
}

bool HWAddressSanitizer::instrumentLandingPads(
SmallVectorImpl<Instruction *> &LandingPadVec) {
for (auto *LP : LandingPadVec) {
IRBuilder<> IRB(LP->getNextNonDebugInstruction());
IRB.CreateCall(
HwasanHandleVfork,
{readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
: "sp")});
{memtag::readRegister(
IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp" : "sp")});
}
return true;
}
Expand Down Expand Up @@ -1642,7 +1615,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,

ShadowBase = nullptr;
StackBaseTag = nullptr;
CachedSP = nullptr;
CachedFP = nullptr;
}

void HWAddressSanitizer::instrumentGlobal(GlobalVariable *GV, uint8_t Tag) {
Expand Down
32 changes: 32 additions & 0 deletions llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "llvm/Analysis/StackSafetyAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"

namespace llvm {
Expand Down Expand Up @@ -236,5 +238,35 @@ void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
Info.AI = NewAI;
}

Value *readRegister(IRBuilder<> &IRB, StringRef Name) {
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
Function *ReadRegister = Intrinsic::getDeclaration(
M, Intrinsic::read_register, IRB.getIntPtrTy(M->getDataLayout()));
MDNode *MD =
MDNode::get(M->getContext(), {MDString::get(M->getContext(), Name)});
Value *Args[] = {MetadataAsValue::get(M->getContext(), MD)};
return IRB.CreateCall(ReadRegister, Args);
}

Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB) {
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
if (TargetTriple.getArch() == Triple::aarch64)
return memtag::readRegister(IRB, "pc");
return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(),
IRB.getIntPtrTy(M->getDataLayout()));
}

Value *getFP(IRBuilder<> &IRB) {
Function *F = IRB.GetInsertBlock()->getParent();
Module *M = F->getParent();
auto *GetStackPointerFn = Intrinsic::getDeclaration(
M, Intrinsic::frameaddress,
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
return IRB.CreatePtrToInt(
IRB.CreateCall(GetStackPointerFn,
{Constant::getNullValue(IRB.getInt32Ty())}),
IRB.getIntPtrTy(M->getDataLayout()));
}

} // namespace memtag
} // namespace llvm