Skip to content

Commit 34d7c53

Browse files
author
Tarcisio Fischer
committed
Fix globals being wrongly tagged after global optimization step
Global tagging for MTE is currently only supported for non-RO data. Most cases are already handled, but some global variables are late marked as RO, and the SanitizerMetadata must be updated.
1 parent 0e63180 commit 34d7c53

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ class GlobalValue : public Constant {
362362
void setSanitizerMetadata(SanitizerMetadata Meta);
363363
void removeSanitizerMetadata();
364364
void setNoSanitizeMetadata();
365+
void disableSanitizerMetadataGlobalTagging();
365366

366367
bool isTagged() const {
367368
return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;

llvm/lib/IR/Globals.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ void GlobalValue::setNoSanitizeMetadata() {
266266
setSanitizerMetadata(Meta);
267267
}
268268

269+
void GlobalValue::disableSanitizerMetadataGlobalTagging() {
270+
if (!isTagged()) {
271+
return;
272+
}
273+
274+
auto MD = getSanitizerMetadata();
275+
MD.Memtag = false;
276+
setSanitizerMetadata(MD);
277+
}
278+
269279
StringRef GlobalObject::getSectionImpl() const {
270280
assert(hasSection());
271281
return getContext().pImpl->GlobalObjectSections[this];

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
15251525
if (GS.Ordering == AtomicOrdering::NotAtomic) {
15261526
assert(!GV->isConstant() && "Expected a non-constant global");
15271527
GV->setConstant(true);
1528+
GV->disableSanitizerMetadataGlobalTagging();
15281529
Changed = true;
15291530
}
15301531

@@ -2257,8 +2258,10 @@ static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,
22572258
<< " stores.\n");
22582259
for (const auto &Pair : NewInitializers)
22592260
Pair.first->setInitializer(Pair.second);
2260-
for (GlobalVariable *GV : Eval.getInvariants())
2261+
for (GlobalVariable *GV : Eval.getInvariants()) {
22612262
GV->setConstant(true);
2263+
GV->disableSanitizerMetadataGlobalTagging();
2264+
}
22622265
}
22632266

22642267
return EvalSuccess;

0 commit comments

Comments
 (0)