Added basic support for Windows kernel drivers

- Manual typedefs for fixed width int types
- Custom `vsnprintf` function
- Disable ZYDIS_ASSERT and ZYDIS_UNREACHABLE
This commit is contained in:
Joel Höner 2017-07-28 22:25:20 +02:00
parent 5ac595eb72
commit 87394ef4da
8 changed files with 74 additions and 17 deletions

View File

@ -2,7 +2,7 @@
Zyan Disassembler Library (Zydis)
Original Author : Florian Bernd
Original Author : Florian Bernd, Joel Höner
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -32,18 +32,47 @@
#ifndef ZYDIS_COMMONTYPES_H
#define ZYDIS_COMMONTYPES_H
#include <Zydis/Defines.h>
/* ============================================================================================== */
/* Integral types */
/* ============================================================================================== */
/**
* uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t
*/
#include <stdint.h>
// Fixed width integer types.
#if defined(ZYDIS_WINKERNEL)
# if !defined(ZYDIS_MSVC)
# error "Windows kernel drivers are only supported with MSVC"
# endif
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
# define UINT8_MAX (255)
# define UINT16_MAX (65535U)
# define UINT32_MAX (4294967295UL)
# define UINT64_MAX (18446744073709551615ULL)
# define INT8_MAX (127)
# define INT8_MIN (-128)
# define INT16_MAX (32767)
# define INT16_MIN (-32767-1)
# define INT32_MIN (-2147483647L-1)
# define INT32_MAX (2147483647L)
# define INT64_MIN (-9223372036854775807LL-1)
# define INT64_MAX (9223372036854775807LL)
# define PRIX8 "hhX"
# define PRIX16 "hX"
# define PRIX32 "X"
# define PRIX64 "llX"
#else
# include <stdint.h>
# include <inttypes.h>
#endif
/**
* size_t, ptrdiff_t
*/
// size_t, ptrdiff_t
#include <stddef.h>
#ifdef __cplusplus

View File

@ -32,8 +32,6 @@
#ifndef ZYDIS_DEFINES_H
#define ZYDIS_DEFINES_H
#include <assert.h>
#include <stdlib.h>
#include <ZydisExportConfig.h>
/* ============================================================================================== */
@ -123,7 +121,12 @@
/* Debugging and optimization macros */
/* ============================================================================================== */
#define ZYDIS_ASSERT(condition) assert(condition)
#if defined(ZYDIS_WINKERNEL)
# define ZYDIS_ASSERT(condition)
#else
# include <assert.h>
# define ZYDIS_ASSERT(condition) assert(condition)
#endif
#if defined(ZYDIS_RELEASE)
# if defined(ZYDIS_GNUC)
@ -137,7 +140,10 @@
# else
# define ZYDIS_UNREACHABLE
# endif
#elif defined(ZYDIS_WINKERNEL)
# define ZYDIS_UNREACHABLE
#else
# include <stdlib.h>
# define ZYDIS_UNREACHABLE { assert(0); abort(); }
#endif

View File

@ -34,6 +34,8 @@
#include <Zydis/Defines.h>
#include <Zydis/Status.h>
#include <Zydis/Register.h>
#include <Zydis/Mnemonic.h>
#include <Zydis/SharedTypes.h>
#ifdef ZYDIS_ENABLE_FEATURE_DECODER
# include <Zydis/DecoderTypes.h>

View File

@ -32,7 +32,6 @@
#ifndef ZYDIS_UTILS_H
#define ZYDIS_UTILS_H
#include <stdint.h>
#include <Zydis/Defines.h>
#include <Zydis/Status.h>
#include <Zydis/DecoderTypes.h>

View File

@ -29,7 +29,6 @@
#include <EncoderData.h>
#include <string.h>
#include <stdint.h>
/* ============================================================================================== */
/* Internal context and table types */

View File

@ -28,9 +28,14 @@
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <Zydis/Formatter.h>
#include <Zydis/Utils.h>
#include <Zydis/CommonTypes.h>
#if defined(ZYDIS_WINKERNEL)
# include <ntddk.h>
# include <Ntstrsafe.h>
#endif
/* ============================================================================================== */
/* String formatting */
@ -68,6 +73,24 @@ enum ZydisStringBufferAppendModes
/* Internal functions */
/* ---------------------------------------------------------------------------------------------- */
#if defined(ZYDIS_WINKERNEL)
static int ZydisVSNPrintF(char* s, size_t n, const char* format, va_list arg)
{
size_t bytesRemaining;
NTSTATUS ret = RtlStringCchVPrintfExA(
s, n, NULL, &bytesRemaining, 0, format, arg
);
if (!NT_SUCCESS(ret)) return -1;
return (int)(n - bytesRemaining);
}
#else
static int ZydisVSNPrintF(char* s, size_t n, const char* format, va_list arg)
{
return vsnprintf(s, n, format, arg);
}
#endif
/**
* @brief Appends the @c text to the given @c buffer and increases the string-buffer pointer by
* the number of chars written.
@ -137,7 +160,7 @@ static ZydisStatus ZydisStringBufferAppendFormat(char** buffer, size_t bufferLen
va_list arglist;
va_start(arglist, format);
int w = vsnprintf(*buffer, bufferLen, format, arglist);
int w = ZydisVSNPrintF(*buffer, bufferLen, format, arglist);
if ((w < 0) || ((size_t)w >= bufferLen))
{
va_end(arglist);

View File

@ -24,7 +24,6 @@
***************************************************************************************************/
#include <stdint.h>
#include <Zydis/Utils.h>
/* ============================================================================================== */

View File

@ -95,7 +95,7 @@ int main(int argc, char** argv)
// TODO: Remove
// DEBUG CODE START
#if 1
#if 0
for (size_t i = 0; i < instruction.length; ++i)
{
printf("%02X ", *(readBuf + readOffs + i));