Skip to content

Commit 6f017d5

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "binder: fix decoding of vintf stability ParcelableHolder" into main
2 parents 3bc04bf + e254323 commit 6f017d5

4 files changed

Lines changed: 68 additions & 28 deletions

File tree

libs/binder/rust/src/binder.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@ impl TryFrom<i32> for Stability {
136136
}
137137
}
138138

139+
/// Same as `Stability`, but in the form of a trait. Used when the stability should be encoded in
140+
/// the type.
141+
///
142+
/// When/if the `adt_const_params` Rust feature is stabilized, this could be replace by using
143+
/// `Stability` directly with const generics.
144+
pub trait StabilityType {
145+
/// The `Stability` represented by this type.
146+
const VALUE: Stability;
147+
}
148+
149+
/// `Stability::Local`.
150+
#[derive(Debug)]
151+
pub enum LocalStabilityType {}
152+
/// `Stability::Vintf`.
153+
#[derive(Debug)]
154+
pub enum VintfStabilityType {}
155+
156+
impl StabilityType for LocalStabilityType {
157+
const VALUE: Stability = Stability::Local;
158+
}
159+
160+
impl StabilityType for VintfStabilityType {
161+
const VALUE: Stability = Stability::Vintf;
162+
}
163+
139164
/// A local service that can be remotable via Binder.
140165
///
141166
/// An object that implement this interface made be made into a Binder service

