1 //===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm-c/Disassembler.h"
11 #include "llvm/Support/TargetSelect.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
symbolLookupCallback(void * DisInfo,uint64_t ReferenceValue,uint64_t * ReferenceType,uint64_t ReferencePC,const char ** ReferenceName)16 static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
17 uint64_t *ReferenceType,
18 uint64_t ReferencePC,
19 const char **ReferenceName) {
20 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
21 return nullptr;
22 }
23
TEST(Disassembler,X86Test)24 TEST(Disassembler, X86Test) {
25 llvm::InitializeAllTargetInfos();
26 llvm::InitializeAllTargetMCs();
27 llvm::InitializeAllDisassemblers();
28
29 uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
30 uint8_t *BytesP = Bytes;
31 const char OutStringSize = 100;
32 char OutString[OutStringSize];
33 LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
34 nullptr, symbolLookupCallback);
35 if (!DCR)
36 return;
37
38 size_t InstSize;
39 unsigned NumBytes = sizeof(Bytes);
40 unsigned PC = 0;
41
42 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
43 OutStringSize);
44 EXPECT_EQ(InstSize, 1U);
45 EXPECT_EQ(StringRef(OutString), "\tnop");
46 PC += InstSize;
47 BytesP += InstSize;
48 NumBytes -= InstSize;
49
50 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
51 OutStringSize);
52 EXPECT_EQ(InstSize, 1U);
53 EXPECT_EQ(StringRef(OutString), "\tnop");
54 PC += InstSize;
55 BytesP += InstSize;
56 NumBytes -= InstSize;
57
58 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
59 OutStringSize);
60 EXPECT_EQ(InstSize, 2U);
61 EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
62
63 LLVMDisasmDispose(DCR);
64 }
65
TEST(Disassembler,WebAssemblyTest)66 TEST(Disassembler, WebAssemblyTest) {
67 llvm::InitializeAllTargetInfos();
68 llvm::InitializeAllTargetMCs();
69 llvm::InitializeAllDisassemblers();
70
71 uint8_t Bytes[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
72 uint8_t *BytesP = Bytes;
73 const char OutStringSize = 100;
74 char OutString[OutStringSize];
75 LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
76 0, nullptr, symbolLookupCallback);
77 if (!DCR)
78 return;
79
80 size_t InstSize;
81 unsigned NumBytes = sizeof(Bytes);
82 unsigned PC = 0;
83
84 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
85 OutStringSize);
86 EXPECT_EQ(InstSize, 1U);
87 EXPECT_EQ(StringRef(OutString), "\ti32.add ");
88 PC += InstSize;
89 BytesP += InstSize;
90 NumBytes -= InstSize;
91
92 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
93 OutStringSize);
94 EXPECT_EQ(InstSize, 2U);
95 EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
96
97 PC += InstSize;
98 BytesP += InstSize;
99 NumBytes -= InstSize;
100
101 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
102 OutStringSize);
103 EXPECT_EQ(InstSize, 3U);
104 EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16, :p2align=1");
105
106 LLVMDisasmDispose(DCR);
107 }
108