Minor refactorings

This commit is contained in:
flobernd 2017-09-20 15:46:51 +02:00
parent 92cfcdac00
commit 66972e43b4
10 changed files with 76 additions and 34 deletions

View File

@ -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[] =
{

View File

@ -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))
{

View File

@ -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;
}

View File

@ -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.
*/

View File

@ -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))
{

View File

@ -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;

View File

@ -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;

View File

@ -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)
{

View File

@ -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"));

View File

@ -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);