2020-10-16 22:04:53 -07:00
|
|
|
//! @brief Syscall stubs when building for programs for non-BPF targets
|
|
|
|
|
2020-10-19 13:19:24 -07:00
|
|
|
#![cfg(not(target_arch = "bpf"))]
|
|
|
|
|
|
|
|
use crate::{account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction};
|
2020-10-16 22:04:53 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref SYSCALL_STUBS: Arc<RwLock<Box<dyn SyscallStubs>>> = Arc::new(RwLock::new(Box::new(DefaultSyscallStubs {})));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default syscall stubs don't do much, but `set_syscalls()` can be used to swap in
|
|
|
|
// alternatives
|
|
|
|
pub fn set_syscall_stubs(syscall_stubs: Box<dyn SyscallStubs>) -> Box<dyn SyscallStubs> {
|
|
|
|
std::mem::replace(&mut SYSCALL_STUBS.write().unwrap(), syscall_stubs)
|
2019-07-08 20:28:05 -08:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:04:53 -07:00
|
|
|
pub trait SyscallStubs: Sync + Send {
|
|
|
|
fn sol_log(&self, message: &str) {
|
|
|
|
println!("{}", message);
|
|
|
|
}
|
|
|
|
fn sol_invoke_signed(
|
|
|
|
&self,
|
|
|
|
_instruction: &Instruction,
|
|
|
|
_account_infos: &[AccountInfo],
|
|
|
|
_signers_seeds: &[&[&[u8]]],
|
|
|
|
) -> ProgramResult {
|
|
|
|
sol_log("SyscallStubs: sol_invoke_signed() not available");
|
2020-10-19 13:19:24 -07:00
|
|
|
Ok(())
|
2020-10-16 22:04:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DefaultSyscallStubs {}
|
|
|
|
impl SyscallStubs for DefaultSyscallStubs {}
|
|
|
|
|
|
|
|
pub(crate) fn sol_log(message: &str) {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_log(message);
|
2019-07-08 20:28:05 -08:00
|
|
|
}
|
2019-09-18 19:54:10 -07:00
|
|
|
|
2020-10-16 22:04:53 -07:00
|
|
|
pub(crate) fn sol_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
|
|
|
|
sol_log(&format!("{} {} {} {} {}", arg1, arg2, arg3, arg4, arg5));
|
2020-04-28 19:41:08 -07:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:04:53 -07:00
|
|
|
pub(crate) fn sol_invoke_signed(
|
|
|
|
instruction: &Instruction,
|
|
|
|
account_infos: &[AccountInfo],
|
|
|
|
signers_seeds: &[&[&[u8]]],
|
|
|
|
) -> ProgramResult {
|
|
|
|
SYSCALL_STUBS
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.sol_invoke_signed(instruction, account_infos, signers_seeds)
|
2019-09-18 19:54:10 -07:00
|
|
|
}
|