blob: 5ac0b973f1405d5737260d83df25f451f5d44cfd [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 Moreland5ae62562021-06-10 03:21:42 +000063 status_t sendConnectionInit(const sp<RpcSession::RpcConnection>& connection,
64 const sp<RpcSession>& session);
65 status_t readConnectionInit(const sp<RpcSession::RpcConnection>& connection,
66 const sp<RpcSession>& session);
Steven Morelandc88b7fc2021-06-10 00:40:39 +000067
Steven Moreland7c5e6c22021-05-01 02:55:20 +000068 // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
Steven Moreland5ae62562021-06-10 03:21:42 +000069 sp<IBinder> getRootObject(const sp<RpcSession::RpcConnection>& connection,
70 const sp<RpcSession>& session);
71 status_t getMaxThreads(const sp<RpcSession::RpcConnection>& connection,
72 const sp<RpcSession>& session, size_t* maxThreadsOut);
73 status_t getSessionId(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland01a6bad2021-06-11 00:59:20 +000074 const sp<RpcSession>& session, RpcAddress* sessionIdOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000075
Steven Moreland5ae62562021-06-10 03:21:42 +000076 [[nodiscard]] status_t transact(const sp<RpcSession::RpcConnection>& connection,
77 const sp<IBinder>& address, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000078 const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
Steven Moreland5ae62562021-06-10 03:21:42 +000079 [[nodiscard]] status_t transactAddress(const sp<RpcSession::RpcConnection>& connection,
80 const RpcAddress& address, uint32_t code,
81 const Parcel& data, const sp<RpcSession>& session,
82 Parcel* reply, uint32_t flags);
83 [[nodiscard]] status_t sendDecStrong(const sp<RpcSession::RpcConnection>& connection,
84 const sp<RpcSession>& session, const RpcAddress& address);
Steven Moreland52eee942021-06-03 00:59:28 +000085
86 enum class CommandType {
87 ANY,
88 CONTROL_ONLY,
89 };
Steven Moreland5ae62562021-06-10 03:21:42 +000090 [[nodiscard]] status_t getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland52eee942021-06-03 00:59:28 +000091 const sp<RpcSession>& session, CommandType type);
Steven Moreland5ae62562021-06-10 03:21:42 +000092 [[nodiscard]] status_t drainCommands(const sp<RpcSession::RpcConnection>& connection,
93 const sp<RpcSession>& session, CommandType type);
Steven Moreland5553ac42020-11-11 02:14:45 +000094
95 /**
96 * Called by Parcel for outgoing binders. This implies one refcount of
97 * ownership to the outgoing binder.
98 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000099 [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
100 RpcAddress* outAddress);
Steven Moreland5553ac42020-11-11 02:14:45 +0000101
102 /**
103 * Called by Parcel for incoming binders. This either returns the refcount
104 * to the process, if this process already has one, or it takes ownership of
105 * that refcount
106 */
Steven Moreland7227c8a2021-06-02 00:24:32 +0000107 [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session,
108 const RpcAddress& address, sp<IBinder>* out);
Steven Moreland5553ac42020-11-11 02:14:45 +0000109
110 size_t countBinders();
111 void dump();
112
Steven Moreland5553ac42020-11-11 02:14:45 +0000113 /**
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000114 * Called when reading or writing data to a session fails to clean up
115 * data associated with the session in order to cleanup binders.
Steven Moreland5553ac42020-11-11 02:14:45 +0000116 * Specifically, we have a strong dependency cycle, since BpBinder is
117 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
118 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000119 * BpBinder -> RpcSession -> RpcState
Steven Moreland5553ac42020-11-11 02:14:45 +0000120 * ^-----------------------------/
121 *
122 * In the success case, eventually all refcounts should be propagated over
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000123 * the session, though this could also be called to eagerly cleanup
124 * the session.
Steven Moreland5553ac42020-11-11 02:14:45 +0000125 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000126 * WARNING: RpcState is responsible for calling this when the session is
Steven Moreland5553ac42020-11-11 02:14:45 +0000127 * no longer recoverable.
128 */
Steven Morelandc9d7b532021-06-04 20:57:41 +0000129 void clear();
Steven Moreland5553ac42020-11-11 02:14:45 +0000130
Steven Moreland659416d2021-05-11 00:47:50 +0000131private:
Steven Moreland583a14a2021-06-04 02:04:58 +0000132 void dumpLocked();
Steven Moreland583a14a2021-06-04 02:04:58 +0000133
Steven Morelanddbe71832021-05-12 23:31:00 +0000134 // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps
135 // large allocations to avoid being requested from allocating too much data.
136 struct CommandData {
137 explicit CommandData(size_t size);
Steven Morelande8393342021-05-05 23:27:53 +0000138 bool valid() { return mSize == 0 || mData != nullptr; }
139 size_t size() { return mSize; }
140 uint8_t* data() { return mData.get(); }
141 uint8_t* release() { return mData.release(); }
142
143 private:
144 std::unique_ptr<uint8_t[]> mData;
145 size_t mSize;
146 };
147
Steven Moreland5ae62562021-06-10 03:21:42 +0000148 [[nodiscard]] status_t rpcSend(const sp<RpcSession::RpcConnection>& connection,
149 const sp<RpcSession>& session, const char* what,
150 const void* data, size_t size);
151 [[nodiscard]] status_t rpcRec(const sp<RpcSession::RpcConnection>& connection,
152 const sp<RpcSession>& session, const char* what, void* data,
153 size_t size);
Steven Moreland5553ac42020-11-11 02:14:45 +0000154
Steven Moreland5ae62562021-06-10 03:21:42 +0000155 [[nodiscard]] status_t waitForReply(const sp<RpcSession::RpcConnection>& connection,
156 const sp<RpcSession>& session, Parcel* reply);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000157 [[nodiscard]] status_t processCommand(const sp<RpcSession::RpcConnection>& connection,
158 const sp<RpcSession>& session,
159 const RpcWireHeader& command, CommandType type);
Steven Moreland5ae62562021-06-10 03:21:42 +0000160 [[nodiscard]] status_t processTransact(const sp<RpcSession::RpcConnection>& connection,
161 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000162 const RpcWireHeader& command);
Steven Moreland5ae62562021-06-10 03:21:42 +0000163 [[nodiscard]] status_t processTransactInternal(const sp<RpcSession::RpcConnection>& connection,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000164 const sp<RpcSession>& session,
Steven Morelandada72bd2021-06-09 23:29:13 +0000165 CommandData transactionData);
Steven Moreland5ae62562021-06-10 03:21:42 +0000166 [[nodiscard]] status_t processDecStrong(const sp<RpcSession::RpcConnection>& connection,
Steven Morelandee3f4662021-05-22 01:07:33 +0000167 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000168 const RpcWireHeader& command);
169
170 struct BinderNode {
171 // Two cases:
172 // A - local binder we are serving
173 // B - remote binder, we are sending transactions to
174 wp<IBinder> binder;
175
176 // if timesSent > 0, this will be equal to binder.promote()
177 sp<IBinder> sentRef;
178
179 // Number of times we've sent this binder out of process, which
180 // translates to an implicit strong count. A client must send RPC binder
181 // socket's dec ref for each time it is sent out of process in order to
182 // deallocate it. Note, a proxy binder we are holding onto might be
183 // sent (this is important when the only remaining refcount of this
184 // binder is the one associated with a transaction sending it back to
185 // its server)
186 size_t timesSent = 0;
187
188 // Number of times we've received this binder, each time corresponds to
189 // a reference we hold over the wire (not a local incStrong/decStrong)
190 size_t timesRecd = 0;
191
192 // transaction ID, for async transactions
193 uint64_t asyncNumber = 0;
194
195 //
196 // CASE A - local binder we are serving
197 //
198
199 // async transaction queue, _only_ for local binder
200 struct AsyncTodo {
Steven Morelandf5174272021-05-25 00:39:28 +0000201 sp<IBinder> ref;
Steven Morelanddbe71832021-05-12 23:31:00 +0000202 CommandData data;
Steven Moreland5553ac42020-11-11 02:14:45 +0000203 uint64_t asyncNumber = 0;
204
205 bool operator<(const AsyncTodo& o) const {
206 return asyncNumber > /* !!! */ o.asyncNumber;
207 }
208 };
209 std::priority_queue<AsyncTodo> asyncTodo;
210
211 //
212 // CASE B - remote binder, we are sending transactions to
213 //
214
215 // (no additional data specific to remote binders)
216 };
217
Steven Moreland31bde7a2021-06-04 00:57:36 +0000218 // checks if there is any reference left to a node and erases it. If erase
219 // happens, and there is a strong reference to the binder kept by
220 // binderNode, this returns that strong reference, so that it can be
221 // dropped after any locks are removed.
222 sp<IBinder> tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it);
Steven Moreland583a14a2021-06-04 02:04:58 +0000223 // true - success
Steven Morelandc9d7b532021-06-04 20:57:41 +0000224 // false - session shutdown, halt
225 [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node);
Steven Moreland31bde7a2021-06-04 00:57:36 +0000226
Steven Moreland5553ac42020-11-11 02:14:45 +0000227 std::mutex mNodeMutex;
228 bool mTerminated = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000229 // binders known by both sides of a session
Steven Moreland5553ac42020-11-11 02:14:45 +0000230 std::map<RpcAddress, BinderNode> mNodeForAddress;
231};
232
233} // namespace android