Skip to content
Draft

Ci venv #6474

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
apply ctypes review
  • Loading branch information
youknowone committed Dec 23, 2025
commit 212f839f388be71b389de3a040db80e96450f96a
11 changes: 10 additions & 1 deletion crates/vm/src/stdlib/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ pub(crate) mod _ctypes {
vm: &VirtualMachine,
) -> PyResult<usize> {
// Default mode: RTLD_NOW | RTLD_LOCAL, always force RTLD_NOW
let mode = load_flags.unwrap_or(libc::RTLD_NOW | libc::RTLD_LOCAL) | libc::RTLD_NOW;
let mode = load_flags
.map(|m| m | libc::RTLD_NOW)
.unwrap_or(libc::RTLD_NOW | libc::RTLD_LOCAL);

match name {
Some(name) => {
Expand Down Expand Up @@ -641,6 +643,13 @@ pub(crate) mod _ctypes {
name: crate::builtins::PyStrRef,
vm: &VirtualMachine,
) -> PyResult<usize> {
// Validate the handle exists in the library cache before calling dlsym
let cache = library::libcache().read();
if cache.get_lib(handle).is_none() {
return Err(vm.new_os_error("invalid library handle"));
}
drop(cache);

let symbol_name = std::ffi::CString::new(name.as_str())
.map_err(|_| vm.new_value_error("symbol name contains null byte"))?;

Expand Down
42 changes: 32 additions & 10 deletions crates/vm/src/stdlib/ctypes/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::ffi::OsStr;
use std::fmt;

#[cfg(unix)]
use libloading::os::unix::Library as UnixLibrary;
use libloading::os::unix::Library as OsLibrary;
#[cfg(windows)]
use libloading::os::windows::Library as OsLibrary;

pub struct SharedLibrary {
pub(crate) lib: PyMutex<Option<Library>>,
Expand All @@ -33,7 +35,7 @@ impl SharedLibrary {
) -> Result<SharedLibrary, libloading::Error> {
Ok(SharedLibrary {
lib: PyMutex::new(Some(unsafe {
UnixLibrary::open(Some(name.as_ref()), mode)?.into()
OsLibrary::open(Some(name.as_ref()), mode)?.into()
})),
})
}
Expand All @@ -42,19 +44,39 @@ impl SharedLibrary {
#[cfg(unix)]
pub fn from_raw_handle(handle: *mut libc::c_void) -> SharedLibrary {
SharedLibrary {
lib: PyMutex::new(Some(unsafe { UnixLibrary::from_raw(handle).into() })),
lib: PyMutex::new(Some(unsafe { OsLibrary::from_raw(handle).into() })),
}
}

/// Get the underlying OS handle (HMODULE on Windows, dlopen handle on Unix)
#[cfg(unix)]
pub fn get_pointer(&self) -> usize {
let lib_lock = self.lib.lock();
if let Some(l) = &*lib_lock {
// libloading::Library internally stores the OS handle directly
// On Windows: HMODULE (*mut c_void)
// On Unix: *mut c_void from dlopen
// We use transmute_copy to read the handle without consuming the Library
unsafe { std::mem::transmute_copy::<Library, usize>(l) }
let mut lib_lock = self.lib.lock();
if let Some(lib) = lib_lock.take() {
// Use official libloading API: convert to platform-specific type,
// extract raw handle, then reconstruct
let unix_lib: OsLibrary = lib.into();
let handle = unix_lib.into_raw();
// Reconstruct the library from the raw handle and put it back
*lib_lock = Some(unsafe { OsLibrary::from_raw(handle) }.into());
handle as usize
} else {
0
}
}

/// Get the underlying OS handle (HMODULE on Windows, dlopen handle on Unix)
#[cfg(windows)]
pub fn get_pointer(&self) -> usize {
let mut lib_lock = self.lib.lock();
if let Some(lib) = lib_lock.take() {
// Use official libloading API: convert to platform-specific type,
// extract raw handle, then reconstruct
let win_lib: OsLibrary = lib.into();
let handle = win_lib.into_raw();
// Reconstruct the library from the raw handle and put it back
*lib_lock = Some(unsafe { OsLibrary::from_raw(handle) }.into());
handle as usize
} else {
0
}
Expand Down