Description
The original problem got fixed by avoiding the broken code paths; this issue now tracks fixing those code paths.
Original issue
In the ffi_helpers
crate we have a Nullable
trait which gives you a generic way to do null pointer checks.
pub trait Nullable {
const NULL: Self;
fn is_null(&self) -> bool;
}
A user recently reported that the crate no longer compiles on nightly
(Michael-F-Bryan/ffi_helpers#2) because type validation detects that std::ptr::null()
and std::ptr::null_mut()
create uninitialized raw pointers.
Compiling playground v0.0.1 (/playground)
error[E0080]: it is undefined behavior to use this value
--> src/lib.rs:17:5
|
17 | const NULL: Self = std::ptr::null_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground`.
I can reproduce this on the playground with the latest nightly, 1.44.0-nightly (2020-04-19 dbf8b6bf116c7bece298)
.
Is the use shown in that playground example (*self == Self::NULL
on a *mut T
) actually UB?
Also, I noticed that calling the is_null()
method defined on a raw pointer with <*const T>::is_null(*self)
doesn't trigger this error, implying that the problem isn't with declaring a constant that contains a null pointer (const NULL: Self = std::ptr::null_mut()
), but the fact that we're using it for a comparison. Was this intended, or is it just an oversight in the error detection code?
Full example with output
pub trait Nullable {
const NULL: Self;
fn is_null(&self) -> bool;
}
impl<T> Nullable for *const T {
const NULL: Self = std::ptr::null();
#[inline]
fn is_null(&self) -> bool {
<*const T>::is_null(*self)
}
}
impl<T> Nullable for *mut T {
const NULL: Self = std::ptr::null_mut();
#[inline]
fn is_null(&self) -> bool {
*self == Self::NULL
}
}
Compiling playground v0.0.1 (/playground)
error[E0080]: it is undefined behavior to use this value
--> src/lib.rs:17:5
|
17 | const NULL: Self = std::ptr::null_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
CC: @RalfJung because it looks like they were the last person to touch that error message (9ee4d1a).
This issue has been assigned to @jumbatm via this comment.
Activity
RalfJung commentedon Apr 20, 2020
Cc @rust-lang/wg-const-eval
This is definitely not intended, that example you gave is entirely safe code and should compile just fine.
The odd thing is, I am indeed responsible for the error but that was back in #66147 which has long arrived on stable -- so the error itself cannot be the source of the regression. Would be good to get the regression narrowed down a bit more.
I also don't understand why
==
seems to be needed to trigger this. Might be some interaction with promotion?The error message is probably (hopefully!) just wrong, I doubt the pointer is actually uninitialized. We just show that error when anything goes wrong in this line:
rust/src/librustc_mir/interpret/validity.rs
Lines 487 to 491 in 4ca5fd2
Likely
ref_to_mplace
really doesn't like that the pointer is NULL. The thing is, this code works fine, and that just makes no sense:jonas-schievink commentedon Apr 20, 2020
@rustbot ping bisect
rustbot commentedon Apr 20, 2020
Hey Cleanup Crew ICE-breakers! This bug has been identified as a good
"Cleanup ICE-breaking candidate". In case it's useful, here are some
instructions for tackling these sorts of bugs. Maybe take a look?
Thanks! <3
cc @AminArria @chrissimpkins @contrun @DutchGhost @elshize @ethanboxx @h-michael @HallerPatrick @hdhoang @hellow554 @imtsuki @jakevossen5 @kanru @KarlK90 @LeSeulArtichaut @MAdrianMattocks @matheus-consoli @mental32 @nmccarty @Noah-Kennedy @pard68 @PeytonT @pierreN @Redblueflame @RobbieClarken @RobertoSnap @robjtede @SarthakSingh31 @senden9 @shekohex @sinato @spastorino @turboladen @woshilapin @yerke
RalfJung commentedon Apr 20, 2020
It's not an ICE, but if ICE-breakers also help with other bisects then yes this would be awesome. :)
Michael-F-Bryan commentedon Apr 20, 2020
If it helps pinpoint when the error was introduced, my
rustc
is usually within a couple days of the latestnightly
and I made a release of that crate on 2020-04-07 without hitting any compilation errors.senden9 commentedon Apr 20, 2020
Regression in nightly-2020-04-17. Looking currently for regression commit between 2020-04-17 and 2020-04-16.
searched nightlies: from nightly-2020-04-07 to nightly-2020-04-19
regressed nightly: nightly-2020-04-17
searched commits: from d223029 to 7f3df57
regressed commit: 7f3df57
Full Log:
35 remaining items
Rollup merge of rust-lang#71663 - jumbatm:caller-handles-validation-e…
LeSeulArtichaut commentedon May 3, 2020
@Mark-Simulacrum As this problem raised in the Crater run, should #71663 be nominated to be backported to beta?
Mark-Simulacrum commentedon May 3, 2020
My understanding is that #71441 was the intended fix for beta and we expect #71663 to go with the trains, i.e. there is no need for a backport. But if I'm wrong, we should indeed nominate it.
LeSeulArtichaut commentedon May 3, 2020
Oh right, I re-read #71353 (comment)
RalfJung commentedon May 3, 2020
Indeed, the beta fix was to revert the original incorrect PR. This PR that just landed now just fixed #69021, which doesn't need backporting.
However, I am going to re-open this issue because validation still swallows layout errors in many cases -- just this one particular case got fixed.
Rollup merge of rust-lang#71950 - RalfJung:try-validation-cleanup, r=…