Skip to content

Commit 26657bf

Browse files
committed
Add support for dylibs with Address Sanitizer
1 parent 0189cec commit 26657bf

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed

src/librustc_metadata/creader.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,14 @@ impl<'a> CrateLoader<'a> {
858858
match *ct {
859859
// Link the runtime
860860
config::CrateTypeExecutable => true,
861+
config::CrateTypeDylib => true,
862+
config::CrateTypeCdylib => true,
863+
config::CrateTypeStaticlib => true,
861864
// This crate will be compiled with the required
862865
// instrumentation pass
863866
config::CrateTypeRlib => false,
864867
_ => {
865-
self.sess.err(&format!("Only executables and rlibs can be \
868+
self.sess.err(&format!("Only executables, dylibs and rlibs can be \
866869
compiled with `-Z sanitizer`"));
867870
false
868871
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-include ../tools.mk
2+
3+
# This test builds a shared object, then an executable that links it as a native
4+
# rust library (constrast to an rlib). The shared library and executable both
5+
# are compiled with address sanitizer, and we assert that a fault in the dylib
6+
# is correctly detected.
7+
8+
# Note: currently we have to add -lasan to link, because it's not automatically
9+
# added in the -Z sanitizer=address process.
10+
11+
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
12+
all:
13+
$(RUSTC) -g -Z sanitizer=address --crate-type dylib --target $(TARGET) -lasan library.rs
14+
$(RUSTC) -g -Z sanitizer=address --crate-type bin --target $(TARGET) -lasan -llibrary program.rs
15+
echo $(RUSTC)
16+
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | grep -q stack-buffer-overflow
17+
else
18+
all:
19+
endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[no_mangle]
12+
pub extern fn overflow() {
13+
let xs = [0, 1, 2, 3];
14+
let y = unsafe { *xs.as_ptr().offset(4) };
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern {
12+
fn overflow();
13+
}
14+
15+
fn main() {
16+
unsafe { overflow() }
17+
}

src/test/run-make/sanitizer-dylib/Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-include ../tools.mk
2+
3+
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
4+
all:
5+
$(RUSTC) -Z sanitizer=leak --crate-type proc-macro --target $(TARGET) hello.rs 2>&1 | grep -q 'Only executables, dylibs and rlibs can be compiled with `-Z sanitizer`'
6+
else
7+
all:
8+
endif

0 commit comments

Comments
 (0)