Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a recursion limit to background evaluation #1878

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions core/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,14 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
/// fields and array elements, we keep evaluating subsequent elements even if one
/// fails.
/// - We only return the accumulated errors; we don't return the eval'ed term.
pub fn eval_permissive(&mut self, rt: RichTerm) -> Vec<EvalError> {
/// - We support a recursion limit, to limit the number of times we recurse into
/// arrays or records.
pub fn eval_permissive(&mut self, rt: RichTerm, recursion_limit: u64) -> Vec<EvalError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: should we use usize instead of u64? I guess in practice u16 is already amply sufficient, but you might prefer to use a word-aligned value. Using u64 sounds like we need the guarantee to have larger values than u32::MAX.

fn inner<R: ImportResolver, C: Cache>(
slf: &mut VirtualMachine<R, C>,
acc: &mut Vec<EvalError>,
rt: RichTerm,
recursion_limit: u64,
) {
let pos = rt.pos;
match slf.eval(rt) {
Expand All @@ -966,7 +969,9 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
attrs.pending_contracts.iter().cloned(),
t.pos,
);
inner(slf, acc, value_with_ctr);
if let Some(r) = recursion_limit.checked_sub(1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: if we test at the very beginning of the function that recursion_limit > 0 (and return immediatly instead), we have to perform the check only once, instead of at each call site (using saturating sub instead of checked_sub)

inner(slf, acc, value_with_ctr, r)
}
}
}
Term::Record(data) => {
Expand All @@ -977,7 +982,9 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
field.pending_contracts.iter().cloned(),
v.pos,
);
inner(slf, acc, value_with_ctr);
if let Some(r) = recursion_limit.checked_sub(1) {
inner(slf, acc, value_with_ctr, r)
}
} else {
acc.push(EvalError::MissingFieldDef {
id: *id,
Expand All @@ -993,7 +1000,7 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
}
}
let mut ret = Vec::new();
inner(self, &mut ret, rt);
inner(self, &mut ret, rt, recursion_limit);
ret
}
}
Expand Down
3 changes: 2 additions & 1 deletion lsp/nls/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
};

const EVAL_TIMEOUT: Duration = Duration::from_secs(1);
const RECURSION_LIMIT: u64 = 128;

#[derive(Debug, Serialize, Deserialize)]
enum Command {
Expand Down Expand Up @@ -97,7 +98,7 @@ pub fn worker_main() -> anyhow::Result<()> {
// We've already checked that parsing and typechecking are successful, so we
// don't expect further errors.
let rt = vm.prepare_eval(file_id).unwrap();
let errors = vm.eval_permissive(rt);
let errors = vm.eval_permissive(rt, RECURSION_LIMIT);
diagnostics.extend(
errors
.into_iter()
Expand Down
7 changes: 7 additions & 0 deletions lsp/nls/tests/inputs/diagnostics-recursion.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### /diagnostics-recursion.ncl
let rec foo = { bar = foo } in
[
foo,
foo.bar.bar.bar.bar.bar.baz
]
### diagnostic = ["file:///diagnostics-recursion.ncl"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: lsp/nls/tests/main.rs
expression: output
---
(file:///diagnostics-recursion.ncl, 0:14-0:27: this record lacks the field `baz`)
(file:///diagnostics-recursion.ncl, 3:2-3:29: missing field `baz`
Did you mean `bar`?)
(file:///diagnostics-recursion.ncl, 3:2-3:29: this requires the field `baz` to exist)
Loading