From 6ad8279c97778914177e4970ab4ddeb06a667e25 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Thu, 29 Jan 2026 00:24:50 +0800 Subject: [PATCH] fix(driver): make OpCode unsafe --- compio-driver/src/sys/fusion/mod.rs | 2 +- compio-driver/src/sys/iocp/mod.rs | 2 +- compio-driver/src/sys/iocp/op.rs | 4 ++-- compio-driver/src/sys/iour/mod.rs | 2 +- compio-driver/src/sys/iour/op.rs | 20 ++++++++++---------- compio-driver/src/sys/poll/mod.rs | 2 +- compio-driver/src/sys/poll/op.rs | 20 ++++++++++---------- compio-driver/src/sys/stub/mod.rs | 2 +- compio-driver/src/sys/stub/op.rs | 20 ++++++++++---------- compio-process/src/windows.rs | 2 +- compio-runtime/tests/event.rs | 2 +- 11 files changed, 39 insertions(+), 39 deletions(-) diff --git a/compio-driver/src/sys/fusion/mod.rs b/compio-driver/src/sys/fusion/mod.rs index bfebddae5..197f5b4ac 100644 --- a/compio-driver/src/sys/fusion/mod.rs +++ b/compio-driver/src/sys/fusion/mod.rs @@ -72,7 +72,7 @@ impl super::Extra { /// Fused [`OpCode`] /// /// This trait encapsulates both operation for `io-uring` and `polling` -pub trait OpCode: PollOpCode + IourOpCode {} +pub unsafe trait OpCode: PollOpCode + IourOpCode {} impl OpCode for T {} diff --git a/compio-driver/src/sys/iocp/mod.rs b/compio-driver/src/sys/iocp/mod.rs index 205181f93..4271a90bf 100644 --- a/compio-driver/src/sys/iocp/mod.rs +++ b/compio-driver/src/sys/iocp/mod.rs @@ -274,7 +274,7 @@ pub enum OpType { } /// Abstraction of IOCP operations. -pub trait OpCode { +pub unsafe trait OpCode { /// Determines that the operation is really overlapped defined by Windows /// API. If not, the driver will try to operate it in another thread. fn op_type(&self) -> OpType { diff --git a/compio-driver/src/sys/iocp/op.rs b/compio-driver/src/sys/iocp/op.rs index 84a15aae8..33f43c44f 100644 --- a/compio-driver/src/sys/iocp/op.rs +++ b/compio-driver/src/sys/iocp/op.rs @@ -154,7 +154,7 @@ impl< } } -impl OpCode for CloseFile { +unsafe impl OpCode for CloseFile { fn op_type(&self) -> OpType { OpType::Blocking } @@ -309,7 +309,7 @@ impl OpCode for ShutdownSocket { } } -impl OpCode for CloseSocket { +unsafe impl OpCode for CloseSocket { fn op_type(&self) -> OpType { OpType::Blocking } diff --git a/compio-driver/src/sys/iour/mod.rs b/compio-driver/src/sys/iour/mod.rs index ff0c0568f..1732ea058 100644 --- a/compio-driver/src/sys/iour/mod.rs +++ b/compio-driver/src/sys/iour/mod.rs @@ -107,7 +107,7 @@ impl From for OpEntry { } /// Abstraction of io-uring operations. -pub trait OpCode { +pub unsafe trait OpCode { /// Create submission entry. fn create_entry(self: Pin<&mut Self>) -> OpEntry; diff --git a/compio-driver/src/sys/iour/op.rs b/compio-driver/src/sys/iour/op.rs index 3865b173e..4225b8294 100644 --- a/compio-driver/src/sys/iour/op.rs +++ b/compio-driver/src/sys/iour/op.rs @@ -61,7 +61,7 @@ impl< } } -impl OpCode for OpenFile { +unsafe impl OpCode for OpenFile { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::OpenAt::new(Fd(libc::AT_FDCWD), self.path.as_ptr()) .flags(self.flags | libc::O_CLOEXEC) @@ -71,7 +71,7 @@ impl OpCode for OpenFile { } } -impl OpCode for CloseFile { +unsafe impl OpCode for CloseFile { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::Close::new(Fd(self.fd.as_fd().as_raw_fd())) .build() @@ -155,7 +155,7 @@ impl PathStat { } } -impl OpCode for PathStat { +unsafe impl OpCode for PathStat { fn create_entry(mut self: Pin<&mut Self>) -> OpEntry { let mut flags = libc::AT_EMPTY_PATH; if !self.follow_symlink { @@ -309,7 +309,7 @@ impl OpCode for Sync { } } -impl OpCode for Unlink { +unsafe impl OpCode for Unlink { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::UnlinkAt::new(Fd(libc::AT_FDCWD), self.path.as_ptr()) .flags(if self.dir { libc::AT_REMOVEDIR } else { 0 }) @@ -318,7 +318,7 @@ impl OpCode for Unlink { } } -impl OpCode for CreateDir { +unsafe impl OpCode for CreateDir { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::MkDirAt::new(Fd(libc::AT_FDCWD), self.path.as_ptr()) .mode(self.mode) @@ -327,7 +327,7 @@ impl OpCode for CreateDir { } } -impl OpCode for Rename { +unsafe impl OpCode for Rename { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::RenameAt::new( Fd(libc::AT_FDCWD), @@ -340,7 +340,7 @@ impl OpCode for Rename { } } -impl OpCode for Symlink { +unsafe impl OpCode for Symlink { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::SymlinkAt::new( Fd(libc::AT_FDCWD), @@ -352,7 +352,7 @@ impl OpCode for Symlink { } } -impl OpCode for HardLink { +unsafe impl OpCode for HardLink { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::LinkAt::new( Fd(libc::AT_FDCWD), @@ -365,7 +365,7 @@ impl OpCode for HardLink { } } -impl OpCode for CreateSocket { +unsafe impl OpCode for CreateSocket { fn create_entry(self: Pin<&mut Self>) -> OpEntry { if super::is_op_supported(opcode::Socket::CODE) { opcode::Socket::new( @@ -397,7 +397,7 @@ impl OpCode for ShutdownSocket { } } -impl OpCode for CloseSocket { +unsafe impl OpCode for CloseSocket { fn create_entry(self: Pin<&mut Self>) -> OpEntry { opcode::Close::new(Fd(self.fd.as_fd().as_raw_fd())) .build() diff --git a/compio-driver/src/sys/poll/mod.rs b/compio-driver/src/sys/poll/mod.rs index e0a5f8235..5612b62c4 100644 --- a/compio-driver/src/sys/poll/mod.rs +++ b/compio-driver/src/sys/poll/mod.rs @@ -48,7 +48,7 @@ impl From for Track { /// `Some(OpType::Fd)` with same fds as the `WaitArg`s. Similarly, if /// `pre_submit` returns `Decision::Aio`, `op_type` must return /// `Some(OpType::Aio)` with the correct `aiocb` pointer. -pub trait OpCode { +pub unsafe trait OpCode { /// Perform the operation before submit, and return [`Decision`] to /// indicate whether submitting the operation to polling is required. fn pre_submit(self: Pin<&mut Self>) -> io::Result; diff --git a/compio-driver/src/sys/poll/op.rs b/compio-driver/src/sys/poll/op.rs index c1f6617ad..f710b025a 100644 --- a/compio-driver/src/sys/poll/op.rs +++ b/compio-driver/src/sys/poll/op.rs @@ -68,7 +68,7 @@ impl< } } -impl OpCode for OpenFile { +unsafe impl OpCode for OpenFile { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -82,7 +82,7 @@ impl OpCode for OpenFile { } } -impl OpCode for CloseFile { +unsafe impl OpCode for CloseFile { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -176,7 +176,7 @@ impl PathStat { } } -impl OpCode for PathStat { +unsafe impl OpCode for PathStat { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -526,7 +526,7 @@ impl OpCode for Sync { } } -impl OpCode for Unlink { +unsafe impl OpCode for Unlink { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -541,7 +541,7 @@ impl OpCode for Unlink { } } -impl OpCode for CreateDir { +unsafe impl OpCode for CreateDir { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -552,7 +552,7 @@ impl OpCode for CreateDir { } } -impl OpCode for Rename { +unsafe impl OpCode for Rename { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -563,7 +563,7 @@ impl OpCode for Rename { } } -impl OpCode for Symlink { +unsafe impl OpCode for Symlink { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -574,7 +574,7 @@ impl OpCode for Symlink { } } -impl OpCode for HardLink { +unsafe impl OpCode for HardLink { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -645,7 +645,7 @@ impl CreateSocket { } } -impl OpCode for CreateSocket { +unsafe impl OpCode for CreateSocket { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } @@ -667,7 +667,7 @@ impl OpCode for ShutdownSocket { } } -impl OpCode for CloseSocket { +unsafe impl OpCode for CloseSocket { fn pre_submit(self: Pin<&mut Self>) -> io::Result { Ok(Decision::Blocking) } diff --git a/compio-driver/src/sys/stub/mod.rs b/compio-driver/src/sys/stub/mod.rs index 6493168cb..aa0d161e4 100644 --- a/compio-driver/src/sys/stub/mod.rs +++ b/compio-driver/src/sys/stub/mod.rs @@ -30,7 +30,7 @@ impl Extra { } /// Operations. -pub trait OpCode {} +pub unsafe trait OpCode {} pub mod op; diff --git a/compio-driver/src/sys/stub/op.rs b/compio-driver/src/sys/stub/op.rs index 0d8c22399..9f72aac02 100644 --- a/compio-driver/src/sys/stub/op.rs +++ b/compio-driver/src/sys/stub/op.rs @@ -24,9 +24,9 @@ impl< { } -impl OpCode for OpenFile {} +unsafe impl OpCode for OpenFile {} -impl OpCode for CloseFile {} +unsafe impl OpCode for CloseFile {} impl OpCode for TruncateFile {} @@ -68,7 +68,7 @@ impl PathStat { } } -impl OpCode for PathStat {} +unsafe impl OpCode for PathStat {} impl IntoInner for PathStat { type Inner = Stat; @@ -96,21 +96,21 @@ impl OpCode for WriteVectored {} impl OpCode for Sync {} -impl OpCode for Unlink {} +unsafe impl OpCode for Unlink {} -impl OpCode for CreateDir {} +unsafe impl OpCode for CreateDir {} -impl OpCode for Rename {} +unsafe impl OpCode for Rename {} -impl OpCode for Symlink {} +unsafe impl OpCode for Symlink {} -impl OpCode for HardLink {} +unsafe impl OpCode for HardLink {} -impl OpCode for CreateSocket {} +unsafe impl OpCode for CreateSocket {} impl OpCode for ShutdownSocket {} -impl OpCode for CloseSocket {} +unsafe impl OpCode for CloseSocket {} impl OpCode for Accept {} diff --git a/compio-process/src/windows.rs b/compio-process/src/windows.rs index 91285cb0d..0aa42b51f 100644 --- a/compio-process/src/windows.rs +++ b/compio-process/src/windows.rs @@ -28,7 +28,7 @@ impl WaitProcess { } } -impl OpCode for WaitProcess { +unsafe impl OpCode for WaitProcess { fn op_type(&self) -> OpType { OpType::Event(self.child.as_raw_handle() as _) } diff --git a/compio-runtime/tests/event.rs b/compio-runtime/tests/event.rs index 9c51f1d3c..3a805fcb0 100644 --- a/compio-runtime/tests/event.rs +++ b/compio-runtime/tests/event.rs @@ -35,7 +35,7 @@ fn win32_event() { event: OwnedHandle, } - impl OpCode for WaitEvent { + unsafe impl OpCode for WaitEvent { fn op_type(&self) -> OpType { OpType::Event(self.event.as_raw_handle() as _) }