更酷的名字是:修改 Lua 虚拟机,修改 Lua 字节码顺序
使用的是 Lua 5.1.5 版本:
修改两个文件 lopcodes.h
& lopcodes.c
:
/*
** grep "ORDER OP" if you change these enums #老外还是很有意思的
*/
typedef enum {
/*----------------------------------------------------------------------
name args description
------------------------------------------------------------------------*/
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
/* ORDER OP */
const char *const luaP_opnames[NUM_OPCODES+1] = {
"ADD",
"CALL",
"MOVE",
测试的 lua 文件:
print('hello')
function p(...)
local b = 2+1 *3/5
print('qt',b)
end
p('b')
编译两个版本字节码:
./luac.5.1.5 -o optest.luac.5.1.5 optest.lua
./luac -o optest.luac optest.lua
对比文件差异:
$ diff optest.luac optest.luac.5.1.5
Binary files optest.luac and optest.luac.5.1.5 differ
使用原版 lua 加载 .luac
文件:
$ ./lua.5.1.5 optest.luac
./lua.5.1.5: optest.luac: bad code in precompiled chunk
而且即使你用 unloac
也解不出来:
$ java -jar ~/Downloads/unluac.jar src/optest.luac
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1025
at unluac.decompile.Function.getConstantExpression(Function.java:38)
at unluac.decompile.Decompiler.processLine(Decompiler.java:184)
at unluac.decompile.Decompiler.processSequence(Decompiler.java:510)
at unluac.decompile.Decompiler.decompile(Decompiler.java:126)
at unluac.Main.main(Main.java:47)
但是可以解 lua5.1.5 的字节码文件:
$ java -jar ~/Downloads/unluac.jar src/optest.luac.5.1.5
print("hello")
function p(...)
local b = 2.6
print("qt", b)
end
p("b")
luadec
https://github.com/viruscamp/luadec
~/test/luadec/luadec [master]$ ./luadec ~/Downloads/lua-5.1.5/src/optest.luac.5.1.5
-- Decompiled using luadec 2.2 rev: 895d923 for Lua 5.1 from https://github.com/viruscamp/luadec
-- Command line: ~/Downloads/lua-5.1.5/src/optest.luac.5.1.5
-- params : ...
-- function num : 0
print("hello")
p = function(...)
-- function num : 0_0
local b = 2.6
print("qt", b)
end
p("b")
~/test/luadec/luadec [master]$ ./luadec ~/Downloads/lua-5.1.5/src/optest.luac
./luadec: ~/Downloads/lua-5.1.5/src/optest.luac: bad code in precompiled chunk
华丽的分割线
但是你修改过的 lua 也加载不了 luac
文件了,一脸懵逼:
$ ./lua optest.luac
./lua: optest.luac: bad code in precompiled chunk
ヾ(。`Д´。)我擦,下回 再接着说少了哪些步骤 ~
网友评论