From ce4b4bccf2a0330d92fcada624c56a9981c018de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Fri, 28 Mar 2025 13:36:28 +0800 Subject: [PATCH 1/2] Add cygwin support --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/symbolize/gimli.rs | 4 ++-- src/symbolize/gimli/libs_windows.rs | 21 +++++++++++++++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2d4f15b..b363e458 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ cpp_demangle = { default-features = false, version = "0.4.0", optional = true, f "alloc", ] } -[target.'cfg(windows)'.dependencies] +[target.'cfg(any(windows, target_os = "cygwin"))'.dependencies] windows-targets = "0.52.6" [target.'cfg(not(all(windows, target_env = "msvc", not(target_vendor = "uwp"))))'.dependencies] diff --git a/src/lib.rs b/src/lib.rs index ab5e64c9..37013bc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -247,5 +247,5 @@ mod lock { ))] mod dbghelp; // Auto-generated by windows-bindgen/riddle -#[cfg(windows)] +#[cfg(any(windows, target_os = "cygwin"))] mod windows_sys; diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index e92f98b5..b1527949 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -193,7 +193,7 @@ fn mmap(path: &Path) -> Option { } cfg_if::cfg_if! { - if #[cfg(windows)] { + if #[cfg(any(windows, target_os = "cygwin"))] { mod coff; use self::coff::{handle_split_dwarf, Object}; } else if #[cfg(any(target_vendor = "apple"))] { @@ -209,7 +209,7 @@ cfg_if::cfg_if! { } cfg_if::cfg_if! { - if #[cfg(windows)] { + if #[cfg(any(windows, target_os = "cygwin"))] { mod libs_windows; use libs_windows::native_libraries; } else if #[cfg(target_vendor = "apple")] { diff --git a/src/symbolize/gimli/libs_windows.rs b/src/symbolize/gimli/libs_windows.rs index 355dc068..25aa1ea6 100644 --- a/src/symbolize/gimli/libs_windows.rs +++ b/src/symbolize/gimli/libs_windows.rs @@ -1,6 +1,5 @@ use super::super::super::windows_sys::*; use super::mystd::ffi::OsString; -use super::mystd::os::windows::prelude::*; use super::{coff, mmap, Library, LibrarySegment}; use alloc::vec; use alloc::vec::Vec; @@ -49,7 +48,25 @@ unsafe fn load_library(me: &MODULEENTRY32W) -> Option { .iter() .position(|i| *i == 0) .unwrap_or(me.szExePath.len()); - let name = OsString::from_wide(&me.szExePath[..pos]); + let mut name_buffer = vec![0_u8; pos * 4]; + let name_len = unsafe { + WideCharToMultiByte( + CP_UTF8, + 0, + me.szExePath.as_ptr(), + pos as i32, + name_buffer.as_mut_ptr(), + name_buffer.len() as i32, + core::ptr::null_mut(), + core::ptr::null_mut(), + ) as usize + }; + if name_len == 0 || name_len > name_buffer.len() { + // This can't happen. + return None; + } + unsafe { name_buffer.set_len(name_len) }; + let name = unsafe { OsString::from_encoded_bytes_unchecked(name_buffer) }; // MinGW libraries currently don't support ASLR // (rust-lang/rust#16514), but DLLs can still be relocated around in From 24a9104420c84ea990c6f3503e11ba353adca65b Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Tue, 1 Apr 2025 12:07:22 +0800 Subject: [PATCH 2/2] Use correct name buffer size --- src/symbolize/gimli/libs_windows.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/symbolize/gimli/libs_windows.rs b/src/symbolize/gimli/libs_windows.rs index 25aa1ea6..b321df71 100644 --- a/src/symbolize/gimli/libs_windows.rs +++ b/src/symbolize/gimli/libs_windows.rs @@ -48,7 +48,19 @@ unsafe fn load_library(me: &MODULEENTRY32W) -> Option { .iter() .position(|i| *i == 0) .unwrap_or(me.szExePath.len()); - let mut name_buffer = vec![0_u8; pos * 4]; + let name_len = unsafe { + WideCharToMultiByte( + CP_UTF8, + 0, + me.szExePath.as_ptr(), + pos as i32, + core::ptr::null_mut(), + 0, + core::ptr::null_mut(), + core::ptr::null_mut(), + ) as usize + }; + let mut name_buffer = vec![0_u8; name_len]; let name_len = unsafe { WideCharToMultiByte( CP_UTF8,