Skip to content

Commit 0154be6

Browse files
committed
initial work on adding 'read/write/alloc wide_str' functions
1 parent 97c2575 commit 0154be6

File tree

2 files changed

+142
-15
lines changed

2 files changed

+142
-15
lines changed

src/helpers.rs

Lines changed: 124 additions & 6 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

44
use rustc::mir;
@@ -447,29 +447,77 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
447447
}
448448
}
449449

450+
fn read_os_str_from_target_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
451+
if cfg!(target_os = "unix") {
452+
self.read_os_str_from_c_str(scalar)
453+
} else if cfg!(target_os = "windows") {
454+
self.read_os_str_from_wide_str(scalar)
455+
} else {
456+
throw_unsup_format!("support for target OS not yet available")
457+
}
458+
}
459+
450460
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
451461
/// the Unix APIs usually handle.
452-
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
462+
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString>
453463
where
454464
'tcx: 'a,
455465
'mir: 'a,
456466
{
457467
#[cfg(target_os = "unix")]
458-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
459-
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes))
468+
fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, OsString> {
469+
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes).to_os_string())
460470
}
461471
#[cfg(not(target_os = "unix"))]
462-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
472+
fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, OsString> {
463473
let s = std::str::from_utf8(bytes)
464474
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
465-
Ok(&OsStr::new(s))
475+
Ok(OsStr::new(s).to_os_string())
466476
}
467477

468478
let this = self.eval_context_ref();
469479
let bytes = this.memory.read_c_str(scalar)?;
470480
bytes_to_os_str(bytes)
471481
}
472482

483+
/// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
484+
/// which is what the Windows APIs usually handle.
485+
fn read_os_str_from_wide_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString>
486+
where
487+
'tcx: 'a,
488+
'mir: 'a,
489+
{
490+
#[cfg(target_os = "windows")]
491+
fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
492+
Ok(std::os::windows::ffi::OsStringExt::from_wide(&u16_vec[..]))
493+
}
494+
#[cfg(not(target_os = "windows"))]
495+
fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
496+
let s = String::from_utf16(&u16_vec[..])
497+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
498+
Ok(s.into())
499+
}
500+
501+
let this = self.eval_context_ref();
502+
let u16_vec = this.memory.read_wide_str(scalar)?;
503+
u16vec_to_osstring(u16_vec)
504+
}
505+
506+
fn write_os_str_to_target_str(
507+
&mut self,
508+
os_str: &OsStr,
509+
scalar: Scalar<Tag>,
510+
size: u64,
511+
) -> InterpResult<'tcx, (bool, u64)> {
512+
if cfg!(target_os = "unix") {
513+
self.write_os_str_to_c_str(os_str, scalar, size)
514+
} else if cfg!(target_os = "windows") {
515+
self.write_os_str_to_wide_str(os_str, scalar, size)
516+
} else {
517+
panic!("support for target OS not yet available")
518+
}
519+
}
520+
473521
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
474522
/// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
475523
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
@@ -509,6 +557,62 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
509557
Ok((true, string_length))
510558
}
511559

560+
/// Helper function to write an OsStr as a 0x0000-terminated u16-sequence, which is what
561+
/// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
562+
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
563+
/// terminator. It returns `Ok((true, length))` if the writing process was successful. The
564+
/// string length returned does not include the null terminator.
565+
fn write_os_str_to_wide_str(
566+
&mut self,
567+
os_str: &OsStr,
568+
scalar: Scalar<Tag>,
569+
size: u64,
570+
) -> InterpResult<'tcx, (bool, u64)> {
571+
#[cfg(target_os = "windows")]
572+
fn os_str_to_u16vec(os_str: &OsStr) -> Vec<u16> {
573+
std::os::windows::ffi::OsStrExt::encode_wide(os_str).collect()
574+
}
575+
#[cfg(not(target_os = "windows"))]
576+
fn os_str_to_u16vec(os_str: &OsStr) -> Vec<u16> {
577+
os_str.to_str().encode_utf16().collect()
578+
}
579+
580+
let u16_vec = os_str_to_u16vec(os_str);
581+
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
582+
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
583+
let string_length = u16_vec.len() as u64;
584+
if size <= string_length {
585+
return Ok((false, string_length));
586+
}
587+
588+
let this = self.eval_context_mut();
589+
590+
// Store the UTF-16 string. We just allocated so we know the bounds are fine.
591+
let char_size = Size::from_bytes(2);
592+
let place_ptr = scalar.assert_ptr();
593+
/*
594+
for (idx, &c) in u16_vec.iter().enumerate() {
595+
let place = this.mplace_field(place, idx as u64)?;
596+
this.write_scalar(Scalar::from_uint(c, char_size), place.into())?;
597+
}
598+
*/
599+
Ok((true, string_length))
600+
}
601+
602+
fn alloc_os_str_as_target_str(
603+
&mut self,
604+
os_str: &OsStr,
605+
memkind: MemoryKind<MiriMemoryKind>,
606+
) -> Pointer<Tag> {
607+
if cfg!(target_os = "unix") {
608+
self.alloc_os_str_as_c_str(os_str, memkind)
609+
} else if cfg!(target_os = "windows") {
610+
self.alloc_os_str_as_wide_str(os_str, memkind)
611+
} else {
612+
panic!("support for target OS not yet available")
613+
}
614+
}
615+
512616
fn alloc_os_str_as_c_str(
513617
&mut self,
514618
os_str: &OsStr,
@@ -522,6 +626,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
522626
self.write_os_str_to_c_str(os_str, arg_place.ptr, size).unwrap();
523627
arg_place.ptr.assert_ptr()
524628
}
629+
630+
fn alloc_os_str_as_wide_str(
631+
&mut self,
632+
os_str: &OsStr,
633+
memkind: MemoryKind<MiriMemoryKind>,
634+
) -> Pointer<Tag> {
635+
let size = os_str.len() as u64 + 1; // Make space for `0x0000` terminator.
636+
let this = self.eval_context_mut();
637+
638+
let arg_type = this.tcx.mk_array(this.tcx.types.u16, size);
639+
let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
640+
self.write_os_str_to_wide_str(os_str, arg_place.ptr, size).unwrap();
641+
arg_place.ptr.assert_ptr()
642+
}
525643
}
526644

