Coverage Report

Created: 2017-10-03 07:32

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