Skip to content

Commit afff74e

Browse files
committed
[HWAsan][NewPM] Handle hwasan like other sanitizers
Move it as an EP callback (-O[123]) or in addSanitizersAtO0. This makes it not run in ThinLTO pre-link (like the other sanitizers), so don't check LTO runs in hwasan-new-pm.c. Changing its position also seems to change the generated IR. I think we just need to make sure the pass runs. Reviewed By: leonardchan Differential Revision: https://reviews.llvm.org/D88936
1 parent 97e7fbb commit afff74e

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,16 @@ static void addSanitizersAtO0(ModulePassManager &MPM,
10701070
ASanPass(SanitizerKind::KernelAddress, /*CompileKernel=*/true);
10711071
}
10721072

1073+
if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1074+
bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1075+
MPM.addPass(HWAddressSanitizerPass(
1076+
/*CompileKernel=*/false, Recover));
1077+
}
1078+
if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1079+
MPM.addPass(HWAddressSanitizerPass(
1080+
/*CompileKernel=*/true, /*Recover=*/true));
1081+
}
1082+
10731083
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
10741084
bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
10751085
int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
@@ -1348,6 +1358,28 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
13481358
/*CompileKernel=*/false, Recover, UseAfterScope)));
13491359
});
13501360
}
1361+
1362+
if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1363+
bool Recover =
1364+
CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1365+
PB.registerOptimizerLastEPCallback(
1366+
[Recover](ModulePassManager &MPM,
1367+
PassBuilder::OptimizationLevel Level) {
1368+
MPM.addPass(HWAddressSanitizerPass(
1369+
/*CompileKernel=*/false, Recover));
1370+
});
1371+
}
1372+
if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1373+
bool Recover =
1374+
CodeGenOpts.SanitizeRecover.has(SanitizerKind::KernelHWAddress);
1375+
PB.registerOptimizerLastEPCallback(
1376+
[Recover](ModulePassManager &MPM,
1377+
PassBuilder::OptimizationLevel Level) {
1378+
MPM.addPass(HWAddressSanitizerPass(
1379+
/*CompileKernel=*/true, Recover));
1380+
});
1381+
}
1382+
13511383
if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts))
13521384
PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
13531385
MPM.addPass(GCOVProfilerPass(*Options));
@@ -1384,16 +1416,6 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
13841416
MPM.addPass(ModuleMemProfilerPass());
13851417
}
13861418

1387-
if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1388-
bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1389-
MPM.addPass(HWAddressSanitizerPass(
1390-
/*CompileKernel=*/false, Recover));
1391-
}
1392-
if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1393-
MPM.addPass(HWAddressSanitizerPass(
1394-
/*CompileKernel=*/true, /*Recover=*/true));
1395-
}
1396-
13971419
if (CodeGenOpts.OptimizationLevel == 0) {
13981420
// FIXME: the backends do not handle matrix intrinsics currently. Make
13991421
// sure they are also lowered in O0. A lightweight version of the pass

clang/test/CodeGen/hwasan-new-pm.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
11
// Test that HWASan and KHWASan runs with the new pass manager.
2-
// We run them under different optimizations and LTOs to ensure the IR is still
2+
// We run them under different optimizations to ensure the IR is still
33
// being instrumented properly.
44

5-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT
6-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT
7-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress -flto=thin %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT
8-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s --check-prefixes=CHECK,HWASAN
9-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,HWASAN
10-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress -flto=thin %s | FileCheck %s
5+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s
6+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s
117

12-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT
13-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT
14-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto=thin %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT
15-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s --check-prefixes=CHECK,KHWASAN
16-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,KHWASAN
17-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto=thin %s | FileCheck %s
8+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s
9+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s
1810

1911
int foo(int *a) { return *a; }
2012

2113
// All the cases above mark the function with sanitize_hwaddress.
22-
// CHECK-DAG: sanitize_hwaddress
23-
24-
// Both sanitizers produce %hwasan.shadow without both thinlto and optimizations.
25-
// HWASAN-DAG: %hwasan.shadow
26-
// KHWASAN-DAG: %hwasan.shadow
27-
28-
// Both sanitizers produce __hwasan_tls without both thinlto and optimizations.
29-
// HWASAN-DAG: __hwasan_tls
30-
// KHWASAN-DAG: __hwasan_tls
31-
32-
// For unoptimized cases, both sanitizers produce different load functions.
33-
// HWASAN-NOOPT-DAG: __hwasan_loadN
34-
// KHWASAN-NOOPT-DAG: __hwasan_loadN_noabort
14+
// CHECK: sanitize_hwaddress

0 commit comments

Comments
 (0)