Skip to content

Commit

Permalink
Encoder: rename operand_count -> operand_count_visible
Browse files Browse the repository at this point in the history
Resolves #458
  • Loading branch information
athre0z committed Oct 15, 2023
1 parent 49fa4db commit 04c092c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
7 changes: 7 additions & 0 deletions assets/porting-guide-v4-v5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Porting Guide v4 -> v5

# Encoder

- `ZydisEncoderDecodedInstructionToEncoderRequest` now expects exactly `instruction->operand_count_visible` to be
passed, not `operand_count_visible` at maximum. Passing a lower value was previously allowed but didn't really
make much sense at all.
16 changes: 9 additions & 7 deletions include/Zydis/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ typedef struct ZydisEncoderRequest_
*/
ZydisOperandSizeHint operand_size_hint;
/**
* The number of instruction-operands.
* The number of visible (explicit) instruction operands.
*
* The encoder does not care about hidden (implicit) operands.
*/
ZyanU8 operand_count;
/**
Expand Down Expand Up @@ -422,11 +424,11 @@ ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstructionAbsolute(ZydisEncoderReques
* Converts decoded instruction to encoder request that can be passed to
* `ZydisEncoderEncodeInstruction`.
*
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operands A pointer to the decoded operands.
* @param operand_count The operand count.
* @param request A pointer to the `ZydisEncoderRequest` struct, that receives
* information necessary for encoder to re-encode the instruction.
* @param instruction A pointer to the `ZydisDecodedInstruction` struct.
* @param operands A pointer to the decoded operands.
* @param operand_count_visible The number of visible instruction operands.
* @param request A pointer to the `ZydisEncoderRequest` struct, that receives
* information necessary for encoder to re-encode the instruction.
*
* This function performs simple structure conversion and does minimal sanity checks on the
* input. There's no guarantee that produced request will be accepted by
Expand All @@ -437,7 +439,7 @@ ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstructionAbsolute(ZydisEncoderReques
*/
ZYDIS_EXPORT ZyanStatus ZydisEncoderDecodedInstructionToEncoderRequest(
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operands,
ZyanU8 operand_count, ZydisEncoderRequest* request);
ZyanU8 operand_count_visible, ZydisEncoderRequest* request);

/**
* Fills provided buffer with `NOP` instructions using longest possible multi-byte instructions.
Expand Down
16 changes: 6 additions & 10 deletions src/Encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -4525,10 +4525,11 @@ ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstructionAbsolute(ZydisEncoderReques
}

ZYDIS_EXPORT ZyanStatus ZydisEncoderDecodedInstructionToEncoderRequest(
const ZydisDecodedInstruction *instruction, const ZydisDecodedOperand* operands,
ZyanU8 operand_count, ZydisEncoderRequest *request)
const ZydisDecodedInstruction *instruction, const ZydisDecodedOperand* operands,
ZyanU8 operand_count_visible, ZydisEncoderRequest *request)
{
if (!instruction || !request || (operand_count && !operands))
if (!instruction || !request || (operand_count_visible && !operands) ||
operand_count_visible != instruction->operand_count_visible)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
Expand Down Expand Up @@ -4635,13 +4636,8 @@ ZYDIS_EXPORT ZyanStatus ZydisEncoderDecodedInstructionToEncoderRequest(
}
request->allowed_encodings = 1 << instruction->encoding;

if ((operand_count > ZYDIS_ENCODER_MAX_OPERANDS) ||
(operand_count > instruction->operand_count_visible))
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
request->operand_count = operand_count;
for (ZyanU8 i = 0; i < operand_count; ++i)
request->operand_count = operand_count_visible;
for (ZyanU8 i = 0; i < operand_count_visible; ++i)
{
const ZydisDecodedOperand *dec_op = &operands[i];
ZydisEncoderOperand *enc_op = &request->operands[i];
Expand Down

0 comments on commit 04c092c

Please sign in to comment.