From 586bfc745d20d525cc9110b2bf974ec3ac33a10b Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Wed, 1 Jan 2025 05:02:55 +0800 Subject: [PATCH] fix: Fix build errors on wasm32 target with no_std (#110) --- Cargo.toml | 1 + frame/solana/Cargo.toml | 4 ++ frame/solana/src/runtime/bank.rs | 2 +- frame/solana/src/svm/account_rent_state.rs | 1 + frame/solana/src/svm/message_processor.rs | 1 + frame/solana/src/svm/program_loader.rs | 4 +- .../src/svm/transaction_account_state_info.rs | 1 + frame/solana/src/svm/transaction_results.rs | 2 +- .../solana/curves/curve25519/src/edwards.rs | 5 +- vendor/solana/curves/curve25519/src/lib.rs | 2 + .../solana/curves/curve25519/src/ristretto.rs | 5 +- vendor/solana/program-runtime/Cargo.toml | 3 +- .../program-runtime/src/invoke_context.rs | 5 +- .../program-runtime/src/loaded_programs.rs | 2 + .../programs/address-lookup-table/src/lib.rs | 1 + .../address-lookup-table/src/processor.rs | 2 +- vendor/solana/programs/bpf_loader/Cargo.toml | 3 ++ vendor/solana/programs/bpf_loader/src/lib.rs | 31 +++++++---- .../programs/bpf_loader/src/serialization.rs | 1 + .../programs/bpf_loader/src/single_thread.rs | 15 ++++++ .../programs/bpf_loader/src/syscalls/cpi.rs | 12 ++--- .../bpf_loader/src/syscalls/mem_ops.rs | 14 ++--- .../programs/bpf_loader/src/syscalls/mod.rs | 51 ++++++++++++------- .../bpf_loader/src/syscalls/sysvar.rs | 4 +- .../solana/programs/compute-budget/src/lib.rs | 6 ++- .../programs/system/src/system_processor.rs | 2 +- 26 files changed, 124 insertions(+), 56 deletions(-) create mode 100644 vendor/solana/programs/bpf_loader/src/single_thread.rs diff --git a/Cargo.toml b/Cargo.toml index 6043b774..83e59e79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,6 +79,7 @@ derive-where = "1.2" eager = "0.1.0" enum-iterator = "1.5.0" ethereum = { version = "0.15.0", default-features = false } +getrandom = { version = "0.2", default-features = false } hex-literal = "0.4" itertools = { version = "0.12.1", default-features = false } k256 = { version = "0.13", default-features = false } diff --git a/frame/solana/Cargo.toml b/frame/solana/Cargo.toml index 73cea4e9..9e832e98 100644 --- a/frame/solana/Cargo.toml +++ b/frame/solana/Cargo.toml @@ -39,6 +39,9 @@ solana-sdk = { workspace = true, features = ["full", "scale"] } solana-system-program = { workspace = true } solana_rbpf = { workspace = true } +# workaround +getrandom = { workspace = true, features = ["custom"] } + [dev-dependencies] assert_matches = { workspace = true } libsecp256k1 = { workspace = true, default-features = true } @@ -62,6 +65,7 @@ std = [ "parity-scale-codec/std", "scale-info/std", "serde/std", + "solana-address-lookup-table-program/std", "solana-bpf-loader-program/std", "solana-compute-budget/std", "solana-compute-budget-program/std", diff --git a/frame/solana/src/runtime/bank.rs b/frame/solana/src/runtime/bank.rs index 6115ea38..639244e2 100644 --- a/frame/solana/src/runtime/bank.rs +++ b/frame/solana/src/runtime/bank.rs @@ -43,7 +43,7 @@ use frame_support::{ BoundedVec, }; use frame_system::pallet_prelude::BlockNumberFor; -use nostd::{marker::PhantomData, sync::Arc}; +use nostd::{marker::PhantomData, prelude::*, sync::Arc}; use np_runtime::traits::LateInit; use solana_program_runtime::loaded_programs::ProgramCacheEntry; use solana_sdk::{ diff --git a/frame/solana/src/svm/account_rent_state.rs b/frame/solana/src/svm/account_rent_state.rs index 57940221..44d12109 100644 --- a/frame/solana/src/svm/account_rent_state.rs +++ b/frame/solana/src/svm/account_rent_state.rs @@ -106,6 +106,7 @@ impl RentState { } fn submit_rent_state_metrics(pre_rent_state: &Self, post_rent_state: &Self) { + #[cfg(feature = "std")] match (pre_rent_state, post_rent_state) { (&RentState::Uninitialized, &RentState::RentPaying { .. }) => { inc_new_counter_info!("rent_paying_err-new_account", 1); diff --git a/frame/solana/src/svm/message_processor.rs b/frame/solana/src/svm/message_processor.rs index a39efa85..295c8146 100644 --- a/frame/solana/src/svm/message_processor.rs +++ b/frame/solana/src/svm/message_processor.rs @@ -15,6 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use nostd::prelude::*; use solana_measure::measure::Measure; use solana_program_runtime::{ invoke_context::InvokeContext, diff --git a/frame/solana/src/svm/program_loader.rs b/frame/solana/src/svm/program_loader.rs index 06629a1f..bfc33899 100644 --- a/frame/solana/src/svm/program_loader.rs +++ b/frame/solana/src/svm/program_loader.rs @@ -16,7 +16,7 @@ // limitations under the License. use crate::svm::transaction_processing_callback::TransactionProcessingCallback; -use nostd::sync::Arc; +use nostd::{prelude::*, sync::Arc}; use solana_program_runtime::{ loaded_programs::{ LoadProgramMetrics, ProgramCacheEntry, ProgramCacheEntryOwner, ProgramCacheEntryType, @@ -53,7 +53,7 @@ pub(crate) fn load_program_from_bytes( deployment_slot: Slot, program_runtime_environment: ProgramRuntimeEnvironment, reloading: bool, -) -> std::result::Result> { +) -> core::result::Result> { if reloading { // Safety: this is safe because the program is being reloaded in the cache. unsafe { diff --git a/frame/solana/src/svm/transaction_account_state_info.rs b/frame/solana/src/svm/transaction_account_state_info.rs index 059415e6..2413a9c6 100644 --- a/frame/solana/src/svm/transaction_account_state_info.rs +++ b/frame/solana/src/svm/transaction_account_state_info.rs @@ -16,6 +16,7 @@ // limitations under the License. use crate::svm::account_rent_state::RentState; +use nostd::prelude::*; use solana_sdk::{ account::ReadableAccount, message::SanitizedMessage, diff --git a/frame/solana/src/svm/transaction_results.rs b/frame/solana/src/svm/transaction_results.rs index 31049a57..fa0a02f7 100644 --- a/frame/solana/src/svm/transaction_results.rs +++ b/frame/solana/src/svm/transaction_results.rs @@ -16,7 +16,7 @@ // limitations under the License. // Re-exported since these have moved to `solana_sdk`. -use nostd::{collections::BTreeMap, sync::Arc}; +use nostd::{collections::BTreeMap, prelude::*, sync::Arc}; use serde::{Deserialize, Serialize}; use solana_program_runtime::loaded_programs::ProgramCacheEntry; #[deprecated( diff --git a/vendor/solana/curves/curve25519/src/edwards.rs b/vendor/solana/curves/curve25519/src/edwards.rs index 4b4893da..135fe4fe 100644 --- a/vendor/solana/curves/curve25519/src/edwards.rs +++ b/vendor/solana/curves/curve25519/src/edwards.rs @@ -1,5 +1,8 @@ -use bytemuck_derive::{Pod, Zeroable}; pub use target_arch::*; +use { + alloc::vec::Vec, + bytemuck_derive::{Pod, Zeroable}, +}; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)] #[repr(transparent)] diff --git a/vendor/solana/curves/curve25519/src/lib.rs b/vendor/solana/curves/curve25519/src/lib.rs index d1c51d29..5f342239 100644 --- a/vendor/solana/curves/curve25519/src/lib.rs +++ b/vendor/solana/curves/curve25519/src/lib.rs @@ -2,6 +2,8 @@ #![allow(clippy::arithmetic_side_effects, clippy::op_ref)] //! Syscall operations for curve25519 +extern crate alloc; + pub mod curve_syscall_traits; pub mod edwards; pub mod errors; diff --git a/vendor/solana/curves/curve25519/src/ristretto.rs b/vendor/solana/curves/curve25519/src/ristretto.rs index 1c71bfee..e41911df 100644 --- a/vendor/solana/curves/curve25519/src/ristretto.rs +++ b/vendor/solana/curves/curve25519/src/ristretto.rs @@ -1,5 +1,8 @@ -use bytemuck_derive::{Pod, Zeroable}; pub use target_arch::*; +use { + alloc::vec::Vec, + bytemuck_derive::{Pod, Zeroable}, +}; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)] #[repr(transparent)] diff --git a/vendor/solana/program-runtime/Cargo.toml b/vendor/solana/program-runtime/Cargo.toml index b340dbea..4060ffa6 100644 --- a/vendor/solana/program-runtime/Cargo.toml +++ b/vendor/solana/program-runtime/Cargo.toml @@ -26,7 +26,7 @@ solana-compute-budget = { workspace = true } solana-frozen-abi = { workspace = true, optional = true } solana-frozen-abi-macro = { workspace = true, optional = true } solana-measure = { workspace = true } -solana-metrics = { workspace = true } +solana-metrics = { workspace = true, optional = true } solana-sdk = { workspace = true } #solana-type-overrides = { workspace = true } #solana-vote = { workspace = true } @@ -62,6 +62,7 @@ std = [ "serde/std", "solana-compute-budget/std", "solana-measure/std", + "solana-metrics", "solana-sdk/std", "solana_rbpf/std", "thiserror/std", diff --git a/vendor/solana/program-runtime/src/invoke_context.rs b/vendor/solana/program-runtime/src/invoke_context.rs index bb3d1d20..2ac2c171 100644 --- a/vendor/solana/program-runtime/src/invoke_context.rs +++ b/vendor/solana/program-runtime/src/invoke_context.rs @@ -14,6 +14,7 @@ use { nostd::{ alloc::Layout, cell::RefCell, + prelude::*, rc::Rc, sync::{atomic::Ordering, Arc}, }, @@ -571,7 +572,7 @@ impl<'a> InvokeContext<'a> { } /// Consume compute units - pub fn consume_checked(&self, amount: u64) -> Result<(), Box> { + pub fn consume_checked(&self, amount: u64) -> Result<(), Box> { let mut compute_meter = self.compute_meter.borrow_mut(); let exceeded = *compute_meter < amount; *compute_meter = compute_meter.saturating_sub(amount); @@ -650,7 +651,7 @@ impl<'a> InvokeContext<'a> { pub fn get_syscall_context(&self) -> Result<&SyscallContext, InstructionError> { self.syscall_context .last() - .and_then(std::option::Option::as_ref) + .and_then(Option::as_ref) .ok_or(InstructionError::CallDepth) } diff --git a/vendor/solana/program-runtime/src/loaded_programs.rs b/vendor/solana/program-runtime/src/loaded_programs.rs index 060144d9..706b9fe1 100644 --- a/vendor/solana/program-runtime/src/loaded_programs.rs +++ b/vendor/solana/program-runtime/src/loaded_programs.rs @@ -5,6 +5,7 @@ use { }, nostd::{ collections::BTreeMap, + prelude::*, sync::{ atomic::{AtomicU64, Ordering}, Arc, @@ -202,6 +203,7 @@ impl LoadProgramMetrics { saturating_add_assign!(timings.create_executor_load_elf_us, self.load_elf_us); saturating_add_assign!(timings.create_executor_verify_code_us, self.verify_code_us); saturating_add_assign!(timings.create_executor_jit_compile_us, self.jit_compile_us); + #[cfg(feature = "std")] datapoint_trace!( "create_executor_trace", ("program_id", self.program_id, String), diff --git a/vendor/solana/programs/address-lookup-table/src/lib.rs b/vendor/solana/programs/address-lookup-table/src/lib.rs index e146dd18..44153c35 100644 --- a/vendor/solana/programs/address-lookup-table/src/lib.rs +++ b/vendor/solana/programs/address-lookup-table/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "std"), no_std)] #![allow(incomplete_features)] #![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(specialization))] diff --git a/vendor/solana/programs/address-lookup-table/src/processor.rs b/vendor/solana/programs/address-lookup-table/src/processor.rs index ee510f70..0f91fcd3 100644 --- a/vendor/solana/programs/address-lookup-table/src/processor.rs +++ b/vendor/solana/programs/address-lookup-table/src/processor.rs @@ -1,5 +1,5 @@ use { - core::convert::TryFrom, + nostd::prelude::*, solana_program_runtime::{declare_process_instruction, ic_msg, invoke_context::InvokeContext}, solana_sdk::{ address_lookup_table::{ diff --git a/vendor/solana/programs/bpf_loader/Cargo.toml b/vendor/solana/programs/bpf_loader/Cargo.toml index cc1f3500..9e625b10 100644 --- a/vendor/solana/programs/bpf_loader/Cargo.toml +++ b/vendor/solana/programs/bpf_loader/Cargo.toml @@ -25,6 +25,9 @@ solana-sdk = { workspace = true } solana_rbpf = { workspace = true } thiserror = { workspace = true } +# workaround +lazy_static = { version = "1.5", default-features = false, features = ["spin"] } + [dev-dependencies] assert_matches = { workspace = true } memoffset = { workspace = true } diff --git a/vendor/solana/programs/bpf_loader/src/lib.rs b/vendor/solana/programs/bpf_loader/src/lib.rs index aa38f478..013238a3 100644 --- a/vendor/solana/programs/bpf_loader/src/lib.rs +++ b/vendor/solana/programs/bpf_loader/src/lib.rs @@ -5,10 +5,15 @@ pub mod serialization; pub mod syscalls; +#[cfg(not(feature = "std"))] +mod single_thread; +#[cfg(not(feature = "std"))] +use single_thread::LocalKey; use { nostd::{ cell::RefCell, mem, + prelude::*, rc::Rc, sync::{atomic::Ordering, Arc}, }, @@ -61,10 +66,16 @@ pub const DEFAULT_LOADER_COMPUTE_UNITS: u64 = 570; pub const DEPRECATED_LOADER_COMPUTE_UNITS: u64 = 1_140; pub const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370; +#[cfg(feature = "std")] thread_local! { pub static MEMORY_POOL: RefCell = RefCell::new(VmMemoryPool::new()); } +#[cfg(not(feature = "std"))] +lazy_static::lazy_static! { + pub static ref MEMORY_POOL: LocalKey = LocalKey::new(VmMemoryPool::new()); +} + #[allow(clippy::too_many_arguments)] pub fn load_program_from_bytes( log_collector: Option>>, @@ -252,7 +263,7 @@ pub fn create_vm<'a, 'b>( invoke_context: &'a mut InvokeContext<'b>, stack: &mut [u8], heap: &mut [u8], -) -> Result>, Box> { +) -> Result>, Box> { let stack_size = stack.len(); let heap_size = heap.len(); let accounts = Rc::clone(invoke_context.transaction_context.accounts()); @@ -358,7 +369,7 @@ fn create_memory_mapping<'a, 'b, C: ContextObject>( heap: &'b mut [u8], additional_regions: Vec, cow_cb: Option, -) -> Result, Box> { +) -> Result, Box> { let config = executable.get_config(); let sbpf_version = executable.get_sbpf_version(); let regions: Vec = vec![ @@ -395,14 +406,14 @@ declare_builtin_function!( _arg3: u64, _arg4: u64, _memory_mapping: &mut MemoryMapping, - ) -> Result> { + ) -> Result> { process_instruction_inner(invoke_context) } ); pub fn process_instruction_inner( invoke_context: &mut InvokeContext, -) -> Result> { +) -> Result> { let log_collector = invoke_context.get_log_collector(); let transaction_context = &invoke_context.transaction_context; let instruction_context = transaction_context.get_current_instruction_context()?; @@ -432,7 +443,7 @@ pub fn process_instruction_inner( Err(InstructionError::IncorrectProgramId) } .map(|_| 0) - .map_err(|error| Box::new(error) as Box); + .map_err(|error| Box::new(error) as Box); } // Program Invocation @@ -462,10 +473,10 @@ pub fn process_instruction_inner( | ProgramCacheEntryType::Closed | ProgramCacheEntryType::DelayVisibility => { ic_logger_msg!(log_collector, "Program is not deployed"); - Err(Box::new(InstructionError::InvalidAccountData) as Box) + Err(Box::new(InstructionError::InvalidAccountData) as Box) } ProgramCacheEntryType::Loaded(executable) => execute(executable, invoke_context), - _ => Err(Box::new(InstructionError::IncorrectProgramId) as Box), + _ => Err(Box::new(InstructionError::IncorrectProgramId) as Box), } .map(|_| 0) } @@ -1341,7 +1352,7 @@ fn common_close_account( fn execute<'a, 'b: 'a>( executable: &'a Executable>, invoke_context: &'a mut InvokeContext<'b>, -) -> Result<(), Box> { +) -> Result<(), Box> { // We dropped the lifetime tracking in the Executor by setting it to 'static, // thus we need to reintroduce the correct lifetime of InvokeContext here again. let executable = unsafe { @@ -1435,7 +1446,7 @@ fn execute<'a, 'b: 'a>( match result { ProgramResult::Ok(status) if status != SUCCESS => { let error: InstructionError = status.into(); - Err(Box::new(error) as Box) + Err(Box::new(error) as Box) } ProgramResult::Err(mut error) => { if direct_mapping { @@ -1502,7 +1513,7 @@ fn execute<'a, 'b: 'a>( let mut deserialize_time = Measure::start("deserialize"); let execute_or_deserialize_result = execution_result.and_then(|_| { deserialize_parameters(invoke_context, parameter_bytes.as_slice(), !direct_mapping) - .map_err(|error| Box::new(error) as Box) + .map_err(|error| Box::new(error) as Box) }); deserialize_time.stop(); diff --git a/vendor/solana/programs/bpf_loader/src/serialization.rs b/vendor/solana/programs/bpf_loader/src/serialization.rs index ad2ed028..5aef377b 100644 --- a/vendor/solana/programs/bpf_loader/src/serialization.rs +++ b/vendor/solana/programs/bpf_loader/src/serialization.rs @@ -3,6 +3,7 @@ use { byteorder::{ByteOrder, LittleEndian}, core::mem::{self, size_of}, + nostd::prelude::*, solana_program_runtime::invoke_context::SerializedAccountMetadata, solana_rbpf::{ aligned_memory::{AlignedMemory, Pod}, diff --git a/vendor/solana/programs/bpf_loader/src/single_thread.rs b/vendor/solana/programs/bpf_loader/src/single_thread.rs new file mode 100644 index 00000000..b41f7b24 --- /dev/null +++ b/vendor/solana/programs/bpf_loader/src/single_thread.rs @@ -0,0 +1,15 @@ +use core::cell::UnsafeCell; + +pub struct LocalKey(UnsafeCell); + +unsafe impl Sync for LocalKey {} + +impl LocalKey { + pub const fn new(value: T) -> Self { + Self(UnsafeCell::new(value)) + } + + pub fn with_borrow_mut(&self, f: impl FnOnce(&mut T) -> R) -> R { + unsafe { f(&mut *self.0.get()) } + } +} diff --git a/vendor/solana/programs/bpf_loader/src/syscalls/cpi.rs b/vendor/solana/programs/bpf_loader/src/syscalls/cpi.rs index 94046f5f..8bc57a07 100644 --- a/vendor/solana/programs/bpf_loader/src/syscalls/cpi.rs +++ b/vendor/solana/programs/bpf_loader/src/syscalls/cpi.rs @@ -1,6 +1,7 @@ use { super::*, crate::serialization::account_data_region_memory_state, + core::{mem, ptr}, scopeguard::defer, solana_measure::measure::Measure, solana_program_runtime::invoke_context::SerializedAccountMetadata, @@ -17,7 +18,6 @@ use { }, transaction_context::BorrowedAccount, }, - std::{mem, ptr}, }; fn check_account_info_pointer( @@ -462,8 +462,8 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust { #[allow(clippy::indexing_slicing)] let account_meta = &account_metas[account_index]; if unsafe { - std::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1 - || std::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8) + core::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1 + || core::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8) > 1 } { return Err(Box::new(InstructionError::InvalidArgument)); @@ -689,8 +689,8 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC { #[allow(clippy::indexing_slicing)] let account_meta = &account_metas[account_index]; if unsafe { - std::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1 - || std::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8) + core::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1 + || core::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8) > 1 } { return Err(Box::new(InstructionError::InvalidArgument)); @@ -1448,7 +1448,7 @@ fn update_caller_account( memory_mapping, caller_account .vm_data_addr - .saturating_sub(std::mem::size_of::() as u64), + .saturating_sub(core::mem::size_of::() as u64), invoke_context.get_check_aligned(), )?; *serialized_len_ptr = post_len as u64; diff --git a/vendor/solana/programs/bpf_loader/src/syscalls/mem_ops.rs b/vendor/solana/programs/bpf_loader/src/syscalls/mem_ops.rs index bbdf0ccc..d29ffaad 100644 --- a/vendor/solana/programs/bpf_loader/src/syscalls/mem_ops.rs +++ b/vendor/solana/programs/bpf_loader/src/syscalls/mem_ops.rs @@ -1,7 +1,7 @@ use { super::*, + core::slice, solana_rbpf::{error::EbpfError, memory_region::MemoryRegion}, - std::slice, }; fn mem_op_consume(invoke_context: &mut InvokeContext, n: u64) -> Result<(), Error> { @@ -170,7 +170,7 @@ fn memmove( )? .as_ptr(); - unsafe { std::ptr::copy(src_ptr, dst_ptr, n as usize) }; + unsafe { core::ptr::copy(src_ptr, dst_ptr, n as usize) }; Ok(0) } } @@ -191,7 +191,7 @@ fn memmove_non_contiguous( memory_mapping, reverse, |src_host_addr, dst_host_addr, chunk_len| { - unsafe { std::ptr::copy(src_host_addr, dst_host_addr as *mut u8, chunk_len) }; + unsafe { core::ptr::copy(src_host_addr, dst_host_addr as *mut u8, chunk_len) }; Ok(0) }, ) @@ -254,16 +254,16 @@ enum MemcmpError { Diff(i32), } -impl std::fmt::Display for MemcmpError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Display for MemcmpError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { MemcmpError::Diff(diff) => write!(f, "memcmp diff: {diff}"), } } } -impl std::error::Error for MemcmpError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for MemcmpError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match self { MemcmpError::Diff(_) => None, } diff --git a/vendor/solana/programs/bpf_loader/src/syscalls/mod.rs b/vendor/solana/programs/bpf_loader/src/syscalls/mod.rs index a4e41cb2..856d0617 100644 --- a/vendor/solana/programs/bpf_loader/src/syscalls/mod.rs +++ b/vendor/solana/programs/bpf_loader/src/syscalls/mod.rs @@ -12,12 +12,13 @@ pub use self::{ }; #[allow(deprecated)] use { - nostd::sync::Arc, nostd::{ alloc::Layout, mem::{align_of, size_of}, + prelude::*, slice::from_raw_parts_mut, str::{from_utf8, Utf8Error}, + sync::Arc, }, solana_compute_budget::compute_budget::ComputeBudget, solana_poseidon as poseidon, @@ -129,7 +130,7 @@ pub enum SyscallError { ArithmeticOverflow, } -type Error = Box; +type Error = Box; pub trait HasherImpl { const NAME: &'static str; @@ -521,7 +522,19 @@ fn translate_type_inner<'a, T>( ) -> Result<&'a mut T, Error> { let host_addr = translate(memory_mapping, access_type, vm_addr, size_of::() as u64)?; if !check_aligned { - Ok(unsafe { std::mem::transmute::(host_addr) }) + #[cfg(feature = "std")] + { + Ok(unsafe { core::mem::transmute::(host_addr) }) + } + // FIXME: Is this enough? + #[cfg(not(feature = "std"))] + { + if host_addr > u32::MAX.into() { + Err(SyscallError::InvalidPointer.into()) + } else { + Ok(unsafe { core::mem::transmute::(host_addr as u32) }) + } + } } else if !address_is_aligned::(host_addr) { Err(SyscallError::UnalignedPointer.into()) } else { @@ -806,14 +819,14 @@ declare_builtin_function!( let address = translate_slice_mut::( memory_mapping, address_addr, - std::mem::size_of::() as u64, + core::mem::size_of::() as u64, invoke_context.get_check_aligned(), )?; if !is_nonoverlapping( bump_seed_ref as *const _ as usize, - std::mem::size_of_val(bump_seed_ref), + core::mem::size_of_val(bump_seed_ref), address.as_ptr() as usize, - std::mem::size_of::(), + core::mem::size_of::(), ) { return Err(SyscallError::CopyOverlapping.into()); } @@ -1392,7 +1405,7 @@ declare_builtin_function!( to_slice.as_ptr() as usize, length as usize, program_id_result as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), ) { return Err(SyscallError::CopyOverlapping.into()); } @@ -1476,36 +1489,36 @@ declare_builtin_function!( if !is_nonoverlapping( result_header as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), program_id as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), ) || !is_nonoverlapping( result_header as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), accounts.as_ptr() as usize, - std::mem::size_of::() + core::mem::size_of::() .saturating_mul(result_header.accounts_len as usize), ) || !is_nonoverlapping( result_header as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), data.as_ptr() as usize, result_header.data_len as usize, ) || !is_nonoverlapping( program_id as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), data.as_ptr() as usize, result_header.data_len as usize, ) || !is_nonoverlapping( program_id as *const _ as usize, - std::mem::size_of::(), + core::mem::size_of::(), accounts.as_ptr() as usize, - std::mem::size_of::() + core::mem::size_of::() .saturating_mul(result_header.accounts_len as usize), ) || !is_nonoverlapping( data.as_ptr() as usize, result_header.data_len as usize, accounts.as_ptr() as usize, - std::mem::size_of::() + core::mem::size_of::() .saturating_mul(result_header.accounts_len as usize), ) { return Err(SyscallError::CopyOverlapping.into()); @@ -1683,8 +1696,8 @@ declare_builtin_function!( return Err(Box::new(SyscallError::InvalidLength)); } - let input_len: u64 = std::cmp::max(params.base_len, params.exponent_len); - let input_len: u64 = std::cmp::max(input_len, params.modulus_len); + let input_len: u64 = core::cmp::max(params.base_len, params.exponent_len); + let input_len: u64 = core::cmp::max(input_len, params.modulus_len); let budget = invoke_context.get_compute_budget(); consume_compute_meter( @@ -1983,7 +1996,7 @@ declare_builtin_function!( let hash_result = translate_slice_mut::( memory_mapping, result_addr, - std::mem::size_of::() as u64, + core::mem::size_of::() as u64, invoke_context.get_check_aligned(), )?; let mut hasher = H::create_hasher(); diff --git a/vendor/solana/programs/bpf_loader/src/syscalls/sysvar.rs b/vendor/solana/programs/bpf_loader/src/syscalls/sysvar.rs index a61b304f..f7a49f3f 100644 --- a/vendor/solana/programs/bpf_loader/src/syscalls/sysvar.rs +++ b/vendor/solana/programs/bpf_loader/src/syscalls/sysvar.rs @@ -1,6 +1,6 @@ use super::*; -fn get_sysvar( +fn get_sysvar( sysvar: Result, InstructionError>, var_addr: u64, check_aligned: bool, @@ -193,7 +193,7 @@ declare_builtin_function!( invoke_context, sysvar_base_cost .saturating_add(sysvar_id_cost) - .saturating_add(std::cmp::max(sysvar_buf_cost, mem_op_base_cost)), + .saturating_add(core::cmp::max(sysvar_buf_cost, mem_op_base_cost)), )?; // Abort: "Not all bytes in VM memory range `[sysvar_id, sysvar_id + 32)` are readable." diff --git a/vendor/solana/programs/compute-budget/src/lib.rs b/vendor/solana/programs/compute-budget/src/lib.rs index 01bbd7a8..355088d9 100644 --- a/vendor/solana/programs/compute-budget/src/lib.rs +++ b/vendor/solana/programs/compute-budget/src/lib.rs @@ -1,4 +1,8 @@ -use solana_program_runtime::declare_process_instruction; +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; + +use {alloc::boxed::Box, solana_program_runtime::declare_process_instruction}; pub const DEFAULT_COMPUTE_UNITS: u64 = 150; diff --git a/vendor/solana/programs/system/src/system_processor.rs b/vendor/solana/programs/system/src/system_processor.rs index 5d93cd6e..8fc6777e 100644 --- a/vendor/solana/programs/system/src/system_processor.rs +++ b/vendor/solana/programs/system/src/system_processor.rs @@ -4,7 +4,7 @@ use { withdraw_nonce_account, }, log::*, - nostd::collections::HashSet, + nostd::{boxed::Box, collections::HashSet}, solana_program_runtime::{ declare_process_instruction, ic_msg, invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,