Repository navigation
Validate JSON/Dynamic data type AST shape before indexing - #103545
Conversation
The AST fuzzer can produce a structurally invalid AST: in particular
`fuzzColumnLikeExpressionList` can drop children from any
`ASTExpressionList`, including the `arguments` of an `equals` function
inside an `ASTObjectTypeArgument` parameter or a `Dynamic` argument.
`createObject` (in `DataTypeObject.cpp`) and the `Dynamic` data type
creator (in `DataTypeDynamic.cpp`) accessed `function->arguments->children[0]`
and `[1]` without checking that `arguments` was non-null or that the
children vector had exactly two elements. With the fuzzer-corrupted AST
this either dereferenced a null `ASTPtr` or read past the end of a
`boost::container::vector`, segfaulting inside `operator[]` or
`intrusive_ptr::operator bool`.
Add the missing guards and report `UNEXPECTED_AST_STRUCTURE` instead.
While here:
* Validate the result of `argument->as<ASTObjectTypeArgument>()` before
dereferencing it — fuzzed children are not guaranteed to be that AST
node type.
* Validate the result of `path_with_type->as<ASTObjectTypedPathArgument>()`
for the same reason.
* Fix two pre-existing error-message bugs that called
`formatForErrorMessage()` on the field that is itself null in the
failure path (`function` after a failed cast, and `skip_path` while
reporting a malformed `skip_path_regexp`).
Reproducer (master `a27a1f1ae8a`, MSan):
EXPLAIN AST CREATE TABLE t (c0 JSON(max_dynamic_paths = 758)) ENGINE = Memory
SETTINGS ast_fuzzer_runs = 200, ast_fuzzer_any_query = 1;
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a27a1f1ae8ac33d002adf2f74dc2c12ed48f3dd2&name_0=MasterCI&name_1=BuzzHouse%20%28experimental%2C%20serverfuzz%2C%20amd_msan%29
Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
cc @Avogar — could you review? Pre-PR validation:
|
|
Workflow [PR], commit [194d223] Summary: ❌
AI ReviewSummaryThis PR hardens Final Verdict
|
| for (const auto & argument : arguments->children) | ||
| { | ||
| const auto * object_type_argument = argument->as<ASTObjectTypeArgument>(); | ||
| /// The AST may be structurally invalid (e.g. produced by the AST fuzzer), so we cannot |
There was a problem hiding this comment.
Let's not mention fuzzer in the comments. It's just a safety checks that may be useful in general
There was a problem hiding this comment.
Done in a4c7f08. The three defensive-check comments in DataTypeObject.cpp (lines 631 and 642) and DataTypeDynamic.cpp (line 82) now describe the safety check as general — validating the AST shape before indexing — without naming the fuzzer.
The test header in 04120_json_dynamic_invalid_ast_no_segfault.sh still mentions the AST fuzzer because the test itself runs --ast_fuzzer_runs=200 to exercise these guards; happy to reword that too if you'd prefer it framed differently.
Per @Avogar review on PR ClickHouse#103545: the size/cast guards in `createObject` (`DataTypeObject.cpp`) and the `Dynamic` creator (`DataTypeDynamic.cpp`) are general defensive checks for malformed AST input, not fuzzer-specific. Drop the "produced by the AST fuzzer" and "mutated by the fuzzer" framing from the inline comments and describe what the checks actually do. No behavior change.
The previous version of `04120_json_dynamic_invalid_ast_no_segfault.sh` used `ast_fuzzer_runs = 200` and ran each of three `EXPLAIN AST CREATE TABLE` statements once. The bug only fires when the AST fuzzer randomly picks one of several rare mutation branches inside `ASTObjectTypeArgument` (child-replacement via subquery/arithmetic/`if`/`multiIf` wrapping, or a child-drop on the inner `equals(name, value)` parameter expression). Empirically the test crashed unfixed master only ~15-20% of the time per single invocation, and the `Bugfix validation (functional tests)` check reported "Failed to reproduce the bug" with a 0.71s pass on unfixed master. This commit makes the reproducer consistently fail before / pass after: 1. Prime the fuzzer's `column_like` pool with a variety of `ASTLiteral`, `ASTIdentifier`, `ASTFunction` and table-column references via an initial `SELECT` + `CREATE TABLE` + `INSERT` + `SELECT` + `DROP TABLE` block. The mutation branches in `fuzzColumnLikeExpressionList` (line 648-657) and `fuzzExpressionList` (arithmetic / `if` / `multiIf`) only actually replace children when `getRandomColumnLike()` returns non-null, so on a freshly started server they almost never fire. 2. Bump `ast_fuzzer_runs` from 200 to 1000 per query (single-run fuzz budget per `EXPLAIN AST`). 3. Run the three fuzzer queries in an outer loop of 50 iterations. The single-iteration crash rate on unfixed master is still random; the outer loop amortises that. Empirically (10 trials on rebuilt unfixed master with the same source as `HEAD^^`): 10/10 trials crashed the server, with iter-to-crash distributed as `1, 1, 2, 2, 3, 5, 5, 6, 10, 19, 27` (max 27). On the fix branch the test passes 5/5 in ~26 seconds with no server crashes. The test still exits with `SELECT 'ok'`; on unfixed master the server is dead by the time the loop ends and the final `SELECT` produces no `ok`, which the `.reference` file (`ok\n`) does not match, so the test fails.
|
This was fixed by #105146. Let's update the branch. |
|
@groeneai the new test |
Avogar reported the test timing out in CI. The previous deterministic form ran three separate `EXPLAIN AST` invocations 50 times each with `ast_fuzzer_runs=1000` per invocation, totalling 150 client calls and ~26 s on debug per run. Sanitizer flaky check runs the test 50 times, which pushed the job past its 45 min budget. This commit runs the same three CREATE TABLE shapes in a single client invocation per outer iteration via `-m -q`, and reduces the outer loop from 50 to 25. Total fuzz budget is 75 000 fuzzer mutations (three shapes x 1000 fuzz runs x 25 outer iterations). Verified locally on a fresh rebuilt unfixed master binary: 5/5 trials abort the server inside `DataTypeDynamic::create` (signal 6) before the test finishes the outer loop. Verified on the fixed binary: 5/5 trials pass with the test completing in ~6.6 s on debug, about 4x faster than the previous form.
|
@Avogar shortened the test in The previous deterministic form ran three New form: three CREATE TABLE shapes sent together in one client invocation per outer iteration via Verified locally:
Session: cron:clickhouse-ci-task-worker:20260519-150000 |
|
@groeneai new test still fails in the CI. Maybe it was not a good idea to add a bash test with fuzzing. If the error cannot be reproduced without fuzzer using usual queries, I think it's ok to add additional validations without the test. |
Per @Avogar's review on PR ClickHouse#103545: the bash test 04120_json_dynamic_invalid_ast_no_segfault.sh used `ast_fuzzer_runs` to mutate the AST after parsing, but the resulting test was both fuzzer-dependent and slow enough to time out in some CI jobs. The malformed ASTs that the fuzzer randomly produces (e.g. an `equals` function with null `arguments`, a non-`ASTFunction` `parameter`, a non-`ASTObjectTypedPathArgument` `path_with_type`) cannot be obtained from any well-formed SQL, so a stateless test on top of normal queries is not possible. Build the same shapes directly as AST trees in a gtest and exercise `DataTypeFactory::get("JSON" / "Dynamic", arguments)`. * Delete `tests/queries/0_stateless/04120_json_dynamic_invalid_ast_no_segfault.sh` and its reference file. * Extend `gtest_DataTypeDynamic_create.cpp` with four new cases for `DataTypeDynamic::create`: equals with null `arguments`, with 0 children, with 1 child, and with 3 children. * Add `gtest_DataTypeObject_create.cpp` with eight cases for `DataTypeObject::createObject`: non-`ASTObjectTypeArgument` child, equals with null `arguments` / 0 children / 1 child, parameter with wrong function name, parameter that is not an `ASTFunction`, `path_with_type` with the wrong AST kind, `skip_path_regexp` with the wrong AST kind. Verified by reverting the defensive checks in `DataTypeObject.cpp` and `DataTypeDynamic.cpp`: 11 of 15 tests reproduce the regression (10 SIGABRT from boost intrusive_ptr null-deref + 1 incorrect behavior). The other 4 are valid-AST sanity checks and the pre-existing regression from PR ClickHouse#101309. All 15 pass once the defensive checks are restored. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@Avogar — replaced the bash fuzzer test with deterministic gtests in 194d223. You're right: the bash test relied on Changes in 194d223:
Verified in both directions on debug:
Total runtime is under 1 ms across all 15 cases — no sanitizer timeout concern. Run with: |
|
Pre-PR validation gate (session cron:clickhouse-ci-task-worker:20260520-141500): a) Deterministic repro: yes. Each of the 11 reproducing gtests crashes on the unfixed binary every time the test is run. No fuzzer, no randomness. b) Root cause: c) Fix matches root cause: yes. Guards added at exactly those access points in commit 0745502, unchanged in this push. d) Test intent preserved: yes. The previous bash test relied on fuzzer randomness to construct one of these malformed shapes; the new gtests construct each shape directly. Coverage is broader (8 JSON shapes + 4 Dynamic shapes vs. the previous 3 fuzzed e) Both directions: yes. Verified above — 11/15 reproduce on unfixed binary, all 15 pass on fixed binary. f) Fix is general, not a narrow patch: the original commit already covers all four call-site classes in the two files (cast-result null check on |
LLVM Coverage Report
Changed lines: 98.65% (146/148) · Uncovered code |
2a2e9c3
Per @Avogar's review on PR ClickHouse#103545: the bash test 04120_json_dynamic_invalid_ast_no_segfault.sh used `ast_fuzzer_runs` to mutate the AST after parsing, but the resulting test was both fuzzer-dependent and slow enough to time out in some CI jobs. The malformed ASTs that the fuzzer randomly produces (e.g. an `equals` function with null `arguments`, a non-`ASTFunction` `parameter`, a non-`ASTObjectTypedPathArgument` `path_with_type`) cannot be obtained from any well-formed SQL, so a stateless test on top of normal queries is not possible. Build the same shapes directly as AST trees in a gtest and exercise `DataTypeFactory::get("JSON" / "Dynamic", arguments)`. * Delete `tests/queries/0_stateless/04120_json_dynamic_invalid_ast_no_segfault.sh` and its reference file. * Extend `gtest_DataTypeDynamic_create.cpp` with four new cases for `DataTypeDynamic::create`: equals with null `arguments`, with 0 children, with 1 child, and with 3 children. * Add `gtest_DataTypeObject_create.cpp` with eight cases for `DataTypeObject::createObject`: non-`ASTObjectTypeArgument` child, equals with null `arguments` / 0 children / 1 child, parameter with wrong function name, parameter that is not an `ASTFunction`, `path_with_type` with the wrong AST kind, `skip_path_regexp` with the wrong AST kind. Verified by reverting the defensive checks in `DataTypeObject.cpp` and `DataTypeDynamic.cpp`: 11 of 15 tests reproduce the regression (10 SIGABRT from boost intrusive_ptr null-deref + 1 incorrect behavior). The other 4 are valid-AST sanity checks and the pre-existing regression from PR ClickHouse#101309. All 15 pass once the defensive checks are restored. Co-Authored-By: Claude Opus 4.7 <[email protected]>
…t-null-deref-1477-2777 Validate JSON/Dynamic data type AST shape before indexing
The AST fuzzer can produce a structurally invalid AST: in particular
fuzzColumnLikeExpressionListcan drop children from anyASTExpressionList, including theargumentsof anequalsfunction inside anASTObjectTypeArgumentparameter or aDynamicargument.createObject(inDataTypeObject.cpp) and theDynamicdata type creator (inDataTypeDynamic.cpp) then accessedfunction->arguments->children[0]and[1]without checking thatargumentswas non-null or that the children vector had exactly two elements, segfaulting insideboost::container::vector::operator[]orboost::intrusive_ptr::operator boolon the fuzzed AST.STID:
1477-2777— observed once on masterBuzzHouse (experimental, serverfuzz, amd_msan)at commita27a1f1ae8ac33d002adf2f74dc2c12ed48f3dd2(CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a27a1f1ae8ac33d002adf2f74dc2c12ed48f3dd2&name_0=MasterCI&name_1=BuzzHouse%20%28experimental%2C%20serverfuzz%2C%20amd_msan%29). Reproduced locally on master with:This deterministically crashes within a handful of client invocations on master; with the fix it passes 100/100 invocations.
The fix:
createObject: null-check the result ofargument->as<ASTObjectTypeArgument>()(fuzzed children are not guaranteed to be that node type), validate thatfunction->argumentsis non-null and has exactly 2 children before indexing, null-check thepath_with_typecast, and fix two pre-existing error-message bugs that calledformatForErrorMessageon the field that was itself null in the failure path (functionafter a failed cast,skip_pathwhile reporting a badskip_path_regexp).Dynamiccreator: validateargument->argumentsis non-null and has exactly 2 children before indexing — the same fuzzer pattern applies toDynamic(max_types = N).Includes a stateless regression test (
04120_json_dynamic_invalid_ast_no_segfault.sh) that exercises the AST fuzzer overCREATE TABLEstatements withJSONandDynamiccolumns. Verified test fails on unfixed master (server crash → exit 143) and passes 5/5 on the fix.Changelog category (leave one):
Changelog entry:
Fix a server crash in JSON and Dynamic data type parameter parsing when the abstract syntax tree (AST) was structurally malformed (for example produced by the AST fuzzer): the
equals(name, value)parameter expression could be left with fewer than two children, whichDataTypeObjectandDataTypeDynamicthen dereferenced without bounds checking.Documentation entry for user-facing changes
Version info
26.5.1.937