From 20a1b8bde942e69d8bf44b32c8fffbd514d58a86 Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Tue, 22 Aug 2023 15:39:49 -0700 Subject: [PATCH] Update the Rust toolchain to `nightly-2023-08-22`. The semantics of int-to-pointer casts under strict provenance has changed. Ideally, we would update our `From for Register` implementation to use the `sptr` crate, but `sptr` does not currently have a suitable license. There is a [PR](https://github.com/Gankra/sptr/pull/14) to change that, but it hasn't gotten a response in 7 months, so I don't think we can use it. Instead, this copies the implementation of `core::ptr::invalid`. This also bumps our `thiserror` dependency, which forces Cargo to pick a newer version of `proc-macro2` that is compatible with recently nightly toolchains. --- platform/src/register.rs | 17 ++++++++++++++--- rust-toolchain | 2 +- unittest/Cargo.toml | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/platform/src/register.rs b/platform/src/register.rs index 976653a53..0afde2f15 100644 --- a/platform/src/register.rs +++ b/platform/src/register.rs @@ -1,3 +1,5 @@ +use core::mem::transmute; + /// In order to work with Miri's `-Zmiri-track-raw-pointers` flag, we cannot /// pass pointers to the kernel through `usize` values (as casting to and from /// `usize` drops the pointer`s tag). Instead, `RawSyscalls` uses the `Register` @@ -16,19 +18,28 @@ pub struct Register(pub *mut ()); impl From for Register { fn from(value: crate::ErrorCode) -> Register { - Register(value as u16 as *mut ()) + (value as usize).into() } } impl From for Register { fn from(value: u32) -> Register { - Register(value as *mut ()) + (value as usize).into() } } impl From for Register { fn from(value: usize) -> Register { - Register(value as *mut ()) + // Note: clippy is wrong here; transmute has different semantics than + // `as` casts under strict provenance. + #[allow(clippy::useless_transmute)] + // We want to convert using the same semantics as core::ptr::invalid: + // convert the usize into a pointer with that address without attaching + // provenance to it. However, core::ptr::invalid is a nightly-only + // function. In order to build on stable, we copy its implementation. + // Safety: Raw pointers do not have any validity invariants that usize + // does not have; a raw pointer can point to any address. + Register(unsafe { transmute(value) }) } } diff --git a/rust-toolchain b/rust-toolchain index 4c36818b2..873e2ff65 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,7 +1,7 @@ [toolchain] # See https://rust-lang.github.io/rustup-components-history/ for a list of # recently nightlies and what components are available for them. -channel = "nightly-2022-06-10" +channel = "nightly-2023-08-22" components = ["clippy", "miri", "rustfmt"] targets = ["thumbv6m-none-eabi", "thumbv7em-none-eabi", diff --git a/unittest/Cargo.toml b/unittest/Cargo.toml index 3a7eb5f08..094f09152 100644 --- a/unittest/Cargo.toml +++ b/unittest/Cargo.toml @@ -10,4 +10,4 @@ version = "0.1.0" [dependencies] libtock_platform = { path = "../platform" } -thiserror = "1.0" +thiserror = "1.0.44"