resolve: validate owns the depth rule; expand keeps a named stack bound with its own code; tests
This commit is contained in:
parent
93a70166c9
commit
a914f79182
2 changed files with 36 additions and 85 deletions
|
|
@ -18,7 +18,8 @@ use super::{Limits, std as thing_std, validate};
|
|||
/// rule (`Limits::max_tree_depth`) is judged by validate on the finished tree, where a slot argument
|
||||
/// the body never places does not count. It exists because an in-memory tree from a Rust caller can
|
||||
/// be deeper than any decoded document (CultCache bounds decoded nesting at 256), and recursion
|
||||
/// over it would overflow the stack.
|
||||
/// over it would overflow the stack. A level costs a few tens of KiB of stack in a debug build, so a
|
||||
/// caller resolving untrusted in-memory trees needs a stack sized for this many levels.
|
||||
const STACK_BOUND: usize = 1024;
|
||||
|
||||
/// A fault the nearest enclosing Use turns into a placeholder (`Invalid`), or one that ends the
|
||||
|
|
@ -326,58 +327,11 @@ impl<'a> Expander<'a> {
|
|||
}
|
||||
},
|
||||
Node::Use(used) => self.expand_use(used, scope, depth, root)?,
|
||||
Node::Text(_) | Node::Image(_) | Node::Toggle(_) | Node::Slider(_) | Node::TextInput(_) | Node::Select(_) | Node::Plot(_) | Node::Embed(_) => {
|
||||
self.leaf(node, scope, root)?
|
||||
Node::Box(b) => {
|
||||
let common = self.common(&b.common, scope, root)?;
|
||||
let children = self.children(&b.children, scope, depth + 1)?;
|
||||
Node::Box(BoxNode { common, style: b.style.clone(), paint: b.paint.clone(), role: b.role.clone(), children })
|
||||
}
|
||||
Node::Box(b) => self.expand_box(b, scope, depth, root)?,
|
||||
Node::Button(b) => self.expand_button(b, scope, depth, root)?,
|
||||
Node::Repeat(r) => self.expand_repeat(r, scope, depth, root)?,
|
||||
Node::Overlay(o) => self.expand_overlay(o, scope, depth, root)?,
|
||||
})
|
||||
}
|
||||
|
||||
// The containers: each in its own frame, so a level of recursion pays for one arm.
|
||||
#[inline(never)]
|
||||
fn expand_box(&mut self, b: &BoxNode, scope: Option<&Scope>, depth: usize, root: bool) -> Res<Node> {
|
||||
let common = self.common(&b.common, scope, root)?;
|
||||
let children = self.children(&b.children, scope, depth + 1)?;
|
||||
Ok(Node::Box(BoxNode { common, style: b.style.clone(), paint: b.paint.clone(), role: b.role.clone(), children }))
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn expand_button(&mut self, b: &ButtonNode, scope: Option<&Scope>, depth: usize, root: bool) -> Res<Node> {
|
||||
let common = self.common(&b.common, scope, root)?;
|
||||
let on_activate = self.command(&b.on_activate, scope)?;
|
||||
let enabled = self.boolean(&b.enabled, scope)?;
|
||||
let content = self.children(&b.content, scope, depth + 1)?;
|
||||
Ok(Node::Button(ButtonNode { common, content, on_activate, enabled }))
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn expand_repeat(&mut self, r: &RepeatNode, scope: Option<&Scope>, depth: usize, root: bool) -> Res<Node> {
|
||||
let common = self.common(&r.common, scope, root)?;
|
||||
let collection = self.collection(&r.collection, scope)?;
|
||||
let template = std::boxed::Box::new(self.node(&r.template, scope, depth + 1, false)?);
|
||||
Ok(Node::Repeat(RepeatNode { common, collection, key_member: r.key_member.clone(), template }))
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn expand_overlay(&mut self, o: &OverlayNode, scope: Option<&Scope>, depth: usize, root: bool) -> Res<Node> {
|
||||
let common = self.common(&o.common, scope, root)?;
|
||||
let anchor = match &o.anchor {
|
||||
Anchor::Viewport => Anchor::Viewport,
|
||||
Anchor::Node(anchor) => Anchor::Node(AnchorNode { node: self.id(&anchor.node, scope) }),
|
||||
};
|
||||
let dismiss = self.command(&o.dismiss, scope)?;
|
||||
let children = self.children(&o.children, scope, depth + 1)?;
|
||||
Ok(Node::Overlay(OverlayNode { common, anchor, modal: o.modal, dismiss, children }))
|
||||
}
|
||||
|
||||
/// The nodes with no child list. Apart from `node` so that its frame, which every level of the
|
||||
/// recursion pays for, holds only what a container needs.
|
||||
#[inline(never)]
|
||||
fn leaf(&mut self, node: &Node, scope: Option<&Scope>, root: bool) -> Res<Node> {
|
||||
Ok(match node {
|
||||
Node::Text(t) => {
|
||||
let common = self.common(&t.common, scope, root)?;
|
||||
let content = self.text(&t.content, scope)?;
|
||||
|
|
@ -388,6 +342,13 @@ impl<'a> Expander<'a> {
|
|||
let alt = self.text(&i.alt, scope)?;
|
||||
Node::Image(ImageNode { common, alt, ..i.clone() })
|
||||
}
|
||||
Node::Button(b) => {
|
||||
let common = self.common(&b.common, scope, root)?;
|
||||
let on_activate = self.command(&b.on_activate, scope)?;
|
||||
let enabled = self.boolean(&b.enabled, scope)?;
|
||||
let content = self.children(&b.content, scope, depth + 1)?;
|
||||
Node::Button(ButtonNode { common, content, on_activate, enabled })
|
||||
}
|
||||
Node::Toggle(t) => {
|
||||
let common = self.common(&t.common, scope, root)?;
|
||||
let checked = self.boolean(&t.checked, scope)?;
|
||||
|
|
@ -421,6 +382,22 @@ impl<'a> Expander<'a> {
|
|||
let on_change = self.command(&s.on_change, scope)?;
|
||||
Node::Select(SelectNode { common, options, value, on_change })
|
||||
}
|
||||
Node::Repeat(r) => {
|
||||
let common = self.common(&r.common, scope, root)?;
|
||||
let collection = self.collection(&r.collection, scope)?;
|
||||
let template = std::boxed::Box::new(self.node(&r.template, scope, depth + 1, false)?);
|
||||
Node::Repeat(RepeatNode { common, collection, key_member: r.key_member.clone(), template })
|
||||
}
|
||||
Node::Overlay(o) => {
|
||||
let common = self.common(&o.common, scope, root)?;
|
||||
let anchor = match &o.anchor {
|
||||
Anchor::Viewport => Anchor::Viewport,
|
||||
Anchor::Node(anchor) => Anchor::Node(AnchorNode { node: self.id(&anchor.node, scope) }),
|
||||
};
|
||||
let dismiss = self.command(&o.dismiss, scope)?;
|
||||
let children = self.children(&o.children, scope, depth + 1)?;
|
||||
Node::Overlay(OverlayNode { common, anchor, modal: o.modal, dismiss, children })
|
||||
}
|
||||
Node::Plot(p) => {
|
||||
let common = self.common(&p.common, scope, root)?;
|
||||
Node::Plot(PlotNode { common, series: self.bind(&p.series), ..p.clone() })
|
||||
|
|
@ -429,9 +406,6 @@ impl<'a> Expander<'a> {
|
|||
let common = self.common(&e.common, scope, root)?;
|
||||
Node::Embed(EmbedNode { common, ..e.clone() })
|
||||
}
|
||||
Node::Slot(_) | Node::Use(_) | Node::Box(_) | Node::Button(_) | Node::Repeat(_) | Node::Overlay(_) => {
|
||||
unreachable!("node() handles containers, Use and Slot")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -937,7 +937,13 @@ fn an_unplaced_slot_argument_does_not_count_toward_the_depth_rule() {
|
|||
#[test]
|
||||
fn input_too_deep_to_recurse_into_is_refused_with_its_own_code() {
|
||||
big(|| {
|
||||
let resolution = run(&surface(chain(stack_test_depth(), false), &[]), &MemState::new());
|
||||
// A debug-build level costs tens of KiB of stack, so 1,100 levels want a stack of their own.
|
||||
let resolution = std::thread::Builder::new()
|
||||
.stack_size(128 * 1024 * 1024)
|
||||
.spawn(|| run(&surface(chain(1100, false), &[]), &MemState::new()))
|
||||
.unwrap()
|
||||
.join()
|
||||
.unwrap();
|
||||
assert!(resolution.tree.is_none());
|
||||
assert_eq!(resolution.diagnostics.len(), 1, "{:?}", resolution.diagnostics);
|
||||
assert_eq!(resolution.diagnostics[0].code, Code::TooDeepToResolve);
|
||||
|
|
@ -954,33 +960,4 @@ fn a_slot_naming_a_param_of_another_kind_is_invalid() {
|
|||
});
|
||||
}
|
||||
|
||||
fn stack_test_depth() -> usize { std::env::var("DEPTH").unwrap().parse().unwrap() }
|
||||
|
||||
#[test]
|
||||
fn probe_build_only() {
|
||||
big(|| {
|
||||
let node = chain(stack_test_depth(), false);
|
||||
let document = surface(node, &[]);
|
||||
eprintln!("built");
|
||||
let copy = document.clone();
|
||||
eprintln!("cloned");
|
||||
drop(copy);
|
||||
drop(document);
|
||||
eprintln!("dropped");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probe_stack() {
|
||||
let mb: usize = std::env::var("MB").unwrap().parse().unwrap();
|
||||
std::thread::Builder::new().stack_size(mb * 1024 * 1024).spawn(|| {
|
||||
let document = surface(chain(200, false), &[]);
|
||||
let resolution = run(&document, &MemState::new());
|
||||
eprintln!("ok {}", resolution.tree.is_some());
|
||||
}).unwrap().join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probe_sizes() {
|
||||
eprintln!("SIZE node={} common={} boxnode={} usenode={}", std::mem::size_of::<Node>(), std::mem::size_of::<NodeCommon>(), std::mem::size_of::<BoxNode>(), std::mem::size_of::<UseNode>());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue