Minor refactorings

- The instruction pointer is now directly passed to the ZydisDecoderDecodeInstruction function
- Removed the user-data pointer in the ZydisOperandInfo struct
This commit is contained in:
flobernd 2017-01-12 20:14:12 +01:00
parent 689708fbd3
commit 5ead1d9345
4 changed files with 64 additions and 123 deletions

View File

@ -36,6 +36,8 @@
#include <inttypes.h> #include <inttypes.h>
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
#include "FormatHelper.h" #include "FormatHelper.h"
#include <stdlib.h>
#include <time.h>
/* ============================================================================================== */ /* ============================================================================================== */
/* Static data */ /* Static data */
@ -46,38 +48,38 @@
*/ */
static const char* conditionCodeStrings[0x20] = static const char* conditionCodeStrings[0x20] =
{ {
"eq", /*00*/ "eq",
"lt", /*01*/ "lt",
"le", /*02*/ "le",
"unord", /*03*/ "unord",
"neq", /*04*/ "neq",
"nlt", /*05*/ "nlt",
"nle", /*06*/ "nle",
"ord", /*07*/ "ord",
"eq_uq", /*08*/ "eq_uq",
"nge", /*09*/ "nge",
"ngt", /*0A*/ "ngt",
"false", /*0B*/ "false",
"oq", /*0C*/ "oq",
"ge", /*0D*/ "ge",
"gt", /*0E*/ "gt",
"true", /*0F*/ "true",
"eq_os", /*10*/ "eq_os",
"lt_oq", /*11*/ "lt_oq",
"le_oq", /*12*/ "le_oq",
"unord_s", /*13*/ "unord_s",
"neq_us", /*14*/ "neq_us",
"nlt_uq", /*15*/ "nlt_uq",
"nle_uq", /*16*/ "nle_uq",
"ord_s", /*17*/ "ord_s",
"eq_us", /*18*/ "eq_us",
"nge_uq", /*19*/ "nge_uq",
"ngt_uq", /*1A*/ "ngt_uq",
"false_os", /*1B*/ "false_os",
"neq_os", /*1C*/ "neq_os",
"ge_oq", /*1D*/ "ge_oq",
"gt_oq", /*1E*/ "gt_oq",
"true_us" /*1F*/ "true_us"
}; };
/* ============================================================================================== */ /* ============================================================================================== */
@ -175,7 +177,6 @@ void disassembleBuffer(uint8_t* data, size_t length, ZydisBool installHooks)
{ {
ZydisInstructionDecoder decoder; ZydisInstructionDecoder decoder;
ZydisDecoderInitInstructionDecoder(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT); ZydisDecoderInitInstructionDecoder(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT);
ZydisDecoderSetInstructionPointer(&decoder, 0x007FFFFFFF400000);
ZydisInstructionFormatter formatter; ZydisInstructionFormatter formatter;
ZydisFormatterInitInstructionFormatterEx(&formatter, ZYDIS_FORMATTER_STYLE_INTEL, ZydisFormatterInitInstructionFormatterEx(&formatter, ZYDIS_FORMATTER_STYLE_INTEL,
@ -191,13 +192,17 @@ void disassembleBuffer(uint8_t* data, size_t length, ZydisBool installHooks)
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM, ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM,
(const void**)&defaultFormatOperandImm); (const void**)&defaultFormatOperandImm);
} }
uint64_t instructionPointer = 0x007FFFFFFF400000;
ZydisInstructionInfo info; ZydisInstructionInfo info;
char buffer[256]; char buffer[256];
while (ZYDIS_SUCCESS(ZydisDecoderDecodeInstruction(&decoder, data, length, &info))) while (ZYDIS_SUCCESS(
ZydisDecoderDecodeInstruction(&decoder, data, length, instructionPointer, &info)))
{ {
data += info.length; data += info.length;
length -= info.length; length -= info.length;
instructionPointer += info.length;
printf("%016" PRIX64 " ", info.instrAddress); printf("%016" PRIX64 " ", info.instrAddress);
ZydisFormatterFormatInstruction(&formatter, &info, &buffer[0], sizeof(buffer)); ZydisFormatterFormatInstruction(&formatter, &info, &buffer[0], sizeof(buffer));
printf(" %s\n", &buffer[0]); printf(" %s\n", &buffer[0]);

View File

@ -79,11 +79,6 @@ typedef struct ZydisInstructionDecoder_
const uint8_t* buffer; const uint8_t* buffer;
size_t bufferLen; size_t bufferLen;
} input; } input;
// TODO: (Maybe) remove from this struct and pass as argument
/**
* @brief The current instruction-pointer value.
*/
uint64_t instructionPointer;
/** /**
* @brief Internal field. @c TRUE, if the @c imm8 value is already initialized. * @brief Internal field. @c TRUE, if the @c imm8 value is already initialized.
*/ */
@ -130,58 +125,37 @@ typedef struct ZydisInstructionDecoder_
ZYDIS_EXPORT ZydisStatus ZydisDecoderInitInstructionDecoder(ZydisInstructionDecoder* decoder, ZYDIS_EXPORT ZydisStatus ZydisDecoderInitInstructionDecoder(ZydisInstructionDecoder* decoder,
ZydisDisassemblerMode disassemblerMode); ZydisDisassemblerMode disassemblerMode);
/**
* @brief Returns the current instruction-pointer of the given @c ZydisInstructionDecoder
* instance.
*
* @param decoder A pointer to the @c ZydisInstructionDecoder instance.
* @param instructionPointer A pointer to the memory that receives the current
* instruction-pointer.
*
* @return A zydis status code.
*/
ZYDIS_EXPORT ZydisStatus ZydisDecoderGetInstructionPointer(
const ZydisInstructionDecoder* decoder, uint64_t* instructionPointer);
/**
* @brief Changes the instruction-pointer of the given @c ZydisInstructionDecoder instance.
*
* @param decoder A pointer to the @c ZydisInstructionDecoder instance.
* @param instructionPointer The new instruction-pointer value.
*
* @return A zydis status code.
*/
ZYDIS_EXPORT ZydisStatus ZydisDecoderSetInstructionPointer(ZydisInstructionDecoder* decoder,
uint64_t instructionPointer);
/** /**
* @brief Decodes the instruction in the given input @c buffer. * @brief Decodes the instruction in the given input @c buffer.
* *
* @param decoder A pointer to the @c ZydisInstructionDecoder instance. * @param decoder A pointer to the @c ZydisInstructionDecoder instance.
* @param buffer A pointer to the input buffer. * @param buffer A pointer to the input buffer.
* @param bufferLen The length of the input buffer. * @param bufferLen The length of the input buffer.
* @param info A pointer to the @c ZydisInstructionInfo struct, that receives the details * @param instructionPointer The instruction-pointer.
* about the decoded instruction. * @param info A pointer to the @c ZydisInstructionInfo struct, that receives the
* details about the decoded instruction.
* *
* @return A zydis status code. * @return A zydis status code.
*/ */
ZYDIS_EXPORT ZydisStatus ZydisDecoderDecodeInstruction(ZydisInstructionDecoder* decoder, ZYDIS_EXPORT ZydisStatus ZydisDecoderDecodeInstruction(ZydisInstructionDecoder* decoder,
const void* buffer, size_t bufferLen, ZydisInstructionInfo* info); const void* buffer, size_t bufferLen, uint64_t instructionPointer, ZydisInstructionInfo* info);
/** /**
* @brief Decodes the instruction in the given input @c buffer. * @brief Decodes the instruction in the given input @c buffer.
* *
* @param decoder A pointer to the @c ZydisInstructionDecoder instance. * @param decoder A pointer to the @c ZydisInstructionDecoder instance.
* @param buffer A pointer to the input buffer. * @param buffer A pointer to the input buffer.
* @param bufferLen The length of the input buffer. * @param bufferLen The length of the input buffer.
* @param flags Additional decoding flags. * @param instructionPointer The instruction-pointer.
* @param info A pointer to the @c ZydisInstructionInfo struct, that receives the details * @param flags Additional decoding flags.
* about the decoded instruction. * @param info A pointer to the @c ZydisInstructionInfo struct, that receives the
* details about the decoded instruction.
* *
* @return A zydis status code. * @return A zydis status code.
*/ */
ZYDIS_EXPORT ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder, ZYDIS_EXPORT ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder,
const void* buffer, size_t bufferLen, ZydisDecoderFlags flags, ZydisInstructionInfo* info); const void* buffer, size_t bufferLen, uint64_t instructionPointer, ZydisDecoderFlags flags,
ZydisInstructionInfo* info);
/* ============================================================================================== */ /* ============================================================================================== */

