Skip to content

Commit 022dc6b

Browse files
authored
[NFC] [HWASan] factor out debug record annotation (#90252)
This will also be used by stack MTE
1 parent ced8497 commit 022dc6b

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Value *getFP(IRBuilder<> &IRB);
8686
Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB);
8787
Value *getAndroidSlotPtr(IRBuilder<> &IRB, int Slot);
8888

89+
void annotateDebugRecords(AllocaInfo &Info, unsigned int Tag);
90+
8991
} // namespace memtag
9092
} // namespace llvm
9193

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

+1-30
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,6 @@ bool HWAddressSanitizer::instrumentLandingPads(
13851385
return true;
13861386
}
13871387

1388-
static DbgAssignIntrinsic *DynCastToDbgAssign(DbgVariableIntrinsic *DVI) {
1389-
return dyn_cast<DbgAssignIntrinsic>(DVI);
1390-
}
1391-
1392-
static DbgVariableRecord *DynCastToDbgAssign(DbgVariableRecord *DVR) {
1393-
return DVR->isDbgAssign() ? DVR : nullptr;
1394-
}
1395-
13961388
bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
13971389
Value *StackTag, Value *UARTag,
13981390
const DominatorTree &DT,
@@ -1448,28 +1440,7 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
14481440
!memtag::isLifetimeIntrinsic(User);
14491441
});
14501442

1451-
// Helper utility for adding DW_OP_LLVM_tag_offset to debug-info records,
1452-
// abstracted over whether they're intrinsic-stored or DbgVariableRecord
1453-
// stored.
1454-
auto AnnotateDbgRecord = [&](auto *DPtr) {
1455-
// Prepend "tag_offset, N" to the dwarf expression.
1456-
// Tag offset logically applies to the alloca pointer, and it makes sense
1457-
// to put it at the beginning of the expression.
1458-
SmallVector<uint64_t, 8> NewOps = {dwarf::DW_OP_LLVM_tag_offset,
1459-
retagMask(N)};
1460-
for (size_t LocNo = 0; LocNo < DPtr->getNumVariableLocationOps(); ++LocNo)
1461-
if (DPtr->getVariableLocationOp(LocNo) == AI)
1462-
DPtr->setExpression(DIExpression::appendOpsToArg(
1463-
DPtr->getExpression(), NewOps, LocNo));
1464-
if (auto *DAI = DynCastToDbgAssign(DPtr)) {
1465-
if (DAI->getAddress() == AI)
1466-
DAI->setAddressExpression(DIExpression::prependOpcodes(
1467-
DAI->getAddressExpression(), NewOps));
1468-
}
1469-
};
1470-
1471-
llvm::for_each(Info.DbgVariableIntrinsics, AnnotateDbgRecord);
1472-
llvm::for_each(Info.DbgVariableRecords, AnnotateDbgRecord);
1443+
memtag::annotateDebugRecords(Info, retagMask(N));
14731444

14741445
auto TagEnd = [&](Instruction *Node) {
14751446
IRB.SetInsertPoint(Node);

llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/PostDominators.h"
1818
#include "llvm/Analysis/StackSafetyAnalysis.h"
1919
#include "llvm/Analysis/ValueTracking.h"
20+
#include "llvm/BinaryFormat/Dwarf.h"
2021
#include "llvm/IR/BasicBlock.h"
2122
#include "llvm/IR/IRBuilder.h"
2223
#include "llvm/IR/IntrinsicInst.h"
@@ -283,5 +284,37 @@ Value *getAndroidSlotPtr(IRBuilder<> &IRB, int Slot) {
283284
IRB.CreateCall(ThreadPointerFunc), 8 * Slot);
284285
}
285286

287+
static DbgAssignIntrinsic *DynCastToDbgAssign(DbgVariableIntrinsic *DVI) {
288+
return dyn_cast<DbgAssignIntrinsic>(DVI);
289+
}
290+
291+
static DbgVariableRecord *DynCastToDbgAssign(DbgVariableRecord *DVR) {
292+
return DVR->isDbgAssign() ? DVR : nullptr;
293+
}
294+
295+
void annotateDebugRecords(AllocaInfo &Info, unsigned int Tag) {
296+
// Helper utility for adding DW_OP_LLVM_tag_offset to debug-info records,
297+
// abstracted over whether they're intrinsic-stored or DbgVariableRecord
298+
// stored.
299+
auto AnnotateDbgRecord = [&](auto *DPtr) {
300+
// Prepend "tag_offset, N" to the dwarf expression.
301+
// Tag offset logically applies to the alloca pointer, and it makes sense
302+
// to put it at the beginning of the expression.
303+
SmallVector<uint64_t, 8> NewOps = {dwarf::DW_OP_LLVM_tag_offset, Tag};
304+
for (size_t LocNo = 0; LocNo < DPtr->getNumVariableLocationOps(); ++LocNo)
305+
if (DPtr->getVariableLocationOp(LocNo) == Info.AI)
306+
DPtr->setExpression(
307+
DIExpression::appendOpsToArg(DPtr->getExpression(), NewOps, LocNo));
308+
if (auto *DAI = DynCastToDbgAssign(DPtr)) {
309+
if (DAI->getAddress() == Info.AI)
310+
DAI->setAddressExpression(
311+
DIExpression::prependOpcodes(DAI->getAddressExpression(), NewOps));
312+
}
313+
};
314+
315+
llvm::for_each(Info.DbgVariableIntrinsics, AnnotateDbgRecord);
316+
llvm::for_each(Info.DbgVariableRecords, AnnotateDbgRecord);
317+
}
318+
286319
} // namespace memtag
287320
} // namespace llvm

0 commit comments

Comments
 (0)