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

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

Powered by Google App Engine
This is Rietveld 408576698