Bug Summary

File:include/llvm/Bitcode/BitstreamReader.h
Warning:line 210, column 39
The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name BitstreamReader.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-config-compatibility-mode=true -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn348900/build-llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-8~svn348900/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-8~svn348900/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn348900/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn348900/build-llvm/lib/Bitcode/Reader -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-12-12-042652-12204-1 -x c++ /build/llvm-toolchain-snapshot-8~svn348900/lib/Bitcode/Reader/BitstreamReader.cpp -faddrsig

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

/build/llvm-toolchain-snapshot-8~svn348900/include/llvm/Bitcode/BitstreamReader.h

1//===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
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// This header defines the BitstreamReader class. This class can be used to
11// read an arbitrary bitstream, regardless of its contents.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_BITCODE_BITSTREAMREADER_H
16#define LLVM_BITCODE_BITSTREAMREADER_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Bitcode/BitCodes.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/MathExtras.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include <algorithm>
26#include <cassert>
27#include <climits>
28#include <cstddef>
29#include <cstdint>
30#include <memory>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace llvm {
36
37/// This class maintains the abbreviations read from a block info block.
38class BitstreamBlockInfo {
39public:
40 /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
41 /// describe abbreviations that all blocks of the specified ID inherit.
42 struct BlockInfo {
43 unsigned BlockID;
44 std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
45 std::string Name;
46 std::vector<std::pair<unsigned, std::string>> RecordNames;
47 };
48
49private:
50 std::vector<BlockInfo> BlockInfoRecords;
51
52public:
53 /// If there is block info for the specified ID, return it, otherwise return
54 /// null.
55 const BlockInfo *getBlockInfo(unsigned BlockID) const {
56 // Common case, the most recent entry matches BlockID.
57 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
58 return &BlockInfoRecords.back();
59
60 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
61 i != e; ++i)
62 if (BlockInfoRecords[i].BlockID == BlockID)
63 return &BlockInfoRecords[i];
64 return nullptr;
65 }
66
67 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
68 if (const BlockInfo *BI = getBlockInfo(BlockID))
69 return *const_cast<BlockInfo*>(BI);
70
71 // Otherwise, add a new record.
72 BlockInfoRecords.emplace_back();
73 BlockInfoRecords.back().BlockID = BlockID;
74 return BlockInfoRecords.back();
75 }
76};
77
78/// This represents a position within a bitstream. There may be multiple
79/// independent cursors reading within one bitstream, each maintaining their
80/// own local state.
81class SimpleBitstreamCursor {
82 ArrayRef<uint8_t> BitcodeBytes;
83 size_t NextChar = 0;
84
85public:
86 /// This is the current data we have pulled from the stream but have not
87 /// returned to the client. This is specifically and intentionally defined to
88 /// follow the word size of the host machine for efficiency. We use word_t in
89 /// places that are aware of this to make it perfectly explicit what is going
90 /// on.
91 using word_t = size_t;
92
93private:
94 word_t CurWord = 0;
95
96 /// This is the number of bits in CurWord that are valid. This is always from
97 /// [0...bits_of(size_t)-1] inclusive.
98 unsigned BitsInCurWord = 0;
99
100public:
101 static const size_t MaxChunkSize = sizeof(word_t) * 8;
102
103 SimpleBitstreamCursor() = default;
104 explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
105 : BitcodeBytes(BitcodeBytes) {}
106 explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
107 : BitcodeBytes(reinterpret_cast<const uint8_t *>(BitcodeBytes.data()),
108 BitcodeBytes.size()) {}
109 explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
110 : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
111
112 bool canSkipToPos(size_t pos) const {
113 // pos can be skipped to if it is a valid address or one byte past the end.
114 return pos <= BitcodeBytes.size();
115 }
116
117 bool AtEndOfStream() {
118 return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
119 }
120
121 /// Return the bit # of the bit we are reading.
122 uint64_t GetCurrentBitNo() const {
123 return NextChar*CHAR_BIT8 - BitsInCurWord;
124 }
125
126 // Return the byte # of the current bit.
127 uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
128
129 ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
130
131 /// Reset the stream to the specified bit number.
132 void JumpToBit(uint64_t BitNo) {
133 size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
134 unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
135 assert(canSkipToPos(ByteNo) && "Invalid location")((canSkipToPos(ByteNo) && "Invalid location") ? static_cast
<void> (0) : __assert_fail ("canSkipToPos(ByteNo) && \"Invalid location\""
, "/build/llvm-toolchain-snapshot-8~svn348900/include/llvm/Bitcode/BitstreamReader.h"
, 135, __PRETTY_FUNCTION__))
;
136
137 // Move the cursor to the right word.
138 NextChar = ByteNo;
139 BitsInCurWord = 0;
140
141 // Skip over any bits that are already consumed.
142 if (WordBitNo)
143 Read(WordBitNo);
144 }
145
146 /// Get a pointer into the bitstream at the specified byte offset.
147 const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
148 return BitcodeBytes.data() + ByteNo;
149 }
150
151 /// Get a pointer into the bitstream at the specified bit offset.
152 ///
153 /// The bit offset must be on a byte boundary.
154 const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
155 assert(!(BitNo % 8) && "Expected bit on byte boundary")((!(BitNo % 8) && "Expected bit on byte boundary") ? static_cast
<void> (0) : __assert_fail ("!(BitNo % 8) && \"Expected bit on byte boundary\""
, "/build/llvm-toolchain-snapshot-8~svn348900/include/llvm/Bitcode/BitstreamReader.h"
, 155, __PRETTY_FUNCTION__))
;
156 return getPointerToByte(BitNo / 8, NumBytes);
157 }
158
159 void fillCurWord() {
160 if (NextChar >= BitcodeBytes.size())
161 report_fatal_error("Unexpected end of file");
162
163 // Read the next word from the stream.
164 const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
165 unsigned BytesRead;
166 if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
167 BytesRead = sizeof(word_t);
168 CurWord =
169 support::endian::read<word_t, support::little, support::unaligned>(
170 NextCharPtr);
171 } else {
172 // Short read.
173 BytesRead = BitcodeBytes.size() - NextChar;
174 CurWord = 0;
175 for (unsigned B = 0; B != BytesRead; ++B)
176 CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
177 }
178 NextChar += BytesRead;
179 BitsInCurWord = BytesRead * 8;
180 }
181
182 word_t Read(unsigned NumBits) {
183 static const unsigned BitsInWord = MaxChunkSize;
184
185 assert(NumBits && NumBits <= BitsInWord &&((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!"
) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\""
, "/build/llvm-toolchain-snapshot-8~svn348900/include/llvm/Bitcode/BitstreamReader.h"
, 186, __PRETTY_FUNCTION__))
6
'?' condition is true
186 "Cannot return zero or more than BitsInWord bits!")((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!"
) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\""
, "/build/llvm-toolchain-snapshot-8~svn348900/include/llvm/Bitcode/BitstreamReader.h"
, 186, __PRETTY_FUNCTION__))
;
187
188 static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
7
'?' condition is true
189
190 // If the field is fully contained by CurWord, return it quickly.
191 if (BitsInCurWord >= NumBits) {
8
Assuming the condition is false
9
Taking false branch
192 word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
193
194 // Use a mask to avoid undefined behavior.
195 CurWord >>= (NumBits & Mask);
196
197 BitsInCurWord -= NumBits;
198 return R;
199 }
200
201 word_t R = BitsInCurWord ? CurWord : 0;
10
Assuming the condition is true
11
'?' condition is true
202 unsigned BitsLeft = NumBits - BitsInCurWord;
203
204 fillCurWord();
205
206 // If we run out of data, abort.
207 if (BitsLeft > BitsInCurWord)
12
Assuming the condition is false
13
Taking false branch
208 report_fatal_error("Unexpected end of file");
209
210 word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
14
The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t'
211
212 // Use a mask to avoid undefined behavior.
213 CurWord >>= (BitsLeft & Mask);
214
215 BitsInCurWord -= BitsLeft;
216
217 R |= R2 << (NumBits - BitsLeft);
218
219 return R;
220 }
221
222 uint32_t ReadVBR(unsigned NumBits) {
223 uint32_t Piece = Read(NumBits);
5
Calling 'SimpleBitstreamCursor::Read'
224 if ((Piece & (1U << (NumBits-1))) == 0)
225 return Piece;
226
227 uint32_t Result = 0;
228 unsigned NextBit = 0;
229 while (true) {
230 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
231
232 if ((Piece & (1U << (NumBits-1))) == 0)
233 return Result;
234
235 NextBit += NumBits-1;
236 Piece = Read(NumBits);
237 }
238 }
239
240 // Read a VBR that may have a value up to 64-bits in size. The chunk size of
241 // the VBR must still be <= 32 bits though.
242 uint64_t ReadVBR64(unsigned NumBits) {
243 uint32_t Piece = Read(NumBits);
244 if ((Piece & (1U << (NumBits-1))) == 0)
245 return uint64_t(Piece);
246
247 uint64_t Result = 0;
248 unsigned NextBit = 0;
249 while (true) {
250 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
251
252 if ((Piece & (1U << (NumBits-1))) == 0)
253 return Result;
254
255 NextBit += NumBits-1;
256 Piece = Read(NumBits);
257 }
258 }
259
260 void SkipToFourByteBoundary() {
261 // If word_t is 64-bits and if we've read less than 32 bits, just dump
262 // the bits we have up to the next 32-bit boundary.
263 if (sizeof(word_t) > 4 &&
264 BitsInCurWord >= 32) {
265 CurWord >>= BitsInCurWord-32;
266 BitsInCurWord = 32;
267 return;
268 }
269
270 BitsInCurWord = 0;
271 }
272
273 /// Skip to the end of the file.
274 void skipToEnd() { NextChar = BitcodeBytes.size(); }
275};
276
277/// When advancing through a bitstream cursor, each advance can discover a few
278/// different kinds of entries:
279struct BitstreamEntry {
280 enum {
281 Error, // Malformed bitcode was found.
282 EndBlock, // We've reached the end of the current block, (or the end of the
283 // file, which is treated like a series of EndBlock records.
284 SubBlock, // This is the start of a new subblock of a specific ID.
285 Record // This is a record with a specific AbbrevID.
286 } Kind;
287
288 unsigned ID;
289
290 static BitstreamEntry getError() {
291 BitstreamEntry E; E.Kind = Error; return E;
292 }
293
294 static BitstreamEntry getEndBlock() {
295 BitstreamEntry E; E.Kind = EndBlock; return E;
296 }
297
298 static BitstreamEntry getSubBlock(unsigned ID) {
299 BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
300 }
301
302 static BitstreamEntry getRecord(unsigned AbbrevID) {
303 BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
304 }
305};
306
307/// This represents a position within a bitcode file, implemented on top of a
308/// SimpleBitstreamCursor.
309///
310/// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
311/// be passed by value.
312class BitstreamCursor : SimpleBitstreamCursor {
313 // This is the declared size of code values used for the current block, in
314 // bits.
315 unsigned CurCodeSize = 2;
316
317 /// Abbrevs installed at in this block.
318 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
319
320 struct Block {
321 unsigned PrevCodeSize;
322 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
323
324 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
325 };
326
327 /// This tracks the codesize of parent blocks.
328 SmallVector<Block, 8> BlockScope;
329
330 BitstreamBlockInfo *BlockInfo = nullptr;
331
332public:
333 static const size_t MaxChunkSize = sizeof(word_t) * 8;
334
335 BitstreamCursor() = default;
336 explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
337 : SimpleBitstreamCursor(BitcodeBytes) {}
338 explicit BitstreamCursor(StringRef BitcodeBytes)
339 : SimpleBitstreamCursor(BitcodeBytes) {}
340 explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
341 : SimpleBitstreamCursor(BitcodeBytes) {}
342
343 using SimpleBitstreamCursor::canSkipToPos;
344 using SimpleBitstreamCursor::AtEndOfStream;
345 using SimpleBitstreamCursor::getBitcodeBytes;
346 using SimpleBitstreamCursor::GetCurrentBitNo;
347 using SimpleBitstreamCursor::getCurrentByteNo;
348 using SimpleBitstreamCursor::getPointerToByte;
349 using SimpleBitstreamCursor::JumpToBit;
350 using SimpleBitstreamCursor::fillCurWord;
351 using SimpleBitstreamCursor::Read;
352 using SimpleBitstreamCursor::ReadVBR;
353 using SimpleBitstreamCursor::ReadVBR64;
354
355 /// Return the number of bits used to encode an abbrev #.
356 unsigned getAbbrevIDWidth() const { return CurCodeSize; }
357
358 /// Flags that modify the behavior of advance().
359 enum {
360 /// If this flag is used, the advance() method does not automatically pop
361 /// the block scope when the end of a block is reached.
362 AF_DontPopBlockAtEnd = 1,
363
364 /// If this flag is used, abbrev entries are returned just like normal
365 /// records.
366 AF_DontAutoprocessAbbrevs = 2
367 };
368
369 /// Advance the current bitstream, returning the next entry in the stream.
370 BitstreamEntry advance(unsigned Flags = 0) {
371 while (true) {
372 if (AtEndOfStream())
373 return BitstreamEntry::getError();
374
375 unsigned Code = ReadCode();
376 if (Code == bitc::END_BLOCK) {
377 // Pop the end of the block unless Flags tells us not to.
378 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
379 return BitstreamEntry::getError();
380 return BitstreamEntry::getEndBlock();
381 }
382
383 if (Code == bitc::ENTER_SUBBLOCK)
384 return BitstreamEntry::getSubBlock(ReadSubBlockID());
385
386 if (Code == bitc::DEFINE_ABBREV &&
387 !(Flags & AF_DontAutoprocessAbbrevs)) {
388 // We read and accumulate abbrev's, the client can't do anything with
389 // them anyway.
390 ReadAbbrevRecord();
391 continue;
392 }
393
394 return BitstreamEntry::getRecord(Code);
395 }
396 }
397
398 /// This is a convenience function for clients that don't expect any
399 /// subblocks. This just skips over them automatically.
400 BitstreamEntry advanceSkippingSubblocks(unsigned Flags = 0) {
401 while (true) {
402 // If we found a normal entry, return it.
403 BitstreamEntry Entry = advance(Flags);
404 if (Entry.Kind != BitstreamEntry::SubBlock)
405 return Entry;
406
407 // If we found a sub-block, just skip over it and check the next entry.
408 if (SkipBlock())
409 return BitstreamEntry::getError();
410 }
411 }
412
413 unsigned ReadCode() {
414 return Read(CurCodeSize);
415 }
416
417 // Block header:
418 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
419
420 /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
421 unsigned ReadSubBlockID() {
422 return ReadVBR(bitc::BlockIDWidth);
423 }
424
425 /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
426 /// of this block. If the block record is malformed, return true.
427 bool SkipBlock() {
428 // Read and ignore the codelen value. Since we are skipping this block, we
429 // don't care what code widths are used inside of it.
430 ReadVBR(bitc::CodeLenWidth);
431 SkipToFourByteBoundary();
432 size_t NumFourBytes = Read(bitc::BlockSizeWidth);
433
434 // Check that the block wasn't partially defined, and that the offset isn't
435 // bogus.
436 size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8;
437 if (AtEndOfStream() || !canSkipToPos(SkipTo/8))
438 return true;
439
440 JumpToBit(SkipTo);
441 return false;
442 }
443
444 /// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true
445 /// if the block has an error.
446 bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
447
448 bool ReadBlockEnd() {
449 if (BlockScope.empty()) return true;
450
451 // Block tail:
452 // [END_BLOCK, <align4bytes>]
453 SkipToFourByteBoundary();
454
455 popBlockScope();
456 return false;
457 }
458
459private:
460 void popBlockScope() {
461 CurCodeSize = BlockScope.back().PrevCodeSize;
462
463 CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
464 BlockScope.pop_back();
465 }
466
467 //===--------------------------------------------------------------------===//
468 // Record Processing
469 //===--------------------------------------------------------------------===//
470
471public:
472 /// Return the abbreviation for the specified AbbrevId.
473 const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
474 unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
475 if (AbbrevNo >= CurAbbrevs.size())
476 report_fatal_error("Invalid abbrev number");
477 return CurAbbrevs[AbbrevNo].get();
478 }
479
480 /// Read the current record and discard it, returning the code for the record.
481 unsigned skipRecord(unsigned AbbrevID);
482
483 unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
484 StringRef *Blob = nullptr);
485
486 //===--------------------------------------------------------------------===//
487 // Abbrev Processing
488 //===--------------------------------------------------------------------===//
489 void ReadAbbrevRecord();
490
491 /// Read and return a block info block from the bitstream. If an error was
492 /// encountered, return None.
493 ///
494 /// \param ReadBlockInfoNames Whether to read block/record name information in
495 /// the BlockInfo block. Only llvm-bcanalyzer uses this.
496 Optional<BitstreamBlockInfo>
497 ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
498
499 /// Set the block info to be used by this BitstreamCursor to interpret
500 /// abbreviated records.
501 void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
502};
503
504} // end llvm namespace
505
506#endif // LLVM_BITCODE_BITSTREAMREADER_H