atlas_sysvar/
program_stubs.rs

1//! Implementations of syscalls used when `atlas-program` is built for non-SBF targets.
2
3#![cfg(not(target_os = "atlas"))]
4
5use {
6    base64::{prelude::BASE64_STANDARD, Engine},
7    atlas_account_info::AccountInfo,
8    atlas_instruction::{error::UNSUPPORTED_SYSVAR, Instruction},
9    atlas_program_error::ProgramResult,
10    atlas_program_memory::stubs,
11    atlas_pubkey::Pubkey,
12    std::sync::{Arc, RwLock},
13};
14
15lazy_static::lazy_static! {
16    static ref SYSCALL_STUBS: Arc<RwLock<Box<dyn SyscallStubs>>> = Arc::new(RwLock::new(Box::new(DefaultSyscallStubs {})));
17}
18
19// The default syscall stubs may not do much, but `set_syscalls()` can be used
20// to swap in alternatives
21pub fn set_syscall_stubs(syscall_stubs: Box<dyn SyscallStubs>) -> Box<dyn SyscallStubs> {
22    std::mem::replace(&mut SYSCALL_STUBS.write().unwrap(), syscall_stubs)
23}
24
25pub trait SyscallStubs: Sync + Send {
26    fn atlas_log(&self, message: &str) {
27        println!("{message}");
28    }
29    fn atlas_log_compute_units(&self) {
30        atlas_log("SyscallStubs: atlas_log_compute_units() not available");
31    }
32    fn atlas_remaining_compute_units(&self) -> u64 {
33        atlas_log("SyscallStubs: atlas_remaining_compute_units() defaulting to 0");
34        0
35    }
36    fn atlas_invoke_signed(
37        &self,
38        _instruction: &Instruction,
39        _account_infos: &[AccountInfo],
40        _signers_seeds: &[&[&[u8]]],
41    ) -> ProgramResult {
42        atlas_log("SyscallStubs: atlas_invoke_signed() not available");
43        Ok(())
44    }
45    fn atlas_get_sysvar(
46        &self,
47        _sysvar_id_addr: *const u8,
48        _var_addr: *mut u8,
49        _offset: u64,
50        _length: u64,
51    ) -> u64 {
52        UNSUPPORTED_SYSVAR
53    }
54    fn atlas_get_clock_sysvar(&self, _var_addr: *mut u8) -> u64 {
55        UNSUPPORTED_SYSVAR
56    }
57    fn atlas_get_epoch_schedule_sysvar(&self, _var_addr: *mut u8) -> u64 {
58        UNSUPPORTED_SYSVAR
59    }
60    fn atlas_get_fees_sysvar(&self, _var_addr: *mut u8) -> u64 {
61        UNSUPPORTED_SYSVAR
62    }
63    fn atlas_get_rent_sysvar(&self, _var_addr: *mut u8) -> u64 {
64        UNSUPPORTED_SYSVAR
65    }
66    fn atlas_get_epoch_rewards_sysvar(&self, _var_addr: *mut u8) -> u64 {
67        UNSUPPORTED_SYSVAR
68    }
69    fn atlas_get_last_restart_slot(&self, _var_addr: *mut u8) -> u64 {
70        UNSUPPORTED_SYSVAR
71    }
72    fn atlas_get_epoch_stake(&self, _vote_address: *const u8) -> u64 {
73        0
74    }
75    /// # Safety
76    unsafe fn atlas_memcpy(&self, dst: *mut u8, src: *const u8, n: usize) {
77        stubs::atlas_memcpy(dst, src, n)
78    }
79    /// # Safety
80    unsafe fn atlas_memmove(&self, dst: *mut u8, src: *const u8, n: usize) {
81        stubs::atlas_memmove(dst, src, n)
82    }
83    /// # Safety
84    unsafe fn atlas_memcmp(&self, s1: *const u8, s2: *const u8, n: usize, result: *mut i32) {
85        stubs::atlas_memcmp(s1, s2, n, result)
86    }
87    /// # Safety
88    unsafe fn atlas_memset(&self, s: *mut u8, c: u8, n: usize) {
89        stubs::atlas_memset(s, c, n)
90    }
91    fn atlas_get_return_data(&self) -> Option<(Pubkey, Vec<u8>)> {
92        None
93    }
94    fn atlas_set_return_data(&self, _data: &[u8]) {}
95    fn atlas_log_data(&self, fields: &[&[u8]]) {
96        println!(
97            "data: {}",
98            fields
99                .iter()
100                .map(|v| BASE64_STANDARD.encode(v))
101                .collect::<Vec<_>>()
102                .join(" ")
103        );
104    }
105    fn atlas_get_processed_sibling_instruction(&self, _index: usize) -> Option<Instruction> {
106        None
107    }
108    fn atlas_get_stack_height(&self) -> u64 {
109        0
110    }
111}
112
113struct DefaultSyscallStubs {}
114impl SyscallStubs for DefaultSyscallStubs {}
115
116pub fn atlas_log(message: &str) {
117    SYSCALL_STUBS.read().unwrap().atlas_log(message);
118}
119
120pub fn atlas_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
121    atlas_log(&format!(
122        "{arg1:#x}, {arg2:#x}, {arg3:#x}, {arg4:#x}, {arg5:#x}"
123    ));
124}
125
126pub fn atlas_log_compute_units() {
127    SYSCALL_STUBS.read().unwrap().atlas_log_compute_units();
128}
129
130pub fn atlas_remaining_compute_units() -> u64 {
131    SYSCALL_STUBS.read().unwrap().atlas_remaining_compute_units()
132}
133
134pub fn atlas_invoke_signed(
135    instruction: &Instruction,
136    account_infos: &[AccountInfo],
137    signers_seeds: &[&[&[u8]]],
138) -> ProgramResult {
139    SYSCALL_STUBS
140        .read()
141        .unwrap()
142        .atlas_invoke_signed(instruction, account_infos, signers_seeds)
143}
144
145#[allow(dead_code)]
146pub(crate) fn atlas_get_sysvar(
147    sysvar_id_addr: *const u8,
148    var_addr: *mut u8,
149    offset: u64,
150    length: u64,
151) -> u64 {
152    SYSCALL_STUBS
153        .read()
154        .unwrap()
155        .atlas_get_sysvar(sysvar_id_addr, var_addr, offset, length)
156}
157
158pub(crate) fn atlas_get_clock_sysvar(var_addr: *mut u8) -> u64 {
159    SYSCALL_STUBS.read().unwrap().atlas_get_clock_sysvar(var_addr)
160}
161
162pub(crate) fn atlas_get_epoch_schedule_sysvar(var_addr: *mut u8) -> u64 {
163    SYSCALL_STUBS
164        .read()
165        .unwrap()
166        .atlas_get_epoch_schedule_sysvar(var_addr)
167}
168
169pub(crate) fn atlas_get_fees_sysvar(var_addr: *mut u8) -> u64 {
170    SYSCALL_STUBS.read().unwrap().atlas_get_fees_sysvar(var_addr)
171}
172
173pub(crate) fn atlas_get_rent_sysvar(var_addr: *mut u8) -> u64 {
174    SYSCALL_STUBS.read().unwrap().atlas_get_rent_sysvar(var_addr)
175}
176
177pub(crate) fn atlas_get_last_restart_slot(var_addr: *mut u8) -> u64 {
178    SYSCALL_STUBS
179        .read()
180        .unwrap()
181        .atlas_get_last_restart_slot(var_addr)
182}
183
184pub fn atlas_get_epoch_stake(vote_address: *const u8) -> u64 {
185    SYSCALL_STUBS
186        .read()
187        .unwrap()
188        .atlas_get_epoch_stake(vote_address)
189}
190
191pub fn atlas_get_return_data() -> Option<(Pubkey, Vec<u8>)> {
192    SYSCALL_STUBS.read().unwrap().atlas_get_return_data()
193}
194
195pub fn atlas_set_return_data(data: &[u8]) {
196    SYSCALL_STUBS.read().unwrap().atlas_set_return_data(data)
197}
198
199pub fn atlas_log_data(data: &[&[u8]]) {
200    SYSCALL_STUBS.read().unwrap().atlas_log_data(data)
201}
202
203pub fn atlas_get_processed_sibling_instruction(index: usize) -> Option<Instruction> {
204    SYSCALL_STUBS
205        .read()
206        .unwrap()
207        .atlas_get_processed_sibling_instruction(index)
208}
209
210pub fn atlas_get_stack_height() -> u64 {
211    SYSCALL_STUBS.read().unwrap().atlas_get_stack_height()
212}
213
214pub(crate) fn atlas_get_epoch_rewards_sysvar(var_addr: *mut u8) -> u64 {
215    SYSCALL_STUBS
216        .read()
217        .unwrap()
218        .atlas_get_epoch_rewards_sysvar(var_addr)
219}