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