Skip to content

Commit

Permalink
Auto merge of #925 - christianpoveda:environ-shim, r=RalfJung
Browse files Browse the repository at this point in the history
Write name and value for each env var

In order to res0lve #756 is necessary to have the whole `"NAME=VALUE"` sequence of bytes written into memory instead of just the value.

This change does not affect the interface of the `shim::envs::EnvVars` type in any way.

r? @RalfJung @oli-obk
  • Loading branch information
bors committed Aug 27, 2019
2 parents 69268fb + 7d93cc7 commit 283928a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::*;

#[derive(Default)]
pub struct EnvVars {
/// Stores pointers to the environment variables. These variables must be stored as
/// null-terminated C strings with the `"{name}={value}"` format.
map: HashMap<Vec<u8>, Pointer<Tag>>,
}

Expand All @@ -16,17 +18,19 @@ impl EnvVars {
) {
if ecx.machine.communicate {
for (name, value) in std::env::vars() {
let value = alloc_env_value(value.as_bytes(), ecx.memory_mut());
ecx.machine.env_vars.map.insert(name.into_bytes(), value);
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
}
}
}
}

fn alloc_env_value<'mir, 'tcx>(
bytes: &[u8],
fn alloc_env_var<'mir, 'tcx>(
name: &[u8],
value: &[u8],
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
) -> Pointer<Tag> {
let bytes = [name, b"=", value].concat();
let tcx = {memory.tcx.tcx};
let length = bytes.len() as u64;
// `+1` for the null terminator.
Expand Down Expand Up @@ -57,7 +61,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let name = this.memory().read_c_str(name_ptr)?;
Ok(match this.machine.env_vars.map.get(name) {
Some(&var) => Scalar::Ptr(var),
// The offset is used to strip the "{name}=" part of the string.
Some(var_ptr) => Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?),
None => Scalar::ptr_null(&*this.tcx),
})
}
Expand All @@ -80,8 +85,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}
if let Some((name, value)) = new {
let value_copy = alloc_env_value(&value, this.memory_mut());
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
let var_ptr = alloc_env_var(&name, &value, this.memory_mut());
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
}
Ok(0)
Expand Down

0 comments on commit 283928a

Please sign in to comment.