2014-11-13 17:52:07 +01:00
|
|
|
//===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-11-13 17:52:07 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Disassembler.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
|
|
|
|
uint64_t *ReferenceType,
|
|
|
|
uint64_t ReferencePC,
|
|
|
|
const char **ReferenceName) {
|
|
|
|
*ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-11 00:16:44 +02:00
|
|
|
TEST(Disassembler, X86Test) {
|
2014-11-13 17:52:07 +01:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
|
|
|
|
uint8_t *BytesP = Bytes;
|
|
|
|
const char OutStringSize = 100;
|
|
|
|
char OutString[OutStringSize];
|
|
|
|
LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
|
|
|
|
nullptr, symbolLookupCallback);
|
|
|
|
if (!DCR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t InstSize;
|
|
|
|
unsigned NumBytes = sizeof(Bytes);
|
|
|
|
unsigned PC = 0;
|
|
|
|
|
2020-01-13 19:34:10 +01:00
|
|
|
InstSize =
|
|
|
|
LLVMDisasmInstruction(DCR, BytesP, 0, PC, OutString, OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 0U);
|
|
|
|
|
2014-11-13 17:52:07 +01:00
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tnop");
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tnop");
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 2U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
|
2014-11-15 11:53:12 +01:00
|
|
|
|
|
|
|
LLVMDisasmDispose(DCR);
|
2014-11-13 17:52:07 +01:00
|
|
|
}
|
2018-05-11 00:16:44 +02:00
|
|
|
|
|
|
|
TEST(Disassembler, WebAssemblyTest) {
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
uint8_t Bytes[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
|
|
|
|
uint8_t *BytesP = Bytes;
|
|
|
|
const char OutStringSize = 100;
|
|
|
|
char OutString[OutStringSize];
|
2018-07-17 01:09:29 +02:00
|
|
|
LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
|
|
|
|
0, nullptr, symbolLookupCallback);
|
2018-05-11 00:16:44 +02:00
|
|
|
if (!DCR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t InstSize;
|
|
|
|
unsigned NumBytes = sizeof(Bytes);
|
|
|
|
unsigned PC = 0;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
2018-06-19 02:02:34 +02:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti32.add ");
|
2018-05-11 00:16:44 +02:00
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 2U);
|
2018-06-19 02:02:34 +02:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
|
2018-05-11 00:16:44 +02:00
|
|
|
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 3U);
|
2018-08-27 17:45:51 +02:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16:p2align=1");
|
2018-05-11 00:16:44 +02:00
|
|
|
|
|
|
|
LLVMDisasmDispose(DCR);
|
|
|
|
}
|