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 | |
| 27 | namespace android { |
| 28 | |
| 29 | struct 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 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame^] | 45 | #define RPC_FLAKE_PRONE false |
| 46 | |
| 47 | #ifdef RPC_FLAKE_PRONE |
| 48 | void rpcMaybeWaitToFlake(); |
| 49 | #define MAYBE_WAIT_IN_FLAKE_MODE rpcMaybeWaitToFlake() |
| 50 | #else |
| 51 | #define MAYBE_WAIT_IN_FLAKE_MODE do {} while (false) |
| 52 | #endif |
| 53 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Abstracts away management of ref counts and the wire format from |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 56 | * RpcSession |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 57 | */ |
| 58 | class RpcState { |
| 59 | public: |
| 60 | RpcState(); |
| 61 | ~RpcState(); |
| 62 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 63 | status_t sendConnectionInit(const base::unique_fd& fd, const sp<RpcSession>& session); |
| 64 | status_t readConnectionInit(const base::unique_fd& fd, const sp<RpcSession>& session); |
| 65 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 66 | // TODO(b/182940634): combine some special transactions into one "getServerInfo" call? |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 67 | sp<IBinder> getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session); |
| 68 | status_t getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 69 | size_t* maxThreadsOut); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 70 | status_t getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 71 | int32_t* sessionIdOut); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 72 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 73 | [[nodiscard]] status_t transact(const base::unique_fd& fd, const sp<IBinder>& address, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 74 | uint32_t code, const Parcel& data, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 75 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 76 | [[nodiscard]] status_t transactAddress(const base::unique_fd& fd, const RpcAddress& address, |
| 77 | uint32_t code, const Parcel& data, |
| 78 | const sp<RpcSession>& session, Parcel* reply, |
| 79 | uint32_t flags); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 80 | [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 81 | const RpcAddress& address); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 82 | |
| 83 | enum class CommandType { |
| 84 | ANY, |
| 85 | CONTROL_ONLY, |
| 86 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 87 | [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd, |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 88 | const sp<RpcSession>& session, CommandType type); |
| 89 | [[nodiscard]] status_t drainCommands(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 90 | CommandType type); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Called by Parcel for outgoing binders. This implies one refcount of |
| 94 | * ownership to the outgoing binder. |
| 95 | */ |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 96 | [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
| 97 | RpcAddress* outAddress); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Called by Parcel for incoming binders. This either returns the refcount |
| 101 | * to the process, if this process already has one, or it takes ownership of |
| 102 | * that refcount |
| 103 | */ |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 104 | [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session, |
| 105 | const RpcAddress& address, sp<IBinder>* out); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | |
| 107 | size_t countBinders(); |
| 108 | void dump(); |
| 109 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 110 | /** |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 111 | * Called when reading or writing data to a session fails to clean up |
| 112 | * data associated with the session in order to cleanup binders. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 113 | * Specifically, we have a strong dependency cycle, since BpBinder is |
| 114 | * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true). |
| 115 | * |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 116 | * BpBinder -> RpcSession -> RpcState |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 117 | * ^-----------------------------/ |
| 118 | * |
| 119 | * In the success case, eventually all refcounts should be propagated over |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 120 | * the session, though this could also be called to eagerly cleanup |
| 121 | * the session. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 122 | * |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 123 | * WARNING: RpcState is responsible for calling this when the session is |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 124 | * no longer recoverable. |
| 125 | */ |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 126 | void clear(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 127 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 128 | private: |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 129 | void dumpLocked(); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 130 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 131 | // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps |
| 132 | // large allocations to avoid being requested from allocating too much data. |
| 133 | struct CommandData { |
| 134 | explicit CommandData(size_t size); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 135 | bool valid() { return mSize == 0 || mData != nullptr; } |
| 136 | size_t size() { return mSize; } |
| 137 | uint8_t* data() { return mData.get(); } |
| 138 | uint8_t* release() { return mData.release(); } |
| 139 | |
| 140 | private: |
| 141 | std::unique_ptr<uint8_t[]> mData; |
| 142 | size_t mSize; |
| 143 | }; |
| 144 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 145 | [[nodiscard]] status_t rpcSend(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 146 | const char* what, const void* data, size_t size); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 147 | [[nodiscard]] status_t rpcRec(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 148 | const char* what, void* data, size_t size); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 149 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 150 | [[nodiscard]] status_t waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 151 | Parcel* reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 152 | [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 153 | const sp<RpcSession>& session, |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 154 | const RpcWireHeader& command, CommandType type); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 155 | [[nodiscard]] status_t processTransact(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 156 | const RpcWireHeader& command); |
| 157 | [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 158 | const sp<RpcSession>& session, |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 159 | CommandData transactionData); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 160 | [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd, |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 161 | const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 162 | const RpcWireHeader& command); |
| 163 | |
| 164 | struct BinderNode { |
| 165 | // Two cases: |
| 166 | // A - local binder we are serving |
| 167 | // B - remote binder, we are sending transactions to |
| 168 | wp<IBinder> binder; |
| 169 | |
| 170 | // if timesSent > 0, this will be equal to binder.promote() |
| 171 | sp<IBinder> sentRef; |
| 172 | |
| 173 | // Number of times we've sent this binder out of process, which |
| 174 | // translates to an implicit strong count. A client must send RPC binder |
| 175 | // socket's dec ref for each time it is sent out of process in order to |
| 176 | // deallocate it. Note, a proxy binder we are holding onto might be |
| 177 | // sent (this is important when the only remaining refcount of this |
| 178 | // binder is the one associated with a transaction sending it back to |
| 179 | // its server) |
| 180 | size_t timesSent = 0; |
| 181 | |
| 182 | // Number of times we've received this binder, each time corresponds to |
| 183 | // a reference we hold over the wire (not a local incStrong/decStrong) |
| 184 | size_t timesRecd = 0; |
| 185 | |
| 186 | // transaction ID, for async transactions |
| 187 | uint64_t asyncNumber = 0; |
| 188 | |
| 189 | // |
| 190 | // CASE A - local binder we are serving |
| 191 | // |
| 192 | |
| 193 | // async transaction queue, _only_ for local binder |
| 194 | struct AsyncTodo { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 195 | sp<IBinder> ref; |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 196 | CommandData data; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 197 | uint64_t asyncNumber = 0; |
| 198 | |
| 199 | bool operator<(const AsyncTodo& o) const { |
| 200 | return asyncNumber > /* !!! */ o.asyncNumber; |
| 201 | } |
| 202 | }; |
| 203 | std::priority_queue<AsyncTodo> asyncTodo; |
| 204 | |
| 205 | // |
| 206 | // CASE B - remote binder, we are sending transactions to |
| 207 | // |
| 208 | |
| 209 | // (no additional data specific to remote binders) |
| 210 | }; |
| 211 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 212 | // checks if there is any reference left to a node and erases it. If erase |
| 213 | // happens, and there is a strong reference to the binder kept by |
| 214 | // binderNode, this returns that strong reference, so that it can be |
| 215 | // dropped after any locks are removed. |
| 216 | sp<IBinder> tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 217 | // true - success |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 218 | // false - session shutdown, halt |
| 219 | [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node); |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 220 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 221 | std::mutex mNodeMutex; |
| 222 | bool mTerminated = false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 223 | // binders known by both sides of a session |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 224 | std::map<RpcAddress, BinderNode> mNodeForAddress; |
| 225 | }; |
| 226 | |
| 227 | } // namespace android |