Skip to content

Commit 93b4749

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 93b4749

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ 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("Instrument globals, even those within user-defined sections"),
166+
cl::Hidden, cl::init(false));
167+
163168
static cl::opt<int> ClMatchAllTag(
164169
"hwasan-match-all-tag",
165170
cl::desc("don't report bad accesses via pointers with this tag"),
@@ -452,6 +457,7 @@ class HWAddressSanitizer {
452457
bool InstrumentWithCalls;
453458
bool InstrumentStack;
454459
bool InstrumentGlobals;
460+
bool InstrumentAllGlobals;
455461
bool DetectUseAfterScope;
456462
bool UsePageAliases;
457463
bool UseMatchAllCallback;
@@ -679,6 +685,7 @@ void HWAddressSanitizer::initializeModule() {
679685

680686
InstrumentGlobals =
681687
!CompileKernel && !UsePageAliases && optOr(ClGlobals, NewRuntime);
688+
InstrumentAllGlobals = InstrumentGlobals && ClAllGlobals;
682689

683690
if (!CompileKernel) {
684691
createHwasanCtorComdat();
@@ -1780,11 +1787,21 @@ void HWAddressSanitizer::instrumentGlobals() {
17801787
if (GV.hasCommonLinkage())
17811788
continue;
17821789

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

17891806
Globals.push_back(&GV);
17901807
}

0 commit comments

Comments
 (0)