Skip to content

Commit 1996a1f

Browse files
authored
Merge pull request #368 from solson/rustup
Rustup
2 parents 919604e + c05d570 commit 1996a1f

20 files changed

+512
-300
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: rust
2+
cache: cargo
23
rust:
34
- nightly
45
before_script:
@@ -23,7 +24,7 @@ script:
2324
# Test cargo miri
2425
cd cargo-miri-test &&
2526
cargo miri &&
26-
cargo miri test &&
27+
#cargo miri test &&
2728
cd ..
2829
- |
2930
# and run all tests with full mir

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ path = "miri/lib.rs"
2323

2424
[dependencies]
2525
byteorder = { version = "1.1", features = ["i128"]}
26-
cargo_metadata = { version = "0.2", optional = true }
26+
cargo_metadata = { version = "0.5", optional = true }
2727
regex = "0.2.2"
2828
lazy_static = "1.0"
29+
env_logger = "0.5.0-rc.1"
30+
log = "0.4"
2931

3032
[features]
3133
cargo_miri = ["cargo_metadata"]
3234

3335
[dev-dependencies]
34-
compiletest_rs = { version = "0.3.3", features = ["tmp"] }
36+
compiletest_rs = { version = "0.3.4", features = ["tmp"] }

miri/bin/cargo-miri.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ fn main() {
121121

122122
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
123123
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
124-
let sys_root = if let (Some(home), Some(toolchain)) = (home, toolchain) {
124+
let sys_root = if let Ok(sysroot) = ::std::env::var("MIRI_SYSROOT") {
125+
sysroot
126+
} else if let (Some(home), Some(toolchain)) = (home, toolchain) {
125127
format!("{}/toolchains/{}", home, toolchain)
126128
} else {
127129
option_env!("RUST_SYSROOT")

miri/bin/miri.rs

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#![feature(rustc_private, i128_type)]
1+
#![feature(rustc_private)]
22

33
extern crate getopts;
44
extern crate miri;
55
extern crate rustc;
66
extern crate rustc_driver;
77
extern crate rustc_errors;
8+
extern crate rustc_trans_utils;
89
extern crate env_logger;
910
extern crate log_settings;
1011
extern crate syntax;
@@ -17,11 +18,16 @@ use rustc_driver::driver::{CompileState, CompileController};
1718
use rustc::session::config::{self, Input, ErrorOutputType};
1819
use rustc::hir::{self, itemlikevisit};
1920
use rustc::ty::TyCtxt;
20-
use syntax::ast::{self, MetaItemKind, NestedMetaItemKind};
21+
use rustc_trans_utils::trans_crate::TransCrate;
22+
use syntax::ast;
2123
use std::path::PathBuf;
2224

2325
struct MiriCompilerCalls {
2426
default: RustcDefaultCalls,
27+
/// Whether to begin interpretation at the start_fn lang item or not
28+
///
29+
/// If false, the interpretation begins at the `main` function
30+
start_fn: bool,
2531
}
2632

2733
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
@@ -61,14 +67,15 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
6167
}
6268
fn late_callback(
6369
&mut self,
70+
trans: &TransCrate,
6471
matches: &getopts::Matches,
6572
sess: &Session,
6673
cstore: &CrateStore,
6774
input: &Input,
6875
odir: &Option<PathBuf>,
6976
ofile: &Option<PathBuf>,
7077
) -> Compilation {
71-
self.default.late_callback(matches, sess, cstore, input, odir, ofile)
78+
self.default.late_callback(trans, matches, sess, cstore, input, odir, ofile)
7279
}
7380
fn build_controller(
7481
&mut self,
@@ -77,7 +84,8 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
7784
) -> CompileController<'a> {
7885
let mut control = self.default.build_controller(sess, matches);
7986
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
80-
control.after_analysis.callback = Box::new(after_analysis);
87+
let start_fn = self.start_fn;
88+
control.after_analysis.callback = Box::new(move |state| after_analysis(state, start_fn));
8189
if sess.target.target != sess.host {
8290
// only fully compile targets on the host. linking will fail for cross-compilation.
8391
control.after_analysis.stop = Compilation::Stop;
@@ -94,135 +102,92 @@ fn after_hir_lowering(state: &mut CompileState) {
94102
state.session.plugin_attributes.borrow_mut().push(attr);
95103
}
96104

97-
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>) {
105+
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>, use_start_fn: bool) {
98106
state.session.abort_if_errors();
99107

100108
let tcx = state.tcx.unwrap();
101-
let limits = resource_limits_from_attributes(state);
102109

103110
if std::env::args().any(|arg| arg == "--test") {
104111
struct Visitor<'a, 'tcx: 'a>(
105-
miri::ResourceLimits,
106112
TyCtxt<'a, 'tcx, 'tcx>,
107113
&'a CompileState<'a, 'tcx>
108114
);
109115
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
110116
fn visit_item(&mut self, i: &'hir hir::Item) {
111117
if let hir::Item_::ItemFn(_, _, _, _, _, body_id) = i.node {
112118
if i.attrs.iter().any(|attr| {
113-
attr.name().map_or(false, |n| n == "test")
119+
attr.name() == "test"
114120
})
115121
{
116-
let did = self.1.hir.body_owner_def_id(body_id);
122+
let did = self.0.hir.body_owner_def_id(body_id);
117123
println!(
118124
"running test: {}",
119-
self.1.def_path_debug_str(did),
125+
self.0.def_path_debug_str(did),
120126
);
121-
miri::eval_main(self.1, did, None, self.0);
122-
self.2.session.abort_if_errors();
127+
miri::eval_main(self.0, did, None);
128+
self.1.session.abort_if_errors();
123129
}
124130
}
125131
}
126132
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
127133
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
128134
}
129135
state.hir_crate.unwrap().visit_all_item_likes(
130-
&mut Visitor(limits, tcx, state),
136+
&mut Visitor(tcx, state),
131137
);
132-
} else if let Some((entry_node_id, _)) = *state.session.entry_fn.borrow() {
138+
} else if let Some((entry_node_id, _, _)) = *state.session.entry_fn.borrow() {
133139
let entry_def_id = tcx.hir.local_def_id(entry_node_id);
134140
let start_wrapper = tcx.lang_items().start_fn().and_then(|start_fn| {
135-
if tcx.is_mir_available(start_fn) {
141+
if use_start_fn {
136142
Some(start_fn)
137143
} else {
138144
None
139145
}
140146
});
141-
miri::eval_main(tcx, entry_def_id, start_wrapper, limits);
147+
miri::eval_main(tcx, entry_def_id, start_wrapper);
142148

143149
state.session.abort_if_errors();
144150
} else {
145151
println!("no main function found, assuming auxiliary build");
146152
}
147153
}
148154

149-
fn resource_limits_from_attributes(state: &CompileState) -> miri::ResourceLimits {
150-
let mut limits = miri::ResourceLimits::default();
151-
let krate = state.hir_crate.as_ref().unwrap();
152-
let err_msg = "miri attributes need to be in the form `miri(key = value)`";
153-
let extract_int = |lit: &syntax::ast::Lit| -> u128 {
154-
match lit.node {
155-
syntax::ast::LitKind::Int(i, _) => i,
156-
_ => {
157-
state.session.span_fatal(
158-
lit.span,
159-
"expected an integer literal",
160-
)
161-
}
162-
}
163-
};
164-
165-
for attr in krate.attrs.iter().filter(|a| {
166-
a.name().map_or(false, |n| n == "miri")
167-
})
168-
{
169-
if let Some(items) = attr.meta_item_list() {
170-
for item in items {
171-
if let NestedMetaItemKind::MetaItem(ref inner) = item.node {
172-
if let MetaItemKind::NameValue(ref value) = inner.node {
173-
match &inner.name().as_str()[..] {
174-
"memory_size" => limits.memory_size = extract_int(value) as u64,
175-
"step_limit" => limits.step_limit = extract_int(value) as u64,
176-
"stack_limit" => limits.stack_limit = extract_int(value) as usize,
177-
_ => state.session.span_err(item.span, "unknown miri attribute"),
178-
}
179-
} else {
180-
state.session.span_err(inner.span, err_msg);
181-
}
182-
} else {
183-
state.session.span_err(item.span, err_msg);
184-
}
185-
}
186-
} else {
187-
state.session.span_err(attr.span, err_msg);
188-
}
189-
}
190-
limits
191-
}
192-
193155
fn init_logger() {
194-
let format = |record: &log::LogRecord| {
195-
if record.level() == log::LogLevel::Trace {
156+
let format = |formatter: &mut env_logger::fmt::Formatter, record: &log::Record| {
157+
use std::io::Write;
158+
if record.level() == log::Level::Trace {
196159
// prepend frame number
197160
let indentation = log_settings::settings().indentation;
198-
format!(
161+
writeln!(
162+
formatter,
199163
"{indentation}:{lvl}:{module}: {text}",
200164
lvl = record.level(),
201-
module = record.location().module_path(),
165+
module = record.module_path().unwrap_or("<unknown module>"),
202166
indentation = indentation,
203167
text = record.args(),
204168
)
205169
} else {
206-
format!(
170+
writeln!(
171+
formatter,
207172
"{lvl}:{module}: {text}",
208173
lvl = record.level(),
209-
module = record.location().module_path(),
174+
module = record.module_path().unwrap_or("<unknown_module>"),
210175
text = record.args(),
211176
)
212177
}
213178
};
214179

215-
let mut builder = env_logger::LogBuilder::new();
180+
let mut builder = env_logger::Builder::new();
216181
builder.format(format).filter(
217182
None,
218-
log::LogLevelFilter::Info,
183+
log::LevelFilter::Info,
219184
);
220185

221186
if std::env::var("MIRI_LOG").is_ok() {
222187
builder.parse(&std::env::var("MIRI_LOG").unwrap());
223188
}
224189

225-
builder.init().unwrap();
190+
builder.init();
226191
}
227192

228193
fn find_sysroot() -> String {
@@ -246,6 +211,7 @@ fn find_sysroot() -> String {
246211
}
247212

248213
fn main() {
214+
rustc_driver::init_rustc_env_logger();
249215
init_logger();
250216
let mut args: Vec<String> = std::env::args().collect();
251217

@@ -255,10 +221,21 @@ fn main() {
255221
args.push(find_sysroot());
256222
}
257223

224+
let mut start_fn = false;
225+
args.retain(|arg| {
226+
if arg == "-Zmiri-start-fn" {
227+
start_fn = true;
228+
false
229+
} else {
230+
true
231+
}
232+
});
233+
258234
// Make sure we always have all the MIR (e.g. for auxilary builds in unit tests).
259235
args.push("-Zalways-encode-mir".to_owned());
260236

261237
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls {
262238
default: RustcDefaultCalls,
239+
start_fn,
263240
}, None, None);
264241
}

0 commit comments

Comments
 (0)