jjdredd / ljd

LuaJIT raw-bytecode decompiler
MIT License
10 stars 7 forks source link

simple upvalue decompile incrrect #6

Open kernus opened 9 years ago

kernus commented 9 years ago

lua code:

local b = 2 function add() c = 3 return b + c end print(add())

decompiled:

slot0 = 2 function add() c = 3 return 3 + c end print(add()) return

with bugfix-locals branch, ljd crashed: Traceback (most recent call last): File "main.py", line 123, in retval = main() File "main.py", line 104, in main ljd.ast.unwarper.unwarp(ast) File "/Users/../ljd/ast/unwarper.py", line 38, in unwarp _glue_flows(node) File "/Users/../ljd/ast/unwarper.py", line 61, in _glue_flows assert isinstance(blocks[-1].warp, nodes.EndWarp) AttributeError: 'Return' object has no attribute 'warp'

jjdredd commented 9 years ago

yes, bugfix-locals is bugged, please use bugfix or master.

kernus commented 9 years ago

well, does this upvalue thing hard to fix? I get the bytecode parsed like below:

1b 4c 4a 01 02

23 00 flags: no ffi, no vararg, no child 00 argsize 02 framesize 01 upvalues number 01 00 06 instruction count 27 00 03 00 KSHORT 0 3 35 00 00 00 GSET 0 0 ; "c" 2b 00 00 00 UGET 0 0 ; b 34 01 00 00 GGET 1 0 ; "c" 1e 00 01 00 ADDVV 0 0 1 48 00 02 00 RET1 0 2

00 c0 upvalue refs 06 63 c

36 03 flags: no ffi, vararg, has child 00 03 00 kup 03 kgc 00 knum 09 instruction number 27 00 02 00 KSHORT 0 2 31 01 00 00 FNEW 1 0 ; number.lua:3 35 01 01 00 GSET 1 1 ; "add" 34 01 02 00 GGET 1 2 ; "print" 34 02 01 00 GGET 2 1 ; "add" 3e 02 01 00 CALL 2 0 1 3d 01 00 01 CALLM 1 1 0 30 00 00 80 UCLO 0 0009 47 00 01 00 RET0 0 1

0a 70 72 69 6e 74 print 08 61 64 64 add 00 child

00 eof

hard to run it in mind.

jjdredd commented 9 years ago

It's hard for me to fix, the easiest solution here is to use a less bugged branch like master or bugfix. Why did you chose to use bugfix-locals anyway?

kernus commented 9 years ago

With master, I get: slot0 = 2 function add() c = 3 return 3 + c end print(add()) return

the upvalue parsing is wrong.

As for bugfix-locals, I just thought it's latest enough, lol.

Really hope this project is active, I have reading it for some weeks, it's interesting, however, I have little experience with luajit or ljd.

the upvalue ref in add function is 00 c0, how can it refs to b = 2 (i.e. KSHORT 0 2?)? googled it for whole day, just no useful ideas.

jjdredd commented 9 years ago

As for bugfix-locals, I just thought it's latest enough, lol.

It's kinda experimental. As you can see I have made several lame attempts to fix the locals/slotworks, but I don't understand the ljd algorithm very well and don't have much time to work with it.

Really hope this project is active

I'm sorry, it's not. the original author abandoned it and I don't think I can fix this. IDK if it's easier to rewrite the whole thing or try to fix it.

If you ever return to irc, be sure to ping me (Judge_Dredd). EDIT: you can find some discussions withe the original author in the original repo's issues on github: https://github.com/NightNord/ljd/issues

kernus commented 9 years ago

Another found, scripts below works, just remove local c

local b = 2

function add() return b end

print(add())

decompiled

slot0 = 2

function add() return slot0 end

print(add())

Guess the slot0 within add function stands for the original c variable, not the outside b slot0, will check it tomorrow.