Skip to main content

buildl_lua/
limits.rs

1//! The ceilings a build file's evaluation runs under.
2//!
3//! Its own file because the limits are the adapter's only configuration. The composition root is
4//! meant to fill them from the workspace manifest's `[declaration]` table. They are this crate's
5//! own type so that no caller has to name an airsl type to configure the adapter.
6//!
7//! Responsibilities: [`DeclarationLimits`] and its conversion into airsl's resource limits.
8//!
9//! Non-responsibilities: parsing the manifest, which happens before an adapter is built.
10
11use core::num::NonZeroU64;
12
13use airsl::{InstructionLimit, MemoryLimit, ResourceLimits};
14
15/// 16 MiB, the declaration memory ceiling when the manifest sets none.
16const DEFAULT_MEMORY_BYTES: NonZeroU64 = NonZeroU64::MIN.saturating_add(16 * 1024 * 1024 - 1);
17/// The declaration instruction ceiling when the manifest sets none.
18const DEFAULT_INSTRUCTIONS: NonZeroU64 = NonZeroU64::MIN.saturating_add(10_000_000 - 1);
19
20/// 100 000 entries, the host-side source walk ceiling when the manifest sets none.
21const DEFAULT_WALK_ENTRIES: NonZeroU64 = NonZeroU64::MIN.saturating_add(100_000 - 1);
22
23/// The memory, instruction and source-walk ceilings for evaluating one build file.
24///
25/// The memory ceiling bounds the Lua heap, and it is also the byte budget for the declarations the
26/// file stages outside that heap. The walk ceiling bounds the directory entries the host visits for
27/// `buildl.sources`, counted across every call the file makes.
28///
29/// # Examples
30///
31/// ```
32/// use std::num::NonZeroU64;
33///
34/// use buildl_lua::DeclarationLimits;
35///
36/// let defaults = DeclarationLimits::default();
37/// assert_eq!(defaults.memory_bytes().get(), 16 * 1024 * 1024);
38/// assert_eq!(defaults.instructions().get(), 10_000_000);
39/// assert_eq!(defaults.walk_entries().get(), 100_000);
40///
41/// let tight = DeclarationLimits::new(NonZeroU64::MIN, NonZeroU64::MIN)
42///     .with_walk_entries(NonZeroU64::MIN);
43/// assert_eq!(tight.memory_bytes().get(), 1);
44/// assert_eq!(tight.walk_entries().get(), 1);
45/// ```
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct DeclarationLimits {
48    memory_bytes: NonZeroU64,
49    instructions: NonZeroU64,
50    walk_entries: NonZeroU64,
51}
52
53impl DeclarationLimits {
54    /// Limits of `memory_bytes` bytes and `instructions` Lua VM instructions per build file, with
55    /// the default walk ceiling.
56    #[must_use]
57    pub const fn new(memory_bytes: NonZeroU64, instructions: NonZeroU64) -> Self {
58        Self {
59            memory_bytes,
60            instructions,
61            walk_entries: DEFAULT_WALK_ENTRIES,
62        }
63    }
64
65    /// The same limits with a ceiling of `walk_entries` directory entries per build file.
66    #[must_use]
67    pub const fn with_walk_entries(self, walk_entries: NonZeroU64) -> Self {
68        Self {
69            walk_entries,
70            ..self
71        }
72    }
73
74    /// The ceiling on directory entries the `buildl.sources` walk may visit, across all its calls
75    /// in one build file.
76    #[must_use]
77    pub const fn walk_entries(&self) -> NonZeroU64 {
78        self.walk_entries
79    }
80
81    /// The memory ceiling, and staging budget, in bytes.
82    #[must_use]
83    pub const fn memory_bytes(&self) -> NonZeroU64 {
84        self.memory_bytes
85    }
86
87    /// The instruction ceiling.
88    #[must_use]
89    pub const fn instructions(&self) -> NonZeroU64 {
90        self.instructions
91    }
92
93    /// The same ceilings in airsl's terms. A memory ceiling beyond the address space is clamped to
94    /// it.
95    pub(crate) fn resource_limits(&self) -> ResourceLimits {
96        let memory = usize::try_from(self.memory_bytes.get()).unwrap_or(usize::MAX);
97        ResourceLimits::new(
98            Some(MemoryLimit::bytes(memory)),
99            Some(InstructionLimit::count(self.instructions.get())),
100        )
101    }
102}
103
104impl Default for DeclarationLimits {
105    /// 16 MiB, 10 000 000 instructions and 100 000 walk entries.
106    fn default() -> Self {
107        Self::new(DEFAULT_MEMORY_BYTES, DEFAULT_INSTRUCTIONS)
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use core::num::NonZeroU64;
114
115    use airsl::{InstructionLimit, MemoryLimit};
116
117    use super::DeclarationLimits;
118
119    #[test]
120    fn defaults_to_sixteen_mebibytes_ten_million_instructions_and_a_hundred_thousand_entries() {
121        let limits = DeclarationLimits::default();
122        assert_eq!(limits.memory_bytes().get(), 16_777_216);
123        assert_eq!(limits.instructions().get(), 10_000_000);
124        assert_eq!(limits.walk_entries().get(), 100_000);
125    }
126
127    #[test]
128    fn new_takes_the_default_walk_ceiling_and_with_walk_entries_replaces_only_it() {
129        let two = NonZeroU64::MIN.saturating_add(1);
130        let limits = DeclarationLimits::new(two, two);
131        assert_eq!(limits.walk_entries().get(), 100_000);
132        let walked = limits.with_walk_entries(NonZeroU64::MIN);
133        assert_eq!(walked.walk_entries().get(), 1);
134        assert_eq!(walked.memory_bytes(), two);
135        assert_eq!(walked.instructions(), two);
136    }
137
138    #[test]
139    fn converts_into_both_airsl_ceilings() {
140        let limits = DeclarationLimits::new(NonZeroU64::MIN, NonZeroU64::MIN.saturating_add(1));
141        let resource = limits.resource_limits();
142        assert_eq!(resource.memory(), Some(MemoryLimit::bytes(1)));
143        assert_eq!(resource.instructions(), Some(InstructionLimit::count(2)));
144    }
145}