Skip to content

Commit

Permalink
sanitizers: Add documentation for stable sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
rcvalle committed Dec 20, 2024
1 parent a7576ae commit cbf6926
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/doc/rustc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
- [Checking Conditional Configurations](check-cfg.md)
- [Cargo Specifics](check-cfg/cargo-specifics.md)
- [Exploit Mitigations](exploit-mitigations.md)
- [Sanitizers](sanitizers.md)
- [Symbol Mangling](symbol-mangling/index.md)
- [v0 Symbol Format](symbol-mangling/v0.md)
- [Contributing to `rustc`](contributing.md)
43 changes: 43 additions & 0 deletions src/doc/rustc/src/codegen-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,49 @@ enabled. It takes one of the following values:
* `y`, `yes`, `on`, `true` or no value: enable rpath.
* `n`, `no`, `off` or `false`: disable rpath (the default).

## sanitize

Sanitizers are a set of compiler-based runtime error detection tools that
instrument programs to detect bugs during execution. They work by instrumenting
code at compile time and runtime to monitor program behavior and detect specific
classes of errors at runtime. Sanitizers enable precise, low-overhead runtime
bug detection, improving software reliability and security.

This option allows for use of one or more of these sanitizers:

* [AddressSanitizer
(ASan)](https://doc.rust-lang.org/rustc/sanitizers.html#addresssanitizer):
Detects memory errors (e.g., buffer overflows, use after free).
* [LeakSanitizer
(LSan)](https://doc.rust-lang.org/rustc/sanitizers.html#leaksanitizer):
Detects memory leaks either as part of AddressSanitizer or as a standalone
tool.

These are the valid values for this option for targets that support one or more
of these sanitizers:

| Target | Sanitizers |
|-----------------------------|-----------------|
| aarch64-unknown-linux-gnu | address, leak |
| i686-pc-windows-msvc | address |
| i686-unknown-linux-gnu | address |
| x86_64-apple-darwin | address, leak |
| x86_64-pc-windows-msvc | address |
| x86_64-unknown-linux-gnu | address, leak |

The quality of the Sanitizers implementation and support varies across operating
systems and architectures, and relies heavily on LLVM implementation--they are
mostly implemented in and supported by LLVM.

Using a different LLVM or runtime version than the one used by the Rust compiler
is not supported. Using Sanitizers in mixed-language binaries (also known as
“mixed binaries”) is supported when the same LLVM and runtime version is used by
all languages.

For more information about the Sanitizers, see the [Sanitizers
chapter](https://doc.rust-lang.org/rustc/sanitizers.html) of [The rustc
book](https://doc.rust-lang.org/rustc/).

## save-temps

This flag controls whether temporary files generated during compilation are
Expand Down
95 changes: 95 additions & 0 deletions src/doc/rustc/src/sanitizers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Sanitizers

## Introduction

Sanitizers are a set of compiler-based runtime error detection tools that
instrument programs to detect bugs during execution. They work by instrumenting
code at compile time and runtime to monitor program behavior and detect specific
classes of errors at runtime. Sanitizers enable precise, low-overhead runtime
bug detection, improving software reliability and security.

This option allows for use of one or more of these sanitizers:

* [AddressSanitizer (ASan)](#addresssanitizer): Detects memory errors (e.g.,
buffer overflows, use after free).
* [LeakSanitizer (LSan)](#leaksanitizer): Detects memory leaks either as part
of AddressSanitizer or as a standalone tool.

These are the valid values for this option for targets that support one or more
of these sanitizers:

| Target | Sanitizers |
|-----------------------------|-----------------|
| aarch64-unknown-linux-gnu | address, leak |
| i686-pc-windows-msvc | address |
| i686-unknown-linux-gnu | address |
| x86_64-apple-darwin | address, leak |
| x86_64-pc-windows-msvc | address |
| x86_64-unknown-linux-gnu | address, leak |

## AddressSanitizer

AddressSanitizer (ASan) detects memory errors by instrumenting code at compile
time and runtime to mark regions around allocated memory (i.e., red zones) as
unaddressable (i.e., poisoned), quarantine and mark deallocated memory as
unaddressable, and add checks before memory accesses. It uses a shadow memory
mapping to store metadata information about whether a memory region is
addressable. It can detect:

* Heap-based buffer overflows, Stack-based buffer overflows, and other variants
of out-of-bounds reads and writes.
* Use after free, double free, and other variants of expired pointer dereference
(also known as “dangling pointer”).
* Initialization order bugs (such as [“Static Initialization Order
Fiasco”](https://en.cppreference.com/w/cpp/language/siof)).
* Memory leaks.

AddressSanitizer uses both instrumentation at compile time and runtime. It is
recommended to recompile all code using AddressSanitizer for best results. If
parts of the compiled code are not instrumented, AddressSanitizer may not detect
certain memory errors or detect false positives.

AddressSanitizer increases memory usage and also impacts performance due to the
red zones and shadow memory mapping, and the added checks before memory
accesses. AddressSanitizer and its runtime are not suitable for production use.

For more information, see the [AddressSanitizer
documentation](https://clang.llvm.org/docs/AddressSanitizer.html).

## LeakSanitizer

LeakSanitizer (LSan) detects memory leaks either as part of AddressSanitizer or
as a standalone tool by instrumenting code at runtime to track all memory and
thread management functions (i.e., interceptors), and searching for memory that
remain allocated but are no longer reachable by any references in the program,
at program termination or during specific checkpoints.

LeakSanitizer can detect:

* Memory allocated dynamically (e.g., via `malloc`, `new`) that are not freed or
deleted and is no longer referenced in the program (i.e., directly leaked
memory).
* Memory allocated dynamically that is referenced by another memory that are not
freed or deleted and is no longer referenced in the program (i.e., indirectly
leaked memory).

LeakSanitizer does not use instrumentation at compile time and works without
recompiling all code using LeakSanitizer.

LeakSanitizer impacts performance due to the interceptors and the checks for
memory leaks at program termination. LeakSanitizer and its runtime are not
suitable for production use.

For more information, see the [LeakSanitizer
documentation](https://clang.llvm.org/docs/LeakSanitizer.html).

## Disclaimer

The quality of the Sanitizers implementation and support varies across operating
systems and architectures, and relies heavily on LLVM implementation--they are
mostly implemented in and supported by LLVM.

Using a different LLVM or runtime version than the one used by the Rust compiler
is not supported. Using Sanitizers in mixed-language binaries (also known as
“mixed binaries”) is supported when the same LLVM and runtime version is used by
all languages.

0 comments on commit cbf6926

Please sign in to comment.