Skip to content
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

new lint: pointer created from transmuting an int into a pointer is not able to be dereferenced #13140

Open
timrobertsdev opened this issue Jul 21, 2024 · 3 comments · May be fixed by #13192
Open
Labels
A-lint Area: New lints

Comments

@timrobertsdev
Copy link

timrobertsdev commented Jul 21, 2024

What it does

Points out potential UB when transmuting an integer into a pointer.

As of Rust 1.78, LLVM is informed that this is UB. In <= 1.77 this works, and the change caught me off guard, as an int->ptr conversion happens a lot when dealing with win32.

Advantage

The original code will cause UB if the pointer is dereferenced. The recommended code works as intended.

Drawbacks

Possible false positives, though I'm not sure of the use case.

Example

// win32 tends to hand out isize and wants you to use them as pointers
let l_param: isize = ...; 
let evt: *mut MSLLHOOKSTRUCT = std::mem::transmute(l_param);

Could be written as:

let l_param: isize = ...;
let evt = l_param as *mut MSLLHOOKSTRUCT;
@timrobertsdev timrobertsdev added the A-lint Area: New lints label Jul 21, 2024
@Alexendoo
Copy link
Member

Do you know where/how this became UB?

There is https://rust-lang.github.io/rust-clippy/master/index.html#/transmutes_expressible_as_ptr_casts but it doesn't seem to catch it currently, we could fix that and raise it to a correctness lint

@timrobertsdev
Copy link
Author

timrobertsdev commented Jul 22, 2024

from the transmute docs: "Transmuting integers to pointers is a largely unspecified operation. It is likely not equivalent to an as cast. Doing non-zero-sized memory accesses with a pointer constructed this way is currently considered undefined behavior."

https://doc.rust-lang.org/std/mem/fn.transmute.html

I was informed via Zulip that this is reported to LLVM as UB as of Rust 1.78.

edit: link to the zulip topic https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20Function.20miscompilation.20with.20rustc.201.2E78.20and.20--release

@Alexendoo
Copy link
Member

Great, thanks!

The LLVM change is probably rust-lang/rust#121282

@csmoe csmoe linked a pull request Jul 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants