Skip to content

Commit ac23676

Browse files
committed
[hwasan] Add hwasan-all-globals option
hwasan-globals does not instrument globals with custom sections, because existing code may use __start_/__stop_ symbols to iterate over globals in such a way which will cause hwasan assertions. Introduce new hwasan-all-globals option, which instruments all user-defined globals (but not those globals which are generated by the hwasan instrumentation itself), including those with custom sections.
1 parent 7c57b55 commit ac23676

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ static cl::opt<bool> ClGenerateTagsWithCalls(
160160
static cl::opt<bool> ClGlobals("hwasan-globals", cl::desc("Instrument globals"),
161161
cl::Hidden, cl::init(false));
162162

163+
static cl::opt<bool> ClAllGlobals(
164+
"hwasan-all-globals",
165+
cl::desc(
166+
"Instrument globals, even those within user-defined sections. Warning: "
167+
"This may break existing code which walks globals via linker-generated "
168+
"symbols, expects certain globals to be contiguous with each other, or "
169+
"makes other assumptions which are invalidated by HWASan "
170+
"instrumentation."),
171+
cl::Hidden, cl::init(false));
172+
163173
static cl::opt<int> ClMatchAllTag(
164174
"hwasan-match-all-tag",
165175
cl::desc("don't report bad accesses via pointers with this tag"),
@@ -452,6 +462,7 @@ class HWAddressSanitizer {
452462
bool InstrumentWithCalls;
453463
bool InstrumentStack;
454464
bool InstrumentGlobals;
465+
bool InstrumentAllGlobals;
455466
bool DetectUseAfterScope;
456467
bool UsePageAliases;
457468
bool UseMatchAllCallback;
@@ -679,6 +690,7 @@ void HWAddressSanitizer::initializeModule() {
679690

680691
InstrumentGlobals =
681692
!CompileKernel && !UsePageAliases && optOr(ClGlobals, NewRuntime);
693+
InstrumentAllGlobals = ClAllGlobals;
682694

683695
if (!CompileKernel) {
684696
createHwasanCtorComdat();
@@ -1780,11 +1792,21 @@ void HWAddressSanitizer::instrumentGlobals() {
17801792
if (GV.hasCommonLinkage())
17811793
continue;
17821794

1783-
// Globals with custom sections may be used in __start_/__stop_ enumeration,
1784-
// which would be broken both by adding tags and potentially by the extra
1785-
// padding/alignment that we insert.
1786-
if (GV.hasSection())
1787-
continue;
1795+
if (InstrumentAllGlobals) {
1796+
// Avoid adding metadata emitted for the hwasan instrumentation itself.
1797+
// Code which makes assumptions about memory layout of globals between
1798+
// __start_<section>/__end_<section> linker-generated symbols may need
1799+
// manual adaptation.
1800+
auto section = GV.getSection();
1801+
if (section == "hwasan_globals" || section == ".note.hwasan.globals")
1802+
continue;
1803+
} else {
1804+
// Globals with custom sections may be used in __start_/__stop_
1805+
// enumeration, which would be broken both by adding tags and potentially
1806+
// by the extra padding/alignment that we insert.
1807+
if (GV.hasSection())
1808+
continue;
1809+
}
17881810

17891811
Globals.push_back(&GV);
17901812
}

0 commit comments

Comments
 (0)