From e31a92c80d87573ed1781ccc05fcf2a4656d1072 Mon Sep 17 00:00:00 2001 From: Noa Date: Mon, 17 Feb 2025 11:48:50 -0600 Subject: [PATCH] Update rand to 0.9 --- .github/workflows/ci.yaml | 3 ++- Cargo.lock | 20 ++++++++++---------- Cargo.toml | 4 ++-- common/src/hash.rs | 6 +++--- stdlib/Cargo.toml | 5 ++--- stdlib/src/random.rs | 8 +------- stdlib/src/uuid.rs | 20 +++----------------- vm/Cargo.toml | 4 ++-- vm/src/import.rs | 3 +-- vm/src/stdlib/os.rs | 2 +- wasm/lib/.cargo/config.toml | 5 +++++ wasm/lib/Cargo.toml | 5 ++++- wasm/wasm-unknown-test/.cargo/config.toml | 5 +++++ wasm/wasm-unknown-test/Cargo.toml | 1 + wasm/wasm-unknown-test/src/lib.rs | 8 ++++++++ 15 files changed, 50 insertions(+), 49 deletions(-) create mode 100644 wasm/lib/.cargo/config.toml create mode 100644 wasm/wasm-unknown-test/.cargo/config.toml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7bbed2c3c2..80012e71e6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -378,7 +378,8 @@ jobs: with: { wabt-version: "1.0.30" } - name: check wasm32-unknown without js run: | - cargo build --release --manifest-path wasm/wasm-unknown-test/Cargo.toml --target wasm32-unknown-unknown --verbose + cd wasm/wasm-unknown-test + cargo build --release --verbose if wasm-objdump -xj Import target/wasm32-unknown-unknown/release/wasm_unknown_test.wasm; then echo "ERROR: wasm32-unknown module expects imports from the host environment" >2 fi diff --git a/Cargo.lock b/Cargo.lock index a62a5dfced..1cb0b4299d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -808,8 +808,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.13.3+wasi-0.2.2", + "wasm-bindgen", "windows-targets 0.52.6", ] @@ -1306,11 +1308,11 @@ dependencies = [ [[package]] name = "mt19937" -version = "2.0.1" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12ca7f22ed370d5991a9caec16a83187e865bc8a532f889670337d5a5689e3a1" +checksum = "df7151a832e54d2d6b2c827a20e5bcdd80359281cd2c354e725d4b82e7c471de" dependencies = [ - "rand_core 0.6.4", + "rand_core 0.9.0", ] [[package]] @@ -2007,7 +2009,7 @@ dependencies = [ "once_cell", "parking_lot", "radium", - "rand 0.8.5", + "rand 0.9.0", "rustpython-format", "siphasher 0.3.11", "volatile", @@ -2216,8 +2218,7 @@ dependencies = [ "parking_lot", "paste", "puruspe", - "rand 0.8.5", - "rand_core 0.6.4", + "rand 0.9.0", "rustix", "rustpython-common", "rustpython-derive", @@ -2262,7 +2263,7 @@ dependencies = [ "exitcode", "flame", "flamer", - "getrandom 0.2.15", + "getrandom 0.3.1", "glob", "half 2.4.1", "hex", @@ -2285,7 +2286,7 @@ dependencies = [ "optional", "parking_lot", "paste", - "rand 0.8.5", + "rand 0.9.0", "result-like", "rustc_version", "rustix", @@ -2329,6 +2330,7 @@ name = "rustpython_wasm" version = "0.4.0" dependencies = [ "console_error_panic_hook", + "getrandom 0.2.15", "js-sys", "rustpython-common", "rustpython-parser", @@ -3000,8 +3002,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0" dependencies = [ "atomic", - "getrandom 0.3.1", - "rand 0.9.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 958bc5eeff..bda7286afd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -146,7 +146,7 @@ cfg-if = "1.0" chrono = "0.4.39" crossbeam-utils = "0.8.21" flame = "0.2.2" -getrandom = "0.2.15" +getrandom = "0.3" glob = "0.3" hex = "0.4.3" indexmap = { version = "2.2.6", features = ["std"] } @@ -168,7 +168,7 @@ num_enum = { version = "0.7", default-features = false } once_cell = "1.20.3" parking_lot = "0.12.3" paste = "1.0.15" -rand = "0.8.5" +rand = "0.9" rustix = { version = "0.38", features = ["event"] } rustyline = "15.0.0" serde = { version = "1.0.133", default-features = false } diff --git a/common/src/hash.rs b/common/src/hash.rs index 4e87eff799..700d2ceef5 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -37,11 +37,11 @@ impl BuildHasher for HashSecret { } } -impl rand::distributions::Distribution for rand::distributions::Standard { +impl rand::distr::Distribution for rand::distr::StandardUniform { fn sample(&self, rng: &mut R) -> HashSecret { HashSecret { - k0: rng.gen(), - k1: rng.gen(), + k0: rng.random(), + k1: rng.random(), } } } diff --git a/stdlib/Cargo.toml b/stdlib/Cargo.toml index 3d91b6fab9..31f5bd18d4 100644 --- a/stdlib/Cargo.toml +++ b/stdlib/Cargo.toml @@ -54,8 +54,7 @@ xml-rs = "0.8.14" # random rand = { workspace = true } -rand_core = "0.6.4" -mt19937 = "2.0.1" +mt19937 = "3.1" # Crypto: digest = "0.10.3" @@ -88,7 +87,7 @@ bzip2 = { version = "0.4", optional = true } # uuid [target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32", target_os = "redox")))'.dependencies] mac_address = "1.1.3" -uuid = { version = "1.1.2", features = ["v1", "fast-rng"] } +uuid = { version = "1.1.2", features = ["v1"] } # mmap [target.'cfg(all(unix, not(target_arch = "wasm32")))'.dependencies] diff --git a/stdlib/src/random.rs b/stdlib/src/random.rs index 1dfc4fcc30..35cbd94457 100644 --- a/stdlib/src/random.rs +++ b/stdlib/src/random.rs @@ -23,7 +23,7 @@ mod _random { impl Default for PyRng { fn default() -> Self { - PyRng::Std(Box::new(StdRng::from_entropy())) + PyRng::Std(Box::new(StdRng::from_os_rng())) } } @@ -46,12 +46,6 @@ mod _random { Self::MT(m) => m.fill_bytes(dest), } } - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { - match self { - Self::Std(s) => s.try_fill_bytes(dest), - Self::MT(m) => m.try_fill_bytes(dest), - } - } } #[pyattr] diff --git a/stdlib/src/uuid.rs b/stdlib/src/uuid.rs index e5434da0e1..4cadbb787b 100644 --- a/stdlib/src/uuid.rs +++ b/stdlib/src/uuid.rs @@ -5,33 +5,19 @@ mod _uuid { use crate::{builtins::PyNone, vm::VirtualMachine}; use mac_address::get_mac_address; use once_cell::sync::OnceCell; - use rand::Rng; - use std::time::{Duration, SystemTime}; - use uuid::{ - v1::{Context, Timestamp}, - Uuid, - }; + use uuid::{timestamp::Timestamp, Context, Uuid}; fn get_node_id() -> [u8; 6] { match get_mac_address() { Ok(Some(_ma)) => get_mac_address().unwrap().unwrap().bytes(), - _ => rand::thread_rng().gen::<[u8; 6]>(), + _ => rand::random::<[u8; 6]>(), } } - pub fn now_unix_duration() -> Duration { - use std::time::UNIX_EPOCH; - - let now = SystemTime::now(); - now.duration_since(UNIX_EPOCH) - .expect("SystemTime before UNIX EPOCH!") - } - #[pyfunction] fn generate_time_safe() -> (Vec, PyNone) { static CONTEXT: Context = Context::new(0); - let now = now_unix_duration(); - let ts = Timestamp::from_unix(&CONTEXT, now.as_secs(), now.subsec_nanos()); + let ts = Timestamp::now(&CONTEXT); static NODE_ID: OnceCell<[u8; 6]> = OnceCell::new(); let unique_node_id = NODE_ID.get_or_init(get_node_id); diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 1468651aeb..1247330db6 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -23,7 +23,7 @@ ast = ["rustpython-ast"] codegen = ["rustpython-codegen", "ast"] parser = ["rustpython-parser", "ast"] serde = ["dep:serde"] -wasmbind = ["chrono/wasmbind", "getrandom/js", "wasm-bindgen"] +wasmbind = ["chrono/wasmbind", "getrandom/wasm_js", "wasm-bindgen"] [dependencies] rustpython-compiler = { workspace = true, optional = true } @@ -144,7 +144,7 @@ features = [ [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] wasm-bindgen = { workspace = true, optional = true } -getrandom = { workspace = true, features = ["custom"] } +getrandom = { workspace = true } [build-dependencies] glob = { workspace = true } diff --git a/vm/src/import.rs b/vm/src/import.rs index f14ae31ef4..fbd563dabb 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -8,7 +8,6 @@ use crate::{ vm::{thread, VirtualMachine}, AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, }; -use rand::Rng; pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult { flame_guard!("init importlib"); @@ -50,7 +49,7 @@ pub(crate) fn init_importlib_package(vm: &VirtualMachine, importlib: PyObjectRef let mut magic = get_git_revision().into_bytes(); magic.truncate(4); if magic.len() != 4 { - magic = rand::thread_rng().gen::<[u8; 4]>().to_vec(); + magic = rand::random::<[u8; 4]>().to_vec(); } let magic: PyObjectRef = vm.ctx.new_bytes(magic).into(); importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?; diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 0b3f617552..6519af094d 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -978,7 +978,7 @@ pub(super) mod _os { return Err(vm.new_value_error("negative argument not allowed".to_owned())); } let mut buf = vec![0u8; size as usize]; - getrandom::getrandom(&mut buf).map_err(|e| match e.raw_os_error() { + getrandom::fill(&mut buf).map_err(|e| match e.raw_os_error() { Some(errno) => io::Error::from_raw_os_error(errno).into_pyexception(vm), None => vm.new_os_error("Getting random failed".to_owned()), })?; diff --git a/wasm/lib/.cargo/config.toml b/wasm/lib/.cargo/config.toml new file mode 100644 index 0000000000..ce1e7c694a --- /dev/null +++ b/wasm/lib/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target = "wasm32-unknown-unknown" + +[target.wasm32-unknown-unknown] +rustflags = ["--cfg=getrandom_backend=\"wasm_js\""] diff --git a/wasm/lib/Cargo.toml b/wasm/lib/Cargo.toml index 1e5c37f4ef..4703cb9f4a 100644 --- a/wasm/lib/Cargo.toml +++ b/wasm/lib/Cargo.toml @@ -28,6 +28,9 @@ rustpython-parser = { workspace = true } serde = { workspace = true } wasm-bindgen = { workspace = true } +# remove once getrandom 0.2 is no longer otherwise in the dependency tree +getrandom = { version = "0.2", features = ["js"] } + console_error_panic_hook = "0.1" js-sys = "0.3" serde-wasm-bindgen = "0.3.1" @@ -47,4 +50,4 @@ web-sys = { version = "0.3", features = [ wasm-opt = false#["-O1"] [lints] -workspace = true \ No newline at end of file +workspace = true diff --git a/wasm/wasm-unknown-test/.cargo/config.toml b/wasm/wasm-unknown-test/.cargo/config.toml new file mode 100644 index 0000000000..f86ad96761 --- /dev/null +++ b/wasm/wasm-unknown-test/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target = "wasm32-unknown-unknown" + +[target.wasm32-unknown-unknown] +rustflags = ["--cfg=getrandom_backend=\"custom\""] diff --git a/wasm/wasm-unknown-test/Cargo.toml b/wasm/wasm-unknown-test/Cargo.toml index f5e0b55786..ca8b15cfc5 100644 --- a/wasm/wasm-unknown-test/Cargo.toml +++ b/wasm/wasm-unknown-test/Cargo.toml @@ -8,6 +8,7 @@ crate-type = ["cdylib"] [dependencies] getrandom = { version = "0.2.12", features = ["custom"] } +getrandom_03 = { package = "getrandom", version = "0.3" } rustpython-vm = { path = "../../vm", default-features = false, features = ["compiler"] } [workspace] diff --git a/wasm/wasm-unknown-test/src/lib.rs b/wasm/wasm-unknown-test/src/lib.rs index fd043aea3a..cfdc445574 100644 --- a/wasm/wasm-unknown-test/src/lib.rs +++ b/wasm/wasm-unknown-test/src/lib.rs @@ -14,3 +14,11 @@ fn getrandom_always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> { } getrandom::register_custom_getrandom!(getrandom_always_fail); + +#[unsafe(no_mangle)] +unsafe extern "Rust" fn __getrandom_v03_custom( + _dest: *mut u8, + _len: usize, +) -> Result<(), getrandom_03::Error> { + Err(getrandom_03::Error::UNSUPPORTED) +}