-
Notifications
You must be signed in to change notification settings - Fork 44
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
mimalloc_rust may be unsound due to calls to getenv
#68
Comments
There's no soundness hole, since the only safe way of using this crate is by setting the More detailsMimalloc reads these values from the environment either in a static constructor (e.g. Unix/posix platforms are the platforms that have a thread-unsafe setenv (it's fine on windows, although this turns out not to matter really). The way this can trigger is only in multithreaded scenarios, e.g. one thread must be doing a env::set_var, while the C code on another thread performs getenv. There are two ways to use mimalloc provided by this repo, one of which could have a soundness hole, the other can't:
I could probably go on further but yeah, there's no soundness hole here, the only way you'll trigger this is by both:
A bit of a rant about this particular problemWe're somewhat lucky here that the issue can't trigger soundly for mimalloc, as almost any potential fix would be too costly for performance — allocation is insanely hot, and taking an env lock around the allocator would absolutely destroy performance in multi-threaded use cases, which are a big selling point for mimalloc, and something its algorithms are tuned for (I guess we could ask upstream for a way to disble env options, but that's unfortunate too). The thing is, This mismatch is hard to fix, but IMO libstd needs to do something about it (the current "solution" is not really viable for very many applications — anything where Rust isn't calling C, but is called by C, anything where To add insult to injury, even in cases where the issue does exist (like with the So yah, 🤷 I don't really know if it's great to go around filing issues about this for what are essentially stdlib soundness holes, especially if they will almost never be hit in real code, especially if they have no workaround for most cases... (IMO the Sorry for the rant, but I find this specific issue deeply annoying. |
mimalloc has an undocumented MI_NO_GETENV build flag which may be desirable to expose through a crate feature. Not because this is unsound, but because one might want to build binaries which aren't influenced through a bunch of environment variables. |
I was looking at the time crate wondering why the time crate didn't give offsets, and I stumbled upon this thread on Internals. This seems like a big problem so I went looking for FFI crates where this could be a problem.
Looking at mimalloc source code, and the list of environment variables https://microsoft.github.io/mimalloc/environment.html, it looks like this wrapper has a data race when
set_env
is called in another thread.The text was updated successfully, but these errors were encountered: