Skip to content

Commit 4a1ce8a

Browse files
committed
Auto merge of #2604 - RalfJung:ret-scalar, r=RalfJung
use Scalar return types for Windows shims I started doing this while working on #2595; now I don't need the rest of this patch but this part still makes sense.
2 parents da29c7a + 1086863 commit 4a1ce8a

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/shims/env.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
144144
name_op: &OpTy<'tcx, Provenance>, // LPCWSTR
145145
buf_op: &OpTy<'tcx, Provenance>, // LPWSTR
146146
size_op: &OpTy<'tcx, Provenance>, // DWORD
147-
) -> InterpResult<'tcx, u32> {
147+
) -> InterpResult<'tcx, Scalar<Provenance>> {
148148
// ^ Returns DWORD (u32 on Windows)
149149

150150
let this = self.eval_context_mut();
@@ -165,12 +165,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
165165
let buf_ptr = this.read_pointer(buf_op)?;
166166
// `buf_size` represents the size in characters.
167167
let buf_size = u64::from(this.read_scalar(size_op)?.to_u32()?);
168-
windows_check_buffer_size(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
168+
Scalar::from_u32(windows_check_buffer_size(
169+
this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?,
170+
))
169171
}
170172
None => {
171173
let envvar_not_found = this.eval_windows("c", "ERROR_ENVVAR_NOT_FOUND")?;
172174
this.set_last_error(envvar_not_found)?;
173-
0 // return zero upon failure
175+
Scalar::from_u32(0) // return zero upon failure
174176
}
175177
})
176178
}
@@ -200,14 +202,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
200202
fn FreeEnvironmentStringsW(
201203
&mut self,
202204
env_block_op: &OpTy<'tcx, Provenance>,
203-
) -> InterpResult<'tcx, i32> {
205+
) -> InterpResult<'tcx, Scalar<Provenance>> {
204206
let this = self.eval_context_mut();
205207
this.assert_target_os("windows", "FreeEnvironmentStringsW");
206208

207209
let env_block_ptr = this.read_pointer(env_block_op)?;
208210
let result = this.deallocate_ptr(env_block_ptr, None, MiriMemoryKind::Runtime.into());
209211
// If the function succeeds, the return value is nonzero.
210-
Ok(i32::from(result.is_ok()))
212+
Ok(Scalar::from_i32(i32::from(result.is_ok())))
211213
}
212214

213215
fn setenv(
@@ -249,7 +251,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
249251
&mut self,
250252
name_op: &OpTy<'tcx, Provenance>, // LPCWSTR
251253
value_op: &OpTy<'tcx, Provenance>, // LPCWSTR
252-
) -> InterpResult<'tcx, i32> {
254+
) -> InterpResult<'tcx, Scalar<Provenance>> {
253255
let this = self.eval_context_mut();
254256
this.assert_target_os("windows", "SetEnvironmentVariableW");
255257

@@ -272,15 +274,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
272274
this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
273275
this.update_environ()?;
274276
}
275-
Ok(1) // return non-zero on success
277+
Ok(Scalar::from_i32(1)) // return non-zero on success
276278
} else {
277279
let value = this.read_os_str_from_wide_str(value_ptr)?;
278280
let var_ptr = alloc_env_var_as_wide_str(&name, &value, this)?;
279281
if let Some(var) = this.machine.env_vars.map.insert(name, var_ptr) {
280282
this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
281283
}
282284
this.update_environ()?;
283-
Ok(1) // return non-zero on success
285+
Ok(Scalar::from_i32(1)) // return non-zero on success
284286
}
285287
}
286288

@@ -347,7 +349,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
347349
&mut self,
348350
size_op: &OpTy<'tcx, Provenance>, // DWORD
349351
buf_op: &OpTy<'tcx, Provenance>, // LPTSTR
350-
) -> InterpResult<'tcx, u32> {
352+
) -> InterpResult<'tcx, Scalar<Provenance>> {
351353
let this = self.eval_context_mut();
352354
this.assert_target_os("windows", "GetCurrentDirectoryW");
353355

