Skip to content

Commit d14a113

Browse files
Merge changes Id4db842d,I58e41d34 into main am: dcca938
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/3303357 Change-Id: Ib7e6f56d178f09361838d8b036a0f2001d4324f9 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 parents 52c3914 + dcca938 commit d14a113

13 files changed

Lines changed: 159 additions & 25 deletions

File tree

libs/binder/rust/Android.bp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ rust_bindgen {
139139
"--raw-line",
140140
"use libc::sockaddr;",
141141
],
142+
cflags: [
143+
"-DANDROID_PLATFORM",
144+
],
142145
shared_libs: [
143146
"libbinder_ndk",
144147
],
@@ -179,6 +182,9 @@ rust_bindgen {
179182
// rustified
180183
"libbinder_ndk_bindgen_flags.txt",
181184
],
185+
cflags: [
186+
"-DANDROID_PLATFORM",
187+
],
182188
shared_libs: [
183189
"libbinder_ndk_on_trusty_mock",
184190
"libc++",

libs/binder/rust/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "android-binder"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Safe bindings to Android Binder, restricted to the NDK"
6+
license = "Apache-2.0"
7+
8+
[dependencies]
9+
binder-ndk-sys = { package = "android-binder-ndk-sys", version = "0.1", path = "./sys" }
10+
downcast-rs = "1.2.1"
11+
libc = "0.2.159"
12+
13+
[lints.rust.unexpected_cfgs]
14+
level = "warn"
15+
check-cfg = ["cfg(android_vendor)", "cfg(android_ndk)", "cfg(android_vndk)", "cfg(trusty)"]

libs/binder/rust/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
// Anything with cargo is NDK only. If you want to access anything else, use Soong.
3+
println!("cargo::rustc-cfg=android_ndk");
4+
}

libs/binder/rust/src/binder.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ pub const LAST_CALL_TRANSACTION: TransactionCode = sys::LAST_CALL_TRANSACTION;
207207
/// Corresponds to TF_ONE_WAY -- an asynchronous call.
208208
pub const FLAG_ONEWAY: TransactionFlags = sys::FLAG_ONEWAY;
209209
/// Corresponds to TF_CLEAR_BUF -- clear transaction buffers after call is made.
210+
#[cfg(not(android_ndk))]
210211
pub const FLAG_CLEAR_BUF: TransactionFlags = sys::FLAG_CLEAR_BUF;
211212
/// Set to the vendor flag if we are building for the VNDK, 0 otherwise
213+
#[cfg(not(android_ndk))]
212214
pub const FLAG_PRIVATE_LOCAL: TransactionFlags = sys::FLAG_PRIVATE_LOCAL;
213215

