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

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: 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
(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";
Karl 2016/06/09 21:20:56 Why not prefix with "$" so that it won't conflict
Jim Stichnoth 2016/06/09 22:27:38 Or, __rz or __$rz because "__" prefix is reserved
tlively 2016/06/09 23:30:10 Done.
25
26 // Create redzones between all global variables, ensuring that the initializer
27 // types of the redzones and their associated globals match so that they are
28 // laid out together in memory.
29 // TODO(tlively): make everything here thread safe, including dumps
30 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) {
31 if (didInsertRedZones)
Jim Stichnoth 2016/06/09 22:27:39 I'm not sure how this code ends up getting invoked
tlively 2016/06/09 23:30:10 Done.
32 return;
33
34 VariableDeclarationList NewGlobals;
35 // Global holding pointers to all redzones
36 VariableDeclaration *RzArray;
37 // Global holding the size of RzArray
38 VariableDeclaration *RzArraySizeVar;
39 static SizeT RzArraySize;
40
41 // used to construct new redzones
42 uint32_t RzNum = 0;
43 auto NextRzName = [&] {
Karl 2016/06/09 21:20:56 Why not make this a private method of the class, o
tlively 2016/06/09 23:30:10 Done.
44 std::stringstream Name;
45 Name << RzPrefix << "." << RzNum++;
46 return Name.str();
47 };
48
49 auto CreateRz = [&](VariableDeclaration *Global) {
Karl 2016/06/09 21:20:56 Why not make this a private method of the class?
tlively 2016/06/09 23:30:10 Done.
50 VariableDeclaration *Rz = VariableDeclaration::create(&NewGlobals);
51 Rz->setName(Ctx, NextRzName());
52 if (Global->hasNonzeroInitializer()) {
53 Rz->addInitializer(
Karl 2016/06/09 21:20:56 Question? If all initializers are zero initialiali
tlively 2016/06/09 23:30:10 Done.
54 VariableDeclaration::ZeroInitializer::create(&NewGlobals, RzSize-1));
55 Rz->addInitializer(
56 VariableDeclaration::ZeroInitializer::create(&NewGlobals, 1));
57 } else {
58 Rz->addInitializer(
59 VariableDeclaration::ZeroInitializer::create(
60 &NewGlobals, RzSize));
61 }
62 Rz->setIsConstant(Global->getIsConstant());
63 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create(
64 &NewGlobals, Rz, RelocOffsetArray(0)));
65 ++RzArraySize;
66 return Rz;
67 };
68
69 RzArray = VariableDeclaration::create(&NewGlobals);
70 RzArraySizeVar = VariableDeclaration::create(&NewGlobals);
71 RzArray->setName(Ctx, NextRzName());
72 RzArraySizeVar->setName(Ctx, NextRzName());
73 NewGlobals.push_back(RzArray);
Jim Stichnoth 2016/06/09 22:27:38 I believe emplace_back is the newer improveder way
tlively 2016/06/09 23:30:10 VariableDeclarationList is a wrapper that only exp
74 NewGlobals.push_back(RzArraySizeVar);
75
76 // TODO(tlively): Consider alignment when determining redzone layout.
77 for (VariableDeclaration *Global : Globals) {
78 VariableDeclaration *RzLeft = CreateRz(Global);
79 VariableDeclaration *RzRight = CreateRz(Global);
80 NewGlobals.push_back(RzLeft);
81 NewGlobals.push_back(Global);
82 NewGlobals.push_back(RzRight);
83 }
84
85 // update the contents of the RzArraySize global (as a 4-byte little endian in t)
Jim Stichnoth 2016/06/09 22:27:39 80-col here and elsewhere
tlively 2016/06/09 23:30:10 Done.
86 llvm::NaClBitcodeRecord::RecordVector SizeContents;
87 for (int i = 0; i < 4; i++) {
Karl 2016/06/09 21:20:56 Use sizeof(uint32) instead of 4. This will give yo
tlively 2016/06/09 23:30:10 Done.
88 SizeContents.push_back(RzArraySize % (1 << 8));
Karl 2016/06/09 21:20:56 Use CHAR_BIT for the number of bits in a character
tlively 2016/06/09 23:30:09 Done.
89 RzArraySize /= (1 << 8);
Karl 2016/06/09 21:20:56 Since RzArraySize is unsigned (or it should be if
tlively 2016/06/09 23:30:10 Done.
90 }
91 assert(RzArraySize == 0 && "There were too many globals for ASan to instrument ");
Karl 2016/06/09 21:20:56 This line is too long. In general, you should "re
tlively 2016/06/09 23:30:10 Done.
92 RzArraySizeVar->addInitializer(VariableDeclaration::DataInitializer::create(
93 &NewGlobals, SizeContents));
94
95 // Replace old list of globals
Karl 2016/06/09 21:20:56 An more efficient solution for the next two statem
tlively 2016/06/09 23:30:10 Not possible due to VariableDeclarationList being
96 Globals.clear();
97 Globals.merge(&NewGlobals);
98 didInsertRedZones = true;
99
100 // Log the new set of globals
101 if (BuildDefs::dump()) {
102 Ctx->getStrDump() << "========= Instrumented Globals =========\n";
Jim Stichnoth 2016/06/09 22:27:38 You should "lock" the dump stream so that this out
tlively 2016/06/09 23:30:10 Done.
103 for (VariableDeclaration *Global : Globals) {
104 Global->dump(Ctx->getStrDump());
105 }
106 }
107 }
108
109 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698