View File

@ -370,10 +370,6 @@ typedef struct ZydisOperandInfo_
*/ */
uint8_t dataOffset; uint8_t dataOffset;
} imm; } imm;
/**
* @brief This field is intended for custom data and may be freely set by the user.
*/
void* userData;
} ZydisOperandInfo; } ZydisOperandInfo;
/* ============================================================================================== */ /* ============================================================================================== */
@ -490,7 +486,7 @@ typedef uint64_t ZydisInstructionAttributes;
/** /**
* @brief The instruction has the EVEX prefix. * @brief The instruction has the EVEX prefix.
*/ */
#define ZYDIS_ATTRIB_HAS_EVEX 0x0000000000000020 #define ZYDIS_ATTRIB_HAS_EVEX 0x0000000000000020
/** /**
* @brief The instruction has one or more operands with position-relative offsets. * @brief The instruction has one or more operands with position-relative offsets.
*/ */

View File

@ -2275,40 +2275,18 @@ ZydisStatus ZydisDecoderInitInstructionDecoder(ZydisInstructionDecoder* decoder,
decoder->disassemblerMode = disassemblerMode; decoder->disassemblerMode = disassemblerMode;
decoder->input.buffer = NULL; decoder->input.buffer = NULL;
decoder->input.bufferLen = 0; decoder->input.bufferLen = 0;
decoder->instructionPointer = 0;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisDecoderGetInstructionPointer(const ZydisInstructionDecoder* decoder,
uint64_t* instructionPointer)
{
if (!decoder || !instructionPointer)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
*instructionPointer = decoder->instructionPointer;
return ZYDIS_STATUS_SUCCESS;
}
ZydisStatus ZydisDecoderSetInstructionPointer(ZydisInstructionDecoder* decoder,
uint64_t instructionPointer)
{
if (!decoder)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
decoder->instructionPointer = instructionPointer;
return ZYDIS_STATUS_SUCCESS;
}
ZydisStatus ZydisDecoderDecodeInstruction(ZydisInstructionDecoder* decoder, ZydisStatus ZydisDecoderDecodeInstruction(ZydisInstructionDecoder* decoder,
const void* buffer, size_t bufferLen, ZydisInstructionInfo* info) const void* buffer, size_t bufferLen, uint64_t instructionPointer, ZydisInstructionInfo* info)
{ {
return ZydisDecoderDecodeInstructionEx(decoder, buffer, bufferLen, 0, info); return ZydisDecoderDecodeInstructionEx(decoder, buffer, bufferLen, instructionPointer, 0, info);
} }
ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder, ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder,
const void* buffer, size_t bufferLen, ZydisDecoderFlags flags, ZydisInstructionInfo* info) const void* buffer, size_t bufferLen, uint64_t instructionPointer, ZydisDecoderFlags flags,
ZydisInstructionInfo* info)
{ {
(void)flags; (void)flags;
@ -2328,20 +2306,11 @@ ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder,
decoder->lastSegmentPrefix = 0; decoder->lastSegmentPrefix = 0;
decoder->imm8initialized = ZYDIS_FALSE; decoder->imm8initialized = ZYDIS_FALSE;
void* userData[6]; void* userData = info->userData;
for (int i = 0; i < 5; ++i)
{
userData[i] = info->operands[i].userData;
}
userData[5] = info->userData;
memset(info, 0, sizeof(*info)); memset(info, 0, sizeof(*info));
info->mode = decoder->disassemblerMode; info->mode = decoder->disassemblerMode;
info->instrAddress = decoder->instructionPointer; info->instrAddress = instructionPointer;
for (int i = 0; i < 5; ++i) info->userData = userData;
{
info->operands[i].userData = userData[i];
}
info->userData = userData[5];
ZYDIS_CHECK(ZydisCollectOptionalPrefixes(decoder, info)); ZYDIS_CHECK(ZydisCollectOptionalPrefixes(decoder, info));
ZYDIS_CHECK(ZydisDecodeOpcode(decoder, info)); ZYDIS_CHECK(ZydisDecodeOpcode(decoder, info));
@ -2426,9 +2395,6 @@ ZydisStatus ZydisDecoderDecodeInstructionEx(ZydisInstructionDecoder* decoder,
} }
} }
decoder->instructionPointer += info->length;
info->instrPointer = decoder->instructionPointer;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }