Rizin
unix-like reverse engineering framework and cli tools
analysis_53.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 // SPDX-FileCopyrightText: 2017 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
4 
5 #include "arch_53.h"
6 
7 int lua53_anal_op(RzAnalysis *anal, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len) {
8  if (!op) {
9  return 0;
10  }
11 
12  memset(op, 0, sizeof(RzAnalysisOp));
14 
15  ut32 extra_arg = 0;
16  op->addr = addr;
17  op->size = 4;
19  op->eob = false;
20 
22  return op->size;
23  }
24  // op->mnemonic = strdup ();
25 
26  switch (GET_OPCODE(instruction)) {
27  case OP_MOVE: /* A B R(A) := R(B) */
29  break;
30  case OP_LOADK: /* A Bx R(A) := Kst(Bx) */
32  break;
33  case OP_LOADKX: /* A R(A) := Kst(extra arg) */
35  extra_arg = lua_build_instruction(data + 4);
36  if (GET_OPCODE(extra_arg) == OP_EXTRAARG) {
37  op->size = 8;
38  }
39  break;
40  case OP_LOADBOOL: /* A B C R(A) := (Bool)B; if (C) pc++ */
42  op->val = !!GETARG_B(instruction);
43  op->jump = op->addr + 8;
44  op->fail = op->addr + 4;
45  break;
46  case OP_LOADNIL: /* A B R(A), R(A+1), ..., R(A+B) := nil */
47  break;
48  case OP_GETUPVAL: /* A B R(A) := UpValue[B] */
49  case OP_GETTABUP: /* A B C R(A) := UpValue[B][RK(C)] */
51  break;
52  case OP_GETTABLE: /* A B C R(A) := R(B)[RK(C)] */
53  break;
54 
55  case OP_SETTABUP: /* A B C UpValue[A][RK(B)] := RK(C) */
56  case OP_SETUPVAL: /* A B UpValue[B] := R(A) */
58  break;
59  case OP_SETTABLE: /* A B C R(A)[RK(B)] := RK(C) */
60  break;
61  case OP_NEWTABLE: /* A B C R(A) := {} (size = B,C) */
63  break;
64  case OP_SELF: /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
65  break;
66  case OP_ADD: /* A B C R(A) := RK(B) + RK(C) */
68  break;
69  case OP_SUB: /* A B C R(A) := RK(B) - RK(C) */
71  break;
72  case OP_MUL: /* A B C R(A) := RK(B) * RK(C) */
74  break;
75  case OP_MOD: /* A B C R(A) := RK(B) % RK(C) */
77  break;
78  case OP_POW: /* A B C R(A) := RK(B) ^ RK(C) */
79  break;
80  case OP_DIV: /* A B C R(A) := RK(B) / RK(C) */
81  case OP_IDIV: /* A B C R(A) := RK(B) // RK(C) */
83  break;
84  case OP_BAND: /* A B C R(A) := RK(B) & RK(C) */
86  break;
87  case OP_BOR: /* A B C R(A) := RK(B) | RK(C) */
88  op->type = RZ_ANALYSIS_OP_TYPE_OR;
89  break;
90  case OP_BXOR: /* A B C R(A) := RK(B) ~ RK(C) */
92  break;
93  case OP_SHL: /* A B C R(A) := RK(B) << RK(C) */
95  break;
96  case OP_SHR: /* A B C R(A) := RK(B) >> RK(C) */
98  break;
99  case OP_UNM: /* A B R(A) := -R(B) */
100  break;
101  case OP_BNOT: /* A B R(A) := ~R(B) */
102  op->type = RZ_ANALYSIS_OP_TYPE_CPL;
103  break;
104  case OP_NOT: /* A B R(A) := not R(B) */
105  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
106  break;
107  case OP_LEN: /* A B R(A) := length of R(B) */
108  case OP_CONCAT: /* A B C R(A) := R(B).. ... ..R(C) */
109  break;
110  case OP_JMP: /* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
112  op->jump = op->addr + (st32)(4 * GETARG_sBx(instruction));
113  op->fail = op->addr + 4;
114  break;
115  case OP_EQ: /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
116  case OP_LT: /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
117  case OP_LE: /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
118  case OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
120  op->jump = op->addr + 8;
121  op->fail = op->addr + 4;
122  break;
123  case OP_TESTSET: /* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
125  op->jump = op->addr + 8;
126  op->fail = op->addr + 4;
127  break;
128  case OP_CALL: /* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
130  break;
131  case OP_TAILCALL: /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
133  op->type2 = RZ_ANALYSIS_OP_TYPE_RET;
134  op->eob = true;
135  op->stackop = RZ_ANALYSIS_STACK_INC;
136  op->stackptr = -4;
137  break;
138  case OP_RETURN: /* A B return R(A), ... ,R(A+B-2) (see note) */
139  op->type = RZ_ANALYSIS_OP_TYPE_RET;
140  op->eob = true;
141  op->stackop = RZ_ANALYSIS_STACK_INC;
142  op->stackptr = -4;
143  break;
144  case OP_FORLOOP: /* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
146  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
147  op->fail = op->addr + 4;
148  break;
149  case OP_FORPREP: /* A sBx R(A)-=R(A+2); pc+=sBx */
150  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
151  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
152  op->fail = op->addr + 4;
153  break;
154  case OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
156  break;
157  case OP_TFORLOOP: /* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
159  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
160  op->fail = op->addr + 4;
161  break;
162  case OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
164  break;
165  case OP_CLOSURE: /* A Bx R(A) := closure(KPROTO[Bx]) */
166  case OP_VARARG: /* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
167  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
168  break;
169  }
170  return op->size;
171 }
size_t len
Definition: 6502dis.c:15
@ OP_DIV
Definition: 8051_ops.h:51
@ OP_ADD
Definition: 8051_ops.h:42
@ OP_MUL
Definition: 8051_ops.h:63
@ OP_JMP
Definition: 8051_ops.h:57
int lua53_anal_op(RzAnalysis *anal, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len)
Definition: analysis_53.c:7
#define GETARG_sBx(i)
Definition: arch_53.h:191
@ OP_SETLIST
Definition: arch_53.h:122
@ OP_EQ
Definition: arch_53.h:104
@ OP_VARARG
Definition: arch_53.h:126
@ OP_CONCAT
Definition: arch_53.h:101
@ OP_BOR
Definition: arch_53.h:92
@ OP_SETTABLE
Definition: arch_53.h:78
@ OP_POW
Definition: arch_53.h:88
@ OP_NOT
Definition: arch_53.h:98
@ OP_TESTSET
Definition: arch_53.h:109
@ OP_MOD
Definition: arch_53.h:87
@ OP_CLOSURE
Definition: arch_53.h:124
@ OP_SETUPVAL
Definition: arch_53.h:77
@ OP_FORPREP
Definition: arch_53.h:117
@ OP_LEN
Definition: arch_53.h:99
@ OP_LOADNIL
Definition: arch_53.h:70
@ OP_BAND
Definition: arch_53.h:91
@ OP_SELF
Definition: arch_53.h:82
@ OP_SUB
Definition: arch_53.h:85
@ OP_SHR
Definition: arch_53.h:95
@ OP_LT
Definition: arch_53.h:105
@ OP_TFORLOOP
Definition: arch_53.h:120
@ OP_SHL
Definition: arch_53.h:94
@ OP_TEST
Definition: arch_53.h:108
@ OP_TFORCALL
Definition: arch_53.h:119
@ OP_FORLOOP
Definition: arch_53.h:115
@ OP_GETTABLE
Definition: arch_53.h:74
@ OP_LOADK
Definition: arch_53.h:67
@ OP_GETUPVAL
Definition: arch_53.h:71
@ OP_SETTABUP
Definition: arch_53.h:76
@ OP_IDIV
Definition: arch_53.h:90
@ OP_GETTABUP
Definition: arch_53.h:73
@ OP_LE
Definition: arch_53.h:106
@ OP_RETURN
Definition: arch_53.h:113
@ OP_BNOT
Definition: arch_53.h:97
@ OP_MOVE
Definition: arch_53.h:66
@ OP_UNM
Definition: arch_53.h:96
@ OP_EXTRAARG
Definition: arch_53.h:128
@ OP_LOADKX
Definition: arch_53.h:68
@ OP_NEWTABLE
Definition: arch_53.h:80
@ OP_LOADBOOL
Definition: arch_53.h:69
@ OP_BXOR
Definition: arch_53.h:93
@ OP_TAILCALL
Definition: arch_53.h:112
#define GETARG_B(i)
Definition: arch_53.h:179
#define GET_OPCODE(i)
Definition: arch_53.h:168
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
LuaInstruction lua_build_instruction(const ut8 *buf)
Definition: lua_arch.c:6
#define OP_CALL
Definition: nios2.h:263
@ RZ_ANALYSIS_STACK_INC
Definition: rz_analysis.h:457
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ RZ_ANALYSIS_OP_TYPE_UNK
Definition: rz_analysis.h:388
@ RZ_ANALYSIS_OP_TYPE_MUL
Definition: rz_analysis.h:404
@ RZ_ANALYSIS_OP_TYPE_JMP
Definition: rz_analysis.h:368
@ RZ_ANALYSIS_OP_TYPE_AND
Definition: rz_analysis.h:411
@ RZ_ANALYSIS_OP_TYPE_MOD
Definition: rz_analysis.h:422
@ RZ_ANALYSIS_OP_TYPE_CMOV
Definition: rz_analysis.h:391
@ RZ_ANALYSIS_OP_TYPE_ADD
Definition: rz_analysis.h:401
@ RZ_ANALYSIS_OP_TYPE_OR
Definition: rz_analysis.h:410
@ RZ_ANALYSIS_OP_TYPE_STORE
Definition: rz_analysis.h:415
@ RZ_ANALYSIS_OP_TYPE_CPL
Definition: rz_analysis.h:429
@ RZ_ANALYSIS_OP_TYPE_SHR
Definition: rz_analysis.h:406
@ RZ_ANALYSIS_OP_TYPE_CJMP
Definition: rz_analysis.h:373
@ RZ_ANALYSIS_OP_TYPE_DIV
Definition: rz_analysis.h:405
@ RZ_ANALYSIS_OP_TYPE_MOV
Definition: rz_analysis.h:390
@ RZ_ANALYSIS_OP_TYPE_SHL
Definition: rz_analysis.h:407
@ RZ_ANALYSIS_OP_TYPE_NOT
Definition: rz_analysis.h:414
@ RZ_ANALYSIS_OP_TYPE_RET
Definition: rz_analysis.h:385
@ RZ_ANALYSIS_OP_TYPE_RCALL
Definition: rz_analysis.h:380
@ RZ_ANALYSIS_OP_TYPE_XOR
Definition: rz_analysis.h:412
@ RZ_ANALYSIS_OP_TYPE_NEW
Definition: rz_analysis.h:427
#define st32
Definition: rz_types_base.h:12
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58