Skip to content

Commit 1146504

Browse files
authored
Merge pull request rust-lang#23 from oli-obk/miri_run_no_more
don't use `#[miri_run]` anymore, but execute the `main` function
2 parents d69ffa0 + d82a792 commit 1146504

33 files changed

+134
-250
lines changed

benches/fibonacci_helper.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![feature(custom_attribute)]
2-
#![allow(unused_attributes)]
3-
4-
#[miri_run]
51
#[inline(never)]
62
pub fn main() {
73
assert_eq!(fib(10), 55);

benches/fibonacci_helper_iterative.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![feature(custom_attribute)]
2-
#![allow(unused_attributes)]
3-
4-
#[miri_run]
51
#[inline(never)]
62
pub fn main() {
73
assert_eq!(fib(10), 55);

benches/smoke_helper.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![feature(custom_attribute)]
2-
#![allow(unused_attributes)]
3-
4-
#[miri_run]
51
#[inline(never)]
62
pub fn main() {
73
}

src/bin/miri.rs

+35-47
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use miri::{
1919
use rustc::session::Session;
2020
use rustc_driver::{driver, CompilerCalls};
2121
use rustc::ty::{TyCtxt, subst};
22-
use rustc::mir::mir_map::MirMap;
2322
use rustc::hir::def_id::DefId;
2423

2524
struct MiriCompilerCalls;
@@ -34,57 +33,46 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
3433

3534
control.after_analysis.callback = Box::new(|state| {
3635
state.session.abort_if_errors();
37-
interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
38-
});
39-
40-
control
41-
}
42-
}
43-
4436

37+
let tcx = state.tcx.unwrap();
38+
let mir_map = state.mir_map.unwrap();
39+
40+
let (node_id, span) = state.session.entry_fn.borrow().expect("no main or start function found");
41+
debug!("found `main` function at: {:?}", span);
42+
43+
let mir = mir_map.map.get(&node_id).expect("no mir for main function");
44+
let def_id = tcx.map.local_def_id(node_id);
45+
let mut ecx = EvalContext::new(tcx, mir_map);
46+
let substs = tcx.mk_substs(subst::Substs::empty());
47+
let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs).expect("main function should not be diverging");
48+
49+
ecx.push_stack_frame(def_id, mir.span, CachedMir::Ref(mir), substs, Some(return_ptr));
50+
51+
if mir.arg_decls.len() == 2 {
52+
// start function
53+
let ptr_size = ecx.memory().pointer_size;
54+
let nargs = ecx.memory_mut().allocate(ptr_size);
55+
ecx.memory_mut().write_usize(nargs, 0).unwrap();
56+
let args = ecx.memory_mut().allocate(ptr_size);
57+
ecx.memory_mut().write_usize(args, 0).unwrap();
58+
ecx.frame_mut().locals[0] = nargs;
59+
ecx.frame_mut().locals[1] = args;
60+
}
4561

46-
fn interpret_start_points<'a, 'tcx>(
47-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
48-
mir_map: &MirMap<'tcx>,
49-
) {
50-
let initial_indentation = ::log_settings::settings().indentation;
51-
for (&id, mir) in &mir_map.map {
52-
for attr in tcx.map.attrs(id) {
53-
use syntax::attr::AttrMetaMethods;
54-
if attr.check_name("miri_run") {
55-
let item = tcx.map.expect_item(id);
56-
57-
::log_settings::settings().indentation = initial_indentation;
58-
59-
debug!("Interpreting: {}", item.name);
60-
61-
let mut ecx = EvalContext::new(tcx, mir_map);
62-
let substs = tcx.mk_substs(subst::Substs::empty());
63-
let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs);
64-
65-
ecx.push_stack_frame(tcx.map.local_def_id(id), mir.span, CachedMir::Ref(mir), substs, return_ptr);
66-
67-
loop {
68-
match step(&mut ecx) {
69-
Ok(true) => {}
70-
Ok(false) => {
71-
match return_ptr {
72-
Some(ptr) => if log_enabled!(::log::LogLevel::Debug) {
73-
ecx.memory().dump(ptr.alloc_id);
74-
},
75-
None => warn!("diverging function returned"),
76-
}
77-
break;
78-
}
79-
// FIXME: diverging functions can end up here in some future miri
80-
Err(e) => {
81-
report(tcx, &ecx, e);
82-
break;
83-
}
62+
loop {
63+
match step(&mut ecx) {
64+
Ok(true) => {}
65+
Ok(false) => break,
66+
// FIXME: diverging functions can end up here in some future miri
67+
Err(e) => {
68+
report(tcx, &ecx, e);
69+
break;
8470
}
8571
}
8672
}
87-
}
73+
});
74+
75+
control
8876
}
8977
}
9078

