-
Notifications
You must be signed in to change notification settings - Fork 9
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
ClaimOnOom Example's Correctness is Not Yet Definitive #32
Comments
I'm under the impression that this is not the case. I may be wrong, please just point me somewhere that spells this out ( Miri thinks the following code is fine: static mut THING: usize = 1;
fn main() {
let ptr = unsafe { std::ptr::addr_of!(THING) };
unsafe {
*(ptr as *mut _) = 3;
}
let x = unsafe { *ptr };
println!("The value of x is {}", x);
} And it prints 3 and all that good stuff. (Of course, changing Either way, indeed, not having const_mut_refs makes this initialization code a lot less convenient than it ought to be. (using addr_of made it slightly less awful but it also feels very dirty, UB or not.) |
Miri doesn't catch this as it currently conservatively assumes it is fine: https://github.com/rust-lang/rust/blob/7c4ac0603e9ee5295bc802c90575391288a69a8a/compiler/rustc_const_eval/src/transform/check_consts/resolver.rs#L100-L102 ( If |
Thanks. I wasn't aware So it looks like this particular code is in the grey zone? I'm not hugely concerned by that. Perhaps Please let me know what you think of this path of action. However, this means some of the API surface of Talc is genuinely problematic, particularly the Here, the fn main() {
let mut arena = [0u8; 10000];
unsafe {
let mut talc = talc::Talc::new(talc::ClaimOnOom::new(arena.as_ref().into()));
let x = talc.malloc(std::alloc::Layout::new::<[u64; 3]>()).unwrap();
*x.as_ptr().cast::<[u64; 3]>() = [1, 2, 3];
}
} That's a big screw-up. Whoops. These conversion functions should not exist. So... hurry a V5 out the door and yank everything after V4.2 ...? I don't know if this is reasonable. Will the compiler actually trip up on this yet? I believe it's not super aggressive in the interest of avoiding atypical LLVM execution paths but I stand to be corrected. I've got quite a lot I want to put into a Talc V5 and don't have the time right now to do it. In a few weeks I'll have completed my exams so I'll have more time thereafter. This is why I'd prefer to delay it. But this stuff can just be a V6 if need be. I'll go open an issue about this so long. I'm eager to hear your opinion on the severity of this. |
Opened #33 for the latter issue. |
Makes sense. I don't think there is much risk of this miscompiling in the short term.
I don't think yanking is necessary. It is a footgun, but still requires unsafe, so it isn't unsound. Do deprecations work for impls? If so adding a deprecation in a patch release us the best option IMO. Otherwise I would suggest adding a doc comment to the impl to discourge using it and leave it at that until the next breaking release.
Good luck with your exams! |
Solid suggestions. (That I should've taken.) I tried to get this all sorted with talc V5 but didn't manage to get it done before the end of vacation. A feature request came in so I'll address what I can from this once it gets merged. |
With |
The pointer returned by
addr_of!()
may only be used to read from the pointee. Writing through it is UB. Unfortunately usingaddr_of_mut!()
it still unstable: rust-lang/rust#57349, so there is not a whole lot that can be done for now. One possible solution I can think of is to have a macro which expands to aClaimOnOom
like type, except hard coded for a single specified memory area. This way the address of this memory area only needs to be taken at runtime. If this macro also defines the static for the memory area to claim itself, then this may even allow it to be used entirely without any unsafe code as the macro can guarantee that the memory area is writable and not used for any other purposes.The text was updated successfully, but these errors were encountered: