blob: 825fd7c4e5e6dfaa18d0f3034c6188c1a9a768cf [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>
21#include <binder/RpcConnection.h>
22
23#include <map>
24#include <queue>
25
26namespace android {
27
28struct RpcWireHeader;
29
30/**
31 * Log a lot more information about RPC calls, when debugging issues. Usually,
32 * you would want to enable this in only one process. If repeated issues require
33 * a specific subset of logs to debug, this could be broken up like
34 * IPCThreadState's.
35 */
36#define SHOULD_LOG_RPC_DETAIL false
37
38#if SHOULD_LOG_RPC_DETAIL
39#define LOG_RPC_DETAIL(...) ALOGI(__VA_ARGS__)
40#else
41#define LOG_RPC_DETAIL(...) ALOGV(__VA_ARGS__) // for type checking
42#endif
43
44/**
45 * Abstracts away management of ref counts and the wire format from
46 * RpcConnection
47 */
48class RpcState {
49public:
50 RpcState();
51 ~RpcState();
52
Steven Moreland7c5e6c22021-05-01 02:55:20 +000053 // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
Steven Moreland5553ac42020-11-11 02:14:45 +000054 sp<IBinder> getRootObject(const base::unique_fd& fd, const sp<RpcConnection>& connection);
Steven Morelandf137de92021-04-24 01:54:26 +000055 status_t getMaxThreads(const base::unique_fd& fd, const sp<RpcConnection>& connection,
56 size_t* maxThreadsOut);
Steven Moreland7c5e6c22021-05-01 02:55:20 +000057 status_t getConnectionId(const base::unique_fd& fd, const sp<RpcConnection>& connection,
58 int32_t* connectionIdOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000059
60 [[nodiscard]] status_t transact(const base::unique_fd& fd, const RpcAddress& address,
61 uint32_t code, const Parcel& data,
62 const sp<RpcConnection>& connection, Parcel* reply,
63 uint32_t flags);
64 [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const RpcAddress& address);
65 [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd,
66 const sp<RpcConnection>& connection);
67
68 /**
69 * Called by Parcel for outgoing binders. This implies one refcount of
70 * ownership to the outgoing binder.
71 */
72 [[nodiscard]] status_t onBinderLeaving(const sp<RpcConnection>& connection,
73 const sp<IBinder>& binder, RpcAddress* outAddress);
74
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 */
80 sp<IBinder> onBinderEntering(const sp<RpcConnection>& connection, const RpcAddress& address);
81
82 size_t countBinders();
83 void dump();
84
85private:
86 /**
87 * Called when reading or writing data to a connection fails to clean up
88 * data associated with the connection in order to cleanup binders.
89 * Specifically, we have a strong dependency cycle, since BpBinder is
90 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
91 *
92 * BpBinder -> RpcConnection -> RpcState
93 * ^-----------------------------/
94 *
95 * In the success case, eventually all refcounts should be propagated over
96 * the connection, though this could also be called to eagerly cleanup
97 * the connection.
98 *
99 * WARNING: RpcState is responsible for calling this when the connection is
100 * no longer recoverable.
101 */
102 void terminate();
103
104 [[nodiscard]] bool rpcSend(const base::unique_fd& fd, const char* what, const void* data,
105 size_t size);
106 [[nodiscard]] bool rpcRec(const base::unique_fd& fd, const char* what, void* data, size_t size);
107
108 [[nodiscard]] status_t waitForReply(const base::unique_fd& fd,
109 const sp<RpcConnection>& connection, Parcel* reply);
110 [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd,
111 const sp<RpcConnection>& connection,
112 const RpcWireHeader& command);
113 [[nodiscard]] status_t processTransact(const base::unique_fd& fd,
114 const sp<RpcConnection>& connection,
115 const RpcWireHeader& command);
116 [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd,
117 const sp<RpcConnection>& connection,
118 std::vector<uint8_t>&& transactionData);
119 [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd,
120 const RpcWireHeader& command);
121
122 struct BinderNode {
123 // Two cases:
124 // A - local binder we are serving
125 // B - remote binder, we are sending transactions to
126 wp<IBinder> binder;
127
128 // if timesSent > 0, this will be equal to binder.promote()
129 sp<IBinder> sentRef;
130
131 // Number of times we've sent this binder out of process, which
132 // translates to an implicit strong count. A client must send RPC binder
133 // socket's dec ref for each time it is sent out of process in order to
134 // deallocate it. Note, a proxy binder we are holding onto might be
135 // sent (this is important when the only remaining refcount of this
136 // binder is the one associated with a transaction sending it back to
137 // its server)
138 size_t timesSent = 0;
139
140 // Number of times we've received this binder, each time corresponds to
141 // a reference we hold over the wire (not a local incStrong/decStrong)
142 size_t timesRecd = 0;
143
144 // transaction ID, for async transactions
145 uint64_t asyncNumber = 0;
146
147 //
148 // CASE A - local binder we are serving
149 //
150
151 // async transaction queue, _only_ for local binder
152 struct AsyncTodo {
153 std::vector<uint8_t> data; // most convenient format, to move it here
154 uint64_t asyncNumber = 0;
155
156 bool operator<(const AsyncTodo& o) const {
157 return asyncNumber > /* !!! */ o.asyncNumber;
158 }
159 };
160 std::priority_queue<AsyncTodo> asyncTodo;
161
162 //
163 // CASE B - remote binder, we are sending transactions to
164 //
165
166 // (no additional data specific to remote binders)
167 };
168
169 std::mutex mNodeMutex;
170 bool mTerminated = false;
171 // binders known by both sides of a connection
172 std::map<RpcAddress, BinderNode> mNodeForAddress;
173};
174
175} // namespace android