Files
RedBear-OS/src/ld_so/callbacks.rs
T
Anhad Singh 42d884c6a3 fix(ld.so): errors UB
Currently, how ld.so errors are handled by functions in dlfcn is wrong.
dlopen calls `linker.load_library` which returns a
`Result<ObjectHandler, goblin::Error>`. Now `goblin::Error` may have
variants that are heap allocated. For example:
`Error::Malformed(format!("invalid path: '{}': {}", path, err))`. The
error string would be allocated by ld.so's allocator but will be dropped
inside libc. This is UB.

After this patch, we now return a custom `DlError` instead. To get more
information about the error, `LD_DEBUG=all` can be set.

Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-31 18:35:20 +11:00

39 lines
1.1 KiB
Rust

use super::linker::{Linker, ObjectHandle, ObjectScope, Resolve, Result};
use crate::platform::types::c_void;
use alloc::boxed::Box;
pub struct LinkerCallbacks {
pub unload: Box<dyn Fn(&mut Linker, ObjectHandle)>,
pub load_library:
Box<dyn Fn(&mut Linker, Option<&str>, Resolve, ObjectScope, bool) -> Result<ObjectHandle>>,
pub get_sym: Box<dyn Fn(&Linker, Option<ObjectHandle>, &str) -> Option<*mut c_void>>,
}
impl LinkerCallbacks {
pub fn new() -> LinkerCallbacks {
LinkerCallbacks {
unload: Box::new(unload),
load_library: Box::new(load_library),
get_sym: Box::new(get_sym),
}
}
}
fn unload(linker: &mut Linker, handle: ObjectHandle) {
linker.unload(handle)
}
fn load_library(
linker: &mut Linker,
name: Option<&str>,
resolve: Resolve,
scope: ObjectScope,
noload: bool,
) -> Result<ObjectHandle> {
linker.load_library(name, resolve, scope, noload)
}
fn get_sym(linker: &Linker, handle: Option<ObjectHandle>, name: &str) -> Option<*mut c_void> {
linker.get_sym(handle, name)
}