1 //===- subzero/src/IceASanInstrumentation.h - AddressSanitizer --*- C++ -*-===// 2 // 3 // The Subzero Code Generator 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Declares the AddressSanitizer instrumentation class. 12 /// 13 /// This class is responsible for inserting redzones around global and stack 14 /// variables, inserting code responsible for poisoning those redzones, and 15 /// performing any other instrumentation necessary to implement 16 /// AddressSanitizer. 17 /// 18 //===----------------------------------------------------------------------===// 19 20 #ifndef SUBZERO_SRC_ICEASANINSTRUMENTATION_H 21 #define SUBZERO_SRC_ICEASANINSTRUMENTATION_H 22 23 #include "IceGlobalInits.h" 24 #include "IceInstrumentation.h" 25 26 namespace Ice { 27 28 using VarSizeMap = std::unordered_map<Operand *, SizeT>; 29 using GlobalSizeMap = std::unordered_map<GlobalString, SizeT>; 30 31 class ASanInstrumentation : public Instrumentation { 32 ASanInstrumentation() = delete; 33 ASanInstrumentation(const ASanInstrumentation &) = delete; 34 ASanInstrumentation &operator=(const ASanInstrumentation &) = delete; 35 36 public: ASanInstrumentation(GlobalContext * Ctx)37 ASanInstrumentation(GlobalContext *Ctx) : Instrumentation(Ctx), RzNum(0) { 38 ICE_TLS_INIT_FIELD(LocalVars); 39 ICE_TLS_INIT_FIELD(LocalDtors); 40 ICE_TLS_INIT_FIELD(CurNode); 41 ICE_TLS_INIT_FIELD(CheckedVars); 42 } 43 void instrumentGlobals(VariableDeclarationList &Globals) override; 44 45 private: 46 std::string nextRzName(); 47 bool isOkGlobalAccess(Operand *Op, SizeT Size); 48 ConstantRelocatable *instrumentReloc(ConstantRelocatable *Reloc); 49 bool isInstrumentable(Cfg *Func) override; 50 void instrumentFuncStart(LoweringContext &Context) override; 51 void instrumentCall(LoweringContext &Context, InstCall *Instr) override; 52 void instrumentRet(LoweringContext &Context, InstRet *Instr) override; 53 void instrumentLoad(LoweringContext &Context, InstLoad *Instr) override; 54 void instrumentStore(LoweringContext &Context, InstStore *Instr) override; 55 void instrumentAccess(LoweringContext &Context, Operand *Op, SizeT Size, 56 Constant *AccessFunc); 57 void instrumentStart(Cfg *Func) override; 58 void finishFunc(Cfg *Func) override; 59 ICE_TLS_DECLARE_FIELD(VarSizeMap *, LocalVars); 60 ICE_TLS_DECLARE_FIELD(std::vector<InstStore *> *, LocalDtors); 61 ICE_TLS_DECLARE_FIELD(CfgNode *, CurNode); 62 ICE_TLS_DECLARE_FIELD(VarSizeMap *, CheckedVars); 63 GlobalSizeMap GlobalSizes; 64 std::atomic<uint32_t> RzNum; 65 bool DidProcessGlobals = false; 66 SizeT RzGlobalsNum = 0; 67 std::mutex GlobalsMutex; 68 }; 69 } // end of namespace Ice 70 71 #endif // SUBZERO_SRC_ICEASANINSTRUMENTATION_H 72