Skip to content

Commit

Permalink
sanitizers: Fix tests/ui/sanitize/leak.rs fails on
Browse files Browse the repository at this point in the history
Fix rust-lang#111073 by checking if `-Zexport-executable-symbols` is passed when
LeakSanitizer is enabled.
  • Loading branch information
rcvalle committed Aug 26, 2024
1 parent 22572d0 commit 1b04bb8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_session/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ session_sanitizer_cfi_requires_single_codegen_unit = `-Zsanitizer=cfi` with `-Cl
session_sanitizer_kcfi_requires_panic_abort = `-Z sanitizer=kcfi` requires `-C panic=abort`
session_sanitizer_leak_requires_export_executable_symbols = `-Zsanitizer=leak` requires `-Zexport-executable-symbols`
session_sanitizer_not_supported = {$us} sanitizer is not supported for this target
session_sanitizers_not_supported = {$us} sanitizers are not supported for this target
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ pub(crate) struct CannotMixAndMatchSanitizers {
#[diag(session_cannot_enable_crt_static_linux)]
pub(crate) struct CannotEnableCrtStaticLinux;

#[derive(Diagnostic)]
#[diag(session_sanitizer_leak_requires_export_executable_symbols)]
pub(crate) struct SanitizerLeakRequiresExportExecutableSymbols;

#[derive(Diagnostic)]
#[diag(session_sanitizer_cfi_requires_lto)]
pub(crate) struct SanitizerCfiRequiresLto;
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ impl Session {
self.opts.unstable_opts.sanitizer.contains(SanitizerSet::KCFI)
}

pub fn is_sanitizer_leak_enabled(&self) -> bool {
self.opts.unstable_opts.sanitizer.contains(SanitizerSet::LEAK)
}

pub fn is_split_lto_unit_enabled(&self) -> bool {
self.opts.unstable_opts.split_lto_unit == Some(true)
}
Expand Down Expand Up @@ -1223,6 +1227,11 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
sess.dcx().emit_err(errors::CannotEnableCrtStaticLinux);
}

// LeakSanitizer requires export_executable_symbols.
if sess.is_sanitizer_leak_enabled() && !sess.opts.unstable_opts.export_executable_symbols {
sess.dcx().emit_err(errors::SanitizerLeakRequiresExportExecutableSymbols);
}

// LLVM CFI requires LTO.
if sess.is_sanitizer_cfi_enabled()
&& !(sess.lto() == config::Lto::Fat || sess.opts.cg.linker_plugin_lto.enabled())
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/sanitizer/no-sanitize-inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//@ revisions: ASAN LSAN
//@ compile-flags: -Copt-level=3 -Zmir-opt-level=4 -Ctarget-feature=-crt-static
//@[ASAN] compile-flags: -Zsanitizer=address
//@[LSAN] compile-flags: -Zsanitizer=leak
//@[LSAN] compile-flags: -Zsanitizer=leak -Zexport-executable-symbols

#![crate_type = "lib"]
#![feature(no_sanitize)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/sanitizer/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//@[kcfi]compile-flags: -Zsanitizer=kcfi --cfg kcfi --target x86_64-unknown-none
//@[kcfi]compile-flags: -C panic=abort
//@[leak]needs-sanitizer-leak
//@[leak]compile-flags: -Zsanitizer=leak --cfg leak
//@[leak]compile-flags: -Zsanitizer=leak --cfg leak -Zexport-executable-symbols
//@[memory]needs-sanitizer-memory
//@[memory]compile-flags: -Zsanitizer=memory --cfg memory
//@[thread]needs-sanitizer-thread
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/sanitizer/leak-requires-export-executable-symbols.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Verifies that `-Zsanitizer=leak` requires `-Zexport-executable-symbols`.
//
//@ needs-sanitizer-leak
//@ compile-flags: -Zsanitizer=leak

#![feature(no_core)]
#![no_core]
#![no_main]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: `-Zsanitizer=leak` requires `-Zexport-executable-symbols`

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/sanitizer/unsupported-target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ compile-flags: -Z sanitizer=leak --target i686-unknown-linux-gnu
//@ compile-flags: -Z sanitizer=leak -Zexport-executable-symbols --target i686-unknown-linux-gnu
//@ needs-llvm-components: x86
//@ error-pattern: error: leak sanitizer is not supported for this target
#![feature(no_core)]
Expand Down

0 comments on commit 1b04bb8

Please sign in to comment.