Skip to content

Commit e28d3e9

Browse files
committed
[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 35bf8e7 commit e28d3e9

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 {
@@ -80,6 +81,10 @@ uint64_t getAllocaSizeInBytes(const AllocaInst &AI);
8081
void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align);
8182
bool isLifetimeIntrinsic(Value *V);
8283

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

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

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

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

380378
void instrumentPersonalityFunctions();
@@ -449,7 +447,7 @@ class HWAddressSanitizer {
449447

450448
Value *ShadowBase = nullptr;
451449
Value *StackBaseTag = nullptr;
452-
Value *CachedSP = nullptr;
450+
Value *CachedFP = nullptr;
453451
GlobalValue *ThreadPtrGlobal = nullptr;
454452
};
455453

@@ -1159,10 +1157,10 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
11591157
// Extract some entropy from the stack pointer for the tags.
11601158
// Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
11611159
// between functions).
1162-
Value *StackPointerLong = getFP(IRB);
1160+
Value *FramePointerLong = getCachedFP(IRB);
11631161
Value *StackTag =
1164-
applyTagMask(IRB, IRB.CreateXor(StackPointerLong,
1165-
IRB.CreateLShr(StackPointerLong, 20)));
1162+
applyTagMask(IRB, IRB.CreateXor(FramePointerLong,
1163+
IRB.CreateLShr(FramePointerLong, 20)));
11661164
StackTag->setName("hwasan.stack.base.tag");
11671165
return StackTag;
11681166
}
@@ -1176,9 +1174,9 @@ Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
11761174
}
11771175

11781176
Value *HWAddressSanitizer::getUARTag(IRBuilder<> &IRB) {
1179-
Value *StackPointerLong = getFP(IRB);
1177+
Value *FramePointerLong = getCachedFP(IRB);
11801178
Value *UARTag =
1181-
applyTagMask(IRB, IRB.CreateLShr(StackPointerLong, PointerTagShift));
1179+
applyTagMask(IRB, IRB.CreateLShr(FramePointerLong, PointerTagShift));
11821180

11831181
UARTag->setName("hwasan.uar.tag");
11841182
return UARTag;
@@ -1237,41 +1235,25 @@ Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
12371235
return nullptr;
12381236
}
12391237

1240-
Value *HWAddressSanitizer::getPC(IRBuilder<> &IRB) {
1241-
if (TargetTriple.getArch() == Triple::aarch64)
1242-
return readRegister(IRB, "pc");
1243-
return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(), IntptrTy);
1244-
}
1245-
1246-
Value *HWAddressSanitizer::getFP(IRBuilder<> &IRB) {
1247-
if (!CachedSP) {
1248-
// FIXME: use addressofreturnaddress (but implement it in aarch64 backend
1249-
// first).
1250-
Function *F = IRB.GetInsertBlock()->getParent();
1251-
Module *M = F->getParent();
1252-
auto *GetStackPointerFn = Intrinsic::getDeclaration(
1253-
M, Intrinsic::frameaddress,
1254-
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
1255-
CachedSP = IRB.CreatePtrToInt(
1256-
IRB.CreateCall(GetStackPointerFn, {Constant::getNullValue(Int32Ty)}),
1257-
IntptrTy);
1258-
}
1259-
return CachedSP;
1238+
Value *HWAddressSanitizer::getCachedFP(IRBuilder<> &IRB) {
1239+
if (!CachedFP)
1240+
CachedFP = memtag::getFP(IRB);
1241+
return CachedFP;
12601242
}
12611243

12621244
Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
12631245
// Prepare ring buffer data.
1264-
Value *PC = getPC(IRB);
1265-
Value *SP = getFP(IRB);
1246+
Value *PC = memtag::getPC(TargetTriple, IRB);
1247+
Value *FP = getCachedFP(IRB);
12661248

1267-
// Mix SP and PC.
1249+
// Mix FP and PC.
12681250
// Assumptions:
12691251
// PC is 0x0000PPPPPPPPPPPP (48 bits are meaningful, others are zero)
1270-
// SP is 0xsssssssssssSSSS0 (4 lower bits are zero)
1271-
// We only really need ~20 lower non-zero bits (SSSS), so we mix like this:
1272-
// 0xSSSSPPPPPPPPPPPP
1273-
SP = IRB.CreateShl(SP, 44);
1274-
return IRB.CreateOr(PC, SP);
1252+
// FP is 0xfffffffffffFFFF0 (4 lower bits are zero)
1253+
// We only really need ~20 lower non-zero bits (FFFF), so we mix like this:
1254+
// 0xFFFFPPPPPPPPPPPP
1255+
FP = IRB.CreateShl(FP, 44);
1256+
return IRB.CreateOr(PC, FP);
12751257
}
12761258

12771259
void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
@@ -1356,23 +1338,14 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
13561338
}
13571339
}
13581340

1359-
Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
1360-
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1361-
Function *ReadRegister =
1362-
Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
1363-
MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
1364-
Value *Args[] = {MetadataAsValue::get(*C, MD)};
1365-
return IRB.CreateCall(ReadRegister, Args);
1366-
}
1367-
13681341
bool HWAddressSanitizer::instrumentLandingPads(
13691342
SmallVectorImpl<Instruction *> &LandingPadVec) {
13701343
for (auto *LP : LandingPadVec) {
13711344
IRBuilder<> IRB(LP->getNextNonDebugInstruction());
13721345
IRB.CreateCall(
13731346
HwasanHandleVfork,
1374-
{readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
1375-
: "sp")});
1347+
{memtag::readRegister(
1348+
IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp" : "sp")});
13761349
}
13771350
return true;
13781351
}
@@ -1637,7 +1610,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
16371610

16381611
ShadowBase = nullptr;
16391612
StackBaseTag = nullptr;
1640-
CachedSP = nullptr;
1613+
CachedFP = nullptr;
16411614
}
16421615

16431616
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 {
@@ -241,5 +243,35 @@ bool isLifetimeIntrinsic(Value *V) {
241243
return II && II->isLifetimeStartOrEnd();
242244
}
243245

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

0 commit comments

Comments
 (0)