Skip to content

Commit d37bbeb

Browse files
committed
CI check clippy
1 parent fe17ef8 commit d37bbeb

File tree

11 files changed

+144
-23
lines changed

11 files changed

+144
-23
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,22 @@ jobs:
2121
toolchain: ${{ matrix.target == 'i686-pc-windows-gnu' && format('{0}-i686-pc-windows-gnu', matrix.channel) || matrix.channel }}
2222
target: ${{ matrix.target }}
2323
override: true
24-
components: rustfmt
25-
- uses: actions-rs/cargo@v1
24+
components: rustfmt, clippy
25+
- name: Check code format
26+
uses: actions-rs/cargo@v1
2627
with:
2728
command: fmt
2829
args: --all -- --check
30+
- name: Check clippy with default features
31+
uses: actions-rs/cargo@v1
32+
with:
33+
command: clippy
34+
args: --all -- -D warnings
35+
- name: Check clippy with all features
36+
uses: actions-rs/cargo@v1
37+
with:
38+
command: clippy
39+
args: --all --all-features -- -D warnings
2940
- name: Run cargo deny
3041
if: ${{ contains(matrix.os, 'ubuntu') }}
3142
uses: EmbarkStudios/cargo-deny-action@v2

core/src/coroutine/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'c> CoroutineLocal<'c> {
1616
pub fn put<V>(&self, key: &'c str, val: V) -> Option<V> {
1717
let v = Box::leak(Box::new(val));
1818
self.0
19-
.insert(key, std::ptr::from_mut::<V>(v) as usize)
19+
.insert(key, std::ptr::from_mut(v) as usize)
2020
.map(|ptr| unsafe { *Box::from_raw((ptr as *mut c_void).cast::<V>()) })
2121
}
2222

core/src/net/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1111
use std::sync::{Arc, Condvar, Mutex};
1212
use std::time::Duration;
1313

14-
/// 做C兼容时会用到
15-
pub type UserFunc = extern "C" fn(usize) -> usize;
16-
1714
cfg_if::cfg_if! {
1815
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
1916
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t};
2017
use std::ffi::{c_longlong, c_void};
2118
}
2219
}
2320

21+
/// 做C兼容时会用到
22+
pub type UserFunc = extern "C" fn(usize) -> usize;
23+
2424
mod selector;
2525

2626
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]

core/src/net/operator/linux/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ impl Operator<'_> {
574574
)
575575
}
576576

577-
#[allow(clippy::too_many_arguments)]
578577
pub(crate) fn sendto(
579578
&self,
580579
user_data: usize,

core/src/net/operator/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(clippy::too_many_arguments)]
12
#[cfg(all(target_os = "linux", feature = "io_uring"))]
23
mod linux;
34
#[cfg(all(target_os = "linux", feature = "io_uring"))]

