1 //! Handles dynamic library loading for proc macro
10 use libloading::Library;
13 use proc_macro_api::{read_dylib_info, ProcMacroKind};
17 const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_";
19 fn invalid_data_err(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> io::Error {
20 io::Error::new(io::ErrorKind::InvalidData, e)
23 fn is_derive_registrar_symbol(symbol: &str) -> bool {
24 symbol.contains(NEW_REGISTRAR_SYMBOL)
27 fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> {
28 let file = File::open(file)?;
29 let buffer = unsafe { Mmap::map(&file)? };
31 Ok(object::File::parse(&*buffer)
32 .map_err(invalid_data_err)?
34 .map_err(invalid_data_err)?
36 .map(|export| export.name())
37 .filter_map(|sym| String::from_utf8(sym.into()).ok())
38 .find(|sym| is_derive_registrar_symbol(sym))
41 // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
42 // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be
43 // prepended with an underscore.
44 if cfg!(target_os = "macos") && sym.starts_with('_') {
52 /// Loads dynamic library in platform dependent manner.
54 /// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
55 /// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
56 /// and [here](https://github.com/rust-lang/rust/issues/60593).
58 /// Usage of RTLD_DEEPBIND
59 /// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
61 /// It seems that on Windows that behaviour is default, so we do nothing in that case.
63 fn load_library(file: &Path) -> Result<Library, libloading::Error> {
64 unsafe { Library::new(file) }
68 fn load_library(file: &Path) -> Result<Library, libloading::Error> {
69 use libloading::os::unix::Library as UnixLibrary;
70 use std::os::raw::c_int;
72 const RTLD_NOW: c_int = 0x00002;
73 const RTLD_DEEPBIND: c_int = 0x00008;
75 unsafe { UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) }
79 pub enum LoadProcMacroDylibError {
81 LibLoading(libloading::Error),
85 impl fmt::Display for LoadProcMacroDylibError {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 Self::Io(e) => e.fmt(f),
89 Self::UnsupportedABI => write!(f, "unsupported ABI version"),
90 Self::LibLoading(e) => e.fmt(f),
95 impl From<io::Error> for LoadProcMacroDylibError {
96 fn from(e: io::Error) -> Self {
97 LoadProcMacroDylibError::Io(e)
101 impl From<libloading::Error> for LoadProcMacroDylibError {
102 fn from(e: libloading::Error) -> Self {
103 LoadProcMacroDylibError::LibLoading(e)
107 struct ProcMacroLibraryLibloading {
108 // Hold on to the library so it doesn't unload
113 impl ProcMacroLibraryLibloading {
114 fn open(file: &Path) -> Result<Self, LoadProcMacroDylibError> {
115 let symbol_name = find_registrar_symbol(file)?.ok_or_else(|| {
116 invalid_data_err(format!("Cannot find registrar symbol in file {}", file.display()))
119 let version_info = read_dylib_info(file)?;
121 let lib = load_library(file).map_err(invalid_data_err)?;
122 let abi = Abi::from_lib(&lib, symbol_name, version_info)?;
123 Ok(ProcMacroLibraryLibloading { _lib: lib, abi })
127 pub struct Expander {
128 inner: ProcMacroLibraryLibloading,
132 pub fn new(lib: &Path) -> Result<Expander, LoadProcMacroDylibError> {
133 // Some libraries for dynamic loading require canonicalized path even when it is
135 let lib = lib.canonicalize()?;
137 let lib = ensure_file_with_lock_free_access(&lib)?;
139 let library = ProcMacroLibraryLibloading::open(&lib)?;
141 Ok(Expander { inner: library })
147 macro_body: &tt::Subtree,
148 attributes: Option<&tt::Subtree>,
149 ) -> Result<tt::Subtree, String> {
150 let result = self.inner.abi.expand(macro_name, macro_body, attributes);
151 result.map_err(|e| e.as_str().unwrap_or_else(|| "<unknown error>".to_string()))
154 pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
155 self.inner.abi.list_macros()
159 /// Copy the dylib to temp directory to prevent locking in Windows
161 fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
162 use std::collections::hash_map::RandomState;
163 use std::ffi::OsString;
164 use std::hash::{BuildHasher, Hasher};
166 let mut to = std::env::temp_dir();
168 let file_name = path.file_name().ok_or_else(|| {
170 io::ErrorKind::InvalidInput,
171 format!("File path is invalid: {}", path.display()),
175 // Generate a unique number by abusing `HashMap`'s hasher.
176 // Maybe this will also "inspire" a libs team member to finally put `rand` in libstd.
177 let t = RandomState::new().build_hasher().finish();
179 let mut unique_name = OsString::from(t.to_string());
180 unique_name.push(file_name);
182 to.push(unique_name);
183 std::fs::copy(path, &to).unwrap();
188 fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
189 Ok(path.to_path_buf())