Skip to main content

solana_sysvar/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
3//! Access to special accounts with dynamically-updated data.
4//!
5//! Sysvars are special accounts that contain dynamically-updated data about the
6//! network cluster, the blockchain history, and the executing transaction. Each
7//! sysvar is defined in its own submodule within this module. The [`clock`],
8//! [`epoch_schedule`], and [`rent`] sysvars are most useful to on-chain
9//! programs.
10//!
11//! Simple sysvars implement the [`Sysvar::get`] method, which loads a sysvar
12//! directly from the runtime, as in this example that logs the `clock` sysvar:
13//!
14//! ```
15//! use solana_account_info::AccountInfo;
16//! use solana_msg::msg;
17//! use solana_sysvar::Sysvar;
18//! use solana_program_error::ProgramResult;
19//! use solana_pubkey::Pubkey;
20//!
21//! fn process_instruction(
22//!     program_id: &Pubkey,
23//!     accounts: &[AccountInfo],
24//!     instruction_data: &[u8],
25//! ) -> ProgramResult {
26//!     let clock = solana_clock::Clock::get()?;
27//!     msg!("clock: {:#?}", clock);
28//!     Ok(())
29//! }
30//! ```
31//!
32//! Since Solana sysvars are accounts, if the `AccountInfo` is provided to the
33//! program, then the program can deserialize the sysvar with
34//! [`SysvarSerialize::from_account_info`] to access its data, as in this example that
35//! again logs the [`clock`] sysvar.
36//!
37//! ```
38//! use solana_account_info::{AccountInfo, next_account_info};
39//! use solana_msg::msg;
40//! use solana_sysvar::{Sysvar, SysvarSerialize};
41//! use solana_program_error::ProgramResult;
42//! use solana_pubkey::Pubkey;
43//!
44//! fn process_instruction(
45//!     program_id: &Pubkey,
46//!     accounts: &[AccountInfo],
47//!     instruction_data: &[u8],
48//! ) -> ProgramResult {
49//!     let account_info_iter = &mut accounts.iter();
50//!     let clock_account = next_account_info(account_info_iter)?;
51//!     let clock = solana_clock::Clock::from_account_info(&clock_account)?;
52//!     msg!("clock: {:#?}", clock);
53//!     Ok(())
54//! }
55//! ```
56//!
57//! When possible, programs should prefer to call `Sysvar::get` instead of
58//! deserializing with `Sysvar::from_account_info`, as the latter imposes extra
59//! overhead of deserialization while also requiring the sysvar account address
60//! be passed to the program, wasting the limited space available to
61//! transactions. Deserializing sysvars that can instead be retrieved with
62//! `Sysvar::get` should be only be considered for compatibility with older
63//! programs that pass around sysvar accounts.
64//!
65//! Some sysvars are too large to deserialize within a program, and
66//! `Sysvar::from_account_info` returns an error, or the serialization attempt
67//! will exhaust the program's compute budget. Some sysvars do not implement
68//! `Sysvar::get` and return an error. Some sysvars have custom deserializers
69//! that do not implement the `Sysvar` trait. These cases are documented in the
70//! modules for individual sysvars.
71//!
72//! All sysvar accounts are owned by the account identified by [`sysvar::ID`].
73//!
74//! [`sysvar::ID`]: https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/sysvar/constant.ID.html
75//!
76//! For more details see the Solana [documentation on sysvars][sysvardoc].
77//!
78//! [sysvardoc]: https://docs.solanalabs.com/runtime/sysvars
79
80// hidden re-exports to make macros work
81pub mod __private {
82    #[cfg(target_os = "solana")]
83    pub use solana_define_syscall::definitions;
84    pub use {solana_program_entrypoint::SUCCESS, solana_program_error::ProgramError};
85}
86#[cfg(feature = "bincode")]
87use {solana_account_info::AccountInfo, solana_sysvar_id::SysvarId};
88use {solana_program_error::ProgramError, solana_pubkey::Pubkey};
89
90pub mod clock;
91pub mod epoch_rewards;
92pub mod epoch_schedule;
93pub mod fees;
94pub mod last_restart_slot;
95pub mod program_stubs;
96pub mod recent_blockhashes;
97pub mod rent;
98pub mod rewards;
99pub mod slot_hashes;
100pub mod slot_history;
101
102/// Return value indicating that the  `offset + length` is greater than the length of
103/// the sysvar data.
104//
105// Defined in the bpf loader as [`OFFSET_LENGTH_EXCEEDS_SYSVAR`](https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/sysvar.rs#L172).
106const OFFSET_LENGTH_EXCEEDS_SYSVAR: u64 = 1;
107
108/// Return value indicating that the sysvar was not found.
109//
110// Defined in the bpf loader as [`SYSVAR_NOT_FOUND`](https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/sysvar.rs#L171).
111const SYSVAR_NOT_FOUND: u64 = 2;
112
113/// Interface for loading a sysvar.
114pub trait Sysvar: Default + Sized {
115    /// Load the sysvar directly from the runtime.
116    ///
117    /// This is the preferred way to load a sysvar. Calling this method does not
118    /// incur any deserialization overhead, and does not require the sysvar
119    /// account to be passed to the program.
120    ///
121    /// Not all sysvars support this method. If not, it returns
122    /// [`ProgramError::UnsupportedSysvar`].
123    fn get() -> Result<Self, ProgramError> {
124        Err(ProgramError::UnsupportedSysvar)
125    }
126}
127
128#[cfg(feature = "bincode")]
129/// A type that holds sysvar data.
130pub trait SysvarSerialize:
131    Sysvar + SysvarId + serde::Serialize + serde::de::DeserializeOwned
132{
133    /// The size in bytes of the sysvar as serialized account data.
134    fn size_of() -> usize {
135        bincode::serialized_size(&Self::default()).unwrap() as usize
136    }
137
138    /// Deserializes the sysvar from its `AccountInfo`.
139    ///
140    /// # Errors
141    ///
142    /// If `account_info` does not have the same ID as the sysvar this function
143    /// returns [`ProgramError::InvalidArgument`].
144    fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
145        if !Self::check_id(account_info.unsigned_key()) {
146            return Err(ProgramError::InvalidArgument);
147        }
148        bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
149    }
150
151    /// Serializes the sysvar to `AccountInfo`.
152    ///
153    /// # Errors
154    ///
155    /// Returns `None` if serialization failed.
156    fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
157        bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
158    }
159}
160
161/// Implements the [`Sysvar::get`] method for both SBF and host targets.
162#[macro_export]
163macro_rules! impl_sysvar_get {
164    // DEPRECATED: This variant is only for the deprecated Fees sysvar and should be
165    // removed once Fees is no longer in use. It uses the old-style direct syscall
166    // approach instead of the new sol_get_sysvar syscall.
167    ($syscall_name:ident) => {
168        fn get() -> Result<Self, $crate::__private::ProgramError> {
169            let mut var = Self::default();
170            let var_addr = &mut var as *mut _ as *mut u8;
171
172            #[cfg(target_os = "solana")]
173            let result = unsafe { $crate::__private::definitions::$syscall_name(var_addr) };
174
175            #[cfg(not(target_os = "solana"))]
176            let result = $crate::program_stubs::$syscall_name(var_addr);
177
178            match result {
179                $crate::__private::SUCCESS => Ok(var),
180                _ => Err($crate::__private::ProgramError::UnsupportedSysvar),
181            }
182        }
183    };
184    // Variant for sysvars with padding at the end. Loads bincode-serialized data
185    // (size - padding bytes) and zeros the padding to avoid undefined behavior.
186    // Only supports sysvars where padding is at the end of the layout. Caller
187    // must supply the correct number of padding bytes.
188    ($sysvar_id:expr, $padding:literal) => {
189        fn get() -> Result<Self, $crate::__private::ProgramError> {
190            let mut var = core::mem::MaybeUninit::<Self>::uninit();
191            let var_addr = var.as_mut_ptr() as *mut u8;
192            let length = core::mem::size_of::<Self>().saturating_sub($padding);
193            let sysvar_id_ptr = (&$sysvar_id) as *const _ as *const u8;
194            // SAFETY: The allocation is valid for `size_of::<Self>()`. We zero
195            // the padding bytes first, then load `(size - padding)` bytes from
196            // the syscall, which matches bincode serialization.
197            let result = unsafe {
198                var_addr.add(length).write_bytes(0, $padding);
199                $crate::get_sysvar_unchecked(var_addr, sysvar_id_ptr, 0, length as u64)
200            };
201            match result {
202                // SAFETY: All bytes initialized: padding was zeroed above,
203                // syscall filled the data bytes.
204                Ok(()) => Ok(unsafe { var.assume_init() }),
205                // Unexpected errors are folded into `UnsupportedSysvar`.
206                Err(_) => Err($crate::__private::ProgramError::UnsupportedSysvar),
207            }
208        }
209    };
210    // Variant for sysvars without padding (struct size matches bincode size).
211    ($sysvar_id:expr) => {
212        $crate::impl_sysvar_get!($sysvar_id, 0);
213    };
214}
215
216/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
217/// syscall.
218pub fn get_sysvar(
219    dst: &mut [u8],
220    sysvar_id: &Pubkey,
221    offset: u64,
222    length: u64,
223) -> Result<(), solana_program_error::ProgramError> {
224    // Check that the provided destination buffer is large enough to hold the
225    // requested data.
226    if dst.len() < length as usize {
227        return Err(solana_program_error::ProgramError::InvalidArgument);
228    }
229
230    let sysvar_id = sysvar_id as *const _ as *const u8;
231    let var_addr = dst as *mut _ as *mut u8;
232
233    #[cfg(target_os = "solana")]
234    let result = unsafe {
235        solana_define_syscall::definitions::sol_get_sysvar(sysvar_id, var_addr, offset, length)
236    };
237
238    #[cfg(not(target_os = "solana"))]
239    let result = crate::program_stubs::sol_get_sysvar(sysvar_id, var_addr, offset, length);
240
241    match result {
242        solana_program_entrypoint::SUCCESS => Ok(()),
243        OFFSET_LENGTH_EXCEEDS_SYSVAR => Err(solana_program_error::ProgramError::InvalidArgument),
244        _ => Err(solana_program_error::ProgramError::UnsupportedSysvar),
245    }
246}
247
248/// Internal helper for retrieving sysvar data directly into a raw buffer.
249///
250/// # Safety
251///
252/// This function bypasses the slice-length check that `get_sysvar` performs.
253/// The caller must ensure that `var_addr` points to a writable buffer of at
254/// least `length` bytes. This is typically used with `MaybeUninit` to load
255/// compact representations of sysvars.
256#[doc(hidden)]
257pub unsafe fn get_sysvar_unchecked(
258    var_addr: *mut u8,
259    sysvar_id: *const u8,
260    offset: u64,
261    length: u64,
262) -> Result<(), solana_program_error::ProgramError> {
263    #[cfg(target_os = "solana")]
264    let result =
265        solana_define_syscall::definitions::sol_get_sysvar(sysvar_id, var_addr, offset, length);
266
267    #[cfg(not(target_os = "solana"))]
268    let result = crate::program_stubs::sol_get_sysvar(sysvar_id, var_addr, offset, length);
269
270    match result {
271        solana_program_entrypoint::SUCCESS => Ok(()),
272        OFFSET_LENGTH_EXCEEDS_SYSVAR => Err(solana_program_error::ProgramError::InvalidArgument),
273        SYSVAR_NOT_FOUND => Err(solana_program_error::ProgramError::UnsupportedSysvar),
274        // Unexpected errors are folded into `UnsupportedSysvar`.
275        _ => Err(solana_program_error::ProgramError::UnsupportedSysvar),
276    }
277}
278
279#[cfg(test)]
280mod tests {
281    use {
282        super::*,
283        crate::program_stubs::{set_syscall_stubs, SyscallStubs},
284        serde_derive::{Deserialize, Serialize},
285        solana_program_entrypoint::SUCCESS,
286        solana_program_error::ProgramError,
287        solana_pubkey::Pubkey,
288        std::{cell::RefCell, rc::Rc},
289    };
290
291    #[repr(C)]
292    #[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
293    struct TestSysvar {
294        something: Pubkey,
295    }
296    solana_pubkey::declare_id!("TestSysvar111111111111111111111111111111111");
297    impl solana_sysvar_id::SysvarId for TestSysvar {
298        fn id() -> solana_pubkey::Pubkey {
299            id()
300        }
301
302        fn check_id(pubkey: &solana_pubkey::Pubkey) -> bool {
303            check_id(pubkey)
304        }
305    }
306    impl Sysvar for TestSysvar {}
307    impl SysvarSerialize for TestSysvar {}
308
309    // NOTE: Tests using these mocks MUST carry the #[serial] attribute
310    // because they modify global SYSCALL_STUBS state.
311
312    struct MockGetSysvarSyscall {
313        data: Vec<u8>,
314    }
315
316    impl SyscallStubs for MockGetSysvarSyscall {
317        #[allow(clippy::arithmetic_side_effects)]
318        fn sol_get_sysvar(
319            &self,
320            _sysvar_id_addr: *const u8,
321            var_addr: *mut u8,
322            offset: u64,
323            length: u64,
324        ) -> u64 {
325            let slice = unsafe { std::slice::from_raw_parts_mut(var_addr, length as usize) };
326            slice.copy_from_slice(&self.data[offset as usize..(offset + length) as usize]);
327            SUCCESS
328        }
329    }
330
331    /// Mock syscall stub for tests. Requires `#[serial]` attribute.
332    pub fn mock_get_sysvar_syscall(data: &[u8]) {
333        set_syscall_stubs(Box::new(MockGetSysvarSyscall {
334            data: data.to_vec(),
335        }));
336    }
337
338    struct ValidateIdSyscall {
339        data: Vec<u8>,
340        expected_id: Pubkey,
341    }
342
343    impl SyscallStubs for ValidateIdSyscall {
344        #[allow(clippy::arithmetic_side_effects)]
345        fn sol_get_sysvar(
346            &self,
347            sysvar_id_addr: *const u8,
348            var_addr: *mut u8,
349            offset: u64,
350            length: u64,
351        ) -> u64 {
352            // Validate that the correct sysvar id pointer was passed
353            let passed_id = unsafe { *(sysvar_id_addr as *const Pubkey) };
354            assert_eq!(passed_id, self.expected_id);
355
356            let slice = unsafe { std::slice::from_raw_parts_mut(var_addr, length as usize) };
357            slice.copy_from_slice(&self.data[offset as usize..(offset + length) as usize]);
358            SUCCESS
359        }
360    }
361
362    /// Mock syscall stub that validates sysvar ID. Requires `#[serial]` attribute.
363    pub fn mock_get_sysvar_syscall_with_id(
364        data: &[u8],
365        expected_id: &Pubkey,
366    ) -> Box<dyn SyscallStubs> {
367        set_syscall_stubs(Box::new(ValidateIdSyscall {
368            data: data.to_vec(),
369            expected_id: *expected_id,
370        }))
371    }
372
373    /// Convert a value to its in-memory byte representation.
374    ///
375    /// # Safety
376    ///
377    /// This function is only safe for plain-old-data types with no padding.
378    /// Intended for test use only.
379    pub fn to_bytes<T>(value: &T) -> Vec<u8> {
380        let size = core::mem::size_of::<T>();
381        let ptr = (value as *const T) as *const u8;
382        let mut data = vec![0u8; size];
383        unsafe {
384            std::ptr::copy_nonoverlapping(ptr, data.as_mut_ptr(), size);
385        }
386        data
387    }
388
389    #[test]
390    fn test_sysvar_account_info_to_from() {
391        let test_sysvar = TestSysvar::default();
392        let key = id();
393        let wrong_key = Pubkey::new_unique();
394        let owner = Pubkey::new_unique();
395        let mut lamports = 42;
396        let mut data = vec![0_u8; TestSysvar::size_of()];
397        let mut account_info =
398            AccountInfo::new(&key, false, true, &mut lamports, &mut data, &owner, false);
399
400        test_sysvar.to_account_info(&mut account_info).unwrap();
401        let new_test_sysvar = TestSysvar::from_account_info(&account_info).unwrap();
402        assert_eq!(test_sysvar, new_test_sysvar);
403
404        account_info.key = &wrong_key;
405        assert_eq!(
406            TestSysvar::from_account_info(&account_info),
407            Err(ProgramError::InvalidArgument)
408        );
409
410        let mut small_data = vec![];
411        account_info.data = Rc::new(RefCell::new(&mut small_data));
412        assert_eq!(test_sysvar.to_account_info(&mut account_info), None);
413    }
414}