Skip to content

Fix Z3 sort mismatch when dereferencing an integer-based address - #2

Open
edmcman wants to merge 1 commit into
mainfrom
fix-integer-address-base-sort-mismatch
Open

edmcman wants to merge 1 commit into
mainfrom
fix-integer-address-base-sort-mismatch

Conversation

@edmcman

@edmcman edmcman commented Oct 26, 2026

Copy link
Copy Markdown

Problem

With the default representation (bitvector integers, mathematical-integer pointers), dereferencing an address built from an integer crashed:

execute("long f(void) { return *(long *)(get_base() + 8); }")
# z3.z3types.Z3Exception: sort mismatch

AddressableValue.compute_offset recovered the offset by subtracting the base symbol from the address expression. A cast that changes the z3 representation wraps the whole sum (BV2Int(a + 8) - a), so the subtraction mixed sorts, or could not cancel even when sorts matched. The same crash hit ordinary decompiler code such as *(long *)(a1 + 8) with an __int64 parameter, and the round trip (long *)((unsigned long)p + 8) failed with "Could not remove base address".

Changes

  • Carry offsets across casts. A cast that changes representation records the offset converted as a signed displacement; the address expression itself stays exact, so value comparisons are unaffected. The offset is only carried when the cast preserves the base's own value, so truncating casts raise an ExecutionError on dereference instead of being silently accepted.
  • Refuse addresses that may have wrapped. In bitvector mode, an address computed in an integer narrower than a pointer is refused on dereference: base + offset may have wrapped at the narrow width, and assuming otherwise produced false "equivalent" verdicts (e.g. unsigned a; *(long *)(a + 8) vs *(long *)((unsigned long)a + 8)). Integer mode, which never models wraparound, still accepts these.
  • Signedness-independent integer-to-pointer casts. In the default mode the cast kept the integer's sign, so (void *)-1L and (void *)0xffffffffffffffffUL were different addresses. The cast now extends by the integer's signedness and reads the bits as an unsigned address, matching the bitvector-pointer representation. Pointer-arithmetic indices use a separate pointer_index conversion that keeps their sign (p - (-1) is p + 1).
  • Inference. The pointer-cast byte-base rule now also treats the result of an undeclared call used as (T *)(call() + bytes) as void *, as it already did for untyped globals. Explicitly declared integer returns keep their width.

Known limitation (unchanged)

The motivating dataset pair (search_function from demmys/desk) is equivalent in integer mode, with both negative controls (next-pointer offset 48→40, return 0LL) rejected. In bitvector mode it no longer crashes but is reported nonequivalent: resolve_to_compatible_z3_repr never pairs a pointer symbol with an integer symbol under mixed representations, so the original's fd is never matched to the decompiled __int64 i. That is a separate change.

Testing

  • Regression tests for the minimal reproducer, explicit integer bases in both modes, rejection of narrow and truncated bases, signed/unsigned pointer equivalence, negative indices, call-result inference, and the dataset pair with its negative controls.
  • Full suite passes (327 tests), excluding the known hanging test_differing_break_statements_results_in_nonequivalent_phis.

AddressableValue.compute_offset recovered the offset by subtracting the base
symbol from the address expression. A cast that changes the z3 representation
(e.g. a bitvector integer to a mathematical-integer pointer) wraps the whole
sum, so the subtraction either mixed sorts and crashed, or could not cancel.
This affected *(long *)(get_base() + 8) as well as plain integer parameters
like *(long *)(a1 + 8).

- Casts that change representation now carry the offset across as a signed
  displacement, keeping the address expression exact. The offset is only
  carried when the base's own value is preserved by the cast.
- Addresses computed in an integer narrower than a pointer are refused on
  dereference in bitvector mode, since base + offset may have wrapped at the
  narrow width.
- Integer-to-pointer casts now extend by the integer's signedness and read the
  bits as an unsigned address, matching the bitvector pointer representation,
  so signed and unsigned pointer-width integers give the same address.
  Pointer-arithmetic indices use a separate pointer_index conversion that
  keeps their sign.
- Type inference treats the result of an undeclared call used as
  (T *)(call() + bytes) as void *, as it already did for untyped globals.
@edmcman

edmcman commented Oct 26, 2026

Copy link
Copy Markdown
Author

This is completely vibe coded. Use at your own risk.

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.

1 participant