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 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 21 | #include <android-base/macros.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 22 | #include <binder/BpBinder.h> |
Tomasz Wasilczyk | d6c8a4c | 2023-10-11 20:41:51 +0000 | [diff] [blame] | 23 | #include <binder/Functional.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <binder/RpcServer.h> |
| 26 | |
| 27 | #include "Debug.h" |
| 28 | #include "RpcWireFormat.h" |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 29 | #include "Utils.h" |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 30 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 31 | #include <random> |
Tomasz Wasilczyk | 3caae30 | 2023-10-12 20:57:02 +0000 | [diff] [blame] | 32 | #include <sstream> |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 33 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | #include <inttypes.h> |
| 35 | |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame] | 36 | #ifdef __ANDROID__ |
| 37 | #include <cutils/properties.h> |
| 38 | #endif |
| 39 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Tomasz Wasilczyk | d6c8a4c | 2023-10-11 20:41:51 +0000 | [diff] [blame] | 42 | using namespace android::binder::impl; |
| 43 | |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 44 | #if RPC_FLAKE_PRONE |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 45 | void rpcMaybeWaitToFlake() { |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 46 | [[clang::no_destroy]] static std::random_device r; |
Steven Moreland | 7e2675c | 2022-09-28 23:34:52 +0000 | [diff] [blame] | 47 | [[clang::no_destroy]] static RpcMutex m; |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 48 | unsigned num; |
| 49 | { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 50 | RpcMutexLockGuard lock(m); |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 51 | num = r(); |
| 52 | } |
| 53 | if (num % 10 == 0) usleep(num % 1000); |
| 54 | } |
| 55 | #endif |
| 56 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 57 | static bool enableAncillaryFds(RpcSession::FileDescriptorTransportMode mode) { |
| 58 | switch (mode) { |
| 59 | case RpcSession::FileDescriptorTransportMode::NONE: |
| 60 | return false; |
| 61 | case RpcSession::FileDescriptorTransportMode::UNIX: |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 62 | case RpcSession::FileDescriptorTransportMode::TRUSTY: |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 63 | return true; |
| 64 | } |
Tomasz Wasilczyk | 7b5430b | 2023-06-28 14:04:51 -0700 | [diff] [blame] | 65 | LOG_ALWAYS_FATAL("Invalid FileDescriptorTransportMode: %d", static_cast<int>(mode)); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 68 | RpcState::RpcState() {} |
| 69 | RpcState::~RpcState() {} |
| 70 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 71 | status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 72 | uint64_t* outAddress) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 73 | bool isRemote = binder->remoteBinder(); |
| 74 | bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder(); |
| 75 | |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 76 | if (isRpc && binder->remoteBinder()->getPrivateAccessor().rpcSession() != session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 77 | // We need to be able to send instructions over the socket for how to |
| 78 | // connect to a different server, and we also need to let the host |
| 79 | // process know that this is happening. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 80 | ALOGE("Cannot send binder from unrelated binder RPC session."); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 81 | return INVALID_OPERATION; |
| 82 | } |
| 83 | |
| 84 | if (isRemote && !isRpc) { |
| 85 | // Without additional work, this would have the effect of using this |
| 86 | // process to proxy calls from the socket over to the other process, and |
| 87 | // it would make those calls look like they come from us (not over the |
| 88 | // sockets). In order to make this work transparently like binder, we |
| 89 | // would instead need to send instructions over the socket for how to |
| 90 | // connect to the host process, and we also need to let the host process |
| 91 | // know this was happening. |
| 92 | ALOGE("Cannot send binder proxy %p over sockets", binder.get()); |
| 93 | return INVALID_OPERATION; |
| 94 | } |
| 95 | |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 96 | RpcMutexLockGuard _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 97 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 98 | |
| 99 | // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map |
| 100 | // in RpcState |
| 101 | for (auto& [addr, node] : mNodeForAddress) { |
| 102 | if (binder == node.binder) { |
| 103 | if (isRpc) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 104 | // check integrity of data structure |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 105 | uint64_t actualAddr = binder->remoteBinder()->getPrivateAccessor().rpcAddress(); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 106 | LOG_ALWAYS_FATAL_IF(addr != actualAddr, "Address mismatch %" PRIu64 " vs %" PRIu64, |
| 107 | addr, actualAddr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 108 | } |
| 109 | node.timesSent++; |
| 110 | node.sentRef = binder; // might already be set |
| 111 | *outAddress = addr; |
| 112 | return OK; |
| 113 | } |
| 114 | } |
| 115 | LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point"); |
| 116 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 117 | bool forServer = session->server() != nullptr; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 118 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 119 | // arbitrary limit for maximum number of nodes in a process (otherwise we |
| 120 | // might run out of addresses) |
| 121 | if (mNodeForAddress.size() > 100000) { |
| 122 | return NO_MEMORY; |
| 123 | } |
| 124 | |
| 125 | while (true) { |
| 126 | RpcWireAddress address{ |
| 127 | .options = RPC_WIRE_ADDRESS_OPTION_CREATED, |
| 128 | .address = mNextId, |
| 129 | }; |
| 130 | if (forServer) { |
| 131 | address.options |= RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 132 | } |
| 133 | |
| 134 | // avoid ubsan abort |
| 135 | if (mNextId >= std::numeric_limits<uint32_t>::max()) { |
| 136 | mNextId = 0; |
| 137 | } else { |
| 138 | mNextId++; |
| 139 | } |
| 140 | |
| 141 | auto&& [it, inserted] = mNodeForAddress.insert({RpcWireAddress::toRaw(address), |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 142 | BinderNode{ |
| 143 | .binder = binder, |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 144 | .sentRef = binder, |
Andrei Homescu | 5a036f3 | 2022-03-08 22:54:40 +0000 | [diff] [blame] | 145 | .timesSent = 1, |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 146 | }}); |
| 147 | if (inserted) { |
| 148 | *outAddress = it->first; |
| 149 | return OK; |
| 150 | } |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 151 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 154 | status_t RpcState::onBinderEntering(const sp<RpcSession>& session, uint64_t address, |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 155 | sp<IBinder>* out) { |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 156 | // ensure that: if we want to use addresses for something else in the future (for |
| 157 | // instance, allowing transitive binder sends), that we don't accidentally |
| 158 | // send those addresses to old server. Accidentally ignoring this in that |
| 159 | // case and considering the binder to be recognized could cause this |
| 160 | // process to accidentally proxy transactions for that binder. Of course, |
| 161 | // if we communicate with a binder, it could always be proxying |
| 162 | // information. However, we want to make sure that isn't done on accident |
| 163 | // by a client. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 164 | RpcWireAddress addr = RpcWireAddress::fromRaw(address); |
| 165 | constexpr uint32_t kKnownOptions = |
| 166 | RPC_WIRE_ADDRESS_OPTION_CREATED | RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 167 | if (addr.options & ~kKnownOptions) { |
| 168 | ALOGE("Address is of an unknown type, rejecting: %" PRIu64, address); |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 169 | return BAD_VALUE; |
| 170 | } |
| 171 | |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 172 | RpcMutexLockGuard _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 173 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 174 | |
| 175 | if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 176 | *out = it->second.binder.promote(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 177 | |
| 178 | // implicitly have strong RPC refcount, since we received this binder |
| 179 | it->second.timesRecd++; |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 180 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 183 | // we don't know about this binder, so the other side of the connection |
| 184 | // should have created it. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 185 | if ((addr.options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER) == !!session->server()) { |
| 186 | ALOGE("Server received unrecognized address which we should own the creation of %" PRIu64, |
| 187 | address); |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 188 | return BAD_VALUE; |
| 189 | } |
| 190 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 191 | auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}}); |
| 192 | LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy"); |
| 193 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 194 | // 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] | 195 | // device global binders in the RPC world). |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 196 | it->second.binder = *out = BpBinder::PrivateAccessor::create(session, it->first); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 197 | it->second.timesRecd = 1; |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 198 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 201 | status_t RpcState::flushExcessBinderRefs(const sp<RpcSession>& session, uint64_t address, |
| 202 | const sp<IBinder>& binder) { |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame] | 203 | // We can flush all references when the binder is destroyed. No need to send |
| 204 | // extra reference counting packets now. |
| 205 | if (binder->remoteBinder()) return OK; |
| 206 | |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 207 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 208 | if (mTerminated) return DEAD_OBJECT; |
| 209 | |
| 210 | auto it = mNodeForAddress.find(address); |
| 211 | |
| 212 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Can't be deleted while we hold sp<>"); |
| 213 | LOG_ALWAYS_FATAL_IF(it->second.binder != binder, |
| 214 | "Caller of flushExcessBinderRefs using inconsistent arguments"); |
| 215 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame] | 216 | LOG_ALWAYS_FATAL_IF(it->second.timesSent <= 0, "Local binder must have been sent %p", |
| 217 | binder.get()); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 218 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame] | 219 | // For a local binder, we only need to know that we sent it. Now that we |
| 220 | // have an sp<> for this call, we don't need anything more. If the other |
| 221 | // process is done with this binder, it needs to know we received the |
| 222 | // refcount associated with this call, so we can acknowledge that we |
| 223 | // received it. Once (or if) it has no other refcounts, it would reply with |
| 224 | // its own decStrong so that it could be removed from this session. |
| 225 | if (it->second.timesRecd != 0) { |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 226 | _l.unlock(); |
| 227 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame] | 228 | return session->sendDecStrongToTarget(address, 0); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return OK; |
| 232 | } |
| 233 | |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 234 | status_t RpcState::sendObituaries(const sp<RpcSession>& session) { |
| 235 | RpcMutexUniqueLock _l(mNodeMutex); |
| 236 | |
| 237 | // Gather strong pointers to all of the remote binders for this session so |
| 238 | // we hold the strong references. remoteBinder() returns a raw pointer. |
| 239 | // Send the obituaries and drop the strong pointers outside of the lock so |
| 240 | // the destructors and the onBinderDied calls are not done while locked. |
| 241 | std::vector<sp<IBinder>> remoteBinders; |
| 242 | for (const auto& [_, binderNode] : mNodeForAddress) { |
| 243 | if (auto binder = binderNode.binder.promote()) { |
| 244 | remoteBinders.push_back(std::move(binder)); |
| 245 | } |
| 246 | } |
| 247 | _l.unlock(); |
| 248 | |
| 249 | for (const auto& binder : remoteBinders) { |
| 250 | if (binder->remoteBinder() && |
| 251 | binder->remoteBinder()->getPrivateAccessor().rpcSession() == session) { |
| 252 | binder->remoteBinder()->sendObituary(); |
| 253 | } |
| 254 | } |
| 255 | return OK; |
| 256 | } |
| 257 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 258 | size_t RpcState::countBinders() { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 259 | RpcMutexLockGuard _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 260 | return mNodeForAddress.size(); |
| 261 | } |
| 262 | |
| 263 | void RpcState::dump() { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 264 | RpcMutexLockGuard _l(mNodeMutex); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 265 | dumpLocked(); |
| 266 | } |
| 267 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 268 | void RpcState::clear() { |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 269 | return clear(RpcMutexUniqueLock(mNodeMutex)); |
| 270 | } |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 271 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 272 | void RpcState::clear(RpcMutexUniqueLock nodeLock) { |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 273 | if (mTerminated) { |
| 274 | LOG_ALWAYS_FATAL_IF(!mNodeForAddress.empty(), |
| 275 | "New state should be impossible after terminating!"); |
| 276 | return; |
| 277 | } |
Steven Moreland | 0092fe3 | 2022-07-15 00:15:34 +0000 | [diff] [blame] | 278 | mTerminated = true; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 279 | |
| 280 | if (SHOULD_LOG_RPC_DETAIL) { |
| 281 | ALOGE("RpcState::clear()"); |
| 282 | dumpLocked(); |
| 283 | } |
| 284 | |
Steven Moreland | 0092fe3 | 2022-07-15 00:15:34 +0000 | [diff] [blame] | 285 | // invariants |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 286 | for (auto& [address, node] : mNodeForAddress) { |
Steven Moreland | 0092fe3 | 2022-07-15 00:15:34 +0000 | [diff] [blame] | 287 | bool guaranteedHaveBinder = node.timesSent > 0; |
| 288 | if (guaranteedHaveBinder) { |
| 289 | LOG_ALWAYS_FATAL_IF(node.sentRef == nullptr, |
| 290 | "Binder expected to be owned with address: %" PRIu64 " %s", address, |
| 291 | node.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Steven Moreland | 0092fe3 | 2022-07-15 00:15:34 +0000 | [diff] [blame] | 295 | // if the destructor of a binder object makes another RPC call, then calling |
| 296 | // decStrong could deadlock. So, we must hold onto these binders until |
| 297 | // mNodeMutex is no longer taken. |
| 298 | auto temp = std::move(mNodeForAddress); |
| 299 | mNodeForAddress.clear(); // RpcState isn't reusable, but for future/explicit |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 300 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 301 | nodeLock.unlock(); |
Steven Moreland | 0092fe3 | 2022-07-15 00:15:34 +0000 | [diff] [blame] | 302 | temp.clear(); // explicit |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void RpcState::dumpLocked() { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 306 | ALOGE("DUMP OF RpcState %p", this); |
| 307 | ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size()); |
| 308 | for (const auto& [address, node] : mNodeForAddress) { |
Steven Moreland | 3fa3292 | 2022-07-14 18:45:51 +0000 | [diff] [blame] | 309 | ALOGE("- address: %" PRIu64 " %s", address, node.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 310 | } |
| 311 | ALOGE("END DUMP OF RpcState"); |
| 312 | } |
| 313 | |
Steven Moreland | 3fa3292 | 2022-07-14 18:45:51 +0000 | [diff] [blame] | 314 | std::string RpcState::BinderNode::toString() const { |
| 315 | sp<IBinder> strongBinder = this->binder.promote(); |
| 316 | |
| 317 | const char* desc; |
| 318 | if (strongBinder) { |
| 319 | if (strongBinder->remoteBinder()) { |
| 320 | if (strongBinder->remoteBinder()->isRpcBinder()) { |
| 321 | desc = "(rpc binder proxy)"; |
| 322 | } else { |
| 323 | desc = "(binder proxy)"; |
| 324 | } |
| 325 | } else { |
| 326 | desc = "(local binder)"; |
| 327 | } |
| 328 | } else { |
| 329 | desc = "(not promotable)"; |
| 330 | } |
| 331 | |
Tomasz Wasilczyk | 3caae30 | 2023-10-12 20:57:02 +0000 | [diff] [blame] | 332 | std::stringstream ss; |
| 333 | ss << "node{" << intptr_t(this->binder.unsafe_get()) << " times sent: " << this->timesSent |
| 334 | << " times recd: " << this->timesRecd << " type: " << desc << "}"; |
| 335 | return ss.str(); |
Steven Moreland | 3fa3292 | 2022-07-14 18:45:51 +0000 | [diff] [blame] | 336 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 337 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 338 | RpcState::CommandData::CommandData(size_t size) : mSize(size) { |
| 339 | // The maximum size for regular binder is 1MB for all concurrent |
| 340 | // transactions. A very small proportion of transactions are even |
| 341 | // larger than a page, but we need to avoid allocating too much |
| 342 | // data on behalf of an arbitrary client, or we could risk being in |
| 343 | // a position where a single additional allocation could run out of |
| 344 | // memory. |
| 345 | // |
| 346 | // Note, this limit may not reflect the total amount of data allocated for a |
| 347 | // transaction (in some cases, additional fixed size amounts are added), |
| 348 | // though for rough consistency, we should avoid cases where this data type |
| 349 | // is used for multiple dynamic allocations for a single transaction. |
| 350 | constexpr size_t kMaxTransactionAllocation = 100 * 1000; |
| 351 | if (size == 0) return; |
| 352 | if (size > kMaxTransactionAllocation) { |
| 353 | ALOGW("Transaction requested too much data allocation %zu", size); |
| 354 | return; |
| 355 | } |
| 356 | mData.reset(new (std::nothrow) uint8_t[size]); |
| 357 | } |
| 358 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 359 | status_t RpcState::rpcSend( |
| 360 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 361 | const char* what, iovec* iovs, int niovs, |
Sebastian Pickl | 84b7cff | 2023-10-30 08:02:24 +0000 | [diff] [blame^] | 362 | const std::optional<android::base::function_ref<status_t()>>& altPoll, |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 363 | const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 364 | for (int i = 0; i < niovs; i++) { |
Andrei Homescu | 0a69235 | 2022-03-29 06:04:26 +0000 | [diff] [blame] | 365 | LOG_RPC_DETAIL("Sending %s (part %d of %d) on RpcTransport %p: %s", |
| 366 | what, i + 1, niovs, connection->rpcTransport.get(), |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 367 | HexString(iovs[i].iov_base, iovs[i].iov_len).c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 370 | if (status_t status = |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 371 | connection->rpcTransport->interruptableWriteFully(session->mShutdownTrigger.get(), |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 372 | iovs, niovs, altPoll, |
| 373 | ancillaryFds); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 374 | status != OK) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 375 | LOG_RPC_DETAIL("Failed to write %s (%d iovs) on RpcTransport %p, error: %s", what, niovs, |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 376 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 377 | (void)session->shutdownAndWait(false); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 378 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 381 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 384 | status_t RpcState::rpcRec( |
| 385 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 386 | const char* what, iovec* iovs, int niovs, |
| 387 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) { |
| 388 | if (status_t status = |
| 389 | connection->rpcTransport->interruptableReadFully(session->mShutdownTrigger.get(), |
| 390 | iovs, niovs, std::nullopt, |
| 391 | ancillaryFds); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 392 | status != OK) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 393 | LOG_RPC_DETAIL("Failed to read %s (%d iovs) on RpcTransport %p, error: %s", what, niovs, |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 394 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | ae58f43 | 2021-08-05 17:53:16 -0700 | [diff] [blame] | 395 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 396 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 399 | for (int i = 0; i < niovs; i++) { |
Andrei Homescu | 0a69235 | 2022-03-29 06:04:26 +0000 | [diff] [blame] | 400 | LOG_RPC_DETAIL("Received %s (part %d of %d) on RpcTransport %p: %s", |
| 401 | what, i + 1, niovs, connection->rpcTransport.get(), |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 402 | HexString(iovs[i].iov_base, iovs[i].iov_len).c_str()); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 403 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 404 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 407 | bool RpcState::validateProtocolVersion(uint32_t version) { |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame] | 408 | if (version == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL) { |
| 409 | #if defined(__ANDROID__) |
| 410 | char codename[PROPERTY_VALUE_MAX]; |
| 411 | property_get("ro.build.version.codename", codename, ""); |
| 412 | if (!strcmp(codename, "REL")) { |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 413 | ALOGE("Cannot use experimental RPC binder protocol in a release configuration."); |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | #else |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 417 | // TODO(b/305983144) |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame] | 418 | // don't restrict on other platforms, though experimental should |
| 419 | // only really be used for testing, we don't have a good way to see |
| 420 | // what is shipping outside of Android |
| 421 | #endif |
| 422 | } else if (version >= RPC_WIRE_PROTOCOL_VERSION_NEXT) { |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 423 | ALOGE("Cannot use RPC binder protocol version %u which is unknown (current protocol " |
| 424 | "version " |
| 425 | "is %u).", |
| 426 | version, RPC_WIRE_PROTOCOL_VERSION); |
| 427 | return false; |
| 428 | } |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame] | 429 | |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 433 | status_t RpcState::readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection, |
| 434 | const sp<RpcSession>& session, uint32_t* version) { |
| 435 | RpcNewSessionResponse response; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 436 | iovec iov{&response, sizeof(response)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 437 | if (status_t status = rpcRec(connection, session, "new session response", &iov, 1, nullptr); |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 438 | status != OK) { |
| 439 | return status; |
| 440 | } |
| 441 | *version = response.version; |
| 442 | return OK; |
| 443 | } |
| 444 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 445 | status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 446 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 447 | RpcOutgoingConnectionInit init{ |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 448 | .msg = RPC_CONNECTION_INIT_OKAY, |
| 449 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 450 | iovec iov{&init, sizeof(init)}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 451 | return rpcSend(connection, session, "connection init", &iov, 1, std::nullopt); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 454 | status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 455 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 456 | RpcOutgoingConnectionInit init; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 457 | iovec iov{&init, sizeof(init)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 458 | if (status_t status = rpcRec(connection, session, "connection init", &iov, 1, nullptr); |
| 459 | status != OK) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 460 | return status; |
| 461 | |
| 462 | static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); |
| 463 | if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { |
| 464 | ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)), |
| 465 | init.msg); |
| 466 | return BAD_VALUE; |
| 467 | } |
| 468 | return OK; |
| 469 | } |
| 470 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 471 | sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 472 | const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 473 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 474 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 475 | Parcel reply; |
| 476 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 477 | status_t status = |
| 478 | transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_ROOT, data, session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 479 | if (status != OK) { |
| 480 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 481 | return nullptr; |
| 482 | } |
| 483 | |
| 484 | return reply.readStrongBinder(); |
| 485 | } |
| 486 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 487 | status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 488 | const sp<RpcSession>& session, size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 489 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 490 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 491 | Parcel reply; |
| 492 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 493 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, data, |
| 494 | session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 495 | if (status != OK) { |
| 496 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 497 | return status; |
| 498 | } |
| 499 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 500 | int32_t maxThreads; |
| 501 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 502 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 503 | if (maxThreads <= 0) { |
| 504 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 505 | return BAD_VALUE; |
| 506 | } |
| 507 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 508 | *maxThreadsOut = maxThreads; |
| 509 | return OK; |
| 510 | } |
| 511 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 512 | status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 513 | const sp<RpcSession>& session, std::vector<uint8_t>* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 514 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 515 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 516 | Parcel reply; |
| 517 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 518 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_SESSION_ID, data, |
| 519 | session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 520 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 521 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 522 | return status; |
| 523 | } |
| 524 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 525 | return reply.readByteVector(sessionIdOut); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 528 | status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection, |
| 529 | const sp<IBinder>& binder, uint32_t code, const Parcel& data, |
| 530 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 531 | std::string errorMsg; |
| 532 | if (status_t status = validateParcel(session, data, &errorMsg); status != OK) { |
| 533 | ALOGE("Refusing to send RPC on binder %p code %" PRIu32 ": Parcel %p failed validation: %s", |
| 534 | binder.get(), code, &data, errorMsg.c_str()); |
| 535 | return status; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 536 | } |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 537 | uint64_t address; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 538 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 539 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 540 | return transactAddress(connection, address, code, data, session, reply, flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 543 | status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 544 | uint64_t address, uint32_t code, const Parcel& data, |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 545 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 546 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 547 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 548 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 549 | uint64_t asyncNumber = 0; |
| 550 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 551 | if (address != 0) { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 552 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 553 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 554 | auto it = mNodeForAddress.find(address); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 555 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 556 | "Sending transact on unknown address %" PRIu64, address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 557 | |
| 558 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 559 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 560 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 561 | _l.unlock(); |
| 562 | (void)session->shutdownAndWait(false); |
| 563 | return DEAD_OBJECT; |
| 564 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 568 | auto* rpcFields = data.maybeRpcFields(); |
| 569 | LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); |
| 570 | |
| 571 | Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(), |
| 572 | rpcFields->mObjectPositions.size()}; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 573 | |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 574 | uint32_t bodySize; |
| 575 | LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(sizeof(RpcWireTransaction), data.dataSize(), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 576 | &bodySize) || |
| 577 | __builtin_add_overflow(objectTableSpan.byteSize(), bodySize, |
| 578 | &bodySize), |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 579 | "Too much data %zu", data.dataSize()); |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 580 | RpcWireHeader command{ |
| 581 | .command = RPC_COMMAND_TRANSACT, |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 582 | .bodySize = bodySize, |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 583 | }; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 584 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 585 | RpcWireTransaction transaction{ |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 586 | .address = RpcWireAddress::fromRaw(address), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 587 | .code = code, |
| 588 | .flags = flags, |
| 589 | .asyncNumber = asyncNumber, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 590 | // bodySize didn't overflow => this cast is safe |
| 591 | .parcelDataSize = static_cast<uint32_t>(data.dataSize()), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 592 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 593 | |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 594 | // Oneway calls have no sync point, so if many are sent before, whether this |
| 595 | // is a twoway or oneway transaction, they may have filled up the socket. |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 596 | // So, make sure we drain them before polling |
Steven Moreland | da31af6 | 2023-02-25 01:55:58 +0000 | [diff] [blame] | 597 | constexpr size_t kWaitMaxUs = 1000000; |
| 598 | constexpr size_t kWaitLogUs = 10000; |
| 599 | size_t waitUs = 0; |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 600 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 601 | iovec iovs[]{ |
| 602 | {&command, sizeof(RpcWireHeader)}, |
| 603 | {&transaction, sizeof(RpcWireTransaction)}, |
| 604 | {const_cast<uint8_t*>(data.data()), data.dataSize()}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 605 | objectTableSpan.toIovec(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 606 | }; |
Sebastian Pickl | 84b7cff | 2023-10-30 08:02:24 +0000 | [diff] [blame^] | 607 | if (status_t status = rpcSend( |
| 608 | connection, session, "transaction", iovs, arraysize(iovs), |
| 609 | [&] { |
| 610 | if (waitUs > kWaitLogUs) { |
| 611 | ALOGE("Cannot send command, trying to process pending refcounts. Waiting " |
| 612 | "%zuus. Too many oneway calls?", |
| 613 | waitUs); |
| 614 | } |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 615 | |
Sebastian Pickl | 84b7cff | 2023-10-30 08:02:24 +0000 | [diff] [blame^] | 616 | if (waitUs > 0) { |
| 617 | usleep(waitUs); |
| 618 | waitUs = std::min(kWaitMaxUs, waitUs * 2); |
| 619 | } else { |
| 620 | waitUs = 1; |
| 621 | } |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 622 | |
Sebastian Pickl | 84b7cff | 2023-10-30 08:02:24 +0000 | [diff] [blame^] | 623 | return drainCommands(connection, session, CommandType::CONTROL_ONLY); |
| 624 | }, |
| 625 | rpcFields->mFds.get()); |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 626 | status != OK) { |
Steven Moreland | da31af6 | 2023-02-25 01:55:58 +0000 | [diff] [blame] | 627 | // rpcSend calls shutdownAndWait, so all refcounts should be reset. If we ever tolerate |
| 628 | // errors here, then we may need to undo the binder-sent counts for the transaction as |
| 629 | // well as for the binder objects in the Parcel |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 630 | return status; |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 631 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 632 | |
| 633 | if (flags & IBinder::FLAG_ONEWAY) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 634 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on RpcTransport %p", |
| 635 | connection->rpcTransport.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 636 | |
| 637 | // Do not wait on result. |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 638 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 642 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 643 | return waitForReply(connection, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 646 | static void cleanup_reply_data(const uint8_t* data, size_t dataSize, const binder_size_t* objects, |
| 647 | size_t objectsCount) { |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 648 | delete[] const_cast<uint8_t*>(data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 649 | (void)dataSize; |
| 650 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 651 | (void)objectsCount; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 654 | status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 655 | const sp<RpcSession>& session, Parcel* reply) { |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 656 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> ancillaryFds; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 657 | RpcWireHeader command; |
| 658 | while (true) { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 659 | iovec iov{&command, sizeof(command)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 660 | if (status_t status = rpcRec(connection, session, "command header (for reply)", &iov, 1, |
| 661 | enableAncillaryFds(session->getFileDescriptorTransportMode()) |
| 662 | ? &ancillaryFds |
| 663 | : nullptr); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 664 | status != OK) |
| 665 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 666 | |
| 667 | if (command.command == RPC_COMMAND_REPLY) break; |
| 668 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 669 | if (status_t status = processCommand(connection, session, command, CommandType::ANY, |
| 670 | std::move(ancillaryFds)); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 671 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 672 | return status; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 673 | |
| 674 | // Reset to avoid spurious use-after-move warning from clang-tidy. |
| 675 | ancillaryFds = decltype(ancillaryFds)(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 678 | const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value()); |
| 679 | |
| 680 | if (command.bodySize < rpcReplyWireSize) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 681 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 682 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 683 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 684 | return BAD_VALUE; |
| 685 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 686 | |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 687 | RpcWireReply rpcReply; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 688 | memset(&rpcReply, 0, sizeof(RpcWireReply)); // zero because of potential short read |
| 689 | |
| 690 | CommandData data(command.bodySize - rpcReplyWireSize); |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 691 | if (!data.valid()) return NO_MEMORY; |
| 692 | |
| 693 | iovec iovs[]{ |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 694 | {&rpcReply, rpcReplyWireSize}, |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 695 | {data.data(), data.size()}, |
| 696 | }; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 697 | if (status_t status = rpcRec(connection, session, "reply body", iovs, arraysize(iovs), nullptr); |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 698 | status != OK) |
| 699 | return status; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 700 | |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 701 | if (rpcReply.status != OK) return rpcReply.status; |
| 702 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 703 | Span<const uint8_t> parcelSpan = {data.data(), data.size()}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 704 | Span<const uint32_t> objectTableSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 705 | if (session->getProtocolVersion().value() >= |
| 706 | RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) { |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 707 | std::optional<Span<const uint8_t>> objectTableBytes = |
| 708 | parcelSpan.splitOff(rpcReply.parcelDataSize); |
| 709 | if (!objectTableBytes.has_value()) { |
| 710 | ALOGE("Parcel size larger than available bytes: %" PRId32 " vs %zu. Terminating!", |
| 711 | rpcReply.parcelDataSize, parcelSpan.byteSize()); |
| 712 | (void)session->shutdownAndWait(false); |
| 713 | return BAD_VALUE; |
| 714 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 715 | std::optional<Span<const uint32_t>> maybeSpan = |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 716 | objectTableBytes->reinterpret<const uint32_t>(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 717 | if (!maybeSpan.has_value()) { |
| 718 | ALOGE("Bad object table size inferred from RpcWireReply. Saw bodySize=%" PRId32 |
| 719 | " sizeofHeader=%zu parcelSize=%" PRId32 " objectTableBytesSize=%zu. Terminating!", |
| 720 | command.bodySize, rpcReplyWireSize, rpcReply.parcelDataSize, |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 721 | objectTableBytes->size); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 722 | return BAD_VALUE; |
| 723 | } |
| 724 | objectTableSpan = *maybeSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 725 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 726 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 727 | data.release(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 728 | return reply->rpcSetDataReference(session, parcelSpan.data, parcelSpan.size, |
| 729 | objectTableSpan.data, objectTableSpan.size, |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 730 | std::move(ancillaryFds), cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 733 | status_t RpcState::sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection, |
| 734 | const sp<RpcSession>& session, uint64_t addr, |
| 735 | size_t target) { |
| 736 | RpcDecStrong body = { |
| 737 | .address = RpcWireAddress::fromRaw(addr), |
| 738 | }; |
| 739 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 740 | { |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 741 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 742 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 743 | auto it = mNodeForAddress.find(addr); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 744 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 745 | "Sending dec strong on unknown address %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 746 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 747 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd < target, "Can't dec count of %zu to %zu.", |
| 748 | it->second.timesRecd, target); |
| 749 | |
| 750 | // typically this happens when multiple threads send dec refs at the |
| 751 | // same time - the transactions will get combined automatically |
| 752 | if (it->second.timesRecd == target) return OK; |
| 753 | |
| 754 | body.amount = it->second.timesRecd - target; |
| 755 | it->second.timesRecd = target; |
| 756 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 757 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(session, std::move(_l), it), |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 758 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 759 | // LOCK ALREADY RELEASED |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | RpcWireHeader cmd = { |
| 763 | .command = RPC_COMMAND_DEC_STRONG, |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 764 | .bodySize = sizeof(RpcDecStrong), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 765 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 766 | iovec iovs[]{{&cmd, sizeof(cmd)}, {&body, sizeof(body)}}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 767 | return rpcSend(connection, session, "dec ref", iovs, arraysize(iovs), std::nullopt); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 770 | status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
| 771 | const sp<RpcSession>& session, CommandType type) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 772 | LOG_RPC_DETAIL("getAndExecuteCommand on RpcTransport %p", connection->rpcTransport.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 773 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 774 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> ancillaryFds; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 775 | RpcWireHeader command; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 776 | iovec iov{&command, sizeof(command)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 777 | if (status_t status = |
| 778 | rpcRec(connection, session, "command header (for server)", &iov, 1, |
| 779 | enableAncillaryFds(session->getFileDescriptorTransportMode()) ? &ancillaryFds |
| 780 | : nullptr); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 781 | status != OK) |
| 782 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 783 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 784 | return processCommand(connection, session, command, type, std::move(ancillaryFds)); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 787 | status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 788 | const sp<RpcSession>& session, CommandType type) { |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 789 | while (true) { |
Andrei Homescu | 1975aaa | 2022-03-19 02:34:57 +0000 | [diff] [blame] | 790 | status_t status = connection->rpcTransport->pollRead(); |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 791 | if (status == WOULD_BLOCK) break; |
| 792 | if (status != OK) return status; |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 793 | |
| 794 | status = getAndExecuteCommand(connection, session, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 795 | if (status != OK) return status; |
| 796 | } |
| 797 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 800 | status_t RpcState::processCommand( |
| 801 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 802 | const RpcWireHeader& command, CommandType type, |
| 803 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 804 | #ifdef BINDER_WITH_KERNEL_IPC |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 805 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 806 | IPCThreadState::SpGuard spGuard{ |
| 807 | .address = __builtin_frame_address(0), |
Steven Moreland | e42ffd0 | 2022-07-06 21:46:23 +0000 | [diff] [blame] | 808 | .context = "processing binder RPC command (where RpcServer::setPerSessionRootObject is " |
| 809 | "used to distinguish callers)", |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 810 | }; |
| 811 | const IPCThreadState::SpGuard* origGuard; |
| 812 | if (kernelBinderState != nullptr) { |
| 813 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 814 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 815 | |
Tomasz Wasilczyk | d6c8a4c | 2023-10-11 20:41:51 +0000 | [diff] [blame] | 816 | auto guardUnguard = make_scope_guard([&]() { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 817 | if (kernelBinderState != nullptr) { |
| 818 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 819 | } |
Tomasz Wasilczyk | d6c8a4c | 2023-10-11 20:41:51 +0000 | [diff] [blame] | 820 | }); |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 821 | #endif // BINDER_WITH_KERNEL_IPC |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 822 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 823 | switch (command.command) { |
| 824 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 825 | if (type != CommandType::ANY) return BAD_TYPE; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 826 | return processTransact(connection, session, command, std::move(ancillaryFds)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 827 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 828 | return processDecStrong(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // We should always know the version of the opposing side, and since the |
| 832 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 833 | // to understand where the current command ends and the next one begins. We |
| 834 | // 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] | 835 | // to kill us, so ending the session for misbehaving client. |
| 836 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 837 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 838 | return DEAD_OBJECT; |
| 839 | } |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 840 | status_t RpcState::processTransact( |
| 841 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 842 | const RpcWireHeader& command, |
| 843 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 844 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 845 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 846 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 847 | if (!transactionData.valid()) { |
| 848 | return NO_MEMORY; |
| 849 | } |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 850 | iovec iov{transactionData.data(), transactionData.size()}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 851 | if (status_t status = rpcRec(connection, session, "transaction body", &iov, 1, nullptr); |
| 852 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 853 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 854 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 855 | return processTransactInternal(connection, session, std::move(transactionData), |
| 856 | std::move(ancillaryFds)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 859 | static void do_nothing_to_transact_data(const uint8_t* data, size_t dataSize, |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 860 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 861 | (void)data; |
| 862 | (void)dataSize; |
| 863 | (void)objects; |
| 864 | (void)objectsCount; |
| 865 | } |
| 866 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 867 | status_t RpcState::processTransactInternal( |
| 868 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 869 | CommandData transactionData, |
| 870 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 871 | // for 'recursive' calls to this, we have already read and processed the |
| 872 | // binder from the transaction data and taken reference counts into account, |
| 873 | // so it is cached here. |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 874 | sp<IBinder> target; |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 875 | processTransactInternalTailCall: |
| 876 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 877 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 878 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 879 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 880 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 881 | return BAD_VALUE; |
| 882 | } |
| 883 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 884 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 885 | uint64_t addr = RpcWireAddress::toRaw(transaction->address); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 886 | bool oneway = transaction->flags & IBinder::FLAG_ONEWAY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 887 | |
| 888 | status_t replyStatus = OK; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 889 | if (addr != 0) { |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 890 | if (!target) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 891 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 894 | if (replyStatus != OK) { |
| 895 | // do nothing |
| 896 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 897 | // This can happen if the binder is remote in this process, and |
| 898 | // another thread has called the last decStrong on this binder. |
| 899 | // However, for local binders, it indicates a misbehaving client |
| 900 | // (any binder which is being transacted on should be holding a |
| 901 | // strong ref count), so in either case, terminating the |
| 902 | // session. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 903 | ALOGE("While transacting, binder has been deleted at address %" PRIu64 ". Terminating!", |
| 904 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 905 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 906 | replyStatus = BAD_VALUE; |
| 907 | } else if (target->localBinder() == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 908 | ALOGE("Unknown binder address or non-local binder, not address %" PRIu64 |
| 909 | ". Terminating!", |
| 910 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 911 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 912 | replyStatus = BAD_VALUE; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 913 | } else if (oneway) { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 914 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 915 | auto it = mNodeForAddress.find(addr); |
| 916 | if (it->second.binder.promote() != target) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 917 | ALOGE("Binder became invalid during transaction. Bad client? %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 918 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 919 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 920 | // we need to process some other asynchronous transaction |
| 921 | // first |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 922 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 923 | .ref = target, |
| 924 | .data = std::move(transactionData), |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 925 | .ancillaryFds = std::move(ancillaryFds), |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 926 | .asyncNumber = transaction->asyncNumber, |
| 927 | }); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 928 | |
| 929 | size_t numPending = it->second.asyncTodo.size(); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 930 | LOG_RPC_DETAIL("Enqueuing %" PRIu64 " on %" PRIu64 " (%zu pending)", |
| 931 | transaction->asyncNumber, addr, numPending); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 932 | |
| 933 | constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000; |
| 934 | constexpr size_t kArbitraryOnewayCallWarnLevel = 1000; |
| 935 | constexpr size_t kArbitraryOnewayCallWarnPer = 1000; |
| 936 | |
| 937 | if (numPending >= kArbitraryOnewayCallWarnLevel) { |
| 938 | if (numPending >= kArbitraryOnewayCallTerminateLevel) { |
| 939 | ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending); |
| 940 | _l.unlock(); |
| 941 | (void)session->shutdownAndWait(false); |
| 942 | return FAILED_TRANSACTION; |
| 943 | } |
| 944 | |
| 945 | if (numPending % kArbitraryOnewayCallWarnPer == 0) { |
| 946 | ALOGW("Warning: many oneway transactions built up on %p (%zu)", |
| 947 | target.get(), numPending); |
| 948 | } |
| 949 | } |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 950 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 955 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 956 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 957 | |
| 958 | if (replyStatus == OK) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 959 | Span<const uint8_t> parcelSpan = {transaction->data, |
| 960 | transactionData.size() - |
| 961 | offsetof(RpcWireTransaction, data)}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 962 | Span<const uint32_t> objectTableSpan; |
Steven Moreland | 28c8728 | 2023-04-14 21:03:01 +0000 | [diff] [blame] | 963 | if (session->getProtocolVersion().value() >= |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 964 | RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) { |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 965 | std::optional<Span<const uint8_t>> objectTableBytes = |
| 966 | parcelSpan.splitOff(transaction->parcelDataSize); |
| 967 | if (!objectTableBytes.has_value()) { |
| 968 | ALOGE("Parcel size (%" PRId32 ") greater than available bytes (%zu). Terminating!", |
| 969 | transaction->parcelDataSize, parcelSpan.byteSize()); |
| 970 | (void)session->shutdownAndWait(false); |
| 971 | return BAD_VALUE; |
| 972 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 973 | std::optional<Span<const uint32_t>> maybeSpan = |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 974 | objectTableBytes->reinterpret<const uint32_t>(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 975 | if (!maybeSpan.has_value()) { |
| 976 | ALOGE("Bad object table size inferred from RpcWireTransaction. Saw bodySize=%zu " |
| 977 | "sizeofHeader=%zu parcelSize=%" PRId32 |
| 978 | " objectTableBytesSize=%zu. Terminating!", |
| 979 | transactionData.size(), sizeof(RpcWireTransaction), |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 980 | transaction->parcelDataSize, objectTableBytes->size); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 981 | return BAD_VALUE; |
| 982 | } |
| 983 | objectTableSpan = *maybeSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 986 | Parcel data; |
| 987 | // transaction->data is owned by this function. Parcel borrows this data and |
| 988 | // only holds onto it for the duration of this function call. Parcel will be |
| 989 | // deleted before the 'transactionData' object. |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 990 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 991 | replyStatus = |
| 992 | data.rpcSetDataReference(session, parcelSpan.data, parcelSpan.size, |
| 993 | objectTableSpan.data, objectTableSpan.size, |
| 994 | std::move(ancillaryFds), do_nothing_to_transact_data); |
| 995 | // Reset to avoid spurious use-after-move warning from clang-tidy. |
| 996 | ancillaryFds = std::remove_reference<decltype(ancillaryFds)>::type(); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 997 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 998 | if (replyStatus == OK) { |
| 999 | if (target) { |
| 1000 | bool origAllowNested = connection->allowNested; |
| 1001 | connection->allowNested = !oneway; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1002 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1003 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1004 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1005 | connection->allowNested = origAllowNested; |
| 1006 | } else { |
| 1007 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1008 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1009 | switch (transaction->code) { |
| 1010 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 1011 | replyStatus = reply.writeInt32(session->getMaxIncomingThreads()); |
| 1012 | break; |
| 1013 | } |
| 1014 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 1015 | // for client connections, this should always report the value |
| 1016 | // originally returned from the server, so this is asserting |
| 1017 | // that it exists |
| 1018 | replyStatus = reply.writeByteVector(session->mId); |
| 1019 | break; |
| 1020 | } |
| 1021 | default: { |
| 1022 | sp<RpcServer> server = session->server(); |
| 1023 | if (server) { |
| 1024 | switch (transaction->code) { |
| 1025 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 1026 | sp<IBinder> root = session->mSessionSpecificRootObject |
| 1027 | ?: server->getRootObject(); |
| 1028 | replyStatus = reply.writeStrongBinder(root); |
| 1029 | break; |
| 1030 | } |
| 1031 | default: { |
| 1032 | replyStatus = UNKNOWN_TRANSACTION; |
| 1033 | } |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 1034 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 1037 | } |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 1038 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1039 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1044 | if (oneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1045 | if (replyStatus != OK) { |
| 1046 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 1047 | } |
| 1048 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1049 | LOG_RPC_DETAIL("Processed async transaction %" PRIu64 " on %" PRIu64, |
| 1050 | transaction->asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1051 | |
| 1052 | // Check to see if there is another asynchronous transaction to process. |
| 1053 | // This behavior differs from binder behavior, since in the binder |
| 1054 | // driver, asynchronous transactions will be processed after existing |
| 1055 | // pending binder transactions on the queue. The downside of this is |
| 1056 | // that asynchronous transactions can be drowned out by synchronous |
| 1057 | // transactions. However, we have no easy way to queue these |
| 1058 | // transactions after the synchronous transactions we may want to read |
| 1059 | // from the wire. So, in socket binder here, we have the opposite |
| 1060 | // downside: asynchronous transactions may drown out synchronous |
| 1061 | // transactions. |
| 1062 | { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 1063 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1064 | auto it = mNodeForAddress.find(addr); |
| 1065 | // last refcount dropped after this transaction happened |
| 1066 | if (it == mNodeForAddress.end()) return OK; |
| 1067 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1068 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 1069 | _l.unlock(); |
| 1070 | (void)session->shutdownAndWait(false); |
| 1071 | return DEAD_OBJECT; |
| 1072 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1073 | |
Andrei Homescu | ae5f0d1 | 2023-02-25 05:03:31 +0000 | [diff] [blame] | 1074 | if (it->second.asyncTodo.size() != 0 && |
| 1075 | it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1076 | LOG_RPC_DETAIL("Found next async transaction %" PRIu64 " on %" PRIu64, |
| 1077 | it->second.asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1078 | |
| 1079 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1080 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1081 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1082 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 1083 | |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 1084 | // reset up arguments |
| 1085 | transactionData = std::move(todo.data); |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 1086 | ancillaryFds = std::move(todo.ancillaryFds); |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 1087 | LOG_ALWAYS_FATAL_IF(target != todo.ref, |
| 1088 | "async list should be associated with a binder"); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1089 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1090 | it->second.asyncTodo.pop(); |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 1091 | goto processTransactInternalTailCall; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 1094 | |
| 1095 | // done processing all the async commands on this binder that we can, so |
| 1096 | // write decstrongs on the binder |
| 1097 | if (addr != 0 && replyStatus == OK) { |
| 1098 | return flushExcessBinderRefs(session, addr, target); |
| 1099 | } |
| 1100 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1101 | return OK; |
| 1102 | } |
| 1103 | |
Steven Moreland | 6709cf4 | 2021-09-30 15:21:54 -0700 | [diff] [blame] | 1104 | // Binder refs are flushed for oneway calls only after all calls which are |
| 1105 | // built up are executed. Otherwise, they fill up the binder buffer. |
| 1106 | if (addr != 0 && replyStatus == OK) { |
| 1107 | replyStatus = flushExcessBinderRefs(session, addr, target); |
| 1108 | } |
| 1109 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1110 | std::string errorMsg; |
| 1111 | if (status_t status = validateParcel(session, reply, &errorMsg); status != OK) { |
| 1112 | ALOGE("Reply Parcel failed validation: %s", errorMsg.c_str()); |
| 1113 | // Forward the error to the client of the transaction. |
| 1114 | reply.freeData(); |
| 1115 | reply.markForRpc(session); |
| 1116 | replyStatus = status; |
| 1117 | } |
| 1118 | |
| 1119 | auto* rpcFields = reply.maybeRpcFields(); |
| 1120 | LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); |
| 1121 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1122 | const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value()); |
| 1123 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1124 | Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(), |
| 1125 | rpcFields->mObjectPositions.size()}; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1126 | |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 1127 | uint32_t bodySize; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1128 | LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(rpcReplyWireSize, reply.dataSize(), &bodySize) || |
| 1129 | __builtin_add_overflow(objectTableSpan.byteSize(), bodySize, |
| 1130 | &bodySize), |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1131 | "Too much data for reply %zu", reply.dataSize()); |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1132 | RpcWireHeader cmdReply{ |
| 1133 | .command = RPC_COMMAND_REPLY, |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 1134 | .bodySize = bodySize, |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1135 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1136 | RpcWireReply rpcReply{ |
| 1137 | .status = replyStatus, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1138 | // NOTE: Not necessarily written to socket depending on session |
| 1139 | // version. |
| 1140 | // NOTE: bodySize didn't overflow => this cast is safe |
| 1141 | .parcelDataSize = static_cast<uint32_t>(reply.dataSize()), |
| 1142 | .reserved = {0, 0, 0}, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1143 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1144 | iovec iovs[]{ |
| 1145 | {&cmdReply, sizeof(RpcWireHeader)}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1146 | {&rpcReply, rpcReplyWireSize}, |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1147 | {const_cast<uint8_t*>(reply.data()), reply.dataSize()}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1148 | objectTableSpan.toIovec(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1149 | }; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1150 | return rpcSend(connection, session, "reply", iovs, arraysize(iovs), std::nullopt, |
| 1151 | rpcFields->mFds.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 1154 | status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 1155 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1156 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 1157 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1158 | if (command.bodySize != sizeof(RpcDecStrong)) { |
| 1159 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcDecStrong. Terminating!", |
| 1160 | sizeof(RpcDecStrong), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1161 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1162 | return BAD_VALUE; |
| 1163 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1164 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1165 | RpcDecStrong body; |
| 1166 | iovec iov{&body, sizeof(RpcDecStrong)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 1167 | if (status_t status = rpcRec(connection, session, "dec ref body", &iov, 1, nullptr); |
| 1168 | status != OK) |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1169 | return status; |
| 1170 | |
| 1171 | uint64_t addr = RpcWireAddress::toRaw(body.address); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 1172 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1173 | auto it = mNodeForAddress.find(addr); |
| 1174 | if (it == mNodeForAddress.end()) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1175 | ALOGE("Unknown binder address %" PRIu64 " for dec strong.", addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1176 | return OK; |
| 1177 | } |
| 1178 | |
| 1179 | sp<IBinder> target = it->second.binder.promote(); |
| 1180 | if (target == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1181 | ALOGE("While requesting dec strong, binder has been deleted at address %" PRIu64 |
| 1182 | ". Terminating!", |
| 1183 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1184 | _l.unlock(); |
| 1185 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1186 | return BAD_VALUE; |
| 1187 | } |
| 1188 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1189 | if (it->second.timesSent < body.amount) { |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1190 | ALOGE("Record of sending binder %zu times, but requested decStrong for %" PRIu64 " of %u", |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1191 | it->second.timesSent, addr, body.amount); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1192 | return OK; |
| 1193 | } |
| 1194 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1195 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %" PRIu64, |
| 1196 | addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1197 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1198 | LOG_RPC_DETAIL("Processing dec strong of %" PRIu64 " by %u from %zu", addr, body.amount, |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1199 | it->second.timesSent); |
| 1200 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1201 | it->second.timesSent -= body.amount; |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1202 | sp<IBinder> tempHold = tryEraseNode(session, std::move(_l), it); |
| 1203 | // LOCK ALREADY RELEASED |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1204 | tempHold = nullptr; // destructor may make binder calls on this session |
| 1205 | |
| 1206 | return OK; |
| 1207 | } |
| 1208 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1209 | status_t RpcState::validateParcel(const sp<RpcSession>& session, const Parcel& parcel, |
| 1210 | std::string* errorMsg) { |
| 1211 | auto* rpcFields = parcel.maybeRpcFields(); |
| 1212 | if (rpcFields == nullptr) { |
| 1213 | *errorMsg = "Parcel not crafted for RPC call"; |
| 1214 | return BAD_TYPE; |
| 1215 | } |
| 1216 | |
| 1217 | if (rpcFields->mSession != session) { |
| 1218 | *errorMsg = "Parcel's session doesn't match"; |
| 1219 | return BAD_TYPE; |
| 1220 | } |
| 1221 | |
| 1222 | uint32_t protocolVersion = session->getProtocolVersion().value(); |
| 1223 | if (protocolVersion < RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE && |
| 1224 | !rpcFields->mObjectPositions.empty()) { |
Tomasz Wasilczyk | 3caae30 | 2023-10-12 20:57:02 +0000 | [diff] [blame] | 1225 | std::stringstream ss; |
| 1226 | ss << "Parcel has attached objects but the session's protocol version (" << protocolVersion |
| 1227 | << ") is too old, must be at least " |
| 1228 | << RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE; |
| 1229 | *errorMsg = ss.str(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1230 | return BAD_VALUE; |
| 1231 | } |
| 1232 | |
| 1233 | if (rpcFields->mFds && !rpcFields->mFds->empty()) { |
| 1234 | switch (session->getFileDescriptorTransportMode()) { |
| 1235 | case RpcSession::FileDescriptorTransportMode::NONE: |
| 1236 | *errorMsg = |
| 1237 | "Parcel has file descriptors, but no file descriptor transport is enabled"; |
| 1238 | return FDS_NOT_ALLOWED; |
| 1239 | case RpcSession::FileDescriptorTransportMode::UNIX: { |
| 1240 | constexpr size_t kMaxFdsPerMsg = 253; |
| 1241 | if (rpcFields->mFds->size() > kMaxFdsPerMsg) { |
Tomasz Wasilczyk | 3caae30 | 2023-10-12 20:57:02 +0000 | [diff] [blame] | 1242 | std::stringstream ss; |
| 1243 | ss << "Too many file descriptors in Parcel for unix domain socket: " |
| 1244 | << rpcFields->mFds->size() << " (max is " << kMaxFdsPerMsg << ")"; |
| 1245 | *errorMsg = ss.str(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1246 | return BAD_VALUE; |
| 1247 | } |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 1248 | break; |
| 1249 | } |
| 1250 | case RpcSession::FileDescriptorTransportMode::TRUSTY: { |
| 1251 | // Keep this in sync with trusty_ipc.h!!! |
| 1252 | // We could import that file here on Trusty, but it's not |
| 1253 | // available on Android |
| 1254 | constexpr size_t kMaxFdsPerMsg = 8; |
| 1255 | if (rpcFields->mFds->size() > kMaxFdsPerMsg) { |
Tomasz Wasilczyk | 3caae30 | 2023-10-12 20:57:02 +0000 | [diff] [blame] | 1256 | std::stringstream ss; |
| 1257 | ss << "Too many file descriptors in Parcel for Trusty IPC connection: " |
| 1258 | << rpcFields->mFds->size() << " (max is " << kMaxFdsPerMsg << ")"; |
| 1259 | *errorMsg = ss.str(); |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 1260 | return BAD_VALUE; |
| 1261 | } |
| 1262 | break; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return OK; |
| 1268 | } |
| 1269 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1270 | sp<IBinder> RpcState::tryEraseNode(const sp<RpcSession>& session, RpcMutexUniqueLock nodeLock, |
| 1271 | std::map<uint64_t, BinderNode>::iterator& it) { |
| 1272 | bool shouldShutdown = false; |
| 1273 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1274 | sp<IBinder> ref; |
| 1275 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1276 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1277 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1278 | |
| 1279 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 1280 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 1281 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1282 | mNodeForAddress.erase(it); |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (mNodeForAddress.size() == 0) { |
| 1285 | shouldShutdown = true; |
| 1286 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1290 | // If we shutdown, prevent RpcState from being re-used. This prevents another |
| 1291 | // thread from getting the root object again. |
| 1292 | if (shouldShutdown) { |
| 1293 | clear(std::move(nodeLock)); |
| 1294 | } else { |
| 1295 | nodeLock.unlock(); // explicit |
| 1296 | } |
| 1297 | // LOCK IS RELEASED |
| 1298 | |
| 1299 | if (shouldShutdown) { |
| 1300 | ALOGI("RpcState has no binders left, so triggering shutdown..."); |
| 1301 | (void)session->shutdownAndWait(false); |
| 1302 | } |
| 1303 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1304 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1307 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1308 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 1309 | // a single binder |
| 1310 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 1311 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | node->asyncNumber++; |
| 1315 | return true; |
| 1316 | } |
| 1317 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1318 | } // namespace android |