difftreelog
refactor! implement stack size limit using thread local
in: master
18 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -85,8 +85,8 @@
/// Set the maximum stack depth.
#[no_mangle]
-pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {
- vm.settings_mut().max_stack = v as usize;
+pub extern "C" fn jsonnet_max_stack(_vm: &State, _v: c_uint) {
+ todo!()
}
/// Set the number of objects required before a garbage collection cycle is allowed.
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{AppSettings, IntoApp, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, ManifestOpts, OutputOpts};9use jrsonnet_evaluator::{error::LocError, State};1011#[cfg(feature = "mimalloc")]12#[global_allocator]13static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1415#[derive(Parser)]16enum SubOpts {17 /// Generate completions for specified shell18 Generate {19 /// Target shell name20 shell: Shell,21 },22}2324#[derive(Parser)]25#[clap(next_help_heading = "DEBUG")]26struct DebugOpts {27 /// Required OS stack size.28 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.29 #[clap(long, name = "size")]30 pub os_stack: Option<usize>,31}3233#[derive(Parser)]34#[clap(next_help_heading = "INPUT")]35struct InputOpts {36 /// Treat input as code, evaluate them instead of reading file37 #[clap(long, short = 'e')]38 pub exec: bool,3940 /// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself41 pub input: Option<String>,42}4344#[derive(Parser)]45#[clap(46 global_setting = AppSettings::DeriveDisplayOrder,47 args_conflicts_with_subcommands = true,48)]49struct Opts {50 #[clap(subcommand)]51 sub: Option<SubOpts>,5253 #[clap(flatten)]54 input: InputOpts,55 #[clap(flatten)]56 general: GeneralOpts,57 #[clap(flatten)]58 manifest: ManifestOpts,59 #[clap(flatten)]60 output: OutputOpts,61 #[clap(flatten)]62 debug: DebugOpts,63 #[clap(flatten)]64 gc: GcOpts,65}6667fn main() {68 let opts: Opts = Opts::parse();6970 if let Some(sub) = opts.sub {71 match sub {72 SubOpts::Generate { shell } => {73 use clap_complete::generate;74 let app = &mut Opts::command();75 let buf = &mut std::io::stdout();76 generate(shell, app, "jrsonnet", buf);77 std::process::exit(0)78 }79 }80 }8182 let success = if let Some(size) = opts.debug.os_stack {83 std::thread::Builder::new()84 .stack_size(size * 1024 * 1024)85 .spawn(|| main_catch(opts))86 .expect("new thread spawned")87 .join()88 .expect("thread finished successfully")89 } else {90 main_catch(opts)91 };92 if !success {93 std::process::exit(1);94 }95}9697#[derive(thiserror::Error, Debug)]98enum Error {99 // Handled differently100 #[error("evaluation error")]101 Evaluation(jrsonnet_evaluator::error::LocError),102 #[error("io error")]103 Io(#[from] std::io::Error),104 #[error("input is not utf8 encoded")]105 Utf8(#[from] std::str::Utf8Error),106 #[error("missing input argument")]107 MissingInputArgument,108}109impl From<LocError> for Error {110 fn from(e: LocError) -> Self {111 Self::Evaluation(e)112 }113}114115fn main_catch(opts: Opts) -> bool {116 let _printer = opts.gc.stats_printer();117 let s = State::default();118 if let Err(e) = main_real(&s, opts) {119 if let Error::Evaluation(e) = e {120 eprintln!("{}", s.stringify_err(&e));121 } else {122 eprintln!("{}", e);123 }124 return false;125 }126 true127}128129fn main_real(s: &State, opts: Opts) -> Result<(), Error> {130 opts.general.configure(s)?;131 opts.manifest.configure(s)?;132133 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;134 let val = if opts.input.exec {135 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?136 } else if input == "-" {137 let mut input = Vec::new();138 std::io::stdin().read_to_end(&mut input)?;139 let input_str = std::str::from_utf8(&input)?;140 s.evaluate_snippet("<stdin>".to_owned(), input_str)?141 } else {142 s.import(&input)?143 };144145 let val = s.with_tla(val)?;146147 if let Some(multi) = opts.output.multi {148 if opts.output.create_output_dirs {149 let mut dir = multi.clone();150 dir.pop();151 create_dir_all(dir)?;152 }153 for (file, data) in s.manifest_multi(val)?.iter() {154 let mut path = multi.clone();155 path.push(file as &str);156 if opts.output.create_output_dirs {157 let mut dir = path.clone();158 dir.pop();159 create_dir_all(dir)?;160 }161 println!("{}", path.to_str().expect("path"));162 let mut file = File::create(path)?;163 writeln!(file, "{}", data)?;164 }165 } else if let Some(path) = opts.output.output_file {166 if opts.output.create_output_dirs {167 let mut dir = path.clone();168 dir.pop();169 create_dir_all(dir)?;170 }171 let mut file = File::create(path)?;172 writeln!(file, "{}", s.manifest(val)?)?;173 } else {174 let output = s.manifest(val)?;175 if !output.is_empty() {176 println!("{}", output);177 }178 }179180 Ok(())181}1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{AppSettings, IntoApp, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{ConfigureState, GeneralOpts, ManifestOpts, OutputOpts};9use jrsonnet_evaluator::{error::LocError, State};1011#[cfg(feature = "mimalloc")]12#[global_allocator]13static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1415#[derive(Parser)]16enum SubOpts {17 /// Generate completions for specified shell18 Generate {19 /// Target shell name20 shell: Shell,21 },22}2324#[derive(Parser)]25#[clap(next_help_heading = "DEBUG")]26struct DebugOpts {27 /// Required OS stack size.28 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.29 #[clap(long, name = "size")]30 pub os_stack: Option<usize>,31}3233#[derive(Parser)]34#[clap(next_help_heading = "INPUT")]35struct InputOpts {36 /// Treat input as code, evaluate them instead of reading file37 #[clap(long, short = 'e')]38 pub exec: bool,3940 /// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself41 pub input: Option<String>,42}4344#[derive(Parser)]45#[clap(46 global_setting = AppSettings::DeriveDisplayOrder,47 args_conflicts_with_subcommands = true,48)]49struct Opts {50 #[clap(subcommand)]51 sub: Option<SubOpts>,5253 #[clap(flatten)]54 input: InputOpts,55 #[clap(flatten)]56 general: GeneralOpts,57 #[clap(flatten)]58 manifest: ManifestOpts,59 #[clap(flatten)]60 output: OutputOpts,61 #[clap(flatten)]62 debug: DebugOpts,63}6465fn main() {66 let opts: Opts = Opts::parse();6768 if let Some(sub) = opts.sub {69 match sub {70 SubOpts::Generate { shell } => {71 use clap_complete::generate;72 let app = &mut Opts::command();73 let buf = &mut std::io::stdout();74 generate(shell, app, "jrsonnet", buf);75 std::process::exit(0)76 }77 }78 }7980 let success = if let Some(size) = opts.debug.os_stack {81 std::thread::Builder::new()82 .stack_size(size * 1024 * 1024)83 .spawn(|| main_catch(opts))84 .expect("new thread spawned")85 .join()86 .expect("thread finished successfully")87 } else {88 main_catch(opts)89 };90 if !success {91 std::process::exit(1);92 }93}9495#[derive(thiserror::Error, Debug)]96enum Error {97 // Handled differently98 #[error("evaluation error")]99 Evaluation(jrsonnet_evaluator::error::LocError),100 #[error("io error")]101 Io(#[from] std::io::Error),102 #[error("input is not utf8 encoded")]103 Utf8(#[from] std::str::Utf8Error),104 #[error("missing input argument")]105 MissingInputArgument,106}107impl From<LocError> for Error {108 fn from(e: LocError) -> Self {109 Self::Evaluation(e)110 }111}112113fn main_catch(opts: Opts) -> bool {114 let s = State::default();115 if let Err(e) = main_real(&s, opts) {116 if let Error::Evaluation(e) = e {117 eprintln!("{}", s.stringify_err(&e));118 } else {119 eprintln!("{}", e);120 }121 return false;122 }123 true124}125126fn main_real(s: &State, opts: Opts) -> Result<(), Error> {127 let _guards = opts.general.configure(s)?;128 opts.manifest.configure(s)?;129130 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;131 let val = if opts.input.exec {132 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?133 } else if input == "-" {134 let mut input = Vec::new();135 std::io::stdin().read_to_end(&mut input)?;136 let input_str = std::str::from_utf8(&input)?;137 s.evaluate_snippet("<stdin>".to_owned(), input_str)?138 } else {139 s.import(&input)?140 };141142 let val = s.with_tla(val)?;143144 if let Some(multi) = opts.output.multi {145 if opts.output.create_output_dirs {146 let mut dir = multi.clone();147 dir.pop();148 create_dir_all(dir)?;149 }150 for (file, data) in s.manifest_multi(val)?.iter() {151 let mut path = multi.clone();152 path.push(file as &str);153 if opts.output.create_output_dirs {154 let mut dir = path.clone();155 dir.pop();156 create_dir_all(dir)?;157 }158 println!("{}", path.to_str().expect("path"));159 let mut file = File::create(path)?;160 writeln!(file, "{}", data)?;161 }162 } else if let Some(path) = opts.output.output_file {163 if opts.output.create_output_dirs {164 let mut dir = path.clone();165 dir.pop();166 create_dir_all(dir)?;167 }168 let mut file = File::create(path)?;169 writeln!(file, "{}", s.manifest(val)?)?;170 } else {171 let output = s.manifest(val)?;172 if !output.is_empty() {173 println!("{}", output);174 }175 }176177 Ok(())178}crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,10 +3,12 @@
mod tla;
mod trace;
-use std::{env, path::PathBuf};
+use std::{env, marker::PhantomData, path::PathBuf};
use clap::Parser;
-use jrsonnet_evaluator::{error::Result, FileImportResolver, State};
+use jrsonnet_evaluator::{
+ error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
+};
use jrsonnet_gcmodule::with_thread_object_space;
pub use manifest::*;
pub use stdlib::*;
@@ -14,7 +16,9 @@
pub use trace::*;
pub trait ConfigureState {
- fn configure(&self, s: &State) -> Result<()>;
+ type Guards;
+
+ fn configure(&self, s: &State) -> Result<Self::Guards>;
}
#[derive(Parser)]
@@ -44,7 +48,8 @@
jpath: Vec<PathBuf>,
}
impl ConfigureState for MiscOpts {
- fn configure(&self, s: &State) -> Result<()> {
+ type Guards = StackDepthLimitOverrideGuard;
+ fn configure(&self, s: &State) -> Result<Self::Guards> {
let mut library_paths = self.jpath.clone();
library_paths.reverse();
if let Some(path) = env::var_os("JSONNET_PATH") {
@@ -53,8 +58,8 @@
s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
- s.set_max_stack(self.max_stack);
- Ok(())
+ let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);
+ Ok(_depth_limit)
}
}
@@ -72,16 +77,24 @@
#[clap(flatten)]
trace: TraceOpts,
+
+ #[clap(flatten)]
+ gc: GcOpts,
}
impl ConfigureState for GeneralOpts {
- fn configure(&self, s: &State) -> Result<()> {
+ type Guards = (
+ <MiscOpts as ConfigureState>::Guards,
+ <GcOpts as ConfigureState>::Guards,
+ );
+ fn configure(&self, s: &State) -> Result<Self::Guards> {
// Configure trace first, because tla-code/ext-code can throw
self.trace.configure(s)?;
- self.misc.configure(s)?;
+ let misc_guards = self.misc.configure(s)?;
self.tla.configure(s)?;
self.std.configure(s)?;
- Ok(())
+ let gc_guards = self.gc.configure(s)?;
+ Ok((misc_guards, gc_guards))
}
}
@@ -100,20 +113,22 @@
#[clap(long)]
gc_collect_before_printing_stats: bool,
}
-impl GcOpts {
- pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {
+impl ConfigureState for GcOpts {
+ type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);
+
+ fn configure(&self, _s: &State) -> Result<Self::Guards> {
// Constructed structs have side-effects in Drop impl
#[allow(clippy::unnecessary_lazy_evaluations)]
- (
+ Ok((
self.gc_print_stats.then(|| GcStatsPrinter {
collect_before_printing_stats: self.gc_collect_before_printing_stats,
}),
- (!self.gc_collect_on_exit).then(|| LeakSpace {}),
- )
+ (!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),
+ ))
}
}
-pub struct LeakSpace {}
+pub struct LeakSpace(PhantomData<()>);
impl Drop for LeakSpace {
fn drop(&mut self) {
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -47,6 +47,7 @@
exp_preserve_order: bool,
}
impl ConfigureState for ManifestOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
if self.string {
s.set_manifest_format(ManifestFormat::String);
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -106,6 +106,7 @@
ext_code_file: Vec<ExtFile>,
}
impl ConfigureState for StdOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
if self.no_stdlib {
return Ok(());
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -47,6 +47,7 @@
tla_code_file: Vec<ExtFile>,
}
impl ConfigureState for TLAOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
for tla in self.tla_str.iter() {
s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -41,6 +41,7 @@
max_trace: usize,
}
impl ConfigureState for TraceOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
let resolver = PathResolver::new_cwd_fallback();
match self
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -20,8 +20,8 @@
exp-preserve-order = []
# Implements field destructuring
exp-destruct = ["jrsonnet-parser/exp-destruct"]
-# Provide Typed for conversions to/from serde_json::Value type
-serde_json = ["dep:serde_json"]
+# Improves performance, and implements some useful things using nightly-only features
+nightly = []
[dependencies]
jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,11 +2,11 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
-use crate::{stdlib::format::FormatError, typed::TypeLocError};
+use crate::{function::CallLocation, stdlib::format::FormatError, typed::TypeLocError};
fn format_found(list: &[IStr], what: &str) -> String {
if list.is_empty() {
@@ -262,7 +262,72 @@
}
}
+pub trait ErrorSource {
+ fn to_location(self) -> Option<ExprLocation>;
+}
+impl ErrorSource for &LocExpr {
+ fn to_location(self) -> Option<ExprLocation> {
+ Some(self.1.clone())
+ }
+}
+impl ErrorSource for &ExprLocation {
+ fn to_location(self) -> Option<ExprLocation> {
+ Some(self.clone())
+ }
+}
+impl ErrorSource for CallLocation<'_> {
+ fn to_location(self) -> Option<ExprLocation> {
+ self.0.cloned()
+ }
+}
+
pub type Result<V, E = LocError> = std::result::Result<V, E>;
+pub trait ResultExt: Sized {
+ #[must_use]
+ fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;
+ #[must_use]
+ fn description(self, msg: &str) -> Self {
+ self.with_description(|| msg)
+ }
+
+ #[must_use]
+ fn with_description_src<O: Into<String>>(
+ self,
+ src: impl ErrorSource,
+ msg: impl FnOnce() -> O,
+ ) -> Self;
+ #[must_use]
+ fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {
+ self.with_description_src(src, || msg)
+ }
+}
+impl<T> ResultExt for Result<T, LocError> {
+ fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {
+ if let Err(e) = &mut self {
+ let trace = e.trace_mut();
+ trace.0.push(StackTraceElement {
+ location: None,
+ desc: msg().into(),
+ });
+ }
+ self
+ }
+
+ fn with_description_src<O: Into<String>>(
+ mut self,
+ src: impl ErrorSource,
+ msg: impl FnOnce() -> O,
+ ) -> Self {
+ if let Err(e) = &mut self {
+ let trace = e.trace_mut();
+ trace.0.push(StackTraceElement {
+ location: src.to_location(),
+ desc: msg().into(),
+ });
+ }
+ self
+ }
+}
#[macro_export]
macro_rules! throw {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -34,7 +34,7 @@
pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
- FieldName::Dyn(expr) => s.push(
+ FieldName::Dyn(expr) => State::push(
CallLocation::new(&expr.1),
|| "evaluating field name".to_string(),
|| {
@@ -184,14 +184,11 @@
.with_add(*plus)
.with_visibility(*visibility)
.with_location(value.1.clone())
- .bindable(
- s.clone(),
- tb!(UnboundValue {
- uctx: uctx.clone(),
- value: value.clone(),
- name: name.clone()
- }),
- )?;
+ .bindable(tb!(UnboundValue {
+ uctx: uctx.clone(),
+ value: value.clone(),
+ name: name.clone()
+ }))?;
}
Member::Field(FieldMember {
name,
@@ -233,15 +230,12 @@
.member(name.clone())
.hide()
.with_location(value.1.clone())
- .bindable(
- s.clone(),
- tb!(UnboundMethod {
- uctx: uctx.clone(),
- value: value.clone(),
- params: params.clone(),
- name: name.clone()
- }),
- )?;
+ .bindable(tb!(UnboundMethod {
+ uctx: uctx.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name: name.clone()
+ }))?;
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
@@ -324,13 +318,10 @@
.member(n)
.with_location(obj.value.1.clone())
.with_add(obj.plus)
- .bindable(
- s.clone(),
- tb!(UnboundValue {
- uctx,
- value: obj.value.clone(),
- }),
- )?;
+ .bindable(tb!(UnboundValue {
+ uctx,
+ value: obj.value.clone(),
+ }))?;
}
v => throw!(FieldMustBeStringGot(v.value_type())),
}
@@ -364,7 +355,7 @@
if tailstrict {
body()?
} else {
- s.push(loc, || format!("function <{}> call", f.name()), body)?
+ State::push(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -374,13 +365,13 @@
pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = s.push(
+ let assertion_result = State::push(
CallLocation::new(&value.1),
|| "assertion condition".to_owned(),
|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
)?;
if !assertion_result {
- s.push(
+ State::push(
CallLocation::new(&value.1),
|| "assertion failure".to_owned(),
|| {
@@ -432,7 +423,7 @@
Num(v) => Val::new_checked_num(*v)?,
BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,
- Var(name) => s.push(
+ Var(name) => State::push(
CallLocation::new(loc),
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(s.clone()),
@@ -442,7 +433,7 @@
evaluate(s.clone(), ctx.clone(), value)?,
evaluate(s.clone(), ctx, index)?,
) {
- (Val::Obj(v), Val::Str(key)) => s.push(
+ (Val::Obj(v), Val::Str(key)) => State::push(
CallLocation::new(loc),
|| format!("field <{key}> access"),
|| match v.get(s.clone(), key.clone()) {
@@ -571,7 +562,7 @@
evaluate_assert(s.clone(), ctx.clone(), assert)?;
evaluate(s, ctx, returned)?
}
- ErrorStmt(e) => s.push(
+ ErrorStmt(e) => State::push(
CallLocation::new(loc),
|| "error statement".to_owned(),
|| {
@@ -585,7 +576,7 @@
cond_then,
cond_else,
} => {
- if s.push(
+ if State::push(
CallLocation::new(loc),
|| "if condition".to_owned(),
|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),
@@ -607,7 +598,7 @@
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
- Ok(Some(s.push(
+ Ok(Some(State::push(
loc,
|| format!("slice {desc}"),
|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
@@ -630,7 +621,7 @@
let tmp = loc.clone().0;
let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
match i {
- Import(_) => s.push(
+ Import(_) => State::push(
CallLocation::new(loc),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,5 +1,5 @@
//! jsonnet interpreter implementation
-
+#![cfg_attr(feature = "nightly", feature(thread_local))]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(
clippy::all,
@@ -51,6 +51,7 @@
mod integrations;
mod map;
mod obj;
+pub mod stack;
pub mod stdlib;
pub mod trace;
pub mod typed;
@@ -67,7 +68,7 @@
pub use ctx::*;
pub use dynamic::*;
-use error::{Error::*, LocError, Result, StackTraceElement};
+use error::{Error::*, LocError, Result, ResultExt};
pub use evaluate::*;
use function::{CallLocation, TlaArg};
use gc::{GcHashMap, TraceBox};
@@ -78,6 +79,7 @@
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
+use stack::check_depth;
use trace::{CompactFormat, TraceFormat};
pub use val::{ManifestFormat, Thunk, Val};
@@ -143,8 +145,6 @@
/// Dynamically reconfigurable evaluation settings
pub struct EvaluationSettings {
- /// Limits recursion by limiting the number of stack frames
- pub max_stack: usize,
/// Limits amount of stack trace items preserved
pub max_trace: usize,
/// TLA vars
@@ -162,7 +162,6 @@
impl Default for EvaluationSettings {
fn default() -> Self {
Self {
- max_stack: 200,
max_trace: 20,
context_initializer: Box::new(DummyContextInitializer),
tla_vars: HashMap::default(),
@@ -179,19 +178,7 @@
}
}
}
-
-#[derive(Default)]
-struct EvaluationData {
- /// Used for stack overflow detection, stacktrace is populated on unwind
- stack_depth: usize,
- /// Updated every time stack entry is popt
- stack_generation: usize,
- breakpoints: Breakpoints,
-
- /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
- files: GcHashMap<SourcePath, FileData>,
-}
struct FileData {
string: Option<IStr>,
bytes: Option<IBytes>,
@@ -217,46 +204,14 @@
parsed: None,
evaluated: None,
evaluating: false,
- }
- }
-}
-
-#[allow(clippy::type_complexity)]
-pub struct Breakpoint {
- loc: ExprLocation,
- collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
-}
-#[derive(Default)]
-struct Breakpoints(Vec<Rc<Breakpoint>>);
-impl Breakpoints {
- fn insert(
- &self,
- stack_depth: usize,
- stack_generation: usize,
- loc: &ExprLocation,
- result: Result<Val>,
- ) -> Result<Val> {
- if self.0.is_empty() {
- return result;
- }
- for item in &self.0 {
- if item.loc.belongs_to(loc) {
- let mut collected = item.collected.borrow_mut();
- let (depth, vals) = collected.entry(stack_generation).or_default();
- if stack_depth > *depth {
- vals.clear();
- }
- vals.push(result.clone());
- }
}
- result
}
}
#[derive(Default)]
pub struct EvaluationStateInternals {
/// Internal state
- data: RefCell<EvaluationData>,
+ file_cache: RefCell<GcHashMap<SourcePath, FileData>>,
/// Settings, safe to change at runtime
settings: RefCell<EvaluationSettings>,
}
@@ -268,8 +223,8 @@
impl State {
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -303,8 +258,8 @@
}
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -330,8 +285,8 @@
}
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -383,16 +338,16 @@
throw!(InfiniteRecursionDetected)
}
file.evaluating = true;
- // Dropping file here, as it borrows data, which may be used in evaluation
- drop(data);
+ // Dropping file cache guard here, as evaluation may use this map too
+ drop(file_cache);
let res = evaluate(
self.clone(),
self.create_default_context(file_name),
&parsed,
);
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -426,35 +381,13 @@
/// Executes code creating a new stack frame
pub fn push<T>(
- &self,
e: CallLocation<'_>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: e.0.cloned(),
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description_src(e, frame_desc)
}
/// Executes code creating a new stack frame
@@ -464,64 +397,18 @@
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<Val>,
) -> Result<Val> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let mut result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- result = data
- .breakpoints
- .insert(data.stack_depth, data.stack_generation, e, result);
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: Some(e.clone()),
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description_src(e, frame_desc)
}
/// Executes code creating a new stack frame
pub fn push_description<T>(
- &self,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: None,
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description(frame_desc)
}
/// # Panics
@@ -536,7 +423,7 @@
}
pub fn manifest(&self, val: Val) -> Result<IStr> {
- self.push_description(
+ Self::push_description(
|| "manifestification".to_string(),
|| val.manifest(self.clone(), &self.manifest_format()),
)
@@ -551,7 +438,7 @@
/// If passed value is function then call with set TLA
pub fn with_tla(&self, val: Val) -> Result<Val> {
Ok(match val {
- Val::Func(func) => self.push_description(
+ Val::Func(func) => State::push_description(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
@@ -573,8 +460,8 @@
/// Internals
impl State {
- fn data_mut(&self) -> RefMut<'_, EvaluationData> {
- self.0.data.borrow_mut()
+ fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {
+ self.0.file_cache.borrow_mut()
}
pub fn settings(&self) -> Ref<'_, EvaluationSettings> {
self.0.settings.borrow()
@@ -675,12 +562,5 @@
}
pub fn set_max_trace(&self, trace: usize) {
self.settings_mut().max_trace = trace;
- }
-
- pub fn max_stack(&self) -> usize {
- self.settings().max_stack
- }
- pub fn set_max_stack(&self, trace: usize) {
- self.settings_mut().max_stack = trace;
}
}
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -577,22 +577,29 @@
pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
impl ObjMemberBuilder<ValueBuilder<'_>> {
- pub fn value(self, s: State, value: Val) -> Result<()> {
- self.binding(s, MaybeUnbound::Bound(Thunk::evaluated(value)))
+ /// Inserts value, replacing if it is already defined
+ pub fn value_unchecked(self, value: Val) {
+ let (receiver, name, member) =
+ self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));
+ let entry = receiver.0.map.entry(name);
+ entry.insert(member);
}
- pub fn bindable(
- self,
- s: State,
- bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>,
- ) -> Result<()> {
- self.binding(s, MaybeUnbound::Unbound(Cc::new(bindable)))
+
+ pub fn value(self, value: Val) -> Result<()> {
+ self.thunk(Thunk::evaluated(value))
+ }
+ pub fn thunk(self, value: Thunk<Val>) -> Result<()> {
+ self.binding(MaybeUnbound::Bound(value))
+ }
+ pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) -> Result<()> {
+ self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))
}
- pub fn binding(self, s: State, binding: MaybeUnbound) -> Result<()> {
+ pub fn binding(self, binding: MaybeUnbound) -> Result<()> {
let (receiver, name, member) = self.build_member(binding);
let location = member.location.clone();
let old = receiver.0.map.insert(name.clone(), member);
if old.is_some() {
- s.push(
+ State::push(
CallLocation(location.as_ref()),
|| format!("field <{}> initializtion", name.clone()),
|| throw!(DuplicateFieldName(name.clone())),
crates/jrsonnet-evaluator/src/stack.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/stack.rs
@@ -0,0 +1,109 @@
+use std::{cell::Cell, marker::PhantomData};
+
+use crate::error::{Error, LocError};
+
+struct StackLimit {
+ max_stack_size: Cell<usize>,
+ current_depth: Cell<usize>,
+}
+
+#[cfg(feature = "nightly")]
+#[thread_local]
+static STACK_LIMIT: StackLimit = StackLimit {
+ max_stack_size: Cell::new(200),
+ current_depth: Cell::new(0),
+};
+#[cfg(not(feature = "nightly"))]
+thread_local! {
+ static STACK_LIMIT: StackLimit = StackLimit {
+ max_stack_size: Cell::new(200),
+ current_depth: Cell::new(0),
+ };
+}
+
+pub struct StackOverflowError;
+impl From<StackOverflowError> for Error {
+ fn from(_: StackOverflowError) -> Self {
+ Error::StackOverflow
+ }
+}
+impl From<StackOverflowError> for LocError {
+ fn from(_: StackOverflowError) -> Self {
+ Error::StackOverflow.into()
+ }
+}
+
+/// Used to implement stack depth limitation
+pub struct StackDepthGuard(PhantomData<()>);
+impl Drop for StackDepthGuard {
+ #[cfg(feature = "nightly")]
+ fn drop(&mut self) {
+ STACK_LIMIT
+ .current_depth
+ .set(STACK_LIMIT.current_depth.get() - 1)
+ }
+ #[cfg(not(feature = "nightly"))]
+ fn drop(&mut self) {
+ STACK_LIMIT.with(|limit| limit.current_depth.set(limit.current_depth.get() - 1));
+ }
+}
+
+// #[cfg(feature = "nightly")]
+pub fn check_depth() -> Result<StackDepthGuard, StackOverflowError> {
+ fn internal(limit: &StackLimit) -> Result<StackDepthGuard, StackOverflowError> {
+ let current = limit.current_depth.get();
+ if current < limit.max_stack_size.get() {
+ limit.current_depth.set(current + 1);
+ Ok(StackDepthGuard(PhantomData))
+ } else {
+ Err(StackOverflowError)
+ }
+ }
+ #[cfg(feature = "nightly")]
+ {
+ internal(&STACK_LIMIT)
+ }
+ #[cfg(not(feature = "nightly"))]
+ {
+ STACK_LIMIT.with(internal)
+ }
+}
+
+pub struct StackDepthLimitOverrideGuard {
+ old_limit: usize,
+}
+impl Drop for StackDepthLimitOverrideGuard {
+ #[cfg(feature = "nightly")]
+ fn drop(&mut self) {
+ STACK_LIMIT.max_stack_size.set(self.old_limit)
+ }
+ #[cfg(not(feature = "nightly"))]
+ fn drop(&mut self) {
+ STACK_LIMIT.with(|limit| limit.max_stack_size.set(self.old_limit));
+ }
+}
+
+pub fn limit_stack_depth(depth_limit: usize) -> StackDepthLimitOverrideGuard {
+ fn internal(limit: &StackLimit, depth_limit: usize) -> StackDepthLimitOverrideGuard {
+ let old_limit = limit.max_stack_size.get();
+ let current_depth = limit.current_depth.get();
+
+ limit.max_stack_size.set(current_depth + depth_limit);
+ StackDepthLimitOverrideGuard { old_limit }
+ }
+ #[cfg(feature = "nightly")]
+ {
+ internal(&STACK_LIMIT, depth_limit)
+ }
+ #[cfg(not(feature = "nightly"))]
+ {
+ STACK_LIMIT.with(|limit| internal(limit, depth_limit))
+ }
+}
+
+/// Like [`limit_stack_depth`], but set depth is not guarded, and will be kept
+///
+/// Used to implement `set_max_stack` in C api, prefer to use [`limit_stack_depth`] instead
+pub fn set_stack_depth_limit(depth_limit: usize) {
+ std::mem::forget(limit_stack_depth(depth_limit));
+}
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -111,7 +111,7 @@
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
buf.push_str(options.key_val_sep);
- s.push_description(
+ State::push_description(
|| format!("field <{}> manifestification", field.clone()),
|| {
let value = obj.get(s.clone(), field.clone())?.unwrap();
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -10,7 +10,7 @@
pub mod manifest;
pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
- s.push(
+ State::push(
CallLocation::native(),
|| format!("std.format of {str}"),
|| {
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -85,12 +85,11 @@
}
fn push_type_description(
- s: State,
error_reason: impl Fn() -> String,
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- s.push_description(error_reason, || match item() {
+ State::push_description(error_reason, || match item() {
Ok(_) => Ok(()),
Err(mut e) => {
if let Error::TypeError(e) = &mut e.error_mut() {
@@ -170,7 +169,6 @@
Val::Arr(a) => {
for (i, item) in a.iter(s.clone()).enumerate() {
push_type_description(
- s.clone(),
|| format!("array index {i}"),
|| ValuePathItem::Index(i as u64),
|| elem_type.check(s.clone(), &item.clone()?),
@@ -184,7 +182,6 @@
Val::Arr(a) => {
for (i, item) in a.iter(s.clone()).enumerate() {
push_type_description(
- s.clone(),
|| format!("array index {i}"),
|| ValuePathItem::Index(i as u64),
|| elem_type.check(s.clone(), &item.clone()?),
@@ -199,7 +196,6 @@
for (k, v) in elems.iter() {
if let Some(got_v) = obj.get(s.clone(), (*k).into())? {
push_type_description(
- s.clone(),
|| format!("property {k}"),
|| ValuePathItem::Field((*k).into()),
|| v.check(s.clone(), &got_v),
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -299,7 +299,7 @@
cfg_attrs,
} => {
let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");
- let eval = quote! {s.push_description(
+ let eval = quote! {jrsonnet_evaluator::State::push_description(
|| format!("argument <{}> evaluation", #name),
|| <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),
)?};
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -137,43 +137,36 @@
builder
.member(name.into())
.hide()
- .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))
+ .value(Val::Func(FuncVal::StaticBuiltin(builtin)))
.expect("no conflict");
}
builder
.member("extVar".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {
- settings: settings.clone()
- })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {
+ settings: settings.clone()
+ })))))
.expect("no conflict");
builder
.member("native".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {
- settings: settings.clone()
- })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {
+ settings: settings.clone()
+ })))))
.expect("no conflict");
builder
.member("trace".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace {
+ settings
+ })))))
.expect("no conflict");
builder
.member("id".into())
.hide()
- .value(s, Val::Func(FuncVal::Id))
+ .value(Val::Func(FuncVal::Id))
.expect("no conflict");
builder.build()