2020-04-04 08:09:36 +00:00
|
|
|
//! Handles dynamic library loading for proc macro
|
|
|
|
|
2020-12-11 12:53:22 +00:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2020-04-04 08:09:36 +00:00
|
|
|
|
|
|
|
use libloading::Library;
|
2021-02-02 15:25:17 +00:00
|
|
|
use memmap2::Mmap;
|
2020-12-11 12:53:22 +00:00
|
|
|
use object::Object;
|
2020-08-13 10:07:28 +00:00
|
|
|
use proc_macro_api::ProcMacroKind;
|
2020-12-11 12:53:22 +00:00
|
|
|
|
|
|
|
use crate::{proc_macro::bridge, rustc_server::TokenStream};
|
2020-04-04 08:09:36 +00:00
|
|
|
|
2020-04-11 06:53:13 +00:00
|
|
|
const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_";
|
2020-04-04 08:09:36 +00:00
|
|
|
|
2020-04-20 18:26:10 +00:00
|
|
|
fn invalid_data_err(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> io::Error {
|
|
|
|
io::Error::new(io::ErrorKind::InvalidData, e)
|
2020-04-11 06:53:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:54:43 +00:00
|
|
|
fn is_derive_registrar_symbol(symbol: &str) -> bool {
|
2020-04-17 08:12:05 +00:00
|
|
|
symbol.contains(NEW_REGISTRAR_SYMBOL)
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:26:10 +00:00
|
|
|
fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> {
|
2020-04-17 07:47:15 +00:00
|
|
|
let file = File::open(file)?;
|
|
|
|
let buffer = unsafe { Mmap::map(&file)? };
|
2020-04-11 06:53:13 +00:00
|
|
|
|
2020-12-11 12:53:22 +00:00
|
|
|
Ok(object::File::parse(&buffer)
|
|
|
|
.map_err(invalid_data_err)?
|
|
|
|
.exports()
|
|
|
|
.map_err(invalid_data_err)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|export| export.name())
|
|
|
|
.filter_map(|sym| String::from_utf8(sym.into()).ok())
|
|
|
|
.find(|sym| is_derive_registrar_symbol(sym))
|
|
|
|
.map(|sym| {
|
|
|
|
// From MacOS docs:
|
|
|
|
// 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 cfg!(target_os = "macos") && sym.starts_with('_') {
|
|
|
|
sym[1..].to_owned()
|
|
|
|
} else {
|
|
|
|
sym
|
|
|
|
}
|
|
|
|
}))
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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, libloading::Error> {
|
2021-02-11 15:07:49 +00:00
|
|
|
unsafe { Library::new(file) }
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn load_library(file: &Path) -> Result<Library, libloading::Error> {
|
|
|
|
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;
|
|
|
|
|
2021-02-11 15:07:49 +00:00
|
|
|
unsafe { UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) }
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ProcMacroLibraryLibloading {
|
2020-04-20 18:26:10 +00:00
|
|
|
// Hold the dylib to prevent it from unloading
|
2020-04-09 18:06:14 +00:00
|
|
|
_lib: Library,
|
2020-04-04 08:09:36 +00:00
|
|
|
exported_macros: Vec<bridge::client::ProcMacro>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcMacroLibraryLibloading {
|
2020-04-20 18:26:10 +00:00
|
|
|
fn open(file: &Path) -> io::Result<Self> {
|
|
|
|
let symbol_name = find_registrar_symbol(file)?.ok_or_else(|| {
|
2020-04-20 18:54:43 +00:00
|
|
|
invalid_data_err(format!("Cannot find registrar symbol in file {}", file.display()))
|
2020-04-20 18:26:10 +00:00
|
|
|
})?;
|
2020-04-04 08:09:36 +00:00
|
|
|
|
2020-04-11 06:53:13 +00:00
|
|
|
let lib = load_library(file).map_err(invalid_data_err)?;
|
2020-04-04 08:09:36 +00:00
|
|
|
let exported_macros = {
|
|
|
|
let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> =
|
2020-04-11 06:53:13 +00:00
|
|
|
unsafe { lib.get(symbol_name.as_bytes()) }.map_err(invalid_data_err)?;
|
2020-04-04 08:09:36 +00:00
|
|
|
macros.to_vec()
|
|
|
|
};
|
|
|
|
|
2020-04-09 18:06:14 +00:00
|
|
|
Ok(ProcMacroLibraryLibloading { _lib: lib, exported_macros })
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Expander {
|
2020-04-26 09:58:56 +00:00
|
|
|
inner: ProcMacroLibraryLibloading,
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Expander {
|
2020-04-26 09:58:56 +00:00
|
|
|
pub fn new(lib: &Path) -> io::Result<Expander> {
|
2020-04-20 18:26:10 +00:00
|
|
|
// Some libraries for dynamic loading require canonicalized path even when it is
|
|
|
|
// already absolute
|
2020-04-26 09:58:56 +00:00
|
|
|
let lib = lib.canonicalize()?;
|
2020-04-04 08:09:36 +00:00
|
|
|
|
2020-04-26 09:58:56 +00:00
|
|
|
let lib = ensure_file_with_lock_free_access(&lib)?;
|
2020-04-25 04:29:49 +00:00
|
|
|
|
2020-04-26 09:58:56 +00:00
|
|
|
let library = ProcMacroLibraryLibloading::open(&lib)?;
|
2020-04-04 08:09:36 +00:00
|
|
|
|
2020-04-24 02:23:01 +00:00
|
|
|
Ok(Expander { inner: library })
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
macro_name: &str,
|
2020-08-12 14:46:20 +00:00
|
|
|
macro_body: &tt::Subtree,
|
|
|
|
attributes: Option<&tt::Subtree>,
|
|
|
|
) -> Result<tt::Subtree, bridge::PanicMessage> {
|
2020-04-04 08:09:36 +00:00
|
|
|
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())
|
|
|
|
});
|
|
|
|
|
2020-04-24 02:23:01 +00:00
|
|
|
for proc_macro in &self.inner.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,
|
2020-12-27 10:00:59 +00:00
|
|
|
false,
|
2020-04-24 02:23:01 +00:00
|
|
|
);
|
|
|
|
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,
|
2020-12-27 10:00:59 +00:00
|
|
|
false,
|
2020-04-24 02:23:01 +00:00
|
|
|
);
|
|
|
|
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,
|
2020-12-27 10:00:59 +00:00
|
|
|
false,
|
2020-04-24 02:23:01 +00:00
|
|
|
);
|
|
|
|
return res.map(|it| it.subtree);
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
2020-04-24 02:23:01 +00:00
|
|
|
_ => continue,
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(bridge::PanicMessage::String("Nothing to expand".to_string()))
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:26:10 +00:00
|
|
|
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
|
2020-04-24 02:23:01 +00:00
|
|
|
self.inner
|
|
|
|
.exported_macros
|
2020-04-20 18:26:10 +00:00
|
|
|
.iter()
|
|
|
|
.map(|proc_macro| 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
2020-04-04 08:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 04:29:49 +00:00
|
|
|
|
2020-04-26 09:58:56 +00:00
|
|
|
/// Copy the dylib to temp directory to prevent locking in Windows
|
2020-04-25 04:48:59 +00:00
|
|
|
#[cfg(windows)]
|
2020-04-26 09:58:56 +00:00
|
|
|
fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
|
2020-04-26 10:52:21 +00:00
|
|
|
use std::{ffi::OsString, time::SystemTime};
|
|
|
|
|
2020-04-25 04:29:49 +00:00
|
|
|
let mut to = std::env::temp_dir();
|
2020-04-26 10:52:21 +00:00
|
|
|
|
2020-04-25 04:29:49 +00:00
|
|
|
let file_name = path.file_name().ok_or_else(|| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
format!("File path is invalid: {}", path.display()),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2020-04-26 10:52:21 +00:00
|
|
|
// generate a time deps unique number
|
|
|
|
let t = SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("Time went backwards");
|
|
|
|
|
|
|
|
let mut unique_name = OsString::from(t.as_millis().to_string());
|
|
|
|
unique_name.push(file_name);
|
|
|
|
|
|
|
|
to.push(unique_name);
|
|
|
|
std::fs::copy(path, &to).unwrap();
|
2020-04-25 04:29:49 +00:00
|
|
|
Ok(to)
|
|
|
|
}
|
2020-04-25 04:48:59 +00:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2020-04-26 09:58:56 +00:00
|
|
|
fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
|
2020-04-25 04:48:59 +00:00
|
|
|
Ok(path.to_path_buf())
|
|
|
|
}
|