blob: 31f8a22065d7ab81eaa48f51b830cc9d5e1a73fb [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
61 [[nodiscard]] status_t transact(const base::unique_fd& fd, const RpcAddress& address,
62 uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000063 const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
Steven Moreland5553ac42020-11-11 02:14:45 +000064 [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const RpcAddress& address);
65 [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000066 const sp<RpcSession>& session);
Steven Moreland5553ac42020-11-11 02:14:45 +000067
68 /**
69 * Called by Parcel for outgoing binders. This implies one refcount of
70 * ownership to the outgoing binder.
71 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000072 [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
73 RpcAddress* outAddress);
Steven Moreland5553ac42020-11-11 02:14:45 +000074
75 /**
76 * Called by Parcel for incoming binders. This either returns the refcount
77 * to the process, if this process already has one, or it takes ownership of
78 * that refcount
79 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000080 sp<IBinder> onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address);
Steven Moreland5553ac42020-11-11 02:14:45 +000081
82 size_t countBinders();
83 void dump();
84
85private:
86 /**
Steven Morelandbdb53ab2021-05-05 17:57:41 +000087 * Called when reading or writing data to a session fails to clean up
88 * data associated with the session in order to cleanup binders.
Steven Moreland5553ac42020-11-11 02:14:45 +000089 * Specifically, we have a strong dependency cycle, since BpBinder is
90 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
91 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +000092 * BpBinder -> RpcSession -> RpcState
Steven Moreland5553ac42020-11-11 02:14:45 +000093 * ^-----------------------------/
94 *
95 * In the success case, eventually all refcounts should be propagated over
Steven Morelandbdb53ab2021-05-05 17:57:41 +000096 * the session, though this could also be called to eagerly cleanup
97 * the session.
Steven Moreland5553ac42020-11-11 02:14:45 +000098 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +000099 * WARNING: RpcState is responsible for calling this when the session is
Steven Moreland5553ac42020-11-11 02:14:45 +0000100 * no longer recoverable.
101 */
102 void terminate();
103
Steven Morelanddbe71832021-05-12 23:31:00 +0000104 // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps
105 // large allocations to avoid being requested from allocating too much data.
106 struct CommandData {
107 explicit CommandData(size_t size);
Steven Morelande8393342021-05-05 23:27:53 +0000108 bool valid() { return mSize == 0 || mData != nullptr; }
109 size_t size() { return mSize; }
110 uint8_t* data() { return mData.get(); }
111 uint8_t* release() { return mData.release(); }
112
113 private:
114 std::unique_ptr<uint8_t[]> mData;
115 size_t mSize;
116 };
117
Steven Moreland5553ac42020-11-11 02:14:45 +0000118 [[nodiscard]] bool rpcSend(const base::unique_fd& fd, const char* what, const void* data,
119 size_t size);
120 [[nodiscard]] bool rpcRec(const base::unique_fd& fd, const char* what, void* data, size_t size);
121
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000122 [[nodiscard]] status_t waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session,
123 Parcel* reply);
Steven Moreland5553ac42020-11-11 02:14:45 +0000124 [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000125 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000126 const RpcWireHeader& command);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000127 [[nodiscard]] status_t processTransact(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000128 const RpcWireHeader& command);
129 [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000130 const sp<RpcSession>& session,
Steven Morelanddbe71832021-05-12 23:31:00 +0000131 CommandData transactionData);
Steven Moreland5553ac42020-11-11 02:14:45 +0000132 [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd,
133 const RpcWireHeader& command);
134
135 struct BinderNode {
136 // Two cases:
137 // A - local binder we are serving
138 // B - remote binder, we are sending transactions to
139 wp<IBinder> binder;
140
141 // if timesSent > 0, this will be equal to binder.promote()
142 sp<IBinder> sentRef;
143
144 // Number of times we've sent this binder out of process, which
145 // translates to an implicit strong count. A client must send RPC binder
146 // socket's dec ref for each time it is sent out of process in order to
147 // deallocate it. Note, a proxy binder we are holding onto might be
148 // sent (this is important when the only remaining refcount of this
149 // binder is the one associated with a transaction sending it back to
150 // its server)
151 size_t timesSent = 0;
152
153 // Number of times we've received this binder, each time corresponds to
154 // a reference we hold over the wire (not a local incStrong/decStrong)
155 size_t timesRecd = 0;
156
157 // transaction ID, for async transactions
158 uint64_t asyncNumber = 0;
159
160 //
161 // CASE A - local binder we are serving
162 //
163
164 // async transaction queue, _only_ for local binder
165 struct AsyncTodo {
Steven Morelanddbe71832021-05-12 23:31:00 +0000166 CommandData data;
Steven Moreland5553ac42020-11-11 02:14:45 +0000167 uint64_t asyncNumber = 0;
168
169 bool operator<(const AsyncTodo& o) const {
170 return asyncNumber > /* !!! */ o.asyncNumber;
171 }
172 };
173 std::priority_queue<AsyncTodo> asyncTodo;
174
175 //
176 // CASE B - remote binder, we are sending transactions to
177 //
178
179 // (no additional data specific to remote binders)
180 };
181
182 std::mutex mNodeMutex;
183 bool mTerminated = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000184 // binders known by both sides of a session
Steven Moreland5553ac42020-11-11 02:14:45 +0000185 std::map<RpcAddress, BinderNode> mNodeForAddress;
186};
187
188} // namespace android