libs/binder/rust/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ pub type Result<T> = std::result::Result<T, Status>;
128128
/// without AIDL.
129129
pub mod binder_impl {
130130
pub use crate::binder::{
131-
IBinderInternal, InterfaceClass, Remotable, Stability, ToAsyncInterface, ToSyncInterface,
132-
TransactionCode, TransactionFlags, FIRST_CALL_TRANSACTION, FLAG_CLEAR_BUF, FLAG_ONEWAY,
133-
FLAG_PRIVATE_LOCAL, LAST_CALL_TRANSACTION,
131+
IBinderInternal, InterfaceClass, LocalStabilityType, Remotable, Stability, StabilityType,
132+
ToAsyncInterface, ToSyncInterface, TransactionCode, TransactionFlags, VintfStabilityType,
133+
FIRST_CALL_TRANSACTION, FLAG_CLEAR_BUF, FLAG_ONEWAY, FLAG_PRIVATE_LOCAL,
134+
LAST_CALL_TRANSACTION,
134135
};
135136
pub use crate::binder_async::BinderAsyncRuntime;
136137
pub use crate::error::status_t;

libs/binder/rust/src/parcel/parcelable_holder.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
use crate::binder::Stability;
18+
use crate::binder::StabilityType;
1819
use crate::error::StatusCode;
1920
use crate::parcel::{
2021
BorrowedParcel, Deserialize, Parcel, Parcelable, Serialize, NON_NULL_PARCELABLE_FLAG,
@@ -60,21 +61,25 @@ enum ParcelableHolderData {
6061
/// `Send` nor `Sync`), mainly because it internally contains
6162
/// a `Parcel` which in turn is not thread-safe.
6263
#[derive(Debug)]
63-
pub struct ParcelableHolder {
64+
pub struct ParcelableHolder<STABILITY: StabilityType> {
6465
// This is a `Mutex` because of `get_parcelable`
6566
// which takes `&self` for consistency with C++.
6667
// We could make `get_parcelable` take a `&mut self`
6768
// and get rid of the `Mutex` here for a performance
6869
// improvement, but then callers would require a mutable
6970
// `ParcelableHolder` even for that getter method.
7071
data: Mutex<ParcelableHolderData>,
71-
stability: Stability,
72+
73+
_stability_phantom: std::marker::PhantomData<STABILITY>,
7274
}
7375

74-
impl ParcelableHolder {
76+
impl<STABILITY: StabilityType> ParcelableHolder<STABILITY> {
7577
/// Construct a new `ParcelableHolder` with the given stability.
76-
pub fn new(stability: Stability) -> Self {
77-
Self { data: Mutex::new(ParcelableHolderData::Empty), stability }
78+
pub fn new() -> Self {
79+
Self {
80+
data: Mutex::new(ParcelableHolderData::Empty),
81+
_stability_phantom: Default::default(),
82+
}
7883
}
7984

8085
/// Reset the contents of this `ParcelableHolder`.
@@ -91,7 +96,7 @@ impl ParcelableHolder {
9196
where
9297
T: Any + Parcelable + ParcelableMetadata + std::fmt::Debug + Send + Sync,
9398
{
94-
if self.stability > p.get_stability() {
99+
if STABILITY::VALUE > p.get_stability() {
95100
return Err(StatusCode::BAD_VALUE);
96101
}
97102

@@ -157,30 +162,36 @@ impl ParcelableHolder {
157162

158163
/// Return the stability value of this object.
159164
pub fn get_stability(&self) -> Stability {
160-
self.stability
165+
STABILITY::VALUE
166+
}
167+
}
168+
169+
impl<STABILITY: StabilityType> Default for ParcelableHolder<STABILITY> {
170+
fn default() -> Self {
171+
Self::new()
161172
}
162173
}
163174

164-
impl Clone for ParcelableHolder {
165-
fn clone(&self) -> ParcelableHolder {
175+
impl<STABILITY: StabilityType> Clone for ParcelableHolder<STABILITY> {
176+
fn clone(&self) -> Self {
166177
ParcelableHolder {
167178
data: Mutex::new(self.data.lock().unwrap().clone()),
168-
stability: self.stability,
179+
_stability_phantom: Default::default(),
169180
}
170181
}
171182
}
172183

173-
impl Serialize for ParcelableHolder {
184+
impl<STABILITY: StabilityType> Serialize for ParcelableHolder<STABILITY> {
174185
fn serialize(&self, parcel: &mut BorrowedParcel<'_>) -> Result<(), StatusCode> {
175186
parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
176187
self.write_to_parcel(parcel)
177188
}
178189
}
179190

180-
impl Deserialize for ParcelableHolder {
191+
impl<STABILITY: StabilityType> Deserialize for ParcelableHolder<STABILITY> {
181192
type UninitType = Self;
182193
fn uninit() -> Self::UninitType {
183-
Self::new(Default::default())
194+
Self::new()
184195
}
185196
fn from_init(value: Self) -> Self::UninitType {
186197
value
@@ -191,16 +202,16 @@ impl Deserialize for ParcelableHolder {
191202
if status == NULL_PARCELABLE_FLAG {
192203
Err(StatusCode::UNEXPECTED_NULL)
193204
} else {
194-
let mut parcelable = ParcelableHolder::new(Default::default());
205+
let mut parcelable = Self::new();
195206
parcelable.read_from_parcel(parcel)?;
196207
Ok(parcelable)
197208
}
198209
}
199210
}
200211

201-
impl Parcelable for ParcelableHolder {
212+
impl<STABILITY: StabilityType> Parcelable for ParcelableHolder<STABILITY> {
202213
fn write_to_parcel(&self, parcel: &mut BorrowedParcel<'_>) -> Result<(), StatusCode> {
203-
parcel.write(&self.stability)?;
214+
parcel.write(&STABILITY::VALUE)?;
204215

205216
let mut data = self.data.lock().unwrap();
206217
match *data {
@@ -236,7 +247,7 @@ impl Parcelable for ParcelableHolder {
236247
}
237248

238249
fn read_from_parcel(&mut self, parcel: &BorrowedParcel<'_>) -> Result<(), StatusCode> {
239-
if self.stability != parcel.read()? {
250+
if self.get_stability() != parcel.read()? {
240251
return Err(StatusCode::BAD_VALUE);
241252
}
242253

libs/binder/rust/tests/parcel_fuzzer/parcel_fuzzer.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ mod read_utils;
2121

2222
use crate::read_utils::READ_FUNCS;
2323
use binder::binder_impl::{
24-
Binder, BorrowedParcel, IBinderInternal, Parcel, Stability, TransactionCode,
24+
Binder, BorrowedParcel, IBinderInternal, LocalStabilityType, Parcel, TransactionCode,
25+
VintfStabilityType,
2526
};
2627
use binder::{
2728
declare_binder_interface, BinderFeatures, Interface, Parcelable, ParcelableHolder, SpIBinder,
@@ -121,13 +122,15 @@ fn do_read_fuzz(read_operations: Vec<ReadOperation>, data: &[u8]) {
121122
}
122123

123124
ReadOperation::ReadParcelableHolder { is_vintf } => {
124-
let stability = if is_vintf { Stability::Vintf } else { Stability::Local };
125-
let mut holder: ParcelableHolder = ParcelableHolder::new(stability);
126-
match holder.read_from_parcel(parcel.borrowed_ref()) {
127-
Ok(result) => result,
128-
Err(err) => {
129-
println!("error occurred while reading from parcel: {:?}", err)
130-
}
125+
let result = if is_vintf {
126+
ParcelableHolder::<VintfStabilityType>::new()
127+
.read_from_parcel(parcel.borrowed_ref())
128+
} else {
129+
ParcelableHolder::<LocalStabilityType>::new()
130+
.read_from_parcel(parcel.borrowed_ref())
131+
};
132+
if let Err(e) = result {
133+
println!("error occurred while reading from parcel: {e:?}")
131134
}
132135
}
133136

0 commit comments

Comments
 (0)