Skip to content

Commit 3d88f2b

Browse files
committed
support raw_attribute with raw pointer
1 parent f9b1614 commit 3d88f2b

File tree

2 files changed

+79
-14
lines changed

2 files changed

+79
-14
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,
@@ -346,6 +348,14 @@ pub trait CommandExt: Sealed {
346348
attribute: usize,
347349
value: T,
348350
) -> &mut process::Command;
351+
352+
#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
353+
unsafe fn raw_attribute_ptr(
354+
&mut self,
355+
attribute: usize,
356+
value_ptr: *const c_void,
357+
value_size: usize,
358+
) -> &mut process::Command;
349359
}
350360

351361
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -382,6 +392,16 @@ impl CommandExt for process::Command {
382392
self.as_inner_mut().raw_attribute(attribute, value);
383393
self
384394
}
395+
396+
unsafe fn raw_attribute_ptr(
397+
&mut self,
398+
attribute: usize,
399+
value_ptr: *const c_void,
400+
value_size: usize,
401+
) -> &mut process::Command {
402+
self.as_inner_mut().raw_attribute_ptr(attribute, value_ptr, value_size);
403+
self
404+
}
385405
}
386406

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

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

+59-14
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,25 @@ impl Command {
255255
) {
256256
self.proc_thread_attributes.insert(
257257
attribute,
258-
ProcThreadAttributeValue { size: mem::size_of::<T>(), data: Box::new(value) },
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,
276+
}),
259277
);
260278
}
261279

@@ -889,11 +907,21 @@ impl Drop for ProcThreadAttributeList {
889907
}
890908

891909
/// Wrapper around the value data to be used as a Process Thread Attribute.
892-
struct ProcThreadAttributeValue {
910+
struct ProcThreadAttributeValueData {
893911
data: Box<dyn Send + Sync>,
894912
size: usize,
895913
}
896914

915+
struct ProcThreadAttributeValuePointer {
916+
pointer: *const c_void,
917+
size: usize,
918+
}
919+
920+
enum ProcThreadAttributeValue {
921+
Data(ProcThreadAttributeValueData),
922+
Pointer(ProcThreadAttributeValuePointer),
923+
}
924+
897925
fn make_proc_thread_attribute_list(
898926
attributes: &BTreeMap<usize, ProcThreadAttributeValue>,
899927
) -> io::Result<ProcThreadAttributeList> {
@@ -935,18 +963,35 @@ fn make_proc_thread_attribute_list(
935963
// It's theoretically possible for the attribute count to exceed a u32 value.
936964
// Therefore, we ensure that we don't add more attributes than the buffer was initialized for.
937965
for (&attribute, value) in attributes.iter().take(attribute_count as usize) {
938-
let value_ptr = core::ptr::addr_of!(*value.data) as _;
939-
cvt(unsafe {
940-
c::UpdateProcThreadAttribute(
941-
proc_thread_attribute_list.0.as_mut_ptr() as _,
942-
0,
943-
attribute,
944-
value_ptr,
945-
value.size,
946-
ptr::null_mut(),
947-
ptr::null_mut(),
948-
)
949-
})?;
966+
match value {
967+
ProcThreadAttributeValue::Data(value) => {
968+
let value_ptr = core::ptr::addr_of!(*value.data) as _;
969+
cvt(unsafe {
970+
c::UpdateProcThreadAttribute(
971+
proc_thread_attribute_list.0.as_mut_ptr() as _,
972+
0,
973+
attribute,
974+
value_ptr,
975+
value.size,
976+
ptr::null_mut(),
977+
ptr::null_mut(),
978+
)
979+
})?;
980+
}
981+
ProcThreadAttributeValue::Pointer(value) => {
982+
cvt(unsafe {
983+
c::UpdateProcThreadAttribute(
984+
proc_thread_attribute_list.0.as_mut_ptr() as _,
985+
0,
986+
attribute,
987+
value.pointer,
988+
value.size,
989+
ptr::null_mut(),
990+
ptr::null_mut(),
991+
)
992+
})?;
993+
}
994+
}
950995
}
951996

952997
Ok(proc_thread_attribute_list)

0 commit comments

Comments
 (0)