Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f347cc1 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "binder: fix decoding of vintf stability ParcelableHolder" into main am:...

Merge "binder: fix decoding of vintf stability ParcelableHolder" into main am: 6f017d59 am: 54d92a4f

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/3267798



Change-Id: I7f5f1096f23e371e2db85062412200e6bd7f32cf
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 7a23ce53 54d92a4f
Loading
Loading
Loading
Loading
+25 −0
Original line number Original line Diff line number Diff line
@@ -136,6 +136,31 @@ impl TryFrom<i32> for Stability {
    }
    }
}
}


/// Same as `Stability`, but in the form of a trait. Used when the stability should be encoded in
/// the type.
///
/// When/if the `adt_const_params` Rust feature is stabilized, this could be replace by using
/// `Stability` directly with const generics.
pub trait StabilityType {
    /// The `Stability` represented by this type.
    const VALUE: Stability;
}

/// `Stability::Local`.
#[derive(Debug)]
pub enum LocalStabilityType {}
/// `Stability::Vintf`.
#[derive(Debug)]
pub enum VintfStabilityType {}

impl StabilityType for LocalStabilityType {
    const VALUE: Stability = Stability::Local;
}

impl StabilityType for VintfStabilityType {
    const VALUE: Stability = Stability::Vintf;
}

