Skip to content

Commit 7e3e936

Browse files
authored
remove some 'as' cast and unnecessary mod (#332)
2 parents 0e800ef + 60ff74f commit 7e3e936

File tree

16 files changed

+144
-182
lines changed

16 files changed

+144
-182
lines changed

core/src/coroutine/korosensei.rs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ pub struct Coroutine<'c, Param, Yield, Return> {
3636
impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
3737
cfg_if::cfg_if! {
3838
if #[cfg(unix)] {
39-
#[allow(
40-
clippy::cast_possible_truncation,
41-
clippy::too_many_lines,
42-
clippy::cast_sign_loss,
43-
clippy::cast_possible_wrap
44-
)]
39+
#[allow(clippy::too_many_lines)]
4540
extern "C" fn trap_handler(
4641
_signum: libc::c_int,
4742
_siginfo: *mut libc::siginfo_t,
@@ -56,43 +51,40 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
5651
any(target_os = "linux", target_os = "android"),
5752
target_arch = "x86_64",
5853
))] {
59-
let sp = context.uc_mcontext.gregs[libc::REG_RSP as usize] as usize;
54+
let sp = u64::try_from(context.uc_mcontext.gregs[usize::try_from(libc::REG_RSP).expect("overflow")]).expect("overflow");
6055
} else if #[cfg(all(
6156
any(target_os = "linux", target_os = "android"),
6257
target_arch = "x86",
6358
))] {
64-
let sp = context.uc_mcontext.gregs[libc::REG_ESP as usize] as usize;
59+
let sp = u64::from(std::mem::transmute::<_, std::ffi::c_uint>(context.uc_mcontext.gregs[usize::try_from(libc::REG_ESP).expect("overflow")]));
6560
} else if #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] {
66-
let sp = (*context.uc_mcontext).__ss.__rsp as usize;
61+
let sp = u64::try_from((*context.uc_mcontext).__ss.__rsp).expect("overflow");
6762
} else if #[cfg(all(
6863
any(target_os = "linux", target_os = "android"),
6964
target_arch = "aarch64",
7065
))] {
71-
let sp = context.uc_mcontext.sp as usize;
66+
let sp = u64::try_from(context.uc_mcontext.sp).expect("overflow");
7267
} else if #[cfg(all(
7368
any(target_os = "linux", target_os = "android"),
7469
target_arch = "arm",
7570
))] {
76-
let sp = context.uc_mcontext.arm_sp as usize;
71+
let sp = u64::try_from(context.uc_mcontext.arm_sp).expect("overflow");
7772
} else if #[cfg(all(
7873
any(target_os = "linux", target_os = "android"),
7974
any(target_arch = "riscv64", target_arch = "riscv32"),
8075
))] {
81-
let sp = context.uc_mcontext.__gregs[libc::REG_SP] as usize;
76+
let sp = u64::try_from(context.uc_mcontext.__gregs[libc::REG_SP]).expect("overflow");
8277
} else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] {
83-
let sp = (*context.uc_mcontext).__ss.__sp as usize;
78+
let sp = (*context.uc_mcontext).__ss.__sp;
8479
} else if #[cfg(all(target_os = "linux", target_arch = "loongarch64"))] {
85-
let sp = context.uc_mcontext.__gregs[3] as usize;
80+
let sp = u64::try_from(context.uc_mcontext.__gregs[3]).expect("overflow");
8681
} else {
8782
compile_error!("Unsupported platform");
8883
}
8984
}
9085
if let Some(co) = Self::current() {
91-
let handler = co.inner.trap_handler();
92-
// assert!(handler.stack_ptr_in_bounds(sp), "coroutine {} stack overflow !", co.get_name());
93-
// let regs = handler.setup_trap_handler(|| Err("invalid memory reference"));
94-
let stack_ptr_in_bounds = handler.stack_ptr_in_bounds(sp);
95-
let regs = handler.setup_trap_handler(move || {
86+
let stack_ptr_in_bounds = co.stack_ptr_in_bounds(sp);
87+
let regs = co.inner.trap_handler().setup_trap_handler(move || {
9688
Err(if stack_ptr_in_bounds {
9789
"invalid memory reference"
9890
} else {
@@ -105,21 +97,21 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
10597
target_arch = "x86_64",
10698
))] {
10799
let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
108-
context.uc_mcontext.gregs[libc::REG_RIP as usize] = rip as i64;
109-
context.uc_mcontext.gregs[libc::REG_RSP as usize] = rsp as i64;
110-
context.uc_mcontext.gregs[libc::REG_RBP as usize] = rbp as i64;
111-
context.uc_mcontext.gregs[libc::REG_RDI as usize] = rdi as i64;
112-
context.uc_mcontext.gregs[libc::REG_RSI as usize] = rsi as i64;
100+
context.uc_mcontext.gregs[usize::try_from(libc::REG_RIP).expect("overflow")] = std::ffi::c_longlong::try_from(rip).expect("overflow");
101+
context.uc_mcontext.gregs[usize::try_from(libc::REG_RSP).expect("overflow")] = std::ffi::c_longlong::try_from(rsp).expect("overflow");
102+
context.uc_mcontext.gregs[usize::try_from(libc::REG_RBP).expect("overflow")] = std::ffi::c_longlong::try_from(rbp).expect("overflow");
103+
context.uc_mcontext.gregs[usize::try_from(libc::REG_RDI).expect("overflow")] = std::ffi::c_longlong::try_from(rdi).expect("overflow");
104+
context.uc_mcontext.gregs[usize::try_from(libc::REG_RSI).expect("overflow")] = std::ffi::c_longlong::try_from(rsi).expect("overflow");
113105
} else if #[cfg(all(
114106
any(target_os = "linux", target_os = "android"),
115107
target_arch = "x86",
116108
))] {
117109
let TrapHandlerRegs { eip, esp, ebp, ecx, edx } = regs;
118-
context.uc_mcontext.gregs[libc::REG_EIP as usize] = eip as i32;
119-
context.uc_mcontext.gregs[libc::REG_ESP as usize] = esp as i32;
120-
context.uc_mcontext.gregs[libc::REG_EBP as usize] = ebp as i32;
121-
context.uc_mcontext.gregs[libc::REG_ECX as usize] = ecx as i32;
122-
context.uc_mcontext.gregs[libc::REG_EDX as usize] = edx as i32;
110+
context.uc_mcontext.gregs[usize::try_from(libc::REG_EIP).expect("overflow")] = std::mem::transmute(eip);
111+
context.uc_mcontext.gregs[usize::try_from(libc::REG_ESP).expect("overflow")] = std::mem::transmute(esp);
112+
context.uc_mcontext.gregs[usize::try_from(libc::REG_EBP).expect("overflow")] = std::mem::transmute(ebp);
113+
context.uc_mcontext.gregs[usize::try_from(libc::REG_ECX).expect("overflow")] = std::mem::transmute(ecx);
114+
context.uc_mcontext.gregs[usize::try_from(libc::REG_EDX).expect("overflow")] = std::mem::transmute(edx);
123115
} else if #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] {
124116
let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
125117
(*context.uc_mcontext).__ss.__rip = rip;
@@ -175,12 +167,12 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
175167
any(target_arch = "riscv64", target_arch = "riscv32"),
176168
))] {
177169
let TrapHandlerRegs { pc, ra, sp, a0, a1, s0 } = regs;
178-
context.uc_mcontext.__gregs[libc::REG_PC] = pc as libc::c_ulong;
179-
context.uc_mcontext.__gregs[libc::REG_RA] = ra as libc::c_ulong;
180-
context.uc_mcontext.__gregs[libc::REG_SP] = sp as libc::c_ulong;
181-
context.uc_mcontext.__gregs[libc::REG_A0] = a0 as libc::c_ulong;
182-
context.uc_mcontext.__gregs[libc::REG_A0 + 1] = a1 as libc::c_ulong;
183-
context.uc_mcontext.__gregs[libc::REG_S0] = s0 as libc::c_ulong;
170+
context.uc_mcontext.__gregs[libc::REG_PC] = std::ffi::c_ulong::try_from(pc).expect("overflow");
171+
context.uc_mcontext.__gregs[libc::REG_RA] = std::ffi::c_ulong::try_from(ra).expect("overflow");
172+
context.uc_mcontext.__gregs[libc::REG_SP] = std::ffi::c_ulong::try_from(sp).expect("overflow");
173+
context.uc_mcontext.__gregs[libc::REG_A0] = std::ffi::c_ulong::try_from(a0).expect("overflow");
174+
context.uc_mcontext.__gregs[libc::REG_A0 + 1] = std::ffi::c_ulong::try_from(a1).expect("overflow");
175+
context.uc_mcontext.__gregs[libc::REG_S0] = std::ffi::c_ulong::try_from(s0).expect("overflow");
184176
} else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] {
185177
let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
186178
(*context.uc_mcontext).__ss.__pc = pc;
@@ -214,23 +206,16 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
214206
if let Some(co) = Self::current() {
215207
cfg_if::cfg_if! {
216208
if #[cfg(target_arch = "x86_64")] {
217-
let sp = usize::try_from((*(*exception_info).ContextRecord).Rsp).expect("parse RSP failed");
209+
let sp = (*(*exception_info).ContextRecord).Rsp;
218210
} else if #[cfg(target_arch = "x86")] {
219-
let sp = (*(*exception_info).ContextRecord).Esp as usize;
211+
let sp = u64::from((*(*exception_info).ContextRecord).Esp);
220212
} else {
221213
compile_error!("Unsupported platform");
222214
}
223215
}
224216

