Skip to content

Commit 1b32764

Browse files
committed
windows: basic support for GetUserProfileDirectoryW
1 parent ed5e0e6 commit 1b32764

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ aes = { version = "0.8.3", features = ["hazmat"] }
2525
measureme = "11"
2626
ctrlc = "3.2.5"
2727
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
28+
directories = "5"
2829

2930
# Copied from `compiler/rustc/Cargo.toml`.
3031
# But only for some targets, it fails for others. Rustc configures this in its CI, but we can't

src/shims/env.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,65 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
494494
fn GetCurrentProcessId(&mut self) -> InterpResult<'tcx, u32> {
495495
let this = self.eval_context_mut();
496496
this.assert_target_os("windows", "GetCurrentProcessId");
497-
498497
this.check_no_isolation("`GetCurrentProcessId`")?;
499498

500499
Ok(std::process::id())
501500
}
501+
502+
#[allow(non_snake_case)]
503+
fn GetUserProfileDirectoryW(
504+
&mut self,
505+
token: &OpTy<'tcx, Provenance>, // HANDLE
506+
buf: &OpTy<'tcx, Provenance>, // LPWSTR
507+
size: &OpTy<'tcx, Provenance>, // LPDWORD
508+
) -> InterpResult<'tcx, Scalar<Provenance>> // returns BOOL
509+
{
510+
let this = self.eval_context_mut();
511+
this.assert_target_os("windows", "GetUserProfileDirectoryW");
512+
this.check_no_isolation("`GetUserProfileDirectoryW`")?;
513+
514+
let token = this.read_target_isize(token)?;
515+
let buf = this.read_pointer(buf)?;
516+
let size = this.deref_pointer(size)?;
517+
518+
if token != -4 {
519+
throw_unsup_format!(
520+
"GetUserProfileDirectoryW: only CURRENT_PROCESS_TOKEN is supported"
521+
);
522+
}
523+
524+
// See <https://learn.microsoft.com/en-us/windows/win32/api/userenv/nf-userenv-getuserprofiledirectoryw> for docs.
525+
Ok(match directories::UserDirs::new() {
526+
Some(dirs) => {
527+
let home = dirs.home_dir();
528+
let size_avail = if this.ptr_is_null(size.ptr())? {
529+
0 // if the buf pointer is null, we can't write to it; `size` will be updated to the required length
530+
} else {
531+
this.read_scalar(&size)?.to_u32()?
532+
};
533+
// Of course we cannot use `windows_check_buffer_size` here since this uses
534+
// a different method for dealing with a too-small buffer than the other functions...
535+
let (success, len) = this.write_path_to_wide_str(
536+
home,
537+
buf,
538+
size_avail.into(),
539+
/*truncate*/ false,
540+
)?;
541+
// The Windows docs just say that this is written on failure. But std
542+
// seems to rely on it always being written.
543+
this.write_scalar(Scalar::from_u32(len.try_into().unwrap()), &size)?;
544+
if success {
545+
Scalar::from_i32(1) // return TRUE
546+
} else {
547+
this.set_last_error(this.eval_windows("c", "ERROR_INSUFFICIENT_BUFFER"))?;
548+
Scalar::from_i32(0) // return FALSE
549+
}
550+
}
551+
None => {
552+
// We have to pick some error code.
553+
this.set_last_error(this.eval_windows("c", "ERROR_BAD_USER_PROFILE"))?;
554+
Scalar::from_i32(0) // return FALSE
555+
}
556+
})
557+
}
502558
}

src/shims/windows/foreign_items.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
135135
let result = this.SetCurrentDirectoryW(path)?;
136136
this.write_scalar(result, dest)?;
137137
}
138+
"GetUserProfileDirectoryW" => {
139+
let [token, buf, size] =
140+
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
141+
let result = this.GetUserProfileDirectoryW(token, buf, size)?;
142+
this.write_scalar(result, dest)?;
143+
}
138144

139145
// File related shims
140146
"NtWriteFile" => {

tests/pass/shims/env/home.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
//@ignore-target-windows: home_dir is not supported on Windows
21
//@compile-flags: -Zmiri-disable-isolation
32
use std::env;
43

54
fn main() {
65
env::remove_var("HOME"); // make sure we enter the interesting codepath
6+
env::remove_var("USERPROFILE"); // Windows also looks as this env var
77
#[allow(deprecated)]
88
env::home_dir().unwrap();
99
}

0 commit comments

Comments
 (0)