mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Fix multiple issues from code review
* check metadata version * use memmap * use Result instead of unwrap with Jay Somedon <jay.somedon@outlook.com>
This commit is contained in:
parent
8fd7cd7406
commit
55d73bc675
3 changed files with 44 additions and 34 deletions
33
Cargo.lock
generated
33
Cargo.lock
generated
|
@ -80,7 +80,7 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"miniz_oxide",
|
"miniz_oxide",
|
||||||
"object 0.23.0",
|
"object",
|
||||||
"rustc-demangle",
|
"rustc-demangle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -891,6 +891,16 @@ version = "2.3.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
|
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memmap"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memmap2"
|
name = "memmap2"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
@ -1008,16 +1018,6 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "object"
|
|
||||||
version = "0.22.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
|
|
||||||
dependencies = [
|
|
||||||
"flate2",
|
|
||||||
"wasmparser",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.23.0"
|
version = "0.23.0"
|
||||||
|
@ -1164,7 +1164,8 @@ dependencies = [
|
||||||
"crossbeam-channel",
|
"crossbeam-channel",
|
||||||
"jod-thread",
|
"jod-thread",
|
||||||
"log",
|
"log",
|
||||||
"object 0.22.0",
|
"memmap",
|
||||||
|
"object",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"snap",
|
"snap",
|
||||||
|
@ -1180,7 +1181,7 @@ dependencies = [
|
||||||
"libloading",
|
"libloading",
|
||||||
"mbe",
|
"mbe",
|
||||||
"memmap2",
|
"memmap2",
|
||||||
"object 0.23.0",
|
"object",
|
||||||
"proc_macro_api",
|
"proc_macro_api",
|
||||||
"proc_macro_test",
|
"proc_macro_test",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
|
@ -1898,12 +1899,6 @@ dependencies = [
|
||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasmparser"
|
|
||||||
version = "0.57.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
|
@ -21,3 +21,4 @@ base_db = { path = "../base_db", version = "0.0.0" }
|
||||||
stdx = { path = "../stdx", version = "0.0.0" }
|
stdx = { path = "../stdx", version = "0.0.0" }
|
||||||
snap = "1"
|
snap = "1"
|
||||||
object = { version = "0.23.0", default-features = false, features = ["std", "read_core", "elf", "macho", "pe", "unaligned"] }
|
object = { version = "0.23.0", default-features = false, features = ["std", "read_core", "elf", "macho", "pe", "unaligned"] }
|
||||||
|
memmap = "0.7.0"
|
||||||
|
|
|
@ -9,15 +9,22 @@ pub mod msg;
|
||||||
mod process;
|
mod process;
|
||||||
mod rpc;
|
mod rpc;
|
||||||
|
|
||||||
use std::{ffi::OsStr, fs::read as fsread, io::{self, Read}, path::{Path, PathBuf}, sync::Arc};
|
|
||||||
|
|
||||||
use base_db::{Env, ProcMacro};
|
use base_db::{Env, ProcMacro};
|
||||||
|
use std::{
|
||||||
|
ffi::OsStr,
|
||||||
|
fs::File,
|
||||||
|
io::{self, Read},
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use tt::{SmolStr, Subtree};
|
use tt::{SmolStr, Subtree};
|
||||||
|
|
||||||
use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
|
use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
|
||||||
|
|
||||||
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
|
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
|
||||||
|
|
||||||
|
use memmap::Mmap;
|
||||||
use object::read::{File as BinaryFile, Object, ObjectSection};
|
use object::read::{File as BinaryFile, Object, ObjectSection};
|
||||||
use snap::read::FrameDecoder as SnapDecoder;
|
use snap::read::FrameDecoder as SnapDecoder;
|
||||||
|
|
||||||
|
@ -110,13 +117,13 @@ impl ProcMacroClient {
|
||||||
|
|
||||||
// This is used inside self.read_version() to locate the ".rustc" section
|
// This is used inside self.read_version() to locate the ".rustc" section
|
||||||
// from a proc macro crate's binary file.
|
// from a proc macro crate's binary file.
|
||||||
fn read_section<'a>(&self, dylib_binary: &'a [u8], section_name: &str) -> &'a [u8] {
|
fn read_section<'a>(&self, dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> {
|
||||||
BinaryFile::parse(dylib_binary)
|
BinaryFile::parse(dylib_binary)
|
||||||
.unwrap()
|
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
|
||||||
.section_by_name(section_name)
|
.section_by_name(section_name)
|
||||||
.unwrap()
|
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "section read error"))?
|
||||||
.data()
|
.data()
|
||||||
.unwrap()
|
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the version of rustc that was used to compile a proc macro crate's
|
// Check the version of rustc that was used to compile a proc macro crate's
|
||||||
|
@ -138,10 +145,19 @@ impl ProcMacroClient {
|
||||||
// * [some more bytes that we don really care but still there] :-)
|
// * [some more bytes that we don really care but still there] :-)
|
||||||
// Check this issue for more about the bytes layout:
|
// Check this issue for more about the bytes layout:
|
||||||
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
|
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
|
||||||
fn read_version(&self, dylib_path: &Path) -> String {
|
#[allow(unused)]
|
||||||
let dylib_binary = fsread(dylib_path).unwrap();
|
fn read_version(&self, dylib_path: &Path) -> io::Result<String> {
|
||||||
|
let dylib_file = File::open(dylib_path)?;
|
||||||
|
let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?;
|
||||||
|
|
||||||
let dot_rustc = self.read_section(&dylib_binary, ".rustc");
|
let dot_rustc = self.read_section(&dylib_mmaped, ".rustc")?;
|
||||||
|
|
||||||
|
let header = &dot_rustc[..8];
|
||||||
|
const EXPECTED_HEADER: [u8; 8] = [b'r', b'u', b's', b't', 0, 0, 0, 5];
|
||||||
|
// check if header is valid
|
||||||
|
if !(header == EXPECTED_HEADER) {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::InvalidData, format!(".rustc section should start with header {:?}; header {:?} is actually presented.",EXPECTED_HEADER ,header)));
|
||||||
|
}
|
||||||
|
|
||||||
let snappy_portion = &dot_rustc[8..];
|
let snappy_portion = &dot_rustc[8..];
|
||||||
|
|
||||||
|
@ -154,14 +170,12 @@ impl ProcMacroClient {
|
||||||
// so 13 bytes in total, and we should check the 13th byte
|
// so 13 bytes in total, and we should check the 13th byte
|
||||||
// to know the length
|
// to know the length
|
||||||
let mut bytes_before_version = [0u8; 13];
|
let mut bytes_before_version = [0u8; 13];
|
||||||
snappy_decoder
|
snappy_decoder.read_exact(&mut bytes_before_version)?;
|
||||||
.read_exact(&mut bytes_before_version)
|
|
||||||
.unwrap();
|
|
||||||
let length = bytes_before_version[12]; // what? can't use -1 indexing?
|
let length = bytes_before_version[12]; // what? can't use -1 indexing?
|
||||||
|
|
||||||
let mut version_string_utf8 = vec![0u8; length as usize];
|
let mut version_string_utf8 = vec![0u8; length as usize];
|
||||||
snappy_decoder.read_exact(&mut version_string_utf8).unwrap();
|
snappy_decoder.read_exact(&mut version_string_utf8)?;
|
||||||
let version_string = String::from_utf8(version_string_utf8).unwrap();
|
let version_string = String::from_utf8(version_string_utf8);
|
||||||
version_string
|
version_string.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue