Skip to content

Commit 12b6915

Browse files
committed
5.3 disasm
1 parent 84ac2c9 commit 12b6915

File tree

13 files changed

+280
-69
lines changed

13 files changed

+280
-69
lines changed

librz/arch/isa/luac/lua_arch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
#define LUA_CLOSURE_MARK "#CLOSURE"
2525
#define LUA_KX_MARK " CONST[#Ex]"
2626

27+
/* parameter flags */
28+
#define PARAM_A 1
29+
#define PARAM_B 2
30+
#define PARAM_C 4
31+
#define PARAM_Ax 8
32+
#define PARAM_Bx 16
33+
#define PARAM_sBx 32
34+
35+
#define PARAM_sJ 64
36+
#define PARAM_sC 128
37+
#define PARAM_sB 256
38+
#define PARAM_k 512
39+
40+
#define has_param_flag(flag, bit) ((flag) & (bit)) ? true : false
41+
2742
/* Opcode Instruction Type */
2843
typedef ut32 LuaInstruction;
2944

librz/arch/isa/luac/v52/arch_52.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ typedef enum {
3535
iAx
3636
} LuaOpMode;
3737

38-
/* parameter flags */
39-
#define PARAM_A 1
40-
#define PARAM_B 2
41-
#define PARAM_C 4
42-
#define PARAM_Ax 8
43-
#define PARAM_Bx 16
44-
#define PARAM_sBx 32
45-
46-
#define has_param_flag(flag, bit) ((flag) & (bit)) ? true : false
47-
4838
/* Offset of arguments in opcode */
4939
#define SIZE_C 9
5040
#define SIZE_B 9
@@ -195,15 +185,15 @@ enum OpArgMask {
195185
OpArgK /* argument is a constant or register/constant */
196186
};
197187

198-
extern const ut8 luaP_opmodes[LUA_NUM_OPCODES];
188+
extern const ut8 luaP_opmodes52[LUA_NUM_OPCODES];
199189

200190
#define opmode(t, a, b, c, m) (((t) << 7) | ((a) << 6) | ((b) << 4) | ((c) << 2) | (m))
201191

202-
#define getOpMode(m) (cast(LuaOpMode, luaP_opmodes[m] & 3))
203-
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
204-
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
205-
#define testAMode(m) (luaP_opmodes[m] & (1 << 6))
206-
#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
192+
#define getOpMode(m) (cast(LuaOpMode, luaP_opmodes52[m] & 3))
193+
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes52[m] >> 4) & 3))
194+
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes52[m] >> 2) & 3))
195+
#define testAMode(m) (luaP_opmodes52[m] & (1 << 6))
196+
#define testTMode(m) (luaP_opmodes52[m] & (1 << 7))
207197

208198
#define MYK(x) (-1 - (x))
209199

librz/arch/isa/luac/v52/disassembly_52.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ int lua52_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
3030
int special_c = 0xFF - c;
3131
int special_b = 0xFF - b;
3232

33+
op->size = 4;
34+
35+
if (opcode > LUA_NUM_OPCODES) {
36+
rz_strbuf_append(&op->buf_asm, "invalid");
37+
return op->size;
38+
}
39+
3340
char *asm_string;
3441
switch (getOpMode(opcode)) {
3542
case iABC:
@@ -143,8 +150,6 @@ int lua52_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
143150
}
144151

145152
rz_strbuf_append(&op->buf_asm, asm_string);
146-
op->size = 4;
147-
148153
RZ_FREE(asm_string);
149-
return 4;
154+
return op->size;
150155
}

librz/arch/isa/luac/v52/opcode_52.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "arch_52.h"
77

