Skip to content

x86: fix operand and immediate sizes and 16-bit decoding - #3170

Closed
ricardojrdez wants to merge 6 commits into
capstone-engine:nextfrom
reverseame:x86-imm-16bit-fixes
Closed

ricardojrdez wants to merge 6 commits into
capstone-engine:nextfrom
reverseame:x86-imm-16bit-fixes

Conversation

@ricardojrdez

@ricardojrdez ricardojrdez commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Your checklist for this pull request

  • I've documented or updated the documentation of every API function and
    struct this PR changes.
  • I've added tests that prove my fix is effective or that my feature works
    (if possible)

Detailed description

  • 01 -- x86: fix PUSH and ENTER immediate sizes
    • PUSH imm8 (6A) in 32-bit mode was sign-extending the immediate to 64 bits (0xffffffffffffffff instead of 0xffffffff).
    • ENTER (C8) printed both immediates as signed with size 4/4. They are now unsigned (enter 0xfefe, 0xfe) with actual sizes of 2 and 1 bytes.
  • 02 -- x86: honor mandatory 66 prefix in 16-bit mode
    • 0x66 in 16-bit mode was only handled as an operand-size override. It is now also recognized as a mandatory opcode prefix where required, while keeping its override behavior for GP instructions like bswap.
  • 03 -- x86: fix XSAVE and PTWRITE decoding in 16-bit mode
    • Reject 66 0f ae 20 in 16-bit mode (and 67 66 0f ae 20 in 32/64-bit) instead of decoding it as xsave, and decode f3 0f ae 20 as ptwrite dword ptr [...].
  • 04 -- x86: fix far JMP and CALL immediate sizes
    • imm_size was left uninitialized on direct far JMP (EA) and CALL (9A), and the offset size was always reported as 4 bytes. imm_size is now set to 2 (selector size) and the offset size is 2 bytes when applicable (16-bit mode without prefix, or 32-bit mode with 0x66).
  • 05 -- x86: fix hint NOP and CMOV operand sizes
    • Accept reserved hint NOPs across the full 0F 18–0F 1E range (including register forms and cldemote with 66/F2/F3). Also prevent ignored F2/F3 prefixes in 16-bit mode from changing operand sizes (e.g., on CMOVcc).

Test plan

Added test cases to tests/details/x86.yaml and tests/issues/x86-mandatory-prefixes.yaml covering:

  • 6a ff (32-bit) and c8 fe fe fe immediate values and sizes.
  • 66 0f 2f 00 (comisd) and 66 0f ef c0 (pxor) in 16-bit mode.
  • Invalid 66 0f ae 20 / 67 66 0f ae 20 rejection and f3 0f ae 20 (ptwrite) in 16-bit mode.
  • Far JMP/CALL (EA/9A) imm_size and offset sizes in 16-bit and 32-bit modes (with and without 0x66).
  • 0F 18–0F 1E hint NOPs (0f 1c 08, 66 0f 1c 00) and 0xF3-prefixed CMOVO in 16-bit mode.

Run with cstest tests/.

Closing issues

N/A

- 6a ff (32, unsigned): the sign-extended imm8 of PUSH is now masked to
  the 32-bit operand size when printed, giving 0xffffffff instead of
  0xffffffffffffffff.
- c8 fe fe fe (32): ENTER immediates are no longer sign-extended; they
  now print as 0xfefe and 0xfe, with sizes 2 and 1 instead of 4 and 4.
- 66 0f 2f 00 (16): the 66 prefix is now taken as the mandatory prefix
  in 16-bit mode when it selects another instruction, so this decodes as
  COMISD with a qword operand instead of COMISS.
- 66 0f ef c0 (16): for the same reason, PXOR now uses the SSE2 xmm
  registers instead of the MMX ones.
- 66 0f ae 20 (16): XSAVE has no 66-prefixed form, so this encoding is
  now rejected as invalid.
- f3 0f ae 20 (16): PTWRITE was rejected in 16-bit mode; it now decodes
  with a dword memory operand.
- 66 ea 11 22 33 44 (32): far JMP with an operand-size prefix now
  reports imm_size 2 and a 2-byte offset, instead of an uninitialized
  imm_size and a 4-byte offset.
- 0f 1c 08 (32): 0F 1C /1 is a reserved hint NOP and now decodes as nop
  dword ptr [eax] instead of being rejected.
- 66 0f 1c 00 (16): CLDEMOTE takes no 66 prefix, and in 16-bit mode the
  66 prefix selects a 32-bit operand, so the hint NOP operand is now
  dword instead of word.
- f3 0f 40 00 (16): the F3 prefix no longer changes the operand size of
  CMOVO, which is word again, the 16-bit default.

@Rot127 Rot127 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not allowed generate the PR description with AI. Please update it.

@hainest If you have a minute, would you mind taking a look please?

SStream_concat(O, "$0x%" PRIx64, imm);
else
SStream_concat(O, "$%" PRIu64, imm);
break;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an API for printing integers. Use that one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely. I'll wait until after the tests are all green.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.38462% with 16 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (next@a71a4e5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
arch/X86/X86DisassemblerDecoder.c 75.00% 3 Missing and 8 partials ⚠️
arch/X86/X86ATTInstPrinter.c 70.00% 2 Missing and 1 partial ⚠️
arch/X86/X86IntelInstPrinter.c 81.81% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             next    #3170   +/-   ##
=======================================
  Coverage        ?   61.67%           
=======================================
  Files           ?      243           
  Lines           ?    56894           
  Branches        ?    10127           
=======================================
  Hits            ?    35091           
  Misses          ?    16593           
  Partials        ?     5210           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Same here, do you want me to split this in smaller PRs? Perhaps they are easy to review individually

@Rot127

Rot127 commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Yes please.

@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Closing this in favor of smaller PRs:

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

Labels

X86 Arch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants