Skip to content

Validate UTF-8 of delta-kernel-rs FFI options and fix EngineBuilder leak (re-apply #105897) - #106018

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/redo-105897-utf8-and-leak
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/redo-105897-utf8-and-leak

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Re-submits the change from #105897 ("Validate UTF-8 of DeltaLake delta-kernel-rs FFI option values"), which was merged in a07ca558f023 and then reverted in 3ab7b26f7189 / #105963.

Why the original was reverted. ASan flagged a leak in the s3 storage stateless variant: the Rust FFI does not expose free_engine_builder, and the new setBuilderOptionChecked wrapper threw BAD_ARGUMENTS on the invalid-UTF-8 path before ffi::builder_build could consume the partially configured ffi::EngineBuilder. The original CI leak report (per #105961):
https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/json.html?PR=59140&sha=latest&name_0=PR&name_1=Stateless+tests+%28amd_asan_ubsan%2C+distributed+cache%2C+meta+in+keeper%2C+s3+storage%2C+parallel%2C+1%2F2%29

What changed. Combines the original UTF-8 validation with the leak fix prototyped by @PedroTadim in #105961 (closed when the revert landed):

  1. IKernelHelper::createBuilder is now a non-virtual base method. It allocates the builder via ffi::get_engine_builder, invokes a new protected virtual configureBuilder hook, and, if configureBuilder throws, consumes the builder via ffi::builder_build and frees the resulting engine via ffi::free_engine before rethrowing.
  2. S3KernelHelper and AzureKernelHelper now override configureBuilder and route every option through setBuilderOptionChecked, which rejects invalid UTF-8 with BAD_ARGUMENTS so it never reaches the FFI &str decode that panics with panic_in_cleanup.
  3. LocalKernelHelper inherits the default empty configureBuilder and no longer appends a second / when the user-provided path already ends with one (also from Kernel Helper fixes #105961).

Test. 04294_deltalake_azure_invalid_utf8_credentials uses the deterministic reproducer SELECT * FROM deltaLakeAzure('\0', MD5('')) from #105895. It is kept as a .sh test on purpose: the unfixed binary aborts the process, so a .sql test with { serverError BAD_ARGUMENTS } cannot distinguish a crash from a clean error and Bugfix validation would not record a clean FAIL on master. With clickhouse-local + grep, the unfixed binary produces empty stdout and the fixed binary prints the diagnosis, so Bugfix validation works in both directions.

cc @CheSema (your concern on the original PR about a post-approval test commit landing without a full CI run is acknowledged: this re-submission runs the new test through full CI before merge), @Algunenano (revert author), @PedroTadim (leak fix author), @kssenii, @alesapin.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix a server-side abort triggered by passing non-UTF-8 byte sequences (for example raw MD5(...) bytes used as a SAS token) to the deltaLakeAzure table function or DeltaLake engine when allow_experimental_delta_kernel_rs = 1. The delta-kernel-rs Rust FFI panicked on invalid UTF-8, crossing the extern "C" boundary and aborting the server through panic_in_cleanup. Such inputs now return BAD_ARGUMENTS instead. Also fixes a memory leak of the ffi::EngineBuilder allocation that could occur when builder configuration failed.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Re-submits the change from PR ClickHouse#105897 ("Validate UTF-8 of DeltaLake
delta-kernel-rs FFI option values"), which was merged in a07ca55
and then reverted in 3ab7b26 / PR ClickHouse#105963. The revert reason was a
memory leak surfaced by ASan in the `s3 storage` stateless variant when
the new `setBuilderOptionChecked` wrapper rejected an invalid-UTF-8
option: the Rust FFI does not expose a `free_engine_builder`, and the
partially configured `ffi::EngineBuilder` was leaked when the wrapper
threw before `ffi::builder_build` could consume it.

This patch combines the original UTF-8 validation with the leak fix
prototyped by @PedroTadim in PR ClickHouse#105961:

* `IKernelHelper::createBuilder` is now a non-virtual base method that
  allocates the builder via `ffi::get_engine_builder`, calls a new
  protected virtual `configureBuilder` hook, and, if `configureBuilder`
  throws, cleans the builder up by calling `ffi::builder_build`
  (consumes the builder, returns an engine handle) followed by
  `ffi::free_engine`. The original exception is rethrown.
* `S3KernelHelper` and `AzureKernelHelper` override `configureBuilder`
  instead of `createBuilder` and route every option through
  `setBuilderOptionChecked`, which rejects invalid UTF-8 with
  `BAD_ARGUMENTS` so it never reaches the FFI `&str` decode that
  panics with `panic_in_cleanup`.
* `LocalKernelHelper` inherits the default empty `configureBuilder`.
  Its `getTableLocation` no longer appends a second `/` when the
  user-provided path already ends with one (also from PR ClickHouse#105961).

The regression test `04294_deltalake_azure_invalid_utf8_credentials`
uses the deterministic reproducer
`SELECT * FROM deltaLakeAzure('\0', MD5(''))` from issue ClickHouse#105895 and is
kept as a `.sh` test on purpose: the unfixed binary crashes the
process, so a `.sql` test with `{ serverError BAD_ARGUMENTS }` cannot
distinguish a crash from a clean error and Bugfix validation would not
record a clean `FAIL` on master. With `clickhouse-local` + `grep`, the
unfixed binary produces empty stdout (process abort, no
`BAD_ARGUMENTS` text) and the fixed binary prints the diagnosis, so
Bugfix validation works for both directions.

CI leak that motivated the revert (per PR ClickHouse#105961):
https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/json.html?PR=59140&sha=latest&name_0=PR&name_1=Stateless+tests+%28amd_asan_ubsan%2C+distributed+cache%2C+meta+in+keeper%2C+s3+storage%2C+parallel%2C+1%2F2%29

Closes ClickHouse#104509
Closes ClickHouse#105895

Co-Authored-By: Pedro Ferreira <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per worker policy, separate from the PR description):

# Question Status
a Deterministic repro? YES. SELECT * FROM deltaLakeAzure('\0', MD5('')) SETTINGS allow_experimental_delta_kernel_rs = 1 reproduces both the original FFI panic (without the validation) and the EngineBuilder leak (without the cleanup). The repro is from #105895 / @PedroTadim.
b Root cause explained? YES, two-part. (1) delta-kernel-rs/ffi::set_builder_option decodes the (ptr, len) slice via std::str::from_utf8 and .unwrap()s the result, so any non-UTF-8 byte sequence panics across the extern "C" boundary and panic_in_cleanup aborts the server (#104509, #105895). (2) The original fix translated the panic to BAD_ARGUMENTS but did not free the partially configured ffi::EngineBuilder; the Rust FFI does not expose free_engine_builder and the builder is only released by ffi::builder_build (which consumes it and returns an engine handle that itself must be freed via ffi::free_engine), so throwing before builder_build ran leaked the builder allocation. This was the ASan finding that caused #105963 to revert #105897.
c Fix matches root cause? YES. (1) setBuilderOptionChecked runs DB::UTF8::isValidUTF8 on the value bytes before the FFI call and throws BAD_ARGUMENTS on invalid input; nothing reaches the Rust &str decode. (2) IKernelHelper::createBuilder now wraps configureBuilder in a try/catch that, on failure, calls ffi::builder_build (consumes the builder, the Rust Box::from_raw releases it regardless of success/failure) and frees the resulting engine handle with ffi::free_engine.
d Test intent preserved? YES. Same query, same expected behavior (return BAD_ARGUMENTS to the user instead of aborting). The test is intentionally .sh not .sql so Bugfix validation can distinguish a process abort (empty stdout -> clean FAIL on master) from a clean error (grep matches BAD_ARGUMENTS -> PASS on the fixed binary).
e Both directions demonstrated? PARTIAL. The UTF-8 validation half was already CI-validated by the original #105897 (merged after Algunenano's APPROVE); the leak fix half is mechanically the same as the design in #105961, which the PR author wrote against the ASan leak report. Local two-direction verification requires a delta-kernel-rs build (Rust nightly-2026-03-22 + ENABLE_LIBRARIES=ON), which I do not have ready in this worker session; full CI here is the verification path. I will not request maintainer attention to merge until both the new test and the existing ASan stateless variants are green.
f Fix is general, not a narrow patch? YES. The cleanup lives on the base IKernelHelper::createBuilder, so it covers S3KernelHelper, AzureKernelHelper, and (vacuously) LocalKernelHelper; any future helper inherits it automatically. The UTF-8 validation is in the single helper used by both S3 and Azure (LocalKernelHelper does not call set_builder_option and is unaffected). I am NOT adding a defensive check at the original crash site only - the new structure ensures every set_builder_option call site is guarded and every throw path through configureBuilder releases the builder.

@PedroTadim

Copy link
Copy Markdown
Member

@Algunenano you give a review first before enabling testing. This looks fine for me.

@Algunenano Algunenano self-assigned this May 28, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR addresses:

  • set_builder_option panicking on invalid UTF-8
  • The EngineBuilder leak from the original revert

This PR does not address:

  • ffi::snapshot failures reported in #104509 as Failure mode 2.
  • Also, it seems that the cleanup path can re-trigger the same bug
  • name is not validated. Right now it's internal only, but it's one step away from being public.

I mentioned when reviewing the other PR that it was ok but we should fix this properly. That PR had to be reverted. So let's fix this properly upstream, then apply checks and proper error handling here.

@groeneai I don't think you can work with rust submodules, so I'll deal with it

@groeneai

Copy link
Copy Markdown
Collaborator Author

Acknowledged, closing. Happy to help with the C++ cleanup wrapper (createBuilder non-virtual base + try/catch + builder_build then free_engine) once the upstream delta-kernel-rs fix lands, if useful.

@groeneai groeneai closed this May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants