From 8dd16259287f58f9273002717ec4d27e97127719 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:43:14 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- third_party/rust/libloading/.cargo-checksum.json | 2 +- third_party/rust/libloading/Cargo.toml | 16 +- third_party/rust/libloading/README.mkd | 7 +- third_party/rust/libloading/src/changelog.rs | 45 ++++++ third_party/rust/libloading/src/error.rs | 8 +- third_party/rust/libloading/src/lib.rs | 8 +- third_party/rust/libloading/src/os/unix/consts.rs | 12 +- third_party/rust/libloading/src/os/unix/mod.rs | 186 +++++++++++++--------- third_party/rust/libloading/src/os/windows/mod.rs | 154 ++++++++---------- third_party/rust/libloading/tests/functions.rs | 20 ++- 10 files changed, 266 insertions(+), 192 deletions(-) (limited to 'third_party/rust/libloading') diff --git a/third_party/rust/libloading/.cargo-checksum.json b/third_party/rust/libloading/.cargo-checksum.json index 04164017dd..4874dbade8 100644 --- a/third_party/rust/libloading/.cargo-checksum.json +++ b/third_party/rust/libloading/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"30f23c025249e7779afd2a52fdaba7145dd75c2a3e5a0a23f3322261d6b94c59","LICENSE":"b29f8b01452350c20dd1af16ef83b598fea3053578ccc1c7a0ef40e57be2620f","README.mkd":"973a5fa332a8f33abc73ffc0ed509f81c66dbd681d8f50969cac32cdfe16ecb2","src/changelog.rs":"a49ff30e81a245fdcef957a3fffed5a5a5b56825bc51f6ed26fe4c12f75d359c","src/error.rs":"156c53e299e8f1cd4694b277a5d92772a8a9e7ec4004bae067367f4c2502de8b","src/lib.rs":"2eebab93230a59cd87c7884e95c91b3a8736c0a76814dd040864b8c25f0d5f9e","src/os/mod.rs":"6c59ef8c1120953ae6b6c32f27766c643ca90d85075c49c3545d2fe1ed82cedd","src/os/unix/consts.rs":"1418cc26329d01ee6bc9c1144881873a66ac2183c1bf1d589904d569656c80f2","src/os/unix/mod.rs":"dff9590acbbb9a02baea9d2eb870515104c9d519b1896887f3bf1434db442929","src/os/windows/mod.rs":"b14478d7f9d4e8edc8afcbc7aa695ede1ccdd739a94242cb8dbb154fe04e4999","src/safe.rs":"b0dc1cb5c8e0216e365063b5e84218b2377bb7a62714fca9a6215a22a7bc58b8","src/test_helpers.rs":"201403e143e5b3204864124cd38067cf8813d5273dc1a9099288a9dc4bdd15b6","src/util.rs":"0b6dcfb9eafff2d87966460ef6b1b99980f888813037e787ed92deee602f8c2b","tests/constants.rs":"4778c062605ed22238c1bed16de4c076d0857282f090f36e6d985dafb7b4544d","tests/functions.rs":"0cbcc193f8aad71df626aefab9881a76f17c5a4b241855b602ce874018392db7","tests/library_filename.rs":"b1481f0bb374687c5f24e25113426d2a95f08a45fb8bc41a41e8702bd5a7b4bf","tests/markers.rs":"0ebc8f807b92e39452d35732988012cdca7ce96231c57eaac9c3f4217225ad39","tests/nagisa32.dll":"5c69b2bd9c8a6ad04165c221075fc9fade1dd66ca697399ace528a5a62328e36","tests/nagisa64.dll":"e20b95e3036f3289421abd100760874d4f455afd33c3b5b64fec56b191f7d477","tests/windows.rs":"35584a46658b305cd539712d3ba3c21fe7a130fd693aa1389e4886a67625d532"},"package":"b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"} \ No newline at end of file +{"files":{"Cargo.toml":"d97d9cdce69985cab52ba027b089854906d4cf653d581e9742530a536b4599fc","LICENSE":"b29f8b01452350c20dd1af16ef83b598fea3053578ccc1c7a0ef40e57be2620f","README.mkd":"707e1cae9fa4b691ce5cb8a3976573158fc60b67cb89948f8f5d51c5908bd0a8","src/changelog.rs":"e7c18ebb9be9624b64bcca3f39d714084cbbaa2ffbc35bfc033967b81be1af66","src/error.rs":"488004e68022adba1002c95d1c8f568fdf1f7bc88aedb6a0f5e31a3dab6f8509","src/lib.rs":"0142a630bb27d1443a8a79d1d5559ea10e859d587442c596d2be7d837507afa0","src/os/mod.rs":"6c59ef8c1120953ae6b6c32f27766c643ca90d85075c49c3545d2fe1ed82cedd","src/os/unix/consts.rs":"a1f98372a58acc6d00a1df19550e0593ede60a53f2aa3ff26cf678cf1015594c","src/os/unix/mod.rs":"15e284c70b61c2728cdf312c2c8740d1671a53dbc7161d8ad5c8a909416644cc","src/os/windows/mod.rs":"8a8792569d43140d982edd6d401abb82023894eb02f2eeba321ee9887568cf3b","src/safe.rs":"b0dc1cb5c8e0216e365063b5e84218b2377bb7a62714fca9a6215a22a7bc58b8","src/test_helpers.rs":"201403e143e5b3204864124cd38067cf8813d5273dc1a9099288a9dc4bdd15b6","src/util.rs":"0b6dcfb9eafff2d87966460ef6b1b99980f888813037e787ed92deee602f8c2b","tests/constants.rs":"4778c062605ed22238c1bed16de4c076d0857282f090f36e6d985dafb7b4544d","tests/functions.rs":"c3d4770704de40e9592cfd109cf506e6b04f0738d8a1ee02ec858b4fa4632a2b","tests/library_filename.rs":"b1481f0bb374687c5f24e25113426d2a95f08a45fb8bc41a41e8702bd5a7b4bf","tests/markers.rs":"0ebc8f807b92e39452d35732988012cdca7ce96231c57eaac9c3f4217225ad39","tests/nagisa32.dll":"5c69b2bd9c8a6ad04165c221075fc9fade1dd66ca697399ace528a5a62328e36","tests/nagisa64.dll":"e20b95e3036f3289421abd100760874d4f455afd33c3b5b64fec56b191f7d477","tests/windows.rs":"35584a46658b305cd539712d3ba3c21fe7a130fd693aa1389e4886a67625d532"},"package":"0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"} \ No newline at end of file diff --git a/third_party/rust/libloading/Cargo.toml b/third_party/rust/libloading/Cargo.toml index 65168d5858..d5e55487b7 100644 --- a/third_party/rust/libloading/Cargo.toml +++ b/third_party/rust/libloading/Cargo.toml @@ -10,9 +10,9 @@ # See Cargo.toml.orig for the original contents. [package] -rust-version = "1.40.0" +rust-version = "1.56.0" name = "libloading" -version = "0.7.4" +version = "0.8.3" authors = ["Simonas Kazlauskas "] description = "Bindings around the platform's dynamic library loading primitives with greatly improved memory safety." documentation = "https://docs.rs/libloading/" @@ -43,9 +43,9 @@ version = "1.1" [target."cfg(unix)".dependencies.cfg-if] version = "1" -[target."cfg(windows)".dependencies.winapi] -version = "0.3" -features = [ - "errhandlingapi", - "libloaderapi", -] +[target."cfg(windows)".dependencies.windows-targets] +version = ">=0.48, <0.53" + +[target."cfg(windows)".dev-dependencies.windows-sys] +version = "0.52" +features = ["Win32_Foundation"] diff --git a/third_party/rust/libloading/README.mkd b/third_party/rust/libloading/README.mkd index 66abb30b6e..7ff55b04b2 100644 --- a/third_party/rust/libloading/README.mkd +++ b/third_party/rust/libloading/README.mkd @@ -1,8 +1,11 @@ # libloading -Bindings around the platform's dynamic library loading primitives with greatly improved memory safety. The most important safety guarantee of this library is the prevention of dangling `Symbol`s that may occur after a `Library` is unloaded. +Bindings around the platform's dynamic library loading primitives with greatly improved memory +safety. The most important safety guarantee of this library is the prevention of dangling `Symbol`s +that may occur after a `Library` is unloaded. -Using this library allows the loading of dynamic libraries, also known as shared libraries, as well as the use of the functions and static variables that these libraries may contain. +Using this library allows the loading of dynamic libraries, also known as shared libraries, as well +as the use of the functions and static variables that these libraries may contain. * [Documentation][docs] * [Changelog][changelog] 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(wrap: fn(crate::error::DlDescription) -> crate::Error, closure: F) --> Result> -where F: FnOnce() -> Option { +/// 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(closure: F, error: fn(&CStr) -> Error) -> Result> +where + F: FnOnce() -> Option, +{ // 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 { // 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 { // 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 { /// 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

