| 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 |  | 
 | 17 | #define LOG_TAG "RpcState" | 
 | 18 |  | 
 | 19 | #include "RpcState.h" | 
 | 20 |  | 
 | 21 | #include <binder/BpBinder.h> | 
 | 22 | #include <binder/RpcServer.h> | 
 | 23 |  | 
 | 24 | #include "Debug.h" | 
 | 25 | #include "RpcWireFormat.h" | 
 | 26 |  | 
 | 27 | #include <inttypes.h> | 
 | 28 |  | 
 | 29 | namespace android { | 
 | 30 |  | 
 | 31 | RpcState::RpcState() {} | 
 | 32 | RpcState::~RpcState() {} | 
 | 33 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 34 | status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 35 |                                    RpcAddress* outAddress) { | 
 | 36 |     bool isRemote = binder->remoteBinder(); | 
 | 37 |     bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder(); | 
 | 38 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 39 |     if (isRpc && binder->remoteBinder()->getPrivateAccessorForId().rpcSession() != session) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 40 |         // We need to be able to send instructions over the socket for how to | 
 | 41 |         // connect to a different server, and we also need to let the host | 
 | 42 |         // process know that this is happening. | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 43 |         ALOGE("Cannot send binder from unrelated binder RPC session."); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 44 |         return INVALID_OPERATION; | 
 | 45 |     } | 
 | 46 |  | 
 | 47 |     if (isRemote && !isRpc) { | 
 | 48 |         // Without additional work, this would have the effect of using this | 
 | 49 |         // process to proxy calls from the socket over to the other process, and | 
 | 50 |         // it would make those calls look like they come from us (not over the | 
 | 51 |         // sockets). In order to make this work transparently like binder, we | 
 | 52 |         // would instead need to send instructions over the socket for how to | 
 | 53 |         // connect to the host process, and we also need to let the host process | 
 | 54 |         // know this was happening. | 
 | 55 |         ALOGE("Cannot send binder proxy %p over sockets", binder.get()); | 
 | 56 |         return INVALID_OPERATION; | 
 | 57 |     } | 
 | 58 |  | 
 | 59 |     std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 60 |  | 
 | 61 |     // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map | 
 | 62 |     // in RpcState | 
 | 63 |     for (auto& [addr, node] : mNodeForAddress) { | 
 | 64 |         if (binder == node.binder) { | 
 | 65 |             if (isRpc) { | 
 | 66 |                 const RpcAddress& actualAddr = | 
 | 67 |                         binder->remoteBinder()->getPrivateAccessorForId().rpcAddress(); | 
 | 68 |                 // TODO(b/182939933): this is only checking integrity of data structure | 
 | 69 |                 // a different data structure doesn't need this | 
 | 70 |                 LOG_ALWAYS_FATAL_IF(addr < actualAddr, "Address mismatch"); | 
 | 71 |                 LOG_ALWAYS_FATAL_IF(actualAddr < addr, "Address mismatch"); | 
 | 72 |             } | 
 | 73 |             node.timesSent++; | 
 | 74 |             node.sentRef = binder; // might already be set | 
 | 75 |             *outAddress = addr; | 
 | 76 |             return OK; | 
 | 77 |         } | 
 | 78 |     } | 
 | 79 |     LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point"); | 
 | 80 |  | 
 | 81 |     auto&& [it, inserted] = mNodeForAddress.insert({RpcAddress::unique(), | 
 | 82 |                                                     BinderNode{ | 
 | 83 |                                                             .binder = binder, | 
 | 84 |                                                             .timesSent = 1, | 
 | 85 |                                                             .sentRef = binder, | 
 | 86 |                                                     }}); | 
 | 87 |     // TODO(b/182939933): better organization could avoid needing this log | 
 | 88 |     LOG_ALWAYS_FATAL_IF(!inserted); | 
 | 89 |  | 
 | 90 |     *outAddress = it->first; | 
 | 91 |     return OK; | 
 | 92 | } | 
 | 93 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 94 | sp<IBinder> RpcState::onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 95 |     std::unique_lock<std::mutex> _l(mNodeMutex); | 
 | 96 |  | 
 | 97 |     if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) { | 
 | 98 |         sp<IBinder> binder = it->second.binder.promote(); | 
 | 99 |  | 
 | 100 |         // implicitly have strong RPC refcount, since we received this binder | 
 | 101 |         it->second.timesRecd++; | 
 | 102 |  | 
 | 103 |         _l.unlock(); | 
 | 104 |  | 
 | 105 |         // We have timesRecd RPC refcounts, but we only need to hold on to one | 
 | 106 |         // when we keep the object. All additional dec strongs are sent | 
 | 107 |         // immediately, we wait to send the last one in BpBinder::onLastDecStrong. | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 108 |         (void)session->sendDecStrong(address); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 109 |  | 
 | 110 |         return binder; | 
 | 111 |     } | 
 | 112 |  | 
 | 113 |     auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}}); | 
 | 114 |     LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy"); | 
 | 115 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 116 |     // Currently, all binders are assumed to be part of the same session (no | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 117 |     // device global binders in the RPC world). | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 118 |     sp<IBinder> binder = BpBinder::create(session, it->first); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 119 |     it->second.binder = binder; | 
 | 120 |     it->second.timesRecd = 1; | 
 | 121 |     return binder; | 
 | 122 | } | 
 | 123 |  | 
 | 124 | size_t RpcState::countBinders() { | 
 | 125 |     std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 126 |     return mNodeForAddress.size(); | 
 | 127 | } | 
 | 128 |  | 
 | 129 | void RpcState::dump() { | 
 | 130 |     std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 131 |     ALOGE("DUMP OF RpcState %p", this); | 
 | 132 |     ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size()); | 
 | 133 |     for (const auto& [address, node] : mNodeForAddress) { | 
 | 134 |         sp<IBinder> binder = node.binder.promote(); | 
 | 135 |  | 
 | 136 |         const char* desc; | 
 | 137 |         if (binder) { | 
 | 138 |             if (binder->remoteBinder()) { | 
 | 139 |                 if (binder->remoteBinder()->isRpcBinder()) { | 
 | 140 |                     desc = "(rpc binder proxy)"; | 
 | 141 |                 } else { | 
 | 142 |                     desc = "(binder proxy)"; | 
 | 143 |                 } | 
 | 144 |             } else { | 
 | 145 |                 desc = "(local binder)"; | 
 | 146 |             } | 
 | 147 |         } else { | 
 | 148 |             desc = "(null)"; | 
 | 149 |         } | 
 | 150 |  | 
 | 151 |         ALOGE("- BINDER NODE: %p times sent:%zu times recd: %zu a:%s type:%s", | 
 | 152 |               node.binder.unsafe_get(), node.timesSent, node.timesRecd, address.toString().c_str(), | 
 | 153 |               desc); | 
 | 154 |     } | 
 | 155 |     ALOGE("END DUMP OF RpcState"); | 
 | 156 | } | 
 | 157 |  | 
 | 158 | void RpcState::terminate() { | 
 | 159 |     if (SHOULD_LOG_RPC_DETAIL) { | 
 | 160 |         ALOGE("RpcState::terminate()"); | 
 | 161 |         dump(); | 
 | 162 |     } | 
 | 163 |  | 
 | 164 |     // if the destructor of a binder object makes another RPC call, then calling | 
 | 165 |     // decStrong could deadlock. So, we must hold onto these binders until | 
 | 166 |     // mNodeMutex is no longer taken. | 
 | 167 |     std::vector<sp<IBinder>> tempHoldBinder; | 
 | 168 |  | 
 | 169 |     { | 
 | 170 |         std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 171 |         mTerminated = true; | 
 | 172 |         for (auto& [address, node] : mNodeForAddress) { | 
 | 173 |             sp<IBinder> binder = node.binder.promote(); | 
 | 174 |             LOG_ALWAYS_FATAL_IF(binder == nullptr, "Binder %p expected to be owned.", binder.get()); | 
 | 175 |  | 
 | 176 |             if (node.sentRef != nullptr) { | 
 | 177 |                 tempHoldBinder.push_back(node.sentRef); | 
 | 178 |             } | 
 | 179 |         } | 
 | 180 |  | 
 | 181 |         mNodeForAddress.clear(); | 
 | 182 |     } | 
 | 183 | } | 
 | 184 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 185 | RpcState::CommandData::CommandData(size_t size) : mSize(size) { | 
 | 186 |     // The maximum size for regular binder is 1MB for all concurrent | 
 | 187 |     // transactions. A very small proportion of transactions are even | 
 | 188 |     // larger than a page, but we need to avoid allocating too much | 
 | 189 |     // data on behalf of an arbitrary client, or we could risk being in | 
 | 190 |     // a position where a single additional allocation could run out of | 
 | 191 |     // memory. | 
 | 192 |     // | 
 | 193 |     // Note, this limit may not reflect the total amount of data allocated for a | 
 | 194 |     // transaction (in some cases, additional fixed size amounts are added), | 
 | 195 |     // though for rough consistency, we should avoid cases where this data type | 
 | 196 |     // is used for multiple dynamic allocations for a single transaction. | 
 | 197 |     constexpr size_t kMaxTransactionAllocation = 100 * 1000; | 
 | 198 |     if (size == 0) return; | 
 | 199 |     if (size > kMaxTransactionAllocation) { | 
 | 200 |         ALOGW("Transaction requested too much data allocation %zu", size); | 
 | 201 |         return; | 
 | 202 |     } | 
 | 203 |     mData.reset(new (std::nothrow) uint8_t[size]); | 
 | 204 | } | 
 | 205 |  | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 206 | bool RpcState::rpcSend(const base::unique_fd& fd, const char* what, const void* data, size_t size) { | 
 | 207 |     LOG_RPC_DETAIL("Sending %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str()); | 
 | 208 |  | 
 | 209 |     if (size > std::numeric_limits<ssize_t>::max()) { | 
 | 210 |         ALOGE("Cannot send %s at size %zu (too big)", what, size); | 
 | 211 |         terminate(); | 
 | 212 |         return false; | 
 | 213 |     } | 
 | 214 |  | 
| Steven Moreland | c6ddf36 | 2021-04-02 01:13:36 +0000 | [diff] [blame] | 215 |     ssize_t sent = TEMP_FAILURE_RETRY(send(fd.get(), data, size, MSG_NOSIGNAL)); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 216 |  | 
 | 217 |     if (sent < 0 || sent != static_cast<ssize_t>(size)) { | 
 | 218 |         ALOGE("Failed to send %s (sent %zd of %zu bytes) on fd %d, error: %s", what, sent, size, | 
 | 219 |               fd.get(), strerror(errno)); | 
 | 220 |  | 
 | 221 |         terminate(); | 
 | 222 |         return false; | 
 | 223 |     } | 
 | 224 |  | 
 | 225 |     return true; | 
 | 226 | } | 
 | 227 |  | 
 | 228 | bool RpcState::rpcRec(const base::unique_fd& fd, const char* what, void* data, size_t size) { | 
 | 229 |     if (size > std::numeric_limits<ssize_t>::max()) { | 
 | 230 |         ALOGE("Cannot rec %s at size %zu (too big)", what, size); | 
 | 231 |         terminate(); | 
 | 232 |         return false; | 
 | 233 |     } | 
 | 234 |  | 
| Steven Moreland | c6ddf36 | 2021-04-02 01:13:36 +0000 | [diff] [blame] | 235 |     ssize_t recd = TEMP_FAILURE_RETRY(recv(fd.get(), data, size, MSG_WAITALL | MSG_NOSIGNAL)); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 236 |  | 
 | 237 |     if (recd < 0 || recd != static_cast<ssize_t>(size)) { | 
 | 238 |         terminate(); | 
 | 239 |  | 
 | 240 |         if (recd == 0 && errno == 0) { | 
 | 241 |             LOG_RPC_DETAIL("No more data when trying to read %s on fd %d", what, fd.get()); | 
 | 242 |             return false; | 
 | 243 |         } | 
 | 244 |  | 
 | 245 |         ALOGE("Failed to read %s (received %zd of %zu bytes) on fd %d, error: %s", what, recd, size, | 
 | 246 |               fd.get(), strerror(errno)); | 
 | 247 |         return false; | 
 | 248 |     } else { | 
 | 249 |         LOG_RPC_DETAIL("Received %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str()); | 
 | 250 |     } | 
 | 251 |  | 
 | 252 |     return true; | 
 | 253 | } | 
 | 254 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 255 | sp<IBinder> RpcState::getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 256 |     Parcel data; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 257 |     data.markForRpc(session); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 258 |     Parcel reply; | 
 | 259 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 260 |     status_t status = transact(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_ROOT, data, session, | 
 | 261 |                                &reply, 0); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 262 |     if (status != OK) { | 
 | 263 |         ALOGE("Error getting root object: %s", statusToString(status).c_str()); | 
 | 264 |         return nullptr; | 
 | 265 |     } | 
 | 266 |  | 
 | 267 |     return reply.readStrongBinder(); | 
 | 268 | } | 
 | 269 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 270 | status_t RpcState::getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session, | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 271 |                                  size_t* maxThreadsOut) { | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 272 |     Parcel data; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 273 |     data.markForRpc(session); | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 274 |     Parcel reply; | 
 | 275 |  | 
 | 276 |     status_t status = transact(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, data, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 277 |                                session, &reply, 0); | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 278 |     if (status != OK) { | 
 | 279 |         ALOGE("Error getting max threads: %s", statusToString(status).c_str()); | 
 | 280 |         return status; | 
 | 281 |     } | 
 | 282 |  | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 283 |     int32_t maxThreads; | 
 | 284 |     status = reply.readInt32(&maxThreads); | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 285 |     if (status != OK) return status; | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 286 |     if (maxThreads <= 0) { | 
 | 287 |         ALOGE("Error invalid max maxThreads: %d", maxThreads); | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 288 |         return BAD_VALUE; | 
 | 289 |     } | 
 | 290 |  | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 291 |     *maxThreadsOut = maxThreads; | 
 | 292 |     return OK; | 
 | 293 | } | 
 | 294 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 295 | status_t RpcState::getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session, | 
 | 296 |                                 int32_t* sessionIdOut) { | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 297 |     Parcel data; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 298 |     data.markForRpc(session); | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 299 |     Parcel reply; | 
 | 300 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 301 |     status_t status = transact(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_SESSION_ID, data, | 
 | 302 |                                session, &reply, 0); | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 303 |     if (status != OK) { | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 304 |         ALOGE("Error getting session ID: %s", statusToString(status).c_str()); | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 305 |         return status; | 
 | 306 |     } | 
 | 307 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 308 |     int32_t sessionId; | 
 | 309 |     status = reply.readInt32(&sessionId); | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 310 |     if (status != OK) return status; | 
 | 311 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 312 |     *sessionIdOut = sessionId; | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 313 |     return OK; | 
 | 314 | } | 
 | 315 |  | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 316 | status_t RpcState::transact(const base::unique_fd& fd, const RpcAddress& address, uint32_t code, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 317 |                             const Parcel& data, const sp<RpcSession>& session, Parcel* reply, | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 318 |                             uint32_t flags) { | 
 | 319 |     uint64_t asyncNumber = 0; | 
 | 320 |  | 
 | 321 |     if (!address.isZero()) { | 
 | 322 |         std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 323 |         if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races | 
 | 324 |         auto it = mNodeForAddress.find(address); | 
 | 325 |         LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending transact on unknown address %s", | 
 | 326 |                             address.toString().c_str()); | 
 | 327 |  | 
 | 328 |         if (flags & IBinder::FLAG_ONEWAY) { | 
 | 329 |             asyncNumber = it->second.asyncNumber++; | 
 | 330 |         } | 
 | 331 |     } | 
 | 332 |  | 
 | 333 |     if (!data.isForRpc()) { | 
 | 334 |         ALOGE("Refusing to send RPC with parcel not crafted for RPC"); | 
 | 335 |         return BAD_TYPE; | 
 | 336 |     } | 
 | 337 |  | 
 | 338 |     if (data.objectsCount() != 0) { | 
 | 339 |         ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data); | 
 | 340 |         return BAD_TYPE; | 
 | 341 |     } | 
 | 342 |  | 
 | 343 |     RpcWireTransaction transaction{ | 
 | 344 |             .address = address.viewRawEmbedded(), | 
 | 345 |             .code = code, | 
 | 346 |             .flags = flags, | 
 | 347 |             .asyncNumber = asyncNumber, | 
 | 348 |     }; | 
 | 349 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 350 |     CommandData transactionData(sizeof(RpcWireTransaction) + data.dataSize()); | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 351 |     if (!transactionData.valid()) { | 
 | 352 |         return NO_MEMORY; | 
 | 353 |     } | 
 | 354 |  | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 355 |     memcpy(transactionData.data() + 0, &transaction, sizeof(RpcWireTransaction)); | 
 | 356 |     memcpy(transactionData.data() + sizeof(RpcWireTransaction), data.data(), data.dataSize()); | 
 | 357 |  | 
 | 358 |     if (transactionData.size() > std::numeric_limits<uint32_t>::max()) { | 
 | 359 |         ALOGE("Transaction size too big %zu", transactionData.size()); | 
 | 360 |         return BAD_VALUE; | 
 | 361 |     } | 
 | 362 |  | 
 | 363 |     RpcWireHeader command{ | 
 | 364 |             .command = RPC_COMMAND_TRANSACT, | 
 | 365 |             .bodySize = static_cast<uint32_t>(transactionData.size()), | 
 | 366 |     }; | 
 | 367 |  | 
 | 368 |     if (!rpcSend(fd, "transact header", &command, sizeof(command))) { | 
 | 369 |         return DEAD_OBJECT; | 
 | 370 |     } | 
 | 371 |     if (!rpcSend(fd, "command body", transactionData.data(), transactionData.size())) { | 
 | 372 |         return DEAD_OBJECT; | 
 | 373 |     } | 
 | 374 |  | 
 | 375 |     if (flags & IBinder::FLAG_ONEWAY) { | 
 | 376 |         return OK; // do not wait for result | 
 | 377 |     } | 
 | 378 |  | 
 | 379 |     LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); | 
 | 380 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 381 |     return waitForReply(fd, session, reply); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 382 | } | 
 | 383 |  | 
| Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 384 | static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize, | 
 | 385 |                                const binder_size_t* objects, size_t objectsCount) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 386 |     (void)p; | 
 | 387 |     delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data)); | 
 | 388 |     (void)dataSize; | 
 | 389 |     LOG_ALWAYS_FATAL_IF(objects != nullptr); | 
 | 390 |     LOG_ALWAYS_FATAL_IF(objectsCount, 0); | 
 | 391 | } | 
 | 392 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 393 | status_t RpcState::waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session, | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 394 |                                 Parcel* reply) { | 
 | 395 |     RpcWireHeader command; | 
 | 396 |     while (true) { | 
 | 397 |         if (!rpcRec(fd, "command header", &command, sizeof(command))) { | 
 | 398 |             return DEAD_OBJECT; | 
 | 399 |         } | 
 | 400 |  | 
 | 401 |         if (command.command == RPC_COMMAND_REPLY) break; | 
 | 402 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 403 |         status_t status = processServerCommand(fd, session, command); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 404 |         if (status != OK) return status; | 
 | 405 |     } | 
 | 406 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 407 |     CommandData data(command.bodySize); | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 408 |     if (!data.valid()) { | 
 | 409 |         return NO_MEMORY; | 
 | 410 |     } | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 411 |  | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 412 |     if (!rpcRec(fd, "reply body", data.data(), command.bodySize)) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 413 |         return DEAD_OBJECT; | 
 | 414 |     } | 
 | 415 |  | 
 | 416 |     if (command.bodySize < sizeof(RpcWireReply)) { | 
 | 417 |         ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", | 
 | 418 |               sizeof(RpcWireReply), command.bodySize); | 
 | 419 |         terminate(); | 
 | 420 |         return BAD_VALUE; | 
 | 421 |     } | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 422 |     RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data()); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 423 |     if (rpcReply->status != OK) return rpcReply->status; | 
 | 424 |  | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 425 |     data.release(); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 426 |     reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data), | 
| Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 427 |                                nullptr, 0, cleanup_reply_data); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 428 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 429 |     reply->markForRpc(session); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 430 |  | 
 | 431 |     return OK; | 
 | 432 | } | 
 | 433 |  | 
 | 434 | status_t RpcState::sendDecStrong(const base::unique_fd& fd, const RpcAddress& addr) { | 
 | 435 |     { | 
 | 436 |         std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 437 |         if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races | 
 | 438 |         auto it = mNodeForAddress.find(addr); | 
 | 439 |         LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending dec strong on unknown address %s", | 
 | 440 |                             addr.toString().c_str()); | 
 | 441 |         LOG_ALWAYS_FATAL_IF(it->second.timesRecd <= 0, "Bad dec strong %s", | 
 | 442 |                             addr.toString().c_str()); | 
 | 443 |  | 
 | 444 |         it->second.timesRecd--; | 
 | 445 |         if (it->second.timesRecd == 0 && it->second.timesSent == 0) { | 
 | 446 |             mNodeForAddress.erase(it); | 
 | 447 |         } | 
 | 448 |     } | 
 | 449 |  | 
 | 450 |     RpcWireHeader cmd = { | 
 | 451 |             .command = RPC_COMMAND_DEC_STRONG, | 
 | 452 |             .bodySize = sizeof(RpcWireAddress), | 
 | 453 |     }; | 
 | 454 |     if (!rpcSend(fd, "dec ref header", &cmd, sizeof(cmd))) return DEAD_OBJECT; | 
 | 455 |     if (!rpcSend(fd, "dec ref body", &addr.viewRawEmbedded(), sizeof(RpcWireAddress))) | 
 | 456 |         return DEAD_OBJECT; | 
 | 457 |     return OK; | 
 | 458 | } | 
 | 459 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 460 | status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcSession>& session) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 461 |     LOG_RPC_DETAIL("getAndExecuteCommand on fd %d", fd.get()); | 
 | 462 |  | 
 | 463 |     RpcWireHeader command; | 
 | 464 |     if (!rpcRec(fd, "command header", &command, sizeof(command))) { | 
 | 465 |         return DEAD_OBJECT; | 
 | 466 |     } | 
 | 467 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 468 |     return processServerCommand(fd, session, command); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 469 | } | 
 | 470 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 471 | status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 472 |                                         const RpcWireHeader& command) { | 
 | 473 |     switch (command.command) { | 
 | 474 |         case RPC_COMMAND_TRANSACT: | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 475 |             return processTransact(fd, session, command); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 476 |         case RPC_COMMAND_DEC_STRONG: | 
 | 477 |             return processDecStrong(fd, command); | 
 | 478 |     } | 
 | 479 |  | 
 | 480 |     // We should always know the version of the opposing side, and since the | 
 | 481 |     // RPC-binder-level wire protocol is not self synchronizing, we have no way | 
 | 482 |     // to understand where the current command ends and the next one begins. We | 
 | 483 |     // also can't consider it a fatal error because this would allow any client | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 484 |     // to kill us, so ending the session for misbehaving client. | 
 | 485 |     ALOGE("Unknown RPC command %d - terminating session", command.command); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 486 |     terminate(); | 
 | 487 |     return DEAD_OBJECT; | 
 | 488 | } | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 489 | status_t RpcState::processTransact(const base::unique_fd& fd, const sp<RpcSession>& session, | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 490 |                                    const RpcWireHeader& command) { | 
 | 491 |     LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); | 
 | 492 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 493 |     CommandData transactionData(command.bodySize); | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 494 |     if (!transactionData.valid()) { | 
 | 495 |         return NO_MEMORY; | 
 | 496 |     } | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 497 |     if (!rpcRec(fd, "transaction body", transactionData.data(), transactionData.size())) { | 
 | 498 |         return DEAD_OBJECT; | 
 | 499 |     } | 
 | 500 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 501 |     return processTransactInternal(fd, session, std::move(transactionData)); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 502 | } | 
 | 503 |  | 
| Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 504 | static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize, | 
 | 505 |                                         const binder_size_t* objects, size_t objectsCount) { | 
 | 506 |     (void)p; | 
 | 507 |     (void)data; | 
 | 508 |     (void)dataSize; | 
 | 509 |     (void)objects; | 
 | 510 |     (void)objectsCount; | 
 | 511 | } | 
 | 512 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 513 | status_t RpcState::processTransactInternal(const base::unique_fd& fd, const sp<RpcSession>& session, | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 514 |                                            CommandData transactionData) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 515 |     if (transactionData.size() < sizeof(RpcWireTransaction)) { | 
 | 516 |         ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", | 
 | 517 |               sizeof(RpcWireTransaction), transactionData.size()); | 
 | 518 |         terminate(); | 
 | 519 |         return BAD_VALUE; | 
 | 520 |     } | 
 | 521 |     RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); | 
 | 522 |  | 
 | 523 |     // TODO(b/182939933): heap allocation just for lookup in mNodeForAddress, | 
 | 524 |     // maybe add an RpcAddress 'view' if the type remains 'heavy' | 
 | 525 |     auto addr = RpcAddress::fromRawEmbedded(&transaction->address); | 
 | 526 |  | 
 | 527 |     status_t replyStatus = OK; | 
 | 528 |     sp<IBinder> target; | 
 | 529 |     if (!addr.isZero()) { | 
 | 530 |         std::lock_guard<std::mutex> _l(mNodeMutex); | 
 | 531 |  | 
 | 532 |         auto it = mNodeForAddress.find(addr); | 
 | 533 |         if (it == mNodeForAddress.end()) { | 
 | 534 |             ALOGE("Unknown binder address %s.", addr.toString().c_str()); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 535 |             replyStatus = BAD_VALUE; | 
 | 536 |         } else { | 
 | 537 |             target = it->second.binder.promote(); | 
 | 538 |             if (target == nullptr) { | 
 | 539 |                 // This can happen if the binder is remote in this process, and | 
 | 540 |                 // another thread has called the last decStrong on this binder. | 
 | 541 |                 // However, for local binders, it indicates a misbehaving client | 
 | 542 |                 // (any binder which is being transacted on should be holding a | 
 | 543 |                 // strong ref count), so in either case, terminating the | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 544 |                 // session. | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 545 |                 ALOGE("While transacting, binder has been deleted at address %s. Terminating!", | 
 | 546 |                       addr.toString().c_str()); | 
 | 547 |                 terminate(); | 
 | 548 |                 replyStatus = BAD_VALUE; | 
 | 549 |             } else if (target->localBinder() == nullptr) { | 
 | 550 |                 ALOGE("Transactions can only go to local binders, not address %s. Terminating!", | 
 | 551 |                       addr.toString().c_str()); | 
 | 552 |                 terminate(); | 
 | 553 |                 replyStatus = BAD_VALUE; | 
 | 554 |             } else if (transaction->flags & IBinder::FLAG_ONEWAY) { | 
 | 555 |                 if (transaction->asyncNumber != it->second.asyncNumber) { | 
 | 556 |                     // we need to process some other asynchronous transaction | 
 | 557 |                     // first | 
 | 558 |                     // TODO(b/183140903): limit enqueues/detect overfill for bad client | 
 | 559 |                     // TODO(b/183140903): detect when an object is deleted when it still has | 
 | 560 |                     //        pending async transactions | 
 | 561 |                     it->second.asyncTodo.push(BinderNode::AsyncTodo{ | 
 | 562 |                             .data = std::move(transactionData), | 
 | 563 |                             .asyncNumber = transaction->asyncNumber, | 
 | 564 |                     }); | 
 | 565 |                     LOG_RPC_DETAIL("Enqueuing %" PRId64 " on %s", transaction->asyncNumber, | 
 | 566 |                                    addr.toString().c_str()); | 
 | 567 |                     return OK; | 
 | 568 |                 } | 
 | 569 |             } | 
 | 570 |         } | 
 | 571 |     } | 
 | 572 |  | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 573 |     Parcel reply; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 574 |     reply.markForRpc(session); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 575 |  | 
 | 576 |     if (replyStatus == OK) { | 
| Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 577 |         Parcel data; | 
 | 578 |         // transaction->data is owned by this function. Parcel borrows this data and | 
 | 579 |         // only holds onto it for the duration of this function call. Parcel will be | 
 | 580 |         // deleted before the 'transactionData' object. | 
 | 581 |         data.ipcSetDataReference(transaction->data, | 
 | 582 |                                  transactionData.size() - offsetof(RpcWireTransaction, data), | 
 | 583 |                                  nullptr /*object*/, 0 /*objectCount*/, | 
 | 584 |                                  do_nothing_to_transact_data); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 585 |         data.markForRpc(session); | 
| Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 586 |  | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 587 |         if (target) { | 
 | 588 |             replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); | 
 | 589 |         } else { | 
 | 590 |             LOG_RPC_DETAIL("Got special transaction %u", transaction->code); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 591 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 592 |             sp<RpcServer> server = session->server().promote(); | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 593 |             if (server) { | 
 | 594 |                 // special case for 'zero' address (special server commands) | 
 | 595 |                 switch (transaction->code) { | 
 | 596 |                     case RPC_SPECIAL_TRANSACT_GET_ROOT: { | 
 | 597 |                         replyStatus = reply.writeStrongBinder(server->getRootObject()); | 
 | 598 |                         break; | 
 | 599 |                     } | 
 | 600 |                     case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { | 
 | 601 |                         replyStatus = reply.writeInt32(server->getMaxThreads()); | 
 | 602 |                         break; | 
 | 603 |                     } | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 604 |                     case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { | 
 | 605 |                         // only sessions w/ services can be the source of a | 
 | 606 |                         // session ID (so still guarded by non-null server) | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 607 |                         // | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 608 |                         // sessions associated with servers must have an ID | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 609 |                         // (hence abort) | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 610 |                         int32_t id = session->getPrivateAccessorForId().get().value(); | 
| Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 611 |                         replyStatus = reply.writeInt32(id); | 
 | 612 |                         break; | 
 | 613 |                     } | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 614 |                     default: { | 
 | 615 |                         replyStatus = UNKNOWN_TRANSACTION; | 
 | 616 |                     } | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 617 |                 } | 
| Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 618 |             } else { | 
 | 619 |                 ALOGE("Special command sent, but no server object attached."); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 620 |             } | 
 | 621 |         } | 
 | 622 |     } | 
 | 623 |  | 
 | 624 |     if (transaction->flags & IBinder::FLAG_ONEWAY) { | 
 | 625 |         if (replyStatus != OK) { | 
 | 626 |             ALOGW("Oneway call failed with error: %d", replyStatus); | 
 | 627 |         } | 
 | 628 |  | 
 | 629 |         LOG_RPC_DETAIL("Processed async transaction %" PRId64 " on %s", transaction->asyncNumber, | 
 | 630 |                        addr.toString().c_str()); | 
 | 631 |  | 
 | 632 |         // Check to see if there is another asynchronous transaction to process. | 
 | 633 |         // This behavior differs from binder behavior, since in the binder | 
 | 634 |         // driver, asynchronous transactions will be processed after existing | 
 | 635 |         // pending binder transactions on the queue. The downside of this is | 
 | 636 |         // that asynchronous transactions can be drowned out by synchronous | 
 | 637 |         // transactions. However, we have no easy way to queue these | 
 | 638 |         // transactions after the synchronous transactions we may want to read | 
 | 639 |         // from the wire. So, in socket binder here, we have the opposite | 
 | 640 |         // downside: asynchronous transactions may drown out synchronous | 
 | 641 |         // transactions. | 
 | 642 |         { | 
 | 643 |             std::unique_lock<std::mutex> _l(mNodeMutex); | 
 | 644 |             auto it = mNodeForAddress.find(addr); | 
 | 645 |             // last refcount dropped after this transaction happened | 
 | 646 |             if (it == mNodeForAddress.end()) return OK; | 
 | 647 |  | 
 | 648 |             // note - only updated now, instead of later, so that other threads | 
 | 649 |             // will queue any later transactions | 
 | 650 |  | 
 | 651 |             // TODO(b/183140903): support > 2**64 async transactions | 
 | 652 |             //     (we can do this by allowing asyncNumber to wrap, since we | 
 | 653 |             //     don't expect more than 2**64 simultaneous transactions) | 
 | 654 |             it->second.asyncNumber++; | 
 | 655 |  | 
 | 656 |             if (it->second.asyncTodo.size() == 0) return OK; | 
 | 657 |             if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { | 
 | 658 |                 LOG_RPC_DETAIL("Found next async transaction %" PRId64 " on %s", | 
 | 659 |                                it->second.asyncNumber, addr.toString().c_str()); | 
 | 660 |  | 
 | 661 |                 // justification for const_cast (consider avoiding priority_queue): | 
 | 662 |                 // - AsyncTodo operator< doesn't depend on 'data' object | 
 | 663 |                 // - gotta go fast | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 664 |                 CommandData data = std::move( | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 665 |                         const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()).data); | 
 | 666 |                 it->second.asyncTodo.pop(); | 
 | 667 |                 _l.unlock(); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 668 |                 return processTransactInternal(fd, session, std::move(data)); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 669 |             } | 
 | 670 |         } | 
 | 671 |         return OK; | 
 | 672 |     } | 
 | 673 |  | 
 | 674 |     RpcWireReply rpcReply{ | 
 | 675 |             .status = replyStatus, | 
 | 676 |     }; | 
 | 677 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 678 |     CommandData replyData(sizeof(RpcWireReply) + reply.dataSize()); | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 679 |     if (!replyData.valid()) { | 
 | 680 |         return NO_MEMORY; | 
 | 681 |     } | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 682 |     memcpy(replyData.data() + 0, &rpcReply, sizeof(RpcWireReply)); | 
 | 683 |     memcpy(replyData.data() + sizeof(RpcWireReply), reply.data(), reply.dataSize()); | 
 | 684 |  | 
 | 685 |     if (replyData.size() > std::numeric_limits<uint32_t>::max()) { | 
 | 686 |         ALOGE("Reply size too big %zu", transactionData.size()); | 
 | 687 |         terminate(); | 
 | 688 |         return BAD_VALUE; | 
 | 689 |     } | 
 | 690 |  | 
 | 691 |     RpcWireHeader cmdReply{ | 
 | 692 |             .command = RPC_COMMAND_REPLY, | 
 | 693 |             .bodySize = static_cast<uint32_t>(replyData.size()), | 
 | 694 |     }; | 
 | 695 |  | 
 | 696 |     if (!rpcSend(fd, "reply header", &cmdReply, sizeof(RpcWireHeader))) { | 
 | 697 |         return DEAD_OBJECT; | 
 | 698 |     } | 
 | 699 |     if (!rpcSend(fd, "reply body", replyData.data(), replyData.size())) { | 
 | 700 |         return DEAD_OBJECT; | 
 | 701 |     } | 
 | 702 |     return OK; | 
 | 703 | } | 
 | 704 |  | 
 | 705 | status_t RpcState::processDecStrong(const base::unique_fd& fd, const RpcWireHeader& command) { | 
 | 706 |     LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); | 
 | 707 |  | 
| Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 708 |     CommandData commandData(command.bodySize); | 
| Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 709 |     if (!commandData.valid()) { | 
 | 710 |         return NO_MEMORY; | 
 | 711 |     } | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 712 |     if (!rpcRec(fd, "dec ref body", commandData.data(), commandData.size())) { | 
 | 713 |         return DEAD_OBJECT; | 
 | 714 |     } | 
 | 715 |  | 
 | 716 |     if (command.bodySize < sizeof(RpcWireAddress)) { | 
 | 717 |         ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireAddress. Terminating!", | 
 | 718 |               sizeof(RpcWireAddress), command.bodySize); | 
 | 719 |         terminate(); | 
 | 720 |         return BAD_VALUE; | 
 | 721 |     } | 
 | 722 |     RpcWireAddress* address = reinterpret_cast<RpcWireAddress*>(commandData.data()); | 
 | 723 |  | 
 | 724 |     // TODO(b/182939933): heap allocation just for lookup | 
 | 725 |     auto addr = RpcAddress::fromRawEmbedded(address); | 
 | 726 |     std::unique_lock<std::mutex> _l(mNodeMutex); | 
 | 727 |     auto it = mNodeForAddress.find(addr); | 
 | 728 |     if (it == mNodeForAddress.end()) { | 
 | 729 |         ALOGE("Unknown binder address %s for dec strong.", addr.toString().c_str()); | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 730 |         return OK; | 
 | 731 |     } | 
 | 732 |  | 
 | 733 |     sp<IBinder> target = it->second.binder.promote(); | 
 | 734 |     if (target == nullptr) { | 
 | 735 |         ALOGE("While requesting dec strong, binder has been deleted at address %s. Terminating!", | 
 | 736 |               addr.toString().c_str()); | 
 | 737 |         terminate(); | 
 | 738 |         return BAD_VALUE; | 
 | 739 |     } | 
 | 740 |  | 
 | 741 |     if (it->second.timesSent == 0) { | 
 | 742 |         ALOGE("No record of sending binder, but requested decStrong: %s", addr.toString().c_str()); | 
 | 743 |         return OK; | 
 | 744 |     } | 
 | 745 |  | 
 | 746 |     LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %s", | 
 | 747 |                         addr.toString().c_str()); | 
 | 748 |  | 
 | 749 |     sp<IBinder> tempHold; | 
 | 750 |  | 
 | 751 |     it->second.timesSent--; | 
 | 752 |     if (it->second.timesSent == 0) { | 
 | 753 |         tempHold = it->second.sentRef; | 
 | 754 |         it->second.sentRef = nullptr; | 
 | 755 |  | 
 | 756 |         if (it->second.timesRecd == 0) { | 
 | 757 |             mNodeForAddress.erase(it); | 
 | 758 |         } | 
 | 759 |     } | 
 | 760 |  | 
 | 761 |     _l.unlock(); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 762 |     tempHold = nullptr; // destructor may make binder calls on this session | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 763 |  | 
 | 764 |     return OK; | 
 | 765 | } | 
 | 766 |  | 
 | 767 | } // namespace android |