Rizin
unix-like reverse engineering framework and cli tools
analysis_54.c File Reference
#include "arch_54.h"

Go to the source code of this file.

Functions

int lua54_anal_op (RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len)
 

Function Documentation

◆ lua54_anal_op()

int lua54_anal_op ( RzAnalysis analysis,
RzAnalysisOp op,
ut64  addr,
const ut8 data,
int  len 
)

Definition at line 6 of file analysis_54.c.

6  {
7  if (!op || len < 4) {
8  return 0;
9  }
10 
11  memset(op, 0, sizeof(RzAnalysisOp));
13 
14  op->size = 4;
15  op->addr = addr;
16 
18  return op->size;
19  }
20 
21  switch (LUA_GET_OPCODE(instruction)) {
22  case OP_MOVE: /* A B R[A] := R[B] */
24  break;
25  case OP_LOADI: /* A sBx R[A] := sBx */
26  case OP_LOADF: /* A sBx R[A] := (lua_Number)sBx */
27  case OP_LOADK: /* A Bx R[A] := K[Bx] */
28  case OP_LOADTRUE: /* A R[A] := true */
29  case OP_LOADNIL: /* A B R[A], R[A+1], ..., R[A+B] := nil */
30  case OP_LOADFALSE: /* A R[A] := false */
32  break;
33  case OP_LOADKX: /* A R[A] := K[extra arg] */
35  op->size = 4;
36  break;
37  case OP_LFALSESKIP: /*A R[A] := false; pc++ */
39  op->size = 8;
40  break;
41  case OP_GETTABUP: /* A B C R[A] := UpValue[B][K[C]:string] */
42  case OP_GETUPVAL: /* A B R[A] := UpValue[B] */
43  case OP_GETI: /* A B C R[A] := R[B][C] */
44  case OP_GETFIELD: /* A B C R[A] := R[B][K[C]:string] */
45  case OP_GETTABLE: /* A B C R[A] := R[B][R[C]] */
46  case OP_SETTABLE: /* A B C R[A][R[B]] := RK(C) */
48  break;
49  case OP_SETTABUP: /* A B C UpValue[A][K[B]:string] := RK(C) */
50  case OP_SETUPVAL: /* A B UpValue[B] := R[A] */
51  case OP_SETI: /* A B C R[A][B] := RK(C) */
52  case OP_SETFIELD: /* A B C R[A][K[B]:string] := RK(C) */
54  break;
55  case OP_NEWTABLE: /* A B C k R[A] := {} */
57  op->size = 4;
58  break;
59  case OP_SELF: /* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
60  break;
61  case OP_ADDI: /* A B sC R[A] := R[B] + sC */
62  case OP_ADDK: /* A B C R[A] := R[B] + K[C] */
63  case OP_ADD: /* A B C R[A] := R[B] + R[C] */
65  break;
66  case OP_SUBK: /* A B C R[A] := R[B] - K[C] */
67  case OP_SUB: /* A B C R[A] := R[B] - R[C] */
69  break;
70  case OP_MULK: /* A B C R[A] := R[B] * K[C] */
71  case OP_MUL: /* A B C R[A] := R[B] * R[C] */
73  break;
74  case OP_MOD: /* A B C R[A] := R[B] % R[C] */
75  case OP_MODK: /* A B C R[A] := R[B] % K[C] */
77  break;
78  case OP_POW: /* A B C R[A] := R[B] ^ R[C] */
79  case OP_POWK: /* A B C R[A] := R[B] ^ K[C] */
80  break;
81  case OP_DIVK: /* A B C R[A] := R[B] / K[C] */
82  case OP_IDIVK: /* A B C R[A] := R[B] // K[C] */
83  case OP_DIV: /* A B C R[A] := R[B] / R[C] */
84  case OP_IDIV: /* A B C R[A] := R[B] // R[C] */
86  break;
87  case OP_BANDK: /* A B C R[A] := R[B] & K[C]:integer */
88  case OP_BAND: /* A B C R[A] := R[B] & R[C] */
90  break;
91  case OP_BOR: /* A B C R[A] := R[B] | R[C] */
92  case OP_BORK: /* A B C R[A] := R[B] | K[C]:integer */
93  op->type = RZ_ANALYSIS_OP_TYPE_OR;
94  break;
95  case OP_BXOR: /* A B C R[A] := R[B] ~ R[C] */
96  case OP_BXORK: /* A B C R[A] := R[B] ~ K[C]:integer */
98  break;
99  case OP_NOT: /* A B R[A] := not R[B] */
100  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
101  break;
102  case OP_BNOT: /* A B R[A] := ~R[B] */
103  op->type = RZ_ANALYSIS_OP_TYPE_CPL;
104  break;
105  case OP_SHRI: /* A B sC R[A] := R[B] >> sC */
106  case OP_SHR: /* A B C R[A] := R[B] >> R[C] */
107  op->type = RZ_ANALYSIS_OP_TYPE_SHR;
108  break;
109  case OP_SHLI: /* A B sC R[A] := sC << R[B] */
110  case OP_SHL: /* A B C R[A] := R[B] << R[C] */
111  op->type = RZ_ANALYSIS_OP_TYPE_SHL;
112  break;
113  case OP_MMBIN: /* A B C call C metamethod over R[A] and R[B] */
114  case OP_MMBINI: /* A sB C k call C metamethod over R[A] and sB */
115  case OP_MMBINK: /* A B C k call C metamethod over R[A] and K[B] */
117  break;
118  case OP_UNM: /* A B R[A] := -R[B] */
119  case OP_LEN: /* A B R[A] := #R[B] (length operator) */
120  case OP_CONCAT: /* A B R[A] := R[A].. ... ..R[A + B - 1] */
121  case OP_CLOSE: /* A close all upvalues >= R[A] */
122  case OP_TBC: /* A mark variable A "to be closed" */
123  break;
124  case OP_JMP: /* sJ pc += sJ */
126  op->jump = op->addr + (st32)(4 * (LUA_GETARG_sJ(instruction)));
127  op->fail = op->addr + 4;
128  break;
129  case OP_EQ: /* A B k if ((R[A] == R[B]) ~= k) then pc++ */
130  case OP_LT: /* A B k if ((R[A] < R[B]) ~= k) then pc++ */
131  case OP_LE: /* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
132  case OP_EQK: /* A B k if ((R[A] == K[B]) ~= k) then pc++ */
133  case OP_EQI: /* A sB k if ((R[A] == sB) ~= k) then pc++ */
134  case OP_LTI: /* A sB k if ((R[A] < sB) ~= k) then pc++ */
135  case OP_LEI: /* A sB k if ((R[A] <= sB) ~= k) then pc++ */
136  case OP_GTI: /* A sB k if ((R[A] > sB) ~= k) then pc++ */
137  case OP_GEI: /* A sB k if ((R[A] >= sB) ~= k) then pc++ */
138  case OP_TEST: /* A k if (not R[A] == k) then pc++ */
140  op->jump = op->addr + 8;
141  op->fail = op->addr + 4;
142  break;
143  case OP_TESTSET: /* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
145  op->jump = op->addr + 8;
146  op->fail = op->addr + 4;
147  break;
148  case OP_CALL: /* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
150  break;
151  case OP_TAILCALL: /* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
154  op->eob = true;
155  op->stackop = RZ_ANALYSIS_STACK_INC;
156  op->stackptr = -4;
157  break;
158  case OP_RETURN: /* A B C k return R[A], ... ,R[A+B-2] (see note) */
159  case OP_RETURN1: /* A return R[A] */
160  case OP_RETURN0: /* return */
161  op->type = RZ_ANALYSIS_OP_TYPE_RET;
162  op->eob = true;
163  op->stackop = RZ_ANALYSIS_STACK_INC;
164  op->stackptr = -4;
165  break;
166  case OP_FORLOOP: /* A Bx update counters; if loop continues then pc-=Bx; */
167  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
168  op->jump = op->addr + 4 - 4 * (LUA_GETARG_Bx(instruction));
169  op->fail = op->addr + 4;
170  break;
171  case OP_FORPREP: /* A Bx <check values and prepare counters>;
172  if not to run then pc+=Bx+1; */
173  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
174  op->jump = op->addr + 4 + 4 * (LUA_GETARG_Bx(instruction) + 1);
175  op->fail = op->addr + 4;
176  break;
177  case OP_TFORPREP: /* A Bx create upvalue for R[A + 3]; pc+=Bx */
178  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
179  op->jump = op->addr + 4 + 4 * (LUA_GETARG_Bx(instruction));
180  op->fail = op->addr + 4;
181  break;
182  case OP_TFORCALL: /* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
184  break;
185  case OP_TFORLOOP: /* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
187  op->jump = op->addr + 4 - 4 * (LUA_GETARG_Bx(instruction));
188  op->fail = op->addr + 4;
189  break;
190  case OP_SETLIST: /* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
192  break;
193  case OP_CLOSURE: /* A Bx R[A] := closure(KPROTO[Bx]) */
194  case OP_VARARG: /* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
195  case OP_VARARGPREP: /*A (adjust vararg parameters) */
196  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
197  op->size = 4;
198  break;
199  }
200  return op->size;
201 }
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
@ 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_BXOR
Definition: arch_53.h:93
@ OP_TAILCALL
Definition: arch_53.h:112
#define LUA_GET_OPCODE(i)
Definition: arch_54.h:198
@ OP_MMBINK
Definition: arch_54.h:131
@ OP_EQI
Definition: arch_54.h:148
@ OP_VARARGPREP
Definition: arch_54.h:178
@ OP_LFALSESKIP
Definition: arch_54.h:78
@ OP_MMBINI
Definition: arch_54.h:130
@ OP_POWK
Definition: arch_54.h:104
@ OP_BANDK
Definition: arch_54.h:108
@ OP_MMBIN
Definition: arch_54.h:129
@ OP_MULK
Definition: arch_54.h:102
@ OP_SETFIELD
Definition: arch_54.h:92
@ OP_TBC
Definition: arch_54.h:141
@ OP_GTI
Definition: arch_54.h:151
@ OP_SETI
Definition: arch_54.h:91
@ OP_BORK
Definition: arch_54.h:109
@ OP_LOADI
Definition: arch_54.h:73
@ OP_LTI
Definition: arch_54.h:149
@ OP_LOADF
Definition: arch_54.h:74
@ OP_RETURN1
Definition: arch_54.h:162
@ OP_LOADFALSE
Definition: arch_54.h:77
@ OP_DIVK
Definition: arch_54.h:105
@ OP_SHRI
Definition: arch_54.h:112
@ OP_GETI
Definition: arch_54.h:86
@ OP_IDIVK
Definition: arch_54.h:106
@ OP_CLOSE
Definition: arch_54.h:140
@ OP_ADDK
Definition: arch_54.h:100
@ OP_TFORPREP
Definition: arch_54.h:168
@ OP_SHLI
Definition: arch_54.h:113
@ OP_GETFIELD
Definition: arch_54.h:87
@ OP_LOADTRUE
Definition: arch_54.h:79
@ OP_EQK
Definition: arch_54.h:147
@ OP_MODK
Definition: arch_54.h:103
@ OP_GEI
Definition: arch_54.h:152
@ OP_RETURN0
Definition: arch_54.h:161
@ OP_LEI
Definition: arch_54.h:150
@ OP_BXORK
Definition: arch_54.h:110
@ OP_SUBK
Definition: arch_54.h:101
#define LUA_GETARG_sJ(i)
Definition: arch_54.h:213
#define LUA_GETARG_Bx(i)
Definition: arch_54.h:210
return memset(p, 0, total)
LuaInstruction lua_build_instruction(const ut8 *buf)
Definition: lua_arch.c:6
ut32 LuaInstruction
Definition: lua_arch.h:27
#define OP_CALL
Definition: nios2.h:263
#define OP_ADDI
Definition: nios2.h:253
@ RZ_ANALYSIS_STACK_INC
Definition: rz_analysis.h:457
@ RZ_ANALYSIS_ADDR_HINT_TYPE_RET
Definition: rz_analysis.h:636
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ 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_CALL
Definition: rz_analysis.h:378
@ 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
static int addr
Definition: z80asm.c:58

