Skip to content

Commit 7b4d550

Browse files
committed
support raw_attribute with raw pointer
1 parent f9b1614 commit 7b4d550

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-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

+54-14
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,19 @@ 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{ size: mem::size_of::<T>(), data: Box::new(value) } ),
259+
);
260+
}
261+
262+
pub unsafe fn raw_attribute_ptr(
263+
&mut self,
264+
attribute: usize,
265+
value_ptr: *const c_void,
266+
value_size: usize,
267+
) {
268+
self.proc_thread_attributes.insert(
269+
attribute,
270+
ProcThreadAttributeValue::Pointer( ProcThreadAttributeValuePointer{ size: value_size, pointer: value_ptr } ),
259271
);
260272
}
261273

@@ -889,11 +901,21 @@ impl Drop for ProcThreadAttributeList {
889901
}
890902

891903
/// Wrapper around the value data to be used as a Process Thread Attribute.
892-
struct ProcThreadAttributeValue {
904+
struct ProcThreadAttributeValueData {
893905
data: Box<dyn Send + Sync>,
894906
size: usize,
895907
}
896908

909+
struct ProcThreadAttributeValuePointer {
910+
pointer: *const c_void,
911+
size: usize,
912+
}
913+
914+
enum ProcThreadAttributeValue {
915+
Data(ProcThreadAttributeValueData),
916+
Pointer(ProcThreadAttributeValuePointer),
917+
}
918+
897919
fn make_proc_thread_attribute_list(
898920
attributes: &BTreeMap<usize, ProcThreadAttributeValue>,
899921
) -> io::Result<ProcThreadAttributeList> {
@@ -935,18 +957,36 @@ fn make_proc_thread_attribute_list(
935957
// It's theoretically possible for the attribute count to exceed a u32 value.
936958
// Therefore, we ensure that we don't add more attributes than the buffer was initialized for.
937959
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-
})?;
960+
match value {
961+
ProcThreadAttributeValue::Data(value) => {
962+
let value_ptr = core::ptr::addr_of!(*value.data) as _;
963+
cvt(unsafe {
964+
c::UpdateProcThreadAttribute(
965+
proc_thread_attribute_list.0.as_mut_ptr() as _,
966+
0,
967+
attribute,
968+
value_ptr,
969+
value.size,
970+
ptr::null_mut(),
971+
ptr::null_mut(),
972+
)
973+
})?;
974+
}
975+
ProcThreadAttributeValue::Pointer(value) => {
976+
cvt(unsafe {
977+
c::UpdateProcThreadAttribute(
978+
proc_thread_attribute_list.0.as_mut_ptr() as _,
979+
0,
980+
attribute,
981+
value.pointer,
982+
value.size,
983+
ptr::null_mut(),
984+
ptr::null_mut(),
985+
)
986+
})?;
987+
}
988+
}
989+
950990
}
951991

952992
Ok(proc_thread_attribute_list)

0 commit comments

Comments
 (0)