Skip to content

Commit 08c2a0c

Browse files
committed
support raw_attribute with raw pointer
1 parent 9f57edf commit 08c2a0c

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed

library/std/src/os/windows/process.rs

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
#![stable(feature = "process_extensions", since = "1.2.0")]
66

7+
use core::ffi::c_void;
8+
79
use crate::ffi::OsStr;
810
use crate::os::windows::io::{
911
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
@@ -368,6 +370,14 @@ pub trait CommandExt: Sealed {
368370
attribute: usize,
369371
value: T,
370372
) -> &mut process::Command;
373+
374+
#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
375+
unsafe fn raw_attribute_ptr(
376+
&mut self,
377+
attribute: usize,
378+
value_ptr: *const c_void,
379+
value_size: usize,
380+
) -> &mut process::Command;
371381
}
372382

373383
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -409,6 +419,18 @@ impl CommandExt for process::Command {
409419
unsafe { self.as_inner_mut().raw_attribute(attribute, value) };
410420
self
411421
}
422+
423+
unsafe fn raw_attribute_ptr(
424+
&mut self,
425+
attribute: usize,
426+
value_ptr: *const c_void,
427+
value_size: usize,
428+
) -> &mut process::Command {
429+
unsafe {
430+
self.as_inner_mut().raw_attribute_ptr(attribute, value_ptr, value_size);
431+
}
432+
self
433+
}
412434
}
413435

414436
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]

library/std/src/sys/pal/windows/process.rs

+65-17
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,28 @@ impl Command {
253253
attribute: usize,
254254
value: T,
255255
) {
256-
self.proc_thread_attributes.insert(attribute, ProcThreadAttributeValue {
257-
size: mem::size_of::<T>(),
258-
data: Box::new(value),
259-
});
256+
self.proc_thread_attributes.insert(
257+
attribute,
258+
ProcThreadAttributeValue::Data(ProcThreadAttributeValueData {
259+
size: mem::size_of::<T>(),
260+
data: Box::new(value),
261+
}),
262+
);
263+
}
264+
265+
pub unsafe fn raw_attribute_ptr(
266+
&mut self,
267+
attribute: usize,
268+
value_ptr: *const c_void,
269+
value_size: usize,
270+
) {
271+
self.proc_thread_attributes.insert(
272+
attribute,
273+
ProcThreadAttributeValue::Pointer(ProcThreadAttributeValuePointer {
274+
size: value_size,
275+
pointer: value_ptr as isize,
276+
}),
277+
);
260278
}
261279

262280
pub fn spawn(
@@ -907,11 +925,21 @@ impl Drop for ProcThreadAttributeList {
907925
}
908926

909927
/// Wrapper around the value data to be used as a Process Thread Attribute.
910-
struct ProcThreadAttributeValue {
928+
struct ProcThreadAttributeValueData {
911929
data: Box<dyn Send + Sync>,
912930
size: usize,
913931
}
914932

933+
struct ProcThreadAttributeValuePointer {
934+
pointer: isize, // using isize instead of *const c_void to have it sendable
935+
size: usize,
936+
}
937+
938+
enum ProcThreadAttributeValue {
939+
Data(ProcThreadAttributeValueData),
940+
Pointer(ProcThreadAttributeValuePointer),
941+
}
942+
915943
fn make_proc_thread_attribute_list(
916944
attributes: &BTreeMap<usize, ProcThreadAttributeValue>,
917945
) -> io::Result<ProcThreadAttributeList> {
@@ -953,18 +981,38 @@ fn make_proc_thread_attribute_list(
953981
// It's theoretically possible for the attribute count to exceed a u32 value.
954982
// Therefore, we ensure that we don't add more attributes than the buffer was initialized for.
955983
for (&attribute, value) in attributes.iter().take(attribute_count as usize) {
956-
let value_ptr = (&raw const *value.data) as _;
957-
cvt(unsafe {
958-
c::UpdateProcThreadAttribute(
959-
proc_thread_attribute_list.0.as_mut_ptr() as _,
960-
0,
961-
attribute,
962-
value_ptr,
963-
value.size,
964-
ptr::null_mut(),
965-
ptr::null_mut(),
966-
)
967-
})?;
984+
match value {
985+
ProcThreadAttributeValue::Data(value) => {
986+
let value_ptr = (&raw const *value.data) as _;
987+
cvt(unsafe {
988+
c::UpdateProcThreadAttribute(
989+
proc_thread_attribute_list.0.as_mut_ptr() as _,
990+
0,
991+
attribute,
992+
value_ptr,
993+
value.size,
994+
ptr::null_mut(),
995+
ptr::null_mut(),
996+
)
997+
})?;
998+
}
999+
ProcThreadAttributeValue::Pointer(value) => {
1000+
cvt(
1001+
unsafe {
1002+
#![allow(fuzzy_provenance_casts)]
1003+
c::UpdateProcThreadAttribute(
1004+
proc_thread_attribute_list.0.as_mut_ptr() as _,
1005+
0,
1006+
attribute,
1007+
value.pointer as *const c_void,
1008+
value.size,
1009+
ptr::null_mut(),
1010+
ptr::null_mut(),
1011+
)
1012+
},
1013+
)?;
1014+
}
1015+
}
9681016
}
9691017

9701018
Ok(proc_thread_attribute_list)

0 commit comments

Comments
 (0)