Skip to main content

solana_program_runtime/program_cache/
env.rs

1use {
2    crate::invoke_context::InvokeContext, solana_sbpf::program::BuiltinProgram,
3    solana_svm_type_overrides::sync::Arc,
4};
5
6/// The registered functions (syscalls) and VM configuration (`Config`) that an
7/// sBPF executable is verified, compiled, and executed against.
8///
9/// Compared and hashed by pointer identity, not by contents: environments built
10/// separately are never equal, even if their functions and configuration match.
11#[repr(transparent)]
12#[derive(Clone, Debug)]
13pub struct ProgramRuntimeEnvironment(Arc<BuiltinProgram<InvokeContext<'static, 'static>>>);
14
15impl std::hash::Hash for ProgramRuntimeEnvironment {
16    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
17        Arc::<BuiltinProgram<InvokeContext<'static, 'static>>>::as_ptr(&self.0).hash(state);
18    }
19}
20
21impl PartialEq for ProgramRuntimeEnvironment {
22    fn eq(&self, other: &Self) -> bool {
23        Arc::ptr_eq(&self.0, &other.0)
24    }
25}
26
27impl Eq for ProgramRuntimeEnvironment {}
28
29impl std::ops::Deref for ProgramRuntimeEnvironment {
30    type Target = Arc<BuiltinProgram<InvokeContext<'static, 'static>>>;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl ProgramRuntimeEnvironment {
38    pub fn from(inner: BuiltinProgram<InvokeContext<'static, 'static>>) -> Self {
39        Self(Arc::new(inner))
40    }
41
42    pub const fn from_ref<'a>(
43        inner: &'a Arc<BuiltinProgram<InvokeContext<'static, 'static>>>,
44    ) -> &'a Self {
45        // Safety: This wrapper type is transparent and shares the same representation as the underlying type
46        unsafe { std::mem::transmute(inner) }
47    }
48}
49
50/// Paired execution and deployment environments.
51///
52/// Registered functions within each program runtime environment (syscalls)
53/// depend on per-epoch feature gate statuses. In most cases, the list of
54/// registered functions in the two environments will be the same. However,
55/// it's possible that the effective epoch of deployment could be in the
56/// *next epoch*.
57///
58/// SIMD-0688 (Single Program Runtime Environment) removes the need for a
59/// separate deployment environment. Once it's active, this API can be cleaned
60/// up.
61pub struct ProgramRuntimeEnvironments {
62    /// Environment compiled for the current epoch in which programs are
63    /// executing.
64    execution: ProgramRuntimeEnvironment,
65    /// Environment compiled for the epoch of the next slot at which a program
66    /// deployed in the current slot will execute.
67    deployment: ProgramRuntimeEnvironment,
68}
69
70impl ProgramRuntimeEnvironments {
71    /// Create a new ProgramRuntimeEnvironments from an `execution` and
72    /// `deployment` environment.
73    pub fn new(
74        execution: ProgramRuntimeEnvironment,
75        deployment: ProgramRuntimeEnvironment,
76    ) -> Self {
77        Self {
78            execution,
79            deployment,
80        }
81    }
82
83    /// Get the program runtime environment for execution.
84    pub fn get_env_for_execution(&self) -> &ProgramRuntimeEnvironment {
85        &self.execution
86    }
87
88    /// Get the program runtime environment for deployment.
89    pub fn get_env_for_deployment(&self) -> &ProgramRuntimeEnvironment {
90        &self.deployment
91    }
92
93    #[cfg(feature = "dev-context-only-utils")]
94    pub fn mock() -> Self {
95        Self {
96            execution: get_mock_program_runtime_environment(),
97            deployment: get_mock_program_runtime_environment(),
98        }
99    }
100}
101
102#[cfg(feature = "dev-context-only-utils")]
103pub fn get_mock_program_runtime_environment() -> ProgramRuntimeEnvironment {
104    static MOCK_ENVIRONMENT: std::sync::OnceLock<ProgramRuntimeEnvironment> =
105        std::sync::OnceLock::<ProgramRuntimeEnvironment>::new();
106    MOCK_ENVIRONMENT
107        .get_or_init(|| ProgramRuntimeEnvironment::from(BuiltinProgram::new_mock()))
108        .clone()
109}