From f7f121d60d0f6b8f280e09397b14a8afd0a369c1 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 4 Apr 2020 16:09:36 +0800 Subject: Add dylib loading code --- crates/ra_proc_macro_srv/Cargo.toml | 4 +- crates/ra_proc_macro_srv/src/dylib.rs | 220 +++++++++++++++++++++++++++ crates/ra_proc_macro_srv/src/lib.rs | 2 + crates/ra_proc_macro_srv/src/rustc_server.rs | 4 + 4 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 crates/ra_proc_macro_srv/src/dylib.rs (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml index f08de5fc7..437b8f475 100644 --- a/crates/ra_proc_macro_srv/Cargo.toml +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -12,9 +12,11 @@ doctest = false ra_tt = { path = "../ra_tt" } ra_mbe = { path = "../ra_mbe" } ra_proc_macro = { path = "../ra_proc_macro" } +goblin = "0.2.1" +libloading = "0.6.0" [dev-dependencies] cargo_metadata = "0.9.1" difference = "2.0.0" # used as proc macro test target -serde_derive = "=1.0.104" \ No newline at end of file +serde_derive = "=1.0.104" diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs new file mode 100644 index 000000000..525c7ac7b --- /dev/null +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -0,0 +1,220 @@ +//! Handles dynamic library loading for proc macro + +use crate::{proc_macro::bridge, rustc_server::TokenStream}; +use std::fs::File; +use std::io::Read; +use std::path::Path; + +use goblin::{mach::Mach, Object}; +use libloading::Library; +use ra_proc_macro::ProcMacroKind; + +static NEW_REGISTRAR_SYMBOL: &str = "__rustc_proc_macro_decls_"; +static _OLD_REGISTRAR_SYMBOL: &str = "__rustc_derive_registrar_"; + +fn read_bytes(file: &Path) -> Option> { + let mut fd = File::open(file).ok()?; + let mut buffer = Vec::new(); + fd.read_to_end(&mut buffer).ok()?; + + Some(buffer) +} + +fn get_symbols_from_lib(file: &Path) -> Option> { + let buffer = read_bytes(file)?; + let object = Object::parse(&buffer).ok()?; + + return match object { + Object::Elf(elf) => { + let symbols = elf.dynstrtab.to_vec().ok()?; + let names = symbols.iter().map(|s| s.to_string()).collect(); + + Some(names) + } + + Object::PE(pe) => { + let symbol_names = + pe.exports.iter().flat_map(|s| s.name).map(|n| n.to_string()).collect(); + Some(symbol_names) + } + + Object::Mach(mach) => match mach { + Mach::Binary(binary) => { + let exports = binary.exports().ok()?; + let names = exports.iter().map(|s| s.name.clone()).collect(); + + Some(names) + } + + Mach::Fat(_) => None, + }, + + Object::Archive(_) | Object::Unknown(_) => None, + }; +} + +fn is_derive_registrar_symbol(symbol: &str) -> bool { + symbol.contains(NEW_REGISTRAR_SYMBOL) +} + +fn find_registrar_symbol(file: &Path) -> Option { + let symbols = get_symbols_from_lib(file)?; + + symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| s.to_string()) +} + +/// Loads dynamic library in platform dependent manner. +/// +/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described +/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample) +/// and [here](https://github.com/rust-lang/rust/issues/60593). +/// +/// Usage of RTLD_DEEPBIND +/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1) +/// +/// It seems that on Windows that behaviour is default, so we do nothing in that case. +#[cfg(windows)] +fn load_library(file: &Path) -> Result { + Library::new(file) +} + +#[cfg(unix)] +fn load_library(file: &Path) -> Result { + use libloading::os::unix::Library as UnixLibrary; + use std::os::raw::c_int; + + const RTLD_NOW: c_int = 0x00002; + const RTLD_DEEPBIND: c_int = 0x00008; + + UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) +} + +struct ProcMacroLibraryLibloading { + // Hold the dylib to prevent it for unloadeding + #[allow(dead_code)] + lib: Library, + exported_macros: Vec, +} + +impl ProcMacroLibraryLibloading { + fn open(file: &Path) -> Result { + let symbol_name = find_registrar_symbol(file) + .ok_or(format!("Cannot find registrar symbol in file {:?}", file))?; + + let lib = load_library(file).map_err(|e| e.to_string())?; + + let exported_macros = { + let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> = + unsafe { lib.get(symbol_name.as_bytes()) }.map_err(|e| e.to_string())?; + + macros.to_vec() + }; + + Ok(ProcMacroLibraryLibloading { lib, exported_macros }) + } +} + +type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; + +pub struct Expander { + libs: Vec, +} + +impl Expander { + pub fn new>(lib: &P) -> Result { + let mut libs = vec![]; + + /* Some libraries for dynamic loading require canonicalized path (even when it is + already absolute + */ + let lib = + lib.as_ref().canonicalize().expect(&format!("Cannot canonicalize {:?}", lib.as_ref())); + + let library = ProcMacroLibraryImpl::open(&lib)?; + libs.push(library); + + Ok(Expander { libs }) + } + + pub fn expand( + &self, + macro_name: &str, + macro_body: &ra_tt::Subtree, + attributes: Option<&ra_tt::Subtree>, + ) -> Result { + let parsed_body = TokenStream::with_subtree(macro_body.clone()); + + let parsed_attributes = attributes + .map_or(crate::rustc_server::TokenStream::new(), |attr| { + TokenStream::with_subtree(attr.clone()) + }); + + for lib in &self.libs { + for proc_macro in &lib.exported_macros { + match proc_macro { + bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } + if *trait_name == macro_name => + { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + + return res.map(|it| it.subtree); + } + + bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + + return res.map(|it| it.subtree); + } + + bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_attributes, + parsed_body, + ); + + return res.map(|it| it.subtree); + } + + _ => { + continue; + } + } + } + } + + Err(bridge::PanicMessage::String("Nothing to expand".to_string())) + } + + pub fn list_macros(&self) -> Result, bridge::PanicMessage> { + let mut result = vec![]; + + for lib in &self.libs { + for proc_macro in &lib.exported_macros { + let res = match proc_macro { + bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { + (trait_name.to_string(), ProcMacroKind::CustomDerive) + } + bridge::client::ProcMacro::Bang { name, .. } => { + (name.to_string(), ProcMacroKind::FuncLike) + } + bridge::client::ProcMacro::Attr { name, .. } => { + (name.to_string(), ProcMacroKind::Attr) + } + }; + result.push(res); + } + } + + Ok(result) + } +} diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index f376df236..f5a526dbf 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -17,6 +17,8 @@ mod proc_macro; #[doc(hidden)] mod rustc_server; +mod dylib; + use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; diff --git a/crates/ra_proc_macro_srv/src/rustc_server.rs b/crates/ra_proc_macro_srv/src/rustc_server.rs index 92d1fd989..ec0d35692 100644 --- a/crates/ra_proc_macro_srv/src/rustc_server.rs +++ b/crates/ra_proc_macro_srv/src/rustc_server.rs @@ -34,6 +34,10 @@ impl TokenStream { TokenStream { subtree: Default::default() } } + pub fn with_subtree(subtree: tt::Subtree) -> Self { + TokenStream { subtree } + } + pub fn is_empty(&self) -> bool { self.subtree.token_trees.is_empty() } -- cgit v1.2.3 From e5698faf4c3d1964d2a644f7b0856d5b3577df73 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 4 Apr 2020 16:10:45 +0800 Subject: Implement expand_task and list_macros --- crates/ra_proc_macro_srv/src/lib.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index f5a526dbf..8fba73ac9 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -22,10 +22,33 @@ mod dylib; use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; -pub fn expand_task(_task: &ExpansionTask) -> Result { - unimplemented!() +pub fn expand_task(task: &ExpansionTask) -> Result { + let expander = dylib::Expander::new(&task.lib) + .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); + + match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { + Ok(expansion) => Ok(ExpansionResult { expansion }), + Err(msg) => { + let reason = format!( + "Cannot perform expansion for {}: error {:?}!", + &task.macro_name, + msg.as_str() + ); + Err(reason) + } + } } -pub fn list_macros(_task: &ListMacrosTask) -> Result { - unimplemented!() +pub fn list_macros(task: &ListMacrosTask) -> Result { + let expander = dylib::Expander::new(&task.lib) + .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); + + match expander.list_macros() { + Ok(macros) => Ok(ListMacrosResult { macros }), + Err(msg) => { + let reason = + format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str()); + Err(reason) + } + } } -- cgit v1.2.3 From 460ddde1762a429265445f7bfddc6589d56f48e6 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 10 Apr 2020 01:43:47 +0800 Subject: Add tests --- crates/ra_proc_macro_srv/Cargo.toml | 1 + crates/ra_proc_macro_srv/src/lib.rs | 3 + .../tests/fixtures/test_serialize_proc_macro.txt | 188 +++++++++++++++++++++ crates/ra_proc_macro_srv/src/tests/mod.rs | 47 ++++++ crates/ra_proc_macro_srv/src/tests/utils.rs | 65 +++++++ 5 files changed, 304 insertions(+) create mode 100644 crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt create mode 100644 crates/ra_proc_macro_srv/src/tests/mod.rs create mode 100644 crates/ra_proc_macro_srv/src/tests/utils.rs (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml index 437b8f475..1e0f50339 100644 --- a/crates/ra_proc_macro_srv/Cargo.toml +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -14,6 +14,7 @@ ra_mbe = { path = "../ra_mbe" } ra_proc_macro = { path = "../ra_proc_macro" } goblin = "0.2.1" libloading = "0.6.0" +test_utils = { path = "../test_utils" } [dev-dependencies] cargo_metadata = "0.9.1" diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 8fba73ac9..59716cbb3 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -52,3 +52,6 @@ pub fn list_macros(task: &ListMacrosTask) -> Result { } } } + +#[cfg(test)] +mod tests; diff --git a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt new file mode 100644 index 000000000..24507d98d --- /dev/null +++ b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt @@ -0,0 +1,188 @@ +SUBTREE $ + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT non_upper_case_globals 4294967295 + PUNCH , [alone] 4294967295 + IDENT unused_attributes 4294967295 + PUNCH , [alone] 4294967295 + IDENT unused_qualifications 4294967295 + IDENT const 4294967295 + IDENT _IMPL_SERIALIZE_FOR_Foo 4294967295 + PUNCH : [alone] 4294967295 + SUBTREE () 4294967295 + PUNCH = [alone] 4294967295 + SUBTREE {} 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT unknown_lints 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT cfg_attr 4294967295 + SUBTREE () 4294967295 + IDENT feature 4294967295 + PUNCH = [alone] 4294967295 + SUBTREE $ + LITERAL "cargo-clippy" 0 + PUNCH , [alone] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT useless_attribute 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT rust_2018_idioms 4294967295 + IDENT extern 4294967295 + IDENT crate 4294967295 + IDENT serde 4294967295 + IDENT as 4294967295 + IDENT _serde 4294967295 + PUNCH ; [alone] 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT unused_macros 4294967295 + IDENT macro_rules 4294967295 + PUNCH ! [alone] 4294967295 + IDENT try 4294967295 + SUBTREE {} 4294967295 + SUBTREE () 4294967295 + PUNCH $ [alone] 4294967295 + IDENT __expr 4294967295 + PUNCH : [alone] 4294967295 + IDENT expr 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT match 4294967295 + PUNCH $ [alone] 4294967295 + IDENT __expr 4294967295 + SUBTREE {} 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Ok 4294967295 + SUBTREE () 4294967295 + IDENT __val 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + IDENT __val 4294967295 + PUNCH , [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Err 4294967295 + SUBTREE () 4294967295 + IDENT __err 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT return 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Err 4294967295 + SUBTREE () 4294967295 + IDENT __err 4294967295 + PUNCH ; [alone] 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT automatically_derived 4294967295 + IDENT impl 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serialize 4294967295 + IDENT for 4294967295 + IDENT Foo 1 + SUBTREE {} 4294967295 + IDENT fn 4294967295 + IDENT serialize 4294967295 + PUNCH < [alone] 4294967295 + IDENT __S 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE () 4294967295 + PUNCH & [alone] 4294967295 + IDENT self 4294967295 + PUNCH , [alone] 4294967295 + IDENT __serializer 4294967295 + PUNCH : [alone] 4294967295 + IDENT __S 4294967295 + PUNCH - [joint] 4294967295 + PUNCH > [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Result 4294967295 + PUNCH < [alone] 4294967295 + IDENT __S 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Ok 4294967295 + PUNCH , [alone] 4294967295 + IDENT __S 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Error 4294967295 + PUNCH > [alone] 4294967295 + IDENT where 4294967295 + IDENT __S 4294967295 + PUNCH : [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serializer 4294967295 + PUNCH , [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT let 4294967295 + IDENT __serde_state 4294967295 + PUNCH = [alone] 4294967295 + IDENT try 4294967295 + PUNCH ! [alone] 4294967295 + SUBTREE () 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serializer 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT serialize_struct 4294967295 + SUBTREE () 4294967295 + IDENT __serializer 4294967295 + PUNCH , [alone] 4294967295 + LITERAL "Foo" 4294967295 + PUNCH , [alone] 4294967295 + IDENT false 4294967295 + IDENT as 4294967295 + IDENT usize 4294967295 + PUNCH ; [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT ser 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT SerializeStruct 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT end 4294967295 + SUBTREE () 4294967295 + IDENT __serde_state 4294967295 + PUNCH ; [alone] 4294967295 \ No newline at end of file diff --git a/crates/ra_proc_macro_srv/src/tests/mod.rs b/crates/ra_proc_macro_srv/src/tests/mod.rs new file mode 100644 index 000000000..03f79bc5d --- /dev/null +++ b/crates/ra_proc_macro_srv/src/tests/mod.rs @@ -0,0 +1,47 @@ +//! proc-macro tests + +#[macro_use] +mod utils; +use test_utils::assert_eq_text; +use utils::*; + +#[test] +fn test_derive_serialize_proc_macro() { + assert_expand( + "serde_derive", + "Serialize", + "1.0.104", + r##"struct Foo {}"##, + include_str!("fixtures/test_serialize_proc_macro.txt"), + ); +} + +#[test] +fn test_derive_serialize_proc_macro_failed() { + assert_expand( + "serde_derive", + "Serialize", + "1.0.104", + r##" + struct {} +"##, + r##" +SUBTREE $ + IDENT compile_error 4294967295 + PUNCH ! [alone] 4294967295 + SUBTREE {} 4294967295 + LITERAL "expected identifier" 4294967295 +"##, + ); +} + +#[test] +fn test_derive_proc_macro_list() { + let res = list("serde_derive", "1.0.104").join("\n"); + + assert_eq_text!( + &res, + r#"Serialize [CustomDerive] +Deserialize [CustomDerive]"# + ); +} diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs new file mode 100644 index 000000000..1ee409449 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -0,0 +1,65 @@ +//! utils used in proc-macro tests + +use crate::dylib; +use crate::list_macros; +pub use difference::Changeset as __Changeset; +use ra_proc_macro::ListMacrosTask; +use std::str::FromStr; +use test_utils::assert_eq_text; + +mod fixtures { + use cargo_metadata::{parse_messages, Message}; + use std::process::Command; + + // Use current project metadata to get the proc-macro dylib path + pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { + let command = Command::new("cargo") + .args(&["check", "--message-format", "json"]) + .output() + .unwrap() + .stdout; + + for message in parse_messages(command.as_slice()) { + match message.unwrap() { + Message::CompilerArtifact(artifact) => { + if artifact.target.kind.contains(&"proc-macro".to_string()) { + let repr = format!("{} {}", crate_name, version); + if artifact.package_id.repr.starts_with(&repr) { + return artifact.filenames[0].clone(); + } + } + } + _ => (), // Unknown message + } + } + + panic!("No proc-macro dylib for {} found!", crate_name); + } +} + +fn parse_string(code: &str) -> Option { + Some(crate::rustc_server::TokenStream::from_str(code).unwrap()) +} + +pub fn assert_expand( + crate_name: &str, + macro_name: &str, + version: &str, + fixture: &str, + expect: &str, +) { + let path = fixtures::dylib_path(crate_name, version); + let expander = dylib::Expander::new(&path).unwrap(); + let fixture = parse_string(fixture).unwrap(); + + let res = expander.expand(macro_name, &fixture.subtree, None).unwrap(); + assert_eq_text!(&format!("{:?}", res), &expect.trim()); +} + +pub fn list(crate_name: &str, version: &str) -> Vec { + let path = fixtures::dylib_path(crate_name, version); + let task = ListMacrosTask { lib: path }; + + let res = list_macros(&task).unwrap(); + res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() +} -- cgit v1.2.3 From 197039b9fe2b14fbaa81cae18fff855625f07def Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 10 Apr 2020 02:06:14 +0800 Subject: Remove allow(unused) --- crates/ra_proc_macro_srv/src/dylib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 525c7ac7b..738b10d56 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -91,8 +91,7 @@ fn load_library(file: &Path) -> Result { struct ProcMacroLibraryLibloading { // Hold the dylib to prevent it for unloadeding - #[allow(dead_code)] - lib: Library, + _lib: Library, exported_macros: Vec, } @@ -110,7 +109,7 @@ impl ProcMacroLibraryLibloading { macros.to_vec() }; - Ok(ProcMacroLibraryLibloading { lib, exported_macros }) + Ok(ProcMacroLibraryLibloading { _lib: lib, exported_macros }) } } -- cgit v1.2.3 From 857c03df079f919ddc0ea1c7c0a24313a66f2e2f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 10 Apr 2020 04:09:11 +0800 Subject: Fix macos symbol name bug --- crates/ra_proc_macro_srv/src/dylib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 738b10d56..eca9c8576 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -57,10 +57,28 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool { symbol.contains(NEW_REGISTRAR_SYMBOL) } +#[cfg(not(target_os = "macos"))] +fn adjust_symbol_name(name: &str) -> String { + name.to_string() +} + +#[cfg(target_os = "macos")] +fn adjust_symbol_name(s: &str) -> String { + // In macos doc: + // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html + // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be + // prepended with an underscore. + if s.starts_with("_") { + s[1..s.len()].to_string() + } else { + s.to_string() + } +} + fn find_registrar_symbol(file: &Path) -> Option { let symbols = get_symbols_from_lib(file)?; - symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| s.to_string()) + symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| adjust_symbol_name(&s)) } /// Loads dynamic library in platform dependent manner. -- cgit v1.2.3 From 5fc8f90a216adbd557fdf7fff08de1c49d91b5a3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 10 Apr 2020 19:06:53 +0800 Subject: tidy up a bit --- crates/ra_proc_macro_srv/src/dylib.rs | 68 ++++++++++------------------------- 1 file changed, 18 insertions(+), 50 deletions(-) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index eca9c8576..8403187b9 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -1,54 +1,50 @@ //! Handles dynamic library loading for proc macro use crate::{proc_macro::bridge, rustc_server::TokenStream}; -use std::fs::File; -use std::io::Read; use std::path::Path; use goblin::{mach::Mach, Object}; use libloading::Library; use ra_proc_macro::ProcMacroKind; -static NEW_REGISTRAR_SYMBOL: &str = "__rustc_proc_macro_decls_"; -static _OLD_REGISTRAR_SYMBOL: &str = "__rustc_derive_registrar_"; - -fn read_bytes(file: &Path) -> Option> { - let mut fd = File::open(file).ok()?; - let mut buffer = Vec::new(); - fd.read_to_end(&mut buffer).ok()?; - - Some(buffer) -} +static NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; fn get_symbols_from_lib(file: &Path) -> Option> { - let buffer = read_bytes(file)?; + let buffer = std::fs::read(file).ok()?; let object = Object::parse(&buffer).ok()?; return match object { Object::Elf(elf) => { let symbols = elf.dynstrtab.to_vec().ok()?; let names = symbols.iter().map(|s| s.to_string()).collect(); - Some(names) } - Object::PE(pe) => { let symbol_names = pe.exports.iter().flat_map(|s| s.name).map(|n| n.to_string()).collect(); Some(symbol_names) } - Object::Mach(mach) => match mach { Mach::Binary(binary) => { let exports = binary.exports().ok()?; - let names = exports.iter().map(|s| s.name.clone()).collect(); - + let names = exports + .iter() + .map(|s| { + // In macos doc: + // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html + // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be + // prepended with an underscore. + if s.name.starts_with("_") { + s.name[1..].to_string() + } else { + s.name.to_string() + } + }) + .collect(); Some(names) } - Mach::Fat(_) => None, }, - Object::Archive(_) | Object::Unknown(_) => None, }; } @@ -57,28 +53,10 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool { symbol.contains(NEW_REGISTRAR_SYMBOL) } -#[cfg(not(target_os = "macos"))] -fn adjust_symbol_name(name: &str) -> String { - name.to_string() -} - -#[cfg(target_os = "macos")] -fn adjust_symbol_name(s: &str) -> String { - // In macos doc: - // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html - // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be - // prepended with an underscore. - if s.starts_with("_") { - s[1..s.len()].to_string() - } else { - s.to_string() - } -} - fn find_registrar_symbol(file: &Path) -> Option { let symbols = get_symbols_from_lib(file)?; - symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| adjust_symbol_name(&s)) + symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| s.clone()) } /// Loads dynamic library in platform dependent manner. @@ -119,11 +97,9 @@ impl ProcMacroLibraryLibloading { .ok_or(format!("Cannot find registrar symbol in file {:?}", file))?; let lib = load_library(file).map_err(|e| e.to_string())?; - let exported_macros = { let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> = unsafe { lib.get(symbol_name.as_bytes()) }.map_err(|e| e.to_string())?; - macros.to_vec() }; @@ -140,7 +116,6 @@ pub struct Expander { impl Expander { pub fn new>(lib: &P) -> Result { let mut libs = vec![]; - /* Some libraries for dynamic loading require canonicalized path (even when it is already absolute */ @@ -177,20 +152,16 @@ impl Expander { crate::rustc_server::Rustc::default(), parsed_body, ); - return res.map(|it| it.subtree); } - bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { let res = client.run( &crate::proc_macro::bridge::server::SameThread, crate::rustc_server::Rustc::default(), parsed_body, ); - return res.map(|it| it.subtree); } - bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { let res = client.run( &crate::proc_macro::bridge::server::SameThread, @@ -201,10 +172,7 @@ impl Expander { return res.map(|it| it.subtree); } - - _ => { - continue; - } + _ => continue, } } } -- cgit v1.2.3 From 31d163aa3be9c938ffe713534e4f648550a35f6c Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 11 Apr 2020 14:53:13 +0800 Subject: Improve error handling --- crates/ra_proc_macro_srv/src/dylib.rs | 54 +++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 24 deletions(-) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 8403187b9..ec63d587b 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -7,28 +7,35 @@ use goblin::{mach::Mach, Object}; use libloading::Library; use ra_proc_macro::ProcMacroKind; -static NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; +use std::io::Error as IoError; +use std::io::ErrorKind as IoErrorKind; -fn get_symbols_from_lib(file: &Path) -> Option> { - let buffer = std::fs::read(file).ok()?; - let object = Object::parse(&buffer).ok()?; +const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; - return match object { +fn invalid_data_err(e: impl Into>) -> IoError { + IoError::new(IoErrorKind::InvalidData, e) +} + +fn get_symbols_from_lib(file: &Path) -> Result, IoError> { + let buffer = std::fs::read(file)?; + let object = Object::parse(&buffer).map_err(invalid_data_err)?; + + match object { Object::Elf(elf) => { - let symbols = elf.dynstrtab.to_vec().ok()?; + let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; let names = symbols.iter().map(|s| s.to_string()).collect(); - Some(names) + Ok(names) } Object::PE(pe) => { let symbol_names = pe.exports.iter().flat_map(|s| s.name).map(|n| n.to_string()).collect(); - Some(symbol_names) + Ok(symbol_names) } Object::Mach(mach) => match mach { Mach::Binary(binary) => { - let exports = binary.exports().ok()?; + let exports = binary.exports().map_err(invalid_data_err)?; let names = exports - .iter() + .into_iter() .map(|s| { // In macos doc: // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html @@ -37,26 +44,25 @@ fn get_symbols_from_lib(file: &Path) -> Option> { if s.name.starts_with("_") { s.name[1..].to_string() } else { - s.name.to_string() + s.name } }) .collect(); - Some(names) + Ok(names) } - Mach::Fat(_) => None, + Mach::Fat(_) => Ok(vec![]), }, - Object::Archive(_) | Object::Unknown(_) => None, - }; + Object::Archive(_) | Object::Unknown(_) => Ok(vec![]), + } } fn is_derive_registrar_symbol(symbol: &str) -> bool { symbol.contains(NEW_REGISTRAR_SYMBOL) } -fn find_registrar_symbol(file: &Path) -> Option { +fn find_registrar_symbol(file: &Path) -> Result, IoError> { let symbols = get_symbols_from_lib(file)?; - - symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| s.clone()) + Ok(symbols.into_iter().find(|s| is_derive_registrar_symbol(s))) } /// Loads dynamic library in platform dependent manner. @@ -92,14 +98,14 @@ struct ProcMacroLibraryLibloading { } impl ProcMacroLibraryLibloading { - fn open(file: &Path) -> Result { - let symbol_name = find_registrar_symbol(file) - .ok_or(format!("Cannot find registrar symbol in file {:?}", file))?; + fn open(file: &Path) -> Result { + let symbol_name = find_registrar_symbol(file)? + .ok_or(invalid_data_err(format!("Cannot find registrar symbol in file {:?}", file)))?; - let lib = load_library(file).map_err(|e| e.to_string())?; + let lib = load_library(file).map_err(invalid_data_err)?; let exported_macros = { let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> = - unsafe { lib.get(symbol_name.as_bytes()) }.map_err(|e| e.to_string())?; + unsafe { lib.get(symbol_name.as_bytes()) }.map_err(invalid_data_err)?; macros.to_vec() }; @@ -122,7 +128,7 @@ impl Expander { let lib = lib.as_ref().canonicalize().expect(&format!("Cannot canonicalize {:?}", lib.as_ref())); - let library = ProcMacroLibraryImpl::open(&lib)?; + let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; libs.push(library); Ok(Expander { libs }) -- cgit v1.2.3