From 33d722eb0ebb5c87e8b4731a31190b3414429448 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 10 Jul 2024 14:09:47 +0000 Subject: [PATCH] Drop MSRV to 1.63 w/o `RUSTC_BOOTSTRAP` hacks with a bad assumption While the Rust language reference says "you should not rely on this", in practice slices are laid out in memory as a two-tuple of `(pointer, length)`. Here we rely on that assumption to replace `alloc::slice::from_raw_parts` with a `core::mem::transmute`. --- src/crypto/bigint.rs | 18 +++++++++++++++--- src/http.rs | 4 ---- src/lib.rs | 4 ---- test.sh | 12 +++--------- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/crypto/bigint.rs b/src/crypto/bigint.rs index ff478ee..0b87ac7 100644 --- a/src/crypto/bigint.rs +++ b/src/crypto/bigint.rs @@ -29,11 +29,23 @@ const fn const_subslice<'a, T>(a: &'a [T], start: usize, end: usize) -> &'a [T] assert!(end >= start); let mut startptr = a.as_ptr(); startptr = unsafe { startptr.add(start) }; - let len = end - start; - // The docs for from_raw_parts do not mention any requirements that the pointer be valid if the + let len: usize = end - start; + // We should use alloc::slice::from_raw_parts here, but sadly it was only stabilized as const + // in 1.64, whereas we need an MSRV of 1.63. Instead we rely on that which "you should not rely + // on" - that slices are laid out as a simple tuple of pointer + length. + // + // The Rust language reference doesn't specify the layout of slices at all, but does give us a + // hint, saying + // Note: Though you should not rely on this, all pointers to DSTs are currently twice the + // size of the size of usize and have the same alignment. + // This leaves only two possibilities (for today's rust) - `(length, pointer)` and + // `(pointer, length)`. Today, in practice, this seems to always be `(pointer, length)`, so we + // just assume it and hope to move to a later MSRV soon. + unsafe { core::mem::transmute((startptr, len)) } + /*// The docs for from_raw_parts do not mention any requirements that the pointer be valid if the // length is zero, aside from requiring proper alignment (which is met here). Thus, // one-past-the-end should be an acceptable pointer for a 0-length slice. - unsafe { alloc::slice::from_raw_parts(startptr, len) } + unsafe { alloc::slice::from_raw_parts(startptr, len) }*/ } /// Const version of `dest[dest_start..dest_end].copy_from_slice(source)` diff --git a/src/http.rs b/src/http.rs index 00383b6..3079ed2 100644 --- a/src/http.rs +++ b/src/http.rs @@ -2,10 +2,6 @@ #![deny(missing_docs)] -// const_slice_from_raw_parts was stabilized in 1.64, however we support building on 1.63 as well. -// Luckily, it seems to work fine in 1.63 with the feature flag (and RUSTC_BOOTSTRAP=1) enabled. -#![cfg_attr(all(feature = "validation", rust_1_63), feature(const_slice_from_raw_parts))] - #![allow(clippy::new_without_default)] // why is this even a lint #![allow(clippy::result_unit_err)] // Why in the hell is this a lint? #![allow(clippy::get_first)] // Sometimes this improves readability diff --git a/src/lib.rs b/src/lib.rs index 4cf920d..27421ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,10 +33,6 @@ #![deny(rustdoc::broken_intra_doc_links)] #![deny(rustdoc::private_intra_doc_links)] -// const_slice_from_raw_parts was stabilized in 1.64, however we support building on 1.63 as well. -// Luckily, it seems to work fine in 1.63 with the feature flag (and RUSTC_BOOTSTRAP=1) enabled. -#![cfg_attr(rust_1_63, feature(const_slice_from_raw_parts))] - #![allow(clippy::new_without_default)] // why is this even a lint #![allow(clippy::result_unit_err)] // Why in the hell is this a lint? #![allow(clippy::get_first)] // Sometimes this improves readability diff --git a/test.sh b/test.sh index 0ffe705..d16e03a 100755 --- a/test.sh +++ b/test.sh @@ -1,12 +1,6 @@ #!/bin/sh set -eox -RUSTC_MINOR_VERSION=$(rustc --version | awk '{ split($2,a,"."); print a[2] }') -if [ "$RUSTC_MINOR_VERSION" = 63 ]; then - export RUSTC_BOOTSTRAP=1 - export RUSTFLAGS=--cfg=rust_1_63 -fi - cargo test --no-default-features cargo test cargo test --no-default-features --features std @@ -23,7 +17,7 @@ cargo build --lib --features std,tokio,validation --release cargo build --bin http_proof_gen --features build_server cargo doc --features std,tokio,validation cd fuzz -RUSTFLAGS="$RUSTFLAGS --cfg=fuzzing" RUSTC_BOOTSTRAP=1 cargo build --features stdin_fuzz -RUSTFLAGS="$RUSTFLAGS --cfg=fuzzing" RUSTC_BOOTSTRAP=1 cargo test +RUSTFLAGS="--cfg=fuzzing" RUSTC_BOOTSTRAP=1 cargo build --features stdin_fuzz +RUSTFLAGS="--cfg=fuzzing" RUSTC_BOOTSTRAP=1 cargo test cd ../bench -RUSTFLAGS="$RUSTFLAGS --cfg=dnssec_validate_bench" cargo bench +RUSTFLAGS="--cfg=dnssec_validate_bench" cargo bench -- 2.39.5