error: the option `Z` is only accepted on the nightly compiler is not solved

error: the option `Z` is only accepted on the nightly compiler is not solved

我正在尝试通过本手册在 Rust 中使用 Address Sanitizer(https://github.com/japaric/rust-san), 但是当我使用命令 RUSTFLAGS="-Z sanitier=address" cargo rustc -- --emit=llvm-ir 构建此代码时,它会导致这样的错误;

错误:未能运行rustc了解目标特定信息

原因: 进程未成功退出:rustc - --crate-name ___ --print=file-names -Z sanitier=address --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg(退出状态:1) --- 标准错误 错误:选项 Z 仅在夜间编译器上接受'

我认为“选项 Z 只在夜间编译器上被接受”是个问题,所以我设置了夜间编译器 rustup install nightlyrustup default nightly,但是当我使用该命令构建时它会导致同样的错误。

我该怎么办?

也许您应该使用以下命令构建代码:

RUSTFLAGS="-Z sanitier=address" cargo run ${your-binary-name} --target x86_64-unknown-linux-gnu -- --emit=llvm-ir

这对我在手册中检查 example 有用:

RUSTFLAGS="-Z sanitizer=address" cargo run --example out-of-bounds --target x86_64-unknown-linux-gnu -- --emit=llvm-ir

**==618620==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffbcbf1250 at pc 0x55adfda84e90 bp 0x7fffbcbf1210 sp 0x7fffbcbf1208
READ of size 4 at 0x7fffbcbf1250 thread T0
    #0 0x55adfda84e8f in out_of_bounds::main::hf228b092630ab849 /tmp/rust-san/asan/examples/out-of-bounds.rs:3:22
    #1 0x55adfda846fa in core::ops::function::FnOnce::call_once::h84454ea25b7d75ab /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/core/src/ops/function.rs:248:5
    #2 0x55adfda84a94 in std::sys_common::backtrace::__rust_begin_short_backtrace::hbc7697f2c0b7e35d /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/std/src/sys_common/backtrace.rs:122:18
    #3 0x55adfda853a3 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h01c2d6f3a231ead0 /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/std/src/rt.rs:145:18
    #4 0x55adfda9704d in core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::hfce3f72e51a03fc4 /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/core/src/ops/function.rs:280:13
**

根据手册,您应该始终将 --target x86_64-unknown-linux-gnu 传递给 Cargo。

Be sure to always pass --target x86_64-unknown-linux-gnu to Cargo or else you'll end up sanitizing the build scripts that Cargo runs or run into compilation error if your crate depends on a dylib.

您可以通过以下方式检查您的工具链版本:

> rustup default
nightly-x86_64-unknown-linux-gnu (default)