Repository: rust
Issue: Rustdoc testing fails to build with ASan enabled (in some cases)
Labels: T-rustdoc,A-sanitizers,A-doctests

While creating/maintaining C++ -> Rust bindings using different sanitizers is very useful. [Recently we added example code](https://github.com/wasmx/fizzy/pull/724) to module-level rustdoc comments for documentation purposes.

It turns out that building doc testing using ASan is not possible and likely the ASan library is not passed as a linker flag to the built rustdoc binaries.

An example source is the following (this is `src/lib.rs` and I named the create `rustdoc_asan`):
```rust
//! ```
//! extern crate rustdoc_asan;
//! fn main() {
//!   rustdoc_asan::test();
//! }
//! ```

pub fn test() {}
```

And the following is the linking error:
```
$ RUSTFLAGS="-Z sanitizer=address" cargo +nightly test 
   Compiling rustdoc-asan v0.1.0 (/Users/alex/Projects/rustdoc-asan)
    Finished test [unoptimized + debuginfo] target(s) in 3.24s
     Running /Users/alex/Projects/rustdoc-asan/target/debug/deps/rustdoc_asan-9b62e53181c3a5a1

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests rustdoc-asan

running 1 test
test src/lib.rs - (line 1) ... FAILED

failures:

---- src/lib.rs - (line 1) stdout ----
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "-L" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/var/folders/17/48vzfp593nz15n_6qczdkptr0000gn/T/rustdoctestXBL9Ea/rust_out.rust_out.7rcbfp3g-cgu.0.rcgu.o" "-o" "/var/folders/17/48vzfp593nz15n_6qczdkptr0000gn/T/rustdoctestXBL9Ea/rust_out" "/var/folders/17/48vzfp593nz15n_6qczdkptr0000gn/T/rustdoctestXBL9Ea/rust_out.33dyzt1ekirinwy8.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/alex/Projects/rustdoc-asan/target/debug/deps" "-L" "/Users/alex/Projects/rustdoc-asan/target/debug/deps" "-L" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/alex/Projects/rustdoc-asan/target/debug/deps/librustdoc_asan-9d4abc0162e9d28e.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-ab84b4dcbfd8d769.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-eef208cbe41d8926.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-eb72097f4095ae1c.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-2b7555b78fcc6194.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-304bde8dafaa97b5.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-67199606c0281c89.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-5a98e4a568df86a7.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-2422f4c55a37e801.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-3fa281be56d3f774.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-59a9c8519d392d15.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-50e4694516c58a71.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-bf6a3bfdc4a5883c.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-8357f853e5f39333.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-80c77ff1434731cf.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-7464f80379472ea6.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: Undefined symbols for architecture x86_64:
            "___asan_init", referenced from:
                _asan.module_ctor in librustdoc_asan-9d4abc0162e9d28e.rlib(rustdoc_asan-9d4abc0162e9d28e.1lf8fxf7uh11q5pf.rcgu.o)
            "___asan_version_mismatch_check_v8", referenced from:
                _asan.module_ctor in librustdoc_asan-9d4abc0162e9d28e.rlib(rustdoc_asan-9d4abc0162e9d28e.1lf8fxf7uh11q5pf.rcgu.o)
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

Couldn't compile the test.

failures:
    src/lib.rs - (line 1)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.52s

error: test failed, to rerun pass '--doc'
```

Interestingly using the leak sanitizer compiles (`RUSTFLAGS="-Z sanitizer=leak" cargo +nightly test `). While the above output is on macOS, in our project the CI runs it on Linux x86-64 where the same problem happens.

(For anyone interested, our temporary solutions is disabling the execution of rustdoc tests on the ASan build by adding the `doctest=false` flag to `Cargo.toml`. Interestingly cargo only has a `test --doc` option to only run it, but no commandline option to avoid running it.)

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
$ rustc +nightly --version --verbose
rustc 1.51.0-nightly (04caa632d 2021-01-30)
binary: rustc
commit-hash: 04caa632dd10c2bf64b69524c7f9c4c30a436877
commit-date: 2021-01-30
host: x86_64-apple-darwin
release: 1.51.0-nightly
LLVM version: 11.0.1
```