@@ -357,7 +357,6 @@ class HWAddressSanitizer {
357
357
bool instrumentStack (memtag::StackInfo &Info, Value *StackTag, Value *UARTag,
358
358
const DominatorTree &DT, const PostDominatorTree &PDT,
359
359
const LoopInfo &LI);
360
- Value *readRegister (IRBuilder<> &IRB, StringRef Name);
361
360
bool instrumentLandingPads (SmallVectorImpl<Instruction *> &RetVec);
362
361
Value *getNextTagWithCall (IRBuilder<> &IRB);
363
362
Value *getStackBaseTag (IRBuilder<> &IRB);
@@ -373,8 +372,7 @@ class HWAddressSanitizer {
373
372
void instrumentGlobal (GlobalVariable *GV, uint8_t Tag);
374
373
void instrumentGlobals ();
375
374
376
- Value *getPC (IRBuilder<> &IRB);
377
- Value *getFP (IRBuilder<> &IRB);
375
+ Value *getCachedFP (IRBuilder<> &IRB);
378
376
Value *getFrameRecordInfo (IRBuilder<> &IRB);
379
377
380
378
void instrumentPersonalityFunctions ();
@@ -449,7 +447,7 @@ class HWAddressSanitizer {
449
447
450
448
Value *ShadowBase = nullptr ;
451
449
Value *StackBaseTag = nullptr ;
452
- Value *CachedSP = nullptr ;
450
+ Value *CachedFP = nullptr ;
453
451
GlobalValue *ThreadPtrGlobal = nullptr ;
454
452
};
455
453
@@ -1159,10 +1157,10 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
1159
1157
// Extract some entropy from the stack pointer for the tags.
1160
1158
// Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
1161
1159
// between functions).
1162
- Value *StackPointerLong = getFP (IRB);
1160
+ Value *FramePointerLong = getCachedFP (IRB);
1163
1161
Value *StackTag =
1164
- applyTagMask (IRB, IRB.CreateXor (StackPointerLong ,
1165
- IRB.CreateLShr (StackPointerLong , 20 )));
1162
+ applyTagMask (IRB, IRB.CreateXor (FramePointerLong ,
1163
+ IRB.CreateLShr (FramePointerLong , 20 )));
1166
1164
StackTag->setName (" hwasan.stack.base.tag" );
1167
1165
return StackTag;
1168
1166
}
@@ -1176,9 +1174,9 @@ Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
1176
1174
}
1177
1175
1178
1176
Value *HWAddressSanitizer::getUARTag (IRBuilder<> &IRB) {
1179
- Value *StackPointerLong = getFP (IRB);
1177
+ Value *FramePointerLong = getCachedFP (IRB);
1180
1178
Value *UARTag =
1181
- applyTagMask (IRB, IRB.CreateLShr (StackPointerLong , PointerTagShift));
1179
+ applyTagMask (IRB, IRB.CreateLShr (FramePointerLong , PointerTagShift));
1182
1180
1183
1181
UARTag->setName (" hwasan.uar.tag" );
1184
1182
return UARTag;
@@ -1237,41 +1235,25 @@ Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
1237
1235
return nullptr ;
1238
1236
}
1239
1237
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;
1260
1242
}
1261
1243
1262
1244
Value *HWAddressSanitizer::getFrameRecordInfo (IRBuilder<> &IRB) {
1263
1245
// 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);
1266
1248
1267
- // Mix SP and PC.
1249
+ // Mix FP and PC.
1268
1250
// Assumptions:
1269
1251
// 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 );
1275
1257
}
1276
1258
1277
1259
void HWAddressSanitizer::emitPrologue (IRBuilder<> &IRB, bool WithFrameRecord) {
@@ -1356,23 +1338,14 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
1356
1338
}
1357
1339
}
1358
1340
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
-
1368
1341
bool HWAddressSanitizer::instrumentLandingPads (
1369
1342
SmallVectorImpl<Instruction *> &LandingPadVec) {
1370
1343
for (auto *LP : LandingPadVec) {
1371
1344
IRBuilder<> IRB (LP->getNextNonDebugInstruction ());
1372
1345
IRB.CreateCall (
1373
1346
HwasanHandleVfork,
1374
- {readRegister (IRB, (TargetTriple. getArch () == Triple::x86_64) ? " rsp "
1375
- : " sp" )});
1347
+ {memtag::readRegister (
1348
+ IRB, (TargetTriple. getArch () == Triple::x86_64) ? " rsp " : " sp" )});
1376
1349
}
1377
1350
return true ;
1378
1351
}
@@ -1637,7 +1610,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
1637
1610
1638
1611
ShadowBase = nullptr ;
1639
1612
StackBaseTag = nullptr ;
1640
- CachedSP = nullptr ;
1613
+ CachedFP = nullptr ;
1641
1614
}
1642
1615
1643
1616
void HWAddressSanitizer::instrumentGlobal (GlobalVariable *GV, uint8_t Tag) {
0 commit comments