-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add lint against integer to pointer transmutes #144531
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
base: master
Are you sure you want to change the base?
Conversation
The Miri subtree was changed cc @rust-lang/miri |
This is confusing since exposed provenance is not previously mentioned in the error message.
The error should probably make it clear that int-to-ptr transmute also create a pointer without provenance. |
This comment has been minimized.
This comment has been minimized.
b8f46d7
to
950af22
Compare
I have adjusted the diagnostics to make it clear that int-to-ptr transmute also create a pointer without transmute and added a note about exposed provenance (since suggestions always go at the end). |
950af22
to
66d7c24
Compare
unsafe fn should_not_lint(a: usize) { | ||
let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(0usize) }; // linted by other lints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the other lint? I'm not immediately seeing another diagnostic in testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(We have deref-nullptr
, but that fires when dereferencing it.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For references, we lint on transmutes with the 0
literal with the invalid_value
lint:
warning: the type `&i32` does not permit zero-initialization
--> src/lib.rs:2:39
|
2 | let _val: &'static i32 = unsafe { std::mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| this code causes undefined behavior when executed
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
|
= note: references must be non-null
= note: `#[warn(invalid_value)]` on by default
For pointer we lint on some function with the invalid_null_arguments
lint:
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> /home/archie/Projects/RustProjects/rust/tests/ui/lint/invalid_null_args.rs:27:23
|
LL | let _: &[usize] = std::slice::from_raw_parts(mem::transmute(0usize), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could not care about the invalid_null_arguments
lint and lint on transmutes to raw pointer.
For references I think we should keep it under the invalid_value
lint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like nobody will be surprised that transmuting 0 to a pointer will not give you something you can dereference...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even in the context of #141260? (I haven't checked carefully, it just rang a bell)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For out-of-AM volatile accesses, provenance does not matter.
This comment has been minimized.
This comment has been minimized.
66d7c24
to
f29459b
Compare
f29459b
to
a2f8fde
Compare
This comment has been minimized.
This comment has been minimized.
a2f8fde
to
eec7ccb
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
eec7ccb
to
186956d
Compare
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
integer_to_ptr_transmutes
warn-by-default
The
integer_to_ptr_transmutes
lint detects integer to pointer transmutes where the resulting pointers are undefined behavior to dereference.Example
Explanation
Any attempt to use the resulting pointers are undefined behavior as the resulting pointers won't have any provenance.
Alternatively,
std::ptr::with_exposed_provenance
should be used, as they do not carry the provenance requirement or if the wanting to create pointers without provenancestd::ptr::without_provenance_mut
should be used.See std::mem::transmute in the reference for more details.
People are getting tripped up on this, see #128409 and #141220. There are >90 cases like these on GitHub search.
Fixes rust-lang/rust-clippy#13140
Fixes #141220
@rustbot labels +I-lang-nominated +T-lang
cc @traviscross
r? compiler