Skip to content

Commit 6c489d2

Browse files
committed
5.5 asm/disasm
1 parent 6b0e533 commit 6c489d2

File tree

8 files changed

+97
-41
lines changed

8 files changed

+97
-41
lines changed

librz/arch/isa/luac/v54/disassembly_54.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int lua54_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
136136
break;
137137

138138
case OP_RETURN0: /* return */
139-
asm_string = rz_str_newf("return0");
139+
asm_string = rz_str_dup(opnames[opcode]);
140140
break;
141141

142142
/* iABx instructions */

librz/arch/isa/luac/v55/arch_55.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ typedef enum {
110110
OP_BORK, /* A B C R[A] := R[B] | K[C]:integer */
111111
OP_BXORK, /* A B C R[A] := R[B] ~ K[C]:integer */
112112

113-
OP_SHRI, /* A B sC R[A] := R[B] >> sC */
114113
OP_SHLI, /* A B sC R[A] := sC << R[B] */
114+
OP_SHRI, /* A B sC R[A] := R[B] >> sC */
115115

116116
OP_ADD, /* A B C R[A] := R[B] + R[C] */
117117
OP_SUB, /* A B C R[A] := R[B] - R[C] */
@@ -176,6 +176,10 @@ typedef enum {
176176

177177
OP_VARARG, /* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
178178

179+
OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */
180+
181+
OP_ERRNNIL, /* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/
182+
179183
OP_VARARGPREP, /*A (adjust vararg parameters) */
180184

181185
OP_EXTRAARG /* Ax extra (larger) argument for previous opcode */

librz/arch/isa/luac/v55/assembly_55.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16
99
int args[4];
1010
char buffer[64]; // buffer for digits
1111
int cur_cnt = 0;
12-
int delta_offset;
13-
14-
if (arg_num > sizeof(args)) {
15-
return -1;
16-
}
1712

1813
for (int i = 0; i < arg_num; ++i) {
19-
delta_offset = lua_load_next_arg_start(arg_start, buffer);
20-
if (delta_offset == 0) {
21-
return -1;
22-
}
23-
if (lua_is_valid_num_value_string(buffer)) {
14+
const int delta_offset = lua_load_next_arg_start(arg_start, buffer);
15+
char *ptr = strchr(buffer, 'k');
16+
if (ptr != NULL) {
17+
*ptr = '\0';
2418
args[i] = lua_convert_str_to_num(buffer);
25-
arg_start += delta_offset;
26-
} else {
27-
return -1;
19+
args[arg_num] = 1;
20+
arg_num++;
21+
flag |= PARAM_k;
22+
break;
2823
}
24+
args[i] = lua_convert_str_to_num(buffer);
25+
arg_start += delta_offset;
2926
}
3027

3128
LUA_SET_OPCODE(instruction, opcode);
@@ -95,27 +92,20 @@ static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16
9592
}
9693

9794
bool lua55_assembly(const char *input, st32 input_size, LuaInstruction *instruction_p) {
98-
const char *opcode_start; // point to the header
99-
const char *opcode_end; // point to the first white space
100-
int opcode_len;
101-
102-
const char *arg_start;
103-
104-
ut8 opcode;
105-
LuaInstruction instruction = 0x00;
106-
10795
/* Find the opcode */
108-
opcode_start = input;
109-
opcode_end = strchr(input, ' ');
96+
const char *opcode_start = input; ///< point to the header
97+
const char *opcode_end = strchr(input, ' '); ///< point to the first white space
11098
if (opcode_end == NULL) {
11199
opcode_end = input + input_size;
112100
}
113101

114-
opcode_len = opcode_end - opcode_start;
115-
opcode = get_lua54_opcode_by_name(opcode_start, opcode_len);
102+
const int opcode_len = opcode_end - opcode_start;
103+
const ut8 opcode = get_lua55_opcode_by_name(opcode_start, opcode_len);
116104

117105
/* Find the arguments */
118-
arg_start = rz_str_trim_head_ro(opcode_end);
106+
const char *arg_start = rz_str_trim_head_ro(opcode_end);
107+
108+
LuaInstruction instruction = 0x00;
119109

120110
/* Encode opcode and args */
121111
switch (opcode) {
@@ -152,14 +142,18 @@ bool lua55_assembly(const char *input, st32 input_size, LuaInstruction *instruct
152142
// iABC k instruction
153143
case OP_TAILCALL:
154144
case OP_RETURN:
155-
case OP_NEWTABLE:
156-
case OP_SETLIST:
157-
case OP_MMBINK:
158145
case OP_SETTABUP:
159146
case OP_SETTABLE:
160147
case OP_SETI:
161148
case OP_SETFIELD:
162149
case OP_SELF:
150+
instruction = encode_instruction(opcode, arg_start,
151+
PARAM_A | PARAM_B | PARAM_C,
152+
3);
153+
break;
154+
case OP_NEWTABLE:
155+
case OP_SETLIST:
156+
case OP_MMBINK:
163157
instruction = encode_instruction(opcode, arg_start,
164158
PARAM_A | PARAM_B | PARAM_C | PARAM_k,
165159
4);
@@ -235,8 +229,8 @@ bool lua55_assembly(const char *input, st32 input_size, LuaInstruction *instruct
235229
// A with k
236230
case OP_TEST:
237231
instruction = encode_instruction(opcode, arg_start,
238-
PARAM_A | PARAM_k,
239-
2);
232+
PARAM_A,
233+
1);
240234
break;
241235
// no arg
242236
case OP_RETURN0:

librz/arch/isa/luac/v55/disassembly_55.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ int lua55_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
101101
case OP_LE: /* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
102102
case OP_TESTSET: /* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
103103
case OP_EQK: /* A B k if ((R[A] == K[B]) ~= k) then pc++ */
104-
asm_string = luaop_new_str_2arg_ex(opnames[opcode], a, b, isk);
104+
asm_string = luaop_new_str_2arg_ex_ki(opnames[opcode], a, b, isk);
105105
break;
106106
/* iABC - A & sB with k instructions */
107107
case OP_EQI: /* A sB k if ((R[A] == sB) ~= k) then pc++ */
108108
case OP_LTI: /* A sB k if ((R[A] < sB) ~= k) then pc++ */
109109
case OP_LEI: /* A sB k if ((R[A] <= sB) ~= k) then pc++ */
110110
case OP_GTI: /* A sB k if ((R[A] > sB) ~= k) then pc++ */
111111
case OP_GEI: /* A sB k if ((R[A] >= sB) ~= k) then pc++ */
112-
asm_string = luaop_new_str_2arg_ex(opnames[opcode], a, sb, isk);
112+
asm_string = luaop_new_str_2arg_ex_ki(opnames[opcode], a, sb, isk);
113113
break;
114114

115115
/* iABC - A & C instructions */
@@ -136,7 +136,7 @@ int lua55_disasm(RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList opnames) {
136136
break;
137137

138138
case OP_RETURN0: /* return */
139-
asm_string = rz_str_newf("RETURN0");
139+
asm_string = rz_str_dup(opnames[opcode]);
140140
break;
141141

142142
/* iABx instructions */

librz/arch/isa/luac/v55/opcode_55.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ LuaOpNameList get_lua55_opnames(void) {
4747
list[OP_BANDK] = "bandk",
4848
list[OP_BORK] = "bork",
4949
list[OP_BXORK] = "bxork",
50-
list[OP_SHRI] = "shri",
5150
list[OP_SHLI] = "shli",
51+
list[OP_SHRI] = "shri",
5252
list[OP_ADD] = "add",
5353
list[OP_SUB] = "sub",
5454
list[OP_MUL] = "mul",
@@ -96,6 +96,8 @@ LuaOpNameList get_lua55_opnames(void) {
9696
list[OP_SETLIST] = "setlist",
9797
list[OP_CLOSURE] = "closure",
9898
list[OP_VARARG] = "vararg",
99+
list[OP_GETVARG] = "getvarg",
100+
list[OP_ERRNNIL] = "errnnil",
99101
list[OP_VARARGPREP] = "varargprep",
100102
list[OP_EXTRAARG] = "extraarg",
101103
list[LUA_NUM_OPCODES] = NULL;
@@ -207,6 +209,8 @@ ut8 get_lua55_opcode_by_name(const char *name, int limit) {
207209
lua_strcase("closure") return OP_CLOSURE;
208210

209211
lua_strcase("vararg") return OP_VARARG;
212+
lua_strcase("getvarg") return OP_GETVARG;
213+
lua_strcase("errnnil") return OP_ERRNNIL;
210214

211215
lua_strcase("varargprep") return OP_VARARGPREP;
212216

test/db/analysis/luac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ EXPECT=<<EOF
2222
| ,===< 0x00000055 jmp 5
2323
| |`--> 0x00000059 gettabup 0 0 0
2424
| | : 0x0000005d addi 0 0 1
25-
| | : 0x00000061 mmbini 0 1 6 0
26-
| | : 0x00000065 settabup 0 0 0 0
25+
| | : 0x00000061 mmbini 0 1 6
26+
| | : 0x00000065 settabup 0 0 0
2727
| `-`=< 0x00000069 jmp -8
2828
EOF
2929
RUN

test/db/asm/luac_5.4_0

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ ad "setfield 0 2 1" 12000201
2828
ad "gettabup 0 0 3" 0b000003
2929
ad "closure 1 1" cf800000
3030
ad "setfield 0 4 1" 12000401
31-
ad "gettabup 0 0 3" 0b000003
3231
ad "self 0 0 2k" 14800002
3332
ad "loadi 2 1000" 0181f381
3433
ad "call 0 3 2" 44000302

test/db/asm/luac_5.5_0

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ad "settabup 0 0 1k" 0f800001
2+
ad "lti 0 50 0" 3e00b100
3+
ad "jmp 5" 38020080
4+
ad "addi 0 0 1" 15000080
5+
ad "mmbini 0 1 6" 2f008006
6+
ad "loadk 1 10" 83000500
7+
ad "jmp 4" b8010080
8+
ad "call 0 2 1" 44000201
9+
ad "test 0k" 42800000
10+
ad "gti 0 40 0" 4000a700
11+
ad "add 4 4 3" 22020403
12+
ad "eqi 0 0 0" 3d007f00
13+
ad "forprep 0 4" 4a000200
14+
ad "closure 0 1" 4f800000
15+
ad "newtable 2 2 0" 13010200
16+
ad "setfield 2 44 21k" 12812c15
17+
18+
# official luac -l
19+
ad "varargprep 0" 53000000
20+
ad "newtable 0 0 0" 13000000
21+
ad "extraarg 0" 54000000
22+
ad "settabup 0 0 0" 0f000000
23+
ad "gettabup 0 0 0" 0b000000
24+
ad "gettabup 1 0 0" 8b000000
25+
ad "setfield 0 1 1" 12000101
26+
ad "closure 1 0" cf000000
27+
ad "setfield 0 2 1" 12000201
28+
ad "closure 1 1" cf800000
29+
ad "setfield 0 3 1" 12000301
30+
ad "self 0 0 2" 14000002
31+
ad "loadi 2 1000" 0181f381
32+
ad "call 0 3 2" 44000302
33+
ad "settabup 0 4 0" 0f000400
34+
ad "gettabup 0 0 4" 0b000004
35+
ad "self 0 0 3" 14000003
36+
ad "loadi 2 100" 01813180
37+
ad "call 0 3 1" 44000301
38+
ad "return 0 1 1" 46000101
39+
40+
ad "newtable 2 0 0" 13010000
41+
ad "gettabup 3 0 0" 8b010000
42+
ad "move 4 2" 00020200
43+
ad "gettabup 5 0 1" 8b020001
44+
ad "call 3 3 1" c4010301
45+
ad "setfield 2 2 1" 12010201
46+
47+
d "return1 2" 48010200 # wrong assembling
48+
#ad "return1 1" 48010200 # ??
49+
#ad "return0" c7010100 # ?? Not wrong in official
50+
d "return0" 47010100 # ?? Not wrong in official, but wrong assembling
51+
52+
ad "getfield 2 0 0" 0e010000
53+
ad "sub 2 2 1" 23010201
54+
ad "mmbin 2 1 7" 2e010107
55+
ad "setfield 0 0 2" 12000002

0 commit comments

Comments
 (0)