• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use binder::binder_impl::BorrowedParcel;
18 use binder::{ParcelFileDescriptor, Parcelable, SpIBinder};
19 use binderReadParcelIface::aidl::parcelables::EmptyParcelable::EmptyParcelable;
20 use binderReadParcelIface::aidl::parcelables::GenericDataParcelable::GenericDataParcelable;
21 use binderReadParcelIface::aidl::parcelables::SingleDataParcelable::SingleDataParcelable;
22 
23 macro_rules! read_parcel_interface {
24     ($data_type:ty) => {
25         |parcel: &BorrowedParcel<'_>| {
26             let _res = parcel.read::<$data_type>();
27         }
28     };
29 }
30 
31 #[derive(Debug, Default)]
32 pub struct SomeParcelable {
33     pub data: i32,
34 }
35 
36 impl binder::Parcelable for SomeParcelable {
write_to_parcel( &self, parcel: &mut binder::binder_impl::BorrowedParcel, ) -> std::result::Result<(), binder::StatusCode>37     fn write_to_parcel(
38         &self,
39         parcel: &mut binder::binder_impl::BorrowedParcel,
40     ) -> std::result::Result<(), binder::StatusCode> {
41         parcel.sized_write(|subparcel| subparcel.write(&self.data))
42     }
43 
read_from_parcel( &mut self, parcel: &binder::binder_impl::BorrowedParcel, ) -> std::result::Result<(), binder::StatusCode>44     fn read_from_parcel(
45         &mut self,
46         parcel: &binder::binder_impl::BorrowedParcel,
47     ) -> std::result::Result<(), binder::StatusCode> {
48         parcel.sized_read(|subparcel| match subparcel.read() {
49             Ok(result) => {
50                 self.data = result;
51                 Ok(())
52             }
53             Err(e) => Err(e),
54         })
55     }
56 }
57 
58 binder::impl_deserialize_for_parcelable!(SomeParcelable);
59 
60 pub const READ_FUNCS: &[fn(&BorrowedParcel<'_>)] = &[
61     //read basic types
62     read_parcel_interface!(bool),
63     read_parcel_interface!(i8),
64     read_parcel_interface!(i32),
65     read_parcel_interface!(i64),
66     read_parcel_interface!(f32),
67     read_parcel_interface!(f64),
68     read_parcel_interface!(u16),
69     read_parcel_interface!(u32),
70     read_parcel_interface!(u64),
71     read_parcel_interface!(String),
72     //read vec of basic types
73     read_parcel_interface!(Vec<i8>),
74     read_parcel_interface!(Vec<i32>),
75     read_parcel_interface!(Vec<i64>),
76     read_parcel_interface!(Vec<f32>),
77     read_parcel_interface!(Vec<f64>),
78     read_parcel_interface!(Vec<u16>),
79     read_parcel_interface!(Vec<u32>),
80     read_parcel_interface!(Vec<u64>),
81     read_parcel_interface!(Vec<String>),
82     read_parcel_interface!(Option<Vec<i8>>),
83     read_parcel_interface!(Option<Vec<i32>>),
84     read_parcel_interface!(Option<Vec<i64>>),
85     read_parcel_interface!(Option<Vec<f32>>),
86     read_parcel_interface!(Option<Vec<f64>>),
87     read_parcel_interface!(Option<Vec<u16>>),
88     read_parcel_interface!(Option<Vec<u32>>),
89     read_parcel_interface!(Option<Vec<u64>>),
90     read_parcel_interface!(Option<Vec<String>>),
91     read_parcel_interface!(ParcelFileDescriptor),
92     read_parcel_interface!(Vec<Option<ParcelFileDescriptor>>),
93     read_parcel_interface!(Option<Vec<ParcelFileDescriptor>>),
94     read_parcel_interface!(Option<Vec<Option<ParcelFileDescriptor>>>),
95     read_parcel_interface!(SpIBinder),
96     read_parcel_interface!(Vec<Option<SpIBinder>>),
97     read_parcel_interface!(Option<Vec<SpIBinder>>),
98     read_parcel_interface!(Option<Vec<Option<SpIBinder>>>),
99     read_parcel_interface!(SomeParcelable),
100     read_parcel_interface!(Vec<Option<SomeParcelable>>),
101     read_parcel_interface!(Option<Vec<SomeParcelable>>),
102     read_parcel_interface!(Option<Vec<Option<SomeParcelable>>>),
103     // Fuzz read_from_parcel for AIDL generated parcelables
104     |parcel| {
105         let mut empty_parcelable: EmptyParcelable = EmptyParcelable::default();
106         match empty_parcelable.read_from_parcel(parcel) {
107             Ok(result) => result,
108             Err(e) => {
109                 println!("EmptyParcelable: error occurred while reading from a parcel: {:?}", e)
110             }
111         }
112     },
113     |parcel| {
114         let mut single_parcelable: SingleDataParcelable = SingleDataParcelable::default();
115         match single_parcelable.read_from_parcel(parcel) {
116             Ok(result) => result,
117             Err(e) => println!(
118                 "SingleDataParcelable: error occurred while reading from a parcel: {:?}",
119                 e
120             ),
121         }
122     },
123     |parcel| {
124         let mut generic_parcelable: GenericDataParcelable = GenericDataParcelable::default();
125         match generic_parcelable.read_from_parcel(parcel) {
126             Ok(result) => result,
127             Err(e) => println!(
128                 "GenericDataParcelable: error occurred while reading from a parcel: {:?}",
129                 e
130             ),
131         }
132     },
133 ];
134