References addr, len, lua_build_instruction(), LUA_GET_OPCODE, LUA_GETARG_Bx, LUA_GETARG_sJ, memset(), OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXOR, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MOD, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, RZ_ANALYSIS_ADDR_HINT_TYPE_RET, RZ_ANALYSIS_OP_TYPE_ADD, RZ_ANALYSIS_OP_TYPE_AND, RZ_ANALYSIS_OP_TYPE_CALL, RZ_ANALYSIS_OP_TYPE_CJMP, RZ_ANALYSIS_OP_TYPE_CMOV, RZ_ANALYSIS_OP_TYPE_CPL, RZ_ANALYSIS_OP_TYPE_DIV, RZ_ANALYSIS_OP_TYPE_JMP, RZ_ANALYSIS_OP_TYPE_LOAD, RZ_ANALYSIS_OP_TYPE_MOD, RZ_ANALYSIS_OP_TYPE_MOV, RZ_ANALYSIS_OP_TYPE_MUL, RZ_ANALYSIS_OP_TYPE_NEW, RZ_ANALYSIS_OP_TYPE_NOT, RZ_ANALYSIS_OP_TYPE_OR, RZ_ANALYSIS_OP_TYPE_RCALL, RZ_ANALYSIS_OP_TYPE_RET, RZ_ANALYSIS_OP_TYPE_SHL, RZ_ANALYSIS_OP_TYPE_SHR, RZ_ANALYSIS_OP_TYPE_STORE, RZ_ANALYSIS_OP_TYPE_SUB, RZ_ANALYSIS_OP_TYPE_XOR, RZ_ANALYSIS_STACK_INC, and st32.

Referenced by rz_lua_analysis_op().