Skip to content

Commit cc8c404

Browse files
committed
Print warning if proc-macro built by old rustc
1 parent 0669abd commit cc8c404

File tree

2 files changed

+148
-73
lines changed

2 files changed

+148
-73
lines changed

crates/proc_macro_api/src/lib.rs

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
pub mod msg;
99
mod process;
1010
mod rpc;
11+
mod version;
1112

1213
use base_db::{Env, ProcMacro};
1314
use std::{
1415
ffi::OsStr,
15-
fs::File,
16-
io::{self, Read},
16+
io,
1717
path::{Path, PathBuf},
1818
sync::Arc,
1919
};
@@ -24,10 +24,6 @@ use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
2424

2525
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
2626

27-
use memmap::Mmap;
28-
use object::read::{File as BinaryFile, Object, ObjectSection};
29-
use snap::read::FrameDecoder as SnapDecoder;
30-
3127
#[derive(Debug, Clone)]
3228
struct ProcMacroProcessExpander {
3329
process: Arc<ProcMacroProcessSrv>,
@@ -80,6 +76,21 @@ impl ProcMacroClient {
8076
}
8177

8278
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
79+
match version::read_info(dylib_path) {
80+
Ok(info) => {
81+
if info.version.0 < 1 || info.version.1 < 47 {
82+
eprintln!("proc-macro {} built by {:#?} is not supported by Rust Analyzer, please update your rust version.", dylib_path.to_string_lossy(), info);
83+
}
84+
}
85+
Err(err) => {
86+
eprintln!(
87+
"proc-macro {} failed to find the given version. Reason: {}",
88+
dylib_path.to_string_lossy(),
89+
err
90+
);
91+
}
92+
}
93+
8394
let macros = match self.process.find_proc_macros(dylib_path) {
8495
Err(err) => {
8596
eprintln!("Failed to find proc macros. Error: {:#?}", err);
@@ -107,71 +118,4 @@ impl ProcMacroClient {
107118
})
108119
.collect()
109120
}
110-
111-
// This is used inside self.read_version() to locate the ".rustc" section
112-
// from a proc macro crate's binary file.
113-
fn read_section<'a>(&self, dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> {
114-
BinaryFile::parse(dylib_binary)
115-
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
116-
.section_by_name(section_name)
117-
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "section read error"))?
118-
.data()
119-
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
120-
}
121-
122-
// Check the version of rustc that was used to compile a proc macro crate's
123-
// binary file.
124-
// A proc macro crate binary's ".rustc" section has following byte layout:
125-
// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes
126-
// * ff060000 734e6150 is followed, it's the snappy format magic bytes,
127-
// means bytes from here(including this sequence) are compressed in
128-
// snappy compression format. Version info is inside here, so decompress
129-
// this.
130-
// The bytes you get after decompressing the snappy format portion has
131-
// following layout:
132-
// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes(again)
133-
// * [crate root bytes] next 4 bytes is to store crate root position,
134-
// according to rustc's source code comment
135-
// * [length byte] next 1 byte tells us how many bytes we should read next
136-
// for the version string's utf8 bytes
137-
// * [version string bytes encoded in utf8] <- GET THIS BOI
138-
// * [some more bytes that we don really care but still there] :-)
139-
// Check this issue for more about the bytes layout:
140-
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
141-
#[allow(unused)]
142-
fn read_version(&self, dylib_path: &Path) -> io::Result<String> {
143-
let dylib_file = File::open(dylib_path)?;
144-
let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?;
145-
146-
let dot_rustc = self.read_section(&dylib_mmaped, ".rustc")?;
147-
148-
let header = &dot_rustc[..8];
149-
const EXPECTED_HEADER: [u8; 8] = [b'r', b'u', b's', b't', 0, 0, 0, 5];
150-
// check if header is valid
151-
if header != EXPECTED_HEADER {
152-
return Err(io::Error::new(
153-
io::ErrorKind::InvalidData,
154-
format!("only metadata version 5 is supported, section header was: {:?}", header),
155-
));
156-
}
157-
158-
let snappy_portion = &dot_rustc[8..];
159-
160-
let mut snappy_decoder = SnapDecoder::new(snappy_portion);
161-
162-
// the bytes before version string bytes, so this basically is:
163-
// 8 bytes for [b'r',b'u',b's',b't',0,0,0,5]
164-
// 4 bytes for [crate root bytes]
165-
// 1 byte for length of version string
166-
// so 13 bytes in total, and we should check the 13th byte
167-
// to know the length
168-
let mut bytes_before_version = [0u8; 13];
169-
snappy_decoder.read_exact(&mut bytes_before_version)?;
170-
let length = bytes_before_version[12]; // what? can't use -1 indexing?
171-
172-
let mut version_string_utf8 = vec![0u8; length as usize];
173-
snappy_decoder.read_exact(&mut version_string_utf8)?;
174-
let version_string = String::from_utf8(version_string_utf8);
175-
version_string.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
176-
}
177121
}

