Skip to content

Commit 3bb5d85

Browse files
committed
Windows shims for
GetEnvironmentVariableW/SetEnvironmentVariableW/GetEnvironmentStringsW
1 parent 6e302b8 commit 3bb5d85

File tree

4 files changed

+231
-33
lines changed

4 files changed

+231
-33
lines changed

src/helpers.rs

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::OsStr;
1+
use std::ffi::{OsStr, OsString};
22
use std::{iter, mem};
33
use std::convert::TryFrom;
44

@@ -456,6 +456,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
456456
}
457457
}
458458

459+
/// Dispatches to appropriate implementations for reading an OsString from Memory,
460+
/// depending on the interpretation target.
461+
fn read_os_str_from_target_str(&self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
462+
let target_os = self.eval_context_ref().tcx.sess.target.target.target_os.as_str();
463+
match target_os {
464+
"linux" | "macos" => self.read_os_str_from_c_str(scalar).map(|x| x.to_os_string()),
465+
"windows" => self.read_os_str_from_wide_str(scalar),
466+
_ => throw_unsup_format!("OsString support for target OS not yet available"),
467+
}
468+
}
469+
459470
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
460471
/// the Unix APIs usually handle.
461472
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
@@ -471,14 +482,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
471482
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
472483
let s = std::str::from_utf8(bytes)
473484
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
474-
Ok(&OsStr::new(s))
485+
Ok(OsStr::new(s))
475486
}
476487

477488
let this = self.eval_context_ref();
478489
let bytes = this.memory.read_c_str(scalar)?;
479490
bytes_to_os_str(bytes)
480491
}
481492

493+
/// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
494+
/// which is what the Windows APIs usually handle.
495+
fn read_os_str_from_wide_str(&self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
496+
let u16_vec = self.eval_context_ref().memory.read_wide_str(scalar)?;
497+
u16vec_to_osstring(u16_vec)
498+
}
499+
500+
482501
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
483502
/// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
484503
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
@@ -518,21 +537,106 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
518537
Ok((true, string_length))
519538
}
520539

