Skip to content

Commit 1428cf6

Browse files
committed
Only parse the object file once
1 parent aef05d4 commit 1428cf6

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/tools/rust-analyzer/crates/proc-macro-srv/src/dylib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use proc_macro::bridge;
66
use std::{fmt, fs, io, time::SystemTime};
77

88
use libloading::Library;
9-
use memmap2::Mmap;
109
use object::Object;
1110
use paths::{Utf8Path, Utf8PathBuf};
1211
use proc_macro_api::ProcMacroKind;
@@ -23,8 +22,8 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
2322
symbol.contains(NEW_REGISTRAR_SYMBOL)
2423
}
2524

26-
fn find_registrar_symbol(buffer: &[u8]) -> object::Result<Option<String>> {
27-
Ok(object::File::parse(buffer)?
25+
fn find_registrar_symbol(obj: &object::File<'_>) -> object::Result<Option<String>> {
26+
Ok(obj
2827
.exports()?
2928
.into_iter()
3029
.map(|export| export.name())
@@ -109,15 +108,16 @@ struct ProcMacroLibraryLibloading {
109108

110109
impl ProcMacroLibraryLibloading {
111110
fn open(path: &Utf8Path) -> Result<Self, LoadProcMacroDylibError> {
112-
let buffer = unsafe { Mmap::map(&fs::File::open(path)?)? };
111+
let file = fs::File::open(path)?;
112+
let file = unsafe { memmap2::Mmap::map(&file) }?;
113+
let obj = object::File::parse(&*file)
114+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
115+
let version_info = version::read_dylib_info(&obj)?;
113116
let symbol_name =
114-
find_registrar_symbol(&buffer).map_err(invalid_data_err)?.ok_or_else(|| {
117+
find_registrar_symbol(&obj).map_err(invalid_data_err)?.ok_or_else(|| {
115118
invalid_data_err(format!("Cannot find registrar symbol in file {path}"))
116119
})?;
117120

118-
let version_info = version::read_dylib_info(&buffer)?;
119-
drop(buffer);
120-
121121
let lib = load_library(path).map_err(invalid_data_err)?;
122122
let proc_macros = crate::proc_macros::ProcMacros::from_lib(
123123
&lib,

src/tools/rust-analyzer/crates/proc-macro-srv/src/dylib/version.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::io::{self, Read};
44

5-
use object::read::{File as BinaryFile, Object, ObjectSection};
5+
use object::read::{Object, ObjectSection};
66

77
#[derive(Debug)]
88
#[allow(dead_code)]
@@ -16,14 +16,14 @@ pub struct RustCInfo {
1616
}
1717

1818
/// Read rustc dylib information
19-
pub fn read_dylib_info(buffer: &[u8]) -> io::Result<RustCInfo> {
19+
pub fn read_dylib_info(obj: &object::File<'_>) -> io::Result<RustCInfo> {
2020
macro_rules! err {
2121
($e:literal) => {
2222
io::Error::new(io::ErrorKind::InvalidData, $e)
2323
};
2424
}
2525

26-
let ver_str = read_version(buffer)?;
26+
let ver_str = read_version(obj)?;
2727
let mut items = ver_str.split_whitespace();
2828
let tag = items.next().ok_or_else(|| err!("version format error"))?;
2929
if tag != "rustc" {
@@ -70,10 +70,8 @@ pub fn read_dylib_info(buffer: &[u8]) -> io::Result<RustCInfo> {
7070

7171
/// This is used inside read_version() to locate the ".rustc" section
7272
/// from a proc macro crate's binary file.
73-
fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> {
74-
BinaryFile::parse(dylib_binary)
75-
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
76-
.section_by_name(section_name)
73+
fn read_section<'a>(obj: &object::File<'a>, section_name: &str) -> io::Result<&'a [u8]> {
74+
obj.section_by_name(section_name)
7775
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "section read error"))?
7876
.data()
7977
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
@@ -101,8 +99,8 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
10199
///
102100
/// Check this issue for more about the bytes layout:
103101
/// <https://github.com/rust-lang/rust-analyzer/issues/6174>
104-
pub fn read_version(buffer: &[u8]) -> io::Result<String> {
105-
let dot_rustc = read_section(buffer, ".rustc")?;
102+
pub fn read_version(obj: &object::File<'_>) -> io::Result<String> {
103+
let dot_rustc = read_section(obj, ".rustc")?;
106104

107105
// check if magic is valid
108106
if &dot_rustc[0..4] != b"rust" {
@@ -151,10 +149,10 @@ pub fn read_version(buffer: &[u8]) -> io::Result<String> {
151149

152150
#[test]
153151
fn test_version_check() {
154-
let info = read_dylib_info(&unsafe {
155-
memmap2::Mmap::map(&std::fs::File::open(crate::proc_macro_test_dylib_path()).unwrap())
156-
.unwrap()
157-
})
152+
let info = read_dylib_info(
153+
&object::File::parse(&*std::fs::read(crate::proc_macro_test_dylib_path()).unwrap())
154+
.unwrap(),
155+
)
158156
.unwrap();
159157

160158
assert_eq!(

0 commit comments

Comments
 (0)