Skip to content

Conversation

isimluk
Copy link
Contributor

@isimluk isimluk commented Aug 17, 2025

perf: do not search and dlopen on every sql cmd

This extracts the handler of zen-internal FFI to a thread safe singleton.
Ensuring the DLL is searched for and loaded only once. Previously, we
would run multiple syscalls to get the location of the dll/so and then
built the python datastructures (CDLL object) that has to be later GCed.

I haven't microbenchmarked the improvements rigorously, as I feel GC
savings may contribute considerably in real-world scenario.

Although,

poetry run pytest -rP  ./aikido_zen/vulnerabilities/sql_injection/

improves by over 4.3% on my set-up.

_instances = {}
_lock = threading.Lock() # Ensures thread safety

def __call__(cls, *args, **kwargs):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remain undecided. While I appreciate your insightful observation regarding the contentious nature of metaclasses, in this specific instance—where the singleton pattern is widely recognized—it may be acceptable.

Ultimately, the choice of which singleton implementation (each has its own drawbacks) to adopt is largely a matter of personal preference and not probabilistic exercise. Therefore, I would value the perspective of a real human being on this matter.

This extracts a handle of zen-internal FFI to a thread safe singleton.
Ensuring the DLL is searched for and loaded only once. Previously, we
would run multiple syscalls to get the location of the dll/so and then
built the python datastructures (CDLL object) that has to be later GCed.

I haven't microbenchmarked the improvements rigorously, as I feel GC
savings may contribute considerably in real-world scenario.

Although,

    poetry run pytest -rP  ./aikido_zen/vulnerabilities/sql_injection/

improves by over 4.3% on my set-up.
_instances = {}
_lock = threading.Lock() # Ensures thread safety

def __call__(cls, *args, **kwargs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call method implements complex singleton pattern with thread safety using atomic check-and-create operations, but lacks explanatory comments to help developers understand the locking mechanism and why it's necessary for thread-safe singleton creation. More info


class ZenInternal(metaclass=__Singleton):
# Reference : [rust lib]/src/sql_injection/helpers/select_dialect_based_on_enum.rs
SQL_DIALECTS = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have helper function for this btw

Copy link
Contributor Author

@isimluk isimluk Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean map_dialect_to_rust_int?

I took that down ✂️ in the second commit.

I felt that this fn carried intimate knowledge of the FFI/zen-internal and thus should be part of the class that deals with these matters. Further, I decided to expand the method and leave only the map in there as I saw that these things are contextually well collocated and there is no need for it to exist as separate fn.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, generally we just don't like to refactor stuff & the helpers can include logic specific to parts of code, it's just a way of abstracting away more tedious stuff that rarely changes (like this map). For me it's fine either way, but it's easier to leave it as is and cut down on the refactors (at least that's my opinion).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants