mirror of
https://github.com/1dot13/source.git
synced 2026-08-26 14:30:26 +02:00
Add AddressSanitizer support for clang-cl builds
Select the clang-cl-asan CMake preset (RelWithDebInfo, clang-cl, ADDRESS_SANITIZER=ON) to instrument first-party code with AddressSanitizer. The wiring lives in cmake/AddressSanitizer.cmake; SANITIZERS.md tells how to add the clang-cl tools, build, and read the report. Details: - Add the clang-cl-asan preset so the asan build is one selection in Visual Studio, and a base for a CMakeUserPresets.json to inherit. - Instrument first-party code only; the vendored libraries keep default flags. - Use the release CRT and disable MSVC-STL container annotations, so instrumented and un-instrumented TUs stay compatible. - Pass /bigobj to the TUs asan inflates past the COFF section cap. - Link the asan runtime for clang-cl (lld-link does not infer it). - Stub Bink into the exe: retail binkw32.dll cannot load in an asan process (its image base is the 32-bit shadow), so compile no-op exports instead. - Route the asan report to gamedir/asan.report.<pid>, since every app is a WIN32 GUI app with no console to receive the default stderr report. - Opt functions with 32-bit inline __asm out of instrumentation with cmake/asan-ignorelist.txt, one function at a time (asan reserves a register the asm needs). The rest of each translation unit stays instrumented. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
committed by
majcosta
co-authored by
Claude Opus 4.8
parent
69764e4459
commit
1bed0047ea
@@ -0,0 +1,53 @@
|
||||
# AddressSanitizer
|
||||
|
||||
AddressSanitizer (asan) finds memory errors at runtime: out-of-bounds accesses,
|
||||
use-after-free, and similar. It reports the exact faulting access.
|
||||
|
||||
## Requirements
|
||||
|
||||
asan requires `C++ Clang tools for Windows` (MSVC's AddressSanitizer does not
|
||||
support -fsanitize-ignorelist).
|
||||
|
||||
It is NOT installed by default . You can install it via the Visual Studio
|
||||
Installer.
|
||||
|
||||
## Build and run
|
||||
|
||||
Either use the included `clang-cl-asan` preset, or inherit from it in your own
|
||||
`CMakeUserPresets.json`
|
||||
|
||||
## Debug from Visual Studio
|
||||
|
||||
asan needs `clang_rt.asan_dynamic-i386.dll` at run time. MSVC ships this DLL
|
||||
with the x86 tools, but Visual Studio does not add that folder to the debugger
|
||||
PATH. To start the exe with F5, add the tools folder to the
|
||||
debugger PATH:
|
||||
|
||||
1. Access the CMake Targets View in Solution Explorer
|
||||
2. Right-click the exe target (for example `JA2.exe`) and select `Add Debug Configuration`.
|
||||
Visual Studio creates `launch.vs.json` in the `.vs` folder and adds a configuration with
|
||||
the correct `projectTarget`.
|
||||
3. Add an `env` block to that configuration:
|
||||
|
||||
```json
|
||||
"env": {
|
||||
"PATH": "C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\VC\\Tools\\MSVC\\14.51.36231\\bin\\Hostx64\\x86;${env.PATH}"
|
||||
}
|
||||
```
|
||||
|
||||
Set the first path to the `bin\Hostx64\x86` folder of your MSVC tools. To find
|
||||
it, search your Visual Studio installation for `clang_rt.asan_dynamic-i386.dll`.
|
||||
Your edition (`Community`), MSVC version (`14.51.36231`), and host (`Hostx64` or
|
||||
`Hostx86`) can differ.
|
||||
|
||||
## Reading the report
|
||||
|
||||
Every app is a GUI app with no console, so asan writes the report to a file by
|
||||
default:
|
||||
|
||||
```
|
||||
gamedir/asan.report.<pid>
|
||||
```
|
||||
|
||||
When asan finds an error, the run stops and the report names the faulting access.
|
||||
Set the `ASAN_OPTIONS` environment variable to change asan behaviour.
|
||||
Reference in New Issue
Block a user