CodeViewRecordIO.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //===- CodeViewRecordIO.cpp -------------------------------------*- 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. #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
  9. #include "llvm/DebugInfo/CodeView/CodeView.h"
  10. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  11. #include "llvm/Support/BinaryStreamReader.h"
  12. #include "llvm/Support/BinaryStreamWriter.h"
  13. using namespace llvm;
  14. using namespace llvm::codeview;
  15. Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
  16. RecordLimit Limit;
  17. Limit.MaxLength = MaxLength;
  18. Limit.BeginOffset = getCurrentOffset();
  19. Limits.push_back(Limit);
  20. return Error::success();
  21. }
  22. Error CodeViewRecordIO::endRecord() {
  23. assert(!Limits.empty() && "Not in a record!");
  24. Limits.pop_back();
  25. // We would like to assert that we actually read / wrote all the bytes that we
  26. // expected to for this record, but unfortunately we can't do this. Some
  27. // producers such as MASM over-allocate for certain types of records and
  28. // commit the extraneous data, so when reading we can't be sure every byte
  29. // will have been read. And when writing we over-allocate temporarily since
  30. // we don't know how big the record is until we're finished writing it, so
  31. // even though we don't commit the extraneous data, we still can't guarantee
  32. // we're at the end of the allocated data.
  33. if (isStreaming()) {
  34. // For streaming mode, add padding to align with 4 byte boundaries for each
  35. // record
  36. uint32_t Align = getStreamedLen() % 4;
  37. if (Align == 0)
  38. return Error::success();
  39. int PaddingBytes = 4 - Align;
  40. while (PaddingBytes > 0) {
  41. char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
  42. StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
  43. Streamer->emitBytes(BytesSR);
  44. --PaddingBytes;
  45. }
  46. resetStreamedLen();
  47. }
  48. return Error::success();
  49. }
  50. uint32_t CodeViewRecordIO::maxFieldLength() const {
  51. if (isStreaming())
  52. return 0;
  53. assert(!Limits.empty() && "Not in a record!");
  54. // The max length of the next field is the minimum of all lengths that would
  55. // be allowed by any of the sub-records we're in. In practice, we can only
  56. // ever be at most 1 sub-record deep (in a FieldList), but this works for
  57. // the general case.
  58. uint32_t Offset = getCurrentOffset();
  59. Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
  60. for (auto X : makeArrayRef(Limits).drop_front()) {
  61. Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
  62. if (ThisMin.hasValue())
  63. Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
  64. }
  65. assert(Min.hasValue() && "Every field must have a maximum length!");
  66. return *Min;
  67. }
  68. Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
  69. if (isReading())
  70. return Reader->padToAlignment(Align);
  71. return Writer->padToAlignment(Align);
  72. }
  73. Error CodeViewRecordIO::skipPadding() {
  74. assert(!isWriting() && "Cannot skip padding while writing!");
  75. if (Reader->bytesRemaining() == 0)
  76. return Error::success();
  77. uint8_t Leaf = Reader->peek();
  78. if (Leaf < LF_PAD0)
  79. return Error::success();
  80. // Leaf is greater than 0xf0. We should advance by the number of bytes in
  81. // the low 4 bits.
  82. unsigned BytesToAdvance = Leaf & 0x0F;
  83. return Reader->skip(BytesToAdvance);
  84. }
  85. Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes,
  86. const Twine &Comment) {
  87. if (isStreaming()) {
  88. emitComment(Comment);
  89. Streamer->emitBinaryData(toStringRef(Bytes));
  90. incrStreamedLen(Bytes.size());
  91. } else if (isWriting()) {
  92. if (auto EC = Writer->writeBytes(Bytes))
  93. return EC;
  94. } else {
  95. if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
  96. return EC;
  97. }
  98. return Error::success();
  99. }
  100. Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
  101. const Twine &Comment) {
  102. ArrayRef<uint8_t> BytesRef(Bytes);
  103. if (auto EC = mapByteVectorTail(BytesRef, Comment))
  104. return EC;
  105. if (!isWriting())
  106. Bytes.assign(BytesRef.begin(), BytesRef.end());
  107. return Error::success();
  108. }
  109. Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
  110. if (isStreaming()) {
  111. std::string TypeNameStr = Streamer->getTypeName(TypeInd);
  112. if (!TypeNameStr.empty())
  113. emitComment(Comment + ": " + TypeNameStr);
  114. else
  115. emitComment(Comment);
  116. Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
  117. incrStreamedLen(sizeof(TypeInd.getIndex()));
  118. } else if (isWriting()) {
  119. if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
  120. return EC;
  121. } else {
  122. uint32_t I;
  123. if (auto EC = Reader->readInteger(I))
  124. return EC;
  125. TypeInd.setIndex(I);
  126. }
  127. return Error::success();
  128. }
  129. Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
  130. const Twine &Comment) {
  131. if (isStreaming()) {
  132. if (Value >= 0)
  133. emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
  134. else
  135. emitEncodedSignedInteger(Value, Comment);
  136. } else if (isWriting()) {
  137. if (Value >= 0) {
  138. if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
  139. return EC;
  140. } else {
  141. if (auto EC = writeEncodedSignedInteger(Value))
  142. return EC;
  143. }
  144. } else {
  145. APSInt N;
  146. if (auto EC = consume(*Reader, N))
  147. return EC;
  148. Value = N.getExtValue();
  149. }
  150. return Error::success();
  151. }
  152. Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
  153. const Twine &Comment) {
  154. if (isStreaming())
  155. emitEncodedUnsignedInteger(Value, Comment);
  156. else if (isWriting()) {
  157. if (auto EC = writeEncodedUnsignedInteger(Value))
  158. return EC;
  159. } else {
  160. APSInt N;
  161. if (auto EC = consume(*Reader, N))
  162. return EC;
  163. Value = N.getZExtValue();
  164. }
  165. return Error::success();
  166. }
  167. Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
  168. if (isStreaming()) {
  169. // FIXME: We also need to handle big values here, but it's
  170. // not clear how we can excercise this code path yet.
  171. if (Value.isSigned())
  172. emitEncodedSignedInteger(Value.getSExtValue(), Comment);
  173. else
  174. emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
  175. } else if (isWriting()) {
  176. if (Value.isSigned())
  177. return writeEncodedSignedInteger(
  178. Value.isSingleWord() ? Value.getSExtValue() : INT64_MIN);
  179. return writeEncodedUnsignedInteger(Value.getLimitedValue());
  180. } else
  181. return consume(*Reader, Value);
  182. return Error::success();
  183. }
  184. Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
  185. if (isStreaming()) {
  186. auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
  187. emitComment(Comment);
  188. Streamer->emitBytes(NullTerminatedString);
  189. incrStreamedLen(NullTerminatedString.size());
  190. } else if (isWriting()) {
  191. // Truncate if we attempt to write too much.
  192. StringRef S = Value.take_front(maxFieldLength() - 1);
  193. if (auto EC = Writer->writeCString(S))
  194. return EC;
  195. } else {
  196. if (auto EC = Reader->readCString(Value))
  197. return EC;
  198. }
  199. return Error::success();
  200. }
  201. Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
  202. constexpr uint32_t GuidSize = 16;
  203. if (isStreaming()) {
  204. StringRef GuidSR =
  205. StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
  206. emitComment(Comment);
  207. Streamer->emitBytes(GuidSR);
  208. incrStreamedLen(GuidSize);
  209. return Error::success();
  210. }
  211. if (maxFieldLength() < GuidSize)
  212. return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
  213. if (isWriting()) {
  214. if (auto EC = Writer->writeBytes(Guid.Guid))
  215. return EC;
  216. } else {
  217. ArrayRef<uint8_t> GuidBytes;
  218. if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
  219. return EC;
  220. memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
  221. }
  222. return Error::success();
  223. }
  224. Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
  225. const Twine &Comment) {
  226. if (!isReading()) {
  227. emitComment(Comment);
  228. for (auto V : Value) {
  229. if (auto EC = mapStringZ(V))
  230. return EC;
  231. }
  232. uint8_t FinalZero = 0;
  233. if (auto EC = mapInteger(FinalZero))
  234. return EC;
  235. } else {
  236. StringRef S;
  237. if (auto EC = mapStringZ(S))
  238. return EC;
  239. while (!S.empty()) {
  240. Value.push_back(S);
  241. if (auto EC = mapStringZ(S))
  242. return EC;
  243. };
  244. }
  245. return Error::success();
  246. }
  247. void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
  248. const Twine &Comment) {
  249. // FIXME: There are no test cases covering this function.
  250. // This may be because we always consider enumerators to be unsigned.
  251. // See FIXME at CodeViewDebug.cpp : CodeViewDebug::lowerTypeEnum.
  252. if (Value >= std::numeric_limits<int8_t>::min()) {
  253. Streamer->emitIntValue(LF_CHAR, 2);
  254. emitComment(Comment);
  255. Streamer->emitIntValue(Value, 1);
  256. incrStreamedLen(3);
  257. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  258. Streamer->emitIntValue(LF_SHORT, 2);
  259. emitComment(Comment);
  260. Streamer->emitIntValue(Value, 2);
  261. incrStreamedLen(4);
  262. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  263. Streamer->emitIntValue(LF_LONG, 2);
  264. emitComment(Comment);
  265. Streamer->emitIntValue(Value, 4);
  266. incrStreamedLen(6);
  267. } else {
  268. Streamer->emitIntValue(LF_QUADWORD, 2);
  269. emitComment(Comment);
  270. Streamer->emitIntValue(Value, 4); // FIXME: Why not 8 (size of quadword)?
  271. incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
  272. }
  273. }
  274. void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
  275. const Twine &Comment) {
  276. if (Value < LF_NUMERIC) {
  277. emitComment(Comment);
  278. Streamer->emitIntValue(Value, 2);
  279. incrStreamedLen(2);
  280. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  281. Streamer->emitIntValue(LF_USHORT, 2);
  282. emitComment(Comment);
  283. Streamer->emitIntValue(Value, 2);
  284. incrStreamedLen(4);
  285. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  286. Streamer->emitIntValue(LF_ULONG, 2);
  287. emitComment(Comment);
  288. Streamer->emitIntValue(Value, 4);
  289. incrStreamedLen(6);
  290. } else {
  291. // FIXME: There are no test cases covering this block.
  292. Streamer->emitIntValue(LF_UQUADWORD, 2);
  293. emitComment(Comment);
  294. Streamer->emitIntValue(Value, 8);
  295. incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
  296. }
  297. }
  298. Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
  299. if (Value >= std::numeric_limits<int8_t>::min()) {
  300. if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
  301. return EC;
  302. if (auto EC = Writer->writeInteger<int8_t>(Value))
  303. return EC;
  304. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  305. if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
  306. return EC;
  307. if (auto EC = Writer->writeInteger<int16_t>(Value))
  308. return EC;
  309. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  310. if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
  311. return EC;
  312. if (auto EC = Writer->writeInteger<int32_t>(Value))
  313. return EC;
  314. } else {
  315. if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
  316. return EC;
  317. if (auto EC = Writer->writeInteger(Value))
  318. return EC;
  319. }
  320. return Error::success();
  321. }
  322. Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
  323. if (Value < LF_NUMERIC) {
  324. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  325. return EC;
  326. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  327. if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
  328. return EC;
  329. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  330. return EC;
  331. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  332. if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
  333. return EC;
  334. if (auto EC = Writer->writeInteger<uint32_t>(Value))
  335. return EC;
  336. } else {
  337. if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
  338. return EC;
  339. if (auto EC = Writer->writeInteger(Value))
  340. return EC;
  341. }
  342. return Error::success();
  343. }