Skip to content

Commit 2bf8c3a

Browse files
committed
Auto merge of #938 - christianpoveda:clean-env-alloc, r=RalfJung
Rewrite alloc_env_var Just to simplify the logic behind `shims::env`
2 parents 9fbe719 + 4afa3bc commit 2bf8c3a

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

src/shims/env.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use rustc::ty::layout::{Size, Align};
3+
use rustc::ty::layout::{Size};
44
use rustc_mir::interpret::{Pointer, Memory};
55
use crate::stacked_borrows::Tag;
66
use crate::*;
@@ -36,24 +36,11 @@ fn alloc_env_var<'mir, 'tcx>(
3636
value: &[u8],
3737
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
3838
) -> Pointer<Tag> {
39-
let bytes = [name, b"=", value].concat();
40-
let tcx = {memory.tcx.tcx};
41-
let length = bytes.len() as u64;
42-
// `+1` for the null terminator.
43-
let ptr = memory.allocate(
44-
Size::from_bytes(length + 1),
45-
Align::from_bytes(1).unwrap(),
46-
MiriMemoryKind::Env.into(),
47-
);
48-
// We just allocated these, so the write cannot fail.
49-
let alloc = memory.get_mut(ptr.alloc_id).unwrap();
50-
alloc.write_bytes(&tcx, ptr, &bytes).unwrap();
51-
let trailing_zero_ptr = ptr.offset(
52-
Size::from_bytes(length),
53-
&tcx,
54-
).unwrap();
55-
alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
56-
ptr
39+
let mut bytes = name.to_vec();
40+
bytes.push(b'=');
41+
bytes.extend_from_slice(value);
42+
bytes.push(0);
43+
memory.allocate_static_bytes(bytes.as_slice(), MiriMemoryKind::Env.into())
5744
}
5845

5946
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}

0 commit comments

Comments
 (0)