Skip to content

Commit 26e8913

Browse files
authored
[NFC] [hwasan] factor get[PC|FP] out of HWASan class (#84404)
Also be consistent about naming SP / FP. This is to prepare for stack history buffer for memtag-stack
1 parent cda55ad commit 26e8913

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/SmallVector.h"
1818
#include "llvm/Analysis/LoopInfo.h"
1919
#include "llvm/Analysis/StackSafetyAnalysis.h"
20+
#include "llvm/IR/IRBuilder.h"
2021
#include "llvm/Support/Alignment.h"
2122

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

83+
Value *readRegister(IRBuilder<> &IRB, StringRef Name);
84+
Value *getFP(IRBuilder<> &IRB);
85+
Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB);
86+
8287
} // namespace memtag
8388
} // namespace llvm
8489

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ class HWAddressSanitizer {
356356
bool instrumentStack(memtag::StackInfo &Info, Value *StackTag, Value *UARTag,
357357
const DominatorTree &DT, const PostDominatorTree &PDT,
358358
const LoopInfo &LI);
359-
Value *readRegister(IRBuilder<> &IRB, StringRef Name);
360359
bool instrumentLandingPads(SmallVectorImpl<Instruction *> &RetVec);
361360
Value *getNextTagWithCall(IRBuilder<> &IRB);
362361
Value *getStackBaseTag(IRBuilder<> &IRB);
@@ -372,8 +371,7 @@ class HWAddressSanitizer {
372371
void instrumentGlobal(GlobalVariable *GV, uint8_t Tag);
373372
void instrumentGlobals();
374373

375-
Value *getPC(IRBuilder<> &IRB);
376-
Value *getFP(IRBuilder<> &IRB);
374+
Value *getCachedFP(IRBuilder<> &IRB);
377375
Value *getFrameRecordInfo(IRBuilder<> &IRB);
378376

379377
void instrumentPersonalityFunctions();
@@ -448,7 +446,7 @@ class HWAddressSanitizer {
448446

449447
Value *ShadowBase = nullptr;
450448
Value *StackBaseTag = nullptr;
451-
Value *CachedSP = nullptr;
449+
Value *CachedFP = nullptr;
452450
GlobalValue *ThreadPtrGlobal = nullptr;
453451
};
454452

@@ -1168,10 +1166,10 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
11681166
// Extract some entropy from the stack pointer for the tags.
11691167
// Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
11701168
// between functions).
1171-
Value *StackPointerLong = getFP(IRB);
1169+
Value *FramePointerLong = getCachedFP(IRB);
11721170
Value *StackTag =
1173-
applyTagMask(IRB, IRB.CreateXor(StackPointerLong,
1174-
IRB.CreateLShr(StackPointerLong, 20)));
1171+
applyTagMask(IRB, IRB.CreateXor(FramePointerLong,
1172+
IRB.CreateLShr(FramePointerLong, 20)));
11751173
StackTag->setName("hwasan.stack.base.tag");
11761174
return StackTag;
11771175
}
@@ -1185,9 +1183,9 @@ Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
11851183
}
11861184

