# Add AddressSanitizer and UBSan CI to a CMake Project

Yes. An agent adds a sanitizer option to your CMake build, runs the test suite under ASan and UBSan in its sandbox, and opens a PR with a CI job that does the same.

[Run this on Rush](https://cloud.getrush.ai/run/add-sanitizer-ci/cmake/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the build before it changes it. In a project like jarro2783/cxxopts, the root `CMakeLists.txt` asks for CMake 3.5 through 3.19, declares an `INTERFACE` library for the single header in `include/cxxopts.hpp`, and exposes options `CXXOPTS_BUILD_EXAMPLES`, `CXXOPTS_BUILD_TESTS`, `CXXOPTS_ENABLE_INSTALL` and `CXXOPTS_ENABLE_WARNINGS`, all on when the project is built standalone, plus `CXXOPTS_USE_UNICODE_HELP` for ICU. `cmake/cxxopts.cmake` sets C++11 without extensions and, with warnings on, adds `-Wall -Werror -Wextra -Wshadow -Weffc++` and more for GCC and Clang. `test/CMakeLists.txt` builds two Catch binaries, `options_test` and `options_test_noregex`, from `main.cpp` and `options.cpp`, two no-exception smoke tests compiled with `-fno-exceptions` on GCC and Clang, `link_test` from `link_a.cpp` and `link_b.cpp`, nested CMake runs for `find-package-test`, `add-subdirectory-test` and three pkg-config variants, and a libFuzzer target on Clang on Linux. It also finds that the two Catch tests are already compiled with `-fsanitize=undefined -fno-sanitize-recover=undefined` on GCC and Clang outside Windows, with a comment naming `std::abs(INT64_MIN)` as the reason, so UBSan exists but only on those two targets and only in whatever build type CI uses.

The workflows show that build type. `.github/workflows/cmake.yml` builds `Release` on a matrix of `ubuntu-22.04` and `ubuntu-24.04` with `g++-9`, `g++-10` and `clang++`, on macOS, and on Windows with ClangCl, and runs `ctest` from `build/test`; `build.yml` does a plain `make` and `ctest` on `ubuntu-latest`; `cifuzz.yml` runs the OSS-Fuzz harness for 300 seconds on pull requests. No job builds with AddressSanitizer, and no job builds with debug information, so a sanitizer report in CI would have no symbols.

The agent adds an option `CXXOPTS_ENABLE_SANITIZERS`, off by default, and a function in `cmake/cxxopts.cmake` that, for GCC and Clang, appends `-fsanitize=address,undefined`, `-fno-omit-frame-pointer` and `-fno-sanitize-recover=all` to the compile and link flags of the whole project, so the examples, `link_test` and the no-exception tests are instrumented too, while the existing per-target UBSan lines stay as they are. It leaves MSVC alone, since the flags differ and the project's Windows job uses ClangCl. It notes that the nested test runs forward only the compiler, standard and build type through `--build-options`, so those child builds stay uninstrumented unless `CMAKE_CXX_FLAGS` is forwarded as well, and it makes that choice explicit in the pull request. Then it adds a `sanitizers` job to `cmake.yml` on `ubuntu-24.04` for `g++` and `clang++`: configure with `-DCMAKE_BUILD_TYPE=Debug -DCXXOPTS_ENABLE_SANITIZERS=ON`, build, and run `ctest --output-on-failure` with `ASAN_OPTIONS` and `UBSAN_OPTIONS` set to print stack traces and fail on the first report.

In its sandbox it installs GCC and Clang, configures and builds with the option on and off, runs the test suite under both compilers, and checks that the instrumentation is real: the test binaries reference `__asan_init`, and a throwaway out-of-bounds program compiled with the same flags aborts with a report. If the sandbox refuses LeakSanitizer's ptrace use, it runs there with `detect_leaks=0` and says so, since the GitHub runner will not need it. It also confirms `-Werror` still passes under the sanitizer flags and that the fuzzer target, which sets `-fsanitize=fuzzer` on its own, still links with Clang. Then it opens the pull request and watches the new job run on it until it passes.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add AddressSanitizer and UndefinedBehaviorSanitizer builds to this CMake project and open a pull request. Read CMakeLists.txt, cmake/cxxopts.cmake, test/CMakeLists.txt, the nested test projects under test/, and every workflow under .github/workflows first. Add an option, off by default, that for GCC and Clang compiles and links the whole project with -fsanitize=address, the matching UBSan -fsanitize flag, -fno-omit-frame-pointer and -fno-sanitize-recover=all, keeping the UBSan flags the test CMakeLists already sets on its Catch targets. Say in the pull request whether the nested find-package and pkg-config test builds receive the flags. Add a job to the CMake workflow that builds Debug with the option on for g++ and clang++ on ubuntu-24.04 and runs ctest --output-on-failure with ASAN_OPTIONS and UBSAN_OPTIONS that print stack traces. In the sandbox, build with the option on and off, run the full test suite under both compilers, prove the instrumentation is active, and keep the output. Then open the pull request and make sure the new job passes on it.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch that edits `CMakeLists.txt` and `cmake/cxxopts.cmake` for the new option and its flags, adds the `sanitizers` job to `.github/workflows/cmake.yml`, and documents the option in the README next to the existing ones. The description quotes the configure lines, the `ctest` summary for each compiler with and without the option, the `__asan_init` check and the aborting probe program's report, and states plainly which test targets are instrumented and which nested builds are not. The job's first run on the pull request shows as a check beside the existing matrix.

Before merging, a reviewer checks that a sanitized Debug build is what they want in CI rather than `RelWithDebInfo`, whether `-fno-sanitize-recover=all` is too strict for code paths that intentionally rely on defined-but-flagged behaviour, and whether the nested test builds should be instrumented too. They also decide whether the option should be on by default for standalone builds like the other options.

## Run it on every C++ repository without sanitizer builds

A team with several CMake projects sends one `POST /o/{org}/sessions` per repository, this prompt and the repository in `resources`. `GET /o/{org}/github` lists every repository the Rush GitHub App installation can reach, so a script can start a run for each one with a `CMakeLists.txt` and no `-fsanitize=address` anywhere in its CMake files or workflows. Each run reads that project's own options and test layout, so a project with a `tests` target under CTest and one with a custom runner each get a job that fits, and each opens its own pull request.

Set a webhook URL on the API key and Rush posts `run.completed` or `run.failed` when a run's turn finishes, with `session_url` so your server can read the pull request link. Send a fresh UUID as `request_id` with each request so a retry returns the run already started.

## Why run it on Rush?

A sanitizer job is easy to add and easy to get wrong silently: flags that reach the compile step but not the link step, a `Release` build that strips the frames a report needs, nested builds that quietly ignore the flags, or a runner that cannot ptrace for LeakSanitizer. On Rush the agent builds and runs the suite under both compilers in its own sandbox, proves the instrumentation with a program that must abort, and then reads the first real run of the job on the pull request, so the result is a passing job and not a YAML file.

Every configure line, build log and `ctest` summary stays in the run's record, and nothing becomes a required check until a person merges. The same request adds sanitizer coverage to every C++ repository the team owns, one run and one pull request each.

## Frequently asked questions

### Does it slow down the existing CI matrix?

No. The existing jobs keep building `Release` with the option off. The sanitized build is a separate job on one runner with two compilers, and the option defaults to off, so local builds and downstream projects that add this header-only library with `add_subdirectory` are untouched.

### What about MSVC and Windows?

The function adds flags only for GCC and Clang. The project's Windows job uses ClangCl, where the flag syntax and runtime differ, so the pull request leaves Windows out of the sanitizer job and says so rather than adding a job it could not verify in a Linux sandbox.

### Will it change the UBSan flags already on the Catch tests?

No. Those lines stay, so the parser tests remain under UBSan in every build on GCC and Clang. The new option adds AddressSanitizer and extends UBSan to the examples, the link test and the no-exception tests when it is on.

### Can it also add MemorySanitizer or ThreadSanitizer?

MemorySanitizer needs an instrumented standard library and ThreadSanitizer cannot be combined with AddressSanitizer, so each would be its own build and job. Ask for either in a follow-up message to the same run, and the agent adds a second option and job on the same pattern.

## More CMake jobs

- [Fix "Compatibility with CMake < 3.5 Has Been Removed from CMake" After a CMake 4 Upgrade](https://getrush.ai/run/fix-compatibility-with-cmake-3-5-has-been-removed/cmake/)
- [Review Pull Requests on a CMake C++ Library](https://getrush.ai/run/review-pull-requests/cmake/)
- [Update GitHub Actions in a CMake Project's CI](https://getrush.ai/run/update-github-actions/cmake/)

## More jobs like this: Hand off a long task

- [Add an SBOM to a Gradle Build's CI](https://getrush.ai/run/add-sbom-to-ci/gradle/)
- [Add an SBOM to CI for a Maven Project](https://getrush.ai/run/add-sbom-to-ci/maven/)
- [Add an SBOM to the Release of a Cobra CLI](https://getrush.ai/run/add-sbom-to-release/cobra/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
