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

Commit a884566f authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk: read/write array for primitive types.

   type  | contiguous in NDK API | contiguous on the wire
  --------------------------------------------------------
   bool  | no*                   | no
   char  | yes                   | no**
   other | yes                   | yes

 *  std::vector<bool> as an implementation detail stores a packed byte
    array. This packed byte array is not exposed as an implementation
    detail, so even if we made bool contiguous on the wire, we couldn't
    ever make it contiguous in the NDK API.

 ** read/write Int32/Int is called repeatedly which writes each
    char16_t into a 32-bit wide section on the wire.

Arrays in Java AIDL are also very annoying. Whereas in C++, their size
can be changed, in Java, because of the way out-parameters work, the
array size cannot be changed. We might consider adding a better error
when this happens.

Bug: 111445392
Test: atest android.binder.cts

Change-Id: I3934f7d5cd749810c4a512ac3fb57b63f9ae4ea5
parent 68561001
Loading
Loading
Loading
Loading
+213 −18
Original line number Diff line number Diff line
@@ -50,6 +50,99 @@ typedef struct AParcel AParcel;
 */
void AParcel_delete(AParcel* parcel) __INTRODUCED_IN(29);

/**
 * This is called to allocate an array with a given length. If allocation fails, null should be
 * returned.
 */
typedef void* (*AParcel_arrayReallocator)(void* vectorData, size_t length);

