4004: Use mmap for proc macro libs r=matklad a=lnicola

Fixes #4002.

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2020-04-17 09:40:53 +00:00 committed by GitHub
commit 46105cdaba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View file

@ -14,6 +14,7 @@ ra_mbe = { path = "../ra_mbe" }
ra_proc_macro = { path = "../ra_proc_macro" }
goblin = "0.2.1"
libloading = "0.6.0"
memmap = "0.7"
test_utils = { path = "../test_utils" }
[dev-dependencies]

View file

@ -1,10 +1,12 @@
//! Handles dynamic library loading for proc macro
use crate::{proc_macro::bridge, rustc_server::TokenStream};
use std::fs::File;
use std::path::Path;
use goblin::{mach::Mach, Object};
use libloading::Library;
use memmap::Mmap;
use ra_proc_macro::ProcMacroKind;
use std::io::Error as IoError;
@ -21,7 +23,8 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
}
fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> {
let buffer = std::fs::read(file)?;
let file = File::open(file)?;
let buffer = unsafe { Mmap::map(&file)? };
let object = Object::parse(&buffer).map_err(invalid_data_err)?;
match object {
@ -55,7 +58,7 @@ fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> {
&s.name
}
})
.find(|s| is_derive_registrar_symbol(&s))
.find(|s| is_derive_registrar_symbol(s))
.map(|s| s.to_string());
Ok(name)
}