Skip to content

Commit

Permalink
Detect use-after-scope bugs with AddressSanitizer
Browse files Browse the repository at this point in the history
Enable use-after-scope checks by default when using AddressSanitizer.
They allow to detect incorrect use of stack objects after their scope
have already ended. The detection is based on LLVM lifetime intrinsics.

To facilitate the use of this functionality, the lifetime intrinsics are
now emitted regardless of optimization level if enabled sanitizer makes
use of them.
  • Loading branch information
tmiasko committed Jan 27, 2020
1 parent 1d5f6d4 commit 08a1c56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use libc::{c_char, c_uint};
use log::debug;
use rustc::session::config;
use rustc::session::config::{self, Sanitizer};
use rustc::ty::layout::{self, Align, Size, TyLayout};
use rustc::ty::{self, Ty, TyCtxt};
use rustc_codegen_ssa::base::to_immediate;
Expand Down Expand Up @@ -1232,12 +1232,19 @@ impl Builder<'a, 'll, 'tcx> {
}

fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
if self.cx.sess().opts.optimize == config::OptLevel::No {
let size = size.bytes();
if size == 0 {
return;
}

let size = size.bytes();
if size == 0 {
let opts = &self.cx.sess().opts;
let emit = match opts.debugging_opts.sanitizer {
// Some sanitizer use lifetime intrinsics. When they are in use,
// emit lifetime intrinsics regardless of optimization level.
Some(Sanitizer::Address) | Some(Sanitizer::Memory) => true,
_ => opts.optimize != config::OptLevel::No,
};
if !emit {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ extern "C" LLVMPassRef LLVMRustFindAndCreatePass(const char *PassName) {

extern "C" LLVMPassRef LLVMRustCreateAddressSanitizerFunctionPass(bool Recover) {
const bool CompileKernel = false;
const bool UseAfterScope = true;

return wrap(createAddressSanitizerFunctionPass(CompileKernel, Recover));
return wrap(createAddressSanitizerFunctionPass(CompileKernel, Recover, UseAfterScope));
}

extern "C" LLVMPassRef LLVMRustCreateModuleAddressSanitizerPass(bool Recover) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/sanitizer-use-after-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// needs-sanitizer-support
// only-x86_64
//
// compile-flags: -Zsanitizer=address
// run-fail
// error-pattern: ERROR: AddressSanitizer: stack-use-after-scope

static mut P: *mut usize = std::ptr::null_mut();

fn main() {
unsafe {
{
let mut x = 0;
P = &mut x;
}
std::ptr::write_volatile(P, 123);
}
}

0 comments on commit 08a1c56

Please sign in to comment.