rizin
rizin copied to clipboard
Reduce heap allocations in disassembly and analysis plugins
Reduce heap allocation in disassembly plugins (e.g. substitute most rz_str_newf()
calls with the rz_strf()
ones, or use newly created rz_asm_op_setf_asm()
API
Is this still an issue?
Yes, last thing I've seen was the string ds->opstr
being freed and duplicated up to four times. Somewhere in asm.c
.
Could you provide file references?
Sure:
The pattern is in ds_build_op_str()
:
https://github.com/rizinorg/rizin/blob/c6537d917e2dbb6b5522cb2ecb553d3fa52dee69/librz/core/disasm.c#L923-L924
https://github.com/rizinorg/rizin/blob/c6537d917e2dbb6b5522cb2ecb553d3fa52dee69/librz/core/disasm.c#L987-L988
https://github.com/rizinorg/rizin/blob/c6537d917e2dbb6b5522cb2ecb553d3fa52dee69/librz/core/disasm.c#L1013-L1014
https://github.com/rizinorg/rizin/blob/c6537d917e2dbb6b5522cb2ecb553d3fa52dee69/librz/core/disasm.c#L1064-L1065
The function (maybe the callees as well) needs to be refactored for this I think. I haven't looked in detail, but it might be a good idea to do the editing of the opstr
on a stack allocated array and then copy it to the ds->opstr
member, before ds_build_op_str()
returns.
@LukeTheEngineer You might want to take a look at https://github.com/rizinorg/rizin/issues/4238. It is loosely connected to this task. But it can give you an idea where to look else.
Could you assign this and #4238 to me?
@LukeTheEngineer done, but I cannot do that in #4238 unless you write in that issue as well.
Done
As a recommendation you can checkout Valgrinds DHAT tool. It should help al lot to spot the critical allocations.
Alright, I've done some code studying, and I'm still trying to understand the issue. Could any of you provide a explanation as to what the issue is and potential solutions?
The analysis and disassembly logic is naturally the one which is most often executed. The task is basically to find the hot execution paths and check where string operations happen.
Because the analysis and especially the asm plugins operate a lot on strings. And usually allocates those on the heap, although it is not necessary and they could be just as well allocated on the stack. Which would improve performance.
The task is to find these heap allocated strings and replace them with stack allocated ones, if possible.