git.delta.rocks / jrsonnet / refs/commits / 9c0fa0107dd8

difftreelog

feat detect infinite recursion in object evaluation

Yaroslav Bolyukin2022-04-20parent: #5a22275.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -106,7 +106,7 @@
 	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
 	StackOverflow,
 	#[error("infinite recursion detected")]
-	RecursiveLazyValueEvaluation,
+	InfiniteRecursionDetected,
 	#[error("tried to index by fractional value")]
 	FractionalIndex,
 	#[error("attempted to divide by zero")]
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj.rs
1use crate::function::CallLocation;2use crate::gc::{GcHashMap, GcHashSet, TraceBox};3use crate::operator::evaluate_add_op;4use crate::push_frame;5use crate::{6	cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,7	Result, Val,8};9use gcmodule::{Cc, Trace, Weak};10use jrsonnet_interner::IStr;11use jrsonnet_parser::{ExprLocation, Visibility};12use rustc_hash::FxHashMap;13use std::cell::RefCell;14use std::fmt::Debug;15use std::hash::{Hash, Hasher};1617#[derive(Debug, Trace)]18pub struct ObjMember {19	pub add: bool,20	pub visibility: Visibility,21	pub invoke: LazyBinding,22	pub location: Option<ExprLocation>,23}2425pub trait ObjectAssertion: Trace {26	fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;27}2829// Field => This30type CacheKey = (IStr, WeakObjValue);31#[derive(Trace)]32#[force_tracking]33pub struct ObjValueInternals {34	super_obj: Option<ObjValue>,35	assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,36	assertions_ran: RefCell<GcHashSet<ObjValue>>,37	this_obj: Option<ObjValue>,38	this_entries: Cc<GcHashMap<IStr, ObjMember>>,39	value_cache: RefCell<GcHashMap<CacheKey, Option<Val>>>,40}4142#[derive(Clone, Trace)]43pub struct WeakObjValue(#[skip_trace] pub(crate) Weak<ObjValueInternals>);4445impl PartialEq for WeakObjValue {46	fn eq(&self, other: &Self) -> bool {47		weak_ptr_eq(self.0.clone(), other.0.clone())48	}49}5051impl Eq for WeakObjValue {}52impl Hash for WeakObjValue {53	fn hash<H: Hasher>(&self, hasher: &mut H) {54		hasher.write_usize(weak_raw(self.0.clone()) as usize)55	}56}5758#[derive(Clone, Trace)]59pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);60impl Debug for ObjValue {61	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {62		if let Some(super_obj) = self.0.super_obj.as_ref() {63			if f.alternate() {64				write!(f, "{:#?}", super_obj)?;65			} else {66				write!(f, "{:?}", super_obj)?;67			}68			write!(f, " + ")?;69		}70		let mut debug = f.debug_struct("ObjValue");71		for (name, member) in self.0.this_entries.iter() {72			debug.field(name, member);73		}74		debug.finish_non_exhaustive()75	}76}7778impl ObjValue {79	pub fn new(80		super_obj: Option<Self>,81		this_entries: Cc<GcHashMap<IStr, ObjMember>>,82		assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,83	) -> Self {84		Self(Cc::new(ObjValueInternals {85			super_obj,86			assertions,87			assertions_ran: RefCell::new(GcHashSet::new()),88			this_obj: None,89			this_entries,90			value_cache: RefCell::new(GcHashMap::new()),91		}))92	}93	pub fn new_empty() -> Self {94		Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))95	}96	pub fn extend_from(&self, super_obj: Self) -> Self {97		match &self.0.super_obj {98			None => Self::new(99				Some(super_obj),100				self.0.this_entries.clone(),101				self.0.assertions.clone(),102			),103			Some(v) => Self::new(104				Some(v.extend_from(super_obj)),105				self.0.this_entries.clone(),106				self.0.assertions.clone(),107			),108		}109	}110	pub fn with_this(&self, this_obj: Self) -> Self {111		Self(Cc::new(ObjValueInternals {112			super_obj: self.0.super_obj.clone(),113			assertions: self.0.assertions.clone(),114			assertions_ran: RefCell::new(GcHashSet::new()),115			this_obj: Some(this_obj),116			this_entries: self.0.this_entries.clone(),117			value_cache: RefCell::new(GcHashMap::new()),118		}))119	}120121	pub fn is_empty(&self) -> bool {122		if !self.0.this_entries.is_empty() {123			return false;124		}125		self.0126			.super_obj127			.as_ref()128			.map(|s| s.is_empty())129			.unwrap_or(true)130	}131132	/// Run callback for every field found in object133	pub(crate) fn enum_fields(&self, handler: &mut impl FnMut(&IStr, &ObjMember) -> bool) -> bool {134		if let Some(s) = &self.0.super_obj {135			if s.enum_fields(handler) {136				return true;137			}138		}139		for (name, member) in self.0.this_entries.iter() {140			if handler(name, member) {141				return true;142			}143		}144		false145	}146147	pub fn fields_visibility(&self) -> FxHashMap<IStr, bool> {148		let mut out = FxHashMap::default();149		self.enum_fields(&mut |name, member| {150			match member.visibility {151				Visibility::Normal => {152					let entry = out.entry(name.to_owned());153					entry.or_insert(true);154				}155				Visibility::Hidden => {156					out.insert(name.to_owned(), false);157				}158				Visibility::Unhide => {159					out.insert(name.to_owned(), true);160				}161			};162			false163		});164		out165	}166	pub fn fields_ex(&self, include_hidden: bool) -> Vec<IStr> {167		let mut fields: Vec<_> = self168			.fields_visibility()169			.into_iter()170			.filter(|(_k, v)| include_hidden || *v)171			.map(|(k, _)| k)172			.collect();173		fields.sort_unstable();174		fields175	}176	pub fn fields(&self) -> Vec<IStr> {177		self.fields_ex(false)178	}179180	pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {181		if let Some(m) = self.0.this_entries.get(&name) {182			Some(match &m.visibility {183				Visibility::Normal => self184					.0185					.super_obj186					.as_ref()187					.and_then(|super_obj| super_obj.field_visibility(name))188					.unwrap_or(Visibility::Normal),189				v => *v,190			})191		} else if let Some(super_obj) = &self.0.super_obj {192			super_obj.field_visibility(name)193		} else {194			None195		}196	}197198	fn has_field_include_hidden(&self, name: IStr) -> bool {199		if self.0.this_entries.contains_key(&name) {200			true201		} else if let Some(super_obj) = &self.0.super_obj {202			super_obj.has_field_include_hidden(name)203		} else {204			false205		}206	}207208	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {209		if include_hidden {210			self.has_field_include_hidden(name)211		} else {212			self.has_field(name)213		}214	}215	pub fn has_field(&self, name: IStr) -> bool {216		self.field_visibility(name)217			.map(|v| v.is_visible())218			.unwrap_or(false)219	}220221	pub fn get(&self, key: IStr) -> Result<Option<Val>> {222		self.run_assertions()?;223		self.get_raw(key, self.0.this_obj.as_ref())224	}225226	pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {227		let mut new = GcHashMap::with_capacity(1);228		new.insert(key, value);229		Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))230	}231232	fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {233		let real_this = real_this.unwrap_or(self);234		let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));235236		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {237			return Ok(v.clone());238		}239		let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {240			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),241			(Some(k), Some(s)) => {242				let our = self.evaluate_this(k, real_this)?;243				if k.add {244					s.get_raw(key, Some(real_this))?245						.map_or(Ok(Some(our.clone())), |v| {246							Ok(Some(evaluate_add_op(&v, &our)?))247						})248				} else {249					Ok(Some(our))250				}251			}252			(None, Some(s)) => s.get_raw(key, Some(real_this)),253			(None, None) => Ok(None),254		}?;255		self.0256			.value_cache257			.borrow_mut()258			.insert(cache_key, value.clone());259		Ok(value)260	}261	fn evaluate_this(&self, v: &ObjMember, real_this: &Self) -> Result<Val> {262		v.invoke263			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?264			.evaluate()265	}266267	fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {268		if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {269			for assertion in self.0.assertions.iter() {270				if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {271					self.0.assertions_ran.borrow_mut().remove(real_this);272					return Err(e);273				}274			}275			if let Some(super_obj) = &self.0.super_obj {276				super_obj.run_assertions_raw(real_this)?;277			}278		}279		Ok(())280	}281	pub fn run_assertions(&self) -> Result<()> {282		self.run_assertions_raw(self)283	}284285	pub fn ptr_eq(a: &Self, b: &Self) -> bool {286		cc_ptr_eq(&a.0, &b.0)287	}288}289290impl PartialEq for ObjValue {291	fn eq(&self, other: &Self) -> bool {292		cc_ptr_eq(&self.0, &other.0)293	}294}295296impl Eq for ObjValue {}297impl Hash for ObjValue {298	fn hash<H: Hasher>(&self, hasher: &mut H) {299		hasher.write_usize(&*self.0 as *const _ as usize)300	}301}302303pub struct ObjValueBuilder {304	super_obj: Option<ObjValue>,305	map: GcHashMap<IStr, ObjMember>,306	assertions: Vec<TraceBox<dyn ObjectAssertion>>,307}308impl ObjValueBuilder {309	pub fn new() -> Self {310		Self::with_capacity(0)311	}312	pub fn with_capacity(capacity: usize) -> Self {313		Self {314			super_obj: None,315			map: GcHashMap::with_capacity(capacity),316			assertions: Vec::new(),317		}318	}319	pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {320		self.assertions.reserve_exact(capacity);321		self322	}323	pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {324		self.super_obj = Some(super_obj);325		self326	}327328	pub fn assert(&mut self, assertion: TraceBox<dyn ObjectAssertion>) -> &mut Self {329		self.assertions.push(assertion);330		self331	}332	pub fn member(&mut self, name: IStr) -> ObjMemberBuilder {333		ObjMemberBuilder {334			value: self,335			name,336			add: false,337			visibility: Visibility::Normal,338			location: None,339		}340	}341342	pub fn build(self) -> ObjValue {343		ObjValue::new(self.super_obj, Cc::new(self.map), Cc::new(self.assertions))344	}345}346impl Default for ObjValueBuilder {347	fn default() -> Self {348		Self::with_capacity(0)349	}350}351352#[must_use = "value not added unless binding() was called"]353pub struct ObjMemberBuilder<'v> {354	value: &'v mut ObjValueBuilder,355	name: IStr,356	add: bool,357	visibility: Visibility,358	location: Option<ExprLocation>,359}360361#[allow(clippy::missing_const_for_fn)]362impl<'v> ObjMemberBuilder<'v> {363	pub const fn with_add(mut self, add: bool) -> Self {364		self.add = add;365		self366	}367	pub fn add(self) -> Self {368		self.with_add(true)369	}370	pub fn with_visibility(mut self, visibility: Visibility) -> Self {371		self.visibility = visibility;372		self373	}374	pub fn hide(self) -> Self {375		self.with_visibility(Visibility::Hidden)376	}377	pub fn with_location(mut self, location: ExprLocation) -> Self {378		self.location = Some(location);379		self380	}381	pub fn value(self, value: Val) -> Result<()> {382		self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))383	}384	pub fn bindable(self, bindable: TraceBox<dyn Bindable>) -> Result<()> {385		self.binding(LazyBinding::Bindable(Cc::new(bindable)))386	}387	pub fn binding(self, binding: LazyBinding) -> Result<()> {388		let old = self.value.map.insert(389			self.name.clone(),390			ObjMember {391				add: self.add,392				visibility: self.visibility,393				invoke: binding,394				location: self.location.clone(),395			},396		);397		if old.is_some() {398			push_frame(399				CallLocation(self.location.as_ref()),400				|| format!("field <{}> initializtion", self.name.clone()),401				|| throw!(DuplicateFieldName(self.name.clone())),402			)?403		}404		Ok(())405	}406}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -47,7 +47,7 @@
 		match &*self.0.borrow() {
 			LazyValInternals::Computed(v) => return Ok(v.clone()),
 			LazyValInternals::Errored(e) => return Err(e.clone()),
-			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),
+			LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),
 			_ => (),
 		};
 		let value = if let LazyValInternals::Waiting(value) =