Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Zydis
https://github.com/zyantific/zydis
Admin
Fast and lightweight x86/x86-64 disassembler and code generation library
Tokens:
8,153
Snippets:
45
Trust Score:
8.9
License:
MIT
Update:
1 year ago
Context
Chat
Benchmark
57.38
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Configure Examples Build (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Determines whether the example programs for Zydis should be built. The 'auto' value indicates it will be built if dependencies are met or if explicitly enabled. ```configuration option( 'examples', type: 'feature', value: 'auto', description: 'Build examples', ) ``` -------------------------------- ### EncodeFromScratch: Assemble Function from Scratch Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Example assembling a basic function returning 0x1337 in rax from scratch using Zydis encoder. This demonstrates the process of generating machine code. ```c #include <Zydis/Zydis.h> // Placeholder for EncodeFromScratch.c content // Actual code would be here to demonstrate encoding instructions from scratch. ``` -------------------------------- ### ZydisWinKernel: Windows Kernel-Mode Driver Example Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Implements an example Windows kernel-mode driver that likely utilizes Zydis for its functionality. This showcases Zydis usage in a kernel environment. ```c #include <Zydis/Zydis.h> #include <ntddk.h> // Placeholder for ZydisWinKernel.c content // Actual code would be here for the Windows kernel driver. ``` -------------------------------- ### Formatter01: Basic Formatter Hooking with Custom Symbol Resolver Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Demonstrates basic hooking functionality of the ZydisFormatter class by implementing a custom symbol-resolver. This example showcases how to customize instruction formatting. ```c #include <Zydis/Zydis.h> // Placeholder for Formatter01.c content // Actual code would be here to demonstrate hooking and symbol resolution. ``` -------------------------------- ### Install Zydis using Package Managers Source: https://github.com/zyantific/zydis/blob/master/README.md Provides installation commands for Zydis across various package managers on different platforms. Ensure the correct command is used based on your operating system's package manager. ```shell pacman -S zydis ``` ```shell apt-get install libzydis-dev zydis-tools ``` ```shell brew install zydis ``` ```shell nix-shell -p zydis ``` ```shell vcpkg install zydis ``` -------------------------------- ### Formatter03: Formatter Tokenizing Feature Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Demonstrates the tokenizing feature of the ZydisFormatter class. This allows for granular control over the output of disassembled instructions. ```c #include <Zydis/Zydis.h> // Placeholder for Formatter03.c content // Actual code would be here to demonstrate the tokenizing feature. ``` -------------------------------- ### RewriteCode: Rewrite (Reassemble) Instructions Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Demonstrates how to rewrite or reassemble instructions using the Zydis encoder. This is useful for modifying existing machine code. ```c #include <Zydis/Zydis.h> // Placeholder for RewriteCode.c content // Actual code would be here to demonstrate rewriting instructions. ``` -------------------------------- ### Configure Manpages Build (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the building of manpages for Zydis tools. This option requires Ronn-NG to be installed. 'auto' means it will be built if dependencies are met or if explicitly selected. ```configuration option( 'man', type: 'feature', value: 'auto', description: 'Build manpages for the tools (requires Ronn-NG)', ) ``` -------------------------------- ### Generate Man Pages with Ronn Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt This CMake snippet handles the generation of man pages. It finds the `ronn` executable and then uses `add_custom_command` to convert `.ronn` files from the source directory into roff format. The generated man pages are then installed. ```cmake if (ZYDIS_BUILD_MAN) set(MAN_NAMES "ZydisDisasm.1" "ZydisInfo.1") find_program(RONN_BIN "ronn") foreach(MAN_NAME ${MAN_NAMES}) add_custom_command( OUTPUT ${MAN_NAME} COMMAND ${RONN_BIN} ARGS "--roff" "--output-dir=${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/man/${MAN_NAME}.ronn" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${MAN_NAME}" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") endforeach() add_custom_target(man ALL DEPENDS ${MAN_NAMES}) endif () ``` -------------------------------- ### Configure Doxygen Documentation Build (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the building of Doxygen documentation for Zydis. This option requires Doxygen to be installed. The 'auto' value indicates it will be built if dependencies are met or if explicitly chosen. ```configuration option( 'doc', type: 'feature', value: 'auto', description: 'Build doxygen documentation (requires Doxygen)', ) ``` -------------------------------- ### Zydis v4 Formatter API Changes Source: https://github.com/zyantific/zydis/blob/master/assets/porting-guide-v3-v4.md The Zydis v4 formatter API has been updated to accommodate the new decoder API. Arguments from the `Ex` variants of functions like `ZydisFormatterFormatInstruction` and `ZydisFormatterFormatOperand` have been integrated into the non-`Ex` variants. ```c #include "Zydis.h" // Example of the updated function signature: // ZydisFormatterFormatInstruction( // ZydisFormatter* formatter, // const ZydisDecodedInstruction* instruction, // const ZydisDecodedOperand* operands, // ZydisU8 operandCount, // ZydisString* output, // ZydisVoid* user_data // ); // Previously, there might have been an Ex variant with additional arguments. ``` -------------------------------- ### Zydis v4 Decoder API Additions Source: https://github.com/zyantific/zydis/blob/master/assets/porting-guide-v3-v4.md Zydis v4 introduces new functions for individual instruction and operand decoding, enhancing performance by allowing selective decoding when operands are not needed. These functions are `ZydisDecoderDecodeInstruction` and `ZydisDecoderDecodeOperands`. ```c #include "Zydis.h" // Example usage: ZydisDecoder decoder; ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); ZydisDecodedInstruction instruction; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; // Decode instruction only if (ZYAN_SUCCESS(ZydisDecoderDecodeInstruction(&decoder, data, size, &instruction))) { // Process instruction details without operands } // Decode instruction and operands if (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, data, size, &instruction, operands))) { // Process instruction and all its operands } ``` -------------------------------- ### Formatter02: Formatter Hooking and Operand Omission Source: https://github.com/zyantific/zydis/blob/master/examples/README.md Demonstrates basic hooking functionality of the ZydisFormatter class and the ability to completely omit specific operands. It rewrites mnemonics for CMPPS and CMPPD instructions. ```c #include <Zydis/Zydis.h> // Placeholder for Formatter02.c content // Actual code would be here to demonstrate operand omission and mnemonic rewriting. ``` -------------------------------- ### Zydis v4 Encoder API Addition Source: https://github.com/zyantific/zydis/blob/master/assets/porting-guide-v3-v4.md Zydis version 4 introduces an encoder component, with related header files (`Encoder.h`), new types, and functions for encoding assembly instructions. This is a significant new feature compared to previous versions. ```c #include "Zydis.h" #include "Zydis/Encoder.h" // Example of encoder usage (conceptual): // ZydisEncoder encoder; // ZydisEncoderInit(&encoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); // // ZydisEncoderRequest request; // ZydisEncoderOperation operands[ZYDIS_MAX_OPERAND_COUNT]; // /* ... populate request and operands ... */ // // ZydisDecodedInstruction instruction; // if (ZYAN_SUCCESS(ZydisEncoderEncodeInstruction(&encoder, &request, instruction.raw.bytes, sizeof(instruction.raw.bytes), &instruction.length))) { // // Instruction encoded successfully // } ``` -------------------------------- ### Zydis v4 Decoder Buffer Replacement Source: https://github.com/zyantific/zydis/blob/master/assets/porting-guide-v3-v4.md In Zydis v4, the `ZydisDecoderDecodeBuffer` function has been replaced by `ZydisDecoderDecodeFull`. This change is part of the API refinement for instruction decoding. ```c #include "Zydis.h" // Example of using the new function: ZydisDecoder decoder; ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); ZydisDecodedInstruction instruction; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; if (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, data, size, &instruction, operands))) { // Successfully decoded instruction and operands } ``` -------------------------------- ### Generate Doxygen Documentation Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt This snippet configures CMake to generate Doxygen documentation if Doxygen is found. It reads the Doxyfile, processes its content to set CMake variables, and then uses `doxygen_add_docs` to create the documentation. The generated HTML documentation is installed to the appropriate directory. ```cmake if (ZYDIS_BUILD_DOXYGEN) find_package(Doxygen) if (DOXYGEN_FOUND) # Read Doxygen options from the Doxyfile and set them as CMake variables # to accomodate doxygen_add_docs() file(READ "Doxyfile" DOXYFILE) # Remove comments string(REGEX REPLACE "\n?#[^\n]*\n" "" DOXYFILE ${DOXYFILE}) # Remove empty lines string(REGEX REPLACE "\n[ \t\r\n]" "\n" DOXYFILE "${DOXYFILE}") # Strip leading and trailing spaces string(STRIP "${DOXYFILE}" DOXYFILE) # Tranform the file in a list of '='-separated options string(REPLACE "\n" ";" DOXYFILE "${DOXYFILE}") foreach(option IN LISTS DOXYFILE) string(REPLACE "=" ";" option "${option}") list(GET option 0 opt_name) list(GET option 1 opt_value) string(STRIP "${opt_name}" opt_name) string(STRIP "${opt_value}" opt_value) if (opt_name STREQUAL "INPUT") # Save the INPUTs in a list to be used later string(REGEX REPLACE "[ ]+" ";" DOC_PATHS "${opt_value}") # Skip as the input files are not set by a DOXYGEN_INPUT variable continue() endif () if (opt_name STREQUAL "OUTPUT_DIRECTORY") # Skip as CMake writes the output files in the build directory continue() endif () set("DOXYGEN_${opt_name}" ${opt_value}) endforeach() set(DOXYGEN_QUIET YES) set(DOXYGEN_WARNINGS NO) set(DOXYGEN_WARN_IF_UNDOCUMENTED NO) doxygen_add_docs(ZydisDoc ${DOC_PATHS} ALL) install( DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html/" DESTINATION "${CMAKE_INSTALL_DOCDIR}/api" COMPONENT Documentation ) else () message("Can't generate documentation, Doxygen not found.") endif () endif () ``` -------------------------------- ### Build Zydis with CMake Source: https://github.com/zyantific/zydis/blob/master/README.md This bash script outlines the process of building the Zydis library using CMake. It first clones the repository recursively to include any submodules, then navigates into the cloned directory. Finally, it generates the build files using CMake and proceeds to build the project with parallel jobs. ```bash git clone --recursive 'https://github.com/zyantific/zydis.git' cd zydis cmake -B build cmake --build build -j4 ``` -------------------------------- ### Configure Tools Build (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Controls the building of associated tools for the Zydis project. 'auto' suggests it will be built if dependencies are satisfied or if explicitly chosen. ```configuration option( 'tools', type: 'feature', value: 'auto', description: 'Build tools', ) ``` -------------------------------- ### Configure Tests Build (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Determines whether the test suite for Zydis should be built. The 'auto' setting implies it will be built if dependencies are satisfied or if explicitly enabled. ```configuration option( 'tests', type: 'feature', value: 'auto', description: 'Build tests', ) ``` -------------------------------- ### Configure Minimal Mode (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the minimal mode for the Zydis decoder. When enabled, it forces the ZYDIS_DECODER_MODE_MINIMAL runtime option. ```configuration option( 'minimal', type: 'feature', value: 'disabled', description: 'Enable minimal mode (forces ZYDIS_DECODER_MODE_MINIMAL runtime option)', ) ``` -------------------------------- ### Configure Instruction Formatting (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the instruction formatting functionality of Zydis. The 'auto' setting indicates it will be built if prerequisites are satisfied or explicitly chosen. ```configuration option( 'formatter', type: 'feature', value: 'auto', description: 'Enable instruction formatting functionality', ) ``` -------------------------------- ### Configure KNC Support (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables support for Intel's Many Integrated Core (MIC) Architecture, often referred to as Knights Corner (KNC) instructions in Zydis. 'auto' implies it will be built if dependencies are met or if chosen. ```configuration option( 'knc', type: 'feature', value: 'auto', description: 'Enable support for KNC instructions', ) ``` -------------------------------- ### Configure Instruction Encoding (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the instruction encoding functionality of Zydis. 'auto' suggests it will be built based on available dependencies or explicit selection. ```configuration option( 'encoder', type: 'feature', value: 'auto', description: 'Enable instruction encoding functionality', ) ``` -------------------------------- ### Build ZydisInfo Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisInfo executable, which provides information about the Zydis library. It links against Zydis, includes shared tool source files, and conditionally defines ZYDIS_DISABLE_ENCODER if the encoder feature is not enabled. ```cmake add_executable("ZydisInfo" "tools/ZydisInfo.c" "tools/ZydisToolsShared.c" "tools/ZydisToolsShared.h") target_link_libraries("ZydisInfo" PUBLIC "Zydis") set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS") if (NOT ZYDIS_FEATURE_ENCODER) target_compile_definitions("ZydisInfo" PUBLIC "ZYDIS_DISABLE_ENCODER") endif () zyan_set_common_flags("ZydisInfo") zyan_maybe_enable_wpo("ZydisInfo") _maybe_set_emscripten_cfg("ZydisInfo") install(TARGETS "ZydisInfo" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ``` -------------------------------- ### Zydis Core Source Files (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Lists the core header and source files for the Zydis library, including general definitions, metadata, mnemonics, registers, and utility functions. ```cmake target_sources("Zydis" PRIVATE "${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/ShortString.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" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/SharedData.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/String.h" "src/MetaInfo.c" "src/Mnemonic.c" "src/Register.c" "src/SharedData.c" "src/String.c" "src/Utils.c" "src/Zydis.c") ``` -------------------------------- ### Configure Instruction Segment API (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the instruction segment API for Zydis. The 'auto' setting means it will be built if its dependencies are met or if explicitly selected. ```configuration option( 'segment', type: 'feature', value: 'auto', description: 'Enable instruction segment API', ) ``` -------------------------------- ### Encode x86/x86-64 Instructions Source: https://github.com/zyantific/zydis/blob/master/README.md This C code snippet illustrates the use of Zydis for encoding x86/x86-64 instructions. It demonstrates how to construct an instruction, such as a 'MOV' operation, and then encode it into its raw byte representation. This is useful for generating machine code dynamically or for tasks requiring direct manipulation of executable code. ```c /* * Example program that uses Zydis to encode machine code. */ #include <stdio.h> #include <string.h> #include <Zydis/Zydis.h> int main(int argc, char **argv) { // Initialize the encoder. ZydisEncoder encoder; ZydisEncoderInit(&encoder, ZydisMachineMode_LONG_64, ZydisAddressWidth_64); // Initialize the instruction to encode. ZydisDecodedInstruction instruction; memset(&instruction, 0, sizeof(instruction)); // We want to encode the following instruction: // MOV RAX, 0x0000000000001337 // Set the instruction details. instruction.mnemonic = ZydisMnemonic_MOV; instruction.operand_count_visible = 2; instruction.operand_count_encoded = 2; instruction.machine_mode = ZydisMachineMode_LONG_64; instruction.address_width = ZydisAddressWidth_64; instruction.attributes = ZydisInstructionEncodingAttributes_NONE; // Set the operands. ZydisEncoderOperandEncode(&encoder, &instruction, 0, ZydisOperandType_REGISTER, ZydisRegister_RAX, ZYAN_NULL); ZydisEncoderOperandEncode(&encoder, &instruction, 1, ZydisOperandType_IMMEDIATE, 0x1337, ZYAN_NULL); // Encode the instruction. Zyd8_t buffer[128]; Zyd8_t *encoded = buffer; ZydisStatus status = ZydisEncoderEncodeInstruction(&encoder, &instruction, encoded, sizeof(buffer), ZYAN_NULL); if (ZYAN_SUCCESS(status)) { // Print the encoded instruction. for (Zyd8_t i = 0; i < instruction.length; ++i) { printf("%02X ", encoded[i]); } printf("\n"); } else { fprintf(stderr, "Failed to encode instruction: %d\n", status); return 1; } return 0; } ``` -------------------------------- ### Disassemble x86/x86-64 Code Source: https://github.com/zyantific/zydis/blob/master/README.md This C code snippet demonstrates how to use the Zydis library to disassemble a given memory buffer containing x86 or x86-64 instructions. It prints the disassembled output to the console, showing the address, mnemonic, and operands for each instruction. This functionality is core to understanding the execution flow of machine code. ```c /* * Example program that uses Zydis to disassemble a given * memory buffer and prints the output to the console. */ #include <stdio.h> #include <string.h> #include <Zydis/Zydis.h> // A buffer that contains some sample machine code. static const Zyd8_t CODE[] = { 0x55, // push rcx 0x8D, 0x45, 0xFF, // lea eax, [rbp-0x01] 0x50, // push rax 0xFF, 0x75, 0x0C, // push qword ptr [rbp+0x0C] 0xFF, 0x75, 0x08, // push qword ptr [rbp+0x08] 0xE8, 0x00, 0x00, 0x00, 0x00, // call <relative or absolute> 0x48, 0x85, 0xC0, // test rax, rax 0x0F, 0x83, 0x15, 0x00, 0x00, 0x00 // js 0x007FFFFFFF42DB15 }; int main(int argc, char **argv) { // Initialize the deconstruction. ZydisDecoder decoder; ZydisDecoderInit(&decoder, ZydisMachineMode_LONG_64, ZydisAddressWidth_64); // Initialize the deconstruction. ZydisDecoder decoder; ZydisDecoderInit(&decoder, ZydisMachineMode_LONG_64, ZydisAddressWidth_64); // Initialize the formatter. ZydisFormatter formatter; ZydisFormatterInit(&formatter, ZydisOutputFormat_INTEL); // Enable runtime data ZydisFormatterSetOption(&formatter, ZydisFormatterOption_PRINT_EXEC_INFO, ZYAN_TRUE); // Pointer to the current instruction const Zyd8_t *ip = CODE; // Pointer to the end of the buffer const Zyd8_t *end = CODE + sizeof(CODE); // Disassemble the buffer. while (ip < end) { // Initialize the runtime instruction decoding structure. ZydisDecodedInstruction instruction; // Decode the instruction if (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(&decoder, ip, end - ip, &instruction))) { // Format the instruction. char buffer[256]; ZydisFormatterFormatInstruction(&formatter, &instruction, buffer, sizeof(buffer), ip - CODE, ZYAN_NULL); // Print the formatted instruction. printf("%016p %s\n", ip, buffer); // Advance the instruction pointer. ip += instruction.length; } else { // We can't decode this, so we'll just print a question mark. printf("%016p ???\n", ip); ip++; } } return 0; } ``` -------------------------------- ### Build ZydisDisasm Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisDisasm executable. This tool is used for disassembling code using the Zydis library. It links against the Zydis library and sets specific compile definitions and properties for the target. ```cmake add_executable("ZydisDisasm" "tools/ZydisDisasm.c" "tools/ZydisToolsShared.c" "tools/ZydisToolsShared.h") target_link_libraries("ZydisDisasm" PUBLIC "Zydis") set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS") zyan_set_common_flags("ZydisDisasm") zyan_maybe_enable_wpo("ZydisDisasm") _maybe_set_emscripten_cfg("ZydisDisasm") install(TARGETS "ZydisDisasm" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ``` -------------------------------- ### Zydis Target Properties and Flags (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Sets version information, SOVERSION, a define symbol for exporting, common flags, and potentially enables WPO (WholeProgram Optimization) for the Zydis target. ```cmake target_link_libraries("Zydis" PUBLIC "Zycore::Zycore") target_include_directories("Zydis" PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> PRIVATE "src") target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS") set_target_properties("Zydis" PROPERTIES VERSION "${Zydis_VERSION}" SOVERSION "${Zydis_VERSION_MAJOR}.${Zydis_VERSION_MINOR}" DEFINE_SYMBOL "ZYDIS_SHOULD_EXPORT") zyan_set_common_flags("Zydis") zyan_maybe_enable_wpo("Zydis") _maybe_set_fuzzer_cfg("Zydis") ``` -------------------------------- ### Build ZydisFuzzDecoder Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisFuzzDecoder executable, a fuzzer for the Zydis decoder. It links against the Zydis library and includes specific configurations for fuzzing, disabling the encoder if the ZYDIS_FEATURE_ENCODER is not enabled. ```cmake add_executable("ZydisFuzzDecoder" "tools/ZydisFuzzDecoder.c" "tools/ZydisFuzzShared.c" "tools/ZydisFuzzShared.h") target_link_libraries("ZydisFuzzDecoder" PUBLIC "Zydis") set_target_properties("ZydisFuzzDecoder" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisFuzzDecoder" PRIVATE "_CRT_SECURE_NO_WARNINGS") if (NOT ZYDIS_FEATURE_ENCODER) # For 'ZydisFuzzShared.c' target_compile_definitions("ZydisFuzzDecoder" PUBLIC "ZYDIS_DISABLE_ENCODER") endif () zyan_set_common_flags("ZydisFuzzDecoder") zyan_maybe_enable_wpo("ZydisFuzzDecoder") _maybe_set_emscripten_cfg("ZydisFuzzDecoder") _maybe_set_fuzzer_cfg("ZydisFuzzDecoder") ``` -------------------------------- ### Build ZydisFuzzReEncoding Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisFuzzReEncoding executable, designed for fuzzing re-encoding capabilities of Zydis. It depends on the Zydis library and utilizes shared fuzzing source files. Common flags and fuzzer configurations are applied. ```cmake add_executable("ZydisFuzzReEncoding" "tools/ZydisFuzzReEncoding.c" "tools/ZydisFuzzShared.c" "tools/ZydisFuzzShared.h") target_link_libraries("ZydisFuzzReEncoding" PUBLIC "Zydis") set_target_properties("ZydisFuzzReEncoding" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisFuzzReEncoding" PRIVATE "_CRT_SECURE_NO_WARNINGS") zyan_set_common_flags("ZydisFuzzReEncoding") zyan_maybe_enable_wpo("ZydisFuzzReEncoding") _maybe_set_emscripten_cfg("ZydisFuzzReEncoding") _maybe_set_fuzzer_cfg("ZydisFuzzReEncoding") ``` -------------------------------- ### Troubleshooting Shared Library Builds with -fPIC Source: https://github.com/zyantific/zydis/blob/master/README.md This snippet addresses a common issue when building shared libraries that link against a static Zydis library. It shows the linker error and provides the CMake flag to resolve it, ensuring proper relocation information is generated. ```bash /usr/bin/ld: ./libfoo.a(foo.c.o): relocation R_X86_64_PC32 against symbol `bar' can not be used when making a shared object; recompile with -fPIC ``` ```bash cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON .. ``` -------------------------------- ### Configure AVX-512 Support (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables support for Advanced Vector Extensions 512 (AVX-512) instructions within Zydis. The 'auto' value means it will be built if dependencies are met or if explicitly selected. ```configuration option( 'avx512', type: 'feature', value: 'auto', description: 'Enable support for AVX-512 instructions', ) ``` -------------------------------- ### Build ZydisTestEncoderAbsolute Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisTestEncoderAbsolute executable for testing absolute encoder functionality. This executable is built only when ZYDIS_BUILD_SHARED_LIB is not enabled. It links against the Zydis library and applies common build settings. ```cmake if (NOT ZYDIS_BUILD_SHARED_LIB) add_executable("ZydisTestEncoderAbsolute" "tools/ZydisTestEncoderAbsolute.c") target_link_libraries("ZydisTestEncoderAbsolute" PUBLIC "Zydis") set_target_properties("ZydisTestEncoderAbsolute" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisTestEncoderAbsolute" PRIVATE "_CRT_SECURE_NO_WARNINGS") zyan_set_common_flags("ZydisTestEncoderAbsolute") zyan_maybe_enable_wpo("ZydisTestEncoderAbsolute") _maybe_set_emscripten_cfg("ZydisTestEncoderAbsolute") endif () ``` -------------------------------- ### Build ZydisFuzzEncoder Executable (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Configures and builds the ZydisFuzzEncoder executable, a fuzzer for the Zydis encoder. This target is created only if ZYDIS_FEATURE_ENCODER is enabled. It links against Zydis and applies common flags and specific fuzzer configurations. ```cmake add_executable("ZydisFuzzEncoder" "tools/ZydisFuzzEncoder.c" "tools/ZydisFuzzShared.c" "tools/ZydisFuzzShared.h") target_link_libraries("ZydisFuzzEncoder" PUBLIC "Zydis") set_target_properties("ZydisFuzzEncoder" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisFuzzEncoder" PRIVATE "_CRT_SECURE_NO_WARNINGS") zyan_set_common_flags("ZydisFuzzEncoder") zyan_maybe_enable_wpo("ZydisFuzzEncoder") _maybe_set_emscripten_cfg("ZydisFuzzEncoder") _maybe_set_fuzzer_cfg("ZydisFuzzEncoder") ``` -------------------------------- ### Configure LLVM Fuzzer Mode (Boolean) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt A boolean option to enable LLVM libFuzzer mode. When enabled, it also reduces the amount of output printed by ZydisFuzzIn, optimizing for fuzzing. ```configuration option( 'llvm-fuzz', type: 'boolean', value: false, description: 'Enables LLVM libfuzzer mode and reduces prints in ZydisFuzzIn', ) ``` -------------------------------- ### Set Up Python Regression Tests Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt This section of the CMake script enables testing if `ZYDIS_BUILD_TESTS` is defined. It finds Python 3 and adds regression tests using Python scripts. Specific tests are added for `ZydisInfo` and encoder-related fuzzing targets, passing necessary executable paths as arguments. ```cmake if (ZYDIS_BUILD_TESTS) enable_testing() find_package(Python 3 REQUIRED) if (TARGET ZydisInfo) add_test( NAME "ZydisRegression" COMMAND "${Python_EXECUTABLE}" regression.py test $<TARGET_FILE:ZydisInfo> WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests" ) endif () if (TARGET ZydisFuzzReEncoding AND TARGET ZydisFuzzEncoder AND TARGET ZydisTestEncoderAbsolute) add_test( NAME "ZydisRegressionEncoder" COMMAND "${Python_EXECUTABLE}" regression_encoder.py $<TARGET_FILE:ZydisFuzzReEncoding> $<TARGET_FILE:ZydisFuzzEncoder> $<TARGET_FILE:ZydisTestEncoderAbsolute> WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests" ) endif () endif () ``` -------------------------------- ### Configure No C Standard Library (Boolean) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt A boolean option to control whether Zydis should avoid using any C standard library functions. This is typically used for environments like kernel drivers where the standard library might not be available or suitable. ```configuration option( 'nolibc', type: 'boolean', value: false, description: 'Do not use any C standard library functions (for exotic build-envs like kernel drivers)', ) ``` -------------------------------- ### Configure Instruction Decoding (Feature) Source: https://github.com/zyantific/zydis/blob/master/meson_options.txt Enables or disables the instruction decoding functionality of Zydis. The value 'auto' typically means it will be built if dependencies are met or if explicitly enabled. ```configuration option( 'decoder', type: 'feature', value: 'auto', description: 'Enable instruction decoding functionality', ) ``` -------------------------------- ### Zydis Decoder Source Files (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Conditionally adds source files for the Zydis decoder if ZYDIS_FEATURE_DECODER is enabled. Includes decoder-specific headers and implementation files. ```cmake if (ZYDIS_FEATURE_DECODER) target_sources("Zydis" PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Decoder.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/DecoderTypes.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/DecoderData.h" "src/Decoder.c" "src/DecoderData.c") if (ZYDIS_FEATURE_ENCODER) target_sources("Zydis" PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Encoder.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/EncoderData.h" "src/Encoder.c" "src/EncoderData.c") endif () if (ZYDIS_FEATURE_FORMATTER AND (NOT ZYDIS_MINIMAL_MODE)) target_sources("Zydis" PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Disassembler.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/FormatterBuffer.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/FormatterATT.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/FormatterBase.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/FormatterIntel.h" ``` -------------------------------- ### Zydis Feature Dependency Checks (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Ensures that certain features are enabled correctly. ZYDIS_FEATURE_FORMATTER requires ZYDIS_FEATURE_DECODER, and ZYDIS_FEATURE_ENCODER has several prerequisites. ```cmake if (ZYDIS_FEATURE_FORMATTER AND NOT ZYDIS_FEATURE_DECODER) message( FATAL_ERROR "\nZYDIS_FEATURE_FORMATTER requires ZYDIS_FEATURE_DECODER to be enabled" ) endif () if (ZYDIS_FEATURE_ENCODER AND (ZYDIS_MINIMAL_MODE OR NOT ZYDIS_FEATURE_DECODER OR NOT ZYDIS_FEATURE_AVX512 OR NOT ZYDIS_FEATURE_KNC)) message( FATAL_ERROR "\nZYDIS_FEATURE_ENCODER requires ZYDIS_FEATURE_DECODER in full mode (ZYDIS_MINIMAL_MODE \n disabled) with all ISA extensions (ZYDIS_FEATURE_AVX512 and ZYDIS_FEATURE_KNC enabled)" ) endif () ``` -------------------------------- ### Zydis Library Target Definition (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Defines the Zydis library as either SHARED or STATIC based on the ZYDIS_BUILD_SHARED_LIB flag. It also sets a public compile definition for static builds. ```cmake if (ZYDIS_BUILD_SHARED_LIB) add_library("Zydis" SHARED) else () add_library("Zydis" STATIC) target_compile_definitions("Zydis" PUBLIC "ZYDIS_STATIC_BUILD") endif () add_library("Zydis::Zydis" ALIAS "Zydis") ``` -------------------------------- ### Fuzzer Configuration Function (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt A CMake function to configure fuzzing options for a target. It sets compile definitions for AFL_FAST and LIBFUZZER, and adds sanitizer/library for MSVC when LIBFUZZER is enabled. ```cmake function (_maybe_set_fuzzer_cfg target) if (ZYDIS_FUZZ_AFL_FAST) target_compile_definitions("${target}" PRIVATE "ZYDIS_FUZZ_AFL_FAST") endif () if (ZYDIS_LIBFUZZER) target_compile_definitions("${target}" PRIVATE "ZYDIS_LIBFUZZER") if (MSVC) target_compile_options("${target}" PRIVATE "-fsanitize=fuzzer") target_link_libraries("${target}" PRIVATE "clang_rt.fuzzer-x86_64.lib") endif () endif () endfunction () ``` -------------------------------- ### Zydis Feature Compile Definitions (CMake) Source: https://github.com/zyantific/zydis/blob/master/CMakeLists.txt Sets compile definitions to enable or disable specific Zydis features based on CMake variables like ZYDIS_MINIMAL_MODE and ZYDIS_FEATURE_DECODER. ```cmake if (ZYDIS_MINIMAL_MODE) target_compile_definitions("Zydis" PUBLIC "ZYDIS_MINIMAL_MODE") endif () if (NOT ZYDIS_FEATURE_DECODER) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_DECODER") endif () if (NOT ZYDIS_FEATURE_ENCODER) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_ENCODER") endif () if (NOT ZYDIS_FEATURE_FORMATTER) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_FORMATTER") endif () if (NOT ZYDIS_FEATURE_AVX512) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_AVX512") endif () if (NOT ZYDIS_FEATURE_KNC) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_KNC") endif () if (NOT ZYDIS_FEATURE_SEGMENT) target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_SEGMENT") endif () ```