diff --git a/crates/xcl/src/loader.rs b/crates/xcl/src/loader.rs index 016b5e3..9561c8d 100644 --- a/crates/xcl/src/loader.rs +++ b/crates/xcl/src/loader.rs @@ -182,7 +182,7 @@ fn map_image_ex(path: &Path, is_main: bool) -> Result, String> let adj = |va: u64| (va as i64 + delta) as usize; let template = adj(t.start_raw); let template_len = t.template_len() as usize; - let index_ptr = adj(t.address_of_index) as *mut u32; + let index_ptr = if t.address_of_index == 0 { std::ptr::null_mut() } else { adj(t.address_of_index) as *mut u32 }; unsafe { tls_install(&module, template, template_len, t.total_len(), index_ptr)?; if t.address_of_callbacks != 0 { diff --git a/crates/xcl/src/tls.rs b/crates/xcl/src/tls.rs index 3be4e39..c09e1e2 100644 --- a/crates/xcl/src/tls.rs +++ b/crates/xcl/src/tls.rs @@ -335,7 +335,9 @@ pub unsafe fn take_slot_zero(img_name: &str, title_index: *mut u32, k: u32) -> R for (arr, block) in carriers { *arr = block; } - *title_index = 0; + if !title_index.is_null() { + *title_index = 0; + } if let Ok(images) = IMAGES.read() { for img in images.iter().filter(|i| i.name == img_name) { img.index.store(0, Ordering::Release); @@ -488,7 +490,9 @@ pub unsafe fn install( if index == NO_INDEX { return Err("Windows did not allocate a TLS slot for the carrier (TLS directory ignored)".into()); } - *index_ptr = index; + if !index_ptr.is_null() { + *index_ptr = index; + } img.index.store(index, Ordering::Release); let n = fill_existing_threads(&img, index); crate::xlog_info!( @@ -592,4 +596,19 @@ mod tests { .unwrap(); assert!(ok); } + + #[test] + fn install_without_an_index_variable() { + let dir = std::env::temp_dir().join(format!("xcl_tls_test_noidx_{}", std::process::id())); + let template: &'static [u8; 16] = Box::leak(Box::new([0xA5u8; 16])); + let idx = unsafe { + install("unit-noidx", template.as_ptr() as usize, 16, 0x1000, std::ptr::null_mut(), &dir).unwrap() + }; + assert_ne!(idx, NO_INDEX); + unsafe { + let b = tls_block_of(current_teb(), idx); + assert!(!b.is_null()); + assert_eq!(std::slice::from_raw_parts(b, 16), &template[..]); + } + } }