Skip to content

Commit a70bf19

Browse files
jturner314bluss
authored andcommitted
Fix Miri failure with -Zmiri-tag-raw-pointers
Before this commit, running `MIRIFLAGS="-Zmiri-tag-raw-pointers" cargo miri test test_map_axis` caused Miri to report undefined behavior in the `test_map_axis` test. This commit fixes the underlying issue. Basically, Miri doesn't like us using a reference to an element to access other elements. Here's some sample code to illustrate the issue: ```rust let a: Vec<i32> = vec![5, 6]; let first_elt: &i32 = &a[0]; let ptr: *const i32 = first_elt as *const i32; // Okay: the data is contained in the data referenced by `first_elt`. let a0 = unsafe { &*ptr.offset(0) }; assert_eq!(*a0, 5); // Not okay: the data is not contained in the data referenced by `first_elt`. let a1 = unsafe { &*ptr.offset(1) }; assert_eq!(*a1, 6); ``` Before this commit, we were using `self.index_axis(axis, 0).map(|first_elt| ...)` to create views of the lanes, from references to the first elements in the lanes. Accessing elements within those views (other than the first element) led to the Miri error, since the view's pointer was derived from a reference to a single element. Thanks to @5225225 for reporting the issue. (This commit fixes #1137.)
1 parent f124a16 commit a70bf19

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

src/impl_methods.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -2752,17 +2752,11 @@ where
27522752
A: 'a,
27532753
S: Data,
27542754
{
2755-
let view_len = self.len_of(axis);
2756-
let view_stride = self.strides.axis(axis);
2757-
if view_len == 0 {
2755+
if self.len_of(axis) == 0 {
27582756
let new_dim = self.dim.remove_axis(axis);
27592757
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayView::from(&[])))
27602758
} else {
2761-
// use the 0th subview as a map to each 1d array view extended from
2762-
// the 0th element.
2763-
self.index_axis(axis, 0).map(|first_elt| unsafe {
2764-
mapping(ArrayView::new_(first_elt, Ix1(view_len), Ix1(view_stride)))
2765-
})
2759+
Zip::from(self.lanes(axis)).map_collect(mapping)
27662760
}
27672761
}
27682762

@@ -2783,21 +2777,11 @@ where
27832777
A: 'a,
27842778
S: DataMut,
27852779
{
2786-
let view_len = self.len_of(axis);
2787-
let view_stride = self.strides.axis(axis);
2788-
if view_len == 0 {
2780+
if self.len_of(axis) == 0 {
27892781
let new_dim = self.dim.remove_axis(axis);
27902782
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayViewMut::from(&mut [])))
27912783
} else {
2792-
// use the 0th subview as a map to each 1d array view extended from
2793-
// the 0th element.
2794-
self.index_axis_mut(axis, 0).map_mut(|first_elt| unsafe {
2795-
mapping(ArrayViewMut::new_(
2796-
first_elt,
2797-
Ix1(view_len),
2798-
Ix1(view_stride),
2799-
))
2800-
})
2784+
Zip::from(self.lanes_mut(axis)).map_collect(mapping)
28012785
}
28022786
}
28032787

0 commit comments

Comments
 (0)