Skip to content

Commit 4449d4e

Browse files
Change callback handling from Arc to Box
There is only ever one ref count for this callback object, and it's always completely dropped when the Accessor or AccessorProvider objects are dropped, so Box is a better type. Test: atest rustBinderTest # has the *callback_destruction tests Bug: 358427181 Change-Id: Ifc4b17a589b27f232c20c7e8a82d2b23a717fda8
1 parent a3867c7 commit 4449d4e

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

libs/binder/rust/src/system_only.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::ffi::{c_void, CStr, CString};
2323
use std::os::raw::c_char;
2424

2525
use libc::{sockaddr, sockaddr_un, sockaddr_vm, socklen_t};
26-
use std::sync::Arc;
26+
use std::boxed::Box;
2727
use std::{mem, ptr};
2828

2929
/// Rust wrapper around ABinderRpc_Accessor objects for RPC binder service management.
@@ -65,7 +65,7 @@ impl Accessor {
6565
where
6666
F: Fn(&str) -> Option<ConnectionInfo> + Send + Sync + 'static,
6767
{
68-
let callback: *mut c_void = Arc::into_raw(Arc::new(callback)) as *mut c_void;
68+
let callback: *mut c_void = Box::into_raw(Box::new(callback)) as *mut c_void;
6969
let inst = CString::new(instance).unwrap();
7070

7171
// Safety: The function pointer is a valid connection_info callback.
@@ -149,7 +149,7 @@ impl Accessor {
149149
/// the string within isize::MAX from the pointer. The memory must not be mutated for
150150
/// the duration of this function call and must be valid for reads from the pointer
151151
/// to the null terminator.
152-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
152+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
153153
/// the caller must hold a ref-count to it.
154154
unsafe extern "C" fn connection_info<F>(
155155
instance: *const c_char,
@@ -162,7 +162,7 @@ impl Accessor {
162162
log::error!("Cookie({cookie:p}) or instance({instance:p}) is null!");
163163
return ptr::null_mut();
164164
}
165-
// Safety: The caller promises that `cookie` is for an Arc<F>.
165+
// Safety: The caller promises that `cookie` is for a Box<F>.
166166
let callback = unsafe { (cookie as *const F).as_ref().unwrap() };
167167

168168
// Safety: The caller in libbinder_ndk will have already verified this is a valid
@@ -207,19 +207,19 @@ impl Accessor {
207207
}
208208
}
209209

210-
/// Callback that decrements the ref-count.
210+
/// Callback that drops the `Box<F>`.
211211
/// This is invoked from C++ when a binder is unlinked.
212212
///
213213
/// # Safety
214214
///
215-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
215+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
216216
/// the owner must give up a ref-count to it.
217217
unsafe extern "C" fn cookie_decr_refcount<F>(cookie: *mut c_void)
218218
where
219219
F: Fn(&str) -> Option<ConnectionInfo> + Send + Sync + 'static,
220220
{
221-
// Safety: The caller promises that `cookie` is for an Arc<F>.
222-
unsafe { Arc::decrement_strong_count(cookie as *const F) };
221+
// Safety: The caller promises that `cookie` is for a Box<F>.
222+
unsafe { std::mem::drop(Box::from_raw(cookie as *mut F)) };
223223
}
224224
}
225225

@@ -296,7 +296,7 @@ impl AccessorProvider {
296296
where
297297
F: Fn(&str) -> Option<Accessor> + Send + Sync + 'static,
298298
{
299-
let callback: *mut c_void = Arc::into_raw(Arc::new(provider)) as *mut c_void;
299+
let callback: *mut c_void = Box::into_raw(Box::new(provider)) as *mut c_void;
300300
let c_str_instances: Vec<CString> =
301301
instances.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect();
302302
let mut c_instances: Vec<*const c_char> =
@@ -346,7 +346,7 @@ impl AccessorProvider {
346346
log::error!("Cookie({cookie:p}) or instance({instance:p}) is null!");
347347
return ptr::null_mut();
348348
}
349-
// Safety: The caller promises that `cookie` is for an Arc<F>.
349+
// Safety: The caller promises that `cookie` is for a Box<F>.
350350
let callback = unsafe { (cookie as *const F).as_ref().unwrap() };
351351

352352
let inst = {
@@ -377,14 +377,14 @@ impl AccessorProvider {
377377
///
378378
/// # Safety
379379
///
380-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
380+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
381381
/// the owner must give up a ref-count to it.
382382
unsafe extern "C" fn accessor_cookie_decr_refcount<F>(cookie: *mut c_void)
383383
where
384384
F: Fn(&str) -> Option<Accessor> + Send + Sync + 'static,
385385
{
386-
// Safety: The caller promises that `cookie` is for an Arc<F>.
387-
unsafe { Arc::decrement_strong_count(cookie as *const F) };
386+
// Safety: The caller promises that `cookie` is for a Box<F>.
387+
unsafe { std::mem::drop(Box::from_raw(cookie as *mut F)) };
388388
}
389389
}
390390

0 commit comments

Comments
 (0)