1
- #![ allow( non_snake_case) ]
2
-
3
1
use std:: ffi:: { OsString , OsStr } ;
4
2
use std:: env;
5
3
use std:: convert:: TryFrom ;
6
- use std:: collections:: hash_map:: Values ;
7
4
8
5
use crate :: stacked_borrows:: Tag ;
9
6
use crate :: rustc_target:: abi:: LayoutOf ;
@@ -47,10 +44,6 @@ impl<'tcx> EnvVars<'tcx> {
47
44
}
48
45
ecx. update_environ ( )
49
46
}
50
-
51
- fn values ( & self ) -> InterpResult < ' tcx , Values < ' _ , OsString , Pointer < Tag > > > {
52
- Ok ( self . map . values ( ) )
53
- }
54
47
}
55
48
56
49
fn alloc_env_var_as_c_str < ' mir , ' tcx > (
@@ -115,11 +108,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
115
108
} )
116
109
}
117
110
111
+ #[ allow( non_snake_case) ]
118
112
fn GetEnvironmentVariableW (
119
113
& mut self ,
120
- name_op : OpTy < ' tcx , Tag > , // LPCWSTR lpName
121
- buf_op : OpTy < ' tcx , Tag > , // LPWSTR lpBuffer
122
- size_op : OpTy < ' tcx , Tag > , // DWORD nSize
114
+ name_op : OpTy < ' tcx , Tag > , // LPCWSTR
115
+ buf_op : OpTy < ' tcx , Tag > , // LPWSTR
116
+ size_op : OpTy < ' tcx , Tag > , // DWORD
123
117
) -> InterpResult < ' tcx , u64 > {
124
118
let this = self . eval_context_mut ( ) ;
125
119
this. assert_target_os ( "windows" , "GetEnvironmentVariableW" ) ;
@@ -134,7 +128,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
134
128
let var_ptr = Scalar :: from ( var_ptr. offset ( Size :: from_bytes ( name_offset_bytes) , this) ?) ;
135
129
136
130
let var_size = u64:: try_from ( this. read_os_str_from_wide_str ( var_ptr) ?. len ( ) ) . unwrap ( ) ;
137
- // `buf_size` represent size in characters.
131
+ // `buf_size` represents the size in characters.
138
132
let buf_size = u64:: try_from ( this. read_scalar ( size_op) ?. to_u32 ( ) ?) . unwrap ( ) ;
139
133
let return_val = if var_size. checked_add ( 1 ) . unwrap ( ) > buf_size {
140
134
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
@@ -157,31 +151,41 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157
151
} )
158
152
}
159
153
154
+ #[ allow( non_snake_case) ]
160
155
fn GetEnvironmentStringsW ( & mut self ) -> InterpResult < ' tcx , Scalar < Tag > > {
161
156
let this = self . eval_context_mut ( ) ;
162
157
this. assert_target_os ( "windows" , "GetEnvironmentStringsW" ) ;
163
158
164
159
// Info on layout of environment blocks in Windows:
165
160
// https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables
166
161
let mut env_vars = std:: ffi:: OsString :: new ( ) ;
167
- for & item in this. machine . env_vars . values ( ) ? {
162
+ for & item in this. machine . env_vars . map . values ( ) {
168
163
let env_var = this. read_os_str_from_wide_str ( Scalar :: from ( item) ) ?;
169
164
env_vars. push ( env_var) ;
170
165
env_vars. push ( "\0 " ) ;
171
166
}
172
167
// Allocate environment block & Store environment variables to environment block.
173
168
// Final null terminator(block terminator) is added by `alloc_os_str_to_wide_str`.
169
+ // FIXME: MemoryKind should be `MiMemoryKind::Machine`,
170
+ // but using it results in a Stacked Borrows error when running MIRI on 'tests/run-pass/env.rs'
171
+ // For now, use `MiriMemoryKind::WinHeap` instead.
174
172
let envblock_ptr = this. alloc_os_str_as_wide_str ( & env_vars, MiriMemoryKind :: WinHeap . into ( ) ) ;
175
-
173
+ // If the function succeeds, the return value is a pointer to the environment block of the current process.
176
174
Ok ( envblock_ptr. into ( ) )
177
175
}
178
176
179
- fn FreeEnvironmentStringsW ( & mut self , env_block_op : OpTy < ' tcx , Tag > ) -> InterpResult < ' tcx , bool > {
177
+ #[ allow( non_snake_case) ]
178
+ fn FreeEnvironmentStringsW ( & mut self , env_block_op : OpTy < ' tcx , Tag > ) -> InterpResult < ' tcx , i32 > {
180
179
let this = self . eval_context_mut ( ) ;
181
180
this. assert_target_os ( "windows" , "FreeEnvironmentStringsW" ) ;
182
181
183
182
let env_block_ptr = this. read_scalar ( env_block_op) ?. not_undef ( ) ?;
184
- Ok ( this. memory . deallocate ( this. force_ptr ( env_block_ptr) ?, None , MiriMemoryKind :: WinHeap . into ( ) ) . is_ok ( ) )
183
+ // FIXME: MemoryKind should be `MiMemoryKind::Machine`,
184
+ // but using it results in a Stacked Borrows error when running MIRI on 'tests/run-pass/env.rs'
185
+ // For now, use `MiriMemoryKind::WinHeap` instead.
186
+ let result = this. memory . deallocate ( this. force_ptr ( env_block_ptr) ?, None , MiriMemoryKind :: WinHeap . into ( ) ) ;
187
+ // If the function succeeds, the return value is nonzero.
188
+ Ok ( result. is_ok ( ) as i32 )
185
189
}
186
190
187
191
fn setenv (
@@ -220,10 +224,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
220
224
}
221
225
}
222
226
227
+ #[ allow( non_snake_case) ]
223
228
fn SetEnvironmentVariableW (
224
229
& mut self ,
225
- name_op : OpTy < ' tcx , Tag > , // LPCWSTR lpName,
226
- value_op : OpTy < ' tcx , Tag > , // LPCWSTR lpValue,
230
+ name_op : OpTy < ' tcx , Tag > , // LPCWSTR
231
+ value_op : OpTy < ' tcx , Tag > , // LPCWSTR
227
232
) -> InterpResult < ' tcx , i32 > {
228
233
let mut this = self . eval_context_mut ( ) ;
229
234
this. assert_target_os ( "windows" , "SetEnvironmentVariableW" ) ;
@@ -233,14 +238,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
233
238
234
239
if this. is_null ( name_ptr) ? {
235
240
// ERROR CODE is not clearly explained in docs.. For now, throw UB instead.
236
- throw_ub_format ! ( "Pointer to environment variable name is NULL" ) ;
241
+ throw_ub_format ! ( "pointer to environment variable name is NULL" ) ;
237
242
}
238
243
239
244
let name = this. read_os_str_from_wide_str ( name_ptr) ?;
240
245
if name. is_empty ( ) {
241
- throw_unsup_format ! ( "Environment variable name is an empty string" ) ;
246
+ throw_unsup_format ! ( "environment variable name is an empty string" ) ;
242
247
} else if name. to_string_lossy ( ) . contains ( '=' ) {
243
- throw_unsup_format ! ( "Environment variable name contains '='" ) ;
248
+ throw_unsup_format ! ( "environment variable name contains '='" ) ;
244
249
} else if this. is_null ( value_ptr) ? {
245
250
// Delete environment variable `{name}`
246
251
if let Some ( var) = this. machine . env_vars . map . remove ( & name) {
0 commit comments