Skip to content

Fix AST optimization bug incorrectly eliminating symbolic expressions#1432

Merged
JonathanSalwan merged 1 commit intoJonathanSalwan:dev-v1.0from
JexAmro:fix-bvxor-symbolic-optimization
Nov 20, 2025
Merged

Fix AST optimization bug incorrectly eliminating symbolic expressions#1432
JonathanSalwan merged 1 commit intoJonathanSalwan:dev-v1.0from
JexAmro:fix-bvxor-symbolic-optimization

Conversation

@JexAmro
Copy link

@JexAmro JexAmro commented Nov 19, 2025

AST Optimization Bug

Three AST optimizations (A^A=0, A-A=0, A|A=A) use equalTo() to detect when both operands
are identical. However, equalTo() compares concrete evaluation values rather than AST structure:

bool AbstractNode::equalTo(const SharedAbstractNode& other) const {
  return (this->evaluate() == other->evaluate()) &&
         (this->getBitvectorSize() == other->getBitvectorSize()) &&
         (this->getHash() == other->getHash()) &&
         (this->getLevel() == other->getLevel());
}

This causes different symbolic expressions to be considered "equal" if they happen to have the same
concrete value, hash, size, and level at evaluation time, leading to incorrect symbolic
information elimination.

Affected Operations

Function Optimization Behavior Impact
bvxor A ^ A = 0 Returns concrete 0 Loses ALL symbolic info
bvsub A - A = 0 Returns concrete 0 Loses ALL symbolic info
bvor A | A = A Returns one operand Loses other operand's dependency

Note: bvand already had the correct check (!expr1->isSymbolized() && !expr2->isSymbolized()). This
fix makes the other three operations consistent with that pattern.

Impact

When symbolic expressions are incorrectly replaced with concrete values or have dependencies
removed, downstream symbolic analysis breaks.

Real-World Reproduction (AArch64)

ldr w8, [sp, #4]     ; w8 = symbolic value (happens to evaluate to 0)
ldr w9, [sp, #0xc]   ; w9 = concrete 0
subs w8, w8, w9      ; should set symbolic carry flag
b.lo #target         ; should be symbolized, but isn't

In the cfSub_s() semantics for the carry flag computation:
cf = (MSB(((op1 ^ op2 ^ result) ^ ((op1 ^ result) & (op1 ^ op2))))) ^ 1

When op1 (symbolic, eval=0) is XORed with other expressions that also evaluate to 0, the A^A=0
optimization incorrectly triggers:

  • [bvxor] Optimization A^A=0 triggered
  • [bvxor] expr1 sym=1 eval=0
  • [bvxor] expr2 sym=1 eval=0

Result: The carry flag becomes concrete instead of symbolic, causing the conditional branch b.lo to
not be recognized as symbolized, breaking path exploration.

Solution

Add isSymbolized() checks to ensure these optimizations only apply when both operands are concrete:

/* bvxor - A ^ A = 0 */
if (!expr1->isSymbolized() && !expr2->isSymbolized() && expr1->equalTo(expr2))
  return this->bv(0, expr1->getBitvectorSize());

/* bvsub - A - A = 0 */
if (!expr1->isSymbolized() && !expr2->isSymbolized() && expr1->equalTo(expr2))
  return this->bv(0, expr1->getBitvectorSize());

/* bvor - A | A = A */
if (!expr1->isSymbolized() && !expr2->isSymbolized() && expr1->equalTo(expr2))
  return expr1;

This preserves symbolic information while still allowing optimizations for concrete expressions.

Systematic Fix

This PR fixes a systematic bug across three AST operations. The pattern was:

  • bvand - already correct (had the symbolized checks)
  • bvor - fixed by adding symbolized checks
  • bvsub - fixed by adding symbolized checks
  • bvxor - fixed by adding symbolized checks

All three now follow the same safe pattern as bvand.

Testing

  • Verified fix resolves the AArch64 conditional branch symbolization issue
  • All three optimizations now preserve symbolic information correctly
  • Concrete-only optimization paths still function as expected

@JonathanSalwan
Copy link
Owner

JonathanSalwan commented Nov 20, 2025

Thx a lot for this reporting! Do we have to apply this patch also for these two ones?

  • A | A = A
  • A - A = 0

@JexAmro
Copy link
Author

JexAmro commented Nov 20, 2025

Yes should patch both! I have just tested and confirmed that "A - A = 0" is an identical issue and it occurs a lot in Arch64.
"A | A = A" has less impact but obviously significantly reduces exploration and constraint solving.

@JonathanSalwan
Copy link
Owner

Can you add those two in your patch and then I will merge :)

@JexAmro
Copy link
Author

JexAmro commented Nov 20, 2025

Yes, already planning to do but after testing. Will test and update the patch first thing tomorrow. Thanks for your fast response on this!

Three optimizations (A^A=0, A|A=A, A-A=0) were using equalTo() to
detect identical operands. However, equalTo() compares concrete values
rather than AST structure, causing different symbolic expressions with
the same concrete value to be incorrectly identified as equal.

This caused the optimizer to eliminate symbolic information:
- bvxor: A^A=0 replaced symbolic XOR with concrete 0
- bvsub: A-A=0 replaced symbolic SUB with concrete 0
- bvor: A|A=A returned one operand, losing the other's dependency

Example: In AArch64 cfSub_s(), the carry flag computation would lose
symbolic status when symbolic operands evaluated to 0, breaking
conditional branch symbolization (e.g., b.lo not recognized as symbolic).

The fix adds isSymbolized() checks to ensure these optimizations only
apply when both operands are concrete, preserving symbolic information.

Note: bvand already had the correct check; this fix makes bvor, bvsub,
and bvxor consistent with that pattern.
@JexAmro JexAmro force-pushed the fix-bvxor-symbolic-optimization branch from 1420e2a to a34d106 Compare November 20, 2025 15:25
@JexAmro JexAmro changed the title Fix bvxor A^A=0 optimization incorrectly eliminating symbolic expressions Fix AST optimization bug incorrectly eliminating symbolic expressions Nov 20, 2025
@JexAmro
Copy link
Author

JexAmro commented Nov 20, 2025

Done!

@JonathanSalwan JonathanSalwan merged commit 9ce30cc into JonathanSalwan:dev-v1.0 Nov 20, 2025
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants