@@ -356,7 +356,6 @@ class HWAddressSanitizer {
356
356
bool instrumentStack (memtag::StackInfo &Info, Value *StackTag, Value *UARTag,
357
357
const DominatorTree &DT, const PostDominatorTree &PDT,
358
358
const LoopInfo &LI);
359
- Value *readRegister (IRBuilder<> &IRB, StringRef Name);
360
359
bool instrumentLandingPads (SmallVectorImpl<Instruction *> &RetVec);
361
360
Value *getNextTagWithCall (IRBuilder<> &IRB);
362
361
Value *getStackBaseTag (IRBuilder<> &IRB);
@@ -372,8 +371,7 @@ class HWAddressSanitizer {
372
371
void instrumentGlobal (GlobalVariable *GV, uint8_t Tag);
373
372
void instrumentGlobals ();
374
373
375
- Value *getPC (IRBuilder<> &IRB);
376
- Value *getFP (IRBuilder<> &IRB);
374
+ Value *getCachedFP (IRBuilder<> &IRB);
377
375
Value *getFrameRecordInfo (IRBuilder<> &IRB);
378
376
379
377
void instrumentPersonalityFunctions ();
@@ -448,7 +446,7 @@ class HWAddressSanitizer {
448
446
449
447
Value *ShadowBase = nullptr ;
450
448
Value *StackBaseTag = nullptr ;
451
- Value *CachedSP = nullptr ;
449
+ Value *CachedFP = nullptr ;
452
450
GlobalValue *ThreadPtrGlobal = nullptr ;
453
451
};
454
452
@@ -1168,10 +1166,10 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
1168
1166
// Extract some entropy from the stack pointer for the tags.
1169
1167
// Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
1170
1168
// between functions).
1171
- Value *StackPointerLong = getFP (IRB);
1169
+ Value *FramePointerLong = getCachedFP (IRB);
1172
1170
Value *StackTag =
1173
- applyTagMask (IRB, IRB.CreateXor (StackPointerLong ,
1174
- IRB.CreateLShr (StackPointerLong , 20 )));
1171
+ applyTagMask (IRB, IRB.CreateXor (FramePointerLong ,
1172
+ IRB.CreateLShr (FramePointerLong , 20 )));
1175
1173
StackTag->setName (" hwasan.stack.base.tag" );
1176
1174
return StackTag;
1177
1175
}
@@ -1185,9 +1183,9 @@ Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
1185
1183
}
1186
1184
1187
1185
Value *HWAddressSanitizer::getUARTag (IRBuilder<> &IRB) {
1188
- Value *StackPointerLong = getFP (IRB);
1186
+ Value *FramePointerLong = getCachedFP (IRB);
1189
1187
Value *UARTag =
1190
- applyTagMask (IRB, IRB.CreateLShr (StackPointerLong , PointerTagShift));
1188
+ applyTagMask (IRB, IRB.CreateLShr (FramePointerLong , PointerTagShift));
1191
1189
1192
1190
UARTag->setName (" hwasan.uar.tag" );
1193
1191
return UARTag;
@@ -1246,41 +1244,25 @@ Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
1246
1244
return nullptr ;
1247
1245
}
1248
1246
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;
1269
1251
}
1270
1252
1271
1253
Value *HWAddressSanitizer::getFrameRecordInfo (IRBuilder<> &IRB) {
1272
1254
// 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);
1275
1257
1276
- // Mix SP and PC.
1258
+ // Mix FP and PC.
1277
1259
// Assumptions:
1278
1260
// 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 );
1284
1266
}
1285
1267
1286
1268
void HWAddressSanitizer::emitPrologue (IRBuilder<> &IRB, bool WithFrameRecord) {
@@ -1365,23 +1347,14 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
1365
1347
}
1366
1348
}
1367
1349
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
-
1377
1350
bool HWAddressSanitizer::instrumentLandingPads (
1378
1351
SmallVectorImpl<Instruction *> &LandingPadVec) {
1379
1352
for (auto *LP : LandingPadVec) {
1380
1353
IRBuilder<> IRB (LP->getNextNonDebugInstruction ());
1381
1354
IRB.CreateCall (
1382
1355
HwasanHandleVfork,
1383
- {readRegister (IRB, (TargetTriple. getArch () == Triple::x86_64) ? " rsp "
1384
- : " sp" )});
1356
+ {memtag::readRegister (
1357
+ IRB, (TargetTriple. getArch () == Triple::x86_64) ? " rsp " : " sp" )});
1385
1358
}
1386
1359
return true ;
1387
1360
}
@@ -1642,7 +1615,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
1642
1615
1643
1616
ShadowBase = nullptr ;
1644
1617
StackBaseTag = nullptr ;
1645
- CachedSP = nullptr ;
1618
+ CachedFP = nullptr ;
1646
1619
}
1647
1620
1648
1621
void HWAddressSanitizer::instrumentGlobal (GlobalVariable *GV, uint8_t Tag) {
0 commit comments