Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
athre0z committed Oct 17, 2017
2 parents 4a79d57 + 750808b commit 4dd6324
Show file tree
Hide file tree
Showing 54 changed files with 29,056 additions and 62,608 deletions.
90 changes: 50 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ project(Zydis VERSION 2.0)
option(ZYDIS_FEATURE_DECODER
"Enable instruction decoding and formtting functionality"
ON)
option(ZYDIS_FEATURE_ENCODER
"Enable instruction encoding functionality"
OFF)
#option(ZYDIS_FEATURE_ENCODER
# "Enable instruction encoding functionality"
# OFF)
option(ZYDIS_FEATURE_EVEX
"Enable support for EVEX instructions"
ON)
Expand All @@ -29,6 +29,9 @@ option(ZYDIS_FEATURE_CPUID
OFF)

# Build configuration
option(BUILD_SHARED_LIBS
"Build shared libraries"
OFF)
option(ZYDIS_BUILD_EXAMPLES
"Build examples"
ON)
Expand Down Expand Up @@ -70,11 +73,11 @@ target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPO
generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h")

if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER)
message(
FATAL_ERROR
"\nIt's dangerous to go alone! Take at least one of these:\n"
"[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER"
)
message(
FATAL_ERROR
"\nIt's dangerous to go alone! Take at least one of these:\n"
"[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER"
)
endif ()

if (ZYDIS_FEATURE_EVEX)
Expand All @@ -92,21 +95,23 @@ endif ()
if (ZYDIS_FEATURE_DECODER)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_ENABLE_FEATURE_DECODER")
endif ()
if (ZYDIS_FEATURE_ENCODER)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_ENABLE_FEATURE_ENCODER")
endif ()
#if (ZYDIS_FEATURE_ENCODER)
# target_compile_definitions("Zydis" PUBLIC "ZYDIS_ENABLE_FEATURE_ENCODER")
#endif ()

target_sources("Zydis"
PUBLIC
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/CommonTypes.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Defines.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/MetaInfo.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Mnemonic.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Register.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/SharedTypes.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Status.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Utils.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Zydis.h"
PRIVATE
"src/MetaInfo.c"
"src/Mnemonic.c"
"src/Register.c"
"src/SharedData.h"
Expand All @@ -122,20 +127,22 @@ if (ZYDIS_FEATURE_DECODER)
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h"
PRIVATE
"src/DecoderData.h"
"src/FormatHelper.h"
"src/Decoder.c"
"src/DecoderData.c"
"src/Formatter.c")
"src/Formatter.c"
"src/FormatHelper.c")
endif ()

if (ZYDIS_FEATURE_ENCODER)
target_sources("Zydis"
PUBLIC
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Encoder.h"
PRIVATE
"src/EncoderData.h"
"src/Encoder.c"
"src/EncoderData.c")
endif ()
#if (ZYDIS_FEATURE_ENCODER)
# target_sources("Zydis"
# PUBLIC
# "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Encoder.h"
# PRIVATE
# "src/EncoderData.h"
# "src/Encoder.c"
# "src/EncoderData.c")
#endif ()

