1
Fork 0

Improve error when an .rlib can't be parsed

This usually describes either an error in the compiler itself or some
sort of IO error. Either way, we should report it to the user rather
than just saying "crate not found".

This only gives an error if the crate couldn't be loaded at all - if the
compiler finds another .rlib or .rmeta file which was valid, it will
continue to compile the crate.

Example output:
```
error[E0785]: found invalid metadata files for crate `foo`
 --> bar.rs:3:24
  |
3 |         println!("{}", foo::FOO_11_49[0]);
  |                        ^^^
  |
  = warning: failed to parse rlib '/home/joshua/test-rustdoc/libfoo.rlib': Invalid archive extended name offset
```
This commit is contained in:
Joshua Nelson 2021-08-26 18:37:44 +00:00
parent 90a273b785
commit 257ac1b498
9 changed files with 118 additions and 16 deletions

View file

@ -484,6 +484,7 @@ E0782: include_str!("./error_codes/E0782.md"),
E0783: include_str!("./error_codes/E0783.md"), E0783: include_str!("./error_codes/E0783.md"),
E0784: include_str!("./error_codes/E0784.md"), E0784: include_str!("./error_codes/E0784.md"),
E0785: include_str!("./error_codes/E0785.md"), E0785: include_str!("./error_codes/E0785.md"),
E0786: include_str!("./error_codes/E0786.md"),
; ;
// E0006, // merged with E0005 // E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard // E0008, // cannot bind by-move into a pattern guard

View file

@ -0,0 +1,14 @@
A metadata file was invalid.
Erroneous code example:
```ignore (needs extern files)
use ::foo; // error: found invalid metadata files for crate `foo`
```
When loading crates, each crate must have a valid metadata file.
Invalid files could be caused by filesystem corruption,
an IO error while reading the file, or (rarely) a bug in the compiler itself.
Consider deleting the file and recreating it,
or reporting a bug against the compiler.

View file

