Skip to content

target/i386: Reject ModRM.reg == 7 early when decoding 0xFE/0xFF (group 4 and 5) instructions - #2362

Open
unknown-1-0 wants to merge 1 commit into
unicorn-engine:devfrom
unknown-1-0:i386-fix-group-4-and-5-decoding
Open

target/i386: Reject ModRM.reg == 7 early when decoding 0xFE/0xFF (group 4 and 5) instructions#2362
unknown-1-0 wants to merge 1 commit into
unicorn-engine:devfrom
unknown-1-0:i386-fix-group-4-and-5-decoding

Conversation

@unknown-1-0

Copy link
Copy Markdown

According to both table A-6 in Volume 2 of Intel 64 and IA-32 Architectures Software Developer's Manual and table A-6 in Volume 3 of AMD64 Architecture Programmer's Manual, the opcode for ModRM.reg == 7 is reserved for both group 4 (0xFE) and 5 (0xFF) instructions.

Although Unicorn already rejects ModRM.reg == 7 early with group 4 (0xFE) instructions, that is not the case with group 5 (0xFF) instructions. The latter case is eventually handled in the default: block of switch(op), but by the time that code is reached, some micro-ops are already generated. These may include a memory load micro-op. As a result, an invalid memory access error may be produced instead of an expected invalid instruction error.

Fix this bug by rejecting group 4 and 5 instructions with ModRM.reg == 7 before generating any micro-operations.

@wtdcode

wtdcode commented Jul 7, 2026

Copy link
Copy Markdown
Member

For such instruction semantic related fixes, a unit test could greatly speed up my reviewing process because I can debug and see the difference. Thanks in advance if you wish adding one.

@unknown-1-0

unknown-1-0 commented Jul 7, 2026

Copy link
Copy Markdown
Author

Noted. Working on it

@unknown-1-0
unknown-1-0 force-pushed the i386-fix-group-4-and-5-decoding branch from 1368440 to be8dc80 Compare July 7, 2026 13:43
@unknown-1-0

Copy link
Copy Markdown
Author

Added the unit test

@unknown-1-0

Copy link
Copy Markdown
Author

I see there's a merge conflict in test_x86.c across my open PRs. Please let me know when you are ready to merge each PR, and I'll resolve the conflicts one by one.

…up 4 and 5) instructions

According to both table A-6 in Volume 2 of Intel 64 and IA-32 Architectures
Software Developer's Manual and table A-6 in Volume 3 of AMD64 Architecture
Programmer's Manual, the opcode for ModRM.reg == 7 is reserved for both group 4
(0xFE) and 5 (0xFF) instructions.

Although Unicorn already rejects ModRM.reg == 7 early with group 4 (0xFE)
instructions, that is not the case with group 5 (0xFF) instructions.
The latter case is eventually handled in the `default:` block of `switch(op)`,
but by the time that code is reached, some micro-ops are already generated.
These may include a memory load micro-op. As a result, an invalid memory access
error may be produced instead of an expected invalid instruction error.

Fix this bug by rejecting group 4 and 5 instructions with ModRM.reg == 7
before generating any micro-operations.

Signed-off-by: Andrey Polivoda <apolivodaa433@gmail.com>
@unknown-1-0
unknown-1-0 force-pushed the i386-fix-group-4-and-5-decoding branch from be8dc80 to 8200be4 Compare July 22, 2026 16:33
@unknown-1-0

Copy link
Copy Markdown
Author

Resolved the conflict. Moved the test between existing ones to prevent future conflicts. Also the commit is signed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants