blob: b21677084c268848047d6fb92999676d1043224d [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <android-base/unique_fd.h>
19#include <binder/IBinder.h>
20#include <binder/Parcel.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000021#include <binder/RpcSession.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000022
23#include <map>
Steven Morelande8393342021-05-05 23:27:53 +000024#include <optional>
Steven Moreland5553ac42020-11-11 02:14:45 +000025#include <queue>
26
27namespace android {
28
29struct RpcWireHeader;
30
31/**
32 * Log a lot more information about RPC calls, when debugging issues. Usually,
33 * you would want to enable this in only one process. If repeated issues require
34 * a specific subset of logs to debug, this could be broken up like
35 * IPCThreadState's.
36 */
37#define SHOULD_LOG_RPC_DETAIL false
38
39#if SHOULD_LOG_RPC_DETAIL
40#define LOG_RPC_DETAIL(...) ALOGI(__VA_ARGS__)
41#else
42#define LOG_RPC_DETAIL(...) ALOGV(__VA_ARGS__) // for type checking
43#endif
44
Steven Morelandb8176792021-06-22 20:29:21 +000045#define RPC_FLAKE_PRONE false
46
Devin Moore08256432021-07-02 13:03:49 -070047#if RPC_FLAKE_PRONE
Steven Morelandb8176792021-06-22 20:29:21 +000048void rpcMaybeWaitToFlake();
49#define MAYBE_WAIT_IN_FLAKE_MODE rpcMaybeWaitToFlake()
50#else
51#define MAYBE_WAIT_IN_FLAKE_MODE do {} while (false)
52#endif
53
Steven Moreland5553ac42020-11-11 02:14:45 +000054/**
55 * Abstracts away management of ref counts and the wire format from
Steven Morelandbdb53ab2021-05-05 17:57:41 +000056 * RpcSession
Steven Moreland5553ac42020-11-11 02:14:45 +000057 */
58class RpcState {
59public:
60 RpcState();
61 ~RpcState();
62
Steven Morelandbf57bce2021-07-26 15:26:12 -070063 status_t readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection,
64 const sp<RpcSession>& session, uint32_t* version);
Steven Moreland5ae62562021-06-10 03:21:42 +000065 status_t sendConnectionInit(const sp<RpcSession::RpcConnection>& connection,
66 const sp<RpcSession>& session);
67 status_t readConnectionInit(const sp<RpcSession::RpcConnection>& connection,
68 const sp<RpcSession>& session);
Steven Morelandc88b7fc2021-06-10 00:40:39 +000069
Steven Moreland7c5e6c22021-05-01 02:55:20 +000070 // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
Steven Moreland5ae62562021-06-10 03:21:42 +000071 sp<IBinder> getRootObject(const sp<RpcSession::RpcConnection>& connection,
72 const sp<RpcSession>& session);
73 status_t getMaxThreads(const sp<RpcSession::RpcConnection>& connection,
74 const sp<RpcSession>& session, size_t* maxThreadsOut);
75 status_t getSessionId(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland826367f2021-09-10 14:05:31 -070076 const sp<RpcSession>& session, std::vector<uint8_t>* sessionIdOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000077
Steven Moreland5ae62562021-06-10 03:21:42 +000078 [[nodiscard]] status_t transact(const sp<RpcSession::RpcConnection>& connection,
79 const sp<IBinder>& address, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000080 const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
Steven Moreland5ae62562021-06-10 03:21:42 +000081 [[nodiscard]] status_t transactAddress(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland5623d1a2021-09-10 15:45:34 -070082 uint64_t address, uint32_t code, const Parcel& data,
83 const sp<RpcSession>& session, Parcel* reply,
84 uint32_t flags);
Steven Morelandfd1e8a02021-07-21 23:30:29 +000085
86 /**
87 * The ownership model here carries an implicit strong refcount whenever a
88 * binder is sent across processes. Since we have a local strong count in
89 * sp<> over these objects, we only ever need to keep one of these. So,
90 * typically we tell the remote process that we drop all the implicit dec
91 * strongs, and we hold onto the last one. 'target' here is the target
92 * timesRecd (the number of remaining reference counts) we wish to keep.
93 * Typically this should be '0' or '1'. The target is used instead of an
94 * explicit decrement count in order to allow multiple threads to lower the
95 * number of counts simultaneously. Since we only lower the count to 0 when
96 * a binder is deleted, targets of '1' should only be sent when the caller
97 * owns a local strong reference to the binder. Larger targets may be used
98 * for testing, and to make the function generic, but generally this should
99 * be avoided because it would be hard to guarantee another thread doesn't
100 * lower the number of held refcounts to '1'. Note also, these refcounts
101 * must be sent actively. If they are sent when binders are deleted, this
102 * can cause leaks, since even remote binders carry an implicit strong ref
103 * when they are sent to another process.
104 */
105 [[nodiscard]] status_t sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection,
106 const sp<RpcSession>& session, uint64_t address,
107 size_t target);
Steven Moreland52eee942021-06-03 00:59:28 +0000108
109 enum class CommandType {
110 ANY,
111 CONTROL_ONLY,
112 };
Steven Moreland5ae62562021-06-10 03:21:42 +0000113 [[nodiscard]] status_t getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland52eee942021-06-03 00:59:28 +0000114 const sp<RpcSession>& session, CommandType type);
Steven Moreland5ae62562021-06-10 03:21:42 +0000115 [[nodiscard]] status_t drainCommands(const sp<RpcSession::RpcConnection>& connection,
116 const sp<RpcSession>& session, CommandType type);
Steven Moreland5553ac42020-11-11 02:14:45 +0000117
118 /**
119 * Called by Parcel for outgoing binders. This implies one refcount of
120 * ownership to the outgoing binder.
121 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000122 [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
Steven Moreland5623d1a2021-09-10 15:45:34 -0700123 uint64_t* outAddress);
Steven Moreland5553ac42020-11-11 02:14:45 +0000124
125 /**
126 * Called by Parcel for incoming binders. This either returns the refcount
127 * to the process, if this process already has one, or it takes ownership of
128 * that refcount
129 */
Steven Moreland5623d1a2021-09-10 15:45:34 -0700130 [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session, uint64_t address,
131 sp<IBinder>* out);
Steven Moreland5553ac42020-11-11 02:14:45 +0000132
133 size_t countBinders();
134 void dump();
135
Steven Moreland5553ac42020-11-11 02:14:45 +0000136 /**
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000137 * Called when reading or writing data to a session fails to clean up
138 * data associated with the session in order to cleanup binders.
Steven Moreland5553ac42020-11-11 02:14:45 +0000139 * Specifically, we have a strong dependency cycle, since BpBinder is
140 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
141 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000142 * BpBinder -> RpcSession -> RpcState
Steven Moreland5553ac42020-11-11 02:14:45 +0000143 * ^-----------------------------/
144 *
145 * In the success case, eventually all refcounts should be propagated over
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000146 * the session, though this could also be called to eagerly cleanup
147 * the session.
Steven Moreland5553ac42020-11-11 02:14:45 +0000148 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000149 * WARNING: RpcState is responsible for calling this when the session is
Steven Moreland5553ac42020-11-11 02:14:45 +0000150 * no longer recoverable.
151 */
Steven Morelandc9d7b532021-06-04 20:57:41 +0000152 void clear();
Steven Moreland5553ac42020-11-11 02:14:45 +0000153
Steven Moreland659416d2021-05-11 00:47:50 +0000154private:
Steven Moreland583a14a2021-06-04 02:04:58 +0000155 void dumpLocked();
Steven Moreland583a14a2021-06-04 02:04:58 +0000156
Steven Morelanddbe71832021-05-12 23:31:00 +0000157 // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps
158 // large allocations to avoid being requested from allocating too much data.
159 struct CommandData {
160 explicit CommandData(size_t size);
Steven Morelande8393342021-05-05 23:27:53 +0000161 bool valid() { return mSize == 0 || mData != nullptr; }
162 size_t size() { return mSize; }
163 uint8_t* data() { return mData.get(); }
164 uint8_t* release() { return mData.release(); }
165
166 private:
167 std::unique_ptr<uint8_t[]> mData;
168 size_t mSize;
169 };
170
Steven Moreland5ae62562021-06-10 03:21:42 +0000171 [[nodiscard]] status_t rpcSend(const sp<RpcSession::RpcConnection>& connection,
172 const sp<RpcSession>& session, const char* what,
173 const void* data, size_t size);
174 [[nodiscard]] status_t rpcRec(const sp<RpcSession::RpcConnection>& connection,
175 const sp<RpcSession>& session, const char* what, void* data,
176 size_t size);
Steven Moreland5553ac42020-11-11 02:14:45 +0000177
Steven Moreland5ae62562021-06-10 03:21:42 +0000178 [[nodiscard]] status_t waitForReply(const sp<RpcSession::RpcConnection>& connection,
179 const sp<RpcSession>& session, Parcel* reply);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000180 [[nodiscard]] status_t processCommand(const sp<RpcSession::RpcConnection>& connection,
181 const sp<RpcSession>& session,
182 const RpcWireHeader& command, CommandType type);
Steven Moreland5ae62562021-06-10 03:21:42 +0000183 [[nodiscard]] status_t processTransact(const sp<RpcSession::RpcConnection>& connection,
184 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000185 const RpcWireHeader& command);
Steven Moreland5ae62562021-06-10 03:21:42 +0000186 [[nodiscard]] status_t processTransactInternal(const sp<RpcSession::RpcConnection>& connection,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000187 const sp<RpcSession>& session,
Steven Morelandada72bd2021-06-09 23:29:13 +0000188 CommandData transactionData);
Steven Moreland5ae62562021-06-10 03:21:42 +0000189 [[nodiscard]] status_t processDecStrong(const sp<RpcSession::RpcConnection>& connection,
Steven Morelandee3f4662021-05-22 01:07:33 +0000190 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000191 const RpcWireHeader& command);
192
193 struct BinderNode {
194 // Two cases:
195 // A - local binder we are serving
196 // B - remote binder, we are sending transactions to
197 wp<IBinder> binder;
198
199 // if timesSent > 0, this will be equal to binder.promote()
200 sp<IBinder> sentRef;
201
202 // Number of times we've sent this binder out of process, which
203 // translates to an implicit strong count. A client must send RPC binder
204 // socket's dec ref for each time it is sent out of process in order to
205 // deallocate it. Note, a proxy binder we are holding onto might be
206 // sent (this is important when the only remaining refcount of this
207 // binder is the one associated with a transaction sending it back to
208 // its server)
209 size_t timesSent = 0;
210
211 // Number of times we've received this binder, each time corresponds to
212 // a reference we hold over the wire (not a local incStrong/decStrong)
213 size_t timesRecd = 0;
214
215 // transaction ID, for async transactions
216 uint64_t asyncNumber = 0;
217
218 //
219 // CASE A - local binder we are serving
220 //
221
222 // async transaction queue, _only_ for local binder
223 struct AsyncTodo {
Steven Morelandf5174272021-05-25 00:39:28 +0000224 sp<IBinder> ref;
Steven Morelanddbe71832021-05-12 23:31:00 +0000225 CommandData data;
Steven Moreland5553ac42020-11-11 02:14:45 +0000226 uint64_t asyncNumber = 0;
227
228 bool operator<(const AsyncTodo& o) const {
229 return asyncNumber > /* !!! */ o.asyncNumber;
230 }
231 };
232 std::priority_queue<AsyncTodo> asyncTodo;
233
234 //
235 // CASE B - remote binder, we are sending transactions to
236 //
237
238 // (no additional data specific to remote binders)
239 };
240
Steven Moreland31bde7a2021-06-04 00:57:36 +0000241 // checks if there is any reference left to a node and erases it. If erase
242 // happens, and there is a strong reference to the binder kept by
243 // binderNode, this returns that strong reference, so that it can be
244 // dropped after any locks are removed.
Steven Moreland5623d1a2021-09-10 15:45:34 -0700245 sp<IBinder> tryEraseNode(std::map<uint64_t, BinderNode>::iterator& it);
Steven Moreland583a14a2021-06-04 02:04:58 +0000246 // true - success
Steven Morelandc9d7b532021-06-04 20:57:41 +0000247 // false - session shutdown, halt
248 [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node);
Steven Moreland31bde7a2021-06-04 00:57:36 +0000249
Steven Moreland5553ac42020-11-11 02:14:45 +0000250 std::mutex mNodeMutex;
251 bool mTerminated = false;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700252 uint32_t mNextId = 0;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000253 // binders known by both sides of a session
Steven Moreland5623d1a2021-09-10 15:45:34 -0700254 std::map<uint64_t, BinderNode> mNodeForAddress;
Steven Moreland5553ac42020-11-11 02:14:45 +0000255};
256
257} // namespace android