@ -350,6 +350,7 @@ impl<'a> CrateLocator<'a> {
self.crate_rejections.via_kind.clear(); self.crate_rejections.via_kind.clear();
self.crate_rejections.via_version.clear(); self.crate_rejections.via_version.clear();
self.crate_rejections.via_filename.clear(); self.crate_rejections.via_filename.clear();
self.crate_rejections.via_invalid.clear();
} }
crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> { crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
@ -548,7 +549,17 @@ impl<'a> CrateLocator<'a> {
continue; continue;
} }
} }
Err(err) => { Err(MetadataError::LoadFailure(err)) => {
info!("no metadata found: {}", err);
// The file was present and created by the same compiler version, but we
// couldn't load it for some reason. Give a hard error instead of silently
// ignoring it, but only if we would have given an error anyway.
self.crate_rejections
.via_invalid
.push(CrateMismatch { path: lib, got: err });
continue;
}
Err(err @ MetadataError::NotPresent(_)) => {
info!("no metadata found: {}", err); info!("no metadata found: {}", err);
continue; continue;
} }
@ -726,25 +737,28 @@ impl<'a> CrateLocator<'a> {
fn get_metadata_section( fn get_metadata_section(
target: &Target, target: &Target,
flavor: CrateFlavor, flavor: CrateFlavor,
filename: &Path, filename: &'p Path,
loader: &dyn MetadataLoader, loader: &dyn MetadataLoader,
) -> Result<MetadataBlob, String> { ) -> Result<MetadataBlob, MetadataError<'p>> {
if !filename.exists() { if !filename.exists() {
return Err(format!("no such file: '{}'", filename.display())); return Err(MetadataError::NotPresent(filename));
} }
let raw_bytes: MetadataRef = match flavor { let raw_bytes: MetadataRef = match flavor {
CrateFlavor::Rlib => loader.get_rlib_metadata(target, filename)?, CrateFlavor::Rlib => {
loader.get_rlib_metadata(target, filename).map_err(MetadataError::LoadFailure)?
}
CrateFlavor::Dylib => { CrateFlavor::Dylib => {
let buf = loader.get_dylib_metadata(target, filename)?; let buf =
loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
// The header is uncompressed // The header is uncompressed
let header_len = METADATA_HEADER.len(); let header_len = METADATA_HEADER.len();
debug!("checking {} bytes of metadata-version stamp", header_len); debug!("checking {} bytes of metadata-version stamp", header_len);
let header = &buf[..cmp::min(header_len, buf.len())]; let header = &buf[..cmp::min(header_len, buf.len())];
if header != METADATA_HEADER { if header != METADATA_HEADER {
return Err(format!( return Err(MetadataError::LoadFailure(format!(
"incompatible metadata version found: '{}'", "invalid metadata version found: {}",
filename.display() filename.display()
)); )));
} }
// Header is okay -> inflate the actual metadata // Header is okay -> inflate the actual metadata
@ -756,17 +770,28 @@ fn get_metadata_section(
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) { match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()), Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
Err(_) => { Err(_) => {
return Err(format!("failed to decompress metadata: {}", filename.display())); return Err(MetadataError::LoadFailure(format!(
"failed to decompress metadata: {}",
filename.display()
)));
} }
} }
} }
CrateFlavor::Rmeta => { CrateFlavor::Rmeta => {
// mmap the file, because only a small fraction of it is read. // mmap the file, because only a small fraction of it is read.
let file = std::fs::File::open(filename) let file = std::fs::File::open(filename).map_err(|_| {
.map_err(|_| format!("failed to open rmeta metadata: '{}'", filename.display()))?; MetadataError::LoadFailure(format!(
"failed to open rmeta metadata: '{}'",
filename.display()
))
})?;
let mmap = unsafe { Mmap::map(file) }; let mmap = unsafe { Mmap::map(file) };
let mmap = mmap let mmap = mmap.map_err(|_| {
.map_err(|_| format!("failed to mmap rmeta metadata: '{}'", filename.display()))?; MetadataError::LoadFailure(format!(
"failed to mmap rmeta metadata: '{}'",
filename.display()
))
})?;
rustc_erase_owner!(OwningRef::new(mmap).map_owner_box()) rustc_erase_owner!(OwningRef::new(mmap).map_owner_box())
} }
@ -775,7 +800,10 @@ fn get_metadata_section(
if blob.is_compatible() { if blob.is_compatible() {
Ok(blob) Ok(blob)
} else { } else {
Err(format!("incompatible metadata version found: '{}'", filename.display())) Err(MetadataError::LoadFailure(format!(
"invalid metadata version found: {}",
filename.display()
)))
} }
} }
@ -854,6 +882,7 @@ struct CrateRejections {
via_kind: Vec<CrateMismatch>, via_kind: Vec<CrateMismatch>,
via_version: Vec<CrateMismatch>, via_version: Vec<CrateMismatch>,
via_filename: Vec<CrateMismatch>, via_filename: Vec<CrateMismatch>,
via_invalid: Vec<CrateMismatch>,
} }
/// Candidate rejection reasons collected during crate search. /// Candidate rejection reasons collected during crate search.
@ -883,6 +912,24 @@ crate enum CrateError {
NonDylibPlugin(Symbol), NonDylibPlugin(Symbol),
} }
enum MetadataError<'a> {
/// The file was missing.
NotPresent(&'a Path),
/// The file was present and invalid.
LoadFailure(String),
}
impl fmt::Display for MetadataError<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MetadataError::NotPresent(filename) => {
f.write_str(&format!("no such file: '{}'", filename.display()))
}
MetadataError::LoadFailure(msg) => f.write_str(msg),
}
}
}
impl CrateError { impl CrateError {
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! { crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! {
let mut err = match self { let mut err = match self {
@ -1064,6 +1111,19 @@ impl CrateError {
} }
err.note(&msg); err.note(&msg);
err err
} else if !locator.crate_rejections.via_invalid.is_empty() {
let mut err = struct_span_err!(
sess,
span,
E0786,
"found invalid metadata files for crate `{}`{}",
crate_name,
add,
);
for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid {
err.note(&got);
}
err
} else { } else {
let mut err = struct_span_err!( let mut err = struct_span_err!(
sess, sess,

View file

@ -3,4 +3,4 @@
all: all:
touch $(TMPDIR)/lib.rmeta touch $(TMPDIR)/lib.rmeta
$(AR) crus $(TMPDIR)/libfoo-ffffffff-1.0.rlib $(TMPDIR)/lib.rmeta $(AR) crus $(TMPDIR)/libfoo-ffffffff-1.0.rlib $(TMPDIR)/lib.rmeta
$(RUSTC) foo.rs 2>&1 | $(CGREP) "can't find crate for" $(RUSTC) foo.rs 2>&1 | $(CGREP) "found invalid metadata"

View file

@ -0,0 +1,7 @@
include ../../run-make-fulldeps/tools.mk
DYLIB_NAME := $(shell echo | $(RUSTC) --crate-name foo --crate-type dylib --print file-names -)
all:
echo >> $(TMPDIR)/$(DYLIB_NAME)
$(RUSTC) --crate-type lib --extern foo=$(TMPDIR)/$(DYLIB_NAME) bar.rs 2>&1 | $(CGREP) 'invalid metadata files for crate `foo`'

View file

@ -0,0 +1 @@
extern crate foo;

View file

@ -0,0 +1,8 @@
// compile-flags: --crate-type lib --extern foo={{src-base}}/crate-loading/auxiliary/libfoo.rlib
// normalize-stderr-test: "failed to mmap file '.*auxiliary/libfoo.rlib':.*" -> "failed to mmap file 'auxiliary/libfoo.rlib'"
// don't emit warn logging, it's basically the same as the errors and it's annoying to normalize
// rustc-env:RUSTC_LOG=error
// edition:2018
#![no_std]
use ::foo; //~ ERROR invalid metadata files for crate `foo`
//~| NOTE failed to mmap file

View file

@ -0,0 +1,11 @@
error[E0786]: found invalid metadata files for crate `foo`
--> $DIR/invalid-rlib.rs:7:7
|
LL | use ::foo;
| ^^^
|
= note: failed to mmap file 'auxiliary/libfoo.rlib'
error: aborting due to previous error
For more information about this error, try `rustc --explain E0786`.