/
miri-rustc-tests.rs
295 lines (270 loc) · 11.4 KB
/
miri-rustc-tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#![feature(rustc_private)]
extern crate miri;
extern crate getopts;
extern crate rustc;
extern crate rustc_metadata;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_codegen_utils;
extern crate syntax;
use std::path::{PathBuf, Path};
use std::io::Write;
use std::sync::{Mutex, Arc};
use std::io;
use rustc::session::Session;
use rustc_metadata::cstore::CStore;
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
use rustc_driver::driver::{CompileState, CompileController};
use rustc::session::config::{self, Input, ErrorOutputType};
use rustc::hir::{self, itemlikevisit};
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc::ty::TyCtxt;
use syntax::ast;
use rustc::hir::def_id::LOCAL_CRATE;
use miri::MiriConfig;
struct MiriCompilerCalls {
default: Box<RustcDefaultCalls>,
/// whether we are building for the host
host_target: bool,
}
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
fn early_callback(
&mut self,
matches: &getopts::Matches,
sopts: &config::Options,
cfg: &ast::CrateConfig,
descriptions: &rustc_errors::registry::Registry,
output: ErrorOutputType
) -> Compilation {
self.default.early_callback(matches, sopts, cfg, descriptions, output)
}
fn no_input(
&mut self,
matches: &getopts::Matches,
sopts: &config::Options,
cfg: &ast::CrateConfig,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>,
descriptions: &rustc_errors::registry::Registry
) -> Option<(Input, Option<PathBuf>)> {
self.default.no_input(matches, sopts, cfg, odir, ofile, descriptions)
}
fn late_callback(
&mut self,
trans: &CodegenBackend,
matches: &getopts::Matches,
sess: &Session,
cstore: &CStore,
input: &Input,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>,
) -> Compilation {
self.default.late_callback(trans, matches, sess, cstore, input, odir, ofile)
}
fn build_controller(self: Box<Self>, sess: &Session, matches: &getopts::Matches) -> CompileController<'a> {
let this = *self;
let mut control = this.default.build_controller(sess, matches);
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
control.after_analysis.callback = Box::new(after_analysis);
if !this.host_target {
// only fully compile targets on the host
control.after_analysis.stop = Compilation::Stop;
}
control
}
}
fn after_hir_lowering(state: &mut CompileState) {
let attr = (String::from("miri"), syntax::feature_gate::AttributeType::Whitelisted);
state.session.plugin_attributes.borrow_mut().push(attr);
}
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>) {
state.session.abort_if_errors();
let tcx = state.tcx.unwrap();
if std::env::args().any(|arg| arg == "--test") {
struct Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>, &'a CompileState<'a, 'tcx>);
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
fn visit_item(&mut self, i: &'hir hir::Item) {
if let hir::ItemKind::Fn(.., body_id) = i.node {
if i.attrs.iter().any(|attr| attr.name() == "test") {
let config = MiriConfig { validate: true, args: vec![] };
let did = self.0.hir().body_owner_def_id(body_id);
println!("running test: {}", self.0.def_path_debug_str(did));
miri::eval_main(self.0, did, config);
self.1.session.abort_if_errors();
}
}
}
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
}
state.hir_crate.unwrap().visit_all_item_likes(&mut Visitor(tcx, state));
} else if let Some((entry_def_id, _)) = tcx.entry_fn(LOCAL_CRATE) {
let config = MiriConfig { validate: true, args: vec![] };
miri::eval_main(tcx, entry_def_id, config);
state.session.abort_if_errors();
} else {
println!("no main function found, assuming auxiliary build");
}
}
fn main() {
let path = option_env!("MIRI_RUSTC_TEST")
.map(String::from)
.unwrap_or_else(|| {
std::env::var("MIRI_RUSTC_TEST")
.expect("need to set MIRI_RUSTC_TEST to path of rustc tests")
});
let mut mir_not_found = Vec::new();
let mut crate_not_found = Vec::new();
let mut success = 0;
let mut failed = Vec::new();
let mut c_abi_fns = Vec::new();
let mut abi = Vec::new();
let mut unsupported = Vec::new();
let mut unimplemented_intrinsic = Vec::new();
let mut limits = Vec::new();
let mut files: Vec<_> = std::fs::read_dir(path).unwrap().collect();
while let Some(file) = files.pop() {
let file = file.unwrap();
let path = file.path();
if file.metadata().unwrap().is_dir() {
if !path.to_str().unwrap().ends_with("auxiliary") {
// add subdirs recursively
files.extend(std::fs::read_dir(path).unwrap());
}
continue;
}
if !file.metadata().unwrap().is_file() || !path.to_str().unwrap().ends_with(".rs") {
continue;
}
let stderr = std::io::stderr();
write!(stderr.lock(), "test [miri-pass] {} ... ", path.display()).unwrap();
let mut host_target = false;
let mut args: Vec<String> = std::env::args().filter(|arg| {
if arg == "--miri_host_target" {
host_target = true;
false // remove the flag, rustc doesn't know it
} else {
true
}
}).collect();
args.splice(1..1, miri::miri_default_args().iter().map(ToString::to_string));
// file to process
args.push(path.display().to_string());
let sysroot_flag = String::from("--sysroot");
if !args.contains(&sysroot_flag) {
args.push(sysroot_flag);
args.push(Path::new(&std::env::var("HOME").unwrap()).join(".xargo").join("HOST").display().to_string());
}
// A threadsafe buffer for writing.
#[derive(Default, Clone)]
struct BufWriter(Arc<Mutex<Vec<u8>>>);
impl Write for BufWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().unwrap().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.lock().unwrap().flush()
}
}
let buf = BufWriter::default();
let output = buf.clone();
let result = std::panic::catch_unwind(|| {
rustc_driver::run_compiler(&args, Box::new(MiriCompilerCalls {
default: Box::new(RustcDefaultCalls),
host_target,
}), None, Some(Box::new(buf)));
});
match result {
Ok(()) => {
success += 1;
writeln!(stderr.lock(), "ok").unwrap()
},
Err(_) => {
let output = output.0.lock().unwrap();
let output_err = std::str::from_utf8(&output).unwrap();
if let Some(text) = output_err.splitn(2, "no mir for `").nth(1) {
let end = text.find('`').unwrap();
mir_not_found.push(text[..end].to_string());
writeln!(stderr.lock(), "NO MIR FOR `{}`", &text[..end]).unwrap();
} else if let Some(text) = output_err.splitn(2, "can't find crate for `").nth(1) {
let end = text.find('`').unwrap();
crate_not_found.push(text[..end].to_string());
writeln!(stderr.lock(), "CAN'T FIND CRATE FOR `{}`", &text[..end]).unwrap();
} else {
for text in output_err.split("error: ").skip(1) {
let end = text.find('\n').unwrap_or(text.len());
let c_abi = "can't call C ABI function: ";
let unimplemented_intrinsic_s = "unimplemented intrinsic: ";
let unsupported_s = "miri does not support ";
let abi_s = "can't handle function with ";
let limit_s = "reached the configured maximum ";
if text.starts_with(c_abi) {
c_abi_fns.push(text[c_abi.len()..end].to_string());
} else if text.starts_with(unimplemented_intrinsic_s) {
unimplemented_intrinsic.push(text[unimplemented_intrinsic_s.len()..end].to_string());
} else if text.starts_with(unsupported_s) {
unsupported.push(text[unsupported_s.len()..end].to_string());
} else if text.starts_with(abi_s) {
abi.push(text[abi_s.len()..end].to_string());
} else if text.starts_with(limit_s) {
limits.push(text[limit_s.len()..end].to_string());
} else if text.find("aborting").is_none() {
failed.push(text[..end].to_string());
}
}
writeln!(stderr.lock(), "stderr: \n {}", output_err).unwrap();
}
}
}
}
let stderr = std::io::stderr();
let mut stderr = stderr.lock();
writeln!(stderr, "{} success, {} no mir, {} crate not found, {} failed, \
{} C fn, {} ABI, {} unsupported, {} intrinsic",
success, mir_not_found.len(), crate_not_found.len(), failed.len(),
c_abi_fns.len(), abi.len(), unsupported.len(), unimplemented_intrinsic.len()).unwrap();
writeln!(stderr, "# The \"other reasons\" errors").unwrap();
writeln!(stderr, "(sorted, deduplicated)").unwrap();
print_vec(&mut stderr, failed);
writeln!(stderr, "# can't call C ABI function").unwrap();
print_vec(&mut stderr, c_abi_fns);
writeln!(stderr, "# unsupported ABI").unwrap();
print_vec(&mut stderr, abi);
writeln!(stderr, "# unsupported").unwrap();
print_vec(&mut stderr, unsupported);
writeln!(stderr, "# unimplemented intrinsics").unwrap();
print_vec(&mut stderr, unimplemented_intrinsic);
writeln!(stderr, "# mir not found").unwrap();
print_vec(&mut stderr, mir_not_found);
writeln!(stderr, "# crate not found").unwrap();
print_vec(&mut stderr, crate_not_found);
}
fn print_vec<W: std::io::Write>(stderr: &mut W, v: Vec<String>) {
writeln!(stderr, "```").unwrap();
for (n, s) in vec_to_hist(v).into_iter().rev() {
writeln!(stderr, "{:4} {}", n, s).unwrap();
}
writeln!(stderr, "```").unwrap();
}
fn vec_to_hist<T: PartialEq + Ord>(mut v: Vec<T>) -> Vec<(usize, T)> {
v.sort();
let mut v = v.into_iter();
let mut result = Vec::new();
let mut current = v.next();
'outer: while let Some(current_val) = current {
let mut n = 1;
for next in &mut v {
if next == current_val {
n += 1;
} else {
result.push((n, current_val));
current = Some(next);
continue 'outer;
}
}
result.push((n, current_val));
break;
}
result.sort();
result
}