core/src/syscall/unix/connect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ impl<I: ConnectSyscall> ConnectSyscall for NioConnectSyscall<I> {
7474
{
7575
break;
7676
}
77-
let mut err: c_int = 0;
77+
let mut err = 0;
7878
unsafe {
7979
let mut len: socklen_t = std::mem::zeroed();
8080
r = libc::getsockopt(
8181
fd,
8282
libc::SOL_SOCKET,
8383
libc::SO_ERROR,
84-
(std::ptr::addr_of_mut!(err)).cast::<c_void>(),
84+
std::ptr::addr_of_mut!(err).cast::<c_void>(),
8585
&mut len,
8686
);
8787
}

core/src/syscall/unix/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ macro_rules! impl_facade {
2323
let new_state = $crate::common::constants::SyscallState::Executing;
2424
if co.syscall((), syscall, new_state).is_err() {
2525
$crate::error!("{} change to syscall {} {} failed !",
26-
co.name(), syscall, new_state);
26+
co.name(), syscall, new_state
27+
);
2728
}
2829
}
2930
let r = self.inner.$syscall(fn_ptr, $($arg, )*);
@@ -66,9 +67,7 @@ macro_rules! impl_io_uring {
6667
if co.syscall((), syscall, new_state).is_err() {
6768
$crate::error!(
6869
"{} change to syscall {} {} failed !",
69-
co.name(),
70-
syscall,
71-
new_state
70+
co.name(), syscall, new_state
7271
);
7372
}
7473
}
@@ -84,9 +83,7 @@ macro_rules! impl_io_uring {
8483
if co.syscall((), syscall, new_state).is_err() {
8584
$crate::error!(
8685
"{} change to syscall {} {} failed !",
87-
co.name(),
88-
syscall,
89-
new_state
86+
co.name(), syscall, new_state
9087
);
9188
}
9289
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use crate::net::EventLoops;
2+
use crate::syscall::common::{is_blocking, reset_errno, set_blocking, set_errno, set_non_blocking};
3+
use once_cell::sync::Lazy;
4+
use std::ffi::c_int;
5+
use std::io::Error;
6+
use windows_sys::Win32::Networking::WinSock::{getpeername, getsockopt, SO_ERROR, SOCKADDR, SOCKET, SOL_SOCKET, WSAEALREADY, WSAEINPROGRESS, WSAEINTR, WSAETIMEDOUT};
7+
8+
#[must_use]
9+
pub extern "system" fn connect(
10+
fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>,
11+
socket: SOCKET,
12+
address: *const SOCKADDR,
13+
len: c_int,
14+
) -> c_int {
15+
static CHAIN: Lazy<ConnectSyscallFacade<NioConnectSyscall<RawConnectSyscall>>> =
16+
Lazy::new(Default::default);
17+
CHAIN.connect(fn_ptr, socket, address, len)
18+
}
19+
20+
trait ConnectSyscall {
21+
extern "system" fn connect(
22+
&self,
23+
fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>,
24+
fd: SOCKET,
25+
address: *const SOCKADDR,
26+
len: c_int,
27+
) -> c_int;
28+
}
29+
30+
impl_facade!(ConnectSyscallFacade, ConnectSyscall,
31+
connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int
32+
);
33+
34+
#[repr(C)]
35+
#[derive(Debug, Default)]
36+
struct NioConnectSyscall<I: ConnectSyscall> {
37+
inner: I,
38+
}
39+
40+
impl<I: ConnectSyscall> ConnectSyscall for NioConnectSyscall<I> {
41+
extern "system" fn connect(
42+
&self,
43+
fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>,
44+
fd: SOCKET,
45+
address: *const SOCKADDR,
46+
len: c_int,
47+
) -> c_int {
48+
let blocking = is_blocking(fd);
49+
if blocking {
50+
set_non_blocking(fd);
51+
}
52+
let mut r = self.inner.connect(fn_ptr, fd, address, len);
53+
loop {
54+
if r == 0 {
55+
reset_errno();
56+
break;
57+
}
58+
let errno = Error::last_os_error().raw_os_error();
59+
if errno == Some(WSAEINPROGRESS) || errno == Some(WSAEALREADY) {
60+
//阻塞,直到写事件发生
61+
if EventLoops::wait_write_event(
62+
fd as _,
63+
Some(crate::common::constants::SLICE)
64+
).is_err() {
65+
break;
66+
}
67+
let mut err = 0;
68+
unsafe {
69+
let mut len: c_int = std::mem::zeroed();
70+
r = getsockopt(
71+
fd,
72+
SOL_SOCKET,
73+
SO_ERROR,
74+
std::ptr::addr_of_mut!(err).cast::<u8>(),
75+
&mut len,
76+
);
77+
}
78+
if r != 0 {
79+
r = -1;
80+
break;
81+
}
82+
if err != 0 {
83+
set_errno(err);
84+
r = -1;
85+
break;
86+
};
87+
unsafe {
88+
let mut address = std::mem::zeroed();
89+
let mut address_len = std::mem::zeroed();
90+
r = getpeername(fd, &mut address, &mut address_len);
91+
}
92+
} else if errno != Some(WSAEINTR) {
93+
break;
94+
}
95+
}
96+
if r == -1 && Error::last_os_error().raw_os_error() == Some(WSAETIMEDOUT) {
97+
set_errno(WSAEINPROGRESS.try_into().expect("overflow"));
98+
}
99+
if blocking {
100+
set_blocking(fd);
101+
}
102+
r
103+
}
104+
}
105+
106+
impl_raw!(RawConnectSyscall, ConnectSyscall, windows_sys::Win32::Networking::WinSock,
107+
connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int
108+
);

core/src/syscall/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ syscall_mod!(
481481
send;
482482
shutdown;
483483
socket;
484+
connect;
484485
CreateFileW;
485486
SetFilePointerEx;
486487
WaitOnAddress

hook/src/syscall/windows.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,18 @@ unsafe fn attach() -> std::io::Result<()> {
142142
lpnewfilepointer : *mut c_longlong,
143143
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
144144
) -> BOOL);
145-
// NOTE: unhook WaitOnAddress due to stack overflow or bug
145+
// NOTE: unhook WaitOnAddress/connect due to stack overflow or bug
146146
// impl_hook!("api-ms-win-core-synch-l1-2-0.dll", WAITONADDRESS, WaitOnAddress(
147147
// address: *const c_void,
148148
// compareaddress: *const c_void,
149149
// addresssize: usize,
150150
// dwmilliseconds: c_uint
151151
// ) -> BOOL);
152+
// impl_hook!("ws2_32.dll", CONNECT, connect(
153+
// fd: SOCKET,
154+
// address: *const SOCKADDR,
155+
// len: c_int
156+
// ) -> c_int);
152157
// Enable the hook
153158
minhook::MinHook::enable_all_hooks()
154159
.map_err(|_| Error::new(ErrorKind::Other, "init all hooks failed !"))

0 commit comments

Comments
 (0)