AD2041: RustEnableSanitizersPE¶
Summary¶
| Property | Value |
|---|---|
| ID | AD2041 |
| Name | RustEnableSanitizersPE |
| Category | Security |
| Severity | Note |
| Applies to | PE (Windows, Rust compiled) |
Description¶
Rust PE binaries used for testing or development should consider using sanitizers to detect memory safety issues, undefined behavior, and other bugs.
Note: This is an informational rule. Sanitizers are typically used in testing, not production.
How It Works¶
The rule checks for sanitizer runtime symbols:
- AddressSanitizer (ASAN) symbols
- UndefinedBehaviorSanitizer (UBSAN) symbols
- ThreadSanitizer (TSAN) symbols (where supported)
Why This Matters¶
Even with Rust's memory safety guarantees, unsafe code blocks and FFI can introduce bugs. Sanitizers help catch issues in unsafe code and C/C++ dependencies.
Rust + Sanitizers¶
| Sanitizer | Catches |
|---|---|
| ASAN | Memory errors in unsafe/FFI |
| UBSAN | Undefined behavior |
| MSAN | Uninitialized memory reads |
| TSAN | Data races |
Unsafe Code Coverage¶
// Safe Rust: Memory safe by design
fn safe_function() { ... }
// Unsafe Rust: Needs extra verification
unsafe fn unsafe_function() {
// ASAN catches errors here
let ptr = alloc(...);
*ptr = value; // ASAN monitors this
}
Windows Support¶
| Sanitizer | Windows Support |
|---|---|
| ASAN | Supported (nightly) |
| UBSAN | Limited |
| TSAN | Not supported |
| MSAN | Not supported |
Performance Considerations¶
Rust sanitizers have significant overhead—use only for testing:
| Sanitizer | CPU Overhead | Memory Overhead |
|---|---|---|
| ASAN | 2-3x | 2-3x |
| UBSAN | 1.5-2x | Minimal |
| MSAN | 3x+ | 2x |
| TSAN | 5-15x | 5-10x |
Rust-specific considerations:
- Safe Rust has minimal interaction with sanitizers (already memory-safe)
- Overhead primarily affects unsafe blocks and FFI code
- Mixed Rust/C codebases see higher relative overhead
Test infrastructure impact:
| Aspect | Recommendation |
|---|---|
| CI time | 2-3x longer for ASAN tests |
| Test coverage | Focus on unsafe code paths |
| FFI testing | Always use sanitizers |
Recommended approach:
# Run sanitizer tests on unsafe-heavy crates
RUSTFLAGS="-Zsanitizer=address" cargo +nightly test -p unsafe_crate
Resolution¶
Enable sanitizers in test builds:
# Nightly Rust with ASAN
RUSTFLAGS="-Zsanitizer=address" cargo +nightly build
# Run tests with sanitizer
RUSTFLAGS="-Zsanitizer=address" cargo +nightly test