LLVM  mainline
Instrumentation.h
Go to the documentation of this file.
00001 //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines constructor functions for instrumentation passes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
00015 #define LLVM_TRANSFORMS_INSTRUMENTATION_H
00016 
00017 #include "llvm/ADT/StringRef.h"
00018 #include "llvm/IR/BasicBlock.h"
00019 #include <vector>
00020 
00021 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
00022 inline void *getDFSanArgTLSPtrForJIT() {
00023   extern __thread __attribute__((tls_model("initial-exec")))
00024     void *__dfsan_arg_tls;
00025   return (void *)&__dfsan_arg_tls;
00026 }
00027 
00028 inline void *getDFSanRetValTLSPtrForJIT() {
00029   extern __thread __attribute__((tls_model("initial-exec")))
00030     void *__dfsan_retval_tls;
00031   return (void *)&__dfsan_retval_tls;
00032 }
00033 #endif
00034 
00035 namespace llvm {
00036 
00037 class TargetMachine;
00038 
00039 /// Instrumentation passes often insert conditional checks into entry blocks.
00040 /// Call this function before splitting the entry block to move instructions
00041 /// that must remain in the entry block up before the split point. Static
00042 /// allocas and llvm.localescape calls, for example, must remain in the entry
00043 /// block.
00044 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
00045                                               BasicBlock::iterator IP);
00046 
00047 class ModulePass;
00048 class FunctionPass;
00049 
00050 // Insert GCOV profiling instrumentation
00051 struct GCOVOptions {
00052   static GCOVOptions getDefault();
00053 
00054   // Specify whether to emit .gcno files.
00055   bool EmitNotes;
00056 
00057   // Specify whether to modify the program to emit .gcda files when run.
00058   bool EmitData;
00059 
00060   // A four-byte version string. The meaning of a version string is described in
00061   // gcc's gcov-io.h
00062   char Version[4];
00063 
00064   // Emit a "cfg checksum" that follows the "line number checksum" of a
00065   // function. This affects both .gcno and .gcda files.
00066   bool UseCfgChecksum;
00067 
00068   // Add the 'noredzone' attribute to added runtime library calls.
00069   bool NoRedZone;
00070 
00071   // Emit the name of the function in the .gcda files. This is redundant, as
00072   // the function identifier can be used to find the name from the .gcno file.
00073   bool FunctionNamesInData;
00074 
00075   // Emit the exit block immediately after the start block, rather than after
00076   // all of the function body's blocks.
00077   bool ExitBlockBeforeBody;
00078 };
00079 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
00080                                    GCOVOptions::getDefault());
00081 
00082 // PGO Instrumention
00083 ModulePass *createPGOInstrumentationGenPass();
00084 ModulePass *
00085 createPGOInstrumentationUsePass(StringRef Filename = StringRef(""));
00086 
00087 /// Options for the frontend instrumentation based profiling pass.
00088 struct InstrProfOptions {
00089   InstrProfOptions() : NoRedZone(false) {}
00090 
00091   // Add the 'noredzone' attribute to added runtime library calls.
00092   bool NoRedZone;
00093 
00094   // Name of the profile file to use as output
00095   std::string InstrProfileOutput;
00096 };
00097 
00098 /// Insert frontend instrumentation based profiling.
00099 ModulePass *createInstrProfilingPass(
00100     const InstrProfOptions &Options = InstrProfOptions());
00101 
00102 // Insert AddressSanitizer (address sanity checking) instrumentation
00103 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
00104                                                  bool Recover = false);
00105 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
00106                                              bool Recover = false);
00107 
00108 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
00109 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0);
00110 
00111 // Insert ThreadSanitizer (race detection) instrumentation
00112 FunctionPass *createThreadSanitizerPass();
00113 
00114 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
00115 ModulePass *createDataFlowSanitizerPass(
00116     const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
00117     void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
00118 
00119 // Options for sanitizer coverage instrumentation.
00120 struct SanitizerCoverageOptions {
00121   SanitizerCoverageOptions()
00122       : CoverageType(SCK_None), IndirectCalls(false), TraceBB(false),
00123         TraceCmp(false), Use8bitCounters(false) {}
00124 
00125   enum Type {
00126     SCK_None = 0,
00127     SCK_Function,
00128     SCK_BB,
00129     SCK_Edge
00130   } CoverageType;
00131   bool IndirectCalls;
00132   bool TraceBB;
00133   bool TraceCmp;
00134   bool Use8bitCounters;
00135 };
00136 
00137 // Insert SanitizerCoverage instrumentation.
00138 ModulePass *createSanitizerCoverageModulePass(
00139     const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
00140 
00141 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
00142 inline ModulePass *createDataFlowSanitizerPassForJIT(
00143     const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
00144   return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
00145                                      getDFSanRetValTLSPtrForJIT);
00146 }
00147 #endif
00148 
00149 // BoundsChecking - This pass instruments the code to perform run-time bounds
00150 // checking on loads, stores, and other memory intrinsics.
00151 FunctionPass *createBoundsCheckingPass();
00152 
00153 /// \brief This pass splits the stack into a safe stack and an unsafe stack to
00154 /// protect against stack-based overflow vulnerabilities.
00155 FunctionPass *createSafeStackPass(const TargetMachine *TM = nullptr);
00156 
00157 /// \brief Calculate what to divide by to scale counts.
00158 ///
00159 /// Given the maximum count, calculate a divisor that will scale all the
00160 /// weights to strictly less than UINT32_MAX.
00161 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
00162   return MaxCount < UINT32_MAX ? 1 : MaxCount / UINT32_MAX + 1;
00163 }
00164 
00165 /// \brief Scale an individual branch count.
00166 ///
00167 /// Scale a 64-bit weight down to 32-bits using \c Scale.
00168 ///
00169 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
00170   uint64_t Scaled = Count / Scale;
00171   assert(Scaled <= UINT32_MAX && "overflow 32-bits");
00172   return Scaled;
00173 }
00174 
00175 } // End llvm namespace
00176 
00177 #endif