Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Jul 22, 2018
2 parents 14eeda7 + 3113025 commit 251a83e
Show file tree
Hide file tree
Showing 13 changed files with 7,395 additions and 7,217 deletions.
24 changes: 22 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
include(GenerateExportHeader)
include(GNUInstallDirs)

project(Zydis VERSION 2.0)
project(Zydis VERSION 2.0.2)

# =============================================================================================== #
# Overridable options #
Expand Down Expand Up @@ -67,8 +67,19 @@ endif ()
# Library configuration #
# =============================================================================================== #

add_library("Zydis")
function (_set_common_flags target)
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
target_compile_options("${target}" PRIVATE "-std=c99")
endif ()
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
target_compile_options("${target}" PRIVATE "-fPIC")
endif ()
endfunction ()

add_library("Zydis")
_set_common_flags("Zydis")
target_include_directories("Zydis"
PUBLIC "include" ${PROJECT_BINARY_DIR}
PRIVATE "src")
Expand Down Expand Up @@ -170,11 +181,13 @@ install(DIRECTORY "include/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if (ZYDIS_BUILD_EXAMPLES)
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
add_executable("FormatterHooks" "examples/FormatterHooks.c")
_set_common_flags("FormatterHooks")
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")
_set_common_flags("ZydisFuzzIn")
target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
Expand All @@ -187,6 +200,7 @@ if (ZYDIS_BUILD_EXAMPLES)
endif ()

add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
_set_common_flags("ZydisPerfTest")
target_link_libraries("ZydisPerfTest" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
Expand All @@ -199,6 +213,10 @@ if (ZYDIS_BUILD_EXAMPLES)
find_package(Threads REQUIRED)
target_link_libraries("ZydisPerfTest" Threads::Threads)
endif ()
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
find_package(Threads REQUIRED)
target_link_libraries("ZydisPerfTest" Threads::Threads)
endif ()
endif ()
endif ()

Expand All @@ -209,6 +227,7 @@ endif ()
if (ZYDIS_BUILD_TOOLS)
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
_set_common_flags("ZydisDisasm")
target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
Expand All @@ -218,6 +237,7 @@ if (ZYDIS_BUILD_TOOLS)
endif ()

add_executable("ZydisInfo" "tools/ZydisInfo.c")
_set_common_flags("ZydisInfo")
target_link_libraries("ZydisInfo" "Zydis")
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ Either use the [Visual Studio 2017 project](https://github.com/zyantific/zydis/t
## `ZydisInfo` tool
![ZydisInfo](https://raw.githubusercontent.com/zyantific/zydis/master/assets/screenshots/ZydisInfo.png)

## Bindings

Official bindings exist for a selection of languages:
- [Rust](https://github.com/zyantific/zydis-rs)
- [Pascal](https://github.com/zyantific/zydis-pascal)

Inofficial but actively maintained bindings:
- [Python 3](https://github.com/novogen/pydis)

## Credits
- Intel (for open-sourcing [XED](https://github.com/intelxed/xed), allowing for automatic comparision of our tables against theirs, improving both)
- [LLVM](https://llvm.org) (for providing pretty solid instruction data as well)
Expand Down
14 changes: 12 additions & 2 deletions examples/ZydisPerfTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#elif defined(ZYDIS_LINUX)
# include <sys/time.h>
# include <pthread.h>
#elif defined(ZYDIS_FREEBSD)
# include <sys/time.h>
# include <pthread.h>
# include <pthread_np.h>
#else
# error "Unsupported platform detected"
#endif
Expand Down Expand Up @@ -95,7 +99,7 @@ double GetCounter()

return (double)elapsed * timebaseInfo.numer / timebaseInfo.denom / 1000000;
}
#elif defined(ZYDIS_LINUX)
#elif defined(ZYDIS_LINUX) || defined(ZYDIS_FREEBSD)
struct timeval t1;

void StartCounter()
Expand Down Expand Up @@ -138,12 +142,18 @@ void adjustProcessAndThreadPriority()
}
}
#endif
#ifdef ZYDIS_LINUX
#if defined(ZYDIS_LINUX)
pthread_t thread = pthread_self();
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET(0, &cpus);
pthread_setaffinity_np(thread, sizeof(cpus), &cpus);
#elif defined(ZYDIS_FREEBSD)
pthread_t thread = pthread_self();
cpuset_t cpus;
CPU_ZERO(&cpus);
CPU_SET(0, &cpus);
pthread_setaffinity_np(thread, sizeof(cpus), &cpus);
#endif
}

Expand Down
22 changes: 22 additions & 0 deletions include/Zydis/DecoderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,33 @@ typedef ZydisU8 ZydisCPUFlagAction;
*/
enum ZydisCPUFlagActions
{
/**
* @brief The CPU flag is not touched by the instruction.
*/
ZYDIS_CPUFLAG_ACTION_NONE,
/**
* @brief The CPU flag is tested (read).
*/
ZYDIS_CPUFLAG_ACTION_TESTED,
/**
* @brief The CPU flag is tested and modified aferwards (read-write).
*/
ZYDIS_CPUFLAG_ACTION_TESTED_MODIFIED,
/**
* @brief The CPU flag is modified (write).
*/
ZYDIS_CPUFLAG_ACTION_MODIFIED,
/**
* @brief The CPU flag is set to 0 (write).
*/
ZYDIS_CPUFLAG_ACTION_SET_0,
/**
* @brief The CPU flag is set to 1 (write).
*/
ZYDIS_CPUFLAG_ACTION_SET_1,
/**
* @brief The CPU flag is undefined (write).
*/
ZYDIS_CPUFLAG_ACTION_UNDEFINED,

/**
Expand Down
8 changes: 7 additions & 1 deletion include/Zydis/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
#elif defined(__linux)
# define ZYDIS_LINUX
# define ZYDIS_POSIX
#elif defined(__FreeBSD__)
# define ZYDIS_FREEBSD
# define ZYDIS_POSIX
#elif defined(__unix)
# define ZYDIS_UNIX
# define ZYDIS_POSIX
Expand Down Expand Up @@ -170,7 +173,10 @@
#if __STDC_VERSION__ >= 201112L
# define ZYDIS_STATIC_ASSERT(x) _Static_assert(x, #x)
#else
# define ZYDIS_STATIC_ASSERT(x) typedef int ZYDIS_SASSERT_IMPL[(x) ? 1 : -1]
# define ZYDIS_MACRO_CONCAT2(x, y) x##y
# define ZYDIS_MACRO_CONCAT(x, y) ZYDIS_MACRO_CONCAT2(x, y)
# define ZYDIS_STATIC_ASSERT(x) \
typedef int ZYDIS_MACRO_CONCAT(ZYDIS_SASSERT_, __COUNTER__) [(x) ? 1 : -1]
#endif

/**
Expand Down
2 changes: 1 addition & 1 deletion include/Zydis/Zydis.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern "C" {
/**
* @brief A macro that defines the zydis version.
*/
#define ZYDIS_VERSION (ZydisU64)0x0002000000010000
#define ZYDIS_VERSION (ZydisU64)0x0002000000020000

/* ---------------------------------------------------------------------------------------------- */
/* Helper macros */
Expand Down
25 changes: 16 additions & 9 deletions src/Decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1999,11 +1999,11 @@ static ZydisStatus ZydisDecodeOperands(ZydisDecoderContext* context,
}

#if !defined(ZYDIS_DISABLE_EVEX) || !defined(ZYDIS_DISABLE_MVEX)
// Fix operand-action for EVEX instructions with merge-mask
// Fix operand-action for EVEX/MVEX instructions with merge-mask
if (instruction->avx.mask.reg && (instruction->avx.mask.mode == ZYDIS_MASK_MODE_MERGE) &&
!instruction->avx.mask.isControlMask)
{
ZYDIS_ASSERT(instruction->operandCount >= 2);
ZYDIS_ASSERT(instruction->operandCount >= 1);
switch (instruction->operands[0].action)
{
case ZYDIS_OPERAND_ACTION_WRITE:
Expand Down Expand Up @@ -4231,8 +4231,7 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
case ZYDIS_REG_CONSTRAINTS_MASK:
break;
case ZYDIS_REG_CONSTRAINTS_BND:
ZYDIS_ASSERT(!context->cache.X);
if (context->cache.B || instruction->raw.modrm.rm > 3)
if (context->cache.B || context->cache.X || instruction->raw.modrm.rm > 3)
{
return ZYDIS_STATUS_BAD_REGISTER;
}
Expand Down Expand Up @@ -4293,7 +4292,7 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
dest = dest | (context->cache.R << 3) | (context->cache.R2 << 4);
index = index | (context->cache.X << 3) | (context->cache.V2 << 4);
}
ZydisU8 mask = 0xFF;
ZydisU8 mask = 0xF0;

switch (instruction->encoding)
{
Expand All @@ -4312,10 +4311,18 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
break;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
ZYDIS_ASSERT((constrREG == ZYDIS_REG_CONSTRAINTS_NONE) &&
(constrRM == ZYDIS_REG_CONSTRAINTS_VSIB) &&
(constrNDSNDD == ZYDIS_REG_CONSTRAINTS_UNUSED));
break;
ZYDIS_ASSERT(((constrREG == ZYDIS_REG_CONSTRAINTS_UNUSED) ||
(constrREG == ZYDIS_REG_CONSTRAINTS_NONE)) &&
(constrRM == ZYDIS_REG_CONSTRAINTS_VSIB) &&
(constrNDSNDD == ZYDIS_REG_CONSTRAINTS_UNUSED));

// Some gather instructions (like `VGATHERPF0{D|Q}{PS|PD}`) doe not have a destination
// operand
if (constrREG == ZYDIS_REG_CONSTRAINTS_UNUSED)
{
dest = 0xF1;
}
break;
default:
ZYDIS_UNREACHABLE;
}
Expand Down
Loading

0 comments on commit 251a83e

Please sign in to comment.