Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- subzero/src/IceASanInstrumentation.cpp - ASan ------------*- 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 Implements the AddressSanitizer instrumentation class. | |
| 12 /// | |
| 13 //===----------------------------------------------------------------------===// | |
| 14 | |
| 15 #include "IceASanInstrumentation.h" | |
| 16 | |
| 17 #include "IceBuildDefs.h" | |
| 18 #include "IceGlobalInits.h" | |
| 19 | |
| 20 #include <sstream> | |
| 21 | |
| 22 namespace Ice { | |
| 23 | |
| 24 const std::string ASanInstrumentation::RzPrefix = "__$rz"; | |
| 25 const llvm::NaClBitcodeRecord::RecordVector ASanInstrumentation::RzContents = | |
| 26 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); | |
|
Karl
2016/06/10 18:04:50
Minor nit. Since these globals are not needed in t
tlively
2016/06/10 19:17:22
Done.
| |
| 27 | |
| 28 // Create redzones between all global variables, ensuring that the initializer | |
| 29 // types of the redzones and their associated globals match so that they are | |
| 30 // laid out together in memory. | |
| 31 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { | |
| 32 if (BuildDefs::minimal() || DidInsertRedZones) | |
| 33 return; | |
| 34 | |
| 35 VariableDeclarationList NewGlobals; | |
| 36 // Global holding pointers to all redzones | |
| 37 VariableDeclaration *RzArray = VariableDeclaration::create(&NewGlobals); | |
| 38 // Global holding the size of RzArray | |
| 39 VariableDeclaration *RzArraySizeVar = | |
| 40 VariableDeclaration::create(&NewGlobals); | |
| 41 static SizeT RzArraySize = 0; | |
| 42 | |
| 43 RzArray->setName(Ctx, nextRzName()); | |
| 44 RzArraySizeVar->setName(Ctx, nextRzName()); | |
| 45 NewGlobals.push_back(RzArray); | |
| 46 NewGlobals.push_back(RzArraySizeVar); | |
| 47 | |
| 48 // TODO(tlively): Consider alignment when determining redzone layout. | |
| 49 for (VariableDeclaration *Global : Globals) { | |
| 50 VariableDeclaration *RzLeft = | |
| 51 createRz(&NewGlobals, RzArray, RzArraySize, Global); | |
| 52 VariableDeclaration *RzRight = | |
| 53 createRz(&NewGlobals, RzArray, RzArraySize, Global); | |
| 54 NewGlobals.push_back(RzLeft); | |
| 55 NewGlobals.push_back(Global); | |
| 56 NewGlobals.push_back(RzRight); | |
| 57 } | |
| 58 | |
| 59 // update the contents of the RzArraySize global | |
| 60 llvm::NaClBitcodeRecord::RecordVector SizeContents; | |
| 61 for (unsigned i = 0; i < sizeof(RzArraySize); i++) { | |
| 62 SizeContents.emplace_back(RzArraySize % (1 << CHAR_BIT)); | |
| 63 RzArraySize >>= CHAR_BIT; | |
| 64 } | |
| 65 RzArraySizeVar->addInitializer( | |
| 66 VariableDeclaration::DataInitializer::create(&NewGlobals, SizeContents)); | |
| 67 | |
| 68 // Replace old list of globals, without messing up arena allocators | |
| 69 Globals.clear(); | |
| 70 Globals.merge(&NewGlobals); | |
| 71 DidInsertRedZones = true; | |
| 72 | |
| 73 // Log the new set of globals | |
| 74 if (BuildDefs::dump()) { | |
| 75 OstreamLocker _(Ctx); | |
| 76 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; | |
| 77 for (VariableDeclaration *Global : Globals) { | |
| 78 Global->dump(Ctx->getStrDump()); | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 std::string ASanInstrumentation::nextRzName() { | |
| 84 std::stringstream Name; | |
| 85 Name << RzPrefix << RzNum++; | |
| 86 return Name.str(); | |
| 87 } | |
| 88 | |
| 89 VariableDeclaration * | |
| 90 ASanInstrumentation::createRz(VariableDeclarationList *List, | |
| 91 VariableDeclaration *RzArray, SizeT &RzArraySize, | |
| 92 VariableDeclaration *Global) { | |
| 93 VariableDeclaration *Rz = VariableDeclaration::create(List); | |
| 94 Rz->setName(Ctx, nextRzName()); | |
| 95 if (Global->hasNonzeroInitializer()) { | |
| 96 Rz->addInitializer( | |
| 97 VariableDeclaration::DataInitializer::create(List, RzContents)); | |
| 98 } else { | |
| 99 Rz->addInitializer( | |
| 100 VariableDeclaration::ZeroInitializer::create(List, RzSize)); | |
| 101 } | |
| 102 Rz->setIsConstant(Global->getIsConstant()); | |
| 103 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | |
| 104 List, Rz, RelocOffsetArray(0))); | |
| 105 ++RzArraySize; | |
| 106 return Rz; | |
| 107 } | |
| 108 | |
| 109 } // end of namespace Ice | |
| OLD | NEW |