Skip to content

Commit a303bb9

Browse files
committed
simplify mm
1 parent 57fa933 commit a303bb9

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/windows/spawn.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,39 @@ pub(in crate::windows) struct ChildParams {
138138
// TODO: upstream to winapi: https://github.com/retep998/winapi-rs/pull/933/
139139
const MAGIC_PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES: usize = 131081;
140140

141+
struct AlignedMemBlock(*mut u8, usize);
142+
143+
impl AlignedMemBlock {
144+
fn layout(cnt: usize) -> std::alloc::Layout {
145+
assert!(cnt > 0);
146+
std::alloc::Layout::from_size_align(cnt, 8).unwrap()
147+
}
148+
149+
fn new(cnt: usize) -> AlignedMemBlock {
150+
let ptr = unsafe { std::alloc::alloc_zeroed(Self::layout(cnt)) };
151+
AlignedMemBlock(ptr, cnt)
152+
}
153+
154+
fn ptr(&self) -> *mut u8 {
155+
self.0
156+
}
157+
}
158+
159+
impl Drop for AlignedMemBlock {
160+
fn drop(&mut self) {
161+
unsafe {
162+
std::alloc::dealloc(self.0, Self::layout(self.1));
163+
}
164+
}
165+
}
166+
141167
pub(in crate::windows) fn spawn(
142168
sandbox: &WindowsSandbox,
143169
stdio: Stdio,
144170
params: ChildParams,
145171
) -> Result<PROCESS_INFORMATION, Error> {
146-
let mut proc_thread_attr_list_storage: Vec<u64>;
147-
let mut security_capabilities: ();
172+
let mut proc_thread_attr_list_storage;
173+
let mut security_capabilities;
148174
let mut startup_info = unsafe {
149175
let mut startup_info: STARTUPINFOEXW = std::mem::zeroed();
150176
let mut proc_thread_attr_list_len = 0;
@@ -160,26 +186,28 @@ pub(in crate::windows) fn spawn(
160186
return Err(Error::last());
161187
}
162188
}
163-
proc_thread_attr_list_storage = Vec::with_capacity((proc_thread_attr_list_len - 1) / 8 + 1);
164-
let proc_thread_attr_list: *mut u8 = proc_thread_attr_list_storage.as_mut_ptr().cast();
165-
proc_thread_attr_list.write_bytes(0, proc_thread_attr_list_len);
189+
proc_thread_attr_list_storage = AlignedMemBlock::new(proc_thread_attr_list_len);
190+
let proc_thread_attr_list = proc_thread_attr_list_storage.ptr();
166191
startup_info.lpAttributeList = proc_thread_attr_list.cast();
167192
Cvt::nonzero(InitializeProcThreadAttributeList(
168193
startup_info.lpAttributeList,
169194
1,
170195
0,
171196
&mut proc_thread_attr_list_len,
172197
))?;
173-
/*security_capabilities = sandbox.profile.get_security_capabilities();
198+
security_capabilities = sandbox.profile.get_security_capabilities();
174199
Cvt::nonzero(UpdateProcThreadAttribute(
175200
startup_info.lpAttributeList,
201+
// reserved
176202
0,
177203
MAGIC_PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES,
178204
(&mut security_capabilities as *mut SECURITY_CAPABILITIES).cast(),
179205
std::mem::size_of::<SECURITY_ATTRIBUTES>(),
206+
// reserved
180207
std::ptr::null_mut(),
208+
// reserved
181209
std::ptr::null_mut(),
182-
))?;*/
210+
))?;
183211

184212
startup_info.StartupInfo.cb = size_of::<STARTUPINFOEXW>() as u32;
185213
startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
@@ -211,13 +239,12 @@ pub(in crate::windows) fn spawn(
211239
// inherit handles
212240
TRUE,
213241
creation_flags,
214-
// TEMP DEBUG
215-
std::ptr::null_mut(),
216-
//env.as_mut_ptr().cast(),
242+
env.as_mut_ptr().cast(),
217243
cwd.as_ptr(),
218244
(&mut startup_info as *mut STARTUPINFOEXW).cast(),
219245
&mut info,
220246
))?;
247+
DeleteProcThreadAttributeList(startup_info.lpAttributeList);
221248
}
222249
Ok(info)
223250
}

0 commit comments

Comments
 (0)