Pass ENABLE_ADDRESS_SANITIZER=YES to xcodebuild test, then read the first ASan report from the access stack down. AddressSanitizer (ASan) is compile-time instrumentation from Clang, not a separate download. Once it is on, the runtime checks every memory operation and, on the first illegal access (a heap overflow, a use-after-free, a stack smash), prints a report and stops the process before the corruption spreads. The Clang reference lists the full detection set and suppression semantics, and Apple's page on diagnosing memory, thread and crash issues early covers enabling it in a scheme.
What does AddressSanitizer detect?
| Violation | What it finds |
|---|---|
| Heap buffer overflow | Read or write past the end of a heap allocation |
| Use-after-free | Access to memory already deallocated |
| Stack buffer overflow | Write past the end of a local array |
| Global buffer overflow | Write past a global's allocation |
| Use-after-return | Access to a local after its function returned |
| Use-after-scope | Access to a local outside its lexical scope |
ASan does not catch every leak (use the Memory Graph Debugger or LeakSanitizer) and does not catch data races (use ThreadSanitizer). For a use-after-free or buffer overflow that triggers only on a few test paths, it is the right first tool.
How do I enable ASan?
Pick one of three methods. Each uses a documented Xcode option.
1. Scheme toggle, persistent. Edit Scheme → Test → Diagnostics → check "Address Sanitizer". The scheme file carries the setting, so every later xcodebuild test -scheme <that scheme> uses it. This is Apple's documented path.
2. Build-setting override, one run.
xcodebuild test \
-workspace "$WORKSPACE" \
-scheme "$SCHEME" \
-destination "platform=iOS Simulator,name=iPhone 16 Pro" \
ENABLE_ADDRESS_SANITIZER=YES \
2>&1 | tee asan-raw.log
ENABLE_ADDRESS_SANITIZER=YES is the build-setting form current Xcode recognises. Pass your own workspace and scheme values, and do not run this against a scheme your project lacks.
3. xcconfig for CI. Add ENABLE_ADDRESS_SANITIZER = YES to a dedicated Test xcconfig and reference it from the test configuration. This keeps the Release xcconfig untouched.
How do I run the suite and read the report?
The script below is an untested template, not a benchmark. Substitute real values and check xcodebuild -help on your installed Xcode for the exact flags before using it in CI.
#!/usr/bin/env bash
# asan-test.sh SCHEME WORKSPACE "iPhone 16 Pro"
set -euo pipefail
SCHEME="${1:?scheme required}"
WORKSPACE="${2:?workspace required}"
DEVICE="${3:-iPhone 16 Pro}"
LOG="asan-$(date +%Y%m%d%H%M).log"
# ASan violations make tests fail; keep the exit non-zero but inspect the log.
set +e
xcodebuild test \
-workspace "$WORKSPACE" \
-scheme "$SCHEME" \
-destination "platform=iOS Simulator,name=$DEVICE" \
ENABLE_ADDRESS_SANITIZER=YES \
2>&1 | tee "$LOG"
BUILD_STATUS=${PIPESTATUS[0]}
set -e
echo "==> xcodebuild exit: $BUILD_STATUS"
echo "==> AddressSanitizer findings:"
grep -E "ERROR: AddressSanitizer|READ of size|WRITE of size|heap-use-after-free|heap-buffer-overflow|stack-buffer-overflow|SUMMARY: AddressSanitizer" "$LOG" \
|| echo "No ASan findings in log."
exit "$BUILD_STATUS"
The script preserves the real xcodebuild exit code, so a crash is never recorded as a passing run. A use-after-free report has four parts, best read in this order: the violation type and address, the access stack (where you touched the memory), the free stack (where it was released) and the allocation stack (where it came from). Start at the top frame of the access stack. That is your line.
How much slower does a run get?
ASan instruments every load and store, so CPU use and memory rise under test. The Clang manual puts the slowdown at roughly 2x on typical C/C++ code, and it varies by workload. Time your own suite. VM vCPUs are shared rather than reserved, so timings on a VM move with the host's load; for stable comparisons use one machine type.
What about false positives and suppressions?
Some ARC bridging and Objective-C runtime frames trigger ASan reports unrelated to your code. Clang's docs describe the suppression file format and the ASAN_OPTIONS=suppressions=… entry point. Keep suppressions tight: interceptor_via_fun:objc_msgSend is a library interceptor, while a suppression in your own code hides the bug you came for. Store the suppression file in the repo so the rented Mac and your laptop behave identically.
Can the Intel Mac VM build with AddressSanitizer?
The Intel Mac VM ($25 a month, $2 a day) runs macOS Sequoia, where Apple lists Xcode 26.0 to 26.3. It builds and runs Simulator tests with ASan for projects that stay on Xcode 26.3 or earlier. Use the Apple Silicon Mac VM ($39 a month, $6 a day) for Xcode 26.4 or later or Apple-silicon-only simulator runtimes; check the macOS version shown before purchase against Apple's Xcode requirements.
A rough year for a developer running ASan daily, using published prices checked on 2026-11-10:
- Intel Mac VM: 12 × $25 = $300.
- Apple Silicon Mac VM: 12 × $39 = $468. If you only need ASan before releases, four days a month is 4 × $6 = $24.
- CloudMac M4, 16 GB, 256 GB: $99.99 a month, or $999 a year on the annual plan. That is a whole Mac mini; the Figment VMs are shared VMs, so compare memory and disk as well as price.
Pair ASan with the Memory Graph Debugger once corruption is out of the way, add realistic network conditions with the Network Link Conditioner guide, and wire the run into CI with the GitHub Actions self-hosted runner guide. Plans are at /mac-hosting.