diff --git a/Cargo.lock b/Cargo.lock index aa00b870319..a4a2d739a93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1152,8 +1152,8 @@ name = "oak_abi" version = "0.1.0" dependencies = [ "oak_utils 0.1.0", - "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", - "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "prost 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "prost-build 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/examples/abitest/module_0/rust/src/lib.rs b/examples/abitest/module_0/rust/src/lib.rs index 53024914ecd..70bfc29c785 100644 --- a/examples/abitest/module_0/rust/src/lib.rs +++ b/examples/abitest/module_0/rust/src/lib.rs @@ -303,11 +303,11 @@ impl FrontendNode { let mut read = 0u64; unsafe { expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_create(invalid_raw_offset(), &mut read as *mut u64) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_create(&mut write as *mut u64, invalid_raw_offset()) ); } @@ -336,14 +336,11 @@ impl FrontendNode { let (w, r, _) = channel_create_raw(); unsafe { - expect_eq!(OakStatus::OK.value() as u32, oak_abi::channel_close(w)); - expect_eq!(OakStatus::OK.value() as u32, oak_abi::channel_close(r)); + expect_eq!(OakStatus::Ok as u32, oak_abi::channel_close(w)); + expect_eq!(OakStatus::Ok as u32, oak_abi::channel_close(r)); + expect_eq!(OakStatus::ErrBadHandle as u32, oak_abi::channel_close(w)); expect_eq!( - OakStatus::ERR_BAD_HANDLE.value() as u32, - oak_abi::channel_close(w) - ); - expect_eq!( - OakStatus::ERR_BAD_HANDLE.value() as u32, + OakStatus::ErrBadHandle as u32, oak_abi::channel_close(9_999_999) ); } @@ -354,9 +351,9 @@ impl FrontendNode { let (w, r) = oak::channel_create().unwrap(); expect_eq!(Ok(()), oak::channel_close(w.handle)); expect_eq!(Ok(()), oak::channel_close(r.handle)); - expect_eq!(Err(OakStatus::ERR_BAD_HANDLE), oak::channel_close(w.handle)); + expect_eq!(Err(OakStatus::ErrBadHandle), oak::channel_close(w.handle)); expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_close(oak::Handle::from_raw(9_999_999)) ); @@ -377,7 +374,7 @@ impl FrontendNode { unsafe { // Try invalid values for the 4 linear memory offset arguments. expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_read( in_channel, invalid_raw_offset() as *mut u8, @@ -389,7 +386,7 @@ impl FrontendNode { ) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_read( in_channel, buf.as_mut_ptr(), @@ -401,7 +398,7 @@ impl FrontendNode { ) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_read( in_channel, buf.as_mut_ptr(), @@ -413,7 +410,7 @@ impl FrontendNode { ) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_read( in_channel, buf.as_mut_ptr(), @@ -427,7 +424,7 @@ impl FrontendNode { // Valid case. expect_eq!( - OakStatus::ERR_CHANNEL_EMPTY.value() as u32, + OakStatus::ErrChannelEmpty as u32, oak_abi::channel_read( in_channel, buf.as_mut_ptr(), @@ -441,10 +438,10 @@ impl FrontendNode { expect_eq!(0, actual_size); expect_eq!(0, actual_handle_count); } - expect_eq!(OakStatus::OK.value() as u32, unsafe { + expect_eq!(OakStatus::Ok as u32, unsafe { oak_abi::channel_close(out_channel) }); - expect_eq!(OakStatus::OK.value() as u32, unsafe { + expect_eq!(OakStatus::Ok as u32, unsafe { oak_abi::channel_close(in_channel) }); Ok(()) @@ -457,7 +454,7 @@ impl FrontendNode { let mut buffer = Vec::::with_capacity(5); let mut handles = Vec::with_capacity(1); expect_eq!( - Err(OakStatus::ERR_CHANNEL_EMPTY), + Err(OakStatus::ErrChannelEmpty), oak::channel_read(in_channel, &mut buffer, &mut handles) ); expect_eq!(0, buffer.len()); @@ -467,7 +464,7 @@ impl FrontendNode { let mut nonempty_buffer = vec![0x91, 0x92, 0x93]; let mut nonempty_handles = vec![out_channel.handle]; expect_eq!( - Err(OakStatus::ERR_CHANNEL_EMPTY), + Err(OakStatus::ErrChannelEmpty), oak::channel_read(in_channel, &mut nonempty_buffer, &mut nonempty_handles) ); expect_eq!(0, nonempty_buffer.len()); @@ -485,7 +482,7 @@ impl FrontendNode { // Reading again zeroes the vector length. expect_eq!( - Err(OakStatus::ERR_CHANNEL_EMPTY), + Err(OakStatus::ErrChannelEmpty), oak::channel_read(in_channel, &mut buffer, &mut handles) ); expect_eq!(0, buffer.len()); @@ -508,7 +505,7 @@ impl FrontendNode { handle: oak::Handle::from_raw(99999), }; expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_read(bogus_channel, &mut buffer, &mut handles) ); @@ -539,7 +536,7 @@ impl FrontendNode { // Reading again clears the buffer and the handles. expect_eq!( - Err(OakStatus::ERR_CHANNEL_EMPTY), + Err(OakStatus::ErrChannelEmpty), oak::channel_read(in_channel, &mut buffer, &mut handles) ); expect_eq!(0, buffer.len()); @@ -560,7 +557,7 @@ impl FrontendNode { let mut buffer = Vec::::with_capacity(5); let mut handles = Vec::with_capacity(5); expect_eq!( - Err(OakStatus::ERR_CHANNEL_CLOSED), + Err(OakStatus::ErrChannelClosed), oak::channel_read(in_channel, &mut buffer, &mut handles) ); @@ -575,7 +572,7 @@ impl FrontendNode { let handles = vec![in_channel]; unsafe { expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_write( out_channel, invalid_raw_offset() as *const u8, @@ -585,7 +582,7 @@ impl FrontendNode { ) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::channel_write( out_channel, buf.as_ptr(), @@ -595,10 +592,10 @@ impl FrontendNode { ) ); } - expect_eq!(OakStatus::OK.value() as u32, unsafe { + expect_eq!(OakStatus::Ok as u32, unsafe { oak_abi::channel_close(in_channel) }); - expect_eq!(OakStatus::OK.value() as u32, unsafe { + expect_eq!(OakStatus::Ok as u32, unsafe { oak_abi::channel_close(out_channel) }); Ok(()) @@ -624,7 +621,7 @@ impl FrontendNode { handle: oak::Handle::from_raw(99999), }; expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_write(bogus_channel, &data, &[]) ); @@ -642,7 +639,7 @@ impl FrontendNode { // There's no way to read from the channel, so writing fails. let data = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]; expect_eq!( - Err(OakStatus::ERR_CHANNEL_CLOSED), + Err(OakStatus::ErrChannelClosed), oak::channel_write(out_channel, &data, &[]) ); @@ -658,12 +655,12 @@ impl FrontendNode { // Write a message to the channel so wait operations don't block. let data = vec![0x01, 0x02, 0x03]; expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::channel_write(out_channel, data.as_ptr(), data.len(), &[] as *const u8, 0) ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::wait_on_channels(invalid_raw_offset() as *mut u8, 1) ); } @@ -686,19 +683,16 @@ impl FrontendNode { space.push(0x00); expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::wait_on_channels(space.as_mut_ptr(), COUNT as u32) ); expect_eq!( - ChannelReadStatus::INVALID_CHANNEL.value(), + ChannelReadStatus::InvalidChannel as i32, i32::from(space[8]) ); + expect_eq!(ChannelReadStatus::ReadReady as i32, i32::from(space[9 + 8])); expect_eq!( - ChannelReadStatus::READ_READY.value(), - i32::from(space[9 + 8]) - ); - expect_eq!( - ChannelReadStatus::NOT_READY.value(), + ChannelReadStatus::NotReady as i32, i32::from(space[9 + 9 + 8]) ); } @@ -716,21 +710,15 @@ impl FrontendNode { .unwrap(); space.push(0x00); expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::wait_on_channels(space.as_mut_ptr(), COUNT as u32) ); expect_eq!( - ChannelReadStatus::INVALID_CHANNEL.value(), + ChannelReadStatus::InvalidChannel as i32, i32::from(space[8]) ); - expect_eq!( - ChannelReadStatus::READ_READY.value(), - i32::from(space[9 + 8]) - ); - expect_eq!( - OakStatus::OK.value() as u32, - oak_abi::channel_close(out_channel) - ); + expect_eq!(ChannelReadStatus::ReadReady as i32, i32::from(space[9 + 8])); + expect_eq!(OakStatus::Ok as u32, oak_abi::channel_close(out_channel)); } // Still a pending message on in_channel even though the only write half for @@ -743,10 +731,10 @@ impl FrontendNode { .unwrap(); space.push(0x00); expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::wait_on_channels(space.as_mut_ptr(), COUNT as u32) ); - expect_eq!(ChannelReadStatus::READ_READY.value(), i32::from(space[8])); + expect_eq!(ChannelReadStatus::ReadReady as i32, i32::from(space[8])); } // Consume the pending message. let mut buffer = Vec::with_capacity(5); @@ -755,7 +743,7 @@ impl FrontendNode { let mut recv_handles = 0u32; unsafe { expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::channel_read( in_channel, buffer.as_mut_ptr() as *mut u8, @@ -779,24 +767,21 @@ impl FrontendNode { .unwrap(); space.push(0x00); expect_eq!( - OakStatus::ERR_BAD_HANDLE.value() as u32, + OakStatus::ErrBadHandle as u32, oak_abi::wait_on_channels(space.as_mut_ptr(), COUNT as u32) ); - expect_eq!(ChannelReadStatus::ORPHANED.value(), i32::from(space[8])); + expect_eq!(ChannelReadStatus::Orphaned as i32, i32::from(space[8])); - expect_eq!( - OakStatus::OK.value() as u32, - oak_abi::channel_close(in_channel) - ); + expect_eq!(OakStatus::Ok as u32, oak_abi::channel_close(in_channel)); } unsafe { expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::channel_close(in_empty_channel) ); expect_eq!( - OakStatus::OK.value() as u32, + OakStatus::Ok as u32, oak_abi::channel_close(out_empty_channel) ); } @@ -810,7 +795,7 @@ impl FrontendNode { // Waiting on (just) non-read channel handles should fail immediately. expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::wait_on_channels(&[ oak::ReadHandle { handle: out1.handle @@ -826,18 +811,18 @@ impl FrontendNode { expect_eq!(Ok(()), oak::channel_write(out1, &data, &[])); expect_eq!( - vec![ChannelReadStatus::READ_READY, ChannelReadStatus::NOT_READY], + vec![ChannelReadStatus::ReadReady, ChannelReadStatus::NotReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); // No read so still ready (level triggered not edge triggered). expect_eq!( - vec![ChannelReadStatus::READ_READY, ChannelReadStatus::NOT_READY], + vec![ChannelReadStatus::ReadReady, ChannelReadStatus::NotReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); expect_eq!(Ok(()), oak::channel_write(out2, &data, &[])); expect_eq!( - vec![ChannelReadStatus::READ_READY, ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::ReadReady, ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); @@ -848,16 +833,16 @@ impl FrontendNode { expect_eq!(0, handles.len()); expect_eq!( - vec![ChannelReadStatus::NOT_READY, ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::NotReady, ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); // Write channels and nonsense handles are ignored. expect_eq!( vec![ - ChannelReadStatus::NOT_READY, - ChannelReadStatus::READ_READY, - ChannelReadStatus::INVALID_CHANNEL + ChannelReadStatus::NotReady, + ChannelReadStatus::ReadReady, + ChannelReadStatus::InvalidChannel ], status_convert(oak::wait_on_channels(&[ in1, @@ -869,9 +854,9 @@ impl FrontendNode { ); expect_eq!( vec![ - ChannelReadStatus::NOT_READY, - ChannelReadStatus::READ_READY, - ChannelReadStatus::INVALID_CHANNEL + ChannelReadStatus::NotReady, + ChannelReadStatus::ReadReady, + ChannelReadStatus::InvalidChannel ], status_convert(oak::wait_on_channels(&[ in1, @@ -888,7 +873,7 @@ impl FrontendNode { // Still a pending message on in2 even though the only write half for // the channel is closed. expect_eq!( - vec![ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in2]))? ); @@ -908,7 +893,7 @@ impl FrontendNode { expect_eq!(Ok(()), oak::channel_write(out1, &data, &[])); expect_eq!(Ok(()), oak::channel_write(out2, &data, &[])); expect_eq!( - vec![ChannelReadStatus::READ_READY, ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::ReadReady, ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); @@ -917,7 +902,7 @@ impl FrontendNode { // Channel is still read-ready because there's a queued message. expect_eq!( - vec![ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in1]))? ); @@ -930,7 +915,7 @@ impl FrontendNode { // Now expect the channel status to be orphaned. expect_eq!( - vec![ChannelReadStatus::ORPHANED, ChannelReadStatus::READ_READY], + vec![ChannelReadStatus::Orphaned, ChannelReadStatus::ReadReady], status_convert(oak::wait_on_channels(&[in1, in2]))? ); @@ -947,7 +932,7 @@ impl FrontendNode { let non_utf8_name: Vec = vec![0xc3, 0x28]; unsafe { expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::node_create( invalid_raw_offset() as *mut u8, 1, @@ -958,7 +943,7 @@ impl FrontendNode { ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::node_create( non_utf8_name.as_ptr(), non_utf8_name.len(), @@ -969,7 +954,7 @@ impl FrontendNode { ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::node_create( valid.as_ptr(), valid.len(), @@ -980,7 +965,7 @@ impl FrontendNode { ); expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::node_create( valid.as_ptr(), valid.len(), @@ -994,7 +979,7 @@ impl FrontendNode { } fn test_node_create(&mut self) -> TestResult { expect_eq!( - Err(OakStatus::ERR_INVALID_ARGS), + Err(OakStatus::ErrInvalidArgs), oak::node_create( "no-such-config", BACKEND_ENTRYPOINT_NAME, @@ -1002,7 +987,7 @@ impl FrontendNode { ) ); expect_eq!( - Err(OakStatus::ERR_INVALID_ARGS), + Err(OakStatus::ErrInvalidArgs), oak::node_create( BACKEND_CONFIG_NAME, "no-such-entrypoint", @@ -1010,7 +995,7 @@ impl FrontendNode { ) ); expect_eq!( - Err(OakStatus::ERR_INVALID_ARGS), + Err(OakStatus::ErrInvalidArgs), oak::node_create( BACKEND_CONFIG_NAME, "backend_fake_main", // exists but wrong signature @@ -1018,7 +1003,7 @@ impl FrontendNode { ) ); expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::node_create( BACKEND_CONFIG_NAME, BACKEND_ENTRYPOINT_NAME, @@ -1045,7 +1030,7 @@ impl FrontendNode { fn test_random_get_raw(&mut self) -> TestResult { unsafe { expect_eq!( - OakStatus::ERR_INVALID_ARGS.value() as u32, + OakStatus::ErrInvalidArgs as u32, oak_abi::random_get(invalid_raw_offset() as *mut u8, 1) ); } @@ -1080,7 +1065,7 @@ impl FrontendNode { let mut buffer = Vec::::with_capacity(5); let mut handles = Vec::with_capacity(5); expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_read( oak::ReadHandle { handle: oak::Handle::from_raw(9_987_123) @@ -1092,8 +1077,8 @@ impl FrontendNode { // Wait on an invalid handle. expect_eq!( Ok(vec![ - ChannelReadStatus::READ_READY, - ChannelReadStatus::INVALID_CHANNEL + ChannelReadStatus::ReadReady, + ChannelReadStatus::InvalidChannel ]), oak::wait_on_channels(&[ in_handle, @@ -1105,11 +1090,11 @@ impl FrontendNode { // Close both of the previously mentioned invalid handles. expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_close(oak::Handle::from_raw(9_987_123)) ); expect_eq!( - Err(OakStatus::ERR_BAD_HANDLE), + Err(OakStatus::ErrBadHandle), oak::channel_close(oak::Handle::from_raw(9_987_321)) ); @@ -1191,7 +1176,7 @@ impl FrontendNode { handle: oak::Handle::from_raw(0), }; for (j, ready) in readies.iter().enumerate() { - if *ready == ChannelReadStatus::READ_READY { + if *ready == ChannelReadStatus::ReadReady { info!("got response from backend[{}]", j); expect_eq!(oak::Handle::from_raw(0), new_in_channel.handle); let mut handles = Vec::with_capacity(1); diff --git a/examples/abitest/module_1/rust/src/lib.rs b/examples/abitest/module_1/rust/src/lib.rs index 2781681a6eb..851b7f3049e 100644 --- a/examples/abitest/module_1/rust/src/lib.rs +++ b/examples/abitest/module_1/rust/src/lib.rs @@ -30,7 +30,7 @@ pub extern "C" fn backend_oak_main(handle: u64) { oak::set_panic_hook(); match inner_main(handle) { - Err(oak::OakStatus::ERR_TERMINATED) => { + Err(oak::OakStatus::ErrTerminated) => { info!("node terminated"); } Err(s) => { @@ -53,7 +53,7 @@ fn inner_main(in_handle: u64) -> Result<(), oak::OakStatus> { let mut handles = Vec::with_capacity(2); oak::channel_read(in_channel, &mut buf, &mut handles)?; if handles.len() != 1 { - return Err(oak::OakStatus::ERR_INTERNAL); + return Err(oak::OakStatus::ErrInternal); } let out_handle = handles[0]; info!("backend node: in={:?}, out={:?}", in_channel, out_handle); @@ -65,7 +65,7 @@ fn inner_main(in_handle: u64) -> Result<(), oak::OakStatus> { let ready_status = oak::wait_on_channels(&wait_handles)?; // If there is a message on in_channel, it is expected to contain // a collection of read handles for future listening - if ready_status[0] == oak::ChannelReadStatus::READ_READY { + if ready_status[0] == oak::ChannelReadStatus::ReadReady { let mut buf = Vec::::with_capacity(16); let mut handles = Vec::with_capacity(5); oak::channel_read(in_channel, &mut buf, &mut handles)?; @@ -79,8 +79,8 @@ fn inner_main(in_handle: u64) -> Result<(), oak::OakStatus> { // messages. let mut orphaned_handles = HashSet::new(); for i in 1..ready_status.len() { - if ready_status[i] != oak::ChannelReadStatus::READ_READY { - if ready_status[i] == oak::ChannelReadStatus::ORPHANED { + if ready_status[i] != oak::ChannelReadStatus::ReadReady { + if ready_status[i] == oak::ChannelReadStatus::Orphaned { let orphan_handle = wait_handles[i].handle; orphaned_handles.insert(orphan_handle); info!("close orphaned channel[{}]={:?}", i, orphan_handle); @@ -96,8 +96,7 @@ fn inner_main(in_handle: u64) -> Result<(), oak::OakStatus> { let mut handles = Vec::with_capacity(1); oak::channel_read(wait_handles[i], &mut buf, &mut handles).or_else(|err| { - if err == oak::OakStatus::ERR_CHANNEL_CLOSED - || err == oak::OakStatus::ERR_CHANNEL_EMPTY + if err == oak::OakStatus::ErrChannelClosed || err == oak::OakStatus::ErrChannelEmpty { // Multiple backend Nodes are attempting to read the message from // the channel, so it's entirely possible that one of them has diff --git a/examples/abitest/tests/src/lib.rs b/examples/abitest/tests/src/lib.rs index 6817bc1c42a..c766d1df429 100644 --- a/examples/abitest/tests/src/lib.rs +++ b/examples/abitest/tests/src/lib.rs @@ -15,7 +15,6 @@ // use log::error; -use protobuf::ProtobufEnum; #[cfg(test)] mod tests; @@ -23,5 +22,5 @@ mod tests; #[no_mangle] pub extern "C" fn oak_main(_handle: u64) -> i32 { error!("Dummy oak_main invoked"); - oak::OakStatus::ERR_TERMINATED.value() + oak::OakStatus::ErrTerminated as i32 } diff --git a/examples/chat/module/rust/src/command.rs b/examples/chat/module/rust/src/command.rs index ad975ade9da..d574a3579d9 100644 --- a/examples/chat/module/rust/src/command.rs +++ b/examples/chat/module/rust/src/command.rs @@ -36,7 +36,7 @@ pub enum Command { impl oak::io::Encodable for Command { fn encode(&self) -> Result { // TODO: Propagate more details about the source error. - let bytes = bincode::serialize(self).map_err(|_| oak::OakStatus::ERR_INVALID_ARGS)?; + let bytes = bincode::serialize(self).map_err(|_| oak::OakStatus::ErrInvalidArgs)?; // Serialize handles separately. let handles = match self { Command::Join(h) => vec![h.handle], @@ -51,7 +51,7 @@ impl oak::io::Decodable for Command { fn decode(message: &oak::io::Message) -> Result { // TODO: Propagate more details about the source error. let command: Command = - bincode::deserialize(&message.bytes).map_err(|_| oak::OakStatus::ERR_INVALID_ARGS)?; + bincode::deserialize(&message.bytes).map_err(|_| oak::OakStatus::ErrInvalidArgs)?; // Restore handles in the received message. let command = match command { Command::Join(_) => Command::Join(oak::WriteHandle { diff --git a/oak/server/rust/oak_abi/Cargo.toml b/oak/server/rust/oak_abi/Cargo.toml index 528fae3d8f2..b38b326791f 100644 --- a/oak/server/rust/oak_abi/Cargo.toml +++ b/oak/server/rust/oak_abi/Cargo.toml @@ -5,14 +5,9 @@ authors = ["David Drysdale "] edition = "2018" license = "Apache-2.0" -[features] -std = ["protobuf"] -no_std = [] -default = ["std"] - [dependencies] -protobuf = { version = "*", optional = true } +prost = "*" [build-dependencies] oak_utils = "*" -protoc-rust = "*" +prost-build = "*" diff --git a/oak/server/rust/oak_abi/build.rs b/oak/server/rust/oak_abi/build.rs index 6fe24804379..8018ba46922 100644 --- a/oak/server/rust/oak_abi/build.rs +++ b/oak/server/rust/oak_abi/build.rs @@ -14,12 +14,15 @@ // limitations under the License. // +use std::path; + fn main() { - oak_utils::run_protoc_rust(protoc_rust::Args { - out_dir: "src/proto", - input: &["../../../../oak/proto/oak_api.proto"], - includes: &["../../../../oak/proto"], - customize: protoc_rust::Customize::default(), - }) - .expect("protoc"); + let proto_dir = path::Path::new("../../../../oak/proto/"); + let oak_api_path = &*proto_dir.join("oak_api.proto"); + + // Tell Cargo that if the given file changes, to rerun this build script. + // https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed + println!("cargo:rerun-if-changed={}", oak_api_path.to_str().unwrap()); + + prost_build::compile_protos(&[oak_api_path], &[proto_dir]).unwrap(); } diff --git a/oak/server/rust/oak_abi/src/lib.rs b/oak/server/rust/oak_abi/src/lib.rs index 531684d7c9d..419c26080b5 100644 --- a/oak/server/rust/oak_abi/src/lib.rs +++ b/oak/server/rust/oak_abi/src/lib.rs @@ -15,48 +15,8 @@ // //! Type, constant and Wasm host function definitions for the Oak application binary interface. - -// TODO(#638): Generate from protobuf in a no_std compatible way -#[cfg(feature = "std")] pub mod proto; -#[cfg(feature = "std")] -mod inner { - pub use super::proto::oak_api::{ChannelReadStatus, OakStatus}; -} - -#[cfg(feature = "no_std")] -mod inner { - #![allow(dead_code)] - #![allow(missing_docs)] - #![allow(non_camel_case_types)] - #![allow(non_snake_case)] - #![allow(non_upper_case_globals)] - - #[derive(Clone, PartialEq, Eq, Debug, Hash)] - pub enum OakStatus { - OAK_STATUS_UNSPECIFIED = 0, - OK = 1, - ERR_BAD_HANDLE = 2, - ERR_INVALID_ARGS = 3, - ERR_CHANNEL_CLOSED = 4, - ERR_BUFFER_TOO_SMALL = 5, - ERR_HANDLE_SPACE_TOO_SMALL = 6, - ERR_OUT_OF_RANGE = 7, - ERR_INTERNAL = 8, - ERR_TERMINATED = 9, - ERR_CHANNEL_EMPTY = 10, - } - - #[derive(Clone, PartialEq, Eq, Debug, Hash)] - pub enum ChannelReadStatus { - NOT_READY = 0, - READ_READY = 1, - INVALID_CHANNEL = 2, - ORPHANED = 3, - } -} - -pub use inner::*; +pub use proto::*; /// Handle used to identify read or write channel halves. /// @@ -103,17 +63,17 @@ extern "C" { /// If the provided spaces for data (`buf` plus `size`) or handles /// (`handle_buf` plus 8 x `handle_count`) are not large enough for the read /// operation, then no data will be returned and either - /// [`ERR_BUFFER_TOO_SMALL`] or [`ERR_HANDLE_SPACE_TOO_SMALL`] will be + /// [`ErrBufferTooSmall`] or [`ErrHandleSpaceTooSmall`] will be /// returned. In either case, the required sizes will be returned in the /// spaces provided by `actual_size` and `actual_handle_count`. /// /// Returns the status of the operation, as an [`OakStatus`] value. - /// If no message is available on the channel, [`ERR_CHANNEL_EMPTY`] will be + /// If no message is available on the channel, [`ErrChannelEmpty`] will be /// returned. /// - /// [`ERR_BUFFER_TOO_SMALL`]: crate::OakStatus::ERR_BUFFER_TOO_SMALL - /// [`ERR_CHANNEL_EMPTY`]: crate::OakStatus::ERR_CHANNEL_EMPTY - /// [`ERR_HANDLE_SPACE_TOO_SMALL`]: crate::OakStatus::ERR_HANDLE_SPACE_TOO_SMALL + /// [`ErrBufferTooSmall`]: crate::OakStatus::ErrBufferTooSmall + /// [`ErrChannelEmpty`]: crate::OakStatus::ErrChannelEmpty + /// [`ErrHandleSpaceTooSmall`]: crate::OakStatus::ErrHandleSpaceTooSmall /// [`OakStatus`]: crate::OakStatus pub fn channel_read( handle: u64, diff --git a/oak/server/rust/oak_abi/src/proto/mod.rs b/oak/server/rust/oak_abi/src/proto.rs similarity index 92% rename from oak/server/rust/oak_abi/src/proto/mod.rs rename to oak/server/rust/oak_abi/src/proto.rs index 25c877eddfb..fe87d118f2a 100644 --- a/oak/server/rust/oak_abi/src/proto/mod.rs +++ b/oak/server/rust/oak_abi/src/proto.rs @@ -14,5 +14,4 @@ // limitations under the License. // -#[allow(clippy::all)] -pub mod oak_api; +include!(concat!(env!("OUT_DIR"), "/oak.rs")); diff --git a/oak/server/rust/oak_abi/src/proto/oak_api.rs b/oak/server/rust/oak_abi/src/proto/oak_api.rs deleted file mode 100644 index 5805ecf8297..00000000000 --- a/oak/server/rust/oak_abi/src/proto/oak_api.rs +++ /dev/null @@ -1,199 +0,0 @@ -// This file is generated by rust-protobuf 2.10.1. Do not edit -// @generated - -// https://github.com/rust-lang/rust-clippy/issues/702 -#![allow(unknown_lints)] -#![allow(clippy::all)] - -#![cfg_attr(rustfmt, rustfmt_skip)] - -#![allow(box_pointers)] -#![allow(dead_code)] -#![allow(missing_docs)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(trivial_casts)] -#![allow(unsafe_code)] -#![allow(unused_imports)] -#![allow(unused_results)] -//! Generated file from `oak_api.proto` - -use protobuf::Message as Message_imported_for_functions; -use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions; - -/// Generated files are compatible only with the same version -/// of protobuf runtime. -// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_10_1; - -#[derive(Clone,PartialEq,Eq,Debug,Hash)] -pub enum OakStatus { - OAK_STATUS_UNSPECIFIED = 0, - OK = 1, - ERR_BAD_HANDLE = 2, - ERR_INVALID_ARGS = 3, - ERR_CHANNEL_CLOSED = 4, - ERR_BUFFER_TOO_SMALL = 5, - ERR_HANDLE_SPACE_TOO_SMALL = 6, - ERR_OUT_OF_RANGE = 7, - ERR_INTERNAL = 8, - ERR_TERMINATED = 9, - ERR_CHANNEL_EMPTY = 10, -} - -impl ::protobuf::ProtobufEnum for OakStatus { - fn value(&self) -> i32 { - *self as i32 - } - - fn from_i32(value: i32) -> ::std::option::Option { - match value { - 0 => ::std::option::Option::Some(OakStatus::OAK_STATUS_UNSPECIFIED), - 1 => ::std::option::Option::Some(OakStatus::OK), - 2 => ::std::option::Option::Some(OakStatus::ERR_BAD_HANDLE), - 3 => ::std::option::Option::Some(OakStatus::ERR_INVALID_ARGS), - 4 => ::std::option::Option::Some(OakStatus::ERR_CHANNEL_CLOSED), - 5 => ::std::option::Option::Some(OakStatus::ERR_BUFFER_TOO_SMALL), - 6 => ::std::option::Option::Some(OakStatus::ERR_HANDLE_SPACE_TOO_SMALL), - 7 => ::std::option::Option::Some(OakStatus::ERR_OUT_OF_RANGE), - 8 => ::std::option::Option::Some(OakStatus::ERR_INTERNAL), - 9 => ::std::option::Option::Some(OakStatus::ERR_TERMINATED), - 10 => ::std::option::Option::Some(OakStatus::ERR_CHANNEL_EMPTY), - _ => ::std::option::Option::None - } - } - - fn values() -> &'static [Self] { - static values: &'static [OakStatus] = &[ - OakStatus::OAK_STATUS_UNSPECIFIED, - OakStatus::OK, - OakStatus::ERR_BAD_HANDLE, - OakStatus::ERR_INVALID_ARGS, - OakStatus::ERR_CHANNEL_CLOSED, - OakStatus::ERR_BUFFER_TOO_SMALL, - OakStatus::ERR_HANDLE_SPACE_TOO_SMALL, - OakStatus::ERR_OUT_OF_RANGE, - OakStatus::ERR_INTERNAL, - OakStatus::ERR_TERMINATED, - OakStatus::ERR_CHANNEL_EMPTY, - ]; - values - } - - fn enum_descriptor_static() -> &'static ::protobuf::reflect::EnumDescriptor { - static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::EnumDescriptor> = ::protobuf::lazy::Lazy { - lock: ::protobuf::lazy::ONCE_INIT, - ptr: 0 as *const ::protobuf::reflect::EnumDescriptor, - }; - unsafe { - descriptor.get(|| { - ::protobuf::reflect::EnumDescriptor::new("OakStatus", file_descriptor_proto()) - }) - } - } -} - -impl ::std::marker::Copy for OakStatus { -} - -impl ::std::default::Default for OakStatus { - fn default() -> Self { - OakStatus::OAK_STATUS_UNSPECIFIED - } -} - -impl ::protobuf::reflect::ProtobufValue for OakStatus { - fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef { - ::protobuf::reflect::ProtobufValueRef::Enum(self.descriptor()) - } -} - -#[derive(Clone,PartialEq,Eq,Debug,Hash)] -pub enum ChannelReadStatus { - NOT_READY = 0, - READ_READY = 1, - INVALID_CHANNEL = 2, - ORPHANED = 3, -} - -impl ::protobuf::ProtobufEnum for ChannelReadStatus { - fn value(&self) -> i32 { - *self as i32 - } - - fn from_i32(value: i32) -> ::std::option::Option { - match value { - 0 => ::std::option::Option::Some(ChannelReadStatus::NOT_READY), - 1 => ::std::option::Option::Some(ChannelReadStatus::READ_READY), - 2 => ::std::option::Option::Some(ChannelReadStatus::INVALID_CHANNEL), - 3 => ::std::option::Option::Some(ChannelReadStatus::ORPHANED), - _ => ::std::option::Option::None - } - } - - fn values() -> &'static [Self] { - static values: &'static [ChannelReadStatus] = &[ - ChannelReadStatus::NOT_READY, - ChannelReadStatus::READ_READY, - ChannelReadStatus::INVALID_CHANNEL, - ChannelReadStatus::ORPHANED, - ]; - values - } - - fn enum_descriptor_static() -> &'static ::protobuf::reflect::EnumDescriptor { - static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::EnumDescriptor> = ::protobuf::lazy::Lazy { - lock: ::protobuf::lazy::ONCE_INIT, - ptr: 0 as *const ::protobuf::reflect::EnumDescriptor, - }; - unsafe { - descriptor.get(|| { - ::protobuf::reflect::EnumDescriptor::new("ChannelReadStatus", file_descriptor_proto()) - }) - } - } -} - -impl ::std::marker::Copy for ChannelReadStatus { -} - -impl ::std::default::Default for ChannelReadStatus { - fn default() -> Self { - ChannelReadStatus::NOT_READY - } -} - -impl ::protobuf::reflect::ProtobufValue for ChannelReadStatus { - fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef { - ::protobuf::reflect::ProtobufValueRef::Enum(self.descriptor()) - } -} - -static file_descriptor_proto_data: &'static [u8] = b"\ - \n\roak_api.proto\x12\x03oak*\xfe\x01\n\tOakStatus\x12\x1a\n\x16OAK_STAT\ - US_UNSPECIFIED\x10\0\x12\x06\n\x02OK\x10\x01\x12\x12\n\x0eERR_BAD_HANDLE\ - \x10\x02\x12\x14\n\x10ERR_INVALID_ARGS\x10\x03\x12\x16\n\x12ERR_CHANNEL_\ - CLOSED\x10\x04\x12\x18\n\x14ERR_BUFFER_TOO_SMALL\x10\x05\x12\x1e\n\x1aER\ - R_HANDLE_SPACE_TOO_SMALL\x10\x06\x12\x14\n\x10ERR_OUT_OF_RANGE\x10\x07\ - \x12\x10\n\x0cERR_INTERNAL\x10\x08\x12\x12\n\x0eERR_TERMINATED\x10\t\x12\ - \x15\n\x11ERR_CHANNEL_EMPTY\x10\n*U\n\x11ChannelReadStatus\x12\r\n\tNOT_\ - READY\x10\0\x12\x0e\n\nREAD_READY\x10\x01\x12\x13\n\x0fINVALID_CHANNEL\ - \x10\x02\x12\x0c\n\x08ORPHANED\x10\x03b\x06proto3\ -"; - -static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy { - lock: ::protobuf::lazy::ONCE_INIT, - ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto, -}; - -fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto { - ::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap() -} - -pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto { - unsafe { - file_descriptor_proto_lazy.get(|| { - parse_descriptor_proto() - }) - } -} diff --git a/oak/server/rust/oak_runtime/Cargo.toml b/oak/server/rust/oak_runtime/Cargo.toml index 75b19d6641c..aa1d110337e 100644 --- a/oak/server/rust/oak_runtime/Cargo.toml +++ b/oak/server/rust/oak_runtime/Cargo.toml @@ -9,15 +9,15 @@ edition = "2018" license = "Apache-2.0" [features] -std = ["no-std-compat/std", "oak_abi/std"] -no_std = ["oak_abi/no_std"] +std = ["no-std-compat/std"] +no_std = [] default = ["std"] [dependencies] byteorder = { version = "*", default-features = false } itertools = "*" log = { version = "*" } -oak_abi = { version = "=0.1.0", default-features = false } +oak_abi = "=0.1.0" protobuf = "*" rand = { version = "*" } wasmi = { version = "*", default-features = false, features = ["core"] } diff --git a/oak/server/rust/oak_runtime/src/channel.rs b/oak/server/rust/oak_runtime/src/channel.rs index ce42bc4ad33..b9e13d5441e 100644 --- a/oak/server/rust/oak_runtime/src/channel.rs +++ b/oak/server/rust/oak_runtime/src/channel.rs @@ -140,11 +140,11 @@ impl std::ops::Deref for ChannelRef { } impl ChannelWriter { - /// Write a message to a channel. Fails with `OakStatus::ERR_CHANNEL_CLOSED` if the underlying + /// Write a message to a channel. Fails with `OakStatus::ErrChannelClosed` if the underlying /// channel has been orphaned. pub fn write(&self, msg: Message) -> Result<(), OakStatus> { if self.is_orphan() { - return Err(OakStatus::ERR_CHANNEL_CLOSED); + return Err(OakStatus::ErrChannelClosed); } { @@ -205,7 +205,7 @@ pub enum ReadStatus { } impl ChannelReader { - /// Thread safe. Read a message from a channel. Fails with `OakStatus::ERR_CHANNEL_CLOSED` if + /// Thread safe. Read a message from a channel. Fails with `OakStatus::ErrChannelClosed` if /// the underlying channel is empty and has been orphaned. pub fn read(&self) -> Result, OakStatus> { let mut messages = self.messages.write().unwrap(); @@ -213,7 +213,7 @@ impl ChannelReader { Some(m) => Ok(Some(m)), None => { if self.is_orphan() { - Err(OakStatus::ERR_CHANNEL_CLOSED) + Err(OakStatus::ErrChannelClosed) } else { Ok(None) } @@ -222,22 +222,22 @@ impl ChannelReader { } /// Thread safe. This function will return: - /// - `READ_READY` if there is at least one message in the channel. - /// - `ORPHANED` if there are no messages and there are no writers + /// - `ReadReady` if there is at least one message in the channel. + /// - `Orphaned` if there are no messages and there are no writers /// - `NOT_READ` if there are no messages but there are some writers pub fn status(&self) -> ChannelReadStatus { let messages = self.messages.read().unwrap(); if messages.front().is_some() { - ChannelReadStatus::READ_READY + ChannelReadStatus::ReadReady } else if self.is_orphan() { - ChannelReadStatus::ORPHANED + ChannelReadStatus::Orphaned } else { - ChannelReadStatus::NOT_READY + ChannelReadStatus::NotReady } } /// Thread safe. Reads a message from the channel if `bytes_capacity` and `handles_capacity` are - /// large enough to accept the message. Fails with `OakStatus::ERR_CHANNEL_CLOSED` if the + /// large enough to accept the message. Fails with `OakStatus::ErrChannelClosed` if the /// underlying channel has been orphaned _and_ is empty. If there was not enough /// `bytes_capacity` or `handles_capacity`, `try_read_message` will return /// `Some(ReadStatus::NeedsCapacity(needed_bytes_capacity,needed_handles_capacity))`. Does not @@ -268,7 +268,7 @@ impl ChannelReader { } None => { if self.is_orphan() { - Err(OakStatus::ERR_CHANNEL_CLOSED) + Err(OakStatus::ErrChannelClosed) } else { Ok(None) } @@ -307,22 +307,22 @@ impl Clone for ChannelReader { } /// Reads the statuses from a slice of `Option<&ChannelReader>`s. -/// `ChannelReadStatus::INVALID_CHANNEL` is set for `None` readers in the slice. For `Some(_)` +/// `ChannelReadStatus::InvalidChannel` is set for `None` readers in the slice. For `Some(_)` /// readers, the result is set from a call to `has_message`. pub fn readers_statuses(readers: &[Option<&ChannelReader>]) -> Vec { readers .iter() - .map(|chan| chan.map_or(ChannelReadStatus::INVALID_CHANNEL, |chan| chan.status())) + .map(|chan| chan.map_or(ChannelReadStatus::InvalidChannel, |chan| chan.status())) .collect() } /// Waits on a slice of `Option<&ChannelReader>`s, blocking until one of the following conditions: -/// - If the `Runtime` is terminating this will return immediately with an `ERR_TERMINATED` status +/// - If the `Runtime` is terminating this will return immediately with an `ErrTerminated` status /// for each channel. /// - If all readers are in an erroneous status, e.g. when all `ChannelReaders` are orphaned, this /// will immediately return the channels statuses. /// - If any of the channels is able to read a message, the corresponding element in the returned -/// vector will be set to `Ok(READ_READY)`, with `Ok(NOT_READY)` signaling the channel has no +/// vector will be set to `Ok(ReadReady)`, with `Ok(NotReady)` signaling the channel has no /// message available /// /// In particular, if there is at least one channel in good status and no messages on said channel @@ -354,8 +354,8 @@ pub fn wait_on_channels( let all_unreadable = statuses .iter() - .all(|&s| s == ChannelReadStatus::INVALID_CHANNEL || s == ChannelReadStatus::ORPHANED); - let any_ready = statuses.iter().any(|&s| s == ChannelReadStatus::READ_READY); + .all(|&s| s == ChannelReadStatus::InvalidChannel || s == ChannelReadStatus::Orphaned); + let any_ready = statuses.iter().any(|&s| s == ChannelReadStatus::ReadReady); if all_unreadable || any_ready { return Ok(statuses); @@ -373,5 +373,5 @@ pub fn wait_on_channels( oak_platform::current_thread().id() ); } - Err(OakStatus::ERR_TERMINATED) + Err(OakStatus::ErrTerminated) } diff --git a/oak/server/rust/oak_runtime/src/config.rs b/oak/server/rust/oak_runtime/src/config.rs index aef3c2735df..899813c7447 100644 --- a/oak/server/rust/oak_runtime/src/config.rs +++ b/oak/server/rust/oak_runtime/src/config.rs @@ -93,7 +93,7 @@ pub fn from_protobuf( match &node.config_type { None => { error!("Node config {} with no type", node.name); - return Err(OakStatus::ERR_INVALID_ARGS); + return Err(OakStatus::ErrInvalidArgs); } Some(NodeConfiguration_oneof_config_type::log_config(_)) => { node::Configuration::LogNode @@ -102,11 +102,11 @@ pub fn from_protobuf( WebAssemblyConfiguration { module_bytes, .. }, )) => load_wasm(&module_bytes).map_err(|e| { error!("Error loading Wasm module: {}", e); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?, Some(_) => { error!("Pseudo-node not implemented!"); - return Err(OakStatus::ERR_INVALID_ARGS); + return Err(OakStatus::ErrInvalidArgs); } }, ); diff --git a/oak/server/rust/oak_runtime/src/node/wasm.rs b/oak/server/rust/oak_runtime/src/node/wasm.rs index 967ccb96d17..7e337972e6d 100644 --- a/oak/server/rust/oak_runtime/src/node/wasm.rs +++ b/oak/server/rust/oak_runtime/src/node/wasm.rs @@ -118,7 +118,7 @@ impl WasmInterface { let byte_size: wasmi::memory_units::Bytes = self.get_memory().current_size().into(); if byte_size < wasmi::memory_units::Bytes((addr as usize) + (offset as usize)) { - return Err(OakStatus::ERR_INVALID_ARGS); + return Err(OakStatus::ErrInvalidArgs); } Ok(()) @@ -159,12 +159,12 @@ impl WasmInterface { "node_create: Unable to read name from guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let config_name = String::from_utf8(config_name).map_err(|err| { error!("node_create: Unable to parse config_name: {:?}", err); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; debug!( @@ -180,12 +180,12 @@ impl WasmInterface { "node_create: Unable to read entrypoint from guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let entrypoint = String::from_utf8(entrypoint).map_err(|err| { error!("node_create: Unable to parse entrypoint: {:?}", err); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; debug!( @@ -195,7 +195,7 @@ impl WasmInterface { let channel_ref = self.readers.get(&initial_handle).ok_or(()).map_err(|_| { error!("node_create: Invalid handle"); - OakStatus::ERR_BAD_HANDLE + OakStatus::ErrBadHandle })?; self.runtime @@ -205,7 +205,7 @@ impl WasmInterface { "node_create: Config \"{}\" entrypoint \"{}\" not found", config_name, entrypoint ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs }) } @@ -230,7 +230,7 @@ impl WasmInterface { "channel_create: Unable to write writer handle into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; self.get_memory() @@ -240,7 +240,7 @@ impl WasmInterface { "channel_create: Unable to write reader handle into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs }) } @@ -256,7 +256,7 @@ impl WasmInterface { ) -> Result<(), OakStatus> { let writer = self.writers.get(&writer_handle).ok_or(()).map_err(|_| { error!("channel_write: No such handle"); - OakStatus::ERR_BAD_HANDLE + OakStatus::ErrBadHandle })?; let data = self @@ -267,7 +267,7 @@ impl WasmInterface { "channel_write: Unable to read message data from guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let raw_handles = self @@ -278,7 +278,7 @@ impl WasmInterface { "channel_write: Unable to read handles from guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let handles: Vec = raw_handles @@ -294,7 +294,7 @@ impl WasmInterface { Some(channel) => Ok(ChannelEither::Reader(channel.clone())), None => { error!("channel_write: Can't find handle {} to send", handle); - Err(OakStatus::ERR_BAD_HANDLE) + Err(OakStatus::ErrBadHandle) } }, }) @@ -327,7 +327,7 @@ impl WasmInterface { ) -> Result<(), OakStatus> { let reader = self.readers.get(&reader_handle).ok_or(()).map_err(|_| { error!("channel_read: No such handle"); - OakStatus::ERR_BAD_HANDLE + OakStatus::ErrBadHandle })?; self.validate_ptr(dest, dest_capacity)?; @@ -350,7 +350,7 @@ impl WasmInterface { "channel_read: Unable to write actual length into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let raw_writer = &mut [0; 4]; @@ -362,7 +362,7 @@ impl WasmInterface { "channel_read: Unable to write actual handle count into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; match msg { @@ -372,7 +372,7 @@ impl WasmInterface { "channel_read: Unable to write destination buffer into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let mut raw_writer: Vec = vec![0; actual_handle_count * 8]; @@ -389,15 +389,15 @@ impl WasmInterface { "channel_read: Unable to write destination buffer into guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs }) } - None => Err(OakStatus::ERR_CHANNEL_EMPTY), + None => Err(OakStatus::ErrChannelEmpty), Some(ReadStatus::NeedsCapacity(x, _)) => { if x > dest_capacity as usize { - Err(OakStatus::ERR_BUFFER_TOO_SMALL) + Err(OakStatus::ErrBufferTooSmall) } else { - Err(OakStatus::ERR_HANDLE_SPACE_TOO_SMALL) + Err(OakStatus::ErrHandleSpaceTooSmall) } } } @@ -431,7 +431,7 @@ impl WasmInterface { "wait_on_channels: Unable to read handles from guest memory: {:?}", err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; let channels: Vec> = handles_raw @@ -453,15 +453,15 @@ impl WasmInterface { "wait_on_channels: Unable to set status {} to {:?}: {:?}", i, status, err ); - OakStatus::ERR_INVALID_ARGS + OakStatus::ErrInvalidArgs })?; } if statuses .iter() - .all(|&s| s == ChannelReadStatus::INVALID_CHANNEL || s == ChannelReadStatus::ORPHANED) + .all(|&s| s == ChannelReadStatus::InvalidChannel || s == ChannelReadStatus::Orphaned) { - Err(OakStatus::ERR_BAD_HANDLE) + Err(OakStatus::ErrBadHandle) } else { Ok(()) } @@ -472,14 +472,14 @@ impl WasmInterface { /// `wasmi` specific result type `Result, wasmi::Trap>`. /// /// This maps`Result<(), OakStatus>` to `Ok(Some())` where -/// - `Ok(())` to `OakStatus::OK` +/// - `Ok(())` to `OakStatus::Ok` /// - `Err(x)` to x fn map_host_errors( result: Result<(), OakStatus>, ) -> Result, wasmi::Trap> { Ok(Some(wasmi::RuntimeValue::I32(result.map_or_else( |x: OakStatus| x as i32, - |_| OakStatus::OK as i32, + |_| OakStatus::Ok as i32, )))) } @@ -517,7 +517,7 @@ impl wasmi::Externals for WasmInterface { if self.runtime.is_terminating() { debug!("{} returning terminated", self.pretty_name); return Ok(Some(wasmi::RuntimeValue::I32( - OakStatus::ERR_TERMINATED as i32, + OakStatus::ErrTerminated as i32, ))); } @@ -548,9 +548,9 @@ impl wasmi::Externals for WasmInterface { if self.readers.remove(&channel_id).is_some() || self.writers.remove(&channel_id).is_some() { - OakStatus::OK as i32 + OakStatus::Ok as i32 } else { - OakStatus::ERR_BAD_HANDLE as i32 + OakStatus::ErrBadHandle as i32 }, ))) } @@ -567,7 +567,7 @@ impl wasmi::Externals for WasmInterface { if self.runtime.is_terminating() { debug!("{} returning terminated", self.pretty_name); return Ok(Some(wasmi::RuntimeValue::I32( - OakStatus::ERR_TERMINATED as i32, + OakStatus::ErrTerminated as i32, ))); } @@ -727,7 +727,7 @@ impl wasmi::ModuleImportResolver for WasmInterface { } /// Create a new instance of a Wasm node. If the entry point is not found, -/// `ERR(OakStatus::ERR_INVALID_ARGS)` will be returned immediately. +/// `ERR(OakStatus::ErrInvalidArgs)` will be returned immediately. pub fn new_instance( config_name: &str, runtime: RuntimeRef, @@ -759,7 +759,7 @@ pub fn new_instance( .and_then(|e| e.as_func().map(|f| f.signature() != &entrypoint_sig)) .unwrap_or(true) { - return Err(OakStatus::ERR_INVALID_ARGS); + return Err(OakStatus::ErrInvalidArgs); } } diff --git a/oak/server/rust/oak_runtime/src/runtime.rs b/oak/server/rust/oak_runtime/src/runtime.rs index ed90a2ebf1f..486087a711b 100644 --- a/oak/server/rust/oak_runtime/src/runtime.rs +++ b/oak/server/rust/oak_runtime/src/runtime.rs @@ -108,19 +108,19 @@ impl RuntimeRef { reader: ChannelReader, ) -> Result<(), OakStatus> { if self.is_terminating() { - return Err(OakStatus::ERR_TERMINATED); + return Err(OakStatus::ErrTerminated); } let mut node_threads = self.node_threads.lock().unwrap(); if self.is_terminating() { - return Err(OakStatus::ERR_TERMINATED); + return Err(OakStatus::ErrTerminated); } let join_handle = self .configurations .get(module_name) - .ok_or(OakStatus::ERR_INVALID_ARGS) + .ok_or(OakStatus::ErrInvalidArgs) .and_then(|conf| { conf.new_instance(module_name, self.clone(), entrypoint.to_owned(), reader) })?; diff --git a/sdk/rust/oak/src/io/mod.rs b/sdk/rust/oak/src/io/mod.rs index 426ced1ca8a..e7ce80dfbd3 100644 --- a/sdk/rust/oak/src/io/mod.rs +++ b/sdk/rust/oak/src/io/mod.rs @@ -44,31 +44,29 @@ pub struct Message { /// Map a non-OK [`OakStatus`] value to the nearest available [`std::io::Error`]. /// -/// Panics if passed an `OakStatus::OK` value. +/// Panics if passed an `OakStatus::Ok` value. pub fn error_from_nonok_status(status: OakStatus) -> io::Error { match status { - OakStatus::OAK_STATUS_UNSPECIFIED => { + OakStatus::Unspecified => { io::Error::new(io::ErrorKind::Other, "Unspecified Oak status value") } - OakStatus::OK => panic!("OK status found"), - OakStatus::ERR_BAD_HANDLE => io::Error::new(io::ErrorKind::NotConnected, "Bad handle"), - OakStatus::ERR_INVALID_ARGS => { + OakStatus::Ok => panic!("OK status found"), + OakStatus::ErrBadHandle => io::Error::new(io::ErrorKind::NotConnected, "Bad handle"), + OakStatus::ErrInvalidArgs => { io::Error::new(io::ErrorKind::InvalidInput, "Invalid arguments") } - OakStatus::ERR_CHANNEL_CLOSED => { + OakStatus::ErrChannelClosed => { io::Error::new(io::ErrorKind::ConnectionReset, "Channel closed") } - OakStatus::ERR_BUFFER_TOO_SMALL => { + OakStatus::ErrBufferTooSmall => { io::Error::new(io::ErrorKind::UnexpectedEof, "Buffer too small") } - OakStatus::ERR_HANDLE_SPACE_TOO_SMALL => { + OakStatus::ErrHandleSpaceTooSmall => { io::Error::new(io::ErrorKind::UnexpectedEof, "Handle space too small") } - OakStatus::ERR_OUT_OF_RANGE => io::Error::new(io::ErrorKind::NotConnected, "Out of range"), - OakStatus::ERR_INTERNAL => io::Error::new(io::ErrorKind::Other, "Internal error"), - OakStatus::ERR_TERMINATED => io::Error::new(io::ErrorKind::Other, "Node terminated"), - OakStatus::ERR_CHANNEL_EMPTY => { - io::Error::new(io::ErrorKind::UnexpectedEof, "Channel empty") - } + OakStatus::ErrOutOfRange => io::Error::new(io::ErrorKind::NotConnected, "Out of range"), + OakStatus::ErrInternal => io::Error::new(io::ErrorKind::Other, "Internal error"), + OakStatus::ErrTerminated => io::Error::new(io::ErrorKind::Other, "Node terminated"), + OakStatus::ErrChannelEmpty => io::Error::new(io::ErrorKind::UnexpectedEof, "Channel empty"), } } diff --git a/sdk/rust/oak/src/lib.rs b/sdk/rust/oak/src/lib.rs index dd7d2a33506..21b2a78116d 100644 --- a/sdk/rust/oak/src/lib.rs +++ b/sdk/rust/oak/src/lib.rs @@ -16,7 +16,6 @@ use byteorder::{ReadBytesExt, WriteBytesExt}; use log::{debug, error, info}; -use protobuf::ProtobufEnum; use serde::{Deserialize, Serialize}; // Re-export ABI constants that are also visible as part of the SDK API. @@ -180,7 +179,7 @@ pub fn wait_on_channels(handles: &[ReadHandle]) -> Result .and_then(ChannelReadStatus::from_i32) { Some(status) => results.push(status), - None => return Err(OakStatus::OAK_STATUS_UNSPECIFIED), + None => return Err(OakStatus::Unspecified), } } Ok(results) @@ -224,7 +223,7 @@ pub fn channel_read( match status { Some(s) => match s { - OakStatus::OK | OakStatus::ERR_CHANNEL_EMPTY => { + OakStatus::Ok | OakStatus::ErrChannelEmpty => { unsafe { // The read operation succeeded, and overwrote some fraction // of the vectors' available capacity with returned data (possibly @@ -235,16 +234,14 @@ pub fn channel_read( handles_buf.set_len(actual_handle_count as usize * 8); } Handle::unpack(&handles_buf, actual_handle_count, handles); - if s == OakStatus::OK { + if s == OakStatus::Ok { return Ok(()); } else { return Err(s); } } - OakStatus::ERR_BUFFER_TOO_SMALL | OakStatus::ERR_HANDLE_SPACE_TOO_SMALL - if !(*resized) => - { + OakStatus::ErrBufferTooSmall | OakStatus::ErrHandleSpaceTooSmall if !(*resized) => { // Extend the vectors to be large enough for the message debug!( "Got space for {} bytes, need {}", @@ -275,12 +272,12 @@ pub fn channel_read( } }, None => { - return Err(OakStatus::ERR_INTERNAL); + return Err(OakStatus::ErrInternal); } } } error!("unreachable code reached"); - Err(OakStatus::ERR_INTERNAL) + Err(OakStatus::ErrInternal) } /// Write a message to a channel. @@ -361,9 +358,9 @@ pub fn random_get(buf: &mut [u8]) -> Result<(), OakStatus> { /// system, so these values would usually be converted (via a cast) to `i32` by callers. pub fn result_from_status(status: i32, val: T) -> Result { match OakStatus::from_i32(status) { - Some(OakStatus::OK) => Ok(val), + Some(OakStatus::Ok) => Ok(val), Some(status) => Err(status), - None => Err(OakStatus::OAK_STATUS_UNSPECIFIED), + None => Err(OakStatus::Unspecified), } } @@ -428,7 +425,7 @@ pub fn run_event_loop>(mut node: N, in_handl info!("starting event loop"); loop { // First wait until a message is available. If the node was terminated while waiting, this - // will return `ERR_TERMINATED`, which indicates that the event loop should be terminated. + // will return `ErrTerminated`, which indicates that the event loop should be terminated. // For any other error raised while waiting is logged, we try and determine whether it is // transient or not, and then continue or terminate the event loop, respectively. match receiver.wait() { @@ -436,7 +433,7 @@ pub fn run_event_loop>(mut node: N, in_handl error!("error waiting for command: {:?}", status); use crate::OakStatus::*; match status { - ERR_TERMINATED | ERR_BAD_HANDLE | ERR_CHANNEL_CLOSED => { + ErrTerminated | ErrBadHandle | ErrChannelClosed => { info!("non-transient error: terminating event loop"); return; } diff --git a/sdk/rust/oak/src/storage/mod.rs b/sdk/rust/oak/src/storage/mod.rs index 1e536a2662d..9df5d7211dc 100644 --- a/sdk/rust/oak/src/storage/mod.rs +++ b/sdk/rust/oak/src/storage/mod.rs @@ -23,7 +23,7 @@ use crate::proto::storage_channel::{ StorageChannelReadResponse, StorageChannelWriteRequest, StorageChannelWriteResponse, }; use log::{info, warn}; -use protobuf::{Message, ProtobufEnum}; +use protobuf::Message; /// Default name for predefined node config that corresponds to a storage /// pseudo-Node. @@ -102,10 +102,7 @@ impl Storage { Err(status) => { return Err(grpc::build_status( grpc::Code::INTERNAL, - &format!( - "failed to create storage response channel: {}", - status.value() - ), + &format!("failed to create storage response channel: {:?}", status), )); } }; @@ -117,7 +114,7 @@ impl Storage { // Block until there is a response available. loop { let wait_result = crate::wait_on_channels(&[rsp_in]).unwrap(); - if wait_result[0] == crate::ChannelReadStatus::READ_READY { + if wait_result[0] == crate::ChannelReadStatus::ReadReady { break; } } diff --git a/sdk/rust/oak_log/src/lib.rs b/sdk/rust/oak_log/src/lib.rs index 80a34ae75e0..d9a63b7ea1f 100644 --- a/sdk/rust/oak_log/src/lib.rs +++ b/sdk/rust/oak_log/src/lib.rs @@ -65,7 +65,7 @@ impl Log for OakChannelLogger { }; match self.channel.send(&log_entry) { Ok(()) => (), - Err(oak::OakError::OakStatus(oak::OakStatus::ERR_TERMINATED)) => (), + Err(oak::OakError::OakStatus(oak::OakStatus::ErrTerminated)) => (), Err(e) => panic!("could not send log message over log channel: {}", e), } }