Skip to content

Commit 7a7d88e

Browse files
committed
support raw_attribute with raw pointer
1 parent 648d024 commit 7a7d88e

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

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

+20
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,16 @@ 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+
self.as_inner_mut().raw_attribute_ptr(attribute, value_ptr, value_size);
430+
self
431+
}
412432
}
413433

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

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

+64-17
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,27 @@ 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+
}),
260277
}
261278

262279
pub fn spawn(
@@ -907,11 +924,21 @@ impl Drop for ProcThreadAttributeList {
907924
}
908925

909926
/// Wrapper around the value data to be used as a Process Thread Attribute.
910-
struct ProcThreadAttributeValue {
927+
struct ProcThreadAttributeValueData {
911928
data: Box<dyn Send + Sync>,
912929
size: usize,
913930
}
914931

932+
struct ProcThreadAttributeValuePointer {
933+
pointer: isize, // using isize instead of *const c_void to have it sendable
934+
size: usize,
935+
}
936+
937+
enum ProcThreadAttributeValue {
938+
Data(ProcThreadAttributeValueData),
939+
Pointer(ProcThreadAttributeValuePointer),
940+
}
941+
915942
fn make_proc_thread_attribute_list(
916943
attributes: &BTreeMap<usize, ProcThreadAttributeValue>,
917944
) -> io::Result<ProcThreadAttributeList> {
@@ -953,18 +980,38 @@ fn make_proc_thread_attribute_list(
953980
// It's theoretically possible for the attribute count to exceed a u32 value.
954981
// Therefore, we ensure that we don't add more attributes than the buffer was initialized for.
955982
for (&attribute, value) in attributes.iter().take(attribute_count as usize) {
956-
let value_ptr = core::ptr::addr_of!(*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-
})?;
983+
match value {
984+
ProcThreadAttributeValue::Data(value) => {
985+
let value_ptr = core::ptr::addr_of!(*value.data) as _;
986+
cvt(unsafe {
987+
c::UpdateProcThreadAttribute(
988+
proc_thread_attribute_list.0.as_mut_ptr() as _,
989+
0,
990+
attribute,
991+
value_ptr,
992+
value.size,
993+
ptr::null_mut(),
994+
ptr::null_mut(),
995+
)
996+
})?;
997+
}
998+
ProcThreadAttributeValue::Pointer(value) => {
999+
cvt(
1000+
unsafe {
1001+
#![allow(fuzzy_provenance_casts)]
1002+
c::UpdateProcThreadAttribute(
1003+
proc_thread_attribute_list.0.as_mut_ptr() as _,
1004+
0,
1005+
attribute,
1006+
value.pointer as *const c_void,
1007+
value.size,
1008+
ptr::null_mut(),
1009+
ptr::null_mut(),
1010+
)
1011+
},
1012+
)?;
1013+
}
1014+
}
9681015
}
9691016

9701017
Ok(proc_thread_attribute_list)

0 commit comments

Comments
 (0)