Skip to content

Commit

Permalink
Fix macos tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhao committed Jul 1, 2024
1 parent b36208b commit 88072f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions mallockit/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn plan(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[cfg(any(feature = "malloc", feature = "mallockit/malloc"))]
#[::mallockit::ctor]
unsafe fn ctor() {
<<Plan as ::mallockit::Plan>::Mutator as ::mallockit::mutator::TLS>::current();
::mallockit::util::sys::hooks::process_start(&*PLAN);
}

Expand Down Expand Up @@ -92,20 +93,30 @@ pub fn mutator(_attr: TokenStream, item: TokenStream) -> TokenStream {


mod __mallockit_mutator {
#[cfg(not(target_os = "macos"))]
fn init() -> super::#name {
::mallockit::mutator::init_pthread_specific();
<super::#name as ::mallockit::Mutator>::new()
}

#[cfg(not(target_os = "macos"))]
#[thread_local]
pub(super) static mut MUTATOR: ::mallockit::util::Lazy<super::#name, ::mallockit::util::Local> = ::mallockit::util::Lazy::new(init);

#[no_mangle]
#[cfg(not(target_os = "macos"))]
extern "C" fn mallockit_pthread_destructor() {
unsafe {
MUTATOR.reset(init);
}
}

#[no_mangle]
#[cfg(target_os = "macos")]
extern "C" fn mallockit_pthread_destructor() {
use crate::mallockit::mutator::TLS;
<super::#name as ::mallockit::mutator::TLS>::current().reset();
}
}

impl ::mallockit::mutator::TLS for #name {
Expand Down
14 changes: 10 additions & 4 deletions mallockit/src/mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub trait TLS: Sized {
fn current() -> &'static mut Self {
unsafe { &mut *macos_tls::get_tls::<Self>() }
}

fn reset(&mut self) {
*self = Self::new();
}
}

impl TLS for u8 {
Expand Down Expand Up @@ -211,8 +215,7 @@ mod macos_tls {
fn init_tls<T: TLS>() -> *mut (InternalTLS, T) {
let ptr = alloc_tls::<(InternalTLS, T)>();
unsafe {
(*ptr).0 = InternalTLS::new();
(*ptr).1 = T::new();
std::ptr::write(&mut (*ptr).0, InternalTLS::new());
unsafe {
let mut tcb: *mut *mut T;
asm! {
Expand All @@ -224,6 +227,8 @@ mod macos_tls {
}
tcb.add(SLOT).write(ptr as *mut T)
}
std::ptr::write(&mut (*ptr).1, T::new());
crate::mutator::init_pthread_specific();
}
ptr
}
Expand All @@ -234,9 +239,10 @@ mod macos_tls {
fn init_tls<T: TLS>() -> *mut (InternalTLS, T) {
let ptr = alloc_tls::<(InternalTLS, T)>();
unsafe {
(*ptr).0 = InternalTLS::new();
(*ptr).1 = T::new();
std::ptr::write(&mut (*ptr).0, InternalTLS::new());
asm!("mov gs:{offset}, {0}", in(reg) ptr, offset = const OFFSET);
std::ptr::write(&mut (*ptr).1, T::new());
crate::mutator::init_pthread_specific();
}
ptr
}
Expand Down

0 comments on commit 88072f2

Please sign in to comment.