Skip to content

Commit 41f8cfa

Browse files
committed
Move env shims to its own module
1 parent 1f504ea commit 41f8cfa

File tree

4 files changed

+83
-72
lines changed

4 files changed

+83
-72
lines changed

src/eval.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
3939
Evaluator::new(config.communicate),
4040
MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate),
4141
);
42-
4342
// Complete initialization.
4443
EnvVars::init(&mut ecx, config.communicate);
4544

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextEx
3333
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
3434
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
3535
pub use crate::shims::dlsym::{Dlsym, EvalContextExt as DlsymEvalContextExt};
36-
pub use crate::shims::env::EnvVars;
36+
pub use crate::shims::env::{EnvVars, EvalContextExt as EnvEvalContextExt};
3737
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
3838
pub use crate::range_map::RangeMap;
3939
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};

src/shims/env.rs

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,9 @@ impl EnvVars {
2222
}
2323
}
2424
}
25-
26-
pub(crate) fn get(&self, name: &[u8]) -> Option<&Pointer<Tag>> {
27-
self.map.get(name)
28-
}
29-
30-
pub(crate) fn unset(&mut self, name: &[u8]) -> Option<Pointer<Tag>> {
31-
self.map.remove(name)
32-
}
33-
34-
pub(crate) fn set(&mut self, name: Vec<u8>, ptr: Pointer<Tag>) -> Option<Pointer<Tag>>{
35-
self.map.insert(name, ptr)
36-
}
3725
}
3826

39-
pub(crate) fn alloc_env_value<'mir, 'tcx>(
27+
fn alloc_env_value<'mir, 'tcx>(
4028
bytes: &[u8],
4129
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
4230
) -> Pointer<Tag> {
@@ -58,3 +46,81 @@ pub(crate) fn alloc_env_value<'mir, 'tcx>(
5846
alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
5947
ptr
6048
}
49+
50+
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
51+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
52+
fn getenv(
53+
&mut self,
54+
name_op: OpTy<'tcx, Tag>,
55+
dest: PlaceTy<'tcx, Tag>
56+
) -> InterpResult<'tcx> {
57+
let this = self.eval_context_mut();
58+
59+
let result = {
60+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
61+
let name = this.memory().read_c_str(name_ptr)?;
62+
match this.machine.env_vars.map.get(name) {
63+
Some(&var) => Scalar::Ptr(var),
64+
None => Scalar::ptr_null(&*this.tcx),
65+
}
66+
};
67+
this.write_scalar(result, dest)?;
68+
Ok(())
69+
}
70+
71+
fn setenv(
72+
&mut self,
73+
name_op: OpTy<'tcx, Tag>,
74+
value_op: OpTy<'tcx, Tag>,
75+
dest: PlaceTy<'tcx, Tag>
76+
) -> InterpResult<'tcx> {
77+
let this = self.eval_context_mut();
78+
79+
let mut new = None;
80+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
81+
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
82+
let value = this.memory().read_c_str(value_ptr)?;
83+
if !this.is_null(name_ptr)? {
84+
let name = this.memory().read_c_str(name_ptr)?;
85+
if !name.is_empty() && !name.contains(&b'=') {
86+
new = Some((name.to_owned(), value.to_owned()));
87+
}
88+
}
89+
if let Some((name, value)) = new {
90+
let value_copy = alloc_env_value(&value, this.memory_mut());
91+
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
92+
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
93+
}
94+
this.write_null(dest)?;
95+
} else {
96+
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
97+
}
98+
Ok(())
99+
}
100+
101+
fn unsetenv(
102+
&mut self,
103+
name_op: OpTy<'tcx, Tag>,
104+
dest: PlaceTy<'tcx, Tag>
105+
) -> InterpResult<'tcx> {
106+
let this = self.eval_context_mut();
107+
108+
let mut success = None;
109+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
110+
if !this.is_null(name_ptr)? {
111+
let name = this.memory().read_c_str(name_ptr)?.to_owned();
112+
if !name.is_empty() && !name.contains(&b'=') {
113+
success = Some(this.machine.env_vars.map.remove(&name));
114+
}
115+
}
116+
if let Some(old) = success {
117+
if let Some(var) = old {
118+
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
119+
}
120+
this.write_null(dest)?;
121+
} else {
122+
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
123+
}
124+
Ok(())
125+
}
126+
}

src/shims/foreign_items.rs

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use syntax::attr;
88
use syntax::symbol::sym;
99

1010
use crate::*;
11-
use crate::shims::env::alloc_env_value;
1211

1312
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1413
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -422,62 +421,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
422421
}
423422
}
424423

425-
"getenv" => {
426-
let result = {
427-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
428-
let name = this.memory().read_c_str(name_ptr)?;
429-
match this.machine.env_vars.get(name) {
430-
Some(&var) => Scalar::Ptr(var),
431-
None => Scalar::ptr_null(&*this.tcx),
432-
}
433-
};
434-
this.write_scalar(result, dest)?;
435-
}
436-
437-
"unsetenv" => {
438-
let mut success = None;
439-
{
440-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
441-
if !this.is_null(name_ptr)? {
442-
let name = this.memory().read_c_str(name_ptr)?.to_owned();
443-
if !name.is_empty() && !name.contains(&b'=') {
444-
success = Some(this.machine.env_vars.unset(&name));
445-
}
446-
}
447-
}
448-
if let Some(old) = success {
449-
if let Some(var) = old {
450-
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
451-
}
452-
this.write_null(dest)?;
453-
} else {
454-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
455-
}
456-
}
457-
458-
"setenv" => {
459-
let mut new = None;
460-
{
461-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
462-
let value_ptr = this.read_scalar(args[1])?.not_undef()?;
463-
let value = this.memory().read_c_str(value_ptr)?;
464-
if !this.is_null(name_ptr)? {
465-
let name = this.memory().read_c_str(name_ptr)?;
466-
if !name.is_empty() && !name.contains(&b'=') {
467-
new = Some((name.to_owned(), value.to_owned()));
468-
}
469-
}
470-
}
471-
if let Some((name, value)) = new {
472-
let value_copy = alloc_env_value(&value, this.memory_mut());
473-
if let Some(var) = this.machine.env_vars.set(name.to_owned(), value_copy) {
474-
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
475-
}
476-
this.write_null(dest)?;
477-
} else {
478-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
479-
}
480-
}
424+
"getenv" => this.getenv(args[0], dest)?,
425+
"unsetenv" => this.unsetenv(args[0], dest)?,
426+
"setenv" => this.setenv(args[0], args[1], dest)?,
481427

482428
"write" => {
483429
let fd = this.read_scalar(args[0])?.to_i32()?;

0 commit comments

Comments
 (0)