@@ -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,72 @@ 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+ ) -> InterpResult < ' tcx , Scalar < Tag > > {
56+ let this = self . eval_context_mut ( ) ;
57+
58+ let name_ptr = this. read_scalar ( name_op) ?. not_undef ( ) ?;
59+ let name = this. memory ( ) . read_c_str ( name_ptr) ?;
60+ Ok ( match this. machine . env_vars . map . get ( name) {
61+ Some ( & var) => Scalar :: Ptr ( var) ,
62+ None => Scalar :: ptr_null ( & * this. tcx ) ,
63+ } )
64+ }
65+
66+ fn setenv (
67+ & mut self ,
68+ name_op : OpTy < ' tcx , Tag > ,
69+ value_op : OpTy < ' tcx , Tag > ,
70+ ) -> InterpResult < ' tcx , i32 > {
71+ let this = self . eval_context_mut ( ) ;
72+
73+ let name_ptr = this. read_scalar ( name_op) ?. not_undef ( ) ?;
74+ let value_ptr = this. read_scalar ( value_op) ?. not_undef ( ) ?;
75+ let value = this. memory ( ) . read_c_str ( value_ptr) ?;
76+ let mut new = None ;
77+ if !this. is_null ( name_ptr) ? {
78+ let name = this. memory ( ) . read_c_str ( name_ptr) ?;
79+ if !name. is_empty ( ) && !name. contains ( & b'=' ) {
80+ new = Some ( ( name. to_owned ( ) , value. to_owned ( ) ) ) ;
81+ }
82+ }
83+ if let Some ( ( name, value) ) = new {
84+ let value_copy = alloc_env_value ( & value, this. memory_mut ( ) ) ;
85+ if let Some ( var) = this. machine . env_vars . map . insert ( name. to_owned ( ) , value_copy) {
86+ this. memory_mut ( ) . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
87+ }
88+ Ok ( 0 )
89+ } else {
90+ Ok ( -1 )
91+ }
92+ }
93+
94+ fn unsetenv (
95+ & mut self ,
96+ name_op : OpTy < ' tcx , Tag > ,
97+ ) -> InterpResult < ' tcx , i32 > {
98+ let this = self . eval_context_mut ( ) ;
99+
100+ let name_ptr = this. read_scalar ( name_op) ?. not_undef ( ) ?;
101+ let mut success = None ;
102+ if !this. is_null ( name_ptr) ? {
103+ let name = this. memory ( ) . read_c_str ( name_ptr) ?. to_owned ( ) ;
104+ if !name. is_empty ( ) && !name. contains ( & b'=' ) {
105+ success = Some ( this. machine . env_vars . map . remove ( & name) ) ;
106+ }
107+ }
108+ if let Some ( old) = success {
109+ if let Some ( var) = old {
110+ this. memory_mut ( ) . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
111+ }
112+ Ok ( 0 )
113+ } else {
114+ Ok ( -1 )
115+ }
116+ }
117+ }
0 commit comments