marsinator358 / luajit-decompiler-v2 Goto Github PK
View Code? Open in Web Editor NEWLuaJIT bytecode decompiler
License: Other
LuaJIT bytecode decompiler
License: Other
Recently requested for LJD, although it's an enhancement I've been considering for a while: Aussiemon/ljd#14
Lua syntactic sugar allows trailing commas after the last element in a table definition. Always adding this comma means diffs are smaller when new elements are added to the table.
mytable = {
1,
2,
}
It seems cleaner to leave empty function definitions with either a single return or collapse them into a single line, e.g.:
function MissionObjectiveZoneScannable:init(unit)
end
becomes
function MissionObjectiveZoneScannable:init(unit)
return
end
or
function MissionObjectiveZoneScannable:init(unit) return end
This snippet at the top of Lua::write_block
restores the return:
if (!block.size()) {
write_indent();
if (!function.block.size()) {
write("return", NEW_LINE);
} else {
write("-- Nothing", NEW_LINE);
}
return;
}
Could be a decompilation argument instead?
Error in decompiling a certain construct
I use this compiler https://www.blast.hk/threads/17276/
Examples of malfunctions. The foo7 function shows that removing "return" solves the problem
-- source
function foo1()
local data = "text"
data = data:match("(.+)") or data:match("(.+)")
return data
end
function foo2()
local data = 0
data = F(data) or F(data)
return data
end
function foo3()
local data = 0
data = data:F() or data:F()
return data
end
function foo4()
local data = 0
data = data or data:F()
return data
end
function foo5()
local data = 0
data = data:F() or data
return data
end
function foo6()
local data = 0
data = data or data
return data
end
function foo7()
local data = 0
data = F(data) or F(data)
end
-- after compilation and decompilation
function foo1()
local var_1_0 = ("text"):match("(.+)") or :match("(.+)")
return var_1_0
end
function foo2()
local var_2_0 = 0
local var_2_1 = F(var_2_0) or F()
return var_2_1
end
function foo3()
local var_3_0 = (0):F() or :F()
return var_3_0
end
function foo4()
local var_4_0 = 0 or :F()
return var_4_0
end
function foo5()
local var_5_0 = 0
var_5_0 = var_5_0:F() or var_5_0
return var_5_0
end
function foo6()
local var_6_0 = 0
if not var_6_0 then
-- block empty
end
return var_6_0
end
function foo7()
local var_7_0 = 0
var_7_0 = F(var_7_0) or F(var_7_0)
end
Invalid bytecode version (0xA1).
Is there a way to add support for this version? Here is the example script and the compiler that was used.
script&compiler.zip
Empty lines and returns contain trailing whitespace.
Diff comparison; green is luajit-decompiler-v2 output:
Potential fix for returns in Lua::write_block
:
case Ast::AST_STATEMENT_RETURN:
write_indent();
if (i != block.size() - 1) write("do ");
if (block[i]->assignment.expressions.size() || block[i]->assignment.multresReturn) {
write("return ");
} else {
write("return");
}
Whitespace in empty lines could be fixed by moving write_indent();
into every case of switch (block[i]->type)
in Lua::write_block
instead of having it precede the switch, but maybe there's a better way.
Consider choosing a license. Software licenses are important for establishing how a project can be used, modified, and redistributed; as well as limiting your liability.
LJD was originally MIT-licensed, but was re-licensed to GPLv3+ by ZNixian: Aussiemon/ljd@89db2e6
Of course, you're free to license or not license your project however you wish!
It would be useful to have a decompilation option for disabling function definition syntactic sugar:
function <table>:<name>(...)
would become
<table>.<name> = function (self, ...)
Could be accomplished with this snippet in Lua::write_block:
if (isFunctionDefinition) {
if (!previousLineIsEmpty) {
write(NEW_LINE);
write_indent();
}
if (block[i]->assignment.variables.back().type == Ast::AST_VARIABLE_TABLE_INDEX
&& block[i]->assignment.expressions.back()->function->parameterNames.size()
&& block[i]->assignment.expressions.back()->function->parameterNames.front() == "self") {
write_variable(*block[i]->assignment.variables.back().table->variable, false);
write(".", block[i]->assignment.variables.back().tableIndex->constant->string);
write(" = function ");
write_function_definition(*block[i]->assignment.expressions.back()->function, false);
} else {
write_variable(block[i]->assignment.variables.back(), false);
write(" = function ");
write_function_definition(*block[i]->assignment.expressions.back()->function, false);
}
I have a question, how to recompile the file that I have decompiled? That is, the file is decompiled correctly, but if I want to leave it back as it was, how would I do it?
This file silently crashes the decompiler (no output, no error, no further decompilation of other files): options_view.zip
Tested with both my fork and the March 24th release:
The file could be successfully decompiled in previous versions: https://github.com/Aussiemon/Vermintide-2-Source-Code/blob/master/scripts/ui/views/options_view.lua
Hi, I've been trying to decompile some game files. Your compiler has worked for some of them, but not all. For most files I get this error:
Error running Bytecode::Prototype::get_next_byte
Source: bytecode\prototype.cpp:200
Prototype read would exceed end of buffer
I'm attaching one of the files that don't work and its decompiled version from an earlier version of the game (they might be the same in the end, i don't know, that's what I'm trying to find out). Could you have a look at it? If it's the file that is faulty, could you tell me what I'd have to change about it to make it work with your compiler? Thanks!
Hi there, i still got the problem with nested luajit code that doesn't get decompiled. When i run the decompiler the lua file gets decompiled but not the nested part for each line. Here is an example line:
package.preload["random.package.name"] = assert(loadstring("\x1BLJ\x01\x02๏ฟฝ\x02\x02\x00\b\x00\x05\x00\x0E4\x00\x00\x00%\x01\x01\x00>\x00\x02\x024\x01\x00\x00%\x02\x02\x00>\x01\x02\x02%\x02\x03\x00%\x03\x04\x00\x10\x04\x00\x00\x10\x05\x02\x00\x10\x06\x03\x00\x10\a\x01\x00>\x04\x04\x02H\x04\x02\[email protected]/com/example/path/Monitor6Example.logJExample.foobar\frequire\x00", "@Example/path/to/file.lua"))
Whats the problem here?
When using the long option to optimize output formatting it won't work. The short one (-m) works fine.
Hey, you've done amazing work with this project!
However I came across a problem: unsigned numbers are not properly handled by the decompiler...
So sometimes an instruction will perform arithmetic on an unsigned number, which can be a byte of value 241 value for instance(it's unsigned), but when I run the decompiled code, the 241 value is rather interpreted as -14 which causes everything to break.
I can provide example files and the exact line where it happens if required? Just lmk !
Currently argument variable names seem to be defined as a global pool within the file (ie: arg_{func index}_{arg_index}
), which makes sense if they remain available across file scope.
Having them named this way means that if a function is added or removed from that file, the argument variable names end up being named differently across versions of the same script, which makes diffing different versions of the scripts a lot more annoying since every function following the function change that refers to argument variables will be "different".
Is there any way this could be addressed so that argument variable names are consistent despite function addition/removal?
The snippet
b = (y or x) and z or x
becomes
b = not (not y and not x) and z or x
And the snippet
function a()
local x = y()
if z <= x then
b = 1
end
end
becomes
function a()
local x = y()
if x >= z then
b = 1
end
end
The latter will also flip operands in the >
,<
, and <=
cases.
Both are still logically equivalent.
Do you have any interest in replacing the windows-specific code with cross platform c++?
I am willing to do the port myself and make a pull request if there is any interest.
file_issue.txt
Hi, i face an issue when using luajit-decompiler-v2_new.exe , please fix this bug. thank you so much
Running LuaJIT Decompiler v2 through Powershell with this command:
Start-Process -NoNewWindow -FilePath $decompiler_path -ArgumentList "`"$script_directory`" -o `"$output_path`" -e lua -s" -Wait
Paths are:
$decompiler_path="<truncated>\luajit-decompiler-v2.exe"
$script_directory="<truncated>\extracted_testset\vermintide2\scripts"
$output_path="<truncated>\decompiled_testset\vermintide2\scripts"
Example output:
LuaJIT Decompiler v2
Compiled on Nov 23 2023
Failed to open output path: <truncated>\decompiled_testset\vermintide2\scripts
No directories exist in decompiled_testset
, so it seems the application fails to create the intermediate directories required to create the output files in the scripts
directory.
Hi there - I'm working on a small reversing project and the included Lua files are giving me trouble. The header magic and the strings in the file look okay on the surface, but I have very limited knowledge of Lua bytecode so I'm unable to debug it further myself.
Attached is a file that can replicate the issue.
lua.txt
Hi, first of all, thanks for this project, it's very useful!
I get the following error , when I try to decompile a file:
"Prototype has invalid instruction (0xE0)"
How could skip invalid/unsupported instructions and print code using known instructions only?
Thanks!
Thank you for your great work!
Is there any plans to add bytecode input via stdin and decompiled output via stdout (stderr for messages)?
That feature would be very helpful.
The snippet
while x or true do
print ("while x or true")
end
becomes
::label_0_1::
if not x then
-- Nothing
end
repeat
print("while x or true")
goto label_0_1
until true
which is problematic for the same reason as #17.
Several more minor edge cases are:
if x or true then
print("if or true")
end
becomes
if not x then
-- Nothing
end
print("if or true")
And
if x == 0 then
print ("then")
else
end
becomes
if x == 0 then
print("then")
end
if false then
-- Nothing
end
And
while true do
if x then
break
end
end
becomes
repeat
-- Nothing
until x
i really hope you will finish this one, cause this is for sure something i cannot.
Hi,
I'm trying to decompile this lua file: https://fromsmash.com/NU_8IYL_j--dt
But I get the following error:
"Failed to close slot scopes"
How could I solve it?
Thanks!
Ps: could you share your contact so I won't open issue here every time?
LuaJIT Decompiler v2
Compiled on Nov 21 2023
--------------------
Input file: \collect_slot_scopes\ai_bot_group_system.lua
Error running Ast::collect_slot_scopes
Source: ast\ast.cpp:992
Slot scope does not match with variable debug info
--------------------
collect_slot_scopes_compiled.zip
collect_slot_scopes_ljd_decompiled.zip
File tested with the latest binary in Releases.
Hi sir, i found bug in the file, unsupport instruction
tmp2.txt
Hi there and thanks for this awesome decompiler which worked great so far! ๐๐ป
With your newest version it seems it's not possible to decompile anything. I attached one of my files to (hopefully) help with debugging. If i can "help" any further then please let me know!
how is this tool build / compiled? i cant find a makefile or such or any instructions how to build the tool.
Hello !
When i execut the program with this luaJIt, i have this error
thank you in advance
tmp.txt
hope u fix it.
thank you so much!
ItemMng.zip
Please let's try this file . You can get exception at step ast build .
Hope you try and fix that error !
Thank you so much
Is there a way to encode the lua in utf-8 without using BOM (Using BOM will cause lua to send the error message C:\Program Files (x86)\Lua\5.1\lua.exe: .\luafile.lua:1: unexpected symbol near 'ยด'
)
4 files with the same "Failed to build if statement" error; all tested with the latest binary in Releases:
build_if_statements_compiled.zip
build_if_statements_ljd_decompiled.zip
LuaJIT Decompiler v2
Compiled on Nov 21 2023
--------------------
Input file: \build_if_statements\cutscene_player_loadout.lua
Error running Ast::build_if_statements
Source: ast\ast.cpp:2690
Failed to build if statement
--------------------
Input file: \build_if_statements\hud_element_player_ability.lua
Error running Ast::build_if_statements
Source: ast\ast.cpp:2690
Failed to build if statement
--------------------
Input file: \build_if_statements\ui_widgets.lua
Error running Ast::build_if_statements
Source: ast\ast.cpp:2690
Failed to build if statement
--------------------
Input file: \build_if_statements\ui_widgets_deus.lua
Error running Ast::build_if_statements
Source: ast\ast.cpp:2690
Failed to build if statement
--------------------
Thanks for your efforts on this project! It decompiles all but one script in my target without any visible errors so far.
Error running Ast::eliminate_conditions
Source: ast\ast.cpp:2367
Failed to eliminate all test and copy conditions
Thanks for creating this updated decompiler, it works a lot better than the old python one! :)
Would love to have some more features for using the decompiler as a CLI:
For some reason, the program used to be able to work with this file (it seems), but now it refuses to decompile it, although the file has not changed, has something happened to the program?
Test.zip
This snippet
for i = 1, 2, 3 do
if x then
print("Then")
if y then
print ("Nested then")
else
break
end
else
break
end
end
becomes
for i = 1, 2, 3 do
if x then
print("Then")
if y then
print("Nested then")
goto label_0_0
end
end
do break end
if false then
break
end
::label_0_0::
end
Breaks converting to gotos is problematic when expecting Lua 5.1 output.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.