BitstreamReader.h 19 KB

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