librz/arch/lua: reduced heap allocations during LuaJIT processing (#6596)

Co-authored-by: Arya-1-HR <[email protected]>
This commit is contained in:
Arya H R
2026-07-31 02:49:01 +08:00
committed by GitHub
co-authored by Arya-1-HR
parent c85a1fdde7
commit 2e834543d6
3 changed files with 26 additions and 30 deletions
+10 -10
View File
@@ -10,24 +10,24 @@ st32 luajitop_get_value(ut32 instr) {
return offset;
}
char *luajitop_new_str_3arg(char *opname, int a, int b, int c) {
return rz_str_newf("%s r%d %d %d", opname, a, b, c);
void luajitop_setf_asm_3arg(RzAsmOp *op, char *opname, int a, int b, int c) {
rz_asm_op_setf_asm(op, "%s r%d %d %d", opname, a, b, c);
}
char *luajitop_new_str_2arg(char *opname, int a, int b) {
return rz_str_newf("%s r%d %d", opname, a, b);
void luajitop_setf_asm_2arg(RzAsmOp *op, char *opname, int a, int b) {
rz_asm_op_setf_asm(op, "%s r%d %d", opname, a, b);
}
char *luajitop_new_str_1arg(char *opname, int a) {
return rz_str_newf("%s %d", opname, a);
void luajitop_setf_asm_1arg(RzAsmOp *op, char *opname, int a) {
rz_asm_op_setf_asm(op, "%s r%d", opname, a);
}
// For Register to constant value (e.g., KSHORT r1, 8)
char *luajitop_new_str_reg_const(const char *opname, ut32 a, st32 d) {
return rz_str_newf("%s r%d %d", opname, a, d);
void luajitop_setf_asm_reg_const(RzAsmOp *op, const char *opname, ut32 a, st32 d) {
rz_asm_op_setf_asm(op, "%s r%d %d", opname, a, d);
}
// For Register to Register (e.g., MOV r5, r4)
char *luajitop_new_str_reg_reg(const char *opname, ut32 a, ut32 d) {
return rz_str_newf("%s r%d r%d", opname, a, d);
void luajitop_setf_asm_reg_reg(RzAsmOp *op, const char *opname, ut32 a, st32 d) {
rz_asm_op_setf_asm(op, "%s r%d r%d", opname, a, d);
}
+5 -6
View File
@@ -150,12 +150,11 @@ int luajit_analysis_op(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const
/* Asm */
int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions instr, LuaJITOpCode opcode);
char *luajitop_new_str_3arg(char *opname, int a, int b, int c);
char *luajitop_new_str_2arg(char *opname, int a, int b);
char *luajitop_new_str_1arg(char *opname, int a);
char *luajitop_new_str_reg_reg(const char *opname, ut32 a, ut32 d);
char *luajitop_new_str_reg_const(const char *opname, ut32 a, st32 d);
void luajitop_setf_asm_3arg(RzAsmOp *op, char *opname, int a, int b, int c);
void luajitop_setf_asm_2arg(RzAsmOp *op, char *opname, int a, int b);
void luajitop_setf_asm_1arg(RzAsmOp *op, char *opname, int a);
void luajitop_setf_asm_reg_const(RzAsmOp *op, const char *opname, ut32 a, st32 d);
void luajitop_setf_asm_reg_reg(RzAsmOp *op, const char *opname, ut32 a, st32 d);
#define LUAJIT_GET_OPCODE(i) (cast(int, (i) & 0xFF))
#define LUAJIT_GET_A(i) (cast(int, ((i) >> 8) & 0xFF)) /*Get A*/
#define LUAJIT_GET_C(i) (cast(int, ((i) >> 16) & 0xFF)) /*Get B*/
+11 -14
View File
@@ -14,8 +14,6 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
int c = LUAJIT_GET_C(instr);
int d = LUAJIT_GET_D(instr);
char *asm_str = NULL;
switch (opcode) {
// A D FORMAT (2 Operands)
@@ -44,7 +42,7 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
case LUAJIT_OP_NOT: /* A D R(A) := ~R(D) */
case LUAJIT_OP_UNM: /* A D R(A) := -R(D) */
case LUAJIT_OP_LEN: /* A D R(A) := length of R(D) */
asm_str = luajitop_new_str_reg_reg(opname, a, d);
luajitop_setf_asm_reg_reg(op, opname, a, d);
break;
/* Constant loads */
case LUAJIT_OP_KSTR: /* A D R(A) := KSTR(D) */
@@ -64,7 +62,7 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
case LUAJIT_OP_TDUP: /* A D R(A) := duplicate table(KTAB(D)) */
case LUAJIT_OP_GGET: /* A D R(A) := _G[KSTR(D)] */
case LUAJIT_OP_GSET: /* A D _G[KSTR(D)] := R(A) */
asm_str = luajitop_new_str_reg_const(opname, a, d);
luajitop_setf_asm_reg_const(op, opname, a, d);
break;
/* Calls and vararg handling (AD only) */
case LUAJIT_OP_CALLMT: /* A D return R(A)(R(A+1)...) */
@@ -87,18 +85,18 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
case LUAJIT_OP_LOOP: /* A D generic loop body */
case LUAJIT_OP_ILOOP: /* A D interpreted loop body */
case LUAJIT_OP_JLOOP: /* A D JIT-compiled loop body */
asm_str = luajitop_new_str_2arg(opname, a, d);
luajitop_setf_asm_2arg(op, opname, a, d);
break;
case LUAJIT_OP_JMP: /* A D pc += (D - 0x8000) */
d = luajitop_get_value(instr);
asm_str = luajitop_new_str_2arg(opname, a, d);
luajitop_setf_asm_2arg(op, opname, a, d);
break;
case LUAJIT_OP_KSHORT: /* A D R(A) := (int16_t)D */
// Note: Needs the cast to signed 16-bit
asm_str = luajitop_new_str_reg_const(opname, a, (st16)d);
luajitop_setf_asm_reg_const(op, opname, a, (st16)d);
break;
case LUAJIT_OP_KNIL: /* A D R(A) ... R(D) := nil */
asm_str = rz_str_newf("%s r%d...r%d", opname, a, d);
rz_asm_op_setf_asm(op, "%s r%d...r%d", opname, a, d);
break;
// A B C FORMAT (3 Operands)
@@ -136,7 +134,7 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
case LUAJIT_OP_ITERC: /* A B C R(A)... := R(A-3)(R(A-2), R(A-1)) */
case LUAJIT_OP_ITERN: /* A B C fast loop iterator */
case LUAJIT_OP_VARG: /* A B C R(A)...R(A+B-2) := vararg */
asm_str = luajitop_new_str_3arg(opname, a, b, c);
luajitop_setf_asm_3arg(op, opname, a, b, c);
break;
// A FORMAT (1 Operands)
@@ -149,17 +147,16 @@ int luajit_disasm(RzAsmOp *op, int len, LuaJITOpName opname, LuaJITInstructions
case LUAJIT_OP_JFUNCV: /* A JIT-compiled function header, varargs */
case LUAJIT_OP_FUNCC: /* A C function header */
case LUAJIT_OP_FUNCCW: /* A C function header with wrapper */
asm_str = luajitop_new_str_1arg(opname, a);
luajitop_setf_asm_1arg(op, opname, a);
break;
// FALLBACKS
case LUAJIT_OP__MAX: /* The maximum opcode */
asm_str = rz_str_newf("Max");
rz_asm_op_setf_asm(op, "%s", "Max");
break;
default:
asm_str = rz_str_newf("invalid");
rz_asm_op_setf_asm(op, "%s", "invalid");
break;
}
op->size = 4;
rz_asm_op_set_asm(op, asm_str);
RZ_FREE(asm_str);
return 4;
}