@@ -357,16 +359,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
357359
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
358360
this.reject_in_isolation("`GetCurrentDirectoryW`", reject_with)?;
359361
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
360-
return Ok(0);
362+
return Ok(Scalar::from_u32(0));
361363
}
362364

363365
// If we cannot get the current directory, we return 0
364366
match env::current_dir() {
365367
Ok(cwd) =>
366-
return Ok(windows_check_buffer_size(this.write_path_to_wide_str(&cwd, buf, size)?)),
368+
return Ok(Scalar::from_u32(windows_check_buffer_size(
369+
this.write_path_to_wide_str(&cwd, buf, size)?,
370+
))),
367371
Err(e) => this.set_last_error_from_io_error(e.kind())?,
368372
}
369-
Ok(0)
373+
Ok(Scalar::from_u32(0))
370374
}
371375

372376
fn chdir(&mut self, path_op: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx, i32> {
@@ -395,7 +399,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
395399
fn SetCurrentDirectoryW(
396400
&mut self,
397401
path_op: &OpTy<'tcx, Provenance>, // LPCTSTR
398-
) -> InterpResult<'tcx, i32> {
402+
) -> InterpResult<'tcx, Scalar<Provenance>> {
399403
// ^ Returns BOOL (i32 on Windows)
400404

401405
let this = self.eval_context_mut();
@@ -407,14 +411,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
407411
this.reject_in_isolation("`SetCurrentDirectoryW`", reject_with)?;
408412
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
409413

410-
return Ok(0);
414+
return Ok(Scalar::from_i32(0));
411415
}
412416

413417
match env::set_current_dir(path) {
414-
Ok(()) => Ok(1),
418+
Ok(()) => Ok(Scalar::from_i32(1)),
415419
Err(e) => {
416420
this.set_last_error_from_io_error(e.kind())?;
417-
Ok(0)
421+
Ok(Scalar::from_i32(0))
418422
}
419423
}
420424
}

src/shims/time.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
119119
fn QueryPerformanceCounter(
120120
&mut self,
121121
lpPerformanceCount_op: &OpTy<'tcx, Provenance>,
122-
) -> InterpResult<'tcx, i32> {
122+
) -> InterpResult<'tcx, Scalar<Provenance>> {
123123
let this = self.eval_context_mut();
124124

125125
this.assert_target_os("windows", "QueryPerformanceCounter");
@@ -134,14 +134,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
134134
Scalar::from_i64(qpc),
135135
&this.deref_operand(lpPerformanceCount_op)?.into(),
136136
)?;
137-
Ok(-1) // return non-zero on success
137+
Ok(Scalar::from_i32(-1)) // return non-zero on success
138138
}
139139

140140
#[allow(non_snake_case)]
141141
fn QueryPerformanceFrequency(
142142
&mut self,
143143
lpFrequency_op: &OpTy<'tcx, Provenance>,
144-
) -> InterpResult<'tcx, i32> {
144+
) -> InterpResult<'tcx, Scalar<Provenance>> {
145145
let this = self.eval_context_mut();
146146

147147
this.assert_target_os("windows", "QueryPerformanceFrequency");
@@ -155,7 +155,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
155155
Scalar::from_i64(1_000_000_000),
156156
&this.deref_operand(lpFrequency_op)?.into(),
157157
)?;
158-
Ok(-1) // Return non-zero on success
158+
Ok(Scalar::from_i32(-1)) // Return non-zero on success
159159
}
160160