/// A local service that can be remotable via Binder.
/// A local service that can be remotable via Binder.
///
///
/// An object that implement this interface made be made into a Binder service
/// An object that implement this interface made be made into a Binder service
+4 −3
Original line number Original line Diff line number Diff line
@@ -128,9 +128,10 @@ pub type Result<T> = std::result::Result<T, Status>;
/// without AIDL.
/// without AIDL.
pub mod binder_impl {
pub mod binder_impl {
    pub use crate::binder::{
    pub use crate::binder::{
        IBinderInternal, InterfaceClass, Remotable, Stability, ToAsyncInterface, ToSyncInterface,
        IBinderInternal, InterfaceClass, LocalStabilityType, Remotable, Stability, StabilityType,
        TransactionCode, TransactionFlags, FIRST_CALL_TRANSACTION, FLAG_CLEAR_BUF, FLAG_ONEWAY,
        ToAsyncInterface, ToSyncInterface, TransactionCode, TransactionFlags, VintfStabilityType,
        FLAG_PRIVATE_LOCAL, LAST_CALL_TRANSACTION,
        FIRST_CALL_TRANSACTION, FLAG_CLEAR_BUF, FLAG_ONEWAY, FLAG_PRIVATE_LOCAL,
        LAST_CALL_TRANSACTION,
    };
    };
    pub use crate::binder_async::BinderAsyncRuntime;
    pub use crate::binder_async::BinderAsyncRuntime;
    pub use crate::error::status_t;
    pub use crate::error::status_t;
+28 −17
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@
 */
 */


use crate::binder::Stability;
use crate::binder::Stability;
use crate::binder::StabilityType;
use crate::error::StatusCode;
use crate::error::StatusCode;
use crate::parcel::{
use crate::parcel::{
    BorrowedParcel, Deserialize, Parcel, Parcelable, Serialize, NON_NULL_PARCELABLE_FLAG,
    BorrowedParcel, Deserialize, Parcel, Parcelable, Serialize, NON_NULL_PARCELABLE_FLAG,
@@ -60,7 +61,7 @@ enum ParcelableHolderData {
/// `Send` nor `Sync`), mainly because it internally contains
/// `Send` nor `Sync`), mainly because it internally contains
/// a `Parcel` which in turn is not thread-safe.
/// a `Parcel` which in turn is not thread-safe.
#[derive(Debug)]
#[derive(Debug)]
pub struct ParcelableHolder {
pub struct ParcelableHolder<STABILITY: StabilityType> {
    // This is a `Mutex` because of `get_parcelable`
    // This is a `Mutex` because of `get_parcelable`
    // which takes `&self` for consistency with C++.
    // which takes `&self` for consistency with C++.
    // We could make `get_parcelable` take a `&mut self`
    // We could make `get_parcelable` take a `&mut self`
@@ -68,13 +69,17 @@ pub struct ParcelableHolder {
    // improvement, but then callers would require a mutable
    // improvement, but then callers would require a mutable
    // `ParcelableHolder` even for that getter method.
    // `ParcelableHolder` even for that getter method.
    data: Mutex<ParcelableHolderData>,
    data: Mutex<ParcelableHolderData>,
    stability: Stability,

    _stability_phantom: std::marker::PhantomData<STABILITY>,
}
}


impl ParcelableHolder {
impl<STABILITY: StabilityType> ParcelableHolder<STABILITY> {
    /// Construct a new `ParcelableHolder` with the given stability.
    /// Construct a new `ParcelableHolder` with the given stability.
    pub fn new(stability: Stability) -> Self {
    pub fn new() -> Self {
        Self { data: Mutex::new(ParcelableHolderData::Empty), stability }
        Self {
            data: Mutex::new(ParcelableHolderData::Empty),
            _stability_phantom: Default::default(),
        }
    }
    }


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


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


    /// Return the stability value of this object.
    /// Return the stability value of this object.
    pub fn get_stability(&self) -> Stability {
    pub fn get_stability(&self) -> Stability {
        self.stability
        STABILITY::VALUE
    }
}

impl<STABILITY: StabilityType> Default for ParcelableHolder<STABILITY> {
    fn default() -> Self {
        Self::new()
    }
    }
}
}


impl Clone for ParcelableHolder {
impl<STABILITY: StabilityType> Clone for ParcelableHolder<STABILITY> {
    fn clone(&self) -> ParcelableHolder {
    fn clone(&self) -> Self {
        ParcelableHolder {
        ParcelableHolder {
            data: Mutex::new(self.data.lock().unwrap().clone()),
            data: Mutex::new(self.data.lock().unwrap().clone()),
            stability: self.stability,
            _stability_phantom: Default::default(),
        }
        }
    }
    }
}
}


impl Serialize for ParcelableHolder {
impl<STABILITY: StabilityType> Serialize for ParcelableHolder<STABILITY> {
    fn serialize(&self, parcel: &mut BorrowedParcel<'_>) -> Result<(), StatusCode> {
    fn serialize(&self, parcel: &mut BorrowedParcel<'_>) -> Result<(), StatusCode> {
        parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
        parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
        self.write_to_parcel(parcel)
        self.write_to_parcel(parcel)
    }
    }
}
}


impl Deserialize for ParcelableHolder {
impl<STABILITY: StabilityType> Deserialize for ParcelableHolder<STABILITY> {
    type UninitType = Self;
    type UninitType = Self;
    fn uninit() -> Self::UninitType {
    fn uninit() -> Self::UninitType {
        Self::new(Default::default())
        Self::new()
    }
    }
    fn from_init(value: Self) -> Self::UninitType {
    fn from_init(value: Self) -> Self::UninitType {
        value
        value
@@ -191,16 +202,16 @@ impl Deserialize for ParcelableHolder {
        if status == NULL_PARCELABLE_FLAG {
        if status == NULL_PARCELABLE_FLAG {
            Err(StatusCode::UNEXPECTED_NULL)
            Err(StatusCode::UNEXPECTED_NULL)
        } else {
        } else {
            let mut parcelable = ParcelableHolder::new(Default::default());
            let mut parcelable = Self::new();
            parcelable.read_from_parcel(parcel)?;
            parcelable.read_from_parcel(parcel)?;
            Ok(parcelable)
            Ok(parcelable)
        }
        }
    }
    }
}
}


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


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


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


+11 −8
Original line number Original line Diff line number Diff line
@@ -21,7 +21,8 @@ mod read_utils;


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


            ReadOperation::ReadParcelableHolder { is_vintf } => {
            ReadOperation::ReadParcelableHolder { is_vintf } => {
                let stability = if is_vintf { Stability::Vintf } else { Stability::Local };
                let result = if is_vintf {
                let mut holder: ParcelableHolder = ParcelableHolder::new(stability);
                    ParcelableHolder::<VintfStabilityType>::new()
                match holder.read_from_parcel(parcel.borrowed_ref()) {
                        .read_from_parcel(parcel.borrowed_ref())
                    Ok(result) => result,
                } else {
                    Err(err) => {
                    ParcelableHolder::<LocalStabilityType>::new()
                        println!("error occurred while reading from parcel: {:?}", err)
                        .read_from_parcel(parcel.borrowed_ref())
                    }
                };
                if let Err(e) = result {
                    println!("error occurred while reading from parcel: {e:?}")
                }
                }
            }
            }