527645
pub fn immty_from_int_checked<'tcx>(

src/shims/env.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'tcx> EnvVars<'tcx> {
2828
for (name, value) in env::vars() {
2929
if !excluded_env_vars.contains(&name) {
3030
let var_ptr =
31-
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx);
31+
alloc_env_var_as_target_str(name.as_ref(), value.as_ref(), ecx);
3232
ecx.machine.env_vars.map.insert(OsString::from(name), var_ptr);
3333
}
3434
}
@@ -37,15 +37,15 @@ impl<'tcx> EnvVars<'tcx> {
3737
}
3838
}
3939

40-
fn alloc_env_var_as_c_str<'mir, 'tcx>(
40+
fn alloc_env_var_as_target_str<'mir, 'tcx>(
4141
name: &OsStr,
4242
value: &OsStr,
4343
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
4444
) -> Pointer<Tag> {
4545
let mut name_osstring = name.to_os_string();
4646
name_osstring.push("=");
4747
name_osstring.push(value);
48-
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
48+
ecx.alloc_os_str_as_target_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
4949
}
5050

5151
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -54,8 +54,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5454
let this = self.eval_context_mut();
5555

5656
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
57-
let name = this.read_os_str_from_c_str(name_ptr)?;
58-
Ok(match this.machine.env_vars.map.get(name) {
57+
let name = this.read_os_str_from_target_str(name_ptr)?;
58+
Ok(match this.machine.env_vars.map.get(&name) {
5959
// The offset is used to strip the "{name}=" part of the string.
6060
Some(var_ptr) => {
6161
Scalar::from(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?)
@@ -64,6 +64,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6464
})
6565
}
6666

67+
68+
fn getenvironmentvariablew() {
69+
70+
}
71+
6772
fn setenv(
6873
&mut self,
6974
name_op: OpTy<'tcx, Tag>,
@@ -73,16 +78,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7378

7479
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
7580
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
76-
let value = this.read_os_str_from_c_str(value_ptr)?;
81+
let value = this.read_os_str_from_target_str(value_ptr)?;
7782
let mut new = None;
7883
if !this.is_null(name_ptr)? {
79-
let name = this.read_os_str_from_c_str(name_ptr)?;
84+
let name = this.read_os_str_from_target_str(name_ptr)?;
8085
if !name.is_empty() && !name.to_string_lossy().contains('=') {
8186
new = Some((name.to_owned(), value.to_owned()));
8287
}
8388
}
8489
if let Some((name, value)) = new {
85-
let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this);
90+
let var_ptr = alloc_env_var_as_target_str(&name, &value, &mut this);
8691
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
8792
this.memory
8893
.deallocate(var, None, MiriMemoryKind::Machine.into())?;
@@ -94,13 +99,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9499
}
95100
}
96101

102+
fn setenvironmentvariablew() {
103+
104+
}
105+
97106
fn unsetenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
98107
let this = self.eval_context_mut();
99108

100109
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
101110
let mut success = None;
102111
if !this.is_null(name_ptr)? {
103-
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
112+
let name = this.read_os_str_from_target_str(name_ptr)?.to_owned();
104113
if !name.is_empty() && !name.to_string_lossy().contains('=') {
105114
success = Some(this.machine.env_vars.map.remove(&name));
106115
}

0 commit comments

Comments
 (0)