161161
fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar<Provenance>> {

src/shims/windows/foreign_items.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
3737
let [name, buf, size] =
3838
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
3939
let result = this.GetEnvironmentVariableW(name, buf, size)?;
40-
this.write_scalar(Scalar::from_u32(result), dest)?;
40+
this.write_scalar(result, dest)?;
4141
}
4242
"SetEnvironmentVariableW" => {
4343
let [name, value] =
4444
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
4545
let result = this.SetEnvironmentVariableW(name, value)?;
46-
this.write_scalar(Scalar::from_i32(result), dest)?;
46+
this.write_scalar(result, dest)?;
4747
}
4848
"GetEnvironmentStringsW" => {
4949
let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
@@ -54,19 +54,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5454
let [env_block] =
5555
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
5656
let result = this.FreeEnvironmentStringsW(env_block)?;
57-
this.write_scalar(Scalar::from_i32(result), dest)?;
57+
this.write_scalar(result, dest)?;
5858
}
5959
"GetCurrentDirectoryW" => {
6060
let [size, buf] =
6161
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
6262
let result = this.GetCurrentDirectoryW(size, buf)?;
63-
this.write_scalar(Scalar::from_u32(result), dest)?;
63+
this.write_scalar(result, dest)?;
6464
}
6565
"SetCurrentDirectoryW" => {
6666
let [path] =
6767
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
6868
let result = this.SetCurrentDirectoryW(path)?;
69-
this.write_scalar(Scalar::from_i32(result), dest)?;
69+
this.write_scalar(result, dest)?;
7070
}
7171

7272
// Allocation
@@ -218,14 +218,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
218218
let [lpPerformanceCount] =
219219
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
220220
let result = this.QueryPerformanceCounter(lpPerformanceCount)?;
221-
this.write_scalar(Scalar::from_i32(result), dest)?;
221+
this.write_scalar(result, dest)?;
222222
}
223223
"QueryPerformanceFrequency" => {
224224
#[allow(non_snake_case)]
225225
let [lpFrequency] =
226226
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
227227
let result = this.QueryPerformanceFrequency(lpFrequency)?;
228-
this.write_scalar(Scalar::from_i32(result), dest)?;
228+
this.write_scalar(result, dest)?;
229229
}
230230
"Sleep" => {
231231
let [timeout] =
@@ -246,7 +246,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
246246
"TryAcquireSRWLockExclusive" => {
247247
let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
248248
let ret = this.TryAcquireSRWLockExclusive(ptr)?;
249-
this.write_scalar(Scalar::from_u8(ret), dest)?;
249+
this.write_scalar(ret, dest)?;
250250
}
251251
"AcquireSRWLockShared" => {
252252
let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
@@ -259,7 +259,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
259259
"TryAcquireSRWLockShared" => {
260260
let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
261261
let ret = this.TryAcquireSRWLockShared(ptr)?;
262-
this.write_scalar(Scalar::from_u8(ret), dest)?;
262+
this.write_scalar(ret, dest)?;
263263
}
264264

265265
// Dynamic symbol loading

src/shims/windows/sync.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5858
fn TryAcquireSRWLockExclusive(
5959
&mut self,
6060
lock_op: &OpTy<'tcx, Provenance>,
61-
) -> InterpResult<'tcx, u8> {
61+
) -> InterpResult<'tcx, Scalar<Provenance>> {
6262
let this = self.eval_context_mut();
6363
let id = srwlock_get_or_create_id(this, lock_op)?;
6464
let active_thread = this.get_active_thread();
6565

6666
if this.rwlock_is_locked(id) {
6767
// Lock is already held.
68-
Ok(0)
68+
Ok(Scalar::from_u8(0))
6969
} else {
7070
this.rwlock_writer_lock(id, active_thread);
71-
Ok(1)
71+
Ok(Scalar::from_u8(1))
7272
}
7373
}
7474

@@ -107,16 +107,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
107107
fn TryAcquireSRWLockShared(
108108
&mut self,
109109
lock_op: &OpTy<'tcx, Provenance>,
110-
) -> InterpResult<'tcx, u8> {
110+
) -> InterpResult<'tcx, Scalar<Provenance>> {
111111
let this = self.eval_context_mut();
112112
let id = srwlock_get_or_create_id(this, lock_op)?;
113113
let active_thread = this.get_active_thread();
114114

115115
if this.rwlock_is_write_locked(id) {
116-
Ok(0)
116+
Ok(Scalar::from_u8(0))
117117
} else {
118118
this.rwlock_reader_lock(id, active_thread);
119-
Ok(1)
119+
Ok(Scalar::from_u8(1))
120120
}
121121
}
122122

0 commit comments

Comments
 (0)