Skip to content

Commit 3a96227

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 923566a commit 3a96227

File tree

7 files changed

+46
-167
lines changed

7 files changed

+46
-167
lines changed

clang/lib/CodeGen/SanitizerMetadata.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ static SanitizerMask expandKernelSanitizerMasks(SanitizerMask Mask) {
3434
return Mask;
3535
}
3636

37+
static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
38+
// For now, don't instrument constant data, as it'll be in .rodata anyway. It
39+
// may be worth instrumenting these in future to stop them from being used as
40+
// gadgets.
41+
if (G.getName().starts_with("llvm.") || G.isThreadLocal() || G.isConstant())
42+
return false;
43+
44+
// Globals can be placed implicitly or explicitly in sections. There's two
45+
// different types of globals that meet this criteria that cause problems:
46+
// 1. Function pointers that are going into various init arrays (either
47+
// explicitly through `__attribute__((section(<foo>)))` or implicitly
48+
// through `__attribute__((constructor)))`, such as ".(pre)init(_array)",
49+
// ".fini(_array)", ".ctors", and ".dtors". These function pointers end up
50+
// overaligned and overpadded, making iterating over them problematic, and
51+
// each function pointer is individually tagged (so the iteration over
52+
// them causes SIGSEGV/MTE[AS]ERR).
53+
// 2. Global variables put into an explicit section, where the section's name
54+
// is a valid C-style identifier. The linker emits a `__start_<name>` and
55+
// `__stop_<name>` symbol for the section, so that you can iterate over
56+
// globals within this section. Unfortunately, again, these globals would
57+
// be tagged and so iteration causes SIGSEGV/MTE[AS]ERR.
58+
//
59+
// To mitigate both these cases, and because specifying a section is rare
60+
// outside of these two cases, disable MTE protection for globals in any
61+
// section.
62+
if (G.hasSection())
63+
return false;
64+
65+
return true;
66+
}
67+
3768
void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV,
3869
SourceLocation Loc, StringRef Name,
3970
QualType Ty,
@@ -60,11 +91,15 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV,
6091
Meta.NoHWAddress |= CGM.isInNoSanitizeList(
6192
FsanitizeArgument.Mask & SanitizerKind::HWAddress, GV, Loc, Ty);
6293

63-
Meta.Memtag |=
64-
static_cast<bool>(FsanitizeArgument.Mask & SanitizerKind::MemtagGlobals);
65-
Meta.Memtag &= !NoSanitizeAttrSet.hasOneOf(SanitizerKind::MemTag);
66-
Meta.Memtag &= !CGM.isInNoSanitizeList(
67-
FsanitizeArgument.Mask & SanitizerKind::MemTag, GV, Loc, Ty);
94+
if (shouldTagGlobal(*GV)) {
95+
Meta.Memtag |=
96+
static_cast<bool>(FsanitizeArgument.Mask & SanitizerKind::MemtagGlobals);
97+
Meta.Memtag &= !NoSanitizeAttrSet.hasOneOf(SanitizerKind::MemTag);
98+
Meta.Memtag &= !CGM.isInNoSanitizeList(
99+
FsanitizeArgument.Mask & SanitizerKind::MemTag, GV, Loc, Ty);
100+
} else {
101+
Meta.Memtag = false;
102+
}
68103

69104
Meta.IsDynInit = IsDynInit && !Meta.NoAddress &&
70105
FsanitizeArgument.has(SanitizerKind::Address) &&

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,16 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
764764

765765
const DataLayout &DL = GV->getDataLayout();
766766
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
767+
if (GV->isTagged()) Size = alignTo(Size, 16);
767768

768769
// If the alignment is specified, we *must* obey it. Overaligning a global
769770
// with a specified alignment is a prompt way to break globals emitted to
770771
// sections and expected to be contiguous (e.g. ObjC metadata).
771-
const Align Alignment = getGVAlignment(GV, DL);
772+
Align Alignment = getGVAlignment(GV, DL);
773+
if (GV->isTagged() && Alignment < 16) {
774+
assert(!GV->hasSection());
775+
Alignment = Align(16);
776+
}
772777

773778
for (auto &Handler : DebugHandlers)
774779
Handler->setSymbolSize(GVSym, Size);

llvm/lib/Target/AArch64/AArch64.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ FunctionPass *createAArch64PostLegalizerLowering();
7272
FunctionPass *createAArch64PostSelectOptimize();
7373
FunctionPass *createAArch64StackTaggingPass(bool IsOptNone);
7474
FunctionPass *createAArch64StackTaggingPreRAPass();
75-
ModulePass *createAArch64GlobalsTaggingPass();
7675
ModulePass *createAArch64Arm64ECCallLoweringPass();
7776

7877
void initializeAArch64A53Fix835769Pass(PassRegistry&);
@@ -89,7 +88,6 @@ void initializeAArch64ConditionalComparesPass(PassRegistry &);
8988
void initializeAArch64DAGToDAGISelLegacyPass(PassRegistry &);
9089
void initializeAArch64DeadRegisterDefinitionsPass(PassRegistry&);
9190
void initializeAArch64ExpandPseudoPass(PassRegistry &);
92-
void initializeAArch64GlobalsTaggingPass(PassRegistry &);
9391
void initializeAArch64LoadStoreOptPass(PassRegistry&);
9492
void initializeAArch64LowerHomogeneousPrologEpilogPass(PassRegistry &);
9593
void initializeAArch64MIPeepholeOptPass(PassRegistry &);

llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp

Lines changed: 0 additions & 155 deletions
This file was deleted.

llvm/lib/Target/AArch64/AArch64TargetMachine.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Target() {
269269
initializeAArch64StackTaggingPreRAPass(*PR);
270270
initializeAArch64LowerHomogeneousPrologEpilogPass(*PR);
271271
initializeAArch64DAGToDAGISelLegacyPass(*PR);
272-
initializeAArch64GlobalsTaggingPass(*PR);
273272
}
274273

275274
//===----------------------------------------------------------------------===//
@@ -632,7 +631,6 @@ void AArch64PassConfig::addIRPasses() {
632631
if (getOptLevel() == CodeGenOptLevel::Aggressive && EnableSelectOpt)
633632
addPass(createSelectOptimizePass());
634633

635-
addPass(createAArch64GlobalsTaggingPass());
636634
addPass(createAArch64StackTaggingPass(
637635
/*IsOptNone=*/TM->getOptLevel() == CodeGenOptLevel::None));
638636

llvm/lib/Target/AArch64/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ add_llvm_target(AArch64CodeGen
5757
AArch64FastISel.cpp
5858
AArch64A53Fix835769.cpp
5959
AArch64FrameLowering.cpp
60-
AArch64GlobalsTagging.cpp
6160
AArch64CompressJumpTables.cpp
6261
AArch64ConditionOptimizer.cpp
6362
AArch64RedundantCopyElimination.cpp

llvm/utils/gn/secondary/llvm/lib/Target/AArch64/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ static_library("LLVMAArch64CodeGen") {
126126
"AArch64FalkorHWPFFix.cpp",
127127
"AArch64FastISel.cpp",
128128
"AArch64FrameLowering.cpp",
129-
"AArch64GlobalsTagging.cpp",
130129
"AArch64ISelDAGToDAG.cpp",
131130
"AArch64ISelLowering.cpp",
132131
"AArch64InstrInfo.cpp",

0 commit comments

Comments
 (0)