11871185
Value *HWAddressSanitizer::getUARTag(IRBuilder<> &IRB) {
1188-
Value *StackPointerLong = getFP(IRB);
1186+
Value *FramePointerLong = getCachedFP(IRB);
11891187
Value *UARTag =
1190-
applyTagMask(IRB, IRB.CreateLShr(StackPointerLong, PointerTagShift));
1188+
applyTagMask(IRB, IRB.CreateLShr(FramePointerLong, PointerTagShift));
11911189

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

1249-
Value *HWAddressSanitizer::getPC(IRBuilder<> &IRB) {
1250-
if (TargetTriple.getArch() == Triple::aarch64)
1251-
return readRegister(IRB, "pc");
1252-
return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(), IntptrTy);
1253-
}
1254-
1255-
Value *HWAddressSanitizer::getFP(IRBuilder<> &IRB) {
1256-
if (!CachedSP) {
1257-
// FIXME: use addressofreturnaddress (but implement it in aarch64 backend
1258-
// first).
1259-
Function *F = IRB.GetInsertBlock()->getParent();
1260-
Module *M = F->getParent();
1261-
auto *GetStackPointerFn = Intrinsic::getDeclaration(
1262-
M, Intrinsic::frameaddress,
1263-
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
1264-
CachedSP = IRB.CreatePtrToInt(
1265-
IRB.CreateCall(GetStackPointerFn, {Constant::getNullValue(Int32Ty)}),
1266-
IntptrTy);
1267-
}
1268-
return CachedSP;
1247+
Value *HWAddressSanitizer::getCachedFP(IRBuilder<> &IRB) {
1248+
if (!CachedFP)
1249+
CachedFP = memtag::getFP(IRB);
1250+
return CachedFP;
12691251
}
12701252

12711253
Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
12721254
// Prepare ring buffer data.
1273-
Value *PC = getPC(IRB);
1274-
Value *SP = getFP(IRB);
1255+
Value *PC = memtag::getPC(TargetTriple, IRB);
1256+
Value *FP = getCachedFP(IRB);
12751257

1276-
// Mix SP and PC.
1258+
// Mix FP and PC.
12771259
// Assumptions:
12781260
// PC is 0x0000PPPPPPPPPPPP (48 bits are meaningful, others are zero)
1279-
// SP is 0xsssssssssssSSSS0 (4 lower bits are zero)
1280-
// We only really need ~20 lower non-zero bits (SSSS), so we mix like this:
1281-
// 0xSSSSPPPPPPPPPPPP
1282-
SP = IRB.CreateShl(SP, 44);
1283-
return IRB.CreateOr(PC, SP);
1261+
// FP is 0xfffffffffffFFFF0 (4 lower bits are zero)
1262+
// We only really need ~20 lower non-zero bits (FFFF), so we mix like this:
1263+
// 0xFFFFPPPPPPPPPPPP
1264+
FP = IRB.CreateShl(FP, 44);
1265+
return IRB.CreateOr(PC, FP);
12841266
}
12851267

12861268
void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
@@ -1365,23 +1347,14 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
13651347
}
13661348
}
13671349

1368-
Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
1369-
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1370-
Function *ReadRegister =
1371-
Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
1372-
MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
1373-
Value *Args[] = {MetadataAsValue::get(*C, MD)};
1374-
return IRB.CreateCall(ReadRegister, Args);
1375-
}
1376-
13771350
bool HWAddressSanitizer::instrumentLandingPads(
13781351
SmallVectorImpl<Instruction *> &LandingPadVec) {
13791352
for (auto *LP : LandingPadVec) {
13801353
IRBuilder<> IRB(LP->getNextNonDebugInstruction());
13811354
IRB.CreateCall(
13821355
HwasanHandleVfork,
1383-
{readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
1384-
: "sp")});
1356+
{memtag::readRegister(
1357+
IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp" : "sp")});
13851358
}
13861359
return true;
13871360
}
@@ -1642,7 +1615,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
16421615

16431616
ShadowBase = nullptr;
16441617
StackBaseTag = nullptr;
1645-
CachedSP = nullptr;
1618+
CachedFP = nullptr;
16461619
}
16471620

16481621
void HWAddressSanitizer::instrumentGlobal(GlobalVariable *GV, uint8_t Tag) {

llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "llvm/Analysis/StackSafetyAnalysis.h"
1919
#include "llvm/Analysis/ValueTracking.h"
2020
#include "llvm/IR/BasicBlock.h"
21+
#include "llvm/IR/IRBuilder.h"
2122
#include "llvm/IR/IntrinsicInst.h"
23+
#include "llvm/TargetParser/Triple.h"
2224
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
2325

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

241+
Value *readRegister(IRBuilder<> &IRB, StringRef Name) {
242+
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
243+
Function *ReadRegister = Intrinsic::getDeclaration(
244+
M, Intrinsic::read_register, IRB.getIntPtrTy(M->getDataLayout()));
245+
MDNode *MD =
246+
MDNode::get(M->getContext(), {MDString::get(M->getContext(), Name)});
247+
Value *Args[] = {MetadataAsValue::get(M->getContext(), MD)};
248+
return IRB.CreateCall(ReadRegister, Args);
249+
}
250+
251+
Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB) {
252+
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
253+
if (TargetTriple.getArch() == Triple::aarch64)
254+
return memtag::readRegister(IRB, "pc");
255+
return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(),
256+
IRB.getIntPtrTy(M->getDataLayout()));
257+
}
258+
259+
Value *getFP(IRBuilder<> &IRB) {
260+
Function *F = IRB.GetInsertBlock()->getParent();
261+
Module *M = F->getParent();
262+
auto *GetStackPointerFn = Intrinsic::getDeclaration(
263+
M, Intrinsic::frameaddress,
264+
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
265+
return IRB.CreatePtrToInt(
266+
IRB.CreateCall(GetStackPointerFn,
267+
{Constant::getNullValue(IRB.getInt32Ty())}),
268+
IRB.getIntPtrTy(M->getDataLayout()));
269+
}
270+
239271
} // namespace memtag
240272
} // namespace llvm

0 commit comments

Comments
 (0)