Skip to content

Commit b6abcaf

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 01b47eb commit b6abcaf

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 16 additions & 6 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"),
@@ -681,11 +691,11 @@ void HWAddressSanitizer::initializeModule() {
681691
!CompileKernel && !UsePageAliases && optOr(ClGlobals, NewRuntime);
682692

683693
if (!CompileKernel) {
684-
createHwasanCtorComdat();
685-
686694
if (InstrumentGlobals)
687695
instrumentGlobals();
688696

697+
createHwasanCtorComdat();
698+
689699
bool InstrumentPersonalityFunctions =
690700
optOr(ClInstrumentPersonalityFunctions, NewRuntime);
691701
if (InstrumentPersonalityFunctions)
@@ -1780,10 +1790,10 @@ 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())
1793+
// Globals with custom sections may be used in __start_/__stop_
1794+
// enumeration, which would be broken both by adding tags and potentially
1795+
// by the extra padding/alignment that we insert.
1796+
if (GV.hasSection() && !ClAllGlobals)
17871797
continue;
17881798

17891799
Globals.push_back(&GV);

llvm/test/Instrumentation/HWAddressSanitizer/globals.ll

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

45
; CHECK29: @four = global
56

67
; CHECK: @specialcaselisted = global i16 2, no_sanitize_hwaddress
7-
; CHECK: @insection = global i16 2, section "custom"
8-
; CHECK: @__start_hwasan_globals = external hidden constant [0 x i8]
9-
; CHECK: @__stop_hwasan_globals = external hidden constant [0 x i8]
10-
11-
; CHECK: @hwasan.note = private constant { i32, i32, i32, [8 x i8], i32, i32 } { i32 8, i32 8, i32 3, [8 x i8] c"LLVM\00\00\00\00", i32 trunc (i64 sub (i64 ptrtoint (ptr @__start_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @__stop_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32) }, section ".note.hwasan.globals", comdat($hwasan.module_ctor), align 4
12-
13-
; CHECK: @hwasan.dummy.global = private constant [0 x i8] zeroinitializer, section "hwasan_globals", comdat($hwasan.module_ctor), !associated [[NOTE:![0-9]+]]
8+
; NOALLGLOBALS: @insection = global i16 2, section "custom"
149

1510
; CHECK30: @four.hwasan = private global { i32, [12 x i8] } { i32 1, [12 x i8] c"\00\00\00\00\00\00\00\00\00\00\00\AC" }, align 16
1611
; CHECK30: @four.hwasan.descriptor = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (ptr @four.hwasan to i64), i64 ptrtoint (ptr @four.hwasan.descriptor to i64)) to i32), i32 -1409286140 }, section "hwasan_globals", !associated [[FOUR:![0-9]+]]
@@ -22,14 +17,23 @@
2217
; CHECK30: @huge.hwasan.descriptor = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (ptr @huge.hwasan to i64), i64 ptrtoint (ptr @huge.hwasan.descriptor to i64)) to i32), i32 -1358954512 }, section "hwasan_globals", !associated [[HUGE:![0-9]+]]
2318
; CHECK30: @huge.hwasan.descriptor.1 = private constant { i32, i32 } { i32 trunc (i64 add (i64 sub (i64 ptrtoint (ptr @huge.hwasan to i64), i64 ptrtoint (ptr @huge.hwasan.descriptor.1 to i64)), i64 16777200) to i32), i32 -1375731680 }, section "hwasan_globals", !associated [[HUGE]]
2419

20+
; ALLGLOBALS: @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
21+
22+
; CHECK: @__start_hwasan_globals = external hidden constant [0 x i8]
23+
; CHECK: @__stop_hwasan_globals = external hidden constant [0 x i8]
24+
25+
; CHECK: @hwasan.note = private constant { i32, i32, i32, [8 x i8], i32, i32 } { i32 8, i32 8, i32 3, [8 x i8] c"LLVM\00\00\00\00", i32 trunc (i64 sub (i64 ptrtoint (ptr @__start_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @__stop_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32) }, section ".note.hwasan.globals", comdat($hwasan.module_ctor), align 4
26+
27+
; CHECK: @hwasan.dummy.global = private constant [0 x i8] zeroinitializer, section "hwasan_globals", comdat($hwasan.module_ctor), !associated [[NOTE:![0-9]+]]
28+
2529
; CHECK30: @four = alias i32, inttoptr (i64 add (i64 ptrtoint (ptr @four.hwasan to i64), i64 -6052837899185946624) to ptr)
2630
; CHECK30: @sixteen = alias [16 x i8], inttoptr (i64 add (i64 ptrtoint (ptr @sixteen.hwasan to i64), i64 -5980780305148018688) to ptr)
2731
; CHECK30: @huge = alias [16777232 x i8], inttoptr (i64 add (i64 ptrtoint (ptr @huge.hwasan to i64), i64 -5908722711110090752) to ptr)
2832

29-
; CHECK: [[NOTE]] = !{ptr @hwasan.note}
3033
; CHECK30: [[FOUR]] = !{ptr @four.hwasan}
3134
; CHECK30: [[SIXTEEN]] = !{ptr @sixteen.hwasan}
3235
; CHECK30: [[HUGE]] = !{ptr @huge.hwasan}
36+
; CHECK: [[NOTE]] = !{ptr @hwasan.note}
3337

3438
source_filename = "foo"
3539

0 commit comments

Comments
 (0)