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 namespace { |
| 25 constexpr SizeT RzSize = 32; |
| 26 const std::string RzPrefix = "__$rz"; |
| 27 const llvm::NaClBitcodeRecord::RecordVector RzContents = |
| 28 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); |
| 29 } // end of anonymous namespace |
| 30 |
| 31 // Create redzones between all global variables, ensuring that the initializer |
| 32 // types of the redzones and their associated globals match so that they are |
| 33 // laid out together in memory. |
| 34 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { |
| 35 if (BuildDefs::minimal() || DidInsertRedZones) |
| 36 return; |
| 37 |
| 38 VariableDeclarationList NewGlobals; |
| 39 // Global holding pointers to all redzones |
| 40 auto *RzArray = VariableDeclaration::create(&NewGlobals); |
| 41 // Global holding the size of RzArray |
| 42 auto *RzArraySizeVar = VariableDeclaration::create(&NewGlobals); |
| 43 SizeT RzArraySize = 0; |
| 44 |
| 45 RzArray->setName(Ctx, nextRzName()); |
| 46 RzArraySizeVar->setName(Ctx, nextRzName()); |
| 47 RzArray->setIsConstant(true); |
| 48 RzArraySizeVar->setIsConstant(true); |
| 49 NewGlobals.push_back(RzArray); |
| 50 NewGlobals.push_back(RzArraySizeVar); |
| 51 |
| 52 for (VariableDeclaration *Global : Globals) { |
| 53 VariableDeclaration *RzLeft = createRz(&NewGlobals, RzArray, RzArraySize, Gl
obal); |
| 54 VariableDeclaration *RzRight = createRz(&NewGlobals, RzArray, RzArraySize, G
lobal); |
| 55 NewGlobals.push_back(RzLeft); |
| 56 NewGlobals.push_back(Global); |
| 57 NewGlobals.push_back(RzRight); |
| 58 } |
| 59 |
| 60 // update the contents of the RzArraySize global |
| 61 llvm::NaClBitcodeRecord::RecordVector SizeContents; |
| 62 for (unsigned i = 0; i < sizeof(RzArraySize); i++) { |
| 63 SizeContents.emplace_back(RzArraySize % (1 << CHAR_BIT)); |
| 64 RzArraySize >>= CHAR_BIT; |
| 65 } |
| 66 RzArraySizeVar->addInitializer( |
| 67 VariableDeclaration::DataInitializer::create(&NewGlobals, SizeContents)); |
| 68 |
| 69 // Replace old list of globals, without messing up arena allocators |
| 70 Globals.clear(); |
| 71 Globals.merge(&NewGlobals); |
| 72 DidInsertRedZones = true; |
| 73 |
| 74 // Log the new set of globals |
| 75 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { |
| 76 OstreamLocker _(Ctx); |
| 77 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; |
| 78 for (VariableDeclaration *Global : Globals) { |
| 79 Global->dump(Ctx->getStrDump()); |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 std::string ASanInstrumentation::nextRzName() { |
| 85 if (BuildDefs::minimal()) |
| 86 return ""; |
| 87 std::stringstream Name; |
| 88 Name << RzPrefix << RzNum++; |
| 89 return Name.str(); |
| 90 } |
| 91 |
| 92 VariableDeclaration * |
| 93 ASanInstrumentation::createRz(VariableDeclarationList *List, |
| 94 VariableDeclaration *RzArray, SizeT &RzArraySize, |
| 95 VariableDeclaration *Global) { |
| 96 if (BuildDefs::minimal()) |
| 97 return nullptr; |
| 98 auto *Rz = VariableDeclaration::create(List); |
| 99 Rz->setName(Ctx, nextRzName()); |
| 100 if (Global->hasNonzeroInitializer()) { |
| 101 Rz->addInitializer( |
| 102 VariableDeclaration::DataInitializer::create(List, RzContents)); |
| 103 } else { |
| 104 Rz->addInitializer( |
| 105 VariableDeclaration::ZeroInitializer::create(List, RzSize)); |
| 106 } |
| 107 Rz->setIsConstant(Global->getIsConstant()); |
| 108 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 109 List, Rz, RelocOffsetArray(0))); |
| 110 ++RzArraySize; |
| 111 return Rz; |
| 112 } |
| 113 |
| 114 } // end of namespace Ice |
OLD | NEW |