[PATCH] D61709: [NewPM] Port HWASan and Kernel HWASan
This revision was automatically updated to reflect the committed changes. leonardchan marked 2 inline comments as done. Closed by commit rC360707: [NewPM] Port HWASan and Kernel HWASan (authored by leonardchan, committed by ). Changed prior to commit: https://reviews.llvm.org/D61709?vs=198889&id=199511#toc Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61709/new/ https://reviews.llvm.org/D61709 Files: lib/CodeGen/BackendUtil.cpp test/CodeGen/hwasan-new-pm.c Index: test/CodeGen/hwasan-new-pm.c === --- test/CodeGen/hwasan-new-pm.c +++ test/CodeGen/hwasan-new-pm.c @@ -0,0 +1,34 @@ +// Test that HWASan and KHWASan runs with the new pass manager. +// We run them under different optimizations and LTOs to ensure the IR is still +// being instrumented properly. + +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=hwaddress -flto=thin %s | FileCheck %s --check-prefixes=CHECK,HWASAN,HWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress %s | FileCheck %s --check-prefixes=CHECK,HWASAN +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,HWASAN +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=hwaddress -flto=thin %s | FileCheck %s + +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto=thin %s | FileCheck %s --check-prefixes=CHECK,KHWASAN,KHWASAN-NOOPT +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress %s | FileCheck %s --check-prefixes=CHECK,KHWASAN +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto %s | FileCheck %s --check-prefixes=CHECK,KHWASAN +// RUN: %clang_cc1 -S -emit-llvm -o - -O1 -fexperimental-new-pass-manager -fsanitize=kernel-hwaddress -flto=thin %s | FileCheck %s + +int foo(int *a) { return *a; } + +// All the cases above mark the function with sanitize_hwaddress. +// CHECK-DAG: sanitize_hwaddress + +// Both sanitizers produce %hwasan.shadow without both thinlto and optimizations. +// HWASAN-DAG: %hwasan.shadow +// KHWASAN-DAG: %hwasan.shadow + +// Both sanitizers produce __hwasan_tls without both thinlto and optimizations. +// HWASAN-DAG: __hwasan_tls +// KHWASAN-DAG: __hwasan_tls + +// For unoptimized cases, both sanitizers produce different load functions. +// HWASAN-NOOPT-DAG: __hwasan_loadN +// KHWASAN-NOOPT-DAG: __hwasan_loadN_noabort Index: lib/CodeGen/BackendUtil.cpp === --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -57,6 +57,7 @@ #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" #include "llvm/Transforms/Instrumentation/BoundsChecking.h" #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" +#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h" #include "llvm/Transforms/Instrumentation/InstrProfiling.h" #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" @@ -265,12 +266,13 @@ static_cast(Builder); const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress); - PM.add(createHWAddressSanitizerPass(/*CompileKernel*/ false, Recover)); + PM.add( + createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover)); } static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { - PM.add(createHWAddressSanitizerPass( + PM.add(createHWAddressSanitizerLegacyPassPass( /*CompileKernel*/ true, /*Recover*/ true)); } @@ -962,6 +964,17 @@ if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); } + + if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) { +bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress); +MPM.addPass(createModuleToFunctionPassAdaptor( +HWAddressSanitizerPass(/*CompileKernel=*/false, Recover)))
[PATCH] D61709: [NewPM] Port HWASan and Kernel HWASan
leonardchan added inline comments. Comment at: llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:179 - bool runOnFunction(Function &F) override; - bool doInitialization(Module &M) override; + bool instrumentFunction(Function &F); + void initializeWithModule(Module &M); philip.pfaffe wrote: > There are some naming clashes here. In the other sanitizers these functions > are called `sanitizeFunction` and `initializeModule`. Renamed Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61709/new/ https://reviews.llvm.org/D61709 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61709: [NewPM] Port HWASan and Kernel HWASan
philip.pfaffe accepted this revision. philip.pfaffe added a comment. This revision is now accepted and ready to land. Nit aside, looks good! Comment at: llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:179 - bool runOnFunction(Function &F) override; - bool doInitialization(Module &M) override; + bool instrumentFunction(Function &F); + void initializeWithModule(Module &M); There are some naming clashes here. In the other sanitizers these functions are called `sanitizeFunction` and `initializeModule`. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61709/new/ https://reviews.llvm.org/D61709 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61709: [NewPM] Port HWASan and Kernel HWASan
leonardchan updated this revision to Diff 198889. leonardchan marked 2 inline comments as done. leonardchan retitled this revision from "[NewPM] Port HWASan" to "[NewPM] Port HWASan and Kernel HWASan". leonardchan edited the summary of this revision. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61709/new/ https://reviews.llvm.org/D61709 Files: clang/lib/CodeGen/BackendUtil.cpp clang/test/CodeGen/hwasan-new-pm.c llvm/include/llvm/InitializePasses.h llvm/include/llvm/Transforms/Instrumentation.h llvm/include/llvm/Transforms/Instrumentation/HWAddressSanitizer.h llvm/lib/Passes/PassBuilder.cpp llvm/lib/Passes/PassRegistry.def llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp llvm/lib/Transforms/Instrumentation/Instrumentation.cpp llvm/test/Instrumentation/HWAddressSanitizer/basic.ll Index: llvm/test/Instrumentation/HWAddressSanitizer/basic.ll === --- llvm/test/Instrumentation/HWAddressSanitizer/basic.ll +++ llvm/test/Instrumentation/HWAddressSanitizer/basic.ll @@ -5,6 +5,12 @@ ; RUN: opt < %s -hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,ABORT,ABORT-ZERO-BASED-SHADOW ; RUN: opt < %s -hwasan -hwasan-recover=1 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,RECOVER,RECOVER-ZERO-BASED-SHADOW +; Ensure than hwasan runs with the new PM pass +; RUN: opt < %s -passes='function(hwasan)' -hwasan-recover=0 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=CHECK,ABORT,ABORT-DYNAMIC-SHADOW +; RUN: opt < %s -passes='function(hwasan)' -hwasan-recover=1 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=CHECK,RECOVER,RECOVER-DYNAMIC-SHADOW +; RUN: opt < %s -passes='function(hwasan)' -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,ABORT,ABORT-ZERO-BASED-SHADOW +; RUN: opt < %s -passes='function(hwasan)' -hwasan-recover=1 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,RECOVER,RECOVER-ZERO-BASED-SHADOW + ; CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @hwasan.module_ctor, i8* bitcast (void ()* @hwasan.module_ctor to i8*) }] ; CHECK: @__hwasan = private constant [0 x i8] zeroinitializer, section "__hwasan_frames", comdat($hwasan.module_ctor) Index: llvm/lib/Transforms/Instrumentation/Instrumentation.cpp === --- llvm/lib/Transforms/Instrumentation/Instrumentation.cpp +++ llvm/lib/Transforms/Instrumentation/Instrumentation.cpp @@ -114,7 +114,7 @@ initializeInstrOrderFileLegacyPassPass(Registry); initializeInstrProfilingLegacyPassPass(Registry); initializeMemorySanitizerLegacyPassPass(Registry); - initializeHWAddressSanitizerPass(Registry); + initializeHWAddressSanitizerLegacyPassPass(Registry); initializeThreadSanitizerLegacyPassPass(Registry); initializeSanitizerCoverageModulePass(Registry); initializeDataFlowSanitizerPass(Registry); Index: llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp === --- llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -11,6 +11,7 @@ /// based on tagged addressing. //===--===// +#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" @@ -164,22 +165,19 @@ /// An instrumentation pass implementing detection of addressability bugs /// using tagged pointers. -class HWAddressSanitizer : public FunctionPass { +class HWAddressSanitizer { public: - // Pass identification, replacement for typeid. - static char ID; - - explicit HWAddressSanitizer(bool CompileKernel = false, bool Recover = false) - : FunctionPass(ID) { + explicit HWAddressSanitizer(Module &M, bool CompileKernel = false, + bool Recover = false) { this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover; this->CompileKernel = ClEnableKhwasan.getNumOccurrences() > 0 ? ClEnableKhwasan : CompileKernel; - } - StringRef getPassName() const override { return "HWAddressSanitizer"; } +initializeWithModule(M); + } - bool runOnFunction(Function &F) override; - bool doInitialization(Module &M) override; + bool instrumentFunction(Function &F); + void initializeWithModule(Module &M); void initializeCallbacks(Module &M); @@ -279,29 +277,61 @@ GlobalValue *ThreadPtrGlobal = nullptr; }; +class HWAddressSanitizerLegacyPass : public FunctionPass { +public: + // Pass identification, replacement for typeid. + static char ID; + + explicit HWAddressSanitizerLegacyPass(boo