Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/run-validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jobs:
name: Check if `no_std` target compiles as expected
runs-on:
group: organization/Default

steps:
- uses: actions/checkout@v5

Expand All @@ -118,12 +119,16 @@ jobs:

- name: Run cargo check tool to check if the `no_std` code are valid
uses: actions-rs/cargo@v1
env:
RUSTFLAGS: '--cfg getrandom_backend="unsupported"'
with:
command: check
args: --lib --no-default-features --features=full_no_std_platform_independent,mock_getrandom
args: --lib --no-default-features --features=full_no_std_platform_independent

- name: Run cargo check tool to check if the additional example code are valid
uses: actions-rs/cargo@v1
env:
RUSTFLAGS: '--cfg getrandom_backend="custom"'
with:
command: check
args: --manifest-path examples/no_std/Cargo.toml --target thumbv7m-none-eabi
Expand Down
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ contract_test = ["parse_token", "publish", "access", "crypto", "std", "subscribe
full_no_std = ["serde", "reqwest", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "tokio", "presence"]
full_no_std_platform_independent = ["serde", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
pubnub_only = ["crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
mock_getrandom = ["getrandom/custom"]
# TODO: temporary treated as internal until we officially release it
subscribe = ["dep:futures"]
presence = ["dep:futures"]
Expand All @@ -91,7 +90,7 @@ sha2 = { version = "0.10", default-features = false }
time = { version = "0.3", features = ["alloc"], default-features = false }

# serde
serde = { version = "1.0", features = ["derive"], optional = true, default-features = false }
serde = { version = "1.0", features = ["derive", "alloc"], optional = true, default-features = false }
serde_json = { version = "1.0", optional = true, features = ["alloc"], default-features = false }

# reqwest
Expand All @@ -101,7 +100,7 @@ bytes = { version = "1.4", default-features = false, optional = true }
# crypto
aes = { version = "0.8.2", optional = true }
cbc = { version = "0.1.2", optional = true }
getrandom = { version = "0.2", optional = true }
getrandom = { version = "0.3", optional = true }

# parse_token
ciborium = { version = "0.2.1", default-features = false, optional = true }
Expand All @@ -115,7 +114,7 @@ async-channel = { version = "1.8", optional = true }
portable-atomic = { version = "1.3", optional = true, default-features = false, features = ["require-cas", "critical-section"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
getrandom = { version = "0.3", features = ["wasm_js"] }

[dev-dependencies]
async-trait = "0.1"
Expand All @@ -126,7 +125,7 @@ cucumber = { version = "0.20.2", features = ["output-junit"] }
reqwest = { version = "0.12", features = ["json"] }
test-case = "3.0"
hashbrown = { version = "0.14.0", features = ["serde"] }
getrandom = { version = "0.2", features = ["custom"] }
getrandom = { version = "0.3" }

[build-dependencies]
built = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion examples/no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
pubnub = { path = "../../", default-features = false, features = ["blocking", "serde", "publish", "subscribe", "presence"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
getrandom = { version = "0.2", default-features = false, features = ["custom"] }
getrandom = { version = "0.3", default_features = false }

[[bin]]
name = "publish"
Expand Down
16 changes: 15 additions & 1 deletion examples/no_std/src/here_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use pubnub::{

// As getrandom crate has limited support of targets, we need to provide custom
// implementation of `getrandom` function.
getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
Expand All @@ -37,6 +36,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}

// This function is used to register the custom implementation of `getrandom` function.
#[no_mangle]
unsafe extern "Rust" fn __getrandom_v03_custom(
dest: *mut u8,
len: usize,
) -> Result<(), getrandom::Error> {
let buf = unsafe {
// fill the buffer with zeros
core::ptr::write_bytes(dest, 0, len);
// create mutable byte slice
core::slice::from_raw_parts_mut(dest, len)
};
custom_random(buf)
}

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport` trait.
Expand Down
16 changes: 15 additions & 1 deletion examples/no_std/src/presence_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct State {

// As getrandom crate has limited support of targets, we need to provide custom
// implementation of `getrandom` function.
getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
Expand All @@ -44,6 +43,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}

// This function is used to register the custom implementation of `getrandom` function.
#[no_mangle]
unsafe extern "Rust" fn __getrandom_v03_custom(
dest: *mut u8,
len: usize,
) -> Result<(), getrandom::Error> {
let buf = unsafe {
// fill the buffer with zeros
core::ptr::write_bytes(dest, 0, len);
// create mutable byte slice
core::slice::from_raw_parts_mut(dest, len)
};
custom_random(buf)
}

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport` trait.
Expand Down
16 changes: 15 additions & 1 deletion examples/no_std/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct Message {

// As getrandom crate has limited support of targets, we need to provide custom
// implementation of `getrandom` function.
getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
Expand All @@ -45,6 +44,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}

// This function is used to register the custom implementation of `getrandom` function.
#[no_mangle]
unsafe extern "Rust" fn __getrandom_v03_custom(
dest: *mut u8,
len: usize,
) -> Result<(), getrandom::Error> {
let buf = unsafe {
// fill the buffer with zeros
core::ptr::write_bytes(dest, 0, len);
// create mutable byte slice
core::slice::from_raw_parts_mut(dest, len)
};
custom_random(buf)
}

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport` trait.
Expand Down
19 changes: 16 additions & 3 deletions examples/no_std/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,32 @@ struct Message {

// As getrandom crate has limited support of targets, we need to provide custom
// implementation of `getrandom` function.
getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
// In your program, you should use proper random number generator that is
// supported by your target.
// In your program, you should use proper random number generator that is supported by your target.
for i in buf.iter_mut() {
*i = 42;
}

Ok(())
}

// This function is used to register the custom implementation of `getrandom` function.
#[no_mangle]
unsafe extern "Rust" fn __getrandom_v03_custom(
dest: *mut u8,
len: usize,
) -> Result<(), getrandom::Error> {
let buf = unsafe {
// fill the buffer with zeros
core::ptr::write_bytes(dest, 0, len);
// create mutable byte slice
core::slice::from_raw_parts_mut(dest, len)
};
custom_random(buf)
}

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport`
Expand Down
16 changes: 15 additions & 1 deletion examples/no_std/src/where_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use pubnub::{

// As getrandom crate has limited support of targets, we need to provide custom
// implementation of `getrandom` function.
getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
Expand All @@ -37,6 +36,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}

// This function is used to register the custom implementation of `getrandom` function.
#[no_mangle]
unsafe extern "Rust" fn __getrandom_v03_custom(
dest: *mut u8,
len: usize,
) -> Result<(), getrandom::Error> {
let buf = unsafe {
// fill the buffer with zeros
core::ptr::write_bytes(dest, 0, len);
// create mutable byte slice
core::slice::from_raw_parts_mut(dest, len)
};
custom_random(buf)
}

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport` trait.
Expand Down
4 changes: 1 addition & 3 deletions src/core/retry_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
//! [`PubNub API`]: https://www.pubnub.com/docs
//! [`pubnub`]: ../index.html

use getrandom::getrandom;

use crate::{core::PubNubError, lib::alloc::vec::Vec};

/// List of known endpoint groups (by context)
Expand Down Expand Up @@ -310,7 +308,7 @@ impl RequestRetryConfiguration {
let delay = delay_in_seconds * MICROS_IN_SECOND;
let mut random_bytes = [0u8; 8];

if getrandom(&mut random_bytes).is_err() {
if getrandom::fill(&mut random_bytes).is_err() {
return Some(delay);
}

Expand Down
13 changes: 0 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,3 @@ mod lib {
}
}
}

// Mocking random for checking if `no_std` compiles.
// Don't use that feature in production.
#[cfg(feature = "mock_getrandom")]
mod mock_getrandom {
use getrandom::{register_custom_getrandom, Error};

pub fn do_nothing(_buf: &mut [u8]) -> Result<(), Error> {
Ok(())
}

register_custom_getrandom!(do_nothing);
}
2 changes: 1 addition & 1 deletion src/providers/crypto/cryptors/aes_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl AesCbcCryptor {

fn initialization_vector(&self) -> [u8; AES_BLOCK_SIZE] {
let mut random = [0u8; AES_BLOCK_SIZE];
getrandom::getrandom(&mut random).ok();
getrandom::fill(&mut random).ok();
random
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/crypto/cryptors/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl LegacyCryptor {
fn initialization_vector(&self) -> [u8; 16] {
if self.use_random_iv {
let mut random = [0u8; AES_BLOCK_SIZE];
getrandom::getrandom(&mut random).ok();
getrandom::fill(&mut random).ok();
random
} else {
*b"0123456789012345"
Expand Down
2 changes: 1 addition & 1 deletion tests/crypto/legacy/crypto_aescbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl AesCbcCrypto {
Some(iv) => Vec::from(iv.as_slice()),
None => {
let mut random = [0u8; AES_BLOCK_SIZE];
getrandom::getrandom(&mut random).ok();
getrandom::fill(&mut random).ok();
Vec::from(random)
}
}
Expand Down
Loading