metaworm/luac-parser-rs

Incorrect and-or construct decompilation

Opened this issue · 3 comments

When given the following script:

local fenv = getfenv and getfenv() or _ENV
print(fenv._VERSION)

Metaworm's luadec fails to identify the and-or construct, and instead produces this code:

local r0_0 = getfenv
if r0_0 then
  r0_0 = bozo or _ENV
else
  goto label_6	-- block#2 is visited secondly
end
print(r0_0._VERSION)

You found the key problem, yes, the decompilation of and-or statements is a big problem, lua compiles the "and/or" operators as conditional jumps instead of handling them with special instructions, and in many cases the decompilation fails for statements containing "and/or", thus showing the original conditional jumps as they are, i.e., if-else, which is what I've been thinking about , how to restore and-or statements more accurately

@metaworm I've had the same problem when writing my decompiler and I've managed to find a perfect solution.
Go check out this paper, specifically the part where they build a control flow graph and then search for "boolean atoms".
It is meant for java but with a few adjustments it also works for lua/luajit bytecode.

@marsinator358 Thank you for your advice. The handling of this part is really a little complicated. I will try to improve it when I have a long time.