Skip to content

Revert "Revert "Print file offsets in the bare fatal stack traces and build the release binary as PIE"" - #119284

Merged
Algunenano merged 10 commits into
masterfrom
reland-117449-pie-trace-offsets
Sep 15, 2026
Merged

Algunenano merged 10 commits into
masterfrom
reland-117449-pie-trace-offsets

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Reverts #119279, restoring #117449 ("Print file offsets in the bare fatal stack traces and build the release binary as PIE").

#119279 reverted that change to get master green - 05076_error_trace_file_offsets was failing on every release run. The Darwin half had already been fixed on master by #119145, and its no-darwin tag comes back with this re-land.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...


Workflow [PR]
Sync PR [sync-upstream/pr/119284]

Version info

  • Merged into: 26.9.1.1514 (included in 26.9 and later)

… build the release binary as PIE""

This reverts 34421af (pull request #119279), restoring pull request
#117449.

#119279 reverted the change to get `master` green: the test it added,
`05076_error_trace_file_offsets`, was failing on every release run. The Darwin
half had already been fixed on `master` by 734d678 (pull request #119145),
whose `no-darwin` tag comes back with this re-land, but `amd_release` was still
failing and remains undiagnosed.

Do not merge this before that is fixed - it re-introduces a known red job.

What is known about the remaining failure:

- Only `Stateless tests (amd_release, parallel, 1/2)` fails.
  `arm_release` passes at the same times, and so does every other configuration.
- It is deterministic, not flaky: 48 of 48 runs with the randomized settings and
  49 of 49 without them.
- It is the second column that fails, dropping from 1 to 0: no captured frame
  lies outside the main executable, so either every frame is being stored as a
  file offset, or `max(address_end)` from `system.symbols` is above all of them.
- Both release builds are configured identically apart from the toolchain file,
  and both set `ENABLE_THINLTO=1`. Before this change PIE was gated on
  `NOT ENABLE_THINLTO`, so the release builds are exactly the ones whose PIE
  status it flips - and only the amd64 one fails. The difference is therefore in
  architecture-specific link or runtime behaviour, not in the CMake
  configuration.

Related: #117449
Related: #119145
Related: #119279
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [047e62c]

Summary: ✅


AI Review

Summary

This PR re-lands PIE for Linux release builds and updates the crash / error / system.stack_trace trace surfaces so main-binary frames are stored as file offsets while non-main frames stay in their runtime form. Most of the earlier review points were addressed, but two contract gaps remain in the current code and one earlier symbol-lookup problem still looks real despite the discussion dismissal.

Findings

⚠️ Majors

  • [ci/jobs/compatibility_check.py:62] process_pie_check is meant to prove that the packaged clickhouse executable is PIE, but it runs readelf on /usr/bin/clickhouse before the self-extracting wrapper has rewritten that path to the real inner ELF. A future regression where the wrapper stays PIE but the extracted server does not would still pass this job. Suggested fix: force one synchronous decompression first (for example clickhouse --version) and inspect the rewritten file, or read the inner ELF artifact directly.
  • [src/Common/SignalHandlers.cpp:581-584,650] The first Stack trace: line used to be the raw fallback that still worked when symbolization later failed. writeResolvedAddress now routes that fallback through StackTrace::resolveAddress and lazy SymbolIndex::instance() initialization, so a fault during early startup or during symbol-index initialization can lose the only addr2line-able dump. Suggested fix: keep the first bare dump raw, or make writeResolvedAddress fall back to writePointerHex(addr) when symbol lookup is unavailable.
  • [src/Common/SymbolIndex.cpp:761-773] [dismissed by author -- https://github.com/Revert "Revert "Print file offsets in the bare fatal stack traces and build the release binary as PIE"" #119284#discussion_r4003221621] findSymbol still normalizes a runtime address to an offset and then searches one global offset-sorted symbol table, discarding the object it just recovered. Non-main objects do exist here (dl_iterate_phdr and the new trace tests both rely on libc / loader frames), so overlapping offsets between DSOs can still resolve to the wrong symbol and the “runtime address keeps object identity” contract is not actually implemented for addressToSymbol. Suggested fix: make the lookup object-aware once findObject succeeds, or narrow the new comments/docs so they do not claim that property.
Final Verdict

Request changes.

LLVM Coverage Report

Measured on commit 047e62c.

Metric Baseline Current Δ
Lines 89.00% 89.10% +0.10%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.50% +0.00%

Changed lines: Changed C/C++ lines covered: 100/109 (91.74%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 10, 2026
@Algunenano Algunenano self-assigned this Sep 10, 2026
groeneai and others added 2 commits September 10, 2026 16:18
SymbolIndex copied Elf64_Sym::st_value (and + st_size) into
Symbol::offset_begin/offset_end without checking that the symbol is defined
relative to a section. When st_shndx is a reserved index, the value is a
constant rather than an address.

-fwhole-program-vtables, enabled for every ThinLTO build, emits LLVM's
control-flow-integrity type-id globals (__typeid__*) as absolute symbols whose
value is a bit mask. system.symbols published those verbatim, so on amd_release
max(address_end) read 1162219258676258 instead of 685204737 (505 such symbols;
amd_debug, built with ENABLE_THINLTO=0, has none). An "offset" of 1.16
petabytes into a 4.6 GB binary is impossible on its face.

05076_error_trace_file_offsets bounds captured stack frames by that value, so
the raw runtime addresses of frames outside the main executable stopped
exceeding it and the test printed "1 0" on every master commit since #117449
merged. The inflated bound also cost the first column its meaning on that
flavour, since it counted those same raw addresses as in-range.

No test is changed. Once the bound again reflects the largest real symbol
offset, both columns of 05076, and the same bound in
03172_error_log_table_not_empty, hold for the reason their comments state.

Of the 505 absolute values none lands inside any of the 620643 real FUNC symbol
ranges, so no code address resolves differently. The Mach-O collector already
skips N_STAB, N_UNDF and n_value == 0; it is left alone because both Darwin
recipes disable ThinLTO and no macOS job could validate the change.

Co-Authored-By: Claude Opus 5 <[email protected]>
`n_value` is an address only for a symbol defined in a section. `N_ABS` holds a
constant such as a bit mask and `N_INDR` holds a string table index, so neither
belongs in the offset ranges the symbol index is searched by.

The loop skipped `N_STAB`, `N_UNDF` and `n_value == 0`; requiring `N_SECT`
instead of rejecting `N_UNDF` covers all four non-section types at once and
mirrors `symbolValueIsAddress` on the ELF side.

Both Darwin recipes disable ThinLTO, so the `__typeid__*` symbols that motivated
the ELF change do not exist there and no macOS job can exercise this. It is the
same invariant, enforced in the one remaining place that turns a symbol table
entry into an offset.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Algunenano
Algunenano marked this pull request as ready for review September 10, 2026 16:19
{
writeChar(' ', bare_stacktrace);
writePointerHex(stack_trace.getFramePointers()[i], bare_stacktrace);
writeResolvedAddress(stack_trace.getFramePointers()[i], bare_stacktrace);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This Stack trace: line used to be the crash handler's raw fallback, so you still got plain PCs even if symbolization later failed. Calling writeResolvedAddress here makes that fallback depend on StackTrace::resolveAddress -> SymbolIndex::instance(), i.e. the same lazy symbol-index/DWARF machinery the symbolized trace uses. On the server path build_id() comes from daemon->build_id, so a fault during early startup or during lazy SymbolIndex initialization can now skip the only raw-address dump instead of leaving something addr2line-able behind; please keep the first dump raw, or make writeResolvedAddress fall back to writePointerHex(addr) without constructing SymbolIndex.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🕵 Adopted. The PR was merged before this landed, so the fix is in a follow-up: #120148. It adds SymbolIndex::instanceIfInitialized (an atomic pointer published after the constructor finishes, so it never touches the function-local static's guard) and StackTrace::tryResolveAddress on top of it; writeResolvedAddress uses that and falls back to writePointerHex(addr) when the index is not built yet. The server keeps printing offsets because BaseDaemon builds the index at startup for the build id, which is what test_crash_log checks.

Comment thread src/Common/StackTrace.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). label Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 047e62cbf with master cc102704b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: binary sizes, symbol sizes.

Binary sizes ⚠️
Binary Master PR Δ
programs/clickhouse-stripped 718.60 MiB 746.94 MiB +28.34 MiB (+3.94%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

15515 object files changed (-3.25 MiB total), 0 added.

Object file Master PR Δ
contrib/hive-metastore-cmake/CMakeFiles/_hivemetastore.dir/__/hive-metastore/ThriftHiveMetastore.cp… 5.81 MiB 5.68 MiB -133.43 KiB (-2.24%)
contrib/google-cloud-cpp-cmake/CMakeFiles/google_cloud_cpp_storage_protos.dir/google/storage/v2/sto… 1.76 MiB 1.71 MiB -46.26 KiB (-2.57%)
contrib/vectorscan-cmake/CMakeFiles/_vectorscan.dir/__/vectorscan/src/rose/rose_build_program.cpp.o 732.05 KiB 698.10 KiB -33.96 KiB (-4.64%)
contrib/arrow-cmake/CMakeFiles/_parquet.dir/__/arrow/cpp/src/generated/parquet_types.cpp.o 1017.44 KiB 986.57 KiB -30.87 KiB (-3.03%)
contrib/google-protobuf-cmake/CMakeFiles/_libprotobuf.dir/__/google-protobuf/src/google/protobuf/co… 365.34 KiB 336.43 KiB -28.90 KiB (-7.91%)
base/poco/Foundation/CMakeFiles/_poco_foundation.dir/src/Exception.cpp.o 356.32 KiB 327.53 KiB -28.79 KiB (-8.08%)
contrib/libhdfs3-cmake/CMakeFiles/_hdfs3.dir/ClientNamenodeProtocol.pb.cc.o 1.07 MiB 1.04 MiB -24.19 KiB (-2.22%)
contrib/icu-cmake/CMakeFiles/_icui18n.dir/__/icu/icu4c/source/i18n/messageformat2_data_model.cpp.o 192.21 KiB 168.35 KiB -23.86 KiB (-12.41%)
contrib/icu-cmake/CMakeFiles/_icui18n.dir/__/icu/icu4c/source/i18n/number_fluent.cpp.o 124.48 KiB 103.95 KiB -20.53 KiB (-16.49%)
src/CMakeFiles/dbms.dir/Interpreters/ActionsDAG.cpp.o 1.26 MiB 1.24 MiB -20.34 KiB (-1.57%)
contrib/llvm-project-cmake/CMakeFiles/LLVMAnalysis.dir/__/llvm-project/llvm/lib/Analysis/ScalarEvol… 1.32 MiB 1.30 MiB -18.73 KiB (-1.39%)
contrib/llvm-project-cmake/CMakeFiles/LLVMCore.dir/__/llvm-project/llvm/lib/IR/ConstantRange.cpp.o 273.43 KiB 256.75 KiB -16.68 KiB (-6.10%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

4087 translation units recompiled, 19113 s compile time in total, 4087 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +1112.4 s (+6%) in total before that adjustment.

Symbol sizes ⚠️
Binary Symbol Master PR Δ
programs/clickhouse _ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist new 864.07 KiB +864.07 KiB
programs/clickhouse-keeper _ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist new 864.07 KiB +864.07 KiB
programs/clickhouse-keeper wordlist new 242.38 KiB +242.38 KiB
programs/clickhouse wordlist new 242.38 KiB +242.38 KiB
programs/clickhouse rd_kafka_properties new 131.25 KiB +131.25 KiB
programs/clickhouse _ZGRN2DB12_GLOBAL__N_15nounsE_ new 102.33 KiB +102.33 KiB
programs/clickhouse-keeper _ZGRN2DB12_GLOBAL__N_15nounsE_ new 102.33 KiB +102.33 KiB
programs/clickhouse a_2 new 82.50 KiB +82.50 KiB
programs/clickhouse-keeper a_2 new 82.50 KiB +82.50 KiB

Job report

Algunenano and others added 2 commits September 11, 2026 15:07
`system.errors` keeps one mutable row per error code for the whole server, and
`FUNCTION_THROW_IF_VALUE_IS_NON_ZERO` is thrown by around 140 stateless tests, so a test running in
parallel answered the read with its own trace. A throw trace holds at most 32 frames, so a deeper
throw is cut off before the C library frames at the bottom of the thread stack and the second column
read 0. Throwing `CANNOT_DLSYM`, which no query reaches, leaves this test as the only writer of the
row it reads.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`StorageSystemStackTrace` subtracted the base of whichever object contained a frame, so an offset
into a library was stored as a bare number indistinguishable from a main executable offset, and
`addressToSymbol` on it answers with the main executable symbol at that offset. `resolveAddressForStorage`
keeps only main executable frames as file offsets, which is what `system.trace_log`, `system.crash_log`,
`system.error_log` and `system.errors` already store.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread src/Common/StackTrace.cpp Resolved
Algunenano and others added 4 commits September 14, 2026 10:12
The only test reading that column checks that some frame demangles to a `DB::` symbol, which holds
whether frames are stored as runtime addresses or as file offsets. This asserts the split itself:
frames in the main binary below the largest offset in `system.symbols`, and the frames outside it
above that bound.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The value of an `STT_TLS` symbol is an offset into the thread local block of its object, not into the
object itself, but `symbolValueIsAddress` only looked at the section index and thread local symbols
have a real one. 381 of them were published by `system.symbols` with values between 0 and 46608.

Those were the only symbols of the main binary below 2263648, the largest symbol offset of any library
loaded into the server. Without them the lowest indexed offset of the main binary is 30880872, so the
offsets of the main binary and of the libraries no longer overlap and an offset resolves in the object
it belongs to.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`addressToSymbol` does not answer with the main binary's symbol at the offset of a library frame, so
that was the wrong reason to keep runtime addresses. The right one is that a runtime address records
which object the frame is in and a bare offset does not.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread ci/jobs/compatibility_check.py Resolved

@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.

The test looks stable now. Merging

@Algunenano
Algunenano added this pull request to the merge queue Sep 15, 2026
Merged via the queue into master with commit 36b76e1 Sep 15, 2026
177 checks passed
@Algunenano
Algunenano deleted the reland-117449-pie-trace-offsets branch September 15, 2026 09:13
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 15, 2026
alexey-milovidov added a commit to shankar-iyer/ClickHouse that referenced this pull request Sep 22, 2026
The `Stack trace:` line in the fatal signal handler is the raw fallback that has to
come out even when symbolization later fails, but `writeResolvedAddress` routed it
through `StackTrace::resolveAddress`, which calls `SymbolIndex::instance` and so builds
the index on demand. `instance` is a function-local static: a fault in the thread that
was building it would leave the signal listener blocked on the static's guard before
the first dump, and a fault during startup would print nothing until ~25 MiB of ELF
parsing had been done inside the handler.

Add `SymbolIndex::instanceIfInitialized`, which hands out the index only after the
constructor has published it through an atomic pointer and never touches the guard,
and `StackTrace::tryResolveAddress` on top of it. The bare dump uses that and falls
back to the raw runtime address when the index is not built yet. A daemon builds the
index during initialization for its build id, so the server still prints the
ASLR-independent offsets that `test_crash_log` checks.

Addresses ClickHouse#119284 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants