mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-25 09:57:34 +00:00
fix(core): Mark dlopen/dlsym wrappers unsafe in core::mods
This commit is contained in:
3 files changed
+53
-16
No files matched your search
+25
-7
@@ -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
@@ -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)?;
|
||||
|
||||
+15
-4
@@ -42,7 +42,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
|
||||
let main_lock = server.mods.read().await;
|
||||
let main_mod = (*main_lock).last().expect("main module loaded");
|
||||
if starts {
|
||||
let start = main_mod.get::<StartFuncProto>("start")?;
|
||||
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
|
||||
// "Rust" fn start` with signature matching `StartFuncProto`, compiled
|
||||
// with the same toolchain and flags.
|
||||
let start = unsafe { main_mod.get::<StartFuncProto>("start")? };
|
||||
match start(&server.server).await {
|
||||
| Ok(services) => server.services.lock().await.insert(services),
|
||||
| Err(error) => {
|
||||
@@ -54,7 +57,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
|
||||
|
||||
server.server.stopping.store(false, Ordering::Release);
|
||||
|
||||
let run = main_mod.get::<RunFuncProto>("run")?;
|
||||
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
|
||||
// "Rust" fn run` with signature matching `RunFuncProto`, compiled with the
|
||||
// same toolchain and flags.
|
||||
let run = unsafe { main_mod.get::<RunFuncProto>("run")? };
|
||||
if let Err(error) = run(server
|
||||
.services
|
||||
.lock()
|
||||
@@ -72,7 +78,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
|
||||
let stops = !reloads || stale_from_mods(&main_lock)? <= restart_thresh();
|
||||
let starts = reloads && stops;
|
||||
if stops {
|
||||
let stop = main_mod.get::<StopFuncProto>("stop")?;
|
||||
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
|
||||
// "Rust" fn stop` with signature matching `StopFuncProto`, compiled
|
||||
// with the same toolchain and flags.
|
||||
let stop = unsafe { main_mod.get::<StopFuncProto>("stop")? };
|
||||
if let Err(error) = stop(
|
||||
server
|
||||
.services
|
||||
@@ -102,7 +111,9 @@ pub(crate) async fn open(server: &Arc<Server>) -> Result<usize, Error> {
|
||||
|
||||
for (i, name) in MODULE_NAMES.iter().enumerate() {
|
||||
if mods.get(i).is_none() {
|
||||
mods.push(mods::Module::from_name(name)?);
|
||||
// SAFETY: `MODULE_NAMES` are this workspace's own hot-reloadable
|
||||
// shared libraries built with the same compiler/ABI.
|
||||
mods.push(unsafe { mods::Module::from_name(name)? });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user