1
1
use std:: collections:: HashMap ;
2
+ use std:: env;
2
3
3
4
use rustc:: ty:: layout:: { Size } ;
4
5
use rustc_mir:: interpret:: { Pointer , Memory } ;
@@ -21,7 +22,7 @@ impl EnvVars {
21
22
excluded_env_vars. push ( "TERM" . to_owned ( ) ) ;
22
23
23
24
if ecx. machine . communicate {
24
- for ( name, value) in std :: env:: vars ( ) {
25
+ for ( name, value) in env:: vars ( ) {
25
26
if !excluded_env_vars. contains ( & name) {
26
27
let var_ptr = alloc_env_var ( name. as_bytes ( ) , value. as_bytes ( ) , ecx. memory_mut ( ) ) ;
27
28
ecx. machine . env_vars . map . insert ( name. into_bytes ( ) , var_ptr) ;
@@ -111,4 +112,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
111
112
Ok ( -1 )
112
113
}
113
114
}
115
+
116
+ fn getcwd (
117
+ & mut self ,
118
+ buf_op : OpTy < ' tcx , Tag > ,
119
+ size_op : OpTy < ' tcx , Tag > ,
120
+ ) -> InterpResult < ' tcx , Scalar < Tag > > {
121
+ let this = self . eval_context_mut ( ) ;
122
+ let tcx = & { this. tcx . tcx } ;
123
+
124
+ let buf = this. force_ptr ( this. read_scalar ( buf_op) ?. not_undef ( ) ?) ?;
125
+ let size = this. read_scalar ( size_op) ?. to_usize ( & * this. tcx ) ?;
126
+ // If we cannot get the current directory, we return null
127
+ if let Ok ( cwd) = env:: current_dir ( ) {
128
+ // It is not clear what happens with non-utf8 paths here
129
+ let mut bytes = cwd. display ( ) . to_string ( ) . into_bytes ( ) ;
130
+ // If the buffer is smaller than the path, we return null
131
+ if bytes. len ( ) as u64 <= size {
132
+ // We need `size` bytes exactly
133
+ bytes. resize ( size as usize , 0 ) ;
134
+ this. memory_mut ( ) . get_mut ( buf. alloc_id ) ?. write_bytes ( tcx, buf, & bytes) ?;
135
+ return Ok ( Scalar :: Ptr ( buf) )
136
+ }
137
+ }
138
+ Ok ( Scalar :: ptr_null ( & * this. tcx ) )
139
+ }
114
140
}
0 commit comments