Skip to content

Commit

Permalink
implement Windows shim for 'GetEnvironmentStringsW'
Browse files Browse the repository at this point in the history
  • Loading branch information
JOE1994 committed Mar 21, 2020
1 parent 2e0bda8 commit 72aa9c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
22 changes: 11 additions & 11 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
/// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
/// which is what the Windows APIs usually handle.
fn read_os_str_from_wide_str(&self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
#[cfg(target_os = "windows")]
fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
Ok(std::os::windows::ffi::OsStringExt::from_wide(&u16_vec[..]))
}
#[cfg(not(target_os = "windows"))]
fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
let s = String::from_utf16(&u16_vec[..])
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
Ok(s.into())
}

let u16_vec = self.eval_context_ref().memory.read_wide_str(scalar)?;
u16vec_to_osstring(u16_vec)
}
Expand Down Expand Up @@ -637,6 +626,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}

#[cfg(target_os = "windows")]
pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
Ok(std::os::windows::ffi::OsStringExt::from_wide(&u16_vec[..]))
}
#[cfg(not(target_os = "windows"))]
pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
let s = String::from_utf16(&u16_vec[..])
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
Ok(s.into())
}

pub fn immty_from_int_checked<'tcx>(
int: impl Into<i128>,
layout: TyLayout<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub use crate::diagnostics::{
NonHaltingDiagnostic,
};
pub use crate::eval::{create_ecx, eval_main, MiriConfig, TerminationInfo};
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt, u16vec_to_osstring};
pub use crate::machine::{
AllocExtra, Evaluator, FrameData, MemoryExtra, MiriEvalContext, MiriEvalContextExt,
MiriMemoryKind, NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
Expand Down
9 changes: 5 additions & 4 deletions src/shims/foreign_items/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::*;
use crate::rustc_target::abi::LayoutOf;
use crate::helpers::u16vec_to_osstring;
use rustc::mir;
use rustc::ty::layout::Size;
use std::iter;
Expand Down Expand Up @@ -44,14 +45,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
for &item in this.machine.env_vars.values()? {
let env_var = this.read_os_str_from_target_str(Scalar::from(item))?;
env_vars.push(env_var);
env_vars.push(" ");
env_vars.push(u16vec_to_osstring(vec![0x0000 as u16])?);
}

// Allocate environment block
let tcx = this.tcx;
let env_block_size = env_vars.len() + 1;
let env_block_type = tcx.mk_array(tcx.types.u16, u64::try_from(env_block_size).unwrap());
let env_block_place = this.allocate(this.layout_of(env_block_type)?, MiriMemoryKind::Machine.into());
let env_block_place = this.allocate(this.layout_of(env_block_type)?, MiriMemoryKind::WinHeap.into());

// Store environment variables to environment block
// Final null terminator(block terminator) is pushed by `write_os_str_to_wide_str`
Expand All @@ -62,8 +63,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}

"FreeEnvironmentStringsW" => {
// let old_vars_ptr = this.read_scalar(args[0])?.not_undef()?;
// this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
let old_vars_ptr = this.read_scalar(args[0])?.not_undef()?;
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::WinHeap.into())?;
}

// File related shims
Expand Down

0 comments on commit 72aa9c2

Please sign in to comment.