| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 | //! Container for messages that are sent via binder. | 
|  | 18 |  | 
|  | 19 | use crate::binder::AsNative; | 
|  | 20 | use crate::error::{status_result, Result, StatusCode}; | 
|  | 21 | use crate::proxy::SpIBinder; | 
|  | 22 | use crate::sys; | 
|  | 23 |  | 
| Stephen Crane | aae7638 | 2020-08-03 14:12:15 -0700 | [diff] [blame] | 24 | use std::cell::RefCell; | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 25 | use std::convert::TryInto; | 
|  | 26 | use std::mem::ManuallyDrop; | 
|  | 27 | use std::ptr; | 
|  | 28 |  | 
|  | 29 | mod file_descriptor; | 
|  | 30 | mod parcelable; | 
|  | 31 |  | 
|  | 32 | pub use self::file_descriptor::ParcelFileDescriptor; | 
|  | 33 | pub use self::parcelable::{ | 
|  | 34 | Deserialize, DeserializeArray, DeserializeOption, Serialize, SerializeArray, SerializeOption, | 
|  | 35 | }; | 
|  | 36 |  | 
|  | 37 | /// Container for a message (data and object references) that can be sent | 
|  | 38 | /// through Binder. | 
|  | 39 | /// | 
|  | 40 | /// A Parcel can contain both serialized data that will be deserialized on the | 
|  | 41 | /// other side of the IPC, and references to live Binder objects that will | 
|  | 42 | /// result in the other side receiving a proxy Binder connected with the | 
|  | 43 | /// original Binder in the Parcel. | 
|  | 44 | pub enum Parcel { | 
|  | 45 | /// Owned parcel pointer | 
|  | 46 | Owned(*mut sys::AParcel), | 
|  | 47 | /// Borrowed parcel pointer (will not be destroyed on drop) | 
|  | 48 | Borrowed(*mut sys::AParcel), | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | /// # Safety | 
|  | 52 | /// | 
|  | 53 | /// The `Parcel` constructors guarantee that a `Parcel` object will always | 
|  | 54 | /// contain a valid pointer to an `AParcel`. | 
|  | 55 | unsafe impl AsNative<sys::AParcel> for Parcel { | 
|  | 56 | fn as_native(&self) -> *const sys::AParcel { | 
|  | 57 | match *self { | 
|  | 58 | Self::Owned(x) | Self::Borrowed(x) => x, | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | fn as_native_mut(&mut self) -> *mut sys::AParcel { | 
|  | 63 | match *self { | 
|  | 64 | Self::Owned(x) | Self::Borrowed(x) => x, | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | impl Parcel { | 
|  | 70 | /// Create a borrowed reference to a parcel object from a raw pointer. | 
|  | 71 | /// | 
|  | 72 | /// # Safety | 
|  | 73 | /// | 
|  | 74 | /// This constructor is safe if the raw pointer parameter is either null | 
|  | 75 | /// (resulting in `None`), or a valid pointer to an `AParcel` object. | 
|  | 76 | pub(crate) unsafe fn borrowed(ptr: *mut sys::AParcel) -> Option<Parcel> { | 
|  | 77 | ptr.as_mut().map(|ptr| Self::Borrowed(ptr)) | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | /// Create an owned reference to a parcel object from a raw pointer. | 
|  | 81 | /// | 
|  | 82 | /// # Safety | 
|  | 83 | /// | 
|  | 84 | /// This constructor is safe if the raw pointer parameter is either null | 
|  | 85 | /// (resulting in `None`), or a valid pointer to an `AParcel` object. The | 
|  | 86 | /// parcel object must be owned by the caller prior to this call, as this | 
|  | 87 | /// constructor takes ownership of the parcel and will destroy it on drop. | 
|  | 88 | pub(crate) unsafe fn owned(ptr: *mut sys::AParcel) -> Option<Parcel> { | 
|  | 89 | ptr.as_mut().map(|ptr| Self::Owned(ptr)) | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | /// Consume the parcel, transferring ownership to the caller if the parcel | 
|  | 93 | /// was owned. | 
|  | 94 | pub(crate) fn into_raw(mut self) -> *mut sys::AParcel { | 
|  | 95 | let ptr = self.as_native_mut(); | 
|  | 96 | let _ = ManuallyDrop::new(self); | 
|  | 97 | ptr | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | // Data serialization methods | 
|  | 102 | impl Parcel { | 
| Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 103 | /// Data written to parcelable is zero'd before being deleted or reallocated. | 
|  | 104 | pub fn mark_sensitive(&mut self) { | 
|  | 105 | unsafe { | 
|  | 106 | // Safety: guaranteed to have a parcel object, and this method never fails | 
|  | 107 | sys::AParcel_markSensitive(self.as_native()) | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 111 | /// Write a type that implements [`Serialize`] to the `Parcel`. | 
|  | 112 | pub fn write<S: Serialize + ?Sized>(&mut self, parcelable: &S) -> Result<()> { | 
|  | 113 | parcelable.serialize(self) | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | /// Writes the length of a slice to the `Parcel`. | 
|  | 117 | /// | 
|  | 118 | /// This is used in AIDL-generated client side code to indicate the | 
|  | 119 | /// allocated space for an output array parameter. | 
|  | 120 | pub fn write_slice_size<T>(&mut self, slice: Option<&[T]>) -> Result<()> { | 
|  | 121 | if let Some(slice) = slice { | 
|  | 122 | let len: i32 = slice.len().try_into().or(Err(StatusCode::BAD_VALUE))?; | 
|  | 123 | self.write(&len) | 
|  | 124 | } else { | 
|  | 125 | self.write(&-1i32) | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 |  | 
| Stephen Crane | aae7638 | 2020-08-03 14:12:15 -0700 | [diff] [blame] | 129 | /// Perform a series of writes to the `Parcel`, prepended with the length | 
|  | 130 | /// (in bytes) of the written data. | 
|  | 131 | /// | 
|  | 132 | /// The length `0i32` will be written to the parcel first, followed by the | 
|  | 133 | /// writes performed by the callback. The initial length will then be | 
|  | 134 | /// updated to the length of all data written by the callback, plus the | 
|  | 135 | /// size of the length elemement itself (4 bytes). | 
|  | 136 | /// | 
|  | 137 | /// # Examples | 
|  | 138 | /// | 
|  | 139 | /// After the following call: | 
|  | 140 | /// | 
|  | 141 | /// ``` | 
|  | 142 | /// # use binder::{Binder, Interface, Parcel}; | 
|  | 143 | /// # let mut parcel = Parcel::Owned(std::ptr::null_mut()); | 
|  | 144 | /// parcel.sized_write(|subparcel| { | 
|  | 145 | ///     subparcel.write(&1u32)?; | 
|  | 146 | ///     subparcel.write(&2u32)?; | 
|  | 147 | ///     subparcel.write(&3u32) | 
|  | 148 | /// }); | 
|  | 149 | /// ``` | 
|  | 150 | /// | 
|  | 151 | /// `parcel` will contain the following: | 
|  | 152 | /// | 
|  | 153 | /// ```ignore | 
|  | 154 | /// [16i32, 1u32, 2u32, 3u32] | 
|  | 155 | /// ``` | 
|  | 156 | pub fn sized_write<F>(&mut self, f: F) -> Result<()> | 
|  | 157 | where for<'a> | 
|  | 158 | F: Fn(&'a WritableSubParcel<'a>) -> Result<()> | 
|  | 159 | { | 
|  | 160 | let start = self.get_data_position(); | 
|  | 161 | self.write(&0i32)?; | 
|  | 162 | { | 
|  | 163 | let subparcel = WritableSubParcel(RefCell::new(self)); | 
|  | 164 | f(&subparcel)?; | 
|  | 165 | } | 
|  | 166 | let end = self.get_data_position(); | 
|  | 167 | unsafe { | 
|  | 168 | self.set_data_position(start)?; | 
|  | 169 | } | 
|  | 170 | assert!(end >= start); | 
|  | 171 | self.write(&(end - start))?; | 
|  | 172 | unsafe { | 
|  | 173 | self.set_data_position(end)?; | 
|  | 174 | } | 
|  | 175 | Ok(()) | 
|  | 176 | } | 
|  | 177 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 178 | /// Returns the current position in the parcel data. | 
|  | 179 | pub fn get_data_position(&self) -> i32 { | 
|  | 180 | unsafe { | 
|  | 181 | // Safety: `Parcel` always contains a valid pointer to an `AParcel`, | 
|  | 182 | // and this call is otherwise safe. | 
|  | 183 | sys::AParcel_getDataPosition(self.as_native()) | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
| Andrei Homescu | b048744 | 2021-05-12 07:16:16 +0000 | [diff] [blame] | 187 | /// Returns the total size of the parcel. | 
|  | 188 | pub fn get_data_size(&self) -> i32 { | 
|  | 189 | unsafe { | 
|  | 190 | // Safety: `Parcel` always contains a valid pointer to an `AParcel`, | 
|  | 191 | // and this call is otherwise safe. | 
|  | 192 | sys::AParcel_getDataSize(self.as_native()) | 
|  | 193 | } | 
|  | 194 | } | 
|  | 195 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 196 | /// Move the current read/write position in the parcel. | 
|  | 197 | /// | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 198 | /// # Safety | 
|  | 199 | /// | 
|  | 200 | /// This method is safe if `pos` is less than the current size of the parcel | 
|  | 201 | /// data buffer. Otherwise, we are relying on correct bounds checking in the | 
|  | 202 | /// Parcel C++ code on every subsequent read or write to this parcel. If all | 
|  | 203 | /// accesses are bounds checked, this call is still safe, but we can't rely | 
|  | 204 | /// on that. | 
|  | 205 | pub unsafe fn set_data_position(&self, pos: i32) -> Result<()> { | 
|  | 206 | status_result(sys::AParcel_setDataPosition(self.as_native(), pos)) | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 |  | 
| Stephen Crane | aae7638 | 2020-08-03 14:12:15 -0700 | [diff] [blame] | 210 | /// A segment of a writable parcel, used for [`Parcel::sized_write`]. | 
|  | 211 | pub struct WritableSubParcel<'a>(RefCell<&'a mut Parcel>); | 
|  | 212 |  | 
|  | 213 | impl<'a> WritableSubParcel<'a> { | 
|  | 214 | /// Write a type that implements [`Serialize`] to the sub-parcel. | 
|  | 215 | pub fn write<S: Serialize + ?Sized>(&self, parcelable: &S) -> Result<()> { | 
|  | 216 | parcelable.serialize(&mut *self.0.borrow_mut()) | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 220 | // Data deserialization methods | 
|  | 221 | impl Parcel { | 
|  | 222 | /// Attempt to read a type that implements [`Deserialize`] from this | 
|  | 223 | /// `Parcel`. | 
|  | 224 | pub fn read<D: Deserialize>(&self) -> Result<D> { | 
|  | 225 | D::deserialize(self) | 
|  | 226 | } | 
|  | 227 |  | 
| Andrei Homescu | 5000615 | 2021-05-01 07:34:51 +0000 | [diff] [blame] | 228 | /// Attempt to read a type that implements [`Deserialize`] from this | 
|  | 229 | /// `Parcel` onto an existing value. This operation will overwrite the old | 
|  | 230 | /// value partially or completely, depending on how much data is available. | 
|  | 231 | pub fn read_onto<D: Deserialize>(&self, x: &mut D) -> Result<()> { | 
|  | 232 | x.deserialize_from(self) | 
|  | 233 | } | 
|  | 234 |  | 
| Andrei Homescu | b048744 | 2021-05-12 07:16:16 +0000 | [diff] [blame] | 235 | /// Safely read a sized parcelable. | 
|  | 236 | /// | 
|  | 237 | /// Read the size of a parcelable, compute the end position | 
|  | 238 | /// of that parcelable, then build a sized readable sub-parcel | 
|  | 239 | /// and call a closure with the sub-parcel as its parameter. | 
|  | 240 | /// The closure can keep reading data from the sub-parcel | 
|  | 241 | /// until it runs out of input data. The closure is responsible | 
|  | 242 | /// for calling [`ReadableSubParcel::has_more_data`] to check for | 
|  | 243 | /// more data before every read, at least until Rust generators | 
|  | 244 | /// are stabilized. | 
|  | 245 | /// After the closure returns, skip to the end of the current | 
|  | 246 | /// parcelable regardless of how much the closure has read. | 
|  | 247 | /// | 
|  | 248 | /// # Examples | 
|  | 249 | /// | 
|  | 250 | /// ```no_run | 
|  | 251 | /// let mut parcelable = Default::default(); | 
|  | 252 | /// parcel.sized_read(|subparcel| { | 
|  | 253 | ///     if subparcel.has_more_data() { | 
|  | 254 | ///         parcelable.a = subparcel.read()?; | 
|  | 255 | ///     } | 
|  | 256 | ///     if subparcel.has_more_data() { | 
|  | 257 | ///         parcelable.b = subparcel.read()?; | 
|  | 258 | ///     } | 
|  | 259 | ///     Ok(()) | 
|  | 260 | /// }); | 
|  | 261 | /// ``` | 
|  | 262 | /// | 
|  | 263 | pub fn sized_read<F>(&self, mut f: F) -> Result<()> | 
|  | 264 | where | 
|  | 265 | for<'a> F: FnMut(ReadableSubParcel<'a>) -> Result<()> | 
|  | 266 | { | 
|  | 267 | let start = self.get_data_position(); | 
|  | 268 | let parcelable_size: i32 = self.read()?; | 
|  | 269 | if parcelable_size < 0 { | 
|  | 270 | return Err(StatusCode::BAD_VALUE); | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | let end = start.checked_add(parcelable_size) | 
|  | 274 | .ok_or(StatusCode::BAD_VALUE)?; | 
|  | 275 | if end > self.get_data_size() { | 
|  | 276 | return Err(StatusCode::NOT_ENOUGH_DATA); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | let subparcel = ReadableSubParcel { | 
|  | 280 | parcel: self, | 
|  | 281 | end_position: end, | 
|  | 282 | }; | 
|  | 283 | f(subparcel)?; | 
|  | 284 |  | 
|  | 285 | // Advance the data position to the actual end, | 
|  | 286 | // in case the closure read less data than was available | 
|  | 287 | unsafe { | 
|  | 288 | self.set_data_position(end)?; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | Ok(()) | 
|  | 292 | } | 
|  | 293 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 294 | /// Read a vector size from the `Parcel` and resize the given output vector | 
|  | 295 | /// to be correctly sized for that amount of data. | 
|  | 296 | /// | 
|  | 297 | /// This method is used in AIDL-generated server side code for methods that | 
|  | 298 | /// take a mutable slice reference parameter. | 
|  | 299 | pub fn resize_out_vec<D: Default + Deserialize>(&self, out_vec: &mut Vec<D>) -> Result<()> { | 
|  | 300 | let len: i32 = self.read()?; | 
|  | 301 |  | 
|  | 302 | if len < 0 { | 
|  | 303 | return Err(StatusCode::UNEXPECTED_NULL); | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | // usize in Rust may be 16-bit, so i32 may not fit | 
|  | 307 | let len = len.try_into().unwrap(); | 
|  | 308 | out_vec.resize_with(len, Default::default); | 
|  | 309 |  | 
|  | 310 | Ok(()) | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | /// Read a vector size from the `Parcel` and either create a correctly sized | 
|  | 314 | /// vector for that amount of data or set the output parameter to None if | 
|  | 315 | /// the vector should be null. | 
|  | 316 | /// | 
|  | 317 | /// This method is used in AIDL-generated server side code for methods that | 
|  | 318 | /// take a mutable slice reference parameter. | 
|  | 319 | pub fn resize_nullable_out_vec<D: Default + Deserialize>( | 
|  | 320 | &self, | 
|  | 321 | out_vec: &mut Option<Vec<D>>, | 
|  | 322 | ) -> Result<()> { | 
|  | 323 | let len: i32 = self.read()?; | 
|  | 324 |  | 
|  | 325 | if len < 0 { | 
|  | 326 | *out_vec = None; | 
|  | 327 | } else { | 
|  | 328 | // usize in Rust may be 16-bit, so i32 may not fit | 
|  | 329 | let len = len.try_into().unwrap(); | 
|  | 330 | let mut vec = Vec::with_capacity(len); | 
|  | 331 | vec.resize_with(len, Default::default); | 
|  | 332 | *out_vec = Some(vec); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | Ok(()) | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
| Andrei Homescu | b048744 | 2021-05-12 07:16:16 +0000 | [diff] [blame] | 339 | /// A segment of a readable parcel, used for [`Parcel::sized_read`]. | 
|  | 340 | pub struct ReadableSubParcel<'a> { | 
|  | 341 | parcel: &'a Parcel, | 
|  | 342 | end_position: i32, | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | impl<'a> ReadableSubParcel<'a> { | 
|  | 346 | /// Read a type that implements [`Deserialize`] from the sub-parcel. | 
|  | 347 | pub fn read<D: Deserialize>(&self) -> Result<D> { | 
|  | 348 | // The caller should have checked this, | 
|  | 349 | // but it can't hurt to double-check | 
|  | 350 | assert!(self.has_more_data()); | 
|  | 351 | D::deserialize(self.parcel) | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | /// Check if the sub-parcel has more data to read | 
|  | 355 | pub fn has_more_data(&self) -> bool { | 
|  | 356 | self.parcel.get_data_position() < self.end_position | 
|  | 357 | } | 
|  | 358 | } | 
|  | 359 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 360 | // Internal APIs | 
|  | 361 | impl Parcel { | 
|  | 362 | pub(crate) fn write_binder(&mut self, binder: Option<&SpIBinder>) -> Result<()> { | 
|  | 363 | unsafe { | 
|  | 364 | // Safety: `Parcel` always contains a valid pointer to an | 
|  | 365 | // `AParcel`. `AsNative` for `Option<SpIBinder`> will either return | 
|  | 366 | // null or a valid pointer to an `AIBinder`, both of which are | 
|  | 367 | // valid, safe inputs to `AParcel_writeStrongBinder`. | 
|  | 368 | // | 
|  | 369 | // This call does not take ownership of the binder. However, it does | 
|  | 370 | // require a mutable pointer, which we cannot extract from an | 
|  | 371 | // immutable reference, so we clone the binder, incrementing the | 
|  | 372 | // refcount before the call. The refcount will be immediately | 
|  | 373 | // decremented when this temporary is dropped. | 
|  | 374 | status_result(sys::AParcel_writeStrongBinder( | 
|  | 375 | self.as_native_mut(), | 
|  | 376 | binder.cloned().as_native_mut(), | 
|  | 377 | )) | 
|  | 378 | } | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | pub(crate) fn read_binder(&self) -> Result<Option<SpIBinder>> { | 
|  | 382 | let mut binder = ptr::null_mut(); | 
|  | 383 | let status = unsafe { | 
|  | 384 | // Safety: `Parcel` always contains a valid pointer to an | 
|  | 385 | // `AParcel`. We pass a valid, mutable out pointer to the `binder` | 
|  | 386 | // parameter. After this call, `binder` will be either null or a | 
|  | 387 | // valid pointer to an `AIBinder` owned by the caller. | 
|  | 388 | sys::AParcel_readStrongBinder(self.as_native(), &mut binder) | 
|  | 389 | }; | 
|  | 390 |  | 
|  | 391 | status_result(status)?; | 
|  | 392 |  | 
|  | 393 | Ok(unsafe { | 
|  | 394 | // Safety: `binder` is either null or a valid, owned pointer at this | 
|  | 395 | // point, so can be safely passed to `SpIBinder::from_raw`. | 
|  | 396 | SpIBinder::from_raw(binder) | 
|  | 397 | }) | 
|  | 398 | } | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | impl Drop for Parcel { | 
|  | 402 | fn drop(&mut self) { | 
|  | 403 | // Run the C++ Parcel complete object destructor | 
|  | 404 | if let Self::Owned(ptr) = *self { | 
|  | 405 | unsafe { | 
|  | 406 | // Safety: `Parcel` always contains a valid pointer to an | 
|  | 407 | // `AParcel`. If we own the parcel, we can safely delete it | 
|  | 408 | // here. | 
|  | 409 | sys::AParcel_delete(ptr) | 
|  | 410 | } | 
|  | 411 | } | 
|  | 412 | } | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | #[cfg(test)] | 
|  | 416 | impl Parcel { | 
|  | 417 | /// Create a new parcel tied to a bogus binder. TESTING ONLY! | 
|  | 418 | /// | 
|  | 419 | /// This can only be used for testing! All real parcel operations must be | 
|  | 420 | /// done in the callback to [`IBinder::transact`] or in | 
|  | 421 | /// [`Remotable::on_transact`] using the parcels provided to these methods. | 
|  | 422 | pub(crate) fn new_for_test(binder: &mut SpIBinder) -> Result<Self> { | 
|  | 423 | let mut input = ptr::null_mut(); | 
|  | 424 | let status = unsafe { | 
|  | 425 | // Safety: `SpIBinder` guarantees that `binder` always contains a | 
|  | 426 | // valid pointer to an `AIBinder`. We pass a valid, mutable out | 
|  | 427 | // pointer to receive a newly constructed parcel. When successful | 
|  | 428 | // this function assigns a new pointer to an `AParcel` to `input` | 
|  | 429 | // and transfers ownership of this pointer to the caller. Thus, | 
|  | 430 | // after this call, `input` will either be null or point to a valid, | 
|  | 431 | // owned `AParcel`. | 
|  | 432 | sys::AIBinder_prepareTransaction(binder.as_native_mut(), &mut input) | 
|  | 433 | }; | 
|  | 434 | status_result(status)?; | 
|  | 435 | unsafe { | 
|  | 436 | // Safety: `input` is either null or a valid, owned pointer to an | 
|  | 437 | // `AParcel`, so is valid to safe to | 
|  | 438 | // `Parcel::owned`. `Parcel::owned` takes ownership of the parcel | 
|  | 439 | // pointer. | 
|  | 440 | Parcel::owned(input).ok_or(StatusCode::UNEXPECTED_NULL) | 
|  | 441 | } | 
|  | 442 | } | 
|  | 443 | } | 
|  | 444 |  | 
|  | 445 | #[test] | 
|  | 446 | fn test_read_write() { | 
|  | 447 | use crate::binder::Interface; | 
|  | 448 | use crate::native::Binder; | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 449 |  | 
|  | 450 | let mut service = Binder::new(()).as_binder(); | 
|  | 451 | let mut parcel = Parcel::new_for_test(&mut service).unwrap(); | 
|  | 452 | let start = parcel.get_data_position(); | 
|  | 453 |  | 
|  | 454 | assert_eq!(parcel.read::<bool>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 455 | assert_eq!(parcel.read::<i8>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 456 | assert_eq!(parcel.read::<u16>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 457 | assert_eq!(parcel.read::<i32>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 458 | assert_eq!(parcel.read::<u32>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 459 | assert_eq!(parcel.read::<i64>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 460 | assert_eq!(parcel.read::<u64>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 461 | assert_eq!(parcel.read::<f32>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
|  | 462 | assert_eq!(parcel.read::<f64>(), Err(StatusCode::NOT_ENOUGH_DATA)); | 
| Stephen Crane | 76072e8 | 2020-08-03 13:09:36 -0700 | [diff] [blame] | 463 | assert_eq!(parcel.read::<Option<String>>(), Ok(None)); | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 464 | assert_eq!(parcel.read::<String>(), Err(StatusCode::UNEXPECTED_NULL)); | 
|  | 465 |  | 
|  | 466 | assert_eq!(parcel.read_binder().err(), Some(StatusCode::BAD_TYPE)); | 
|  | 467 |  | 
|  | 468 | parcel.write(&1i32).unwrap(); | 
|  | 469 |  | 
|  | 470 | unsafe { | 
|  | 471 | parcel.set_data_position(start).unwrap(); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | let i: i32 = parcel.read().unwrap(); | 
|  | 475 | assert_eq!(i, 1i32); | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | #[test] | 
|  | 479 | #[allow(clippy::float_cmp)] | 
|  | 480 | fn test_read_data() { | 
|  | 481 | use crate::binder::Interface; | 
|  | 482 | use crate::native::Binder; | 
|  | 483 |  | 
|  | 484 | let mut service = Binder::new(()).as_binder(); | 
|  | 485 | let mut parcel = Parcel::new_for_test(&mut service).unwrap(); | 
|  | 486 | let str_start = parcel.get_data_position(); | 
|  | 487 |  | 
|  | 488 | parcel.write(&b"Hello, Binder!\0"[..]).unwrap(); | 
|  | 489 | // Skip over string length | 
|  | 490 | unsafe { | 
|  | 491 | assert!(parcel.set_data_position(str_start).is_ok()); | 
|  | 492 | } | 
|  | 493 | assert_eq!(parcel.read::<i32>().unwrap(), 15); | 
|  | 494 | let start = parcel.get_data_position(); | 
|  | 495 |  | 
|  | 496 | assert_eq!(parcel.read::<bool>().unwrap(), true); | 
|  | 497 |  | 
|  | 498 | unsafe { | 
|  | 499 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | assert_eq!(parcel.read::<i8>().unwrap(), 72i8); | 
|  | 503 |  | 
|  | 504 | unsafe { | 
|  | 505 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | assert_eq!(parcel.read::<u16>().unwrap(), 25928); | 
|  | 509 |  | 
|  | 510 | unsafe { | 
|  | 511 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | assert_eq!(parcel.read::<i32>().unwrap(), 1819043144); | 
|  | 515 |  | 
|  | 516 | unsafe { | 
|  | 517 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | assert_eq!(parcel.read::<u32>().unwrap(), 1819043144); | 
|  | 521 |  | 
|  | 522 | unsafe { | 
|  | 523 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | assert_eq!(parcel.read::<i64>().unwrap(), 4764857262830019912); | 
|  | 527 |  | 
|  | 528 | unsafe { | 
|  | 529 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | assert_eq!(parcel.read::<u64>().unwrap(), 4764857262830019912); | 
|  | 533 |  | 
|  | 534 | unsafe { | 
|  | 535 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | assert_eq!( | 
|  | 539 | parcel.read::<f32>().unwrap(), | 
|  | 540 | 1143139100000000000000000000.0 | 
|  | 541 | ); | 
|  | 542 | assert_eq!(parcel.read::<f32>().unwrap(), 40.043392); | 
|  | 543 |  | 
|  | 544 | unsafe { | 
|  | 545 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | assert_eq!(parcel.read::<f64>().unwrap(), 34732488246.197815); | 
|  | 549 |  | 
|  | 550 | // Skip back to before the string length | 
|  | 551 | unsafe { | 
|  | 552 | assert!(parcel.set_data_position(str_start).is_ok()); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | assert_eq!(parcel.read::<Vec<u8>>().unwrap(), b"Hello, Binder!\0"); | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | #[test] | 
|  | 559 | fn test_utf8_utf16_conversions() { | 
|  | 560 | use crate::binder::Interface; | 
|  | 561 | use crate::native::Binder; | 
|  | 562 |  | 
|  | 563 | let mut service = Binder::new(()).as_binder(); | 
|  | 564 | let mut parcel = Parcel::new_for_test(&mut service).unwrap(); | 
|  | 565 | let start = parcel.get_data_position(); | 
|  | 566 |  | 
|  | 567 | assert!(parcel.write("Hello, Binder!").is_ok()); | 
|  | 568 | unsafe { | 
|  | 569 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 570 | } | 
|  | 571 | assert_eq!( | 
|  | 572 | parcel.read::<Option<String>>().unwrap().unwrap(), | 
| Stephen Crane | 76072e8 | 2020-08-03 13:09:36 -0700 | [diff] [blame] | 573 | "Hello, Binder!", | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 574 | ); | 
|  | 575 | unsafe { | 
|  | 576 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 577 | } | 
| Stephen Crane | 76072e8 | 2020-08-03 13:09:36 -0700 | [diff] [blame] | 578 |  | 
|  | 579 | assert!(parcel.write("Embedded null \0 inside a string").is_ok()); | 
|  | 580 | unsafe { | 
|  | 581 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 582 | } | 
|  | 583 | assert_eq!( | 
|  | 584 | parcel.read::<Option<String>>().unwrap().unwrap(), | 
|  | 585 | "Embedded null \0 inside a string", | 
|  | 586 | ); | 
|  | 587 | unsafe { | 
|  | 588 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 589 | } | 
|  | 590 |  | 
| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 591 | assert!(parcel.write(&["str1", "str2", "str3"][..]).is_ok()); | 
|  | 592 | assert!(parcel | 
|  | 593 | .write( | 
|  | 594 | &[ | 
|  | 595 | String::from("str4"), | 
|  | 596 | String::from("str5"), | 
|  | 597 | String::from("str6"), | 
|  | 598 | ][..] | 
|  | 599 | ) | 
|  | 600 | .is_ok()); | 
|  | 601 |  | 
|  | 602 | let s1 = "Hello, Binder!"; | 
|  | 603 | let s2 = "This is a utf8 string."; | 
|  | 604 | let s3 = "Some more text here."; | 
|  | 605 |  | 
|  | 606 | assert!(parcel.write(&[s1, s2, s3][..]).is_ok()); | 
|  | 607 | unsafe { | 
|  | 608 | assert!(parcel.set_data_position(start).is_ok()); | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | assert_eq!( | 
|  | 612 | parcel.read::<Vec<String>>().unwrap(), | 
|  | 613 | ["str1", "str2", "str3"] | 
|  | 614 | ); | 
|  | 615 | assert_eq!( | 
|  | 616 | parcel.read::<Vec<String>>().unwrap(), | 
|  | 617 | ["str4", "str5", "str6"] | 
|  | 618 | ); | 
|  | 619 | assert_eq!(parcel.read::<Vec<String>>().unwrap(), [s1, s2, s3]); | 
|  | 620 | } | 
| Stephen Crane | aae7638 | 2020-08-03 14:12:15 -0700 | [diff] [blame] | 621 |  | 
|  | 622 | #[test] | 
|  | 623 | fn test_sized_write() { | 
|  | 624 | use crate::binder::Interface; | 
|  | 625 | use crate::native::Binder; | 
|  | 626 |  | 
|  | 627 | let mut service = Binder::new(()).as_binder(); | 
|  | 628 | let mut parcel = Parcel::new_for_test(&mut service).unwrap(); | 
|  | 629 | let start = parcel.get_data_position(); | 
|  | 630 |  | 
|  | 631 | let arr = [1i32, 2i32, 3i32]; | 
|  | 632 |  | 
|  | 633 | parcel.sized_write(|subparcel| { | 
|  | 634 | subparcel.write(&arr[..]) | 
|  | 635 | }).expect("Could not perform sized write"); | 
|  | 636 |  | 
|  | 637 | // i32 sub-parcel length + i32 array length + 3 i32 elements | 
|  | 638 | let expected_len = 20i32; | 
|  | 639 |  | 
|  | 640 | assert_eq!(parcel.get_data_position(), start + expected_len); | 
|  | 641 |  | 
|  | 642 | unsafe { | 
|  | 643 | parcel.set_data_position(start).unwrap(); | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | assert_eq!( | 
|  | 647 | expected_len, | 
|  | 648 | parcel.read().unwrap(), | 
|  | 649 | ); | 
|  | 650 |  | 
|  | 651 | assert_eq!( | 
|  | 652 | parcel.read::<Vec<i32>>().unwrap(), | 
|  | 653 | &arr, | 
|  | 654 | ); | 
|  | 655 | } |