-
Contributor:
Ahmed Kamal AbdElrasol -
Location:
Cairo, Egypt -
Email:
ahmedkamal200427@gmail.com -
Mentors:
Giovanni Grazioli,Florian Märkl -
Project:
Thread safety improvement -
Organization:
RizinOrg
Rizin’s codebase has long relied too heavily on static and global variables. This design choice has become a major obstacle to making Rizin multi-threaded, limiting performance improvements and preventing smoother integration with Cutter.
The reliance on shared state prevents safe thread parallel execution, introduces race conditions, and forces heavy locking mechanisms that undermine scalability. As a result, Cutter cannot fully leverage Rizin as a responsive backend, which negatively impacts the overall user experience.
Eliminating global/static state and encapsulating functionality within well-defined contexts is a necessary step forward. Doing so would enable true multi-threading, improve responsiveness, and make Rizin far easier to extend, delivering a better, faster, and more seamless experience for Cutter users.
All threads within a process share the same data segment, which is where global and static variables are stored. This means every thread sees and modifies the same global/static variables, which leads to race conditions and unsafe concurrent behavior.
In contrast, contexts work safely because they are allocated on the stack (per thread) or allocate separate heap memory for each thread and hand them their own copy. so each thread can hold its own instance of a context, meaning the data is private to that thread unless explicitly shared. This eliminates accidental interference, reduces the need for heavy locking, and makes the system safer without race conditions.
Here is a list containing most of the project goals:
- RzUtil (
path.c): Replace static variables with RzSys and RzPath context structures and remove constructor/destructor functions. - RzCore (
agraph.c): Encapsulate static state into a non-exposed AGraphContext. - Update libmagic based on
openbsd/src@0e59d0d: Update library and clean up any static variables. - RzIO Plugins (
io_wingbg,io_self,io_qnx,io_plugin,io_gdb): Create a dedicated context per plugin to manage plugin-specific state. - RzAsm Plugins (
cris,vax,lanai,arc,hppa,z80): Create a dedicated context per plugin to manage plugin-specific state. - RzBin Plugins (
sms,avr,z64): Create a dedicated context per plugin to manage plugin-specific state. - WASM analysis: Remove static/global and replace with contexts.
- RzLang: Remove static/global and replace with contexts.
- RzEgg: Remove static/global and replace with contexts.
- RzCons (
cons.c,output.c): Carefully refactor static state into non-exposed structures while preserving Windows-specific behaviors “by testing on Windows environment”. - RzCore Terminal UI (TUI) (
./librz/core/tui/): Encapsulate static state into a context to support multi-threaded TUI operation. - SDB: Remove unused static-variable-dependent functions, and refactor necessary ones to use context structures.
This project required a deep understanding of the Rizin codebase. To make precise and effective changes, I had to thoroughly study its architecture, including the core, utils, sdb, magic, and plugin subsystems (analysis, IO, ASM, bin, etc.).
Over the last three months, I submitted numerous PRs focused on removing static and global variables and replacing them with contexts and structs. This required significant refactoring to ensure the code functioned correctly with the new design. To validate these changes, We used extensive testing, including unit, integration, and regression tests, across multiple CI workflows on GitHub Actions, Travis CI, TinyCC, as well as local environments. Through this process, we confirmed that the refactor worked as expected, which also improved thread safety in the codebase.
-
PR : rizinorg/rizin#5148
Working on this project came with several challenges that pushed me to grow both technically and personally:
-
Rizin is a mature and extensive project. Gaining a deep understanding of how components like core, util, sdb, cmd, and plugins interact required significant time and effort.
-
Static and global variables were deeply embedded across multiple layers of the codebase. Refactoring them without breaking functionality required extreme caution, careful analysis, and step-by-step changes to preserve functionality.
-
Since Rizin supports Linux, macOS, FreeBSD, OpenBSD, QNX and Windows, every refactor had to be validated across many environments. Windows in particular presented unique challenges due to platform-specific behaviors and API differences.
-
One of the most challenging tasks was updating
libmagic. Rizin was relying on an outdated version offileimplementation from OpenBSD (2009), which lacked modern improvements. Bringing it up to the 2015 implementation required carefully merging years of upstream changes while ensuring compatibility with Rizin’s existing codebase. This was not a straightforward replacement, I had to resolve API differences, adapt Rizin’s integration to the new structure, and verify that the behavior remained consistent across all supported platforms. The process demanded extensive testing and debugging, but in the end, it removed static dependencies, improved maintainability, and aligned Rizin with a more modern and secure upstream library.
As a result of this project, the vast majority of the Rizin codebase is now thread safe. Only a few components remain, and the final decision on whether to refactor, remove, or keep them unchanged will be made by the mentors. Overall, this work brought Rizin to an important milestone, the codebase is now over 90% thread safe.
For future work, the remaining parts of the codebase should be discussed with the Rizin community to decide whether they should be removed, redesigned, or refactored. This collaboration will help push Rizin closer to becoming 100% thread-safe.
I truly appreciate the Rizin community, it is unique, supportive, and filled with talented people. I am proud to be part of it, and I plan to continue contributing to the Rizin codebase and infrastructure beyond GSoC. Many exciting changes in the Rizin ecosystem are expected in the near future, and I will do my best to be part of them.
Thank you all! 🙏
Hello maintainers,
My name is Sachin and I am interested in the GSoC project focusing on improving Rizin’s thread safety (removing global variables, refactoring RzUtil/RzCore/AGraph, etc.).
To get started, could you recommend:
that aligns with the goals of this project?
I would like to begin contributing and get familiar with the relevant parts of the codebase.
Thank you!