Skip to content

Commit 0d07975

Browse files
committed
Add target_arch bpf
1 parent fc73a1a commit 0d07975

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

sha256-hasher/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ solana-hash = { workspace = true }
2121
[target.'cfg(not(target_os = "solana"))'.dependencies]
2222
sha2 = { workspace = true, optional = true }
2323

24+
[target.'cfg(target_arch = "bpf")'.dependencies]
25+
solana-define-syscall = { workspace = true }
26+
2427
[target.'cfg(target_os = "solana")'.dependencies]
2528
solana-define-syscall = { workspace = true }
2629

sha256-hasher/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#![no_std]
22
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
3+
4+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
45
use sha2::{Digest, Sha256};
5-
use solana_hash::Hash;
6+
use solana_hash::{Hash, HASH_BYTES};
7+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
8+
pub use {core::mem::MaybeUninit, solana_define_syscall::definitions::sol_sha256};
69

7-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
10+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
811
#[derive(Clone, Default)]
912
pub struct Hasher {
1013
hasher: Sha256,
1114
}
1215

13-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
16+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
1417
impl Hasher {
1518
pub fn hash(&mut self, val: &[u8]) {
1619
self.hasher.update(val);
@@ -21,19 +24,16 @@ impl Hasher {
2124
}
2225
}
2326
pub fn result(self) -> Hash {
24-
let bytes: [u8; solana_hash::HASH_BYTES] = self.hasher.finalize().into();
27+
let bytes: [u8; HASH_BYTES] = self.hasher.finalize().into();
2528
bytes.into()
2629
}
2730
}
2831

29-
#[cfg(target_os = "solana")]
30-
pub use solana_define_syscall::definitions::sol_sha256;
31-
3232
/// Return a Sha256 hash for the given data.
3333
pub fn hashv(vals: &[&[u8]]) -> Hash {
3434
// Perform the calculation inline, calling this from within a program is
3535
// not supported
36-
#[cfg(not(target_os = "solana"))]
36+
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
3737
{
3838
#[cfg(feature = "sha2")]
3939
{
@@ -48,17 +48,18 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
4848
}
4949
}
5050
// Call via a system call to perform the calculation
51-
#[cfg(target_os = "solana")]
51+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
5252
{
53-
let mut hash_result = [0; solana_hash::HASH_BYTES];
53+
let mut hash_result = MaybeUninit::<[u8; HASH_BYTES]>::uninit();
5454
unsafe {
5555
sol_sha256(
5656
vals as *const _ as *const u8,
5757
vals.len() as u64,
5858
&mut hash_result as *mut _ as *mut u8,
5959
);
6060
}
61-
Hash::new_from_array(hash_result)
61+
// SAFETY: the syscall initializes the `hash_result`.
62+
Hash::new_from_array(unsafe { hash_result.assume_init() })
6263
}
6364
}
6465

0 commit comments

Comments
 (0)