Revert "libavf: Add callback support"
Revert submission 3408863
Reason for revert: Likely culprit for b/391356639 - verifying through ABTD before revert submission. This is part of the standard investigation process, and does not mean your CL will be reverted.
Reverted changes: /q/submissionid:3408863
Change-Id: Ic004d9cf526332538a9fc9c283f055f41e1c4d30
diff --git a/libs/libavf/src/lib.rs b/libs/libavf/src/lib.rs
index 256803f..50c5e2e 100644
--- a/libs/libavf/src/lib.rs
+++ b/libs/libavf/src/lib.rs
@@ -19,7 +19,6 @@
use std::os::fd::{FromRawFd, IntoRawFd};
use std::os::raw::{c_char, c_int};
use std::ptr;
-use std::sync::Arc;
use std::time::Duration;
use android_system_virtualizationservice::{
@@ -30,10 +29,10 @@
},
binder::{ParcelFileDescriptor, Strong},
};
-use avf_bindgen::{AVirtualMachineStopReason, AVirtualMachine_stopCallback};
+use avf_bindgen::AVirtualMachineStopReason;
use libc::timespec;
use log::error;
-use vmclient::{DeathReason, ErrorCode, VirtualizationService, VmCallback, VmInstance};
+use vmclient::{DeathReason, VirtualizationService, VmInstance};
/// Create a new virtual machine config object with no properties.
#[no_mangle]
@@ -343,49 +342,6 @@
}
}
-struct LocalVmInstance {
- vm: Arc<VmInstance>,
- callback: AVirtualMachine_stopCallback,
-}
-
-impl VmCallback for LocalVmInstance {
- fn on_payload_started(&self, _cid: i32) {
- // Microdroid only. no-op.
- }
-
- fn on_payload_ready(&self, _cid: i32) {
- // Microdroid only. no-op.
- }
-
- fn on_payload_finished(&self, _cid: i32, _exit_code: i32) {
- // Microdroid only. no-op.
- }
-
- fn on_error(&self, _cid: i32, _error_code: ErrorCode, _message: &str) {
- // Microdroid only. no-op.
- }
-
- fn on_died(&self, _cid: i32, death_reason: DeathReason) {
- let Some(callback) = self.callback else {
- return;
- };
- let stop_reason = death_reason_to_stop_reason(death_reason);
- let vm_ptr: *const VmInstance = Arc::into_raw(Arc::clone(&self.vm));
-
- // SAFETY: `callback` is assumed to be a valid, non-null function pointer passed by
- // `AVirtualMachine_start`.
- unsafe {
- callback(vm_ptr.cast(), stop_reason);
- }
-
- // drop ptr after use.
- // SAFETY: `vm_ptr` is a valid, non-null pointer casted above.
- unsafe {
- let _ = Arc::from_raw(vm_ptr);
- }
- }
-}
-
/// Create a virtual machine with given `config`.
///
/// # Safety
@@ -401,7 +357,7 @@
console_out_fd: c_int,
console_in_fd: c_int,
log_fd: c_int,
- vm_ptr: *mut *const VmInstance,
+ vm_ptr: *mut *mut VmInstance,
) -> c_int {
// SAFETY: `service` is assumed to be a valid, non-null pointer returned by
// `AVirtualizationService_create` or `AVirtualizationService_create_early`. It's the only
@@ -420,8 +376,9 @@
match VmInstance::create(service.as_ref(), &config, console_out, console_in, log, None) {
Ok(vm) => {
// SAFETY: `vm_ptr` is assumed to be a valid, non-null pointer to a mutable raw pointer.
+ // `vm` is the only reference here and `vm_ptr` takes ownership.
unsafe {
- *vm_ptr = Arc::into_raw(Arc::new(vm));
+ *vm_ptr = Box::into_raw(Box::new(vm));
}
0
}
@@ -437,20 +394,11 @@
/// # Safety
/// `vm` must be a pointer returned by `AVirtualMachine_createRaw`.
#[no_mangle]
-pub unsafe extern "C" fn AVirtualMachine_start(
- vm: *const VmInstance,
- callback: AVirtualMachine_stopCallback,
-) -> c_int {
+pub unsafe extern "C" fn AVirtualMachine_start(vm: *const VmInstance) -> c_int {
// SAFETY: `vm` is assumed to be a valid, non-null pointer returned by
// `AVirtualMachine_createRaw`. It's the only reference to the object.
- let vm = unsafe { Arc::from_raw(vm) };
- let callback = callback.map(|_| {
- let cb: Box<dyn VmCallback + Send + Sync> =
- Box::new(LocalVmInstance { vm: Arc::clone(&vm), callback });
- cb
- });
-
- match vm.start(callback) {
+ let vm = unsafe { &*vm };
+ match vm.start(None) {
Ok(_) => 0,
Err(e) => {
error!("AVirtualMachine_start failed: {e:?}");
@@ -561,12 +509,12 @@
/// `vm` must be a pointer returned by `AVirtualMachine_createRaw`. `vm` must not be reused after
/// deletion.
#[no_mangle]
-pub unsafe extern "C" fn AVirtualMachine_destroy(vm: *const VmInstance) {
+pub unsafe extern "C" fn AVirtualMachine_destroy(vm: *mut VmInstance) {
if !vm.is_null() {
// SAFETY: `vm` is assumed to be a valid, non-null pointer returned by
// AVirtualMachine_create. It's the only reference to the object.
unsafe {
- let _ = Arc::from_raw(vm);
+ let _ = Box::from_raw(vm);
}
}
}