/* * 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 #include #include // for legacy reasons #include #include #include #include #include #include #ifndef BINDER_DISABLE_NATIVE_HANDLE #include #endif #include #include #include #include #include #include #include //NOLINTNEXTLINE(google-runtime-int) b/173188702 typedef unsigned long long binder_size_t; struct flat_binder_object; // --------------------------------------------------------------------------- namespace android { template class Flattenable; template class LightFlattenable; class IBinder; class IPCThreadState; class ProcessState; class RpcSession; class String8; class TextOutput; namespace binder { class Status; namespace debug { class RecordedTransaction; } } class Parcel { friend class IPCThreadState; friend class RpcState; public: class ReadableBlob; class WritableBlob; LIBBINDER_EXPORTED Parcel(); LIBBINDER_EXPORTED ~Parcel(); LIBBINDER_EXPORTED const uint8_t* data() const; LIBBINDER_EXPORTED size_t dataSize() const; LIBBINDER_EXPORTED size_t dataAvail() const; LIBBINDER_EXPORTED size_t dataPosition() const; LIBBINDER_EXPORTED size_t dataCapacity() const; LIBBINDER_EXPORTED size_t dataBufferSize() const; LIBBINDER_EXPORTED status_t setDataSize(size_t size); // this must only be used to set a data position that was previously returned from // dataPosition(). If writes are made, the exact same types of writes must be made (e.g. // auto i = p.dataPosition(); p.writeInt32(0); p.setDataPosition(i); p.writeInt32(1);). // Writing over objects, such as file descriptors and binders, is not supported. LIBBINDER_EXPORTED void setDataPosition(size_t pos) const; LIBBINDER_EXPORTED status_t setDataCapacity(size_t size); LIBBINDER_EXPORTED status_t setData(const uint8_t* buffer, size_t len); LIBBINDER_EXPORTED status_t appendFrom(const Parcel* parcel, size_t start, size_t len); LIBBINDER_EXPORTED int compareData(const Parcel& other); LIBBINDER_EXPORTED status_t compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset, size_t length, int* result) const; LIBBINDER_EXPORTED bool allowFds() const; LIBBINDER_EXPORTED bool pushAllowFds(bool allowFds); LIBBINDER_EXPORTED void restoreAllowFds(bool lastValue); LIBBINDER_EXPORTED bool hasFileDescriptors() const; LIBBINDER_EXPORTED status_t hasBinders(bool* result) const; LIBBINDER_EXPORTED status_t hasFileDescriptorsInRange(size_t offset, size_t length, bool* result) const; LIBBINDER_EXPORTED status_t hasBindersInRange(size_t offset, size_t length, bool* result) const; // returns all binder objects in the Parcel LIBBINDER_EXPORTED std::vector> debugReadAllStrongBinders() const; // returns all file descriptors in the Parcel // does not dup LIBBINDER_EXPORTED std::vector debugReadAllFileDescriptors() 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. LIBBINDER_EXPORTED 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). LIBBINDER_EXPORTED void markForBinder(const sp& binder); // Whenever possible, markForBinder should be preferred. This method is // called automatically on reply Parcels for RPC transactions. LIBBINDER_EXPORTED void markForRpc(const sp& session); // Whether this Parcel is written for RPC transactions (after calls to // markForBinder or markForRpc). LIBBINDER_EXPORTED bool isForRpc() const; // Writes the IPC/RPC header. LIBBINDER_EXPORTED status_t writeInterfaceToken(const String16& interface); LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED bool enforceInterface(const String16& interface, IPCThreadState* threadState = nullptr) const; LIBBINDER_EXPORTED bool enforceInterface(const char16_t* interface, size_t len, IPCThreadState* threadState = nullptr) const; LIBBINDER_EXPORTED bool checkInterface(IBinder*) const; // Verify there are no bytes left to be read on the Parcel. // Returns Status(EX_BAD_PARCELABLE) when the Parcel is not consumed. LIBBINDER_EXPORTED binder::Status enforceNoDataAvail() const; // This Api is used by fuzzers to skip dataAvail checks. LIBBINDER_EXPORTED void setEnforceNoDataAvail(bool enforceNoDataAvail); // When fuzzing, we want to remove certain ABI checks that cause significant // lost coverage, and we also want to avoid logs that cost too much to write. LIBBINDER_EXPORTED void setServiceFuzzing(); LIBBINDER_EXPORTED bool isServiceFuzzing() const; LIBBINDER_EXPORTED void freeData(); LIBBINDER_EXPORTED size_t objectsCount() const; LIBBINDER_EXPORTED status_t errorCheck() const; LIBBINDER_EXPORTED void setError(status_t err); LIBBINDER_EXPORTED status_t write(const void* data, size_t len); LIBBINDER_EXPORTED void* writeInplace(size_t len); LIBBINDER_EXPORTED status_t writeUnpadded(const void* data, size_t len); LIBBINDER_EXPORTED status_t writeInt32(int32_t val); LIBBINDER_EXPORTED status_t writeUint32(uint32_t val); LIBBINDER_EXPORTED status_t writeInt64(int64_t val); LIBBINDER_EXPORTED status_t writeUint64(uint64_t val); LIBBINDER_EXPORTED status_t writeFloat(float val); LIBBINDER_EXPORTED status_t writeDouble(double val); LIBBINDER_EXPORTED status_t writeCString(const char* str); LIBBINDER_EXPORTED status_t writeString8(const String8& str); LIBBINDER_EXPORTED status_t writeString8(const char* str, size_t len); LIBBINDER_EXPORTED status_t writeString16(const String16& str); LIBBINDER_EXPORTED status_t writeString16(const std::optional& str); LIBBINDER_EXPORTED status_t writeString16(const std::unique_ptr& str) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeString16(const char16_t* str, size_t len); LIBBINDER_EXPORTED status_t writeStrongBinder(const sp& val); LIBBINDER_EXPORTED status_t writeInt32Array(size_t len, const int32_t* val); LIBBINDER_EXPORTED status_t writeByteArray(size_t len, const uint8_t* val); LIBBINDER_EXPORTED status_t writeBool(bool val); LIBBINDER_EXPORTED status_t writeChar(char16_t val); LIBBINDER_EXPORTED status_t writeByte(int8_t val); // Take a UTF8 encoded string, convert to UTF16, write it to the parcel. LIBBINDER_EXPORTED status_t writeUtf8AsUtf16(const std::string& str); LIBBINDER_EXPORTED status_t writeUtf8AsUtf16(const std::optional& str); LIBBINDER_EXPORTED status_t writeUtf8AsUtf16(const std::unique_ptr& str) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeByteVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeByteVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeByteVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeByteVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeByteVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeByteVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeInt32Vector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeInt32Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeInt32Vector(const std::vector& val); LIBBINDER_EXPORTED status_t writeInt64Vector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeInt64Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeInt64Vector(const std::vector& val); LIBBINDER_EXPORTED status_t writeUint64Vector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeUint64Vector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeUint64Vector(const std::vector& val); LIBBINDER_EXPORTED status_t writeFloatVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeFloatVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeFloatVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeDoubleVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeDoubleVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeDoubleVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeBoolVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeBoolVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeBoolVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeCharVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeCharVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeCharVector(const std::vector& val); LIBBINDER_EXPORTED status_t writeString16Vector(const std::optional>>& val); LIBBINDER_EXPORTED status_t writeString16Vector(const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeString16Vector(const std::vector& val); LIBBINDER_EXPORTED status_t writeUtf8VectorAsUtf16Vector(const std::optional>>& val); LIBBINDER_EXPORTED status_t writeUtf8VectorAsUtf16Vector( const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeUtf8VectorAsUtf16Vector(const std::vector& val); LIBBINDER_EXPORTED status_t writeStrongBinderVector(const std::optional>>& val); LIBBINDER_EXPORTED status_t writeStrongBinderVector(const std::unique_ptr>>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t writeStrongBinderVector(const std::vector>& val); // Write an IInterface or a vector of IInterface's template , bool> = true> status_t writeStrongBinder(const sp& val) { return writeStrongBinder(T::asBinder(val)); } template , bool> = true> status_t writeStrongBinderVector(const std::vector>& val) { return writeData(val); } template , bool> = true> status_t writeStrongBinderVector(const std::optional>>& val) { return writeData(val); } template status_t writeFixedArray(const std::array& val) { return writeData(val); } template status_t writeFixedArray(const std::optional>& val) { return writeData(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> [[deprecated("use std::optional version instead")]] // status_t writeEnumVector(const std::unique_ptr>& val) { 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> [[deprecated("use std::optional version instead")]] // status_t writeEnumVector(const std::unique_ptr>& val) { return writeData(val); } template status_t writeParcelableVector(const std::optional>>& val) { return writeData(val); } template [[deprecated("use std::optional version instead")]] // status_t writeParcelableVector(const std::unique_ptr>>& val) { return writeData(val); } template [[deprecated("use std::optional version instead")]] // status_t writeParcelableVector(const std::shared_ptr>>& val) { 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) { return writeData(parcelable); } LIBBINDER_EXPORTED 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"))); #ifndef BINDER_DISABLE_NATIVE_HANDLE // 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. LIBBINDER_EXPORTED status_t writeNativeHandle(const native_handle* handle); #endif // 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED status_t writeUniqueFileDescriptor(const binder::unique_fd& fd); // Place a vector of file desciptors into the parcel. Each descriptor is // dup'd as in writeDupFileDescriptor LIBBINDER_EXPORTED status_t writeUniqueFileDescriptorVector(const std::optional>& val); LIBBINDER_EXPORTED status_t writeUniqueFileDescriptorVector(const std::unique_ptr>& val) __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED status_t writeDupImmutableBlobFileDescriptor(int fd); LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED status_t writeNoException(); LIBBINDER_EXPORTED status_t read(void* outData, size_t len) const; LIBBINDER_EXPORTED const void* readInplace(size_t len) const; LIBBINDER_EXPORTED int32_t readInt32() const; LIBBINDER_EXPORTED status_t readInt32(int32_t* pArg) const; LIBBINDER_EXPORTED uint32_t readUint32() const; LIBBINDER_EXPORTED status_t readUint32(uint32_t* pArg) const; LIBBINDER_EXPORTED int64_t readInt64() const; LIBBINDER_EXPORTED status_t readInt64(int64_t* pArg) const; LIBBINDER_EXPORTED uint64_t readUint64() const; LIBBINDER_EXPORTED status_t readUint64(uint64_t* pArg) const; LIBBINDER_EXPORTED float readFloat() const; LIBBINDER_EXPORTED status_t readFloat(float* pArg) const; LIBBINDER_EXPORTED double readDouble() const; LIBBINDER_EXPORTED status_t readDouble(double* pArg) const; LIBBINDER_EXPORTED bool readBool() const; LIBBINDER_EXPORTED status_t readBool(bool* pArg) const; LIBBINDER_EXPORTED char16_t readChar() const; LIBBINDER_EXPORTED status_t readChar(char16_t* pArg) const; LIBBINDER_EXPORTED int8_t readByte() const; LIBBINDER_EXPORTED status_t readByte(int8_t* pArg) const; // Read a UTF16 encoded string, convert to UTF8 LIBBINDER_EXPORTED status_t readUtf8FromUtf16(std::string* str) const; LIBBINDER_EXPORTED status_t readUtf8FromUtf16(std::optional* str) const; LIBBINDER_EXPORTED status_t readUtf8FromUtf16(std::unique_ptr* str) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED const char* readCString() const; LIBBINDER_EXPORTED String8 readString8() const; LIBBINDER_EXPORTED status_t readString8(String8* pArg) const; LIBBINDER_EXPORTED const char* readString8Inplace(size_t* outLen) const; LIBBINDER_EXPORTED String16 readString16() const; LIBBINDER_EXPORTED status_t readString16(String16* pArg) const; LIBBINDER_EXPORTED status_t readString16(std::optional* pArg) const; LIBBINDER_EXPORTED status_t readString16(std::unique_ptr* pArg) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED const char16_t* readString16Inplace(size_t* outLen) const; LIBBINDER_EXPORTED sp readStrongBinder() const; LIBBINDER_EXPORTED status_t readStrongBinder(sp* val) const; LIBBINDER_EXPORTED 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> [[deprecated("use std::optional version instead")]] // status_t readEnumVector(std::unique_ptr>* val) const { 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> [[deprecated("use std::optional version instead")]] // status_t readEnumVector(std::unique_ptr>* val) const { 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 [[deprecated("use std::optional version instead")]] // status_t readParcelableVector( std::unique_ptr>>* val) const { return readData(val); } template status_t readParcelableVector(std::vector* val) const { return readData(val); } LIBBINDER_EXPORTED 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 { 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; LIBBINDER_EXPORTED status_t readStrongBinderVector(std::optional>>* val) const; LIBBINDER_EXPORTED status_t readStrongBinderVector(std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readStrongBinderVector(std::vector>* val) const; template , bool> = true> status_t readStrongBinderVector(std::vector>* val) const { return readData(val); } template , bool> = true> status_t readStrongBinderVector(std::optional>>* val) const { return readData(val); } LIBBINDER_EXPORTED status_t readByteVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readByteVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readByteVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readByteVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readByteVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readByteVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readInt32Vector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readInt32Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readInt32Vector(std::vector* val) const; LIBBINDER_EXPORTED status_t readInt64Vector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readInt64Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readInt64Vector(std::vector* val) const; LIBBINDER_EXPORTED status_t readUint64Vector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readUint64Vector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readUint64Vector(std::vector* val) const; LIBBINDER_EXPORTED status_t readFloatVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readFloatVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readFloatVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readDoubleVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readDoubleVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readDoubleVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readBoolVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readBoolVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readBoolVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readCharVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readCharVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readCharVector(std::vector* val) const; LIBBINDER_EXPORTED status_t readString16Vector(std::optional>>* val) const; LIBBINDER_EXPORTED status_t readString16Vector(std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readString16Vector(std::vector* val) const; LIBBINDER_EXPORTED status_t readUtf8VectorFromUtf16Vector( std::optional>>* val) const; LIBBINDER_EXPORTED status_t readUtf8VectorFromUtf16Vector( std::unique_ptr>>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED status_t readUtf8VectorFromUtf16Vector(std::vector* val) const; template status_t readFixedArray(std::array* val) const { return readData(val); } template status_t readFixedArray(std::optional>* val) const { return readData(val); } 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. LIBBINDER_EXPORTED int32_t readExceptionCode() const; #ifndef BINDER_DISABLE_NATIVE_HANDLE // 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(). LIBBINDER_EXPORTED native_handle* readNativeHandle() const; #endif // 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. LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED int readParcelFileDescriptor() const; // Retrieve a smart file descriptor from the parcel. LIBBINDER_EXPORTED status_t readUniqueFileDescriptor(binder::unique_fd* val) const; // Retrieve a Java "parcel file descriptor" from the parcel. LIBBINDER_EXPORTED status_t readUniqueParcelFileDescriptor(binder::unique_fd* val) const; // Retrieve a vector of smart file descriptors from the parcel. LIBBINDER_EXPORTED status_t readUniqueFileDescriptorVector(std::optional>* val) const; LIBBINDER_EXPORTED status_t readUniqueFileDescriptorVector(std::unique_ptr>* val) const __attribute__((deprecated("use std::optional version instead"))); LIBBINDER_EXPORTED 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. LIBBINDER_EXPORTED status_t readBlob(size_t len, ReadableBlob* outBlob) const; LIBBINDER_EXPORTED const flat_binder_object* readObject(bool nullMetaData) const; // Explicitly close all file descriptors in the parcel. LIBBINDER_EXPORTED void closeFileDescriptors(); // Debugging: get metrics on current allocations. LIBBINDER_EXPORTED static size_t getGlobalAllocSize(); LIBBINDER_EXPORTED static size_t getGlobalAllocCount(); LIBBINDER_EXPORTED bool replaceCallingWorkSourceUid(uid_t uid); // Returns the work source provided by the caller. This can only be trusted for trusted calling // uid. LIBBINDER_EXPORTED uid_t readCallingWorkSourceUid() const; LIBBINDER_EXPORTED void print(std::ostream& to, uint32_t flags = 0) const; private: // `objects` and `objectsSize` always 0 for RPC Parcels. typedef void (*release_func)(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); // Takes ownership even when an error is returned. status_t rpcSetDataReference( const sp& session, const uint8_t* data, size_t dataSize, const uint32_t* objectTable, size_t objectTableSize, std::vector>&& ancillaryFds, release_func relFunc); status_t finishWrite(size_t len); void releaseObjects(); void acquireObjects(); status_t growData(size_t len); // Clear the Parcel and set the capacity to `desired`. // Doesn't reset the RPC session association. status_t restartWrite(size_t desired); // Set the capacity to `desired`, truncating the Parcel if necessary. status_t continueWrite(size_t desired); status_t truncateRpcObjects(size_t newObjectsSize); 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 scanForBinders(bool* result) 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; LIBBINDER_EXPORTED status_t readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) 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, 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