// @START-PRIMITIVE-VECTOR-GETTERS
/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef int32_t* (*AParcel_int32ArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef uint32_t* (*AParcel_uint32ArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef int64_t* (*AParcel_int64ArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef uint64_t* (*AParcel_uint64ArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef float* (*AParcel_floatArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef double* (*AParcel_doubleArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef bool (*AParcel_boolArrayGetter)(const void* arrayData, size_t index);

/**
 * This is called to set an underlying value in an arrayData object at index.
 */
typedef void (*AParcel_boolArraySetter)(void* arrayData, size_t index, bool value);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef char16_t* (*AParcel_charArrayGetter)(void* arrayData);

/**
 * This is called to get the underlying data from an arrayData object.
 *
 * This will never be called for an empty array.
 */
typedef int8_t* (*AParcel_byteArrayGetter)(void* arrayData);

// @END-PRIMITIVE-VECTOR-GETTERS

/**
 * This is called to allocate a buffer
 *
 * The length here includes the space required to insert a '\0' for a properly formed c-str. If the
 * buffer returned from this function is retStr, it will be filled by AParcel_readString with the
 * data from the remote process, and it will be filled such that retStr[length] == '\0'.
 *
 * If allocation fails, null should be returned.
 */
typedef void* (*AParcel_string_reallocator)(void* stringData, size_t length);

/**
 * This is called to get the buffer from a stringData object.
 */
typedef char* (*AParcel_string_getter)(void* stringData);

/**
 * Writes an AIBinder to the next location in a non-null parcel. Can be null.
 */
@@ -94,22 +187,6 @@ binder_status_t AParcel_readStatusHeader(const AParcel* parcel, AStatus** status
binder_status_t AParcel_writeString(AParcel* parcel, const char* string, size_t length)
        __INTRODUCED_IN(29);

/**
 * This is called to allocate a buffer
 *
 * The length here includes the space required to insert a '\0' for a properly formed c-str. If the
 * buffer returned from this function is retStr, it will be filled by AParcel_readString with the
 * data from the remote process, and it will be filled such that retStr[length] == '\0'.
 *
 * If allocation fails, null should be returned.
 */
typedef void* (*AParcel_string_reallocator)(void* stringData, size_t length);

/**
 * This is called to get the buffer from a stringData object.
 */
typedef char* (*AParcel_string_getter)(void* stringData);

/**
 * Reads and allocates string value from the next location in a non-null parcel.
 *
@@ -125,7 +202,7 @@ binder_status_t AParcel_readString(const AParcel* parcel, AParcel_string_realloc
                                   AParcel_string_getter getter, void** stringData)
        __INTRODUCED_IN(29);

// @START
// @START-PRIMITIVE-READ-WRITE
/**
 * Writes int32_t value to the next location in a non-null parcel.
 */
@@ -216,7 +293,125 @@ binder_status_t AParcel_readChar(const AParcel* parcel, char16_t* value) __INTRO
 */
binder_status_t AParcel_readByte(const AParcel* parcel, int8_t* value) __INTRODUCED_IN(29);

// @END
/**
 * Writes an array of int32_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeInt32Array(AParcel* parcel, const int32_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of uint32_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeUint32Array(AParcel* parcel, const uint32_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of int64_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeInt64Array(AParcel* parcel, const int64_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of uint64_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeUint64Array(AParcel* parcel, const uint64_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of float to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeFloatArray(AParcel* parcel, const float* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of double to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeDoubleArray(AParcel* parcel, const double* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of bool to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeBoolArray(AParcel* parcel, const void* arrayData,
                                       AParcel_boolArrayGetter getter, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of char16_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeCharArray(AParcel* parcel, const char16_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Writes an array of int8_t to the next location in a non-null parcel.
 */
binder_status_t AParcel_writeByteArray(AParcel* parcel, const int8_t* value, size_t length)
        __INTRODUCED_IN(29);

/**
 * Reads an array of int32_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readInt32Array(const AParcel* parcel, void** arrayData,
                                       AParcel_arrayReallocator reallocator,
                                       AParcel_int32ArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of uint32_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readUint32Array(const AParcel* parcel, void** arrayData,
                                        AParcel_arrayReallocator reallocator,
                                        AParcel_uint32ArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of int64_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readInt64Array(const AParcel* parcel, void** arrayData,
                                       AParcel_arrayReallocator reallocator,
                                       AParcel_int64ArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of uint64_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readUint64Array(const AParcel* parcel, void** arrayData,
                                        AParcel_arrayReallocator reallocator,
                                        AParcel_uint64ArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of float from the next location in a non-null parcel.
 */
binder_status_t AParcel_readFloatArray(const AParcel* parcel, void** arrayData,
                                       AParcel_arrayReallocator reallocator,
                                       AParcel_floatArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of double from the next location in a non-null parcel.
 */
binder_status_t AParcel_readDoubleArray(const AParcel* parcel, void** arrayData,
                                        AParcel_arrayReallocator reallocator,
                                        AParcel_doubleArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of bool from the next location in a non-null parcel.
 */
binder_status_t AParcel_readBoolArray(const AParcel* parcel, void** arrayData,
                                      AParcel_arrayReallocator reallocator,
                                      AParcel_boolArraySetter setter) __INTRODUCED_IN(29);

/**
 * Reads an array of char16_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readCharArray(const AParcel* parcel, void** arrayData,
                                      AParcel_arrayReallocator reallocator,
                                      AParcel_charArrayGetter getter) __INTRODUCED_IN(29);

/**
 * Reads an array of int8_t from the next location in a non-null parcel.
 */
binder_status_t AParcel_readByteArray(const AParcel* parcel, void** arrayData,
                                      AParcel_arrayReallocator reallocator,
                                      AParcel_byteArrayGetter getter) __INTRODUCED_IN(29);

// @END-PRIMITIVE-READ-WRITE

#endif //__ANDROID_API__ >= __ANDROID_API_Q__
__END_DECLS
+253 −0
Original line number Diff line number Diff line
@@ -31,9 +31,241 @@
#ifdef __cplusplus

#include <string>
#include <vector>

namespace ndk {

/**
 * This resizes a std::vector of some underlying type to the given length.
 */
template <typename T>
static inline void* AParcel_stdVectorReallocator(void* vectorData, size_t length) {
    std::vector<T>* vec = static_cast<std::vector<T>*>(vectorData);
    if (length > vec->max_size()) return nullptr;

    vec->resize(length);
    return vec;
}

/**
 * This retrieves the underlying contiguous vector from a corresponding vectorData.
 */
template <typename T>
static inline T* AParcel_stdVectorGetter(void* vectorData) {
    std::vector<T>* vec = static_cast<std::vector<T>*>(vectorData);
    return vec->data();
}

/**
 * This retrieves the underlying value in a vector which may not be contiguous at index from a
 * corresponding vectorData.
 */
template <typename T>
static inline T AParcel_stdVectorGetter(const void* vectorData, size_t index) {
    const std::vector<T>* vec = static_cast<const std::vector<T>*>(vectorData);
    return (*vec)[index];
}

/**
 * This sets the underlying value in a corresponding vectorData which may not be contiguous at
 * index.
 */
template <typename T>
static inline void AParcel_stdVectorSetter(void* vectorData, size_t index, T value) {
    std::vector<T>* vec = static_cast<std::vector<T>*>(vectorData);
    (*vec)[index] = value;
}

/**
 * Writes a vector to the next location in a non-null parcel.
 */
template <typename T>
static inline binder_status_t AParcel_writeVector(AParcel* parcel, const std::vector<T>& vec);

/**
 * Reads a vector to the next location in a non-null parcel.
 */
template <typename T>
static inline binder_status_t AParcel_readVector(const AParcel* parcel, std::vector<T>* vec);

// @START
/**
 * Writes a vector of int32_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<int32_t>(AParcel* parcel,
                                                    const std::vector<int32_t>& vec) {
    return AParcel_writeInt32Array(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of int32_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<int32_t>(const AParcel* parcel,
                                                   std::vector<int32_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readInt32Array(parcel, &vectorData, &AParcel_stdVectorReallocator<int32_t>,
                                  AParcel_stdVectorGetter<int32_t>);
}

/**
 * Writes a vector of uint32_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<uint32_t>(AParcel* parcel,
                                                     const std::vector<uint32_t>& vec) {
    return AParcel_writeUint32Array(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of uint32_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<uint32_t>(const AParcel* parcel,
                                                    std::vector<uint32_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readUint32Array(parcel, &vectorData, &AParcel_stdVectorReallocator<uint32_t>,
                                   AParcel_stdVectorGetter<uint32_t>);
}

/**
 * Writes a vector of int64_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<int64_t>(AParcel* parcel,
                                                    const std::vector<int64_t>& vec) {
    return AParcel_writeInt64Array(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of int64_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<int64_t>(const AParcel* parcel,
                                                   std::vector<int64_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readInt64Array(parcel, &vectorData, &AParcel_stdVectorReallocator<int64_t>,
                                  AParcel_stdVectorGetter<int64_t>);
}

/**
 * Writes a vector of uint64_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<uint64_t>(AParcel* parcel,
                                                     const std::vector<uint64_t>& vec) {
    return AParcel_writeUint64Array(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of uint64_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<uint64_t>(const AParcel* parcel,
                                                    std::vector<uint64_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readUint64Array(parcel, &vectorData, &AParcel_stdVectorReallocator<uint64_t>,
                                   AParcel_stdVectorGetter<uint64_t>);
}

/**
 * Writes a vector of float to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<float>(AParcel* parcel, const std::vector<float>& vec) {
    return AParcel_writeFloatArray(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of float from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<float>(const AParcel* parcel, std::vector<float>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readFloatArray(parcel, &vectorData, &AParcel_stdVectorReallocator<float>,
                                  AParcel_stdVectorGetter<float>);
}

/**
 * Writes a vector of double to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<double>(AParcel* parcel,
                                                   const std::vector<double>& vec) {
    return AParcel_writeDoubleArray(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of double from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<double>(const AParcel* parcel, std::vector<double>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readDoubleArray(parcel, &vectorData, &AParcel_stdVectorReallocator<double>,
                                   AParcel_stdVectorGetter<double>);
}

/**
 * Writes a vector of bool to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<bool>(AParcel* parcel, const std::vector<bool>& vec) {
    return AParcel_writeBoolArray(parcel, static_cast<const void*>(&vec),
                                  AParcel_stdVectorGetter<bool>, vec.size());
}

/**
 * Reads a vector of bool from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<bool>(const AParcel* parcel, std::vector<bool>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readBoolArray(parcel, &vectorData, &AParcel_stdVectorReallocator<bool>,
                                 AParcel_stdVectorSetter<bool>);
}

/**
 * Writes a vector of char16_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<char16_t>(AParcel* parcel,
                                                     const std::vector<char16_t>& vec) {
    return AParcel_writeCharArray(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of char16_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<char16_t>(const AParcel* parcel,
                                                    std::vector<char16_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readCharArray(parcel, &vectorData, &AParcel_stdVectorReallocator<char16_t>,
                                 AParcel_stdVectorGetter<char16_t>);
}

/**
 * Writes a vector of int8_t to the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_writeVector<int8_t>(AParcel* parcel,
                                                   const std::vector<int8_t>& vec) {
    return AParcel_writeByteArray(parcel, vec.data(), vec.size());
}

/**
 * Reads a vector of int8_t from the next location in a non-null parcel.
 */
template <>
inline binder_status_t AParcel_readVector<int8_t>(const AParcel* parcel, std::vector<int8_t>* vec) {
    void* vectorData = static_cast<void*>(vec);
    return AParcel_readByteArray(parcel, &vectorData, &AParcel_stdVectorReallocator<int8_t>,
                                 AParcel_stdVectorGetter<int8_t>);
}

// @END

/**
 * Takes a std::string and reallocates it to the specified length. For use with AParcel_readString.
 * See use below in AParcel_readString.
@@ -68,6 +300,27 @@ static inline binder_status_t AParcel_readString(const AParcel* parcel, std::str
                              &stringData);
}

template <typename T>
static inline binder_status_t AParcel_writeVectorSize(AParcel* parcel, const std::vector<T>& vec) {
    if (vec.size() > INT32_MAX) {
        return STATUS_BAD_VALUE;
    }

    return AParcel_writeInt32(parcel, static_cast<int32_t>(vec.size()));
}

template <typename T>
static inline binder_status_t AParcel_resizeVector(const AParcel* parcel, std::vector<T>* vec) {
    int32_t size;
    binder_status_t err = AParcel_readInt32(parcel, &size);

    if (err != STATUS_OK) return err;
    if (size < 0) return STATUS_UNEXPECTED_NULL;

    vec->resize(static_cast<size_t>(size));
    return STATUS_OK;
}

} // namespace ndk

#endif // __cplusplus
+18 −0
Original line number Diff line number Diff line
@@ -24,30 +24,48 @@ LIBBINDER_NDK { # introduced=29
    AIBinder_Weak_promote;
    AParcel_delete;
    AParcel_readBool;
    AParcel_readBoolArray;
    AParcel_readByte;
    AParcel_readByteArray;
    AParcel_readChar;
    AParcel_readCharArray;
    AParcel_readDouble;
    AParcel_readDoubleArray;
    AParcel_readFloat;
    AParcel_readFloatArray;
    AParcel_readInt32;
    AParcel_readInt32Array;
    AParcel_readInt64;
    AParcel_readInt64Array;
    AParcel_readNullableStrongBinder;
    AParcel_readStatusHeader;
    AParcel_readString;
    AParcel_readStrongBinder;
    AParcel_readUint32;
    AParcel_readUint32Array;
    AParcel_readUint64;
    AParcel_readUint64Array;
    AParcel_writeBool;
    AParcel_writeBoolArray;
    AParcel_writeByte;
    AParcel_writeByteArray;
    AParcel_writeChar;
    AParcel_writeCharArray;
    AParcel_writeDouble;
    AParcel_writeDoubleArray;
    AParcel_writeFloat;
    AParcel_writeFloatArray;
    AParcel_writeInt32;
    AParcel_writeInt32Array;
    AParcel_writeInt64;
    AParcel_writeInt64Array;
    AParcel_writeStatusHeader;
    AParcel_writeString;
    AParcel_writeStrongBinder;
    AParcel_writeUint32;
    AParcel_writeUint32Array;
    AParcel_writeUint64;
    AParcel_writeUint64Array;
    AStatus_delete;
    AStatus_fromExceptionCode;
    AStatus_fromExceptionCodeWithMessage;
+253 −0

File changed.

Preview size limit exceeded, changes collapsed.

+98 −5

File changed.

Preview size limit exceeded, changes collapsed.

Loading