Scripts: charge an indexer's object and key to the run's data limits

Reading `object[k]` on an object of a host class with an indexer copied
the object's class name and handle, and a name key, to the indexer
without charging them to the data limits. A member call on the same
object was charged and could be refused. The indexer now reads the
object and a name key the same way, so `object[k]` throws a RangeError
when the handle or the name exceeds the limits.
This commit is contained in:
dotCooCoo committed 2026-11-27 16:47:00 -08:00
1 parent c584697d2d
commit 2a476af2b8
3 files changed
+41 -2

No files matched your search

+17
View File
@@ -307,6 +307,23 @@ impl Interp {
self.host_ref_of(id)?.ok_or_else(|| Abrupt::Internal("host object without a class".into()))
}
/// The arguments an indexer named `name` gets for `object[k]`: the object of a host class `id`
/// and the key `k`, charged to the data limits as the arguments of a member call are. `None`
/// when `id` is not an object of a host class.
pub(crate) fn read_indexer_args(&mut self, name: &Rc<str>, id: ObjId, k: Data) -> JsResult<Option<Vec<Data>>> {
if self.host_ref_size(id)?.is_none() {
return Ok(None);
}
let mut reading =
Reading { limits: self.data_limits, bytes: 0, entries: 0, ancestors: Vec::new(), path: Vec::new(), function: name.clone(), hosted: true };
let this = self.read_host_ref(id, &mut reading)?;
if let Data::String(s) = &k {
reading.path.push(Step::Index(0));
self.charge_reading(&mut reading, s.encode_utf16().count().saturating_mul(3).saturating_add(2))?;
}
Ok(Some(vec![this, k]))
}
fn read_array(&mut self, id: ObjId, length: u32, r: &mut Reading) -> JsResult<Data> {
self.charge_entries(r, length as usize)?;
let mut items = Vec::with_capacity(length as usize);
+6 -2
View File
@@ -239,8 +239,12 @@ impl Interp {
/// gets `true` as a third argument.
pub(crate) fn host_index(&mut self, f: &HostFunction, id: ObjId, k: Data, probe: bool) -> JsResult<Value> {
self.use_fuel()?;
let Some(this) = self.host_ref_of(id)? else { return Ok(Value::Undefined) };
let mut args = vec![this, k];
let Class::Host(h) = &self.object(id)?.class else { return Ok(Value::Undefined) };
let name: Rc<str> = match self.host_classes.get(h.class as usize) {
Some(e) => format!("{}[]", e.name).into(),
None => return Err(Abrupt::Internal("host class out of range".into())),
};
let Some(mut args) = self.read_indexer_args(&name, id, k)? else { return Ok(Value::Undefined) };
if probe {
args.push(Data::Bool(true));
}
+18
View File
@@ -840,6 +840,24 @@ fn the_handles_of_host_objects_given_to_the_host_count_toward_the_data_limits()
assert_eq!(r.run("thing.take([thing, thing]) === undefined").unwrap(), JsValue::Bool(true));
}
#[test]
fn the_object_an_indexer_reads_counts_toward_the_data_limits() {
let mut r = realm(100_000_000);
let index: HostFunction = Rc::new(|_| Ok(Data::Number(1.0)));
let class = HostClass { name: "List".into(), parent: None, members: Vec::new(), index: Some(index), construct: None };
r.define_host_classes(vec![class], vec![("long".into(), host_ref("List", &[0.0; 1024])), ("short".into(), host_ref("List", &[0.0]))]).unwrap();
r.set_data_limits(DataLimits { depth: 64, bytes: 16 << 20, entries: 1_000 });
let probe = "var m = 'none'; try { long[0]; } catch (e) { m = e.name + '|' + e.message; } m";
let JsValue::String(out) = r.run(probe).unwrap() else { panic!("not a string") };
assert!(out.starts_with("RangeError|") && out.contains("too many entries"), "{out}");
// A long name given to the indexer counts too.
r.set_data_limits(DataLimits { depth: 64, bytes: 1_000, entries: 1_000 });
let probe = "var m = 'none'; try { short[new Array(600).join('x')]; } catch (e) { m = e.name + '|' + e.message; } m";
let JsValue::String(out) = r.run(probe).unwrap() else { panic!("not a string") };
assert!(out.starts_with("RangeError|") && out.contains("too large"), "{out}");
assert_eq!(r.run("short[0] + short.name").unwrap(), JsValue::Number(2.0));
}
#[test]
fn a_host_class_constructor_called_with_new_cannot_wait() {
let mut r = realm(1_000_000);