Files
moonwell-client-core/crates/benilla-lua/tests/oom.rs
T
Mathih13 d6655d8c10 benilla-lua: benilla's own 1.12 Lua VM, opt-in behind benilla-vm (#1254)
benilla-lua is a Lua 5.1.5 interpreter in Rust that speaks the 1.12.1 client's grammar. It is opt-in behind the `benilla-vm` feature, with `benilla-mlua`, the slice of mlua that benilla-ui uses, over it. The C build stays the default. Its front end ports 5.1.5's `lparser.c` and `lcode.c`, so registers, limits and messages match the C build's.

Our finish on top:
- the collector counts growth as C does, off the write path;
- frames under a leaky generic `for` start nil, as `luaD_precall`;
- the budget hook counts what C dispatches, and costs nothing when unset;
- `__index` chains stop at 100;
- mlua 0.11's conversions;
- `vm_parity` answers recorded per platform.

Tests: `vm_parity` on both VMs; the front-end corpus (8,543 chunks) against `string.dump`; 7,613 app and UI tests on benilla-lua. Built on macOS; Linux by CI.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Sam <samuv.eth@gmail.com>
2026-10-16 16:16:09 +07:00

59 lines
1.8 KiB
Rust

//! Allocations the allocator refuses: under an address-space limit, a concatenation whose result
//! cannot be built raises `not enough memory` where Rust's allocator would abort the process.
//! Its own test binary: the limit is the whole process's.
#[cfg(target_os = "linux")]
#[test]
fn a_concatenation_the_allocator_refuses_is_not_enough_memory() {
#[repr(C)]
struct Rlimit {
cur: u64,
max: u64,
}
extern "C" {
fn setrlimit(resource: i32, rlim: *const Rlimit) -> i32;
}
/// `RLIMIT_AS` on Linux.
const RLIMIT_AS: i32 = 9;
let gib = 1u64 << 30;
// SAFETY: a plain libc call with a valid struct; it only lowers this process's limit.
assert_eq!(
unsafe {
setrlimit(
RLIMIT_AS,
&Rlimit {
cur: 3 * gib / 2,
max: u64::MAX,
},
)
},
0
);
let got = std::thread::Builder::new()
.stack_size(2 << 20)
.spawn(|| {
let mut vm = benilla_lua::new_script_host_state();
let src = br#"local s = string.rep("x", 2^28)
local ok1, e1 = pcall(function() return s .. s .. s .. s .. s .. s end)
local ok2, e2 = pcall(function() return s .. s .. s .. s end)
return ok1, e1, ok2, e2, string.len(s .. "y")"#;
match vm.exec(src, b"=t") {
Ok(vs) => Ok(vs.into_iter().map(|v| vm.display(v)).collect::<Vec<_>>()),
Err(e) => Err(vm.display(e)),
}
})
.unwrap()
.join()
.unwrap();
assert_eq!(
got,
Ok(vec![
"false".into(),
"not enough memory".into(),
"false".into(),
"not enough memory".into(),
"268435457".into()
])
);
}