Skip to content

Commit 251a83e

Browse files
committed
Merge branch 'develop'
2 parents 14eeda7 + 3113025 commit 251a83e

File tree

13 files changed

+7395
-7217
lines changed

13 files changed

+7395
-7217
lines changed

CMakeLists.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
22
include(GenerateExportHeader)
33
include(GNUInstallDirs)
44

5-
project(Zydis VERSION 2.0)
5+
project(Zydis VERSION 2.0.2)
66

77
# =============================================================================================== #
88
# Overridable options #
@@ -67,8 +67,19 @@ endif ()
6767
# Library configuration #
6868
# =============================================================================================== #
6969

70-
add_library("Zydis")
70+
function (_set_common_flags target)
71+
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
72+
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
73+
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
74+
target_compile_options("${target}" PRIVATE "-std=c99")
75+
endif ()
76+
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
77+
target_compile_options("${target}" PRIVATE "-fPIC")
78+
endif ()
79+
endfunction ()
7180

81+
add_library("Zydis")
82+
_set_common_flags("Zydis")
7283
target_include_directories("Zydis"
7384
PUBLIC "include" ${PROJECT_BINARY_DIR}
7485
PRIVATE "src")
@@ -170,11 +181,13 @@ install(DIRECTORY "include/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
170181
if (ZYDIS_BUILD_EXAMPLES)
171182
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
172183
add_executable("FormatterHooks" "examples/FormatterHooks.c")
184+
_set_common_flags("FormatterHooks")
173185
target_link_libraries("FormatterHooks" "Zydis")
174186
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
175187
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
176188

177189
add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c")
190+
_set_common_flags("ZydisFuzzIn")
178191
target_link_libraries("ZydisFuzzIn" "Zydis")
179192
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
180193
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
@@ -187,6 +200,7 @@ if (ZYDIS_BUILD_EXAMPLES)
187200
endif ()
188201

189202
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
203+
_set_common_flags("ZydisPerfTest")
190204
target_link_libraries("ZydisPerfTest" "Zydis")
191205
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
192206
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
@@ -199,6 +213,10 @@ if (ZYDIS_BUILD_EXAMPLES)
199213
find_package(Threads REQUIRED)
200214
target_link_libraries("ZydisPerfTest" Threads::Threads)
201215
endif ()
216+
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
217+
find_package(Threads REQUIRED)
218+
target_link_libraries("ZydisPerfTest" Threads::Threads)
219+
endif ()
202220
endif ()
203221
endif ()
204222

@@ -209,6 +227,7 @@ endif ()
209227
if (ZYDIS_BUILD_TOOLS)
210228
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
211229
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
230+
_set_common_flags("ZydisDisasm")
212231
target_link_libraries("ZydisDisasm" "Zydis")
213232
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
214233
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
@@ -218,6 +237,7 @@ if (ZYDIS_BUILD_TOOLS)
218237
endif ()
219238

220239
add_executable("ZydisInfo" "tools/ZydisInfo.c")
240+
_set_common_flags("ZydisInfo")
221241
target_link_libraries("ZydisInfo" "Zydis")
222242
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
223243
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ Either use the [Visual Studio 2017 project](https://github.com/zyantific/zydis/t
108108
## `ZydisInfo` tool
109109
![ZydisInfo](https://raw.githubusercontent.com/zyantific/zydis/master/assets/screenshots/ZydisInfo.png)
110110

111+
## Bindings
112+
113+
Official bindings exist for a selection of languages:
114+
- [Rust](https://github.com/zyantific/zydis-rs)
115+
- [Pascal](https://github.com/zyantific/zydis-pascal)
116+
117+
Inofficial but actively maintained bindings:
118+
- [Python 3](https://github.com/novogen/pydis)
119+
111120
## Credits
112121
- Intel (for open-sourcing [XED](https://github.com/intelxed/xed), allowing for automatic comparision of our tables against theirs, improving both)
113122
- [LLVM](https://llvm.org) (for providing pretty solid instruction data as well)

examples/ZydisPerfTest.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#elif defined(ZYDIS_LINUX)
4242
# include <sys/time.h>
4343
# include <pthread.h>
44+
#elif defined(ZYDIS_FREEBSD)
45+
# include <sys/time.h>
46+
# include <pthread.h>
47+
# include <pthread_np.h>
4448
#else
4549
# error "Unsupported platform detected"
4650
#endif
@@ -95,7 +99,7 @@ double GetCounter()
9599

96100
return (double)elapsed * timebaseInfo.numer / timebaseInfo.denom / 1000000;
97101
}
98-
#elif defined(ZYDIS_LINUX)
102+
#elif defined(ZYDIS_LINUX) || defined(ZYDIS_FREEBSD)
99103
struct timeval t1;
100104

101105
void StartCounter()
@@ -138,12 +142,18 @@ void adjustProcessAndThreadPriority()
138142
}
139143
}
140144
#endif
141-
#ifdef ZYDIS_LINUX
145+
#if defined(ZYDIS_LINUX)
142146
pthread_t thread = pthread_self();
143147
cpu_set_t cpus;
144148
CPU_ZERO(&cpus);
145149
CPU_SET(0, &cpus);
146150
pthread_setaffinity_np(thread, sizeof(cpus), &cpus);
151+
#elif defined(ZYDIS_FREEBSD)
152+
pthread_t thread = pthread_self();
153+
cpuset_t cpus;
154+
CPU_ZERO(&cpus);
155+
CPU_SET(0, &cpus);
156+
pthread_setaffinity_np(thread, sizeof(cpus), &cpus);
147157
#endif
148158
}
149159

include/Zydis/DecoderTypes.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,33 @@ typedef ZydisU8 ZydisCPUFlagAction;
520520
*/
521521
enum ZydisCPUFlagActions
522522
{
523+
/**
524+
* @brief The CPU flag is not touched by the instruction.
525+
*/
523526
ZYDIS_CPUFLAG_ACTION_NONE,
527+
/**
528+
* @brief The CPU flag is tested (read).
529+
*/
524530
ZYDIS_CPUFLAG_ACTION_TESTED,
531+
/**
532+
* @brief The CPU flag is tested and modified aferwards (read-write).
533+
*/
534+
ZYDIS_CPUFLAG_ACTION_TESTED_MODIFIED,
535+
/**
536+
* @brief The CPU flag is modified (write).
537+
*/
525538
ZYDIS_CPUFLAG_ACTION_MODIFIED,
539+
/**
540+
* @brief The CPU flag is set to 0 (write).
541+
*/
526542
ZYDIS_CPUFLAG_ACTION_SET_0,
543+
/**
544+
* @brief The CPU flag is set to 1 (write).
545+
*/
527546
ZYDIS_CPUFLAG_ACTION_SET_1,
547+
/**
548+
* @brief The CPU flag is undefined (write).
549+
*/
528550
ZYDIS_CPUFLAG_ACTION_UNDEFINED,
529551

530552
/**

include/Zydis/Defines.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
#elif defined(__linux)
6767
# define ZYDIS_LINUX
6868
# define ZYDIS_POSIX
69+
#elif defined(__FreeBSD__)
70+
# define ZYDIS_FREEBSD
71+
# define ZYDIS_POSIX
6972
#elif defined(__unix)
7073
# define ZYDIS_UNIX
7174
# define ZYDIS_POSIX
@@ -170,7 +173,10 @@
170173
#if __STDC_VERSION__ >= 201112L
171174
# define ZYDIS_STATIC_ASSERT(x) _Static_assert(x, #x)
172175
#else
173-
# define ZYDIS_STATIC_ASSERT(x) typedef int ZYDIS_SASSERT_IMPL[(x) ? 1 : -1]
176+
# define ZYDIS_MACRO_CONCAT2(x, y) x##y
177+
# define ZYDIS_MACRO_CONCAT(x, y) ZYDIS_MACRO_CONCAT2(x, y)
178+
# define ZYDIS_STATIC_ASSERT(x) \
179+
typedef int ZYDIS_MACRO_CONCAT(ZYDIS_SASSERT_, __COUNTER__) [(x) ? 1 : -1]
174180
#endif
175181

176182
/**

include/Zydis/Zydis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern "C" {
6464
/**
6565
* @brief A macro that defines the zydis version.
6666
*/
67-
#define ZYDIS_VERSION (ZydisU64)0x0002000000010000
67+
#define ZYDIS_VERSION (ZydisU64)0x0002000000020000
6868

6969
/* ---------------------------------------------------------------------------------------------- */
7070
/* Helper macros */

src/Decoder.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,11 +1999,11 @@ static ZydisStatus ZydisDecodeOperands(ZydisDecoderContext* context,
19991999
}
20002000

20012001
#if !defined(ZYDIS_DISABLE_EVEX) || !defined(ZYDIS_DISABLE_MVEX)
2002-
// Fix operand-action for EVEX instructions with merge-mask
2002+
// Fix operand-action for EVEX/MVEX instructions with merge-mask
20032003
if (instruction->avx.mask.reg && (instruction->avx.mask.mode == ZYDIS_MASK_MODE_MERGE) &&
20042004
!instruction->avx.mask.isControlMask)
20052005
{
2006-
ZYDIS_ASSERT(instruction->operandCount >= 2);
2006+
ZYDIS_ASSERT(instruction->operandCount >= 1);
20072007
switch (instruction->operands[0].action)
20082008
{
20092009
case ZYDIS_OPERAND_ACTION_WRITE:
@@ -4231,8 +4231,7 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
42314231
case ZYDIS_REG_CONSTRAINTS_MASK:
42324232
break;
42334233
case ZYDIS_REG_CONSTRAINTS_BND:
4234-
ZYDIS_ASSERT(!context->cache.X);
4235-
if (context->cache.B || instruction->raw.modrm.rm > 3)
4234+
if (context->cache.B || context->cache.X || instruction->raw.modrm.rm > 3)
42364235
{
42374236
return ZYDIS_STATUS_BAD_REGISTER;
42384237
}
@@ -4293,7 +4292,7 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
42934292
dest = dest | (context->cache.R << 3) | (context->cache.R2 << 4);
42944293
index = index | (context->cache.X << 3) | (context->cache.V2 << 4);
42954294
}
4296-
ZydisU8 mask = 0xFF;
4295+
ZydisU8 mask = 0xF0;
42974296

42984297
switch (instruction->encoding)
42994298
{
@@ -4312,10 +4311,18 @@ static ZydisStatus ZydisCheckErrorConditions(ZydisDecoderContext* context,
43124311
break;
43134312
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
43144313
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
4315-
ZYDIS_ASSERT((constrREG == ZYDIS_REG_CONSTRAINTS_NONE) &&
4316-
(constrRM == ZYDIS_REG_CONSTRAINTS_VSIB) &&
4317-
(constrNDSNDD == ZYDIS_REG_CONSTRAINTS_UNUSED));
4318-
break;
4314+
ZYDIS_ASSERT(((constrREG == ZYDIS_REG_CONSTRAINTS_UNUSED) ||
4315+
(constrREG == ZYDIS_REG_CONSTRAINTS_NONE)) &&
4316+
(constrRM == ZYDIS_REG_CONSTRAINTS_VSIB) &&
4317+
(constrNDSNDD == ZYDIS_REG_CONSTRAINTS_UNUSED));
4318+
4319+
// Some gather instructions (like `VGATHERPF0{D|Q}{PS|PD}`) doe not have a destination
4320+
// operand
4321+
if (constrREG == ZYDIS_REG_CONSTRAINTS_UNUSED)
4322+
{
4323+
dest = 0xF1;
4324+
}
4325+
break;
43194326
default:
43204327
ZYDIS_UNREACHABLE;
43214328
}

0 commit comments

Comments
 (0)