blob: db142a11f846eb515e612280b5ebd8ddb66011d1 [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
45/**
46 * Abstracts away management of ref counts and the wire format from
Steven Morelandbdb53ab2021-05-05 17:57:41 +000047 * RpcSession
Steven Moreland5553ac42020-11-11 02:14:45 +000048 */
49class RpcState {
50public:
51 RpcState();
52 ~RpcState();
53
Steven Moreland7c5e6c22021-05-01 02:55:20 +000054 // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
Steven Morelandbdb53ab2021-05-05 17:57:41 +000055 sp<IBinder> getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session);
56 status_t getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Morelandf137de92021-04-24 01:54:26 +000057 size_t* maxThreadsOut);
Steven Morelandbdb53ab2021-05-05 17:57:41 +000058 status_t getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session,
59 int32_t* sessionIdOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000060
Steven Morelandf5174272021-05-25 00:39:28 +000061 [[nodiscard]] status_t transact(const base::unique_fd& fd, const sp<IBinder>& address,
Steven Moreland5553ac42020-11-11 02:14:45 +000062 uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000063 const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
Steven Morelandf5174272021-05-25 00:39:28 +000064 [[nodiscard]] status_t transactAddress(const base::unique_fd& fd, const RpcAddress& address,
65 uint32_t code, const Parcel& data,
66 const sp<RpcSession>& session, Parcel* reply,
67 uint32_t flags);
Steven Morelandc9d7b532021-06-04 20:57:41 +000068 [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session,
69 const RpcAddress& address);
Steven Moreland52eee942021-06-03 00:59:28 +000070
71 enum class CommandType {
72 ANY,
73 CONTROL_ONLY,
74 };
Steven Moreland5553ac42020-11-11 02:14:45 +000075 [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd,
Steven Moreland52eee942021-06-03 00:59:28 +000076 const sp<RpcSession>& session, CommandType type);
77 [[nodiscard]] status_t drainCommands(const base::unique_fd& fd, const sp<RpcSession>& session,
78 CommandType type);
Steven Moreland5553ac42020-11-11 02:14:45 +000079
80 /**
81 * Called by Parcel for outgoing binders. This implies one refcount of
82 * ownership to the outgoing binder.
83 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000084 [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
85 RpcAddress* outAddress);
Steven Moreland5553ac42020-11-11 02:14:45 +000086
87 /**
88 * Called by Parcel for incoming binders. This either returns the refcount
89 * to the process, if this process already has one, or it takes ownership of
90 * that refcount
91 */
Steven Moreland7227c8a2021-06-02 00:24:32 +000092 [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session,
93 const RpcAddress& address, sp<IBinder>* out);
Steven Moreland5553ac42020-11-11 02:14:45 +000094
95 size_t countBinders();
96 void dump();
97
Steven Moreland5553ac42020-11-11 02:14:45 +000098 /**
Steven Morelandbdb53ab2021-05-05 17:57:41 +000099 * Called when reading or writing data to a session fails to clean up
100 * data associated with the session in order to cleanup binders.
Steven Moreland5553ac42020-11-11 02:14:45 +0000101 * Specifically, we have a strong dependency cycle, since BpBinder is
102 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
103 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000104 * BpBinder -> RpcSession -> RpcState
Steven Moreland5553ac42020-11-11 02:14:45 +0000105 * ^-----------------------------/
106 *
107 * In the success case, eventually all refcounts should be propagated over
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000108 * the session, though this could also be called to eagerly cleanup
109 * the session.
Steven Moreland5553ac42020-11-11 02:14:45 +0000110 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000111 * WARNING: RpcState is responsible for calling this when the session is
Steven Moreland5553ac42020-11-11 02:14:45 +0000112 * no longer recoverable.
113 */
Steven Morelandc9d7b532021-06-04 20:57:41 +0000114 void clear();
Steven Moreland5553ac42020-11-11 02:14:45 +0000115
Steven Moreland659416d2021-05-11 00:47:50 +0000116private:
Steven Moreland583a14a2021-06-04 02:04:58 +0000117 void dumpLocked();
Steven Moreland583a14a2021-06-04 02:04:58 +0000118
Steven Morelanddbe71832021-05-12 23:31:00 +0000119 // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps
120 // large allocations to avoid being requested from allocating too much data.
121 struct CommandData {
122 explicit CommandData(size_t size);
Steven Morelande8393342021-05-05 23:27:53 +0000123 bool valid() { return mSize == 0 || mData != nullptr; }
124 size_t size() { return mSize; }
125 uint8_t* data() { return mData.get(); }
126 uint8_t* release() { return mData.release(); }
127
128 private:
129 std::unique_ptr<uint8_t[]> mData;
130 size_t mSize;
131 };
132
Steven Morelandc9d7b532021-06-04 20:57:41 +0000133 [[nodiscard]] status_t rpcSend(const base::unique_fd& fd, const sp<RpcSession>& session,
134 const char* what, const void* data, size_t size);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000135 [[nodiscard]] status_t rpcRec(const base::unique_fd& fd, const sp<RpcSession>& session,
136 const char* what, void* data, size_t size);
Steven Moreland5553ac42020-11-11 02:14:45 +0000137
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000138 [[nodiscard]] status_t waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session,
139 Parcel* reply);
Steven Moreland5553ac42020-11-11 02:14:45 +0000140 [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000141 const sp<RpcSession>& session,
Steven Moreland52eee942021-06-03 00:59:28 +0000142 const RpcWireHeader& command, CommandType type);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000143 [[nodiscard]] status_t processTransact(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000144 const RpcWireHeader& command);
145 [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000146 const sp<RpcSession>& session,
Steven Morelandada72bd2021-06-09 23:29:13 +0000147 CommandData transactionData);
Steven Moreland5553ac42020-11-11 02:14:45 +0000148 [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd,
Steven Morelandee3f4662021-05-22 01:07:33 +0000149 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000150 const RpcWireHeader& command);
151
152 struct BinderNode {
153 // Two cases:
154 // A - local binder we are serving
155 // B - remote binder, we are sending transactions to
156 wp<IBinder> binder;
157
158 // if timesSent > 0, this will be equal to binder.promote()
159 sp<IBinder> sentRef;
160
161 // Number of times we've sent this binder out of process, which
162 // translates to an implicit strong count. A client must send RPC binder
163 // socket's dec ref for each time it is sent out of process in order to
164 // deallocate it. Note, a proxy binder we are holding onto might be
165 // sent (this is important when the only remaining refcount of this
166 // binder is the one associated with a transaction sending it back to
167 // its server)
168 size_t timesSent = 0;
169
170 // Number of times we've received this binder, each time corresponds to
171 // a reference we hold over the wire (not a local incStrong/decStrong)
172 size_t timesRecd = 0;
173
174 // transaction ID, for async transactions
175 uint64_t asyncNumber = 0;
176
177 //
178 // CASE A - local binder we are serving
179 //
180
181 // async transaction queue, _only_ for local binder
182 struct AsyncTodo {
Steven Morelandf5174272021-05-25 00:39:28 +0000183 sp<IBinder> ref;
Steven Morelanddbe71832021-05-12 23:31:00 +0000184 CommandData data;
Steven Moreland5553ac42020-11-11 02:14:45 +0000185 uint64_t asyncNumber = 0;
186
187 bool operator<(const AsyncTodo& o) const {
188 return asyncNumber > /* !!! */ o.asyncNumber;
189 }
190 };
191 std::priority_queue<AsyncTodo> asyncTodo;
192
193 //
194 // CASE B - remote binder, we are sending transactions to
195 //
196
197 // (no additional data specific to remote binders)
198 };
199
Steven Moreland31bde7a2021-06-04 00:57:36 +0000200 // checks if there is any reference left to a node and erases it. If erase
201 // happens, and there is a strong reference to the binder kept by
202 // binderNode, this returns that strong reference, so that it can be
203 // dropped after any locks are removed.
204 sp<IBinder> tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it);
Steven Moreland583a14a2021-06-04 02:04:58 +0000205 // true - success
Steven Morelandc9d7b532021-06-04 20:57:41 +0000206 // false - session shutdown, halt
207 [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node);
Steven Moreland31bde7a2021-06-04 00:57:36 +0000208
Steven Moreland5553ac42020-11-11 02:14:45 +0000209 std::mutex mNodeMutex;
210 bool mTerminated = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000211 // binders known by both sides of a session
Steven Moreland5553ac42020-11-11 02:14:45 +0000212 std::map<RpcAddress, BinderNode> mNodeForAddress;
213};
214
215} // namespace android