diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:43:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:43:14 +0000 |
commit | 8dd16259287f58f9273002717ec4d27e97127719 (patch) | |
tree | 3863e62a53829a84037444beab3abd4ed9dfc7d0 /third_party/rust/libloading/src/os/windows | |
parent | Releasing progress-linux version 126.0.1-1~progress7.99u1. (diff) | |
download | firefox-8dd16259287f58f9273002717ec4d27e97127719.tar.xz firefox-8dd16259287f58f9273002717ec4d27e97127719.zip |
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/libloading/src/os/windows')
-rw-r--r-- | third_party/rust/libloading/src/os/windows/mod.rs | 154 |
1 files changed, 67 insertions, 87 deletions
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; |