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