crates/proc_macro_api/src/version.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//! Reading proc-macro rustc version information from binary data
2+
3+
use std::{
4+
fs::File,
5+
io::{self, Read},
6+
path::Path,
7+
};
8+
9+
use memmap::Mmap;
10+
use object::read::{File as BinaryFile, Object, ObjectSection};
11+
use snap::read::FrameDecoder as SnapDecoder;
12+
13+
#[derive(Debug)]
14+
pub(crate) struct RustCInfo {
15+
pub(crate) version: (usize, usize, usize),
16+
pub(crate) channel: String,
17+
pub(crate) commit: String,
18+
pub(crate) date: String,
19+
}
20+
21+
pub(crate) fn read_info(dylib_path: &Path) -> io::Result<RustCInfo> {
22+
macro_rules! err {
23+
($e:literal) => {
24+
io::Error::new(io::ErrorKind::InvalidData, $e)
25+
};
26+
}
27+
28+
let ver_str = read_version(dylib_path)?;
29+
let mut items = ver_str.split_whitespace();
30+
let tag = items.next().ok_or(err!("version format error"))?;
31+
if tag != "rustc" {
32+
return Err(err!("version format error (No rustc tag)"));
33+
}
34+
35+
let version_part = items.next().ok_or(err!("no version string"))?;
36+
let mut version_parts = version_part.split("-");
37+
let version = version_parts.next().ok_or(err!("no version"))?;
38+
let channel = version_parts.next().unwrap_or_default().to_string();
39+
40+
let commit = items.next().ok_or(err!("no commit info"))?;
41+
// remove (
42+
if commit.len() == 0 {
43+
return Err(err!("commit format error"));
44+
}
45+
let commit = commit[1..].to_string();
46+
let date = items.next().ok_or(err!("no date info"))?;
47+
// remove )
48+
if date.len() == 0 {
49+
return Err(err!("date format error"));
50+
}
51+
let date = date[0..date.len() - 2].to_string();
52+
53+
let version_numbers = version
54+
.split(".")
55+
.map(|it| it.parse::<usize>())
56+
.collect::<Result<Vec<_>, _>>()
57+
.map_err(|_| err!("version number error"))?;
58+
59+
if version_numbers.len() != 3 {
60+
return Err(err!("version number format error"));
61+
}
62+
let version = (version_numbers[0], version_numbers[1], version_numbers[2]);
63+
64+
Ok(RustCInfo { version, channel, commit, date })
65+
}
66+
67+
// This is used inside read_version() to locate the ".rustc" section
68+
// from a proc macro crate's binary file.
69+
fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> {
70+
BinaryFile::parse(dylib_binary)
71+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
72+
.section_by_name(section_name)
73+
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "section read error"))?
74+
.data()
75+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
76+
}
77+
78+
// Check the version of rustc that was used to compile a proc macro crate's
79+
// binary file.
80+
// A proc macro crate binary's ".rustc" section has following byte layout:
81+
// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes
82+
// * ff060000 734e6150 is followed, it's the snappy format magic bytes,
83+
// means bytes from here(including this sequence) are compressed in
84+
// snappy compression format. Version info is inside here, so decompress
85+
// this.
86+
// The bytes you get after decompressing the snappy format portion has
87+
// following layout:
88+
// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes(again)
89+
// * [crate root bytes] next 4 bytes is to store crate root position,
90+
// according to rustc's source code comment
91+
// * [length byte] next 1 byte tells us how many bytes we should read next
92+
// for the version string's utf8 bytes
93+
// * [version string bytes encoded in utf8] <- GET THIS BOI
94+
// * [some more bytes that we don really care but still there] :-)
95+
// Check this issue for more about the bytes layout:
96+
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
97+
fn read_version(dylib_path: &Path) -> io::Result<String> {
98+
let dylib_file = File::open(dylib_path)?;
99+
let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?;
100+
101+
let dot_rustc = read_section(&dylib_mmaped, ".rustc")?;
102+
103+
let header = &dot_rustc[..8];
104+
const EXPECTED_HEADER: [u8; 8] = [b'r', b'u', b's', b't', 0, 0, 0, 5];
105+
// check if header is valid
106+
if header != EXPECTED_HEADER {
107+
return Err(io::Error::new(
108+
io::ErrorKind::InvalidData,
109+
format!("only metadata version 5 is supported, section header was: {:?}", header),
110+
));
111+
}
112+
113+
let snappy_portion = &dot_rustc[8..];
114+
115+
let mut snappy_decoder = SnapDecoder::new(snappy_portion);
116+
117+
// the bytes before version string bytes, so this basically is:
118+
// 8 bytes for [b'r',b'u',b's',b't',0,0,0,5]
119+
// 4 bytes for [crate root bytes]
120+
// 1 byte for length of version string
121+
// so 13 bytes in total, and we should check the 13th byte
122+
// to know the length
123+
let mut bytes_before_version = [0u8; 13];
124+
snappy_decoder.read_exact(&mut bytes_before_version)?;
125+
let length = bytes_before_version[12]; // what? can't use -1 indexing?
126+
127+
let mut version_string_utf8 = vec![0u8; length as usize];
128+
snappy_decoder.read_exact(&mut version_string_utf8)?;
129+
let version_string = String::from_utf8(version_string_utf8);
130+
version_string.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
131+
}

0 commit comments

Comments
 (0)