From 66972e43b4ab9649722b985c06920c8b488b8935 Mon Sep 17 00:00:00 2001 From: flobernd Date: Wed, 20 Sep 2017 15:46:51 +0200 Subject: [PATCH] Minor refactorings --- examples/FormatterHooks.c | 5 ++++ examples/ZydisFuzzIn.c | 6 +++++ examples/ZydisPerfTest.c | 17 +++++++++---- include/Zydis/DecoderTypes.h | 9 ++++++- src/Decoder.c | 47 ++++++++++++++++++++---------------- src/Encoder.c | 2 +- src/FormatHelper.c | 4 +-- src/Formatter.c | 4 +-- tools/ZydisDisasm.c | 6 +++++ tools/ZydisInfo.c | 10 ++++++-- 10 files changed, 76 insertions(+), 34 deletions(-) diff --git a/examples/FormatterHooks.c b/examples/FormatterHooks.c index caba2a5..6a2733a 100644 --- a/examples/FormatterHooks.c +++ b/examples/FormatterHooks.c @@ -214,6 +214,11 @@ void disassembleBuffer(ZydisDecoder* decoder, uint8_t* data, size_t length, Zydi int main() { + if (ZydisGetVersion() != ZYDIS_VERSION) + { + fputs("Invalid zydis version\n", stderr); + return EXIT_FAILURE; + } uint8_t data[] = { diff --git a/examples/ZydisFuzzIn.c b/examples/ZydisFuzzIn.c index 22e28d7..fa4bfc9 100644 --- a/examples/ZydisFuzzIn.c +++ b/examples/ZydisFuzzIn.c @@ -56,6 +56,12 @@ typedef struct ZydisFuzzControlBlock_ { int main() { + if (ZydisGetVersion() != ZYDIS_VERSION) + { + fputs("Invalid zydis version\n", stderr); + return EXIT_FAILURE; + } + ZydisFuzzControlBlock controlBlock; if (fread(&controlBlock, 1, sizeof(controlBlock), stdin) != sizeof(controlBlock)) { diff --git a/examples/ZydisPerfTest.c b/examples/ZydisPerfTest.c index 2a1452e..28cdd27 100644 --- a/examples/ZydisPerfTest.c +++ b/examples/ZydisPerfTest.c @@ -125,15 +125,15 @@ void adjustProcessAndThreadPriority() { if (!SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR)1)) { - fputs("Warning: Could not set thread affinity mask.", stderr); + fputs("Warning: Could not set thread affinity mask\n", stderr); } if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) { - fputs("Warning: Could not set process priority class.", stderr); + fputs("Warning: Could not set process priority class\n", stderr); } if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)) { - fputs("Warning: Could not set thread priority class.", stderr); + fputs("Warning: Could not set thread priority class\n", stderr); } } #endif @@ -187,7 +187,7 @@ uint64_t processBuffer(const char* buffer, size_t length, ZydisDecodeGranularity ZYDIS_ASSERT(ZYDIS_SUCCESS(status)); if (!ZYDIS_SUCCESS(status)) { - puts("Unexpected decoding error"); + fputs("Unexpected decoding error\n", stderr); exit(EXIT_FAILURE); } ++count; @@ -314,6 +314,12 @@ void generateTestData(FILE* file, uint8_t encoding) int main(int argc, char** argv) { + if (ZydisGetVersion() != ZYDIS_VERSION) + { + fputs("Invalid zydis version\n", stderr); + return EXIT_FAILURE; + } + if (argc < 3 || (strcmp(argv[1], "-test") && strcmp(argv[1], "-generate"))) { fputs("Usage: PerfTest -[test|generate] [directory]\n", stderr); @@ -380,7 +386,8 @@ int main(int argc, char** argv) void* buffer = malloc(length); if (!buffer) { - fprintf(stderr, "Failed to allocate %" PRIu64 " on the heap", (uint64_t)length); + fprintf(stderr, + "Failed to allocate %" PRIu64 " bytes on the heap", (uint64_t)length); goto NextFile2; } diff --git a/include/Zydis/DecoderTypes.h b/include/Zydis/DecoderTypes.h index 977fd71..391dde3 100644 --- a/include/Zydis/DecoderTypes.h +++ b/include/Zydis/DecoderTypes.h @@ -90,7 +90,14 @@ typedef struct ZydisDecodedOperand_ /** * @brief Extended info for register-operands. */ - ZydisRegister reg; + struct + { + /** + * @brief The register value. + */ + ZydisRegister value; + // TODO: AVX512_4VNNIW MULTISOURCE registers + } reg; /** * @brief Extended info for memory-operands. */ diff --git a/src/Decoder.c b/src/Decoder.c index 777b7af..a0c4274 100644 --- a/src/Decoder.c +++ b/src/Decoder.c @@ -1082,7 +1082,8 @@ static void ZydisSetOperandSizeAndElementInfo(ZydisDecoderContext* context, } else { operand->size = (context->decoder->machineMode == 64) ? - ZydisRegisterGetWidth64(operand->reg) : ZydisRegisterGetWidth(operand->reg); + ZydisRegisterGetWidth64(operand->reg.value) : + ZydisRegisterGetWidth(operand->reg.value); } operand->elementType = ZYDIS_ELEMENT_TYPE_INT; operand->elementSize = operand->size; @@ -1305,30 +1306,33 @@ static ZydisStatus ZydisDecodeOperandRegister(ZydisDecodedInstruction* instructi { if ((instruction->attributes & ZYDIS_ATTRIB_HAS_REX) && (registerId >= 4)) { - operand->reg = ZYDIS_REGISTER_SPL + (registerId - 4); + operand->reg.value = ZYDIS_REGISTER_SPL + (registerId - 4); } else { - operand->reg = ZYDIS_REGISTER_AL + registerId; + operand->reg.value = ZYDIS_REGISTER_AL + registerId; } - if (operand->reg > ZYDIS_REGISTER_R15B) + if (operand->reg.value > ZYDIS_REGISTER_R15B) { return ZYDIS_STATUS_BAD_REGISTER; } } else { - operand->reg = ZydisRegisterEncode(registerClass, registerId); - if (!operand->reg) + operand->reg.value = ZydisRegisterEncode(registerClass, registerId); + if (!operand->reg.value) { return ZYDIS_STATUS_BAD_REGISTER; } - if ((operand->reg == ZYDIS_REGISTER_CR1) || - ((operand->reg >= ZYDIS_REGISTER_CR5) && (operand->reg <= ZYDIS_REGISTER_CR15) && - (operand->reg != ZYDIS_REGISTER_CR8))) + if ((operand->reg.value == ZYDIS_REGISTER_CR1) || + ((operand->reg.value >= ZYDIS_REGISTER_CR5) && + (operand->reg.value <= ZYDIS_REGISTER_CR15) && + (operand->reg.value != ZYDIS_REGISTER_CR8))) { return ZYDIS_STATUS_BAD_REGISTER; } - if ((operand->reg == ZYDIS_REGISTER_DR4) || (operand->reg == ZYDIS_REGISTER_DR5) || - ((operand->reg >= ZYDIS_REGISTER_DR8) && (operand->reg <= ZYDIS_REGISTER_DR15))) + if ((operand->reg.value == ZYDIS_REGISTER_DR4) || + (operand->reg.value == ZYDIS_REGISTER_DR5) || + ((operand->reg.value >= ZYDIS_REGISTER_DR8) && + (operand->reg.value <= ZYDIS_REGISTER_DR15))) { return ZYDIS_STATUS_BAD_REGISTER; } @@ -1550,7 +1554,7 @@ static void ZydisDecodeOperandImplicitRegister(ZydisDecoderContext* context, switch (definition->op.reg.type) { case ZYDIS_IMPLREG_TYPE_STATIC: - operand->reg = definition->op.reg.reg.reg; + operand->reg.value = definition->op.reg.reg.reg; break; case ZYDIS_IMPLREG_TYPE_GPR_OSZ: { @@ -1560,33 +1564,33 @@ static void ZydisDecodeOperandImplicitRegister(ZydisDecoderContext* context, ZYDIS_REGCLASS_GPR32, ZYDIS_REGCLASS_GPR64 }; - operand->reg = ZydisRegisterEncode(lookup[context->eoszIndex], definition->op.reg.reg.id); + operand->reg.value = ZydisRegisterEncode(lookup[context->eoszIndex], definition->op.reg.reg.id); break; } case ZYDIS_IMPLREG_TYPE_GPR_ASZ: - operand->reg = ZydisRegisterEncode( + operand->reg.value = ZydisRegisterEncode( (instruction->addressWidth == 16) ? ZYDIS_REGCLASS_GPR16 : (instruction->addressWidth == 32) ? ZYDIS_REGCLASS_GPR32 : ZYDIS_REGCLASS_GPR64, definition->op.reg.reg.id); break; case ZYDIS_IMPLREG_TYPE_GPR_SSZ: - operand->reg = ZydisRegisterEncode( + operand->reg.value = ZydisRegisterEncode( (context->decoder->addressWidth == 16) ? ZYDIS_REGCLASS_GPR16 : (context->decoder->addressWidth == 32) ? ZYDIS_REGCLASS_GPR32 : ZYDIS_REGCLASS_GPR64, definition->op.reg.reg.id); break; case ZYDIS_IMPLREG_TYPE_IP_ASZ: - operand->reg = + operand->reg.value = (instruction->addressWidth == 16) ? ZYDIS_REGISTER_IP : (instruction->addressWidth == 32) ? ZYDIS_REGISTER_EIP : ZYDIS_REGISTER_RIP; break; case ZYDIS_IMPLREG_TYPE_IP_SSZ: - operand->reg = + operand->reg.value = (context->decoder->addressWidth == 16) ? ZYDIS_REGISTER_EIP : (context->decoder->addressWidth == 32) ? ZYDIS_REGISTER_EIP : ZYDIS_REGISTER_RIP; break; case ZYDIS_IMPLREG_TYPE_FLAGS_SSZ: - operand->reg = + operand->reg.value = (context->decoder->addressWidth == 16) ? ZYDIS_REGISTER_FLAGS : (context->decoder->addressWidth == 32) ? ZYDIS_REGISTER_EFLAGS : ZYDIS_REGISTER_RFLAGS; break; @@ -1961,8 +1965,8 @@ FinalizeOperand: (instruction->avx.mask.mode == ZYDIS_MASK_MODE_MERGE) && (instruction->operandCount >= 3) && (instruction->operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER) && - (instruction->operands[1].reg >= ZYDIS_REGISTER_K1) && - (instruction->operands[1].reg <= ZYDIS_REGISTER_K7)) + (instruction->operands[1].reg.value >= ZYDIS_REGISTER_K1) && + (instruction->operands[1].reg.value <= ZYDIS_REGISTER_K7)) { switch (instruction->operands[0].type) { @@ -4298,7 +4302,8 @@ static ZydisStatus ZydisDecodeInstruction(ZydisDecoderContext* context, break; } ZYDIS_CHECK(ZydisDecodeOperands(context, instruction, definition)); - ZydisRegister reg = instruction->operands[instruction->operandCount - 1].reg; + ZydisRegister reg = + instruction->operands[instruction->operandCount - 1].reg.value; if ((reg == ZYDIS_REGISTER_FLAGS ) || (reg == ZYDIS_REGISTER_EFLAGS) || (reg == ZYDIS_REGISTER_RFLAGS)) { diff --git a/src/Encoder.c b/src/Encoder.c index c9529c5..fd1274c 100644 --- a/src/Encoder.c +++ b/src/Encoder.c @@ -1358,7 +1358,7 @@ ZydisStatus ZydisEncoderDecodedInstructionToRequest( switch (inOp->type) { case ZYDIS_OPERAND_TYPE_REGISTER: - outOp->reg = inOp->reg; + outOp->reg = inOp->reg.value; break; case ZYDIS_OPERAND_TYPE_MEMORY: outOp->mem.segment = inOp->mem.segment; diff --git a/src/FormatHelper.c b/src/FormatHelper.c index 9cf9c58..ddccdc4 100644 --- a/src/FormatHelper.c +++ b/src/FormatHelper.c @@ -76,7 +76,7 @@ ZydisStatus ZydisPrintDecU32(char** buffer, size_t bufferLen, uint32_t value, ui char temp[ZYDIS_MAXCHARS_DEC_32 + 1]; char *p = &temp[ZYDIS_MAXCHARS_DEC_32]; *p = '\0'; - while(value >= 100) + while (value >= 100) { uint32_t const old = value; p -= 2; @@ -180,7 +180,7 @@ ZydisStatus ZydisPrintDecU64(char** buffer, size_t bufferLen, uint64_t value, ui char temp[ZYDIS_MAXCHARS_DEC_64 + 1]; char *p = &temp[ZYDIS_MAXCHARS_DEC_64]; *p = '\0'; - while(value >= 100) + while (value >= 100) { uint64_t const old = value; p -= 2; diff --git a/src/Formatter.c b/src/Formatter.c index a757620..d85b22b 100644 --- a/src/Formatter.c +++ b/src/Formatter.c @@ -121,7 +121,7 @@ static ZydisStatus ZydisFormatterFormatOperandRegIntel(const ZydisFormatter* for return ZYDIS_STATUS_SUCCESS; } - const char* reg = ZydisRegisterGetString(operand->reg); + const char* reg = ZydisRegisterGetString(operand->reg.value); if (!reg) { reg = "invalid"; @@ -416,7 +416,7 @@ static ZydisStatus ZydisFormatterPrintOperandSizeIntel(const ZydisFormatter* for instruction->operands[0].size : 0; if (!typecast && (instruction->operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER) && - (instruction->operands[1].reg == ZYDIS_REGISTER_CL)) + (instruction->operands[1].reg.value == ZYDIS_REGISTER_CL)) { switch (instruction->mnemonic) { diff --git a/tools/ZydisDisasm.c b/tools/ZydisDisasm.c index f1b8b70..0b98d01 100644 --- a/tools/ZydisDisasm.c +++ b/tools/ZydisDisasm.c @@ -39,6 +39,12 @@ int main(int argc, char** argv) { + if (ZydisGetVersion() != ZYDIS_VERSION) + { + fputs("Invalid zydis version\n", stderr); + return EXIT_FAILURE; + } + if (argc < 1 || argc > 2) { fprintf(stderr, "Usage: %s [input file]\n", (argc > 0 ? argv[0] : "ZydisDisasm")); diff --git a/tools/ZydisInfo.c b/tools/ZydisInfo.c index 0bdbab7..17accbd 100644 --- a/tools/ZydisInfo.c +++ b/tools/ZydisInfo.c @@ -168,7 +168,7 @@ void printOperands(ZydisDecodedInstruction* instruction) switch (instruction->operands[i].type) { case ZYDIS_OPERAND_TYPE_REGISTER: - printf(" %27s", ZydisRegisterGetString(instruction->operands[i].reg)); + printf(" %27s", ZydisRegisterGetString(instruction->operands[i].reg.value)); break; case ZYDIS_OPERAND_TYPE_MEMORY: printf(" SEG =%20s\n", ZydisRegisterGetString(instruction->operands[i].mem.segment)); @@ -458,7 +458,7 @@ void printInstruction(ZydisDecodedInstruction* instruction) } if (ZydisRegisterGetClass( - instruction->operands[instruction->operandCount - 1].reg) == ZYDIS_REGCLASS_FLAGS) + instruction->operands[instruction->operandCount - 1].reg.value) == ZYDIS_REGCLASS_FLAGS) { puts(""); printFlags(instruction); @@ -490,6 +490,12 @@ void printInstruction(ZydisDecodedInstruction* instruction) int main(int argc, char** argv) { + if (ZydisGetVersion() != ZYDIS_VERSION) + { + fputs("Invalid zydis version\n", stderr); + return ZYDIS_STATUS_INVALID_OPERATION; + } + if (argc < 3) { fputs("Usage: ZydisInfo -[16|32|64] [hexbytes]\n", stderr);