Skip to content

Fix dangling NameSet reference in UserDefinedSQLFunctionVisitor - #106121

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-udf-normalizer-dangling-nameset
Jun 1, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-udf-normalizer-dangling-nameset

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented May 29, 2026 •

Copy link
Copy Markdown
Member

UserDefinedSQLFunctionVisitor::tryToReplaceFunction constructed a QueryNormalizer::Data with a {} temporary as the source_columns_set argument. Because Data stores source_columns_set as a const NameSet &, the temporary's lifetime ended as soon as the constructor returned, leaving the reference member dangling. The subsequent QueryNormalizer::visit on the UDF body could then read that destroyed unordered_set from QueryNormalizer::visit(ASTIdentifier &, ...) when prefer_column_name_to_alias is set, which the MSan build catches as use-of-uninitialized-value with a "Member fields were destroyed" stack pointing at the same statement.

Trigger conditions: skip_redundant_aliases_in_udf = 1 (gates the path) and prefer_column_name_to_alias = 1 (gates the read of source_columns_set).

Reproduced with the provided MSan binary against a UDF-using SELECT ... SETTINGS enable_analyzer = 0 and against a CREATE TABLE with the UDF in a MATERIALIZED expression. Both stacks match the BuzzHouse report at:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=549ac9afd4ee4fd58d177ad5b7b88b4a517805d6&name_0=MasterCI&name_1=BuzzHouse+%28amd_msan%29

Fix: give the empty NameSet a named local so it lives for the whole statement.

Closes #106040

Changelog category (leave one):

  • CI Fix or Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixes possible memory corruption when expanding SQL user-defined functions with prefer_column_name_to_alias enabled.

Version info

  • Merged into: 26.6.1.294

`UserDefinedSQLFunctionVisitor::tryToReplaceFunction` constructed a
`QueryNormalizer::Data` with a `{}` temporary as the `source_columns_set`
argument. Because `Data` stores `source_columns_set` as a `const NameSet &`,
the temporary's lifetime ended as soon as the constructor returned, leaving
the reference member dangling. The subsequent `QueryNormalizer::visit` on
the UDF body could then read that destroyed `unordered_set` from
`QueryNormalizer::visit(ASTIdentifier &, ...)` when
`prefer_column_name_to_alias` is set, which the MSan build catches as
use-of-uninitialized-value with a "Member fields were destroyed" stack
pointing at the same statement.

Trigger conditions: `skip_redundant_aliases_in_udf = 1` (gates the path)
and `prefer_column_name_to_alias = 1` (gates the read of `source_columns_set`).

Reproduced with the provided MSan binary against a UDF-using
`SELECT ... SETTINGS enable_analyzer = 0` and against a `CREATE TABLE` with
the UDF in a `MATERIALIZED` expression. Both stacks match the BuzzHouse
report at:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=549ac9afd4ee4fd58d177ad5b7b88b4a517805d6&name_0=MasterCI&name_1=BuzzHouse+%28amd_msan%29

Fix: give the empty `NameSet` a named local so it lives for the whole
statement.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0219d84]

Summary: ⏳

job_name test_name status info comment
Bugfix validation (functional tests) FAIL
04280_udf_normalizer_dangling_source_columns FAIL cidb

AI Review

Summary

This PR fixes the dangling NameSet reference in QueryNormalizer::Data callers by keeping the empty source_columns set alive through QueryNormalizer::visit, and adds focused SQL coverage for the UDF expansion path that originally exposed the sanitizer finding. The earlier bot finding for StorageWindowView::getInnerTableCreateQuery is addressed in the current code, and I did not find any remaining correctness or safety blockers in the changed surface.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 29, 2026
KochetovNicolai and others added 2 commits May 29, 2026 18:02
`QueryNormalizer::Data` stores `source_columns_set` by reference, so any
caller passing `{}` for that argument leaves the member dangling. Beyond
the UDF visitor fixed in the previous commit, two other call sites had
the same pattern:

* `StorageWindowView::getInnerTableCreateQuery` — production code path
  used when creating the inner table of a window view; would also be
  affected on the `prefer_column_name_to_alias = 1` path.
* `gtest_cycle_aliases` — both `SimpleLoopAlias` and `SimpleCycleAlias`
  test cases; benign in practice because the default `Settings` keeps
  `prefer_column_name_to_alias = 0`, but still UB.

Apply the same named-local fix in all three.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The flaky check runs the test in concurrent iterations. Because SQL
user-defined functions are global server objects (not per-database),
the fixed `udf_normalizer_04280` name collided across concurrent runs,
failing the `CREATE FUNCTION` with `FUNCTION_ALREADY_EXISTS` (error 609).

This matches the convention used by the rest of the UDF test suite (e.g.
`02354_vector_search_reference_vector_types`, `01856_create_function`),
which tag such tests `no-parallel` for the same reason.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106121&sha=e219665bcc03bb1d7f3fcdfb06b3cd3630aabe2f&name_0=PR

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

The "Server died" / "Unknown error" failure in Stateless tests (amd_asan_ubsan, db disk, distributed plan, sequential) is unrelated to this PR — it is the known flaky crash tracked in #105626 (SIGSEGV in MergeTreeReaderCompact::readData on the "db disk, distributed plan, sequential" shard), triggered by the 03716_mutations_parts_* mutation tests.

All remaining failures were iterations of the new 04280_udf_normalizer_dangling_source_columns test in the flaky check failing with FUNCTION_ALREADY_EXISTS (error 609): SQL user-defined functions are global server objects, so the fixed function name collided across the concurrent flaky-check iterations. Fixed by tagging the test no-parallel, matching the rest of the UDF test suite.

@alexey-milovidov alexey-milovidov added pr-ci and removed pr-bugfix Pull request with bugfix, not backported by default labels Jun 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 20/20 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemorySanitizer: use-of-uninitialized-value (STID: 2518-3457)

3 participants