blob: 1cfa4067917668aef0352107f298397ac27da349 [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
53 sp<IBinder> getRootObject(const base::unique_fd& fd, const sp<RpcConnection>& connection);
Steven Morelandf137de92021-04-24 01:54:26 +000054 status_t getMaxThreads(const base::unique_fd& fd, const sp<RpcConnection>& connection,
55 size_t* maxThreadsOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000056
57 [[nodiscard]] status_t transact(const base::unique_fd& fd, const RpcAddress& address,
58 uint32_t code, const Parcel& data,
59 const sp<RpcConnection>& connection, Parcel* reply,
60 uint32_t flags);
61 [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const RpcAddress& address);
62 [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd,
63 const sp<RpcConnection>& connection);
64
65 /**
66 * Called by Parcel for outgoing binders. This implies one refcount of
67 * ownership to the outgoing binder.
68 */
69 [[nodiscard]] status_t onBinderLeaving(const sp<RpcConnection>& connection,
70 const sp<IBinder>& binder, RpcAddress* outAddress);
71
72 /**
73 * Called by Parcel for incoming binders. This either returns the refcount
74 * to the process, if this process already has one, or it takes ownership of
75 * that refcount
76 */
77 sp<IBinder> onBinderEntering(const sp<RpcConnection>& connection, const RpcAddress& address);
78
79 size_t countBinders();
80 void dump();
81
82private:
83 /**
84 * Called when reading or writing data to a connection fails to clean up
85 * data associated with the connection in order to cleanup binders.
86 * Specifically, we have a strong dependency cycle, since BpBinder is
87 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
88 *
89 * BpBinder -> RpcConnection -> RpcState
90 * ^-----------------------------/
91 *
92 * In the success case, eventually all refcounts should be propagated over
93 * the connection, though this could also be called to eagerly cleanup
94 * the connection.
95 *
96 * WARNING: RpcState is responsible for calling this when the connection is
97 * no longer recoverable.
98 */
99 void terminate();
100
101 [[nodiscard]] bool rpcSend(const base::unique_fd& fd, const char* what, const void* data,
102 size_t size);
103 [[nodiscard]] bool rpcRec(const base::unique_fd& fd, const char* what, void* data, size_t size);
104
105 [[nodiscard]] status_t waitForReply(const base::unique_fd& fd,
106 const sp<RpcConnection>& connection, Parcel* reply);
107 [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd,
108 const sp<RpcConnection>& connection,
109 const RpcWireHeader& command);
110 [[nodiscard]] status_t processTransact(const base::unique_fd& fd,
111 const sp<RpcConnection>& connection,
112 const RpcWireHeader& command);
113 [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd,
114 const sp<RpcConnection>& connection,
115 std::vector<uint8_t>&& transactionData);
116 [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd,
117 const RpcWireHeader& command);
118
119 struct BinderNode {
120 // Two cases:
121 // A - local binder we are serving
122 // B - remote binder, we are sending transactions to
123 wp<IBinder> binder;
124
125 // if timesSent > 0, this will be equal to binder.promote()
126 sp<IBinder> sentRef;
127
128 // Number of times we've sent this binder out of process, which
129 // translates to an implicit strong count. A client must send RPC binder
130 // socket's dec ref for each time it is sent out of process in order to
131 // deallocate it. Note, a proxy binder we are holding onto might be
132 // sent (this is important when the only remaining refcount of this
133 // binder is the one associated with a transaction sending it back to
134 // its server)
135 size_t timesSent = 0;
136
137 // Number of times we've received this binder, each time corresponds to
138 // a reference we hold over the wire (not a local incStrong/decStrong)
139 size_t timesRecd = 0;
140
141 // transaction ID, for async transactions
142 uint64_t asyncNumber = 0;
143
144 //
145 // CASE A - local binder we are serving
146 //
147
148 // async transaction queue, _only_ for local binder
149 struct AsyncTodo {
150 std::vector<uint8_t> data; // most convenient format, to move it here
151 uint64_t asyncNumber = 0;
152
153 bool operator<(const AsyncTodo& o) const {
154 return asyncNumber > /* !!! */ o.asyncNumber;
155 }
156 };
157 std::priority_queue<AsyncTodo> asyncTodo;
158
159 //
160 // CASE B - remote binder, we are sending transactions to
161 //
162
163 // (no additional data specific to remote binders)
164 };
165
166 std::mutex mNodeMutex;
167 bool mTerminated = false;
168 // binders known by both sides of a connection
169 std::map<RpcAddress, BinderNode> mNodeForAddress;
170};
171
172} // namespace android