From 1b4eca67e073f0beefb9924cbe6c65427869f1fa Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 16 Nov 2011 17:34:26 +0000 Subject: [PATCH] Add -f[no-]address-sanitizer flag git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144800 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/CC1Options.td | 2 ++ include/clang/Driver/Options.td | 2 ++ include/clang/Frontend/CodeGenOptions.h | 2 ++ lib/CodeGen/BackendUtil.cpp | 10 ++++++++++ lib/Driver/Tools.cpp | 4 ++++ lib/Frontend/CompilerInvocation.cpp | 3 +++ 6 files changed, 23 insertions(+) diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index ae97341872..c8f4a1d77e 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -114,6 +114,8 @@ def fdebug_compilation_dir : Separate<"-fdebug-compilation-dir">, HelpText<"The compilation directory to embed in the debug info.">; def dwarf_debug_flags : Separate<"-dwarf-debug-flags">, HelpText<"The string to embed in the Dwarf debug flags record.">; +def faddress_sanitizer: Flag<"-faddress-sanitizer">, + HelpText<"Enable AddressSanitizer instrumentation (memory error detection)">; def fforbid_guard_variables : Flag<"-fforbid-guard-variables">, HelpText<"Emit an error if a C++ static local initializer would need a guard variable">; def g : Flag<"-g">, HelpText<"Generate source level debug information">; diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 1e2274a8b7..1cda33d758 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -262,6 +262,8 @@ def faccess_control : Flag<"-faccess-control">, Group; def fallow_unsupported : Flag<"-fallow-unsupported">, Group; def fapple_kext : Flag<"-fapple-kext">, Group; def fapple_pragma_pack : Flag<"-fapple-pragma-pack">, Group; +def faddress_sanitizer : Flag<"-faddress-sanitizer">, Group; +def fno_address_sanitizer : Flag<"-fno-address-sanitizer">, Group; def fasm : Flag<"-fasm">, Group; def fasm_blocks : Flag<"-fasm-blocks">, Group; diff --git a/include/clang/Frontend/CodeGenOptions.h b/include/clang/Frontend/CodeGenOptions.h index 9962ea8cf9..80720d4e36 100644 --- a/include/clang/Frontend/CodeGenOptions.h +++ b/include/clang/Frontend/CodeGenOptions.h @@ -35,6 +35,7 @@ public: Mixed = 2 }; + unsigned AddressSanitizer : 1; /// Enable AddressSanitizer. unsigned AsmVerbose : 1; /// -dA, -fverbose-asm. unsigned ObjCAutoRefCountExceptions : 1; /// Whether ARC should be EH-safe. unsigned CUDAIsDevice : 1; /// Set when compiling for CUDA device. @@ -151,6 +152,7 @@ public: public: CodeGenOptions() { + AddressSanitizer = 0; AsmVerbose = 0; CUDAIsDevice = 0; CXAAtExit = 1; diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 3639dba49a..0410cec736 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -115,6 +115,11 @@ static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase PM.add(createObjCARCOptPass()); } +static void addAddressSanitizerPass(const PassManagerBuilder &Builder, + PassManagerBase &PM) { + PM.add(createAddressSanitizerPass()); +} + void EmitAssemblyHelper::CreatePasses() { unsigned OptLevel = CodeGenOpts.OptimizationLevel; CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; @@ -141,6 +146,11 @@ void EmitAssemblyHelper::CreatePasses() { PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, addObjCARCOptPass); } + + if (CodeGenOpts.AddressSanitizer) { + PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, + addAddressSanitizerPass); + } // Figure out TargetLibraryInfo. Triple TargetTriple(TheModule->getTargetTriple()); diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 6a85fc00b3..52438cc34d 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1701,6 +1701,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (getToolChain().SupportsProfiling()) Args.AddLastArg(CmdArgs, options::OPT_pg); + if (Args.hasFlag(options::OPT_faddress_sanitizer, + options::OPT_fno_address_sanitizer, false)) + CmdArgs.push_back("-faddress-sanitizer"); + // -flax-vector-conversions is default. if (!Args.hasFlag(options::OPT_flax_vector_conversions, options::OPT_fno_lax_vector_conversions)) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 58ee2735b9..1fd967ffcc 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -139,6 +139,8 @@ static void CodeGenOptsToArgs(const CodeGenOptions &Opts, Res.push_back("-dwarf-debug-flags"); Res.push_back(Opts.DwarfDebugFlags); } + if (Opts.AddressSanitizer) + Res.push_back("-faddress-sanitizer"); if (Opts.ObjCRuntimeHasARC) Res.push_back("-fobjc-runtime-has-arc"); if (Opts.ObjCRuntimeHasTerminate) @@ -1049,6 +1051,7 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.UnrollLoops = Args.hasArg(OPT_funroll_loops) || (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize); + Opts.AddressSanitizer = Args.hasArg(OPT_faddress_sanitizer); Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose); Opts.ObjCAutoRefCountExceptions = Args.hasArg(OPT_fobjc_arc_exceptions); Opts.ObjCRuntimeHasARC = Args.hasArg(OPT_fobjc_runtime_has_arc); -- 2.40.0