2020-10-31 14:31:13 -04:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "third-party/zydis/include/Zydis/Zydis.h"
|
|
|
|
|
|
|
|
TEST(Zydis, Basic) {
|
|
|
|
// int3, return
|
|
|
|
u8 code[] = {0xcc, 0xc3};
|
|
|
|
ZydisDecoder decoder;
|
2022-02-07 19:15:37 -05:00
|
|
|
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
|
2020-10-31 14:31:13 -04:00
|
|
|
ZydisFormatter formatter;
|
|
|
|
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
|
|
|
|
|
|
|
|
ZydisDecodedInstruction instr;
|
2022-02-07 19:15:37 -05:00
|
|
|
ZydisDecodedOperand op[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
|
2020-10-31 14:31:13 -04:00
|
|
|
|
|
|
|
// should get first instruction
|
2022-02-07 19:15:37 -05:00
|
|
|
EXPECT_TRUE(ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, code, 2, &instr, op,
|
|
|
|
ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
|
|
|
|
ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY)));
|
2020-10-31 14:31:13 -04:00
|
|
|
char result[256];
|
2022-02-07 19:15:37 -05:00
|
|
|
ZydisFormatterFormatInstruction(&formatter, &instr, op, instr.operand_count_visible, result,
|
|
|
|
sizeof(result), 0);
|
2020-10-31 14:31:13 -04:00
|
|
|
EXPECT_EQ(std::string("int3"), result);
|
|
|
|
EXPECT_EQ(1, instr.length);
|
|
|
|
|
|
|
|
// should get second instruction
|
2022-02-07 19:15:37 -05:00
|
|
|
EXPECT_TRUE(ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, code + 1, 1, &instr, op,
|
|
|
|
ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
|
|
|
|
ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY)));
|
|
|
|
ZydisFormatterFormatInstruction(&formatter, &instr, op, instr.operand_count_visible, result,
|
|
|
|
sizeof(result), 0);
|
2020-10-31 14:31:13 -04:00
|
|
|
EXPECT_EQ(std::string("ret"), result);
|
|
|
|
EXPECT_EQ(1, instr.length);
|
2022-02-07 19:15:37 -05:00
|
|
|
}
|