From 4d03ecc187b4ae90600d61c59c99b294aa77c96e Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 8 Jun 2020 18:32:44 +0800 Subject: [PATCH] decode RPC exception --- src/runtime/src/comms.rs | 42 +++++++++++++++++++++++++++++++--- src/runtime/src/proto_async.rs | 11 --------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 9767f7b..e707804 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -1,9 +1,10 @@ use core::mem::transmute; use core::fmt; use core::cell::RefCell; +use core::str::Utf8Error; use alloc::rc::Rc; use alloc::sync::Arc; -use alloc::{vec, vec::Vec}; +use alloc::{vec, vec::Vec, string::String}; use log::{debug, warn, error}; use num_derive::{FromPrimitive, ToPrimitive}; @@ -32,7 +33,8 @@ pub enum Error { NetworkError(smoltcp::Error), UnexpectedPattern, UnrecognizedPacket, - + BufferExhausted, + Utf8Error(Utf8Error), } pub type Result = core::result::Result; @@ -43,6 +45,8 @@ impl fmt::Display for Error { &Error::NetworkError(error) => write!(f, "network error: {}", error), &Error::UnexpectedPattern => write!(f, "unexpected pattern"), &Error::UnrecognizedPacket => write!(f, "unrecognized packet"), + &Error::BufferExhausted => write!(f, "buffer exhausted"), + &Error::Utf8Error(error) => write!(f, "UTF-8 error: {}", error), } } } @@ -106,6 +110,21 @@ async fn read_request(stream: &TcpStream, allow_close: bool) -> Result Result> { + let length = read_i32(&stream).await? as usize; + if length > max_length { + return Err(Error::BufferExhausted); + } + let mut buffer = vec![0; length]; + read_chunk(&stream, &mut buffer).await?; + Ok(buffer) +} + +async fn read_string(stream: &TcpStream, max_length: usize) -> Result { + let bytes = read_bytes(stream, max_length).await?; + Ok(String::from_utf8(bytes).map_err(|err| Error::Utf8Error(err.utf8_error()))?) +} + async fn handle_run_kernel(stream: &TcpStream, control: &mut kernel::Control) -> Result<()> { control.tx.async_send(kernel::Message::StartRequest).await; loop { @@ -137,7 +156,24 @@ async fn handle_run_kernel(stream: &TcpStream, control: &mut kernel::Control) -> }).await?; control.tx.async_send(kernel::Message::RpcRecvReply(Ok(0))).await; }, - Request::RPCException => unimplemented!(), + Request::RPCException => { + let rpc_recv_request = control.rx.async_recv().await; + match *rpc_recv_request { + kernel::Message::RpcRecvRequest(_) => (), + _ => panic!( + "expected (ignored) root value slot from kernel CPU, not {:?}", rpc_recv_request), + } + let name = read_string(stream, 16384).await?; + let message = read_string(stream, 16384).await?; + let param = [read_i64(stream).await?, + read_i64(stream).await?, + read_i64(stream).await?]; + let file = read_string(stream, 16384).await?; + let line = read_i32(stream).await?; + let column = read_i32(stream).await?; + let function = read_string(stream, 16384).await?; + control.tx.async_send(kernel::Message::RpcRecvReply(Err(()))).await; + }, _ => { error!("unexpected RPC request from host: {:?}", host_request); return Err(Error::UnrecognizedPacket) diff --git a/src/runtime/src/proto_async.rs b/src/runtime/src/proto_async.rs index bd53d07..3e33db7 100644 --- a/src/runtime/src/proto_async.rs +++ b/src/runtime/src/proto_async.rs @@ -1,7 +1,6 @@ use core::task::Poll; use core::cmp::min; use core::cell::RefCell; -use alloc::{vec, vec::Vec}; use libboard_zynq::smoltcp; use libasync::smoltcp::TcpStream; @@ -87,16 +86,6 @@ pub async fn read_chunk(stream: &TcpStream, destination: &mut [u8]) -> Result<() Ok(()) } -pub async fn read_bytes(stream: &TcpStream, max_length: usize) -> Result> { - let length = read_i32(&stream).await? as usize; - if length > max_length { - return Err(smoltcp::Error::Exhausted); - } - let mut buffer = vec![0; length]; - read_chunk(&stream, &mut buffer).await?; - Ok(buffer) -} - pub async fn write_i8(stream: &TcpStream, value: i8) -> Result<()> { stream.send([value as u8].iter().copied()).await?; Ok(())