Skip to content

Commit

Permalink
crucible-mir: Properly parse ArrayToPointer casts
Browse files Browse the repository at this point in the history
Although `crucible-mir` had support for translating these sorts of casts, it
did not parse them properly. This patch adds a dedicated `ArrayToPointer`
`CastKind` and ensures that it is handled properly in the `crucible-mir` JSON
parser and translator.

Fixes #1224.
  • Loading branch information
RyanGlScott committed Jul 18, 2024
1 parent 2909c22 commit b07ad45
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crucible-mir/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# next -- TBA

* Fix a bug in which `crucible-mir` would fail to parse MIR JSON code involving
casts from array references to pointers.

# 0.2 -- 2024-02-05

* `crucible-mir` now supports the `nightly-2023-01-23` Rust toolchain. Some of
Expand Down
1 change: 1 addition & 0 deletions crucible-mir/src/Mir/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ instance FromJSON CastKind where
Just (String "Pointer(UnsafeFnPointer)") -> pure UnsafeFnPointer
Just (String "Pointer(Unsize)") -> pure Unsize
Just (String "Pointer(MutToConstPointer)") -> pure MutToConstPointer
Just (String "Pointer(ArrayToPointer)") -> pure ArrayToPointer
Just (String "UnsizeVtable") -> UnsizeVtable <$> v .: "vtable"
-- TODO: actually plumb this information through if it is relevant
-- instead of using Misc. See
Expand Down
1 change: 1 addition & 0 deletions crucible-mir/src/Mir/Mir.hs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ data CastKind =
| Unsize
| UnsizeVtable VtableName
| MutToConstPointer
| ArrayToPointer
deriving (Show,Eq, Ord, Generic)

data Constant = Constant Ty ConstVal
Expand Down
2 changes: 1 addition & 1 deletion crucible-mir/src/Mir/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ evalCast' ck ty1 e ty2 = do
-> return $ MirExp (MirReferenceRepr tpr) (getSlicePtr e')

-- *const [T; N] -> *const T (get first element)
(M.Misc, M.TyRawPtr (M.TyArray t1 _) m1, M.TyRawPtr t2 m2)
(M.ArrayToPointer, M.TyRawPtr (M.TyArray t1 _) m1, M.TyRawPtr t2 m2)
| t1 == t2, m1 == m2, MirExp (MirReferenceRepr (MirVectorRepr tpr)) e' <- e
-> MirExp (MirReferenceRepr tpr) <$> subindexRef tpr e' (R.App $ usizeLit 0)

Expand Down
20 changes: 20 additions & 0 deletions crux-mir/test/conc_eval/ptr/coerce_array_to_pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type T = u64;
const COUNT: usize = 16;

pub fn output_array(src: &[T; COUNT], dst: *mut T) {
unsafe {
std::ptr::copy(src as *const T, dst, COUNT);
}
}

#[cfg_attr(crux, crux::test)]
pub fn crux_test() -> T {
let src: &[T; COUNT] = &[42; COUNT];
let dst: &mut [T; COUNT] = &mut [0; COUNT];
output_array(src, dst as *mut T);
dst[0]
}

pub fn main() {
println!("{:?}", crux_test());
}

0 comments on commit b07ad45

Please sign in to comment.