Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive Named #2758

Open
riesentoaster opened this issue Dec 11, 2024 · 7 comments
Open

Derive Named #2758

riesentoaster opened this issue Dec 11, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@riesentoaster
Copy link
Contributor

A lot of structs within LibAFL implement Named. And all of those are done manually, leading to a lot of code duplication. Would it make sense to create a derive macro that implements it based on the struct name?

Is there a reason this doesn't exist already?

@riesentoaster riesentoaster added the enhancement New feature or request label Dec 11, 2024
@tokatoka
Copy link
Member

Contrary to what the code is now, I think we should not directly use the struct name for its name.
because they could cause name collision if they use the names for lookups of the metadatas
Each type of struct should have monotonously increasing counter and the name should be <struct name>_<counter id>

If derived macro can do that then it's good to have

@tokatoka
Copy link
Member

are you interested in implementing this?

@riesentoaster
Copy link
Contributor Author

riesentoaster commented Dec 11, 2024

That is a fair point.

It may however be hard to implement:

  1. It'd need some form of synchronisation across clients since they may not run in the same memory space. Alternatively, we can also just say that we're only interested in names unique to a client and ignore this problem.
  2. Having unique names is something that should be enforced by the architecture, wrong implementations should not be possible. This has some consequences for the creation process:
    • It would mean that every struct implementing Named would own a Cow<'static, str> with its name. Adding parts to a struct in a macro is pretty straight-forward I believe (although I have little experience).
    • The name would need to be generated when the struct is created, thus changing code in new functions and such. And that is really hard to do programmatically, since this code is not uniform at all.

If I read the code correctly, Named is exclusively used for handles (Handled), right? If that is true I'm wondering if there is a better, built-in, way to identify structs instead of relying on manual implementations.

@riesentoaster
Copy link
Contributor Author

Even just implementing the bare minimum of this however seems like a lot of work, and I don't have the capacity to do so at the moment.

There is still a point to be made that a macro that just implements Named as follows is progress, since it removes code duplication and we decouple the implementation of Named from the specific struct. This way, if we ever want to change how this is handled, we can do so without changing all the structs again.

impl Named for MyNamedThing {
    fn name(&self) -> &Cow<'static, str> {
        &Cow::Borrowed("MyNamedThing")
    }
}

@tokatoka
Copy link
Member

It'd need some form of synchronisation across clients

For this one, the name can just be local to the process. no need to synchronize

@riesentoaster
Copy link
Contributor Author

There is still a point to be made that a macro that just implements Named as follows is progress, since it removes code duplication and we decouple the implementation of Named from the specific struct. This way, if we ever want to change how this is handled, we can do so without changing all the structs again.

impl Named for MyNamedThing {
    fn name(&self) -> &Cow<'static, str> {
        &Cow::Borrowed("MyNamedThing")
    }
}

Do you want this for now?

@domenukk
Copy link
Member

I think we'd want to create a self.name with name being MyNamedThing if an atomic counter is 0, and MyNamedThing2 if the conuter is > 0
So every created instance has a unique name...

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

No branches or pull requests

3 participants