if (BUILD_SHARED_LIBS AND WIN32)
target_sources("Zydis" PRIVATE "src/VersionInfo.rc")
Expand All @@ -153,42 +160,45 @@ install(DIRECTORY "include" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# =============================================================================================== #

if (ZYDIS_BUILD_EXAMPLES)
if (ZYDIS_FEATURE_DECODER)
add_executable("FormatterHooks"
"examples/FormatterHooks.c"
"examples/FormatHelper.h")
target_link_libraries("FormatterHooks" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_FEATURE_DECODER)
add_executable("FormatterHooks"
"examples/FormatterHooks.c"
"examples/FormatHelper.h")
target_link_libraries("FormatterHooks" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")

add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c")
target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")

if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
target_link_libraries("ZydisPerfTest" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
target_link_libraries("ZydisPerfTest" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_compile_definitions("ZydisPerfTest" PRIVATE "_GNU_SOURCE")
find_package(Threads REQUIRED)
target_link_libraries("ZydisPerfTest" Threads::Threads)
endif ()
endif ()
endif ()
endif ()

# =============================================================================================== #
# Tools #
# =============================================================================================== #

if (ZYDIS_BUILD_TOOLS)
if (ZYDIS_FEATURE_DECODER)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_FEATURE_DECODER)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")

add_executable("ZydisInfo" "tools/ZydisInfo.c")
target_link_libraries("ZydisInfo" "Zydis")
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif ()
endif ()
endif ()
47 changes: 32 additions & 15 deletions examples/FormatterHooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,30 @@ static const char* conditionCodeStrings[0x20] =
/*1F*/ "true_us"
};

/* ============================================================================================== */
/* Enums and Types */
/* ============================================================================================== */

/**
* @brief Custom user data struct.
*/
typedef struct ZydisCustomUserData_
{
ZydisBool ommitImmediate;
} ZydisCustomUserData;

/* ============================================================================================== */
/* Hook callbacks */
/* ============================================================================================== */

ZydisFormatterFormatFunc defaultPrintMnemonic;

static ZydisStatus ZydisFormatterPrintMnemonic(const ZydisFormatter* formatter,
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction)
char** buffer, size_t bufferLen, const ZydisDecodedInstruction* instruction,
ZydisCustomUserData* userData)
{
// We use the user-data field of the instruction-info to pass data to the
// @c ZydisFormatterFormatOperandImm function.
// In this case we are using a simple ordinal value, but you could pass a pointer to a
// complex datatype as well.
instruction->userData = (void*)1;
// We use the user-data to pass data to the @c ZydisFormatterFormatOperandImm function.
userData->ommitImmediate = ZYDIS_TRUE;

// Rewrite the instruction-mnemonic for the given instructions
if (instruction->operands[instruction->operandCount - 1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE)
Expand Down Expand Up @@ -142,31 +152,31 @@ static ZydisStatus ZydisFormatterPrintMnemonic(const ZydisFormatter* formatter,

// We did not rewrite the instruction-mnemonic. Signal the @c ZydisFormatterFormatOperandImm
// function not to omit the operand
instruction->userData = (void*)0;
userData->ommitImmediate = ZYDIS_FALSE;

// Default mnemonic printing
return defaultPrintMnemonic(formatter, buffer, bufferLen, instruction);
return defaultPrintMnemonic(formatter, buffer, bufferLen, instruction, userData);
}

/* ---------------------------------------------------------------------------------------------- */

ZydisFormatterFormatOperandFunc defaultFormatOperandImm;

static ZydisStatus ZydisFormatterFormatOperandImm(const ZydisFormatter* formatter,
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction,
ZydisDecodedOperand* operand)
char** buffer, size_t bufferLen, const ZydisDecodedInstruction* instruction,
const ZydisDecodedOperand* operand, ZydisCustomUserData* userData)
{
// The @c ZydisFormatterFormatMnemonic sinals us to omit the immediate (condition-code)
// operand, because it got replaced by the alias-mnemonic
if ((uintptr_t)instruction->userData == 1)
if (userData->ommitImmediate)
{
// The formatter will automatically omit the operand, if the buffer remains unchanged
// after the callback returns
return ZYDIS_STATUS_SUCCESS;
}

// Default immediate formatting
return defaultFormatOperandImm(formatter, buffer, bufferLen, instruction, operand);
return defaultFormatOperandImm(formatter, buffer, bufferLen, instruction, operand, userData);
}

/* ---------------------------------------------------------------------------------------------- */
Expand All @@ -184,17 +194,18 @@ void disassembleBuffer(ZydisDecoder* decoder, uint8_t* data, size_t length, Zydi

if (installHooks)
{
defaultPrintMnemonic = &ZydisFormatterPrintMnemonic;
defaultPrintMnemonic = (ZydisFormatterFormatFunc)&ZydisFormatterPrintMnemonic;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_HOOK_PRINT_MNEMONIC,
(const void**)&defaultPrintMnemonic);
defaultFormatOperandImm = &ZydisFormatterFormatOperandImm;
defaultFormatOperandImm = (ZydisFormatterFormatOperandFunc)&ZydisFormatterFormatOperandImm;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM,
(const void**)&defaultFormatOperandImm);
}

uint64_t instructionPointer = 0x007FFFFFFF400000;

ZydisDecodedInstruction instruction;
ZydisCustomUserData userData;
char buffer[256];
while (ZYDIS_SUCCESS(
ZydisDecoderDecodeBuffer(decoder, data, length, instructionPointer, &instruction)))
Expand All @@ -203,7 +214,8 @@ void disassembleBuffer(ZydisDecoder* decoder, uint8_t* data, size_t length, Zydi
length -= instruction.length;
instructionPointer += instruction.length;
printf("%016" PRIX64 " ", instruction.instrAddress);
ZydisFormatterFormatInstruction(&formatter, &instruction, &buffer[0], sizeof(buffer));
ZydisFormatterFormatInstructionEx(
&formatter, &instruction, &buffer[0], sizeof(buffer), &userData);
printf(" %s\n", &buffer[0]);
}
}
Expand All @@ -214,6 +226,11 @@ void disassembleBuffer(ZydisDecoder* decoder, uint8_t* data, size_t length, Zydi

int main()
{
if (ZydisGetVersion() != ZYDIS_VERSION)
{
fputs("Invalid zydis version\n", stderr);
return EXIT_FAILURE;
}

uint8_t data[] =
{
Expand Down
6 changes: 6 additions & 0 deletions examples/ZydisFuzzIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ typedef struct ZydisFuzzControlBlock_ {

int main()
{
if (ZydisGetVersion() != ZYDIS_VERSION)
{
fputs("Invalid zydis version\n", stderr);
return EXIT_FAILURE;
}

ZydisFuzzControlBlock controlBlock;
if (fread(&controlBlock, 1, sizeof(controlBlock), stdin) != sizeof(controlBlock))
{
Expand Down
Loading

0 comments on commit 4dd6324

Please sign in to comment.