MinidumpYAMLTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //===- MinidumpYAMLTest.cpp - Tests for Minidump<->YAML code --------------===//
  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/Object/Minidump.h"
  9. #include "llvm/ObjectYAML/yaml2obj.h"
  10. #include "llvm/Support/YAMLTraits.h"
  11. #include "llvm/Testing/Support/Error.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. using namespace llvm::minidump;
  15. static Expected<std::unique_ptr<object::MinidumpFile>>
  16. toBinary(SmallVectorImpl<char> &Storage, StringRef Yaml) {
  17. Storage.clear();
  18. raw_svector_ostream OS(Storage);
  19. yaml::Input YIn(Yaml);
  20. if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
  21. return createStringError(std::errc::invalid_argument,
  22. "unable to convert YAML");
  23. return object::MinidumpFile::create(MemoryBufferRef(OS.str(), "Binary"));
  24. }
  25. TEST(MinidumpYAML, Basic) {
  26. SmallString<0> Storage;
  27. auto ExpectedFile = toBinary(Storage, R"(
  28. --- !minidump
  29. Streams:
  30. - Type: SystemInfo
  31. Processor Arch: ARM64
  32. Platform ID: Linux
  33. CPU:
  34. CPUID: 0x05060708
  35. - Type: LinuxMaps
  36. Text: |
  37. 400d9000-400db000 r-xp 00000000 b3:04 227 /system/bin/app_process
  38. 400db000-400dc000 r--p 00001000 b3:04 227 /system/bin/app_process
  39. - Type: LinuxAuxv
  40. Content: DEADBEEFBAADF00D)");
  41. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  42. object::MinidumpFile &File = **ExpectedFile;
  43. ASSERT_EQ(3u, File.streams().size());
  44. EXPECT_EQ(StreamType::SystemInfo, File.streams()[0].Type);
  45. auto ExpectedSysInfo = File.getSystemInfo();
  46. ASSERT_THAT_EXPECTED(ExpectedSysInfo, Succeeded());
  47. const SystemInfo &SysInfo = *ExpectedSysInfo;
  48. EXPECT_EQ(ProcessorArchitecture::ARM64, SysInfo.ProcessorArch);
  49. EXPECT_EQ(OSPlatform::Linux, SysInfo.PlatformId);
  50. EXPECT_EQ(0x05060708u, SysInfo.CPU.Arm.CPUID);
  51. EXPECT_EQ(StreamType::LinuxMaps, File.streams()[1].Type);
  52. EXPECT_EQ("400d9000-400db000 r-xp 00000000 b3:04 227 "
  53. "/system/bin/app_process\n"
  54. "400db000-400dc000 r--p 00001000 b3:04 227 "
  55. "/system/bin/app_process\n",
  56. toStringRef(*File.getRawStream(StreamType::LinuxMaps)));
  57. EXPECT_EQ(StreamType::LinuxAuxv, File.streams()[2].Type);
  58. EXPECT_EQ((ArrayRef<uint8_t>{0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD, 0xF0, 0x0D}),
  59. File.getRawStream(StreamType::LinuxAuxv));
  60. }
  61. TEST(MinidumpYAML, RawContent) {
  62. SmallString<0> Storage;
  63. auto ExpectedFile = toBinary(Storage, R"(
  64. --- !minidump
  65. Streams:
  66. - Type: LinuxAuxv
  67. Size: 9
  68. Content: DEADBEEFBAADF00D)");
  69. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  70. object::MinidumpFile &File = **ExpectedFile;
  71. EXPECT_EQ(
  72. (ArrayRef<uint8_t>{0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD, 0xF0, 0x0D, 0x00}),
  73. File.getRawStream(StreamType::LinuxAuxv));
  74. }
  75. TEST(MinidumpYAML, X86SystemInfo) {
  76. SmallString<0> Storage;
  77. auto ExpectedFile = toBinary(Storage, R"(
  78. --- !minidump
  79. Streams:
  80. - Type: SystemInfo
  81. Processor Arch: X86
  82. Platform ID: Linux
  83. CPU:
  84. Vendor ID: LLVMLLVMLLVM
  85. Version Info: 0x01020304
  86. Feature Info: 0x05060708
  87. AMD Extended Features: 0x09000102)");
  88. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  89. object::MinidumpFile &File = **ExpectedFile;
  90. ASSERT_EQ(1u, File.streams().size());
  91. auto ExpectedSysInfo = File.getSystemInfo();
  92. ASSERT_THAT_EXPECTED(ExpectedSysInfo, Succeeded());
  93. const SystemInfo &SysInfo = *ExpectedSysInfo;
  94. EXPECT_EQ(ProcessorArchitecture::X86, SysInfo.ProcessorArch);
  95. EXPECT_EQ(OSPlatform::Linux, SysInfo.PlatformId);
  96. EXPECT_EQ("LLVMLLVMLLVM", StringRef(SysInfo.CPU.X86.VendorID,
  97. sizeof(SysInfo.CPU.X86.VendorID)));
  98. EXPECT_EQ(0x01020304u, SysInfo.CPU.X86.VersionInfo);
  99. EXPECT_EQ(0x05060708u, SysInfo.CPU.X86.FeatureInfo);
  100. EXPECT_EQ(0x09000102u, SysInfo.CPU.X86.AMDExtendedFeatures);
  101. }
  102. TEST(MinidumpYAML, OtherSystemInfo) {
  103. SmallString<0> Storage;
  104. auto ExpectedFile = toBinary(Storage, R"(
  105. --- !minidump
  106. Streams:
  107. - Type: SystemInfo
  108. Processor Arch: PPC
  109. Platform ID: Linux
  110. CPU:
  111. Features: 000102030405060708090a0b0c0d0e0f)");
  112. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  113. object::MinidumpFile &File = **ExpectedFile;
  114. ASSERT_EQ(1u, File.streams().size());
  115. auto ExpectedSysInfo = File.getSystemInfo();
  116. ASSERT_THAT_EXPECTED(ExpectedSysInfo, Succeeded());
  117. const SystemInfo &SysInfo = *ExpectedSysInfo;
  118. EXPECT_EQ(ProcessorArchitecture::PPC, SysInfo.ProcessorArch);
  119. EXPECT_EQ(OSPlatform::Linux, SysInfo.PlatformId);
  120. EXPECT_EQ(
  121. (ArrayRef<uint8_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}),
  122. makeArrayRef(SysInfo.CPU.Other.ProcessorFeatures));
  123. }
  124. // Test that we can parse a normal-looking ExceptionStream.
  125. TEST(MinidumpYAML, ExceptionStream) {
  126. SmallString<0> Storage;
  127. auto ExpectedFile = toBinary(Storage, R"(
  128. --- !minidump
  129. Streams:
  130. - Type: Exception
  131. Thread ID: 0x7
  132. Exception Record:
  133. Exception Code: 0x23
  134. Exception Flags: 0x5
  135. Exception Record: 0x0102030405060708
  136. Exception Address: 0x0a0b0c0d0e0f1011
  137. Number of Parameters: 2
  138. Parameter 0: 0x22
  139. Parameter 1: 0x24
  140. Thread Context: 3DeadBeefDefacedABadCafe)");
  141. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  142. object::MinidumpFile &File = **ExpectedFile;
  143. ASSERT_EQ(1u, File.streams().size());
  144. Expected<const minidump::ExceptionStream &> ExpectedStream =
  145. File.getExceptionStream();
  146. ASSERT_THAT_EXPECTED(ExpectedStream, Succeeded());
  147. const minidump::ExceptionStream &Stream = *ExpectedStream;
  148. EXPECT_EQ(0x7u, Stream.ThreadId);
  149. const minidump::Exception &Exception = Stream.ExceptionRecord;
  150. EXPECT_EQ(0x23u, Exception.ExceptionCode);
  151. EXPECT_EQ(0x5u, Exception.ExceptionFlags);
  152. EXPECT_EQ(0x0102030405060708u, Exception.ExceptionRecord);
  153. EXPECT_EQ(0x0a0b0c0d0e0f1011u, Exception.ExceptionAddress);
  154. EXPECT_EQ(2u, Exception.NumberParameters);
  155. EXPECT_EQ(0x22u, Exception.ExceptionInformation[0]);
  156. EXPECT_EQ(0x24u, Exception.ExceptionInformation[1]);
  157. Expected<ArrayRef<uint8_t>> ExpectedContext =
  158. File.getRawData(Stream.ThreadContext);
  159. ASSERT_THAT_EXPECTED(ExpectedContext, Succeeded());
  160. EXPECT_EQ((ArrayRef<uint8_t>{0x3d, 0xea, 0xdb, 0xee, 0xfd, 0xef, 0xac, 0xed,
  161. 0xab, 0xad, 0xca, 0xfe}),
  162. *ExpectedContext);
  163. }
  164. // Test that we can parse an exception stream with no ExceptionInformation.
  165. TEST(MinidumpYAML, ExceptionStream_NoParameters) {
  166. SmallString<0> Storage;
  167. auto ExpectedFile = toBinary(Storage, R"(
  168. --- !minidump
  169. Streams:
  170. - Type: Exception
  171. Thread ID: 0x7
  172. Exception Record:
  173. Exception Code: 0x23
  174. Exception Flags: 0x5
  175. Exception Record: 0x0102030405060708
  176. Exception Address: 0x0a0b0c0d0e0f1011
  177. Thread Context: 3DeadBeefDefacedABadCafe)");
  178. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  179. object::MinidumpFile &File = **ExpectedFile;
  180. ASSERT_EQ(1u, File.streams().size());
  181. Expected<const minidump::ExceptionStream &> ExpectedStream =
  182. File.getExceptionStream();
  183. ASSERT_THAT_EXPECTED(ExpectedStream, Succeeded());
  184. const minidump::ExceptionStream &Stream = *ExpectedStream;
  185. EXPECT_EQ(0x7u, Stream.ThreadId);
  186. const minidump::Exception &Exception = Stream.ExceptionRecord;
  187. EXPECT_EQ(0x23u, Exception.ExceptionCode);
  188. EXPECT_EQ(0x5u, Exception.ExceptionFlags);
  189. EXPECT_EQ(0x0102030405060708u, Exception.ExceptionRecord);
  190. EXPECT_EQ(0x0a0b0c0d0e0f1011u, Exception.ExceptionAddress);
  191. EXPECT_EQ(0u, Exception.NumberParameters);
  192. Expected<ArrayRef<uint8_t>> ExpectedContext =
  193. File.getRawData(Stream.ThreadContext);
  194. ASSERT_THAT_EXPECTED(ExpectedContext, Succeeded());
  195. EXPECT_EQ((ArrayRef<uint8_t>{0x3d, 0xea, 0xdb, 0xee, 0xfd, 0xef, 0xac, 0xed,
  196. 0xab, 0xad, 0xca, 0xfe}),
  197. *ExpectedContext);
  198. }
  199. // Test that we can parse an ExceptionStream where the stated number of
  200. // parameters is greater than the actual size of the ExceptionInformation
  201. // array.
  202. TEST(MinidumpYAML, ExceptionStream_TooManyParameters) {
  203. SmallString<0> Storage;
  204. auto ExpectedFile = toBinary(Storage, R"(
  205. --- !minidump
  206. Streams:
  207. - Type: Exception
  208. Thread ID: 0x8
  209. Exception Record:
  210. Exception Code: 0
  211. Number of Parameters: 16
  212. Parameter 0: 0x0
  213. Parameter 1: 0xff
  214. Parameter 2: 0xee
  215. Parameter 3: 0xdd
  216. Parameter 4: 0xcc
  217. Parameter 5: 0xbb
  218. Parameter 6: 0xaa
  219. Parameter 7: 0x99
  220. Parameter 8: 0x88
  221. Parameter 9: 0x77
  222. Parameter 10: 0x66
  223. Parameter 11: 0x55
  224. Parameter 12: 0x44
  225. Parameter 13: 0x33
  226. Parameter 14: 0x22
  227. Thread Context: 3DeadBeefDefacedABadCafe)");
  228. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  229. object::MinidumpFile &File = **ExpectedFile;
  230. ASSERT_EQ(1u, File.streams().size());
  231. Expected<const minidump::ExceptionStream &> ExpectedStream =
  232. File.getExceptionStream();
  233. ASSERT_THAT_EXPECTED(ExpectedStream, Succeeded());
  234. const minidump::ExceptionStream &Stream = *ExpectedStream;
  235. EXPECT_EQ(0x8u, Stream.ThreadId);
  236. const minidump::Exception &Exception = Stream.ExceptionRecord;
  237. EXPECT_EQ(0x0u, Exception.ExceptionCode);
  238. EXPECT_EQ(0x0u, Exception.ExceptionFlags);
  239. EXPECT_EQ(0x00u, Exception.ExceptionRecord);
  240. EXPECT_EQ(0x0u, Exception.ExceptionAddress);
  241. EXPECT_EQ(16u, Exception.NumberParameters);
  242. EXPECT_EQ(0x0u, Exception.ExceptionInformation[0]);
  243. for (int Index = 1; Index < 15; ++Index) {
  244. EXPECT_EQ(0x110u - Index * 0x11, Exception.ExceptionInformation[Index]);
  245. }
  246. Expected<ArrayRef<uint8_t>> ExpectedContext =
  247. File.getRawData(Stream.ThreadContext);
  248. ASSERT_THAT_EXPECTED(ExpectedContext, Succeeded());
  249. EXPECT_EQ((ArrayRef<uint8_t>{0x3d, 0xea, 0xdb, 0xee, 0xfd, 0xef, 0xac, 0xed,
  250. 0xab, 0xad, 0xca, 0xfe}),
  251. *ExpectedContext);
  252. }
  253. // Test that we can parse an ExceptionStream where the number of
  254. // ExceptionInformation parameters provided is greater than the
  255. // specified Number of Parameters.
  256. TEST(MinidumpYAML, ExceptionStream_ExtraParameter) {
  257. SmallString<0> Storage;
  258. auto ExpectedFile = toBinary(Storage, R"(
  259. --- !minidump
  260. Streams:
  261. - Type: Exception
  262. Thread ID: 0x7
  263. Exception Record:
  264. Exception Code: 0x23
  265. Exception Flags: 0x5
  266. Exception Record: 0x0102030405060708
  267. Exception Address: 0x0a0b0c0d0e0f1011
  268. Number of Parameters: 2
  269. Parameter 0: 0x99
  270. Parameter 1: 0x23
  271. Parameter 2: 0x42
  272. Thread Context: 3DeadBeefDefacedABadCafe)");
  273. ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
  274. object::MinidumpFile &File = **ExpectedFile;
  275. ASSERT_EQ(1u, File.streams().size());
  276. Expected<const minidump::ExceptionStream &> ExpectedStream =
  277. File.getExceptionStream();
  278. ASSERT_THAT_EXPECTED(ExpectedStream, Succeeded());
  279. const minidump::ExceptionStream &Stream = *ExpectedStream;
  280. EXPECT_EQ(0x7u, Stream.ThreadId);
  281. const minidump::Exception &Exception = Stream.ExceptionRecord;
  282. EXPECT_EQ(0x23u, Exception.ExceptionCode);
  283. EXPECT_EQ(0x5u, Exception.ExceptionFlags);
  284. EXPECT_EQ(0x0102030405060708u, Exception.ExceptionRecord);
  285. EXPECT_EQ(0x0a0b0c0d0e0f1011u, Exception.ExceptionAddress);
  286. EXPECT_EQ(2u, Exception.NumberParameters);
  287. EXPECT_EQ(0x99u, Exception.ExceptionInformation[0]);
  288. EXPECT_EQ(0x23u, Exception.ExceptionInformation[1]);
  289. EXPECT_EQ(0x42u, Exception.ExceptionInformation[2]);
  290. Expected<ArrayRef<uint8_t>> ExpectedContext =
  291. File.getRawData(Stream.ThreadContext);
  292. ASSERT_THAT_EXPECTED(ExpectedContext, Succeeded());
  293. EXPECT_EQ((ArrayRef<uint8_t>{0x3d, 0xea, 0xdb, 0xee, 0xfd, 0xef, 0xac, 0xed,
  294. 0xab, 0xad, 0xca, 0xfe}),
  295. *ExpectedContext);
  296. }