-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
lint: transmuting known null pointer to ref #628
Comments
I think I can try this one while Oli's busy (I'm atm also working on #3803 under his mentorship). Would you be willing to mentor me here? :) |
This is practically just two matches on methods. First you have to find calls to If you got any questions, just ask or open a WIP PR. I'm happy to help you from there. |
Okay I'm now making a first UI test for this. It should show all the cases that we're looking for in this lint. Which cases are missing? fn transmute_0() {
let zero : *const i32 = 0 as *const_;
let zero_ts : &i32 = std::mem::transmute(zero);
}
fn transmute_null() {
let nil : *const i32 = std::ptr::null();
let nil_ts : &i32 = std::mem::transmute(nil);
}
fn main() {
transmute_0();
transmute_null();
} |
Maybe you can also add: pub const ZPTR: *const usize = 0 as *const _;
...
... std::mem::transmute(ZPTR); ...
... std::mem::transmute(0 as *const _); ... Maybe also some different types, but that shouldn't be an issue on getting this lint working. |
Thank you! This should use a |
Working on this at #3848 |
Transmuting known null ptr to ref Working on implementing #628
Transmuting known null ptr to ref Working on implementing #628
Could we close this now? |
Maybe? I'm not sure, @lzutao. The thing is, my PR implemented this but only the trivial bits. There are many cases where one would see that a null pointer was transmuted, but we can't lint those yet because The idea was that at some point we could do simple constant propagation over a stretch of code using |
Is there anyone working on this at the moment? If I understand correctly, the constant propagation machinery should now be able to propagate the If so, and if no one is working on this, then I'd like to get back on this so we can close this issue :D |
Nope no one is working on this right now. |
Awesome. If Oliver confirms to me that this can be completed now, I'll tackle it :) |
Unfortunately not yet, but I don't know why. If you click on the three dots in the top left corner and select MIR https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=907e81a3947910a850f01271a054209e you'll see bb0: {
StorageLive(_1); // bb0[0]: scope 1 at src/main.rs:3:13: 3:14
_1 = const 0usize; // bb0[1]: scope 1 at src/main.rs:3:17: 3:18
// ty::Const
// + ty: usize
// + val: Value(Scalar(0x0000000000000000))
// mir::Constant
// + span: src/main.rs:3:17: 3:18
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000000)) }
StorageLive(_2); // bb0[2]: scope 2 at src/main.rs:4:9: 4:46
StorageLive(_3); // bb0[3]: scope 2 at src/main.rs:4:44: 4:45
_3 = _1; // bb0[4]: scope 2 at src/main.rs:4:44: 4:45
_2 = const std::intrinsics::transmute::<usize, &i32>(move _3) -> bb1; // bb0[5]: scope 2 at src/main.rs:4:9: 4:46
// ty::Const
// + ty: unsafe extern "rust-intrinsic" fn(usize) -> &i32 {std::intrinsics::transmute::<usize, &i32>}
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: src/main.rs:4:9: 4:43
// + user_ty: UserType(0)
// + literal: Const { ty: unsafe extern "rust-intrinsic" fn(usize) -> &i32 {std::intrinsics::transmute::<usize, &i32>}, val: Value(Scalar(<ZST>)) }
} which (ignorign a lot of the comments and
I'd expect the above to be
which would then be easy to detect. This needs changes in rustc, but if you're interested, I can mentor you for that. I believe it's not very hard to do, but you'll have to learn quite a bit about MIR. |
Excellent. I accept your mentorship, @oli-obk 🌈 Let's do this. Also: hey, this will serve as a future test for const propagation :D |
FWIW, this seems very similar to the existing "invalid_value" lint in rustc (not sure if Clippy also has such a lint), which already detects |
Oh, that's cool! I think this is more general maybe? Although with dataflow-aware constprop, the |
However, |
mem::transmute(std::ptr::null())
– this is undefined behavior.The text was updated successfully, but these errors were encountered: