From 8f1fdc91d67e73555beec12bb0c8ce6adc6ad7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Tue, 17 Jan 2023 08:54:37 +0100 Subject: [PATCH] Limit op cache in type matching (#3297) Reduces the peak memory usage of the corkami virtgap.exe test from 1.177GB to 241.9MB (measured with massif). --- librz/core/analysis_tp.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/librz/core/analysis_tp.c b/librz/core/analysis_tp.c index 9399acb6eb..35e67b25b3 100644 --- a/librz/core/analysis_tp.c +++ b/librz/core/analysis_tp.c @@ -809,6 +809,8 @@ void propagate_types_among_used_variables(RzCore *core, HtUP *op_cache, RzAnalys } } +#define OP_CACHE_LIMIT 8192 + RZ_API void rz_core_analysis_type_match(RzCore *core, RzAnalysisFunction *fcn, HtUU *loop_table) { RzListIter *it; @@ -925,6 +927,15 @@ RZ_API void rz_core_analysis_type_match(RzCore *core, RzAnalysisFunction *fcn, H } addr += aop->size; rz_list_free(fcns); + // Recreate op_cache if it grows too large to avoid + // excessive memory usage. + if (op_cache->count > OP_CACHE_LIMIT) { + ht_up_free(op_cache); + op_cache = ht_up_new(NULL, free_op_cache_kv, NULL); + if (!op_cache) { + break; + } + } } }