225-
let handler = co.inner.trap_handler();
226-
// if !handler.stack_ptr_in_bounds(sp) {
227-
// // EXCEPTION_CONTINUE_SEARCH
228-
// crate::error!("coroutine {} stack overflow !", co.get_name());
229-
// return 0;
230-
// }
231-
// let regs = handler.setup_trap_handler(|| Err("invalid memory reference"));
232-
let stack_ptr_in_bounds = handler.stack_ptr_in_bounds(sp);
233-
let regs = handler.setup_trap_handler(move || {
217+
let stack_ptr_in_bounds = co.stack_ptr_in_bounds(sp);
218+
let regs = co.inner.trap_handler().setup_trap_handler(move || {
234219
Err(if stack_ptr_in_bounds {
235220
"invalid memory reference"
236221
} else {

core/src/coroutine/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
9696
self.stack_infos.borrow().clone()
9797
}
9898

99+
/// Checks whether the stack pointer at the point where a trap occurred is
100+
/// within the coroutine that this `CoroutineTrapHandler` was produced from.
101+
/// This check includes any guard pages on the stack and will therefore
102+
/// still return true in the case of a stack overflow.
103+
///
104+
/// The result of this function is only meaningful if the coroutine has not
105+
/// been dropped yet.
106+
pub fn stack_ptr_in_bounds(&self, stack_ptr: u64) -> bool {
107+
for info in self.stack_infos.borrow().iter() {
108+
if info.stack_bottom as u64 <= stack_ptr && stack_ptr < info.stack_top as u64 {
109+
return true;
110+
}
111+
}
112+
false
113+
}
114+
99115
/// Grows the call stack if necessary.
100116
///
101117
/// This function is intended to be called at manually instrumented points in a program where

core/src/syscall/common.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

core/src/syscall/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
pub mod common;
1+
macro_rules! syscall_mod {
2+
($($mod_name: ident);*) => {
3+
$(
4+
pub use $mod_name::$mod_name;
5+
mod $mod_name;
6+
)*
7+
}
8+
}
29

310
#[cfg(unix)]
411
pub use unix::*;

core/src/syscall/unix/connect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::common::now;
22
use crate::net::EventLoops;
3-
use crate::syscall::common::{is_blocking, reset_errno, send_time_limit, set_blocking, set_errno, set_non_blocking};
3+
use crate::syscall::{is_blocking, reset_errno, send_time_limit, set_blocking, set_errno, set_non_blocking};
44
use libc::{sockaddr, socklen_t};
55
use once_cell::sync::Lazy;
66
use std::ffi::{c_int, c_void};

0 commit comments

Comments
 (0)