rizin icon indicating copy to clipboard operation
rizin copied to clipboard

Reduce heap allocations in disassembly and analysis plugins

Open XVilka opened this issue 1 year ago • 11 comments

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

XVilka avatar Jan 18 '24 14:01 XVilka

Is this still an issue?

LukeTheEngineer avatar Mar 22 '24 16:03 LukeTheEngineer

Yes, last thing I've seen was the string ds->opstr being freed and duplicated up to four times. Somewhere in asm.c.

Rot127 avatar Mar 23 '24 05:03 Rot127

Could you provide file references?

LukeTheEngineer avatar Apr 02 '24 23:04 LukeTheEngineer

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.

Rot127 avatar Apr 05 '24 04:04 Rot127

@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.

Rot127 avatar Apr 07 '24 06:04 Rot127

Could you assign this and #4238 to me?

LukeTheEngineer avatar Apr 10 '24 14:04 LukeTheEngineer

@LukeTheEngineer done, but I cannot do that in #4238 unless you write in that issue as well.

XVilka avatar Apr 10 '24 14:04 XVilka

Done

LukeTheEngineer avatar Apr 10 '24 14:04 LukeTheEngineer

As a recommendation you can checkout Valgrinds DHAT tool. It should help al lot to spot the critical allocations.

Rot127 avatar Apr 27 '24 14:04 Rot127

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?

LukeTheEngineer avatar Apr 28 '24 12:04 LukeTheEngineer

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.

Rot127 avatar Apr 29 '24 06:04 Rot127