Skip to content

Commit b3bcc4b

Browse files
committed
move closehandle to handle.rs & remove an as cast
1 parent 1d56ea8 commit b3bcc4b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/shims/windows/foreign_items.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_target::spec::abi::Abi;
1010
use crate::thread::Time;
1111
use crate::*;
1212
use shims::foreign_items::EmulateByNameResult;
13-
use shims::windows::handle::Handle;
13+
use shims::windows::handle::{EvalContextExt as _, Handle};
1414
use shims::windows::sync::EvalContextExt as _;
1515
use shims::windows::thread::EvalContextExt as _;
1616

@@ -393,10 +393,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
393393
let [handle] =
394394
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
395395

396-
match Handle::from_scalar(this.read_scalar(handle)?.check_init()?, this)? {
397-
Some(Handle::Thread(thread)) => this.detach_thread(thread)?,
398-
_ => throw_ub_format!("invalid handle"),
399-
};
396+
this.CloseHandle(handle)?;
400397

401398
this.write_scalar(Scalar::from_u32(1), dest)?;
402399
}

src/shims/windows/handle.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ impl Handle {
109109
}
110110

111111
fn from_packed(handle: i64) -> Option<Self> {
112+
let current_thread_val = Self::CURRENT_THREAD_VALUE as i64;
113+
112114
if handle == 0 {
113115
Some(Self::Null)
114-
} else if handle == Self::CURRENT_THREAD_VALUE as i64 {
116+
} else if handle == current_thread_val {
115117
Some(Self::CurrentThread)
116118
} else if let Ok(handle) = handle.try_into() {
117119
match RealHandle::from_packed(handle)? {
@@ -132,3 +134,19 @@ impl Handle {
132134
Ok(Self::from_packed(handle))
133135
}
134136
}
137+
138+
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
139+
140+
#[allow(non_snake_case)]
141+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
142+
fn CloseHandle(&mut self, handle_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
143+
let this = self.eval_context_mut();
144+
145+
match Handle::from_scalar(this.read_scalar(handle_op)?.check_init()?, this)? {
146+
Some(Handle::Thread(thread)) => this.detach_thread(thread)?,
147+
_ => throw_ub_format!("invalid handle"),
148+
};
149+
150+
Ok(())
151+
}
152+
}

0 commit comments

Comments
 (0)