Skip to content

Commit 83f08ad

Browse files
test: improve parse consensus test and add VaryExpressionStackDepthTooDeep variant test, #6627
1 parent d28f8bc commit 83f08ad

5 files changed

+101
-11
lines changed

stackslib/src/chainstate/tests/parse_tests.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

1616
/// This module contains consensus tests related to Clarity Parse errors.
17-
use clarity::vm::ast::parser::v2::MAX_CONTRACT_NAME_LEN;
17+
use clarity::vm::ast::parser::v2::{MAX_CONTRACT_NAME_LEN, MAX_NESTING_DEPTH};
1818
use clarity::vm::ast::stack_depth_checker::AST_CALL_STACK_DEPTH_BUFFER;
1919
use clarity::vm::MAX_CALL_STACK_DEPTH;
2020
#[allow(unused_imports)] // Just used for documentation purpose
@@ -23,21 +23,72 @@ use clarity::vm::{ast::errors::ParseErrors, representations::ContractName};
2323
use crate::chainstate::tests::consensus::contract_deploy_consensus_test;
2424

2525
/// ParserError: [`ParseErrors::ExpressionStackDepthTooDeep`]
26-
/// Caused by: nested contract body exceeding stack depth limit
26+
/// Caused by: nested contract body exceeding stack depth limit on parsing tuples
2727
/// Outcome: block rejected
2828
#[test]
29-
fn test_stack_depth_too_deep() {
29+
fn test_stack_depth_too_deep_case_1_tuple_only_parsing() {
3030
contract_deploy_consensus_test!(
3131
contract_name: "my-contract",
3232
contract_code: &{
33-
let count = AST_CALL_STACK_DEPTH_BUFFER + (MAX_CALL_STACK_DEPTH as u64);
33+
// In parse v2, open brace '{' have a stack count of 2.
34+
let count = MAX_NESTING_DEPTH / 2 + 1;
3435
let body_start = "{ a : ".repeat(count as usize);
3536
let body_end = "} ".repeat(count as usize);
3637
format!("{body_start}u1 {body_end}")
3738
},
3839
);
3940
}
4041

42+
/// ParserError: [`ParseErrors::ExpressionStackDepthTooDeep`]
43+
/// Caused by: nested contract body exceeding stack depth limit on parsing lists
44+
/// Outcome: block rejected
45+
#[test]
46+
fn test_stack_depth_too_deep_case_2_list_only_parsing() {
47+
contract_deploy_consensus_test!(
48+
contract_name: "my-contract",
49+
contract_code: &{
50+
// In parse v2, open parent '(' have a stack count of 1.
51+
let count = MAX_NESTING_DEPTH;
52+
let body_start = "(list ".repeat(count as usize);
53+
let body_end = ")".repeat(count as usize);
54+
format!("{body_start}u1 {body_end}")
55+
},
56+
);
57+
}
58+
59+
/// ParserError: [`ParseErrors::ExpressionStackDepthTooDeep`]
60+
/// Caused by: nested contract body exceeding stack depth limit on checking lists ast
61+
/// Outcome: block rejected
62+
#[test]
63+
fn test_stack_depth_too_deep_case_3_list_only_checker() {
64+
contract_deploy_consensus_test!(
65+
contract_name: "my-contract",
66+
contract_code: &{
67+
// In parse v2, open parent '(' have a stack count of 1.
68+
let count = AST_CALL_STACK_DEPTH_BUFFER + MAX_CALL_STACK_DEPTH as u64;
69+
let body_start = "(list ".repeat(count as usize);
70+
let body_end = ")".repeat(count as usize);
71+
format!("{body_start}u1 {body_end}")
72+
},
73+
);
74+
}
75+
76+
/// ParserError: [`ParseErrors::VaryExpressionStackDepthTooDeep`]
77+
/// Caused by: nested contract body exceeding stack depth limit on checking vary list/tuple ast
78+
/// Outcome: block rejected
79+
#[test]
80+
fn test_vary_stack_depth_too_deep_checker() {
81+
contract_deploy_consensus_test!(
82+
contract_name: "my-contract",
83+
contract_code: &{
84+
let count = AST_CALL_STACK_DEPTH_BUFFER + (MAX_CALL_STACK_DEPTH as u64) - 1;
85+
let body_start = "(list ".repeat(count as usize);
86+
let body_end = ")".repeat(count as usize);
87+
format!("{{ a: {body_start}u1 {body_end} }}")
88+
},
89+
);
90+
}
91+
4192
/// ParserError: [`ParseErrors::FailedParsingIntValue`]
4293
/// Caused by: number bigger than i128
4394
/// Outcome: block accepted
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ source: stackslib/src/chainstate/tests/parse_tests.rs
33
expression: result
44
---
55
[
6-
Failure("Invalid Stacks block ac67c796c42af78177f41f5bfaa242763cf768605a78aa97c74d3d09c848bd51: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
7-
Failure("Invalid Stacks block 2abf2fe1577dd15d16c58d034c7bd5198b7aecf7d5b864e353be579c47e50777: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
8-
Failure("Invalid Stacks block 348a5a7c9b7c225d94018b557cfaec5cf16fdb3a3c010b1a621ace29f2cdf752: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
9-
Failure("Invalid Stacks block 7404fb192410627500be0f8ba5e533a8a8cb15fc2e5ff41461df6e3c9cfb0300: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
10-
Failure("Invalid Stacks block 11aac718d1276584504cb726d97da643abc2d89e105cd44b1ef69167705d7c62: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
11-
Failure("Invalid Stacks block b47f430b610745ee70f08a98d12ff8762f8889406e4da2c655bae90def3ab3be: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
12-
Failure("Invalid Stacks block 5db34beb749c249dce37c64d8460c31b24e9690dfdef2ba377bfe8840ff96b7a: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
6+
Failure("Invalid Stacks block eee942941840b5db1a7fc05d20acb5d2f200ed647bde7bcc0ba3309e695469eb: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
7+
Failure("Invalid Stacks block c8983f3af8d5a2fa9b1eb8e906c6950dd5c7b0c0057a6d265f604f2772777417: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
8+
Failure("Invalid Stacks block 61b6c4d3ffbaf3a978de58caf85d3a2223009c66158f2abc34cc99182ed49c5e: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
9+
Failure("Invalid Stacks block c336a60df8f343ba813e142dfd10d6bdb7c1a3348f83347c7021241eb8541788: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
10+
Failure("Invalid Stacks block a7a3174b62e287d6e48a3e34f5c143237cfb84b3d67b11809dbe28b65640c9ec: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
11+
Failure("Invalid Stacks block 515c90a569195265c29e0282aca733f6c6de1c503bb31d8432268dda88a1f645: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
12+
Failure("Invalid Stacks block c1816691eaf780dd2555b575d2f185ae7db86f6c1aa964aab64ec05ef2eb3fe0: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
1313
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: stackslib/src/chainstate/tests/parse_tests.rs
3+
expression: result
4+
---
5+
[
6+
Failure("Invalid Stacks block 4c2dc4e4b2dd883494871f7ca262bc2433f10f4e2db67730829c64e867dec99c: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
7+
Failure("Invalid Stacks block 03a3bd721492f73d156261ffb1e58dbd332a848ebaaba01fdef22e0b0d182e80: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
8+
Failure("Invalid Stacks block 6057f35bb81ce5c2c92d06be9c9896fbe17da8db99f85faf7543ac9a56ad0a7e: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
9+
Failure("Invalid Stacks block d104a3c57238a85d6e26102c96f19509b83f260216e7d68cf1160b1ab34e096c: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
10+
Failure("Invalid Stacks block f921461ac04e9067df84ff89f9c4588017fe79e1945cdbc7c5e8d701bdd72765: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
11+
Failure("Invalid Stacks block 1dc3586fb63d71b491f39f0c76f511791c9e86128989dfa401899c89a1aa6a7a: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
12+
Failure("Invalid Stacks block e10cd0c718c6b8a06afc14f8257cd7c0f1d34f311d344b1ac7e42d398eb741d8: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
13+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: stackslib/src/chainstate/tests/parse_tests.rs
3+
expression: result
4+
---
5+
[
6+
Failure("Invalid Stacks block aa2b76f30d5fd36e874bda64bfbf34dfebf2fd31134e538d6e0739524bfb9912: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
7+
Failure("Invalid Stacks block f1207424636581f5572aed14578877192c9ce0197785edead14a526110c983d2: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
8+
Failure("Invalid Stacks block 9df8dc7011856ff6c92fffb1237afe8aebc41ebdd55740c5df5c2be573e47d60: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
9+
Failure("Invalid Stacks block 0bd7cebc7cb7f017726d5fec98eea4fa9764b839f91bd78f84ec27e56dbc765f: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
10+
Failure("Invalid Stacks block ddb83008e30bf87982ed58e6098fc09998dadf4285577a893bb169f159a3b165: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
11+
Failure("Invalid Stacks block 96f60e2433bd9513a2bba01375b5ab977c67cf1fd4a0feaa43ae91a94687d8a3: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
12+
Failure("Invalid Stacks block 909b3bc78b09d74e3a47e26d092b427b4c1c11b383f2cbecf663840f3c39719b: ClarityError(Parse(ParseError { err: ExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
13+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: stackslib/src/chainstate/tests/parse_tests.rs
3+
expression: result
4+
---
5+
[
6+
Failure("Invalid Stacks block b085a5de95bae8d7ae5622ffed3417f32ba32784b4a68812e6de93a8d80b25bd: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
7+
Failure("Invalid Stacks block 48181af8c3de9084fcb542d9db61bafab67119999e95e07a141594e60bf58796: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
8+
Failure("Invalid Stacks block bcb3cbdb3df50befd9b216ba0ef49f8b0cb987dd68e0db480f97f1c49af38b1c: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
9+
Failure("Invalid Stacks block 1420e0265448dde5f05b702f814f02e95a21043c69c7aefc7ff8707a5ad9949c: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
10+
Failure("Invalid Stacks block 22ff43430d5b9802ccae90589ffcc5ca7eb40a2a8bc6cb1f096c32e9531dd1bb: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
11+
Failure("Invalid Stacks block 57631dae5417f3444e699094dc9a91486f9c7e68e551bade03b628bcbd1630a9: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
12+
Failure("Invalid Stacks block 9e492e7997f60358f29e0f5ce3df2941903e92d524bd3a31d3b1cc1d838557f2: ClarityError(Parse(ParseError { err: VaryExpressionStackDepthTooDeep, pre_expressions: None, diagnostic: Diagnostic { level: Error, message: \"AST has too deep of an expression nesting. The maximum stack depth is 64\", spans: [], suggestion: None } }))"),
13+
]

0 commit comments

Comments
 (0)