Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Adjust lints in `filesystem::primitives` (#14371)

Alex Crichtonb1daf553b8b4
diff --git a/crates/wasi/src/filesystem/primitives/manually/open.rs b/crates/wasi/src/filesystem/primitives/manually/open.rs
index 06bef1aba9..a145ae19e9 100644
--- a/crates/wasi/src/filesystem/primitives/manually/open.rs
+++ b/crates/wasi/src/filesystem/primitives/manually/open.rs
@@ -211,7 +211,6 @@ impl<'start> Context<'start> {
let dir_required = self.dir_required || use_options.dir_required;
- #[allow(clippy::redundant_clone)]
match open_unchecked(
&self.base,
one.as_ref(),
diff --git a/crates/wasi/src/filesystem/primitives/maybe_owned_file.rs b/crates/wasi/src/filesystem/primitives/maybe_owned_file.rs
index aba86dd0c9..03a8344dc0 100644
--- a/crates/wasi/src/filesystem/primitives/maybe_owned_file.rs
+++ b/crates/wasi/src/filesystem/primitives/maybe_owned_file.rs
@@ -42,7 +42,6 @@ impl<'borrow> MaybeOwnedFile<'borrow> {
/// Produce an owned `File`. This uses `open` on "." if needed to convert a
/// borrowed `File` to an owned one.
- #[cfg_attr(windows, allow(dead_code))]
pub(super) fn into_file(self, options: &OpenOptions) -> io::Result<fs::File> {
match self {
Self::Owned(file) => Ok(file),
diff --git a/crates/wasi/src/filesystem/primitives/mod.rs b/crates/wasi/src/filesystem/primitives/mod.rs
index e090026693..6076f9b69a 100644
--- a/crates/wasi/src/filesystem/primitives/mod.rs
+++ b/crates/wasi/src/filesystem/primitives/mod.rs
@@ -1,21 +1,10 @@
//! Filesystem utilities.
-#![allow(
- trivial_numeric_casts,
- reason = "preexisting from when cap-primitives was imported"
-)]
-#![allow(
- unsafe_op_in_unsafe_fn,
- reason = "preexisting from when cap-primitives was imported"
-)]
#![allow(
clippy::unnecessary_fallible_conversions,
- reason = "platform-agnostic code can't always take advantage of this"
-)]
-#![allow(
- clippy::allow_attributes_without_reason,
- reason = "preexisting from when cap-primitives was imported"
+ reason = "typedefs are platform-specific"
)]
+#![allow(trivial_numeric_casts, reason = "typedefs are platform-specific")]
#![allow(clippy::useless_conversion, reason = "typedefs are platform-specific")]
use std::path::{Component, Path, PathBuf};
@@ -270,13 +259,6 @@ fn open_dir(start: &fs::File, path: &Path) -> io::Result<fs::File> {
open(start, path, &dir_options())
}
-/// Open a directory by performing an unsandboxed `openat`-like operation.
-#[inline]
-#[allow(dead_code)]
-fn open_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<fs::File> {
- open_unchecked(start, path, &dir_options()).map_err(Into::into)
-}
-
pub(crate) fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {
#[cfg(target_os = "freebsd")]
if sys::remove_file_fast(start, path)? {
diff --git a/crates/wasi/src/filesystem/primitives/open_options.rs b/crates/wasi/src/filesystem/primitives/open_options.rs
index 13a7227224..579de4aa94 100644
--- a/crates/wasi/src/filesystem/primitives/open_options.rs
+++ b/crates/wasi/src/filesystem/primitives/open_options.rs
@@ -53,7 +53,6 @@ impl OpenOptions {
/// Creates a blank new set of options ready for configuration.
///
/// This corresponds to [`std::fs::OpenOptions::new`].
- #[allow(clippy::new_without_default)]
#[inline]
pub const fn new() -> Self {
Self {
diff --git a/crates/wasi/src/filesystem/primitives/open_unchecked_error.rs b/crates/wasi/src/filesystem/primitives/open_unchecked_error.rs
index 09ad9a6fca..f5f5b58f06 100644
--- a/crates/wasi/src/filesystem/primitives/open_unchecked_error.rs
+++ b/crates/wasi/src/filesystem/primitives/open_unchecked_error.rs
@@ -17,15 +17,6 @@ pub(crate) enum SymlinkKind {
Dir,
}
-impl OpenUncheckedError {
- #[allow(dead_code)]
- pub(crate) fn kind(&self) -> io::ErrorKind {
- match self {
- Self::Other(err) | Self::Symlink(err, _) | Self::NotFound(err) => err.kind(),
- }
- }
-}
-
impl From<OpenUncheckedError> for io::Error {
fn from(error: OpenUncheckedError) -> Self {
match error {
diff --git a/crates/wasi/src/filesystem/primitives/tests/cap_basics.rs b/crates/wasi/src/filesystem/primitives/tests/cap_basics.rs
index 3d9388f7f1..4bd78bf95e 100644
--- a/crates/wasi/src/filesystem/primitives/tests/cap_basics.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/cap_basics.rs
@@ -1,6 +1,5 @@
use super::helpers as h;
use super::sys_common::io::tmpdir;
-#[allow(unused_imports)]
use super::sys_common::symlink_supported;
use crate::filesystem::primitives as p;
@@ -124,8 +123,7 @@ fn cap_smoke_test() {
#[test]
fn symlinks() {
- #[cfg(windows)]
- if !symlink_supported() {
+ if cfg!(windows) && !symlink_supported() {
return;
}
@@ -204,8 +202,7 @@ fn symlink_loop() {
#[test]
fn symlink_loop_from_rename() {
- #[cfg(windows)]
- if !symlink_supported() {
+ if cfg!(windows) && !symlink_supported() {
return;
}
diff --git a/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs b/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs
index 5332c3cac8..1a1f79acb8 100644
--- a/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs
@@ -3,7 +3,6 @@
// run-pass
-#![allow(deprecated)]
// ignore-cloudabi no files or I/O
// ignore-wasm32-bare no files or I/O
// ignore-emscripten no files
diff --git a/crates/wasi/src/filesystem/primitives/tests/rename_directory.rs b/crates/wasi/src/filesystem/primitives/tests/rename_directory.rs
index a9e208994a..46b08d01fc 100644
--- a/crates/wasi/src/filesystem/primitives/tests/rename_directory.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/rename_directory.rs
@@ -4,7 +4,6 @@
// run-pass
-#![allow(unused_must_use)]
// This test can't be a unit test in std,
// because it needs `TempDir`, which is in extra
@@ -27,7 +26,7 @@ fn rename_directory() {
let new_path = Path::new("quux/blat");
h::create_dir_all(&dir, &new_path).unwrap();
- rename(&dir, &old_path, &dir, &new_path.join("newdir"));
+ rename(&dir, &old_path, &dir, &new_path.join("newdir")).unwrap();
assert!(h::is_dir(&dir, &new_path.join("newdir")));
assert!(h::exists(&dir, &new_path.join("newdir/temp.txt")));
}
diff --git a/crates/wasi/src/filesystem/primitives/tests/sys_common/io.rs b/crates/wasi/src/filesystem/primitives/tests/sys_common/io.rs
index e0dc74003a..e10d25addb 100644
--- a/crates/wasi/src/filesystem/primitives/tests/sys_common/io.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/sys_common/io.rs
@@ -1,6 +1,5 @@
pub use tempfile::TempDir;
-#[allow(unused)]
pub fn tmpdir() -> TempDir {
tempfile::tempdir().expect("expected to be able to create a temporary directory")
}
diff --git a/crates/wasi/src/filesystem/primitives/tests/sys_common/mod.rs b/crates/wasi/src/filesystem/primitives/tests/sys_common/mod.rs
index d13e417d64..0f7a569464 100644
--- a/crates/wasi/src/filesystem/primitives/tests/sys_common/mod.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/sys_common/mod.rs
@@ -1,12 +1,9 @@
-#![allow(unused_imports)]
-
mod symlink_junction;
pub mod io;
pub use symlink_junction::*;
-#[allow(unused)]
macro_rules! check {
($e:expr) => {
match $e {
@@ -17,7 +14,6 @@ macro_rules! check {
}
#[cfg(windows)]
-#[allow(unused)]
macro_rules! error {
($e:expr, $s:expr) => {
match $e {
@@ -35,14 +31,12 @@ macro_rules! error {
}
#[cfg(any(unix, target_os = "wasi"))]
-#[allow(unused)]
macro_rules! error {
($e:expr, $s:expr) => {
error_contains!($e, $s)
};
}
-#[allow(unused)]
macro_rules! error_contains {
($e:expr, $s:expr) => {
match $e {
@@ -64,7 +58,6 @@ macro_rules! error_contains {
// a78a62fc996ba16f7a111c99520b23f77029f4eb.
#[cfg(windows)]
-#[allow(dead_code)]
pub fn symlink_supported() -> bool {
let dir = tempfile::tempdir().unwrap();
@@ -78,9 +71,8 @@ pub fn symlink_supported() -> bool {
}
Err(e) => {
eprintln!(
- "symlinks not supported: {:?}\n\
+ "symlinks not supported: {e:?}\n\
Windows 10 users should enable developer mode.",
- e
);
false
}
@@ -90,7 +82,6 @@ pub fn symlink_supported() -> bool {
}
#[cfg(not(windows))]
-#[allow(dead_code)]
pub fn symlink_supported() -> bool {
true
}
diff --git a/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs b/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs
index 7dad47dc29..6761783e32 100644
--- a/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs
@@ -2,11 +2,12 @@
// library/std/src/sys/windows/fs.rs at revision
// 3ffb27ff89db780e88abe829783565a7122be1c5.
+#![cfg_attr(windows, expect(non_snake_case, reason = "matching C conventions"))]
+
use std::path::Path;
use std::{fs, io};
#[cfg(not(windows))]
-#[allow(dead_code)]
pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(
src: P,
dst_dir: &fs::File,
@@ -16,7 +17,6 @@ pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(
}
#[cfg(windows)]
-#[allow(dead_code)]
pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(
src: P,
dst_dir: &fs::File,
@@ -35,8 +35,6 @@ pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(
struct Align8<T: ?Sized>(pub T);
#[cfg(windows)]
-#[allow(dead_code)]
-#[allow(non_snake_case)]
#[repr(C)]
pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
pub ReparseTag: u32,
@@ -49,7 +47,6 @@ pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
}
#[cfg(windows)]
-#[allow(dead_code)]
pub fn cvt(i: windows_sys::core::BOOL) -> io::Result<windows_sys::core::BOOL> {
if i == 0 {
Err(io::Error::last_os_error())
@@ -64,7 +61,6 @@ pub fn cvt(i: windows_sys::core::BOOL) -> io::Result<windows_sys::core::BOOL> {
//
// http://www.flexhex.com/docs/articles/hard-links.phtml
#[cfg(windows)]
-#[allow(dead_code)]
fn symlink_junction_inner(original: &Path, dir: &fs::File, junction: &Path) -> io::Result<()> {
use crate::filesystem::primitives::{OpenOptions, OpenOptionsExt, create_dir, open};
use std::mem::MaybeUninit;
diff --git a/crates/wasi/src/filesystem/primitives/tests/windows_open.rs b/crates/wasi/src/filesystem/primitives/tests/windows_open.rs
index d449e4a838..d12339d498 100644
--- a/crates/wasi/src/filesystem/primitives/tests/windows_open.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/windows_open.rs
@@ -181,12 +181,12 @@ fn windows_open_special() {
".ext.more. ",
".ext.more .",
] {
- let name = format!("{}{}", device, suffix);
- eprintln!("testing '{}'", name);
+ let name = format!("{device}{suffix}");
+ eprintln!("testing '{name}'");
match h::open(&start, &name).unwrap_err().kind() {
std::io::ErrorKind::NotFound | std::io::ErrorKind::PermissionDenied => {}
- kind => panic!("unexpected error: {:?}", kind),
+ kind => panic!("unexpected error: {kind:?}"),
}
let mut options = p::OpenOptions::new();
@@ -196,12 +196,12 @@ fn windows_open_special() {
.kind()
{
std::io::ErrorKind::NotFound | std::io::ErrorKind::PermissionDenied => {}
- kind => panic!("unexpected error: {:?}", kind),
+ kind => panic!("unexpected error: {kind:?}"),
}
match h::create(&start, &name).unwrap_err().kind() {
std::io::ErrorKind::NotFound | std::io::ErrorKind::PermissionDenied => {}
- kind => panic!("unexpected error: {:?}", kind),
+ kind => panic!("unexpected error: {kind:?}"),
}
}
}
diff --git a/crates/wasi/src/filesystem/primitives/unix/dir_utils.rs b/crates/wasi/src/filesystem/primitives/unix/dir_utils.rs
index 469a33a0ec..2e144b32a2 100644
--- a/crates/wasi/src/filesystem/primitives/unix/dir_utils.rs
+++ b/crates/wasi/src/filesystem/primitives/unix/dir_utils.rs
@@ -65,7 +65,6 @@ pub(crate) fn append_dir_suffix(path: PathBuf) -> PathBuf {
/// Strip trailing `/`s, unless this reduces `path` to `/` itself. This is
/// used by `create_dir` and others to prevent paths like `foo/` from
/// canonicalizing to `foo/.` since these syscalls treat these differently.
-#[allow(clippy::indexing_slicing)]
pub(crate) fn strip_dir_suffix(path: &Path) -> impl Deref<Target = Path> + '_ {
let mut bytes = path.as_os_str().as_bytes();
while bytes.len() > 1 && *bytes.last().unwrap() == b'/' {
diff --git a/crates/wasi/src/filesystem/primitives/unix/is_same_file.rs b/crates/wasi/src/filesystem/primitives/unix/is_same_file.rs
deleted file mode 100644
index 42d7ad07da..0000000000
--- a/crates/wasi/src/filesystem/primitives/unix/is_same_file.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-use crate::filesystem::primitives::Metadata;
-use std::{fs, io};
-
-/// Determine if `a` and `b` refer to the same inode on the same device.
-pub(crate) fn is_same_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
- let a_metadata = Metadata::from_file(a)?;
- let b_metadata = Metadata::from_file(b)?;
- is_same_file_metadata(&a_metadata, &b_metadata)
-}
-
-/// Determine if `a` and `b` are metadata for the same inode on the same
-/// device.
-pub(crate) fn is_same_file_metadata(a: &Metadata, b: &Metadata) -> io::Result<bool> {
- Ok(a.dev() == b.dev() && a.ino() == b.ino())
-}
-
-/// Determine if `a` and `b` definitely refer to different inodes.
-///
-/// This is similar to `is_same_file`, but is conservative, and doesn't depend
-/// on nightly-only features.
-#[allow(dead_code)]
-pub(crate) fn is_different_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
- is_same_file(a, b).map(|same| !same)
-}
-
-/// Determine if `a` and `b` are metadata for definitely different inodes.
-///
-/// This is similar to `is_same_file_metadata`, but is conservative, and
-/// doesn't depend on nightly-only features.
-#[allow(dead_code)]
-pub(crate) fn is_different_file_metadata(a: &Metadata, b: &Metadata) -> io::Result<bool> {
- is_same_file_metadata(a, b).map(|same| !same)
-}
diff --git a/crates/wasi/src/filesystem/primitives/unix/mod.rs b/crates/wasi/src/filesystem/primitives/unix/mod.rs
index b9ff935f3a..e950b8c47e 100644
--- a/crates/wasi/src/filesystem/primitives/unix/mod.rs
+++ b/crates/wasi/src/filesystem/primitives/unix/mod.rs
@@ -14,7 +14,6 @@ use std::time::{Duration, SystemTime};
mod create_dir_unchecked;
mod dir_utils;
mod hard_link_unchecked;
-mod is_same_file;
mod oflags;
mod open_options_ext;
mod open_unchecked;
@@ -45,8 +44,6 @@ pub(crate) use self::linux::*;
pub(crate) use create_dir_unchecked::create_dir_unchecked;
pub(crate) use dir_utils::*;
pub(crate) use hard_link_unchecked::hard_link_unchecked;
-#[allow(unused_imports)]
-pub(crate) use is_same_file::{is_different_file, is_different_file_metadata, is_same_file};
pub(crate) use open_options_ext::ImplOpenOptionsExt;
pub(crate) use open_unchecked::open_unchecked;
pub(crate) use read_link_unchecked::read_link_unchecked;
@@ -199,7 +196,6 @@ pub(crate) fn read_dir(
}))
}
-#[allow(clippy::similar_names)]
pub(super) fn system_time_from_rustix(sec: i64, nsec: u64) -> Option<SystemTime> {
if sec >= 0 {
SystemTime::UNIX_EPOCH.checked_add(Duration::new(u64::try_from(sec).unwrap(), nsec as _))
diff --git a/crates/wasi/src/filesystem/primitives/unix/open_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/open_unchecked.rs
index 5e429f8c3d..b934068788 100644
--- a/crates/wasi/src/filesystem/primitives/unix/open_unchecked.rs
+++ b/crates/wasi/src/filesystem/primitives/unix/open_unchecked.rs
@@ -14,7 +14,6 @@ pub(crate) fn open_unchecked(
) -> Result<fs::File, OpenUncheckedError> {
let oflags = compute_oflags(options).map_err(OpenUncheckedError::Other)?;
- #[allow(clippy::useless_conversion)]
#[cfg(not(target_os = "wasi"))]
let mode = Mode::from_bits_truncate(options.ext.mode as _);
#[cfg(target_os = "wasi")]
diff --git a/crates/wasi/src/filesystem/primitives/unix/times.rs b/crates/wasi/src/filesystem/primitives/unix/times.rs
index 67425497d1..dac69f8c89 100644
--- a/crates/wasi/src/filesystem/primitives/unix/times.rs
+++ b/crates/wasi/src/filesystem/primitives/unix/times.rs
@@ -3,7 +3,6 @@ use rustix::time::Timespec;
use std::io;
use std::time::SystemTime;
-#[allow(clippy::useless_conversion)]
pub(crate) fn to_timespec(ft: Option<SystemTime>) -> io::Result<Timespec> {
Ok(match ft {
None => Timespec {
diff --git a/crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rs b/crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rs
index be0b7c6649..4c523c6800 100644
--- a/crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rs
@@ -1,5 +1,3 @@
-#![allow(unsafe_code)]
-
use std::mem;
use std::os::windows::io::HandleOrInvalid;
use std::ptr::null_mut;
@@ -73,7 +71,7 @@ const FILE_ATTRIBUTE_VALID_FLAGS: FILE_FLAGS_AND_ATTRIBUTES = FILE_ATTRIBUTE_EA
/// Also, the `lpfilename` is a Rust slice instead of a C-style NUL-terminated
/// array, because that's what our callers have and it's closer to what
/// `NtCreatePath` takes.
-#[allow(non_snake_case)]
+#[expect(non_snake_case, reason = "matching windows conventions")]
pub unsafe fn CreateFileAtW(
dir: HANDLE,
lpfilename: &[u16],
@@ -84,197 +82,199 @@ pub unsafe fn CreateFileAtW(
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE,
) -> HandleOrInvalid {
- // Absolute paths are not yet implemented here.
- //
- // It seems like `NtCreatePath` needs the apparently NT-internal `\??\`
- // prefix prepended to absolute paths. It's possible it needs other
- // path transforms as well. `RtlDosPathNameToNtPathName_U` may be a
- // function that does these things, though it's not available in
- // windows-sys and not documented, though one can find
- // [unofficial blog posts], though even they say things like "I`m
- // sorry that I cannot give more details on these functions".
- //
- // [unofficial blog posts]: https://mecanik.dev/en/posts/convert-dos-and-nt-paths-using-rtl-functions/
- assert!(dir != 0 as HANDLE);
-
- // Extended attributes are not implemented yet.
- if htemplatefile != 0 as HANDLE {
- SetLastError(ERROR_NOT_SUPPORTED);
- return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
- }
+ unsafe {
+ // Absolute paths are not yet implemented here.
+ //
+ // It seems like `NtCreatePath` needs the apparently NT-internal `\??\`
+ // prefix prepended to absolute paths. It's possible it needs other
+ // path transforms as well. `RtlDosPathNameToNtPathName_U` may be a
+ // function that does these things, though it's not available in
+ // windows-sys and not documented, though one can find
+ // [unofficial blog posts], though even they say things like "I`m
+ // sorry that I cannot give more details on these functions".
+ //
+ // [unofficial blog posts]: https://mecanik.dev/en/posts/convert-dos-and-nt-paths-using-rtl-functions/
+ assert!(dir != 0 as HANDLE);
- // Convert `dwcreationdisposition` to the `createdisposition` argument
- // to `NtCreateFile`. Do this before converting `lpfilename` so that
- // we can return early on failure.
- let createdisposition = match dwcreationdisposition {
- CREATE_NEW => FILE_CREATE,
- CREATE_ALWAYS => FILE_OVERWRITE_IF,
- OPEN_EXISTING => FILE_OPEN,
- OPEN_ALWAYS => FILE_OPEN_IF,
- TRUNCATE_EXISTING => FILE_OVERWRITE,
- _ => {
- SetLastError(ERROR_INVALID_PARAMETER);
+ // Extended attributes are not implemented yet.
+ if htemplatefile != 0 as HANDLE {
+ SetLastError(ERROR_NOT_SUPPORTED);
return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
}
- };
- // Convert `lpfilename` to a `UNICODE_STRING`.
- let byte_length = lpfilename.len() * mem::size_of::<u16>();
- let length: u16 = match byte_length.try_into() {
- Ok(length) => length,
- Err(_) => {
- SetLastError(ERROR_INVALID_NAME);
- return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
- }
- };
- let mut unicode_string = UNICODE_STRING {
- Buffer: lpfilename.as_ptr() as *mut u16,
- Length: length,
- MaximumLength: length,
- };
+ // Convert `dwcreationdisposition` to the `createdisposition` argument
+ // to `NtCreateFile`. Do this before converting `lpfilename` so that
+ // we can return early on failure.
+ let createdisposition = match dwcreationdisposition {
+ CREATE_NEW => FILE_CREATE,
+ CREATE_ALWAYS => FILE_OVERWRITE_IF,
+ OPEN_EXISTING => FILE_OPEN,
+ OPEN_ALWAYS => FILE_OPEN_IF,
+ TRUNCATE_EXISTING => FILE_OVERWRITE,
+ _ => {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
+ }
+ };
- let mut handle = INVALID_HANDLE_VALUE;
+ // Convert `lpfilename` to a `UNICODE_STRING`.
+ let byte_length = lpfilename.len() * mem::size_of::<u16>();
+ let length: u16 = match byte_length.try_into() {
+ Ok(length) => length,
+ Err(_) => {
+ SetLastError(ERROR_INVALID_NAME);
+ return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
+ }
+ };
+ let mut unicode_string = UNICODE_STRING {
+ Buffer: lpfilename.as_ptr() as *mut u16,
+ Length: length,
+ MaximumLength: length,
+ };
- // Convert `dwdesiredaccess` and `dwflagsandattributes` to the
- // `desiredaccess` argument to `NtCreateFile`.
- let mut desiredaccess = dwdesiredaccess | SYNCHRONIZE | FILE_READ_ATTRIBUTES;
- if dwflagsandattributes & FILE_FLAG_DELETE_ON_CLOSE != 0 {
- desiredaccess |= DELETE;
- }
+ let mut handle = INVALID_HANDLE_VALUE;
- // Compute `objectattributes`' `Attributes` field. Case-insensitive is
- // the expected behavior on Windows.
- let mut attributes = 0;
- if dwflagsandattributes & FILE_FLAG_POSIX_SEMANTICS != 0 {
- attributes |= OBJ_CASE_INSENSITIVE as u32;
- };
- if !lpsecurityattributes.is_null() && (*lpsecurityattributes).bInheritHandle != 0 {
- attributes |= OBJ_INHERIT as u32;
- }
+ // Convert `dwdesiredaccess` and `dwflagsandattributes` to the
+ // `desiredaccess` argument to `NtCreateFile`.
+ let mut desiredaccess = dwdesiredaccess | SYNCHRONIZE | FILE_READ_ATTRIBUTES;
+ if dwflagsandattributes & FILE_FLAG_DELETE_ON_CLOSE != 0 {
+ desiredaccess |= DELETE;
+ }
- // Compute the `objectattributes` argument to `NtCreateFile`.
- let mut objectattributes = mem::zeroed::<OBJECT_ATTRIBUTES>();
- objectattributes.Length = mem::size_of::<OBJECT_ATTRIBUTES>() as _;
- objectattributes.RootDirectory = dir;
- objectattributes.ObjectName = &mut unicode_string;
- objectattributes.Attributes = attributes;
- if !lpsecurityattributes.is_null() {
- objectattributes.SecurityDescriptor = (*lpsecurityattributes)
- .lpSecurityDescriptor
- .cast::<SECURITY_DESCRIPTOR>();
- }
+ // Compute `objectattributes`' `Attributes` field. Case-insensitive is
+ // the expected behavior on Windows.
+ let mut attributes = 0;
+ if dwflagsandattributes & FILE_FLAG_POSIX_SEMANTICS != 0 {
+ attributes |= OBJ_CASE_INSENSITIVE;
+ };
+ if !lpsecurityattributes.is_null() && (*lpsecurityattributes).bInheritHandle != 0 {
+ attributes |= OBJ_INHERIT;
+ }
- // If needed, set `objectattributes`' `SecurityQualityOfService` field.
- let mut qos;
- if dwflagsandattributes & SECURITY_SQOS_PRESENT != 0 {
- qos = mem::zeroed::<SECURITY_QUALITY_OF_SERVICE>();
- qos.Length = mem::size_of::<SECURITY_QUALITY_OF_SERVICE>() as _;
- qos.ImpersonationLevel = ((dwflagsandattributes >> 16) & 0x3) as _;
- qos.ContextTrackingMode = if dwflagsandattributes & SECURITY_CONTEXT_TRACKING != 0 {
- SECURITY_DYNAMIC_TRACKING
- } else {
- SECURITY_STATIC_TRACKING
- } as u8;
- qos.EffectiveOnly = ((dwflagsandattributes & SECURITY_EFFECTIVE_ONLY) != 0) as _;
+ // Compute the `objectattributes` argument to `NtCreateFile`.
+ let mut objectattributes = mem::zeroed::<OBJECT_ATTRIBUTES>();
+ objectattributes.Length = mem::size_of::<OBJECT_ATTRIBUTES>() as _;
+ objectattributes.RootDirectory = dir;
+ objectattributes.ObjectName = &mut unicode_string;
+ objectattributes.Attributes = attributes;
+ if !lpsecurityattributes.is_null() {
+ objectattributes.SecurityDescriptor = (*lpsecurityattributes)
+ .lpSecurityDescriptor
+ .cast::<SECURITY_DESCRIPTOR>();
+ }
- objectattributes.SecurityQualityOfService =
- (&mut qos as *mut SECURITY_QUALITY_OF_SERVICE).cast();
- }
+ // If needed, set `objectattributes`' `SecurityQualityOfService` field.
+ let mut qos;
+ if dwflagsandattributes & SECURITY_SQOS_PRESENT != 0 {
+ qos = mem::zeroed::<SECURITY_QUALITY_OF_SERVICE>();
+ qos.Length = mem::size_of::<SECURITY_QUALITY_OF_SERVICE>() as _;
+ qos.ImpersonationLevel = ((dwflagsandattributes >> 16) & 0x3) as _;
+ qos.ContextTrackingMode = if dwflagsandattributes & SECURITY_CONTEXT_TRACKING != 0 {
+ SECURITY_DYNAMIC_TRACKING
+ } else {
+ SECURITY_STATIC_TRACKING
+ } as u8;
+ qos.EffectiveOnly = ((dwflagsandattributes & SECURITY_EFFECTIVE_ONLY) != 0) as _;
- let mut iostatusblock = mem::zeroed::<IO_STATUS_BLOCK>();
- iostatusblock.Anonymous.Status = STATUS_PENDING;
+ objectattributes.SecurityQualityOfService =
+ (&mut qos as *mut SECURITY_QUALITY_OF_SERVICE).cast();
+ }
- // Compute the `fileattributes` argument to `NtCreateFile`. Mask off
- // unrecognized flags.
- let mut fileattributes = dwflagsandattributes & FILE_ATTRIBUTE_VALID_FLAGS;
- if fileattributes == 0 {
- fileattributes = FILE_ATTRIBUTE_NORMAL;
- }
+ let mut iostatusblock = mem::zeroed::<IO_STATUS_BLOCK>();
+ iostatusblock.Anonymous.Status = STATUS_PENDING;
+
+ // Compute the `fileattributes` argument to `NtCreateFile`. Mask off
+ // unrecognized flags.
+ let mut fileattributes = dwflagsandattributes & FILE_ATTRIBUTE_VALID_FLAGS;
+ if fileattributes == 0 {
+ fileattributes = FILE_ATTRIBUTE_NORMAL;
+ }
- // Compute the `createoptions` argument to `NtCreateFile`.
- let mut createoptions = 0;
- if dwflagsandattributes & FILE_FLAG_BACKUP_SEMANTICS == 0 {
- createoptions |= FILE_NON_DIRECTORY_FILE;
- } else {
- if dwdesiredaccess & GENERIC_ALL != 0 {
- createoptions |= FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REMOTE_INSTANCE;
+ // Compute the `createoptions` argument to `NtCreateFile`.
+ let mut createoptions = 0;
+ if dwflagsandattributes & FILE_FLAG_BACKUP_SEMANTICS == 0 {
+ createoptions |= FILE_NON_DIRECTORY_FILE;
} else {
- if dwdesiredaccess & GENERIC_READ != 0 {
- createoptions |= FILE_OPEN_FOR_BACKUP_INTENT;
- }
- if dwdesiredaccess & GENERIC_WRITE != 0 {
- createoptions |= FILE_OPEN_REMOTE_INSTANCE;
+ if dwdesiredaccess & GENERIC_ALL != 0 {
+ createoptions |= FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REMOTE_INSTANCE;
+ } else {
+ if dwdesiredaccess & GENERIC_READ != 0 {
+ createoptions |= FILE_OPEN_FOR_BACKUP_INTENT;
+ }
+ if dwdesiredaccess & GENERIC_WRITE != 0 {
+ createoptions |= FILE_OPEN_REMOTE_INSTANCE;
+ }
}
}
- }
- if dwflagsandattributes & FILE_FLAG_DELETE_ON_CLOSE != 0 {
- createoptions |= FILE_DELETE_ON_CLOSE;
- }
- if dwflagsandattributes & FILE_FLAG_NO_BUFFERING != 0 {
- createoptions |= FILE_NO_INTERMEDIATE_BUFFERING;
- }
- if dwflagsandattributes & FILE_FLAG_OPEN_NO_RECALL != 0 {
- createoptions |= FILE_OPEN_NO_RECALL;
- }
- if dwflagsandattributes & FILE_FLAG_OPEN_REPARSE_POINT != 0 {
- createoptions |= FILE_OPEN_REPARSE_POINT;
- }
- if dwflagsandattributes & FILE_FLAG_OVERLAPPED == 0 {
- createoptions |= FILE_SYNCHRONOUS_IO_NONALERT;
- }
- // FILE_FLAG_POSIX_SEMANTICS is handled above.
- if dwflagsandattributes & FILE_FLAG_RANDOM_ACCESS != 0 {
- createoptions |= FILE_RANDOM_ACCESS;
- }
- if dwflagsandattributes & FILE_FLAG_SESSION_AWARE != 0 {
- // TODO: How should we handle FILE_FLAG_SESSION_AWARE?
- SetLastError(ERROR_NOT_SUPPORTED);
- return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
- }
- if dwflagsandattributes & FILE_FLAG_SEQUENTIAL_SCAN != 0 {
- createoptions |= FILE_SEQUENTIAL_ONLY;
- }
- if dwflagsandattributes & FILE_FLAG_WRITE_THROUGH != 0 {
- createoptions |= FILE_WRITE_THROUGH;
- }
+ if dwflagsandattributes & FILE_FLAG_DELETE_ON_CLOSE != 0 {
+ createoptions |= FILE_DELETE_ON_CLOSE;
+ }
+ if dwflagsandattributes & FILE_FLAG_NO_BUFFERING != 0 {
+ createoptions |= FILE_NO_INTERMEDIATE_BUFFERING;
+ }
+ if dwflagsandattributes & FILE_FLAG_OPEN_NO_RECALL != 0 {
+ createoptions |= FILE_OPEN_NO_RECALL;
+ }
+ if dwflagsandattributes & FILE_FLAG_OPEN_REPARSE_POINT != 0 {
+ createoptions |= FILE_OPEN_REPARSE_POINT;
+ }
+ if dwflagsandattributes & FILE_FLAG_OVERLAPPED == 0 {
+ createoptions |= FILE_SYNCHRONOUS_IO_NONALERT;
+ }
+ // FILE_FLAG_POSIX_SEMANTICS is handled above.
+ if dwflagsandattributes & FILE_FLAG_RANDOM_ACCESS != 0 {
+ createoptions |= FILE_RANDOM_ACCESS;
+ }
+ if dwflagsandattributes & FILE_FLAG_SESSION_AWARE != 0 {
+ // TODO: How should we handle FILE_FLAG_SESSION_AWARE?
+ SetLastError(ERROR_NOT_SUPPORTED);
+ return HandleOrInvalid::from_raw_handle(INVALID_HANDLE_VALUE as _);
+ }
+ if dwflagsandattributes & FILE_FLAG_SEQUENTIAL_SCAN != 0 {
+ createoptions |= FILE_SEQUENTIAL_ONLY;
+ }
+ if dwflagsandattributes & FILE_FLAG_WRITE_THROUGH != 0 {
+ createoptions |= FILE_WRITE_THROUGH;
+ }
- // Ok, we have what we need to call `NtCreateFile` now!
- let status = NtCreateFile(
- &mut handle,
- desiredaccess,
- &mut objectattributes,
- &mut iostatusblock,
- null_mut(),
- fileattributes,
- dwsharemode,
- createdisposition,
- createoptions,
- null_mut(),
- 0,
- );
+ // Ok, we have what we need to call `NtCreateFile` now!
+ let status = NtCreateFile(
+ &mut handle,
+ desiredaccess,
+ &objectattributes,
+ &mut iostatusblock,
+ null_mut(),
+ fileattributes,
+ dwsharemode,
+ createdisposition,
+ createoptions,
+ null_mut(),
+ 0,
+ );
- // Check for errors.
- if status != STATUS_SUCCESS {
- handle = INVALID_HANDLE_VALUE;
- if status == STATUS_OBJECT_NAME_COLLISION {
- SetLastError(ERROR_FILE_EXISTS);
+ // Check for errors.
+ if status != STATUS_SUCCESS {
+ handle = INVALID_HANDLE_VALUE;
+ if status == STATUS_OBJECT_NAME_COLLISION {
+ SetLastError(ERROR_FILE_EXISTS);
+ } else {
+ SetLastError(RtlNtStatusToDosError(status));
+ }
+ } else if (dwcreationdisposition == CREATE_ALWAYS
+ && iostatusblock.Information == FILE_OVERWRITTEN as usize)
+ || (dwcreationdisposition == OPEN_ALWAYS
+ && iostatusblock.Information == FILE_OPENED as usize)
+ {
+ // Set `ERROR_ALREADY_EXISTS` according to the table for
+ // `dwCreationDisposition` in the [`CreateFileW` docs].
+ //
+ // [`CreateFileW` docs]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
+ SetLastError(ERROR_ALREADY_EXISTS);
} else {
- SetLastError(RtlNtStatusToDosError(status));
+ // Otherwise indicate that we succeeded.
+ SetLastError(SUCCESS);
}
- } else if (dwcreationdisposition == CREATE_ALWAYS
- && iostatusblock.Information == FILE_OVERWRITTEN as usize)
- || (dwcreationdisposition == OPEN_ALWAYS
- && iostatusblock.Information == FILE_OPENED as usize)
- {
- // Set `ERROR_ALREADY_EXISTS` according to the table for
- // `dwCreationDisposition` in the [`CreateFileW` docs].
- //
- // [`CreateFileW` docs]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
- SetLastError(ERROR_ALREADY_EXISTS);
- } else {
- // Otherwise indicate that we succeeded.
- SetLastError(SUCCESS);
- }
- HandleOrInvalid::from_raw_handle(handle as _)
+ HandleOrInvalid::from_raw_handle(handle as _)
+ }
}
diff --git a/crates/wasi/src/filesystem/primitives/windows/mod.rs b/crates/wasi/src/filesystem/primitives/windows/mod.rs
index 307decd70e..37aa982ad1 100644
--- a/crates/wasi/src/filesystem/primitives/windows/mod.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/mod.rs
@@ -1,5 +1,8 @@
use crate::filesystem::primitives::Metadata;
+use std::fs;
+use std::io;
use std::os::windows::fs::MetadataExt;
+use std::path::Path;
mod create_dir_unchecked;
mod create_file_at_w;
@@ -54,3 +57,9 @@ impl Metadata {
std.file_attributes()
}
}
+
+/// Open a directory by performing an unsandboxed `openat`-like operation.
+#[inline]
+pub(super) fn open_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<fs::File> {
+ open_unchecked(start, path, &dir_options()).map_err(Into::into)
+}
diff --git a/crates/wasi/src/filesystem/primitives/windows/open_fast.rs b/crates/wasi/src/filesystem/primitives/windows/open_fast.rs
index b1a52e58c0..d5943bc145 100644
--- a/crates/wasi/src/filesystem/primitives/windows/open_fast.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/open_fast.rs
@@ -40,7 +40,6 @@ fn file_prefix(path: &Path) -> Option<&OsStr> {
// This code is derived from
// https://github.com/rust-lang/rust/blob/9fe9041cc8eddaed402d17aa4facb2ce8f222e95/library/std/src/path.rs#L340
-#[allow(unsafe_code)]
fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
let slice = file.as_encoded_bytes();
if slice == b".." {
diff --git a/crates/wasi/src/filesystem/primitives/windows/open_options_ext.rs b/crates/wasi/src/filesystem/primitives/windows/open_options_ext.rs
index af8361d463..a587d51970 100644
--- a/crates/wasi/src/filesystem/primitives/windows/open_options_ext.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/open_options_ext.rs
@@ -1,5 +1,3 @@
-#![allow(unsafe_code)]
-
use crate::filesystem::primitives::OpenOptions;
use std::io;
use std::ptr::null_mut;
diff --git a/crates/wasi/src/filesystem/primitives/windows/open_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/open_unchecked.rs
index 9adb1ec848..4fe8ee0581 100644
--- a/crates/wasi/src/filesystem/primitives/windows/open_unchecked.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/open_unchecked.rs
@@ -1,7 +1,5 @@
//! Windows implementation of `openat` functionality.
-#![allow(unsafe_code)]
-
use super::create_file_at_w::CreateFileAtW;
use super::prepare_open_options_for_open;
use crate::filesystem::primitives::{
diff --git a/crates/wasmtime/src/runtime/gc/disabled/externref.rs b/crates/wasmtime/src/runtime/gc/disabled/externref.rs
index c81e013aa5..98cd2e0243 100644
--- a/crates/wasmtime/src/runtime/gc/disabled/externref.rs
+++ b/crates/wasmtime/src/runtime/gc/disabled/externref.rs
@@ -21,20 +21,14 @@ impl ExternRef {
pub fn data<'a, T: 'static>(
&self,
_store: impl Into<StoreContext<'a, T>>,
- ) -> Result<&'a (dyn Any + Send + Sync)>
- where
- T: 'a,
- {
+ ) -> Result<&'a (dyn Any + Send + Sync)> {
match *self {}
}
pub fn data_mut<'a, T: 'static>(
&self,
_store: impl Into<StoreContextMut<'a, T>>,
- ) -> Result<&'a mut (dyn Any + Send + Sync)>
- where
- T: 'a,
- {
+ ) -> Result<&'a mut (dyn Any + Send + Sync)> {
match *self {}
}
diff --git a/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs b/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
index 69438ea91f..73556a93dc 100644
--- a/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
@@ -271,6 +271,10 @@ unsafe extern "system" fn exception_handler(exception_info: *mut EXCEPTION_POINT
///
/// Invoked by Windows' vectored exception system; should not be called by
/// anyone else.
+#[allow(
+ clippy::cast_possible_truncation,
+ reason = "too fiddly to handle and wouldn't help much anyway"
+)]
unsafe extern "system" fn continue_handler(exception_info: *mut EXCEPTION_POINTERS) -> i32 {
let context = unsafe { &(*(*exception_info).ContextRecord) };
let last_exception_pc = LAST_EXCEPTION_PC.with(|s| s.replace(0));
diff --git a/crates/wasmtime/src/runtime/vm/sys/windows/vm.rs b/crates/wasmtime/src/runtime/vm/sys/windows/vm.rs
index 32e6f8ecbc..cf767135fa 100644
--- a/crates/wasmtime/src/runtime/vm/sys/windows/vm.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/windows/vm.rs
@@ -39,7 +39,7 @@ pub unsafe fn commit_pages(addr: *mut u8, len: usize) -> io::Result<()> {
unsafe { expose_existing_mapping(addr, len) }
}
-#[allow(non_camel_case_types)] // matching C conventions
+#[expect(non_camel_case_types, reason = "matching C conventions")]
#[cfg(feature = "pooling-allocator")]
pub struct iovec {
pub iov_base: *mut u8,