From 6902fc861d17401436e064b269674b9163df5bec Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 27 May 2025 14:55:50 +0200 Subject: [PATCH] feat: improve stack resiliency --- Cargo.toml | 1 + core/expression/Cargo.toml | 6 ++++-- core/expression/src/compiler/compiler.rs | 2 ++ core/expression/src/intellisense/types/provider.rs | 1 + core/expression/src/parser/standard.rs | 1 + core/expression/src/parser/unary.rs | 1 + core/template/Cargo.toml | 11 +++++++++-- 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 92012b8c..0b5b525f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ tokio = "1" tokio-util = "0.7" once_cell = "1" petgraph = "0.8" +recursive = "0.1" regex = "1" regex-lite = "0.1" strum = "0.27" diff --git a/core/expression/Cargo.toml b/core/expression/Cargo.toml index 7b293a4c..12286cf4 100644 --- a/core/expression/Cargo.toml +++ b/core/expression/Cargo.toml @@ -16,6 +16,7 @@ chrono-tz = "0.10" humantime = { workspace = true } fastrand = { workspace = true } once_cell = { workspace = true } +recursive = { workspace = true, optional = true } regex = { workspace = true, optional = true } regex-lite = { workspace = true, optional = true } serde = { workspace = true, features = ["rc", "derive"] } @@ -35,9 +36,10 @@ csv = "1" serde_json5 = "0.2" [features] -default = ["regex-deprecated"] -regex-deprecated = ["dep:regex"] +default = ["regex-deprecated", "stack-protection"] regex-lite = ["dep:regex-lite"] +regex-deprecated = ["dep:regex"] +stack-protection = ["dep:recursive"] [[bench]] harness = false diff --git a/core/expression/src/compiler/compiler.rs b/core/expression/src/compiler/compiler.rs index f2ad8718..daf6c681 100644 --- a/core/expression/src/compiler/compiler.rs +++ b/core/expression/src/compiler/compiler.rs @@ -112,6 +112,7 @@ impl<'arena, 'bytecode_ref> CompilerInner<'arena, 'bytecode_ref> { self.compile_node(arg) } + #[cfg_attr(feature = "stack-protection", recursive::recursive)] fn compile_member_fast(&mut self, node: &'arena Node<'arena>) -> Option> { match node { Node::Root => Some(vec![FetchFastTarget::Root]), @@ -141,6 +142,7 @@ impl<'arena, 'bytecode_ref> CompilerInner<'arena, 'bytecode_ref> { } } + #[cfg_attr(feature = "stack-protection", recursive::recursive)] fn compile_node(&mut self, node: &'arena Node<'arena>) -> CompilerResult { match node { Node::Null => Ok(self.emit(Opcode::PushNull)), diff --git a/core/expression/src/intellisense/types/provider.rs b/core/expression/src/intellisense/types/provider.rs index 102625f2..4b6d819d 100644 --- a/core/expression/src/intellisense/types/provider.rs +++ b/core/expression/src/intellisense/types/provider.rs @@ -51,6 +51,7 @@ impl TypesProvider { }); } + #[cfg_attr(feature = "stack-protection", recursive::recursive)] fn determine(&mut self, node: &Node, scope: IntelliSenseScope) -> TypeInfo { #[allow(non_snake_case)] let V = |vt: VariableType| TypeInfo::from(vt); diff --git a/core/expression/src/parser/standard.rs b/core/expression/src/parser/standard.rs index b349d54d..b2c23980 100644 --- a/core/expression/src/parser/standard.rs +++ b/core/expression/src/parser/standard.rs @@ -19,6 +19,7 @@ impl<'arena, 'token_ref> Parser<'arena, 'token_ref, Standard> { } } + #[cfg_attr(feature = "stack-protection", recursive::recursive)] fn binary_expression(&self, precedence: u8) -> &'arena Node<'arena> { let mut node_left = self.unary_expression(); let Some(mut token) = self.current() else { diff --git a/core/expression/src/parser/unary.rs b/core/expression/src/parser/unary.rs index db3eba4f..fe4ef43c 100644 --- a/core/expression/src/parser/unary.rs +++ b/core/expression/src/parser/unary.rs @@ -132,6 +132,7 @@ impl<'arena, 'token_ref> Parser<'arena, 'token_ref, Unary> { left_node } + #[cfg_attr(feature = "stack-protection", recursive::recursive)] fn binary_expression(&self, precedence: u8, ctx: ParserContext) -> &'arena Node<'arena> { let mut node_left = self.unary_expression(); let Some(mut token) = self.current() else { diff --git a/core/template/Cargo.toml b/core/template/Cargo.toml index 0fca5022..f241ca68 100644 --- a/core/template/Cargo.toml +++ b/core/template/Cargo.toml @@ -8,7 +8,14 @@ edition = "2021" repository = "https://github.com/gorules/zen.git" [dependencies] -zen-expression = { path = "../expression", version = "0.45.0" } +zen-expression = { path = "../expression", version = "0.45.0", default-features = false } thiserror = { workspace = true } serde = { workspace = true } -serde_json = { workspace = true } \ No newline at end of file +serde_json = { workspace = true } + +[features] +default = ["stack-protection", "regex-deprecated"] + +stack-protection = ["zen-expression/stack-protection"] +regex-lite = ["zen-expression/regex-lite"] +regex-deprecated = ["zen-expression/regex-deprecated"] \ No newline at end of file