Chromium Code Reviews

Side by Side Diff: src/IceASanInstrumentation.cpp

Issue 2054943002: Implemented global redzones. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Moved variables to anonymous namespace. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(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 const SizeT RzSize = 32;
Jim Stichnoth 2016/06/10 22:38:33 constexpr
tlively 2016/06/10 23:43:32 Done.
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 VariableDeclaration *RzArray = VariableDeclaration::create(&NewGlobals);
Jim Stichnoth 2016/06/10 22:38:33 I would use "auto" here, and other places with the
tlively 2016/06/10 23:43:32 Done.
41 // Global holding the size of RzArray
42 VariableDeclaration *RzArraySizeVar =
43 VariableDeclaration::create(&NewGlobals);
44 static SizeT RzArraySize = 0;
Jim Stichnoth 2016/06/10 22:38:33 I don't think this should be static?
tlively 2016/06/10 23:43:32 Done.
45
46 RzArray->setName(Ctx, nextRzName());
47 RzArraySizeVar->setName(Ctx, nextRzName());
48 NewGlobals.push_back(RzArray);
49 NewGlobals.push_back(RzArraySizeVar);
50
51 for (VariableDeclaration *Global : Globals) {
52 VariableDeclaration *RzLeft =
53 createRz(&NewGlobals, RzArray, RzArraySize, Global);
54 VariableDeclaration *RzRight =
55 createRz(&NewGlobals, RzArray, RzArraySize, Global);
56 NewGlobals.push_back(RzLeft);
57 NewGlobals.push_back(Global);
58 NewGlobals.push_back(RzRight);
59 }
60
61 // update the contents of the RzArraySize global
62 llvm::NaClBitcodeRecord::RecordVector SizeContents;
63 for (unsigned i = 0; i < sizeof(RzArraySize); i++) {
64 SizeContents.emplace_back(RzArraySize % (1 << CHAR_BIT));
65 RzArraySize >>= CHAR_BIT;
66 }
67 RzArraySizeVar->addInitializer(
68 VariableDeclaration::DataInitializer::create(&NewGlobals, SizeContents));
69
70 // Replace old list of globals, without messing up arena allocators
71 Globals.clear();
72 Globals.merge(&NewGlobals);
73 DidInsertRedZones = true;
74
75 // Log the new set of globals
76 if (BuildDefs::dump()) {
77 OstreamLocker _(Ctx);
78 Ctx->getStrDump() << "========= Instrumented Globals =========\n";
79 for (VariableDeclaration *Global : Globals) {
80 Global->dump(Ctx->getStrDump());
81 }
82 }
83 }
84
85 std::string ASanInstrumentation::nextRzName() {
86 std::stringstream Name;
Jim Stichnoth 2016/06/10 22:38:33 if (BuildDefs::minimal()) return "";
tlively 2016/06/10 23:43:32 Done.
87 Name << RzPrefix << RzNum++;
88 return Name.str();
89 }
90
91 VariableDeclaration *
92 ASanInstrumentation::createRz(VariableDeclarationList *List,
93 VariableDeclaration *RzArray, SizeT &RzArraySize,
94 VariableDeclaration *Global) {
95 VariableDeclaration *Rz = VariableDeclaration::create(List);
Jim Stichnoth 2016/06/10 22:38:33 if (BuildDefs::minimal()) return nullptr;
tlively 2016/06/10 23:43:32 Done.
96 Rz->setName(Ctx, nextRzName());
97 if (Global->hasNonzeroInitializer()) {
98 Rz->addInitializer(
99 VariableDeclaration::DataInitializer::create(List, RzContents));
100 } else {
101 Rz->addInitializer(
102 VariableDeclaration::ZeroInitializer::create(List, RzSize));
103 }
104 Rz->setIsConstant(Global->getIsConstant());
105 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create(
106 List, Rz, RelocOffsetArray(0)));
107 ++RzArraySize;
108 return Rz;
109 }
110
111 } // end of namespace Ice
OLDNEW

Powered by Google App Engine