Add libavfutil
The library has common utilities for various components in AVF.
Bug: 295168844
Test: atest libavfutil.test
Change-Id: If24309f480d0704c6bcba9c193a4caf578ee9d51
diff --git a/libs/avfutil/Android.bp b/libs/avfutil/Android.bp
new file mode 100644
index 0000000..b9b1c19
--- /dev/null
+++ b/libs/avfutil/Android.bp
@@ -0,0 +1,30 @@
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_defaults {
+ name: "libavfutil.defaults",
+ crate_name: "avfutil",
+ host_supported: true,
+ srcs: ["src/lib.rs"],
+ edition: "2021",
+ rustlibs: [
+ "liblog_rust",
+ ],
+}
+
+rust_library {
+ name: "libavfutil",
+ defaults: ["libavfutil.defaults"],
+ apex_available: [
+ "//apex_available:platform",
+ "//apex_available:anyapex",
+ ],
+}
+
+rust_test {
+ name: "libavfutil.test",
+ defaults: ["libavfutil.defaults"],
+ prefer_rlib: true,
+ test_suites: ["general-tests"],
+}
diff --git a/libs/avfutil/TEST_MAPPING b/libs/avfutil/TEST_MAPPING
new file mode 100644
index 0000000..73bcc20
--- /dev/null
+++ b/libs/avfutil/TEST_MAPPING
@@ -0,0 +1,9 @@
+// When adding or removing tests here, don't forget to amend _all_modules list in
+// wireless/android/busytown/ath_config/configs/prod/avf/tests.gcl
+{
+ "avf-presubmit" : [
+ {
+ "name" : "libavfutil.test"
+ }
+ ]
+}
diff --git a/libs/avfutil/src/lib.rs b/libs/avfutil/src/lib.rs
new file mode 100644
index 0000000..27c8628
--- /dev/null
+++ b/libs/avfutil/src/lib.rs
@@ -0,0 +1,71 @@
+// Copyright 2023, 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.
+
+//! Provides random utilities for components in AVF
+
+use log::error;
+use std::fmt::Debug;
+
+/// Convenient trait for logging an error while returning it
+pub trait LogResult<T, E> {
+ /// If this is `Err`, the error is debug-formatted and is logged via `error!`.
+ fn with_log(self) -> Result<T, E>;
+}
+
+impl<T, E: Debug> LogResult<T, E> for Result<T, E> {
+ fn with_log(self) -> Result<T, E> {
+ self.map_err(|e| {
+ error!("{e:?}");
+ e
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use log::{LevelFilter, Log, Metadata, Record};
+ use std::cell::RefCell;
+ use std::io::{Error, ErrorKind};
+
+ struct TestLogger {
+ last_log: RefCell<String>,
+ }
+ static TEST_LOGGER: TestLogger = TestLogger { last_log: RefCell::new(String::new()) };
+
+ // SAFETY: TestLogger is used only inside the test which is single-treaded.
+ unsafe impl Sync for TestLogger {}
+
+ impl Log for TestLogger {
+ fn enabled(&self, _metadata: &Metadata) -> bool {
+ true
+ }
+ fn log(&self, record: &Record) {
+ *self.last_log.borrow_mut() = format!("{}", record.args());
+ }
+ fn flush(&self) {}
+ }
+
+ #[test]
+ fn test_logresult_emits_error_log() {
+ log::set_logger(&TEST_LOGGER).unwrap();
+ log::set_max_level(LevelFilter::Info);
+
+ let e = Error::from(ErrorKind::NotFound);
+ let res: Result<(), Error> = Err(e).with_log();
+
+ assert!(res.is_err());
+ assert_eq!(TEST_LOGGER.last_log.borrow().as_str(), "Kind(NotFound)");
+ }
+}
diff --git a/microdroid_manager/Android.bp b/microdroid_manager/Android.bp
index d854d54..f23c442 100644
--- a/microdroid_manager/Android.bp
+++ b/microdroid_manager/Android.bp
@@ -15,6 +15,7 @@
"android.system.virtualization.payload-rust",
"libandroid_logger",
"libanyhow",
+ "libavfutil",
"libapexutil_rust",
"libapkverify",
"libbinder_rs",
diff --git a/microdroid_manager/src/vm_payload_service.rs b/microdroid_manager/src/vm_payload_service.rs
index b44b0db..7cc0826 100644
--- a/microdroid_manager/src/vm_payload_service.rs
+++ b/microdroid_manager/src/vm_payload_service.rs
@@ -19,26 +19,13 @@
BnVmPayloadService, IVmPayloadService, VM_PAYLOAD_SERVICE_SOCKET_NAME};
use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService;
use anyhow::{anyhow, Context, Result};
+use avfutil::LogResult;
use binder::{Interface, BinderFeatures, ExceptionCode, Strong, IntoBinderResult};
use diced_open_dice::{DiceArtifacts, OwnedDiceArtifacts};
-use log::{error, info};
+use log::info;
use rpcbinder::RpcServer;
use std::os::unix::io::OwnedFd;
-/// Convenient trait for logging an error while returning it
-trait LogResult<T, E> {
- fn with_log(self) -> std::result::Result<T, E>;
-}
-
-impl<T, E: std::fmt::Debug> LogResult<T, E> for std::result::Result<T, E> {
- fn with_log(self) -> std::result::Result<T, E> {
- self.map_err(|e| {
- error!("{e:?}");
- e
- })
- }
-}
-
/// Implementation of `IVmPayloadService`.
struct VmPayloadService {
allow_restricted_apis: bool,
diff --git a/virtualizationmanager/Android.bp b/virtualizationmanager/Android.bp
index 59e507f..1b5be9f 100644
--- a/virtualizationmanager/Android.bp
+++ b/virtualizationmanager/Android.bp
@@ -27,6 +27,7 @@
"libandroid_logger",
"libanyhow",
"libapkverify",
+ "libavfutil",
"libbase_rust",
"libbinder_rs",
"libclap",
diff --git a/virtualizationmanager/src/aidl.rs b/virtualizationmanager/src/aidl.rs
index ae81a17..b890baf 100644
--- a/virtualizationmanager/src/aidl.rs
+++ b/virtualizationmanager/src/aidl.rs
@@ -50,6 +50,7 @@
};
use anyhow::{anyhow, bail, Context, Result};
use apkverify::{HashAlgorithm, V4Signature};
+use avfutil::LogResult;
use binder::{
self, wait_for_interface, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor,
Status, StatusCode, Strong,
@@ -77,20 +78,6 @@
use vsock::VsockStream;
use zip::ZipArchive;
-/// Convenient trait for logging an error while returning it
-trait LogResult<T, E> {
- fn with_log(self) -> std::result::Result<T, E>;
-}
-
-impl<T, E: std::fmt::Debug> LogResult<T, E> for std::result::Result<T, E> {
- fn with_log(self) -> std::result::Result<T, E> {
- self.map_err(|e| {
- error!("{e:?}");
- e
- })
- }
-}
-
/// The unique ID of a VM used (together with a port number) for vsock communication.
pub type Cid = u32;
diff --git a/virtualizationservice/Android.bp b/virtualizationservice/Android.bp
index 6b39ff9..0fc1a2d 100644
--- a/virtualizationservice/Android.bp
+++ b/virtualizationservice/Android.bp
@@ -27,12 +27,13 @@
"android.os.permissions_aidl-rust",
"libandroid_logger",
"libanyhow",
+ "libavfutil",
"libbinder_rs",
- "libvmclient",
"liblibc",
"liblog_rust",
"libnix",
"librustutils",
+ "libvmclient",
"libstatslog_virtualization_rust",
"libtombstoned_client_rust",
"libvsock",
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index 6dc5485..8401aa0 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -33,6 +33,7 @@
};
use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::VM_TOMBSTONES_SERVICE_PORT;
use anyhow::{anyhow, ensure, Context, Result};
+use avfutil::LogResult;
use binder::{self, wait_for_interface, BinderFeatures, ExceptionCode, Interface, LazyServiceGuard, Status, Strong, IntoBinderResult};
use libc::VMADDR_CID_HOST;
use log::{error, info, warn};
@@ -48,20 +49,6 @@
use vsock::{VsockListener, VsockStream};
use nix::unistd::{chown, Uid};
-/// Convenient trait for logging an error while returning it
-trait LogResult<T, E> {
- fn with_log(self) -> std::result::Result<T, E>;
-}
-
-impl<T, E: std::fmt::Debug> LogResult<T, E> for std::result::Result<T, E> {
- fn with_log(self) -> std::result::Result<T, E> {
- self.map_err(|e| {
- error!("{e:?}");
- e
- })
- }
-}
-
/// The unique ID of a VM used (together with a port number) for vsock communication.
pub type Cid = u32;