@@ -174,7 +174,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
174
174
let this = self . eval_context_ref ( ) ;
175
175
let os_str = this. read_os_str_from_c_str ( ptr) ?;
176
176
177
- Ok ( match this. convert_path_separator ( Cow :: Borrowed ( os_str) , PathConversion :: TargetToHost ) {
177
+ Ok ( match this. convert_path ( Cow :: Borrowed ( os_str) , PathConversion :: TargetToHost ) {
178
178
Cow :: Borrowed ( x) => Cow :: Borrowed ( Path :: new ( x) ) ,
179
179
Cow :: Owned ( y) => Cow :: Owned ( PathBuf :: from ( y) ) ,
180
180
} )
@@ -188,10 +188,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
188
188
let this = self . eval_context_ref ( ) ;
189
189
let os_str = this. read_os_str_from_wide_str ( ptr) ?;
190
190
191
- Ok ( this
192
- . convert_path_separator ( Cow :: Owned ( os_str) , PathConversion :: TargetToHost )
193
- . into_owned ( )
194
- . into ( ) )
191
+ Ok ( this. convert_path ( Cow :: Owned ( os_str) , PathConversion :: TargetToHost ) . into_owned ( ) . into ( ) )
195
192
}
196
193
197
194
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -203,8 +200,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
203
200
size : u64 ,
204
201
) -> InterpResult < ' tcx , ( bool , u64 ) > {
205
202
let this = self . eval_context_mut ( ) ;
206
- let os_str = this
207
- . convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
203
+ let os_str =
204
+ this . convert_path ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
208
205
this. write_os_str_to_c_str ( & os_str, ptr, size)
209
206
}
210
207
@@ -217,8 +214,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
217
214
size : u64 ,
218
215
) -> InterpResult < ' tcx , ( bool , u64 ) > {
219
216
let this = self . eval_context_mut ( ) ;
220
- let os_str = this
221
- . convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
217
+ let os_str =
218
+ this . convert_path ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
222
219
this. write_os_str_to_wide_str ( & os_str, ptr, size)
223
220
}
224
221
@@ -230,18 +227,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
230
227
memkind : MemoryKind < MiriMemoryKind > ,
231
228
) -> InterpResult < ' tcx , Pointer < Option < Provenance > > > {
232
229
let this = self . eval_context_mut ( ) ;
233
- let os_str = this
234
- . convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
230
+ let os_str =
231
+ this . convert_path ( Cow :: Borrowed ( path. as_os_str ( ) ) , PathConversion :: HostToTarget ) ;
235
232
this. alloc_os_str_as_c_str ( & os_str, memkind)
236
233
}
237
234
238
- fn convert_path_separator < ' a > (
235
+ fn convert_path < ' a > (
239
236
& self ,
240
237
os_str : Cow < ' a , OsStr > ,
241
238
direction : PathConversion ,
242
239
) -> Cow < ' a , OsStr > {
243
240
let this = self . eval_context_ref ( ) ;
244
241
let target_os = & this. tcx . sess . target . os ;
242
+
245
243
#[ cfg( windows) ]
246
244
return if target_os == "windows" {
247
245
// Windows-on-Windows, all fine.
@@ -252,10 +250,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
252
250
PathConversion :: HostToTarget => ( '\\' , '/' ) ,
253
251
PathConversion :: TargetToHost => ( '/' , '\\' ) ,
254
252
} ;
255
- let converted = os_str
253
+ let mut converted = os_str
256
254
. encode_wide ( )
257
255
. map ( |wchar| if wchar == from as u16 { to as u16 } else { wchar } )
258
256
. collect :: < Vec < _ > > ( ) ;
257
+ // We also have to adjust the prefix to keep absolute paths absolute.
258
+ match direction {
259
+ PathConversion :: HostToTarget => {
260
+ // If this is an absolute Windows path that does not start with a separator
261
+ // (`C:/...` after separator conversion), we need to make it start with a
262
+ // separator so that Unix targets consider it absolute.
263
+ if converted. get ( 1 ) == Some ( ':' as u16 ) && converted. get ( 2 ) == Some ( '/' as u16 )
264
+ {
265
+ // We add a `/$miri-absolute-path` at the beginning, to store the absolute
266
+ // Windows path in something that looks like an absolute Unix path.
267
+ converted. splice ( 0 ..0 , b"/$miri-absolute-path" . map ( |c| c as u16 ) ) ;
268
+ }
269
+ }
270
+ PathConversion :: TargetToHost => {
271
+ // We have to un-do the `\$miri-absolute-path` prefix, if it is present.
272
+ let prefix = b"\\ $miri-absolute-path" ;
273
+ if converted. len ( ) >= prefix. len ( )
274
+ && converted. iter . zip ( prefix. iter ( ) ) . all ( |( & c, & p) | c == p as u16 )
275
+ {
276
+ converted. splice ( 0 ..prefix. len ( ) , iter:: empty ( ) ) ;
277
+ }
278
+ }
279
+ }
259
280
Cow :: Owned ( OsString :: from_wide ( & converted) )
260
281
} ;
261
282
#[ cfg( unix) ]
@@ -270,6 +291,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
270
291
. iter ( )
271
292
. map ( |& wchar| if wchar == from as u8 { to as u8 } else { wchar } )
272
293
. collect :: < Vec < _ > > ( ) ;
294
+ // TODO: Once we actually support file system things on Windows targets, we'll probably
295
+ // have to also do something clever for absolute path preservation here, like above.
273
296
Cow :: Owned ( OsString :: from_vec ( converted) )
274
297
} else {
275
298
// Unix-on-Unix, all is fine.
0 commit comments