Skip to content

Commit 96e8e34

Browse files
committed
Correct nits from Niko. Add tests for cdylib and staticlib.
1 parent 8567269 commit 96e8e34

File tree

7 files changed

+111
-4
lines changed

7 files changed

+111
-4
lines changed

src/librustc_metadata/creader.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,11 @@ impl<'a> CrateLoader<'a> {
860860
config::CrateTypeExecutable => true,
861861
// This crate will be compiled with the required
862862
// instrumentation pass
863-
config::CrateTypeRlib => false,
864-
config::CrateTypeDylib => false,
865-
config::CrateTypeCdylib => false,
866-
config::CrateTypeStaticlib => false,
863+
config::CrateTypeRlib |
864+
config::CrateTypeDylib |
865+
config::CrateTypeCdylib |
866+
config::CrateTypeStaticlib =>
867+
false,
867868
_ => {
868869
self.sess.err(&format!("Only executables, dylibs and rlibs can be \
869870
compiled with `-Z sanitizer`"));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 cdylib
6+
# is correctly detected.
7+
8+
# NOTE the address sanitizer only supports x86_64 linux and macOS
9+
10+
ifeq ($(TARGET),x86_64-apple-darwin)
11+
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
12+
EXTRA_RUSTFLAG=-C rpath
13+
else
14+
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
15+
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
16+
EXTRA_RUSTFLAG=
17+
endif
18+
endif
19+
20+
all:
21+
ifeq ($(ASAN_SUPPORT),1)
22+
$(RUSTC) -g -Z sanitizer=address --crate-type cdylib --target $(TARGET) library.rs
23+
$(RUSTC) -g -Z sanitizer=address --crate-type bin --target $(TARGET) -llibrary program.rs
24+
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | grep -q stack-buffer-overflow
25+
endif
26+
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-include ../tools.mk
2+
3+
# This test builds a staticlib, then an executable that links to it.
4+
# The staticlib and executable both are compiled with address sanitizer,
5+
# and we assert that a fault in the staticlib is correctly detected.
6+
7+
# NOTE the address sanitizer only supports x86_64 linux and macOS
8+
9+
ifeq ($(TARGET),x86_64-apple-darwin)
10+
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
11+
EXTRA_RUSTFLAG=-C rpath
12+
else
13+
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
14+
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
15+
EXTRA_RUSTFLAG=
16+
endif
17+
endif
18+
19+
all:
20+
ifeq ($(ASAN_SUPPORT),1)
21+
$(RUSTC) -g -Z sanitizer=address --crate-type staticlib --target $(TARGET) library.rs
22+
$(CC) program.c $(call STATICLIB,library) $(call OUT_EXE,program) -lasan $(EXTRACFLAGS) $(EXTRACXXFLAGS)
23+
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | grep -q stack-buffer-overflow
24+
endif
25+
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ignore-license
2+
void overflow();
3+
4+
int main() {
5+
overflow();
6+
return 0;
7+
}
8+

0 commit comments

Comments
 (0)