From ca0b5acb18fbbab23d7b82c17dedf3f50b821ca4 Mon Sep 17 00:00:00 2001 From: Enyby Date: Mon, 2 Jul 2018 04:29:52 +0300 Subject: [PATCH] Print any code Print any code even if it is obfuscated with not valid op codes and other garbage. Also it can print any custom modification of byte code instead of fall with Exception. --- src/core/org/luaj/vm2/Print.java | 208 +++++++++++++++++-------------- 1 file changed, 116 insertions(+), 92 deletions(-) diff --git a/src/core/org/luaj/vm2/Print.java b/src/core/org/luaj/vm2/Print.java index 3c9c570b..8b0bd572 100644 --- a/src/core/org/luaj/vm2/Print.java +++ b/src/core/org/luaj/vm2/Print.java @@ -128,6 +128,10 @@ public class Print extends Lua { } static void printValue( PrintStream ps, LuaValue v ) { + if (v == null) { + ps.print("null"); + return; + } switch ( v.type() ) { case LuaValue.TSTRING: printString( ps, (LuaString) v ); break; default: ps.print( v.tojstring() ); @@ -136,7 +140,7 @@ public class Print extends Lua { } static void printConstant(PrintStream ps, Prototype f, int i) { - printValue( ps, f.k[i] ); + printValue( ps, i < f.k.length ? f.k[i] : LuaValue.valueOf("UNKNOWN_CONST_" + i) ); } static void printUpvalue(PrintStream ps, Upvaldesc u) { @@ -189,80 +193,67 @@ public class Print extends Lua { ps.print("[" + line + "] "); else ps.print("[-] "); - ps.print(OPNAMES[o] + " "); - switch (getOpMode(o)) { - case iABC: - ps.print( a ); - if (getBMode(o) != OpArgN) - ps.print(" "+(ISK(b) ? (-1 - INDEXK(b)) : b)); - if (getCMode(o) != OpArgN) - ps.print(" "+(ISK(c) ? (-1 - INDEXK(c)) : c)); - break; - case iABx: - if (getBMode(o) == OpArgK) { - ps.print(a + " " + (-1 - bx)); - } else { - ps.print(a + " " + (bx)); + if (o >= OPNAMES.length - 1) { + ps.print("UNKNOWN_OP_" + o + " "); + } else { + ps.print(OPNAMES[o] + " "); + switch (getOpMode(o)) { + case iABC: + ps.print( a ); + if (getBMode(o) != OpArgN) + ps.print(" "+(ISK(b) ? (-1 - INDEXK(b)) : b)); + if (getCMode(o) != OpArgN) + ps.print(" "+(ISK(c) ? (-1 - INDEXK(c)) : c)); + break; + case iABx: + if (getBMode(o) == OpArgK) { + ps.print(a + " " + (-1 - bx)); + } else { + ps.print(a + " " + (bx)); + } + break; + case iAsBx: + if (o == OP_JMP) + ps.print( sbx ); + else + ps.print(a + " " + sbx); + break; } - break; - case iAsBx: - if (o == OP_JMP) - ps.print( sbx ); - else - ps.print(a + " " + sbx); - break; - } - switch (o) { - case OP_LOADK: - ps.print(" ; "); - printConstant(ps, f, bx); - break; - case OP_GETUPVAL: - case OP_SETUPVAL: - ps.print(" ; "); - printUpvalue(ps, f.upvalues[b]); - break; - case OP_GETTABUP: - ps.print(" ; "); - printUpvalue(ps, f.upvalues[b]); - ps.print(" "); - if (ISK(c)) - printConstant(ps, f, INDEXK(c)); - else - ps.print("-"); - break; - case OP_SETTABUP: - ps.print(" ; "); - printUpvalue(ps, f.upvalues[a]); - ps.print(" "); - if (ISK(b)) - printConstant(ps, f, INDEXK(b)); - else - ps.print("-"); - ps.print(" "); - if (ISK(c)) - printConstant(ps, f, INDEXK(c)); - else - ps.print("-"); - break; - case OP_GETTABLE: - case OP_SELF: - if (ISK(c)) { + switch (o) { + case OP_LOADK: ps.print(" ; "); - printConstant(ps, f, INDEXK(c)); - } - break; - case OP_SETTABLE: - case OP_ADD: - case OP_SUB: - case OP_MUL: - case OP_DIV: - case OP_POW: - case OP_EQ: - case OP_LT: - case OP_LE: - if (ISK(b) || ISK(c)) { + printConstant(ps, f, bx); + break; + case OP_GETUPVAL: + case OP_SETUPVAL: ps.print(" ; "); + if (b < f.upvalues.length) { + printUpvalue(ps, f.upvalues[b]); + } else { + ps.print("UNKNOWN_UPVALUE_" + b); + } + break; + case OP_GETTABUP: + ps.print(" ; "); + if (b < f.upvalues.length) { + printUpvalue(ps, f.upvalues[b]); + } else { + ps.print("UNKNOWN_UPVALUE_" + b); + } + ps.print(" "); + if (ISK(c)) + printConstant(ps, f, INDEXK(c)); + else + ps.print("-"); + break; + case OP_SETTABUP: + ps.print(" ; "); + if (a < f.upvalues.length) { + printUpvalue(ps, f.upvalues[a]); + } else { + ps.print("UNKNOWN_UPVALUE_" + a); + } + ps.print(" "); if (ISK(b)) printConstant(ps, f, INDEXK(b)); else @@ -272,27 +263,60 @@ public class Print extends Lua { printConstant(ps, f, INDEXK(c)); else ps.print("-"); + break; + case OP_GETTABLE: + case OP_SELF: + if (ISK(c)) { + ps.print(" ; "); + printConstant(ps, f, INDEXK(c)); + } + break; + case OP_SETTABLE: + case OP_ADD: + case OP_SUB: + case OP_MUL: + case OP_DIV: + case OP_POW: + case OP_EQ: + case OP_LT: + case OP_LE: + if (ISK(b) || ISK(c)) { + ps.print(" ; "); + if (ISK(b)) + printConstant(ps, f, INDEXK(b)); + else + ps.print("-"); + ps.print(" "); + if (ISK(c)) + printConstant(ps, f, INDEXK(c)); + else + ps.print("-"); + } + break; + case OP_JMP: + case OP_FORLOOP: + case OP_FORPREP: + ps.print(" ; to " + (sbx + pc + 2)); + break; + case OP_CLOSURE: + if (bx < f.p.length) { + ps.print(" ; " + f.p[bx].getClass().getName()); + } else { + ps.print(" ; UNKNOWN_PROTYPE_" + bx); + } + break; + case OP_SETLIST: + if (c == 0) + ps.print(" ; " + ((int) code[++pc]) + " (stored in the next OP)"); + else + ps.print(" ; " + ((int) c)); + break; + case OP_VARARG: + ps.print( " ; is_vararg="+ f.is_vararg ); + break; + default: + break; } - break; - case OP_JMP: - case OP_FORLOOP: - case OP_FORPREP: - ps.print(" ; to " + (sbx + pc + 2)); - break; - case OP_CLOSURE: - ps.print(" ; " + f.p[bx].getClass().getName()); - break; - case OP_SETLIST: - if (c == 0) - ps.print(" ; " + ((int) code[++pc]) + " (stored in the next OP)"); - else - ps.print(" ; " + ((int) c)); - break; - case OP_VARARG: - ps.print( " ; is_vararg="+ f.is_vararg ); - break; - default: - break; } return pc; }