Field Notes: Cache Correctness & Poisoning
A build cache makes one promise: a hit gives exactly the result a fresh build would have given. Every cache bug is a broken promise, and it's the worst kind of bug: the build is green, the binary is wrong, and the problem disappears when someone clears the cache. These notes collect the ways teams break the promise, and how to keep it.
1. The key must cover every input
A cache key that leaves out an input that matters returns stale results whenever that input changes. ccache's defaults are conservative; the trouble starts when people relax them to get more hits. Here a "compiler upgrade" happens behind ccache's back while compiler_check = none:
mkdir -p stale && cd stale
make_compiler() { # a wrapper standing in for a compiler whose output changes with its version
printf '#!/bin/sh\nexec g++ -DCOMPILER_VERSION=%s "$@"\n' "$1" > mycc && chmod +x mycc
}
printf '#include <cstdio>\nint main() { std::printf("built by compiler %%d\\n", COMPILER_VERSION); }\n' > main.cpp
run() { ccache ./mycc -c main.cpp -o main.o && g++ main.o -o app && ./app; }
for check in mtime none; do
echo "=== compiler_check=$check"
ccache -C > /dev/null; export CCACHE_COMPILERCHECK=$check
make_compiler 1 && run
sleep 1 && make_compiler 2 && run
done=== compiler_check=mtime
built by compiler 1
built by compiler 2
=== compiler_check=none
built by compiler 1
built by compiler 1
With none, ccache never looked at the compiler, so the "upgraded" compiler's compile was answered with the old object. In real life that's an ABI change, a security fix or a code-generation bug fix silently not applied. The same failure happens with:
- Undeclared inputs in custom build steps: a code generator that reads a config file not listed in
DEPENDS, or Bazel rules reading files outside their declared inputs (which sandboxing exists to prevent). - Environment variables that change compiler behaviour but aren't hashed (ccache hashes a few well-known ones; the variables a wrapper script reads are invisible to it).
- Too much sloppiness:
system_headerswhile upgrading the libc or SDK;time_macrosfor a build that really embeds the build time.
Rule: relax a check only for a measured miss, document why, and pick the narrowest option (string:gcc-13.3.0 or content instead of none).
2. Never cache non-deterministic outputs
If the same inputs can produce different outputs, a cache turns randomness into "whatever the first build produced", shared by everyone. Typical sources:
| Source | Example | Fix |
|---|---|---|
| Timestamps | __DATE__, __TIME__, generator writing the current time, archive mtimes |
SOURCE_DATE_EPOCH, -Wdate-time, tar --mtime, ar D |
| Absolute paths | __FILE__, debug info, embedded build dirs |
-ffile-prefix-map, base_dir |
| Unordered iteration | a generator writing a hash map in iteration order, file(GLOB) order |
sort before writing |
| Parallelism | output order of parallel tests baked into a report | sort, or don't cache reports |
| Randomness | generated UUIDs, hash seeds (PYTHONHASHSEED) |
fixed seeds, or derive IDs from content |
For a build-system cache (Bazel, Gradle, REAPI) non-determinism also causes cache misses downstream: an action whose output digest changes every time invalidates every action that consumes it. Tools to find it: build twice and diff (diffoscope), Bazel's --execution_log_* comparisons, Gradle's build-scan comparisons.
3. Cache poisoning: who can write?
A shared cache is shared trust. If an attacker (or a buggy build) can write an entry under a key that other builds will read, every later hit runs their object code: no compile, no review, no trace in the source. That's cache poisoning, and it's a real supply-chain attack path.
- Only trusted builds write. CI builds of protected branches write to the shared cache; pull-request builds, especially from forks, are read-only (
remote_storage = …|read-onlyin ccache,SCCACHE_GCS_RW_MODE=READ_ONLY,--remote_upload_local_results=falsein Bazel). Developer machines read only. - Separate caches per trust level. Release builds shouldn't read entries written by anything less trusted than release builds; many teams give release pipelines their own cache or none at all.
- Scope CI caches by branch. GitHub Actions already prevents a branch from writing to the default branch's cache scope; keep it that way and avoid restore-keys that fall back to arbitrary branches for release jobs.
- Authenticate and audit access to cache servers like any artifact repository: tokens per pipeline, logs of writes.
- Key poisoning by collision is a design flaw to avoid: keys must be cryptographic hashes of all inputs (ccache uses BLAKE3 for keys, REAPI SHA-256). A key built from "branch name + file name" invites one build to overwrite another's entry.
4. Make the cache disposable
- Clearing the cache must always be safe and only cost time. If clearing "fixes" a bug, you found a key problem: find the missing input instead of clearing more often.
- Version your cache namespaces (
CCACHE_NAMESPACE, an S3 key prefix, a CI cache key prefix likev3-), so a bad generation can be abandoned without deleting anything. - Release builds can skip the cache entirely (
CCACHE_DISABLE=1,--noremote_accept_cached) and check that the result matches a cached build: a cheap, continuous correctness test.
Test yourself
With compiler_check=none, the compiler was upgraded and the program still printed 'compiler 1'. Why?
- The upgrade failed
- The compiler wasn't part of the key, so the old object was returned as a hit
- ccache disabled itself
- The linker cached the result
Which builds should be allowed to write to a shared remote cache?
- All builds, for maximum hits
- Only trusted CI builds of protected branches
- Pull requests from forks
- Developer laptops
Clearing the cache made a mysterious bug disappear. What does that indicate?
- The cache is too small
- A missing input in the cache key (or a non-deterministic output) that must be fixed
- Nothing; clear it regularly
- The compiler is broken