fix(dl-tls): TLS overallocation
* `offset` is the offset in TLS to copy initial data to NOT the segment size * Only zero the region which is required to be zeroed Signed-off-by: Anhad Singh <andypython@protonmail.com>
This commit is contained in:
+20
-11
@@ -1,14 +1,17 @@
|
||||
//! dl-tls implementation for Redox
|
||||
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
// FIXME(andypython): remove this when #![allow(warnings, unused_variables)] is
|
||||
// dropped from src/lib.rs.
|
||||
#![warn(warnings, unused_variables)]
|
||||
|
||||
use core::{alloc::Layout, arch::global_asm};
|
||||
|
||||
use alloc::alloc::alloc_zeroed;
|
||||
use alloc::alloc::alloc;
|
||||
use core::{alloc::Layout, ptr};
|
||||
|
||||
use crate::{ld_so::tcb::Tcb, platform::types::*};
|
||||
|
||||
#[repr(C)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct dl_tls_index {
|
||||
pub ti_module: usize,
|
||||
pub ti_offset: usize,
|
||||
@@ -18,7 +21,7 @@ pub struct dl_tls_index {
|
||||
pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
|
||||
let tcb = unsafe { Tcb::current().unwrap() };
|
||||
let ti = unsafe { &*ti };
|
||||
let masters = unsafe { tcb.masters().unwrap() };
|
||||
let masters = tcb.masters().unwrap();
|
||||
|
||||
trace!(
|
||||
"__tls_get_addr({:p}: {:#x}, {:#x}, masters_len={}, dtv_len={})",
|
||||
@@ -40,15 +43,21 @@ pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void {
|
||||
// Allocate TLS for module.
|
||||
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 = unsafe {
|
||||
// FIXME(andypython): master.align
|
||||
let layout = Layout::from_size_align_unchecked(master.segment_size, 16);
|
||||
let ptr = alloc(layout);
|
||||
|
||||
ptr::copy_nonoverlapping(master.ptr, ptr, master.image_size);
|
||||
ptr::write_bytes(
|
||||
ptr.add(master.image_size),
|
||||
0,
|
||||
master.segment_size - master.image_size,
|
||||
);
|
||||
|
||||
ptr
|
||||
};
|
||||
|
||||
let module_tls = unsafe { alloc_zeroed(layout) };
|
||||
|
||||
unsafe { core::ptr::copy_nonoverlapping(master.ptr, module_tls, master.len) };
|
||||
|
||||
// Set the DTV entry.
|
||||
tcb.dtv_mut()[dtv_index] = module_tls;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user