Skip to content

Commit e7522ae

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 a5481e7 commit e7522ae

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 25 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"),
@@ -1780,11 +1790,21 @@ void HWAddressSanitizer::instrumentGlobals() {
17801790
if (GV.hasCommonLinkage())
17811791
continue;
17821792

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

17891809
Globals.push_back(&GV);
17901810
}

llvm/test/Instrumentation/HWAddressSanitizer/globals.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64--linux-android29 | FileCheck --check-prefixes=CHECK,CHECK29 %s
22
; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64--linux-android30 | FileCheck --check-prefixes=CHECK,CHECK30 %s
3+
; RUN: opt < %s -S -passes=hwasan -mtriple=riscv64-unknown-elf -hwasan-globals=1 -hwasan-all-globals=1 | FileCheck --check-prefixes=CHECKALLGLOBALS %s
34

45
; CHECK29: @four = global
56

6-
; CHECK: @specialcaselisted = global i16 2, no_sanitize_hwaddress
7+
; CHECK,CHECKALLGLOBALS: @specialcaselisted = global i16 2, no_sanitize_hwaddress
78
; CHECK: @insection = global i16 2, section "custom"
9+
; CHECKALLGLOBALS: @insection.hwasan = private global { i16, [14 x i8] } { i16 2, [14 x i8] c"\00\00\00\00\00\00\00\00\00\00\00\00\00\AF" }, section "custom", align 16
810
; CHECK: @__start_hwasan_globals = external hidden constant [0 x i8]
911
; CHECK: @__stop_hwasan_globals = external hidden constant [0 x i8]
1012

0 commit comments

Comments
 (0)