Skip to content

Add shim test for DuplicateHandle #4371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/shims/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

if this.ptr_is_null(target_handle_ptr)? {
throw_unsup_format!(
"`DuplicateHandle` `lpTargetHandle` parameter is null, which is unsupported"
);
throw_machine_stop!(TerminationInfo::Abort(
"`DuplicateHandle` `lpTargetHandle` parameter must not be null, as otherwise the \
newly created handle is leaked"
.to_string()
));
}

if options != this.eval_windows("c", "DUPLICATE_SAME_ACCESS") {
Expand Down
35 changes: 30 additions & 5 deletions tests/pass-dep/shims/windows-fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

use std::io::{ErrorKind, Read, Write};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle};
use std::path::Path;
use std::{fs, ptr};
use std::{fs, mem, ptr};

#[path = "../../utils/mod.rs"]
mod utils;

use windows_sys::Wdk::Storage::FileSystem::{NtReadFile, NtWriteFile};
use windows_sys::Win32::Foundation::{
CloseHandle, ERROR_ACCESS_DENIED, ERROR_ALREADY_EXISTS, ERROR_IO_DEVICE, GENERIC_READ,
GENERIC_WRITE, GetLastError, RtlNtStatusToDosError, STATUS_ACCESS_DENIED,
STATUS_IO_DEVICE_ERROR, STATUS_SUCCESS, SetLastError,
CloseHandle, DUPLICATE_SAME_ACCESS, DuplicateHandle, ERROR_ACCESS_DENIED, ERROR_ALREADY_EXISTS,
ERROR_IO_DEVICE, FALSE, GENERIC_READ, GENERIC_WRITE, GetLastError, RtlNtStatusToDosError,
STATUS_ACCESS_DENIED, STATUS_IO_DEVICE_ERROR, STATUS_SUCCESS, SetLastError,
};
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, CREATE_ALWAYS, CREATE_NEW, CreateFileW, DeleteFileW,
Expand All @@ -24,6 +24,7 @@ use windows_sys::Win32::Storage::FileSystem::{
FILE_SHARE_WRITE, GetFileInformationByHandle, OPEN_ALWAYS, OPEN_EXISTING, SetFilePointerEx,
};
use windows_sys::Win32::System::IO::IO_STATUS_BLOCK;
use windows_sys::Win32::System::Threading::GetCurrentProcess;

fn main() {
unsafe {
Expand All @@ -36,6 +37,7 @@ fn main() {
test_ntstatus_to_dos();
test_file_read_write();
test_file_seek();
test_dup_handle();
}
}

Expand Down Expand Up @@ -273,6 +275,29 @@ unsafe fn test_file_read_write() {
assert_eq!(GetLastError(), 1234);
}

unsafe fn test_dup_handle() {
let temp = utils::tmp().join("test_dup.txt");
let first_handle = fs::File::create(&temp).unwrap().into_raw_handle();

let cur_proc = GetCurrentProcess();
let mut second_handle = mem::zeroed();
let res = DuplicateHandle(
cur_proc,
first_handle,
cur_proc,
&mut second_handle,
0,
FALSE,
DUPLICATE_SAME_ACCESS,
);
assert!(res != 0);
let mut file = fs::File::from_raw_handle(second_handle);
file.write(b"Test").unwrap();
// Duplicated permissions, so reading fails on the second file
let err = file.read(&mut [0]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::PermissionDenied, "I/O Error wrong kind: {:?}", err);
}

unsafe fn test_file_seek() {
let temp = utils::tmp().join("test_file_seek.txt");
let mut file = fs::File::options().create(true).write(true).read(true).open(&temp).unwrap();
Expand Down
Loading