8-
const ut8 luaP_opmodes[LUA_NUM_OPCODES] = {
8+
const ut8 luaP_opmodes52[LUA_NUM_OPCODES] = {
99
/* T A B C mode opcode */
1010
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_MOVE */
1111
opmode(0, 1, OpArgK, OpArgN, iABx), /* OP_LOADK */

librz/arch/isa/luac/v53/arch_53.h

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ typedef enum {
3535
iAx
3636
} LuaOpMode;
3737

38-
/* parameter flags */
39-
#define PARAM_A 1
40-
#define PARAM_B 2
41-
#define PARAM_C 4
42-
#define PARAM_Ax 8
43-
#define PARAM_Bx 16
44-
#define PARAM_sBx 32
45-
46-
#define has_param_flag(flag, bit) ((flag) & (bit)) ? true : false
47-
4838
/* Offset of arguments in opcode */
4939
#define SIZE_C 9
5040
#define SIZE_B 9
@@ -60,10 +50,28 @@ typedef enum {
6050
#define POS_Bx POS_C
6151
#define POS_Ax POS_A
6252

53+
/*
54+
** Macros to operate RK indices
55+
*/
56+
57+
/* this bit 1 means constant (0 means register) */
58+
#define BITRK (1 << (SIZE_B - 1))
59+
60+
/* test whether value is a constant */
61+
#define ISK(x) ((x) & BITRK)
62+
63+
/* gets the index of the constant */
64+
#define INDEXK(r) ((int)(r) & ~BITRK)
65+
66+
#define MAXINDEXRK (BITRK - 1)
67+
68+
/* code a constant index as a RK value */
69+
#define RKASK(x) ((x) | BITRK)
70+
6371
typedef enum {
6472
/*----------------------------------------------------------------------
65-
name args description
66-
------------------------------------------------------------------------*/
73+
name args description
74+
------------------------------------------------------------------------*/
6775
OP_MOVE, /* A B R(A) := R(B) */
6876
OP_LOADK, /* A Bx R(A) := Kst(Bx) */
6977
OP_LOADKX, /* A R(A) := Kst(extra arg) */
@@ -131,6 +139,57 @@ name args description
131139

132140
#define LUA_NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
133141

142+
/*===========================================================================
143+
Notes:
144+
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
145+
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
146+
OP_SETLIST) may use 'top'.
147+
148+
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
149+
set top (like in OP_CALL with C == 0).
150+
151+
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
152+
153+
(*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
154+
'instruction' is EXTRAARG(real C).
155+
156+
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
157+
158+
(*) For comparisons, A specifies what condition the test should accept
159+
(true or false).
160+
161+
(*) All 'skips' (pc++) assume that next instruction is a jump.
162+
163+
===========================================================================*/
164+
165+
/*
166+
** masks for instruction properties. The format is:
167+
** bits 0-1: op mode
168+
** bits 2-3: C arg mode
169+
** bits 4-5: B arg mode
170+
** bit 6: instruction set register A
171+
** bit 7: operator is a test (next instruction must be a jump)
172+
*/
173+
174+
enum OpArgMask {
175+
OpArgN, /* argument is not used */
176+
OpArgU, /* argument is used */
177+
OpArgR, /* argument is a register or a jump offset */
178+
OpArgK /* argument is a constant or register/constant */
179+
};
180+
181+
extern const ut8 luaP_opmodes53[LUA_NUM_OPCODES];
182+
183+
#define opmode(t, a, b, c, m) (((t) << 7) | ((a) << 6) | ((b) << 4) | ((c) << 2) | (m))
184+
185+
#define getOpMode(m) (cast(LuaOpMode, luaP_opmodes53[m] & 3))
186+
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes53[m] >> 4) & 3))
187+
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes53[m] >> 2) & 3))
188+
#define testAMode(m) (luaP_opmodes53[m] & (1 << 6))
189+
#define testTMode(m) (luaP_opmodes53[m] & (1 << 7))
190+
191+
#define MYK(x) (-1 - (x))
192+
134193
#define MAX_INT INT_MAX /* maximum value of an int */
135194

136195
#define LUAI_BITSINT 32

librz/arch/isa/luac/v53/assembly_53.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16
1010
int args[3];
1111
char buffer[64]; // buffer for digits
1212
int cur_cnt = 0;
13-
int delta_offset;
1413
int temp;
1514

16-
if (arg_num > sizeof(args)) {
17-
return -1;
18-
}
19-
2015
for (int i = 0; i < arg_num; ++i) {
21-
delta_offset = lua_load_next_arg_start(arg_start, buffer);
16+
const int delta_offset = lua_load_next_arg_start(arg_start, buffer);
2217
if (delta_offset == 0) {
2318
return -1;
2419
}
@@ -30,6 +25,27 @@ static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16
3025
}
3126
}
3227

