fkie-cad / dewolf

A research decompiler implemented as a Binary Ninja plugin.
GNU Lesser General Public License v2.1
162 stars 9 forks source link

Fix ordering of inserted common subexpressions #403

Closed rihi closed 2 months ago

rihi commented 3 months ago

The common subexpression elimination stage currently has a bug, where if multiple eliminated subexpressions depend on each other are inserted in the wrong order into a basic block.

This can result in transformation like this:

print("%d", a * 2);
print("%d", a * 2 + 5);
print("%d", a * 2 + 5);

int c0 = c1 + 5;
int c1 = a * 2;
print("%d", c0);
print("%d", c0);
print("%d", c1);

This is caused by us caching the index of each instruction in the CfgInstruction class and not updating it after inserting new definitions. This PR simply changes this so that the index is always calculated instead of being cached. While theoretically this has worse performance, the extend tests runtime only increased from 10:42 to 11:13 on my machine.