@@ -36,12 +36,7 @@ pub struct Coroutine<'c, Param, Yield, Return> {
3636impl < ' 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 {
0 commit comments