Merge branch 'unsafe_blocks-dlfcn-dl_tls' into 'master'

Unsafe blocks: `dl-tls`, `dlfcn`

See merge request redox-os/relibc!575
This commit is contained in:
Jeremy Soller
2024-12-10 14:49:31 +00:00
2 changed files with 36 additions and 27 deletions
+16 -13
View File
@@ -1,5 +1,7 @@
//! dl-tls implementation for Redox
#![deny(unsafe_op_in_unsafe_fn)]
use core::alloc::Layout;
use alloc::alloc::alloc_zeroed;
@@ -14,37 +16,38 @@ pub struct dl_tls_index {
#[no_mangle]
pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
let tcb = Tcb::current().unwrap();
let tcb = unsafe { Tcb::current().unwrap() };
let ti = unsafe { &*ti };
let masters = unsafe { tcb.masters().unwrap() };
trace!(
"__tls_get_addr({:p}: {:#x}, {:#x}, masters_len={}, dtv_len={})",
ti,
(*ti).ti_module,
(*ti).ti_offset,
tcb.masters().unwrap().len(),
ti.ti_module,
ti.ti_offset,
masters.len(),
tcb.dtv_mut().unwrap().len()
);
if tcb.dtv_mut().unwrap_or_default().len() < tcb.masters().unwrap().len() {
if tcb.dtv_mut().unwrap_or_default().len() < masters.len() {
// Reallocate DTV.
tcb.setup_dtv(tcb.masters().unwrap().len());
tcb.setup_dtv(masters.len());
}
let ti = &*ti;
let dtv_index = ti.ti_module as usize - 1;
if tcb.dtv_mut().unwrap()[dtv_index].is_null() {
// Allocate TLS for module.
let master = &tcb.masters().unwrap()[dtv_index];
let master = &masters[dtv_index];
// FIXME(andypython): master.align
let layout = unsafe {
Layout::from_size_align_unchecked(master.offset /* aligned ph.p_memsz */, 16)
};
let module_tls = alloc_zeroed(layout);
let module_tls = unsafe { alloc_zeroed(layout) };
core::ptr::copy_nonoverlapping(master.ptr, module_tls, master.len);
unsafe { core::ptr::copy_nonoverlapping(master.ptr, module_tls, master.len) };
// Set the DTV entry.
tcb.dtv_mut().unwrap()[dtv_index] = module_tls;
@@ -60,9 +63,9 @@ pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
}
if cfg!(target_arch = "riscv64") {
ptr = ptr.add(0x800 + ti.ti_offset as usize); // dynamic offsets are 0x800-based on risc-v
ptr = unsafe { ptr.add(0x800 + ti.ti_offset as usize) }; // dynamic offsets are 0x800-based on risc-v
} else {
ptr = ptr.add(ti.ti_offset as usize);
ptr = unsafe { ptr.add(ti.ti_offset as usize) };
}
ptr.cast::<c_void>()
@@ -72,5 +75,5 @@ pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
#[cfg(target_arch = "x86")]
#[no_mangle]
pub unsafe extern "C" fn ___tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
__tls_get_addr(ti)
unsafe { __tls_get_addr(ti) }
}
+20 -14
View File
@@ -1,5 +1,7 @@
//! dlfcn implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/dlfcn.h.html
#![deny(unsafe_op_in_unsafe_fn)]
use core::{
ptr, str,
sync::atomic::{AtomicUsize, Ordering},
@@ -28,10 +30,12 @@ pub struct Dl_info {
#[no_mangle]
pub unsafe extern "C" fn dladdr(addr: *mut c_void, info: *mut Dl_info) -> c_int {
//TODO
(*info).dli_fname = ptr::null();
(*info).dli_fbase = ptr::null_mut();
(*info).dli_sname = ptr::null();
(*info).dli_saddr = ptr::null_mut();
unsafe {
(*info).dli_fname = ptr::null();
(*info).dli_fbase = ptr::null_mut();
(*info).dli_sname = ptr::null();
(*info).dli_saddr = ptr::null_mut();
}
0
}
@@ -42,12 +46,14 @@ pub unsafe extern "C" fn dlopen(cfilename: *const c_char, flags: c_int) -> *mut
let filename = if cfilename.is_null() {
None
} else {
Some(str::from_utf8_unchecked(
CStr::from_ptr(cfilename).to_bytes(),
))
unsafe {
Some(str::from_utf8_unchecked(
CStr::from_ptr(cfilename).to_bytes(),
))
}
};
let tcb = match Tcb::current() {
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
@@ -58,7 +64,7 @@ pub unsafe extern "C" fn dlopen(cfilename: *const c_char, flags: c_int) -> *mut
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return ptr::null_mut();
}
let mut linker = (&*tcb.linker_ptr).lock();
let mut linker = unsafe { (&*tcb.linker_ptr).lock() };
let cbs_c = linker.cbs.clone();
let cbs = cbs_c.borrow();
@@ -81,9 +87,9 @@ pub unsafe extern "C" fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *m
return ptr::null_mut();
}
let symbol_str = str::from_utf8_unchecked(CStr::from_ptr(symbol).to_bytes());
let symbol_str = unsafe { str::from_utf8_unchecked(CStr::from_ptr(symbol).to_bytes()) };
let tcb = match Tcb::current() {
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
@@ -96,7 +102,7 @@ pub unsafe extern "C" fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *m
return ptr::null_mut();
}
let linker = (&*tcb.linker_ptr).lock();
let linker = unsafe { (&*tcb.linker_ptr).lock() };
let cbs_c = linker.cbs.clone();
let cbs = cbs_c.borrow();
match (cbs.get_sym)(&linker, handle as usize, symbol_str) {
@@ -110,7 +116,7 @@ pub unsafe extern "C" fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *m
#[no_mangle]
pub unsafe extern "C" fn dlclose(handle: *mut c_void) -> c_int {
let tcb = match Tcb::current() {
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
@@ -122,7 +128,7 @@ pub unsafe extern "C" fn dlclose(handle: *mut c_void) -> c_int {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return -1;
};
let mut linker = (&*tcb.linker_ptr).lock();
let mut linker = unsafe { (&*tcb.linker_ptr).lock() };
let cbs_c = linker.cbs.clone();
let cbs = cbs_c.borrow();
(cbs.unload)(&mut linker, handle as usize);