Skip to content

Commit dc41a0f

Browse files
committed
binary analysis: handle errors and stdout outputs
This is needed to if --emit=obj=- or --emit=obj=/dev/null is used, which is what rustc-option used to test for option availability.
1 parent c1ae7d2 commit dc41a0f

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/binary_analysis/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub(crate) mod stack_size;
1212

1313
pub fn binary_analysis<'tcx>(cx: &AnalysisCtxt<'tcx>, path: &Path) {
1414
let file = File::open(path).unwrap();
15+
let meta = file.metadata().unwrap();
16+
if !meta.is_file() {
17+
// We cannot handle special devices (this can be e.g. /dev/null).
18+
return;
19+
}
20+
1521
let mmap = unsafe { rustc_data_structures::memmap::Mmap::map(file) }.unwrap();
1622
let object = ObjectFile::parse(&*mmap).unwrap();
1723

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,20 @@ impl driver::CallbacksExt for MyCallbacks {
159159
}
160160

161161
fn after_codegen<'tcx>(&mut self, cx: &'tcx AnalysisCtxt<'tcx>) {
162+
// If compilation fails, do not attempt to perform binary analysis, as
163+
// binary might not have been generated.
164+
if cx.dcx().has_errors().is_some() {
165+
return;
166+
}
167+
162168
let outputs = cx.output_filenames(());
163169
if outputs.outputs.contains_key(&OutputType::Object) {
164-
binary_analysis::binary_analysis(cx, outputs.path(OutputType::Object).as_path());
170+
let file = outputs.path(OutputType::Object);
171+
// We cannot retrieve object back from stdout.
172+
if file.is_stdout() {
173+
return;
174+
}
175+
binary_analysis::binary_analysis(cx, file.as_path());
165176
}
166177
}
167178
}

0 commit comments

Comments
 (0)