-
Notifications
You must be signed in to change notification settings - Fork 385
Enable env communication #894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
655f9af
068c448
0df7a72
b731a6a
af623de
253af96
666cd22
67d1357
afc6713
f451fe2
46f902b
451a09a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::collections::HashMap; | ||
|
||
use rustc::ty::layout::{Size, Align}; | ||
use rustc_mir::interpret::{Pointer, Memory}; | ||
use crate::stacked_borrows::Tag; | ||
use crate::*; | ||
|
||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[derive(Default)] | ||
pub struct EnvVars { | ||
map: HashMap<Vec<u8>, Pointer<Tag>>, | ||
} | ||
|
||
impl EnvVars { | ||
pub(crate) fn init<'mir, 'tcx>( | ||
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>, | ||
communicate: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I entirely forgot that the machine has this flag. So can you remove this parameter, and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are probably asleep, I'll do this as part of #909. |
||
) { | ||
if 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); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn get(&self, name: &[u8]) -> Option<&Pointer<Tag>> { | ||
self.map.get(name) | ||
} | ||
|
||
pub(crate) fn unset(&mut self, name: &[u8]) -> Option<Pointer<Tag>> { | ||
self.map.remove(name) | ||
} | ||
|
||
pub(crate) fn set(&mut self, name: Vec<u8>, ptr: Pointer<Tag>) -> Option<Pointer<Tag>>{ | ||
self.map.insert(name, ptr) | ||
} | ||
} | ||
|
||
pub(crate) fn alloc_env_value<'mir, 'tcx>( | ||
bytes: &[u8], | ||
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>, | ||
) -> Pointer<Tag> { | ||
let tcx = {memory.tcx.tcx}; | ||
let length = bytes.len() as u64; | ||
// `+1` for the null terminator. | ||
let ptr = memory.allocate( | ||
Size::from_bytes(length + 1), | ||
Align::from_bytes(1).unwrap(), | ||
MiriMemoryKind::Env.into(), | ||
); | ||
// We just allocated these, so the write cannot fail. | ||
let alloc = memory.get_mut(ptr.alloc_id).unwrap(); | ||
alloc.write_bytes(&tcx, ptr, &bytes).unwrap(); | ||
let trailing_zero_ptr = ptr.offset( | ||
Size::from_bytes(length), | ||
&tcx, | ||
).unwrap(); | ||
alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap(); | ||
ptr | ||
} | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ignore-windows: TODO env var emulation stubbed out on Windows | ||
// compile-flags: -Zmiri-enable-communication | ||
|
||
fn main() { | ||
assert_eq!(std::env::var("MIRI_ENV_VAR_TEST"), Ok("0".to_owned())); | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.