Vector35 / Vector35/binaryninja-api

HLIL drops zx when folding byte-width arithmetic, producing an incorrect expression

Open
#8,513 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1.3k
Forks
298
Avg merge
5d 5h
Merged PRs (30d)
19

Description

Version and Platform (required):

  • Binary Ninja Version: Version 5.0.7648
  • Edition: Non-Commercial
  • OS: Ubuntu
  • OS Version: 24.04
  • CPU Architecture: x64

Bug Description:
HLIL discards zero- and sign-extensions when folding expressions, producing arithmetic that is numerically incorrect.

MLIL correctly models a narrowing step as its own statement, which justifies reducing a constant modulo the narrower width. HLIL folds the chain into a single expression and drops the extension, but keeps the reduced constant — so the reduction is no longer valid.

Expected Behavior:
target_zx should be

uint64_t target_zx(int32_t arg1, int32_t arg2) {
  return (uint8_t)(arg1 - 0x2402) + arg2 - 0x2402;
}

Example where the behaviour is wrong:
If arg1=0x2405 and arg2=0x2405, arg1 - 2 + arg2 - 0x2402 = 0x2406 whereas the expected returned value is 0x6.

How to reproduce:
Open x86_zx_drop or recompile x86_zx_drop.c and look at the functions target_*.

Screenshots/Video Recording:
Image

Binary:
x86_zx_drop.zip

Additional Information:
Reproduces with zx and sx, and whether the extension comes from a byte-width instruction (movzx/movsx, MSP430 .b forms) or from a mask that MLIL canonicalises into an extension (and #0xff). Multiple extensions in one folded chain are all dropped.

Also reproduces on the current Binary Ninja build via Decompiler Explorer (dogbolt.org), so this is not specific to 5.0.7648.

Possibly related to #6371 (also HLIL and extension handling), though the symptom differs.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open or recompile x86_zx_drop.c and inspect the target_* functions in both MLIL and HLIL, focusing on the zero- and sign-extension steps described in the report. Compare the generated HLIL with the expected target_zx expression and verify that zx and sx remain effective after folding, including the supplied argument values.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, reverse-engineering
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.