From 7847c4f29ef547d59e2f46c7bdbf4c7a351efaa9 Mon Sep 17 00:00:00 2001 From: wargio Date: Fri, 5 Jan 2024 22:30:35 +0800 Subject: [PATCH] Remove hidden static in rz_analysis_cc_max_arg --- librz/analysis/cc.c | 33 ++++++++++++-------------- librz/analysis/d/cc-arm-16.sdb.txt | 2 ++ librz/analysis/d/cc-arm-32.sdb.txt | 3 +++ librz/analysis/d/cc-arm-64.sdb.txt | 2 ++ librz/analysis/d/cc-avr-8.sdb.txt | 1 + librz/analysis/d/cc-hexagon-32.sdb.txt | 2 ++ librz/analysis/d/cc-m68k-32.sdb.txt | 1 + librz/analysis/d/cc-mips-32.sdb.txt | 2 ++ librz/analysis/d/cc-mips-64.sdb.txt | 2 ++ librz/analysis/d/cc-ppc-32.sdb.txt | 1 + librz/analysis/d/cc-ppc-64.sdb.txt | 1 + librz/analysis/d/cc-riscv-64.sdb.txt | 1 + librz/analysis/d/cc-sh-32.sdb.txt | 1 + librz/analysis/d/cc-sparc-32.sdb.txt | 1 + librz/analysis/d/cc-spc700-16.sdb.txt | 1 + librz/analysis/d/cc-sysz-64.sdb.txt | 1 + librz/analysis/d/cc-tricore-32.sdb.txt | 2 ++ librz/analysis/d/cc-x86-16.sdb.txt | 5 ++++ librz/analysis/d/cc-x86-32.sdb.txt | 8 +++++++ librz/analysis/d/cc-x86-64.sdb.txt | 4 ++++ librz/analysis/d/cc-xtensa-32.sdb.txt | 1 + librz/analysis/var.c | 8 ++++--- test/db/cmd/cmd_k | 9 +++++++ test/unit/test_analysis_cc.c | 2 ++ test/unit/test_serialize_analysis.c | 2 ++ 25 files changed, 75 insertions(+), 21 deletions(-) diff --git a/librz/analysis/cc.c b/librz/analysis/cc.c index 07dd5a67b43..be0ffc19deb 100644 --- a/librz/analysis/cc.c +++ b/librz/analysis/cc.c @@ -25,6 +25,7 @@ RZ_API void rz_analysis_cc_del(RzAnalysis *analysis, const char *name) { rz_return_if_fail(analysis && name); sdb_unset(DB, name, 0); cc_sdb_unsetf(DB, "cc.%s.ret", name); + cc_sdb_unsetf(DB, "cc.%s.maxargs", name); cc_sdb_unsetf(DB, "cc.%s.argn", name); for (int i = 0; i < RZ_ANALYSIS_CC_MAXARG; i++) { cc_sdb_unsetf(DB, "cc.%s.arg%d", name, i); @@ -77,6 +78,11 @@ RZ_API bool rz_analysis_cc_set(RzAnalysis *analysis, const char *expr) { n++; } } + if (n > rz_analysis_cc_max_arg(analysis, ccname)) { + char maxargs[256]; + rz_strf(maxargs, "%d", n); + cc_sdb_setf(DB, maxargs, "cc.%s.maxargs", ccname); + } rz_list_free(ccArgs); free(e); free(args); @@ -197,27 +203,18 @@ RZ_API void rz_analysis_cc_set_error(RzAnalysis *analysis, const char *conventio } RZ_API int rz_analysis_cc_max_arg(RzAnalysis *analysis, const char *cc) { - int i = 0; rz_return_val_if_fail(analysis && DB && cc, 0); - static void *oldDB = NULL; - static char *oldCC = NULL; - static int oldArg = 0; - if (oldDB == DB && !strcmp(cc, oldCC)) { - return oldArg; + char *query = rz_str_newf("cc.%s.maxargs", cc); + if (!query) { + return 0; } - oldDB = DB; - free(oldCC); - oldCC = strdup(cc); - for (i = 0; i < RZ_ANALYSIS_CC_MAXARG; i++) { - char *query = rz_str_newf("cc.%s.arg%d", cc, i); - const char *res = query ? sdb_const_get(DB, query, 0) : NULL; - free(query); - if (!res) { - break; - } + const char *res = sdb_const_get(DB, query, 0); + free(query); + int maxargs = res ? atoi(res) : 0; + if (maxargs < 0 || maxargs > RZ_ANALYSIS_CC_MAXARG) { + return 0; } - oldArg = i; - return i; + return maxargs; } RZ_API const char *rz_analysis_cc_ret(RzAnalysis *analysis, const char *convention) { diff --git a/librz/analysis/d/cc-arm-16.sdb.txt b/librz/analysis/d/cc-arm-16.sdb.txt index 63e16048d7b..6a201fa2f54 100644 --- a/librz/analysis/d/cc-arm-16.sdb.txt +++ b/librz/analysis/d/cc-arm-16.sdb.txt @@ -6,6 +6,7 @@ cc.arm16.arg1=r1 cc.arm16.arg2=r2 cc.arm16.arg3=r3 cc.arm16.argn=stack +cc.arm16.maxargs=4 cc.arm16.ret=r0 arm32=cc @@ -13,4 +14,5 @@ cc.arm32.arg0=r0 cc.arm32.arg1=r1 cc.arm32.arg2=r2 cc.arm32.argn=stack +cc.arm16.maxargs=3 cc.arm32.ret=r0 diff --git a/librz/analysis/d/cc-arm-32.sdb.txt b/librz/analysis/d/cc-arm-32.sdb.txt index 0f61af4fb80..62af1c01bb7 100644 --- a/librz/analysis/d/cc-arm-32.sdb.txt +++ b/librz/analysis/d/cc-arm-32.sdb.txt @@ -3,6 +3,7 @@ default.cc=arm32 arm32=cc cc.arm32.arg0=r0 cc.arm32.arg1=r1 +cc.arm32.maxargs=2 cc.arm32.ret=r0 arm16=cc @@ -11,6 +12,7 @@ cc.arm16.arg1=r1 cc.arm16.arg2=r2 cc.arm16.arg3=r3 cc.arm16.argn=stack +cc.arm16.maxargs=4 cc.arm16.ret=r0 swift=cc @@ -19,6 +21,7 @@ cc.swift.arg1=r1 cc.swift.arg2=r2 cc.swift.arg3=r3 cc.swift.argn=stack +cc.swift.maxargs=4 cc.swift.self=r10 cc.swift.self=r8 cc.swift.ret=r0 diff --git a/librz/analysis/d/cc-arm-64.sdb.txt b/librz/analysis/d/cc-arm-64.sdb.txt index 3cc74135e20..b9f69c84afa 100644 --- a/librz/analysis/d/cc-arm-64.sdb.txt +++ b/librz/analysis/d/cc-arm-64.sdb.txt @@ -10,6 +10,7 @@ cc.arm64.arg5=x5 cc.arm64.arg6=x6 cc.arm64.arg7=x7 cc.arm64.argn=stack +cc.arm64.maxargs=8 cc.arm64.ret=x0 swift=cc @@ -22,6 +23,7 @@ cc.swift.arg5=x5 cc.swift.arg6=x6 cc.swift.arg7=x7 cc.swift.argn=stack +cc.swift.maxargs=8 cc.swift.self=x20 cc.swift.error=x21 cc.swift.ret=x0 diff --git a/librz/analysis/d/cc-avr-8.sdb.txt b/librz/analysis/d/cc-avr-8.sdb.txt index 263aaaf53d8..9b55fe23fdd 100644 --- a/librz/analysis/d/cc-avr-8.sdb.txt +++ b/librz/analysis/d/cc-avr-8.sdb.txt @@ -9,4 +9,5 @@ cc.avr.arg4=r22 cc.avr.arg5=r21 cc.avr.arg6=r18 cc.avr.argn=stack +cc.avr.maxargs=7 cc.avr.ret=r24 diff --git a/librz/analysis/d/cc-hexagon-32.sdb.txt b/librz/analysis/d/cc-hexagon-32.sdb.txt index eba9cb0e7e8..353794bfc13 100644 --- a/librz/analysis/d/cc-hexagon-32.sdb.txt +++ b/librz/analysis/d/cc-hexagon-32.sdb.txt @@ -8,6 +8,7 @@ cc.hexagon.arg3=r3 cc.hexagon.arg4=r4 cc.hexagon.arg5=r5 cc.hexagon.argn=stack_rev +cc.hexagon.maxargs=6 cc.hexagon.ret=r0 hvx=cc @@ -28,4 +29,5 @@ cc.hvx.arg12=v12 cc.hvx.arg13=v13 cc.hvx.arg14=v14 cc.hvx.arg15=v15 +cc.hvx.maxargs=16 cc.hvx.ret=v0 diff --git a/librz/analysis/d/cc-m68k-32.sdb.txt b/librz/analysis/d/cc-m68k-32.sdb.txt index 637c9ce0f9d..3d31e787f32 100644 --- a/librz/analysis/d/cc-m68k-32.sdb.txt +++ b/librz/analysis/d/cc-m68k-32.sdb.txt @@ -2,4 +2,5 @@ default.cc=m68k m68k=cc cc.m68k.arg0=stack_rev +cc.m68k.maxargs=1 cc.m68k.ret=d0 diff --git a/librz/analysis/d/cc-mips-32.sdb.txt b/librz/analysis/d/cc-mips-32.sdb.txt index 31b57d29a3c..72aafdb4b76 100644 --- a/librz/analysis/d/cc-mips-32.sdb.txt +++ b/librz/analysis/d/cc-mips-32.sdb.txt @@ -6,6 +6,7 @@ cc.o32.arg1=a1 cc.o32.arg2=a2 cc.o32.arg3=a3 cc.o32.argn=stack +cc.o32.maxargs=4 cc.o32.ret=v0 n32=cc @@ -18,5 +19,6 @@ cc.n32.arg5=a5 cc.n32.arg6=a6 cc.n32.arg7=a7 cc.n32.argn=stack +cc.n32.maxargs=8 cc.n32.ret=v0 diff --git a/librz/analysis/d/cc-mips-64.sdb.txt b/librz/analysis/d/cc-mips-64.sdb.txt index 31b57d29a3c..72aafdb4b76 100644 --- a/librz/analysis/d/cc-mips-64.sdb.txt +++ b/librz/analysis/d/cc-mips-64.sdb.txt @@ -6,6 +6,7 @@ cc.o32.arg1=a1 cc.o32.arg2=a2 cc.o32.arg3=a3 cc.o32.argn=stack +cc.o32.maxargs=4 cc.o32.ret=v0 n32=cc @@ -18,5 +19,6 @@ cc.n32.arg5=a5 cc.n32.arg6=a6 cc.n32.arg7=a7 cc.n32.argn=stack +cc.n32.maxargs=8 cc.n32.ret=v0 diff --git a/librz/analysis/d/cc-ppc-32.sdb.txt b/librz/analysis/d/cc-ppc-32.sdb.txt index f38d6471431..834e9a2b445 100644 --- a/librz/analysis/d/cc-ppc-32.sdb.txt +++ b/librz/analysis/d/cc-ppc-32.sdb.txt @@ -10,5 +10,6 @@ cc.ppc-32.arg5=r8 cc.ppc-32.arg6=r9 cc.ppc-32.arg7=r10 cc.ppc-32.argn=stack_rev +cc.ppc-32.maxargs=8 cc.ppc-32.ret=r3 diff --git a/librz/analysis/d/cc-ppc-64.sdb.txt b/librz/analysis/d/cc-ppc-64.sdb.txt index acbc6e51bdd..6df7c46f6b8 100644 --- a/librz/analysis/d/cc-ppc-64.sdb.txt +++ b/librz/analysis/d/cc-ppc-64.sdb.txt @@ -10,5 +10,6 @@ cc.ppc-64.arg5=r8 cc.ppc-64.arg6=r9 cc.ppc-64.arg7=r10 cc.ppc-64.argn=stack_rev +cc.ppc-64.maxargs=8 cc.ppc-64.ret=r3 diff --git a/librz/analysis/d/cc-riscv-64.sdb.txt b/librz/analysis/d/cc-riscv-64.sdb.txt index 22e50b4e193..c594c654f99 100644 --- a/librz/analysis/d/cc-riscv-64.sdb.txt +++ b/librz/analysis/d/cc-riscv-64.sdb.txt @@ -9,4 +9,5 @@ cc.rvg.arg4=a4 cc.rvg.arg5=a5 cc.rvg.arg6=a6 cc.rvg.arg7=a7 +cc.rvg.maxargs=8 cc.rvg.ret=a0 diff --git a/librz/analysis/d/cc-sh-32.sdb.txt b/librz/analysis/d/cc-sh-32.sdb.txt index e0231bb901e..a7d731fb4a5 100644 --- a/librz/analysis/d/cc-sh-32.sdb.txt +++ b/librz/analysis/d/cc-sh-32.sdb.txt @@ -5,4 +5,5 @@ cc.sh4.arg0=r4 cc.sh4.arg1=r5 cc.sh4.arg2=r6 cc.sh4.arg3=r7 +cc.sh4.maxargs=4 cc.sh4.ret=r0 diff --git a/librz/analysis/d/cc-sparc-32.sdb.txt b/librz/analysis/d/cc-sparc-32.sdb.txt index 85641f1a72f..1011dc2f633 100644 --- a/librz/analysis/d/cc-sparc-32.sdb.txt +++ b/librz/analysis/d/cc-sparc-32.sdb.txt @@ -8,4 +8,5 @@ cc.sparc.arg3=i3 cc.sparc.arg4=i4 cc.sparc.arg5=i5 cc.sparc.argn=stack +cc.sparc.maxargs=6 cc.sparc.ret=o0 diff --git a/librz/analysis/d/cc-spc700-16.sdb.txt b/librz/analysis/d/cc-spc700-16.sdb.txt index 589f4c1e891..8867cb048f7 100644 --- a/librz/analysis/d/cc-spc700-16.sdb.txt +++ b/librz/analysis/d/cc-spc700-16.sdb.txt @@ -4,4 +4,5 @@ spc700=cc cc.spc700.arg0=a cc.spc700.arg1=x cc.spc700.arg2=y +cc.spc700.maxargs=3 cc.spc700.ret=a diff --git a/librz/analysis/d/cc-sysz-64.sdb.txt b/librz/analysis/d/cc-sysz-64.sdb.txt index f9a8fc28bd7..f0ef7889195 100644 --- a/librz/analysis/d/cc-sysz-64.sdb.txt +++ b/librz/analysis/d/cc-sysz-64.sdb.txt @@ -6,4 +6,5 @@ cc.sysz.arg1=r3 cc.sysz.arg2=r4 cc.sysz.arg3=r5 cc.sysz.arg4=r6 +cc.sysz.maxargs=5 cc.sysz.ret=r2 diff --git a/librz/analysis/d/cc-tricore-32.sdb.txt b/librz/analysis/d/cc-tricore-32.sdb.txt index 6065f61d218..39e0ba27188 100644 --- a/librz/analysis/d/cc-tricore-32.sdb.txt +++ b/librz/analysis/d/cc-tricore-32.sdb.txt @@ -10,8 +10,10 @@ cc.reg.arg5=a5 cc.reg.arg6=a6 cc.reg.arg7=a7 cc.reg.argn=stack +cc.reg.maxargs=8 cc.reg.ret=d2 stack=cc cc.stack.argn=stack +cc.stack.maxargs=0 cc.stack.ret=d2 diff --git a/librz/analysis/d/cc-x86-16.sdb.txt b/librz/analysis/d/cc-x86-16.sdb.txt index 3d5044ec78a..3e761aa97d8 100644 --- a/librz/analysis/d/cc-x86-16.sdb.txt +++ b/librz/analysis/d/cc-x86-16.sdb.txt @@ -6,6 +6,7 @@ cc.fastcall.arg1=dx cc.fastcall.arg2=bx cc.fastcall.arg3=cx cc.fastcall.argn=stack +cc.fastcall.maxargs=4 cc.fastcall.ret=ax watcom=cc @@ -14,6 +15,7 @@ cc.watcom.arg1=dx cc.watcom.arg2=bx cc.watcom.arg3=cx cc.watcom.argn=stack +cc.watcom.maxargs=4 cc.watcom.ret=si turboc=cc @@ -21,12 +23,14 @@ cc.turboc.arg0=ax cc.turboc.arg1=dx cc.turboc.arg2=bx cc.turboc.argn=stack +cc.turboc.maxargs=3 cc.turboc.ret=ax ms=cc cc.ms.arg0=ax cc.ms.arg1=dx cc.ms.argn=stack +cc.ms.maxargs=2 cc.ms.ret=ax msm=cc @@ -34,4 +38,5 @@ cc.msm.arg0=ax cc.msm.arg1=dx cc.msm.arg2=bx cc.msm.argn=stack +cc.msm.maxargs=3 cc.msm.ret=bx diff --git a/librz/analysis/d/cc-x86-32.sdb.txt b/librz/analysis/d/cc-x86-32.sdb.txt index a9d5955020e..d5c8baa0b36 100644 --- a/librz/analysis/d/cc-x86-32.sdb.txt +++ b/librz/analysis/d/cc-x86-32.sdb.txt @@ -2,11 +2,13 @@ default.cc=cdecl cdecl=cc cc.cdecl.argn=stack +cc.cdecl.maxargs=0 cc.cdecl.ret=eax cdecl-thiscall-ms=cc cc.cdecl-thiscall-ms.arg0=ecx cc.cdecl-fastcall-ms.argn=stack +cc.cdecl-fastcall-ms.maxargs=1 cc.cdecl-fastcall-ms.ret=eax optlink=cc @@ -14,22 +16,26 @@ cc.optlink.arg0=eax cc.optlink.arg1=edx cc.optlink.arg2=ecx cc.optlink.argn=stack +cc.optlink.maxargs=3 cc.optlink.ret=eax stdcall=cc cc.stdcall.argn=stack +cc.stdcall.maxargs=0 cc.stdcall.ret=eax fastcall=cc cc.fastcall.arg0=ecx cc.fastcall.arg1=edx cc.fastcall.argn=stack +cc.fastcall.maxargs=2 cc.fastcall.ret=eax borland=cc cc.borland.arg0=eax cc.borland.arg1=edx cc.borland.arg2=ecx +cc.borland.maxargs=3 cc.borland.argn=stack_rev watcom=cc @@ -38,6 +44,8 @@ cc.watcom.arg1=edx cc.watcom.arg2=ebx cc.watcom.arg3=ecx cc.watcom.argn=stack +cc.watcom.maxargs=4 pascal=cc cc.pascal.argn=stack_rev +cc.pascal.maxargs=0 diff --git a/librz/analysis/d/cc-x86-64.sdb.txt b/librz/analysis/d/cc-x86-64.sdb.txt index b8366729cb0..9d2fd5aacf1 100644 --- a/librz/analysis/d/cc-x86-64.sdb.txt +++ b/librz/analysis/d/cc-x86-64.sdb.txt @@ -6,6 +6,7 @@ cc.ms.arg1=rdx cc.ms.arg2=r8 cc.ms.arg3=r9 cc.ms.argn=stack +cc.ms.maxargs=4 cc.ms.ret=rax amd64=cc @@ -20,6 +21,7 @@ cc.amd64.arg7=xmm1 cc.amd64.arg8=xmm2 cc.amd64.arg9=xmm3 cc.amd64.arg10=xmm4 +cc.amd64.maxargs=11 cc.amd64.ret=rax swift=cc @@ -34,6 +36,7 @@ cc.swift.arg7=xmm1 cc.swift.arg8=xmm2 cc.swift.arg9=xmm3 cc.swift.arg10=xmm4 +cc.swift.maxargs=11 cc.swift.self=r13 cc.swift.error=r12 cc.swift.ret=rax @@ -45,4 +48,5 @@ cc.amd64syscall.arg2=rdx cc.amd64syscall.arg3=r10 cc.amd64syscall.arg4=r8 cc.amd64syscall.arg5=r9 +cc.amd64syscall.maxargs=6 cc.amd64syscall.ret=rax diff --git a/librz/analysis/d/cc-xtensa-32.sdb.txt b/librz/analysis/d/cc-xtensa-32.sdb.txt index ff8fed32186..2c17c5e30c0 100644 --- a/librz/analysis/d/cc-xtensa-32.sdb.txt +++ b/librz/analysis/d/cc-xtensa-32.sdb.txt @@ -8,4 +8,5 @@ cc.call0.arg3=a5 cc.call0.arg4=a6 cc.call0.arg5=a7 cc.call0.argn=stack +cc.call0.maxargs=6 cc.call0.ret=a2 diff --git a/librz/analysis/var.c b/librz/analysis/var.c index 753c391ca0e..a4770049557 100644 --- a/librz/analysis/var.c +++ b/librz/analysis/var.c @@ -779,9 +779,11 @@ RZ_API int rz_analysis_var_get_argnum(RzAnalysisVar *var) { if (!reg) { return -1; } - int i; - int arg_max = var->fcn->cc ? rz_analysis_cc_max_arg(analysis, var->fcn->cc) : 0; - for (i = 0; i < arg_max; i++) { + int arg_max = 0; + if (RZ_STR_ISNOTEMPTY(var->fcn->cc)) { + arg_max = rz_analysis_cc_max_arg(analysis, var->fcn->cc); + } + for (int i = 0; i < arg_max; i++) { const char *reg_arg = rz_analysis_cc_arg(analysis, var->fcn->cc, i); if (reg_arg && !strcmp(reg->name, reg_arg)) { return i; diff --git a/test/db/cmd/cmd_k b/test/db/cmd/cmd_k index 7c451b6a00d..2cf8917a3f6 100644 --- a/test/db/cmd/cmd_k +++ b/test/db/cmd/cmd_k @@ -56,33 +56,42 @@ EXPECT=<