(filename: Option

, flags: raw::c_int) -> Result - where P: AsRef { + where + P: AsRef, + { 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(&self, symbol: &[u8], on_null: F) -> Result, crate::Error> - where F: FnOnce() -> Result, crate::Error> + where + F: FnOnce() -> Result, crate::Error>, { ensure_compatible_types::()?; 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(&self, symbol: &[u8]) -> Result, 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 { pointer: *mut raw::c_void, - pd: marker::PhantomData + pd: marker::PhantomData, } impl Symbol { @@ -409,13 +438,18 @@ impl fmt::Debug for Symbol { 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 fmt::Debug for Symbol { } // 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 { 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 = 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>(filename: P, flags: DWORD) -> Result { + pub unsafe fn load_with_flags>(filename: P, flags: LOAD_LIBRARY_FLAGS) -> Result { let wide_filename: Vec = 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::()?; 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(&self, ordinal: WORD) -> Result, crate::Error> { + pub unsafe fn get_ordinal(&self, ordinal: u16) -> Result, crate::Error> { ensure_compatible_types::()?; 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::; 1024]>::uninit().assume_init(); - let len = libloaderapi::GetModuleFileNameW(self.0, + mem::MaybeUninit::<[mem::MaybeUninit; 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 Symbol { impl Symbol> { /// Lift Option out of the symbol. pub fn lift_option(self) -> Option> { - if self.pointer.is_null() { + if self.pointer.is_none() { None } else { Some(Symbol { @@ -363,16 +327,16 @@ impl Clone for Symbol { impl ::std::ops::Deref for Symbol { 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 fmt::Debug for Symbol { 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 { 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(wrap: fn(crate::error::WindowsError) -> crate::Erro -> Result> where F: FnOnce() -> Option { 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 { }) } + +#[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 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; diff --git a/third_party/rust/libloading/tests/functions.rs b/third_party/rust/libloading/tests/functions.rs index c8d1952b3c..a0175a6747 100644 --- a/third_party/rust/libloading/tests/functions.rs +++ b/third_party/rust/libloading/tests/functions.rs @@ -1,5 +1,5 @@ #[cfg(windows)] -extern crate winapi; +extern crate windows_sys; extern crate libloading; use libloading::{Library, Symbol}; @@ -237,14 +237,13 @@ fn library_this() { #[test] fn works_getlasterror() { use libloading::os::windows::{Library, Symbol}; - use winapi::shared::minwindef::DWORD; - use winapi::um::errhandlingapi; + use windows_sys::Win32::Foundation::{GetLastError, SetLastError}; unsafe { let lib = Library::new("kernel32.dll").unwrap(); - let gle: Symbol DWORD> = lib.get(b"GetLastError").unwrap(); - errhandlingapi::SetLastError(42); - assert_eq!(errhandlingapi::GetLastError(), gle()) + let gle: Symbol u32> = lib.get(b"GetLastError").unwrap(); + SetLastError(42); + assert_eq!(GetLastError(), gle()) } } @@ -252,14 +251,13 @@ fn works_getlasterror() { #[test] fn works_getlasterror0() { use libloading::os::windows::{Library, Symbol}; - use winapi::shared::minwindef::DWORD; - use winapi::um::errhandlingapi; + use windows_sys::Win32::Foundation::{GetLastError, SetLastError}; unsafe { let lib = Library::new("kernel32.dll").unwrap(); - let gle: Symbol DWORD> = lib.get(b"GetLastError\0").unwrap(); - errhandlingapi::SetLastError(42); - assert_eq!(errhandlingapi::GetLastError(), gle()) + let gle: Symbol u32> = lib.get(b"GetLastError\0").unwrap(); + SetLastError(42); + assert_eq!(GetLastError(), gle()) } } -- cgit v1.2.3