-
Notifications
You must be signed in to change notification settings - Fork 629
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
Explain why one use of unsafe
is sound
#885
Conversation
I don't think the unsafety here is about the overflow. It's about transmuting a |
I wonder if it is technically possible to tag |
|
Critically, this doesn't actually use |
That requires the pointer to have been allocated by a proper corresponding call to
|
To actually make this completely memory unsafe, just imagine an allocator pooling by size of the objects. Then suddenly the deallocation refers to the pool of a different size than the allocation originally and UB is definitely not out-of the picture and definitely caused by that |
The length of the vector (in bytes) hasn't actually changed, though. The deallocation would be for the same number of bytes as the original allocation. |
@joshlf Same number of bytes but different size. If the precondition of an |
Oh sorry, I didn't realize that you were referring to the |
Yeah, it would be nice to not have any technically unsound code in the crate, even if it never currently triggers issues. Personally, I'd like to push towards completely eliminating unsafe code in the library so we don't have to debate soundness in the first place, although I understand that any significant performance regressions could cause issues for some users. Also, this isn't the only place where this sort of unsoundness happens in the library. We also depend on the safe_transmute crate which does the same thing |
I'd like to think this is fine but then that crate seems to not check the size of |
At this point I consider removing the dependency from |
We use it for a single line that exhibits exactly the same kind of unsafety discussed here.... |
I didn't know about these soundness issues when I added that dependency. I'd be in favor of removing it, and then looking for how to remove the unsoundness altogether. |
@fintelia I realize, I totally understand your motivation and didn't want to assign any blame here ❤️ (and I'm thankful for all other parts of that related PR :) as well). Afterall, the documentation of that crate actively encourages this use, even after discovering its soundness hole.. |
I suspect that some of these instances could be replaced by uses of the zerocopy crate (disclaimer: I'm the author). I could mentor somebody in that or throw up a PR if you'd like. |
The first preliminary fix is here: HeroicKatora@e6e37c0 If that seems good, I'll apply the same strategy to the other places where I find an occurance of this. Feel free to note these occurances in the comments. |
Replaces image-rs#885, or rather was discovered during it as it was originally trying to reason abouts its safety.
@HeroicKatora that fix looks reasonable. For the HDRDecoder case, I think we'd be better off just changing the signature of |
@HeroicKatora What's the codegen on that like? Does it allocate the entire vec up front? I think we could do better. There's nothing wrong with transforming the slice in this case (eg using Avoiding the copy would be nice, and might be doable by allocating once we know the size but before decoding pixels, but that's a much larger change. @joshlf Nice crate, but it currently requires nightly: |
@philipc It seemingly does not and I am highly disappointed in llvm 😄 |
Or rather, I'll simply extract this to its own method so that we can adjust and reuse as we need. And quickly replace should a transmuted route ever become safe. |
That feature was stabilized recently, so it should support stable as of the next stable release.
Sorry, I recently published a new version. docs.rs has caught up now. |
@joshlf I was already confused but finally |
Unsafe pod transmute, replaces #885
Closing this as outdated |
Replaces #885, or rather was discovered during it as it was originally trying to reason abouts its safety.
No description provided.