use std::cell::RefCell;
use std::rc::Rc;
use super::base::{capture, restore, text};
use super::*;
use crate::host::{HostValue, ProofCounter, ProofHandle, USERDATA_NATIVES};
fn boot_ud(spec: &crate::program::ProtoSpec) -> Runtime {
boot_ud_with(Config::default(), spec)
}
fn boot_ud_with(config: Config, spec: &crate::program::ProtoSpec) -> Runtime {
let mut runtime = Runtime::boot(config, HostRegistry::proof(), spec, false).unwrap();
runtime.install_standard().unwrap();
runtime.install_debug().unwrap();
for (name, _) in USERDATA_NATIVES {
runtime.set_global_native(name, name).unwrap();
}
runtime.set_global_native("park", "park").unwrap();
runtime
}
fn corpus() -> crate::program::ProtoSpec {
let mut chunk = crate::compile(&fixture("corpus_userdata.lua")).unwrap();
chunk.set_chunk_name(b"@corpus_userdata.lua");
chunk.proto
}
fn straight_output(spec: &crate::program::ProtoSpec) -> (String, Runtime) {
let mut runtime = boot_ud(spec);
let written = capture(&mut runtime);
let outcome = runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
assert!(matches!(outcome, StepOutcome::Completed), "{outcome:?}");
(text(&written), runtime)
}
fn printed(source: &str) -> String {
let chunk = crate::compile(source.as_bytes()).unwrap();
straight_output(&chunk.proto).0
}
fn waiting(source: &str) -> (Runtime, crate::WaitKey) {
let chunk = crate::compile(source.as_bytes()).unwrap();
let mut runtime = boot_ud(&chunk.proto);
let outcome = runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
let StepOutcome::Waiting(key) = outcome else {
panic!("{outcome:?}");
};
(runtime, key)
}
#[test]
fn userdata_corpus_matches_lua() {
let (output, _) = straight_output(&corpus());
let expected = String::from_utf8(fixture("corpus_userdata.out")).unwrap();
for (index, (a, b)) in output.lines().zip(expected.lines()).enumerate() {
assert_eq!(a, b, "line {}", index + 1);
}
assert_eq!(output.lines().count(), expected.lines().count());
}
#[test]
#[ignore = "lua54_oracle"]
fn lua54_oracle_matches_the_userdata_corpus() {
let lua = crate::hostcaps::native::test_support::var("MOONSEED_LUA54_UD")
.expect("MOONSEED_LUA54_UD must point at the userdata harness");
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/lua");
let output = lua_command(&lua)
.current_dir(&root)
.arg("corpus_userdata.lua")
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(fixture("corpus_userdata.out")).unwrap()
);
}
#[test]
fn userdata_corpus_keeps_its_output_under_every_schedule() {
let spec = corpus();
let (expected, straight) = straight_output(&spec);
for quantum in [1u64, 3, 7] {
let mut runtime = boot_ud(&spec);
let written = capture(&mut runtime);
let mut journal = Journal::new();
while let StepOutcome::Paused(_) = runtime.run(quantum, &mut journal).unwrap() {}
assert_eq!(text(&written), expected, "quantum {quantum}");
assert_eq!(runtime.fuel_consumed(), straight.fuel_consumed());
}
let written = Rc::new(RefCell::new(Vec::new()));
let attach = |runtime: &mut Runtime| {
let sink = written.clone();
runtime.set_output(Box::new(move |bytes| {
sink.borrow_mut().extend_from_slice(bytes)
}));
};
let mut runtime = boot_ud(&spec);
attach(&mut runtime);
let mut journal = Journal::new();
let mut twin = boot_ud(&spec);
let mut twin_journal = Journal::new();
loop {
runtime.collect();
twin.collect();
runtime = restore(&runtime);
attach(&mut runtime);
let outcome = runtime.run(1, &mut journal).unwrap();
assert_eq!(twin.run(1, &mut twin_journal).unwrap(), outcome);
match outcome {
StepOutcome::Paused(_) => {}
StepOutcome::Completed => break,
other => panic!("{other:?}"),
}
}
assert_eq!(String::from_utf8_lossy(&written.borrow()), expected);
assert_eq!(runtime.fuel_consumed(), twin.fuel_consumed());
}
#[test]
fn user_values_keep_what_they_hold() {
let output = printed(
"local u = newud(0, 2) \
debug.setuservalue(u, string.rep('x', 100000), 1) \
local holder = {} debug.setuservalue(u, holder, 2) \
holder.back = u \
collectgarbage() collectgarbage() \
print(#debug.getuservalue(u, 1), debug.getuservalue(u, 2) == holder) \
local with = collectgarbage('count') \
u, holder = nil, nil \
collectgarbage() \
print(with - collectgarbage('count') > 90)",
);
assert_eq!(output, "100000\ttrue\ntrue\n");
}
#[test]
fn host_objects_take_methods_through_their_metatable() {
let output = printed(
"local Counter = {} Counter.__index = Counter \
Counter.get = counter_get Counter.add = counter_add \
local c = counter_new(5, Counter) \
print(type(c), c:get(), c:add(3):get(), c:add(-10):get()) \
local d = counter_new(1, Counter) \
print(c ~= d, getmetatable(c) == getmetatable(d), d:get()) \
print(pcall(counter_get, newud(0))) \
print(pcall(counter_get, handle_new(1))) \
print(pcall(handle_get, c)) \
print(pcall(counter_get, light(1))) \
print(pcall(counter_add, c, 'x')) \
print(pcall(counter_get)) \
debug.setmetatable(c, { __index = { get = function() return 'replaced' end } }) \
print(c:get(), d:get(), handle_get(handle_new(9)))",
);
assert_eq!(
output,
"userdata\t5\t8\t-2\n\
true\ttrue\t1\n\
false\tbad argument #1 to 'counter_get' (moonseed.Counter expected, got userdata)\n\
false\tbad argument #1 to 'counter_get' (moonseed.Counter expected, got userdata)\n\
false\tbad argument #1 to 'handle_get' (moonseed.Handle expected, got userdata)\n\
false\tbad argument #1 to 'counter_get' (moonseed.Counter expected, got light userdata)\n\
false\tbad argument #2 to 'counter_add' (number expected, got string)\n\
false\tbad argument #1 to 'counter_get' (moonseed.Counter expected, got no value)\n\
replaced\t1\t9\n"
);
}
#[test]
fn the_host_reaches_payloads_only_by_their_type() {
let chunk = crate::compile(
b"local b = newud(4) udpoke(b, 1, 9) \
return b, counter_new(7), handle_new(3), light(5)",
)
.unwrap();
let mut runtime = boot_ud(&chunk.proto);
runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
let results = runtime.results().unwrap();
let id = |index: usize| match results[index] {
HostValue::Object(id) => id,
ref other => panic!("{other:?}"),
};
let (bytes, counter, handle) = (id(0), id(1), id(2));
assert_eq!(
runtime.with_userdata_bytes(bytes, <[u8]>::to_vec),
Some(vec![0, 9, 0, 0])
);
runtime.with_userdata_bytes_mut(bytes, |bytes| bytes[3] = 4);
assert_eq!(
runtime.with_userdata_bytes(bytes, |bytes| bytes[3]),
Some(4)
);
assert_eq!(
runtime.with_userdata::<ProofCounter, _>(counter, |c| c.count),
Some(7)
);
runtime.with_userdata_mut::<ProofCounter, _>(counter, |c| c.count = 70);
assert_eq!(
runtime.with_userdata::<ProofCounter, _>(counter, |c| c.count),
Some(70)
);
assert!(
runtime
.with_userdata::<ProofHandle, _>(counter, |_| ())
.is_none()
);
assert!(
runtime
.with_userdata::<ProofCounter, _>(bytes, |_| ())
.is_none()
);
assert!(runtime.with_userdata_bytes(counter, |_| ()).is_none());
assert_eq!(
runtime.with_userdata::<ProofHandle, _>(handle, |h| h.0),
Some(3)
);
let HostValue::LightUserdata(light) = results[3] else {
panic!("{:?}", results[3]);
};
assert_eq!(light.host_key(), Some(crate::HostLightKey(5)));
}
#[test]
fn portable_host_userdata_cross_a_checkpoint() {
let (runtime, key) = waiting(
"local mt = { __index = { get = counter_get, add = counter_add } } \
local c = counter_new(40, mt) \
debug.setuservalue(c, 'kept') \
local t = { [c] = 'key' } \
local by = park() \
c:add(by) \
return c:get(), t[c], debug.getuservalue(c), getmetatable(c) == mt",
);
let mut restored = restore(&runtime);
restored
.complete_legacy(
key,
crate::host::LegacyCompletion::Return(vec![HostValue::Integer(2)]),
)
.unwrap();
let mut journal = Journal::new();
assert!(matches!(
restored.run_until_terminal(u64::MAX, &mut journal).unwrap(),
StepOutcome::Completed
));
assert_eq!(
restored.results().unwrap(),
vec![
HostValue::Integer(42),
HostValue::String(b"key".to_vec()),
HostValue::String(b"kept".to_vec()),
HostValue::Boolean(true),
]
);
}
#[test]
fn snapshots_refuse_host_userdata_without_a_codec() {
let (mut runtime, key) = waiting("local h = handle_new(1) park() h = nil");
assert_eq!(
runtime.snapshot().err(),
Some(SnapshotError::NonPortableUserdata)
);
runtime
.complete_legacy(key, crate::host::LegacyCompletion::Return(vec![]))
.unwrap();
runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
assert_eq!(
runtime.snapshot().err(),
Some(SnapshotError::NonPortableUserdata)
);
runtime.collect();
restore(&runtime);
}
#[test]
fn userdata_count_against_the_heap_quota() {
let config = Config {
max_logical_heap: 4 << 20,
..Config::default()
};
let run = |source: &str| {
let chunk = crate::compile(source.as_bytes()).unwrap();
let mut runtime = boot_ud_with(config.clone(), &chunk.proto);
let written = capture(&mut runtime);
let outcome = runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
assert!(matches!(outcome, StepOutcome::Completed), "{outcome:?}");
text(&written)
};
for make in [
"newud(65536)",
"newud(0, 4096)",
"counter_new(1, nil, 65536)",
] {
let output = run(&format!(
"local keep = {{}} \
local ok, err = pcall(function() for i = 1, 100000 do keep[i] = {make} end end) \
print(ok, err, #keep < 100)"
));
assert_eq!(output, "false\tnot enough memory\ttrue\n", "{make}");
}
let output = run("print(pcall(newud, 1 << 40)) \
print(pcall(newud, 0, 70000)) \
print(pcall(counter_new, 1, nil, 1 << 62)) \
local c = counter_new(1) \
print(pcall(counter_grow, c, 1 << 30)) \
print(pcall(counter_grow, c, 1024), collectgarbage('count') > 1)");
assert_eq!(
output,
"false\tnot enough memory\n\
false\tnot enough memory\n\
false\tnot enough memory\n\
false\tnot enough memory\n\
true\ttrue\n"
);
}
#[test]
fn restore_refuses_userdata_it_cannot_make() {
use crate::snapshot::{EncValue, Image, PayloadImage};
let (runtime, _) = waiting(
"local c = counter_new(3) local b = newud(4, 1) \
debug.setuservalue(b, debug.upvalueid(function() return c end, 1)) \
park()",
);
restore(&runtime);
let domain = runtime.effect_domain();
let refused = |registry: &HostRegistry, change: &dyn Fn(&mut Image), expected| {
let mut image = runtime.to_image().unwrap();
change(&mut image);
let bytes = snapshot::encode(&image).unwrap();
expect_snapshot(Runtime::from_snapshot(&bytes, registry, domain), expected);
};
let unchanged = |_: &mut Image| {};
fn host(image: &mut Image) -> &mut crate::snapshot::UserdataImage {
image
.userdata
.iter_mut()
.find(|u| matches!(u.payload, PayloadImage::Host { .. }))
.unwrap()
}
fn bytes(image: &mut Image) -> &mut crate::snapshot::UserdataImage {
image
.userdata
.iter_mut()
.find(|u| matches!(u.payload, PayloadImage::Bytes(_)))
.unwrap()
}
let mut without = HostRegistry::proof();
without.register_userdata::<ProofHandle>();
let mut bare = HostRegistry::new();
crate::library::register_standard(&mut bare);
crate::debuglib::register_debug(&mut bare);
for (symbol, function) in USERDATA_NATIVES {
bare.register_native(symbol, crate::NativePolicy::VmLocal, function);
}
bare.register_native(
"park",
crate::NativePolicy::VmLocal,
crate::host::native_park,
);
refused(&bare, &unchanged, SnapshotError::UnknownUserdataType);
refused(
&HostRegistry::proof(),
&|image| {
if let PayloadImage::Host { symbol, .. } = &mut host(image).payload {
*symbol = "moonseed.Handle".into();
}
},
SnapshotError::UserdataPolicyMismatch,
);
let proof = HostRegistry::proof();
refused(
&proof,
&|image| {
if let PayloadImage::Host { bytes, .. } = &mut host(image).payload {
bytes.pop();
}
},
SnapshotError::UserdataDecode,
);
refused(
&proof,
&|image| host(image).charge -= 1,
SnapshotError::UserdataCharge,
);
refused(
&proof,
&|image| bytes(image).charge += 1,
SnapshotError::InvalidStructure,
);
refused(
&proof,
&|image| host(image).charge = image_quota_plus_one(),
SnapshotError::LimitExceeded,
);
refused(
&proof,
&|image| {
let string = image.strings[0].0;
bytes(image).metatable = string;
},
SnapshotError::InvalidStructure,
);
refused(
&proof,
&|image| bytes(image).user_values[0] = EncValue::Table(1 << 40),
SnapshotError::DanglingReference,
);
refused(
&proof,
&|image| {
let next = image.next_object_id;
bytes(image).user_values[0] = EncValue::Light(crate::value::LightDomain::Upvalue, next);
},
SnapshotError::InvalidStructure,
);
refused(
&proof,
&|image| {
bytes(image).user_values[0] = EncValue::Light(crate::value::LightDomain::Upvalue, 0);
},
SnapshotError::InvalidStructure,
);
refused(
&proof,
&|image| {
bytes(image).user_values[0] =
EncValue::Light(crate::value::LightDomain::NativeValue, 1 << 8 | 200);
},
SnapshotError::InvalidStructure,
);
let mut image = runtime.to_image().unwrap();
let EncValue::Light(_, bits) = bytes(&mut image).user_values[0] else {
panic!("no token");
};
bytes(&mut image).user_values[0] = EncValue::Light(crate::value::LightDomain::Host, bits);
Runtime::from_snapshot(&snapshot::encode(&image).unwrap(), &proof, domain).unwrap();
}
fn image_quota_plus_one() -> u64 {
Config::default().max_logical_heap + 1
}
#[test]
fn tampered_userdata_sections_fail_closed() {
let (runtime, _) = waiting("local b = newud(3, 2) udpoke(b, 0, 7) park()");
let bytes = runtime.snapshot().unwrap();
let registry = HostRegistry::proof();
let domain = runtime.effect_domain();
let at = bytes
.windows(7)
.position(|window| window == [3, 0, 0, 0, 7, 0, 0])
.unwrap();
for (len, expected) in [
(u32::MAX, SnapshotError::LimitExceeded),
(1 << 19, SnapshotError::Truncated),
] {
let mut tampered = bytes.clone();
tampered[at..at + 4].copy_from_slice(&len.to_le_bytes());
recrc(&mut tampered);
expect_snapshot(
Runtime::from_snapshot(&tampered, ®istry, domain),
expected,
);
}
let mut tampered = bytes.clone();
tampered[at..at + 4].copy_from_slice(&2u32.to_le_bytes());
recrc(&mut tampered);
assert!(Runtime::from_snapshot(&tampered, ®istry, domain).is_err());
let mut cut = bytes[..bytes.len() - 10].to_vec();
recrc(&mut cut);
assert!(Runtime::from_snapshot(&cut, ®istry, domain).is_err());
}
#[test]
fn userdata_count_against_the_object_limit() {
let config = Config {
max_objects: 1000,
..Config::default()
};
let chunk = crate::compile(
b"keep = {} \
local ok, err = pcall(function() for i = 1, 3000 do keep[i] = newud(0) end end) \
print(ok, err, #keep < 1000)",
)
.unwrap();
let mut runtime = boot_ud_with(config, &chunk.proto);
let written = capture(&mut runtime);
runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
assert_eq!(text(&written), "false\tnot enough memory\ttrue\n");
assert!(runtime.memory().objects <= 1000);
let mut image = runtime.to_image().unwrap();
image.max_objects = 10;
expect_snapshot(
Runtime::from_snapshot(
&snapshot::encode(&image).unwrap(),
&HostRegistry::proof(),
runtime.effect_domain(),
),
SnapshotError::LimitExceeded,
);
}
#[test]
fn unreported_growth_is_refused_when_the_snapshot_is_taken() {
let chunk = crate::compile(b"return counter_new(1)").unwrap();
let mut runtime = boot_ud(&chunk.proto);
runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
let HostValue::Object(id) = runtime.results().unwrap()[0] else {
panic!("no counter");
};
let before = runtime.memory().logical_bytes;
runtime.with_userdata_mut::<ProofCounter, _>(id, |c| c.size = 4096);
assert!(runtime.memory().logical_bytes >= before + 4096 - 16);
restore(&runtime);
let (mut runtime, _) = waiting("local c = counter_new(1) park() return c");
let (index, generation) = runtime
.heap()
.userdata
.iter()
.map(|(index, generation, _)| (index, generation))
.next()
.unwrap();
runtime
.heap_mut()
.userdata
.get_mut(crate::id::Handle::new(index, generation))
.unwrap()
.payload
.host_mut::<ProofCounter>()
.unwrap()
.size = 4096;
assert_eq!(
runtime.snapshot().err(),
Some(SnapshotError::UserdataCharge)
);
}
#[test]
fn only_host_keys_come_in_from_the_host() {
let chunk =
crate::compile(b"local x = 1 return debug.upvalueid(function() return x end, 1)").unwrap();
let mut maker = boot_ud(&chunk.proto);
maker
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
let token = maker.results().unwrap()[0].clone();
assert!(matches!(token, HostValue::LightUserdata(light) if light.host_key().is_none()));
let (mut runtime, key) = waiting("return park() == light(5)");
assert!(
runtime
.complete_legacy(key, crate::host::LegacyCompletion::Return(vec![token]))
.is_err()
);
let host = HostValue::LightUserdata(crate::LightUserdata::host(crate::HostLightKey(5)));
runtime
.complete_legacy(key, crate::host::LegacyCompletion::Return(vec![host]))
.unwrap();
runtime
.run_until_terminal(u64::MAX, &mut Journal::new())
.unwrap();
assert_eq!(runtime.results().unwrap(), vec![HostValue::Boolean(true)]);
}