/* * Copyright (C) 2005 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include // for legacy reasons #include #include #include #include #include #include #include #include #include #include #include #include #ifdef BINDER_IPC_32BIT //NOLINTNEXTLINE(google-runtime-int) b/173188702 typedef unsigned int binder_size_t; #else //NOLINTNEXTLINE(google-runtime-int) b/173188702 typedef unsigned long long binder_size_t; #endif struct flat_binder_object; // --------------------------------------------------------------------------- namespace android { template class Flattenable; template class LightFlattenable; class IBinder; class IPCThreadState; class ProcessState; class RpcSession; class String8; class TextOutput; class Parcel { friend class IPCThreadState; friend class RpcState; public: class ReadableBlob; class WritableBlob; Parcel(); ~Parcel(); const uint8_t* data() const; size_t dataSize() const; size_t dataAvail() const; size_t dataPosition() const; size_t dataCapacity() const; status_t setDataSize(size_t size); void setDataPosition(size_t pos) const; status_t setDataCapacity(size_t size); status_t setData(const uint8_t* buffer, size_t len); status_t appendFrom(const Parcel *parcel, size_t start, size_t len); int compareData(const Parcel& other); bool allowFds() const; bool pushAllowFds(bool allowFds); void restoreAllowFds(bool lastValue); bool hasFileDescriptors() const; // Zeros data when reallocating. Other mitigations may be added // in the future. // // WARNING: some read methods may make additional copies of data. // In order to verify this, heap dumps should be used. void markSensitive() const; // For a 'data' Parcel, this should mark the Parcel as being prepared for a // transaction on this specific binder object. Based on this, the format of // the wire binder protocol may change (data is written differently when it // is for an RPC transaction). void markForBinder(const sp& binder); // Whenever possible, markForBinder should be preferred. This method is // called automatically on reply Parcels for RPC transactions. void markForRpc(const sp& session); // Whether this Parcel is written for RPC transactions (after calls to // markForBinder or markForRpc). bool isForRpc() const; // Writes the IPC/RPC header. status_t writeInterfaceToken(const String16& interface); status_t writeInterfaceToken(const char16_t* str, size_t len); // Parses the RPC header, returning true if the interface name // in the header matches the expected interface from the caller. // // Additionally, enforceInterface does part of the work of // propagating the StrictMode policy mask, populating the current // IPCThreadState, which as an optimization may optionally be // passed in. bool enforceInterface(const String16& interface, IPCThreadState* threadState = nullptr) const; bool enforceInterface(const char16_t* interface, size_t len, IPCThreadState* threadState = nullptr) const; bool checkInterface(IBinder*) const; void freeData(); size_t objectsCount() const; status_t errorCheck() const; void setError(status_t err); status_t write(const void* data, size_t len); void* writeInplace(size_t len); status_t writeUnpadded(const void* data, size_t len); status_t writeInt32(int32_t val); status_t writeUint32(uint32_t val); status_t writeInt64(int64_t val); status_t writeUint64(uint64_t val); status_t writeFloat(float val); status_t writeDouble(double val); status_t writeCString(const char* str); status_t writeString8(const String8& str); status_t writeString8(const char* str, size_t len); status_t writeString16(const String16& str); status_t writeString16(const std::optional& str); status_t writeString16(const std::unique_ptr& str) __attribute__((deprecated("use std::optional version instead"))); status_t writeString16(const char16_t* str, size_t len); status_t writeStrongBinder(const sp& val); status_t writeInt32Array(size_t len, const int32_t *val); status_t writeByteArray(size_t len, const uint8_t *val); status_t writeBool(bool val); status_t writeChar(char16_t val); status_t writeByte(int8_t val); // Take a UTF8 encoded string, convert to UTF16, write it to the parcel. status_t writeUtf8AsUtf16(const std::string& str); status_t writeUtf8AsUtf16(const std::optional& str); status_t writeUtf8AsUtf16(const std::unique_ptr& str) __attribute__((deprecated("use std::optional version instead"))); status_t writeByteVector(const std::optional>& val); status_t writeByteVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeByteVector(const std::vector& val); status_t writeByteVector(const std::optional>& val); status_t writeByteVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeByteVector(const std::vector& val); status_t writeInt32Vector(const std::optional>& val); status_t writeInt32Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeInt32Vector(const std::vector& val); status_t writeInt64Vector(const std::optional>& val); status_t writeInt64Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeInt64Vector(const std::vector& val); status_t writeUint64Vector(const std::optional>& val); status_t writeUint64Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeUint64Vector(const std::vector& val); status_t writeFloatVector(const std::optional>& val); status_t writeFloatVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeFloatVector(const std::vector& val); status_t writeDoubleVector(const std::optional>& val); status_t writeDoubleVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeDoubleVector(const std::vector& val); status_t writeBoolVector(const std::optional>& val); status_t writeBoolVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeBoolVector(const std::vector& val); status_t writeCharVector(const std::optional>& val); status_t writeCharVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeCharVector(const std::vector& val); status_t writeString16Vector( const std::optional>>& val); status_t writeString16Vector( const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeString16Vector(const std::vector& val); status_t writeUtf8VectorAsUtf16Vector( const std::optional>>& val); status_t writeUtf8VectorAsUtf16Vector( const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeUtf8VectorAsUtf16Vector(const std::vector& val); status_t writeStrongBinderVector(const std::optional>>& val); status_t writeStrongBinderVector(const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeStrongBinderVector(const std::vector>& val); // Write an Enum vector with underlying type int8_t. // Does not use padding; each byte is contiguous. template && std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::vector& val) { return writeData(val); } template && std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::optional>& val) { return writeData(val); } template && std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))) { return writeData(val); } // Write an Enum vector with underlying type != int8_t. template && !std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::vector& val) { return writeData(val); } template && !std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::optional>& val) { return writeData(val); } template && !std::is_same_v,int8_t>, bool> = 0> status_t writeEnumVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))) { return writeData(val); } template status_t writeParcelableVector(const std::optional>>& val) { return writeData(val); } template status_t writeParcelableVector(const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))) { return writeData(val); } template status_t writeParcelableVector(const std::shared_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))) { return writeData(val); } template status_t writeParcelableVector(const std::shared_ptr>>& val) { return writeData(val); } template status_t writeParcelableVector(const std::vector& val) { return writeData(val); } template status_t writeNullableParcelable(const std::optional& parcelable) { return writeData(parcelable); } template status_t writeNullableParcelable(const std::unique_ptr& parcelable) __attribute__((deprecated("use std::optional version instead"))) { return writeData(parcelable); } status_t writeParcelable(const Parcelable& parcelable); template status_t write(const Flattenable& val); template status_t write(const LightFlattenable& val); template status_t writeVectorSize(const std::vector& val); template status_t writeVectorSize(const std::optional>& val); template status_t writeVectorSize(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); // Place a native_handle into the parcel (the native_handle's file- // descriptors are dup'ed, so it is safe to delete the native_handle // when this function returns). // Doesn't take ownership of the native_handle. status_t writeNativeHandle(const native_handle* handle); // Place a file descriptor into the parcel. The given fd must remain // valid for the lifetime of the parcel. // The Parcel does not take ownership of the given fd unless you ask it to. status_t writeFileDescriptor(int fd, bool takeOwnership = false); // Place a file descriptor into the parcel. A dup of the fd is made, which // will be closed once the parcel is destroyed. status_t writeDupFileDescriptor(int fd); // Place a Java "parcel file descriptor" into the parcel. The given fd must remain // valid for the lifetime of the parcel. // The Parcel does not take ownership of the given fd unless you ask it to. status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false); // Place a Java "parcel file descriptor" into the parcel. A dup of the fd is made, which will // be closed once the parcel is destroyed. status_t writeDupParcelFileDescriptor(int fd); // Place a file descriptor into the parcel. This will not affect the // semantics of the smart file descriptor. A new descriptor will be // created, and will be closed when the parcel is destroyed. status_t writeUniqueFileDescriptor( const base::unique_fd& fd); // Place a vector of file desciptors into the parcel. Each descriptor is // dup'd as in writeDupFileDescriptor status_t writeUniqueFileDescriptorVector( const std::optional>& val); status_t writeUniqueFileDescriptorVector( const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); status_t writeUniqueFileDescriptorVector( const std::vector& val); // Writes a blob to the parcel. // If the blob is small, then it is stored in-place, otherwise it is // transferred by way of an anonymous shared memory region. Prefer sending // immutable blobs if possible since they may be subsequently transferred between // processes without further copying whereas mutable blobs always need to be copied. // The caller should call release() on the blob after writing its contents. status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob); // Write an existing immutable blob file descriptor to the parcel. // This allows the client to send the same blob to multiple processes // as long as it keeps a dup of the blob file descriptor handy for later. status_t writeDupImmutableBlobFileDescriptor(int fd); status_t writeObject(const flat_binder_object& val, bool nullMetaData); // Like Parcel.java's writeNoException(). Just writes a zero int32. // Currently the native implementation doesn't do any of the StrictMode // stack gathering and serialization that the Java implementation does. status_t writeNoException(); status_t read(void* outData, size_t len) const; const void* readInplace(size_t len) const; int32_t readInt32() const; status_t readInt32(int32_t *pArg) const; uint32_t readUint32() const; status_t readUint32(uint32_t *pArg) const; int64_t readInt64() const; status_t readInt64(int64_t *pArg) const; uint64_t readUint64() const; status_t readUint64(uint64_t *pArg) const; float readFloat() const; status_t readFloat(float *pArg) const; double readDouble() const; status_t readDouble(double *pArg) const; bool readBool() const; status_t readBool(bool *pArg) const; char16_t readChar() const; status_t readChar(char16_t *pArg) const; int8_t readByte() const; status_t readByte(int8_t *pArg) const; // Read a UTF16 encoded string, convert to UTF8 status_t readUtf8FromUtf16(std::string* str) const; status_t readUtf8FromUtf16(std::optional* str) const; status_t readUtf8FromUtf16(std::unique_ptr* str) const __attribute__((deprecated("use std::optional version instead"))); const char* readCString() const; String8 readString8() const; status_t readString8(String8* pArg) const; const char* readString8Inplace(size_t* outLen) const; String16 readString16() const; status_t readString16(String16* pArg) const; status_t readString16(std::optional* pArg) const; status_t readString16(std::unique_ptr* pArg) const __attribute__((deprecated("use std::optional version instead"))); const char16_t* readString16Inplace(size_t* outLen) const; sp readStrongBinder() const; status_t readStrongBinder(sp* val) const; status_t readNullableStrongBinder(sp* val) const; // Read an Enum vector with underlying type int8_t. // Does not use padding; each byte is contiguous. template && std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::vector* val) const { return readData(val); } template && std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))) { return readData(val); } template && std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::optional>* val) const { return readData(val); } // Read an Enum vector with underlying type != int8_t. template && !std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::vector* val) const { return readData(val); } template && !std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))) { return readData(val); } template && !std::is_same_v,int8_t>, bool> = 0> status_t readEnumVector(std::optional>* val) const { return readData(val); } template status_t readParcelableVector( std::optional>>* val) const { return readData(val); } template status_t readParcelableVector( std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))) { return readData(val); } template status_t readParcelableVector(std::vector* val) const { return readData(val); } status_t readParcelable(Parcelable* parcelable) const; template status_t readParcelable(std::optional* parcelable) const { return readData(parcelable); } template status_t readParcelable(std::unique_ptr* parcelable) const __attribute__((deprecated("use std::optional version instead"))) { return readData(parcelable); } // If strong binder would be nullptr, readStrongBinder() returns an error. // TODO: T must be derived from IInterface, fix for clarity. template status_t readStrongBinder(sp* val) const; template status_t readNullableStrongBinder(sp* val) const; status_t readStrongBinderVector(std::optional>>* val) const; status_t readStrongBinderVector(std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readStrongBinderVector(std::vector>* val) const; status_t readByteVector(std::optional>* val) const; status_t readByteVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readByteVector(std::vector* val) const; status_t readByteVector(std::optional>* val) const; status_t readByteVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readByteVector(std::vector* val) const; status_t readInt32Vector(std::optional>* val) const; status_t readInt32Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readInt32Vector(std::vector* val) const; status_t readInt64Vector(std::optional>* val) const; status_t readInt64Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readInt64Vector(std::vector* val) const; status_t readUint64Vector(std::optional>* val) const; status_t readUint64Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readUint64Vector(std::vector* val) const; status_t readFloatVector(std::optional>* val) const; status_t readFloatVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readFloatVector(std::vector* val) const; status_t readDoubleVector(std::optional>* val) const; status_t readDoubleVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readDoubleVector(std::vector* val) const; status_t readBoolVector(std::optional>* val) const; status_t readBoolVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readBoolVector(std::vector* val) const; status_t readCharVector(std::optional>* val) const; status_t readCharVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readCharVector(std::vector* val) const; status_t readString16Vector( std::optional>>* val) const; status_t readString16Vector( std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readString16Vector(std::vector* val) const; status_t readUtf8VectorFromUtf16Vector( std::optional>>* val) const; status_t readUtf8VectorFromUtf16Vector( std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readUtf8VectorFromUtf16Vector(std::vector* val) const; template status_t read(Flattenable& val) const; template status_t read(LightFlattenable& val) const; // resizeOutVector is used to resize AIDL out vector parameters. template status_t resizeOutVector(std::vector* val) const; template status_t resizeOutVector(std::optional>* val) const; template status_t resizeOutVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); // Like Parcel.java's readExceptionCode(). Reads the first int32 // off of a Parcel's header, returning 0 or the negative error // code on exceptions, but also deals with skipping over rich // response headers. Callers should use this to read & parse the // response headers rather than doing it by hand. int32_t readExceptionCode() const; // Retrieve native_handle from the parcel. This returns a copy of the // parcel's native_handle (the caller takes ownership). The caller // must free the native_handle with native_handle_close() and // native_handle_delete(). native_handle* readNativeHandle() const; // Retrieve a file descriptor from the parcel. This returns the raw fd // in the parcel, which you do not own -- use dup() to get your own copy. int readFileDescriptor() const; // Retrieve a Java "parcel file descriptor" from the parcel. This returns the raw fd // in the parcel, which you do not own -- use dup() to get your own copy. int readParcelFileDescriptor() const; // Retrieve a smart file descriptor from the parcel. status_t readUniqueFileDescriptor( base::unique_fd* val) const; // Retrieve a Java "parcel file descriptor" from the parcel. status_t readUniqueParcelFileDescriptor(base::unique_fd* val) const; // Retrieve a vector of smart file descriptors from the parcel. status_t readUniqueFileDescriptorVector( std::optional>* val) const; status_t readUniqueFileDescriptorVector( std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); status_t readUniqueFileDescriptorVector( std::vector* val) const; // Reads a blob from the parcel. // The caller should call release() on the blob after reading its contents. status_t readBlob(size_t len, ReadableBlob* outBlob) const; const flat_binder_object* readObject(bool nullMetaData) const; // Explicitly close all file descriptors in the parcel. void closeFileDescriptors(); // Debugging: get metrics on current allocations. static size_t getGlobalAllocSize(); static size_t getGlobalAllocCount(); bool replaceCallingWorkSourceUid(uid_t uid); // Returns the work source provided by the caller. This can only be trusted for trusted calling // uid. uid_t readCallingWorkSourceUid() const; void print(TextOutput& to, uint32_t flags = 0) const; private: typedef void (*release_func)(Parcel* parcel, const uint8_t* data, size_t dataSize, const binder_size_t* objects, size_t objectsSize); uintptr_t ipcData() const; size_t ipcDataSize() const; uintptr_t ipcObjects() const; size_t ipcObjectsCount() const; void ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects, size_t objectsCount, release_func relFunc); status_t finishWrite(size_t len); void releaseObjects(); void acquireObjects(); status_t growData(size_t len); status_t restartWrite(size_t desired); status_t continueWrite(size_t desired); status_t writePointer(uintptr_t val); status_t readPointer(uintptr_t *pArg) const; uintptr_t readPointer() const; void freeDataNoInit(); void initState(); void scanForFds() const; status_t validateReadData(size_t len) const; void updateWorkSourceRequestHeaderPosition() const; status_t finishFlattenBinder(const sp& binder); status_t finishUnflattenBinder(const sp& binder, sp* out) const; status_t flattenBinder(const sp& binder); status_t unflattenBinder(sp* out) const; template status_t readAligned(T *pArg) const; template T readAligned() const; template status_t writeAligned(T val); status_t writeRawNullableParcelable(const Parcelable* parcelable); //----------------------------------------------------------------------------- // Generic type read and write methods for Parcel: // // readData(T *value) will read a value from the Parcel. // writeData(const T& value) will write a value to the Parcel. // // Our approach to parceling is based on two overloaded functions // readData() and writeData() that generate parceling code for an // object automatically based on its type. The code from templates are generated at // compile time (if constexpr), and decomposes an object through a call graph matching // recursive descent of the template typename. // // This approach unifies handling of complex objects, // resulting in fewer lines of code, greater consistency, // extensibility to nested types, efficiency (decisions made at compile time), // and better code maintainability and optimization. // // Design decision: Incorporate the read and write code into Parcel rather than // as a non-intrusive serializer that emits a byte stream, as we have // active objects, alignment, legacy code, and historical idiosyncrasies. // // --- Overview // // Parceling is a way of serializing objects into a sequence of bytes for communication // between processes, as part of marshaling data for remote procedure calls. // // The Parcel instance contains objects serialized as bytes, such as the following: // // 1) Ordinary primitive data such as int, float. // 2) Established structured data such as String16, std::string. // 3) Parcelables, which are C++ objects that derive from Parcelable (and thus have a // readFromParcel and writeToParcel method). (Similar for Java) // 4) A std::vector<> of such data. // 5) Nullable objects contained in std::optional, std::unique_ptr, or std::shared_ptr. // // And active objects from the Android ecosystem such as: // 6) File descriptors, base::unique_fd (kernel object handles) // 7) Binder objects, sp (active Android RPC handles) // // Objects from (1) through (5) serialize into the mData buffer. // Active objects (6) and (7) serialize into both mData and mObjects buffers. // // --- Data layout details // // Data is read or written to the parcel by recursively decomposing the type of the parameter // type T through readData() and writeData() methods. // // We focus on writeData() here in our explanation of the data layout. // // 1) Alignment // Implementation detail: Regardless of the parameter type, writeData() calls are designed // to finish at a multiple of 4 bytes, the default alignment of the Parcel. // // Writes of single uint8_t, int8_t, enums based on types of size 1, char16_t, etc // will result in 4 bytes being written. The data is widened to int32 and then written; // hence the position of the nonzero bytes depend on the native endianness of the CPU. // // Writes of primitive values with 8 byte size, double, int64_t, uint64_t, // are stored with 4 byte alignment. The ARM and x86/x64 permit unaligned reads // and writes (albeit with potential latency/throughput penalty) which may or may // not be observable unless the process is IO bound. // // 2) Parcelables // Parcelables are detected by the type's base class, and implemented through calling // into the Parcelable type's readFromParcel() or writeToParcel() methods. // Historically, due to null object detection, a (int32_t) 1 is prepended to the data written. // Parcelables must have a default constructor (i.e. one that takes no arguments). // // 3) Arrays // Arrays of uint8_t and int8_t, and enums based on size 1 are written as // a contiguous packed byte stream. Hidden zero padding is applied at the end of the byte // stream to make a multiple of 4 bytes (and prevent info leakage when writing). // // All other array writes can be conceptually thought of as recursively calling // writeData on the individual elements (though may be implemented differently for speed). // As discussed in (1), alignment rules are therefore applied for each element // write (not as an aggregate whole), so the wire representation of data can be // substantially larger. // // Historical Note: // Because of element-wise alignment, CharVector and BoolVector are expanded // element-wise into integers even though they could have been optimized to be packed // just like uint8_t, int8_t (size 1 data). // // 3.1) Arrays accessed by the std::vector type. This is the default for AIDL. // // 4) Nullables // std::optional, std::unique_ptr, std::shared_ptr are all parceled identically // (i.e. result in identical byte layout). // The target of the std::optional, std::unique_ptr, or std::shared_ptr // can either be a std::vector, String16, std::string, or a Parcelable. // // Detection of null relies on peeking the first int32 data and checking if the // the peeked value is considered invalid for the object: // (-1 for vectors, String16, std::string) (0 for Parcelables). If the peeked value // is invalid, then a null is returned. // // Application Note: When to use each nullable type: // // std::optional: Embeds the object T by value rather than creating a new instance // by managed pointer as std::unique_ptr or std::shared_ptr. This will save a malloc // when creating an optional instance. // // Use of std::optionals by value can result in copies of the underlying value stored in it, // so a std::move may be used to move in and move out (for example) a vector value into // the std::optional or for the std::optional itself. // // std::unique_ptr, std::shared_ptr: These are preferred when the lifetime of the object is // already managed by the application. This reduces unnecessary copying of data // especially when the calls are local in-proc (rather than via binder rpc). // // 5) StrongBinder (sp) // StrongBinder objects are written regardless of null. When read, null StrongBinder values // will be interpreted as UNKNOWN_ERROR if the type is a single argument > // or in a vector argument >. However, they will be read without an error // if present in a std::optional, std::unique_ptr, or std::shared_ptr vector, e.g. // >>. // // See AIDL annotation @Nullable, readStrongBinder(), and readNullableStrongBinder(). // // Historical Note: writing a vector of StrongBinder objects > // containing a null will not cause an error. However reading such a vector will cause // an error _and_ early termination of the read. // --- Examples // // Using recursive parceling, we can parcel complex data types so long // as they obey the rules described above. // // Example #1 // Parceling of a 3D vector // // std::vector>> v1 { // { {1}, {2, 3}, {4} }, // {}, // { {10}, {20}, {30, 40} }, // }; // Parcel p1; // p1.writeData(v1); // decltype(v1) v2; // p1.setDataPosition(0); // p1.readData(&v2); // ASSERT_EQ(v1, v2); // // Example #2 // Parceling of mixed shared pointers // // Parcel p1; // auto sp1 = std::make_shared>>>(3); // (*sp1)[2] = std::make_shared>(3); // (*(*sp1)[2])[2] = 2; // p1.writeData(sp1); // decltype(sp1) sp2; // p1.setDataPosition(0); // p1.readData(&sp2); // ASSERT_EQ((*sp1)[0], (*sp2)[0]); // nullptr // ASSERT_EQ((*sp1)[1], (*sp2)[1]); // nullptr // ASSERT_EQ(*(*sp1)[2], *(*sp2)[2]); // { 0, 0, 2} // --- Helper Methods // TODO: move this to a utils header. // // Determine if a type is a specialization of a templated type // Example: is_specialization_v template class Ref> struct is_specialization : std::false_type {}; template