Sanitizers support - Rust Compiler Development Guide (original) (raw)

Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust Compiler Development Guide

Sanitizers support

The rustc compiler contains support for following sanitizers:

How to use the sanitizers?

To enable a sanitizer compile with -Z sanitizer=... option, where value is one of address, cfi, hwaddress, kcfi, leak, memory or thread. For more details on how to use sanitizers, please refer to the sanitizer flag in The Unstable Book.

How are sanitizers implemented in rustc?

The implementation of sanitizers (except CFI) relies almost entirely on LLVM. The rustc is an integration point for LLVM compile time instrumentation passes and runtime libraries. Highlight of the most important aspects of the implementation:

[build]  
sanitizers = true  

The runtimes are placed into target libdir.

Testing sanitizers

Sanitizers are validated by code generation tests intests/codegen-llvm/sanitize*.rs and end-to-end functional tests intests/ui/sanitizer/ directory.

Testing sanitizer functionality requires the sanitizer runtimes (built whensanitizer = true in bootstrap.toml) and target providing support for particular a sanitizer. When a sanitizer is unsupported on a given target, sanitizer tests will be ignored. This behaviour is controlled by compiletest needs-sanitizer-* directives.

Enabling a sanitizer on a new target

To enable a sanitizer on a new target which is already supported by LLVM:

  1. Include the sanitizer in the list of supported_sanitizers in the target definition.rustc --target .. -Zsanitizer=.. should now recognize the sanitizer as supported.
  2. Build the runtime for the target and include it in the libdir.
  3. Teach compiletest that your target now supports the sanitizer.Tests marked with needs-sanitizer-* should now run on the target.
  4. Run tests ./x test --force-rerun tests/ui/sanitize/ to verify.
  5. –enable-sanitizers in the CI configuration to build and distribute the sanitizer runtime as part of the release process.

Additional Information