Vector35 / binaryninja-api

Public API, examples, documentation and issues for Binary Ninja
https://binary.ninja/
MIT License
820 stars 186 forks source link

Simplify common conditional expression #5074

Open plafosse opened 2 months ago

plafosse commented 2 months ago

I'm seeing a lot of this type of condition repeated that should be easy to simplify

(!A || (A && B)) Which simplifies down to just !A || B Similarly (A || (!A && B)) => A || B

plafosse commented 2 months ago

BNDB Shared internally cascade blueprint lantern mosaic has the following conditional at: 100002f37

image
D0ntPanic commented 1 week ago

Added in 4.1.5232

plafosse commented 3 days ago

Looks like the demorgan's variant of this case is still not handled

image

180022bbc In the unicorn chess taco rainbow binary

plafosse commented 3 days ago

So to clarify: The original equation if ((((rax_69 == 0x8007007a || rax_69 == 0x80020013) && rax_55 s>= 0) || (rax_69 != 0x8007007a && rax_69 != 0x80020013)) && rdi s< 0) A = (rax_69 == 0x8007007a || rax_69 == 0x80020013) !A = (rax_69 != 0x8007007a && rax_69 != 0x80020013) B = rax_55 s>= 0 C = rdi s< 0 This boils down to: ((A && B) || !A) && C We dont' care about C so we can drop it and rewrite the equation ((A && B) || !A) which can convert to (B || !A)

The final equation can be simplified down to:

if ((rax_55 s>= 0 || (rax_69 != 0x8007007a && rax_69 != 0x80020013)) && rdi s< 0)