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

Using stdlib functions in a ctor potentially unsound #28

Open
ids1024 opened this issue Feb 4, 2021 · 4 comments
Open

Using stdlib functions in a ctor potentially unsound #28

ids1024 opened this issue Feb 4, 2021 · 4 comments

Comments

@ids1024
Copy link
Contributor

ids1024 commented Feb 4, 2021

According to the README for ctor:

Rust's philosophy is that nothing happens before or after main and this library explicitly subverts that. The code that runs in the ctor and dtor functions should be careful to limit itself to libc functions and code that does not rely on Rust's stdlib services.

Looking at the generated code, this seems to use Vec in the constructor. As I understand the statement from ctor above, this could potentially segfault or be otherwise broken by a future change to std, or on a particular target, or perhaps with custom allocators.

@jdonszelmann
Copy link
Owner

Dear IDS, you are entirely right. However I honestly don't see a way around it. The crate which "defines" the label stores all the references to the functions annotated with the label, and we can't know in advance how many times the annotation will be used (so we need to use vec). The only reason around this is by limiting ourselfs to libc functions. Calling libc's malloc directly and implementing some sort of custom vec-like structure. Do you know of a better way to do this?

@ids1024
Copy link
Contributor Author

ids1024 commented Feb 5, 2021

Calling libc's malloc directly and implementing some sort of custom vec-like structure.

It's rather annoying to have to do it this way, but it shouldn't be to hard to wrap malloc/realloc here. Another not particularly satisfactory solution would be to just use a fixed length array.

Do you know of a better way to do this?

Maybe there's a better way to implement something like this, but unfortunately I don't know what it is.

@jdonszelmann
Copy link
Owner

jdonszelmann commented Feb 5, 2021

The unfortunate thing about using a fixed size array is that it'd either make a huge empty array in the binary (every label you create will equal an array of (pointerwidth * max_labels) bytes long), or make it possible for the compilation to fail just by adding too many functions with labels on them.

@jdonszelmann
Copy link
Owner

Also, may I say that I am not sure if this library is fit to be used with gtk_rs as it depends on a nightly feature. I'm not entirely sure you want that. Unfortunately that's another thing I haven't found a way around yet. I need to find the path to the label definition which I can only find by parsing some of the source around the labelled function. It doesn't look like this will be stable any time soon.

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

No branches or pull requests

2 participants