Skip to content

Commit 3c80007

Browse files
fix: getrandom pulling in std (#1804)
For reasons I am not entirely clear on, when you use `getrandom_v02::Error`, the `getrandom` crate pulls in `std` library. But if you just directly implement what the `register_custom_getrandom!` macro does, this goes away... Fixes CI failure. Full disclosure: I got the idea from https://github.com/risc0/risc0/pull/3106/files#diff-fd010fc66ba8d918981d2705c0d10cd65d19db599969bb29bb14caffa62d3b65R95
1 parent f6e8bb7 commit 3c80007

File tree

7 files changed

+19
-13
lines changed

7 files changed

+19
-13
lines changed

.github/workflows/cli.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
paths:
99
- "crates/circuits/primitives/**"
1010
- "crates/vm/**"
11+
- "crates/toolchain/**"
1112
- "crates/sdk/**"
1213
- "crates/cli/**"
1314
- "examples/**"

.github/workflows/extension-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
paths:
99
- "crates/circuits/**"
1010
- "crates/vm/**"
11+
- "crates/toolchain/**"
1112
- "extensions/**"
1213
- "Cargo.toml"
1314
- ".github/workflows/extension-tests.yml"
@@ -51,6 +52,7 @@ jobs:
5152
filters: |
5253
- "crates/circuits/**"
5354
- "crates/vm/**"
55+
- "crates/toolchain/**"
5456
- "extensions/${{ matrix.extensions.path }}/**"
5557
- ".github/workflows/extension-tests.yml"
5658
- name: Skip if no changes

.github/workflows/guest-lib-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
paths:
99
- "crates/circuits/**"
1010
- "crates/vm/**"
11+
- "crates/toolchain/**"
1112
- "extensions/**"
1213
- "guest-libs/**"
1314
- "Cargo.toml"
@@ -52,6 +53,7 @@ jobs:
5253
filters: |
5354
- "crates/circuits/**"
5455
- "crates/vm/**"
56+
- "crates/toolchain/**"
5557
- "extensions/**"
5658
- "guest-libs/${{ matrix.crate.path }}/**"
5759
- ".github/workflows/guest-lib-tests.yml"

.github/workflows/riscv.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ on:
77
branches: ["**"]
88
paths:
99
- "crates/vm/**"
10-
- "crates/toolchain/platform/**"
11-
- "crates/toolchain/instructions/**"
12-
- "crates/toolchain/build/**"
13-
- "crates/toolchain/macros/**"
14-
- "crates/toolchain/tests/**"
10+
- "crates/toolchain/**"
1511
- "Cargo.toml"
1612
- ".github/workflows/riscv.yml"
1713

.github/workflows/sdk.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
paths:
99
- "crates/circuits/primitives/**"
1010
- "crates/vm/**"
11+
- "crates/toolchain/**"
1112
- "crates/sdk/**"
1213
- "Cargo.toml"
1314
- ".github/workflows/sdk.yml"

crates/toolchain/openvm/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ serde = { workspace = true, features = ["alloc"] }
1616
bytemuck = { workspace = true, features = ["extern_crate_alloc"] }
1717

1818
[target.'cfg(target_os = "zkvm")'.dependencies]
19-
getrandom = { version = "0.3", optional = true }
20-
getrandom-v02 = { version = "0.2", package = "getrandom", features = [
19+
getrandom = { version = "0.3", default-features = false, optional = true }
20+
getrandom-v02 = { version = "0.2", package = "getrandom", default-features = false, features = [
2121
"custom",
2222
], optional = true }
2323

@@ -37,4 +37,4 @@ heap-embedded-alloc = ["openvm-platform/heap-embedded-alloc"]
3737
std = ["serde/std", "openvm-platform/std"]
3838

3939
[package.metadata.cargo-shear]
40-
ignored = ["openvm-custom-insn"]
40+
ignored = ["openvm-custom-insn", "getrandom"]

crates/toolchain/openvm/src/getrandom.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ unsafe extern "Rust" fn __getrandom_v03_custom(
1313
Err(getrandom::Error::UNSUPPORTED)
1414
}
1515

16+
/// This entrypoint for getrandom is used for versions < 0.3
17+
// The ABI is defined here: https://github.com/rust-random/getrandom/blob/ce4144b2c16fe1422037c93e267e6a52336e0834/src/custom.rs#L74
18+
// @dev If you try to use the `getrandom_v02::Error`, it somehow triggers std library
1619
#[cfg(feature = "getrandom-unsupported")]
17-
pub fn __getrandom_v02_custom(_dest: &mut [u8]) -> Result<(), getrandom_v02::Error> {
18-
Err(getrandom_v02::Error::UNSUPPORTED)
20+
#[no_mangle]
21+
unsafe fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
22+
__getrandom_v03_custom(dest, len)
23+
.map_err(|e| e.raw_os_error().unwrap_or(2))
24+
.err()
25+
.unwrap_or(0) as u32
1926
}
20-
// https://docs.rs/getrandom/0.2.16/src/getrandom/custom.rs.html#74
21-
#[cfg(feature = "getrandom-unsupported")]
22-
getrandom_v02::register_custom_getrandom!(__getrandom_v02_custom);

0 commit comments

Comments
 (0)