blob: 765eedccd9f71a68bed33e923a94308bab4e2b46 [file] [log] [blame]
Thomas Lively3f5cb6f2016-06-13 11:23:29 -07001//===- 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
26namespace Ice {
27
Thomas Livelyac27c512016-07-26 11:47:40 -070028using VarSizeMap = std::unordered_map<Operand *, SizeT>;
Thomas Lively181a9bc2016-07-27 15:55:42 -070029using GlobalSizeMap = std::unordered_map<GlobalString, SizeT>;
Thomas Livelyac27c512016-07-26 11:47:40 -070030
Thomas Lively3f5cb6f2016-06-13 11:23:29 -070031class ASanInstrumentation : public Instrumentation {
32 ASanInstrumentation() = delete;
33 ASanInstrumentation(const ASanInstrumentation &) = delete;
34 ASanInstrumentation &operator=(const ASanInstrumentation &) = delete;
35
36public:
Thomas Lively2c9992a2016-07-20 11:19:17 -070037 ASanInstrumentation(GlobalContext *Ctx) : Instrumentation(Ctx), RzNum(0) {
Thomas Livelyac27c512016-07-26 11:47:40 -070038 ICE_TLS_INIT_FIELD(LocalVars);
Thomas Lively1fd80c72016-06-27 14:47:21 -070039 ICE_TLS_INIT_FIELD(LocalDtors);
Thomas Lively9b384972016-08-11 11:24:27 -070040 ICE_TLS_INIT_FIELD(CurNode);
41 ICE_TLS_INIT_FIELD(CheckedVars);
Thomas Lively1fd80c72016-06-27 14:47:21 -070042 }
Thomas Lively3f5cb6f2016-06-13 11:23:29 -070043 void instrumentGlobals(VariableDeclarationList &Globals) override;
44
45private:
46 std::string nextRzName();
Thomas Lively181a9bc2016-07-27 15:55:42 -070047 bool isOkGlobalAccess(Operand *Op, SizeT Size);
Thomas Lively75f52292016-08-18 10:37:46 -070048 ConstantRelocatable *instrumentReloc(ConstantRelocatable *Reloc);
Thomas Lively3f97afb2016-07-07 14:56:21 -070049 bool isInstrumentable(Cfg *Func) override;
Thomas Lively227c9f32016-06-21 11:43:07 -070050 void instrumentFuncStart(LoweringContext &Context) override;
Thomas Lively26c43062016-06-17 15:53:24 -070051 void instrumentCall(LoweringContext &Context, InstCall *Instr) override;
Thomas Lively1fd80c72016-06-27 14:47:21 -070052 void instrumentRet(LoweringContext &Context, InstRet *Instr) override;
Thomas Lively26c43062016-06-17 15:53:24 -070053 void instrumentLoad(LoweringContext &Context, InstLoad *Instr) override;
54 void instrumentStore(LoweringContext &Context, InstStore *Instr) override;
Thomas Livelycf062792016-07-06 10:02:45 -070055 void instrumentAccess(LoweringContext &Context, Operand *Op, SizeT Size,
56 Constant *AccessFunc);
Thomas Lively4e81fe02016-06-15 10:00:21 -070057 void instrumentStart(Cfg *Func) override;
Thomas Lively1fd80c72016-06-27 14:47:21 -070058 void finishFunc(Cfg *Func) override;
Thomas Livelyac27c512016-07-26 11:47:40 -070059 ICE_TLS_DECLARE_FIELD(VarSizeMap *, LocalVars);
Thomas Livelycbd3dbc2016-08-09 15:02:35 -070060 ICE_TLS_DECLARE_FIELD(std::vector<InstStore *> *, LocalDtors);
Thomas Lively9b384972016-08-11 11:24:27 -070061 ICE_TLS_DECLARE_FIELD(CfgNode *, CurNode);
62 ICE_TLS_DECLARE_FIELD(VarSizeMap *, CheckedVars);
Thomas Lively181a9bc2016-07-27 15:55:42 -070063 GlobalSizeMap GlobalSizes;
Thomas Lively227c9f32016-06-21 11:43:07 -070064 std::atomic<uint32_t> RzNum;
Thomas Livelyaedc5e42016-06-30 11:06:12 -070065 bool DidProcessGlobals = false;
66 SizeT RzGlobalsNum = 0;
67 std::mutex GlobalsMutex;
Thomas Lively3f5cb6f2016-06-13 11:23:29 -070068};
69} // end of namespace Ice
70
71#endif // SUBZERO_SRC_ICEASANINSTRUMENTATION_H