repo_name
string | dataset
string | owner
string | lang
string | func_name
string | code
string | docstring
string | url
string | sha
string |
|---|---|---|---|---|---|---|---|---|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeXOP
|
static ZyanStatus ZydisDecodeXOP(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZyanU8 data[3])
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(data[0] == 0x8F);
ZYAN_ASSERT(((data[1] >> 0) & 0x1F) >= 8);
ZYAN_ASSERT(instruction->raw.xop.offset == instruction->length - 3);
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_REAL_16)
{
// XOP is invalid in 16-bit real mode
return ZYDIS_STATUS_DECODING_ERROR;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_XOP;
instruction->raw.xop.R = (data[1] >> 7) & 0x01;
instruction->raw.xop.X = (data[1] >> 6) & 0x01;
instruction->raw.xop.B = (data[1] >> 5) & 0x01;
instruction->raw.xop.m_mmmm = (data[1] >> 0) & 0x1F;
if ((instruction->raw.xop.m_mmmm < 0x08) || (instruction->raw.xop.m_mmmm > 0x0A))
{
// Invalid according to the AMD documentation
return ZYDIS_STATUS_INVALID_MAP;
}
instruction->raw.xop.W = (data[2] >> 7) & 0x01;
instruction->raw.xop.vvvv = (data[2] >> 3) & 0x0F;
instruction->raw.xop.L = (data[2] >> 2) & 0x01;
instruction->raw.xop.pp = (data[2] >> 0) & 0x03;
// Update internal fields
context->vector_unified.W = instruction->raw.xop.W;
context->vector_unified.R = 0x01 & ~instruction->raw.xop.R;
context->vector_unified.X = 0x01 & ~instruction->raw.xop.X;
context->vector_unified.B = 0x01 & ~instruction->raw.xop.B;
context->vector_unified.L = instruction->raw.xop.L;
context->vector_unified.LL = instruction->raw.xop.L;
context->vector_unified.vvvv = (0x0F & ~instruction->raw.xop.vvvv);
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes the `XOP`-prefix.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param data The `XOP` bytes.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L402-L443
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeVEX
|
static ZyanStatus ZydisDecodeVEX(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZyanU8 data[3])
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT((data[0] == 0xC4) || (data[0] == 0xC5));
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_REAL_16)
{
// VEX is invalid in 16-bit real mode
return ZYDIS_STATUS_DECODING_ERROR;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_VEX;
switch (data[0])
{
case 0xC4:
ZYAN_ASSERT(instruction->raw.vex.offset == instruction->length - 3);
instruction->raw.vex.size = 3;
instruction->raw.vex.R = (data[1] >> 7) & 0x01;
instruction->raw.vex.X = (data[1] >> 6) & 0x01;
instruction->raw.vex.B = (data[1] >> 5) & 0x01;
instruction->raw.vex.m_mmmm = (data[1] >> 0) & 0x1F;
instruction->raw.vex.W = (data[2] >> 7) & 0x01;
instruction->raw.vex.vvvv = (data[2] >> 3) & 0x0F;
instruction->raw.vex.L = (data[2] >> 2) & 0x01;
instruction->raw.vex.pp = (data[2] >> 0) & 0x03;
break;
case 0xC5:
ZYAN_ASSERT(instruction->raw.vex.offset == instruction->length - 2);
instruction->raw.vex.size = 2;
instruction->raw.vex.R = (data[1] >> 7) & 0x01;
instruction->raw.vex.X = 1;
instruction->raw.vex.B = 1;
instruction->raw.vex.m_mmmm = 1;
instruction->raw.vex.W = 0;
instruction->raw.vex.vvvv = (data[1] >> 3) & 0x0F;
instruction->raw.vex.L = (data[1] >> 2) & 0x01;
instruction->raw.vex.pp = (data[1] >> 0) & 0x03;
break;
default:
ZYAN_UNREACHABLE;
}
// Map 0 is only valid for some KNC instructions
#ifdef ZYDIS_DISABLE_KNC
if ((instruction->raw.vex.m_mmmm == 0) || (instruction->raw.vex.m_mmmm > 0x03))
#else
if (instruction->raw.vex.m_mmmm > 0x03)
#endif
{
// Invalid according to the intel documentation
return ZYDIS_STATUS_INVALID_MAP;
}
// Update internal fields
context->vector_unified.W = instruction->raw.vex.W;
context->vector_unified.R = 0x01 & ~instruction->raw.vex.R;
context->vector_unified.X = 0x01 & ~instruction->raw.vex.X;
context->vector_unified.B = 0x01 & ~instruction->raw.vex.B;
context->vector_unified.L = instruction->raw.vex.L;
context->vector_unified.LL = instruction->raw.vex.L;
context->vector_unified.vvvv = (0x0F & ~instruction->raw.vex.vvvv);
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes the `VEX`-prefix.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param data The `VEX` bytes.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L454-L518
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeEVEX
|
static ZyanStatus ZydisDecodeEVEX(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZyanU8 data[4])
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(data[0] == 0x62);
ZYAN_ASSERT(instruction->raw.evex.offset == instruction->length - 4);
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_REAL_16)
{
// EVEX is invalid in 16-bit real mode
return ZYDIS_STATUS_DECODING_ERROR;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_EVEX;
instruction->raw.evex.R = (data[1] >> 7) & 0x01;
instruction->raw.evex.X = (data[1] >> 6) & 0x01;
instruction->raw.evex.B = (data[1] >> 5) & 0x01;
instruction->raw.evex.R2 = (data[1] >> 4) & 0x01;
if (data[1] & 0x08)
{
// Invalid according to the intel documentation
return ZYDIS_STATUS_MALFORMED_EVEX;
}
instruction->raw.evex.mmm = (data[1] >> 0) & 0x07;
if ((instruction->raw.evex.mmm == 0x00) ||
(instruction->raw.evex.mmm == 0x04) ||
(instruction->raw.evex.mmm == 0x07))
{
// Invalid according to the intel documentation
return ZYDIS_STATUS_INVALID_MAP;
}
instruction->raw.evex.W = (data[2] >> 7) & 0x01;
instruction->raw.evex.vvvv = (data[2] >> 3) & 0x0F;
ZYAN_ASSERT(((data[2] >> 2) & 0x01) == 0x01);
instruction->raw.evex.pp = (data[2] >> 0) & 0x03;
instruction->raw.evex.z = (data[3] >> 7) & 0x01;
instruction->raw.evex.L2 = (data[3] >> 6) & 0x01;
instruction->raw.evex.L = (data[3] >> 5) & 0x01;
instruction->raw.evex.b = (data[3] >> 4) & 0x01;
instruction->raw.evex.V2 = (data[3] >> 3) & 0x01;
if (!instruction->raw.evex.V2 &&
(instruction->machine_mode != ZYDIS_MACHINE_MODE_LONG_64))
{
return ZYDIS_STATUS_MALFORMED_EVEX;
}
instruction->raw.evex.aaa = (data[3] >> 0) & 0x07;
if (instruction->raw.evex.z && !instruction->raw.evex.aaa)
{
return ZYDIS_STATUS_INVALID_MASK; // TODO: Dedicated status code
}
// Update internal fields
context->vector_unified.W = instruction->raw.evex.W;
context->vector_unified.R = 0x01 & ~instruction->raw.evex.R;
context->vector_unified.X = 0x01 & ~instruction->raw.evex.X;
context->vector_unified.B = 0x01 & ~instruction->raw.evex.B;
context->vector_unified.LL = (data[3] >> 5) & 0x03;
context->vector_unified.R2 = 0x01 & ~instruction->raw.evex.R2;
context->vector_unified.V2 = 0x01 & ~instruction->raw.evex.V2;
context->vector_unified.vvvv = 0x0F & ~instruction->raw.evex.vvvv;
context->vector_unified.mask = instruction->raw.evex.aaa;
if (!instruction->raw.evex.V2 && (instruction->machine_mode != ZYDIS_MACHINE_MODE_LONG_64))
{
return ZYDIS_STATUS_MALFORMED_EVEX;
}
if (!instruction->raw.evex.b && (context->vector_unified.LL == 3))
{
// LL = 3 is only valid for instructions with embedded rounding control
return ZYDIS_STATUS_MALFORMED_EVEX;
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes the `EVEX`-prefix.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param data The `EVEX` bytes.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L530-L612
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeMVEX
|
static ZyanStatus ZydisDecodeMVEX(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZyanU8 data[4])
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(data[0] == 0x62);
ZYAN_ASSERT(instruction->raw.mvex.offset == instruction->length - 4);
if (instruction->machine_mode != ZYDIS_MACHINE_MODE_LONG_64)
{
// MVEX is only valid in 64-bit mode
return ZYDIS_STATUS_DECODING_ERROR;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_MVEX;
instruction->raw.mvex.R = (data[1] >> 7) & 0x01;
instruction->raw.mvex.X = (data[1] >> 6) & 0x01;
instruction->raw.mvex.B = (data[1] >> 5) & 0x01;
instruction->raw.mvex.R2 = (data[1] >> 4) & 0x01;
instruction->raw.mvex.mmmm = (data[1] >> 0) & 0x0F;
if (instruction->raw.mvex.mmmm > 0x03)
{
// Invalid according to the intel documentation
return ZYDIS_STATUS_INVALID_MAP;
}
instruction->raw.mvex.W = (data[2] >> 7) & 0x01;
instruction->raw.mvex.vvvv = (data[2] >> 3) & 0x0F;
ZYAN_ASSERT(((data[2] >> 2) & 0x01) == 0x00);
instruction->raw.mvex.pp = (data[2] >> 0) & 0x03;
instruction->raw.mvex.E = (data[3] >> 7) & 0x01;
instruction->raw.mvex.SSS = (data[3] >> 4) & 0x07;
instruction->raw.mvex.V2 = (data[3] >> 3) & 0x01;
instruction->raw.mvex.kkk = (data[3] >> 0) & 0x07;
// Update internal fields
context->vector_unified.W = instruction->raw.mvex.W;
context->vector_unified.R = 0x01 & ~instruction->raw.mvex.R;
context->vector_unified.X = 0x01 & ~instruction->raw.mvex.X;
context->vector_unified.B = 0x01 & ~instruction->raw.mvex.B;
context->vector_unified.R2 = 0x01 & ~instruction->raw.mvex.R2;
context->vector_unified.V2 = 0x01 & ~instruction->raw.mvex.V2;
context->vector_unified.LL = 2;
context->vector_unified.vvvv = 0x0F & ~instruction->raw.mvex.vvvv;
context->vector_unified.mask = instruction->raw.mvex.kkk;
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes the `MVEX`-prefix.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param data The `MVEX` bytes.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L625-L674
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeModRM
|
static void ZydisDecodeModRM(ZydisDecodedInstruction* instruction, ZyanU8 data)
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(!(instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM));
ZYAN_ASSERT(instruction->raw.modrm.offset == instruction->length - 1);
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
instruction->raw.modrm.mod = (data >> 6) & 0x03;
instruction->raw.modrm.reg = (data >> 3) & 0x07;
instruction->raw.modrm.rm = (data >> 0) & 0x07;
}
|
/**
* Decodes the `ModRM`-byte.
*
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param data The `ModRM` byte.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L683-L693
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeSIB
|
static void ZydisDecodeSIB(ZydisDecodedInstruction* instruction, ZyanU8 data)
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM);
ZYAN_ASSERT(instruction->raw.modrm.rm == 4);
ZYAN_ASSERT(!(instruction->attributes & ZYDIS_ATTRIB_HAS_SIB));
ZYAN_ASSERT(instruction->raw.sib.offset == instruction->length - 1);
instruction->attributes |= ZYDIS_ATTRIB_HAS_SIB;
instruction->raw.sib.scale = (data >> 6) & 0x03;
instruction->raw.sib.index = (data >> 3) & 0x07;
instruction->raw.sib.base = (data >> 0) & 0x07;
}
|
/**
* Decodes the `SIB`-byte.
*
* @param instruction A pointer to the `ZydisDecodedInstruction` struct
* @param data The `SIB` byte.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L701-L713
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisReadDisplacement
|
static ZyanStatus ZydisReadDisplacement(ZydisDecoderState* state,
ZydisDecodedInstruction* instruction, ZyanU8 size)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(instruction->raw.disp.size == 0);
instruction->raw.disp.size = size;
instruction->raw.disp.offset = instruction->length;
switch (size)
{
case 8:
{
ZyanU8 value;
ZYAN_CHECK(ZydisInputNext(state, instruction, &value));
instruction->raw.disp.value = *(ZyanI8*)&value;
break;
}
case 16:
{
ZyanU16 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 2));
instruction->raw.disp.value = *(ZyanI16*)&value;
break;
}
case 32:
{
ZyanU32 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 4));
instruction->raw.disp.value = *(ZyanI32*)&value;
break;
}
case 64:
{
ZyanU64 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 8));
instruction->raw.disp.value = *(ZyanI64*)&value;
break;
}
default:
ZYAN_UNREACHABLE;
}
// TODO: Fix endianess on big-endian systems
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/**
* Reads a displacement value.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param size The physical size of the displacement value.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L726-L773
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisReadImmediate
|
static ZyanStatus ZydisReadImmediate(ZydisDecoderState* state,
ZydisDecodedInstruction* instruction, ZyanU8 id, ZyanU8 size, ZyanBool is_signed,
ZyanBool is_relative)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT((id == 0) || (id == 1));
ZYAN_ASSERT(is_signed || !is_relative);
ZYAN_ASSERT(instruction->raw.imm[id].size == 0);
instruction->raw.imm[id].size = size;
instruction->raw.imm[id].offset = instruction->length;
instruction->raw.imm[id].is_signed = is_signed;
instruction->raw.imm[id].is_relative = is_relative;
switch (size)
{
case 8:
{
ZyanU8 value;
ZYAN_CHECK(ZydisInputNext(state, instruction, &value));
if (is_signed)
{
instruction->raw.imm[id].value.s = (ZyanI8)value;
} else
{
instruction->raw.imm[id].value.u = value;
}
break;
}
case 16:
{
ZyanU16 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 2));
if (is_signed)
{
instruction->raw.imm[id].value.s = (ZyanI16)value;
} else
{
instruction->raw.imm[id].value.u = value;
}
break;
}
case 32:
{
ZyanU32 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 4));
if (is_signed)
{
instruction->raw.imm[id].value.s = (ZyanI32)value;
} else
{
instruction->raw.imm[id].value.u = value;
}
break;
}
case 64:
{
ZyanU64 value;
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 8));
if (is_signed)
{
instruction->raw.imm[id].value.s = (ZyanI64)value;
} else
{
instruction->raw.imm[id].value.u = value;
}
break;
}
default:
ZYAN_UNREACHABLE;
}
// TODO: Fix endianess on big-endian systems
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Reads an immediate value.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param id The immediate id (either `0` or `1`).
* @param size The physical size of the immediate value.
* @param is_signed Signals, if the immediate value is signed.
* @param is_relative Signals, if the immediate value is a relative offset.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L787-L862
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCalcRegisterId
|
static ZyanU8 ZydisCalcRegisterId(const ZydisDecoderContext* context,
const ZydisDecodedInstruction* instruction, ZydisRegisterEncoding encoding,
ZydisRegisterClass register_class)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
// TODO: Combine OPCODE and IS4 in `ZydisPopulateRegisterIds` and get rid of this
// TODO: function entirely
switch (encoding)
{
case ZYDIS_REG_ENCODING_REG:
return context->reg_info.id_reg;
case ZYDIS_REG_ENCODING_NDSNDD:
return context->reg_info.id_ndsndd;
case ZYDIS_REG_ENCODING_RM:
return context->reg_info.id_rm;
case ZYDIS_REG_ENCODING_BASE:
return context->reg_info.id_base;
case ZYDIS_REG_ENCODING_INDEX:
case ZYDIS_REG_ENCODING_VIDX:
return context->reg_info.id_index;
case ZYDIS_REG_ENCODING_OPCODE:
{
ZYAN_ASSERT((register_class == ZYDIS_REGCLASS_GPR8) ||
(register_class == ZYDIS_REGCLASS_GPR16) ||
(register_class == ZYDIS_REGCLASS_GPR32) ||
(register_class == ZYDIS_REGCLASS_GPR64));
ZyanU8 value = (instruction->opcode & 0x0F);
if (value > 7)
{
value = value - 8;
}
if (instruction->machine_mode != ZYDIS_MACHINE_MODE_LONG_64)
{
return value;
}
return value | (context->vector_unified.B << 3);
}
case ZYDIS_REG_ENCODING_IS4:
{
if (instruction->machine_mode != ZYDIS_MACHINE_MODE_LONG_64)
{
return (instruction->raw.imm[0].value.u >> 4) & 0x07;
}
ZyanU8 value = (instruction->raw.imm[0].value.u >> 4) & 0x0F;
// We have to check the instruction-encoding, because the extension by bit [3] is only
// valid for EVEX and MVEX instructions
if ((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
switch (register_class)
{
case ZYDIS_REGCLASS_XMM:
case ZYDIS_REGCLASS_YMM:
case ZYDIS_REGCLASS_ZMM:
value |= ((instruction->raw.imm[0].value.u & 0x08) << 1);
default:
break;
}
}
return value;
}
case ZYDIS_REG_ENCODING_MASK:
return context->vector_unified.mask;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Calculates the register-id for a specific register-encoding and register-class.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the ` ZydisDecodedInstruction` struct.
* @param encoding The register-encoding.
* @param register_class The register-class.
*
* @return A zyan status code.
*
* This function calculates the register-id by combining different fields and flags of previously
* decoded structs.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L882-L951
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisSetOperandSizeAndElementInfo
|
static void ZydisSetOperandSizeAndElementInfo(const ZydisDecoderContext* context,
const ZydisDecodedInstruction* instruction, ZydisDecodedOperand* operand,
const ZydisOperandDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(operand);
ZYAN_ASSERT(definition);
// Operand size
switch (operand->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
{
if (definition->size[context->eosz_index])
{
operand->size = definition->size[context->eosz_index] * 8;
} else
{
operand->size = ZydisRegisterGetWidth(instruction->machine_mode,
operand->reg.value);
}
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = operand->size;
break;
}
case ZYDIS_OPERAND_TYPE_MEMORY:
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
if (operand->mem.type == ZYDIS_MEMOP_TYPE_AGEN)
{
ZYAN_ASSERT(definition->size[context->eosz_index] == 0);
operand->size = instruction->address_width;
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
} else
{
ZYAN_ASSERT(definition->size[context->eosz_index] ||
(instruction->meta.category == ZYDIS_CATEGORY_AMX_TILE));
operand->size = definition->size[context->eosz_index] * 8;
}
break;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
#ifndef ZYDIS_DISABLE_AVX512
if (definition->size[context->eosz_index])
{
// Operand size is hardcoded
operand->size = definition->size[context->eosz_index] * 8;
} else
{
// Operand size depends on the tuple-type, the element-size and the number of
// elements
ZYAN_ASSERT(instruction->avx.vector_length);
ZYAN_ASSERT(context->evex.element_size);
switch (context->evex.tuple_type)
{
case ZYDIS_TUPLETYPE_FV:
if (instruction->avx.broadcast.mode)
{
operand->size = context->evex.element_size;
} else
{
operand->size = instruction->avx.vector_length;
}
break;
case ZYDIS_TUPLETYPE_HV:
if (instruction->avx.broadcast.mode)
{
operand->size = context->evex.element_size;
} else
{
operand->size = (ZyanU16)instruction->avx.vector_length / 2;
}
break;
case ZYDIS_TUPLETYPE_QUARTER:
if (instruction->avx.broadcast.mode)
{
operand->size = context->evex.element_size;
}
else
{
operand->size = (ZyanU16)instruction->avx.vector_length / 4;
}
break;
default:
ZYAN_UNREACHABLE;
}
}
ZYAN_ASSERT(operand->size);
#else
ZYAN_UNREACHABLE;
#endif
break;
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
#ifndef ZYDIS_DISABLE_KNC
if (definition->size[context->eosz_index])
{
// Operand size is hardcoded
operand->size = definition->size[context->eosz_index] * 8;
} else
{
ZYAN_ASSERT(definition->element_type == ZYDIS_IELEMENT_TYPE_VARIABLE);
ZYAN_ASSERT(instruction->avx.vector_length == 512);
switch (instruction->avx.conversion.mode)
{
case ZYDIS_CONVERSION_MODE_INVALID:
operand->size = 512;
switch (context->mvex.functionality)
{
case ZYDIS_MVEX_FUNC_SF_32:
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UF_32:
case ZYDIS_MVEX_FUNC_DF_32:
operand->element_type = ZYDIS_ELEMENT_TYPE_FLOAT32;
operand->element_size = 32;
break;
case ZYDIS_MVEX_FUNC_SF_32_BCST:
operand->size = 256;
operand->element_type = ZYDIS_ELEMENT_TYPE_FLOAT32;
operand->element_size = 32;
break;
case ZYDIS_MVEX_FUNC_SI_32:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_DI_32:
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = 32;
break;
case ZYDIS_MVEX_FUNC_SI_32_BCST:
operand->size = 256;
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = 32;
break;
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_DF_64:
operand->element_type = ZYDIS_ELEMENT_TYPE_FLOAT64;
operand->element_size = 64;
break;
case ZYDIS_MVEX_FUNC_SI_64:
case ZYDIS_MVEX_FUNC_UI_64:
case ZYDIS_MVEX_FUNC_DI_64:
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = 64;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_CONVERSION_MODE_FLOAT16:
operand->size = 256;
operand->element_type = ZYDIS_ELEMENT_TYPE_FLOAT16;
operand->element_size = 16;
break;
case ZYDIS_CONVERSION_MODE_SINT16:
operand->size = 256;
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = 16;
break;
case ZYDIS_CONVERSION_MODE_UINT16:
operand->size = 256;
operand->element_type = ZYDIS_ELEMENT_TYPE_UINT;
operand->element_size = 16;
break;
case ZYDIS_CONVERSION_MODE_SINT8:
operand->size = 128;
operand->element_type = ZYDIS_ELEMENT_TYPE_INT;
operand->element_size = 8;
break;
case ZYDIS_CONVERSION_MODE_UINT8:
operand->size = 128;
operand->element_type = ZYDIS_ELEMENT_TYPE_UINT;
operand->element_size = 8;
break;
default:
ZYAN_UNREACHABLE;
}
switch (instruction->avx.broadcast.mode)
{
case ZYDIS_BROADCAST_MODE_INVALID:
// Nothing to do here
break;
case ZYDIS_BROADCAST_MODE_1_TO_8:
case ZYDIS_BROADCAST_MODE_1_TO_16:
operand->size = operand->element_size;
break;
case ZYDIS_BROADCAST_MODE_4_TO_8:
case ZYDIS_BROADCAST_MODE_4_TO_16:
operand->size = operand->element_size * 4;
break;
default:
ZYAN_UNREACHABLE;
}
}
#else
ZYAN_UNREACHABLE;
#endif
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_OPERAND_TYPE_POINTER:
ZYAN_ASSERT((instruction->raw.imm[0].size == 16) ||
(instruction->raw.imm[0].size == 32));
ZYAN_ASSERT( instruction->raw.imm[1].size == 16);
operand->size = instruction->raw.imm[0].size + instruction->raw.imm[1].size;
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
operand->size = definition->size[context->eosz_index] * 8;
break;
default:
ZYAN_UNREACHABLE;
}
// Element-type and -size
if (definition->element_type && (definition->element_type != ZYDIS_IELEMENT_TYPE_VARIABLE))
{
ZydisGetElementInfo(definition->element_type, &operand->element_type,
&operand->element_size);
if (!operand->element_size)
{
// The element size is the same as the operand size. This is used for single element
// scaling operands
operand->element_size = operand->size;
}
}
// Element count
if (operand->element_size && operand->size && (operand->element_type != ZYDIS_ELEMENT_TYPE_CC))
{
operand->element_count = operand->size / operand->element_size;
} else
{
operand->element_count = 1;
}
}
|
/**
* Sets the operand-size and element-specific information for the given operand.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operand A pointer to the `ZydisDecodedOperand` struct.
* @param definition A pointer to the `ZydisOperandDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L963-L1204
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeOperandRegister
|
static ZyanStatus ZydisDecodeOperandRegister(const ZydisDecodedInstruction* instruction,
ZydisDecodedOperand* operand, ZydisRegisterClass register_class, ZyanU8 register_id)
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(operand);
operand->type = ZYDIS_OPERAND_TYPE_REGISTER;
if (register_class == ZYDIS_REGCLASS_GPR8)
{
if ((instruction->attributes & ZYDIS_ATTRIB_HAS_REX) && (register_id >= 4))
{
operand->reg.value = ZYDIS_REGISTER_SPL + (register_id - 4);
} else
{
operand->reg.value = ZYDIS_REGISTER_AL + register_id;
}
} else
{
operand->reg.value = ZydisRegisterEncode(register_class, register_id);
ZYAN_ASSERT(operand->reg.value);
/*if (!operand->reg.value)
{
return ZYAN_STATUS_BAD_REGISTER;
}*/
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes an register-operand.
*
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operand A pointer to the `ZydisDecodedOperand` struct.
* @param register_class The register class.
* @param register_id The register id.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L1218-L1246
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeOperandMemory
|
static ZyanStatus ZydisDecodeOperandMemory(const ZydisDecoderContext* context,
const ZydisDecodedInstruction* instruction, ZydisDecodedOperand* operand,
ZydisRegisterClass vidx_register_class)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(operand);
ZYAN_ASSERT(instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM);
ZYAN_ASSERT(instruction->raw.modrm.mod != 3);
ZYAN_ASSERT(!vidx_register_class || ((instruction->raw.modrm.rm == 4) &&
((instruction->address_width == 32) || (instruction->address_width == 64))));
operand->type = ZYDIS_OPERAND_TYPE_MEMORY;
operand->mem.type = ZYDIS_MEMOP_TYPE_MEM;
const ZyanU8 modrm_rm = instruction->raw.modrm.rm;
ZyanU8 displacement_size = 0;
switch (instruction->address_width)
{
case 16:
{
static const ZydisRegister bases[] =
{
ZYDIS_REGISTER_BX, ZYDIS_REGISTER_BX, ZYDIS_REGISTER_BP, ZYDIS_REGISTER_BP,
ZYDIS_REGISTER_SI, ZYDIS_REGISTER_DI, ZYDIS_REGISTER_BP, ZYDIS_REGISTER_BX
};
static const ZydisRegister indices[] =
{
ZYDIS_REGISTER_SI, ZYDIS_REGISTER_DI, ZYDIS_REGISTER_SI, ZYDIS_REGISTER_DI,
ZYDIS_REGISTER_NONE, ZYDIS_REGISTER_NONE, ZYDIS_REGISTER_NONE, ZYDIS_REGISTER_NONE
};
operand->mem.base = bases[modrm_rm];
operand->mem.index = indices[modrm_rm];
operand->mem.scale = (operand->mem.index == ZYDIS_REGISTER_NONE) ? 0 : 1;
switch (instruction->raw.modrm.mod)
{
case 0:
if (modrm_rm == 6)
{
displacement_size = 16;
operand->mem.base = ZYDIS_REGISTER_NONE;
}
break;
case 1:
displacement_size = 8;
break;
case 2:
displacement_size = 16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
}
case 32:
{
operand->mem.base = ZYDIS_REGISTER_EAX + ZydisCalcRegisterId(context, instruction,
ZYDIS_REG_ENCODING_BASE, ZYDIS_REGCLASS_GPR32);
switch (instruction->raw.modrm.mod)
{
case 0:
if (modrm_rm == 5)
{
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
operand->mem.base = ZYDIS_REGISTER_EIP;
} else
{
operand->mem.base = ZYDIS_REGISTER_NONE;
}
displacement_size = 32;
}
break;
case 1:
displacement_size = 8;
break;
case 2:
displacement_size = 32;
break;
default:
ZYAN_UNREACHABLE;
}
if (modrm_rm == 4)
{
ZYAN_ASSERT(instruction->attributes & ZYDIS_ATTRIB_HAS_SIB);
operand->mem.index =
ZydisRegisterEncode(vidx_register_class ? vidx_register_class : ZYDIS_REGCLASS_GPR32,
ZydisCalcRegisterId(context, instruction,
vidx_register_class ? ZYDIS_REG_ENCODING_VIDX : ZYDIS_REG_ENCODING_INDEX,
vidx_register_class ? vidx_register_class : ZYDIS_REGCLASS_GPR32));
operand->mem.scale = (1 << instruction->raw.sib.scale);
if (operand->mem.index == ZYDIS_REGISTER_ESP)
{
operand->mem.index = ZYDIS_REGISTER_NONE;
operand->mem.scale = 0;
}
if (operand->mem.base == ZYDIS_REGISTER_EBP)
{
if (instruction->raw.modrm.mod == 0)
{
operand->mem.base = ZYDIS_REGISTER_NONE;
}
displacement_size = (instruction->raw.modrm.mod == 1) ? 8 : 32;
}
} else
{
operand->mem.index = ZYDIS_REGISTER_NONE;
operand->mem.scale = 0;
}
break;
}
case 64:
{
operand->mem.base = ZYDIS_REGISTER_RAX + ZydisCalcRegisterId(context, instruction,
ZYDIS_REG_ENCODING_BASE, ZYDIS_REGCLASS_GPR64);
switch (instruction->raw.modrm.mod)
{
case 0:
if (modrm_rm == 5)
{
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
operand->mem.base = ZYDIS_REGISTER_RIP;
} else
{
operand->mem.base = ZYDIS_REGISTER_NONE;
}
displacement_size = 32;
}
break;
case 1:
displacement_size = 8;
break;
case 2:
displacement_size = 32;
break;
default:
ZYAN_UNREACHABLE;
}
if ((modrm_rm & 0x07) == 4)
{
ZYAN_ASSERT(instruction->attributes & ZYDIS_ATTRIB_HAS_SIB);
operand->mem.index =
ZydisRegisterEncode(vidx_register_class ? vidx_register_class : ZYDIS_REGCLASS_GPR64,
ZydisCalcRegisterId(context, instruction,
vidx_register_class ? ZYDIS_REG_ENCODING_VIDX : ZYDIS_REG_ENCODING_INDEX,
vidx_register_class ? vidx_register_class : ZYDIS_REGCLASS_GPR64));
operand->mem.scale = (1 << instruction->raw.sib.scale);
if (operand->mem.index == ZYDIS_REGISTER_RSP)
{
operand->mem.index = ZYDIS_REGISTER_NONE;
operand->mem.scale = 0;
}
if ((operand->mem.base == ZYDIS_REGISTER_RBP) ||
(operand->mem.base == ZYDIS_REGISTER_R13))
{
if (instruction->raw.modrm.mod == 0)
{
operand->mem.base = ZYDIS_REGISTER_NONE;
}
displacement_size = (instruction->raw.modrm.mod == 1) ? 8 : 32;
}
} else
{
operand->mem.index = ZYDIS_REGISTER_NONE;
operand->mem.scale = 0;
}
break;
}
default:
ZYAN_UNREACHABLE;
}
if (displacement_size)
{
ZYAN_ASSERT(instruction->raw.disp.size == displacement_size);
operand->mem.disp.has_displacement = ZYAN_TRUE;
operand->mem.disp.value = instruction->raw.disp.value;
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes a memory operand.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operand A pointer to the `ZydisDecodedOperand` struct.
* @param vidx_register_class The register-class to use as the index register-class for
* instructions with `VSIB` addressing.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L1261-L1440
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeOperandImplicitRegister
|
static void ZydisDecodeOperandImplicitRegister(const ZydisDecoder* decoder,
const ZydisDecoderContext* context, const ZydisDecodedInstruction* instruction,
ZydisDecodedOperand* operand, const ZydisOperandDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(operand);
ZYAN_ASSERT(definition);
operand->type = ZYDIS_OPERAND_TYPE_REGISTER;
switch (definition->op.reg.type)
{
case ZYDIS_IMPLREG_TYPE_STATIC:
operand->reg.value = definition->op.reg.reg.reg;
break;
case ZYDIS_IMPLREG_TYPE_GPR_OSZ:
{
static const ZydisRegisterClass lookup[3] =
{
ZYDIS_REGCLASS_GPR16,
ZYDIS_REGCLASS_GPR32,
ZYDIS_REGCLASS_GPR64
};
operand->reg.value =
ZydisRegisterEncode(lookup[context->eosz_index], definition->op.reg.reg.id);
break;
}
case ZYDIS_IMPLREG_TYPE_GPR_ASZ:
operand->reg.value = ZydisRegisterEncode(
(instruction->address_width == 16) ? ZYDIS_REGCLASS_GPR16 :
(instruction->address_width == 32) ? ZYDIS_REGCLASS_GPR32 : ZYDIS_REGCLASS_GPR64,
definition->op.reg.reg.id);
break;
case ZYDIS_IMPLREG_TYPE_IP_ASZ:
operand->reg.value =
(instruction->address_width == 16) ? ZYDIS_REGISTER_IP :
(instruction->address_width == 32) ? ZYDIS_REGISTER_EIP : ZYDIS_REGISTER_RIP;
break;
case ZYDIS_IMPLREG_TYPE_GPR_SSZ:
operand->reg.value = ZydisRegisterEncode(
(decoder->stack_width == ZYDIS_STACK_WIDTH_16) ? ZYDIS_REGCLASS_GPR16 :
(decoder->stack_width == ZYDIS_STACK_WIDTH_32) ? ZYDIS_REGCLASS_GPR32 :
ZYDIS_REGCLASS_GPR64,
definition->op.reg.reg.id);
break;
case ZYDIS_IMPLREG_TYPE_IP_SSZ:
operand->reg.value =
(decoder->stack_width == ZYDIS_STACK_WIDTH_16) ? ZYDIS_REGISTER_EIP :
(decoder->stack_width == ZYDIS_STACK_WIDTH_32) ? ZYDIS_REGISTER_EIP :
ZYDIS_REGISTER_RIP;
break;
case ZYDIS_IMPLREG_TYPE_FLAGS_SSZ:
operand->reg.value =
(decoder->stack_width == ZYDIS_STACK_WIDTH_16) ? ZYDIS_REGISTER_FLAGS :
(decoder->stack_width == ZYDIS_STACK_WIDTH_32) ? ZYDIS_REGISTER_EFLAGS :
ZYDIS_REGISTER_RFLAGS;
break;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Decodes an implicit register operand.
*
* @param decoder A pointer to the `ZydisDecoder` instance.
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operand A pointer to the `ZydisDecodedOperand` struct.
* @param definition A pointer to the `ZydisOperandDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L1453-L1514
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeOperandImplicitMemory
|
static void ZydisDecodeOperandImplicitMemory(const ZydisDecoder* decoder,
const ZydisDecoderContext* context, const ZydisDecodedInstruction* instruction,
ZydisDecodedOperand* operand, const ZydisOperandDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(operand);
ZYAN_ASSERT(definition);
static const ZydisRegisterClass lookup[3] =
{
ZYDIS_REGCLASS_GPR16,
ZYDIS_REGCLASS_GPR32,
ZYDIS_REGCLASS_GPR64
};
operand->type = ZYDIS_OPERAND_TYPE_MEMORY;
operand->mem.type = ZYDIS_MEMOP_TYPE_MEM;
switch (definition->op.mem.base)
{
case ZYDIS_IMPLMEM_BASE_AGPR_REG:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index],
ZydisCalcRegisterId(context, instruction, ZYDIS_REG_ENCODING_REG,
lookup[context->easz_index]));
break;
case ZYDIS_IMPLMEM_BASE_AGPR_RM:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index],
ZydisCalcRegisterId(context, instruction, ZYDIS_REG_ENCODING_RM,
lookup[context->easz_index]));
break;
case ZYDIS_IMPLMEM_BASE_AAX:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index], 0);
break;
case ZYDIS_IMPLMEM_BASE_ADX:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index], 2);
break;
case ZYDIS_IMPLMEM_BASE_ABX:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index], 3);
break;
case ZYDIS_IMPLMEM_BASE_ASI:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index], 6);
break;
case ZYDIS_IMPLMEM_BASE_ADI:
operand->mem.base = ZydisRegisterEncode(lookup[context->easz_index], 7);
break;
case ZYDIS_IMPLMEM_BASE_SSP:
operand->mem.base = ZydisRegisterEncode(lookup[decoder->stack_width], 4);
break;
case ZYDIS_IMPLMEM_BASE_SBP:
operand->mem.base = ZydisRegisterEncode(lookup[decoder->stack_width], 5);
break;
default:
ZYAN_UNREACHABLE;
}
if (definition->op.mem.seg)
{
operand->mem.segment =
ZydisRegisterEncode(ZYDIS_REGCLASS_SEGMENT, definition->op.mem.seg - 1);
ZYAN_ASSERT(operand->mem.segment);
}
}
|
/**
* Decodes an implicit memory operand.
*
* @param decoder A pointer to the `ZydisDecoder` instance.
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operand A pointer to the `ZydisDecodedOperand` struct.
* @param definition A pointer to the `ZydisOperandDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L1527-L1588
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisSetAttributes
|
static void ZydisSetAttributes(ZydisDecoderState* state, ZydisDecodedInstruction* instruction,
const ZydisInstructionDefinition* definition)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(definition);
if (definition->cpu_state != ZYDIS_RW_ACTION_NONE)
{
static const ZydisInstructionAttributes mapping[ZYDIS_RW_ACTION_MAX_VALUE + 1] =
{
/* NONE */ 0,
/* READ */ ZYDIS_ATTRIB_CPU_STATE_CR,
/* WRITE */ ZYDIS_ATTRIB_CPU_STATE_CW,
/* READWRITE */ ZYDIS_ATTRIB_CPU_STATE_CR | ZYDIS_ATTRIB_CPU_STATE_CW
};
ZYAN_ASSERT(definition->cpu_state < ZYAN_ARRAY_LENGTH(mapping));
instruction->attributes |= mapping[definition->cpu_state];
}
if (definition->fpu_state != ZYDIS_RW_ACTION_NONE)
{
static const ZydisInstructionAttributes mapping[ZYDIS_RW_ACTION_MAX_VALUE + 1] =
{
/* NONE */ 0,
/* READ */ ZYDIS_ATTRIB_FPU_STATE_CR,
/* WRITE */ ZYDIS_ATTRIB_FPU_STATE_CW,
/* READWRITE */ ZYDIS_ATTRIB_FPU_STATE_CR | ZYDIS_ATTRIB_FPU_STATE_CW
};
ZYAN_ASSERT(definition->fpu_state < ZYAN_ARRAY_LENGTH(mapping));
instruction->attributes |= mapping[definition->fpu_state];
}
if (definition->xmm_state != ZYDIS_RW_ACTION_NONE)
{
static const ZydisInstructionAttributes mapping[ZYDIS_RW_ACTION_MAX_VALUE + 1] =
{
/* NONE */ 0,
/* READ */ ZYDIS_ATTRIB_XMM_STATE_CR,
/* WRITE */ ZYDIS_ATTRIB_XMM_STATE_CW,
/* READWRITE */ ZYDIS_ATTRIB_XMM_STATE_CR | ZYDIS_ATTRIB_XMM_STATE_CW
};
ZYAN_ASSERT(definition->xmm_state < ZYAN_ARRAY_LENGTH(mapping));
instruction->attributes |= mapping[definition->xmm_state];
}
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
{
const ZydisInstructionDefinitionLEGACY* def =
(const ZydisInstructionDefinitionLEGACY*)definition;
if (def->is_privileged)
{
instruction->attributes |= ZYDIS_ATTRIB_IS_PRIVILEGED;
}
if (def->accepts_LOCK)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_LOCK;
if (state->prefixes.has_lock)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_LOCK;
instruction->raw.prefixes[state->prefixes.offset_lock].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
}
if (def->accepts_REP)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_REP;
}
if (def->accepts_REPEREPZ)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_REPE;
}
if (def->accepts_REPNEREPNZ)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_REPNE;
}
if (def->accepts_BOUND)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_BND;
}
if (def->accepts_XACQUIRE)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_XACQUIRE;
}
if (def->accepts_XRELEASE)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_XRELEASE;
}
if (def->accepts_hle_without_lock)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_HLE_WITHOUT_LOCK;
}
switch (state->prefixes.group1)
{
case 0xF2:
if (instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_REPNE)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_REPNE;
break;
}
if (instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_XACQUIRE)
{
if ((instruction->attributes & ZYDIS_ATTRIB_HAS_LOCK) ||
(def->accepts_hle_without_lock))
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_XACQUIRE;
break;
}
}
if (state->decoder->decoder_mode[ZYDIS_DECODER_MODE_MPX] &&
instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_BND)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_BND;
break;
}
break;
case 0xF3:
if (instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_REP)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_REP;
break;
}
if (instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_REPE)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_REPE;
break;
}
if (instruction->attributes & ZYDIS_ATTRIB_ACCEPTS_XRELEASE)
{
if ((instruction->attributes & ZYDIS_ATTRIB_HAS_LOCK) ||
(def->accepts_hle_without_lock))
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_XRELEASE;
break;
}
}
break;
default:
break;
}
if ((instruction->raw.prefixes[state->prefixes.offset_group1].type ==
ZYDIS_PREFIX_TYPE_IGNORED) &&
(instruction->attributes & (
ZYDIS_ATTRIB_HAS_REP | ZYDIS_ATTRIB_HAS_REPE | ZYDIS_ATTRIB_HAS_REPNE |
ZYDIS_ATTRIB_HAS_BND | ZYDIS_ATTRIB_HAS_XACQUIRE | ZYDIS_ATTRIB_HAS_XRELEASE)))
{
instruction->raw.prefixes[state->prefixes.offset_group1].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
if (def->accepts_branch_hints)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_BRANCH_HINTS;
switch (state->prefixes.group2)
{
case 0x2E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_BRANCH_NOT_TAKEN;
instruction->raw.prefixes[state->prefixes.offset_group2].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
break;
case 0x3E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_BRANCH_TAKEN;
instruction->raw.prefixes[state->prefixes.offset_group2].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
break;
default:
break;
}
}
if (def->accepts_NOTRACK)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_NOTRACK;
if (state->decoder->decoder_mode[ZYDIS_DECODER_MODE_CET] &&
(state->prefixes.offset_notrack >= 0))
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_NOTRACK;
instruction->raw.prefixes[state->prefixes.offset_notrack].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
}
if (def->accepts_segment && !def->accepts_branch_hints)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_SEGMENT;
if (state->prefixes.effective_segment &&
!(instruction->attributes & ZYDIS_ATTRIB_HAS_NOTRACK))
{
switch (state->prefixes.effective_segment)
{
case 0x2E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_CS;
break;
case 0x36:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_SS;
break;
case 0x3E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_DS;
break;
case 0x26:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_ES;
break;
case 0x64:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_FS;
break;
case 0x65:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_GS;
break;
default:
ZYAN_UNREACHABLE;
}
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT)
{
instruction->raw.prefixes[state->prefixes.offset_segment].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
}
break;
}
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
if (definition->accepts_segment)
{
instruction->attributes |= ZYDIS_ATTRIB_ACCEPTS_SEGMENT;
if (state->prefixes.effective_segment)
{
switch (state->prefixes.effective_segment)
{
case 0x2E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_CS;
break;
case 0x36:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_SS;
break;
case 0x3E:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_DS;
break;
case 0x26:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_ES;
break;
case 0x64:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_FS;
break;
case 0x65:
instruction->attributes |= ZYDIS_ATTRIB_HAS_SEGMENT_GS;
break;
default:
ZYAN_UNREACHABLE;
}
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT)
{
instruction->raw.prefixes[state->prefixes.offset_segment].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
}
break;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Sets attributes for the given instruction.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param definition A pointer to the `ZydisInstructionDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L1992-L2261
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisSetAVXInformation
|
static void ZydisSetAVXInformation(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZydisInstructionDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(definition);
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
{
// Vector length
static const ZyanU16 lookup[2] =
{
128,
256
};
ZYAN_ASSERT(context->vector_unified.LL < ZYAN_ARRAY_LENGTH(lookup));
instruction->avx.vector_length = lookup[context->vector_unified.LL];
break;
}
case ZYDIS_INSTRUCTION_ENCODING_VEX:
{
// Vector length
static const ZyanU16 lookup[2] =
{
128,
256
};
ZYAN_ASSERT(context->vector_unified.LL < ZYAN_ARRAY_LENGTH(lookup));
instruction->avx.vector_length = lookup[context->vector_unified.LL];
// Static broadcast-factor
const ZydisInstructionDefinitionVEX* def =
(const ZydisInstructionDefinitionVEX*)definition;
if (def->broadcast)
{
instruction->avx.broadcast.is_static = ZYAN_TRUE;
static ZydisBroadcastMode broadcasts[ZYDIS_VEX_STATIC_BROADCAST_MAX_VALUE + 1] =
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_1_TO_2,
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8,
ZYDIS_BROADCAST_MODE_1_TO_16,
ZYDIS_BROADCAST_MODE_1_TO_32,
ZYDIS_BROADCAST_MODE_2_TO_4
};
instruction->avx.broadcast.mode = broadcasts[def->broadcast];
}
break;
}
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
{
#ifndef ZYDIS_DISABLE_AVX512
const ZydisInstructionDefinitionEVEX* def =
(const ZydisInstructionDefinitionEVEX*)definition;
// Vector length
ZyanU8 vector_length = context->vector_unified.LL;
if (def->vector_length)
{
vector_length = def->vector_length - 1;
}
static const ZyanU16 lookup[3] =
{
128,
256,
512
};
ZYAN_ASSERT(vector_length < ZYAN_ARRAY_LENGTH(lookup));
instruction->avx.vector_length = lookup[vector_length];
context->evex.tuple_type = def->tuple_type;
if (def->tuple_type)
{
ZYAN_ASSERT(instruction->raw.modrm.mod != 3);
ZYAN_ASSERT(def->element_size);
// Element size
static const ZyanU8 element_sizes[ZYDIS_IELEMENT_SIZE_MAX_VALUE + 1] =
{
0, 8, 16, 32, 64, 128
};
ZYAN_ASSERT(def->element_size < ZYAN_ARRAY_LENGTH(element_sizes));
context->evex.element_size = element_sizes[def->element_size];
// Compressed disp8 scale and broadcast-factor
switch (def->tuple_type)
{
case ZYDIS_TUPLETYPE_FV:
{
const ZyanU8 evex_b = instruction->raw.evex.b;
ZYAN_ASSERT(evex_b < 2);
ZYAN_ASSERT(!evex_b || ((!context->vector_unified.W && (context->evex.element_size == 16 ||
context->evex.element_size == 32)) ||
( context->vector_unified.W && context->evex.element_size == 64)));
ZYAN_ASSERT(!evex_b || def->functionality == ZYDIS_EVEX_FUNC_BC);
static const ZyanU8 scales[2][3][3] =
{
/*B0*/ { /*16*/ { 16, 32, 64 }, /*32*/ { 16, 32, 64 }, /*64*/ { 16, 32, 64 } },
/*B1*/ { /*16*/ { 2, 2, 2 }, /*32*/ { 4, 4, 4 }, /*64*/ { 8, 8, 8 } }
};
static const ZydisBroadcastMode broadcasts[2][3][3] =
{
/*B0*/
{
/*16*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
},
/*32*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
},
/*64*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
}
},
/*B1*/
{
/*16*/
{
ZYDIS_BROADCAST_MODE_1_TO_8,
ZYDIS_BROADCAST_MODE_1_TO_16,
ZYDIS_BROADCAST_MODE_1_TO_32
},
/*32*/
{
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8,
ZYDIS_BROADCAST_MODE_1_TO_16
},
/*64*/
{
ZYDIS_BROADCAST_MODE_1_TO_2,
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8
}
}
};
const ZyanU8 size_index = context->evex.element_size >> 5;
ZYAN_ASSERT(size_index < 3);
context->cd8_scale = scales[evex_b][size_index][vector_length];
instruction->avx.broadcast.mode = broadcasts[evex_b][size_index][vector_length];
break;
}
case ZYDIS_TUPLETYPE_HV:
{
const ZyanU8 evex_b = instruction->raw.evex.b;
ZYAN_ASSERT(evex_b < 2);
ZYAN_ASSERT(!context->vector_unified.W);
ZYAN_ASSERT((context->evex.element_size == 16) ||
(context->evex.element_size == 32));
ZYAN_ASSERT(!evex_b || def->functionality == ZYDIS_EVEX_FUNC_BC);
static const ZyanU8 scales[2][2][3] =
{
/*B0*/ { /*16*/ { 8, 16, 32 }, /*32*/ { 8, 16, 32 } },
/*B1*/ { /*16*/ { 2, 2, 2 }, /*32*/ { 4, 4, 4 } }
};
static const ZydisBroadcastMode broadcasts[2][2][3] =
{
/*B0*/
{
/*16*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
},
/*32*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
}
},
/*B1*/
{
/*16*/
{
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8,
ZYDIS_BROADCAST_MODE_1_TO_16
},
/*32*/
{
ZYDIS_BROADCAST_MODE_1_TO_2,
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8
}
}
};
const ZyanU8 size_index = context->evex.element_size >> 5;
ZYAN_ASSERT(size_index < 3);
context->cd8_scale = scales[evex_b][size_index][vector_length];
instruction->avx.broadcast.mode = broadcasts[evex_b][size_index][vector_length];
break;
}
case ZYDIS_TUPLETYPE_FVM:
{
static const ZyanU8 scales[3] =
{
16, 32, 64
};
context->cd8_scale = scales[vector_length];
break;
}
case ZYDIS_TUPLETYPE_GSCAT:
switch (context->vector_unified.W)
{
case 0:
ZYAN_ASSERT(context->evex.element_size == 32);
break;
case 1:
ZYAN_ASSERT(context->evex.element_size == 64);
break;
default:
ZYAN_UNREACHABLE;
}
ZYAN_FALLTHROUGH;
case ZYDIS_TUPLETYPE_T1S:
{
static const ZyanU8 scales[6] =
{
/* */ 0,
/* 8*/ 1,
/* 16*/ 2,
/* 32*/ 4,
/* 64*/ 8,
/*128*/ 16,
};
ZYAN_ASSERT(def->element_size < ZYAN_ARRAY_LENGTH(scales));
context->cd8_scale = scales[def->element_size];
break;
};
case ZYDIS_TUPLETYPE_T1F:
{
static const ZyanU8 scales[3] =
{
/* 16*/ 2,
/* 32*/ 4,
/* 64*/ 8
};
const ZyanU8 size_index = context->evex.element_size >> 5;
ZYAN_ASSERT(size_index < 3);
context->cd8_scale = scales[size_index];
break;
}
case ZYDIS_TUPLETYPE_T1_4X:
ZYAN_ASSERT(context->evex.element_size == 32);
ZYAN_ASSERT(context->vector_unified.W == 0);
context->cd8_scale = 16;
break;
case ZYDIS_TUPLETYPE_T2:
switch (context->vector_unified.W)
{
case 0:
ZYAN_ASSERT(context->evex.element_size == 32);
context->cd8_scale = 8;
break;
case 1:
ZYAN_ASSERT(context->evex.element_size == 64);
ZYAN_ASSERT((instruction->avx.vector_length == 256) ||
(instruction->avx.vector_length == 512));
context->cd8_scale = 16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_TUPLETYPE_T4:
switch (context->vector_unified.W)
{
case 0:
ZYAN_ASSERT(context->evex.element_size == 32);
ZYAN_ASSERT((instruction->avx.vector_length == 256) ||
(instruction->avx.vector_length == 512));
context->cd8_scale = 16;
break;
case 1:
ZYAN_ASSERT(context->evex.element_size == 64);
ZYAN_ASSERT(instruction->avx.vector_length == 512);
context->cd8_scale = 32;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_TUPLETYPE_T8:
ZYAN_ASSERT(!context->vector_unified.W);
ZYAN_ASSERT(instruction->avx.vector_length == 512);
ZYAN_ASSERT(context->evex.element_size == 32);
context->cd8_scale = 32;
break;
case ZYDIS_TUPLETYPE_HVM:
{
static const ZyanU8 scales[3] =
{
8, 16, 32
};
context->cd8_scale = scales[vector_length];
break;
}
case ZYDIS_TUPLETYPE_QVM:
{
static const ZyanU8 scales[3] =
{
4, 8, 16
};
context->cd8_scale = scales[vector_length];
break;
}
case ZYDIS_TUPLETYPE_OVM:
{
static const ZyanU8 scales[3] =
{
2, 4, 8
};
context->cd8_scale = scales[vector_length];
break;
}
case ZYDIS_TUPLETYPE_M128:
context->cd8_scale = 16;
break;
case ZYDIS_TUPLETYPE_DUP:
{
static const ZyanU8 scales[3] =
{
8, 32, 64
};
context->cd8_scale = scales[vector_length];
break;
}
case ZYDIS_TUPLETYPE_QUARTER:
{
const ZyanU8 evex_b = instruction->raw.evex.b;
ZYAN_ASSERT(evex_b < 2);
ZYAN_ASSERT(!context->vector_unified.W);
ZYAN_ASSERT(context->evex.element_size == 16);
ZYAN_ASSERT(!evex_b || def->functionality == ZYDIS_EVEX_FUNC_BC);
static const ZyanU8 scales[2][3] =
{
/*B0*/ { 4, 8, 16 },
/*B1*/ { 2, 2, 2 }
};
static const ZydisBroadcastMode broadcasts[2][3] =
{
/*B0*/
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_INVALID
},
/*B1*/
{
ZYDIS_BROADCAST_MODE_1_TO_2,
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8
}
};
context->cd8_scale = scales[evex_b][vector_length];
instruction->avx.broadcast.mode = broadcasts[evex_b][vector_length];
break;
}
default:
ZYAN_UNREACHABLE;
}
} else
{
ZYAN_ASSERT(instruction->raw.modrm.mod == 3);
}
// Static broadcast-factor
if (def->broadcast)
{
ZYAN_ASSERT(!instruction->avx.broadcast.mode);
instruction->avx.broadcast.is_static = ZYAN_TRUE;
static const ZydisBroadcastMode broadcasts[ZYDIS_EVEX_STATIC_BROADCAST_MAX_VALUE + 1] =
{
ZYDIS_BROADCAST_MODE_INVALID,
ZYDIS_BROADCAST_MODE_1_TO_2,
ZYDIS_BROADCAST_MODE_1_TO_4,
ZYDIS_BROADCAST_MODE_1_TO_8,
ZYDIS_BROADCAST_MODE_1_TO_16,
ZYDIS_BROADCAST_MODE_1_TO_32,
ZYDIS_BROADCAST_MODE_1_TO_64,
ZYDIS_BROADCAST_MODE_2_TO_4,
ZYDIS_BROADCAST_MODE_2_TO_8,
ZYDIS_BROADCAST_MODE_2_TO_16,
ZYDIS_BROADCAST_MODE_4_TO_8,
ZYDIS_BROADCAST_MODE_4_TO_16,
ZYDIS_BROADCAST_MODE_8_TO_16
};
ZYAN_ASSERT(def->broadcast < ZYAN_ARRAY_LENGTH(broadcasts));
instruction->avx.broadcast.mode = broadcasts[def->broadcast];
}
// Rounding mode and SAE
if (instruction->raw.evex.b)
{
switch (def->functionality)
{
case ZYDIS_EVEX_FUNC_INVALID:
case ZYDIS_EVEX_FUNC_BC:
// Noting to do here
break;
case ZYDIS_EVEX_FUNC_RC:
instruction->avx.rounding.mode = ZYDIS_ROUNDING_MODE_RN + context->vector_unified.LL;
ZYAN_FALLTHROUGH;
case ZYDIS_EVEX_FUNC_SAE:
instruction->avx.has_sae = ZYAN_TRUE;
break;
default:
ZYAN_UNREACHABLE;
}
}
// Mask
instruction->avx.mask.reg = ZYDIS_REGISTER_K0 + instruction->raw.evex.aaa;
switch (def->mask_override)
{
case ZYDIS_MASK_OVERRIDE_DEFAULT:
instruction->avx.mask.mode = ZYDIS_MASK_MODE_MERGING + instruction->raw.evex.z;
break;
case ZYDIS_MASK_OVERRIDE_ZEROING:
instruction->avx.mask.mode = ZYDIS_MASK_MODE_ZEROING;
break;
case ZYDIS_MASK_OVERRIDE_CONTROL:
instruction->avx.mask.mode = ZYDIS_MASK_MODE_CONTROL + instruction->raw.evex.z;
break;
default:
ZYAN_UNREACHABLE;
}
if (!instruction->raw.evex.aaa)
{
instruction->avx.mask.mode = ZYDIS_MASK_MODE_DISABLED;
}
#else
ZYAN_UNREACHABLE;
#endif
break;
}
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
{
#ifndef ZYDIS_DISABLE_KNC
// Vector length
instruction->avx.vector_length = 512;
const ZydisInstructionDefinitionMVEX* def =
(const ZydisInstructionDefinitionMVEX*)definition;
// Static broadcast-factor
ZyanU8 index = def->has_element_granularity;
ZYAN_ASSERT(!index || !def->broadcast);
if (!index && def->broadcast)
{
instruction->avx.broadcast.is_static = ZYAN_TRUE;
switch (def->broadcast)
{
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_8:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_1_TO_8;
index = 1;
break;
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_16:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_1_TO_16;
index = 1;
break;
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_8:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_4_TO_8;
index = 2;
break;
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_16:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_4_TO_16;
index = 2;
break;
default:
ZYAN_UNREACHABLE;
}
}
// Compressed disp8 scale and broadcast-factor
switch (def->functionality)
{
case ZYDIS_MVEX_FUNC_IGNORED:
case ZYDIS_MVEX_FUNC_INVALID:
case ZYDIS_MVEX_FUNC_RC:
case ZYDIS_MVEX_FUNC_SAE:
case ZYDIS_MVEX_FUNC_SWIZZLE_32:
case ZYDIS_MVEX_FUNC_SWIZZLE_64:
// Nothing to do here
break;
case ZYDIS_MVEX_FUNC_F_32:
case ZYDIS_MVEX_FUNC_I_32:
case ZYDIS_MVEX_FUNC_F_64:
case ZYDIS_MVEX_FUNC_I_64:
context->cd8_scale = 64;
break;
case ZYDIS_MVEX_FUNC_SF_32:
case ZYDIS_MVEX_FUNC_SF_32_BCST:
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UF_32:
{
static const ZyanU8 lookup[3][8] =
{
{ 64, 4, 16, 32, 16, 16, 32, 32 },
{ 4, 0, 0, 2, 1, 1, 2, 2 },
{ 16, 0, 0, 8, 4, 4, 8, 8 }
};
ZYAN_ASSERT(instruction->raw.mvex.SSS < ZYAN_ARRAY_LENGTH(lookup[index]));
context->cd8_scale = lookup[index][instruction->raw.mvex.SSS];
break;
}
case ZYDIS_MVEX_FUNC_SI_32:
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_SI_32_BCST:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
{
static const ZyanU8 lookup[3][8] =
{
{ 64, 4, 16, 0, 16, 16, 32, 32 },
{ 4, 0, 0, 0, 1, 1, 2, 2 },
{ 16, 0, 0, 0, 4, 4, 8, 8 }
};
ZYAN_ASSERT(instruction->raw.mvex.SSS < ZYAN_ARRAY_LENGTH(lookup[index]));
context->cd8_scale = lookup[index][instruction->raw.mvex.SSS];
break;
}
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_SI_64:
case ZYDIS_MVEX_FUNC_UI_64:
{
static const ZyanU8 lookup[3][3] =
{
{ 64, 8, 32 },
{ 8, 0, 0 },
{ 32, 0, 0 }
};
ZYAN_ASSERT(instruction->raw.mvex.SSS < ZYAN_ARRAY_LENGTH(lookup[index]));
context->cd8_scale = lookup[index][instruction->raw.mvex.SSS];
break;
}
case ZYDIS_MVEX_FUNC_DF_32:
case ZYDIS_MVEX_FUNC_DI_32:
{
static const ZyanU8 lookup[2][8] =
{
{ 64, 0, 0, 32, 16, 16, 32, 32 },
{ 4, 0, 0, 2, 1, 1, 2, 2 }
};
ZYAN_ASSERT(index < 2);
ZYAN_ASSERT(instruction->raw.mvex.SSS < ZYAN_ARRAY_LENGTH(lookup[index]));
context->cd8_scale = lookup[index][instruction->raw.mvex.SSS];
break;
}
case ZYDIS_MVEX_FUNC_DF_64:
case ZYDIS_MVEX_FUNC_DI_64:
{
static const ZyanU8 lookup[2][1] =
{
{ 64 },
{ 8 }
};
ZYAN_ASSERT(index < 2);
ZYAN_ASSERT(instruction->raw.mvex.SSS < ZYAN_ARRAY_LENGTH(lookup[index]));
context->cd8_scale = lookup[index][instruction->raw.mvex.SSS];
break;
}
default:
ZYAN_UNREACHABLE;
}
// Rounding mode, sae, swizzle, convert
context->mvex.functionality = def->functionality;
switch (def->functionality)
{
case ZYDIS_MVEX_FUNC_IGNORED:
case ZYDIS_MVEX_FUNC_INVALID:
case ZYDIS_MVEX_FUNC_F_32:
case ZYDIS_MVEX_FUNC_I_32:
case ZYDIS_MVEX_FUNC_F_64:
case ZYDIS_MVEX_FUNC_I_64:
// Nothing to do here
break;
case ZYDIS_MVEX_FUNC_RC:
instruction->avx.rounding.mode = ZYDIS_ROUNDING_MODE_RN + (instruction->raw.mvex.SSS & 3);
ZYAN_FALLTHROUGH;
case ZYDIS_MVEX_FUNC_SAE:
if (instruction->raw.mvex.SSS >= 4)
{
instruction->avx.has_sae = ZYAN_TRUE;
}
break;
case ZYDIS_MVEX_FUNC_SWIZZLE_32:
case ZYDIS_MVEX_FUNC_SWIZZLE_64:
instruction->avx.swizzle.mode = ZYDIS_SWIZZLE_MODE_DCBA + instruction->raw.mvex.SSS;
break;
case ZYDIS_MVEX_FUNC_SF_32:
case ZYDIS_MVEX_FUNC_SF_32_BCST:
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
switch (instruction->raw.mvex.SSS)
{
case 0:
break;
case 1:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_1_TO_16;
break;
case 2:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_4_TO_16;
break;
case 3:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_FLOAT16;
break;
case 4:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT8;
break;
case 5:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT8;
break;
case 6:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT16;
break;
case 7:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_MVEX_FUNC_SI_32:
case ZYDIS_MVEX_FUNC_SI_32_BCST:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
switch (instruction->raw.mvex.SSS)
{
case 0:
break;
case 1:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_1_TO_16;
break;
case 2:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_4_TO_16;
break;
case 4:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT8;
break;
case 5:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT8;
break;
case 6:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT16;
break;
case 7:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_SI_64:
switch (instruction->raw.mvex.SSS)
{
case 0:
break;
case 1:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_1_TO_8;
break;
case 2:
instruction->avx.broadcast.mode = ZYDIS_BROADCAST_MODE_4_TO_8;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_MVEX_FUNC_UF_32:
case ZYDIS_MVEX_FUNC_DF_32:
switch (instruction->raw.mvex.SSS)
{
case 0:
break;
case 3:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_FLOAT16;
break;
case 4:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT8;
break;
case 5:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT8;
break;
case 6:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT16;
break;
case 7:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_DF_64:
break;
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_DI_32:
switch (instruction->raw.mvex.SSS)
{
case 0:
break;
case 4:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT8;
break;
case 5:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT8;
break;
case 6:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_UINT16;
break;
case 7:
instruction->avx.conversion.mode = ZYDIS_CONVERSION_MODE_SINT16;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_MVEX_FUNC_UI_64:
case ZYDIS_MVEX_FUNC_DI_64:
break;
default:
ZYAN_UNREACHABLE;
}
// Eviction hint
if ((instruction->raw.modrm.mod != 3) && instruction->raw.mvex.E)
{
instruction->avx.has_eviction_hint = ZYAN_TRUE;
}
// Mask
instruction->avx.mask.mode = ZYDIS_MASK_MODE_MERGING;
instruction->avx.mask.reg = ZYDIS_REGISTER_K0 + instruction->raw.mvex.kkk;
#else
ZYAN_UNREACHABLE;
#endif
break;
}
default:
// Nothing to do here
break;
}
}
|
/**
* Sets AVX-specific information for the given instruction.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param definition A pointer to the `ZydisInstructionDefinition` struct.
*
* Information set for `XOP`:
* - Vector Length
*
* Information set for `VEX`:
* - Vector length
* - Static broadcast-factor
*
* Information set for `EVEX`:
* - Vector length
* - Broadcast-factor (static and dynamic)
* - Rounding-mode and SAE
* - Mask mode
* - Compressed 8-bit displacement scale-factor
*
* Information set for `MVEX`:
* - Vector length
* - Broadcast-factor (static and dynamic)
* - Rounding-mode and SAE
* - Swizzle- and conversion-mode
* - Mask mode
* - Eviction hint
* - Compressed 8-bit displacement scale-factor
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L2295-L3061
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCollectOptionalPrefixes
|
static ZyanStatus ZydisCollectOptionalPrefixes(ZydisDecoderState* state,
ZydisDecodedInstruction* instruction)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(instruction->raw.prefix_count == 0);
ZyanU8 rex = 0x00;
ZyanU8 offset = 0;
ZyanBool done = ZYAN_FALSE;
do
{
ZyanU8 prefix_byte;
ZYAN_CHECK(ZydisInputPeek(state, instruction, &prefix_byte));
switch (prefix_byte)
{
case 0xF0:
state->prefixes.has_lock = ZYAN_TRUE;
state->prefixes.offset_lock = offset;
break;
case 0xF2:
ZYAN_FALLTHROUGH;
case 0xF3:
state->prefixes.group1 = prefix_byte;
state->prefixes.mandatory_candidate = prefix_byte;
state->prefixes.offset_group1 = offset;
state->prefixes.offset_mandatory = offset;
break;
case 0x2E:
ZYAN_FALLTHROUGH;
case 0x36:
ZYAN_FALLTHROUGH;
case 0x3E:
ZYAN_FALLTHROUGH;
case 0x26:
if (state->decoder->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
if ((prefix_byte == 0x3E) &&
(state->prefixes.effective_segment != 0x64) &&
(state->prefixes.effective_segment != 0x65))
{
state->prefixes.offset_notrack = offset;
}
state->prefixes.group2 = prefix_byte;
state->prefixes.offset_group2 = offset;
break;
}
ZYAN_FALLTHROUGH;
case 0x64:
ZYAN_FALLTHROUGH;
case 0x65:
state->prefixes.group2 = prefix_byte;
state->prefixes.offset_group2 = offset;
state->prefixes.effective_segment = prefix_byte;
state->prefixes.offset_segment = offset;
state->prefixes.offset_notrack = -1;
break;
case 0x66:
// context->prefixes.has_osz_override = ZYAN_TRUE;
state->prefixes.offset_osz_override = offset;
if (!state->prefixes.mandatory_candidate)
{
state->prefixes.mandatory_candidate = 0x66;
state->prefixes.offset_mandatory = offset;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_OPERANDSIZE;
break;
case 0x67:
// context->prefixes.has_asz_override = ZYAN_TRUE;
state->prefixes.offset_asz_override = offset;
instruction->attributes |= ZYDIS_ATTRIB_HAS_ADDRESSSIZE;
break;
default:
if ((state->decoder->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(prefix_byte & 0xF0) == 0x40)
{
rex = prefix_byte;
instruction->raw.rex.offset = offset;
} else
{
done = ZYAN_TRUE;
}
break;
}
if (!done)
{
// Invalidate `REX`, if it's not the last legacy prefix
if (rex && (rex != prefix_byte))
{
rex = 0x00;
instruction->raw.rex.offset = 0;
}
instruction->raw.prefixes[instruction->raw.prefix_count++].value = prefix_byte;
ZydisInputSkip(state, instruction);
++offset;
}
} while (!done);
if (instruction->attributes & ZYDIS_ATTRIB_HAS_OPERANDSIZE)
{
instruction->raw.prefixes[state->prefixes.offset_osz_override].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_ADDRESSSIZE)
{
instruction->raw.prefixes[state->prefixes.offset_asz_override].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
if (rex)
{
instruction->raw.prefixes[instruction->raw.rex.offset].type = ZYDIS_PREFIX_TYPE_EFFECTIVE;
ZydisDecodeREX(state->context, instruction, rex);
}
if ((state->decoder->machine_mode != ZYDIS_MACHINE_MODE_LONG_64) &&
(state->prefixes.group2 == 0x3E))
{
state->prefixes.offset_notrack = state->prefixes.offset_group2;
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Physical instruction decoding */
/* ---------------------------------------------------------------------------------------------- */
/**
* Collects optional instruction prefixes.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
*
* @return A zyan status code.
*
* This function sets the corresponding flag for each prefix and automatically decodes the last
* `REX`-prefix (if exists).
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L3079-L3199
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeOptionalInstructionParts
|
static ZyanStatus ZydisDecodeOptionalInstructionParts(ZydisDecoderState* state,
ZydisDecodedInstruction* instruction, const ZydisInstructionEncodingInfo* info)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(info);
ZydisDecoderContext* context = state->context;
if (info->flags & ZYDIS_INSTR_ENC_FLAG_HAS_MODRM)
{
if (!instruction->raw.modrm.offset)
{
instruction->raw.modrm.offset = instruction->length;
ZyanU8 modrm_byte;
ZYAN_CHECK(ZydisInputNext(state, instruction, &modrm_byte));
ZydisDecodeModRM(instruction, modrm_byte);
}
if (!(info->flags & ZYDIS_INSTR_ENC_FLAG_FORCE_REG_FORM))
{
ZyanU8 has_sib = 0;
ZyanU8 displacement_size = 0;
switch (instruction->address_width)
{
case 16:
switch (instruction->raw.modrm.mod)
{
case 0:
if (instruction->raw.modrm.rm == 6)
{
displacement_size = 16;
}
break;
case 1:
displacement_size = 8;
break;
case 2:
displacement_size = 16;
break;
case 3:
break;
default:
ZYAN_UNREACHABLE;
}
break;
case 32:
case 64:
has_sib =
(instruction->raw.modrm.mod != 3) && (instruction->raw.modrm.rm == 4);
switch (instruction->raw.modrm.mod)
{
case 0:
if (instruction->raw.modrm.rm == 5)
{
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
instruction->attributes |= ZYDIS_ATTRIB_IS_RELATIVE;
}
displacement_size = 32;
}
break;
case 1:
displacement_size = 8;
break;
case 2:
displacement_size = 32;
break;
case 3:
break;
default:
ZYAN_UNREACHABLE;
}
break;
default:
ZYAN_UNREACHABLE;
}
if (has_sib)
{
instruction->raw.sib.offset = instruction->length;
ZyanU8 sib_byte;
ZYAN_CHECK(ZydisInputNext(state, instruction, &sib_byte));
ZydisDecodeSIB(instruction, sib_byte);
if (instruction->raw.sib.base == 5)
{
displacement_size = (instruction->raw.modrm.mod == 1) ? 8 : 32;
}
}
if (displacement_size)
{
ZYAN_CHECK(ZydisReadDisplacement(state, instruction, displacement_size));
}
}
context->reg_info.is_mod_reg = (instruction->raw.modrm.mod == 3) ||
(info->flags & ZYDIS_INSTR_ENC_FLAG_FORCE_REG_FORM);
}
if (info->flags & ZYDIS_INSTR_ENC_FLAG_HAS_DISP)
{
ZYAN_CHECK(ZydisReadDisplacement(
state, instruction, info->disp.size[context->easz_index]));
}
if (info->flags & ZYDIS_INSTR_ENC_FLAG_HAS_IMM0)
{
if (info->imm[0].is_relative)
{
instruction->attributes |= ZYDIS_ATTRIB_IS_RELATIVE;
}
ZYAN_CHECK(ZydisReadImmediate(state, instruction, 0,
info->imm[0].size[context->eosz_index], info->imm[0].is_signed,
info->imm[0].is_relative));
}
if (info->flags & ZYDIS_INSTR_ENC_FLAG_HAS_IMM1)
{
ZYAN_ASSERT(!(info->flags & ZYDIS_INSTR_ENC_FLAG_HAS_DISP));
ZYAN_CHECK(ZydisReadImmediate(state, instruction, 1,
info->imm[1].size[context->eosz_index], info->imm[1].is_signed,
info->imm[1].is_relative));
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Decodes optional instruction parts like the ModRM byte, the SIB byte and
* additional displacements and/or immediate values.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param info A pointer to the `ZydisInstructionEncodingInfo` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L3211-L3335
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisSetEffectiveOperandWidth
|
static void ZydisSetEffectiveOperandWidth(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZydisInstructionDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(definition);
static const ZyanU8 operand_size_map[8][8] =
{
// Default for most instructions
{
16, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
16, // 32 66 W0
32, // 64 __ W0
16, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// Operand size is forced to 8-bit (this is done later to preserve the `eosz_index`)
{
16, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
16, // 32 66 W0
32, // 64 __ W0
16, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// Operand size override 0x66 is ignored
{
16, // 16 __ W0
16, // 16 66 W0
32, // 32 __ W0
32, // 32 66 W0
32, // 64 __ W0
32, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// REX.W promotes to 32-bit instead of 64-bit
{
16, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
16, // 32 66 W0
32, // 64 __ W0
16, // 64 66 W0
32, // 64 __ W1
32 // 64 66 W1
},
// Operand size defaults to 64-bit in 64-bit mode
{
16, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
16, // 32 66 W0
64, // 64 __ W0
16, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// Operand size is forced to 64-bit in 64-bit mode
{
16, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
16, // 32 66 W0
64, // 64 __ W0
64, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// Operand size is forced to 32-bit, if no REX.W is present.
{
32, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
32, // 32 66 W0
32, // 64 __ W0
32, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
},
// Operand size is forced to 64-bit in 64-bit mode and forced to 32-bit in all other modes.
// This is used for e.g. `mov CR, GPR` and `mov GPR, CR`.
{
32, // 16 __ W0
32, // 16 66 W0
32, // 32 __ W0
32, // 32 66 W0
64, // 64 __ W0
64, // 64 66 W0
64, // 64 __ W1
64 // 64 66 W1
}
};
ZyanU8 index = (instruction->attributes & ZYDIS_ATTRIB_HAS_OPERANDSIZE) ? 1 : 0;
if ((instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_32) ||
(instruction->machine_mode == ZYDIS_MACHINE_MODE_LEGACY_32))
{
index += 2;
}
else if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
index += 4;
index += (context->vector_unified.W & 0x01) << 1;
}
ZYAN_ASSERT(definition->operand_size_map < ZYAN_ARRAY_LENGTH(operand_size_map));
ZYAN_ASSERT(index < ZYAN_ARRAY_LENGTH(operand_size_map[definition->operand_size_map]));
instruction->operand_width = operand_size_map[definition->operand_size_map][index];
context->eosz_index = instruction->operand_width >> 5;
// TODO: Cleanup code and remove hardcoded condition
if (definition->operand_size_map == 1)
{
instruction->operand_width = 8;
}
}
|
/* ---------------------------------------------------------------------------------------------- */
/**
* Sets the effective operand size for the given instruction.
*
* @param context A pointer to the `ZydisDecoderContext` struct
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param definition A pointer to the `ZydisInstructionDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L3346-L3469
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisSetEffectiveAddressWidth
|
static void ZydisSetEffectiveAddressWidth(ZydisDecoderContext* context,
ZydisDecodedInstruction* instruction, const ZydisInstructionDefinition* definition)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
static const ZyanU8 address_size_map[3][8] =
{
// Default for most instructions
{
16, // 16 __
32, // 16 67
32, // 32 __
16, // 32 67
64, // 64 __
32 // 64 67
},
// The address-size override is ignored
{
16, // 16 __
16, // 16 67
32, // 32 __
32, // 32 67
64, // 64 __
64 // 64 67
},
// The address-size is forced to 64-bit in 64-bit mode and 32-bit in non 64-bit mode. This
// is used by e.g. `ENCLS`, `ENCLV`, `ENCLU`.
{
32, // 16 __
32, // 16 67
32, // 32 __
32, // 32 67
64, // 64 __
64 // 64 67
}
};
ZyanU8 index = (instruction->attributes & ZYDIS_ATTRIB_HAS_ADDRESSSIZE) ? 1 : 0;
if ((instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_32) ||
(instruction->machine_mode == ZYDIS_MACHINE_MODE_LEGACY_32))
{
index += 2;
}
else if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
index += 4;
}
ZYAN_ASSERT(definition->address_size_map < ZYAN_ARRAY_LENGTH(address_size_map));
ZYAN_ASSERT(index < ZYAN_ARRAY_LENGTH(address_size_map[definition->address_size_map]));
instruction->address_width = address_size_map[definition->address_size_map][index];
context->easz_index = instruction->address_width >> 5;
}
|
/**
* Sets the effective address width for the given instruction.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param definition A pointer to the `ZydisInstructionDefinition` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L3478-L3532
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisNodeHandlerXOP
|
static ZyanStatus ZydisNodeHandlerXOP(const ZydisDecodedInstruction* instruction, ZyanU16* index)
{
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(index);
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
*index = 0;
break;
case ZYDIS_INSTRUCTION_ENCODING_XOP:
ZYAN_ASSERT(instruction->attributes & ZYDIS_ATTRIB_HAS_XOP);
*index = (instruction->raw.xop.m_mmmm - 0x08) + (instruction->raw.xop.pp * 3) + 1;
break;
default:
ZYAN_UNREACHABLE;
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L3536-L3554
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisPopulateRegisterIds
|
static ZyanStatus ZydisPopulateRegisterIds(ZydisDecoderContext* context,
const ZydisDecodedInstruction* instruction, ZyanU8 def_reg, ZyanU8 def_rm, ZyanU8 def_ndsndd)
{
ZYAN_ASSERT(context);
ZYAN_ASSERT(instruction);
const ZyanBool is_64_bit = (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64);
const ZyanBool is_reg = context->reg_info.is_mod_reg;
const ZyanBool has_sib = !is_reg && (instruction->raw.modrm.rm == 4);
const ZyanBool has_vsib = has_sib && (def_rm == ZYDIS_MEMOP_TYPE_VSIB);
ZyanU8 id_reg = instruction->raw.modrm.reg;
ZyanU8 id_rm = instruction->raw.modrm.rm;
ZyanU8 id_ndsndd = is_64_bit ? context->vector_unified.vvvv : context->vector_unified.vvvv & 0x07;
ZyanU8 id_base = has_sib ? instruction->raw.sib.base : instruction->raw.modrm.rm;
ZyanU8 id_index = instruction->raw.sib.index;
if (instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
const ZyanBool is_emvex = (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX);
// The `index` extension by `.v'` is only valid for VSIB operands
const ZyanU8 vsib_v2 = has_vsib ? context->vector_unified.V2 : 0;
// The `rm` extension by `.X` is only valid for EVEX/MVEX instructions
const ZyanU8 evex_x = is_emvex ? context->vector_unified.X : 0;
id_reg |= (context->vector_unified.R2 << 4) | (context->vector_unified.R << 3);
id_rm |= (evex_x << 4) | (context->vector_unified.B << 3);
id_ndsndd |= (context->vector_unified.V2 << 4) ;
id_base |= (context->vector_unified.B << 3);
id_index |= (vsib_v2 << 4) | (context->vector_unified.X << 3);
// The masking emulates the actual CPU behavior and does not verify if the resulting ids
// are actually valid for the given register kind.
static const ZyanU8 mask_reg[ZYDIS_REGKIND_MAX_VALUE + 1] =
{
/* INVALID */ 0,
/* GPR */ (1 << 5) - 1,
/* X87 */ (1 << 3) - 1, // ignore `.R`, ignore `.R'`
/* MMX */ (1 << 3) - 1, // ignore `.R`, ignore `.R'`
/* VR */ (1 << 5) - 1,
/* TMM */ (1 << 5) - 1,
/* SEGMENT */ (1 << 3) - 1, // ignore `.R`, ignore `.R'`
/* TEST */ (1 << 3) - 1, // ignore `.R`, ignore `.R'`
/* CONTROL */ (1 << 4) - 1, // ignore `.R'`
/* DEBUG */ (1 << 4) - 1, // ignore `.R'`
/* MASK */ (1 << 5) - 1,
/* BOUND */ (1 << 4) - 1 // ignore `.R'`
};
id_reg &= mask_reg[def_reg];
static const ZyanU8 mask_rm[ZYDIS_REGKIND_MAX_VALUE + 1] =
{
/* INVALID */ 0,
/* GPR */ (1 << 4) - 1, // ignore `.X`
/* X87 */ (1 << 3) - 1, // ignore `.B`, ignore `.X`
/* MMX */ (1 << 3) - 1, // ignore `.B`, ignore `.X`
/* VR */ (1 << 5) - 1,
/* TMM */ (1 << 4) - 1, // ignore `.X`
/* SEGMENT */ (1 << 3) - 1, // ignore `.B`, ignore `.X`
/* TEST */ (1 << 3) - 1, // ignore `.B`, ignore `.X`
/* CONTROL */ (1 << 4) - 1, // ignore `.X`
/* DEBUG */ (1 << 4) - 1, // ignore `.X`
/* MASK */ (1 << 3) - 1, // ignore `.B`, ignore `.X`
/* BOUND */ (1 << 4) - 1 // ignore `.X`
};
id_rm &= (is_reg ? mask_rm[def_rm] : 0xFF);
// Commented out for future reference. Not required at the moment as it's always either
// a "take all" or "take nothing" situation.
//static const ZyanU8 mask_ndsndd[ZYDIS_REGKIND_MAX_VALUE + 1] =
//{
// /* INVALID */ 0,
// /* GPR */ (1 << 5) - 1,
// /* X87 */ 0, // never encoded in `.vvvv`
// /* MMX */ 0, // never encoded in `.vvvv`
// /* VR */ (1 << 5) - 1,
// /* TMM */ (1 << 5) - 1,
// /* SEGMENT */ 0, // never encoded in `.vvvv`
// /* TEST */ 0, // never encoded in `.vvvv`
// /* CONTROL */ 0, // never encoded in `.vvvv`
// /* DEBUG */ 0, // never encoded in `.vvvv`
// /* MASK */ (1 << 5) - 1,
// /* BOUND */ 0 // never encoded in `.vvvv`
//};
}
// Validate
// `.vvvv` is not allowed, if the instruction does not encode a NDS/NDD operand
if (!def_ndsndd && context->vector_unified.vvvv)
{
return ZYDIS_STATUS_BAD_REGISTER;
}
// `.v'` is not allowed, if the instruction does not encode a NDS/NDD or VSIB operand
if (!def_ndsndd && !has_vsib && context->vector_unified.V2)
{
return ZYDIS_STATUS_BAD_REGISTER;
}
static const ZyanU8 available_regs[2][ZYDIS_REGKIND_MAX_VALUE + 1] =
{
// 16/32 bit mode
{
/* INVALID */ 255,
/* GPR */ 8,
/* X87 */ 8,
/* MMX */ 8,
/* VR */ 8,
/* TMM */ 8,
/* SEGMENT */ 6,
/* TEST */ 8,
/* CONTROL */ 8,
/* DEBUG */ 8,
/* MASK */ 8,
/* BOUND */ 4
},
// 64 bit mode
{
/* INVALID */ 255,
/* GPR */ 16,
/* X87 */ 8,
/* MMX */ 8,
/* VR */ 32,
/* TMM */ 8,
/* SEGMENT */ 6,
/* TEST */ 8,
/* CONTROL */ 16,
// Attempts to reference DR8..DR15 result in undefined opcode (#UD) exceptions. DR4 and
// DR5 are only valid, if the debug extension (DE) flag in CR4 is set. As we can't
// check this at runtime we just allow them.
/* DEBUG */ 8,
/* MASK */ 8,
/* BOUND */ 4
}
};
if ((id_reg >= available_regs[is_64_bit][def_reg]) ||
(id_ndsndd >= available_regs[is_64_bit][def_ndsndd]) ||
(is_reg && (id_rm >= available_regs[is_64_bit][def_rm])))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
ZyanI8 id_cr = -1;
if (def_reg == ZYDIS_REGKIND_CONTROL)
{
id_cr = id_reg;
}
if (is_reg && (def_rm == ZYDIS_REGKIND_CONTROL))
{
id_cr = id_rm;
}
if (id_cr >= 0)
{
// Attempts to reference CR1, CR5, CR6, CR7, and CR9..CR15 result in undefined opcode (#UD)
// exceptions
static const ZyanU8 lookup[16] =
{
1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
};
ZYAN_ASSERT(id_cr < ZYAN_ARRAY_LENGTH(lookup));
if (!lookup[id_cr])
{
return ZYDIS_STATUS_BAD_REGISTER;
}
}
// Assign to context
context->reg_info.id_reg = def_reg ? id_reg : -1;
context->reg_info.id_rm = def_rm && is_reg ? id_rm : -1;
context->reg_info.id_ndsndd = def_ndsndd ? id_ndsndd : -1;
context->reg_info.id_base = id_base; // TODO: Set unused register to -1 as well
context->reg_info.id_index = id_index; // TODO: Set unused register to -1 as well
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/**
* Populates the internal register id fields for `REG`, `RM`, `NDSNDD`, `BASE` and `INDEX`/`VIDX`
* encoded operands and performs sanity checks.
*
* @param context A pointer to the `ZydisDecoderContext` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param def_reg The type definition for the `.reg` encoded operand.
* @param def_rm The type definition for the `.rm` encoded operand.
* @param def_ndsndd The type definition for the `.vvvv` encoded operand.
*
* @return A zyan status code.
*
* This function sets all unused register ids to `-1`. This rule does currently not apply to
* `base` and `index`.
*
* Definition encoding:
* - `def_reg` -> `ZydisRegisterKind`
* - `def_ndsndd` -> `ZydisRegisterKind`
* - `def_rm` -> `ZydisRegisterKind` (`.mod == 3`) or ZydisMemoryOperandType (`.mod != 3`)
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L4152-L4332
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCheckErrorConditions
|
static ZyanStatus ZydisCheckErrorConditions(ZydisDecoderState* state,
const ZydisDecodedInstruction* instruction, const ZydisInstructionDefinition* definition)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
ZYAN_ASSERT(definition);
ZyanU8 def_reg = definition->op_reg;
ZyanU8 def_rm = definition->op_rm;
ZyanU8 def_ndsndd = ZYDIS_REGKIND_INVALID;
ZyanBool is_gather = ZYAN_FALSE;
ZyanBool no_source_dest_match = ZYAN_FALSE;
ZyanBool no_source_source_match = ZYAN_FALSE;
#if !defined(ZYDIS_DISABLE_AVX512) || !defined(ZYDIS_DISABLE_KNC)
ZydisMaskPolicy mask_policy = ZYDIS_MASK_POLICY_INVALID;
#endif
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
{
const ZydisInstructionDefinitionLEGACY* def =
(const ZydisInstructionDefinitionLEGACY*)definition;
if (def->requires_protected_mode &&
(instruction->machine_mode == ZYDIS_MACHINE_MODE_REAL_16))
{
return ZYDIS_STATUS_DECODING_ERROR;
}
if (def->no_compat_mode &&
((instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_16) ||
(instruction->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_32)))
{
return ZYDIS_STATUS_DECODING_ERROR;
}
if (state->prefixes.has_lock && !def->accepts_LOCK)
{
return ZYDIS_STATUS_ILLEGAL_LOCK;
}
break;
}
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
{
break;
}
case ZYDIS_INSTRUCTION_ENCODING_XOP:
{
const ZydisInstructionDefinitionXOP* def =
(const ZydisInstructionDefinitionXOP*)definition;
def_ndsndd = def->op_ndsndd;
break;
}
case ZYDIS_INSTRUCTION_ENCODING_VEX:
{
const ZydisInstructionDefinitionVEX* def =
(const ZydisInstructionDefinitionVEX*)definition;
def_ndsndd = def->op_ndsndd;
is_gather = def->is_gather;
no_source_source_match = def->no_source_source_match;
break;
}
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
{
#ifndef ZYDIS_DISABLE_AVX512
const ZydisInstructionDefinitionEVEX* def =
(const ZydisInstructionDefinitionEVEX*)definition;
def_ndsndd = def->op_ndsndd;
is_gather = def->is_gather;
no_source_dest_match = def->no_source_dest_match;
mask_policy = def->mask_policy;
// Check for invalid zero-mask
if ((instruction->raw.evex.z) && (!def->accepts_zero_mask))
{
return ZYDIS_STATUS_INVALID_MASK; // TODO: Dedicated status code
}
#else
ZYAN_UNREACHABLE;
#endif
break;
}
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
{
#ifndef ZYDIS_DISABLE_KNC
const ZydisInstructionDefinitionMVEX* def =
(const ZydisInstructionDefinitionMVEX*)definition;
def_ndsndd = def->op_ndsndd;
is_gather = def->is_gather;
mask_policy = def->mask_policy;
// Check for invalid MVEX.SSS values
static const ZyanU8 lookup[26][8] =
{
// ZYDIS_MVEX_FUNC_IGNORED
{ 1, 1, 1, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_INVALID
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_RC
{ 1, 1, 1, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_SAE
{ 1, 1, 1, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_F_32
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_I_32
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_F_64
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_I_64
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SWIZZLE_32
{ 1, 1, 1, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_SWIZZLE_64
{ 1, 1, 1, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_SF_32
{ 1, 1, 1, 1, 1, 0, 1, 1 },
// ZYDIS_MVEX_FUNC_SF_32_BCST
{ 1, 1, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16
{ 1, 0, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SF_64
{ 1, 1, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SI_32
{ 1, 1, 1, 0, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_SI_32_BCST
{ 1, 1, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16
{ 1, 0, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_SI_64
{ 1, 1, 1, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_UF_32
{ 1, 0, 0, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_UF_64
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_UI_32
{ 1, 0, 0, 0, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_UI_64
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_DF_32
{ 1, 0, 0, 1, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_DF_64
{ 1, 0, 0, 0, 0, 0, 0, 0 },
// ZYDIS_MVEX_FUNC_DI_32
{ 1, 0, 0, 0, 1, 1, 1, 1 },
// ZYDIS_MVEX_FUNC_DI_64
{ 1, 0, 0, 0, 0, 0, 0, 0 }
};
ZYAN_ASSERT(def->functionality < ZYAN_ARRAY_LENGTH(lookup));
ZYAN_ASSERT(instruction->raw.mvex.SSS < 8);
if (!lookup[def->functionality][instruction->raw.mvex.SSS])
{
return ZYDIS_STATUS_DECODING_ERROR;
}
#else
ZYAN_UNREACHABLE;
#endif
break;
}
default:
ZYAN_UNREACHABLE;
}
ZydisDecoderContext* context = state->context;
const ZyanBool is_reg = context->reg_info.is_mod_reg;
ZyanU8 no_rip_rel = ZYAN_FALSE;
ZyanU8 is_sr_dest_reg = ZYAN_FALSE;
ZyanU8 is_sr_dest_rm = ZYAN_FALSE;
if (def_reg)
{
is_sr_dest_reg = ZYDIS_OPDEF_GET_REG_HIGH_BIT(def_reg);
def_reg = ZYDIS_OPDEF_GET_REG(def_reg);
}
if (def_rm)
{
if (is_reg)
{
is_sr_dest_rm = ZYDIS_OPDEF_GET_REG_HIGH_BIT(def_rm);
def_rm = ZYDIS_OPDEF_GET_REG(def_rm);
}
else
{
no_rip_rel = ZYDIS_OPDEF_GET_MEM_HIGH_BIT(def_rm);
def_rm = ZYDIS_OPDEF_GET_MEM(def_rm);
}
}
// Check RIP-relative memory addressing
if (no_rip_rel)
{
const ZyanBool is_rip_rel =
(state->decoder->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(instruction->raw.modrm.mod == 0) && (instruction->raw.modrm.rm == 5);
if (is_rip_rel)
{
return ZYDIS_STATUS_BAD_REGISTER;
}
}
// Populate- and validate register constraints
ZYAN_CHECK(ZydisPopulateRegisterIds(context, instruction, def_reg, def_rm, def_ndsndd));
// `ZYDIS_REGISTER_CS` is not allowed as `MOV` target
if (is_sr_dest_reg && (context->reg_info.id_reg == 1))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
if (is_sr_dest_rm && (context->reg_info.id_rm == 1))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
// Check gather registers
if (is_gather)
{
// ZYAN_ASSERT(has_VSIB);
ZYAN_ASSERT(instruction->raw.modrm.mod != 3);
ZYAN_ASSERT(instruction->raw.modrm.rm == 4);
const ZyanU8 index = context->reg_info.id_index;
ZyanU8 dest = context->reg_info.id_reg;
ZyanU8 mask = 0xF0;
if (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_VEX)
{
ZYAN_ASSERT((def_reg == ZYDIS_REGKIND_VR) &&
(def_rm == ZYDIS_MEMOP_TYPE_VSIB) &&
(def_ndsndd == ZYDIS_REGKIND_VR));
mask = context->reg_info.id_ndsndd;
}
if ((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
ZYAN_ASSERT(((def_reg == ZYDIS_REGKIND_INVALID) ||
(def_reg == ZYDIS_REGKIND_VR)) &&
(def_rm == ZYDIS_MEMOP_TYPE_VSIB) &&
(def_ndsndd == ZYDIS_REGKIND_INVALID));
// Some gather instructions (like `VGATHERPF0{D|Q}{PS|PD}`) do not have a destination
// operand
if (!def_reg)
{
dest = 0xF1;
}
}
// If any pair of the index, mask, or destination registers are the same, the instruction
// results a UD fault
if ((dest == index) || (dest == mask) || (index == mask))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
}
// Check if any source register matches the destination register
if (no_source_dest_match)
{
ZYAN_ASSERT((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_VEX));
const ZyanU8 dest = context->reg_info.id_reg;
const ZyanU8 source1 = context->reg_info.id_ndsndd;
const ZyanU8 source2 = context->reg_info.id_rm;
if ((dest == source1) || (is_reg && (dest == source2)))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
}
// If any pair of the source or destination registers are the same, the instruction results a
// UD fault
if (no_source_source_match) // TODO: Find better name
{
ZYAN_ASSERT(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_VEX);
ZYAN_ASSERT(is_reg);
const ZyanU8 dest = context->reg_info.id_reg;
const ZyanU8 source1 = context->reg_info.id_ndsndd;
const ZyanU8 source2 = context->reg_info.id_rm;
if ((dest == source1) || (dest == source2) || (source1 == source2))
{
return ZYDIS_STATUS_BAD_REGISTER;
}
}
#if !defined(ZYDIS_DISABLE_AVX512) || !defined(ZYDIS_DISABLE_KNC)
// Check for invalid MASK registers
switch (mask_policy)
{
case ZYDIS_MASK_POLICY_INVALID:
case ZYDIS_MASK_POLICY_ALLOWED:
// Nothing to do here
break;
case ZYDIS_MASK_POLICY_REQUIRED:
if (!context->vector_unified.mask)
{
return ZYDIS_STATUS_INVALID_MASK;
}
break;
case ZYDIS_MASK_POLICY_FORBIDDEN:
if (context->vector_unified.mask)
{
return ZYDIS_STATUS_INVALID_MASK;
}
break;
default:
ZYAN_UNREACHABLE;
}
#endif
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Checks for certain post-decode error-conditions.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param definition A pointer to the `ZydisInstructionDefinition` struct.
*
* @return A zyan status code.
*
* This function is called immediately after a valid instruction-definition was found.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L4345-L4660
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecodeInstruction
|
static ZyanStatus ZydisDecodeInstruction(ZydisDecoderState* state,
ZydisDecodedInstruction* instruction)
{
ZYAN_ASSERT(state);
ZYAN_ASSERT(instruction);
// Iterate through the decoder tree
const ZydisDecoderTreeNode* node = ZydisDecoderTreeGetRootNode();
const ZydisDecoderTreeNode* temp = ZYAN_NULL;
ZydisDecoderTreeNodeType node_type;
do
{
node_type = node->type;
ZyanU16 index = 0;
ZyanStatus status = 0;
switch (node_type)
{
case ZYDIS_NODETYPE_INVALID:
if (temp)
{
node = temp;
temp = ZYAN_NULL;
node_type = ZYDIS_NODETYPE_FILTER_MANDATORY_PREFIX;
if (state->prefixes.mandatory_candidate != 0x00)
{
instruction->raw.prefixes[state->prefixes.offset_mandatory].type =
ZYDIS_PREFIX_TYPE_IGNORED;
}
if (state->prefixes.mandatory_candidate == 0x66)
{
if (state->prefixes.offset_osz_override ==
state->prefixes.offset_mandatory)
{
instruction->raw.prefixes[state->prefixes.offset_mandatory].type =
ZYDIS_PREFIX_TYPE_EFFECTIVE;
}
instruction->attributes |= ZYDIS_ATTRIB_HAS_OPERANDSIZE;
}
continue;
}
return ZYDIS_STATUS_DECODING_ERROR;
case ZYDIS_NODETYPE_FILTER_XOP:
status = ZydisNodeHandlerXOP(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_VEX:
status = ZydisNodeHandlerVEX(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_EMVEX:
status = ZydisNodeHandlerEMVEX(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_OPCODE:
status = ZydisNodeHandlerOpcode(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODE:
status = ZydisNodeHandlerMode(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODE_COMPACT:
status = ZydisNodeHandlerModeCompact(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODRM_MOD:
status = ZydisNodeHandlerModrmMod(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODRM_MOD_COMPACT:
status = ZydisNodeHandlerModrmModCompact(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODRM_REG:
status = ZydisNodeHandlerModrmReg(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_MODRM_RM:
status = ZydisNodeHandlerModrmRm(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_PREFIX_GROUP1:
index = state->prefixes.group1 ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MANDATORY_PREFIX:
status = ZydisNodeHandlerMandatoryPrefix(state, instruction, &index);
temp = ZydisDecoderTreeGetChildNode(node, 0);
// TODO: Return to this point, if index == 0 contains a value and the previous path
// TODO: was not successful
// TODO: Restore consumed prefix
break;
case ZYDIS_NODETYPE_FILTER_OPERAND_SIZE:
status = ZydisNodeHandlerOperandSize(state, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_ADDRESS_SIZE:
status = ZydisNodeHandlerAddressSize(instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_VECTOR_LENGTH:
status = ZydisNodeHandlerVectorLength(state->context, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_REX_W:
status = ZydisNodeHandlerRexW(state->context, instruction, &index);
break;
case ZYDIS_NODETYPE_FILTER_REX_B:
status = ZydisNodeHandlerRexB(state->context, instruction, &index);
break;
#ifndef ZYDIS_DISABLE_AVX512
case ZYDIS_NODETYPE_FILTER_EVEX_B:
status = ZydisNodeHandlerEvexB(instruction, &index);
break;
#endif
#ifndef ZYDIS_DISABLE_KNC
case ZYDIS_NODETYPE_FILTER_MVEX_E:
status = ZydisNodeHandlerMvexE(instruction, &index);
break;
#endif
case ZYDIS_NODETYPE_FILTER_MODE_AMD:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_AMD_BRANCHES] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_KNC:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_KNC] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_MPX:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_MPX] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_CET:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_CET] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_LZCNT:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_LZCNT] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_TZCNT:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_TZCNT] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_WBNOINVD:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_WBNOINVD] ? 1 : 0;
break;
case ZYDIS_NODETYPE_FILTER_MODE_CLDEMOTE:
index = state->decoder->decoder_mode[ZYDIS_DECODER_MODE_CLDEMOTE] ? 1 : 0;
break;
default:
if (node_type & ZYDIS_NODETYPE_DEFINITION_MASK)
{
const ZydisInstructionDefinition* definition;
ZydisGetInstructionDefinition(instruction->encoding, node->value, &definition);
ZydisSetEffectiveOperandWidth(state->context, instruction, definition);
ZydisSetEffectiveAddressWidth(state->context, instruction, definition);
const ZydisInstructionEncodingInfo* info;
ZydisGetInstructionEncodingInfo(node, &info);
ZYAN_CHECK(ZydisDecodeOptionalInstructionParts(state, instruction, info));
ZYAN_CHECK(ZydisCheckErrorConditions(state, instruction, definition));
if (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{
// Get actual 3DNOW opcode and definition
ZYAN_CHECK(ZydisInputNext(state, instruction, &instruction->opcode));
node = ZydisDecoderTreeGetRootNode();
node = ZydisDecoderTreeGetChildNode(node, 0x0F);
node = ZydisDecoderTreeGetChildNode(node, 0x0F);
node = ZydisDecoderTreeGetChildNode(node, instruction->opcode);
if (node->type == ZYDIS_NODETYPE_INVALID)
{
return ZYDIS_STATUS_DECODING_ERROR;
}
ZYAN_ASSERT(node->type == ZYDIS_NODETYPE_FILTER_MODRM_MOD_COMPACT);
node = ZydisDecoderTreeGetChildNode(
node, (instruction->raw.modrm.mod == 0x3) ? 0 : 1);
ZYAN_ASSERT(node->type & ZYDIS_NODETYPE_DEFINITION_MASK);
ZydisGetInstructionDefinition(instruction->encoding, node->value, &definition);
}
instruction->mnemonic = definition->mnemonic;
#ifndef ZYDIS_MINIMAL_MODE
instruction->operand_count = definition->operand_count;
instruction->operand_count_visible = definition->operand_count_visible;
state->context->definition = definition;
instruction->meta.category = definition->category;
instruction->meta.isa_set = definition->isa_set;
instruction->meta.isa_ext = definition->isa_ext;
instruction->meta.branch_type = definition->branch_type;
ZYAN_ASSERT((instruction->meta.branch_type == ZYDIS_BRANCH_TYPE_NONE) ||
((instruction->meta.category == ZYDIS_CATEGORY_CALL) ||
(instruction->meta.category == ZYDIS_CATEGORY_COND_BR) ||
(instruction->meta.category == ZYDIS_CATEGORY_UNCOND_BR) ||
(instruction->meta.category == ZYDIS_CATEGORY_RET)));
instruction->meta.exception_class = definition->exception_class;
if (!state->decoder->decoder_mode[ZYDIS_DECODER_MODE_MINIMAL])
{
ZydisSetAttributes(state, instruction, definition);
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
ZydisSetAVXInformation(state->context, instruction, definition);
break;
default:
break;
}
const ZydisDefinitionAccessedFlags* flags;
if (ZydisGetAccessedFlags(definition, &flags))
{
instruction->attributes |= ZYDIS_ATTRIB_CPUFLAG_ACCESS;
instruction->cpu_flags = &flags->cpu_flags;
instruction->fpu_flags = &flags->fpu_flags;
}
}
#endif
return ZYAN_STATUS_SUCCESS;
}
ZYAN_UNREACHABLE;
}
ZYAN_CHECK(status);
node = ZydisDecoderTreeGetChildNode(node, index);
} while ((node_type != ZYDIS_NODETYPE_INVALID) && !(node_type & ZYDIS_NODETYPE_DEFINITION_MASK));
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/**
* Uses the decoder-tree to decode the current instruction.
*
* @param state A pointer to the `ZydisDecoderState` struct.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L4672-L4887
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisDecoderInit
|
ZyanStatus ZydisDecoderInit(ZydisDecoder* decoder, ZydisMachineMode machine_mode,
ZydisStackWidth stack_width)
{
static const ZyanBool decoder_modes[ZYDIS_DECODER_MODE_MAX_VALUE + 1] =
{
#ifdef ZYDIS_MINIMAL_MODE
ZYAN_TRUE , // ZYDIS_DECODER_MODE_MINIMAL
#else
ZYAN_FALSE, // ZYDIS_DECODER_MODE_MINIMAL
#endif
ZYAN_FALSE, // ZYDIS_DECODER_MODE_AMD_BRANCHES
ZYAN_FALSE, // ZYDIS_DECODER_MODE_KNC
ZYAN_TRUE , // ZYDIS_DECODER_MODE_MPX
ZYAN_TRUE , // ZYDIS_DECODER_MODE_CET
ZYAN_TRUE , // ZYDIS_DECODER_MODE_LZCNT
ZYAN_TRUE , // ZYDIS_DECODER_MODE_TZCNT
ZYAN_FALSE, // ZYDIS_DECODER_MODE_WBNOINVD
ZYAN_TRUE // ZYDIS_DECODER_MODE_CLDEMOTE
};
if (!decoder)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
switch (machine_mode)
{
case ZYDIS_MACHINE_MODE_LONG_64:
if (stack_width != ZYDIS_STACK_WIDTH_64)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
break;
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
case ZYDIS_MACHINE_MODE_LEGACY_32:
case ZYDIS_MACHINE_MODE_LEGACY_16:
case ZYDIS_MACHINE_MODE_REAL_16:
if ((stack_width != ZYDIS_STACK_WIDTH_16) && (stack_width != ZYDIS_STACK_WIDTH_32))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
decoder->machine_mode = machine_mode;
decoder->stack_width = stack_width;
ZYAN_MEMCPY(&decoder->decoder_mode, &decoder_modes, sizeof(decoder_modes));
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Decoder.c#L4895-L4946
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetEncodableEncoding
|
ZydisEncodableEncoding ZydisGetEncodableEncoding(ZydisInstructionEncoding encoding)
{
static const ZydisEncodableEncoding encoding_lookup[6] =
{
ZYDIS_ENCODABLE_ENCODING_LEGACY,
ZYDIS_ENCODABLE_ENCODING_3DNOW,
ZYDIS_ENCODABLE_ENCODING_XOP,
ZYDIS_ENCODABLE_ENCODING_VEX,
ZYDIS_ENCODABLE_ENCODING_EVEX,
ZYDIS_ENCODABLE_ENCODING_MVEX,
};
ZYAN_ASSERT((ZyanUSize)encoding <= ZYDIS_INSTRUCTION_ENCODING_MAX_VALUE);
return encoding_lookup[encoding];
}
|
/* ============================================================================================== */
/* Internal functions */
/* ============================================================================================== */
/**
* Converts `ZydisInstructionEncoding` to `ZydisEncodableEncoding`.
*
* @param encoding `ZydisInstructionEncoding` value to convert.
*
* @return Equivalent `ZydisEncodableEncoding` value.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L265-L278
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetMachineModeWidth
|
ZyanU8 ZydisGetMachineModeWidth(ZydisMachineMode machine_mode)
{
switch (machine_mode)
{
case ZYDIS_MACHINE_MODE_REAL_16:
case ZYDIS_MACHINE_MODE_LEGACY_16:
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
return 16;
case ZYDIS_MACHINE_MODE_LEGACY_32:
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
return 32;
case ZYDIS_MACHINE_MODE_LONG_64:
return 64;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Converts `ZydisMachineMode` to default stack width value expressed in bits.
*
* @param machine_mode `ZydisMachineMode` value to convert.
*
* @return Stack width for requested machine mode.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L287-L303
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetAszFromHint
|
ZyanU8 ZydisGetAszFromHint(ZydisAddressSizeHint hint)
{
switch (hint)
{
case ZYDIS_ADDRESS_SIZE_HINT_NONE:
return 0;
case ZYDIS_ADDRESS_SIZE_HINT_16:
return 16;
case ZYDIS_ADDRESS_SIZE_HINT_32:
return 32;
case ZYDIS_ADDRESS_SIZE_HINT_64:
return 64;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Converts `ZydisAddressSizeHint` to address size expressed in bits.
*
* @param hint Address size hint.
*
* @return Address size in bits.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L312-L327
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetOszFromHint
|
ZyanU8 ZydisGetOszFromHint(ZydisOperandSizeHint hint)
{
switch (hint)
{
case ZYDIS_OPERAND_SIZE_HINT_NONE:
return 0;
case ZYDIS_OPERAND_SIZE_HINT_8:
return 8;
case ZYDIS_OPERAND_SIZE_HINT_16:
return 16;
case ZYDIS_OPERAND_SIZE_HINT_32:
return 32;
case ZYDIS_OPERAND_SIZE_HINT_64:
return 64;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Converts `ZydisOperandSizeHint` to operand size expressed in bits.
*
* @param hint Operand size hint.
*
* @return Operand size in bits.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L336-L353
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetOperandSizeFromElementSize
|
ZyanU8 ZydisGetOperandSizeFromElementSize(ZydisEncoderInstructionMatch *match,
const ZyanU16 *size_table, ZyanU16 desired_size, ZyanBool exact_match_mode)
{
if ((match->base_definition->operand_size_map == ZYDIS_OPSIZE_MAP_DEFAULT64) &&
(match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64))
{
if (size_table[2] == desired_size)
{
return 64;
}
else if (size_table[0] == desired_size)
{
return 16;
}
}
else if ((match->base_definition->operand_size_map == ZYDIS_OPSIZE_MAP_FORCE64) &&
(match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64))
{
if (size_table[2] == desired_size)
{
return 64;
}
}
else
{
static const ZyanI8 eosz_priority_lookup[4][3] =
{
{ 0, 1, -1 },
{ 1, 0, -1 },
{ 1, 2, 0 },
};
const ZyanU8 eosz_index = ZydisGetMachineModeWidth(match->request->machine_mode) >> 5;
for (int i = 0; i < 3; ++i)
{
const ZyanI8 eosz_candidate = eosz_priority_lookup[eosz_index][i];
if ((eosz_candidate == -1) ||
!(match->definition->operand_sizes & (1 << eosz_candidate)))
{
continue;
}
if ((exact_match_mode && (size_table[eosz_candidate] == desired_size)) ||
(!exact_match_mode && (size_table[eosz_candidate] >= desired_size)))
{
return 16 << eosz_candidate;
}
}
}
return 0;
}
|
/**
* Calculates effective operand size.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param size_table Array of possible size values for different operand sizes.
* @param desired_size Operand size requested by caller.
* @param exact_match_mode True if desired_size must be matched exactly, false when
* "not lower than" matching is desired.
*
* @return Effective operand size in bits.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L366-L415
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetScaledImmSize
|
ZyanU8 ZydisGetScaledImmSize(ZydisEncoderInstructionMatch *match, const ZyanU16 *size_table,
ZyanU8 min_imm_size)
{
if (match->eosz == 0)
{
match->eosz = ZydisGetOperandSizeFromElementSize(match, size_table, min_imm_size,
ZYAN_FALSE);
return match->eosz != 0 ? (ZyanU8)size_table[match->eosz >> 5] : 0;
}
const ZyanU8 index = match->eosz >> 5;
return size_table[index] >= min_imm_size ? (ZyanU8)size_table[index] : 0;
}
|
/**
* Calculates effective immediate size.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param size_table Array of possible size values for different operand sizes.
* @param min_imm_size Minimum immediate size.
*
* @return Effective operand size in bits.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L426-L438
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetSignedImmSize
|
ZyanU8 ZydisGetSignedImmSize(ZyanI64 imm)
{
if (imm >= ZYAN_INT8_MIN && imm <= ZYAN_INT8_MAX)
{
return 8;
}
if (imm >= ZYAN_INT16_MIN && imm <= ZYAN_INT16_MAX)
{
return 16;
}
if (imm >= ZYAN_INT32_MIN && imm <= ZYAN_INT32_MAX)
{
return 32;
}
return 64;
}
|
/**
* Calculates size of smallest integral type able to represent provided signed value.
*
* @param imm Immediate to be represented.
*
* @return Size of smallest integral type able to represent provided signed value.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L447-L463
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetUnsignedImmSize
|
ZyanU8 ZydisGetUnsignedImmSize(ZyanU64 imm)
{
if (imm <= ZYAN_UINT8_MAX)
{
return 8;
}
if (imm <= ZYAN_UINT16_MAX)
{
return 16;
}
if (imm <= ZYAN_UINT32_MAX)
{
return 32;
}
return 64;
}
|
/**
* Calculates size of smallest integral type able to represent provided unsigned value.
*
* @param imm Immediate to be represented.
*
* @return Size of smallest integral type able to represent provided unsigned value.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L472-L488
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsImmSigned
|
ZyanBool ZydisIsImmSigned(ZydisOperandEncoding encoding)
{
switch (encoding)
{
case ZYDIS_OPERAND_ENCODING_SIMM8:
case ZYDIS_OPERAND_ENCODING_SIMM16:
case ZYDIS_OPERAND_ENCODING_SIMM32:
case ZYDIS_OPERAND_ENCODING_SIMM64:
case ZYDIS_OPERAND_ENCODING_SIMM16_32_64:
case ZYDIS_OPERAND_ENCODING_SIMM32_32_64:
case ZYDIS_OPERAND_ENCODING_SIMM16_32_32:
case ZYDIS_OPERAND_ENCODING_JIMM8:
case ZYDIS_OPERAND_ENCODING_JIMM16:
case ZYDIS_OPERAND_ENCODING_JIMM32:
case ZYDIS_OPERAND_ENCODING_JIMM64:
case ZYDIS_OPERAND_ENCODING_JIMM16_32_64:
case ZYDIS_OPERAND_ENCODING_JIMM32_32_64:
case ZYDIS_OPERAND_ENCODING_JIMM16_32_32:
return ZYAN_TRUE;
case ZYDIS_OPERAND_ENCODING_DISP8:
case ZYDIS_OPERAND_ENCODING_DISP16:
case ZYDIS_OPERAND_ENCODING_DISP32:
case ZYDIS_OPERAND_ENCODING_DISP64:
case ZYDIS_OPERAND_ENCODING_DISP16_32_64:
case ZYDIS_OPERAND_ENCODING_DISP32_32_64:
case ZYDIS_OPERAND_ENCODING_DISP16_32_32:
case ZYDIS_OPERAND_ENCODING_UIMM8:
case ZYDIS_OPERAND_ENCODING_UIMM16:
case ZYDIS_OPERAND_ENCODING_UIMM32:
case ZYDIS_OPERAND_ENCODING_UIMM64:
case ZYDIS_OPERAND_ENCODING_UIMM16_32_64:
case ZYDIS_OPERAND_ENCODING_UIMM32_32_64:
case ZYDIS_OPERAND_ENCODING_UIMM16_32_32:
case ZYDIS_OPERAND_ENCODING_IS4:
return ZYAN_FALSE;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Checks if operand encoding encodes a signed immediate value.
*
* @param encoding Operand encoding for immediate value.
*
* @return True for encodings that represent signed values, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L497-L535
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetEffectiveImmSize
|
ZyanU8 ZydisGetEffectiveImmSize(ZydisEncoderInstructionMatch *match, ZyanI64 imm,
const ZydisOperandDefinition *def_op)
{
ZyanU8 eisz = 0;
ZyanU8 min_size = ZydisIsImmSigned((ZydisOperandEncoding)def_op->op.encoding)
? ZydisGetSignedImmSize(imm)
: ZydisGetUnsignedImmSize((ZyanU64)imm);
switch (def_op->op.encoding)
{
case ZYDIS_OPERAND_ENCODING_UIMM8:
case ZYDIS_OPERAND_ENCODING_SIMM8:
eisz = 8;
break;
case ZYDIS_OPERAND_ENCODING_IS4:
ZYAN_ASSERT(def_op->element_type == ZYDIS_IELEMENT_TYPE_UINT8);
eisz = ((ZyanU64)imm <= 15) ? 8 : 0;
break;
case ZYDIS_OPERAND_ENCODING_UIMM16:
case ZYDIS_OPERAND_ENCODING_SIMM16:
eisz = 16;
break;
case ZYDIS_OPERAND_ENCODING_UIMM32:
case ZYDIS_OPERAND_ENCODING_SIMM32:
eisz = 32;
break;
case ZYDIS_OPERAND_ENCODING_UIMM64:
case ZYDIS_OPERAND_ENCODING_SIMM64:
eisz = 64;
break;
case ZYDIS_OPERAND_ENCODING_UIMM16_32_64:
case ZYDIS_OPERAND_ENCODING_SIMM16_32_64:
{
static const ZyanU16 simm16_32_64_sizes[3] = { 16, 32, 64 };
return ZydisGetScaledImmSize(match, simm16_32_64_sizes, min_size);
}
case ZYDIS_OPERAND_ENCODING_UIMM32_32_64:
case ZYDIS_OPERAND_ENCODING_SIMM32_32_64:
{
static const ZyanU16 simm32_32_64_sizes[3] = { 32, 32, 64 };
return ZydisGetScaledImmSize(match, simm32_32_64_sizes, min_size);
}
case ZYDIS_OPERAND_ENCODING_UIMM16_32_32:
case ZYDIS_OPERAND_ENCODING_SIMM16_32_32:
{
static const ZyanU16 simm16_32_32_sizes[3] = { 16, 32, 32 };
return ZydisGetScaledImmSize(match, simm16_32_32_sizes, min_size);
}
case ZYDIS_OPERAND_ENCODING_DISP16_32_64:
ZYAN_ASSERT(match->easz == 0);
if (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
if (min_size < 32)
{
min_size = 32;
}
if (min_size == 32 || min_size == 64)
{
match->easz = eisz = min_size;
}
}
else
{
if (min_size < 16)
{
min_size = 16;
}
if (min_size == 16 || min_size == 32)
{
match->easz = eisz = min_size;
}
}
break;
case ZYDIS_OPERAND_ENCODING_JIMM8:
case ZYDIS_OPERAND_ENCODING_JIMM16:
case ZYDIS_OPERAND_ENCODING_JIMM32:
case ZYDIS_OPERAND_ENCODING_JIMM64:
{
ZyanU8 jimm_index = def_op->op.encoding - ZYDIS_OPERAND_ENCODING_JIMM8;
if ((match->request->branch_width != ZYDIS_BRANCH_WIDTH_NONE) &&
(match->request->branch_width != (ZydisBranchWidth)(ZYDIS_BRANCH_WIDTH_8 + jimm_index)))
{
return 0;
}
eisz = 8 << jimm_index;
break;
}
case ZYDIS_OPERAND_ENCODING_JIMM16_32_32:
switch (match->request->branch_width)
{
case ZYDIS_BRANCH_WIDTH_NONE:
{
static const ZyanU16 jimm16_32_32_sizes[3] = { 16, 32, 32 };
return ZydisGetScaledImmSize(match, jimm16_32_32_sizes, min_size);
}
case ZYDIS_BRANCH_WIDTH_16:
eisz = 16;
break;
case ZYDIS_BRANCH_WIDTH_32:
eisz = 32;
break;
case ZYDIS_BRANCH_WIDTH_8:
case ZYDIS_BRANCH_WIDTH_64:
return 0;
default:
ZYAN_UNREACHABLE;
}
break;
default:
ZYAN_UNREACHABLE;
}
return eisz >= min_size ? eisz : 0;
}
|
/**
* Calculates effective immediate size.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param imm Immediate value to encode.
* @param def_op Operand definition for immediate operand.
*
* @return Effective operand size in bits (0 if function failed).
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L546-L659
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCheckOsz
|
ZyanBool ZydisCheckOsz(ZydisEncoderInstructionMatch *match, ZydisRegisterWidth reg_width)
{
ZYAN_ASSERT(reg_width <= ZYAN_UINT8_MAX);
if (match->eosz == 0)
{
if (reg_width == 8)
{
return ZYAN_FALSE;
}
match->eosz = (ZyanU8)reg_width;
return ZYAN_TRUE;
}
return match->eosz == (ZyanU8)reg_width ? ZYAN_TRUE : ZYAN_FALSE;
}
|
/**
* Checks if register width is compatible with effective operand size.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param reg_width Register width in bits.
*
* @return True if width is compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L669-L683
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCheckAsz
|
ZyanBool ZydisCheckAsz(ZydisEncoderInstructionMatch *match, ZydisRegisterWidth reg_width)
{
ZYAN_ASSERT(reg_width <= ZYAN_UINT8_MAX);
if (match->easz == 0)
{
if ((match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(reg_width == 16))
{
return ZYAN_FALSE;
}
match->easz = (ZyanU8)reg_width;
return ZYAN_TRUE;
}
return match->easz == (ZyanU8)reg_width ? ZYAN_TRUE : ZYAN_FALSE;
}
|
/**
* Checks if register width is compatible with effective address size.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param reg_width Register width in bits.
*
* @return True if width is compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L693-L708
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsRegisterAllowed
|
ZyanBool ZydisIsRegisterAllowed(ZydisEncoderInstructionMatch *match, ZydisRegister reg,
ZydisRegisterClass reg_class)
{
const ZyanI8 reg_id = ZydisRegisterGetId(reg);
ZYAN_ASSERT(reg_id >= 0 && reg_id <= 31);
if (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
if ((match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_EVEX) &&
(match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_MVEX) &&
(reg_class != ZYDIS_REGCLASS_GPR8) &&
(reg_id >= 16))
{
return ZYAN_FALSE;
}
}
else
{
if (reg_class == ZYDIS_REGCLASS_GPR64)
{
return ZYAN_FALSE;
}
if (reg_id >= 8)
{
return ZYAN_FALSE;
}
}
return ZYAN_TRUE;
}
|
/**
* Checks if specified register is valid for provided register class, encoding and machine mode.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param reg `ZydisRegister` value.
* @param reg_class Register class.
*
* @return True if register value is allowed, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L719-L747
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsScaleValid
|
ZyanBool ZydisIsScaleValid(ZyanU8 scale)
{
switch (scale)
{
case 0:
case 1:
case 2:
case 4:
case 8:
return ZYAN_TRUE;
default:
return ZYAN_FALSE;
}
}
|
/**
* Checks if specified scale value is valid for use with SIB addressing.
*
* @param scale Scale value.
*
* @return True if value is valid, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L756-L769
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisValidateRexType
|
ZyanBool ZydisValidateRexType(ZydisEncoderInstructionMatch *match, ZydisRegister reg,
ZyanBool addressing_mode)
{
switch (reg)
{
case ZYDIS_REGISTER_AL:
case ZYDIS_REGISTER_CL:
case ZYDIS_REGISTER_DL:
case ZYDIS_REGISTER_BL:
return ZYAN_TRUE;
case ZYDIS_REGISTER_AH:
case ZYDIS_REGISTER_CH:
case ZYDIS_REGISTER_DH:
case ZYDIS_REGISTER_BH:
if (match->rex_type == ZYDIS_REX_TYPE_UNKNOWN)
{
match->rex_type = ZYDIS_REX_TYPE_FORBIDDEN;
}
else if (match->rex_type == ZYDIS_REX_TYPE_REQUIRED)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_REGISTER_SPL:
case ZYDIS_REGISTER_BPL:
case ZYDIS_REGISTER_SIL:
case ZYDIS_REGISTER_DIL:
case ZYDIS_REGISTER_R8B:
case ZYDIS_REGISTER_R9B:
case ZYDIS_REGISTER_R10B:
case ZYDIS_REGISTER_R11B:
case ZYDIS_REGISTER_R12B:
case ZYDIS_REGISTER_R13B:
case ZYDIS_REGISTER_R14B:
case ZYDIS_REGISTER_R15B:
if (match->rex_type == ZYDIS_REX_TYPE_UNKNOWN)
{
match->rex_type = ZYDIS_REX_TYPE_REQUIRED;
}
else if (match->rex_type == ZYDIS_REX_TYPE_FORBIDDEN)
{
return ZYAN_FALSE;
}
break;
default:
if ((ZydisRegisterGetId(reg) > 7) ||
(!addressing_mode && (ZydisRegisterGetClass(reg) == ZYDIS_REGCLASS_GPR64)))
{
if (match->rex_type == ZYDIS_REX_TYPE_UNKNOWN)
{
match->rex_type = ZYDIS_REX_TYPE_REQUIRED;
}
else if (match->rex_type == ZYDIS_REX_TYPE_FORBIDDEN)
{
return ZYAN_FALSE;
}
}
break;
}
return ZYAN_TRUE;
}
|
/**
* Enforces register usage constraints associated with usage of `REX` prefix.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param reg `ZydisRegister` value.
* @param addressing_mode True if checked address is used for address calculations. This
* implies more permissive checks.
*
* @return True if register usage is allowed, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L781-L842
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsValidAddressingClass
|
ZyanBool ZydisIsValidAddressingClass(ZydisEncoderInstructionMatch *match,
ZydisRegisterClass reg_class, ZydisRegister reg)
{
ZyanBool result;
const ZyanBool is_64 = (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64);
switch (reg_class)
{
case ZYDIS_REGCLASS_INVALID:
return ZYAN_TRUE;
case ZYDIS_REGCLASS_GPR16:
result = !is_64;
break;
case ZYDIS_REGCLASS_GPR32:
result = is_64 || ZydisRegisterGetId(reg) < 8;
break;
case ZYDIS_REGCLASS_GPR64:
result = is_64;
break;
default:
return ZYAN_FALSE;
}
return result && ZydisValidateRexType(match, reg, ZYAN_TRUE);
}
|
/**
* Checks if specified register is valid for use with SIB addressing.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param reg_class Register class.
* @param reg `ZydisRegister` value.
*
* @return True if register value is allowed, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L853-L876
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetRm16
|
ZyanI8 ZydisGetRm16(ZydisRegister base, ZydisRegister index)
{
static const ZydisRegister modrm16_lookup[8][2] =
{
{ ZYDIS_REGISTER_BX, ZYDIS_REGISTER_SI },
{ ZYDIS_REGISTER_BX, ZYDIS_REGISTER_DI },
{ ZYDIS_REGISTER_BP, ZYDIS_REGISTER_SI },
{ ZYDIS_REGISTER_BP, ZYDIS_REGISTER_DI },
{ ZYDIS_REGISTER_SI, ZYDIS_REGISTER_NONE },
{ ZYDIS_REGISTER_DI, ZYDIS_REGISTER_NONE },
{ ZYDIS_REGISTER_BP, ZYDIS_REGISTER_NONE },
{ ZYDIS_REGISTER_BX, ZYDIS_REGISTER_NONE },
};
for (ZyanI8 i = 0; i < (ZyanI8)ZYAN_ARRAY_LENGTH(modrm16_lookup); ++i)
{
if ((modrm16_lookup[i][0] == base) &&
(modrm16_lookup[i][1] == index))
{
return i;
}
}
return -1;
}
|
/**
* Helper function that determines correct `ModR/M.RM` value for 16-bit addressing mode.
*
* @param base `ZydisRegister` used as `SIB.base`.
* @param index `ZydisRegister` used as `SIB.index`.
*
* @return `ModR/M.RM` value (-1 if function failed).
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L886-L909
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncodeMvexBroadcastMode
|
ZyanU8 ZydisEncodeMvexBroadcastMode(ZydisBroadcastMode broadcast)
{
switch (broadcast)
{
case ZYDIS_BROADCAST_MODE_INVALID:
return 0;
case ZYDIS_BROADCAST_MODE_1_TO_16:
case ZYDIS_BROADCAST_MODE_1_TO_8:
return 1;
case ZYDIS_BROADCAST_MODE_4_TO_16:
case ZYDIS_BROADCAST_MODE_4_TO_8:
return 2;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Encodes `MVEX.sss` field for specified broadcast mode.
*
* @param broadcast Broadcast mode.
*
* @return Corresponding `MVEX.sss` value.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L918-L933
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncodeMvexConversionMode
|
ZyanU8 ZydisEncodeMvexConversionMode(ZydisConversionMode conversion)
{
switch (conversion)
{
case ZYDIS_CONVERSION_MODE_INVALID:
return 0;
case ZYDIS_CONVERSION_MODE_FLOAT16:
return 3;
case ZYDIS_CONVERSION_MODE_UINT8:
return 4;
case ZYDIS_CONVERSION_MODE_SINT8:
return 5;
case ZYDIS_CONVERSION_MODE_UINT16:
return 6;
case ZYDIS_CONVERSION_MODE_SINT16:
return 7;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Encodes `MVEX.sss` field for specified conversion mode.
*
* @param conversion Conversion mode.
*
* @return Corresponding `MVEX.sss` value.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L942-L961
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetCompDispScaleEvex
|
ZyanU8 ZydisGetCompDispScaleEvex(const ZydisEncoderInstructionMatch *match)
{
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
ZYAN_ASSERT(match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX);
ZYAN_ASSERT(evex_def->tuple_type);
ZYAN_ASSERT(evex_def->element_size);
const ZyanU8 vector_length = match->definition->vector_length - ZYDIS_VECTOR_LENGTH_128;
static const ZyanU8 size_indexes[ZYDIS_IELEMENT_SIZE_MAX_VALUE + 1] =
{
0, 0, 0, 1, 2, 4
};
ZYAN_ASSERT(evex_def->element_size < ZYAN_ARRAY_LENGTH(size_indexes));
const ZyanU8 size_index = size_indexes[evex_def->element_size];
switch (evex_def->tuple_type)
{
case ZYDIS_TUPLETYPE_FV:
{
static const ZyanU8 scales[2][3][3] =
{
/*B0*/ { /*16*/ { 4, 5, 6 }, /*32*/ { 4, 5, 6 }, /*64*/ { 4, 5, 6 } },
/*B1*/ { /*16*/ { 1, 1, 1 }, /*32*/ { 2, 2, 2 }, /*64*/ { 3, 3, 3 } }
};
const ZyanU8 broadcast = match->request->evex.broadcast ? 1 : 0;
ZYAN_ASSERT(size_index < 3);
return scales[broadcast][size_index][vector_length];
}
case ZYDIS_TUPLETYPE_HV:
{
static const ZyanU8 scales[2][2][3] =
{
/*B0*/ { /*16*/ { 3, 4, 5 }, /*32*/ { 3, 4, 5 } },
/*B1*/ { /*16*/ { 1, 1, 1 }, /*32*/ { 2, 2, 2 } }
};
const ZyanU8 broadcast = match->request->evex.broadcast ? 1 : 0;
ZYAN_ASSERT(size_index < 3);
return scales[broadcast][size_index][vector_length];
}
case ZYDIS_TUPLETYPE_FVM:
{
static const ZyanU8 scales[3] =
{
4, 5, 6
};
return scales[vector_length];
}
case ZYDIS_TUPLETYPE_GSCAT:
case ZYDIS_TUPLETYPE_T1S:
{
static const ZyanU8 scales[6] =
{
/* */ 0,
/* 8*/ 0,
/* 16*/ 1,
/* 32*/ 2,
/* 64*/ 3,
/*128*/ 4
};
ZYAN_ASSERT(evex_def->element_size < ZYAN_ARRAY_LENGTH(scales));
return scales[evex_def->element_size];
}
case ZYDIS_TUPLETYPE_T1F:
{
static const ZyanU8 scales[3] =
{
/* 16*/ 1,
/* 32*/ 2,
/* 64*/ 3
};
ZYAN_ASSERT(size_index < 3);
return scales[size_index];
}
case ZYDIS_TUPLETYPE_T1_4X:
return 4;
case ZYDIS_TUPLETYPE_T2:
return match->definition->rex_w ? 4 : 3;
case ZYDIS_TUPLETYPE_T4:
return match->definition->rex_w ? 5 : 4;
case ZYDIS_TUPLETYPE_T8:
return 5;
case ZYDIS_TUPLETYPE_HVM:
{
static const ZyanU8 scales[3] =
{
3, 4, 5
};
return scales[vector_length];
}
case ZYDIS_TUPLETYPE_QVM:
{
static const ZyanU8 scales[3] =
{
2, 3, 4
};
return scales[vector_length];
}
case ZYDIS_TUPLETYPE_OVM:
{
static const ZyanU8 scales[3] =
{
1, 2, 3
};
return scales[vector_length];
}
case ZYDIS_TUPLETYPE_M128:
return 4;
case ZYDIS_TUPLETYPE_DUP:
{
static const ZyanU8 scales[3] =
{
3, 5, 6
};
return scales[vector_length];
}
case ZYDIS_TUPLETYPE_QUARTER:
{
static const ZyanU8 scales[2][3] =
{
/*B0*/ { 2, 3, 4 },
/*B1*/ { 1, 1, 1 }
};
const ZyanU8 broadcast = match->request->evex.broadcast ? 1 : 0;
return scales[broadcast][vector_length];
}
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Determines scale factor for compressed 8-bit displacement (`EVEX` instructions only).
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return log2(scale factor)
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L970-L1098
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetCompDispScaleMvex
|
ZyanU8 ZydisGetCompDispScaleMvex(const ZydisEncoderInstructionMatch *match)
{
const ZydisInstructionDefinitionMVEX *mvex_def =
(const ZydisInstructionDefinitionMVEX *)match->base_definition;
ZyanU8 index = mvex_def->has_element_granularity;
ZYAN_ASSERT(!index || !mvex_def->broadcast);
if (!index && mvex_def->broadcast)
{
switch (mvex_def->broadcast)
{
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_8:
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_16:
index = 1;
break;
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_8:
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_16:
index = 2;
break;
default:
ZYAN_UNREACHABLE;
}
}
const ZyanU8 sss = ZydisEncodeMvexBroadcastMode(match->request->mvex.broadcast) |
ZydisEncodeMvexConversionMode(match->request->mvex.conversion);
switch (mvex_def->functionality)
{
case ZYDIS_MVEX_FUNC_IGNORED:
case ZYDIS_MVEX_FUNC_INVALID:
case ZYDIS_MVEX_FUNC_RC:
case ZYDIS_MVEX_FUNC_SAE:
case ZYDIS_MVEX_FUNC_SWIZZLE_32:
case ZYDIS_MVEX_FUNC_SWIZZLE_64:
return 0;
case ZYDIS_MVEX_FUNC_F_32:
case ZYDIS_MVEX_FUNC_I_32:
case ZYDIS_MVEX_FUNC_F_64:
case ZYDIS_MVEX_FUNC_I_64:
return 6;
case ZYDIS_MVEX_FUNC_SF_32:
case ZYDIS_MVEX_FUNC_SF_32_BCST:
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UF_32:
{
static const ZyanU8 lookup[3][8] =
{
{ 6, 2, 4, 5, 4, 4, 5, 5 },
{ 2, 0, 0, 1, 0, 0, 1, 1 },
{ 4, 0, 0, 3, 2, 2, 3, 3 }
};
ZYAN_ASSERT(sss < ZYAN_ARRAY_LENGTH(lookup[index]));
return lookup[index][sss];
}
case ZYDIS_MVEX_FUNC_SI_32:
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_SI_32_BCST:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
{
static const ZyanU8 lookup[3][8] =
{
{ 6, 2, 4, 0, 4, 4, 5, 5 },
{ 2, 0, 0, 0, 0, 0, 1, 1 },
{ 4, 0, 0, 0, 2, 2, 3, 3 }
};
ZYAN_ASSERT(sss < ZYAN_ARRAY_LENGTH(lookup[index]));
return lookup[index][sss];
}
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_SI_64:
case ZYDIS_MVEX_FUNC_UI_64:
{
static const ZyanU8 lookup[3][3] =
{
{ 6, 3, 5 },
{ 3, 0, 0 },
{ 5, 0, 0 }
};
ZYAN_ASSERT(sss < ZYAN_ARRAY_LENGTH(lookup[index]));
return lookup[index][sss];
}
case ZYDIS_MVEX_FUNC_DF_32:
case ZYDIS_MVEX_FUNC_DI_32:
{
static const ZyanU8 lookup[2][8] =
{
{ 6, 0, 0, 5, 4, 4, 5, 5 },
{ 2, 0, 0, 1, 0, 0, 1, 1 }
};
ZYAN_ASSERT(index < 2);
ZYAN_ASSERT(sss < ZYAN_ARRAY_LENGTH(lookup[index]));
return lookup[index][sss];
}
case ZYDIS_MVEX_FUNC_DF_64:
case ZYDIS_MVEX_FUNC_DI_64:
ZYAN_ASSERT(index < 2);
return index == 0 ? 6 : 3;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Determines scale factor for compressed 8-bit displacement (`MVEX` instructions only).
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return log2(scale factor)
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L1107-L1208
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetCompDispScale
|
ZyanU8 ZydisGetCompDispScale(const ZydisEncoderInstructionMatch *match)
{
switch (match->definition->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
return 0;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
return ZydisGetCompDispScaleEvex(match);
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
return ZydisGetCompDispScaleMvex(match);
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Determines scale factor for compressed 8-bit displacement.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return log2(scale factor)
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L1217-L1233
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsRegisterOperandCompatible
|
ZyanBool ZydisIsRegisterOperandCompatible(ZydisEncoderInstructionMatch *match,
const ZydisEncoderOperand *user_op, const ZydisOperandDefinition *def_op)
{
const ZydisRegisterClass reg_class = ZydisRegisterGetClass(user_op->reg.value);
const ZydisRegisterWidth reg_width = ZydisRegisterClassGetWidth(match->request->machine_mode,
reg_class);
if (reg_width == 0)
{
return ZYAN_FALSE;
}
ZyanBool is4_expected_value = ZYAN_FALSE;
switch (def_op->type)
{
case ZYDIS_SEMANTIC_OPTYPE_IMPLICIT_REG:
switch (def_op->op.reg.type)
{
case ZYDIS_IMPLREG_TYPE_STATIC:
if (def_op->op.reg.reg.reg != user_op->reg.value)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_IMPLREG_TYPE_GPR_OSZ:
if ((reg_class != ZYDIS_REGCLASS_GPR8) &&
(reg_class != ZYDIS_REGCLASS_GPR16) &&
(reg_class != ZYDIS_REGCLASS_GPR32) &&
(reg_class != ZYDIS_REGCLASS_GPR64))
{
return ZYAN_FALSE;
}
if (def_op->op.reg.reg.id != ZydisRegisterGetId(user_op->reg.value))
{
return ZYAN_FALSE;
}
if (!ZydisCheckOsz(match, reg_width))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_IMPLREG_TYPE_GPR_ASZ:
if ((reg_class != ZYDIS_REGCLASS_GPR8) &&
(reg_class != ZYDIS_REGCLASS_GPR16) &&
(reg_class != ZYDIS_REGCLASS_GPR32) &&
(reg_class != ZYDIS_REGCLASS_GPR64))
{
return ZYAN_FALSE;
}
if (def_op->op.reg.reg.id != ZydisRegisterGetId(user_op->reg.value))
{
return ZYAN_FALSE;
}
if (!ZydisCheckAsz(match, reg_width))
{
return ZYAN_FALSE;
}
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR8:
if (reg_class != ZYDIS_REGCLASS_GPR8)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
if (!ZydisValidateRexType(match, user_op->reg.value, ZYAN_FALSE))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR16:
if (reg_class != ZYDIS_REGCLASS_GPR16)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR32:
if (reg_class != ZYDIS_REGCLASS_GPR32)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR64:
if (reg_class != ZYDIS_REGCLASS_GPR64)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR16_32_64:
if ((reg_class != ZYDIS_REGCLASS_GPR16) &&
(reg_class != ZYDIS_REGCLASS_GPR32) &&
(reg_class != ZYDIS_REGCLASS_GPR64))
{
return ZYAN_FALSE;
}
if (!ZydisCheckOsz(match, reg_width))
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
if (!ZydisValidateRexType(match, user_op->reg.value, ZYAN_FALSE))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR32_32_64:
if ((reg_class != ZYDIS_REGCLASS_GPR32) &&
(reg_class != ZYDIS_REGCLASS_GPR64))
{
return ZYAN_FALSE;
}
if (match->eosz == 0)
{
if (reg_class == ZYDIS_REGCLASS_GPR64)
{
match->eosz = 64;
}
else
{
match->eosz64_forbidden = ZYAN_TRUE;
}
}
else if (match->eosz != (ZyanU8)reg_width)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
if (!ZydisValidateRexType(match, user_op->reg.value, ZYAN_FALSE))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR16_32_32:
if ((reg_class != ZYDIS_REGCLASS_GPR16) &&
(reg_class != ZYDIS_REGCLASS_GPR32))
{
return ZYAN_FALSE;
}
if (!ZydisCheckOsz(match, reg_width))
{
if (match->eosz != 64 || reg_class != ZYDIS_REGCLASS_GPR32)
{
return ZYAN_FALSE;
}
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_GPR_ASZ:
if ((reg_class != ZYDIS_REGCLASS_GPR16) &&
(reg_class != ZYDIS_REGCLASS_GPR32) &&
(reg_class != ZYDIS_REGCLASS_GPR64))
{
return ZYAN_FALSE;
}
if (!ZydisCheckAsz(match, reg_width))
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_FPR:
if (reg_class != ZYDIS_REGCLASS_X87)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_MMX:
if (reg_class != ZYDIS_REGCLASS_MMX)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_XMM:
if (reg_class != ZYDIS_REGCLASS_XMM)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
is4_expected_value = def_op->op.encoding == ZYDIS_OPERAND_ENCODING_IS4;
break;
case ZYDIS_SEMANTIC_OPTYPE_YMM:
if (reg_class != ZYDIS_REGCLASS_YMM)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
is4_expected_value = def_op->op.encoding == ZYDIS_OPERAND_ENCODING_IS4;
break;
case ZYDIS_SEMANTIC_OPTYPE_ZMM:
if (reg_class != ZYDIS_REGCLASS_ZMM)
{
return ZYAN_FALSE;
}
if (!ZydisIsRegisterAllowed(match, user_op->reg.value, reg_class))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_TMM:
if (reg_class != ZYDIS_REGCLASS_TMM)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_BND:
if (reg_class != ZYDIS_REGCLASS_BOUND)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_SREG:
if (reg_class != ZYDIS_REGCLASS_SEGMENT)
{
return ZYAN_FALSE;
}
if ((def_op->actions & ZYDIS_OPERAND_ACTION_MASK_WRITE) &&
(user_op->reg.value == ZYDIS_REGISTER_CS))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_CR:
{
if (reg_class != ZYDIS_REGCLASS_CONTROL)
{
return ZYAN_FALSE;
}
static const ZyanU8 cr_lookup[16] =
{
1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
};
const ZyanI8 reg_id = ZydisRegisterGetId(user_op->reg.value);
if ((match->request->machine_mode != ZYDIS_MACHINE_MODE_LONG_64) &&
(reg_id == 8))
{
return ZYAN_FALSE;
}
if (!cr_lookup[reg_id])
{
return ZYAN_FALSE;
}
break;
}
case ZYDIS_SEMANTIC_OPTYPE_DR:
if (reg_class != ZYDIS_REGCLASS_DEBUG)
{
return ZYAN_FALSE;
}
if (user_op->reg.value >= ZYDIS_REGISTER_DR8)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_MASK:
if (reg_class != ZYDIS_REGCLASS_MASK)
{
return ZYAN_FALSE;
}
// MVEX does not require similar policy check
if ((match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) &&
(def_op->op.encoding == ZYDIS_OPERAND_ENCODING_MASK))
{
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
ZYAN_ASSERT((evex_def->mask_policy != ZYDIS_MASK_POLICY_INVALID) &&
(evex_def->mask_policy != ZYDIS_MASK_POLICY_FORBIDDEN));
if ((evex_def->mask_policy == ZYDIS_MASK_POLICY_REQUIRED) &&
(user_op->reg.value == ZYDIS_REGISTER_K0))
{
return ZYAN_FALSE;
}
if ((evex_def->mask_policy == ZYDIS_MASK_POLICY_ALLOWED) &&
(match->request->evex.zeroing_mask) &&
(user_op->reg.value == ZYDIS_REGISTER_K0))
{
return ZYAN_FALSE;
}
}
break;
default:
ZYAN_UNREACHABLE;
}
if (user_op->reg.is4 != is4_expected_value)
{
return ZYAN_FALSE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested operand matches register operand from instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param user_op Operand definition from `ZydisEncoderRequest` structure.
* @param def_op Decoder's operand definition from current instruction definition.
*
* @return True if operands match, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L1244-L1565
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsMemoryOperandCompatible
|
ZyanBool ZydisIsMemoryOperandCompatible(ZydisEncoderInstructionMatch *match,
const ZydisEncoderOperand *user_op, const ZydisOperandDefinition *def_op)
{
switch (def_op->type)
{
case ZYDIS_SEMANTIC_OPTYPE_MEM:
case ZYDIS_SEMANTIC_OPTYPE_AGEN:
case ZYDIS_SEMANTIC_OPTYPE_MIB:
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBX:
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBY:
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBZ:
{
if ((def_op->type == ZYDIS_SEMANTIC_OPTYPE_MIB) &&
(user_op->mem.scale != 0))
{
return ZYAN_FALSE;
}
ZyanI64 displacement = user_op->mem.displacement;
ZyanU8 disp_size = 0;
if (displacement)
{
disp_size = ZydisGetSignedImmSize(displacement);
if (disp_size > 32)
{
return ZYAN_FALSE;
}
if (ZydisGetMachineModeWidth(match->request->machine_mode) == 16)
{
if ((ZyanI16)displacement == 0)
{
disp_size = 0;
}
else
{
disp_size = ZydisGetSignedImmSize((ZyanI16)displacement);
}
}
match->cd8_scale = ZydisGetCompDispScale(match);
if (match->cd8_scale)
{
const ZyanI64 mask = (1 << match->cd8_scale) - 1;
if (!(displacement & mask))
{
disp_size = ZydisGetSignedImmSize(displacement >> match->cd8_scale);
}
else if (disp_size == 8)
{
disp_size = 16;
}
}
}
if (def_op->type != ZYDIS_SEMANTIC_OPTYPE_AGEN)
{
if (match->eosz != 0)
{
const ZyanU8 eosz_index = match->eosz >> 5;
if (def_op->size[eosz_index] != user_op->mem.size)
{
return ZYAN_FALSE;
}
}
else if ((match->definition->vector_length != ZYDIS_VECTOR_LENGTH_INVALID) ||
(match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
ZyanU8 eosz_index = ZydisGetMachineModeWidth(match->request->machine_mode) >> 5;
if (match->eosz64_forbidden && (eosz_index == 2))
{
eosz_index = 1;
}
ZyanU16 allowed_mem_size = def_op->size[eosz_index];
if ((!allowed_mem_size) &&
(match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_VEX))
{
ZYAN_ASSERT((match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX));
switch (match->definition->vector_length)
{
case ZYDIS_VECTOR_LENGTH_128:
allowed_mem_size = 16;
break;
case ZYDIS_VECTOR_LENGTH_256:
allowed_mem_size = 32;
break;
case ZYDIS_VECTOR_LENGTH_INVALID:
ZYAN_ASSERT(match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX);
ZYAN_FALLTHROUGH;
case ZYDIS_VECTOR_LENGTH_512:
allowed_mem_size = 64;
break;
default:
ZYAN_UNREACHABLE;
}
if (match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX)
{
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
static const ZyanU8 element_sizes[ZYDIS_IELEMENT_SIZE_MAX_VALUE + 1] =
{
0, 1, 2, 4, 8, 16
};
ZYAN_ASSERT(evex_def->element_size < ZYAN_ARRAY_LENGTH(element_sizes));
const ZyanU8 element_size = element_sizes[evex_def->element_size];
if (match->request->evex.broadcast || evex_def->broadcast)
{
allowed_mem_size = element_size;
}
else
{
switch (evex_def->tuple_type)
{
case ZYDIS_TUPLETYPE_FV:
break;
case ZYDIS_TUPLETYPE_HV:
allowed_mem_size /= 2;
break;
case ZYDIS_TUPLETYPE_QUARTER:
allowed_mem_size /= 4;
break;
default:
ZYAN_UNREACHABLE;
}
}
}
else
{
const ZydisInstructionDefinitionMVEX *mvex_def =
(const ZydisInstructionDefinitionMVEX *)match->base_definition;
ZyanU16 element_size;
switch (match->request->mvex.conversion)
{
case ZYDIS_CONVERSION_MODE_INVALID:
switch (mvex_def->functionality)
{
case ZYDIS_MVEX_FUNC_SF_32:
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UF_32:
case ZYDIS_MVEX_FUNC_DF_32:
case ZYDIS_MVEX_FUNC_SI_32:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_DI_32:
allowed_mem_size = 64;
element_size = 4;
break;
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_DF_64:
case ZYDIS_MVEX_FUNC_SI_64:
case ZYDIS_MVEX_FUNC_UI_64:
case ZYDIS_MVEX_FUNC_DI_64:
allowed_mem_size = 64;
element_size = 8;
break;
case ZYDIS_MVEX_FUNC_SF_32_BCST:
case ZYDIS_MVEX_FUNC_SI_32_BCST:
allowed_mem_size = 32;
element_size = 4;
break;
default:
ZYAN_UNREACHABLE;
}
break;
case ZYDIS_CONVERSION_MODE_FLOAT16:
case ZYDIS_CONVERSION_MODE_SINT16:
case ZYDIS_CONVERSION_MODE_UINT16:
allowed_mem_size = 32;
element_size = 2;
break;
case ZYDIS_CONVERSION_MODE_SINT8:
case ZYDIS_CONVERSION_MODE_UINT8:
allowed_mem_size = 16;
element_size = 1;
break;
default:
ZYAN_UNREACHABLE;
}
ZYAN_ASSERT(!mvex_def->broadcast || !match->request->mvex.broadcast);
switch (mvex_def->broadcast)
{
case ZYDIS_MVEX_STATIC_BROADCAST_NONE:
break;
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_8:
case ZYDIS_MVEX_STATIC_BROADCAST_1_TO_16:
allowed_mem_size = element_size;
break;
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_8:
case ZYDIS_MVEX_STATIC_BROADCAST_4_TO_16:
allowed_mem_size = element_size * 4;
break;
default:
ZYAN_UNREACHABLE;
}
switch (match->request->mvex.broadcast)
{
case ZYDIS_BROADCAST_MODE_INVALID:
break;
case ZYDIS_BROADCAST_MODE_1_TO_8:
case ZYDIS_BROADCAST_MODE_1_TO_16:
allowed_mem_size = element_size;
break;
case ZYDIS_BROADCAST_MODE_4_TO_8:
case ZYDIS_BROADCAST_MODE_4_TO_16:
allowed_mem_size = element_size * 4;
break;
default:
ZYAN_UNREACHABLE;
}
}
}
if (user_op->mem.size != allowed_mem_size)
{
return ZYAN_FALSE;
}
}
else if (match->definition->rex_w)
{
match->eosz = 64;
}
else if (match->definition->vector_length == ZYDIS_VECTOR_LENGTH_INVALID)
{
match->eosz = ZydisGetOperandSizeFromElementSize(match, def_op->size,
user_op->mem.size, ZYAN_TRUE);
if (match->eosz == 0)
{
return ZYAN_FALSE;
}
}
else
{
ZYAN_UNREACHABLE;
}
}
else
{
if (match->easz != 0)
{
if (match->easz != user_op->mem.size)
{
return ZYAN_FALSE;
}
}
else
{
switch (user_op->mem.size)
{
case 2:
case 4:
case 8:
match->easz = (ZyanU8)user_op->mem.size << 3;
break;
default:
return ZYAN_FALSE;
}
}
}
ZydisRegisterClass vsib_index_class = ZYDIS_REGCLASS_INVALID;
ZyanBool is_vsib = ZYAN_TRUE;
switch (def_op->type)
{
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBX:
vsib_index_class = ZYDIS_REGCLASS_XMM;
break;
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBY:
vsib_index_class = ZYDIS_REGCLASS_YMM;
break;
case ZYDIS_SEMANTIC_OPTYPE_MEM_VSIBZ:
vsib_index_class = ZYDIS_REGCLASS_ZMM;
break;
default:
is_vsib = ZYAN_FALSE;
break;
}
const ZyanBool is_rip_relative = (user_op->mem.base == ZYDIS_REGISTER_RIP) ||
(user_op->mem.base == ZYDIS_REGISTER_EIP);
if (is_rip_relative)
{
const ZyanBool no_rip_rel = ZYDIS_OPDEF_GET_MEM_HIGH_BIT(match->base_definition->op_rm);
if (no_rip_rel || ((match->definition->modrm & 7) == 4))
{
return ZYAN_FALSE;
}
}
const ZydisRegisterClass reg_base_class = ZydisRegisterGetClass(user_op->mem.base);
if ((reg_base_class == ZYDIS_REGCLASS_INVALID) &&
(user_op->mem.base != ZYDIS_REGISTER_NONE))
{
return ZYAN_FALSE;
}
const ZydisRegisterClass reg_index_class = ZydisRegisterGetClass(user_op->mem.index);
if ((reg_index_class == ZYDIS_REGCLASS_INVALID) &&
(user_op->mem.index != ZYDIS_REGISTER_NONE))
{
return ZYAN_FALSE;
}
if (is_vsib)
{
const ZyanU8 mode_width = ZydisGetMachineModeWidth(match->request->machine_mode);
const ZyanI8 reg_index_id = ZydisRegisterGetId(user_op->mem.index);
if (((match->request->machine_mode != ZYDIS_MACHINE_MODE_LONG_64) ||
(reg_base_class != ZYDIS_REGCLASS_GPR64)) &&
(reg_base_class != ZYDIS_REGCLASS_GPR32) &&
(reg_base_class != ZYDIS_REGCLASS_INVALID))
{
return ZYAN_FALSE;
}
if ((reg_base_class == ZYDIS_REGCLASS_GPR32) &&
(mode_width != 64) &&
(ZydisRegisterGetId(user_op->mem.base) > 7))
{
return ZYAN_FALSE;
}
ZyanU8 max_reg_id = 7;
if (mode_width == 64)
{
max_reg_id = match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_VEX ?
31 : 15;
}
if ((reg_index_class != vsib_index_class) ||
(reg_index_id > max_reg_id))
{
return ZYAN_FALSE;
}
}
else
{
if (!ZydisIsValidAddressingClass(match, reg_base_class, user_op->mem.base))
{
if (!is_rip_relative || match->request->machine_mode != ZYDIS_MACHINE_MODE_LONG_64)
{
return ZYAN_FALSE;
}
}
if (!ZydisIsValidAddressingClass(match, reg_index_class, user_op->mem.index))
{
return ZYAN_FALSE;
}
if (reg_base_class != ZYDIS_REGCLASS_INVALID &&
reg_index_class != ZYDIS_REGCLASS_INVALID &&
reg_base_class != reg_index_class)
{
return ZYAN_FALSE;
}
if (user_op->mem.index == ZYDIS_REGISTER_ESP ||
user_op->mem.index == ZYDIS_REGISTER_RSP)
{
return ZYAN_FALSE;
}
}
if (reg_index_class != ZYDIS_REGCLASS_INVALID &&
user_op->mem.scale == 0 &&
def_op->type != ZYDIS_SEMANTIC_OPTYPE_MIB)
{
return ZYAN_FALSE;
}
if (reg_index_class == ZYDIS_REGCLASS_INVALID &&
user_op->mem.scale != 0)
{
return ZYAN_FALSE;
}
ZyanU8 candidate_easz = 0;
ZyanBool disp_only = ZYAN_FALSE;
if (reg_base_class != ZYDIS_REGCLASS_INVALID)
{
if (is_rip_relative)
{
candidate_easz = user_op->mem.base == ZYDIS_REGISTER_RIP ? 64 : 32;
}
else
{
candidate_easz = (ZyanU8)ZydisRegisterClassGetWidth(match->request->machine_mode,
reg_base_class);
}
}
else if (reg_index_class != ZYDIS_REGCLASS_INVALID)
{
if (is_vsib)
{
candidate_easz = ZydisGetMachineModeWidth(match->request->machine_mode);
}
else
{
candidate_easz = (ZyanU8)ZydisRegisterClassGetWidth(match->request->machine_mode,
reg_index_class);
}
}
else
{
ZyanU8 min_disp_size = match->easz ? match->easz : 16;
if (((min_disp_size == 16) && !(match->definition->address_sizes & ZYDIS_WIDTH_16)) ||
(min_disp_size == 64))
{
min_disp_size = 32;
}
if (ZydisGetUnsignedImmSize(displacement) == 16)
{
disp_size = 16;
}
if (disp_size < min_disp_size)
{
disp_size = min_disp_size;
}
if (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
candidate_easz = match->easz == 32 ? 32 : 64;
}
else
{
candidate_easz = disp_size;
}
disp_only = ZYAN_TRUE;
}
if (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
if (is_rip_relative && reg_index_class != ZYDIS_REGCLASS_INVALID)
{
return ZYAN_FALSE;
}
}
else
{
if (candidate_easz == 16 && !disp_only)
{
if (disp_size > 16)
{
return ZYAN_FALSE;
}
const ZyanI8 rm16 = ZydisGetRm16(user_op->mem.base, user_op->mem.index);
if (rm16 == -1)
{
return ZYAN_FALSE;
}
const ZyanU8 allowed_scale = rm16 < 4 ? 1 : 0;
if (user_op->mem.scale != allowed_scale)
{
return ZYAN_FALSE;
}
}
}
if (match->easz != 0)
{
if (match->easz != candidate_easz)
{
return ZYAN_FALSE;
}
}
else
{
match->easz = candidate_easz;
}
if ((match->base_definition->address_size_map == ZYDIS_ADSIZE_MAP_IGNORED) &&
(match->easz != ZydisGetMachineModeWidth(match->request->machine_mode)))
{
return ZYAN_FALSE;
}
match->disp_size = disp_size;
break;
}
case ZYDIS_SEMANTIC_OPTYPE_MOFFS:
if (user_op->mem.base != ZYDIS_REGISTER_NONE ||
user_op->mem.index != ZYDIS_REGISTER_NONE ||
user_op->mem.scale != 0)
{
return ZYAN_FALSE;
}
if (match->eosz != 0)
{
const ZyanU8 eosz_index = match->eosz >> 5;
if (def_op->size[eosz_index] != user_op->mem.size)
{
return ZYAN_FALSE;
}
}
else
{
match->eosz = ZydisGetOperandSizeFromElementSize(match, def_op->size,
user_op->mem.size, ZYAN_TRUE);
if (match->eosz == 0)
{
return ZYAN_FALSE;
}
}
match->disp_size = ZydisGetEffectiveImmSize(match, user_op->mem.displacement, def_op);
if (match->disp_size == 0)
{
return ZYAN_FALSE;
}
// This is not a standard rejection. It's a special case for `mov` instructions (only ones
// to use `moffs` operands). Size of `moffs` is tied to address size attribute, so its
// signedness doesn't matter. However if displacement can be represented as a signed
// integer of smaller size we reject `moffs` variant because it's guaranteed that better
// alternative exists (in terms of size).
ZyanU8 alternative_size = ZydisGetSignedImmSize(user_op->mem.displacement);
const ZyanU8 min_disp_size =
(match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) ? 32 : 16;
if (alternative_size < min_disp_size)
{
alternative_size = min_disp_size;
}
if (alternative_size < match->disp_size)
{
return ZYAN_FALSE;
}
break;
default:
ZYAN_UNREACHABLE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested operand matches memory operand from instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param user_op Operand definition from `ZydisEncoderRequest` structure.
* @param def_op Decoder's operand definition from current instruction definition.
*
* @return True if operands match, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L1576-L2088
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsPointerOperandCompatible
|
ZyanBool ZydisIsPointerOperandCompatible(ZydisEncoderInstructionMatch *match,
const ZydisEncoderOperand *user_op)
{
ZYAN_ASSERT(match->eosz == 0);
ZYAN_ASSERT(match->request->machine_mode != ZYDIS_MACHINE_MODE_LONG_64);
ZYAN_ASSERT((match->request->branch_type == ZYDIS_BRANCH_TYPE_NONE) ||
(match->request->branch_type == ZYDIS_BRANCH_TYPE_FAR));
const ZyanU8 min_disp_size = ZydisGetUnsignedImmSize(user_op->ptr.offset);
const ZyanU8 desired_disp_size = (match->request->branch_width == ZYDIS_BRANCH_WIDTH_NONE)
? ZydisGetMachineModeWidth(match->request->machine_mode)
: (4 << match->request->branch_width);
if (min_disp_size > desired_disp_size)
{
return ZYAN_FALSE;
}
match->eosz = match->disp_size = desired_disp_size;
match->imm_size = 16;
return ZYAN_TRUE;
}
|
/**
* Checks if requested operand matches pointer operand from instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param user_op Operand definition from `ZydisEncoderRequest` structure.
*
* @return True if operands match, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2098-L2116
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsImmediateOperandCompabile
|
ZyanBool ZydisIsImmediateOperandCompabile(ZydisEncoderInstructionMatch *match,
const ZydisEncoderOperand *user_op, const ZydisOperandDefinition *def_op)
{
switch (def_op->type)
{
case ZYDIS_SEMANTIC_OPTYPE_IMPLICIT_IMM1:
if (user_op->imm.u != 1)
{
return ZYAN_FALSE;
}
break;
case ZYDIS_SEMANTIC_OPTYPE_IMM:
case ZYDIS_SEMANTIC_OPTYPE_REL:
{
const ZyanU8 imm_size = ZydisGetEffectiveImmSize(match, user_op->imm.s, def_op);
if (def_op->op.encoding != ZYDIS_OPERAND_ENCODING_IS4)
{
if (imm_size == 0)
{
return ZYAN_FALSE;
}
if (match->imm_size)
{
ZYAN_ASSERT(match->disp_size == 0);
match->disp_size = match->imm_size;
}
}
else
{
ZYAN_ASSERT(match->imm_size == 0);
if (imm_size != 8)
{
return ZYAN_FALSE;
}
}
match->imm_size = imm_size;
match->has_rel_operand = (def_op->type == ZYDIS_SEMANTIC_OPTYPE_REL);
break;
}
default:
ZYAN_UNREACHABLE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested operand matches immediate operand from instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param user_op Operand definition from `ZydisEncoderRequest` structure.
* @param def_op Decoder's operand definition from current instruction definition.
*
* @return True if operands match, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2127-L2171
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsBroadcastModeCompatible
|
ZyanBool ZydisIsBroadcastModeCompatible(const ZydisInstructionDefinitionEVEX *evex_def,
ZydisVectorLength vector_length, ZydisBroadcastMode broadcast)
{
if (broadcast == ZYDIS_BROADCAST_MODE_INVALID)
{
return ZYAN_TRUE;
}
ZyanU8 vector_size = 0;
ZYAN_ASSERT(vector_length != ZYDIS_VECTOR_LENGTH_INVALID);
switch (vector_length)
{
case ZYDIS_VECTOR_LENGTH_128:
vector_size = 16;
break;
case ZYDIS_VECTOR_LENGTH_256:
vector_size = 32;
break;
case ZYDIS_VECTOR_LENGTH_512:
vector_size = 64;
break;
default:
ZYAN_UNREACHABLE;
}
switch (evex_def->tuple_type)
{
case ZYDIS_TUPLETYPE_FV:
break;
case ZYDIS_TUPLETYPE_HV:
vector_size /= 2;
break;
case ZYDIS_TUPLETYPE_QUARTER:
vector_size /= 4;
break;
default:
ZYAN_UNREACHABLE;
}
ZyanU8 element_size;
switch (evex_def->element_size)
{
case ZYDIS_IELEMENT_SIZE_16:
element_size = 2;
break;
case ZYDIS_IELEMENT_SIZE_32:
element_size = 4;
break;
case ZYDIS_IELEMENT_SIZE_64:
element_size = 8;
break;
default:
ZYAN_UNREACHABLE;
}
ZydisBroadcastMode allowed_mode;
const ZyanU8 element_count = vector_size / element_size;
switch (element_count)
{
case 2:
allowed_mode = ZYDIS_BROADCAST_MODE_1_TO_2;
break;
case 4:
allowed_mode = ZYDIS_BROADCAST_MODE_1_TO_4;
break;
case 8:
allowed_mode = ZYDIS_BROADCAST_MODE_1_TO_8;
break;
case 16:
allowed_mode = ZYDIS_BROADCAST_MODE_1_TO_16;
break;
case 32:
allowed_mode = ZYDIS_BROADCAST_MODE_1_TO_32;
break;
default:
ZYAN_UNREACHABLE;
}
if (broadcast != allowed_mode)
{
return ZYAN_FALSE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested boardcast mode is compatible with instruction definition.
*
* @param evex_def Definition for `EVEX`-encoded instruction.
* @param vector_length Vector length.
* @param broadcast Requested broadcast mode.
*
* @return True if broadcast mode is compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2182-L2265
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisAreEvexFeaturesCompatible
|
ZyanBool ZydisAreEvexFeaturesCompatible(const ZydisEncoderInstructionMatch *match,
const ZydisEncoderRequest *request)
{
if (match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_EVEX)
{
return ZYAN_TRUE;
}
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
if ((!evex_def->accepts_zero_mask) &&
(evex_def->mask_override != ZYDIS_MASK_OVERRIDE_ZEROING) &&
(request->evex.zeroing_mask))
{
return ZYAN_FALSE;
}
switch (evex_def->functionality)
{
case ZYDIS_EVEX_FUNC_INVALID:
if ((request->evex.sae) ||
(request->evex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->evex.rounding != ZYDIS_ROUNDING_MODE_INVALID))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_EVEX_FUNC_BC:
if ((request->evex.sae) ||
(request->evex.rounding != ZYDIS_ROUNDING_MODE_INVALID))
{
return ZYAN_FALSE;
}
if (!ZydisIsBroadcastModeCompatible(evex_def, match->definition->vector_length,
request->evex.broadcast))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_EVEX_FUNC_RC:
if (request->evex.broadcast != ZYDIS_BROADCAST_MODE_INVALID)
{
return ZYAN_FALSE;
}
if (request->evex.rounding == ZYDIS_ROUNDING_MODE_INVALID)
{
if (request->evex.sae)
{
return ZYAN_FALSE;
}
}
else
{
if (!request->evex.sae)
{
return ZYAN_FALSE;
}
}
break;
case ZYDIS_EVEX_FUNC_SAE:
if ((request->evex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->evex.rounding != ZYDIS_ROUNDING_MODE_INVALID))
{
return ZYAN_FALSE;
}
break;
default:
ZYAN_UNREACHABLE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested `EVEX`-specific features are compatible with instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param request A pointer to `ZydisEncoderRequest` struct.
*
* @return True if features are compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2275-L2346
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisAreMvexFeaturesCompatible
|
ZyanBool ZydisAreMvexFeaturesCompatible(const ZydisEncoderInstructionMatch *match,
const ZydisEncoderRequest *request)
{
if (match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
return ZYAN_TRUE;
}
if (((match->definition->modrm >> 6) == 3) &&
(request->mvex.eviction_hint))
{
return ZYAN_FALSE;
}
const ZydisInstructionDefinitionMVEX *mvex_def =
(const ZydisInstructionDefinitionMVEX *)match->base_definition;
switch (mvex_def->functionality)
{
case ZYDIS_MVEX_FUNC_IGNORED:
case ZYDIS_MVEX_FUNC_INVALID:
case ZYDIS_MVEX_FUNC_F_32:
case ZYDIS_MVEX_FUNC_I_32:
case ZYDIS_MVEX_FUNC_F_64:
case ZYDIS_MVEX_FUNC_I_64:
case ZYDIS_MVEX_FUNC_UF_64:
case ZYDIS_MVEX_FUNC_UI_64:
case ZYDIS_MVEX_FUNC_DF_64:
case ZYDIS_MVEX_FUNC_DI_64:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_RC:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.eviction_hint))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SAE:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.eviction_hint))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SWIZZLE_32:
case ZYDIS_MVEX_FUNC_SWIZZLE_64:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SF_32:
if ((request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_1_TO_16) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_4_TO_16))
{
return ZYAN_FALSE;
}
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_FLOAT16) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT8) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT16) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_SINT16))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SI_32:
if ((request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_1_TO_16) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_4_TO_16))
{
return ZYAN_FALSE;
}
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT8) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_SINT8) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT16) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_SINT16))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SF_32_BCST:
case ZYDIS_MVEX_FUNC_SI_32_BCST:
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_1_TO_16) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_4_TO_16))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SF_32_BCST_4TO16:
case ZYDIS_MVEX_FUNC_SI_32_BCST_4TO16:
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_4_TO_16))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_SF_64:
case ZYDIS_MVEX_FUNC_SI_64:
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_1_TO_8) &&
(request->mvex.broadcast != ZYDIS_BROADCAST_MODE_4_TO_8))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_UF_32:
case ZYDIS_MVEX_FUNC_DF_32:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_MVEX_FUNC_UI_32:
case ZYDIS_MVEX_FUNC_DI_32:
if ((request->mvex.broadcast != ZYDIS_BROADCAST_MODE_INVALID) ||
(request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID) ||
(request->mvex.swizzle != ZYDIS_SWIZZLE_MODE_INVALID) ||
(request->mvex.sae))
{
return ZYAN_FALSE;
}
if ((request->mvex.conversion != ZYDIS_CONVERSION_MODE_INVALID) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT8) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_SINT8) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_UINT16) &&
(request->mvex.conversion != ZYDIS_CONVERSION_MODE_SINT16))
{
return ZYAN_FALSE;
}
break;
default:
ZYAN_UNREACHABLE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested `MVEX`-specific features are compatible with instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param request A pointer to `ZydisEncoderRequest` struct.
*
* @return True if features are compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2356-L2555
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisCheckConstraints
|
ZyanBool ZydisCheckConstraints(const ZydisEncoderInstructionMatch *match)
{
const ZydisEncoderOperand *operands = match->request->operands;
ZyanBool is_gather = ZYAN_FALSE;
switch (match->definition->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_VEX:
{
const ZydisInstructionDefinitionVEX *vex_def =
(const ZydisInstructionDefinitionVEX *)match->base_definition;
if (vex_def->is_gather)
{
ZYAN_ASSERT(match->request->operand_count == 3);
ZYAN_ASSERT(operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT(operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY);
ZYAN_ASSERT(operands[2].type == ZYDIS_OPERAND_TYPE_REGISTER);
const ZyanI8 dest = ZydisRegisterGetId(operands[0].reg.value);
const ZyanI8 index = ZydisRegisterGetId(operands[1].mem.index);
const ZyanI8 mask = ZydisRegisterGetId(operands[2].reg.value);
// If any pair of the index, mask, or destination registers are the same, the
// instruction results a UD fault.
if ((dest == index) || (dest == mask) || (index == mask))
{
return ZYAN_FALSE;
}
}
if (vex_def->no_source_source_match)
{
ZYAN_ASSERT(match->request->operand_count == 3);
ZYAN_ASSERT(operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT(operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT(operands[2].type == ZYDIS_OPERAND_TYPE_REGISTER);
const ZydisRegister dest = operands[0].reg.value;
const ZydisRegister source1 = operands[1].reg.value;
const ZydisRegister source2 = operands[2].reg.value;
// AMX-E4: #UD if srcdest == src1 OR src1 == src2 OR srcdest == src2.
if ((dest == source1) || (source1 == source2) || (dest == source2))
{
return ZYAN_FALSE;
}
}
return ZYAN_TRUE;
}
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
{
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
is_gather = evex_def->is_gather;
if (evex_def->no_source_dest_match)
{
ZYAN_ASSERT(operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT(operands[2].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT((operands[3].type == ZYDIS_OPERAND_TYPE_REGISTER) ||
(operands[3].type == ZYDIS_OPERAND_TYPE_MEMORY));
const ZydisRegister dest = operands[0].reg.value;
const ZydisRegister source1 = operands[2].reg.value;
const ZydisRegister source2 = (operands[3].type == ZYDIS_OPERAND_TYPE_REGISTER)
? operands[3].reg.value
: ZYDIS_REGISTER_NONE;
if ((dest == source1) || (dest == source2))
{
return ZYAN_FALSE;
}
}
break;
}
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
{
const ZydisInstructionDefinitionMVEX *mvex_def =
(const ZydisInstructionDefinitionMVEX *)match->base_definition;
is_gather = mvex_def->is_gather;
break;
}
default:
return ZYAN_TRUE;
}
if ((is_gather) && (operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER))
{
ZYAN_ASSERT(match->request->operand_count == 3);
ZYAN_ASSERT(operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER);
ZYAN_ASSERT(operands[2].type == ZYDIS_OPERAND_TYPE_MEMORY);
const ZyanI8 dest = ZydisRegisterGetId(operands[0].reg.value);
const ZyanI8 index = ZydisRegisterGetId(operands[2].mem.index);
// EVEX: The instruction will #UD fault if the destination vector zmm1 is the same as
// index vector VINDEX.
// MVEX: The KNC GATHER instructions forbid using the same vector register for destination
// and for the index. (https://github.com/intelxed/xed/issues/281#issuecomment-970074554)
if (dest == index)
{
return ZYAN_FALSE;
}
}
return ZYAN_TRUE;
}
|
/**
* Checks if operands specified in encoder request satisfy additional constraints mandated by
* matched instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return True if operands passed the checks, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2565-L2663
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisIsDefinitionCompatible
|
ZyanBool ZydisIsDefinitionCompatible(ZydisEncoderInstructionMatch *match,
const ZydisEncoderRequest *request)
{
ZYAN_ASSERT(request->operand_count == match->base_definition->operand_count_visible);
match->operands = ZydisGetOperandDefinitions(match->base_definition);
if (!ZydisAreEvexFeaturesCompatible(match, request))
{
return ZYAN_FALSE;
}
if (!ZydisAreMvexFeaturesCompatible(match, request))
{
return ZYAN_FALSE;
}
for (ZyanU8 i = 0; i < request->operand_count; ++i)
{
const ZydisEncoderOperand *user_op = &request->operands[i];
const ZydisOperandDefinition *def_op = &match->operands[i];
ZYAN_ASSERT(def_op->visibility != ZYDIS_OPERAND_VISIBILITY_HIDDEN);
ZyanBool is_compatible = ZYAN_FALSE;
switch (user_op->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
is_compatible = ZydisIsRegisterOperandCompatible(match, user_op, def_op);
break;
case ZYDIS_OPERAND_TYPE_MEMORY:
is_compatible = ZydisIsMemoryOperandCompatible(match, user_op, def_op);
break;
case ZYDIS_OPERAND_TYPE_POINTER:
is_compatible = ZydisIsPointerOperandCompatible(match, user_op);
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
is_compatible = ZydisIsImmediateOperandCompabile(match, user_op, def_op);
break;
default:
ZYAN_UNREACHABLE;
}
if (!is_compatible)
{
return ZYAN_FALSE;
}
}
ZyanU8 eosz = 0;
if (match->base_definition->branch_type != ZYDIS_BRANCH_TYPE_NONE)
{
switch (request->branch_width)
{
case ZYDIS_BRANCH_WIDTH_NONE:
break;
case ZYDIS_BRANCH_WIDTH_8:
if ((!match->has_rel_operand) ||
(match->base_definition->branch_type != ZYDIS_BRANCH_TYPE_SHORT))
{
return ZYAN_FALSE;
}
break;
case ZYDIS_BRANCH_WIDTH_16:
eosz = 16;
break;
case ZYDIS_BRANCH_WIDTH_32:
eosz = ((match->has_rel_operand) &&
(match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(match->base_definition->operand_size_map == ZYDIS_OPSIZE_MAP_FORCE64))
? 64
: 32;
break;
case ZYDIS_BRANCH_WIDTH_64:
if (match->has_rel_operand)
{
return ZYAN_FALSE;
}
eosz = 64;
break;
default:
ZYAN_UNREACHABLE;
}
}
if (eosz)
{
if (match->eosz != 0)
{
if (match->eosz != eosz)
{
return ZYAN_FALSE;
}
}
else
{
match->eosz = eosz;
}
}
if (!ZydisCheckConstraints(match))
{
return ZYAN_FALSE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if operands and encoding-specific features from `ZydisEncoderRequest` match
* encoder's instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param request A pointer to `ZydisEncoderRequest` struct.
*
* @return True if definition is compatible, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2674-L2775
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisArePrefixesCompatible
|
ZyanBool ZydisArePrefixesCompatible(const ZydisEncoderInstructionMatch *match)
{
if ((!match->base_definition->accepts_segment) &&
(match->attributes & ZYDIS_ATTRIB_HAS_SEGMENT))
{
return ZYAN_FALSE;
}
if (match->definition->encoding != ZYDIS_INSTRUCTION_ENCODING_LEGACY)
{
return !(match->attributes & ZYDIS_ENCODABLE_PREFIXES_NO_SEGMENTS);
}
const ZydisInstructionDefinitionLEGACY *legacy_def =
(const ZydisInstructionDefinitionLEGACY *)match->base_definition;
if ((!legacy_def->accepts_LOCK) &&
(match->attributes & ZYDIS_ATTRIB_HAS_LOCK))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_REP) &&
(match->attributes & ZYDIS_ATTRIB_HAS_REP))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_REPEREPZ) &&
(match->attributes & ZYDIS_ATTRIB_HAS_REPE))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_REPNEREPNZ) &&
(match->attributes & ZYDIS_ATTRIB_HAS_REPNE))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_BOUND) &&
(match->attributes & ZYDIS_ATTRIB_HAS_BND))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_XACQUIRE) &&
(match->attributes & ZYDIS_ATTRIB_HAS_XACQUIRE))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_XRELEASE) &&
(match->attributes & ZYDIS_ATTRIB_HAS_XRELEASE))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_branch_hints) &&
(match->attributes & (ZYDIS_ATTRIB_HAS_BRANCH_NOT_TAKEN |
ZYDIS_ATTRIB_HAS_BRANCH_TAKEN)))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_NOTRACK) &&
(match->attributes & ZYDIS_ATTRIB_HAS_NOTRACK))
{
return ZYAN_FALSE;
}
if ((!legacy_def->accepts_hle_without_lock) &&
(match->attributes & (ZYDIS_ATTRIB_HAS_XACQUIRE |
ZYDIS_ATTRIB_HAS_XRELEASE)) &&
!(match->attributes & ZYDIS_ATTRIB_HAS_LOCK))
{
return ZYAN_FALSE;
}
return ZYAN_TRUE;
}
|
/**
* Checks if requested set of prefixes is compatible with instruction definition.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2784-L2853
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetOperandMask
|
ZyanU16 ZydisGetOperandMask(const ZydisEncoderRequest *request)
{
ZyanU16 operand_mask = request->operand_count;
ZyanU8 bit_offset = ZYAN_BITS_TO_REPRESENT(ZYDIS_ENCODER_MAX_OPERANDS);
for (ZyanU8 i = 0; i < request->operand_count; ++i)
{
operand_mask |= (request->operands[i].type - ZYDIS_OPERAND_TYPE_REGISTER) << bit_offset;
bit_offset += ZYAN_BITS_TO_REPRESENT(
ZYDIS_OPERAND_TYPE_MAX_VALUE - ZYDIS_OPERAND_TYPE_REGISTER);
}
return operand_mask;
}
|
/**
* Returns operand mask containing information about operand count and types in a compressed form.
*
* @param request A pointer to `ZydisEncoderRequest` struct.
*
* @return Operand mask.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2862-L2874
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisHandleSwappableDefinition
|
ZyanBool ZydisHandleSwappableDefinition(ZydisEncoderInstructionMatch *match)
{
if (!match->definition->swappable)
{
return ZYAN_FALSE;
}
// Special case for ISA-wide unique conflict between two `mov` variants
// mov gpr16_32_64(encoding=opcode), imm(encoding=uimm16_32_64,scale_factor=osz)
// mov gpr16_32_64(encoding=modrm_rm), imm(encoding=simm16_32_32,scale_factor=osz)
if (match->request->mnemonic == ZYDIS_MNEMONIC_MOV)
{
const ZyanU8 imm_size = ZydisGetSignedImmSize(match->request->operands[1].imm.s);
if ((match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(match->eosz == 64) &&
(imm_size < 64))
{
return ZYAN_TRUE;
}
}
ZYAN_ASSERT((match->request->operand_count == 2) || (match->request->operand_count == 3));
const ZyanU8 src_index = (match->request->operand_count == 3) ? 2 : 1;
const ZyanI8 dest_id = ZydisRegisterGetId(match->request->operands[0].reg.value);
const ZyanI8 src_id = ZydisRegisterGetId(match->request->operands[src_index].reg.value);
if ((dest_id <= 7) && (src_id > 7))
{
++match->definition;
ZydisGetInstructionDefinition(match->definition->encoding,
match->definition->instruction_reference, &match->base_definition);
match->operands = ZydisGetOperandDefinitions(match->base_definition);
return ZYAN_TRUE;
}
return ZYAN_FALSE;
}
|
/**
* Handles optimization opportunities indicated by `swappable` field in instruction definition
* structure. See `ZydisEncodableInstruction` for more information.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return True if definition has been swapped, false otherwise.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2884-L2919
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFindMatchingDefinition
|
ZyanStatus ZydisFindMatchingDefinition(const ZydisEncoderRequest *request,
ZydisEncoderInstructionMatch *match)
{
ZYAN_MEMSET(match, 0, sizeof(ZydisEncoderInstructionMatch));
match->request = request;
match->attributes = request->prefixes;
const ZydisEncodableInstruction *definition = ZYAN_NULL;
const ZyanU8 definition_count = ZydisGetEncodableInstructions(request->mnemonic, &definition);
ZYAN_ASSERT(definition && definition_count);
const ZydisWidthFlag mode_width = ZydisGetMachineModeWidth(request->machine_mode) >> 4;
const ZyanBool is_compat = (request->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_16) ||
(request->machine_mode == ZYDIS_MACHINE_MODE_LONG_COMPAT_32);
const ZyanU8 default_asz = ZydisGetAszFromHint(request->address_size_hint);
const ZyanU8 default_osz = ZydisGetOszFromHint(request->operand_size_hint);
const ZyanU16 operand_mask = ZydisGetOperandMask(request);
for (ZyanU8 i = 0; i < definition_count; ++i, ++definition)
{
if (definition->operand_mask != operand_mask)
{
continue;
}
const ZydisInstructionDefinition *base_definition = ZYAN_NULL;
ZydisGetInstructionDefinition(definition->encoding, definition->instruction_reference,
&base_definition);
if (!(definition->modes & mode_width))
{
continue;
}
if ((request->allowed_encodings != ZYDIS_ENCODABLE_ENCODING_DEFAULT) &&
!(ZydisGetEncodableEncoding(definition->encoding) & request->allowed_encodings))
{
continue;
}
if (request->machine_mode == ZYDIS_MACHINE_MODE_REAL_16)
{
if (base_definition->requires_protected_mode)
{
continue;
}
switch (definition->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
continue;
default:
break;
}
}
else if ((request->machine_mode != ZYDIS_MACHINE_MODE_LONG_64) &&
(definition->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
continue;
}
if (is_compat && base_definition->no_compat_mode)
{
continue;
}
if ((request->branch_type != ZYDIS_BRANCH_TYPE_NONE) &&
(request->branch_type != base_definition->branch_type))
{
continue;
}
if ((base_definition->branch_type == ZYDIS_BRANCH_TYPE_NONE) &&
(request->branch_width != ZYDIS_BRANCH_WIDTH_NONE))
{
continue;
}
match->definition = definition;
match->base_definition = base_definition;
match->operands = ZYAN_NULL;
match->easz = definition->accepts_hint == ZYDIS_SIZE_HINT_ASZ ? default_asz : 0;
match->eosz = definition->accepts_hint == ZYDIS_SIZE_HINT_OSZ ? default_osz : 0;
match->disp_size = match->imm_size = match->cd8_scale = 0;
match->rex_type = ZYDIS_REX_TYPE_UNKNOWN;
match->eosz64_forbidden = ZYAN_FALSE;
match->has_rel_operand = ZYAN_FALSE;
if ((base_definition->operand_size_map != ZYDIS_OPSIZE_MAP_BYTEOP) &&
(match->eosz == 8))
{
continue;
}
if (!ZydisArePrefixesCompatible(match))
{
continue;
}
if (!ZydisIsDefinitionCompatible(match, request))
{
continue;
}
if (ZydisHandleSwappableDefinition(match))
{
if (definition == match->definition)
{
continue;
}
++i;
definition = match->definition;
base_definition = match->base_definition;
}
if (match->easz == 0)
{
if (definition->address_sizes & mode_width)
{
match->easz = (ZyanU8)(mode_width << 4);
}
else if (mode_width == ZYDIS_WIDTH_16)
{
match->easz = 32;
}
else if (mode_width == ZYDIS_WIDTH_32)
{
match->easz = 16;
}
else
{
match->easz = 32;
}
ZYAN_ASSERT(definition->address_sizes & (match->easz >> 4));
}
else if (!(definition->address_sizes & (match->easz >> 4)))
{
continue;
}
if (mode_width == ZYDIS_WIDTH_64)
{
if (base_definition->operand_size_map == ZYDIS_OPSIZE_MAP_DEFAULT64)
{
if (match->eosz == 0)
{
ZYAN_ASSERT(definition->operand_sizes & (ZYDIS_WIDTH_16 | ZYDIS_WIDTH_64));
if (definition->operand_sizes & ZYDIS_WIDTH_64)
{
match->eosz = 64;
}
else
{
match->eosz = 16;
}
}
else if (match->eosz == 32)
{
continue;
}
}
else if (base_definition->operand_size_map == ZYDIS_OPSIZE_MAP_FORCE64)
{
if (match->eosz == 0)
{
match->eosz = 64;
}
else if (match->eosz != 64)
{
continue;
}
}
}
if (match->eosz == 0)
{
const ZydisWidthFlag default_width = (mode_width == ZYDIS_WIDTH_64)
? ZYDIS_WIDTH_32
: mode_width;
if (definition->operand_sizes & default_width)
{
match->eosz = (ZyanU8)(default_width << 4);
}
else if (definition->operand_sizes & ZYDIS_WIDTH_16)
{
match->eosz = 16;
}
else if (definition->operand_sizes & ZYDIS_WIDTH_32)
{
match->eosz = 32;
}
else
{
match->eosz = 64;
}
}
else if (match->eosz64_forbidden && match->eosz == 64)
{
continue;
}
else if (!(definition->operand_sizes & (match->eosz >> 4)))
{
continue;
}
return ZYAN_STATUS_SUCCESS;
}
return ZYDIS_STATUS_IMPOSSIBLE_INSTRUCTION;
}
|
/**
* This function attempts to find a matching instruction definition for provided encoder request.
*
* @param request A pointer to `ZydisEncoderRequest` struct.
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L2929-L3127
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitUInt
|
ZyanStatus ZydisEmitUInt(ZyanU64 data, ZyanU8 size, ZydisEncoderBuffer *buffer)
{
ZYAN_ASSERT(size == 1 || size == 2 || size == 4 || size == 8);
const ZyanUSize new_offset = buffer->offset + size;
if (new_offset > buffer->size)
{
return ZYAN_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
// TODO: fix for big-endian systems
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &data, size);
buffer->offset = new_offset;
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits unsigned integer value.
*
* @param data Value to emit.
* @param size Value size in bytes.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3138-L3153
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitByte
|
ZyanStatus ZydisEmitByte(ZyanU8 byte, ZydisEncoderBuffer *buffer)
{
return ZydisEmitUInt(byte, 1, buffer);
}
|
/**
* Emits a single byte.
*
* @param byte Value to emit.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3163-L3166
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitLegacyPrefixes
|
ZyanStatus ZydisEmitLegacyPrefixes(const ZydisEncoderInstruction *instruction,
ZydisEncoderBuffer *buffer)
{
ZyanBool compressed_prefixes = ZYAN_FALSE;
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
case ZYDIS_INSTRUCTION_ENCODING_VEX:
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
compressed_prefixes = ZYAN_TRUE;
break;
default:
break;
}
// Group 1
if (instruction->attributes & ZYDIS_ATTRIB_HAS_LOCK)
{
ZYAN_CHECK(ZydisEmitByte(0xF0, buffer));
}
if (!compressed_prefixes)
{
if (instruction->attributes & (ZYDIS_ATTRIB_HAS_REPNE |
ZYDIS_ATTRIB_HAS_BND |
ZYDIS_ATTRIB_HAS_XACQUIRE))
{
ZYAN_CHECK(ZydisEmitByte(0xF2, buffer));
}
if (instruction->attributes & (ZYDIS_ATTRIB_HAS_REP |
ZYDIS_ATTRIB_HAS_REPE |
ZYDIS_ATTRIB_HAS_XRELEASE))
{
ZYAN_CHECK(ZydisEmitByte(0xF3, buffer));
}
}
// Group 2
if (instruction->attributes & (ZYDIS_ATTRIB_HAS_SEGMENT_CS |
ZYDIS_ATTRIB_HAS_BRANCH_NOT_TAKEN))
{
ZYAN_CHECK(ZydisEmitByte(0x2E, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS)
{
ZYAN_CHECK(ZydisEmitByte(0x36, buffer));
}
if (instruction->attributes & (ZYDIS_ATTRIB_HAS_SEGMENT_DS |
ZYDIS_ATTRIB_HAS_BRANCH_TAKEN))
{
ZYAN_CHECK(ZydisEmitByte(0x3E, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_ES)
{
ZYAN_CHECK(ZydisEmitByte(0x26, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_FS)
{
ZYAN_CHECK(ZydisEmitByte(0x64, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_GS)
{
ZYAN_CHECK(ZydisEmitByte(0x65, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_NOTRACK)
{
ZYAN_CHECK(ZydisEmitByte(0x3E, buffer));
}
// Group 3
if (!compressed_prefixes)
{
if (instruction->attributes & ZYDIS_ATTRIB_HAS_OPERANDSIZE)
{
ZYAN_CHECK(ZydisEmitByte(0x66, buffer));
}
}
// Group 4
if (instruction->attributes & ZYDIS_ATTRIB_HAS_ADDRESSSIZE)
{
ZYAN_CHECK(ZydisEmitByte(0x67, buffer));
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits legact prefixes.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3176-L3261
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncodeRexLowNibble
|
ZyanU8 ZydisEncodeRexLowNibble(const ZydisEncoderInstruction *instruction, ZyanBool *high_r)
{
if (high_r)
{
*high_r = ZYAN_FALSE;
}
ZyanU8 rex = 0;
if ((instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM) &&
(instruction->attributes & ZYDIS_ATTRIB_HAS_SIB))
{
if (instruction->base & 0x08)
{
rex |= 1;
}
if (instruction->index & 0x08)
{
rex |= 2;
}
if (instruction->reg & 0x08)
{
rex |= 4;
}
if (high_r && (instruction->reg & 0x10))
{
*high_r = ZYAN_TRUE;
}
}
else if (instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM)
{
if (instruction->rm & 0x08)
{
rex |= 1;
}
if (instruction->rm & 0x10)
{
rex |= 2;
}
if (instruction->reg & 0x08)
{
rex |= 4;
}
if (high_r && (instruction->reg & 0x10))
{
*high_r = ZYAN_TRUE;
}
}
else
{
if (instruction->rm & 0x08)
{
rex |= 1;
}
}
if (instruction->rex_w)
{
rex |= 8;
}
return rex;
}
|
/**
* Encodes low nibble of `REX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param high_r A pointer to `ZyanBool` variable that will be set to true when the
* highest `ModR/M.reg` bit cannot be encoded using `REX` prefix.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3272-L3333
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitRex
|
ZyanStatus ZydisEmitRex(const ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
const ZyanU8 rex = ZydisEncodeRexLowNibble(instruction, ZYAN_NULL);
if (rex || (instruction->attributes & ZYDIS_ATTRIB_HAS_REX))
{
ZYAN_CHECK(ZydisEmitByte(0x40 | rex, buffer));
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits `REX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3343-L3352
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncodeVexCommons
|
void ZydisEncodeVexCommons(ZydisEncoderInstruction *instruction, ZyanU8 *mmmmm, ZyanU8 *pp,
ZyanU8 *vvvv, ZyanU8 *rex, ZyanBool *high_r)
{
switch (instruction->opcode_map)
{
case ZYDIS_OPCODE_MAP_DEFAULT:
case ZYDIS_OPCODE_MAP_0F:
case ZYDIS_OPCODE_MAP_0F38:
case ZYDIS_OPCODE_MAP_0F3A:
case ZYDIS_OPCODE_MAP_MAP5:
case ZYDIS_OPCODE_MAP_MAP6:
*mmmmm = (ZyanU8)instruction->opcode_map;
break;
case ZYDIS_OPCODE_MAP_XOP8:
case ZYDIS_OPCODE_MAP_XOP9:
case ZYDIS_OPCODE_MAP_XOPA:
*mmmmm = 8 + ((ZyanU8)instruction->opcode_map - ZYDIS_OPCODE_MAP_XOP8);
break;
default:
ZYAN_UNREACHABLE;
}
instruction->opcode_map = ZYDIS_OPCODE_MAP_DEFAULT;
*pp = 0;
if (instruction->attributes & ZYDIS_ATTRIB_HAS_OPERANDSIZE)
{
*pp = 1;
}
else if (instruction->attributes & ZYDIS_ATTRIB_HAS_REP)
{
*pp = 2;
}
else if (instruction->attributes & ZYDIS_ATTRIB_HAS_REPNE)
{
*pp = 3;
}
*vvvv = ~instruction->vvvv;
*rex = ZydisEncodeRexLowNibble(instruction, high_r);
}
|
/**
* Encodes common parts of `VEX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param mmmmm A pointer to `ZyanU8` variable that will receive `VEX.mmmmm`
* @param pp A pointer to `ZyanU8` variable that will receive `VEX.pp`
* @param vvvv A pointer to `ZyanU8` variable that will receive `VEX.vvvv`
* @param rex A pointer to `ZyanU8` variable that will receive 'REX`
* @param high_r A pointer to `ZyanBool` variable that will be set to true when the
* highest `ModR/M.reg` bit cannot be encoded using `REX` prefix.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3365-L3404
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitXop
|
ZyanStatus ZydisEmitXop(ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
ZyanU8 mmmmm, pp, vvvv, rex;
ZydisEncodeVexCommons(instruction, &mmmmm, &pp, &vvvv, &rex, ZYAN_NULL);
ZYAN_ASSERT(instruction->vector_length <= 1);
const ZyanU8 b1 = (((~rex) & 0x07) << 5) | mmmmm;
const ZyanU8 b2 = ((rex & 0x08) << 4) | ((vvvv & 0xF) << 3) | (instruction->vector_length << 2) | pp;
ZYAN_CHECK(ZydisEmitByte(0x8F, buffer));
ZYAN_CHECK(ZydisEmitByte(b1, buffer));
ZYAN_CHECK(ZydisEmitByte(b2, buffer));
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits `XOP` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3414-L3425
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitVex
|
ZyanStatus ZydisEmitVex(ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
ZyanU8 mmmmm, pp, vvvv, rex;
ZydisEncodeVexCommons(instruction, &mmmmm, &pp, &vvvv, &rex, ZYAN_NULL);
ZYAN_ASSERT(instruction->vector_length <= 1);
if (mmmmm != 1 || (rex & 0x0B))
{
const ZyanU8 b1 = (((~rex) & 0x07) << 5) | mmmmm;
const ZyanU8 b2 = ((rex & 0x08) << 4) |
((vvvv & 0xF) << 3) |
(instruction->vector_length << 2) |
pp;
ZYAN_CHECK(ZydisEmitByte(0xC4, buffer));
ZYAN_CHECK(ZydisEmitByte(b1, buffer));
ZYAN_CHECK(ZydisEmitByte(b2, buffer));
}
else
{
const ZyanU8 b1 = (((~rex) & 0x04) << 5) |
((vvvv & 0xF) << 3) |
(instruction->vector_length << 2) |
pp;
ZYAN_CHECK(ZydisEmitByte(0xC5, buffer));
ZYAN_CHECK(ZydisEmitByte(b1, buffer));
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits `VEX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3435-L3462
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncodeEvexCommons
|
void ZydisEncodeEvexCommons(ZydisEncoderInstruction *instruction, ZyanU8 *p0, ZyanU8 *p1,
ZyanU8 *vvvvv)
{
ZyanBool high_r;
ZyanU8 mmmmm, pp, rex;
ZydisEncodeVexCommons(instruction, &mmmmm, &pp, vvvvv, &rex, &high_r);
*p0 = (((~rex) & 0x07) << 5) | mmmmm;
if (!high_r)
{
*p0 |= 0x10;
}
*p1 = ((rex & 0x08) << 4) | ((*vvvvv & 0x0F) << 3) | 0x04 | pp;
}
|
/**
* Encodes common parts of `EVEX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param p0 A pointer to `ZyanU8` variable that will receive 2nd byte of `EVEX` prefix.
* @param p1 A pointer to `ZyanU8` variable that will receive 3rd byte of `EVEX` prefix.
* @param vvvvv A pointer to `ZyanU8` variable that will receive `EVEX.vvvvv`.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3472-L3484
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitEvex
|
ZyanStatus ZydisEmitEvex(ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
ZyanU8 p0, p1, vvvvv;
ZydisEncodeEvexCommons(instruction, &p0, &p1, &vvvvv);
ZyanU8 p2 = (instruction->vector_length << 5) | ((vvvvv & 0x10) >> 1) | instruction->mask;
if (instruction->zeroing)
{
p2 |= 0x80;
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_EVEX_B)
{
p2 |= 0x10;
}
if (instruction->index & 0x10)
{
p2 &= 0xF7;
}
ZYAN_CHECK(ZydisEmitByte(0x62, buffer));
ZYAN_CHECK(ZydisEmitByte(p0, buffer));
ZYAN_CHECK(ZydisEmitByte(p1, buffer));
ZYAN_CHECK(ZydisEmitByte(p2, buffer));
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits `EVEX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3494-L3517
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitMvex
|
ZyanStatus ZydisEmitMvex(ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
ZyanU8 p0, p1, vvvvv;
ZydisEncodeEvexCommons(instruction, &p0, &p1, &vvvvv);
ZyanU8 p2 = (instruction->sss << 4) | ((vvvvv & 0x10) >> 1) | instruction->mask;
if (instruction->eviction_hint)
{
p2 |= 0x80;
}
if (instruction->index & 0x10)
{
p2 &= 0xF7;
}
ZYAN_CHECK(ZydisEmitByte(0x62, buffer));
ZYAN_CHECK(ZydisEmitByte(p0, buffer));
ZYAN_CHECK(ZydisEmitByte(p1 & 0xFB, buffer));
ZYAN_CHECK(ZydisEmitByte(p2, buffer));
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits `MVEX` prefix.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3527-L3546
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEmitInstruction
|
ZyanStatus ZydisEmitInstruction(ZydisEncoderInstruction *instruction, ZydisEncoderBuffer *buffer)
{
ZYAN_CHECK(ZydisEmitLegacyPrefixes(instruction, buffer));
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
ZYAN_CHECK(ZydisEmitRex(instruction, buffer));
break;
case ZYDIS_INSTRUCTION_ENCODING_XOP:
ZYAN_CHECK(ZydisEmitXop(instruction, buffer));
break;
case ZYDIS_INSTRUCTION_ENCODING_VEX:
ZYAN_CHECK(ZydisEmitVex(instruction, buffer));
break;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
ZYAN_CHECK(ZydisEmitEvex(instruction, buffer));
break;
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
ZYAN_CHECK(ZydisEmitMvex(instruction, buffer));
break;
default:
ZYAN_UNREACHABLE;
}
switch (instruction->opcode_map)
{
case ZYDIS_OPCODE_MAP_DEFAULT:
break;
case ZYDIS_OPCODE_MAP_0F:
ZYAN_CHECK(ZydisEmitByte(0x0F, buffer));
break;
case ZYDIS_OPCODE_MAP_0F38:
ZYAN_CHECK(ZydisEmitByte(0x0F, buffer));
ZYAN_CHECK(ZydisEmitByte(0x38, buffer));
break;
case ZYDIS_OPCODE_MAP_0F3A:
ZYAN_CHECK(ZydisEmitByte(0x0F, buffer));
ZYAN_CHECK(ZydisEmitByte(0x3A, buffer));
break;
case ZYDIS_OPCODE_MAP_0F0F:
ZYAN_CHECK(ZydisEmitByte(0x0F, buffer));
ZYAN_CHECK(ZydisEmitByte(0x0F, buffer));
break;
default:
ZYAN_UNREACHABLE;
}
if (instruction->encoding != ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{
ZYAN_CHECK(ZydisEmitByte(instruction->opcode, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM)
{
const ZyanU8 modrm = (instruction->mod << 6) |
((instruction->reg & 7) << 3) |
(instruction->rm & 7);
ZYAN_CHECK(ZydisEmitByte(modrm, buffer));
}
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SIB)
{
const ZyanU8 sib = (instruction->scale << 6) |
((instruction->index & 7) << 3) |
(instruction->base & 7);
ZYAN_CHECK(ZydisEmitByte(sib, buffer));
}
if (instruction->disp_size)
{
ZYAN_CHECK(ZydisEmitUInt(instruction->disp, instruction->disp_size / 8, buffer));
}
if (instruction->imm_size)
{
ZYAN_CHECK(ZydisEmitUInt(instruction->imm, instruction->imm_size / 8, buffer));
}
if (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{
ZYAN_CHECK(ZydisEmitByte(instruction->opcode, buffer));
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Emits instruction as stream of bytes.
*
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
* @param buffer A pointer to `ZydisEncoderBuffer` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3556-L3637
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisBuildRegisterOperand
|
void ZydisBuildRegisterOperand(const ZydisEncoderOperand *user_op,
const ZydisOperandDefinition *def_op, ZydisEncoderInstruction *instruction)
{
if (def_op->type == ZYDIS_SEMANTIC_OPTYPE_IMPLICIT_REG)
{
return;
}
ZyanU8 reg_id = 0;
if (ZydisRegisterGetClass(user_op->reg.value) != ZYDIS_REGCLASS_GPR8)
{
reg_id = (ZyanU8)ZydisRegisterGetId(user_op->reg.value);
}
else
{
static const ZyanU8 reg8_lookup[] = {
0, 1, 2, 3, // AL, CL, DL, BL
4, 5, 6, 7, // AH, CH, DH, BH
4, 5, 6, 7, // SPL, BPL, SIL, DIL
8, 9, 10, 11, 12, 13, 14, 15, // R8B-R15B
};
ZYAN_ASSERT(
((ZyanUSize)user_op->reg.value - ZYDIS_REGISTER_AL) < ZYAN_ARRAY_LENGTH(reg8_lookup));
reg_id = reg8_lookup[user_op->reg.value - ZYDIS_REGISTER_AL];
if (user_op->reg.value >= ZYDIS_REGISTER_SPL && user_op->reg.value <= ZYDIS_REGISTER_DIL)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_REX;
}
}
switch (def_op->op.encoding)
{
case ZYDIS_OPERAND_ENCODING_MODRM_REG:
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
instruction->reg = reg_id;
break;
case ZYDIS_OPERAND_ENCODING_MODRM_RM:
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
instruction->rm = reg_id;
break;
case ZYDIS_OPERAND_ENCODING_OPCODE:
instruction->opcode += reg_id & 7;
instruction->rm = reg_id;
break;
case ZYDIS_OPERAND_ENCODING_NDSNDD:
instruction->vvvv = reg_id;
break;
case ZYDIS_OPERAND_ENCODING_IS4:
instruction->imm_size = 8;
instruction->imm = reg_id << 4;
break;
case ZYDIS_OPERAND_ENCODING_MASK:
instruction->mask = reg_id;
break;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Encodes register operand as fields inside `ZydisEncoderInstruction` structure.
*
* @param user_op Validated operand definition from `ZydisEncoderRequest` structure.
* @param def_op Decoder's operand definition from instruction definition.
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3646-L3703
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisBuildMemoryOperand
|
void ZydisBuildMemoryOperand(ZydisEncoderInstructionMatch *match,
const ZydisEncoderOperand *user_op, ZydisEncoderInstruction *instruction)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
instruction->disp = (ZyanU64)user_op->mem.displacement;
if (match->easz == 16)
{
const ZyanI8 rm = ZydisGetRm16(user_op->mem.base, user_op->mem.index);
if (rm != -1)
{
instruction->rm = (ZyanU8)rm;
instruction->disp_size = match->disp_size;
switch (instruction->disp_size)
{
case 0:
if (rm == 6)
{
instruction->disp_size = 8;
instruction->mod = 1;
}
break;
case 8:
instruction->mod = 1;
break;
case 16:
instruction->mod = 2;
break;
default:
ZYAN_UNREACHABLE;
}
}
else
{
instruction->rm = 6;
instruction->disp_size = 16;
}
return;
}
if (user_op->mem.index == ZYDIS_REGISTER_NONE)
{
if (user_op->mem.base == ZYDIS_REGISTER_NONE)
{
if (match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
instruction->rm = 4;
instruction->attributes |= ZYDIS_ATTRIB_HAS_SIB;
instruction->base = 5;
instruction->index = 4;
}
else
{
instruction->rm = 5;
}
instruction->disp_size = 32;
return;
}
else if ((user_op->mem.base == ZYDIS_REGISTER_RIP) ||
(user_op->mem.base == ZYDIS_REGISTER_EIP))
{
instruction->rm = 5;
instruction->disp_size = 32;
return;
}
}
const ZyanU8 reg_base_id = (ZyanU8)ZydisRegisterGetId(user_op->mem.base);
const ZyanU8 reg_index_id = (ZyanU8)ZydisRegisterGetId(user_op->mem.index);
instruction->disp_size = match->disp_size;
switch (instruction->disp_size)
{
case 0:
if (reg_base_id == 5 || reg_base_id == 13)
{
instruction->disp_size = 8;
instruction->disp = 0;
instruction->mod = 1;
}
break;
case 8:
instruction->mod = 1;
break;
case 16:
instruction->disp_size = 32;
ZYAN_FALLTHROUGH;
case 32:
instruction->mod = 2;
break;
default:
ZYAN_UNREACHABLE;
}
if ((user_op->mem.index == ZYDIS_REGISTER_NONE) &&
(reg_base_id != 4) &&
(reg_base_id != 12) &&
((match->definition->modrm & 7) != 4))
{
instruction->rm = reg_base_id;
return;
}
instruction->rm = 4;
instruction->attributes |= ZYDIS_ATTRIB_HAS_SIB;
if (reg_base_id != 0xFF)
{
instruction->base = reg_base_id;
}
else
{
instruction->base = 5;
instruction->mod = 0;
instruction->disp_size = 32;
}
if (reg_index_id != 0xFF)
{
instruction->index = reg_index_id;
}
else
{
instruction->index = 4;
}
switch (user_op->mem.scale)
{
case 0:
case 1:
break;
case 2:
instruction->scale = 1;
break;
case 4:
instruction->scale = 2;
break;
case 8:
instruction->scale = 3;
break;
default:
ZYAN_UNREACHABLE;
}
}
|
/**
* Encodes memory operand as fields inside `ZydisEncoderInstruction` structure.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param user_op Decoder's operand definition from instruction definition.
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3712-L3848
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisBuildInstruction
|
ZyanStatus ZydisBuildInstruction(ZydisEncoderInstructionMatch *match,
ZydisEncoderInstruction *instruction)
{
ZYAN_MEMSET(instruction, 0, sizeof(ZydisEncoderInstruction));
instruction->attributes = match->attributes;
instruction->encoding = match->definition->encoding;
instruction->opcode_map = match->definition->opcode_map;
instruction->opcode = match->definition->opcode;
instruction->rex_w = match->definition->rex_w;
instruction->mod = (match->definition->modrm >> 6) & 3;
instruction->reg = (match->definition->modrm >> 3) & 7;
instruction->rm = match->definition->modrm & 7;
if (match->definition->modrm)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
}
switch (match->definition->vector_length)
{
case ZYDIS_VECTOR_LENGTH_INVALID:
case ZYDIS_VECTOR_LENGTH_128:
instruction->vector_length = 0;
break;
case ZYDIS_VECTOR_LENGTH_256:
instruction->vector_length = 1;
break;
case ZYDIS_VECTOR_LENGTH_512:
instruction->vector_length = 2;
break;
default:
ZYAN_UNREACHABLE;
}
if (match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX)
{
const ZydisInstructionDefinitionEVEX *evex_def =
(const ZydisInstructionDefinitionEVEX *)match->base_definition;
if (evex_def->mask_override != ZYDIS_MASK_OVERRIDE_ZEROING)
{
instruction->zeroing = match->request->evex.zeroing_mask;
}
if ((match->request->evex.sae) ||
(match->request->evex.broadcast != ZYDIS_BROADCAST_MODE_INVALID))
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_EVEX_B;
}
if (match->request->evex.rounding != ZYDIS_ROUNDING_MODE_INVALID)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_EVEX_B;
switch (match->request->evex.rounding)
{
case ZYDIS_ROUNDING_MODE_RN:
instruction->vector_length = 0;
break;
case ZYDIS_ROUNDING_MODE_RD:
instruction->vector_length = 1;
break;
case ZYDIS_ROUNDING_MODE_RU:
instruction->vector_length = 2;
break;
case ZYDIS_ROUNDING_MODE_RZ:
instruction->vector_length = 3;
break;
default:
ZYAN_UNREACHABLE;
}
}
}
else if (match->definition->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
instruction->sss |= ZydisEncodeMvexBroadcastMode(match->request->mvex.broadcast);
instruction->sss |= ZydisEncodeMvexConversionMode(match->request->mvex.conversion);
switch (match->request->mvex.rounding)
{
case ZYDIS_ROUNDING_MODE_INVALID:
break;
case ZYDIS_ROUNDING_MODE_RN:
case ZYDIS_ROUNDING_MODE_RD:
case ZYDIS_ROUNDING_MODE_RU:
case ZYDIS_ROUNDING_MODE_RZ:
instruction->sss |= match->request->mvex.rounding - ZYDIS_ROUNDING_MODE_RN;
break;
default:
ZYAN_UNREACHABLE;
}
switch (match->request->mvex.swizzle)
{
case ZYDIS_SWIZZLE_MODE_INVALID:
break;
case ZYDIS_SWIZZLE_MODE_DCBA:
case ZYDIS_SWIZZLE_MODE_CDAB:
case ZYDIS_SWIZZLE_MODE_BADC:
case ZYDIS_SWIZZLE_MODE_DACB:
case ZYDIS_SWIZZLE_MODE_AAAA:
case ZYDIS_SWIZZLE_MODE_BBBB:
case ZYDIS_SWIZZLE_MODE_CCCC:
case ZYDIS_SWIZZLE_MODE_DDDD:
instruction->sss |= match->request->mvex.swizzle - ZYDIS_SWIZZLE_MODE_DCBA;
break;
default:
ZYAN_UNREACHABLE;
}
if ((match->request->mvex.sae) ||
(match->request->mvex.eviction_hint) ||
(match->request->mvex.rounding != ZYDIS_ROUNDING_MODE_INVALID))
{
instruction->eviction_hint = ZYAN_TRUE;
}
if (match->request->mvex.sae)
{
instruction->sss |= 4;
}
// Following instructions violate general `MVEX.EH` handling rules. In all other cases this
// bit is used either as eviction hint (memory operands present) or to encode MVEX-specific
// functionality (register forms). Instructions listed below use `MVEX.EH` to identify
// different instructions with memory operands and don't treat it as eviction hint.
switch (match->request->mnemonic)
{
case ZYDIS_MNEMONIC_VMOVNRAPD:
case ZYDIS_MNEMONIC_VMOVNRAPS:
instruction->eviction_hint = ZYAN_FALSE;
break;
case ZYDIS_MNEMONIC_VMOVNRNGOAPD:
case ZYDIS_MNEMONIC_VMOVNRNGOAPS:
instruction->eviction_hint = ZYAN_TRUE;
break;
default:
break;
}
}
switch (match->definition->mandatory_prefix)
{
case ZYDIS_MANDATORY_PREFIX_NONE:
break;
case ZYDIS_MANDATORY_PREFIX_66:
instruction->attributes |= ZYDIS_ATTRIB_HAS_OPERANDSIZE;
break;
case ZYDIS_MANDATORY_PREFIX_F2:
instruction->attributes |= ZYDIS_ATTRIB_HAS_REPNE;
break;
case ZYDIS_MANDATORY_PREFIX_F3:
instruction->attributes |= ZYDIS_ATTRIB_HAS_REP;
break;
default:
ZYAN_UNREACHABLE;
}
const ZyanU8 mode_width = ZydisGetMachineModeWidth(match->request->machine_mode);
if (match->easz != mode_width)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_ADDRESSSIZE;
}
if ((match->request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(match->base_definition->operand_size_map != ZYDIS_OPSIZE_MAP_FORCE64))
{
switch (match->eosz)
{
case 16:
instruction->attributes |= ZYDIS_ATTRIB_HAS_OPERANDSIZE;
break;
case 32:
break;
case 64:
instruction->rex_w =
match->base_definition->operand_size_map != ZYDIS_OPSIZE_MAP_DEFAULT64;
break;
default:
ZYAN_UNREACHABLE;
}
}
else
{
if (match->eosz != mode_width)
{
instruction->attributes |= ZYDIS_ATTRIB_HAS_OPERANDSIZE;
}
}
for (ZyanU8 i = 0; i < match->request->operand_count; ++i)
{
const ZydisEncoderOperand *user_op = &match->request->operands[i];
const ZydisOperandDefinition *def_op = &match->operands[i];
switch (user_op->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
ZydisBuildRegisterOperand(user_op, def_op, instruction);
break;
case ZYDIS_OPERAND_TYPE_MEMORY:
if (def_op->type != ZYDIS_SEMANTIC_OPTYPE_MOFFS)
{
ZydisBuildMemoryOperand(match, user_op, instruction);
if ((match->cd8_scale) &&
(instruction->disp_size == 8))
{
instruction->disp >>= match->cd8_scale;
}
}
else
{
instruction->disp_size = match->disp_size;
instruction->disp = (ZyanU64)user_op->mem.displacement;
}
break;
case ZYDIS_OPERAND_TYPE_POINTER:
instruction->disp_size = match->disp_size;
instruction->disp = user_op->ptr.offset;
instruction->imm_size = match->imm_size;
instruction->imm = user_op->ptr.segment;
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
if (def_op->type == ZYDIS_SEMANTIC_OPTYPE_IMPLICIT_IMM1)
{
break;
}
if (def_op->op.encoding != ZYDIS_OPERAND_ENCODING_IS4)
{
if (instruction->imm_size)
{
ZYAN_ASSERT(instruction->disp_size == 0);
instruction->disp_size = match->disp_size;
instruction->disp = instruction->imm;
}
instruction->imm_size = match->imm_size;
instruction->imm = user_op->imm.u;
}
else
{
ZYAN_ASSERT(instruction->imm_size == 8);
instruction->imm |= user_op->imm.u;
}
break;
default:
ZYAN_UNREACHABLE;
}
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Encodes instruction as emittable `ZydisEncoderInstruction` struct.
*
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
* @param instruction A pointer to `ZydisEncoderInstruction` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L3858-L4100
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncoderCheckRequestSanity
|
ZyanStatus ZydisEncoderCheckRequestSanity(const ZydisEncoderRequest *request)
{
if (((ZyanUSize)request->machine_mode > ZYDIS_MACHINE_MODE_MAX_VALUE) ||
((ZyanUSize)request->allowed_encodings > ZYDIS_ENCODABLE_ENCODING_MAX_VALUE) ||
((ZyanUSize)request->mnemonic > ZYDIS_MNEMONIC_MAX_VALUE) ||
((ZyanUSize)request->branch_type > ZYDIS_BRANCH_TYPE_MAX_VALUE) ||
((ZyanUSize)request->branch_width > ZYDIS_BRANCH_WIDTH_MAX_VALUE) ||
((ZyanUSize)request->address_size_hint > ZYDIS_ADDRESS_SIZE_MAX_VALUE) ||
((ZyanUSize)request->operand_size_hint > ZYDIS_OPERAND_SIZE_MAX_VALUE) ||
((ZyanUSize)request->evex.broadcast > ZYDIS_BROADCAST_MODE_MAX_VALUE) ||
((ZyanUSize)request->evex.rounding > ZYDIS_ROUNDING_MODE_MAX_VALUE) ||
((ZyanUSize)request->mvex.broadcast > ZYDIS_BROADCAST_MODE_MAX_VALUE) ||
((ZyanUSize)request->mvex.conversion > ZYDIS_CONVERSION_MODE_MAX_VALUE) ||
((ZyanUSize)request->mvex.rounding > ZYDIS_ROUNDING_MODE_MAX_VALUE) ||
((ZyanUSize)request->mvex.swizzle > ZYDIS_SWIZZLE_MODE_MAX_VALUE) ||
(request->operand_count > ZYDIS_ENCODER_MAX_OPERANDS) ||
(request->mnemonic == ZYDIS_MNEMONIC_INVALID) ||
(request->prefixes & ~ZYDIS_ENCODABLE_PREFIXES))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT)
{
if ((request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64) &&
(request->prefixes & ZYDIS_LEGACY_SEGMENTS))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZyanU8 seg_override_count = 0;
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_CS)
{
++seg_override_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_SS)
{
++seg_override_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_DS)
{
++seg_override_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_ES)
{
++seg_override_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_FS)
{
++seg_override_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT_GS)
{
++seg_override_count;
}
if (seg_override_count != 1)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
ZyanU8 rep_family_count = 0;
if (request->prefixes & ZYDIS_ATTRIB_HAS_REP)
{
++rep_family_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_REPE)
{
++rep_family_count;
}
if (request->prefixes & ZYDIS_ATTRIB_HAS_REPNE)
{
++rep_family_count;
}
if (rep_family_count > 1)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if ((request->prefixes & ZYDIS_ATTRIB_HAS_XACQUIRE) &&
(request->prefixes & ZYDIS_ATTRIB_HAS_XRELEASE))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if ((request->prefixes & ZYDIS_ATTRIB_HAS_BRANCH_NOT_TAKEN) &&
(request->prefixes & ZYDIS_ATTRIB_HAS_BRANCH_TAKEN))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if ((request->prefixes & ZYDIS_ATTRIB_HAS_NOTRACK) &&
(request->prefixes & ZYDIS_ATTRIB_HAS_SEGMENT))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
static const ZyanBool branch_lookup
[ZYDIS_BRANCH_WIDTH_MAX_VALUE + 1][ZYDIS_BRANCH_TYPE_MAX_VALUE + 1] =
{
/* NONE */ { ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE },
/* 8 */ { ZYAN_TRUE, ZYAN_TRUE, ZYAN_FALSE, ZYAN_FALSE },
/* 16 */ { ZYAN_TRUE, ZYAN_FALSE, ZYAN_TRUE, ZYAN_TRUE },
/* 32 */ { ZYAN_TRUE, ZYAN_FALSE, ZYAN_TRUE, ZYAN_TRUE },
/* 64 */ { ZYAN_TRUE, ZYAN_FALSE, ZYAN_TRUE, ZYAN_TRUE },
};
if (!branch_lookup[request->branch_width][request->branch_type])
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if (request->machine_mode == ZYDIS_MACHINE_MODE_LONG_64)
{
if (request->address_size_hint == ZYDIS_ADDRESS_SIZE_HINT_16)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
else
{
if ((request->branch_width == ZYDIS_BRANCH_WIDTH_64) ||
(request->address_size_hint == ZYDIS_ADDRESS_SIZE_HINT_64) ||
(request->operand_size_hint == ZYDIS_OPERAND_SIZE_HINT_64))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
for (ZyanU8 i = 0; i < request->operand_count; ++i)
{
const ZydisEncoderOperand *op = &request->operands[i];
if ((op->type == ZYDIS_OPERAND_TYPE_UNUSED) ||
((ZyanUSize)op->type > ZYDIS_OPERAND_TYPE_MAX_VALUE))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
switch (op->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
if (op->reg.value > ZYDIS_REGISTER_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
break;
case ZYDIS_OPERAND_TYPE_MEMORY:
if (((ZyanUSize)op->mem.base > ZYDIS_REGISTER_MAX_VALUE) ||
((ZyanUSize)op->mem.index > ZYDIS_REGISTER_MAX_VALUE) ||
!ZydisIsScaleValid(op->mem.scale))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
break;
case ZYDIS_OPERAND_TYPE_POINTER:
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
return ZYAN_STATUS_SUCCESS;
}
|
/**
* Performs a set of sanity checks that must be satisfied for every valid encoder request.
*
* @param request A pointer to `ZydisEncoderRequest` struct.
*
* @return A zyan status code.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L4109-L4267
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisEncoderEncodeInstruction
|
ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstruction(const ZydisEncoderRequest *request,
void *buffer, ZyanUSize *length)
{
if (!request || !buffer || !length)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYAN_CHECK(ZydisEncoderCheckRequestSanity(request));
ZydisEncoderInstructionMatch match;
ZYAN_CHECK(ZydisFindMatchingDefinition(request, &match));
ZydisEncoderBuffer output;
output.buffer = (ZyanU8 *)buffer;
output.size = *length;
output.offset = 0;
ZydisEncoderInstruction instruction;
ZYAN_CHECK(ZydisBuildInstruction(&match, &instruction));
ZYAN_CHECK(ZydisEmitInstruction(&instruction, &output));
*length = output.offset;
return ZYAN_STATUS_SUCCESS;
}
|
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Encoder.c#L4273-L4294
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterBufferInit
|
void ZydisFormatterBufferInit(ZydisFormatterBuffer* buffer, char* user_buffer,
ZyanUSize length)
{
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(user_buffer);
ZYAN_ASSERT(length);
buffer->is_token_list = ZYAN_FALSE;
buffer->capacity = 0;
buffer->string.flags = ZYAN_STRING_HAS_FIXED_CAPACITY;
buffer->string.vector.allocator = ZYAN_NULL;
buffer->string.vector.growth_factor = 1;
buffer->string.vector.shrink_threshold = 0;
buffer->string.vector.destructor = ZYAN_NULL;
buffer->string.vector.element_size = sizeof(char);
buffer->string.vector.size = 1;
buffer->string.vector.capacity = length;
buffer->string.vector.data = user_buffer;
*user_buffer = '\0';
}
|
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Internal functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Helper functions */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Formatter.c#L58-L78
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterInit
|
ZyanStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle style)
{
if (!formatter || ((ZyanUSize)style > ZYDIS_FORMATTER_STYLE_MAX_VALUE))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYAN_MEMCPY(formatter, FORMATTER_PRESETS[style], sizeof(*formatter));
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Initialization */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Formatter.c#L120-L130
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterSetProperty
|
ZyanStatus ZydisFormatterSetProperty(ZydisFormatter* formatter, ZydisFormatterProperty property,
ZyanUPointer value)
{
if (!formatter)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZydisNumericBase base = (ZydisNumericBase)(-1);
ZyanU8 index = 0xFF;
switch (property)
{
case ZYDIS_FORMATTER_PROP_FORCE_SIZE:
{
formatter->force_memory_size = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_FORCE_SEGMENT:
{
formatter->force_memory_segment = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_FORCE_SCALE_ONE:
{
formatter->force_memory_scale = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_FORCE_RELATIVE_BRANCHES:
{
formatter->force_relative_branches = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_FORCE_RELATIVE_RIPREL:
{
formatter->force_relative_riprel = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_PRINT_BRANCH_SIZE:
{
formatter->print_branch_size = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_DETAILED_PREFIXES:
{
formatter->detailed_prefixes = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_ADDR_BASE:
{
if (value > ZYDIS_NUMERIC_BASE_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->addr_base = (ZydisNumericBase)value;
break;
}
case ZYDIS_FORMATTER_PROP_ADDR_SIGNEDNESS:
{
if (value > ZYDIS_SIGNEDNESS_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->addr_signedness = (ZydisSignedness)value;
break;
}
case ZYDIS_FORMATTER_PROP_ADDR_PADDING_ABSOLUTE:
{
if (((ZydisPadding)value != ZYDIS_PADDING_AUTO) &&
(value > 0xFF))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->addr_padding_absolute = (ZydisPadding)value;
break;
}
case ZYDIS_FORMATTER_PROP_ADDR_PADDING_RELATIVE:
{
if (((ZydisPadding)value != ZYDIS_PADDING_AUTO) &&
(value > 0xFF))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->addr_padding_relative = (ZydisPadding)value;
break;
}
case ZYDIS_FORMATTER_PROP_DISP_BASE:
{
if (value > ZYDIS_NUMERIC_BASE_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->disp_base = (ZydisNumericBase)value;
break;
}
case ZYDIS_FORMATTER_PROP_DISP_SIGNEDNESS:
{
if (value > ZYDIS_SIGNEDNESS_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->disp_signedness = (ZydisSignedness)value;
break;
}
case ZYDIS_FORMATTER_PROP_DISP_PADDING:
{
if ((ZydisPadding)value == ZYDIS_PADDING_AUTO)
{
if ((ZyanUSize)formatter->style > ZYDIS_FORMATTER_STYLE_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->disp_padding = FORMATTER_PRESETS[formatter->style]->disp_padding;
}
else if (value > 0xFF)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->disp_padding = (ZydisPadding)value;
break;
}
case ZYDIS_FORMATTER_PROP_IMM_BASE:
{
if (value > ZYDIS_NUMERIC_BASE_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->imm_base = (ZydisNumericBase)value;
break;
}
case ZYDIS_FORMATTER_PROP_IMM_SIGNEDNESS:
{
if (value > ZYDIS_SIGNEDNESS_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->imm_signedness = (ZydisSignedness)value;
break;
}
case ZYDIS_FORMATTER_PROP_IMM_PADDING:
{
if ((ZydisPadding)value == ZYDIS_PADDING_AUTO)
{
if ((ZyanUSize)formatter->style > ZYDIS_FORMATTER_STYLE_MAX_VALUE)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->imm_padding = FORMATTER_PRESETS[formatter->style]->imm_padding;
}
else if (value > 0xFF)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
formatter->imm_padding = (ZydisPadding)value;
break;
}
case ZYDIS_FORMATTER_PROP_UPPERCASE_PREFIXES:
{
formatter->case_prefixes = (value) ? ZYDIS_LETTER_CASE_UPPER : ZYDIS_LETTER_CASE_DEFAULT;
break;
}
case ZYDIS_FORMATTER_PROP_UPPERCASE_MNEMONIC:
{
formatter->case_mnemonic = (value) ? ZYDIS_LETTER_CASE_UPPER : ZYDIS_LETTER_CASE_DEFAULT;
break;
}
case ZYDIS_FORMATTER_PROP_UPPERCASE_REGISTERS:
{
formatter->case_registers = (value) ? ZYDIS_LETTER_CASE_UPPER : ZYDIS_LETTER_CASE_DEFAULT;
break;
}
case ZYDIS_FORMATTER_PROP_UPPERCASE_TYPECASTS:
{
formatter->case_typecasts = (value) ? ZYDIS_LETTER_CASE_UPPER : ZYDIS_LETTER_CASE_DEFAULT;
break;
}
case ZYDIS_FORMATTER_PROP_UPPERCASE_DECORATORS:
{
formatter->case_decorators = (value) ? ZYDIS_LETTER_CASE_UPPER : ZYDIS_LETTER_CASE_DEFAULT;
break;
}
case ZYDIS_FORMATTER_PROP_DEC_PREFIX:
{
base = ZYDIS_NUMERIC_BASE_DEC;
index = 0;
break;
}
case ZYDIS_FORMATTER_PROP_DEC_SUFFIX:
{
base = ZYDIS_NUMERIC_BASE_DEC;
index = 1;
break;
}
case ZYDIS_FORMATTER_PROP_HEX_UPPERCASE:
{
formatter->hex_uppercase = (value) ? ZYAN_TRUE : ZYAN_FALSE;
break;
}
case ZYDIS_FORMATTER_PROP_HEX_PREFIX:
{
base = ZYDIS_NUMERIC_BASE_HEX;
index = 0;
break;
}
case ZYDIS_FORMATTER_PROP_HEX_SUFFIX:
{
base = ZYDIS_NUMERIC_BASE_HEX;
index = 1;
break;
}
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
// Set prefix or suffix
if (base != (ZydisNumericBase)(-1))
{
if (value)
{
const ZyanUSize len = ZYAN_STRLEN((char*)value);
if (len > 10)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYAN_MEMCPY(formatter->number_format[base][index].buffer, (void*)value, len);
formatter->number_format[base][index].buffer[len] = '\0';
formatter->number_format[base][index].string_data.string.vector.data =
formatter->number_format[base][index].buffer;
formatter->number_format[base][index].string_data.string.vector.size = len + 1;
formatter->number_format[base][index].string =
&formatter->number_format[base][index].string_data;
} else
{
formatter->number_format[base][index].string = ZYAN_NULL;
}
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Setter */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Formatter.c#L136-L374
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterFormatInstruction
|
ZyanStatus ZydisFormatterFormatInstruction(const ZydisFormatter* formatter,
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operands,
ZyanU8 operand_count, char* buffer, ZyanUSize length, ZyanU64 runtime_address)
{
return ZydisFormatterFormatInstructionEx(formatter, instruction, operands, operand_count,
buffer, length, runtime_address, ZYAN_NULL);
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Formatting */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Formatter.c#L450-L456
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterTokenizeInstruction
|
ZyanStatus ZydisFormatterTokenizeInstruction(const ZydisFormatter* formatter,
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operands,
ZyanU8 operand_count, void* buffer, ZyanUSize length, ZyanU64 runtime_address,
ZydisFormatterTokenConst** token)
{
return ZydisFormatterTokenizeInstructionEx(formatter, instruction, operands, operand_count,
buffer, length, runtime_address, token, ZYAN_NULL);
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Tokenizing */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Formatter.c#L559-L566
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterATTFormatInstruction
|
ZyanStatus ZydisFormatterATTFormatInstruction(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYAN_ASSERT(context->instruction);
ZYAN_ASSERT(context->operands);
ZYAN_CHECK(formatter->func_print_prefixes(formatter, buffer, context));
ZYAN_CHECK(formatter->func_print_mnemonic(formatter, buffer, context));
ZyanUPointer state_mnemonic;
ZYDIS_BUFFER_REMEMBER(buffer, state_mnemonic);
const ZyanI8 c = (ZyanI8)context->instruction->operand_count_visible - 1;
for (ZyanI8 i = c; i >= 0; --i)
{
const ZydisDecodedOperand* const operand = &context->operands[i];
// Print embedded-mask registers as decorator instead of a regular operand
if ((i == 1) && (operand->type == ZYDIS_OPERAND_TYPE_REGISTER) &&
(operand->encoding == ZYDIS_OPERAND_ENCODING_MASK))
{
continue;
}
ZyanUPointer buffer_state;
ZYDIS_BUFFER_REMEMBER(buffer, buffer_state);
if (buffer_state != state_mnemonic)
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_OPERAND);
} else
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_MNEMONIC);
}
// Set current operand
context->operand = operand;
ZyanStatus status;
if (formatter->func_pre_operand)
{
status = formatter->func_pre_operand(formatter, buffer, context);
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (!ZYAN_SUCCESS(status))
{
return status;
}
}
switch (operand->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
status = formatter->func_format_operand_reg(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_MEMORY:
status = formatter->func_format_operand_mem(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_POINTER:
status = formatter->func_format_operand_ptr(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
status = formatter->func_format_operand_imm(formatter, buffer, context);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (!ZYAN_SUCCESS(status))
{
return status;
}
if (formatter->func_post_operand)
{
status = formatter->func_post_operand(formatter, buffer, context);
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (ZYAN_SUCCESS(status))
{
return status;
}
}
#if !defined(ZYDIS_DISABLE_AVX512) || !defined(ZYDIS_DISABLE_KNC)
if ((context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
if ((i == 0) &&
(context->instruction->operand_count_visible > 1) &&
(context->operands[1].encoding == ZYDIS_OPERAND_ENCODING_MASK))
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_MASK));
}
if (operand->type == ZYDIS_OPERAND_TYPE_MEMORY)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_BC));
if (context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_CONVERSION));
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_EH));
}
} else
{
ZyanBool decorate_operand;
if (i == (context->instruction->operand_count_visible - 1))
{
decorate_operand = operand->type != ZYDIS_OPERAND_TYPE_IMMEDIATE;
}
else
{
decorate_operand =
(context->instruction->operand_count_visible > (i + 1)) &&
((context->operands[i + 1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE) ||
(context->operands[i + 1].visibility == ZYDIS_OPERAND_VISIBILITY_HIDDEN));
}
if (decorate_operand)
{
if (context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_SWIZZLE));
}
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_RC));
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_SAE));
}
}
}
#endif
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ============================================================================================== */
/* Formatter functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Instruction */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterATT.c#L43-L194
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterATTFormatOperandMEM
|
ZyanStatus ZydisFormatterATTFormatOperandMEM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYAN_CHECK(formatter->func_print_segment(formatter, buffer, context));
const ZyanBool absolute = !formatter->force_relative_riprel &&
(context->runtime_address != ZYDIS_RUNTIME_ADDRESS_NONE);
if (absolute && context->operand->mem.disp.has_displacement &&
(context->operand->mem.index == ZYDIS_REGISTER_NONE) &&
((context->operand->mem.base == ZYDIS_REGISTER_NONE) ||
(context->operand->mem.base == ZYDIS_REGISTER_EIP ) ||
(context->operand->mem.base == ZYDIS_REGISTER_RIP )))
{
// EIP/RIP-relative or absolute-displacement address operand
ZYAN_CHECK(formatter->func_print_address_abs(formatter, buffer, context));
} else
{
const ZyanBool should_print_reg = context->operand->mem.base != ZYDIS_REGISTER_NONE;
const ZyanBool should_print_idx = context->operand->mem.index != ZYDIS_REGISTER_NONE;
const ZyanBool neither_reg_nor_idx = !should_print_reg && !should_print_idx;
// Regular memory operand
if (neither_reg_nor_idx)
{
ZYAN_CHECK(formatter->func_print_address_abs(formatter, buffer, context));
} else if (context->operand->mem.disp.has_displacement && context->operand->mem.disp.value)
{
ZYAN_CHECK(formatter->func_print_disp(formatter, buffer, context));
}
if (neither_reg_nor_idx)
{
return ZYAN_STATUS_SUCCESS;
}
ZYDIS_BUFFER_APPEND(buffer, MEMORY_BEGIN_ATT);
if (should_print_reg)
{
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.base));
}
if (should_print_idx)
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_MEMORY);
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.index));
if (context->operand->mem.scale &&
(context->operand->mem.type != ZYDIS_MEMOP_TYPE_MIB) &&
((context->operand->mem.scale > 1) || formatter->force_memory_scale))
{
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_DELIMITER);
ZYDIS_BUFFER_APPEND(buffer, DELIM_MEMORY);
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_IMMEDIATE);
ZYAN_CHECK(ZydisStringAppendDecU(&buffer->string, context->operand->mem.scale, 0,
ZYAN_NULL, ZYAN_NULL));
}
}
ZYDIS_BUFFER_APPEND(buffer, MEMORY_END_ATT);
return ZYAN_STATUS_SUCCESS;
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Operands */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterATT.c#L200-L268
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterATTPrintMnemonic
|
ZyanStatus ZydisFormatterATTPrintMnemonic(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYAN_ASSERT(context->instruction);
ZYAN_ASSERT(context->operands);
const ZydisShortString* mnemonic = ZydisMnemonicGetStringWrapped(
context->instruction->mnemonic);
if (!mnemonic)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, INVALID_MNEMONIC, formatter->case_mnemonic);
return ZYAN_STATUS_SUCCESS;
}
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_MNEMONIC);
if (context->instruction->meta.branch_type == ZYDIS_BRANCH_TYPE_FAR)
{
ZYAN_CHECK(ZydisStringAppendShortCase(&buffer->string, &STR_FAR_ATT,
formatter->case_mnemonic));
}
ZYAN_CHECK(ZydisStringAppendShortCase(&buffer->string, mnemonic, formatter->case_mnemonic));
// Append operand-size suffix
ZyanU32 size = 0;
for (ZyanU8 i = 0; i < context->instruction->operand_count_visible; ++i)
{
const ZydisDecodedOperand* const operand = &context->operands[i];
if ((operand->type == ZYDIS_OPERAND_TYPE_MEMORY) &&
((operand->mem.type == ZYDIS_MEMOP_TYPE_MEM) ||
(operand->mem.type == ZYDIS_MEMOP_TYPE_VSIB)))
{
size = ZydisFormatterHelperGetExplicitSize(formatter, context, operand);
break;
}
}
switch (size)
{
case 8: ZydisStringAppendShort(&buffer->string, &STR_SIZE_8_ATT ); break;
case 16: ZydisStringAppendShort(&buffer->string, &STR_SIZE_16_ATT ); break;
case 32: ZydisStringAppendShort(&buffer->string, &STR_SIZE_32_ATT ); break;
case 64: ZydisStringAppendShort(&buffer->string, &STR_SIZE_64_ATT ); break;
case 128: ZydisStringAppendShort(&buffer->string, &STR_SIZE_128_ATT); break;
case 256: ZydisStringAppendShort(&buffer->string, &STR_SIZE_256_ATT); break;
case 512: ZydisStringAppendShort(&buffer->string, &STR_SIZE_512_ATT); break;
default:
break;
}
if (formatter->print_branch_size)
{
switch (context->instruction->meta.branch_type)
{
case ZYDIS_BRANCH_TYPE_NONE:
break;
case ZYDIS_BRANCH_TYPE_SHORT:
return ZydisStringAppendShortCase(&buffer->string, &STR_SHORT,
formatter->case_mnemonic);
case ZYDIS_BRANCH_TYPE_NEAR:
return ZydisStringAppendShortCase(&buffer->string, &STR_NEAR,
formatter->case_mnemonic);
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Elemental tokens */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterATT.c#L274-L344
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterHelperGetExplicitSize
|
ZyanU32 ZydisFormatterHelperGetExplicitSize(const ZydisFormatter* formatter,
ZydisFormatterContext* context, const ZydisDecodedOperand* operand)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(context);
ZYAN_ASSERT(operand);
ZYAN_ASSERT(operand->type == ZYDIS_OPERAND_TYPE_MEMORY);
ZYAN_ASSERT((operand->mem.type == ZYDIS_MEMOP_TYPE_MEM) ||
(operand->mem.type == ZYDIS_MEMOP_TYPE_VSIB));
if (formatter->force_memory_size)
{
return operand->size;
}
if (!context->operands)
{
// Single operand formatting. We can not derive the explicit size by using the other
// operands.
return 0;
}
switch (operand->id)
{
case 0:
if (context->instruction->operand_count_visible < 2)
{
return 0;
}
if ((context->operands[1].type == ZYDIS_OPERAND_TYPE_UNUSED) ||
(context->operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE))
{
return context->operands[0].size;
}
if (context->operands[0].size != context->operands[1].size)
{
return context->operands[0].size;
}
if ((context->operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER) &&
(context->operands[1].visibility == ZYDIS_OPERAND_VISIBILITY_IMPLICIT) &&
(context->operands[1].reg.value == ZYDIS_REGISTER_CL))
{
return context->operands[0].size;
}
break;
case 1:
case 2:
if (context->operands[operand->id - 1].size !=
context->operands[operand->id].size)
{
return context->operands[operand->id].size;
}
break;
default:
break;
}
return 0;
}
|
/* ============================================================================================== */
/* Helper functions */
/* ============================================================================================== */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBase.c#L80-L139
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterBaseFormatOperandREG
|
ZyanStatus ZydisFormatterBaseFormatOperandREG(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
return formatter->func_print_register(formatter, buffer, context, context->operand->reg.value);
}
|
/* ============================================================================================== */
/* Formatter functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Operands */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBase.c#L149-L157
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterBasePrintAddressABS
|
ZyanStatus ZydisFormatterBasePrintAddressABS(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZyanU64 address;
ZYAN_CHECK(ZydisCalcAbsoluteAddress(context->instruction, context->operand,
context->runtime_address, &address));
ZyanU8 padding = (formatter->addr_padding_absolute ==
ZYDIS_PADDING_AUTO) ? 0 : (ZyanU8)formatter->addr_padding_absolute;
if ((formatter->addr_padding_absolute == ZYDIS_PADDING_AUTO) &&
(formatter->addr_base == ZYDIS_NUMERIC_BASE_HEX))
{
switch (context->instruction->stack_width)
{
case 16:
padding = 4;
address = (ZyanU16)address;
break;
case 32:
padding = 8;
address = (ZyanU32)address;
break;
case 64:
padding = 16;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_ADDRESS_ABS);
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->addr_base, &buffer->string, address, padding);
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Elemental tokens */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBase.c#L218-L255
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterBasePrintSegment
|
ZyanStatus ZydisFormatterBasePrintSegment(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZyanBool printed_segment = ZYAN_FALSE;
switch (context->operand->mem.segment)
{
case ZYDIS_REGISTER_ES:
case ZYDIS_REGISTER_CS:
case ZYDIS_REGISTER_FS:
case ZYDIS_REGISTER_GS:
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
break;
case ZYDIS_REGISTER_SS:
if ((formatter->force_memory_segment) ||
(context->instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS))
{
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
}
break;
case ZYDIS_REGISTER_DS:
if ((formatter->force_memory_segment) ||
(context->instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_DS))
{
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
}
break;
default:
break;
}
if (printed_segment)
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_SEGMENT);
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Optional tokens */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBase.c#L373-L418
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterTokenGetValue
|
ZyanStatus ZydisFormatterTokenGetValue(const ZydisFormatterToken* token,
ZydisTokenType* type, ZyanConstCharPointer* value)
{
if (!token || !type || !value)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
*type = token->type;
*value = (ZyanConstCharPointer)((ZyanU8*)token + sizeof(ZydisFormatterToken));
return ZYAN_STATUS_SUCCESS;
}
|
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Token */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBuffer.c#L38-L50
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterBufferGetToken
|
ZyanStatus ZydisFormatterBufferGetToken(const ZydisFormatterBuffer* buffer,
ZydisFormatterTokenConst** token)
{
if (!buffer || !token)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
*token = ((ZydisFormatterTokenConst*)buffer->string.vector.data - 1);
if ((*token)->type == ZYDIS_TOKEN_INVALID)
{
return ZYAN_STATUS_INVALID_OPERATION;
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Buffer */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterBuffer.c#L73-L88
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterIntelFormatInstruction
|
ZyanStatus ZydisFormatterIntelFormatInstruction(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYAN_ASSERT(context->instruction);
ZYAN_ASSERT(context->operands);
ZYAN_CHECK(formatter->func_print_prefixes(formatter, buffer, context));
ZYAN_CHECK(formatter->func_print_mnemonic(formatter, buffer, context));
ZyanUPointer state_mnemonic;
ZYDIS_BUFFER_REMEMBER(buffer, state_mnemonic);
for (ZyanU8 i = 0; i < context->instruction->operand_count_visible; ++i)
{
const ZydisDecodedOperand* const operand = &context->operands[i];
// Print embedded-mask registers as decorator instead of a regular operand
if ((i == 1) && (operand->type == ZYDIS_OPERAND_TYPE_REGISTER) &&
(operand->encoding == ZYDIS_OPERAND_ENCODING_MASK))
{
continue;
}
ZyanUPointer buffer_state;
ZYDIS_BUFFER_REMEMBER(buffer, buffer_state);
if (buffer_state != state_mnemonic)
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_OPERAND);
} else
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_MNEMONIC);
}
// Set current operand
context->operand = operand;
ZyanStatus status;
if (formatter->func_pre_operand)
{
status = formatter->func_pre_operand(formatter, buffer, context);
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (!ZYAN_SUCCESS(status))
{
return status;
}
}
switch (operand->type)
{
case ZYDIS_OPERAND_TYPE_REGISTER:
status = formatter->func_format_operand_reg(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_MEMORY:
status = formatter->func_format_operand_mem(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_POINTER:
status = formatter->func_format_operand_ptr(formatter, buffer, context);
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
status = formatter->func_format_operand_imm(formatter, buffer, context);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (!ZYAN_SUCCESS(status))
{
return status;
}
if (formatter->func_post_operand)
{
status = formatter->func_post_operand(formatter, buffer, context);
if (status == ZYDIS_STATUS_SKIP_TOKEN)
{
ZYAN_CHECK(ZydisFormatterBufferRestore(buffer, buffer_state));
continue;
}
if (ZYAN_SUCCESS(status))
{
return status;
}
}
#if !defined(ZYDIS_DISABLE_AVX512) || !defined(ZYDIS_DISABLE_KNC)
if ((context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) ||
(context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX))
{
if ((i == 0) &&
(context->instruction->operand_count_visible > 1) &&
(context->operands[i + 1].encoding == ZYDIS_OPERAND_ENCODING_MASK))
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_MASK));
}
if (operand->type == ZYDIS_OPERAND_TYPE_MEMORY)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_BC));
if (context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_CONVERSION));
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_EH));
}
} else
{
ZyanBool decorate_operand;
if (i == (context->instruction->operand_count_visible - 1))
{
decorate_operand = operand->type != ZYDIS_OPERAND_TYPE_IMMEDIATE;
}
else
{
decorate_operand =
(context->instruction->operand_count_visible > (i + 1)) &&
((context->operands[i + 1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE) ||
(context->operands[i + 1].visibility == ZYDIS_OPERAND_VISIBILITY_HIDDEN));
}
if (decorate_operand)
{
if (context->instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)
{
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_SWIZZLE));
}
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_RC));
ZYAN_CHECK(formatter->func_print_decorator(formatter, buffer, context,
ZYDIS_DECORATOR_SAE));
}
}
}
#endif
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ============================================================================================== */
/* Formatter functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Intel */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterIntel.c#L45-L194
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisFormatterIntelFormatInstructionMASM
|
ZyanStatus ZydisFormatterIntelFormatInstructionMASM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
// Force the formatter to always call our MASM `ZYDIS_FORMATTER_PRINT_ADDRESS_ABS` function.
// This implicitly omits printing of the `RIP`/`EIP` registers for `RIP`/`EIP`-relative
// memory operands
context->runtime_address = 0;
return ZydisFormatterIntelFormatInstruction(formatter, buffer, context);
}
|
/* ---------------------------------------------------------------------------------------------- */
/* MASM */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/FormatterIntel.c#L396-L409
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisRegisterEncode
|
ZydisRegister ZydisRegisterEncode(ZydisRegisterClass register_class, ZyanU8 id)
{
if ((register_class == ZYDIS_REGCLASS_INVALID) ||
(register_class == ZYDIS_REGCLASS_FLAGS) ||
(register_class == ZYDIS_REGCLASS_IP))
{
return ZYDIS_REGISTER_NONE;
}
if ((ZyanUSize)register_class >= ZYAN_ARRAY_LENGTH(REG_CLASS_LOOKUP))
{
return ZYDIS_REGISTER_NONE;
}
const ZydisRegisterClassLookupItem* item = ®_CLASS_LOOKUP[register_class];
if (id <= (item->hi - item->lo))
{
return item->lo + id;
}
return ZYDIS_REGISTER_NONE;
}
|
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Register */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Register.c#L97-L118
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisRegisterClassGetWidth
|
ZydisRegisterWidth ZydisRegisterClassGetWidth(ZydisMachineMode mode,
ZydisRegisterClass register_class)
{
if ((ZyanUSize)register_class >= ZYAN_ARRAY_LENGTH(REG_CLASS_LOOKUP))
{
return 0;
}
return (mode == ZYDIS_MACHINE_MODE_LONG_64)
? REG_CLASS_LOOKUP[register_class].width64
: REG_CLASS_LOOKUP[register_class].width;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Register class */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Register.c#L276-L287
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetInstructionDefinition
|
void ZydisGetInstructionDefinition(ZydisInstructionEncoding encoding, ZyanU16 id,
const ZydisInstructionDefinition** definition)
{
switch (encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_LEGACY[id];
break;
case ZYDIS_INSTRUCTION_ENCODING_3DNOW:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_3DNOW[id];
break;
case ZYDIS_INSTRUCTION_ENCODING_XOP:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_XOP[id];
break;
case ZYDIS_INSTRUCTION_ENCODING_VEX:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_VEX[id];
break;
#ifndef ZYDIS_DISABLE_AVX512
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_EVEX[id];
break;
#endif
#ifndef ZYDIS_DISABLE_KNC
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
*definition = (ZydisInstructionDefinition*)&ISTR_DEFINITIONS_MVEX[id];
break;
#endif
default:
ZYAN_UNREACHABLE;
}
}
|
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Instruction definition */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/SharedData.c#L74-L104
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisStringAppendDecU
|
ZyanStatus ZydisStringAppendDecU(ZyanString* string, ZyanU64 value, ZyanU8 padding_length,
const ZyanStringView* prefix, const ZyanStringView* suffix)
{
if (prefix)
{
ZYAN_CHECK(ZydisStringAppend(string, prefix));
}
#if defined(ZYAN_X64) || defined(ZYAN_AARCH64)
ZYAN_CHECK(ZydisStringAppendDecU64(string, value, padding_length));
#else
if (value & 0xFFFFFFFF00000000)
{
ZYAN_CHECK(ZydisStringAppendDecU64(string, value, padding_length));
}
ZYAN_CHECK(ZydisStringAppendDecU32(string, (ZyanU32)value, padding_length));
#endif
if (suffix)
{
return ZydisStringAppend(string, suffix);
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Public Functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Formatting */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/String.c#L307-L330
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetInstructionSegments
|
ZyanStatus ZydisGetInstructionSegments(const ZydisDecodedInstruction* instruction,
ZydisInstructionSegments* segments)
{
if (!instruction || !segments)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYAN_MEMSET(segments, 0, sizeof(*segments));
// Legacy prefixes and `REX`
if (instruction->raw.prefix_count)
{
const ZyanU8 rex_offset = (instruction->attributes & ZYDIS_ATTRIB_HAS_REX) ? 1 : 0;
if (!rex_offset || (instruction->raw.prefix_count > 1))
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_PREFIXES;
segments->segments[segments->count ].offset = 0;
segments->segments[segments->count++].size =
instruction->raw.prefix_count - rex_offset;
}
if (rex_offset)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_REX;
segments->segments[segments->count ].offset =
instruction->raw.prefix_count - rex_offset;
segments->segments[segments->count++].size = 1;
}
}
// Encoding prefixes
ZydisInstructionSegment segment_type = ZYDIS_INSTR_SEGMENT_NONE;
ZyanU8 segment_offset = 0;
ZyanU8 segment_size = 0;
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
segment_type = ZYDIS_INSTR_SEGMENT_XOP;
segment_offset = instruction->raw.xop.offset;
segment_size = 3;
break;
case ZYDIS_INSTRUCTION_ENCODING_VEX:
segment_type = ZYDIS_INSTR_SEGMENT_VEX;
segment_offset = instruction->raw.vex.offset;
segment_size = instruction->raw.vex.size;
break;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
segment_type = ZYDIS_INSTR_SEGMENT_EVEX;
segment_offset = instruction->raw.evex.offset;
segment_size = 4;
break;
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
segment_type = ZYDIS_INSTR_SEGMENT_MVEX;
segment_offset = instruction->raw.mvex.offset;
segment_size = 4;
break;
default:
break;
}
if (segment_type)
{
segments->segments[segments->count ].type = segment_type;
segments->segments[segments->count ].offset = segment_offset;
segments->segments[segments->count++].size = segment_size;
}
// Opcode
segment_size = 1;
if ((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_LEGACY) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW))
{
switch (instruction->opcode_map)
{
case ZYDIS_OPCODE_MAP_DEFAULT:
break;
case ZYDIS_OPCODE_MAP_0F:
ZYAN_FALLTHROUGH;
case ZYDIS_OPCODE_MAP_0F0F:
segment_size = 2;
break;
case ZYDIS_OPCODE_MAP_0F38:
ZYAN_FALLTHROUGH;
case ZYDIS_OPCODE_MAP_0F3A:
segment_size = 3;
break;
default:
ZYAN_UNREACHABLE;
}
}
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_OPCODE;
if (segments->count)
{
segments->segments[segments->count].offset =
segments->segments[segments->count - 1].offset +
segments->segments[segments->count - 1].size;
} else
{
segments->segments[segments->count].offset = 0;
}
segments->segments[segments->count++].size = segment_size;
// ModRM
if (instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_MODRM;
segments->segments[segments->count ].offset = instruction->raw.modrm.offset;
segments->segments[segments->count++].size = 1;
}
// SIB
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SIB)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_SIB;
segments->segments[segments->count ].offset = instruction->raw.sib.offset;
segments->segments[segments->count++].size = 1;
}
// Displacement
if (instruction->raw.disp.size)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_DISPLACEMENT;
segments->segments[segments->count ].offset = instruction->raw.disp.offset;
segments->segments[segments->count++].size = instruction->raw.disp.size / 8;
}
// Immediates
for (ZyanU8 i = 0; i < 2; ++i)
{
if (instruction->raw.imm[i].size)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_IMMEDIATE;
segments->segments[segments->count ].offset = instruction->raw.imm[i].offset;
segments->segments[segments->count++].size = instruction->raw.imm[i].size / 8;
}
}
if (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{
segments->segments[segments->count].type = ZYDIS_INSTR_SEGMENT_OPCODE;
segments->segments[segments->count].offset = instruction->length -1;
segments->segments[segments->count++].size = 1;
}
return ZYAN_STATUS_SUCCESS;
}
|
/* ---------------------------------------------------------------------------------------------- */
/* Instruction segments */
/* ---------------------------------------------------------------------------------------------- */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Utils.c#L175-L319
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ZydisGetVersion
|
ZyanU64 ZydisGetVersion(void)
{
return ZYDIS_VERSION;
}
|
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/src/Zydis.c#L33-L36
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
main
|
int main(int argc, char** argv)
{
if (ZydisGetVersion() != ZYDIS_VERSION)
{
ZYAN_FPUTS("Invalid zydis version\n", ZYAN_STDERR);
return EXIT_FAILURE;
}
if (argc < 2 || argc > 3)
{
ZYAN_FPRINTF(ZYAN_STDERR, "Usage: %s -[real|16|32|64] [input file]\n",
(argc > 0 ? argv[0] : "ZydisDisasm"));
return EXIT_FAILURE;
}
ZydisDecoder decoder;
if (!ZYAN_STRCMP(argv[1], "-real"))
{
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_REAL_16, ZYDIS_STACK_WIDTH_16);
} else
if (!ZYAN_STRCMP(argv[1], "-16"))
{
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_COMPAT_16, ZYDIS_STACK_WIDTH_16);
} else
if (!ZYAN_STRCMP(argv[1], "-32"))
{
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_COMPAT_32, ZYDIS_STACK_WIDTH_32);
} else
if (!ZYAN_STRCMP(argv[1], "-64"))
{
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
} else
{
ZYAN_FPRINTF(ZYAN_STDERR, "Usage: %s -[real|16|32|64] [input file]\n",
(argc > 0 ? argv[0] : "ZydisDisasm"));
return EXIT_FAILURE;
}
FILE* file = (argc >= 3) ? fopen(argv[2], "rb") : ZYAN_STDIN;
if (!file)
{
ZYAN_FPRINTF(ZYAN_STDERR, "Can not open file: %s\n", strerror(ZYAN_ERRNO));
return EXIT_FAILURE;
}
#ifdef ZYAN_WINDOWS
// The `stdin` pipe uses text-mode on Windows platforms by default. We need it to be opened in
// binary mode
if (file == ZYAN_STDIN)
{
(void)_setmode(_fileno(ZYAN_STDIN), _O_BINARY);
}
#endif
ZydisFormatter formatter;
if (!ZYAN_SUCCESS(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL)) ||
!ZYAN_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_PROP_FORCE_SEGMENT, ZYAN_TRUE)) ||
!ZYAN_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_PROP_FORCE_SIZE, ZYAN_TRUE)))
{
ZYAN_FPUTS("Failed to initialized instruction-formatter\n", ZYAN_STDERR);
return EXIT_FAILURE;
}
ZyanU8 buffer[1024];
ZyanUSize buffer_size;
ZyanUSize buffer_remaining = 0;
ZyanUSize read_offset_base = 0;
do
{
buffer_size = fread(buffer + buffer_remaining, 1, sizeof(buffer) - buffer_remaining, file);
if (buffer_size != (sizeof(buffer) - buffer_remaining))
{
if (ferror(file))
{
return EXIT_FAILURE;
}
ZYAN_ASSERT(feof(file));
}
buffer_size += buffer_remaining;
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status;
ZyanUSize read_offset = 0;
char format_buffer[256];
while ((status = ZydisDecoderDecodeFull(&decoder, buffer + read_offset,
buffer_size - read_offset, &instruction, operands, ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY)) != ZYDIS_STATUS_NO_MORE_DATA)
{
const ZyanU64 runtime_address = read_offset_base + read_offset;
if (!ZYAN_SUCCESS(status))
{
ZYAN_PRINTF("db %02X\n", buffer[read_offset++]);
continue;
}
ZydisFormatterFormatInstruction(&formatter, &instruction, operands,
instruction.operand_count_visible, format_buffer, sizeof(format_buffer),
runtime_address);
ZYAN_PUTS(format_buffer);
read_offset += instruction.length;
}
buffer_remaining = 0;
if (read_offset < sizeof(buffer))
{
buffer_remaining = sizeof(buffer) - read_offset;
memmove(buffer, buffer + read_offset, buffer_remaining);
}
read_offset_base += read_offset;
} while (buffer_size == sizeof(buffer));
return EXIT_SUCCESS;
}
|
/* ============================================================================================== */
/* Entry point */
/* ============================================================================================== */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/dependencies/zydis/tools/ZydisDisasm.c#L46-L163
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.