-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Fix unintentional UB in ui tests #107972
Fix unintentional UB in ui tests #107972
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,10 @@ pub struct ListImpl<T, const N: usize> { | |
|
||
impl<T> List<T> { | ||
const fn as_slice(&self) -> &[T] { | ||
unsafe { std::slice::from_raw_parts(self.data.as_ptr(), self.len) } | ||
unsafe { | ||
let ptr = addr_of!(self.tail) as *const T; | ||
std::slice::from_raw_parts(ptr, self.len) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this even doing? It looks like the original code would run into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI is failing because of padding that I failed to account for in my poorly-written, manual, I don't entirely follow your reasoning, but I think the code is UB before we even get to where you are seeing the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added an even more horrifying implementation that passes
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The empty array is the header, isn't it? pub struct List<T> {
len: usize,
data: [T; 0],
tail: Opaque,
} is basically a variable-sized array, serving as the header for ListImpl. Also doesn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I entirely forgot that I made &Header work with extern types, so what is what is happening here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand why this is done in the way it is. Is there any value for the test to not just express this as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But |
||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ pub fn main() { | |
} | ||
|
||
let data: Box<Foo_<i32>> = Box::new(Foo_ { f: [1, 2, 3] }); | ||
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3)); | ||
let x: &Foo<i32> = mem::transmute(ptr::slice_from_raw_parts(&*data, 3)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I understand why this helps, given that both are transmuted to the same target type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh so the temporary slice actually pretends to have 3 Anyway, patch looks good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That would require relying on the still-unstable slice layout 😉 But yeah I have no idea why anyone would want to write the code in this test. Considering the amount of code in the SIMD tests that were using a pointer to the first element as a pointer to the whole array it might not hurt for people to skim over the tests for some feature or another from time to time... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible to make a reference to a DST without relying on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that's the sort of thing I would advise someone write, but this is a test... so I am not really sure what it is supposed to test. So I'm preserving as much of it as I can in the hope that works. |
||
assert_eq!(x.f.len(), 3); | ||
assert_eq!(x.f[0], 1); | ||
|
||
|
@@ -70,7 +70,7 @@ pub fn main() { | |
|
||
let data: Box<_> = | ||
Box::new(Baz_ { f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] }); | ||
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5)); | ||
let x: &Baz = mem::transmute(ptr::slice_from_raw_parts(&*data, 5)); | ||
assert_eq!(x.f1, 42); | ||
let chs: Vec<char> = x.f2.chars().collect(); | ||
assert_eq!(chs.len(), 5); | ||
|
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.
Why is this needed? Doesn't look like a UB fix. Is this about fixing memory leaks? Is that even a goal or should Miri be run with
-Zmiri-ignore-leaks
when running ui tests?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.
Without this join, the thread can execute after the function returns, which is a use-after-free.
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.
Oh good point!
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.
No need to give me too much credit here, I just looked at Miri's output :p