src/interpreter/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
154154
&self.memory
155155
}
156156

157+
pub fn memory_mut(&mut self) -> &mut Memory<'tcx> {
158+
&mut self.memory
159+
}
160+
157161
pub fn stack(&self) -> &[Frame] {
158162
&self.stack
159163
}
@@ -1373,7 +1377,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
13731377
self.stack.last().expect("no call frames exist")
13741378
}
13751379

1376-
fn frame_mut(&mut self) -> &mut Frame<'a, 'tcx> {
1380+
pub fn frame_mut(&mut self) -> &mut Frame<'a, 'tcx> {
13771381
self.stack.last_mut().expect("no call frames exist")
13781382
}
13791383

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let p = {
3+
let b = Box::new(42);
4+
&*b as *const i32
5+
};
6+
let x = unsafe { *p }; //~ ERROR: dangling pointer was dereferenced
7+
panic!("this should never print: {}", x);
8+
}

tests/compile-fail/deref_fn_ptr.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
#![feature(custom_attribute)]
2-
#![allow(dead_code, unused_attributes)]
3-
41
fn f() {}
52

6-
#[miri_run]
7-
fn deref_fn_ptr() -> i32 {
8-
unsafe {
3+
fn main() {
4+
let x: i32 = unsafe {
95
*std::mem::transmute::<fn(), *const i32>(f) //~ ERROR: tried to dereference a function pointer
10-
}
6+
};
7+
panic!("this should never print: {}", x);
118
}
12-
13-
fn main() {}

tests/compile-fail/errors.rs

-62
This file was deleted.

tests/compile-fail/execute_memory.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
#![feature(custom_attribute, box_syntax)]
2-
#![allow(dead_code, unused_attributes)]
1+
#![feature(box_syntax)]
32

4-
#[miri_run]
5-
fn deref_fn_ptr() {
3+
fn main() {
64
//FIXME: this span is wrong
75
let x = box 42; //~ ERROR: tried to treat a memory pointer as a function pointer
86
unsafe {
97
let f = std::mem::transmute::<Box<i32>, fn()>(x);
108
f()
119
}
1210
}
13-
14-
fn main() {}

tests/compile-fail/invalid_bool.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let b = unsafe { std::mem::transmute::<u8, bool>(2) };
3+
if b { unreachable!() } else { unreachable!() } //~ ERROR: invalid boolean value read
4+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR: attempted to interpret some raw bytes as a pointer address
3+
panic!("this should never print: {}", x);
4+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let v: Vec<u8> = vec![1, 2];
3+
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: memory access of 5..6 outside bounds of allocation 29 which has size 2
4+
panic!("this should never print: {}", x);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let mut p = &42;
3+
unsafe {
4+
let ptr: *mut _ = &mut p;
5+
*(ptr as *mut u8) = 123; // if we ever support 8 bit pointers, this is gonna cause
6+
// "attempted to interpret some raw bytes as a pointer address" instead of
7+
// "attempted to read undefined bytes"
8+
}
9+
let x = *p; //~ ERROR: attempted to read undefined bytes
10+
panic!("this should never print: {}", x);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x: *const u8 = &1;
3+
let y: *const u8 = &2;
4+
if x < y { //~ ERROR: attempted to do math or a comparison on pointers into different allocations
5+
unreachable!()
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let v: Vec<u8> = Vec::with_capacity(10);
3+
let undef = unsafe { *v.get_unchecked(5) };
4+
let x = undef + 1; //~ ERROR: attempted to read undefined bytes
5+
panic!("this should never print: {}", x);
6+
}

tests/compile-fail/unimplemented.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
#![feature(custom_attribute)]
2-
#![allow(dead_code, unused_attributes)]
3-
41
//error-pattern:begin_panic_fmt
52

6-
7-
#[miri_run]
8-
fn failed_assertions() {
3+
fn main() {
94
assert_eq!(5, 6);
105
}
11-
12-
fn main() {}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let p = 42 as *const i32;
3+
let x = unsafe { *p }; //~ ERROR: attempted to interpret some raw bytes as a pointer address
4+
panic!("this should never print: {}", x);
5+
}

tests/compiletest.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ fn run_mode(mode: &'static str) {
1515
.expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
1616
.to_owned(),
1717
};
18-
let sysroot_flag = format!("--sysroot {}", sysroot);
18+
let flags = format!("--sysroot {} -Dwarnings", sysroot);
1919

2020
// FIXME: read directories in sysroot/lib/rustlib and generate the test targets from that
2121
let targets = &["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"];
2222

2323
for &target in targets {
24+
use std::io::Write;
25+
let stderr = std::io::stderr();
26+
write!(stderr.lock(), "running tests for target {}", target).unwrap();
2427
let mut config = compiletest::default_config();
25-
config.host_rustcflags = Some(sysroot_flag.clone());
28+
config.host_rustcflags = Some(flags.clone());
2629
config.mode = mode.parse().expect("Invalid mode");
2730
config.run_lib_path = format!("{}/lib/rustlib/{}/lib", sysroot, target);
2831
config.rustc_path = "target/debug/miri".into();
2932
config.src_base = PathBuf::from(format!("tests/{}", mode));
3033
config.target = target.to_owned();
31-
config.target_rustcflags = Some(sysroot_flag.clone());
34+
config.target_rustcflags = Some(flags.clone());
3235
compiletest::run_tests(&config);
3336
}
3437
}

tests/run-pass/arrays.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1-
#![feature(custom_attribute)]
2-
#![allow(dead_code, unused_attributes)]
3-
4-
#[miri_run]
51
fn empty_array() -> [u16; 0] {
62
[]
73
}
84

9-
#[miri_run]
105
fn mini_array() -> [u16; 1] {
116
[42]
127
}
138

14-
#[miri_run]
159
fn big_array() -> [u16; 5] {
1610
[5, 4, 3, 2, 1]
1711
}
1812

19-
#[miri_run]
2013
fn array_array() -> [[u8; 2]; 3] {
2114
[[5, 4], [3, 2], [1, 0]]
2215
}
2316

24-
#[miri_run]
2517
fn index_unsafe() -> i32 {
2618
let a = [0, 10, 20, 30];
2719
unsafe { *a.get_unchecked(2) }
2820
}
2921

30-
#[miri_run]
3122
fn index() -> i32 {
3223
let a = [0, 10, 20, 30];
3324
a[2]
3425
}
3526

36-
#[miri_run]
3727
fn array_repeat() -> [u8; 8] {
3828
[42; 8]
3929
}
4030

41-
#[miri_run]
4231
fn slice_index() -> u8 {
4332
let arr: &[_] = &[101, 102, 103, 104, 105, 106];
4433
arr[5]
4534
}
4635

47-
#[miri_run]
4836
fn main() {
4937
assert_eq!(empty_array(), []);
5038
assert_eq!(index_unsafe(), 20);
@@ -53,4 +41,5 @@ fn main() {
5341
assert_eq!(big_array(), [5, 4, 3, 2, 1]);
5442
assert_eq!(array_array(), [[5, 4], [3, 2], [1, 0]]);
5543
assert_eq!(array_repeat(), [42; 8]);
44+
assert_eq!(mini_array(), [42]);
5645
}

0 commit comments

Comments
 (0)