Skip to content

Add ENABLE_UBSAN build option - #13610

Open
bryancall wants to merge 5 commits into
apache:masterfrom
bryancall:ubsan-build-option
Open

Add ENABLE_UBSAN build option#13610
bryancall wants to merge 5 commits into
apache:masterfrom
bryancall:ubsan-build-option

Conversation

@bryancall

@bryancall bryancall commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Adds UndefinedBehaviorSanitizer as a build option next to the existing ENABLE_ASAN and ENABLE_TSAN.

Why this is not part of the asan/tsan either-or chain

ASAN replaces the allocator and TSAN replaces the thread runtime, so those two are mutually exclusive and the existing code enforces that with an if/elseif. UBSan only instruments arithmetic, shifts, and type loads, so it composes with either of them. The new block sits outside that chain, which is what lets dev-asan-ubsan produce -fsanitize=address,undefined in a single build.

Presets

A hidden ubsan preset matching the shape of the existing hidden asan and tsan presets, plus two visible ones:

cmake --preset dev-ubsan
cmake --preset dev-asan-ubsan

Two deliberate choices

vptr is excluded. That check needs a matching type_info at every polymorphic access, and a plugin loaded with dlopen does not reliably share type identity with the main image, so it reports the plugin boundary rather than a real defect.

Findings are non-fatal, so a single run reports every distinct site instead of stopping at the first. Setting UBSAN_OPTIONS=halt_on_error=1 at runtime aborts instead, which is what a gating job would want.

No CI job turns this on

Enabling it anywhere would be a separate change. The option is inert until someone asks for one of the presets.

What it reports on the current tree

For context on whether the option earns its place, it finds real defects. One is a hasher buffer declared as a plain char array with a polymorphic object constructed into it, which makes every access through it misaligned on the URL hash and cache key path. Another is the plugin entry point, declared at the call site as void (*)(int, char **) while every plugin in the tree defines TSPluginInit(int, const char *argv[]).

Those fixes are separate pull requests. This one only adds the option so the findings are reproducible.

Testing

Configured and built with clang 22 on Fedora 44 using dev-ubsan with autest and experimental plugins enabled. Build and install both clean, ctest 179/179 passing, and the resulting binary links the UBSan runtime handlers.

Adds UndefinedBehaviorSanitizer as a build option alongside the existing
ENABLE_ASAN and ENABLE_TSAN. UBSan instruments arithmetic, shifts, and type
loads rather than replacing the allocator or thread runtime, so unlike asan
and tsan it composes with them rather than being mutually exclusive.

Three presets: a hidden ubsan preset plus dev-ubsan and dev-asan-ubsan.

vptr is excluded because a plugin loaded with dlopen does not reliably share
type identity with the main image, so the check reports the plugin boundary
rather than a real defect.
Copilot AI lite review requested due to automatic review settings September 1, 2026 14:38
@bryancall bryancall added Build work related to build configuration or environment CMake work related to CMakes scripts or issues New Feature labels Sep 1, 2026
@bryancall bryancall self-assigned this Sep 1, 2026
@bryancall bryancall added this to the 11.0.0 milestone Sep 1, 2026
@bryancall bryancall added the UBSan label Sep 1, 2026

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Adds an ENABLE_UBSAN build option and corresponding CMake presets to enable UndefinedBehaviorSanitizer alongside existing ASAN/TSAN support.

Changes:

  • Introduces ENABLE_UBSAN CMake option.
  • Adds a UBSan CMake configuration block (intentionally outside the ASAN/TSAN mutual-exclusion chain) with vptr excluded.
  • Adds hidden ubsan preset and visible dev-ubsan / dev-asan-ubsan presets.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
CMakePresets.json Adds UBSan-related presets to enable ENABLE_UBSAN (including a combined ASAN+UBSan dev preset).
CMakeLists.txt Adds ENABLE_UBSAN option and applies UBSan compile/link flags when enabled.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt
The checks are instrumentation, but they call into a runtime library that
some distributions package separately from the compiler. Fedora ships
libasan with gcc but puts libubsan in its own package, so a GCC build with
ENABLE_UBSAN compiles for a while and then stops at

    ld.bfd: cannot find libubsan.so.1.0.0: No such file or directory

with nothing to connect that to the option that caused it. Establish the
runtime at configure time instead and name the missing piece.
Copilot AI review requested due to automatic review settings September 2, 2026 15:24

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt Outdated
Two corrections to the flag set.

The comment claimed findings are non-fatal, but nothing in the build asked
for that. Most checks recover by default, which is why a run reports every
site rather than stopping at the first, but the default set differs between
compilers and a few checks cannot recover at all: clang rejects
-fsanitize-recover=unreachable outright. Ask for recovery explicitly so the
behavior does not depend on the compiler, and say in the comment which
checks still abort.

-fno-sanitize=vptr is compile time instrumentation and does nothing on the
link line, so drop it there. Only -fsanitize=undefined is needed at link,
to pull in the runtime.
Copilot AI review requested due to automatic review settings September 2, 2026 18:48

Copilot AI left a comment

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.

🟢 Approval recommended

The change is isolated to build configuration/presets and appears technically sound, with only minor messaging/comment clarity nits identified.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt Outdated
Two comment corrections. The rationale named only asan as something UBSan
composes with, but the block sits outside the asan/tsan chain precisely so
it works with either, so name both and note that only the asan pairing has
a preset.

The configure check fails either because the toolchain does not know
-fsanitize=undefined or because it does and cannot link the runtime, but
the message described only the second. Cover both and keep the libubsan
package note as the GCC specific hint. Renamed the result variable to
match, since it is not only about the runtime.
Copilot AI review requested due to automatic review settings September 2, 2026 19:48

Copilot AI left a comment

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.

🟡 Changes recommended

The new configure-time fatal error message gives distro-specific package guidance (“libubsan”) that can be misleading on non-Fedora platforms, so it should be made distribution-agnostic.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread CMakeLists.txt Outdated
The message named the GCC runtime as "the libubsan package", which is the
Fedora spelling. Debian and Ubuntu call it libubsan1, so naming one
distribution's convention as though it were universal sends readers on the
wrong errand. Say that the runtime ships separately under a name that
varies instead, which stays actionable without being wrong anywhere.
Copilot AI review requested due to automatic review settings September 2, 2026 20:11

Copilot AI left a comment

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.

🟢 Approval recommended

The changes are self-contained to build configuration/presets and include appropriate configure-time validation to avoid late link failures when UBSan is enabled.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@bryancall

Copy link
Copy Markdown
Contributor Author

[approve ci clang-analyzer]

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

Labels

Build work related to build configuration or environment CMake work related to CMakes scripts or issues New Feature UBSan

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants