2021-10-19 16:48:15 -05:00
|
|
|
//! Syscall stubs when building for programs for non-BPF targets
|
2020-10-16 22:04:53 -07:00
|
|
|
|
2020-10-19 13:19:24 -07:00
|
|
|
#![cfg(not(target_arch = "bpf"))]
|
|
|
|
|
2021-04-12 16:04:57 -07:00
|
|
|
use crate::{
|
|
|
|
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction,
|
2021-09-01 10:14:01 +01:00
|
|
|
program_error::UNSUPPORTED_SYSVAR, pubkey::Pubkey,
|
2021-04-12 16:04:57 -07:00
|
|
|
};
|
2021-09-17 09:14:49 +01:00
|
|
|
use itertools::Itertools;
|
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 {})));
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:33:17 -07:00
|
|
|
// The default syscall stubs may not do much, but `set_syscalls()` can be used
|
|
|
|
// to swap in alternatives
|
2020-10-16 22:04:53 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-01 15:33:17 -07:00
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2020-10-16 22:04:53 -07:00
|
|
|
pub trait SyscallStubs: Sync + Send {
|
|
|
|
fn sol_log(&self, message: &str) {
|
|
|
|
println!("{}", message);
|
|
|
|
}
|
2020-10-28 12:39:48 -07:00
|
|
|
fn sol_log_compute_units(&self) {
|
|
|
|
sol_log("SyscallStubs: sol_log_compute_units() not available");
|
|
|
|
}
|
2020-10-16 22:04:53 -07:00
|
|
|
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
|
|
|
}
|
2021-04-12 16:04:57 -07:00
|
|
|
fn sol_get_clock_sysvar(&self, _var_addr: *mut u8) -> u64 {
|
|
|
|
UNSUPPORTED_SYSVAR
|
|
|
|
}
|
|
|
|
fn sol_get_epoch_schedule_sysvar(&self, _var_addr: *mut u8) -> u64 {
|
|
|
|
UNSUPPORTED_SYSVAR
|
|
|
|
}
|
|
|
|
fn sol_get_fees_sysvar(&self, _var_addr: *mut u8) -> u64 {
|
|
|
|
UNSUPPORTED_SYSVAR
|
|
|
|
}
|
|
|
|
fn sol_get_rent_sysvar(&self, _var_addr: *mut u8) -> u64 {
|
|
|
|
UNSUPPORTED_SYSVAR
|
|
|
|
}
|
2021-06-01 15:33:17 -07:00
|
|
|
/// # Safety
|
|
|
|
unsafe fn sol_memcpy(&self, dst: *mut u8, src: *const u8, n: usize) {
|
|
|
|
// cannot be overlapping
|
2021-10-05 22:24:48 -07:00
|
|
|
assert!(
|
|
|
|
!(dst as usize + n > src as usize && src as usize > dst as usize),
|
|
|
|
"memcpy does not support overlapping regions"
|
|
|
|
);
|
2021-06-01 15:33:17 -07:00
|
|
|
std::ptr::copy_nonoverlapping(src, dst, n as usize);
|
|
|
|
}
|
|
|
|
/// # Safety
|
|
|
|
unsafe fn sol_memmove(&self, dst: *mut u8, src: *const u8, n: usize) {
|
|
|
|
std::ptr::copy(src, dst, n as usize);
|
|
|
|
}
|
|
|
|
/// # Safety
|
|
|
|
unsafe fn sol_memcmp(&self, s1: *const u8, s2: *const u8, n: usize, result: *mut i32) {
|
|
|
|
let mut i = 0;
|
|
|
|
while i < n {
|
|
|
|
let a = *s1.add(i);
|
|
|
|
let b = *s2.add(i);
|
|
|
|
if a != b {
|
|
|
|
*result = a as i32 - b as i32;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
*result = 0
|
|
|
|
}
|
|
|
|
/// # Safety
|
|
|
|
unsafe fn sol_memset(&self, s: *mut u8, c: u8, n: usize) {
|
|
|
|
let s = std::slice::from_raw_parts_mut(s, n);
|
|
|
|
for val in s.iter_mut().take(n) {
|
|
|
|
*val = c;
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 10:14:01 +01:00
|
|
|
fn sol_get_return_data(&self) -> Option<(Pubkey, Vec<u8>)> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn sol_set_return_data(&mut self, _data: &[u8]) {}
|
2021-09-17 09:14:49 +01:00
|
|
|
fn sol_log_data(&self, fields: &[&[u8]]) {
|
|
|
|
println!("data: {}", fields.iter().map(base64::encode).join(" "));
|
|
|
|
}
|
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) {
|
2020-11-09 18:43:20 -08:00
|
|
|
sol_log(&format!(
|
|
|
|
"{:#x}, {:#x}, {:#x}, {:#x}, {:#x}",
|
|
|
|
arg1, arg2, arg3, arg4, arg5
|
|
|
|
));
|
2020-04-28 19:41:08 -07:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:39:48 -07:00
|
|
|
pub(crate) fn sol_log_compute_units() {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_log_compute_units();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-04-12 16:04:57 -07:00
|
|
|
|
|
|
|
pub(crate) fn sol_get_clock_sysvar(var_addr: *mut u8) -> u64 {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_get_clock_sysvar(var_addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_get_epoch_schedule_sysvar(var_addr: *mut u8) -> u64 {
|
|
|
|
SYSCALL_STUBS
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.sol_get_epoch_schedule_sysvar(var_addr)
|
|
|
|
}
|
|
|
|
|
2021-08-01 17:31:11 -07:00
|
|
|
pub(crate) fn sol_get_fees_sysvar(_var_addr: *mut u8) -> u64 {
|
|
|
|
UNSUPPORTED_SYSVAR
|
2021-04-12 16:04:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_get_rent_sysvar(var_addr: *mut u8) -> u64 {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_get_rent_sysvar(var_addr)
|
|
|
|
}
|
2021-06-01 15:33:17 -07:00
|
|
|
|
|
|
|
pub(crate) fn sol_memcpy(dst: *mut u8, src: *const u8, n: usize) {
|
|
|
|
unsafe {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_memcpy(dst, src, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_memmove(dst: *mut u8, src: *const u8, n: usize) {
|
|
|
|
unsafe {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_memmove(dst, src, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_memcmp(s1: *const u8, s2: *const u8, n: usize, result: *mut i32) {
|
|
|
|
unsafe {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_memcmp(s1, s2, n, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_memset(s: *mut u8, c: u8, n: usize) {
|
|
|
|
unsafe {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_memset(s, c, n);
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 10:14:01 +01:00
|
|
|
|
|
|
|
pub(crate) fn sol_get_return_data() -> Option<(Pubkey, Vec<u8>)> {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_get_return_data()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sol_set_return_data(data: &[u8]) {
|
|
|
|
SYSCALL_STUBS.write().unwrap().sol_set_return_data(data)
|
|
|
|
}
|
2021-09-17 09:14:49 +01:00
|
|
|
|
|
|
|
pub(crate) fn sol_log_data(data: &[&[u8]]) {
|
|
|
|
SYSCALL_STUBS.read().unwrap().sol_log_data(data)
|
|
|
|
}
|