Skip to content

Commit

Permalink
lib.miri.rs appraoch
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 1, 2024
1 parent 75816e0 commit 6f06afe
Show file tree
Hide file tree
Showing 14 changed files with 559 additions and 588 deletions.
2 changes: 0 additions & 2 deletions library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ compiler-builtins-c = ["compiler_builtins/c"]
compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
# Empty the crate so Miri can run tests
miri-test = ["core/miri-test"]
# Make panics and failed asserts immediately abort without formatting any message
panic_immediate_abort = []
3 changes: 3 additions & 0 deletions library/alloc/src/lib.miri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]
extern crate alloc as realalloc;
pub use realalloc::*;
87 changes: 61 additions & 26 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@
not(test),
not(any(test, bootstrap)),
any(not(feature = "miri-test-libstd"), test, doctest),
all(feature = "miri-test", not(any(test, doctest))),
not(all(feature = "miri-test", not(any(test, doctest)))),
no_global_oom_handling,
not(no_global_oom_handling),
not(no_rc),
Expand Down Expand Up @@ -219,28 +217,6 @@
// from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
#![feature(intra_doc_pointers)]

// We want to be able to `cargo miri test` this crate, but that's tricky.
// We use the `miri-test` feature to indicate that we're running `cargo miri test`, and
// with that feature we turn this crate into a re-export of the sysroot crate. We only do this when
// building the crate that will become a dependency, not when doing the actual (doc)test build.
// See `core/src/lib.rs` for more information.
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
extern crate alloc as realalloc;
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
#[stable(feature = "miri_test", since = "1.0.0")]
pub use realalloc::*;

// Otherwise, we build the crate as usual. Everything that follows should have
// `#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]`. To avoid having to repeat the
// same `cfg` so many times, we `include!("lib_.rs")` with the main crate contents, and `cfg` the
// `include!`. However, some macro-related things can't be behind the `include!` so we have to
// repeat the `cfg` for them.

// Module with internal macros used by other modules (needs to be included before other modules).
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
#[macro_use]
mod macros;

// Allow testing this library
#[cfg(test)]
#[macro_use]
Expand All @@ -250,5 +226,64 @@ extern crate test;
#[cfg(test)]
mod testing;

#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
include!("lib_.rs");
// Module with internal macros used by other modules (needs to be included before other modules).
#[macro_use]
mod macros;

mod raw_vec;

// Heaps provided for low-level allocation strategies

pub mod alloc;

// Primitive types using the heaps above

// Need to conditionally define the mod from `boxed.rs` to avoid
// duplicating the lang-items when building in test cfg; but also need
// to allow code to have `use boxed::Box;` declarations.
#[cfg(not(test))]
pub mod boxed;
#[cfg(test)]
mod boxed {
pub use std::boxed::Box;
}
pub mod borrow;
pub mod collections;
#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
pub mod ffi;
pub mod fmt;
#[cfg(not(no_rc))]
pub mod rc;
pub mod slice;
pub mod str;
pub mod string;
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
pub mod sync;
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
pub mod task;
#[cfg(test)]
mod tests;
pub mod vec;

#[doc(hidden)]
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
pub mod __export {
pub use core::format_args;
}

#[cfg(test)]
#[allow(dead_code)] // Not used in all configurations
pub(crate) mod test_helpers {
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
/// seed not being the same for every RNG invocation too.
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
use std::hash::{BuildHasher, Hash, Hasher};
let mut hasher = std::hash::RandomState::new().build_hasher();
std::panic::Location::caller().hash(&mut hasher);
let hc64 = hasher.finish();
let seed_vec =
hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
rand::SeedableRng::from_seed(seed)
}
}
59 changes: 0 additions & 59 deletions library/alloc/src/lib_.rs

This file was deleted.

3 changes: 3 additions & 0 deletions library/core/src/lib.miri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]
extern crate core as realcore;
pub use realcore::*;
Loading

0 comments on commit 6f06afe

Please sign in to comment.