540+
/// Helper function to write an OsStr as a 0x0000-terminated u16-sequence, which is what
541+
/// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
542+
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
543+
/// terminator. It returns `Ok((true, length))` if the writing process was successful. The
544+
/// string length returned does not include the null terminator.
545+
fn write_os_str_to_wide_str(
546+
&mut self,
547+
os_str: &OsStr,
548+
mplace: MPlaceTy<'tcx, Tag>,
549+
size: u64,
550+
) -> InterpResult<'tcx, (bool, u64)> {
551+
#[cfg(target_os = "windows")]
552+
fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
553+
Ok(std::os::windows::ffi::OsStrExt::encode_wide(os_str).collect())
554+
}
555+
#[cfg(not(target_os = "windows"))]
556+
fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
557+
// On non-unix platforms the best we can do to transform Vec<u16> from/to OS strings is to do the
558+
// intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
559+
// valid.
560+
os_str
561+
.to_str()
562+
.map(|s| s.encode_utf16().collect())
563+
.ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
564+
}
565+
566+
let u16_vec = os_str_to_u16vec(os_str)?;
567+
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
568+
// 0x0000 terminator to memory would cause an out-of-bounds access.
569+
let string_length = u16_vec.len() as u64;
570+
if size <= string_length {
571+
return Ok((false, string_length));
572+
}
573+
574+
let this = self.eval_context_mut();
575+
576+
// Store the UTF-16 string.
577+
let char_size = Size::from_bytes(2);
578+
for (idx, c) in u16_vec.into_iter().chain(iter::once(0x0000)).enumerate() {
579+
let place = this.mplace_field(mplace, idx as u64)?;
580+
this.write_scalar(Scalar::from_uint(c, char_size), place.into())?;
581+
}
582+
Ok((true, string_length))
583+
}
584+
585+
/// Dispatches to appropriate implementations for allocating & writing OsString in Memory,
586+
/// depending on the interpretation target.
587+
fn alloc_os_str_as_target_str(
588+
&mut self,
589+
os_str: &OsStr,
590+
memkind: MemoryKind<MiriMemoryKind>,
591+
) -> InterpResult<'tcx, Pointer<Tag>> {
592+
let target_os = self.eval_context_ref().tcx.sess.target.target.target_os.as_str();
593+
match target_os {
594+
"linux" | "macos" => self.alloc_os_str_as_c_str(os_str, memkind),
595+
"windows" => self.alloc_os_str_as_wide_str(os_str, memkind),
596+
_ => throw_unsup_format!("OsString support for target OS not yet available"),
597+
}
598+
}
599+
521600
fn alloc_os_str_as_c_str(
522601
&mut self,
523602
os_str: &OsStr,
524603
memkind: MemoryKind<MiriMemoryKind>,
525-
) -> Pointer<Tag> {
604+
) -> InterpResult<'tcx, Pointer<Tag>> {
526605
let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0` terminator.
527606
let this = self.eval_context_mut();
528607

529608
let arg_type = this.tcx.mk_array(this.tcx.types.u8, size);
530609
let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
531610
self.write_os_str_to_c_str(os_str, arg_place.ptr, size).unwrap();
532-
arg_place.ptr.assert_ptr()
611+
Ok(arg_place.ptr.assert_ptr())
612+
}
613+
614+
fn alloc_os_str_as_wide_str(
615+
&mut self,
616+
os_str: &OsStr,
617+
memkind: MemoryKind<MiriMemoryKind>,
618+
) -> InterpResult<'tcx, Pointer<Tag>> {
619+
let size = os_str.len() as u64 + 1; // Make space for `0x0000` terminator.
620+
let this = self.eval_context_mut();
621+
622+
let arg_type = this.tcx.mk_array(this.tcx.types.u16, size);
623+
let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
624+
self.write_os_str_to_wide_str(os_str, arg_place, size).unwrap();
625+
Ok(arg_place.ptr.assert_ptr())
533626
}
534627
}
535628

629+
#[cfg(target_os = "windows")]
630+
pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
631+
Ok(std::os::windows::ffi::OsStringExt::from_wide(&u16_vec[..]))
632+
}
633+
#[cfg(not(target_os = "windows"))]
634+
pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
635+
let s = String::from_utf16(&u16_vec[..])
636+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
637+
Ok(s.into())
638+
}
639+
536640
pub fn immty_from_int_checked<'tcx>(
537641
int: impl Into<i128>,
538642
layout: TyLayout<'tcx>,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub use crate::diagnostics::{
4949
TerminationInfo, NonHaltingDiagnostic,
5050
};
5151
pub use crate::eval::{create_ecx, eval_main, MiriConfig};
52-
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
52+
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt, u16vec_to_osstring};
5353
pub use crate::machine::{
5454
AllocExtra, Evaluator, FrameData, MemoryExtra, MiriEvalContext, MiriEvalContextExt,
5555
MiriMemoryKind, NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,

src/shims/env.rs

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ffi::{OsString, OsStr};
22
use std::env;
33
use std::convert::TryFrom;
4+
use std::collections::hash_map::Values;
45

56
use crate::stacked_borrows::Tag;
67
use crate::rustc_target::abi::LayoutOf;
@@ -13,7 +14,7 @@ use rustc_mir::interpret::Pointer;
1314
#[derive(Default)]
1415
pub struct EnvVars<'tcx> {
1516
/// Stores pointers to the environment variables. These variables must be stored as
16-
/// null-terminated C strings with the `"{name}={value}"` format.
17+
/// null-terminated target strings(c_str or wide_str) with the `"{name}={value}"` format.
1718
map: FxHashMap<OsString, Pointer<Tag>>,
1819

1920
/// Place where the `environ` static is stored. Lazily initialized, but then never changes.
@@ -29,42 +30,100 @@ impl<'tcx> EnvVars<'tcx> {
2930
for (name, value) in env::vars() {
3031
if !excluded_env_vars.contains(&name) {
3132
let var_ptr =
32-
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx);
33+
alloc_env_var_as_target_str(name.as_ref(), value.as_ref(), ecx)?;
3334
ecx.machine.env_vars.map.insert(OsString::from(name), var_ptr);
3435
}
3536
}
3637
}
3738
ecx.update_environ()
3839
}
40+
41+
pub(super) fn values(&self) -> InterpResult<'tcx, Values<'_, OsString, Pointer<Tag>>> {
42+
Ok(self.map.values())
43+
}
3944
}
4045

41-
fn alloc_env_var_as_c_str<'mir, 'tcx>(
46+
fn alloc_env_var_as_target_str<'mir, 'tcx>(
4247
name: &OsStr,
4348
value: &OsStr,
4449
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
45-
) -> Pointer<Tag> {
50+
) -> InterpResult<'tcx, Pointer<Tag>> {
4651
let mut name_osstring = name.to_os_string();
4752
name_osstring.push("=");
4853
name_osstring.push(value);
49-
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
54+
Ok(ecx.alloc_os_str_as_target_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())?)
5055
}
5156

5257
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
5358
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
54-
fn getenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
55-
let this = self.eval_context_mut();
59+
fn getenv(&self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
60+
let this = self.eval_context_ref();
5661

5762
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
58-
let name = this.read_os_str_from_c_str(name_ptr)?;
59-
Ok(match this.machine.env_vars.map.get(name) {
60-
// The offset is used to strip the "{name}=" part of the string.
63+
let name = this.read_os_str_from_target_str(name_ptr)?;
64+
Ok(match this.machine.env_vars.map.get(&name) {
6165
Some(var_ptr) => {
66+
// The offset is used to strip the "{name}=" part of the string.
6267
Scalar::from(var_ptr.offset(Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()), this)?)
6368
}
6469
None => Scalar::ptr_null(&*this.tcx),
6570
})
6671
}
6772

73+
74+
fn getenvironmentvariablew(
75+
&mut self,
76+
name_op: OpTy<'tcx, Tag>, // LPCWSTR lpName
77+
buf_op: OpTy<'tcx, Tag>, // LPWSTR lpBuffer
78+
size_op: OpTy<'tcx, Tag>, // DWORD nSize
79+
) -> InterpResult<'tcx, u32> {
80+
let this = self.eval_context_mut();
81+
82+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
83+
let name = this.read_os_str_from_target_str(name_ptr)?;
84+
Ok(match this.machine.env_vars.map.get(&name) {
85+
Some(var_ptr) => {
86+
// The offset is used to strip the "{name}=" part of the string.
87+
let var_ptr = Scalar::from(var_ptr.offset(Size::from_bytes((name.len() as u64 + 1) * 2), this)?);
88+
let buf_size = this.read_scalar(size_op)?.to_i32()? as u64;
89+
let buf_ptr = this.read_scalar(buf_op)?.not_undef()?;
90+
let size_u16 = Size::from_bytes(2);
91+
92+
// The following loop attempts to figure out the length of env_var (`var_size`)
93+
let mut var_size = 0u64;
94+
loop {
95+
let temp_var_ptr = var_ptr.ptr_offset(Size::from_bytes(var_size * 2), this)?;
96+
let bytes = this.memory.read_bytes(temp_var_ptr, size_u16)?;
97+
var_size += 1;
98+
// encountered 0x0000 terminator
99+
if bytes[0] == 0 && bytes[1] == 0 { break; }
100+
}
101+
102+
let return_val = if var_size > buf_size {
103+
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
104+
// required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
105+
var_size
106+
} else {
107+
for i in 0..var_size {
108+
this.memory.copy(
109+
this.force_ptr(var_ptr.ptr_offset(Size::from_bytes(i * 2), this)?)?,
110+
this.force_ptr(buf_ptr.ptr_offset(Size::from_bytes(i * 2), this)?)?,
111+
size_u16,
112+
true,
113+
)?;
114+
}
115+
// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
116+
// not including the terminating null character.
117+
var_size - 1
118+
};
119+
assert_eq!(return_val as u32 as u64, return_val);
120+
return_val as u32
121+
}
122+
// return zero upon failure
123+
None => 0u32
124+
})
125+
}
126+
68127
fn setenv(
69128
&mut self,
70129
name_op: OpTy<'tcx, Tag>,
@@ -74,34 +133,43 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
74133

75134
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
76135
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
77-
let value = this.read_os_str_from_c_str(value_ptr)?;
136+
let value = this.read_os_str_from_target_str(value_ptr)?;
78137
let mut new = None;
79138
if !this.is_null(name_ptr)? {
80-
let name = this.read_os_str_from_c_str(name_ptr)?;
139+
let name = this.read_os_str_from_target_str(name_ptr)?;
81140
if !name.is_empty() && !name.to_string_lossy().contains('=') {
82141
new = Some((name.to_owned(), value.to_owned()));
83142
}
84143
}
85144
if let Some((name, value)) = new {
86-
let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this);
145+
let var_ptr = alloc_env_var_as_target_str(&name, &value, &mut this)?;
87146
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
88147
this.memory
89148
.deallocate(var, None, MiriMemoryKind::Machine.into())?;
90149
}
91150
this.update_environ()?;
92-
Ok(0)
151+
Ok(0) // return zero on success
93152
} else {
94153
Ok(-1)
95154
}
96155
}
97156

157+
fn setenvironmentvariablew(
158+
&mut self,
159+
name_op: OpTy<'tcx, Tag>, // LPCWSTR lpName,
160+
value_op: OpTy<'tcx, Tag>, // LPCWSTR lpValue,
161+
) -> InterpResult<'tcx, i32> {
162+
// return non-zero on success
163+
self.setenv(name_op, value_op).map(|x| x + 1)
164+
}
165+
98166
fn unsetenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
99167
let this = self.eval_context_mut();
100168

101169
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
102170
let mut success = None;
103171
if !this.is_null(name_ptr)? {
104-
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
172+
let name = this.read_os_str_from_target_str(name_ptr)?.to_owned();
105173
if !name.is_empty() && !name.to_string_lossy().contains('=') {
106174
success = Some(this.machine.env_vars.map.remove(&name));
107175
}

0 commit comments

Comments
 (0)