Disassembler.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
  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-c/Disassembler.h"
  9. #include "llvm/Support/TargetSelect.h"
  10. #include "gtest/gtest.h"
  11. using namespace llvm;
  12. static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
  13. uint64_t *ReferenceType,
  14. uint64_t ReferencePC,
  15. const char **ReferenceName) {
  16. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  17. return nullptr;
  18. }
  19. TEST(Disassembler, X86Test) {
  20. llvm::InitializeAllTargetInfos();
  21. llvm::InitializeAllTargetMCs();
  22. llvm::InitializeAllDisassemblers();
  23. uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
  24. uint8_t *BytesP = Bytes;
  25. const char OutStringSize = 100;
  26. char OutString[OutStringSize];
  27. LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
  28. nullptr, symbolLookupCallback);
  29. if (!DCR)
  30. return;
  31. size_t InstSize;
  32. unsigned NumBytes = sizeof(Bytes);
  33. unsigned PC = 0;
  34. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  35. OutStringSize);
  36. EXPECT_EQ(InstSize, 1U);
  37. EXPECT_EQ(StringRef(OutString), "\tnop");
  38. PC += InstSize;
  39. BytesP += InstSize;
  40. NumBytes -= InstSize;
  41. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  42. OutStringSize);
  43. EXPECT_EQ(InstSize, 1U);
  44. EXPECT_EQ(StringRef(OutString), "\tnop");
  45. PC += InstSize;
  46. BytesP += InstSize;
  47. NumBytes -= InstSize;
  48. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  49. OutStringSize);
  50. EXPECT_EQ(InstSize, 2U);
  51. EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
  52. LLVMDisasmDispose(DCR);
  53. }
  54. TEST(Disassembler, WebAssemblyTest) {
  55. llvm::InitializeAllTargetInfos();
  56. llvm::InitializeAllTargetMCs();
  57. llvm::InitializeAllDisassemblers();
  58. uint8_t Bytes[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
  59. uint8_t *BytesP = Bytes;
  60. const char OutStringSize = 100;
  61. char OutString[OutStringSize];
  62. LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
  63. 0, nullptr, symbolLookupCallback);
  64. if (!DCR)
  65. return;
  66. size_t InstSize;
  67. unsigned NumBytes = sizeof(Bytes);
  68. unsigned PC = 0;
  69. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  70. OutStringSize);
  71. EXPECT_EQ(InstSize, 1U);
  72. EXPECT_EQ(StringRef(OutString), "\ti32.add ");
  73. PC += InstSize;
  74. BytesP += InstSize;
  75. NumBytes -= InstSize;
  76. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  77. OutStringSize);
  78. EXPECT_EQ(InstSize, 2U);
  79. EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
  80. PC += InstSize;
  81. BytesP += InstSize;
  82. NumBytes -= InstSize;
  83. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  84. OutStringSize);
  85. EXPECT_EQ(InstSize, 3U);
  86. EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16:p2align=1");
  87. LLVMDisasmDispose(DCR);
  88. }