Bug Summary

File:include/llvm/Bitcode/BitstreamReader.h
Warning:line 208, 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-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 -analyzer-config-compatibility-mode=true -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-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-9~svn362543/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn362543/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/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -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-9~svn362543/build-llvm/lib/Bitcode/Reader -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn362543=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-06-05-060531-1271-1 -x c++ /build/llvm-toolchain-snapshot-9~svn362543/lib/Bitcode/Reader/BitstreamReader.cpp -faddrsig

/build/llvm-toolchain-snapshot-9~svn362543/lib/Bitcode/Reader/BitstreamReader.cpp

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

/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Bitcode/BitstreamReader.h

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