Skip to content

Commit 511210d

Browse files
authored
[hwasan] Add hwasan-static-linking option (#154529)
Discarding the `.note.hwasan.globals` section in ldscript causes a linker error, since `hwasan_globals` refers to the discarded section. The issue comes from `hwasan.dummy.global` being associated via metadata with `.note.hwasan.globals`. Add a new `-hwasan-static-linking` option to skip inserting `.note.hwasan.globals` for static binaries, as it is only needed for instrumenting globals from dynamic libraries. In static binaries, the global variables section can be accessed directly via the `__start_hwasan_globals` and `__stop_hwasan_globals` symbols inserted by the linker.
1 parent 2263210 commit 511210d

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ static cl::opt<float>
212212
"OR because of the hot percentile cutoff, if "
213213
"both are supplied."));
214214

215+
static cl::opt<bool> ClStaticLinking(
216+
"hwasan-static-linking",
217+
cl::desc("Don't use .note.hwasan.globals section to instrument globals "
218+
"from loadable libraries. "
219+
"Note: in static binaries, the global variables section can be "
220+
"accessed directly via linker-provided "
221+
"__start_hwasan_globals and __stop_hwasan_globals symbols"),
222+
cl::Hidden, cl::init(false));
223+
215224
STATISTIC(NumTotalFuncs, "Number of total funcs");
216225
STATISTIC(NumInstrumentedFuncs, "Number of instrumented funcs");
217226
STATISTIC(NumNoProfileSummaryFuncs, "Number of funcs without PS");
@@ -335,6 +344,7 @@ class HWAddressSanitizer {
335344
FunctionAnalysisManager &FAM) const;
336345
void initializeModule();
337346
void createHwasanCtorComdat();
347+
void createHwasanNote();
338348

339349
void initializeCallbacks(Module &M);
340350

@@ -533,20 +543,7 @@ void HWAddressSanitizerPass::printPipeline(
533543
OS << '>';
534544
}
535545

536-
void HWAddressSanitizer::createHwasanCtorComdat() {
537-
std::tie(HwasanCtorFunction, std::ignore) =
538-
getOrCreateSanitizerCtorAndInitFunctions(
539-
M, kHwasanModuleCtorName, kHwasanInitName,
540-
/*InitArgTypes=*/{},
541-
/*InitArgs=*/{},
542-
// This callback is invoked when the functions are created the first
543-
// time. Hook them into the global ctors list in that case:
544-
[&](Function *Ctor, FunctionCallee) {
545-
Comdat *CtorComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
546-
Ctor->setComdat(CtorComdat);
547-
appendToGlobalCtors(M, Ctor, 0, Ctor);
548-
});
549-
546+
void HWAddressSanitizer::createHwasanNote() {
550547
// Create a note that contains pointers to the list of global
551548
// descriptors. Adding a note to the output file will cause the linker to
552549
// create a PT_NOTE program header pointing to the note that we can use to
@@ -630,6 +627,29 @@ void HWAddressSanitizer::createHwasanCtorComdat() {
630627
appendToCompilerUsed(M, Dummy);
631628
}
632629

630+
void HWAddressSanitizer::createHwasanCtorComdat() {
631+
std::tie(HwasanCtorFunction, std::ignore) =
632+
getOrCreateSanitizerCtorAndInitFunctions(
633+
M, kHwasanModuleCtorName, kHwasanInitName,
634+
/*InitArgTypes=*/{},
635+
/*InitArgs=*/{},
636+
// This callback is invoked when the functions are created the first
637+
// time. Hook them into the global ctors list in that case:
638+
[&](Function *Ctor, FunctionCallee) {
639+
Comdat *CtorComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
640+
Ctor->setComdat(CtorComdat);
641+
appendToGlobalCtors(M, Ctor, 0, Ctor);
642+
});
643+
644+
// Do not create .note.hwasan.globals for static binaries, as it is only
645+
// needed for instrumenting globals from dynamic libraries. In static
646+
// binaries, the global variables section can be accessed directly via the
647+
// __start_hwasan_globals and __stop_hwasan_globals symbols inserted by the
648+
// linker.
649+
if (!ClStaticLinking)
650+
createHwasanNote();
651+
}
652+
633653
/// Module-level initialization.
634654
///
635655
/// inserts a call to __hwasan_init to the module's constructor list.

llvm/test/Instrumentation/HWAddressSanitizer/globals.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; 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-android29 -hwasan-static-linking=1 | FileCheck --check-prefixes=CHECK29,STATICLINKING %s
23
; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64--linux-android30 | FileCheck --check-prefixes=CHECK,CHECK30,NOALLGLOBALS %s
34
; RUN: opt < %s -S -passes=hwasan -mtriple=riscv64-unknown-elf -hwasan-globals=1 -hwasan-all-globals=1 | FileCheck --check-prefixes=CHECK,CHECK30,ALLGLOBALS %s
45

@@ -24,6 +25,8 @@
2425

2526
; 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
2627

28+
; STATICLINKING-NOT: @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
29+
2730
; CHECK: @hwasan.dummy.global = private constant [0 x i8] zeroinitializer, section "hwasan_globals", comdat($hwasan.module_ctor), !associated [[NOTE:![0-9]+]]
2831

2932
; CHECK30: @four = alias i32, inttoptr (i64 add (i64 ptrtoint (ptr @four.hwasan to i64), i64 -6052837899185946624) to ptr)

0 commit comments

Comments
 (0)