From ebd10a24f4c3bc24011b399d93fca789fcac8986 Mon Sep 17 00:00:00 2001 From: Leonard Chan Date: Wed, 17 Oct 2018 15:38:22 +0000 Subject: [PATCH] [PassManager/Sanitizer] Enable usage of ported AddressSanitizer passes with -fsanitize=address Enable usage of `AddressSanitizer` and `AddressModuleSanitizer` ported from the legacy to the new PassManager. This patch depends on https://reviews.llvm.org/D52739. Differential Revision: https://reviews.llvm.org/D52814 llvm-svn: 344699 --- clang/lib/CodeGen/BackendUtil.cpp | 11 +++++++++++ clang/test/CodeGen/asan-new-pm.ll | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 clang/test/CodeGen/asan-new-pm.ll diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index fd9a95b..ee9fa84 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -52,6 +52,7 @@ #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/Instrumentation.h" +#include "llvm/Transforms/Instrumentation/AddressSanitizerPass.h" #include "llvm/Transforms/Instrumentation/BoundsChecking.h" #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" #include "llvm/Transforms/ObjCARC.h" @@ -1022,6 +1023,16 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager( CodeGenOpts.DebugPassManager); } } + + if (LangOpts.Sanitize.has(SanitizerKind::Address)) { + bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address); + MPM.addPass(createModuleToFunctionPassAdaptor( + AddressSanitizerPass(/*CompileKernel=*/false, Recover, + CodeGenOpts.SanitizeAddressUseAfterScope))); + bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts); + MPM.addPass(AddressSanitizerPass(/*CompileKernel=*/false, Recover, + ModuleUseAfterScope)); + } } // FIXME: We still use the legacy pass manager to do code generation. We diff --git a/clang/test/CodeGen/asan-new-pm.ll b/clang/test/CodeGen/asan-new-pm.ll new file mode 100644 index 0000000..4cd24cb --- /dev/null +++ b/clang/test/CodeGen/asan-new-pm.ll @@ -0,0 +1,31 @@ +; RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=address %s | FileCheck %s + +; CHECK: @llvm.global_ctors = {{.*}}@asan.module_ctor + +define i32 @test_load(i32* %a) sanitize_address { +entry: +; CHECK: %0 = ptrtoint i32* %a to i64 +; CHECK: %1 = lshr i64 %0, 3 +; CHECK: %2 = add i64 %1, 2147450880 +; CHECK: %3 = inttoptr i64 %2 to i8* +; CHECK: %4 = load i8, i8* %3 +; CHECK: %5 = icmp ne i8 %4, 0 +; CHECK: br i1 %5, label %6, label %12, !prof !0 + +; CHECK:;