28+
switch (getOpMode(opcode)) {
29+
case iABC:
30+
if (getBMode(opcode) != OpArgN) {
31+
args[1] = ISK(args[1]) ? (MYK(INDEXK(args[1]))) : args[1];
32+
}
33+
if (getCMode(opcode) != OpArgN) {
34+
args[2] = ISK(args[2]) ? (MYK(INDEXK(args[2]))) : args[2];
35+
}
36+
break;
37+
case iABx:
38+
if (getBMode(opcode) == OpArgK) {
39+
args[1] = MYK(args[1]);
40+
}
41+
break;
42+
case iAsBx:
43+
break;
44+
case iAx:
45+
args[0] = MYK(args[0]);
46+
break;
47+
}
48+
3349
SET_OPCODE(instruction, opcode);
3450
if (has_param_flag(flag, PARAM_A)) {
3551
SETARG_A(instruction, args[cur_cnt++]);

librz/arch/isa/luac/v53/disassembly_53.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ int lua53_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
3030

3131
char *asm_string;
3232

33+
switch (getOpMode(opcode)) {
34+
case iABC:
35+
if (getBMode(opcode) != OpArgN) {
36+
b = ISK(b) ? (MYK(INDEXK(b))) : b;
37+
}
38+
if (getCMode(opcode) != OpArgN) {
39+
c = ISK(c) ? (MYK(INDEXK(c))) : c;
40+
}
41+
break;
42+
case iABx:
43+
if (getBMode(opcode) == OpArgK) {
44+
bx = MYK(bx);
45+
}
46+
break;
47+
case iAsBx:
48+
break;
49+
case iAx:
50+
ax = MYK(ax);
51+
break;
52+
}
53+
3354
switch (opcode) {
3455
case OP_LOADKX: /* A R(A) := Kst(extra arg) */
3556
asm_string = luaop_new_str_1arg(opnames[opcode], a);

librz/arch/isa/luac/v53/opcode_53.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,64 @@
44
// SPDX-FileCopyrightText: 2025-2026 Sergey Sharshunov <s.sharshunov@gmail.com>
55

66
#include "arch_53.h"
7+
8+
const ut8 luaP_opmodes53[LUA_NUM_OPCODES] = {
9+
/* T A B C mode opcode */
10+
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_MOVE */
11+
opmode(0, 1, OpArgK, OpArgN, iABx), /* OP_LOADK */
12+
opmode(0, 1, OpArgN, OpArgN, iABx), /* OP_LOADKX */
13+
opmode(0, 1, OpArgU, OpArgU, iABC), /* OP_LOADBOOL */
14+
opmode(0, 1, OpArgU, OpArgN, iABC), /* OP_LOADNIL */
15+
opmode(0, 1, OpArgU, OpArgN, iABC), /* OP_GETUPVAL */
16+
opmode(0, 1, OpArgU, OpArgK, iABC), /* OP_GETTABUP */
17+
opmode(0, 1, OpArgR, OpArgK, iABC), /* OP_GETTABLE */
18+
opmode(0, 0, OpArgK, OpArgK, iABC), /* OP_SETTABUP */
19+
opmode(0, 0, OpArgU, OpArgN, iABC), /* OP_SETUPVAL */
20+
opmode(0, 0, OpArgK, OpArgK, iABC), /* OP_SETTABLE */
21+
opmode(0, 1, OpArgU, OpArgU, iABC), /* OP_NEWTABLE */
22+
opmode(0, 1, OpArgR, OpArgK, iABC), /* OP_SELF */
23+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_ADD */
24+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_SUB */
25+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_MUL */
26+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_MOD */
27+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_POW */
28+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_DIV */
29+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_IDIV */
30+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_BAND */
31+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_BOR */
32+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_BXOR */
33+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_SHL */
34+
opmode(0, 1, OpArgK, OpArgK, iABC), /* OP_SHR */
35+
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_UNM */
36+
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_BNOT */
37+
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_NOT */
38+
opmode(0, 1, OpArgR, OpArgN, iABC), /* OP_LEN */
39+
opmode(0, 1, OpArgR, OpArgR, iABC), /* OP_CONCAT */
40+
opmode(0, 0, OpArgR, OpArgN, iAsBx), /* OP_JMP */
41+
opmode(1, 0, OpArgK, OpArgK, iABC), /* OP_EQ */
42+
opmode(1, 0, OpArgK, OpArgK, iABC), /* OP_LT */
43+
opmode(1, 0, OpArgK, OpArgK, iABC), /* OP_LE */
44+
opmode(1, 0, OpArgN, OpArgU, iABC), /* OP_TEST */
45+
opmode(1, 1, OpArgR, OpArgU, iABC), /* OP_TESTSET */
46+
opmode(0, 1, OpArgU, OpArgU, iABC), /* OP_CALL */
47+
opmode(0, 1, OpArgU, OpArgU, iABC), /* OP_TAILCALL */
48+
opmode(0, 0, OpArgU, OpArgN, iABC), /* OP_RETURN */
49+
opmode(0, 1, OpArgR, OpArgN, iAsBx), /* OP_FORLOOP */
50+
opmode(0, 1, OpArgR, OpArgN, iAsBx), /* OP_FORPREP */
51+
opmode(0, 0, OpArgN, OpArgU, iABC), /* OP_TFORCALL */
52+
opmode(0, 1, OpArgR, OpArgN, iAsBx), /* OP_TFORLOOP */
53+
opmode(0, 0, OpArgU, OpArgU, iABC), /* OP_SETLIST */
54+
opmode(0, 1, OpArgU, OpArgN, iABx), /* OP_CLOSURE */
55+
opmode(0, 1, OpArgU, OpArgN, iABC), /* OP_VARARG */
56+
opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
57+
};
58+
759
#define lua_strcase(case_str) if ( \
860
((limit) <= sizeof(case_str) - 1) && \
961
rz_str_ncasecmp((name), (case_str), sizeof(case_str) - 1) == 0)
1062

1163
LuaOpNameList get_lua53_opnames(void) {
12-
LuaOpNameList list = RZ_NEWS(char *, LUA_NUM_OPCODES + 1);
64+
const LuaOpNameList list = RZ_NEWS(char *, LUA_NUM_OPCODES + 1);
1365
if (list == NULL) {
1466
RZ_LOG_ERROR("Cannot allocate lua53 opcode list.\n");
1567
return NULL;

librz/arch/isa/luac/v54/arch_54.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef enum {
7575
OP_LOADF, /* A sBx R[A] := (lua_Number)sBx */
7676
OP_LOADK, /* A Bx R[A] := K[Bx] */
7777
OP_LOADKX, /* A R[A] := K[extra arg] */
78-
OP_LOADFALSE, /* A R[A] := false */
78+
OP_LOADFALSE, /* A R[A] := false */
7979
OP_LFALSESKIP, /*A R[A] := false; pc++ */
8080
OP_LOADTRUE, /* A R[A] := true */
8181
OP_LOADNIL, /* A B R[A], R[A+1], ..., R[A+B] := nil */
@@ -230,20 +230,6 @@ typedef enum {
230230

231231
#define SETARG_k(i, v) LUA_SETARG(i, v, LUAOP_k_OFFSET, 1)
232232

233-
/* parameter flags */
234-
#define PARAM_A 1
235-
#define PARAM_B 2
236-
#define PARAM_C 4
237-
#define PARAM_Ax 8
238-
#define PARAM_Bx 16
239-
#define PARAM_sBx 32
240-
#define PARAM_sJ 64
241-
#define PARAM_sC 128
242-
#define PARAM_sB 256
243-
#define PARAM_k 512
244-
245-
#define has_param_flag(flag, bit) ((flag) & (bit)) ? true : false
246-
247233
#define ISK(isk) ((isk) ? "#CONST" : "#R")
248234
#define ISFLIP(isk) ((isk) ? "#FLIP" : "")
249235

librz/bin/format/luac/luac_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef ut32 LUA_INT;
3131
#define luac_cast_int(i) luac_cast(int, (i))
3232

3333
#define LUAC_MAGIC "\x1b\x4c\x75\x61" ///< "\033Lua"
34+
#define LUAC_MAGIC_SIZE 4
3435
#define LUAC_FORMAT 0 /* this is the official format */
3536
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
3637
#define LUAC_INT_VALIDATION luac_cast_int(0x5678)

0 commit comments

Comments
 (0)