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

Side by Side Diff: lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp

Issue 932953002: Fix the NaCl bitstream reader to report fatal errors. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Merge with master Created 5 years, 9 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 //===- NaClBitstreamReader.cpp --------------------------------------------===// 1 //===- NaClBitstreamReader.cpp --------------------------------------------===//
2 // NaClBitstreamReader implementation 2 // NaClBitstreamReader implementation
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
11 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" 11 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h"
12 #include "llvm/Support/Format.h"
12 #include "llvm/Support/raw_ostream.h" 13 #include "llvm/Support/raw_ostream.h"
13 14
14 using namespace llvm; 15 using namespace llvm;
15 16
17 std::string NaClBitstreamReader::getBitAddress(uint64_t Bit,
18 unsigned MinByteWidth) {
19 std::string Buffer;
20 raw_string_ostream Stream(Buffer);
21 Stream << '%' << MinByteWidth << PRIu64 << ":%u";
22 Stream.flush();
23 std::string FormatString(Buffer);
24 Buffer.clear();
25 Stream << format(FormatString.c_str(),
26 (Bit / 8),
27 static_cast<unsigned>(Bit % 8));
28 return Stream.str();
29 }
30
16 //===----------------------------------------------------------------------===// 31 //===----------------------------------------------------------------------===//
17 // NaClBitstreamCursor implementation 32 // NaClBitstreamCursor implementation
18 //===----------------------------------------------------------------------===// 33 //===----------------------------------------------------------------------===//
19 34
35 void NaClBitstreamCursor::ErrorHandler::
36 Fatal(const std::string &ErrorMessage) const {
37 // Default implementation is simply print message, and the bit where
38 // the error occurred.
39 std::string Buffer;
40 raw_string_ostream StrBuf(Buffer);
41 StrBuf << "Error("
42 << NaClBitstreamReader::getBitAddress(Cursor.GetCurrentBitNo())
43 << "): " << ErrorMessage;
44 report_fatal_error(StrBuf.str());
45 }
46
20 void NaClBitstreamCursor::freeState() { 47 void NaClBitstreamCursor::freeState() {
21 // Free all the Abbrevs. 48 // Free all the Abbrevs.
22 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) 49 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
23 CurAbbrevs[i]->dropRef(); 50 CurAbbrevs[i]->dropRef();
24 CurAbbrevs.clear(); 51 CurAbbrevs.clear();
25 52
26 // Free all the Abbrevs in the block scope. 53 // Free all the Abbrevs in the block scope.
27 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) { 54 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
28 std::vector<NaClBitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs; 55 std::vector<NaClBitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
29 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i) 56 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
30 Abbrevs[i]->dropRef(); 57 Abbrevs[i]->dropRef();
31 } 58 }
32 BlockScope.clear(); 59 BlockScope.clear();
33 } 60 }
34 61
62 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const {
63 std::string Buffer;
64 raw_string_ostream StrBuf(Buffer);
65 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record";
66 ErrHandler->Fatal(StrBuf.str());
67 }
68
69 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const {
70 std::string Buffer;
71 raw_string_ostream StrBuf(Buffer);
72 StrBuf << "Invalid jump to bit " << BitNo;
73 ErrHandler->Fatal(StrBuf.str());
74 }
75
35 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter 76 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
36 /// the block, and return true if the block has an error. 77 /// the block, and return true if the block has an error.
37 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { 78 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
38 // Save the current block's state on BlockScope. 79 // Save the current block's state on BlockScope.
39 BlockScope.push_back(Block(CurCodeSize)); 80 BlockScope.push_back(Block(CurCodeSize));
40 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); 81 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
41 82
42 // Add the abbrevs specific to this block to the CurAbbrevs list. 83 // Add the abbrevs specific to this block to the CurAbbrevs list.
43 if (const NaClBitstreamReader::BlockInfo *Info = 84 if (const NaClBitstreamReader::BlockInfo *Info =
44 BitStream->getBlockInfo(BlockID)) { 85 BitStream->getBlockInfo(BlockID)) {
(...skipping 14 matching lines...) Expand all
59 if (CurCodeSize.NumBits == 0 || AtEndOfStream()) 100 if (CurCodeSize.NumBits == 0 || AtEndOfStream())
60 return true; 101 return true;
61 102
62 return false; 103 return false;
63 } 104 }
64 105
65 void NaClBitstreamCursor::skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op) { 106 void NaClBitstreamCursor::skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op) {
66 // Decode the value as we are commanded. 107 // Decode the value as we are commanded.
67 switch (Op.getEncoding()) { 108 switch (Op.getEncoding()) {
68 case NaClBitCodeAbbrevOp::Literal: 109 case NaClBitCodeAbbrevOp::Literal:
69 report_fatal_error("Not to be used with literals!"); 110 llvm_unreachable("Not to be used with literals!");
111 break;
70 case NaClBitCodeAbbrevOp::Fixed: 112 case NaClBitCodeAbbrevOp::Fixed:
71 (void)Read((unsigned)Op.getValue()); 113 (void)Read((unsigned)Op.getValue());
72 break; 114 break;
73 case NaClBitCodeAbbrevOp::VBR: 115 case NaClBitCodeAbbrevOp::VBR:
74 (void)ReadVBR64((unsigned)Op.getValue()); 116 (void)ReadVBR64((unsigned)Op.getValue());
75 break; 117 break;
76 case NaClBitCodeAbbrevOp::Array: 118 case NaClBitCodeAbbrevOp::Array:
77 report_fatal_error("Bad array abbreviation encoding!"); 119 llvm_unreachable("Bad array abbreviation encoding!");
120 break;
78 case NaClBitCodeAbbrevOp::Char6: 121 case NaClBitCodeAbbrevOp::Char6:
79 (void)Read(6); 122 (void)Read(6);
80 break; 123 break;
81 } 124 }
82 } 125 }
83 126
84 /// skipRecord - Read the current record and discard it. 127 /// skipRecord - Read the current record and discard it.
85 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) { 128 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) {
86 // Skip unabbreviated records by reading past their entries. 129 // Skip unabbreviated records by reading past their entries.
87 if (AbbrevID == naclbitc::UNABBREV_RECORD) { 130 if (AbbrevID == naclbitc::UNABBREV_RECORD) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 break; 183 break;
141 } 184 }
142 return false; 185 return false;
143 } 186 }
144 187
145 uint64_t NaClBitstreamCursor::readArrayAbbreviatedField( 188 uint64_t NaClBitstreamCursor::readArrayAbbreviatedField(
146 const NaClBitCodeAbbrevOp &Op) { 189 const NaClBitCodeAbbrevOp &Op) {
147 // Decode the value as we are commanded. 190 // Decode the value as we are commanded.
148 switch (Op.getEncoding()) { 191 switch (Op.getEncoding()) {
149 case NaClBitCodeAbbrevOp::Literal: 192 case NaClBitCodeAbbrevOp::Literal:
150 report_fatal_error("Not to be used with literals!"); 193 llvm_unreachable("Not to be used with literals!");
194 break;
151 case NaClBitCodeAbbrevOp::Fixed: 195 case NaClBitCodeAbbrevOp::Fixed:
152 return Read((unsigned)Op.getValue()); 196 return Read((unsigned)Op.getValue());
153 case NaClBitCodeAbbrevOp::VBR: 197 case NaClBitCodeAbbrevOp::VBR:
154 return ReadVBR64((unsigned)Op.getValue()); 198 return ReadVBR64((unsigned)Op.getValue());
155 case NaClBitCodeAbbrevOp::Array: 199 case NaClBitCodeAbbrevOp::Array:
156 report_fatal_error("Bad array abbreviation encoding!"); 200 llvm_unreachable("Bad array abbreviation encoding!");
201 break;
157 case NaClBitCodeAbbrevOp::Char6: 202 case NaClBitCodeAbbrevOp::Char6:
158 return NaClBitCodeAbbrevOp::DecodeChar6(Read(6)); 203 return NaClBitCodeAbbrevOp::DecodeChar6(Read(6));
159 } 204 }
160 llvm_unreachable("unhandled NaClBitCodeAbbrevOp encoding"); 205 llvm_unreachable("unhandled NaClBitCodeAbbrevOp encoding");
161 } 206 }
162 207
163 void NaClBitstreamCursor::readArrayAbbrev( 208 void NaClBitstreamCursor::readArrayAbbrev(
164 const NaClBitCodeAbbrevOp &Op, unsigned NumArrayElements, 209 const NaClBitCodeAbbrevOp &Op, unsigned NumArrayElements,
165 SmallVectorImpl<uint64_t> &Vals) { 210 SmallVectorImpl<uint64_t> &Vals) {
166 for (; NumArrayElements; --NumArrayElements) { 211 for (; NumArrayElements; --NumArrayElements) {
167 Vals.push_back(readArrayAbbreviatedField(Op)); 212 Vals.push_back(readArrayAbbreviatedField(Op));
168 } 213 }
169 } 214 }
170 215
171 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID, 216 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID,
172 SmallVectorImpl<uint64_t> &Vals) { 217 SmallVectorImpl<uint64_t> &Vals) {
173 if (AbbrevID == naclbitc::UNABBREV_RECORD) { 218 if (AbbrevID == naclbitc::UNABBREV_RECORD) {
174 unsigned Code = ReadVBR(6); 219 unsigned Code = ReadVBR(6);
175 unsigned NumElts = ReadVBR(6); 220 unsigned NumElts = ReadVBR(6);
176 for (unsigned i = 0; i != NumElts; ++i) 221 for (unsigned i = 0; i != NumElts; ++i)
177 Vals.push_back(ReadVBR64(6)); 222 Vals.push_back(ReadVBR64(6));
178 return Code; 223 return Code;
179 } 224 }
180 225
226 // Read code.
181 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 227 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
182 unsigned NumOperands = Abbv->getNumOperandInfos();
183 assert(NumOperands > 0 && "Too few operands for abbreviation!");
184
185 uint64_t Value; 228 uint64_t Value;
186
187 // Read code.
188 unsigned Code; 229 unsigned Code;
189 if (readRecordAbbrevField(Abbv->getOperandInfo(0), Value)) { 230 if (readRecordAbbrevField(Abbv->getOperandInfo(0), Value)) {
190 // Array found, use to read all elements. 231 // Array found, use to read all elements.
191 assert(Value > 0 && "No code found for record!"); 232 if (Value == 0)
233 ErrHandler->Fatal("No code found for record!");
192 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(1); 234 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(1);
193 Code = readArrayAbbreviatedField(Op); 235 Code = readArrayAbbreviatedField(Op);
194 readArrayAbbrev(Op, Value - 1, Vals); 236 readArrayAbbrev(Op, Value - 1, Vals);
195 return Code; 237 return Code;
196 } 238 }
197 Code = Value; 239 Code = Value;
198 240
199 // Read arguments. 241 // Read arguments.
242 unsigned NumOperands = Abbv->getNumOperandInfos();
200 for (unsigned i = 1; i != NumOperands; ++i) { 243 for (unsigned i = 1; i != NumOperands; ++i) {
201 if (readRecordAbbrevField(Abbv->getOperandInfo(i), Value)) { 244 if (readRecordAbbrevField(Abbv->getOperandInfo(i), Value)) {
202 ++i; 245 ++i;
203 readArrayAbbrev(Abbv->getOperandInfo(i), Value, Vals); 246 readArrayAbbrev(Abbv->getOperandInfo(i), Value, Vals);
204 return Code; 247 return Code;
205 } 248 }
206 Vals.push_back(Value); 249 Vals.push_back(Value);
207 } 250 }
208 return Code; 251 return Code;
209 } 252 }
210 253
211 254
212 namespace { 255 NaClBitCodeAbbrevOp::Encoding NaClBitstreamCursor::
213 256 getEncoding(uint64_t Value) {
214 static NaClBitCodeAbbrevOp::Encoding getEncoding(uint64_t Encoding) { 257 if (!NaClBitCodeAbbrevOp::isValidEncoding(Value)) {
215 if (!NaClBitCodeAbbrevOp::isValidEncoding(Encoding)) {
216 std::string Buffer; 258 std::string Buffer;
217 raw_string_ostream StrBuf(Buffer); 259 raw_string_ostream StrBuf(Buffer);
218 StrBuf << "Invalid abbreviation encoding " << Encoding 260 StrBuf << "Invalid abbreviation encoding specified in bitcode file: "
219 << "specified in bitcode file"; 261 << Value;
220 report_fatal_error(StrBuf.str()); 262 ErrHandler->Fatal(StrBuf.str());
221 } 263 }
222 return NaClBitCodeAbbrevOp::Encoding(Encoding); 264 return NaClBitCodeAbbrevOp::Encoding(Value);
223 } 265 }
224 266
225 } // end of anonymous space.
226
227 void NaClBitstreamCursor::ReadAbbrevRecord(bool IsLocal, 267 void NaClBitstreamCursor::ReadAbbrevRecord(bool IsLocal,
228 NaClAbbrevListener *Listener) { 268 NaClAbbrevListener *Listener) {
229 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 269 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
230 unsigned NumOpInfo = ReadVBR(5); 270 unsigned NumOpInfo = ReadVBR(5);
231 if (Listener) Listener->Values.push_back(NumOpInfo); 271 if (Listener) Listener->Values.push_back(NumOpInfo);
232 for (unsigned i = 0; i != NumOpInfo; ++i) { 272 for (unsigned i = 0; i != NumOpInfo; ++i) {
233 bool IsLiteral = Read(1) ? true : false; 273 bool IsLiteral = Read(1) ? true : false;
234 if (Listener) Listener->Values.push_back(IsLiteral); 274 if (Listener) Listener->Values.push_back(IsLiteral);
235 if (IsLiteral) { 275 if (IsLiteral) {
236 uint64_t Value = ReadVBR64(8); 276 uint64_t Value = ReadVBR64(8);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 if (Record.size() < 1) return true; 392 if (Record.size() < 1) return true;
353 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); 393 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
354 if (Listener) { 394 if (Listener) {
355 Listener->Values = Record; 395 Listener->Values = Record;
356 Listener->SetBID(); 396 Listener->SetBID();
357 } 397 }
358 break; 398 break;
359 } 399 }
360 } 400 }
361 } 401 }
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp ('k') | lib/Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698