Skip to content

Commit 81d8659

Browse files
committed
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 6c8b821 commit 81d8659

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
@@ -2699,17 +2699,11 @@ where
26992699
A: 'a,
27002700
S: Data,
27012701
{
2702-
let view_len = self.len_of(axis);
2703-
let view_stride = self.strides.axis(axis);
2704-
if view_len == 0 {
2702+
if self.len_of(axis) == 0 {
27052703
let new_dim = self.dim.remove_axis(axis);
27062704
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayView::from(&[])))
27072705
} else {
2708-
// use the 0th subview as a map to each 1d array view extended from
2709-
// the 0th element.
2710-
self.index_axis(axis, 0).map(|first_elt| unsafe {
2711-
mapping(ArrayView::new_(first_elt, Ix1(view_len), Ix1(view_stride)))
2712-
})
2706+
Zip::from(self.lanes(axis)).map_collect(mapping)
27132707
}
27142708
}
27152709

@@ -2730,21 +2724,11 @@ where
27302724
A: 'a,
27312725
S: DataMut,
27322726
{
2733-
let view_len = self.len_of(axis);
2734-
let view_stride = self.strides.axis(axis);
2735-
if view_len == 0 {
2727+
if self.len_of(axis) == 0 {
27362728
let new_dim = self.dim.remove_axis(axis);
27372729
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayViewMut::from(&mut [])))
27382730
} else {
2739-
// use the 0th subview as a map to each 1d array view extended from
2740-
// the 0th element.
2741-
self.index_axis_mut(axis, 0).map_mut(|first_elt| unsafe {
2742-
mapping(ArrayViewMut::new_(
2743-
first_elt,
2744-
Ix1(view_len),
2745-
Ix1(view_stride),
2746-
))
2747-
})
2731+
Zip::from(self.lanes_mut(axis)).map_collect(mapping)
27482732
}
27492733
}
27502734

0 commit comments

Comments
 (0)