1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Bitcode/BitstreamReader.h"
11 
12 using namespace llvm;
13 
14 //===----------------------------------------------------------------------===//
15 //  BitstreamCursor implementation
16 //===----------------------------------------------------------------------===//
17 
freeState()18 void BitstreamCursor::freeState() {
19   // Free all the Abbrevs.
20   CurAbbrevs.clear();
21 
22   // Free all the Abbrevs in the block scope.
23   BlockScope.clear();
24 }
25 
26 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27 /// the block, and return true if the block has an error.
EnterSubBlock(unsigned BlockID,unsigned * NumWordsP)28 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
29   // Save the current block's state on BlockScope.
30   BlockScope.push_back(Block(CurCodeSize));
31   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
32 
33   // Add the abbrevs specific to this block to the CurAbbrevs list.
34   if (const BitstreamReader::BlockInfo *Info =
35           getBitStreamReader()->getBlockInfo(BlockID)) {
36     CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
37                       Info->Abbrevs.end());
38   }
39 
40   // Get the codesize of this block.
41   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
42   // We can't read more than MaxChunkSize at a time
43   if (CurCodeSize > MaxChunkSize)
44     return true;
45 
46   SkipToFourByteBoundary();
47   unsigned NumWords = Read(bitc::BlockSizeWidth);
48   if (NumWordsP) *NumWordsP = NumWords;
49 
50   // Validate that this block is sane.
51   return CurCodeSize == 0 || AtEndOfStream();
52 }
53 
readAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)54 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
55                                      const BitCodeAbbrevOp &Op) {
56   assert(!Op.isLiteral() && "Not to be used with literals!");
57 
58   // Decode the value as we are commanded.
59   switch (Op.getEncoding()) {
60   case BitCodeAbbrevOp::Array:
61   case BitCodeAbbrevOp::Blob:
62     llvm_unreachable("Should not reach here");
63   case BitCodeAbbrevOp::Fixed:
64     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
65     return Cursor.Read((unsigned)Op.getEncodingData());
66   case BitCodeAbbrevOp::VBR:
67     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
68     return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
69   case BitCodeAbbrevOp::Char6:
70     return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
71   }
72   llvm_unreachable("invalid abbreviation encoding");
73 }
74 
skipAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)75 static void skipAbbreviatedField(BitstreamCursor &Cursor,
76                                  const BitCodeAbbrevOp &Op) {
77   assert(!Op.isLiteral() && "Not to be used with literals!");
78 
79   // Decode the value as we are commanded.
80   switch (Op.getEncoding()) {
81   case BitCodeAbbrevOp::Array:
82   case BitCodeAbbrevOp::Blob:
83     llvm_unreachable("Should not reach here");
84   case BitCodeAbbrevOp::Fixed:
85     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
86     Cursor.Read((unsigned)Op.getEncodingData());
87     break;
88   case BitCodeAbbrevOp::VBR:
89     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
90     Cursor.ReadVBR64((unsigned)Op.getEncodingData());
91     break;
92   case BitCodeAbbrevOp::Char6:
93     Cursor.Read(6);
94     break;
95   }
96 }
97 
98 
99 
100 /// skipRecord - Read the current record and discard it.
skipRecord(unsigned AbbrevID)101 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
102   // Skip unabbreviated records by reading past their entries.
103   if (AbbrevID == bitc::UNABBREV_RECORD) {
104     unsigned Code = ReadVBR(6);
105     (void)Code;
106     unsigned NumElts = ReadVBR(6);
107     for (unsigned i = 0; i != NumElts; ++i)
108       (void)ReadVBR64(6);
109     return;
110   }
111 
112   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
113 
114   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
115     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
116     if (Op.isLiteral())
117       continue;
118 
119     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
120         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
121       skipAbbreviatedField(*this, Op);
122       continue;
123     }
124 
125     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
126       // Array case.  Read the number of elements as a vbr6.
127       unsigned NumElts = ReadVBR(6);
128 
129       // Get the element encoding.
130       assert(i+2 == e && "array op not second to last?");
131       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
132 
133       // Read all the elements.
134       // Decode the value as we are commanded.
135       switch (EltEnc.getEncoding()) {
136       default:
137         report_fatal_error("Array element type can't be an Array or a Blob");
138       case BitCodeAbbrevOp::Fixed:
139         assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
140         for (; NumElts; --NumElts)
141           Read((unsigned)EltEnc.getEncodingData());
142         break;
143       case BitCodeAbbrevOp::VBR:
144         assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
145         for (; NumElts; --NumElts)
146           ReadVBR64((unsigned)EltEnc.getEncodingData());
147         break;
148       case BitCodeAbbrevOp::Char6:
149         for (; NumElts; --NumElts)
150           Read(6);
151         break;
152       }
153       continue;
154     }
155 
156     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
157     // Blob case.  Read the number of bytes as a vbr6.
158     unsigned NumElts = ReadVBR(6);
159     SkipToFourByteBoundary();  // 32-bit alignment
160 
161     // Figure out where the end of this blob will be including tail padding.
162     size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
163 
164     // If this would read off the end of the bitcode file, just set the
165     // record to empty and return.
166     if (!canSkipToPos(NewEnd/8)) {
167       skipToEnd();
168       break;
169     }
170 
171     // Skip over the blob.
172     JumpToBit(NewEnd);
173   }
174 }
175 
readRecord(unsigned AbbrevID,SmallVectorImpl<uint64_t> & Vals,StringRef * Blob)176 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
177                                      SmallVectorImpl<uint64_t> &Vals,
178                                      StringRef *Blob) {
179   if (AbbrevID == bitc::UNABBREV_RECORD) {
180     unsigned Code = ReadVBR(6);
181     unsigned NumElts = ReadVBR(6);
182     for (unsigned i = 0; i != NumElts; ++i)
183       Vals.push_back(ReadVBR64(6));
184     return Code;
185   }
186 
187   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
188 
189   // Read the record code first.
190   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
191   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
192   unsigned Code;
193   if (CodeOp.isLiteral())
194     Code = CodeOp.getLiteralValue();
195   else {
196     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
197         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
198       report_fatal_error("Abbreviation starts with an Array or a Blob");
199     Code = readAbbreviatedField(*this, CodeOp);
200   }
201 
202   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
203     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
204     if (Op.isLiteral()) {
205       Vals.push_back(Op.getLiteralValue());
206       continue;
207     }
208 
209     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
210         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
211       Vals.push_back(readAbbreviatedField(*this, Op));
212       continue;
213     }
214 
215     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
216       // Array case.  Read the number of elements as a vbr6.
217       unsigned NumElts = ReadVBR(6);
218 
219       // Get the element encoding.
220       if (i + 2 != e)
221         report_fatal_error("Array op not second to last");
222       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
223       if (!EltEnc.isEncoding())
224         report_fatal_error(
225             "Array element type has to be an encoding of a type");
226 
227       // Read all the elements.
228       switch (EltEnc.getEncoding()) {
229       default:
230         report_fatal_error("Array element type can't be an Array or a Blob");
231       case BitCodeAbbrevOp::Fixed:
232         for (; NumElts; --NumElts)
233           Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
234         break;
235       case BitCodeAbbrevOp::VBR:
236         for (; NumElts; --NumElts)
237           Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
238         break;
239       case BitCodeAbbrevOp::Char6:
240         for (; NumElts; --NumElts)
241           Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
242       }
243       continue;
244     }
245 
246     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
247     // Blob case.  Read the number of bytes as a vbr6.
248     unsigned NumElts = ReadVBR(6);
249     SkipToFourByteBoundary();  // 32-bit alignment
250 
251     // Figure out where the end of this blob will be including tail padding.
252     size_t CurBitPos = GetCurrentBitNo();
253     size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
254 
255     // If this would read off the end of the bitcode file, just set the
256     // record to empty and return.
257     if (!canSkipToPos(NewEnd/8)) {
258       Vals.append(NumElts, 0);
259       skipToEnd();
260       break;
261     }
262 
263     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
264     // over tail padding first, in case jumping to NewEnd invalidates the Blob
265     // pointer.
266     JumpToBit(NewEnd);
267     const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
268 
269     // If we can return a reference to the data, do so to avoid copying it.
270     if (Blob) {
271       *Blob = StringRef(Ptr, NumElts);
272     } else {
273       // Otherwise, unpack into Vals with zero extension.
274       for (; NumElts; --NumElts)
275         Vals.push_back((unsigned char)*Ptr++);
276     }
277   }
278 
279   return Code;
280 }
281 
282 
ReadAbbrevRecord()283 void BitstreamCursor::ReadAbbrevRecord() {
284   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
285   unsigned NumOpInfo = ReadVBR(5);
286   for (unsigned i = 0; i != NumOpInfo; ++i) {
287     bool IsLiteral = Read(1);
288     if (IsLiteral) {
289       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
290       continue;
291     }
292 
293     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
294     if (BitCodeAbbrevOp::hasEncodingData(E)) {
295       uint64_t Data = ReadVBR64(5);
296 
297       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
298       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
299       // a slow path in Read() to have to handle reading zero bits.
300       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
301           Data == 0) {
302         Abbv->Add(BitCodeAbbrevOp(0));
303         continue;
304       }
305 
306       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
307           Data > MaxChunkSize)
308         report_fatal_error(
309             "Fixed or VBR abbrev record with size > MaxChunkData");
310 
311       Abbv->Add(BitCodeAbbrevOp(E, Data));
312     } else
313       Abbv->Add(BitCodeAbbrevOp(E));
314   }
315 
316   if (Abbv->getNumOperandInfos() == 0)
317     report_fatal_error("Abbrev record with no operands");
318   CurAbbrevs.push_back(Abbv);
319 }
320 
ReadBlockInfoBlock()321 bool BitstreamCursor::ReadBlockInfoBlock() {
322   // If this is the second stream to get to the block info block, skip it.
323   if (getBitStreamReader()->hasBlockInfoRecords())
324     return SkipBlock();
325 
326   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
327 
328   SmallVector<uint64_t, 64> Record;
329   BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
330 
331   // Read all the records for this module.
332   while (1) {
333     BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
334 
335     switch (Entry.Kind) {
336     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
337     case llvm::BitstreamEntry::Error:
338       return true;
339     case llvm::BitstreamEntry::EndBlock:
340       return false;
341     case llvm::BitstreamEntry::Record:
342       // The interesting case.
343       break;
344     }
345 
346     // Read abbrev records, associate them with CurBID.
347     if (Entry.ID == bitc::DEFINE_ABBREV) {
348       if (!CurBlockInfo) return true;
349       ReadAbbrevRecord();
350 
351       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
352       // appropriate BlockInfo.
353       CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
354       CurAbbrevs.pop_back();
355       continue;
356     }
357 
358     // Read a record.
359     Record.clear();
360     switch (readRecord(Entry.ID, Record)) {
361       default: break;  // Default behavior, ignore unknown content.
362       case bitc::BLOCKINFO_CODE_SETBID:
363         if (Record.size() < 1) return true;
364         CurBlockInfo =
365             &getBitStreamReader()->getOrCreateBlockInfo((unsigned)Record[0]);
366         break;
367       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
368         if (!CurBlockInfo) return true;
369         if (getBitStreamReader()->isIgnoringBlockInfoNames())
370           break; // Ignore name.
371         std::string Name;
372         for (unsigned i = 0, e = Record.size(); i != e; ++i)
373           Name += (char)Record[i];
374         CurBlockInfo->Name = Name;
375         break;
376       }
377       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
378         if (!CurBlockInfo) return true;
379         if (getBitStreamReader()->isIgnoringBlockInfoNames())
380           break; // Ignore name.
381         std::string Name;
382         for (unsigned i = 1, e = Record.size(); i != e; ++i)
383           Name += (char)Record[i];
384         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
385                                                            Name));
386         break;
387       }
388     }
389   }
390 }
391 
392