Skip to content

Commit d2b3d6d

Browse files
committed
Very weird things happen...
1 parent 69d2afb commit d2b3d6d

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

ANDN.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int __stdcall ANDNInstructionEmulator(
77
{
88
UNREFERENCED_PARAMETER(instruction);
99
UNREFERENCED_PARAMETER(context);
10-
10+
1111
unsigned int src1 = getRegValue(instruction.src1, context);
1212
unsigned int src2;
1313
if (instruction.src2 == MEM_32)

Driver.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ extern void setRegValue(
5353

5454
/*** Handlers.c ***/
5555

56-
extern int __stdcall HandleUndefInstruction(char** instruction, CALLER_CONTEXT* context);
56+
extern int __stdcall HandleUndefInstruction(unsigned char** instruction, CALLER_CONTEXT* context);
5757

5858
#pragma pack(pop)

Handlers.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
/* Main instruction handler */
66

7-
int __stdcall HandleUndefInstruction(char** instruction, CALLER_CONTEXT* context)
7+
int __stdcall HandleUndefInstruction(unsigned char** instruction, CALLER_CONTEXT* context)
88
{
9+
UNREFERENCED_PARAMETER(instruction);
10+
UNREFERENCED_PARAMETER(context);
911
ParsedInstruction parsedInstruction = parse(*instruction);
1012
EmulatorRoutine routine;
1113
// Choose routine depending on the instruction type returned by parser

Parser.c

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "Parser.h"
2+
#include <stdio.h>
23

34
//
4-
inline int getPrefix(char* instruction)
5+
inline int getPrefix(unsigned char* instruction)
56
{
67

78
//Check for 0x66 prefix
@@ -11,10 +12,10 @@ inline int getPrefix(char* instruction)
1112
}
1213

1314
//sets the instr_args.type and returns the length of instruction opcode
14-
int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction* instr_args)
15+
int decodeInstructionType(unsigned char* instruction, int prefixOffset, ParsedInstruction* instr_args)
1516
{
1617
//Check for VEX-coded instructions
17-
if (instruction[0] == 0xC4)
18+
if ((instruction[0] & 0xFF) == 0xC4)
1819
{
1920
//second byte check - mandatory 11x on the front and 00010 on the back
2021
if ((instruction[1] & 0b11011111) != 0b11000010)
@@ -24,25 +25,25 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
2425
return 0;
2526
}
2627
//third byte check - mandatory 0xxxx0xx
27-
if ((instruction[2]) != 0)
28+
if ((instruction[2] & 0b10000100) != 0)
2829
{
2930
instr_args->type = INSTR_UNKNOWN;
3031
instr_args->length = 0;
3132
return 0;
3233
}
3334

3435
//fourth byte check
35-
if (instruction[3] == 0xF2)
36+
if ((instruction[3] & 0xFF) == 0xF2)
3637
{
3738
instr_args->type = INSTR_ANDN;
3839
return 4;
3940
}
40-
else if (instruction[3] == 0xF7)
41+
else if ((instruction[3] & 0xFF) == 0xF7)
4142
{
4243
instr_args->type = INSTR_BEXTR;
4344
return 4;
4445
}
45-
else if (instruction[3] == 0xF3)
46+
else if ((instruction[3] & 0xFF) == 0xF3)
4647
{
4748
//check reg value of ModR/M
4849
if ((instruction[4] & 0b00111000) >> 3 == 3)
@@ -76,25 +77,25 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
7677
}
7778

7879
//check for mandatory prefixes of non-vex instructions
79-
if (instruction[0 + prefixOffset] != 0xF3 || instruction[1 + prefixOffset] != 0x0F)
80+
if ((instruction[0 + prefixOffset] & 0xFF) != 0xF3 || (instruction[1 + prefixOffset] & 0xFF) != 0x0F)
8081
{
8182
instr_args->type = INSTR_UNKNOWN;
8283
instr_args->length = 0;
8384
return 0;
8485
}
8586

8687
//decode non-vex instruction type
87-
if (instruction[2 + prefixOffset] == 0xB8)
88+
if ((instruction[2 + prefixOffset] & 0xFF) == 0xB8)
8889
{
8990
instr_args->type = INSTR_POPCNT;
9091
return 3;
9192
}
92-
else if (instruction[2 + prefixOffset] == 0xBC)
93+
else if ((instruction[2 + prefixOffset] & 0xFF) == 0xBC)
9394
{
9495
instr_args->type = INSTR_LZCNT;
9596
return 3;
9697
}
97-
else if (instruction[2 + prefixOffset] == 0xBD)
98+
else if ((instruction[2 + prefixOffset] & 0xFF) == 0xBD)
9899
{
99100
instr_args->type = INSTR_TZCNT;
100101
return 3;
@@ -104,7 +105,7 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
104105
return 0;
105106
}
106107

107-
void decodeInstruction(char* instruction, int offset, int op16bit, ParsedInstruction* instr_args)
108+
void decodeInstruction(unsigned char* instruction, int offset, int op16bit, ParsedInstruction* instr_args)
108109
{
109110
unsigned char mod = instruction[offset] >> 6;
110111
unsigned char reg = (instruction[offset] & 0b00111000) >> 3;
@@ -222,7 +223,7 @@ void decodeInstruction(char* instruction, int offset, int op16bit, ParsedInstruc
222223
}
223224
}
224225

225-
ParsedInstruction parse(char* instruction)
226+
ParsedInstruction parse(unsigned char* instruction)
226227
{
227228
ParsedInstruction instr_args;
228229
instr_args.mem.base = UNDEF;

Parser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121
} mem;
2222
} ParsedInstruction;
2323

24-
extern ParsedInstruction parse(char* instruction);
24+
extern ParsedInstruction parse(unsigned char* instruction);
2525

2626
extern void* getEffectiveVA(
2727
struct MemoryArgument mem,

0 commit comments

Comments
 (0)