diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:35:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:35:29 +0000 |
commit | 59203c63bb777a3bacec32fb8830fba33540e809 (patch) | |
tree | 58298e711c0ff0575818c30485b44a2f21bf28a0 /third_party/rust/libloading/src | |
parent | Adding upstream version 126.0.1. (diff) | |
download | firefox-59203c63bb777a3bacec32fb8830fba33540e809.tar.xz firefox-59203c63bb777a3bacec32fb8830fba33540e809.zip |
Adding upstream version 127.0.upstream/127.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/libloading/src')
-rw-r--r-- | third_party/rust/libloading/src/changelog.rs | 45 | ||||
-rw-r--r-- | third_party/rust/libloading/src/error.rs | 8 | ||||
-rw-r--r-- | third_party/rust/libloading/src/lib.rs | 8 | ||||
-rw-r--r-- | third_party/rust/libloading/src/os/unix/consts.rs | 12 | ||||
-rw-r--r-- | third_party/rust/libloading/src/os/unix/mod.rs | 186 | ||||
-rw-r--r-- | third_party/rust/libloading/src/os/windows/mod.rs | 154 |
6 files changed, 243 insertions, 170 deletions
diff --git a/third_party/rust/libloading/src/changelog.rs b/third_party/rust/libloading/src/changelog.rs index 162544f34f..58101aecf9 100644 --- a/third_party/rust/libloading/src/changelog.rs +++ b/third_party/rust/libloading/src/changelog.rs @@ -1,5 +1,50 @@ //! The change log. +/// Release 0.8.3 (2024-03-05) +/// +/// ## Non-breaking changes +/// +/// A `dev-dependency` on `windows-sys` that was unconditionally introduced in +/// [0.8.2](r0_8_2) has been made conditional. +pub mod r0_8_3 {} + +/// Release 0.8.2 (2024-03-01) +/// +/// ## (Potentially) breaking changes +/// +/// MSRV has been increased to 1.56.0. Since both rustc versions are ancient, this has been deemed +/// to not be breaking enough to warrant a semver-breaking release of libloading. If you're stick +/// with a version of rustc older than 1.56.0, lock `libloading` dependency to `0.8.1`. +/// +/// ## Non-breaking changes +/// +/// * The crate switches the dependency on `windows-sys` to a `windows-target` one for Windows +/// bindings. In order to enable this `libloading` defines any bindings necessary for its operation +/// internally, just like has been done for `unix` targets. This should result in leaner dependency +/// trees. +/// * `os::unix::with_dlerror` has been exposed for the users who need to invoke `dl*` family of +/// functions manually. +pub mod r0_8_2 {} + +/// Release 0.8.1 (2023-09-30) +/// +/// ## Non-breaking changes +/// +/// * Support for GNU Hurd. +pub mod r0_8_1 {} + +/// Release 0.8.0 (2023-04-11) +/// +/// ## (Potentially) breaking changes +/// +/// * `winapi` dependency has been replaced with `windows-sys`. +/// * As a result the MSRV has been increased to 1.48. +/// +/// ## Non-breaking changes +/// +/// * Support for the QNX Neutrino target has been added. +pub mod r0_8_0 {} + /// Release 0.7.4 (2022-11-07) /// /// This release has no functional changes. diff --git a/third_party/rust/libloading/src/error.rs b/third_party/rust/libloading/src/error.rs index bd70ec39ce..ff4891c901 100644 --- a/third_party/rust/libloading/src/error.rs +++ b/third_party/rust/libloading/src/error.rs @@ -1,4 +1,4 @@ -use std::ffi::CString; +use std::ffi::{CString, CStr}; /// A `dlerror` error. pub struct DlDescription(pub(crate) CString); @@ -9,6 +9,12 @@ impl std::fmt::Debug for DlDescription { } } +impl From<&CStr> for DlDescription { + fn from(value: &CStr) -> Self { + Self(value.into()) + } +} + /// A Windows API error. pub struct WindowsError(pub(crate) std::io::Error); diff --git a/third_party/rust/libloading/src/lib.rs b/third_party/rust/libloading/src/lib.rs index 6f0e4cb7f7..3ddf98a34b 100644 --- a/third_party/rust/libloading/src/lib.rs +++ b/third_party/rust/libloading/src/lib.rs @@ -16,7 +16,7 @@ //! //! ```toml //! [dependencies] -//! libloading = "0.7" +//! libloading = "0.8" //! ``` //! //! # Usage @@ -41,15 +41,13 @@ pub mod changelog; pub mod os; mod util; - mod error; -pub use self::error::Error; - #[cfg(any(unix, windows, libloading_docs))] mod safe; + +pub use self::error::Error; #[cfg(any(unix, windows, libloading_docs))] pub use self::safe::{Library, Symbol}; - use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; use std::ffi::{OsStr, OsString}; diff --git a/third_party/rust/libloading/src/os/unix/consts.rs b/third_party/rust/libloading/src/os/unix/consts.rs index ea7a6a102d..ed3edaf829 100644 --- a/third_party/rust/libloading/src/os/unix/consts.rs +++ b/third_party/rust/libloading/src/os/unix/consts.rs @@ -82,6 +82,8 @@ mod posix { target_os = "fuchsia", target_os = "redox", + target_os = "nto", + target_os = "hurd", ))] { pub(super) const RTLD_LAZY: c_int = 1; } else { @@ -115,6 +117,8 @@ mod posix { target_os = "fuchsia", target_os = "redox", + target_os = "nto", + target_os = "hurd", ))] { pub(super) const RTLD_NOW: c_int = 2; } else if #[cfg(all(target_os = "android",target_pointer_width = "32"))] { @@ -162,6 +166,8 @@ mod posix { target_os = "fuchsia", target_os = "redox", + target_os = "nto", + target_os = "hurd", ))] { pub(super) const RTLD_GLOBAL: c_int = 0x100; } else { @@ -172,7 +178,10 @@ mod posix { } cfg_if! { - if #[cfg(target_os = "netbsd")] { + if #[cfg(any( + target_os = "netbsd", + target_os = "nto", + ))] { pub(super) const RTLD_LOCAL: c_int = 0x200; } else if #[cfg(target_os = "aix")] { pub(super) const RTLD_LOCAL: c_int = 0x80000; @@ -200,6 +209,7 @@ mod posix { target_os = "fuchsia", target_os = "redox", + target_os = "hurd", ))] { pub(super) const RTLD_LOCAL: c_int = 0; } else { diff --git a/third_party/rust/libloading/src/os/unix/mod.rs b/third_party/rust/libloading/src/os/unix/mod.rs index df7efdad54..6347e02700 100644 --- a/third_party/rust/libloading/src/os/unix/mod.rs +++ b/third_party/rust/libloading/src/os/unix/mod.rs @@ -16,18 +16,33 @@ use util::{cstr_cow_from_bytes, ensure_compatible_types}; mod consts; -// dl* family of functions did not have enough thought put into it. -// -// Whole error handling scheme is done via setting and querying some global state, therefore it is -// not safe to use dynamic library loading in MT-capable environment at all. Only in POSIX 2008+TC1 -// a thread-local state was allowed for `dlerror`, making the dl* family of functions MT-safe. -// -// In practice (as of 2020-04-01) most of the widely used targets use a thread-local for error -// state and have been doing so for a long time. Regardless the comments in this function shall -// remain as a documentation for the future generations. -fn with_dlerror<T, F>(wrap: fn(crate::error::DlDescription) -> crate::Error, closure: F) --> Result<T, Option<crate::Error>> -where F: FnOnce() -> Option<T> { +/// Run code and handle errors reported by `dlerror`. +/// +/// This function first executes the `closure` function containing calls to the functions that +/// report their errors via `dlerror`. This closure may return either `None` or `Some(*)` to +/// further affect operation of this function. +/// +/// In case the `closure` returns `None`, `with_dlerror` inspects the `dlerror`. `dlerror` may +/// decide to not provide any error description, in which case `Err(None)` is returned to the +/// caller. Otherwise the `error` callback is invoked to allow inspection and conversion of the +/// error message. The conversion result is returned as `Err(Some(Error))`. +/// +/// If the operations that report their errors via `dlerror` were all successful, `closure` should +/// return `Some(T)` instead. In this case `dlerror` is not inspected at all. +/// +/// # Notes +/// +/// The whole `dlerror` handling scheme is done via setting and querying some global state. For +/// that reason it is not safe to use dynamic library loading in MT-capable environment at all. +/// Only in POSIX 2008+TC1 a thread-local state was allowed for `dlerror`, making the dl* family of +/// functions possibly MT-safe, depending on the implementation of `dlerror`. +/// +/// In practice (as of 2020-04-01) most of the widely used targets use a thread-local for error +/// state and have been doing so for a long time. +pub fn with_dlerror<T, F, Error>(closure: F, error: fn(&CStr) -> Error) -> Result<T, Option<Error>> +where + F: FnOnce() -> Option<T>, +{ // We used to guard all uses of dl* functions with our own mutex. This made them safe to use in // MT programs provided the only way a program used dl* was via this library. However, it also // had a number of downsides or cases where it failed to handle the problems. For instance, @@ -53,8 +68,8 @@ where F: FnOnce() -> Option<T> { // or a bug in implementation of dl* family of functions. closure().ok_or_else(|| unsafe { // This code will only get executed if the `closure` returns `None`. - let error = dlerror(); - if error.is_null() { + let dlerror_str = dlerror(); + if dlerror_str.is_null() { // In non-dlsym case this may happen when there’re bugs in our bindings or there’s // non-libloading user of libdl; possibly in another thread. None @@ -64,8 +79,7 @@ where F: FnOnce() -> Option<T> { // ownership over the message? // TODO: should do locale-aware conversion here. OTOH Rust doesn’t seem to work well in // any system that uses non-utf8 locale, so I doubt there’s a problem here. - let message = CStr::from_ptr(error).into(); - Some(wrap(crate::error::DlDescription(message))) + Some(error(CStr::from_ptr(dlerror_str))) // Since we do a copy of the error string above, maybe we should call dlerror again to // let libdl know it may free its copy of the string now? } @@ -74,7 +88,7 @@ where F: FnOnce() -> Option<T> { /// A platform-specific counterpart of the cross-platform [`Library`](crate::Library). pub struct Library { - handle: *mut raw::c_void + handle: *mut raw::c_void, } unsafe impl Send for Library {} @@ -164,30 +178,38 @@ impl Library { /// termination routines contained within the library is safe as well. These routines may be /// executed when the library is unloaded. pub unsafe fn open<P>(filename: Option<P>, flags: raw::c_int) -> Result<Library, crate::Error> - where P: AsRef<OsStr> { + where + P: AsRef<OsStr>, + { let filename = match filename { None => None, Some(ref f) => Some(cstr_cow_from_bytes(f.as_ref().as_bytes())?), }; - with_dlerror(|desc| crate::Error::DlOpen { desc }, move || { - let result = dlopen(match filename { - None => ptr::null(), - Some(ref f) => f.as_ptr() - }, flags); - // ensure filename lives until dlopen completes - drop(filename); - if result.is_null() { - None - } else { - Some(Library { - handle: result - }) - } - }).map_err(|e| e.unwrap_or(crate::Error::DlOpenUnknown)) + with_dlerror( + move || { + let result = dlopen( + match filename { + None => ptr::null(), + Some(ref f) => f.as_ptr(), + }, + flags, + ); + // ensure filename lives until dlopen completes + drop(filename); + if result.is_null() { + None + } else { + Some(Library { handle: result }) + } + }, + |desc| crate::Error::DlOpen { desc: desc.into() }, + ) + .map_err(|e| e.unwrap_or(crate::Error::DlOpenUnknown)) } unsafe fn get_impl<T, F>(&self, symbol: &[u8], on_null: F) -> Result<Symbol<T>, crate::Error> - where F: FnOnce() -> Result<Symbol<T>, crate::Error> + where + F: FnOnce() -> Result<Symbol<T>, crate::Error>, { ensure_compatible_types::<T, *mut raw::c_void>()?; let symbol = cstr_cow_from_bytes(symbol)?; @@ -197,23 +219,26 @@ impl Library { // // We try to leave as little space as possible for this to occur, but we can’t exactly // fully prevent it. - match with_dlerror(|desc| crate::Error::DlSym { desc }, || { - dlerror(); - let symbol = dlsym(self.handle, symbol.as_ptr()); - if symbol.is_null() { - None - } else { - Some(Symbol { - pointer: symbol, - pd: marker::PhantomData - }) - } - }) { + let result = with_dlerror( + || { + dlerror(); + let symbol = dlsym(self.handle, symbol.as_ptr()); + if symbol.is_null() { + None + } else { + Some(Symbol { + pointer: symbol, + pd: marker::PhantomData, + }) + } + }, + |desc| crate::Error::DlSym { desc: desc.into() }, + ); + match result { Err(None) => on_null(), Err(Some(e)) => Err(e), - Ok(x) => Ok(x) + Ok(x) => Ok(x), } - } /// Get a pointer to a function or static variable by symbol name. @@ -284,10 +309,12 @@ impl Library { /// variables that work on e.g. Linux may have unintended behaviour on other targets. #[inline(always)] pub unsafe fn get_singlethreaded<T>(&self, symbol: &[u8]) -> Result<Symbol<T>, crate::Error> { - self.get_impl(symbol, || Ok(Symbol { - pointer: ptr::null_mut(), - pd: marker::PhantomData - })) + self.get_impl(symbol, || { + Ok(Symbol { + pointer: ptr::null_mut(), + pd: marker::PhantomData, + }) + }) } /// Convert the `Library` to a raw handle. @@ -308,9 +335,7 @@ impl Library { /// pointer previously returned by `Library::into_raw` call. It must be valid to call `dlclose` /// with this pointer as an argument. pub unsafe fn from_raw(handle: *mut raw::c_void) -> Library { - Library { - handle - } + Library { handle } } /// Unload the library. @@ -324,13 +349,17 @@ impl Library { /// /// The underlying data structures may still get leaked if an error does occur. pub fn close(self) -> Result<(), crate::Error> { - let result = with_dlerror(|desc| crate::Error::DlClose { desc }, || { - if unsafe { dlclose(self.handle) } == 0 { - Some(()) - } else { - None - } - }).map_err(|e| e.unwrap_or(crate::Error::DlCloseUnknown)); + let result = with_dlerror( + || { + if unsafe { dlclose(self.handle) } == 0 { + Some(()) + } else { + None + } + }, + |desc| crate::Error::DlClose { desc: desc.into() }, + ) + .map_err(|e| e.unwrap_or(crate::Error::DlCloseUnknown)); // While the library is not free'd yet in case of an error, there is no reason to try // dropping it again, because all that will do is try calling `dlclose` again. only // this time it would ignore the return result, which we already seen failing… @@ -359,7 +388,7 @@ impl fmt::Debug for Library { /// `Symbol` does not outlive the `Library` it comes from. pub struct Symbol<T> { pointer: *mut raw::c_void, - pd: marker::PhantomData<T> + pd: marker::PhantomData<T>, } impl<T> Symbol<T> { @@ -409,13 +438,18 @@ impl<T> fmt::Debug for Symbol<T> { if dladdr(self.pointer, info.as_mut_ptr()) != 0 { let info = info.assume_init(); if info.dli_sname.is_null() { - f.write_str(&format!("Symbol@{:p} from {:?}", - self.pointer, - CStr::from_ptr(info.dli_fname))) + f.write_str(&format!( + "Symbol@{:p} from {:?}", + self.pointer, + CStr::from_ptr(info.dli_fname) + )) } else { - f.write_str(&format!("Symbol {:?}@{:p} from {:?}", - CStr::from_ptr(info.dli_sname), self.pointer, - CStr::from_ptr(info.dli_fname))) + f.write_str(&format!( + "Symbol {:?}@{:p} from {:?}", + CStr::from_ptr(info.dli_sname), + self.pointer, + CStr::from_ptr(info.dli_fname) + )) } } else { f.write_str(&format!("Symbol@{:p}", self.pointer)) @@ -425,9 +459,9 @@ impl<T> fmt::Debug for Symbol<T> { } // Platform specific things -#[cfg_attr(any(target_os = "linux", target_os = "android"), link(name="dl"))] -#[cfg_attr(any(target_os = "freebsd", target_os = "dragonfly"), link(name="c"))] -extern { +#[cfg_attr(any(target_os = "linux", target_os = "android"), link(name = "dl"))] +#[cfg_attr(any(target_os = "freebsd", target_os = "dragonfly"), link(name = "c"))] +extern "C" { fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void; fn dlclose(handle: *mut raw::c_void) -> raw::c_int; fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void; @@ -437,8 +471,8 @@ extern { #[repr(C)] struct DlInfo { - dli_fname: *const raw::c_char, - dli_fbase: *mut raw::c_void, - dli_sname: *const raw::c_char, - dli_saddr: *mut raw::c_void + dli_fname: *const raw::c_char, + dli_fbase: *mut raw::c_void, + dli_sname: *const raw::c_char, + dli_saddr: *mut raw::c_void, } diff --git a/third_party/rust/libloading/src/os/windows/mod.rs b/third_party/rust/libloading/src/os/windows/mod.rs index e3da940a29..172801e168 100644 --- a/third_party/rust/libloading/src/os/windows/mod.rs +++ b/third_party/rust/libloading/src/os/windows/mod.rs @@ -1,53 +1,18 @@ // A hack for docs.rs to build documentation that has both windows and linux documentation in the // same rustdoc build visible. #[cfg(all(libloading_docs, not(windows)))] -mod windows_imports { - pub(super) enum WORD {} - pub(super) struct DWORD; - pub(super) enum HMODULE {} - pub(super) enum FARPROC {} - - pub(super) mod consts { - use super::DWORD; - pub(crate) const LOAD_IGNORE_CODE_AUTHZ_LEVEL: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_AS_DATAFILE: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_AS_IMAGE_RESOURCE: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SEARCH_APPLICATION_DIR: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SEARCH_SYSTEM32: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SEARCH_USER_DIRS: DWORD = DWORD; - pub(crate) const LOAD_WITH_ALTERED_SEARCH_PATH: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_REQUIRE_SIGNED_TARGET: DWORD = DWORD; - pub(crate) const LOAD_LIBRARY_SAFE_CURRENT_DIRS: DWORD = DWORD; - } -} +mod windows_imports {} #[cfg(any(not(libloading_docs), windows))] mod windows_imports { - extern crate winapi; - pub(super) use self::winapi::shared::minwindef::{WORD, DWORD, HMODULE, FARPROC}; - pub(super) use self::winapi::shared::ntdef::WCHAR; - pub(super) use self::winapi::um::{errhandlingapi, libloaderapi}; + use super::{DWORD, BOOL, HANDLE, HMODULE, FARPROC}; pub(super) use std::os::windows::ffi::{OsStrExt, OsStringExt}; - pub(super) const SEM_FAILCE: DWORD = 1; - - pub(super) mod consts { - pub(crate) use super::winapi::um::libloaderapi::{ - LOAD_IGNORE_CODE_AUTHZ_LEVEL, - LOAD_LIBRARY_AS_DATAFILE, - LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE, - LOAD_LIBRARY_AS_IMAGE_RESOURCE, - LOAD_LIBRARY_SEARCH_APPLICATION_DIR, - LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, - LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR, - LOAD_LIBRARY_SEARCH_SYSTEM32, - LOAD_LIBRARY_SEARCH_USER_DIRS, - LOAD_WITH_ALTERED_SEARCH_PATH, - LOAD_LIBRARY_REQUIRE_SIGNED_TARGET, - LOAD_LIBRARY_SAFE_CURRENT_DIRS, - }; - } + windows_targets::link!("kernel32.dll" "system" fn GetLastError() -> DWORD); + windows_targets::link!("kernel32.dll" "system" fn SetThreadErrorMode(new_mode: DWORD, old_mode: *mut DWORD) -> BOOL); + windows_targets::link!("kernel32.dll" "system" fn GetModuleHandleExW(flags: u32, module_name: *const u16, module: *mut HMODULE) -> BOOL); + windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(module: HMODULE) -> BOOL); + windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExW(filename: *const u16, file: HANDLE, flags: DWORD) -> HMODULE); + windows_targets::link!("kernel32.dll" "system" fn GetModuleFileNameW(module: HMODULE, filename: *mut u16, size: DWORD) -> DWORD); + windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(module: HMODULE, procname: *const u8) -> FARPROC); } use self::windows_imports::*; @@ -116,9 +81,9 @@ impl Library { /// [MSDN]: https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw pub fn this() -> Result<Library, crate::Error> { unsafe { - let mut handle: HMODULE = std::ptr::null_mut(); + let mut handle: HMODULE = 0; with_get_last_error(|source| crate::Error::GetModuleHandleExW { source }, || { - let result = libloaderapi::GetModuleHandleExW(0, std::ptr::null_mut(), &mut handle); + let result = GetModuleHandleExW(0, std::ptr::null_mut(), &mut handle); if result == 0 { None } else { @@ -149,11 +114,11 @@ impl Library { let wide_filename: Vec<u16> = filename.as_ref().encode_wide().chain(Some(0)).collect(); let ret = unsafe { - let mut handle: HMODULE = std::ptr::null_mut(); + let mut handle: HMODULE = 0; with_get_last_error(|source| crate::Error::GetModuleHandleExW { source }, || { // Make sure no winapi calls as a result of drop happen inside this closure, because // otherwise that might change the return value of the GetLastError. - let result = libloaderapi::GetModuleHandleExW(0, wide_filename.as_ptr(), &mut handle); + let result = GetModuleHandleExW(0, wide_filename.as_ptr(), &mut handle); if result == 0 { None } else { @@ -186,16 +151,15 @@ impl Library { /// Additionally, the callers of this function must also ensure that execution of the /// termination routines contained within the library is safe as well. These routines may be /// executed when the library is unloaded. - pub unsafe fn load_with_flags<P: AsRef<OsStr>>(filename: P, flags: DWORD) -> Result<Library, crate::Error> { + pub unsafe fn load_with_flags<P: AsRef<OsStr>>(filename: P, flags: LOAD_LIBRARY_FLAGS) -> Result<Library, crate::Error> { let wide_filename: Vec<u16> = filename.as_ref().encode_wide().chain(Some(0)).collect(); let _guard = ErrorModeGuard::new(); let ret = with_get_last_error(|source| crate::Error::LoadLibraryExW { source }, || { // Make sure no winapi calls as a result of drop happen inside this closure, because // otherwise that might change the return value of the GetLastError. - let handle = - libloaderapi::LoadLibraryExW(wide_filename.as_ptr(), std::ptr::null_mut(), flags); - if handle.is_null() { + let handle = LoadLibraryExW(wide_filename.as_ptr(), 0, flags); + if handle == 0 { None } else { Some(Library(handle)) @@ -221,8 +185,8 @@ impl Library { ensure_compatible_types::<T, FARPROC>()?; let symbol = cstr_cow_from_bytes(symbol)?; with_get_last_error(|source| crate::Error::GetProcAddress { source }, || { - let symbol = libloaderapi::GetProcAddress(self.0, symbol.as_ptr()); - if symbol.is_null() { + let symbol = GetProcAddress(self.0, symbol.as_ptr().cast()); + if symbol.is_none() { None } else { Some(Symbol { @@ -238,12 +202,12 @@ impl Library { /// # Safety /// /// Users of this API must specify the correct type of the function or variable loaded. - pub unsafe fn get_ordinal<T>(&self, ordinal: WORD) -> Result<Symbol<T>, crate::Error> { + pub unsafe fn get_ordinal<T>(&self, ordinal: u16) -> Result<Symbol<T>, crate::Error> { ensure_compatible_types::<T, FARPROC>()?; with_get_last_error(|source| crate::Error::GetProcAddress { source }, || { - let ordinal = ordinal as usize as *mut _; - let symbol = libloaderapi::GetProcAddress(self.0, ordinal); - if symbol.is_null() { + let ordinal = ordinal as usize as *const _; + let symbol = GetProcAddress(self.0, ordinal); + if symbol.is_none() { None } else { Some(Symbol { @@ -280,7 +244,7 @@ impl Library { /// The underlying data structures may still get leaked if an error does occur. pub fn close(self) -> Result<(), crate::Error> { let result = with_get_last_error(|source| crate::Error::FreeLibrary { source }, || { - if unsafe { libloaderapi::FreeLibrary(self.0) == 0 } { + if unsafe { FreeLibrary(self.0) == 0 } { None } else { Some(()) @@ -296,7 +260,7 @@ impl Library { impl Drop for Library { fn drop(&mut self) { - unsafe { libloaderapi::FreeLibrary(self.0); } + unsafe { FreeLibrary(self.0); } } } @@ -305,17 +269,17 @@ impl fmt::Debug for Library { unsafe { // FIXME: use Maybeuninit::uninit_array when stable let mut buf = - mem::MaybeUninit::<[mem::MaybeUninit::<WCHAR>; 1024]>::uninit().assume_init(); - let len = libloaderapi::GetModuleFileNameW(self.0, + mem::MaybeUninit::<[mem::MaybeUninit<u16>; 1024]>::uninit().assume_init(); + let len = GetModuleFileNameW(self.0, buf[..].as_mut_ptr().cast(), 1024) as usize; if len == 0 { - f.write_str(&format!("Library@{:p}", self.0)) + f.write_str(&format!("Library@{:#x}", self.0)) } else { let string: OsString = OsString::from_wide( // FIXME: use Maybeuninit::slice_get_ref when stable - &*(&buf[..len] as *const [_] as *const [WCHAR]) + &*(&buf[..len] as *const [_] as *const [u16]), ); - f.write_str(&format!("Library@{:p} from {:?}", self.0, string)) + f.write_str(&format!("Library@{:#x} from {:?}", self.0, string)) } } } @@ -340,7 +304,7 @@ impl<T> Symbol<T> { impl<T> Symbol<Option<T>> { /// Lift Option out of the symbol. pub fn lift_option(self) -> Option<Symbol<T>> { - if self.pointer.is_null() { + if self.pointer.is_none() { None } else { Some(Symbol { @@ -363,16 +327,16 @@ impl<T> Clone for Symbol<T> { impl<T> ::std::ops::Deref for Symbol<T> { type Target = T; fn deref(&self) -> &T { - unsafe { - // Additional reference level for a dereference on `deref` return value. - &*(&self.pointer as *const *mut _ as *const T) - } + unsafe { &*((&self.pointer) as *const FARPROC as *const T) } } } impl<T> fmt::Debug for Symbol<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&format!("Symbol@{:p}", self.pointer)) + match self.pointer { + None => f.write_str("Symbol@0x0"), + Some(ptr) => f.write_str(&format!("Symbol@{:p}", ptr as *const ())), + } } } @@ -383,13 +347,13 @@ impl ErrorModeGuard { fn new() -> Option<ErrorModeGuard> { unsafe { let mut previous_mode = 0; - if errhandlingapi::SetThreadErrorMode(SEM_FAILCE, &mut previous_mode) == 0 { + if SetThreadErrorMode(SEM_FAILCRITICALERRORS, &mut previous_mode) == 0 { // How in the world is it possible for what is essentially a simple variable swap // to fail? For now we just ignore the error -- the worst that can happen here is // the previous mode staying on and user seeing a dialog error on older Windows // machines. None - } else if previous_mode == SEM_FAILCE { + } else if previous_mode == SEM_FAILCRITICALERRORS { None } else { Some(ErrorModeGuard(previous_mode)) @@ -401,7 +365,7 @@ impl ErrorModeGuard { impl Drop for ErrorModeGuard { fn drop(&mut self) { unsafe { - errhandlingapi::SetThreadErrorMode(self.0, ptr::null_mut()); + SetThreadErrorMode(self.0, ptr::null_mut()); } } } @@ -410,7 +374,7 @@ fn with_get_last_error<T, F>(wrap: fn(crate::error::WindowsError) -> crate::Erro -> Result<T, Option<crate::Error>> where F: FnOnce() -> Option<T> { closure().ok_or_else(|| { - let error = unsafe { errhandlingapi::GetLastError() }; + let error = unsafe { GetLastError() }; if error == 0 { None } else { @@ -419,13 +383,29 @@ where F: FnOnce() -> Option<T> { }) } + +#[allow(clippy::upper_case_acronyms)] +type BOOL = i32; +#[allow(clippy::upper_case_acronyms)] +type DWORD = u32; +#[allow(clippy::upper_case_acronyms)] +type HANDLE = isize; +#[allow(clippy::upper_case_acronyms)] +type HMODULE = isize; +#[allow(clippy::upper_case_acronyms)] +type FARPROC = Option<unsafe extern "system" fn() -> isize>; +#[allow(non_camel_case_types)] +type LOAD_LIBRARY_FLAGS = DWORD; + +const SEM_FAILCRITICALERRORS: DWORD = 1; + /// Do not check AppLocker rules or apply Software Restriction Policies for the DLL. /// /// This action applies only to the DLL being loaded and not to its dependencies. This value is /// recommended for use in setup programs that must run extracted DLLs during installation. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_IGNORE_CODE_AUTHZ_LEVEL: DWORD = consts::LOAD_IGNORE_CODE_AUTHZ_LEVEL; +pub const LOAD_IGNORE_CODE_AUTHZ_LEVEL: LOAD_LIBRARY_FLAGS = 0x00000010; /// Map the file into the calling process’ virtual address space as if it were a data file. /// @@ -435,7 +415,7 @@ pub const LOAD_IGNORE_CODE_AUTHZ_LEVEL: DWORD = consts::LOAD_IGNORE_CODE_AUTHZ_L /// messages or resources from it. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_AS_DATAFILE: DWORD = consts::LOAD_LIBRARY_AS_DATAFILE; +pub const LOAD_LIBRARY_AS_DATAFILE: LOAD_LIBRARY_FLAGS = 0x00000002; /// Map the file into the calling process’ virtual address space as if it were a data file. /// @@ -444,7 +424,7 @@ pub const LOAD_LIBRARY_AS_DATAFILE: DWORD = consts::LOAD_LIBRARY_AS_DATAFILE; /// while it is in use. However, the DLL can still be opened by other processes. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE: DWORD = consts::LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE; +pub const LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE: LOAD_LIBRARY_FLAGS = 0x00000040; /// Map the file into the process’ virtual address space as an image file. /// @@ -456,7 +436,7 @@ pub const LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE: DWORD = consts::LOAD_LIBRARY_AS_DA /// [`LOAD_LIBRARY_AS_DATAFILE`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_AS_IMAGE_RESOURCE: DWORD = consts::LOAD_LIBRARY_AS_IMAGE_RESOURCE; +pub const LOAD_LIBRARY_AS_IMAGE_RESOURCE: LOAD_LIBRARY_FLAGS = 0x00000020; /// Search the application's installation directory for the DLL and its dependencies. /// @@ -464,7 +444,7 @@ pub const LOAD_LIBRARY_AS_IMAGE_RESOURCE: DWORD = consts::LOAD_LIBRARY_AS_IMAGE_ /// [`LOAD_WITH_ALTERED_SEARCH_PATH`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SEARCH_APPLICATION_DIR: DWORD = consts::LOAD_LIBRARY_SEARCH_APPLICATION_DIR; +pub const LOAD_LIBRARY_SEARCH_APPLICATION_DIR: LOAD_LIBRARY_FLAGS = 0x00000200; /// Search default directories when looking for the DLL and its dependencies. /// @@ -474,7 +454,7 @@ pub const LOAD_LIBRARY_SEARCH_APPLICATION_DIR: DWORD = consts::LOAD_LIBRARY_SEAR /// [`LOAD_WITH_ALTERED_SEARCH_PATH`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: DWORD = consts::LOAD_LIBRARY_SEARCH_DEFAULT_DIRS; +pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: LOAD_LIBRARY_FLAGS = 0x00001000; /// Directory that contains the DLL is temporarily added to the beginning of the list of /// directories that are searched for the DLL’s dependencies. @@ -485,7 +465,7 @@ pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: DWORD = consts::LOAD_LIBRARY_SEARCH_ /// with [`LOAD_WITH_ALTERED_SEARCH_PATH`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: DWORD = consts::LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR; +pub const LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: LOAD_LIBRARY_FLAGS = 0x00000100; /// Search `%windows%\system32` for the DLL and its dependencies. /// @@ -493,7 +473,7 @@ pub const LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: DWORD = consts::LOAD_LIBRARY_SEARCH_ /// [`LOAD_WITH_ALTERED_SEARCH_PATH`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SEARCH_SYSTEM32: DWORD = consts::LOAD_LIBRARY_SEARCH_SYSTEM32; +pub const LOAD_LIBRARY_SEARCH_SYSTEM32: LOAD_LIBRARY_FLAGS = 0x00000800; /// Directories added using the `AddDllDirectory` or the `SetDllDirectory` function are searched /// for the DLL and its dependencies. @@ -503,7 +483,7 @@ pub const LOAD_LIBRARY_SEARCH_SYSTEM32: DWORD = consts::LOAD_LIBRARY_SEARCH_SYST /// combined with [`LOAD_WITH_ALTERED_SEARCH_PATH`]. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SEARCH_USER_DIRS: DWORD = consts::LOAD_LIBRARY_SEARCH_USER_DIRS; +pub const LOAD_LIBRARY_SEARCH_USER_DIRS: LOAD_LIBRARY_FLAGS = 0x00000400; /// If `filename` specifies an absolute path, the system uses the alternate file search strategy /// discussed in the [Remarks section] to find associated executable modules that the specified @@ -518,15 +498,15 @@ pub const LOAD_LIBRARY_SEARCH_USER_DIRS: DWORD = consts::LOAD_LIBRARY_SEARCH_USE /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). /// /// [Remarks]: https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#remarks -pub const LOAD_WITH_ALTERED_SEARCH_PATH: DWORD = consts::LOAD_WITH_ALTERED_SEARCH_PATH; +pub const LOAD_WITH_ALTERED_SEARCH_PATH: LOAD_LIBRARY_FLAGS = 0x00000008; /// Specifies that the digital signature of the binary image must be checked at load time. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_REQUIRE_SIGNED_TARGET: DWORD = consts::LOAD_LIBRARY_REQUIRE_SIGNED_TARGET; +pub const LOAD_LIBRARY_REQUIRE_SIGNED_TARGET: LOAD_LIBRARY_FLAGS = 0x00000080; /// Allow loading a DLL for execution from the current directory only if it is under a directory in /// the Safe load list. /// /// See [flag documentation on MSDN](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw#parameters). -pub const LOAD_LIBRARY_SAFE_CURRENT_DIRS: DWORD = consts::LOAD_LIBRARY_SAFE_CURRENT_DIRS; +pub const LOAD_LIBRARY_SAFE_CURRENT_DIRS: LOAD_LIBRARY_FLAGS = 0x00002000; |