blob: 5aaaa0c3d2ff14e38ec8262a0ea9f5ed4cff86e4 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Steven Morelandc7a871e2020-11-10 21:56:57 +000017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Steven Moreland93fee8d2019-07-12 15:07:06 -070019#include <map> // for legacy reasons
Christopher Wiley9a5e32f2016-01-28 16:56:53 -080020#include <string>
Daniel Normand0337ef2019-09-20 15:46:03 -070021#include <type_traits>
Casey Dahlin451ff582015-10-19 18:12:18 -070022#include <vector>
23
Christopher Wiley83e6b982016-04-19 10:27:00 -070024#include <android-base/unique_fd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <cutils/native_handle.h>
26#include <utils/Errors.h>
27#include <utils/RefBase.h>
28#include <utils/String16.h>
29#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070030#include <utils/Flattenable.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Casey Dahlinf0c13772015-10-27 18:33:56 -070032#include <binder/IInterface.h>
Christopher Wiley97f048d2015-11-19 06:49:05 -080033#include <binder/Parcelable.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070034
Steven Morelandd63ed9d2019-09-04 18:15:25 -070035#ifdef BINDER_IPC_32BIT
Jiyong Park16c6e702020-11-13 20:53:12 +090036//NOLINTNEXTLINE(google-runtime-int) b/173188702
Steven Moreland28723ae2019-04-01 18:52:30 -070037typedef unsigned int binder_size_t;
Steven Morelandd63ed9d2019-09-04 18:15:25 -070038#else
Jiyong Park16c6e702020-11-13 20:53:12 +090039//NOLINTNEXTLINE(google-runtime-int) b/173188702
Steven Moreland28723ae2019-04-01 18:52:30 -070040typedef unsigned long long binder_size_t;
Steven Morelandd63ed9d2019-09-04 18:15:25 -070041#endif
42
Steven Moreland5d242172020-11-13 19:38:50 +000043struct flat_binder_object;
Steven Morelandd63ed9d2019-09-04 18:15:25 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045// ---------------------------------------------------------------------------
46namespace android {
47
Mathias Agopiane1424282013-07-29 21:24:40 -070048template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070049template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070051class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052class ProcessState;
Steven Morelandc9939062021-05-05 17:57:41 +000053class RpcSession;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054class String8;
55class TextOutput;
56
Mathias Agopiane1424282013-07-29 21:24:40 -070057class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000058 friend class IPCThreadState;
Steven Moreland5553ac42020-11-11 02:14:45 +000059 friend class RpcState;
60
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070062 class ReadableBlob;
63 class WritableBlob;
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 Parcel();
66 ~Parcel();
67
68 const uint8_t* data() const;
69 size_t dataSize() const;
70 size_t dataAvail() const;
71 size_t dataPosition() const;
72 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 status_t setDataSize(size_t size);
75 void setDataPosition(size_t pos) const;
76 status_t setDataCapacity(size_t size);
Steven Morelanda86a3562019-08-01 23:28:34 +000077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 status_t setData(const uint8_t* buffer, size_t len);
79
Andreas Huber51faf462011-04-13 10:21:56 -070080 status_t appendFrom(const Parcel *parcel,
81 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
Dianne Hackborn15feb9b2017-04-10 15:34:35 -070083 int compareData(const Parcel& other);
84
Jeff Brown13b16042014-11-11 16:44:25 -080085 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070086 bool pushAllowFds(bool allowFds);
87 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 bool hasFileDescriptors() const;
90
Steven Morelandf183fdd2020-10-27 00:12:12 +000091 // Zeros data when reallocating. Other mitigations may be added
92 // in the future.
93 //
94 // WARNING: some read methods may make additional copies of data.
95 // In order to verify this, heap dumps should be used.
96 void markSensitive() const;
97
Steven Moreland5553ac42020-11-11 02:14:45 +000098 // For a 'data' Parcel, this should mark the Parcel as being prepared for a
99 // transaction on this specific binder object. Based on this, the format of
100 // the wire binder protocol may change (data is written differently when it
101 // is for an RPC transaction).
102 void markForBinder(const sp<IBinder>& binder);
103
Steven Morelandf0769c92021-04-13 02:03:08 +0000104 // Whenever possible, markForBinder should be preferred. This method is
105 // called automatically on reply Parcels for RPC transactions.
Steven Morelandc9939062021-05-05 17:57:41 +0000106 void markForRpc(const sp<RpcSession>& session);
Steven Morelandf0769c92021-04-13 02:03:08 +0000107
Steven Moreland5553ac42020-11-11 02:14:45 +0000108 // Whether this Parcel is written for RPC transactions (after calls to
109 // markForBinder or markForRpc).
110 bool isForRpc() const;
111
112 // Writes the IPC/RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 status_t writeInterfaceToken(const String16& interface);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000114 status_t writeInterfaceToken(const char16_t* str, size_t len);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700115
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700116 // Parses the RPC header, returning true if the interface name
117 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700118 //
119 // Additionally, enforceInterface does part of the work of
120 // propagating the StrictMode policy mask, populating the current
121 // IPCThreadState, which as an optimization may optionally be
122 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700123 bool enforceInterface(const String16& interface,
Yi Kong0cf75842018-07-10 11:44:36 -0700124 IPCThreadState* threadState = nullptr) const;
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700125 bool enforceInterface(const char16_t* interface,
126 size_t len,
127 IPCThreadState* threadState = nullptr) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700128 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -0700129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 void freeData();
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 size_t objectsCount() const;
133
134 status_t errorCheck() const;
135 void setError(status_t err);
136
137 status_t write(const void* data, size_t len);
138 void* writeInplace(size_t len);
139 status_t writeUnpadded(const void* data, size_t len);
140 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800141 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700143 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 status_t writeFloat(float val);
145 status_t writeDouble(double val);
146 status_t writeCString(const char* str);
147 status_t writeString8(const String8& str);
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -0600148 status_t writeString8(const char* str, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 status_t writeString16(const String16& str);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900150 status_t writeString16(const std::optional<String16>& str);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900151 status_t writeString16(const std::unique_ptr<String16>& str) __attribute__((deprecated("use std::optional version instead")));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 status_t writeString16(const char16_t* str, size_t len);
153 status_t writeStrongBinder(const sp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700154 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700155 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700156 status_t writeBool(bool val);
157 status_t writeChar(char16_t val);
158 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700159
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800160 // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
161 status_t writeUtf8AsUtf16(const std::string& str);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900162 status_t writeUtf8AsUtf16(const std::optional<std::string>& str);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900163 status_t writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) __attribute__((deprecated("use std::optional version instead")));
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800164
Jooyung Han2d5878e2020-01-23 12:45:10 +0900165 status_t writeByteVector(const std::optional<std::vector<int8_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900166 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700167 status_t writeByteVector(const std::vector<int8_t>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900168 status_t writeByteVector(const std::optional<std::vector<uint8_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900169 status_t writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin185d3442016-02-09 11:08:35 -0800170 status_t writeByteVector(const std::vector<uint8_t>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900171 status_t writeInt32Vector(const std::optional<std::vector<int32_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900172 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700173 status_t writeInt32Vector(const std::vector<int32_t>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900174 status_t writeInt64Vector(const std::optional<std::vector<int64_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900175 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700176 status_t writeInt64Vector(const std::vector<int64_t>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900177 status_t writeUint64Vector(const std::optional<std::vector<uint64_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900178 status_t writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800179 status_t writeUint64Vector(const std::vector<uint64_t>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900180 status_t writeFloatVector(const std::optional<std::vector<float>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900181 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700182 status_t writeFloatVector(const std::vector<float>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900183 status_t writeDoubleVector(const std::optional<std::vector<double>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900184 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700185 status_t writeDoubleVector(const std::vector<double>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900186 status_t writeBoolVector(const std::optional<std::vector<bool>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900187 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700188 status_t writeBoolVector(const std::vector<bool>& val);
Jooyung Han2d5878e2020-01-23 12:45:10 +0900189 status_t writeCharVector(const std::optional<std::vector<char16_t>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900190 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700191 status_t writeCharVector(const std::vector<char16_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800192 status_t writeString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900193 const std::optional<std::vector<std::optional<String16>>>& val);
194 status_t writeString16Vector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900195 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700196 status_t writeString16Vector(const std::vector<String16>& val);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800197 status_t writeUtf8VectorAsUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900198 const std::optional<std::vector<std::optional<std::string>>>& val);
199 status_t writeUtf8VectorAsUtf16Vector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900200 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) __attribute__((deprecated("use std::optional version instead")));
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800201 status_t writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700202
Jooyung Han2d5878e2020-01-23 12:45:10 +0900203 status_t writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val);
Jooyung Hanceae53e2020-11-18 12:17:50 +0900204 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800205 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
206
Daniel Normand0337ef2019-09-20 15:46:03 -0700207 // Write an Enum vector with underlying type int8_t.
208 // Does not use padding; each byte is contiguous.
209 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800210 status_t writeEnumVector(const std::vector<T>& val)
211 { return writeData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700212 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800213 status_t writeEnumVector(const std::optional<std::vector<T>>& val)
214 { return writeData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900215 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800216 status_t writeEnumVector(const std::unique_ptr<std::vector<T>>& val) __attribute__((deprecated("use std::optional version instead")))
217 { return writeData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700218 // Write an Enum vector with underlying type != int8_t.
219 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800220 status_t writeEnumVector(const std::vector<T>& val)
221 { return writeData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700222 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800223 status_t writeEnumVector(const std::optional<std::vector<T>>& val)
224 { return writeData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900225 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800226 status_t writeEnumVector(const std::unique_ptr<std::vector<T>>& val) __attribute__((deprecated("use std::optional version instead")))
227 { return writeData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700228
Mathias Agopiane1424282013-07-29 21:24:40 -0700229 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800230 status_t writeParcelableVector(const std::optional<std::vector<std::optional<T>>>& val)
231 { return writeData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900232 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800233 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) __attribute__((deprecated("use std::optional version instead")))
234 { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800235 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800236 status_t writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val) __attribute__((deprecated("use std::optional version instead")))
237 { return writeData(val); }
Janis Danisevskis8d747092016-08-11 13:52:12 +0100238 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800239 status_t writeParcelableVector(const std::shared_ptr<std::vector<std::optional<T>>>& val)
240 { return writeData(val); }
Jooyung Han04d19612020-11-19 09:18:17 +0900241 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800242 status_t writeParcelableVector(const std::vector<T>& val)
243 { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800244
245 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800246 status_t writeNullableParcelable(const std::optional<T>& parcelable)
247 { return writeData(parcelable); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900248 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800249 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable) __attribute__((deprecated("use std::optional version instead")))
250 { return writeData(parcelable); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800251
Christopher Wiley97f048d2015-11-19 06:49:05 -0800252 status_t writeParcelable(const Parcelable& parcelable);
253
254 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700255 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256
Mathias Agopian8683fca2012-08-12 19:37:16 -0700257 template<typename T>
258 status_t write(const LightFlattenable<T>& val);
259
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700260 template<typename T>
261 status_t writeVectorSize(const std::vector<T>& val);
262 template<typename T>
Jooyung Han2d5878e2020-01-23 12:45:10 +0900263 status_t writeVectorSize(const std::optional<std::vector<T>>& val);
264 template<typename T>
Jooyung Hanceae53e2020-11-18 12:17:50 +0900265 status_t writeVectorSize(const std::unique_ptr<std::vector<T>>& val) __attribute__((deprecated("use std::optional version instead")));
Mathias Agopian8683fca2012-08-12 19:37:16 -0700266
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700267 // Place a native_handle into the parcel (the native_handle's file-
268 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700269 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700270 // Doesn't take ownership of the native_handle.
271 status_t writeNativeHandle(const native_handle* handle);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 // Place a file descriptor into the parcel. The given fd must remain
274 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700275 // The Parcel does not take ownership of the given fd unless you ask it to.
276 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 // Place a file descriptor into the parcel. A dup of the fd is made, which
279 // will be closed once the parcel is destroyed.
280 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700281
Dianne Hackborn1941a402016-08-29 12:30:43 -0700282 // Place a Java "parcel file descriptor" into the parcel. The given fd must remain
283 // valid for the lifetime of the parcel.
284 // The Parcel does not take ownership of the given fd unless you ask it to.
285 status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false);
286
Ryo Hashimotobf551892018-05-31 16:58:35 +0900287 // Place a Java "parcel file descriptor" into the parcel. A dup of the fd is made, which will
288 // be closed once the parcel is destroyed.
289 status_t writeDupParcelFileDescriptor(int fd);
290
Casey Dahlin06673e32015-11-23 13:24:23 -0800291 // Place a file descriptor into the parcel. This will not affect the
292 // semantics of the smart file descriptor. A new descriptor will be
293 // created, and will be closed when the parcel is destroyed.
294 status_t writeUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700295 const base::unique_fd& fd);
Casey Dahlin06673e32015-11-23 13:24:23 -0800296
297 // Place a vector of file desciptors into the parcel. Each descriptor is
298 // dup'd as in writeDupFileDescriptor
299 status_t writeUniqueFileDescriptorVector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900300 const std::optional<std::vector<base::unique_fd>>& val);
301 status_t writeUniqueFileDescriptorVector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900302 const std::unique_ptr<std::vector<base::unique_fd>>& val) __attribute__((deprecated("use std::optional version instead")));
Casey Dahlinb9872622015-11-25 15:09:45 -0800303 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700304 const std::vector<base::unique_fd>& val);
Casey Dahlin06673e32015-11-23 13:24:23 -0800305
Jeff Brown5707dbf2011-09-23 21:17:56 -0700306 // Writes a blob to the parcel.
307 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800308 // transferred by way of an anonymous shared memory region. Prefer sending
309 // immutable blobs if possible since they may be subsequently transferred between
310 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700311 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800312 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
313
314 // Write an existing immutable blob file descriptor to the parcel.
315 // This allows the client to send the same blob to multiple processes
316 // as long as it keeps a dup of the blob file descriptor handy for later.
317 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
320
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700321 // Like Parcel.java's writeNoException(). Just writes a zero int32.
322 // Currently the native implementation doesn't do any of the StrictMode
323 // stack gathering and serialization that the Java implementation does.
324 status_t writeNoException();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325
326 status_t read(void* outData, size_t len) const;
327 const void* readInplace(size_t len) const;
328 int32_t readInt32() const;
329 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800330 uint32_t readUint32() const;
331 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 int64_t readInt64() const;
333 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700334 uint64_t readUint64() const;
335 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 float readFloat() const;
337 status_t readFloat(float *pArg) const;
338 double readDouble() const;
339 status_t readDouble(double *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700340 bool readBool() const;
341 status_t readBool(bool *pArg) const;
342 char16_t readChar() const;
343 status_t readChar(char16_t *pArg) const;
344 int8_t readByte() const;
345 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800347 // Read a UTF16 encoded string, convert to UTF8
348 status_t readUtf8FromUtf16(std::string* str) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900349 status_t readUtf8FromUtf16(std::optional<std::string>* str) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900350 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const __attribute__((deprecated("use std::optional version instead")));
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 const char* readCString() const;
353 String8 readString8() const;
Roshan Pius87b64d22016-07-18 12:51:02 -0700354 status_t readString8(String8* pArg) const;
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -0600355 const char* readString8Inplace(size_t* outLen) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700357 status_t readString16(String16* pArg) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900358 status_t readString16(std::optional<String16>* pArg) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900359 status_t readString16(std::unique_ptr<String16>* pArg) const __attribute__((deprecated("use std::optional version instead")));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 const char16_t* readString16Inplace(size_t* outLen) const;
361 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700362 status_t readStrongBinder(sp<IBinder>* val) const;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800363 status_t readNullableStrongBinder(sp<IBinder>* val) const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700364
Daniel Normand0337ef2019-09-20 15:46:03 -0700365 // Read an Enum vector with underlying type int8_t.
366 // Does not use padding; each byte is contiguous.
367 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800368 status_t readEnumVector(std::vector<T>* val) const
369 { return readData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700370 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800371 status_t readEnumVector(std::unique_ptr<std::vector<T>>* val) const __attribute__((deprecated("use std::optional version instead")))
372 { return readData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900373 template<typename T, std::enable_if_t<std::is_enum_v<T> && std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800374 status_t readEnumVector(std::optional<std::vector<T>>* val) const
375 { return readData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700376 // Read an Enum vector with underlying type != int8_t.
377 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800378 status_t readEnumVector(std::vector<T>* val) const
379 { return readData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700380 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800381 status_t readEnumVector(std::unique_ptr<std::vector<T>>* val) const __attribute__((deprecated("use std::optional version instead")))
382 { return readData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900383 template<typename T, std::enable_if_t<std::is_enum_v<T> && !std::is_same_v<typename std::underlying_type_t<T>,int8_t>, bool> = 0>
Andy Hung49198cf2020-11-18 11:02:39 -0800384 status_t readEnumVector(std::optional<std::vector<T>>* val) const
385 { return readData(val); }
Daniel Normand0337ef2019-09-20 15:46:03 -0700386
Casey Dahlinf0c13772015-10-27 18:33:56 -0700387 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800388 status_t readParcelableVector(
Andy Hung49198cf2020-11-18 11:02:39 -0800389 std::optional<std::vector<std::optional<T>>>* val) const
390 { return readData(val); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900391 template<typename T>
392 status_t readParcelableVector(
Andy Hung49198cf2020-11-18 11:02:39 -0800393 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const __attribute__((deprecated("use std::optional version instead")))
394 { return readData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800395 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800396 status_t readParcelableVector(std::vector<T>* val) const
397 { return readData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800398
Christopher Wiley97f048d2015-11-19 06:49:05 -0800399 status_t readParcelable(Parcelable* parcelable) const;
400
401 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800402 status_t readParcelable(std::optional<T>* parcelable) const
403 { return readData(parcelable); }
Jooyung Han2d5878e2020-01-23 12:45:10 +0900404 template<typename T>
Andy Hung49198cf2020-11-18 11:02:39 -0800405 status_t readParcelable(std::unique_ptr<T>* parcelable) const __attribute__((deprecated("use std::optional version instead")))
406 { return readData(parcelable); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800407
Andy Hung49198cf2020-11-18 11:02:39 -0800408 // If strong binder would be nullptr, readStrongBinder() returns an error.
409 // TODO: T must be derived from IInterface, fix for clarity.
Casey Dahlinb9872622015-11-25 15:09:45 -0800410 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800411 status_t readStrongBinder(sp<T>* val) const;
412
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800413 template<typename T>
414 status_t readNullableStrongBinder(sp<T>* val) const;
415
Jooyung Han2d5878e2020-01-23 12:45:10 +0900416 status_t readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900417 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800418 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700419
Jooyung Han2d5878e2020-01-23 12:45:10 +0900420 status_t readByteVector(std::optional<std::vector<int8_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900421 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700422 status_t readByteVector(std::vector<int8_t>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900423 status_t readByteVector(std::optional<std::vector<uint8_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900424 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin185d3442016-02-09 11:08:35 -0800425 status_t readByteVector(std::vector<uint8_t>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900426 status_t readInt32Vector(std::optional<std::vector<int32_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900427 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700428 status_t readInt32Vector(std::vector<int32_t>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900429 status_t readInt64Vector(std::optional<std::vector<int64_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900430 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700431 status_t readInt64Vector(std::vector<int64_t>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900432 status_t readUint64Vector(std::optional<std::vector<uint64_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900433 status_t readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800434 status_t readUint64Vector(std::vector<uint64_t>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900435 status_t readFloatVector(std::optional<std::vector<float>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900436 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700437 status_t readFloatVector(std::vector<float>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900438 status_t readDoubleVector(std::optional<std::vector<double>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900439 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700440 status_t readDoubleVector(std::vector<double>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900441 status_t readBoolVector(std::optional<std::vector<bool>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900442 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700443 status_t readBoolVector(std::vector<bool>* val) const;
Jooyung Han2d5878e2020-01-23 12:45:10 +0900444 status_t readCharVector(std::optional<std::vector<char16_t>>* val) const;
Jooyung Hanceae53e2020-11-18 12:17:50 +0900445 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700446 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800447 status_t readString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900448 std::optional<std::vector<std::optional<String16>>>* val) const;
449 status_t readString16Vector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900450 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlin451ff582015-10-19 18:12:18 -0700451 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800452 status_t readUtf8VectorFromUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900453 std::optional<std::vector<std::optional<std::string>>>* val) const;
454 status_t readUtf8VectorFromUtf16Vector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900455 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const __attribute__((deprecated("use std::optional version instead")));
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800456 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700457
Mathias Agopiane1424282013-07-29 21:24:40 -0700458 template<typename T>
459 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700460
Mathias Agopian8683fca2012-08-12 19:37:16 -0700461 template<typename T>
462 status_t read(LightFlattenable<T>& val) const;
463
Andy Hung49198cf2020-11-18 11:02:39 -0800464 // resizeOutVector is used to resize AIDL out vector parameters.
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700465 template<typename T>
466 status_t resizeOutVector(std::vector<T>* val) const;
467 template<typename T>
Jooyung Han2d5878e2020-01-23 12:45:10 +0900468 status_t resizeOutVector(std::optional<std::vector<T>>* val) const;
469 template<typename T>
Jooyung Hanceae53e2020-11-18 12:17:50 +0900470 status_t resizeOutVector(std::unique_ptr<std::vector<T>>* val) const __attribute__((deprecated("use std::optional version instead")));
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700471
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700472 // Like Parcel.java's readExceptionCode(). Reads the first int32
473 // off of a Parcel's header, returning 0 or the negative error
474 // code on exceptions, but also deals with skipping over rich
475 // response headers. Callers should use this to read & parse the
476 // response headers rather than doing it by hand.
477 int32_t readExceptionCode() const;
478
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700479 // Retrieve native_handle from the parcel. This returns a copy of the
480 // parcel's native_handle (the caller takes ownership). The caller
481 // must free the native_handle with native_handle_close() and
482 // native_handle_delete().
483 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484
485
486 // Retrieve a file descriptor from the parcel. This returns the raw fd
487 // in the parcel, which you do not own -- use dup() to get your own copy.
488 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700489
Dianne Hackborn1941a402016-08-29 12:30:43 -0700490 // Retrieve a Java "parcel file descriptor" from the parcel. This returns the raw fd
491 // in the parcel, which you do not own -- use dup() to get your own copy.
492 int readParcelFileDescriptor() const;
493
Casey Dahlin06673e32015-11-23 13:24:23 -0800494 // Retrieve a smart file descriptor from the parcel.
495 status_t readUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700496 base::unique_fd* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800497
Ryo Hashimotobf551892018-05-31 16:58:35 +0900498 // Retrieve a Java "parcel file descriptor" from the parcel.
499 status_t readUniqueParcelFileDescriptor(base::unique_fd* val) const;
500
Casey Dahlin06673e32015-11-23 13:24:23 -0800501
502 // Retrieve a vector of smart file descriptors from the parcel.
503 status_t readUniqueFileDescriptorVector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900504 std::optional<std::vector<base::unique_fd>>* val) const;
505 status_t readUniqueFileDescriptorVector(
Jooyung Hanceae53e2020-11-18 12:17:50 +0900506 std::unique_ptr<std::vector<base::unique_fd>>* val) const __attribute__((deprecated("use std::optional version instead")));
Casey Dahlinb9872622015-11-25 15:09:45 -0800507 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700508 std::vector<base::unique_fd>* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800509
Jeff Brown5707dbf2011-09-23 21:17:56 -0700510 // Reads a blob from the parcel.
511 // The caller should call release() on the blob after reading its contents.
512 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 const flat_binder_object* readObject(bool nullMetaData) const;
515
516 // Explicitly close all file descriptors in the parcel.
517 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800518
519 // Debugging: get metrics on current allocations.
520 static size_t getGlobalAllocSize();
521 static size_t getGlobalAllocCount();
522
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000523 bool replaceCallingWorkSourceUid(uid_t uid);
524 // Returns the work source provided by the caller. This can only be trusted for trusted calling
525 // uid.
Steven Morelandf1b1e492019-05-06 15:05:13 -0700526 uid_t readCallingWorkSourceUid() const;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000527
Steven Moreland161fe122020-11-12 23:16:47 +0000528 void print(TextOutput& to, uint32_t flags = 0) const;
529
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800530private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 typedef void (*release_func)(Parcel* parcel,
532 const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +0000533 const binder_size_t* objects, size_t objectsSize);
534
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800535 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800537 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 size_t ipcObjectsCount() const;
539 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800540 const binder_size_t* objects, size_t objectsCount,
Steven Moreland161fe122020-11-12 23:16:47 +0000541 release_func relFunc);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 status_t finishWrite(size_t len);
544 void releaseObjects();
545 void acquireObjects();
546 status_t growData(size_t len);
547 status_t restartWrite(size_t desired);
548 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000549 status_t writePointer(uintptr_t val);
550 status_t readPointer(uintptr_t *pArg) const;
551 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 void freeDataNoInit();
553 void initState();
554 void scanForFds() const;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800555 status_t validateReadData(size_t len) const;
Andy Hungf37d1bd2021-01-07 08:47:25 -0800556
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000557 void updateWorkSourceRequestHeaderPosition() const;
Steven Morelanda86a3562019-08-01 23:28:34 +0000558
Steven Moreland34b48cb2020-12-01 22:45:38 +0000559 status_t finishFlattenBinder(const sp<IBinder>& binder);
Steven Morelanda86a3562019-08-01 23:28:34 +0000560 status_t finishUnflattenBinder(const sp<IBinder>& binder, sp<IBinder>* out) const;
561 status_t flattenBinder(const sp<IBinder>& binder);
562 status_t unflattenBinder(sp<IBinder>* out) const;
563
Andreas Huber84a6d042009-08-17 13:33:27 -0700564 template<class T>
565 status_t readAligned(T *pArg) const;
566
567 template<class T> T readAligned() const;
568
569 template<class T>
570 status_t writeAligned(T val);
571
Casey Dahlinb9872622015-11-25 15:09:45 -0800572 status_t writeRawNullableParcelable(const Parcelable*
573 parcelable);
574
Andy Hung49198cf2020-11-18 11:02:39 -0800575 //-----------------------------------------------------------------------------
576 // Generic type read and write methods for Parcel:
577 //
578 // readData(T *value) will read a value from the Parcel.
579 // writeData(const T& value) will write a value to the Parcel.
580 //
581 // Our approach to parceling is based on two overloaded functions
582 // readData() and writeData() that generate parceling code for an
583 // object automatically based on its type. The code from templates are generated at
584 // compile time (if constexpr), and decomposes an object through a call graph matching
585 // recursive descent of the template typename.
586 //
587 // This approach unifies handling of complex objects,
588 // resulting in fewer lines of code, greater consistency,
589 // extensibility to nested types, efficiency (decisions made at compile time),
590 // and better code maintainability and optimization.
591 //
592 // Design decision: Incorporate the read and write code into Parcel rather than
593 // as a non-intrusive serializer that emits a byte stream, as we have
594 // active objects, alignment, legacy code, and historical idiosyncrasies.
595 //
596 // --- Overview
597 //
598 // Parceling is a way of serializing objects into a sequence of bytes for communication
599 // between processes, as part of marshaling data for remote procedure calls.
600 //
601 // The Parcel instance contains objects serialized as bytes, such as the following:
602 //
603 // 1) Ordinary primitive data such as int, float.
604 // 2) Established structured data such as String16, std::string.
605 // 3) Parcelables, which are C++ objects that derive from Parcelable (and thus have a
606 // readFromParcel and writeToParcel method). (Similar for Java)
607 // 4) A std::vector<> of such data.
608 // 5) Nullable objects contained in std::optional, std::unique_ptr, or std::shared_ptr.
609 //
610 // And active objects from the Android ecosystem such as:
611 // 6) File descriptors, base::unique_fd (kernel object handles)
612 // 7) Binder objects, sp<IBinder> (active Android RPC handles)
613 //
614 // Objects from (1) through (5) serialize into the mData buffer.
615 // Active objects (6) and (7) serialize into both mData and mObjects buffers.
616 //
617 // --- Data layout details
618 //
619 // Data is read or written to the parcel by recursively decomposing the type of the parameter
620 // type T through readData() and writeData() methods.
621 //
622 // We focus on writeData() here in our explanation of the data layout.
623 //
624 // 1) Alignment
625 // Implementation detail: Regardless of the parameter type, writeData() calls are designed
626 // to finish at a multiple of 4 bytes, the default alignment of the Parcel.
627 //
628 // Writes of single uint8_t, int8_t, enums based on types of size 1, char16_t, etc
629 // will result in 4 bytes being written. The data is widened to int32 and then written;
630 // hence the position of the nonzero bytes depend on the native endianness of the CPU.
631 //
632 // Writes of primitive values with 8 byte size, double, int64_t, uint64_t,
633 // are stored with 4 byte alignment. The ARM and x86/x64 permit unaligned reads
634 // and writes (albeit with potential latency/throughput penalty) which may or may
635 // not be observable unless the process is IO bound.
636 //
637 // 2) Parcelables
638 // Parcelables are detected by the type's base class, and implemented through calling
639 // into the Parcelable type's readFromParcel() or writeToParcel() methods.
640 // Historically, due to null object detection, a (int32_t) 1 is prepended to the data written.
641 // Parcelables must have a default constructor (i.e. one that takes no arguments).
642 //
643 // 3) Arrays
644 // Arrays of uint8_t and int8_t, and enums based on size 1 are written as
645 // a contiguous packed byte stream. Hidden zero padding is applied at the end of the byte
646 // stream to make a multiple of 4 bytes (and prevent info leakage when writing).
647 //
648 // All other array writes can be conceptually thought of as recursively calling
649 // writeData on the individual elements (though may be implemented differently for speed).
650 // As discussed in (1), alignment rules are therefore applied for each element
651 // write (not as an aggregate whole), so the wire representation of data can be
652 // substantially larger.
653 //
654 // Historical Note:
655 // Because of element-wise alignment, CharVector and BoolVector are expanded
656 // element-wise into integers even though they could have been optimized to be packed
657 // just like uint8_t, int8_t (size 1 data).
658 //
659 // 3.1) Arrays accessed by the std::vector type. This is the default for AIDL.
660 //
661 // 4) Nullables
662 // std::optional, std::unique_ptr, std::shared_ptr are all parceled identically
663 // (i.e. result in identical byte layout).
664 // The target of the std::optional, std::unique_ptr, or std::shared_ptr
665 // can either be a std::vector, String16, std::string, or a Parcelable.
666 //
667 // Detection of null relies on peeking the first int32 data and checking if the
668 // the peeked value is considered invalid for the object:
669 // (-1 for vectors, String16, std::string) (0 for Parcelables). If the peeked value
670 // is invalid, then a null is returned.
671 //
672 // Application Note: When to use each nullable type:
673 //
674 // std::optional: Embeds the object T by value rather than creating a new instance
675 // by managed pointer as std::unique_ptr or std::shared_ptr. This will save a malloc
676 // when creating an optional instance.
677 //
678 // Use of std::optionals by value can result in copies of the underlying value stored in it,
679 // so a std::move may be used to move in and move out (for example) a vector value into
680 // the std::optional or for the std::optional itself.
681 //
682 // std::unique_ptr, std::shared_ptr: These are preferred when the lifetime of the object is
683 // already managed by the application. This reduces unnecessary copying of data
684 // especially when the calls are local in-proc (rather than via binder rpc).
685 //
686 // 5) StrongBinder (sp<IBinder>)
687 // StrongBinder objects are written regardless of null. When read, null StrongBinder values
688 // will be interpreted as UNKNOWN_ERROR if the type is a single argument <sp<T>>
689 // or in a vector argument <std::vector<sp<T>>. However, they will be read without an error
690 // if present in a std::optional, std::unique_ptr, or std::shared_ptr vector, e.g.
691 // <std::optional<std::vector<sp<T>>>.
692 //
693 // See AIDL annotation @Nullable, readStrongBinder(), and readNullableStrongBinder().
694 //
695 // Historical Note: writing a vector of StrongBinder objects <std::vector<sp<T>>
696 // containing a null will not cause an error. However reading such a vector will cause
697 // an error _and_ early termination of the read.
Daniel Normand0337ef2019-09-20 15:46:03 -0700698
Andy Hung49198cf2020-11-18 11:02:39 -0800699 // --- Examples
700 //
701 // Using recursive parceling, we can parcel complex data types so long
702 // as they obey the rules described above.
703 //
704 // Example #1
705 // Parceling of a 3D vector
706 //
707 // std::vector<std::vector<std::vector<int32_t>>> v1 {
708 // { {1}, {2, 3}, {4} },
709 // {},
710 // { {10}, {20}, {30, 40} },
711 // };
712 // Parcel p1;
713 // p1.writeData(v1);
714 // decltype(v1) v2;
715 // p1.setDataPosition(0);
716 // p1.readData(&v2);
717 // ASSERT_EQ(v1, v2);
718 //
719 // Example #2
720 // Parceling of mixed shared pointers
721 //
722 // Parcel p1;
723 // auto sp1 = std::make_shared<std::vector<std::shared_ptr<std::vector<int>>>>(3);
724 // (*sp1)[2] = std::make_shared<std::vector<int>>(3);
725 // (*(*sp1)[2])[2] = 2;
726 // p1.writeData(sp1);
727 // decltype(sp1) sp2;
728 // p1.setDataPosition(0);
729 // p1.readData(&sp2);
730 // ASSERT_EQ((*sp1)[0], (*sp2)[0]); // nullptr
731 // ASSERT_EQ((*sp1)[1], (*sp2)[1]); // nullptr
732 // ASSERT_EQ(*(*sp1)[2], *(*sp2)[2]); // { 0, 0, 2}
Daniel Normand0337ef2019-09-20 15:46:03 -0700733
Andy Hung49198cf2020-11-18 11:02:39 -0800734 // --- Helper Methods
735 // TODO: move this to a utils header.
736 //
737 // Determine if a type is a specialization of a templated type
738 // Example: is_specialization_v<T, std::vector>
Daniel Normand0337ef2019-09-20 15:46:03 -0700739
Andy Hung49198cf2020-11-18 11:02:39 -0800740 template <typename Test, template <typename...> class Ref>
741 struct is_specialization : std::false_type {};
742
743 template <template <typename...> class Ref, typename... Args>
744 struct is_specialization<Ref<Args...>, Ref>: std::true_type {};
745
746 template <typename Test, template <typename...> class Ref>
747 static inline constexpr bool is_specialization_v = is_specialization<Test, Ref>::value;
748
749 // Get the first template type from a container, the T from MyClass<T, ...>.
750 template<typename T> struct first_template_type;
751
752 template <template <typename ...> class V, typename T, typename... Args>
753 struct first_template_type<V<T, Args...>> {
754 using type_t = T;
755 };
756
757 template <typename T>
758 using first_template_type_t = typename first_template_type<T>::type_t;
759
760 // For static assert(false) we need a template version to avoid early failure.
761 template <typename T>
762 static inline constexpr bool dependent_false_v = false;
763
764 // primitive types that we consider packed and trivially copyable as an array
765 template <typename T>
766 static inline constexpr bool is_pointer_equivalent_array_v =
767 std::is_same_v<T, int8_t>
768 || std::is_same_v<T, uint8_t>
769 // We could support int16_t and uint16_t, but those aren't currently AIDL types.
770 || std::is_same_v<T, int32_t>
771 || std::is_same_v<T, uint32_t>
772 || std::is_same_v<T, float>
773 // are unaligned reads and write support is assumed.
774 || std::is_same_v<T, uint64_t>
775 || std::is_same_v<T, int64_t>
776 || std::is_same_v<T, double>
777 || (std::is_enum_v<T> && (sizeof(T) == 1 || sizeof(T) == 4)); // size check not type
778
779 // allowed "nullable" types
780 // These are nonintrusive containers std::optional, std::unique_ptr, std::shared_ptr.
781 template <typename T>
782 static inline constexpr bool is_parcel_nullable_type_v =
783 is_specialization_v<T, std::optional>
784 || is_specialization_v<T, std::unique_ptr>
785 || is_specialization_v<T, std::shared_ptr>;
786
787 // special int32 value to indicate NonNull or Null parcelables
788 // This is fixed to be only 0 or 1 by contract, do not change.
789 static constexpr int32_t kNonNullParcelableFlag = 1;
790 static constexpr int32_t kNullParcelableFlag = 0;
791
792 // special int32 size representing a null vector, when applicable in Nullable data.
793 // This fixed as -1 by contract, do not change.
794 static constexpr int32_t kNullVectorSize = -1;
795
796 // --- readData and writeData methods.
797 // We choose a mixture of function and template overloads to improve code readability.
798 // TODO: Consider C++20 concepts when they become available.
799
800 // writeData function overloads.
801 // Implementation detail: Function overloading improves code readability over
802 // template overloading, but prevents writeData<T> from being used for those types.
803
804 status_t writeData(bool t) {
805 return writeBool(t); // this writes as int32_t
806 }
807
808 status_t writeData(int8_t t) {
809 return writeByte(t); // this writes as int32_t
810 }
811
812 status_t writeData(uint8_t t) {
813 return writeByte(static_cast<int8_t>(t)); // this writes as int32_t
814 }
815
816 status_t writeData(char16_t t) {
817 return writeChar(t); // this writes as int32_t
818 }
819
820 status_t writeData(int32_t t) {
821 return writeInt32(t);
822 }
823
824 status_t writeData(uint32_t t) {
825 return writeUint32(t);
826 }
827
828 status_t writeData(int64_t t) {
829 return writeInt64(t);
830 }
831
832 status_t writeData(uint64_t t) {
833 return writeUint64(t);
834 }
835
836 status_t writeData(float t) {
837 return writeFloat(t);
838 }
839
840 status_t writeData(double t) {
841 return writeDouble(t);
842 }
843
844 status_t writeData(const String16& t) {
845 return writeString16(t);
846 }
847
848 status_t writeData(const std::string& t) {
849 return writeUtf8AsUtf16(t);
850 }
851
852 status_t writeData(const base::unique_fd& t) {
853 return writeUniqueFileDescriptor(t);
854 }
855
856 status_t writeData(const Parcelable& t) { // std::is_base_of_v<Parcelable, T>
857 // implemented here. writeParcelable() calls this.
858 status_t status = writeData(static_cast<int32_t>(kNonNullParcelableFlag));
859 if (status != OK) return status;
860 return t.writeToParcel(this);
861 }
862
863 // writeData<T> template overloads.
864 // Written such that the first template type parameter is the complete type
865 // of the first function parameter.
866 template <typename T,
867 typename std::enable_if_t<std::is_enum_v<T>, bool> = true>
868 status_t writeData(const T& t) {
869 // implemented here. writeEnum() calls this.
870 using UT = std::underlying_type_t<T>;
871 return writeData(static_cast<UT>(t)); // recurse
872 }
873
874 template <typename T,
875 typename std::enable_if_t<is_specialization_v<T, sp>, bool> = true>
876 status_t writeData(const T& t) {
877 return writeStrongBinder(t);
878 }
879
880 // std::optional, std::unique_ptr, std::shared_ptr special case.
881 template <typename CT,
882 typename std::enable_if_t<is_parcel_nullable_type_v<CT>, bool> = true>
883 status_t writeData(const CT& c) {
884 using T = first_template_type_t<CT>; // The T in CT == C<T, ...>
885 if constexpr (is_specialization_v<T, std::vector>
886 || std::is_same_v<T, String16>
887 || std::is_same_v<T, std::string>) {
888 if (!c) return writeData(static_cast<int32_t>(kNullVectorSize));
889 } else if constexpr (std::is_base_of_v<Parcelable, T>) {
890 if (!c) return writeData(static_cast<int32_t>(kNullParcelableFlag));
891 } else /* constexpr */ { // could define this, but raise as error.
892 static_assert(dependent_false_v<CT>);
893 }
894 return writeData(*c);
895 }
896
897 template <typename CT,
898 typename std::enable_if_t<is_specialization_v<CT, std::vector>, bool> = true>
899 status_t writeData(const CT& c) {
900 using T = first_template_type_t<CT>; // The T in CT == C<T, ...>
901 if (c.size() > std::numeric_limits<int32_t>::max()) return BAD_VALUE;
902 const auto size = static_cast<int32_t>(c.size());
903 writeData(size);
904 if constexpr (is_pointer_equivalent_array_v<T>) {
905 constexpr size_t limit = std::numeric_limits<size_t>::max() / sizeof(T);
906 if (c.size() > limit) return BAD_VALUE;
907 // is_pointer_equivalent types do not have gaps which could leak info,
908 // which is only a concern when writing through binder.
909
910 // TODO: Padding of the write is suboptimal when the length of the
911 // data is not a multiple of 4. Consider improving the write() method.
912 return write(c.data(), c.size() * sizeof(T));
913 } else if constexpr (std::is_same_v<T, bool>
914 || std::is_same_v<T, char16_t>) {
915 // reserve data space to write to
916 auto data = reinterpret_cast<int32_t*>(writeInplace(c.size() * sizeof(int32_t)));
917 if (data == nullptr) return BAD_VALUE;
918 for (const auto t: c) {
919 *data++ = static_cast<int32_t>(t);
920 }
921 } else /* constexpr */ {
922 for (const auto &t : c) {
923 const status_t status = writeData(t);
924 if (status != OK) return status;
925 }
926 }
927 return OK;
928 }
929
930 // readData function overloads.
931 // Implementation detail: Function overloading improves code readability over
932 // template overloading, but prevents readData<T> from being used for those types.
933
934 status_t readData(bool* t) const {
935 return readBool(t); // this reads as int32_t
936 }
937
938 status_t readData(int8_t* t) const {
939 return readByte(t); // this reads as int32_t
940 }
941
942 status_t readData(uint8_t* t) const {
943 return readByte(reinterpret_cast<int8_t*>(t)); // NOTE: this reads as int32_t
944 }
945
946 status_t readData(char16_t* t) const {
947 return readChar(t); // this reads as int32_t
948 }
949
950 status_t readData(int32_t* t) const {
951 return readInt32(t);
952 }
953
954 status_t readData(uint32_t* t) const {
955 return readUint32(t);
956 }
957
958 status_t readData(int64_t* t) const {
959 return readInt64(t);
960 }
961
962 status_t readData(uint64_t* t) const {
963 return readUint64(t);
964 }
965
966 status_t readData(float* t) const {
967 return readFloat(t);
968 }
969
970 status_t readData(double* t) const {
971 return readDouble(t);
972 }
973
974 status_t readData(String16* t) const {
975 return readString16(t);
976 }
977
978 status_t readData(std::string* t) const {
979 return readUtf8FromUtf16(t);
980 }
981
982 status_t readData(base::unique_fd* t) const {
983 return readUniqueFileDescriptor(t);
984 }
985
986 status_t readData(Parcelable* t) const { // std::is_base_of_v<Parcelable, T>
987 // implemented here. readParcelable() calls this.
988 int32_t present;
989 status_t status = readData(&present);
990 if (status != OK) return status;
991 if (present != kNonNullParcelableFlag) return UNEXPECTED_NULL;
992 return t->readFromParcel(this);
993 }
994
995 // readData<T> template overloads.
996 // Written such that the first template type parameter is the complete type
997 // of the first function parameter.
998
999 template <typename T,
1000 typename std::enable_if_t<std::is_enum_v<T>, bool> = true>
1001 status_t readData(T* t) const {
1002 // implemented here. readEnum() calls this.
1003 using UT = std::underlying_type_t<T>;
1004 return readData(reinterpret_cast<UT*>(t));
1005 }
1006
1007 template <typename T,
1008 typename std::enable_if_t<is_specialization_v<T, sp>, bool> = true>
1009 status_t readData(T* t) const {
1010 return readStrongBinder(t); // Note: on null, returns failure
1011 }
1012
1013
1014 template <typename CT,
1015 typename std::enable_if_t<is_parcel_nullable_type_v<CT>, bool> = true>
1016 status_t readData(CT* c) const {
1017 using T = first_template_type_t<CT>; // The T in CT == C<T, ...>
1018 const size_t startPos = dataPosition();
1019 int32_t peek;
1020 status_t status = readData(&peek);
1021 if (status != OK) return status;
1022 if constexpr (is_specialization_v<T, std::vector>
1023 || std::is_same_v<T, String16>
1024 || std::is_same_v<T, std::string>) {
1025 if (peek == kNullVectorSize) {
1026 c->reset();
1027 return OK;
1028 }
1029 } else if constexpr (std::is_base_of_v<Parcelable, T>) {
1030 if (peek == kNullParcelableFlag) {
1031 c->reset();
1032 return OK;
1033 }
1034 } else /* constexpr */ { // could define this, but raise as error.
1035 static_assert(dependent_false_v<CT>);
1036 }
1037 // create a new object.
1038 if constexpr (is_specialization_v<CT, std::optional>) {
1039 c->emplace();
1040 } else /* constexpr */ {
1041 T* const t = new (std::nothrow) T; // contents read from Parcel below.
1042 if (t == nullptr) return NO_MEMORY;
1043 c->reset(t);
1044 }
1045 // rewind data ptr to reread (this is pretty quick), otherwise we could
1046 // pass an optional argument to readData to indicate a peeked value.
1047 setDataPosition(startPos);
1048 if constexpr (is_specialization_v<T, std::vector>) {
1049 return readData(&**c, READ_FLAG_SP_NULLABLE); // nullable sp<> allowed now
1050 } else {
1051 return readData(&**c);
1052 }
1053 }
1054
1055 // std::vector special case, incorporating flags whether the vector
1056 // accepts nullable sp<> to be read.
1057 enum ReadFlags {
1058 READ_FLAG_NONE = 0,
1059 READ_FLAG_SP_NULLABLE = 1 << 0,
1060 };
1061
1062 template <typename CT,
1063 typename std::enable_if_t<is_specialization_v<CT, std::vector>, bool> = true>
1064 status_t readData(CT* c, ReadFlags readFlags = READ_FLAG_NONE) const {
1065 using T = first_template_type_t<CT>; // The T in CT == C<T, ...>
1066 int32_t size;
1067 status_t status = readInt32(&size);
1068 if (status != OK) return status;
1069 if (size < 0) return UNEXPECTED_NULL;
1070 const size_t availableBytes = dataAvail(); // coarse bound on vector size.
1071 if (static_cast<size_t>(size) > availableBytes) return BAD_VALUE;
1072 c->clear(); // must clear before resizing/reserving otherwise move ctors may be called.
1073 if constexpr (is_pointer_equivalent_array_v<T>) {
1074 // could consider POD without gaps and alignment of 4.
1075 auto data = reinterpret_cast<const T*>(
1076 readInplace(static_cast<size_t>(size) * sizeof(T)));
1077 if (data == nullptr) return BAD_VALUE;
1078 c->insert(c->begin(), data, data + size); // insert should do a reserve().
1079 } else if constexpr (std::is_same_v<T, bool>
1080 || std::is_same_v<T, char16_t>) {
1081 c->reserve(size); // avoids default initialization
1082 auto data = reinterpret_cast<const int32_t*>(
1083 readInplace(static_cast<size_t>(size) * sizeof(int32_t)));
1084 if (data == nullptr) return BAD_VALUE;
1085 for (int32_t i = 0; i < size; ++i) {
1086 c->emplace_back(static_cast<T>(*data++));
1087 }
1088 } else if constexpr (is_specialization_v<T, sp>) {
1089 c->resize(size); // calls ctor
1090 if (readFlags & READ_FLAG_SP_NULLABLE) {
1091 for (auto &t : *c) {
1092 status = readNullableStrongBinder(&t); // allow nullable
1093 if (status != OK) return status;
1094 }
1095 } else {
1096 for (auto &t : *c) {
1097 status = readStrongBinder(&t);
1098 if (status != OK) return status;
1099 }
1100 }
1101 } else /* constexpr */ {
1102 c->resize(size); // calls ctor
1103 for (auto &t : *c) {
1104 status = readData(&t);
1105 if (status != OK) return status;
1106 }
1107 }
1108 return OK;
1109 }
1110
1111 //-----------------------------------------------------------------------------
1112 private:
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 status_t mError;
1115 uint8_t* mData;
1116 size_t mDataSize;
1117 size_t mDataCapacity;
1118 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001119 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 size_t mObjectsSize;
1121 size_t mObjectsCapacity;
1122 mutable size_t mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001123 mutable bool mObjectsSorted;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001125 mutable bool mRequestHeaderPresent;
Steven Moreland5553ac42020-11-11 02:14:45 +00001126
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001127 mutable size_t mWorkSourceRequestHeaderPosition;
1128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 mutable bool mFdsKnown;
1130 mutable bool mHasFds;
Steven Moreland6e5a7752019-08-05 20:30:14 -07001131 bool mAllowFds;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001132
Steven Morelandf183fdd2020-10-27 00:12:12 +00001133 // if this parcelable is involved in a secure transaction, force the
1134 // data to be overridden with zero when deallocated
1135 mutable bool mDeallocZero;
1136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 release_func mOwner;
Steven Moreland161fe122020-11-12 23:16:47 +00001138
Steven Morelandc9939062021-05-05 17:57:41 +00001139 sp<RpcSession> mSession;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140
1141 class Blob {
1142 public:
1143 Blob();
1144 ~Blob();
1145
Jeff Brown13b16042014-11-11 16:44:25 -08001146 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -07001147 void release();
1148 inline size_t size() const { return mSize; }
Colin Cross17576de2016-09-26 13:07:06 -07001149 inline int fd() const { return mFd; }
Jeff Brown13b16042014-11-11 16:44:25 -08001150 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001151
1152 protected:
Jeff Brown13b16042014-11-11 16:44:25 -08001153 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001154
Jeff Brown13b16042014-11-11 16:44:25 -08001155 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -07001156 void* mData;
1157 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -08001158 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 };
1160
Colin Cross97b64db2016-09-26 13:48:02 -07001161 #if defined(__clang__)
1162 #pragma clang diagnostic push
1163 #pragma clang diagnostic ignored "-Wweak-vtables"
1164 #endif
1165
Fabien Sanglardc38992f2016-10-27 19:05:29 -07001166 // FlattenableHelperInterface and FlattenableHelper avoid generating a vtable entry in objects
1167 // following Flattenable template/protocol.
Mathias Agopiane1424282013-07-29 21:24:40 -07001168 class FlattenableHelperInterface {
1169 protected:
1170 ~FlattenableHelperInterface() { }
1171 public:
1172 virtual size_t getFlattenedSize() const = 0;
1173 virtual size_t getFdCount() const = 0;
1174 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
1175 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
1176 };
1177
Colin Cross97b64db2016-09-26 13:48:02 -07001178 #if defined(__clang__)
1179 #pragma clang diagnostic pop
1180 #endif
1181
Fabien Sanglardc38992f2016-10-27 19:05:29 -07001182 // Concrete implementation of FlattenableHelperInterface that delegates virtual calls to the
1183 // specified class T implementing the Flattenable protocol. It "virtualizes" a compile-time
1184 // protocol.
Mathias Agopiane1424282013-07-29 21:24:40 -07001185 template<typename T>
1186 class FlattenableHelper : public FlattenableHelperInterface {
1187 friend class Parcel;
1188 const Flattenable<T>& val;
Colin Cross382ecd32016-09-26 13:33:59 -07001189 explicit FlattenableHelper(const Flattenable<T>& _val) : val(_val) { }
Mathias Agopiane1424282013-07-29 21:24:40 -07001190
Colin Cross97b64db2016-09-26 13:48:02 -07001191 protected:
1192 ~FlattenableHelper() = default;
Mathias Agopiane1424282013-07-29 21:24:40 -07001193 public:
1194 virtual size_t getFlattenedSize() const {
1195 return val.getFlattenedSize();
1196 }
1197 virtual size_t getFdCount() const {
1198 return val.getFdCount();
1199 }
1200 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
1201 return val.flatten(buffer, size, fds, count);
1202 }
1203 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
1204 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
1205 }
1206 };
1207 status_t write(const FlattenableHelperInterface& val);
1208 status_t read(FlattenableHelperInterface& val) const;
1209
Jeff Brown5707dbf2011-09-23 21:17:56 -07001210public:
1211 class ReadableBlob : public Blob {
1212 friend class Parcel;
1213 public:
1214 inline const void* data() const { return mData; }
Yi Kong0cf75842018-07-10 11:44:36 -07001215 inline void* mutableData() { return isMutable() ? mData : nullptr; }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 };
1217
1218 class WritableBlob : public Blob {
1219 friend class Parcel;
1220 public:
1221 inline void* data() { return mData; }
1222 };
Dan Sandleraa5c2342015-04-10 10:08:45 -04001223
1224private:
Adrian Rooscbf37262015-10-22 16:12:53 -07001225 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001226
1227public:
Adrian Roos6bb31142015-10-22 16:46:12 -07001228 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -04001229 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -07001230 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231};
1232
1233// ---------------------------------------------------------------------------
1234
Mathias Agopian8683fca2012-08-12 19:37:16 -07001235template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -07001236status_t Parcel::write(const Flattenable<T>& val) {
1237 const FlattenableHelper<T> helper(val);
1238 return write(helper);
1239}
1240
1241template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -07001242status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -07001243 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -07001244 if (!val.isFixedSize()) {
Colin Cross4c62b4f2016-09-27 13:58:30 -07001245 if (size > INT32_MAX) {
1246 return BAD_VALUE;
1247 }
1248 status_t err = writeInt32(static_cast<int32_t>(size));
Mathias Agopian8683fca2012-08-12 19:37:16 -07001249 if (err != NO_ERROR) {
1250 return err;
1251 }
1252 }
Mathias Agopian20985172012-08-31 14:25:22 -07001253 if (size) {
1254 void* buffer = writeInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001255 if (buffer == nullptr)
Mathias Agopiane1424282013-07-29 21:24:40 -07001256 return NO_MEMORY;
1257 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -07001258 }
1259 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -07001260}
1261
1262template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -07001263status_t Parcel::read(Flattenable<T>& val) const {
1264 FlattenableHelper<T> helper(val);
1265 return read(helper);
1266}
1267
1268template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -07001269status_t Parcel::read(LightFlattenable<T>& val) const {
1270 size_t size;
1271 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -07001272 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -07001273 } else {
1274 int32_t s;
1275 status_t err = readInt32(&s);
1276 if (err != NO_ERROR) {
1277 return err;
1278 }
Colin Cross4c62b4f2016-09-27 13:58:30 -07001279 size = static_cast<size_t>(s);
Mathias Agopian8683fca2012-08-12 19:37:16 -07001280 }
Mathias Agopian20985172012-08-31 14:25:22 -07001281 if (size) {
1282 void const* buffer = readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001283 return buffer == nullptr ? NO_MEMORY :
Mathias Agopian20985172012-08-31 14:25:22 -07001284 val.unflatten(buffer, size);
1285 }
1286 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -07001287}
1288
Casey Dahlinf0c13772015-10-27 18:33:56 -07001289template<typename T>
Christopher Wiley31c1beb2016-08-19 11:43:54 -07001290status_t Parcel::writeVectorSize(const std::vector<T>& val) {
1291 if (val.size() > INT32_MAX) {
1292 return BAD_VALUE;
1293 }
Colin Cross4c62b4f2016-09-27 13:58:30 -07001294 return writeInt32(static_cast<int32_t>(val.size()));
Christopher Wiley31c1beb2016-08-19 11:43:54 -07001295}
1296
1297template<typename T>
Jooyung Han2d5878e2020-01-23 12:45:10 +09001298status_t Parcel::writeVectorSize(const std::optional<std::vector<T>>& val) {
1299 if (!val) {
1300 return writeInt32(-1);
1301 }
1302
1303 return writeVectorSize(*val);
1304}
1305
1306template<typename T>
Christopher Wiley31c1beb2016-08-19 11:43:54 -07001307status_t Parcel::writeVectorSize(const std::unique_ptr<std::vector<T>>& val) {
1308 if (!val) {
1309 return writeInt32(-1);
1310 }
1311
1312 return writeVectorSize(*val);
1313}
1314
1315template<typename T>
1316status_t Parcel::resizeOutVector(std::vector<T>* val) const {
1317 int32_t size;
1318 status_t err = readInt32(&size);
1319 if (err != NO_ERROR) {
1320 return err;
1321 }
1322
1323 if (size < 0) {
1324 return UNEXPECTED_NULL;
1325 }
1326 val->resize(size_t(size));
1327 return OK;
1328}
1329
1330template<typename T>
Jooyung Han2d5878e2020-01-23 12:45:10 +09001331status_t Parcel::resizeOutVector(std::optional<std::vector<T>>* val) const {
1332 int32_t size;
1333 status_t err = readInt32(&size);
1334 if (err != NO_ERROR) {
1335 return err;
1336 }
1337
1338 val->reset();
1339 if (size >= 0) {
1340 val->emplace(size_t(size));
1341 }
1342
1343 return OK;
1344}
1345
1346template<typename T>
Christopher Wiley31c1beb2016-08-19 11:43:54 -07001347status_t Parcel::resizeOutVector(std::unique_ptr<std::vector<T>>* val) const {
1348 int32_t size;
1349 status_t err = readInt32(&size);
1350 if (err != NO_ERROR) {
1351 return err;
1352 }
1353
1354 val->reset();
1355 if (size >= 0) {
1356 val->reset(new std::vector<T>(size_t(size)));
1357 }
1358
1359 return OK;
1360}
1361
1362template<typename T>
Casey Dahlinf0c13772015-10-27 18:33:56 -07001363status_t Parcel::readStrongBinder(sp<T>* val) const {
1364 sp<IBinder> tmp;
1365 status_t ret = readStrongBinder(&tmp);
1366
1367 if (ret == OK) {
1368 *val = interface_cast<T>(tmp);
1369
1370 if (val->get() == nullptr) {
1371 return UNKNOWN_ERROR;
1372 }
1373 }
1374
1375 return ret;
1376}
1377
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001378template<typename T>
1379status_t Parcel::readNullableStrongBinder(sp<T>* val) const {
1380 sp<IBinder> tmp;
1381 status_t ret = readNullableStrongBinder(&tmp);
1382
1383 if (ret == OK) {
1384 *val = interface_cast<T>(tmp);
1385
Christopher Wiley447b00f2016-07-19 09:23:25 -07001386 if (val->get() == nullptr && tmp.get() != nullptr) {
1387 ret = UNKNOWN_ERROR;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001388 }
1389 }
Christopher Wiley447b00f2016-07-19 09:23:25 -07001390
1391 return ret;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001392}
1393
Mathias Agopian8683fca2012-08-12 19:37:16 -07001394// ---------------------------------------------------------------------------
1395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
1397{
1398 parcel.print(to);
1399 return to;
1400}
1401
Steven Moreland61ff8492019-09-26 16:05:45 -07001402} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403
1404// ---------------------------------------------------------------------------