214216
/// Internal interface of binder local or remote objects for making
@@ -221,7 +223,7 @@ pub trait IBinderInternal: IBinder {
221223
fn is_binder_alive(&self) -> bool;
222224

223225
/// Indicate that the service intends to receive caller security contexts.
224-
#[cfg(not(android_vndk))]
226+
#[cfg(not(any(android_vndk, android_ndk)))]
225227
fn set_requesting_sid(&mut self, enable: bool);
226228

227229
/// Dump this object to the given file handle
@@ -346,7 +348,6 @@ impl InterfaceClass {
346348
panic!("Expected non-null class pointer from AIBinder_Class_define!");
347349
}
348350
sys::AIBinder_Class_setOnDump(class, Some(I::on_dump));
349-
sys::AIBinder_Class_setHandleShellCommand(class, None);
350351
class
351352
};
352353
InterfaceClass(ptr)
@@ -714,7 +715,7 @@ unsafe impl<T, V: AsNative<T>> AsNative<T> for Option<V> {
714715
pub struct BinderFeatures {
715716
/// Indicates that the service intends to receive caller security contexts. This must be true
716717
/// for `ThreadState::with_calling_sid` to work.
717-
#[cfg(not(android_vndk))]
718+
#[cfg(not(any(android_vndk, android_ndk)))]
718719
pub set_requesting_sid: bool,
719720
// Ensure that clients include a ..BinderFeatures::default() to preserve backwards compatibility
720721
// when new fields are added. #[non_exhaustive] doesn't work because it prevents struct
@@ -916,8 +917,12 @@ macro_rules! declare_binder_interface {
916917
impl $native {
917918
/// Create a new binder service.
918919
pub fn new_binder<T: $interface + Sync + Send + 'static>(inner: T, features: $crate::BinderFeatures) -> $crate::Strong<dyn $interface> {
920+
#[cfg(not(android_ndk))]
919921
let mut binder = $crate::binder_impl::Binder::new_with_stability($native(Box::new(inner)), $stability);
920-
#[cfg(not(android_vndk))]
922+
#[cfg(android_ndk)]
923+
let mut binder = $crate::binder_impl::Binder::new($native(Box::new(inner)));
924+
925+
#[cfg(not(any(android_vndk, android_ndk)))]
921926
$crate::binder_impl::IBinderInternal::set_requesting_sid(&mut binder, features.set_requesting_sid);
922927
$crate::Strong::new(Box::new(binder))
923928
}

libs/binder/rust/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ mod error;
100100
mod native;
101101
mod parcel;
102102
mod proxy;
103-
#[cfg(not(trusty))]
103+
#[cfg(not(any(trusty, android_ndk)))]
104104
mod service;
105-
#[cfg(not(trusty))]
105+
#[cfg(not(any(trusty, android_ndk)))]
106106
mod state;
107-
#[cfg(not(any(android_vendor, android_vndk)))]
107+
#[cfg(not(any(android_vendor, android_ndk, android_vndk)))]
108108
mod system_only;
109109

110110
use binder_ndk_sys as sys;
@@ -114,15 +114,18 @@ pub use binder::{BinderFeatures, FromIBinder, IBinder, Interface, Strong, Weak};
114114
pub use error::{ExceptionCode, IntoBinderResult, Status, StatusCode};
115115
pub use parcel::{ParcelFileDescriptor, Parcelable, ParcelableHolder};
116116
pub use proxy::{DeathRecipient, SpIBinder, WpIBinder};
117-
#[cfg(not(trusty))]
117+
#[cfg(not(any(trusty, android_ndk)))]
118118
pub use service::{
119119
add_service, check_interface, check_service, force_lazy_services_persist,
120-
get_declared_instances, get_interface, get_service, is_declared, is_handling_transaction,
121-
register_lazy_service, wait_for_interface, wait_for_service, LazyServiceGuard,
120+
get_declared_instances, is_declared, is_handling_transaction, register_lazy_service,
121+
wait_for_interface, wait_for_service, LazyServiceGuard,
122122
};
123-
#[cfg(not(trusty))]
123+
#[cfg(not(any(trusty, android_ndk)))]
124+
#[allow(deprecated)]
125+
pub use service::{get_interface, get_service};
126+
#[cfg(not(any(trusty, android_ndk)))]
124127
pub use state::{ProcessState, ThreadState};
125-
#[cfg(not(any(android_vendor, android_vndk)))]
128+
#[cfg(not(any(android_vendor, android_vndk, android_ndk)))]
126129
pub use system_only::{delegate_accessor, Accessor, ConnectionInfo};
127130

128131
/// Binder result containing a [`Status`] on error.
@@ -134,9 +137,10 @@ pub mod binder_impl {
134137
pub use crate::binder::{
135138
IBinderInternal, InterfaceClass, LocalStabilityType, Remotable, Stability, StabilityType,
136139
ToAsyncInterface, ToSyncInterface, TransactionCode, TransactionFlags, VintfStabilityType,
137-
FIRST_CALL_TRANSACTION, FLAG_CLEAR_BUF, FLAG_ONEWAY, FLAG_PRIVATE_LOCAL,
138-
LAST_CALL_TRANSACTION,
140+
FIRST_CALL_TRANSACTION, FLAG_ONEWAY, LAST_CALL_TRANSACTION,
139141
};
142+
#[cfg(not(android_ndk))]
143+
pub use crate::binder::{FLAG_CLEAR_BUF, FLAG_PRIVATE_LOCAL};
140144
pub use crate::binder_async::BinderAsyncRuntime;
141145
pub use crate::error::status_t;
142146
pub use crate::native::Binder;

libs/binder/rust/src/native.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
use crate::binder::{
18-
AsNative, Interface, InterfaceClassMethods, Remotable, Stability, TransactionCode,
19-
};
17+
#[cfg(not(android_ndk))]
18+
use crate::binder::Stability;
19+
use crate::binder::{AsNative, Interface, InterfaceClassMethods, Remotable, TransactionCode};
2020
use crate::error::{status_result, status_t, Result, StatusCode};
2121
use crate::parcel::{BorrowedParcel, Serialize};
2222
use crate::proxy::SpIBinder;
@@ -76,14 +76,32 @@ impl<T: Remotable> Binder<T> {
7676
/// This moves the `rust_object` into an owned [`Box`] and Binder will
7777
/// manage its lifetime.
7878
pub fn new(rust_object: T) -> Binder<T> {
79-
Self::new_with_stability(rust_object, Stability::default())
79+
#[cfg(not(android_ndk))]
80+
{
81+
Self::new_with_stability(rust_object, Stability::default())
82+
}
83+
#[cfg(android_ndk)]
84+
{
85+
Self::new_unmarked(rust_object)
86+
}
8087
}
8188

8289
/// Create a new Binder remotable object with the given stability
8390
///
8491
/// This moves the `rust_object` into an owned [`Box`] and Binder will
8592
/// manage its lifetime.
93+
#[cfg(not(android_ndk))]
8694
pub fn new_with_stability(rust_object: T, stability: Stability) -> Binder<T> {
95+
let mut binder = Self::new_unmarked(rust_object);
96+
binder.mark_stability(stability);
97+
binder
98+
}
99+
100+
/// Creates a new Binder remotable object with unset stability
101+
///
102+
/// This is internal because normally we want to set the stability explicitly,
103+
/// however for the NDK variant we cannot mark the stability.
104+
fn new_unmarked(rust_object: T) -> Binder<T> {
87105
let class = T::get_class();
88106
let rust_object = Box::into_raw(Box::new(rust_object));
89107
// Safety: `AIBinder_new` expects a valid class pointer (which we
@@ -93,9 +111,7 @@ impl<T: Remotable> Binder<T> {
93111
// decremented via `AIBinder_decStrong` when the reference lifetime
94112
// ends.
95113
let ibinder = unsafe { sys::AIBinder_new(class.into(), rust_object as *mut c_void) };
96-
let mut binder = Binder { ibinder, rust_object };
97-
binder.mark_stability(stability);
98-
binder
114+
Binder { ibinder, rust_object }
99115
}
100116

101117
/// Set the extension of a binder interface. This allows a downstream
@@ -189,6 +205,7 @@ impl<T: Remotable> Binder<T> {
189205
}
190206

191207
/// Mark this binder object with the given stability guarantee
208+
#[cfg(not(android_ndk))]
192209
fn mark_stability(&mut self, stability: Stability) {
193210
match stability {
194211
Stability::Local => self.mark_local_stability(),
@@ -215,7 +232,7 @@ impl<T: Remotable> Binder<T> {
215232

216233
/// Mark this binder object with local stability, which is vendor if we are
217234
/// building for android_vendor and system otherwise.
218-
#[cfg(not(android_vendor))]
235+
#[cfg(not(any(android_vendor, android_ndk)))]
219236
fn mark_local_stability(&mut self) {
220237
// Safety: Self always contains a valid `AIBinder` pointer, so we can
221238
// always call this C API safely.

libs/binder/rust/src/parcel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ unsafe impl<'a> AsNative<sys::AParcel> for BorrowedParcel<'a> {
197197
// Data serialization methods
198198
impl<'a> BorrowedParcel<'a> {
199199
/// Data written to parcelable is zero'd before being deleted or reallocated.
200+
#[cfg(not(android_ndk))]
200201
pub fn mark_sensitive(&mut self) {
201202
// Safety: guaranteed to have a parcel object, and this method never fails
202203
unsafe { sys::AParcel_markSensitive(self.as_native()) }
@@ -342,6 +343,7 @@ impl<'a> WritableSubParcel<'a> {
342343

343344
impl Parcel {
344345
/// Data written to parcelable is zero'd before being deleted or reallocated.
346+
#[cfg(not(android_ndk))]
345347
pub fn mark_sensitive(&mut self) {
346348
self.borrowed().mark_sensitive()
347349
}

libs/binder/rust/src/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<T: AsNative<sys::AIBinder>> IBinderInternal for T {
298298
unsafe { sys::AIBinder_isAlive(self.as_native()) }
299299
}
300300

301-
#[cfg(not(android_vndk))]
301+
#[cfg(not(any(android_vndk, android_ndk)))]
302302
fn set_requesting_sid(&mut self, enable: bool) {
303303
// Safety: `SpIBinder` guarantees that `self` always contains a valid
304304
// pointer to an `AIBinder`.

libs/binder/rust/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub fn wait_for_service(name: &str) -> Option<SpIBinder> {
176176
/// seconds if it doesn't yet exist.
177177
#[deprecated = "this polls 5s, use wait_for_interface or check_interface"]
178178
pub fn get_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong<T>> {
179+
#[allow(deprecated)]
179180
interface_cast(get_service(name))
180181
}
181182

libs/binder/rust/sys/BinderBindings.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
*/
1616

1717
#include <android/binder_ibinder.h>
18+
#include <android/binder_parcel.h>
19+
#include <android/binder_status.h>
20+
21+
/* Platform only */
22+
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
1823
#include <android/binder_ibinder_platform.h>
1924
#include <android/binder_manager.h>
20-
#include <android/binder_parcel.h>
2125
#include <android/binder_parcel_platform.h>
2226
#include <android/binder_process.h>
2327
#include <android/binder_rpc.h>
2428
#include <android/binder_shell.h>
2529
#include <android/binder_stability.h>
26-
#include <android/binder_status.h>
30+
#endif
2731

2832
namespace android {
2933

@@ -81,8 +85,10 @@ enum {
8185

8286
enum {
8387
FLAG_ONEWAY = FLAG_ONEWAY,
88+
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
8489
FLAG_CLEAR_BUF = FLAG_CLEAR_BUF,
8590
FLAG_PRIVATE_LOCAL = FLAG_PRIVATE_LOCAL,
91+
#endif
8692
};
8793

8894
} // namespace consts

0 commit comments

Comments
 (0)