Skip to content

Commit

Permalink
Use FetchContent to fetch Zydis and update Zydis version from 3.1.0 t…
Browse files Browse the repository at this point in the history
…o 4.0.0
  • Loading branch information
kubo committed Jul 5, 2023
1 parent 08e9901 commit 7cb8819
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
36 changes: 14 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,21 @@ endif ()
# zydis
#
if (DISASM_ZYDIS)
include(ExternalProject)
ExternalProject_Add(Zydis_src
GIT_REPOSITORY https://github.com/zyantific/zydis.git
GIT_TAG v3.1.0
GIT_SHALLOW TRUE
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DZYDIS_BUILD_SHARED_LIB=OFF
-DZYDIS_BUILD_EXAMPLES=OFF
-DZYDIS_BUILD_TOOLS=OFF
INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install && ${CMAKE_COMMAND} --build zycore --target install
FetchContent_Declare(
Zydis
GIT_REPOSITORY https://github.com/zyantific/zydis.git
GIT_TAG v4.0.0
GIT_SHALLOW TRUE
)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
add_library(Zydis STATIC IMPORTED)
set_property(TARGET Zydis PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}Zydis${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET Zydis PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/include)
FetchContent_GetProperties(Zydis)
if(NOT zydis_POPULATED)
FetchContent_Populate(Zydis)
set(ZYDIS_BUILD_SHARED_LIB OFF CACHE BOOL "")
set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "")
set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "")
add_subdirectory_pic(${zydis_SOURCE_DIR} ${zydis_BINARY_DIR})
endif()

list(APPEND FUNCHOOK_DEPS Zydis)
set(DISASM Zydis)
endif ()
Expand Down Expand Up @@ -220,9 +215,6 @@ configure_file(src/cmake_config.h.in config.h)

function (add_funchook_library target_name target_type)
add_library(${target_name} ${target_type} ${FUNCHOOK_SOURCES})
if (DISASM_ZYDIS)
add_dependencies(${target_name} Zydis_src)
endif ()
set_target_properties(${target_name} PROPERTIES ${FUNCHOOK_PROPERTIES})
target_include_directories(${target_name} PUBLIC include)
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # to include config.h
Expand Down
1 change: 1 addition & 0 deletions src/disasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef cs_insn funchook_insn_t;

typedef struct {
ZydisDecodedInstruction insn;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
size_t next_address;
} funchook_insn_t;

Expand Down
14 changes: 8 additions & 6 deletions src/disasm_Zydis.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@

#ifdef CPU_X86_64
#define MACHINE_MODE ZYDIS_MACHINE_MODE_LONG_64
#define ADDRESS_WIDTH ZYDIS_ADDRESS_WIDTH_64
#define STACK_WIDTH ZYDIS_STACK_WIDTH_64
#else
#define MACHINE_MODE ZYDIS_MACHINE_MODE_LONG_COMPAT_32
#define ADDRESS_WIDTH ZYDIS_ADDRESS_WIDTH_32
#define STACK_WIDTH ZYDIS_STACK_WIDTH_32
#endif

#define HEX(x) ((x) < 10 ? (x) + '0' : (x) - 10 + 'A')
Expand All @@ -54,7 +54,7 @@ int funchook_disasm_init(funchook_disasm_t *disasm, funchook_t *funchook, const
}

disasm->funchook = funchook;
ZydisDecoderInit(&disasm->decoder, MACHINE_MODE, ADDRESS_WIDTH);
ZydisDecoderInit(&disasm->decoder, MACHINE_MODE, STACK_WIDTH);
ZydisFormatterInit(&disasm->formatter, ZYDIS_FORMATTER_STYLE_INTEL);
disasm->insn.next_address = address;
disasm->code = code;
Expand All @@ -70,7 +70,8 @@ void funchook_disasm_cleanup(funchook_disasm_t *disasm)
int funchook_disasm_next(funchook_disasm_t *disasm, const funchook_insn_t **next_insn)
{
size_t code_size = disasm->code_end - disasm->code;
ZyanStatus status = ZydisDecoderDecodeBuffer(&disasm->decoder, disasm->code, code_size, &disasm->insn.insn);
ZyanStatus status = ZydisDecoderDecodeFull(&disasm->decoder, disasm->code, code_size,
&disasm->insn.insn, disasm->insn.operands);

if (ZYAN_SUCCESS(status)) {
disasm->insn.next_address += disasm->insn.insn.length;
Expand All @@ -97,7 +98,8 @@ void funchook_disasm_log_instruction(funchook_disasm_t *disasm, const funchook_i
char hex[24 * 3];
size_t i;

ZydisFormatterFormatInstruction(&disasm->formatter, &insn->insn, buffer, sizeof(buffer), addr);
ZydisFormatterFormatInstruction(&disasm->formatter, &insn->insn, insn->operands, insn->insn.operand_count,
buffer, sizeof(buffer), addr, ZYAN_NULL);

for (i = 0; i < size; i++) {
hex[i * 3 + 0] = HEX(code[i] >> 4);
Expand Down Expand Up @@ -127,7 +129,7 @@ void funchook_disasm_x86_rip_relative(funchook_disasm_t *disasm, const funchook_
if (insn->insn.raw.disp.offset != 0) {
int i;
for (i = 0; i < insn->insn.operand_count; i++) {
const ZydisDecodedOperand *op = &insn->insn.operands[i];
const ZydisDecodedOperand *op = &insn->operands[i];
if (op->mem.disp.has_displacement && op->mem.base == ZYDIS_REGISTER_RIP) {
// Fix IP-relative addressing such as:
// mov eax, dword ptr [rip + 0x236eda]
Expand Down

0 comments on commit 7cb8819

Please sign in to comment.