Sandboxing
Rune lets a host bound what a script can consume during compilation and execution:
- Instruction budgets: cap how many VM instructions (and cooperating native loop steps) a script may execute.
- Memory limits: cap how much memory Rune’s allocator may reserve while compiling or running a script.
- Compiler limits: cap nesting depth, macro expansion depth, and constant
evaluation work through
Options.
Note: These safeguards are meant to move Rune toward running untrusted code, but a limit only holds where the native functions your host exposes cooperate with it. Vet any custom native functions before relying on these bounds.
Instruction budgeting
Instruction budgeting is configured with budget::with in
rune::runtime::budget.
budget::with wraps both synchronous closures and futures. When wrapping a
future, the budget is suspended and restored automatically as the future is
polled.
The virtual machine consumes one permit per instruction, and Rune’s built-in
native loops draw from the same budget. Custom native functions that loop must
also take permits from rune::runtime::budget; otherwise a host cannot
interrupt them.
Memory limiting
Memory limiting is configured with limit::with in rune::alloc::limit:
#![allow(unused)]
fn main() {
use rune::alloc::prelude::*;
use rune::alloc::limit;
let f = limit::with(1024, || {
let mut vec = Vec::<u32>::try_with_capacity(256)?;
for n in 0..256u32 {
vec.try_push(n)?;
}
Ok::<_, rune_alloc::Error>(vec.into_iter().sum::<u32>())
});
let sum = f.call()?;
assert_eq!(sum, 32640);
}
For memory limiting to apply, allocations must go through the fallible
collections in rune::alloc (forks of std::collections and
hashbrown). Standard library collections bypass this limit.
Like budget::with, limit::with wraps both synchronous closures and futures,
suspending and restoring the limit across .await points.