Skip to content

Commit 861b2a2

Browse files
melvertstellar
authored andcommitted
[KernelAddressSanitizer] Fix globals exclusion for indirect aliases
GlobalAlias::getAliasee() may not always point directly to a GlobalVariable. In such cases, try to find the canonical GlobalVariable that the alias refers to. Link: ClangBuiltLinux/linux#1208 Reviewed By: dvyukov, nickdesaulniers Differential Revision: https://reviews.llvm.org/D92846 (cherry picked from commit c28b18a)
1 parent 8511a8d commit 861b2a2

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

clang/test/CodeGen/asan-globals-alias.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
// RUN: %clang_cc1 -triple x86_64-linux -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,ASAN
2+
// RUN: %clang_cc1 -triple x86_64-linux -O2 -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,ASAN
23
// RUN: %clang_cc1 -triple x86_64-linux -fsanitize=kernel-address -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,KASAN
4+
// RUN: %clang_cc1 -triple x86_64-linux -O2 -fsanitize=kernel-address -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,KASAN
35
//
46
// Not all platforms support aliases - test for Linux only.
57

6-
int global; // to generate ctor for at least 1 global
7-
int aliased_global; // KASAN - ignore globals prefixed by aliases with __-prefix (below)
8-
extern int __attribute__((alias("aliased_global"))) __global_alias; // KASAN - aliased_global ignored
8+
int global; // generate ctor for at least 1 global
9+
int aliased_global; // KASAN ignored
10+
extern int __attribute__((alias("aliased_global"))) __global_alias;
11+
12+
// Recursive alias:
13+
int aliased_global_2; // KASAN ignored
14+
extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
15+
extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
16+
17+
// Potential indirect alias:
18+
struct input_device_id {
19+
unsigned long keybit[24];
20+
unsigned long driver_info;
21+
};
22+
struct input_device_id joydev_ids[] = { { {1}, 1234 } }; // KASAN ignored
23+
extern struct input_device_id __attribute__((alias("joydev_ids"))) __mod_joydev_ids_device_table;
924

1025
// ASAN: @aliased_global{{.*}} global { i32, [60 x i8] }{{.*}}, align 32
26+
// ASAN: @aliased_global_2{{.*}} global { i32, [60 x i8] }{{.*}}, align 32
27+
// ASAN: @joydev_ids{{.*}} global { {{.*}}[56 x i8] zeroinitializer }, align 32
1128
// KASAN: @aliased_global{{.*}} global i32
29+
// KASAN: @aliased_global_2{{.*}} global i32
30+
// KASAN: @joydev_ids{{.*}} global [1 x {{.*}}i64 1234 }], align 16
31+
32+
// Check the aliases exist:
33+
// CHECK: @__global_alias = alias
34+
// CHECK: @global_alias_2 = alias
35+
// CHECK: @__global_alias_2_alias = alias
36+
// CHECK: @__mod_joydev_ids_device_table = alias
1237

1338
// CHECK-LABEL: define internal void @asan.module_ctor
14-
// ASAN: call void @__asan_register_globals({{.*}}, i{{32|64}} 2)
39+
// ASAN: call void @__asan_register_globals({{.*}}, i{{32|64}} 4)
1540
// KASAN: call void @__asan_register_globals({{.*}}, i{{32|64}} 1)
1641
// CHECK-NEXT: ret void
1742

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ class ModuleAddressSanitizer {
792792
StringRef InternalSuffix);
793793
Instruction *CreateAsanModuleDtor(Module &M);
794794

795-
bool canInstrumentAliasedGlobal(const GlobalAlias &GA) const;
795+
const GlobalVariable *getExcludedAliasedGlobal(const GlobalAlias &GA) const;
796796
bool shouldInstrumentGlobal(GlobalVariable *G) const;
797797
bool ShouldUseMachOGlobalsSection() const;
798798
StringRef getGlobalMetadataSection() const;
@@ -1784,20 +1784,22 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
17841784
}
17851785
}
17861786

1787-
bool ModuleAddressSanitizer::canInstrumentAliasedGlobal(
1788-
const GlobalAlias &GA) const {
1787+
const GlobalVariable *
1788+
ModuleAddressSanitizer::getExcludedAliasedGlobal(const GlobalAlias &GA) const {
17891789
// In case this function should be expanded to include rules that do not just
17901790
// apply when CompileKernel is true, either guard all existing rules with an
17911791
// 'if (CompileKernel) { ... }' or be absolutely sure that all these rules
17921792
// should also apply to user space.
17931793
assert(CompileKernel && "Only expecting to be called when compiling kernel");
17941794

1795+
const Constant *C = GA.getAliasee();
1796+
17951797
// When compiling the kernel, globals that are aliased by symbols prefixed
17961798
// by "__" are special and cannot be padded with a redzone.
17971799
if (GA.getName().startswith("__"))
1798-
return false;
1800+
return dyn_cast<GlobalVariable>(C->stripPointerCastsAndAliases());
17991801

1800-
return true;
1802+
return nullptr;
18011803
}
18021804

18031805
bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
@@ -2256,14 +2258,12 @@ bool ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
22562258
*CtorComdat = false;
22572259

22582260
// Build set of globals that are aliased by some GA, where
2259-
// canInstrumentAliasedGlobal(GA) returns false.
2261+
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
22602262
SmallPtrSet<const GlobalVariable *, 16> AliasedGlobalExclusions;
22612263
if (CompileKernel) {
22622264
for (auto &GA : M.aliases()) {
2263-
if (const auto *GV = dyn_cast<GlobalVariable>(GA.getAliasee())) {
2264-
if (!canInstrumentAliasedGlobal(GA))
2265-
AliasedGlobalExclusions.insert(GV);
2266-
}
2265+
if (const GlobalVariable *GV = getExcludedAliasedGlobal(GA))
2266+
AliasedGlobalExclusions.insert(GV);
22672267
}
22682268
}
22692269

0 commit comments

Comments
 (0)