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