Giter VIP home page Giter VIP logo

luajit-decompiler-v2's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

luajit-decompiler-v2's Issues

Minimize diffs of tables by using trailing comma

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,
}

[Suggestion] Leave returns in empty functions

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

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

Trailing whitespace after empty lines and returns

Empty lines and returns contain trailing whitespace.

Diff comparison; green is luajit-decompiler-v2 output:
image

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.

[Suggestion] Option to disable function definition syntactic sugar

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);
	}

how to recompile the file?

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?

Prototype read would exceed end of buffer

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!

test_files.zip

Nested luajit and `Invalid argument: --minimize_diffs`

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.

Problem with unsigned numbers

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 !

Argument variable names

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?

Inverted comparisons and flipped operands

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.

Interest in a cross-platform port

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.

Commandline mode doesn't create output path with -o parameter

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.

Read unexpectedly reached end of file - Bytecode::buffer_next_block()

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

Skip invalid/unsupported instructions

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!

test.luac.zip

CLI standard streams

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.

Miscellaneous malformed output with edge cases

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

daymn

i really hope you will finish this one, cause this is for sure something i cannot.

UTF-8 string support

It would be great to have UTF-8 support for strings. Right now I get this in some of the files I've tried the tool on:
image

Prototype has unsupported instruction

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!

internald.zip

image

Incorupt memory

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

Doubts about encoding (UTF-8 BOM)

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 'ยด' )

Error running Ast::build_if_statements

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
--------------------

Error running Ast::eliminate_conditions

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

failing_script.zip

CLI features

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:

  • Command argument for output folder
  • Command option for filtering file extensions (i.e ".lua")
  • Command option for logging assertion error to stdout instead of showing MessageBox

Bytecode error?

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?
image
Test.zip

Nested breaks turn into goto/label

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.