Make ErrorCode an enum
Change the VS AIDL to define it as an enum. Added a new common AIDL
module to hold it, since this is needed by both existing AIDLs, and we
don't want either directly depending on the other.
Modify vmclient to also have Rust enum for ErrorCode, with translation
logic, in the same way as we did for DeathReason. Switch to Debug
rather than Display for these enums.
Also fixed a couple of AIDL warnings (enum-explicit-default).
We also need translation in the Java API, but I'll do that as part of
https://r.android.com/2192077.
Bug: 236811123
Test: atest MicrodroidTests MicrodroidHostTestCases
Test: composd_cmd test-compile
Change-Id: I561e5f43f0f1c74d1318dc41782ed390bb5f0337
diff --git a/vmclient/Android.bp b/vmclient/Android.bp
index 213125e..88b0c9a 100644
--- a/vmclient/Android.bp
+++ b/vmclient/Android.bp
@@ -8,6 +8,7 @@
srcs: ["src/lib.rs"],
edition: "2021",
rustlibs: [
+ "android.system.virtualizationcommon-rust",
"android.system.virtualizationservice-rust",
"libbinder_rs",
"liblog_rust",
diff --git a/vmclient/src/death_reason.rs b/vmclient/src/death_reason.rs
index b976f6f..fbf2523 100644
--- a/vmclient/src/death_reason.rs
+++ b/vmclient/src/death_reason.rs
@@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use std::fmt::{self, Debug, Display, Formatter};
use android_system_virtualizationservice::{
aidl::android::system::virtualizationservice::{
DeathReason::DeathReason as AidlDeathReason}};
@@ -96,48 +95,3 @@
}
}
}
-
-impl Display for DeathReason {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- let s = match self {
- Self::VirtualizationServiceDied => "VirtualizationService died.",
- Self::InfrastructureError => "Error waiting for VM to finish.",
- Self::Killed => "VM was killed.",
- Self::Unknown => "VM died for an unknown reason.",
- Self::Shutdown => "VM shutdown cleanly.",
- Self::Error => "Error starting VM.",
- Self::Reboot => "VM tried to reboot, possibly due to a kernel panic.",
- Self::Crash => "VM crashed.",
- Self::PvmFirmwarePublicKeyMismatch => {
- "pVM firmware failed to verify the VM because the public key doesn't match."
- }
- Self::PvmFirmwareInstanceImageChanged => {
- "pVM firmware failed to verify the VM because the instance image changed."
- }
- Self::BootloaderPublicKeyMismatch => {
- "Bootloader failed to verify the VM because the public key doesn't match."
- }
- Self::BootloaderInstanceImageChanged => {
- "Bootloader failed to verify the VM because the instance image changed."
- }
- Self::MicrodroidFailedToConnectToVirtualizationService => {
- "The microdroid failed to connect to VirtualizationService's RPC server."
- }
- Self::MicrodroidPayloadHasChanged => "The payload for microdroid is changed.",
- Self::MicrodroidPayloadVerificationFailed => {
- "The microdroid failed to verify given payload APK."
- }
- Self::MicrodroidInvalidPayloadConfig => {
- "The VM config for microdroid is invalid (e.g. missing tasks)."
- }
- Self::MicrodroidUnknownRuntimeError => {
- "There was a runtime error while running microdroid manager."
- }
- Self::Hangup => "VM hangup.",
- Self::Unrecognised(reason) => {
- return write!(f, "Unrecognised death reason {:?}.", reason);
- }
- };
- f.write_str(s)
- }
-}
diff --git a/vmclient/src/error_code.rs b/vmclient/src/error_code.rs
new file mode 100644
index 0000000..a7c442f
--- /dev/null
+++ b/vmclient/src/error_code.rs
@@ -0,0 +1,47 @@
+// Copyright 2022, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::ErrorCode::ErrorCode as AidlErrorCode;
+
+/// Errors reported from within a VM.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum ErrorCode {
+ /// Error code for all other errors not listed below.
+ Unknown,
+
+ /// Error code indicating that the payload can't be verified due to various reasons (e.g invalid
+ /// merkle tree, invalid formats, etc).
+ PayloadVerificationFailed,
+
+ /// Error code indicating that the payload is verified, but has changed since the last boot.
+ PayloadChanged,
+
+ /// Error code indicating that the payload config is invalid.
+ PayloadConfigInvalid,
+
+ /// Payload sent a death reason which was not recognised by the client library.
+ Unrecognised(AidlErrorCode),
+}
+
+impl From<AidlErrorCode> for ErrorCode {
+ fn from(error_code: AidlErrorCode) -> Self {
+ match error_code {
+ AidlErrorCode::UNKNOWN => Self::Unknown,
+ AidlErrorCode::PAYLOAD_VERIFICATION_FAILED => Self::PayloadVerificationFailed,
+ AidlErrorCode::PAYLOAD_CHANGED => Self::PayloadChanged,
+ AidlErrorCode::PAYLOAD_CONFIG_INVALID => Self::PayloadConfigInvalid,
+ _ => Self::Unrecognised(error_code),
+ }
+ }
+}
diff --git a/vmclient/src/errors.rs b/vmclient/src/errors.rs
index 231f81f..a6dca91 100644
--- a/vmclient/src/errors.rs
+++ b/vmclient/src/errors.rs
@@ -22,7 +22,7 @@
#[error("Timed out waiting for VM.")]
TimedOut,
/// The VM died before it was ready.
- #[error("VM died. ({reason})")]
+ #[error("VM died. ({reason:?})")]
Died {
/// The reason why the VM died.
reason: DeathReason,
diff --git a/vmclient/src/lib.rs b/vmclient/src/lib.rs
index 16b5d5a..e6f32b4 100644
--- a/vmclient/src/lib.rs
+++ b/vmclient/src/lib.rs
@@ -15,12 +15,15 @@
//! Client library for VirtualizationService.
mod death_reason;
+mod error_code;
mod errors;
mod sync;
pub use crate::death_reason::DeathReason;
+pub use crate::error_code::ErrorCode;
pub use crate::errors::VmWaitError;
use crate::sync::Monitor;
+use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::ErrorCode::ErrorCode as AidlErrorCode;
use android_system_virtualizationservice::{
aidl::android::system::virtualizationservice::{
DeathReason::DeathReason as AidlDeathReason,
@@ -83,7 +86,7 @@
/// Called when an error has occurred in the VM. The `error_code` and `message` may give
/// further details.
- fn on_error(&self, cid: i32, error_code: i32, message: &str) {}
+ fn on_error(&self, cid: i32, error_code: ErrorCode, message: &str) {}
/// Called when the VM has exited, all resources have been freed, and any logs have been
/// written. `death_reason` gives an indication why the VM exited.
@@ -294,9 +297,10 @@
Ok(())
}
- fn onError(&self, cid: i32, error_code: i32, message: &str) -> BinderResult<()> {
+ fn onError(&self, cid: i32, error_code: AidlErrorCode, message: &str) -> BinderResult<()> {
self.state.notify_state(VirtualMachineState::FINISHED);
if let Some(ref callback) = self.client_callback {
+ let error_code = error_code.into();
callback.on_error(cid, error_code, message);
}
Ok(())