Skip to content

Commit 147a375

Browse files
authored
Removed xsha1/xsha256 workaround (#537)
* Removed `xsha1`/`xsha256` workaround * Reworked `REX2` handling
1 parent 25eb33c commit 147a375

File tree

4 files changed

+9878
-9921
lines changed

4 files changed

+9878
-9921
lines changed

include/Zydis/Internal/EncoderData.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ typedef enum ZydisSizeHint_
112112
ZYDIS_SIZE_HINT_REQUIRED_BITS = ZYAN_BITS_TO_REPRESENT(ZYDIS_SIZE_HINT_MAX_VALUE)
113113
} ZydisSizeHint;
114114

115+
/**
116+
* Used in encoder's table to indicate `REX2` prefix support type.
117+
*/
118+
typedef enum ZydisRex2Type_
119+
{
120+
ZYDIS_REX2_TYPE_FORBIDDEN,
121+
ZYDIS_REX2_TYPE_ALLOWED,
122+
ZYDIS_REX2_TYPE_MANDATORY,
123+
124+
/**
125+
* Maximum value of this enum.
126+
*/
127+
ZYDIS_REX2_TYPE_MAX_VALUE = ZYDIS_REX2_TYPE_MANDATORY,
128+
/**
129+
* The minimum number of bits required to represent all values of this enum.
130+
*/
131+
ZYDIS_REX2_TYPE_REQUIRED_BITS = ZYAN_BITS_TO_REPRESENT(ZYDIS_REX2_TYPE_MAX_VALUE)
132+
} ZydisRex2Type;
133+
115134
/**
116135
* Used in encoder's primary lookup table which allows to access a set of instruction definitions
117136
* for specified mnemonic in constant time.
@@ -183,7 +202,7 @@ typedef struct ZydisEncodableInstruction_
183202
/**
184203
* True if `REX2` prefix is required for this definition.
185204
*/
186-
ZyanU8 rex2 ZYAN_BITFIELD(1);
205+
ZyanU8 rex2 ZYAN_BITFIELD(ZYDIS_REX2_TYPE_REQUIRED_BITS);
187206
/**
188207
* True if `EEVEX.ND` is required for this definition.
189208
*/

src/Encoder.c

+3-57
Original file line numberDiff line numberDiff line change
@@ -721,59 +721,6 @@ static ZyanBool ZydisCheckAsz(ZydisEncoderInstructionMatch *match, ZydisRegister
721721
return match->easz == (ZyanU8)reg_width ? ZYAN_TRUE : ZYAN_FALSE;
722722
}
723723

724-
/**
725-
* Checks if opcode is allowed to use `REX2` prefix.
726-
*
727-
* @param match A pointer to `ZydisEncoderInstructionMatch` struct.
728-
*
729-
* @return True if `REX2` prefix can be used, false otherwise.
730-
*/
731-
static ZyanBool ZydisIsRex2Allowed(ZydisEncoderInstructionMatch *match)
732-
{
733-
// TODO: Remove this function, use `no_rex2` filter
734-
const ZydisOpcodeMap opcode_map = match->definition->opcode_map;
735-
if ((opcode_map != ZYDIS_OPCODE_MAP_DEFAULT) &&
736-
(opcode_map != ZYDIS_OPCODE_MAP_0F))
737-
{
738-
return ZYAN_FALSE;
739-
}
740-
switch (match->request->mnemonic)
741-
{
742-
case ZYDIS_MNEMONIC_XRSTOR:
743-
case ZYDIS_MNEMONIC_XRSTOR64:
744-
case ZYDIS_MNEMONIC_XRSTORS:
745-
case ZYDIS_MNEMONIC_XRSTORS64:
746-
case ZYDIS_MNEMONIC_XSAVE:
747-
case ZYDIS_MNEMONIC_XSAVE64:
748-
case ZYDIS_MNEMONIC_XSAVEC:
749-
case ZYDIS_MNEMONIC_XSAVEC64:
750-
case ZYDIS_MNEMONIC_XSAVEOPT:
751-
case ZYDIS_MNEMONIC_XSAVEOPT64:
752-
case ZYDIS_MNEMONIC_XSAVES:
753-
case ZYDIS_MNEMONIC_XSAVES64:
754-
return ZYAN_FALSE;
755-
default:
756-
break;
757-
}
758-
static const ZyanBool is_rex2_allowed[2][16] =
759-
{
760-
{
761-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE,
762-
ZYAN_FALSE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_FALSE,
763-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_FALSE, ZYAN_TRUE,
764-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_FALSE, ZYAN_TRUE,
765-
},
766-
{
767-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_FALSE,
768-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE,
769-
ZYAN_FALSE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE,
770-
ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE, ZYAN_TRUE,
771-
},
772-
};
773-
const ZyanU8 row = (match->definition->opcode & 0xF0) >> 4;
774-
return is_rex2_allowed[opcode_map][row];
775-
}
776-
777724
/**
778725
* Returns the id of the specified register as used in physical encoding.
779726
*
@@ -833,7 +780,7 @@ static ZyanBool ZydisIsRegisterAllowed(ZydisEncoderInstructionMatch *match, Zydi
833780
case ZYDIS_REGCLASS_GPR16:
834781
case ZYDIS_REGCLASS_GPR32:
835782
case ZYDIS_REGCLASS_GPR64:
836-
return ZydisIsRex2Allowed(match) ||
783+
return (match->definition->rex2 != ZYDIS_REX2_TYPE_FORBIDDEN) ||
837784
(ZydisGetPhysicalId(reg, reg_class) < 16);
838785
default:
839786
return reg_id < 16;
@@ -989,8 +936,7 @@ static ZyanBool ZydisIsValidAddressingClass(ZydisEncoderInstructionMatch *match,
989936
switch (match->definition->encoding)
990937
{
991938
case ZYDIS_INSTRUCTION_ENCODING_LEGACY:
992-
result &=
993-
ZydisIsRex2Allowed(match);
939+
result &= (match->definition->rex2 != ZYDIS_REX2_TYPE_FORBIDDEN);
994940
break;
995941
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
996942
break;
@@ -4157,7 +4103,7 @@ static ZyanStatus ZydisBuildInstruction(ZydisEncoderInstructionMatch *match,
41574103
{
41584104
instruction->attributes |= ZYDIS_ATTRIB_HAS_MODRM;
41594105
}
4160-
if (match->definition->rex2)
4106+
if (match->definition->rex2 == ZYDIS_REX2_TYPE_MANDATORY)
41614107
{
41624108
instruction->attributes |= ZYDIS_ATTRIB_HAS_REX2;
41634109
}

0 commit comments

Comments
 (0)