Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: src/IceASanInstrumentation.cpp

Issue 2079723002: Instrumented malloc and free with dummy functions. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Cleaned up instrumentCall() Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/src/IceASanInstrumentation.cpp - ASan ------------*- C++ -*-===// 1 //===- subzero/src/IceASanInstrumentation.cpp - ASan ------------*- C++ -*-===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
11 /// \brief Implements the AddressSanitizer instrumentation class. 11 /// \brief Implements the AddressSanitizer instrumentation class.
12 /// 12 ///
13 //===----------------------------------------------------------------------===// 13 //===----------------------------------------------------------------------===//
14 14
15 #include "IceASanInstrumentation.h" 15 #include "IceASanInstrumentation.h"
16 16
17 #include "IceBuildDefs.h" 17 #include "IceBuildDefs.h"
18 #include "IceCfgNode.h" 18 #include "IceCfgNode.h"
19 #include "IceGlobalInits.h" 19 #include "IceGlobalInits.h"
20 #include "IceInst.h" 20 #include "IceInst.h"
21 #include "IceTargetLowering.h" 21 #include "IceTargetLowering.h"
22 #include "IceTypes.h" 22 #include "IceTypes.h"
23 23
24 #include <sstream> 24 #include <sstream>
25 #include <unordered_map>
25 26
26 namespace Ice { 27 namespace Ice {
27 28
28 namespace { 29 namespace {
29 constexpr SizeT RzSize = 32; 30 constexpr SizeT RzSize = 32;
30 const std::string RzPrefix = "__$rz"; 31 const std::string RzPrefix = "__$rz";
31 const llvm::NaClBitcodeRecord::RecordVector RzContents = 32 const llvm::NaClBitcodeRecord::RecordVector RzContents =
32 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); 33 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R');
34
35 // TODO(tlively): Handle all allocation functions
36 // In order to instrument the code correctly, the .pexe must not have had its
37 // symbols stripped.
38 using string_map = std::unordered_map<std::string, std::string>;
39 const string_map FuncSubstitutions = {{"malloc", "__asan_malloc"},
40 {"free", "__asan_free"}};
41
33 } // end of anonymous namespace 42 } // end of anonymous namespace
34 43
35 // Create redzones around all global variables, ensuring that the initializer 44 // Create redzones around all global variables, ensuring that the initializer
36 // types of the redzones and their associated globals match so that they are 45 // types of the redzones and their associated globals match so that they are
37 // laid out together in memory. 46 // laid out together in memory.
38 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { 47 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) {
39 if (DidInsertRedZones) 48 if (DidInsertRedZones)
40 return; 49 return;
41 50
42 VariableDeclarationList NewGlobals; 51 VariableDeclarationList NewGlobals;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 Rz->addInitializer( 115 Rz->addInitializer(
107 VariableDeclaration::ZeroInitializer::create(List, RzSize)); 116 VariableDeclaration::ZeroInitializer::create(List, RzSize));
108 } 117 }
109 Rz->setIsConstant(Global->getIsConstant()); 118 Rz->setIsConstant(Global->getIsConstant());
110 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( 119 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create(
111 List, Rz, RelocOffsetArray(0))); 120 List, Rz, RelocOffsetArray(0)));
112 ++RzArraySize; 121 ++RzArraySize;
113 return Rz; 122 return Rz;
114 } 123 }
115 124
125 void ASanInstrumentation::instrumentCall(LoweringContext &Context,
126 InstCall *Instr) {
127 auto *CallTarget =
128 llvm::dyn_cast<ConstantRelocatable>(Instr->getCallTarget());
129 if (CallTarget == nullptr)
130 return;
131
132 std::string TargetName = CallTarget->getName().toStringOrEmpty();
133 auto Subst = FuncSubstitutions.find(TargetName);
134 if (Subst == FuncSubstitutions.end())
135 return;
136
137 std::string SubName = Subst->second;
138 Constant *NewFunc =
139 Ctx->getConstantExternSym(Ctx->getGlobalString(SubName));
140 auto *NewCall =
141 InstCall::create(Context.getNode()->getCfg(), Instr->getNumArgs(),
142 Instr->getDest(), NewFunc, Instr->isTailcall());
143 for (SizeT I = 0, Args = Instr->getNumArgs(); I < Args; ++I)
144 NewCall->addArg(Instr->getArg(I));
145 Context.insert(NewCall);
146 Instr->setDeleted();
147 }
148
116 void ASanInstrumentation::instrumentLoad(LoweringContext &Context, 149 void ASanInstrumentation::instrumentLoad(LoweringContext &Context,
117 const InstLoad *Inst) { 150 InstLoad *Instr) {
118 instrumentAccess(Context, Inst->getSourceAddress(), 151 instrumentAccess(Context, Instr->getSourceAddress(),
119 typeWidthInBytes(Inst->getDest()->getType())); 152 typeWidthInBytes(Instr->getDest()->getType()));
120 } 153 }
121 154
122 void ASanInstrumentation::instrumentStore(LoweringContext &Context, 155 void ASanInstrumentation::instrumentStore(LoweringContext &Context,
123 const InstStore *Inst) { 156 InstStore *Instr) {
124 instrumentAccess(Context, Inst->getAddr(), 157 instrumentAccess(Context, Instr->getAddr(),
125 typeWidthInBytes(Inst->getData()->getType())); 158 typeWidthInBytes(Instr->getData()->getType()));
126 } 159 }
127 160
128 // TODO(tlively): Take size of access into account as well 161 // TODO(tlively): Take size of access into account as well
129 void ASanInstrumentation::instrumentAccess(LoweringContext &Context, 162 void ASanInstrumentation::instrumentAccess(LoweringContext &Context,
130 Operand *Op, SizeT Size) { 163 Operand *Op, SizeT Size) {
131 Constant *AccessCheck = 164 Constant *AccessCheck =
132 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_check")); 165 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_check"));
133 constexpr SizeT NumArgs = 2; 166 constexpr SizeT NumArgs = 2;
134 constexpr Variable *Void = nullptr; 167 constexpr Variable *Void = nullptr;
135 constexpr bool NoTailCall = false; 168 constexpr bool NoTailCall = false;
(...skipping 12 matching lines...) Expand all
148 Constant *ShadowMemInit = 181 Constant *ShadowMemInit =
149 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); 182 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init"));
150 constexpr SizeT NumArgs = 0; 183 constexpr SizeT NumArgs = 0;
151 constexpr Variable *Void = nullptr; 184 constexpr Variable *Void = nullptr;
152 constexpr bool NoTailCall = false; 185 constexpr bool NoTailCall = false;
153 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); 186 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall);
154 Func->getEntryNode()->getInsts().push_front(Call); 187 Func->getEntryNode()->getInsts().push_front(Call);
155 } 188 }
156 189
157 } // end of namespace Ice 190 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698