fix(core): Mark dlopen/dlsym wrappers unsafe in core::mods

This commit is contained in:
Jade Ellis committed 2026-11-21 18:04:50 +00:00
1 parent 66dd3e8e3e
commit 306be392ba
3 files changed
+53 -16

No files matched your search

+25 -7
View File
@@ -13,11 +13,21 @@ pub struct Module {
}
impl Module {
pub fn from_name(name: &str) -> Result<Self> { Self::from_path(path::from_name(name)?) }
/// # Safety
/// See [`new::from_name`]: loads a shared library via `dlopen(3)` and runs
/// its initialization routines.
pub unsafe fn from_name(name: &str) -> Result<Self> {
// SAFETY: Upheld by caller.
unsafe { Self::from_path(path::from_name(name)?) }
}
pub fn from_path(path: OsString) -> Result<Self> {
/// # Safety
/// See [`new::from_path`]: loads a shared library via `dlopen(3)` and runs
/// its initialization routines.
pub unsafe fn from_path(path: OsString) -> Result<Self> {
Ok(Self {
handle: Some(new::from_path(&path)?),
// SAFETY: Upheld by caller.
handle: Some(unsafe { new::from_path(&path)? }),
loaded: SystemTime::now(),
path,
})
@@ -38,16 +48,24 @@ impl Module {
}
}
pub fn get<Prototype>(&self, name: &str) -> Result<Symbol<Prototype>> {
/// Look up a symbol in this module via `dlsym(3)`.
///
/// # Safety
/// `Prototype` is not checked against the symbol's actual type or ABI in
/// the loaded shared library. The caller must ensure `name` refers to a
/// symbol whose type, calling convention, and ABI exactly match
/// `Prototype`.
pub unsafe fn get<Prototype>(&self, name: &str) -> Result<Symbol<'_, Prototype>> {
let cname = CString::new(name.to_owned()).expect("terminated string from provided name");
let handle = self
.handle
.as_ref()
.expect("backing library loaded by this instance");
// SAFETY: Calls dlsym(3) on unix platforms. This might not have to be
// unsafe if wrapped in libloading with_dlerror().
let sym = unsafe { handle.get::<Prototype>(cname.as_bytes()) };
// SAFETY: Caller guarantees `Prototype` matches the symbol's actual
// type and ABI. Passing `as_bytes_with_nul()` avoids a redundant copy
// inside `libloading`.
let sym = unsafe { handle.get::<Prototype>(cname.as_bytes_with_nul()) };
let sym = sym.expect("symbol found; binding successful");
Ok(sym)
+13 -5
View File
@@ -5,14 +5,22 @@ use crate::{Err, Result};
const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL;
pub fn from_name(name: &str) -> Result<Library> {
/// # Safety
/// Loads a shared library via `dlopen(3)`, which executes the library's
/// initialization routines (`.init_array` / constructors). The caller must
/// ensure the resolved module is trusted and ABI-compatible with this binary.
pub unsafe fn from_name(name: &str) -> Result<Library> {
let path = path::from_name(name)?;
from_path(&path)
// SAFETY: Upheld by caller.
unsafe { from_path(&path) }
}
pub fn from_path(path: &OsStr) -> Result<Library> {
//SAFETY: Calls dlopen(3) on unix platforms. This might not have to be
// unsafe if wrapped in with_dlerror.
/// # Safety
/// Loads a shared library via `dlopen(3)`, which executes the library's
/// initialization routines (`.init_array` / constructors). The caller must
/// ensure `path` points to a trusted, ABI-compatible shared library.
pub unsafe fn from_path(path: &OsStr) -> Result<Library> {
// SAFETY: Caller guarantees `path` is a trusted, ABI-compatible library.
let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) };
if let Err(e) = lib {
let name = path::to_name(path)?;