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