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