Skip to content

Commit 4e38fbe

Browse files
committed
follow-up2 to review (few issues not resolved yet)
1 parent 2051805 commit 4e38fbe

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/shims/env.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#![allow(non_snake_case)]
2-
31
use std::ffi::{OsString, OsStr};
42
use std::env;
53
use std::convert::TryFrom;
6-
use std::collections::hash_map::Values;
74

85
use crate::stacked_borrows::Tag;
96
use crate::rustc_target::abi::LayoutOf;
@@ -47,10 +44,6 @@ impl<'tcx> EnvVars<'tcx> {
4744
}
4845
ecx.update_environ()
4946
}
50-
51-
fn values(&self) -> InterpResult<'tcx, Values<'_, OsString, Pointer<Tag>>> {
52-
Ok(self.map.values())
53-
}
5447
}
5548

5649
fn alloc_env_var_as_c_str<'mir, 'tcx>(
@@ -115,11 +108,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
115108
})
116109
}
117110

111+
#[allow(non_snake_case)]
118112
fn GetEnvironmentVariableW(
119113
&mut self,
120-
name_op: OpTy<'tcx, Tag>, // LPCWSTR lpName
121-
buf_op: OpTy<'tcx, Tag>, // LPWSTR lpBuffer
122-
size_op: OpTy<'tcx, Tag>, // DWORD nSize
114+
name_op: OpTy<'tcx, Tag>, // LPCWSTR
115+
buf_op: OpTy<'tcx, Tag>, // LPWSTR
116+
size_op: OpTy<'tcx, Tag>, // DWORD
123117
) -> InterpResult<'tcx, u64> {
124118
let this = self.eval_context_mut();
125119
this.assert_target_os("windows", "GetEnvironmentVariableW");
@@ -134,7 +128,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
134128
let var_ptr = Scalar::from(var_ptr.offset(Size::from_bytes(name_offset_bytes), this)?);
135129

136130
let var_size = u64::try_from(this.read_os_str_from_wide_str(var_ptr)?.len()).unwrap();
137-
// `buf_size` represent size in characters.
131+
// `buf_size` represents the size in characters.
138132
let buf_size = u64::try_from(this.read_scalar(size_op)?.to_u32()?).unwrap();
139133
let return_val = if var_size.checked_add(1).unwrap() > buf_size {
140134
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
@@ -157,31 +151,41 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157151
})
158152
}
159153

154+
#[allow(non_snake_case)]
160155
fn GetEnvironmentStringsW(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
161156
let this = self.eval_context_mut();
162157
this.assert_target_os("windows", "GetEnvironmentStringsW");
163158

164159
// Info on layout of environment blocks in Windows:
165160
// https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables
166161
let mut env_vars = std::ffi::OsString::new();
167-
for &item in this.machine.env_vars.values()? {
162+
for &item in this.machine.env_vars.map.values() {
168163
let env_var = this.read_os_str_from_wide_str(Scalar::from(item))?;
169164
env_vars.push(env_var);
170165
env_vars.push("\0");
171166
}
172167
// Allocate environment block & Store environment variables to environment block.
173168
// Final null terminator(block terminator) is added by `alloc_os_str_to_wide_str`.
169+
// FIXME: MemoryKind should be `MiMemoryKind::Machine`,
170+
// but using it results in a Stacked Borrows error when running MIRI on 'tests/run-pass/env.rs'
171+
// For now, use `MiriMemoryKind::WinHeap` instead.
174172
let envblock_ptr = this.alloc_os_str_as_wide_str(&env_vars, MiriMemoryKind::WinHeap.into());
175-
173+
// If the function succeeds, the return value is a pointer to the environment block of the current process.
176174
Ok(envblock_ptr.into())
177175
}
178176

179-
fn FreeEnvironmentStringsW(&mut self, env_block_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
177+
#[allow(non_snake_case)]
178+
fn FreeEnvironmentStringsW(&mut self, env_block_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
180179
let this = self.eval_context_mut();
181180
this.assert_target_os("windows", "FreeEnvironmentStringsW");
182181

183182
let env_block_ptr = this.read_scalar(env_block_op)?.not_undef()?;
184-
Ok(this.memory.deallocate(this.force_ptr(env_block_ptr)?, None, MiriMemoryKind::WinHeap.into()).is_ok())
183+
// FIXME: MemoryKind should be `MiMemoryKind::Machine`,
184+
// but using it results in a Stacked Borrows error when running MIRI on 'tests/run-pass/env.rs'
185+
// For now, use `MiriMemoryKind::WinHeap` instead.
186+
let result = this.memory.deallocate(this.force_ptr(env_block_ptr)?, None, MiriMemoryKind::WinHeap.into());
187+
// If the function succeeds, the return value is nonzero.
188+
Ok(result.is_ok() as i32)
185189
}
186190

187191
fn setenv(
@@ -220,10 +224,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
220224
}
221225
}
222226

227+
#[allow(non_snake_case)]
223228
fn SetEnvironmentVariableW(
224229
&mut self,
225-
name_op: OpTy<'tcx, Tag>, // LPCWSTR lpName,
226-
value_op: OpTy<'tcx, Tag>, // LPCWSTR lpValue,
230+
name_op: OpTy<'tcx, Tag>, // LPCWSTR
231+
value_op: OpTy<'tcx, Tag>, // LPCWSTR
227232
) -> InterpResult<'tcx, i32> {
228233
let mut this = self.eval_context_mut();
229234
this.assert_target_os("windows", "SetEnvironmentVariableW");
@@ -233,14 +238,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
233238

234239
if this.is_null(name_ptr)? {
235240
// ERROR CODE is not clearly explained in docs.. For now, throw UB instead.
236-
throw_ub_format!("Pointer to environment variable name is NULL");
241+
throw_ub_format!("pointer to environment variable name is NULL");
237242
}
238243

239244
let name = this.read_os_str_from_wide_str(name_ptr)?;
240245
if name.is_empty() {
241-
throw_unsup_format!("Environment variable name is an empty string");
246+
throw_unsup_format!("environment variable name is an empty string");
242247
} else if name.to_string_lossy().contains('=') {
243-
throw_unsup_format!("Environment variable name contains '='");
248+
throw_unsup_format!("environment variable name contains '='");
244249
} else if this.is_null(value_ptr)? {
245250
// Delete environment variable `{name}`
246251
if let Some(var) = this.machine.env_vars.map.remove(&name) {

src/shims/foreign_items/windows.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3434

3535
"GetEnvironmentStringsW" => {
3636
let result = this.GetEnvironmentStringsW()?;
37-
// If the function succeeds, the return value is a pointer to the environment block of the current process.
3837
this.write_scalar(result, dest)?;
3938
}
4039

4140
"FreeEnvironmentStringsW" => {
4241
let result = this.FreeEnvironmentStringsW(args[0])?;
43-
// If the function succeeds, the return value is nonzero.
44-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
42+
this.write_scalar(Scalar::from_i32(result), dest)?;
4543
}
4644

4745
// File related shims

0 commit comments

Comments
 (0)