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