Hello,

Please review the following patch which adds -fasan (AddressSanitizer) flag
to clang.
(Mostly prepared by Chandler Carruth).
The LLVM part of AddressSanitizer has been submitted as r144758.
This patch will allow to enable AddressSanitizer from the clang command
line.
http://codereview.appspot.com/5396042

Coming next in separate patches:
  - a patch to clang driver to pass linker flags for AddressSanitizer
(small)
  - the run-time library and the tests (big)

Thanks,

--kcc
Index: include/clang/Frontend/CodeGenOptions.h
===================================================================
--- include/clang/Frontend/CodeGenOptions.h	(revision 144759)
+++ include/clang/Frontend/CodeGenOptions.h	(working copy)
@@ -35,6 +35,7 @@
     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:
   CodeGenOptions() {
+    AddressSanitizer = 0;
     AsmVerbose = 0;
     CUDAIsDevice = 0;
     CXAAtExit = 1;
Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td	(revision 144759)
+++ include/clang/Driver/CC1Options.td	(working copy)
@@ -114,6 +114,8 @@
   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">;
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td	(revision 144759)
+++ include/clang/Driver/Options.td	(working copy)
@@ -262,6 +262,8 @@
 def fallow_unsupported : Flag<"-fallow-unsupported">, Group<f_Group>;
 def fapple_kext : Flag<"-fapple-kext">, Group<f_Group>;
 def fapple_pragma_pack : Flag<"-fapple-pragma-pack">, Group<f_Group>;
+def fasan : Flag<"-fasan">, Group<f_Group>;
+def fno_asan : Flag<"-fno-asan">, Group<f_Group>;
 def fasm : Flag<"-fasm">, Group<f_Group>;
 
 def fasm_blocks : Flag<"-fasm-blocks">, Group<f_Group>;
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp	(revision 144759)
+++ lib/Frontend/CompilerInvocation.cpp	(working copy)
@@ -139,6 +139,8 @@
     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 @@
   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);
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp	(revision 144759)
+++ lib/Driver/Tools.cpp	(working copy)
@@ -427,6 +427,10 @@
 
   // Add system include arguments.
   getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
+
+  if (Args.hasFlag(options::OPT_fasan, options::OPT_fno_asan, false)) {
+    CmdArgs.push_back("-DADDRESS_SANITIZER=1");
+  }
 }
 
 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
@@ -1701,6 +1705,10 @@
   if (getToolChain().SupportsProfiling())
     Args.AddLastArg(CmdArgs, options::OPT_pg);
 
+  if (Args.hasFlag(options::OPT_fasan, options::OPT_fno_asan, 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))
Index: lib/CodeGen/BackendUtil.cpp
===================================================================
--- lib/CodeGen/BackendUtil.cpp	(revision 144759)
+++ lib/CodeGen/BackendUtil.cpp	(working copy)
@@ -115,6 +115,11 @@
     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 @@
     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
                            addObjCARCOptPass);
   }
+
+  if (CodeGenOpts.AddressSanitizer) {
+    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
+                           addAddressSanitizerPass);
+  }
   
   // Figure out TargetLibraryInfo.
   Triple TargetTriple(TheModule->getTargetTriple());
_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to