Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 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 Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 21 | #include <binder/RpcSession.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 22 | |
| 23 | #include <map> |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 24 | #include <optional> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <queue> |
| 26 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 27 | #include <sys/uio.h> |
| 28 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | struct RpcWireHeader; |
| 32 | |
| 33 | /** |
| 34 | * Log a lot more information about RPC calls, when debugging issues. Usually, |
| 35 | * you would want to enable this in only one process. If repeated issues require |
| 36 | * a specific subset of logs to debug, this could be broken up like |
| 37 | * IPCThreadState's. |
| 38 | */ |
| 39 | #define SHOULD_LOG_RPC_DETAIL false |
| 40 | |
| 41 | #if SHOULD_LOG_RPC_DETAIL |
| 42 | #define LOG_RPC_DETAIL(...) ALOGI(__VA_ARGS__) |
| 43 | #else |
| 44 | #define LOG_RPC_DETAIL(...) ALOGV(__VA_ARGS__) // for type checking |
| 45 | #endif |
| 46 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 47 | #define RPC_FLAKE_PRONE false |
| 48 | |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 49 | #if RPC_FLAKE_PRONE |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 50 | void rpcMaybeWaitToFlake(); |
| 51 | #define MAYBE_WAIT_IN_FLAKE_MODE rpcMaybeWaitToFlake() |
| 52 | #else |
| 53 | #define MAYBE_WAIT_IN_FLAKE_MODE do {} while (false) |
| 54 | #endif |
| 55 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 56 | /** |
| 57 | * Abstracts away management of ref counts and the wire format from |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 58 | * RpcSession |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 59 | */ |
| 60 | class RpcState { |
| 61 | public: |
| 62 | RpcState(); |
| 63 | ~RpcState(); |
| 64 | |
Steven Moreland | fc027e0 | 2021-10-25 15:31:31 -0700 | [diff] [blame] | 65 | [[nodiscard]] status_t readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection, |
| 66 | const sp<RpcSession>& session, uint32_t* version); |
| 67 | [[nodiscard]] status_t sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 68 | const sp<RpcSession>& session); |
| 69 | [[nodiscard]] status_t readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 70 | const sp<RpcSession>& session); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 71 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 72 | // TODO(b/182940634): combine some special transactions into one "getServerInfo" call? |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 73 | sp<IBinder> getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 74 | const sp<RpcSession>& session); |
Steven Moreland | fc027e0 | 2021-10-25 15:31:31 -0700 | [diff] [blame] | 75 | [[nodiscard]] status_t getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 76 | const sp<RpcSession>& session, size_t* maxThreadsOut); |
| 77 | [[nodiscard]] status_t getSessionId(const sp<RpcSession::RpcConnection>& connection, |
| 78 | const sp<RpcSession>& session, |
| 79 | std::vector<uint8_t>* sessionIdOut); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 80 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 81 | [[nodiscard]] status_t transact(const sp<RpcSession::RpcConnection>& connection, |
| 82 | const sp<IBinder>& address, uint32_t code, const Parcel& data, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 83 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 84 | [[nodiscard]] status_t transactAddress(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 85 | uint64_t address, uint32_t code, const Parcel& data, |
| 86 | const sp<RpcSession>& session, Parcel* reply, |
| 87 | uint32_t flags); |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * The ownership model here carries an implicit strong refcount whenever a |
| 91 | * binder is sent across processes. Since we have a local strong count in |
| 92 | * sp<> over these objects, we only ever need to keep one of these. So, |
| 93 | * typically we tell the remote process that we drop all the implicit dec |
| 94 | * strongs, and we hold onto the last one. 'target' here is the target |
| 95 | * timesRecd (the number of remaining reference counts) we wish to keep. |
| 96 | * Typically this should be '0' or '1'. The target is used instead of an |
| 97 | * explicit decrement count in order to allow multiple threads to lower the |
| 98 | * number of counts simultaneously. Since we only lower the count to 0 when |
| 99 | * a binder is deleted, targets of '1' should only be sent when the caller |
| 100 | * owns a local strong reference to the binder. Larger targets may be used |
| 101 | * for testing, and to make the function generic, but generally this should |
| 102 | * be avoided because it would be hard to guarantee another thread doesn't |
| 103 | * lower the number of held refcounts to '1'. Note also, these refcounts |
| 104 | * must be sent actively. If they are sent when binders are deleted, this |
| 105 | * can cause leaks, since even remote binders carry an implicit strong ref |
| 106 | * when they are sent to another process. |
| 107 | */ |
| 108 | [[nodiscard]] status_t sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection, |
| 109 | const sp<RpcSession>& session, uint64_t address, |
| 110 | size_t target); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 111 | |
| 112 | enum class CommandType { |
| 113 | ANY, |
| 114 | CONTROL_ONLY, |
| 115 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 116 | [[nodiscard]] status_t getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 117 | const sp<RpcSession>& session, CommandType type); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 118 | [[nodiscard]] status_t drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 119 | const sp<RpcSession>& session, CommandType type); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Called by Parcel for outgoing binders. This implies one refcount of |
| 123 | * ownership to the outgoing binder. |
| 124 | */ |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 125 | [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 126 | uint64_t* outAddress); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Called by Parcel for incoming binders. This either returns the refcount |
| 130 | * to the process, if this process already has one, or it takes ownership of |
| 131 | * that refcount |
| 132 | */ |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 133 | [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session, uint64_t address, |
| 134 | sp<IBinder>* out); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Called on incoming binders to update refcounting information. This should |
| 137 | * only be called when it is done as part of making progress on a |
| 138 | * transaction. |
| 139 | */ |
| 140 | [[nodiscard]] status_t flushExcessBinderRefs(const sp<RpcSession>& session, uint64_t address, |
| 141 | const sp<IBinder>& binder); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 142 | |
| 143 | size_t countBinders(); |
| 144 | void dump(); |
| 145 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 146 | /** |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 147 | * Called when reading or writing data to a session fails to clean up |
| 148 | * data associated with the session in order to cleanup binders. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 149 | * Specifically, we have a strong dependency cycle, since BpBinder is |
| 150 | * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true). |
| 151 | * |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 152 | * BpBinder -> RpcSession -> RpcState |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 153 | * ^-----------------------------/ |
| 154 | * |
| 155 | * In the success case, eventually all refcounts should be propagated over |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 156 | * the session, though this could also be called to eagerly cleanup |
| 157 | * the session. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 158 | * |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 159 | * WARNING: RpcState is responsible for calling this when the session is |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 160 | * no longer recoverable. |
| 161 | */ |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 162 | void clear(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 163 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 164 | private: |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 165 | void dumpLocked(); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 166 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 167 | // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps |
| 168 | // large allocations to avoid being requested from allocating too much data. |
| 169 | struct CommandData { |
| 170 | explicit CommandData(size_t size); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 171 | bool valid() { return mSize == 0 || mData != nullptr; } |
| 172 | size_t size() { return mSize; } |
| 173 | uint8_t* data() { return mData.get(); } |
| 174 | uint8_t* release() { return mData.release(); } |
| 175 | |
| 176 | private: |
| 177 | std::unique_ptr<uint8_t[]> mData; |
| 178 | size_t mSize; |
| 179 | }; |
| 180 | |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 181 | [[nodiscard]] status_t rpcSend( |
| 182 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 183 | const char* what, iovec* iovs, int niovs, |
| 184 | const std::optional<android::base::function_ref<status_t()>>& altPoll); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 185 | [[nodiscard]] status_t rpcRec(const sp<RpcSession::RpcConnection>& connection, |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 186 | const sp<RpcSession>& session, const char* what, iovec* iovs, |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 187 | int niovs); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 188 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 189 | [[nodiscard]] status_t waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 190 | const sp<RpcSession>& session, Parcel* reply); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 191 | [[nodiscard]] status_t processCommand(const sp<RpcSession::RpcConnection>& connection, |
| 192 | const sp<RpcSession>& session, |
| 193 | const RpcWireHeader& command, CommandType type); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 194 | [[nodiscard]] status_t processTransact(const sp<RpcSession::RpcConnection>& connection, |
| 195 | const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 196 | const RpcWireHeader& command); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 197 | [[nodiscard]] status_t processTransactInternal(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 198 | const sp<RpcSession>& session, |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 199 | CommandData transactionData); |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 200 | [[nodiscard]] status_t processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 201 | const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 202 | const RpcWireHeader& command); |
| 203 | |
| 204 | struct BinderNode { |
| 205 | // Two cases: |
| 206 | // A - local binder we are serving |
| 207 | // B - remote binder, we are sending transactions to |
| 208 | wp<IBinder> binder; |
| 209 | |
| 210 | // if timesSent > 0, this will be equal to binder.promote() |
| 211 | sp<IBinder> sentRef; |
| 212 | |
| 213 | // Number of times we've sent this binder out of process, which |
| 214 | // translates to an implicit strong count. A client must send RPC binder |
| 215 | // socket's dec ref for each time it is sent out of process in order to |
| 216 | // deallocate it. Note, a proxy binder we are holding onto might be |
| 217 | // sent (this is important when the only remaining refcount of this |
| 218 | // binder is the one associated with a transaction sending it back to |
| 219 | // its server) |
| 220 | size_t timesSent = 0; |
| 221 | |
| 222 | // Number of times we've received this binder, each time corresponds to |
| 223 | // a reference we hold over the wire (not a local incStrong/decStrong) |
| 224 | size_t timesRecd = 0; |
| 225 | |
| 226 | // transaction ID, for async transactions |
| 227 | uint64_t asyncNumber = 0; |
| 228 | |
| 229 | // |
| 230 | // CASE A - local binder we are serving |
| 231 | // |
| 232 | |
| 233 | // async transaction queue, _only_ for local binder |
| 234 | struct AsyncTodo { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 235 | sp<IBinder> ref; |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 236 | CommandData data; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 237 | uint64_t asyncNumber = 0; |
| 238 | |
| 239 | bool operator<(const AsyncTodo& o) const { |
| 240 | return asyncNumber > /* !!! */ o.asyncNumber; |
| 241 | } |
| 242 | }; |
| 243 | std::priority_queue<AsyncTodo> asyncTodo; |
| 244 | |
| 245 | // |
| 246 | // CASE B - remote binder, we are sending transactions to |
| 247 | // |
| 248 | |
| 249 | // (no additional data specific to remote binders) |
| 250 | }; |
| 251 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 252 | // checks if there is any reference left to a node and erases it. If erase |
| 253 | // happens, and there is a strong reference to the binder kept by |
| 254 | // binderNode, this returns that strong reference, so that it can be |
| 255 | // dropped after any locks are removed. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 256 | sp<IBinder> tryEraseNode(std::map<uint64_t, BinderNode>::iterator& it); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 257 | // true - success |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 258 | // false - session shutdown, halt |
| 259 | [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node); |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 260 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 261 | std::mutex mNodeMutex; |
| 262 | bool mTerminated = false; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 263 | uint32_t mNextId = 0; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 264 | // binders known by both sides of a session |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 265 | std::map<uint64_t, BinderNode> mNodeForAddress; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | } // namespace android |