[obj2yaml] - Stop triggering UB when dumping corrupted strings.
[llvm-complete.git] / unittests / MC / Disassembler.cpp
blobcdb344474ca26d40a434e5c1dc9b62bf43052921
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 //===----------------------------------------------------------------------===//
9 #include "llvm-c/Disassembler.h"
10 #include "llvm/Support/TargetSelect.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
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;
23 TEST(Disassembler, X86Test) {
24 llvm::InitializeAllTargetInfos();
25 llvm::InitializeAllTargetMCs();
26 llvm::InitializeAllDisassemblers();
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;
37 size_t InstSize;
38 unsigned NumBytes = sizeof(Bytes);
39 unsigned PC = 0;
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;
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;
57 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
58 OutStringSize);
59 EXPECT_EQ(InstSize, 2U);
60 EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
62 LLVMDisasmDispose(DCR);
65 TEST(Disassembler, WebAssemblyTest) {
66 llvm::InitializeAllTargetInfos();
67 llvm::InitializeAllTargetMCs();
68 llvm::InitializeAllDisassemblers();
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;
79 size_t InstSize;
80 unsigned NumBytes = sizeof(Bytes);
81 unsigned PC = 0;
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;
91 InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
92 OutStringSize);
93 EXPECT_EQ(InstSize, 2U);
94 EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
96 PC += InstSize;
97 BytesP += InstSize;
98 NumBytes -= InstSize;
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");
105 LLVMDisasmDispose(DCR);