clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SerializedDiagnosticReader.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 -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangFrontend/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangFrontend/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangFrontend/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangFrontend/../include -I /usr/src/gnu/usr.bin/clang/libclangFrontend/obj -I /usr/src/gnu/usr.bin/clang/libclangFrontend/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangFrontend/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangFrontend/../../../llvm/clang/lib/Frontend/SerializedDiagnosticReader.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "clang/Frontend/SerializedDiagnosticReader.h" |
10 | #include "clang/Basic/FileManager.h" |
11 | #include "clang/Basic/FileSystemOptions.h" |
12 | #include "clang/Frontend/SerializedDiagnostics.h" |
13 | #include "llvm/ADT/Optional.h" |
14 | #include "llvm/ADT/SmallVector.h" |
15 | #include "llvm/ADT/StringRef.h" |
16 | #include "llvm/Bitstream/BitCodes.h" |
17 | #include "llvm/Bitstream/BitstreamReader.h" |
18 | #include "llvm/Support/Compiler.h" |
19 | #include "llvm/Support/ErrorHandling.h" |
20 | #include "llvm/Support/ErrorOr.h" |
21 | #include "llvm/Support/ManagedStatic.h" |
22 | #include <cstdint> |
23 | #include <system_error> |
24 | |
25 | using namespace clang; |
26 | using namespace serialized_diags; |
27 | |
28 | std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { |
29 | |
30 | FileSystemOptions FO; |
31 | FileManager FileMgr(FO); |
32 | |
33 | auto Buffer = FileMgr.getBufferForFile(File); |
34 | if (!Buffer) |
| |
35 | return SDError::CouldNotLoad; |
36 | |
37 | llvm::BitstreamCursor Stream(**Buffer); |
38 | Optional<llvm::BitstreamBlockInfo> BlockInfo; |
39 | |
40 | if (Stream.AtEndOfStream()) |
| |
41 | return SDError::InvalidSignature; |
42 | |
43 | |
44 | for (unsigned char C : {'D', 'I', 'A', 'G'}) { |
| 3 | | Assuming '__begin1' is equal to '__end1' | |
|
45 | if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { |
46 | if (Res.get() == C) |
47 | continue; |
48 | } else { |
49 | |
50 | consumeError(Res.takeError()); |
51 | } |
52 | return SDError::InvalidSignature; |
53 | } |
54 | |
55 | |
56 | while (!Stream.AtEndOfStream()) { |
| 4 | | Loop condition is true. Entering loop body | |
|
57 | if (Expected<unsigned> Res = Stream.ReadCode()) { |
| 5 | | Calling 'BitstreamCursor::ReadCode' | |
|
58 | if (Res.get() != llvm::bitc::ENTER_SUBBLOCK) |
59 | return SDError::InvalidDiagnostics; |
60 | } else { |
61 | |
62 | consumeError(Res.takeError()); |
63 | return SDError::InvalidDiagnostics; |
64 | } |
65 | |
66 | std::error_code EC; |
67 | Expected<unsigned> MaybeSubBlockID = Stream.ReadSubBlockID(); |
68 | if (!MaybeSubBlockID) { |
69 | |
70 | consumeError(MaybeSubBlockID.takeError()); |
71 | return SDError::InvalidDiagnostics; |
72 | } |
73 | |
74 | switch (MaybeSubBlockID.get()) { |
75 | case llvm::bitc::BLOCKINFO_BLOCK_ID: { |
76 | Expected<Optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo = |
77 | Stream.ReadBlockInfoBlock(); |
78 | if (!MaybeBlockInfo) { |
79 | |
80 | consumeError(MaybeBlockInfo.takeError()); |
81 | return SDError::InvalidDiagnostics; |
82 | } |
83 | BlockInfo = std::move(MaybeBlockInfo.get()); |
84 | } |
85 | if (!BlockInfo) |
86 | return SDError::MalformedBlockInfoBlock; |
87 | Stream.setBlockInfo(&*BlockInfo); |
88 | continue; |
89 | case BLOCK_META: |
90 | if ((EC = readMetaBlock(Stream))) |
91 | return EC; |
92 | continue; |
93 | case BLOCK_DIAG: |
94 | if ((EC = readDiagnosticBlock(Stream))) |
95 | return EC; |
96 | continue; |
97 | default: |
98 | if (llvm::Error Err = Stream.SkipBlock()) { |
99 | |
100 | consumeError(std::move(Err)); |
101 | return SDError::MalformedTopLevelBlock; |
102 | } |
103 | continue; |
104 | } |
105 | } |
106 | return {}; |
107 | } |
108 | |
109 | enum class SerializedDiagnosticReader::Cursor { |
110 | Record = 1, |
111 | BlockEnd, |
112 | BlockBegin |
113 | }; |
114 | |
115 | llvm::ErrorOr<SerializedDiagnosticReader::Cursor> |
116 | SerializedDiagnosticReader::skipUntilRecordOrBlock( |
117 | llvm::BitstreamCursor &Stream, unsigned &BlockOrRecordID) { |
118 | BlockOrRecordID = 0; |
119 | |
120 | while (!Stream.AtEndOfStream()) { |
121 | unsigned Code; |
122 | if (Expected<unsigned> Res = Stream.ReadCode()) |
123 | Code = Res.get(); |
124 | else |
125 | return llvm::errorToErrorCode(Res.takeError()); |
126 | |
127 | if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) { |
128 | |
129 | BlockOrRecordID = Code; |
130 | return Cursor::Record; |
131 | } |
132 | switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) { |
133 | case llvm::bitc::ENTER_SUBBLOCK: |
134 | if (Expected<unsigned> Res = Stream.ReadSubBlockID()) |
135 | BlockOrRecordID = Res.get(); |
136 | else |
137 | return llvm::errorToErrorCode(Res.takeError()); |
138 | return Cursor::BlockBegin; |
139 | |
140 | case llvm::bitc::END_BLOCK: |
141 | if (Stream.ReadBlockEnd()) |
142 | return SDError::InvalidDiagnostics; |
143 | return Cursor::BlockEnd; |
144 | |
145 | case llvm::bitc::DEFINE_ABBREV: |
146 | if (llvm::Error Err = Stream.ReadAbbrevRecord()) |
147 | return llvm::errorToErrorCode(std::move(Err)); |
148 | continue; |
149 | |
150 | case llvm::bitc::UNABBREV_RECORD: |
151 | return SDError::UnsupportedConstruct; |
152 | |
153 | case llvm::bitc::FIRST_APPLICATION_ABBREV: |
154 | llvm_unreachable("Unexpected abbrev id."); |
155 | } |
156 | } |
157 | |
158 | return SDError::InvalidDiagnostics; |
159 | } |
160 | |
161 | std::error_code |
162 | SerializedDiagnosticReader::readMetaBlock(llvm::BitstreamCursor &Stream) { |
163 | if (llvm::Error Err = |
164 | Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) { |
165 | |
166 | consumeError(std::move(Err)); |
167 | return SDError::MalformedMetadataBlock; |
168 | } |
169 | |
170 | bool VersionChecked = false; |
171 | |
172 | while (true) { |
173 | unsigned BlockOrCode = 0; |
174 | llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode); |
175 | if (!Res) |
176 | Res.getError(); |
177 | |
178 | switch (Res.get()) { |
179 | case Cursor::Record: |
180 | break; |
181 | case Cursor::BlockBegin: |
182 | if (llvm::Error Err = Stream.SkipBlock()) { |
183 | |
184 | consumeError(std::move(Err)); |
185 | return SDError::MalformedMetadataBlock; |
186 | } |
187 | LLVM_FALLTHROUGH; |
188 | case Cursor::BlockEnd: |
189 | if (!VersionChecked) |
190 | return SDError::MissingVersion; |
191 | return {}; |
192 | } |
193 | |
194 | SmallVector<uint64_t, 1> Record; |
195 | Expected<unsigned> MaybeRecordID = Stream.readRecord(BlockOrCode, Record); |
196 | if (!MaybeRecordID) |
197 | return errorToErrorCode(MaybeRecordID.takeError()); |
198 | unsigned RecordID = MaybeRecordID.get(); |
199 | |
200 | if (RecordID == RECORD_VERSION) { |
201 | if (Record.size() < 1) |
202 | return SDError::MissingVersion; |
203 | if (Record[0] > VersionNumber) |
204 | return SDError::VersionMismatch; |
205 | VersionChecked = true; |
206 | } |
207 | } |
208 | } |
209 | |
210 | std::error_code |
211 | SerializedDiagnosticReader::readDiagnosticBlock(llvm::BitstreamCursor &Stream) { |
212 | if (llvm::Error Err = |
213 | Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) { |
214 | |
215 | consumeError(std::move(Err)); |
216 | return SDError::MalformedDiagnosticBlock; |
217 | } |
218 | |
219 | std::error_code EC; |
220 | if ((EC = visitStartOfDiagnostic())) |
221 | return EC; |
222 | |
223 | SmallVector<uint64_t, 16> Record; |
224 | while (true) { |
225 | unsigned BlockOrCode = 0; |
226 | llvm::ErrorOr<Cursor> Res = skipUntilRecordOrBlock(Stream, BlockOrCode); |
227 | if (!Res) |
228 | Res.getError(); |
229 | |
230 | switch (Res.get()) { |
231 | case Cursor::BlockBegin: |
232 | |
233 | if (BlockOrCode == serialized_diags::BLOCK_DIAG) { |
234 | if ((EC = readDiagnosticBlock(Stream))) |
235 | return EC; |
236 | } else if (llvm::Error Err = Stream.SkipBlock()) { |
237 | |
238 | consumeError(std::move(Err)); |
239 | return SDError::MalformedSubBlock; |
240 | } |
241 | continue; |
242 | case Cursor::BlockEnd: |
243 | if ((EC = visitEndOfDiagnostic())) |
244 | return EC; |
245 | return {}; |
246 | case Cursor::Record: |
247 | break; |
248 | } |
249 | |
250 | |
251 | Record.clear(); |
252 | StringRef Blob; |
253 | Expected<unsigned> MaybeRecID = |
254 | Stream.readRecord(BlockOrCode, Record, &Blob); |
255 | if (!MaybeRecID) |
256 | return errorToErrorCode(MaybeRecID.takeError()); |
257 | unsigned RecID = MaybeRecID.get(); |
258 | |
259 | if (RecID < serialized_diags::RECORD_FIRST || |
260 | RecID > serialized_diags::RECORD_LAST) |
261 | continue; |
262 | |
263 | switch ((RecordIDs)RecID) { |
264 | case RECORD_CATEGORY: |
265 | |
266 | if (Record.size() != 2) |
267 | return SDError::MalformedDiagnosticRecord; |
268 | if ((EC = visitCategoryRecord(Record[0], Blob))) |
269 | return EC; |
270 | continue; |
271 | case RECORD_DIAG: |
272 | |
273 | |
274 | if (Record.size() != 8) |
275 | return SDError::MalformedDiagnosticRecord; |
276 | if ((EC = visitDiagnosticRecord( |
277 | Record[0], Location(Record[1], Record[2], Record[3], Record[4]), |
278 | Record[5], Record[6], Blob))) |
279 | return EC; |
280 | continue; |
281 | case RECORD_DIAG_FLAG: |
282 | |
283 | if (Record.size() != 2) |
284 | return SDError::MalformedDiagnosticRecord; |
285 | if ((EC = visitDiagFlagRecord(Record[0], Blob))) |
286 | return EC; |
287 | continue; |
288 | case RECORD_FILENAME: |
289 | |
290 | |
291 | if (Record.size() != 4) |
292 | return SDError::MalformedDiagnosticRecord; |
293 | if ((EC = visitFilenameRecord(Record[0], Record[1], Record[2], Blob))) |
294 | return EC; |
295 | continue; |
296 | case RECORD_FIXIT: |
297 | |
298 | if (Record.size() != 9) |
299 | return SDError::MalformedDiagnosticRecord; |
300 | if ((EC = visitFixitRecord( |
301 | Location(Record[0], Record[1], Record[2], Record[3]), |
302 | Location(Record[4], Record[5], Record[6], Record[7]), Blob))) |
303 | return EC; |
304 | continue; |
305 | case RECORD_SOURCE_RANGE: |
306 | |
307 | if (Record.size() != 8) |
308 | return SDError::MalformedDiagnosticRecord; |
309 | if ((EC = visitSourceRangeRecord( |
310 | Location(Record[0], Record[1], Record[2], Record[3]), |
311 | Location(Record[4], Record[5], Record[6], Record[7])))) |
312 | return EC; |
313 | continue; |
314 | case RECORD_VERSION: |
315 | |
316 | if (Record.size() != 1) |
317 | return SDError::MalformedDiagnosticRecord; |
318 | if ((EC = visitVersionRecord(Record[0]))) |
319 | return EC; |
320 | continue; |
321 | } |
322 | } |
323 | } |
324 | |
325 | namespace { |
326 | |
327 | class SDErrorCategoryType final : public std::error_category { |
328 | const char *name() const noexcept override { |
329 | return "clang.serialized_diags"; |
330 | } |
331 | |
332 | std::string message(int IE) const override { |
333 | auto E = static_cast<SDError>(IE); |
334 | switch (E) { |
335 | case SDError::CouldNotLoad: |
336 | return "Failed to open diagnostics file"; |
337 | case SDError::InvalidSignature: |
338 | return "Invalid diagnostics signature"; |
339 | case SDError::InvalidDiagnostics: |
340 | return "Parse error reading diagnostics"; |
341 | case SDError::MalformedTopLevelBlock: |
342 | return "Malformed block at top-level of diagnostics"; |
343 | case SDError::MalformedSubBlock: |
344 | return "Malformed sub-block in a diagnostic"; |
345 | case SDError::MalformedBlockInfoBlock: |
346 | return "Malformed BlockInfo block"; |
347 | case SDError::MalformedMetadataBlock: |
348 | return "Malformed Metadata block"; |
349 | case SDError::MalformedDiagnosticBlock: |
350 | return "Malformed Diagnostic block"; |
351 | case SDError::MalformedDiagnosticRecord: |
352 | return "Malformed Diagnostic record"; |
353 | case SDError::MissingVersion: |
354 | return "No version provided in diagnostics"; |
355 | case SDError::VersionMismatch: |
356 | return "Unsupported diagnostics version"; |
357 | case SDError::UnsupportedConstruct: |
358 | return "Bitcode constructs that are not supported in diagnostics appear"; |
359 | case SDError::HandlerFailed: |
360 | return "Generic error occurred while handling a record"; |
361 | } |
362 | llvm_unreachable("Unknown error type!"); |
363 | } |
364 | }; |
365 | |
366 | } |
367 | |
368 | static llvm::ManagedStatic<SDErrorCategoryType> ErrorCategory; |
369 | const std::error_category &clang::serialized_diags::SDErrorCategory() { |
370 | return *ErrorCategory; |
371 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_BITSTREAM_BITSTREAMREADER_H |
15 | #define LLVM_BITSTREAM_BITSTREAMREADER_H |
16 | |
17 | #include "llvm/ADT/ArrayRef.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/Bitstream/BitCodes.h" |
20 | #include "llvm/Support/Endian.h" |
21 | #include "llvm/Support/Error.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 | |
35 | namespace llvm { |
36 | |
37 | |
38 | class BitstreamBlockInfo { |
39 | public: |
40 | |
41 | |
42 | struct BlockInfo { |
43 | unsigned BlockID = 0; |
44 | std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs; |
45 | std::string Name; |
46 | std::vector<std::pair<unsigned, std::string>> RecordNames; |
47 | }; |
48 | |
49 | private: |
50 | std::vector<BlockInfo> BlockInfoRecords; |
51 | |
52 | public: |
53 | |
54 | |
55 | const BlockInfo *getBlockInfo(unsigned BlockID) const { |
56 | |
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 | |
72 | BlockInfoRecords.emplace_back(); |
73 | BlockInfoRecords.back().BlockID = BlockID; |
74 | return BlockInfoRecords.back(); |
75 | } |
76 | }; |
77 | |
78 | |
79 | |
80 | |
81 | class SimpleBitstreamCursor { |
82 | ArrayRef<uint8_t> BitcodeBytes; |
83 | size_t NextChar = 0; |
84 | |
85 | public: |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | using word_t = size_t; |
92 | |
93 | private: |
94 | word_t CurWord = 0; |
95 | |
96 | |
97 | |
98 | unsigned BitsInCurWord = 0; |
99 | |
100 | public: |
101 | static const constexpr 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(arrayRefFromStringRef(BitcodeBytes)) {} |
108 | explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes) |
109 | : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {} |
110 | |
111 | bool canSkipToPos(size_t pos) const { |
112 | |
113 | return pos <= BitcodeBytes.size(); |
114 | } |
115 | |
116 | bool AtEndOfStream() { |
117 | return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar; |
118 | } |
119 | |
120 | |
121 | uint64_t GetCurrentBitNo() const { |
122 | return NextChar*CHAR_BIT - BitsInCurWord; |
123 | } |
124 | |
125 | |
126 | uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; } |
127 | |
128 | ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; } |
129 | |
130 | |
131 | Error JumpToBit(uint64_t BitNo) { |
132 | size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1); |
133 | unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1)); |
134 | assert(canSkipToPos(ByteNo) && "Invalid location"); |
135 | |
136 | |
137 | NextChar = ByteNo; |
138 | BitsInCurWord = 0; |
139 | |
140 | |
141 | if (WordBitNo) { |
142 | if (Expected<word_t> Res = Read(WordBitNo)) |
143 | return Error::success(); |
144 | else |
145 | return Res.takeError(); |
146 | } |
147 | |
148 | return Error::success(); |
149 | } |
150 | |
151 | |
152 | const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) { |
153 | return BitcodeBytes.data() + ByteNo; |
154 | } |
155 | |
156 | |
157 | |
158 | |
159 | const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) { |
160 | assert(!(BitNo % 8) && "Expected bit on byte boundary"); |
161 | return getPointerToByte(BitNo / 8, NumBytes); |
162 | } |
163 | |
164 | Error fillCurWord() { |
165 | if (NextChar >= BitcodeBytes.size()) |
166 | return createStringError(std::errc::io_error, |
167 | "Unexpected end of file reading %u of %u bytes", |
168 | NextChar, BitcodeBytes.size()); |
169 | |
170 | |
171 | const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar; |
172 | unsigned BytesRead; |
173 | if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) { |
174 | BytesRead = sizeof(word_t); |
175 | CurWord = |
176 | support::endian::read<word_t, support::little, support::unaligned>( |
177 | NextCharPtr); |
178 | } else { |
179 | |
180 | BytesRead = BitcodeBytes.size() - NextChar; |
181 | CurWord = 0; |
182 | for (unsigned B = 0; B != BytesRead; ++B) |
183 | CurWord |= uint64_t(NextCharPtr[B]) << (B * 8); |
184 | } |
185 | NextChar += BytesRead; |
186 | BitsInCurWord = BytesRead * 8; |
187 | return Error::success(); |
188 | } |
189 | |
190 | Expected<word_t> Read(unsigned NumBits) { |
191 | static const unsigned BitsInWord = MaxChunkSize; |
192 | |
193 | assert(NumBits && NumBits <= BitsInWord && |
194 | "Cannot return zero or more than BitsInWord bits!"); |
195 | |
196 | static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f; |
| |
197 | |
198 | |
199 | if (BitsInCurWord >= NumBits) { |
| 8 | | Assuming 'NumBits' is <= field 'BitsInCurWord' | |
|
| |
200 | word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits)); |
| 10 | | 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' |
|
201 | |
202 | |
203 | CurWord >>= (NumBits & Mask); |
204 | |
205 | BitsInCurWord -= NumBits; |
206 | return R; |
207 | } |
208 | |
209 | word_t R = BitsInCurWord ? CurWord : 0; |
210 | unsigned BitsLeft = NumBits - BitsInCurWord; |
211 | |
212 | if (Error fillResult = fillCurWord()) |
213 | return std::move(fillResult); |
214 | |
215 | |
216 | if (BitsLeft > BitsInCurWord) |
217 | return createStringError(std::errc::io_error, |
218 | "Unexpected end of file reading %u of %u bits", |
219 | BitsInCurWord, BitsLeft); |
220 | |
221 | word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft)); |
222 | |
223 | |
224 | CurWord >>= (BitsLeft & Mask); |
225 | |
226 | BitsInCurWord -= BitsLeft; |
227 | |
228 | R |= R2 << (NumBits - BitsLeft); |
229 | |
230 | return R; |
231 | } |
232 | |
233 | Expected<uint32_t> ReadVBR(unsigned NumBits) { |
234 | Expected<unsigned> MaybeRead = Read(NumBits); |
235 | if (!MaybeRead) |
236 | return MaybeRead; |
237 | uint32_t Piece = MaybeRead.get(); |
238 | |
239 | if ((Piece & (1U << (NumBits-1))) == 0) |
240 | return Piece; |
241 | |
242 | uint32_t Result = 0; |
243 | unsigned NextBit = 0; |
244 | while (true) { |
245 | Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit; |
246 | |
247 | if ((Piece & (1U << (NumBits-1))) == 0) |
248 | return Result; |
249 | |
250 | NextBit += NumBits-1; |
251 | MaybeRead = Read(NumBits); |
252 | if (!MaybeRead) |
253 | return MaybeRead; |
254 | Piece = MaybeRead.get(); |
255 | } |
256 | } |
257 | |
258 | |
259 | |
260 | Expected<uint64_t> ReadVBR64(unsigned NumBits) { |
261 | Expected<uint64_t> MaybeRead = Read(NumBits); |
262 | if (!MaybeRead) |
263 | return MaybeRead; |
264 | uint32_t Piece = MaybeRead.get(); |
265 | |
266 | if ((Piece & (1U << (NumBits-1))) == 0) |
267 | return uint64_t(Piece); |
268 | |
269 | uint64_t Result = 0; |
270 | unsigned NextBit = 0; |
271 | while (true) { |
272 | Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit; |
273 | |
274 | if ((Piece & (1U << (NumBits-1))) == 0) |
275 | return Result; |
276 | |
277 | NextBit += NumBits-1; |
278 | MaybeRead = Read(NumBits); |
279 | if (!MaybeRead) |
280 | return MaybeRead; |
281 | Piece = MaybeRead.get(); |
282 | } |
283 | } |
284 | |
285 | void SkipToFourByteBoundary() { |
286 | |
287 | |
288 | if (sizeof(word_t) > 4 && |
289 | BitsInCurWord >= 32) { |
290 | CurWord >>= BitsInCurWord-32; |
291 | BitsInCurWord = 32; |
292 | return; |
293 | } |
294 | |
295 | BitsInCurWord = 0; |
296 | } |
297 | |
298 | |
299 | size_t SizeInBytes() const { return BitcodeBytes.size(); } |
300 | |
301 | |
302 | void skipToEnd() { NextChar = BitcodeBytes.size(); } |
303 | }; |
304 | |
305 | |
306 | |
307 | struct BitstreamEntry { |
308 | enum { |
309 | Error, |
310 | EndBlock, |
311 | |
312 | SubBlock, |
313 | Record |
314 | } Kind; |
315 | |
316 | unsigned ID; |
317 | |
318 | static BitstreamEntry getError() { |
319 | BitstreamEntry E; E.Kind = Error; return E; |
320 | } |
321 | |
322 | static BitstreamEntry getEndBlock() { |
323 | BitstreamEntry E; E.Kind = EndBlock; return E; |
324 | } |
325 | |
326 | static BitstreamEntry getSubBlock(unsigned ID) { |
327 | BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E; |
328 | } |
329 | |
330 | static BitstreamEntry getRecord(unsigned AbbrevID) { |
331 | BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E; |
332 | } |
333 | }; |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | class BitstreamCursor : SimpleBitstreamCursor { |
341 | |
342 | |
343 | unsigned CurCodeSize = 2; |
344 | |
345 | |
346 | std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs; |
347 | |
348 | struct Block { |
349 | unsigned PrevCodeSize; |
350 | std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs; |
351 | |
352 | explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} |
353 | }; |
354 | |
355 | |
356 | SmallVector<Block, 8> BlockScope; |
357 | |
358 | BitstreamBlockInfo *BlockInfo = nullptr; |
359 | |
360 | public: |
361 | static const size_t MaxChunkSize = sizeof(word_t) * 8; |
362 | |
363 | BitstreamCursor() = default; |
364 | explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) |
365 | : SimpleBitstreamCursor(BitcodeBytes) {} |
366 | explicit BitstreamCursor(StringRef BitcodeBytes) |
367 | : SimpleBitstreamCursor(BitcodeBytes) {} |
368 | explicit BitstreamCursor(MemoryBufferRef BitcodeBytes) |
369 | : SimpleBitstreamCursor(BitcodeBytes) {} |
370 | |
371 | using SimpleBitstreamCursor::AtEndOfStream; |
372 | using SimpleBitstreamCursor::canSkipToPos; |
373 | using SimpleBitstreamCursor::fillCurWord; |
374 | using SimpleBitstreamCursor::getBitcodeBytes; |
375 | using SimpleBitstreamCursor::GetCurrentBitNo; |
376 | using SimpleBitstreamCursor::getCurrentByteNo; |
377 | using SimpleBitstreamCursor::getPointerToByte; |
378 | using SimpleBitstreamCursor::JumpToBit; |
379 | using SimpleBitstreamCursor::Read; |
380 | using SimpleBitstreamCursor::ReadVBR; |
381 | using SimpleBitstreamCursor::ReadVBR64; |
382 | using SimpleBitstreamCursor::SizeInBytes; |
383 | using SimpleBitstreamCursor::skipToEnd; |
384 | |
385 | |
386 | unsigned getAbbrevIDWidth() const { return CurCodeSize; } |
387 | |
388 | |
389 | enum { |
390 | |
391 | |
392 | AF_DontPopBlockAtEnd = 1, |
393 | |
394 | |
395 | |
396 | AF_DontAutoprocessAbbrevs = 2 |
397 | }; |
398 | |
399 | |
400 | Expected<BitstreamEntry> advance(unsigned Flags = 0) { |
401 | while (true) { |
402 | if (AtEndOfStream()) |
403 | return BitstreamEntry::getError(); |
404 | |
405 | Expected<unsigned> MaybeCode = ReadCode(); |
406 | if (!MaybeCode) |
407 | return MaybeCode.takeError(); |
408 | unsigned Code = MaybeCode.get(); |
409 | |
410 | if (Code == bitc::END_BLOCK) { |
411 | |
412 | if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) |
413 | return BitstreamEntry::getError(); |
414 | return BitstreamEntry::getEndBlock(); |
415 | } |
416 | |
417 | if (Code == bitc::ENTER_SUBBLOCK) { |
418 | if (Expected<unsigned> MaybeSubBlock = ReadSubBlockID()) |
419 | return BitstreamEntry::getSubBlock(MaybeSubBlock.get()); |
420 | else |
421 | return MaybeSubBlock.takeError(); |
422 | } |
423 | |
424 | if (Code == bitc::DEFINE_ABBREV && |
425 | !(Flags & AF_DontAutoprocessAbbrevs)) { |
426 | |
427 | |
428 | if (Error Err = ReadAbbrevRecord()) |
429 | return std::move(Err); |
430 | continue; |
431 | } |
432 | |
433 | return BitstreamEntry::getRecord(Code); |
434 | } |
435 | } |
436 | |
437 | |
438 | |
439 | Expected<BitstreamEntry> advanceSkippingSubblocks(unsigned Flags = 0) { |
440 | while (true) { |
441 | |
442 | Expected<BitstreamEntry> MaybeEntry = advance(Flags); |
443 | if (!MaybeEntry) |
444 | return MaybeEntry; |
445 | BitstreamEntry Entry = MaybeEntry.get(); |
446 | |
447 | if (Entry.Kind != BitstreamEntry::SubBlock) |
448 | return Entry; |
449 | |
450 | |
451 | if (Error Err = SkipBlock()) |
452 | return std::move(Err); |
453 | } |
454 | } |
455 | |
456 | Expected<unsigned> ReadCode() { return Read(CurCodeSize); } |
| 6 | | Calling 'SimpleBitstreamCursor::Read' | |
|
457 | |
458 | |
459 | |
460 | |
461 | |
462 | Expected<unsigned> ReadSubBlockID() { return ReadVBR(bitc::BlockIDWidth); } |
463 | |
464 | |
465 | |
466 | Error SkipBlock() { |
467 | |
468 | if (Expected<uint32_t> Res = ReadVBR(bitc::CodeLenWidth)) |
469 | ; |
470 | |
471 | else |
472 | return Res.takeError(); |
473 | |
474 | SkipToFourByteBoundary(); |
475 | Expected<unsigned> MaybeNum = Read(bitc::BlockSizeWidth); |
476 | if (!MaybeNum) |
477 | return MaybeNum.takeError(); |
478 | size_t NumFourBytes = MaybeNum.get(); |
479 | |
480 | |
481 | |
482 | size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8; |
483 | if (AtEndOfStream()) |
484 | return createStringError(std::errc::illegal_byte_sequence, |
485 | "can't skip block: already at end of stream"); |
486 | if (!canSkipToPos(SkipTo / 8)) |
487 | return createStringError(std::errc::illegal_byte_sequence, |
488 | "can't skip to bit %zu from %" PRIu64, SkipTo, |
489 | GetCurrentBitNo()); |
490 | |
491 | if (Error Res = JumpToBit(SkipTo)) |
492 | return Res; |
493 | |
494 | return Error::success(); |
495 | } |
496 | |
497 | |
498 | Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr); |
499 | |
500 | bool ReadBlockEnd() { |
501 | if (BlockScope.empty()) return true; |
502 | |
503 | |
504 | |
505 | SkipToFourByteBoundary(); |
506 | |
507 | popBlockScope(); |
508 | return false; |
509 | } |
510 | |
511 | private: |
512 | void popBlockScope() { |
513 | CurCodeSize = BlockScope.back().PrevCodeSize; |
514 | |
515 | CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs); |
516 | BlockScope.pop_back(); |
517 | } |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | public: |
524 | |
525 | const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) { |
526 | unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV; |
527 | if (AbbrevNo >= CurAbbrevs.size()) |
528 | report_fatal_error("Invalid abbrev number"); |
529 | return CurAbbrevs[AbbrevNo].get(); |
530 | } |
531 | |
532 | |
533 | Expected<unsigned> skipRecord(unsigned AbbrevID); |
534 | |
535 | Expected<unsigned> readRecord(unsigned AbbrevID, |
536 | SmallVectorImpl<uint64_t> &Vals, |
537 | StringRef *Blob = nullptr); |
538 | |
539 | |
540 | |
541 | |
542 | Error ReadAbbrevRecord(); |
543 | |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | Expected<Optional<BitstreamBlockInfo>> |
550 | ReadBlockInfoBlock(bool ReadBlockInfoNames = false); |
551 | |
552 | |
553 | |
554 | void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; } |
555 | }; |
556 | |
557 | } |
558 | |
559 | #endif // LLVM_BITSTREAM_BITSTREAMREADER_H |