Skip to main content

ic_memory/runtime/
policy.rs

1use crate::{
2    AllocationPolicy, MemoryManagerSlot, PolicyIdentity, PolicyIdentityError,
3    RuntimeBootstrapPolicy, StableKey, slot::IC_MEMORY_LEDGER_STABLE_KEY,
4};
5use std::convert::Infallible;
6
7// Resolution admits every external row under the host pool. This adapter only
8// keeps private governance outside application callbacks; it cannot accept raw
9// declarations from a public caller or supply a different allocation policy.
10pub(super) struct RuntimeMemoryManagerPolicy<'a, P> {
11    pub(super) custom_policy: &'a P,
12}
13
14impl<P: AllocationPolicy> AllocationPolicy for RuntimeMemoryManagerPolicy<'_, P> {
15    type Error = P::Error;
16
17    fn validate_key(&self, key: &StableKey) -> Result<(), Self::Error> {
18        if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
19            return Ok(());
20        }
21        self.custom_policy.validate_key(key)
22    }
23
24    fn validate_slot(&self, key: &StableKey, slot: &MemoryManagerSlot) -> Result<(), Self::Error> {
25        if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
26            return Ok(());
27        }
28        self.custom_policy.validate_slot(key, slot)
29    }
30}
31
32///
33/// GenericAllocationPolicy
34///
35/// Built-in bootstrap policy for hosts needing no additional application checks.
36/// The runtime enforces host namespace grants, the common pool and governance;
37/// this policy adds no application-specific restrictions. Passing it directly
38/// to allocation validation outside the runtime does not enforce host pool admission.
39///
40/// Use with configured bootstrap when the host does not require a custom
41/// policy. It retains the built-in policy identity and does not authorize
42/// replacing a different policy already bound to the runtime.
43///
44pub struct GenericAllocationPolicy;
45
46impl AllocationPolicy for GenericAllocationPolicy {
47    type Error = Infallible;
48
49    fn validate_key(&self, _key: &StableKey) -> Result<(), Self::Error> {
50        Ok(())
51    }
52
53    fn validate_slot(
54        &self,
55        _key: &StableKey,
56        _slot: &MemoryManagerSlot,
57    ) -> Result<(), Self::Error> {
58        Ok(())
59    }
60}
61
62impl RuntimeBootstrapPolicy for GenericAllocationPolicy {
63    fn runtime_bootstrap_identity(&self) -> Result<PolicyIdentity, PolicyIdentityError> {
64        PolicyIdentity::new("ic-memory.noop-policy", 1)
65    }
66}