Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RpcState" |
| 18 | |
| 19 | #include "RpcState.h" |
| 20 | |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 21 | #include <android-base/hex.h> |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 22 | #include <android-base/macros.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 23 | #include <android-base/scopeguard.h> |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 27 | #include <binder/RpcServer.h> |
| 28 | |
| 29 | #include "Debug.h" |
| 30 | #include "RpcWireFormat.h" |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 31 | #include "Utils.h" |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 32 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 33 | #include <random> |
| 34 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 35 | #include <inttypes.h> |
| 36 | |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame^] | 37 | #ifdef __ANDROID__ |
| 38 | #include <cutils/properties.h> |
| 39 | #endif |
| 40 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 43 | using base::StringPrintf; |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 44 | |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 45 | #if RPC_FLAKE_PRONE |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 46 | void rpcMaybeWaitToFlake() { |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 47 | [[clang::no_destroy]] static std::random_device r; |
Steven Moreland | 7e2675c | 2022-09-28 23:34:52 +0000 | [diff] [blame] | 48 | [[clang::no_destroy]] static RpcMutex m; |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 49 | unsigned num; |
| 50 | { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 51 | RpcMutexLockGuard lock(m); |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 52 | num = r(); |
| 53 | } |
| 54 | if (num % 10 == 0) usleep(num % 1000); |
| 55 | } |
| 56 | #endif |
| 57 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 58 | static bool enableAncillaryFds(RpcSession::FileDescriptorTransportMode mode) { |
| 59 | switch (mode) { |
| 60 | case RpcSession::FileDescriptorTransportMode::NONE: |
| 61 | return false; |
| 62 | case RpcSession::FileDescriptorTransportMode::UNIX: |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 63 | case RpcSession::FileDescriptorTransportMode::TRUSTY: |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 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 | |
| 332 | return StringPrintf("node{%p times sent: %zu times recd: %zu type: %s}", |
| 333 | this->binder.unsafe_get(), this->timesSent, this->timesRecd, desc); |
| 334 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 335 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 336 | RpcState::CommandData::CommandData(size_t size) : mSize(size) { |
| 337 | // The maximum size for regular binder is 1MB for all concurrent |
| 338 | // transactions. A very small proportion of transactions are even |
| 339 | // larger than a page, but we need to avoid allocating too much |
| 340 | // data on behalf of an arbitrary client, or we could risk being in |
| 341 | // a position where a single additional allocation could run out of |
| 342 | // memory. |
| 343 | // |
| 344 | // Note, this limit may not reflect the total amount of data allocated for a |
| 345 | // transaction (in some cases, additional fixed size amounts are added), |
| 346 | // though for rough consistency, we should avoid cases where this data type |
| 347 | // is used for multiple dynamic allocations for a single transaction. |
| 348 | constexpr size_t kMaxTransactionAllocation = 100 * 1000; |
| 349 | if (size == 0) return; |
| 350 | if (size > kMaxTransactionAllocation) { |
| 351 | ALOGW("Transaction requested too much data allocation %zu", size); |
| 352 | return; |
| 353 | } |
| 354 | mData.reset(new (std::nothrow) uint8_t[size]); |
| 355 | } |
| 356 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 357 | status_t RpcState::rpcSend( |
| 358 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 359 | const char* what, iovec* iovs, int niovs, |
| 360 | const std::optional<android::base::function_ref<status_t()>>& altPoll, |
| 361 | const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 362 | for (int i = 0; i < niovs; i++) { |
Andrei Homescu | 0a69235 | 2022-03-29 06:04:26 +0000 | [diff] [blame] | 363 | LOG_RPC_DETAIL("Sending %s (part %d of %d) on RpcTransport %p: %s", |
| 364 | what, i + 1, niovs, connection->rpcTransport.get(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 365 | android::base::HexString(iovs[i].iov_base, iovs[i].iov_len).c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 368 | if (status_t status = |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 369 | connection->rpcTransport->interruptableWriteFully(session->mShutdownTrigger.get(), |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 370 | iovs, niovs, altPoll, |
| 371 | ancillaryFds); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 372 | status != OK) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 373 | 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] | 374 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 375 | (void)session->shutdownAndWait(false); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 376 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 379 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 382 | status_t RpcState::rpcRec( |
| 383 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 384 | const char* what, iovec* iovs, int niovs, |
| 385 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) { |
| 386 | if (status_t status = |
| 387 | connection->rpcTransport->interruptableReadFully(session->mShutdownTrigger.get(), |
| 388 | iovs, niovs, std::nullopt, |
| 389 | ancillaryFds); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 390 | status != OK) { |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 391 | 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] | 392 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | ae58f43 | 2021-08-05 17:53:16 -0700 | [diff] [blame] | 393 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 394 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame] | 397 | for (int i = 0; i < niovs; i++) { |
Andrei Homescu | 0a69235 | 2022-03-29 06:04:26 +0000 | [diff] [blame] | 398 | LOG_RPC_DETAIL("Received %s (part %d of %d) on RpcTransport %p: %s", |
| 399 | what, i + 1, niovs, connection->rpcTransport.get(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 400 | android::base::HexString(iovs[i].iov_base, iovs[i].iov_len).c_str()); |
| 401 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 402 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 405 | bool RpcState::validateProtocolVersion(uint32_t version) { |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame^] | 406 | if (version == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL) { |
| 407 | #if defined(__ANDROID__) |
| 408 | char codename[PROPERTY_VALUE_MAX]; |
| 409 | property_get("ro.build.version.codename", codename, ""); |
| 410 | if (!strcmp(codename, "REL")) { |
| 411 | ALOGE("Cannot use experimental RPC binder protocol on a release branch."); |
| 412 | return false; |
| 413 | } |
| 414 | #else |
| 415 | // don't restrict on other platforms, though experimental should |
| 416 | // only really be used for testing, we don't have a good way to see |
| 417 | // what is shipping outside of Android |
| 418 | #endif |
| 419 | } else if (version >= RPC_WIRE_PROTOCOL_VERSION_NEXT) { |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 420 | ALOGE("Cannot use RPC binder protocol version %u which is unknown (current protocol " |
| 421 | "version " |
| 422 | "is %u).", |
| 423 | version, RPC_WIRE_PROTOCOL_VERSION); |
| 424 | return false; |
| 425 | } |
Steven Moreland | 09034a9 | 2023-05-31 20:49:11 +0000 | [diff] [blame^] | 426 | |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 430 | status_t RpcState::readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection, |
| 431 | const sp<RpcSession>& session, uint32_t* version) { |
| 432 | RpcNewSessionResponse response; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 433 | iovec iov{&response, sizeof(response)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 434 | 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] | 435 | status != OK) { |
| 436 | return status; |
| 437 | } |
| 438 | *version = response.version; |
| 439 | return OK; |
| 440 | } |
| 441 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 442 | status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 443 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 444 | RpcOutgoingConnectionInit init{ |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 445 | .msg = RPC_CONNECTION_INIT_OKAY, |
| 446 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 447 | iovec iov{&init, sizeof(init)}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 448 | return rpcSend(connection, session, "connection init", &iov, 1, std::nullopt); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 451 | status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 452 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 453 | RpcOutgoingConnectionInit init; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 454 | iovec iov{&init, sizeof(init)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 455 | if (status_t status = rpcRec(connection, session, "connection init", &iov, 1, nullptr); |
| 456 | status != OK) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 457 | return status; |
| 458 | |
| 459 | static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); |
| 460 | if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { |
| 461 | ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)), |
| 462 | init.msg); |
| 463 | return BAD_VALUE; |
| 464 | } |
| 465 | return OK; |
| 466 | } |
| 467 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 468 | sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 469 | const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 470 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 471 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 472 | Parcel reply; |
| 473 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 474 | status_t status = |
| 475 | transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_ROOT, data, session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 476 | if (status != OK) { |
| 477 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 478 | return nullptr; |
| 479 | } |
| 480 | |
| 481 | return reply.readStrongBinder(); |
| 482 | } |
| 483 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 484 | status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 485 | const sp<RpcSession>& session, size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 486 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 487 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 488 | Parcel reply; |
| 489 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 490 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, data, |
| 491 | session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 492 | if (status != OK) { |
| 493 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 494 | return status; |
| 495 | } |
| 496 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 497 | int32_t maxThreads; |
| 498 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 499 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 500 | if (maxThreads <= 0) { |
| 501 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 502 | return BAD_VALUE; |
| 503 | } |
| 504 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 505 | *maxThreadsOut = maxThreads; |
| 506 | return OK; |
| 507 | } |
| 508 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 509 | status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 510 | const sp<RpcSession>& session, std::vector<uint8_t>* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 511 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 512 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 513 | Parcel reply; |
| 514 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 515 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_SESSION_ID, data, |
| 516 | session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 517 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 518 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 519 | return status; |
| 520 | } |
| 521 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 522 | return reply.readByteVector(sessionIdOut); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 525 | status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection, |
| 526 | const sp<IBinder>& binder, uint32_t code, const Parcel& data, |
| 527 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 528 | std::string errorMsg; |
| 529 | if (status_t status = validateParcel(session, data, &errorMsg); status != OK) { |
| 530 | ALOGE("Refusing to send RPC on binder %p code %" PRIu32 ": Parcel %p failed validation: %s", |
| 531 | binder.get(), code, &data, errorMsg.c_str()); |
| 532 | return status; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 533 | } |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 534 | uint64_t address; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 535 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 536 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 537 | return transactAddress(connection, address, code, data, session, reply, flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 540 | status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 541 | uint64_t address, uint32_t code, const Parcel& data, |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 542 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 543 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 544 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 545 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 546 | uint64_t asyncNumber = 0; |
| 547 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 548 | if (address != 0) { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 549 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 550 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 551 | auto it = mNodeForAddress.find(address); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 552 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 553 | "Sending transact on unknown address %" PRIu64, address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 554 | |
| 555 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 556 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 557 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 558 | _l.unlock(); |
| 559 | (void)session->shutdownAndWait(false); |
| 560 | return DEAD_OBJECT; |
| 561 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 565 | auto* rpcFields = data.maybeRpcFields(); |
| 566 | LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); |
| 567 | |
| 568 | Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(), |
| 569 | rpcFields->mObjectPositions.size()}; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 570 | |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 571 | uint32_t bodySize; |
| 572 | LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(sizeof(RpcWireTransaction), data.dataSize(), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 573 | &bodySize) || |
| 574 | __builtin_add_overflow(objectTableSpan.byteSize(), bodySize, |
| 575 | &bodySize), |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 576 | "Too much data %zu", data.dataSize()); |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 577 | RpcWireHeader command{ |
| 578 | .command = RPC_COMMAND_TRANSACT, |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 579 | .bodySize = bodySize, |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 580 | }; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 581 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 582 | RpcWireTransaction transaction{ |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 583 | .address = RpcWireAddress::fromRaw(address), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 584 | .code = code, |
| 585 | .flags = flags, |
| 586 | .asyncNumber = asyncNumber, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 587 | // bodySize didn't overflow => this cast is safe |
| 588 | .parcelDataSize = static_cast<uint32_t>(data.dataSize()), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 589 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 590 | |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 591 | // Oneway calls have no sync point, so if many are sent before, whether this |
| 592 | // 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] | 593 | // So, make sure we drain them before polling |
Steven Moreland | da31af6 | 2023-02-25 01:55:58 +0000 | [diff] [blame] | 594 | constexpr size_t kWaitMaxUs = 1000000; |
| 595 | constexpr size_t kWaitLogUs = 10000; |
| 596 | size_t waitUs = 0; |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 597 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 598 | iovec iovs[]{ |
| 599 | {&command, sizeof(RpcWireHeader)}, |
| 600 | {&transaction, sizeof(RpcWireTransaction)}, |
| 601 | {const_cast<uint8_t*>(data.data()), data.dataSize()}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 602 | objectTableSpan.toIovec(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 603 | }; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 604 | if (status_t status = rpcSend( |
| 605 | connection, session, "transaction", iovs, arraysize(iovs), |
| 606 | [&] { |
| 607 | if (waitUs > kWaitLogUs) { |
| 608 | ALOGE("Cannot send command, trying to process pending refcounts. Waiting " |
| 609 | "%zuus. Too many oneway calls?", |
| 610 | waitUs); |
| 611 | } |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 612 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 613 | if (waitUs > 0) { |
| 614 | usleep(waitUs); |
| 615 | waitUs = std::min(kWaitMaxUs, waitUs * 2); |
| 616 | } else { |
| 617 | waitUs = 1; |
| 618 | } |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 619 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 620 | return drainCommands(connection, session, CommandType::CONTROL_ONLY); |
| 621 | }, |
| 622 | rpcFields->mFds.get()); |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 623 | status != OK) { |
Steven Moreland | da31af6 | 2023-02-25 01:55:58 +0000 | [diff] [blame] | 624 | // rpcSend calls shutdownAndWait, so all refcounts should be reset. If we ever tolerate |
| 625 | // errors here, then we may need to undo the binder-sent counts for the transaction as |
| 626 | // well as for the binder objects in the Parcel |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 627 | return status; |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 628 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 629 | |
| 630 | if (flags & IBinder::FLAG_ONEWAY) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 631 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on RpcTransport %p", |
| 632 | connection->rpcTransport.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 633 | |
| 634 | // Do not wait on result. |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 635 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 639 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 640 | return waitForReply(connection, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 643 | static void cleanup_reply_data(const uint8_t* data, size_t dataSize, const binder_size_t* objects, |
| 644 | size_t objectsCount) { |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 645 | delete[] const_cast<uint8_t*>(data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 646 | (void)dataSize; |
| 647 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 648 | (void)objectsCount; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 651 | status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 652 | const sp<RpcSession>& session, Parcel* reply) { |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 653 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> ancillaryFds; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 654 | RpcWireHeader command; |
| 655 | while (true) { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 656 | iovec iov{&command, sizeof(command)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 657 | if (status_t status = rpcRec(connection, session, "command header (for reply)", &iov, 1, |
| 658 | enableAncillaryFds(session->getFileDescriptorTransportMode()) |
| 659 | ? &ancillaryFds |
| 660 | : nullptr); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 661 | status != OK) |
| 662 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 663 | |
| 664 | if (command.command == RPC_COMMAND_REPLY) break; |
| 665 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 666 | if (status_t status = processCommand(connection, session, command, CommandType::ANY, |
| 667 | std::move(ancillaryFds)); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 668 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 669 | return status; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 670 | |
| 671 | // Reset to avoid spurious use-after-move warning from clang-tidy. |
| 672 | ancillaryFds = decltype(ancillaryFds)(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 675 | const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value()); |
| 676 | |
| 677 | if (command.bodySize < rpcReplyWireSize) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 678 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 679 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 680 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 681 | return BAD_VALUE; |
| 682 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 683 | |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 684 | RpcWireReply rpcReply; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 685 | memset(&rpcReply, 0, sizeof(RpcWireReply)); // zero because of potential short read |
| 686 | |
| 687 | CommandData data(command.bodySize - rpcReplyWireSize); |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 688 | if (!data.valid()) return NO_MEMORY; |
| 689 | |
| 690 | iovec iovs[]{ |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 691 | {&rpcReply, rpcReplyWireSize}, |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 692 | {data.data(), data.size()}, |
| 693 | }; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 694 | 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] | 695 | status != OK) |
| 696 | return status; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 697 | |
Frederick Mayle | 68aa3bc | 2022-06-07 15:51:31 +0000 | [diff] [blame] | 698 | if (rpcReply.status != OK) return rpcReply.status; |
| 699 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 700 | Span<const uint8_t> parcelSpan = {data.data(), data.size()}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 701 | Span<const uint32_t> objectTableSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 702 | if (session->getProtocolVersion().value() >= |
| 703 | RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) { |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 704 | std::optional<Span<const uint8_t>> objectTableBytes = |
| 705 | parcelSpan.splitOff(rpcReply.parcelDataSize); |
| 706 | if (!objectTableBytes.has_value()) { |
| 707 | ALOGE("Parcel size larger than available bytes: %" PRId32 " vs %zu. Terminating!", |
| 708 | rpcReply.parcelDataSize, parcelSpan.byteSize()); |
| 709 | (void)session->shutdownAndWait(false); |
| 710 | return BAD_VALUE; |
| 711 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 712 | std::optional<Span<const uint32_t>> maybeSpan = |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 713 | objectTableBytes->reinterpret<const uint32_t>(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 714 | if (!maybeSpan.has_value()) { |
| 715 | ALOGE("Bad object table size inferred from RpcWireReply. Saw bodySize=%" PRId32 |
| 716 | " sizeofHeader=%zu parcelSize=%" PRId32 " objectTableBytesSize=%zu. Terminating!", |
| 717 | command.bodySize, rpcReplyWireSize, rpcReply.parcelDataSize, |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 718 | objectTableBytes->size); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 719 | return BAD_VALUE; |
| 720 | } |
| 721 | objectTableSpan = *maybeSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 722 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 723 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 724 | data.release(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 725 | return reply->rpcSetDataReference(session, parcelSpan.data, parcelSpan.size, |
| 726 | objectTableSpan.data, objectTableSpan.size, |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 727 | std::move(ancillaryFds), cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 730 | status_t RpcState::sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection, |
| 731 | const sp<RpcSession>& session, uint64_t addr, |
| 732 | size_t target) { |
| 733 | RpcDecStrong body = { |
| 734 | .address = RpcWireAddress::fromRaw(addr), |
| 735 | }; |
| 736 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 737 | { |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 738 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 739 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 740 | auto it = mNodeForAddress.find(addr); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 741 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 742 | "Sending dec strong on unknown address %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 743 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 744 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd < target, "Can't dec count of %zu to %zu.", |
| 745 | it->second.timesRecd, target); |
| 746 | |
| 747 | // typically this happens when multiple threads send dec refs at the |
| 748 | // same time - the transactions will get combined automatically |
| 749 | if (it->second.timesRecd == target) return OK; |
| 750 | |
| 751 | body.amount = it->second.timesRecd - target; |
| 752 | it->second.timesRecd = target; |
| 753 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 754 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(session, std::move(_l), it), |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 755 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 756 | // LOCK ALREADY RELEASED |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | RpcWireHeader cmd = { |
| 760 | .command = RPC_COMMAND_DEC_STRONG, |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 761 | .bodySize = sizeof(RpcDecStrong), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 762 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 763 | iovec iovs[]{{&cmd, sizeof(cmd)}, {&body, sizeof(body)}}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 764 | return rpcSend(connection, session, "dec ref", iovs, arraysize(iovs), std::nullopt); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 767 | status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
| 768 | const sp<RpcSession>& session, CommandType type) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 769 | LOG_RPC_DETAIL("getAndExecuteCommand on RpcTransport %p", connection->rpcTransport.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 770 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 771 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> ancillaryFds; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 772 | RpcWireHeader command; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 773 | iovec iov{&command, sizeof(command)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 774 | if (status_t status = |
| 775 | rpcRec(connection, session, "command header (for server)", &iov, 1, |
| 776 | enableAncillaryFds(session->getFileDescriptorTransportMode()) ? &ancillaryFds |
| 777 | : nullptr); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 778 | status != OK) |
| 779 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 780 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 781 | return processCommand(connection, session, command, type, std::move(ancillaryFds)); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 784 | status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 785 | const sp<RpcSession>& session, CommandType type) { |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 786 | while (true) { |
Andrei Homescu | 1975aaa | 2022-03-19 02:34:57 +0000 | [diff] [blame] | 787 | status_t status = connection->rpcTransport->pollRead(); |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 788 | if (status == WOULD_BLOCK) break; |
| 789 | if (status != OK) return status; |
Andrei Homescu | 5ad71b5 | 2022-03-11 03:49:12 +0000 | [diff] [blame] | 790 | |
| 791 | status = getAndExecuteCommand(connection, session, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 792 | if (status != OK) return status; |
| 793 | } |
| 794 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 797 | status_t RpcState::processCommand( |
| 798 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 799 | const RpcWireHeader& command, CommandType type, |
| 800 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 801 | #ifdef BINDER_WITH_KERNEL_IPC |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 802 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 803 | IPCThreadState::SpGuard spGuard{ |
| 804 | .address = __builtin_frame_address(0), |
Steven Moreland | e42ffd0 | 2022-07-06 21:46:23 +0000 | [diff] [blame] | 805 | .context = "processing binder RPC command (where RpcServer::setPerSessionRootObject is " |
| 806 | "used to distinguish callers)", |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 807 | }; |
| 808 | const IPCThreadState::SpGuard* origGuard; |
| 809 | if (kernelBinderState != nullptr) { |
| 810 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 811 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 812 | |
| 813 | base::ScopeGuard guardUnguard = [&]() { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 814 | if (kernelBinderState != nullptr) { |
| 815 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 816 | } |
| 817 | }; |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 818 | #endif // BINDER_WITH_KERNEL_IPC |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 819 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 820 | switch (command.command) { |
| 821 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 822 | if (type != CommandType::ANY) return BAD_TYPE; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 823 | return processTransact(connection, session, command, std::move(ancillaryFds)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 824 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 825 | return processDecStrong(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | // We should always know the version of the opposing side, and since the |
| 829 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 830 | // to understand where the current command ends and the next one begins. We |
| 831 | // 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] | 832 | // to kill us, so ending the session for misbehaving client. |
| 833 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 834 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 835 | return DEAD_OBJECT; |
| 836 | } |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 837 | status_t RpcState::processTransact( |
| 838 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 839 | const RpcWireHeader& command, |
| 840 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 841 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 842 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 843 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 844 | if (!transactionData.valid()) { |
| 845 | return NO_MEMORY; |
| 846 | } |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 847 | iovec iov{transactionData.data(), transactionData.size()}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 848 | if (status_t status = rpcRec(connection, session, "transaction body", &iov, 1, nullptr); |
| 849 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 850 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 851 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 852 | return processTransactInternal(connection, session, std::move(transactionData), |
| 853 | std::move(ancillaryFds)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Frederick Mayle | 53b6ffe | 2022-07-15 20:14:01 +0000 | [diff] [blame] | 856 | 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] | 857 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 858 | (void)data; |
| 859 | (void)dataSize; |
| 860 | (void)objects; |
| 861 | (void)objectsCount; |
| 862 | } |
| 863 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 864 | status_t RpcState::processTransactInternal( |
| 865 | const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session, |
| 866 | CommandData transactionData, |
| 867 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds) { |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 868 | // for 'recursive' calls to this, we have already read and processed the |
| 869 | // binder from the transaction data and taken reference counts into account, |
| 870 | // so it is cached here. |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 871 | sp<IBinder> target; |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 872 | processTransactInternalTailCall: |
| 873 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 874 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 875 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 876 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 877 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 878 | return BAD_VALUE; |
| 879 | } |
| 880 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 881 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 882 | uint64_t addr = RpcWireAddress::toRaw(transaction->address); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 883 | bool oneway = transaction->flags & IBinder::FLAG_ONEWAY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 884 | |
| 885 | status_t replyStatus = OK; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 886 | if (addr != 0) { |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 887 | if (!target) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 888 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 891 | if (replyStatus != OK) { |
| 892 | // do nothing |
| 893 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 894 | // This can happen if the binder is remote in this process, and |
| 895 | // another thread has called the last decStrong on this binder. |
| 896 | // However, for local binders, it indicates a misbehaving client |
| 897 | // (any binder which is being transacted on should be holding a |
| 898 | // strong ref count), so in either case, terminating the |
| 899 | // session. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 900 | ALOGE("While transacting, binder has been deleted at address %" PRIu64 ". Terminating!", |
| 901 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 902 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 903 | replyStatus = BAD_VALUE; |
| 904 | } else if (target->localBinder() == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 905 | ALOGE("Unknown binder address or non-local binder, not address %" PRIu64 |
| 906 | ". Terminating!", |
| 907 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 908 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 909 | replyStatus = BAD_VALUE; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 910 | } else if (oneway) { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 911 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 912 | auto it = mNodeForAddress.find(addr); |
| 913 | if (it->second.binder.promote() != target) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 914 | ALOGE("Binder became invalid during transaction. Bad client? %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 915 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 916 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 917 | // we need to process some other asynchronous transaction |
| 918 | // first |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 919 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 920 | .ref = target, |
| 921 | .data = std::move(transactionData), |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 922 | .ancillaryFds = std::move(ancillaryFds), |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 923 | .asyncNumber = transaction->asyncNumber, |
| 924 | }); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 925 | |
| 926 | size_t numPending = it->second.asyncTodo.size(); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 927 | LOG_RPC_DETAIL("Enqueuing %" PRIu64 " on %" PRIu64 " (%zu pending)", |
| 928 | transaction->asyncNumber, addr, numPending); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 929 | |
| 930 | constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000; |
| 931 | constexpr size_t kArbitraryOnewayCallWarnLevel = 1000; |
| 932 | constexpr size_t kArbitraryOnewayCallWarnPer = 1000; |
| 933 | |
| 934 | if (numPending >= kArbitraryOnewayCallWarnLevel) { |
| 935 | if (numPending >= kArbitraryOnewayCallTerminateLevel) { |
| 936 | ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending); |
| 937 | _l.unlock(); |
| 938 | (void)session->shutdownAndWait(false); |
| 939 | return FAILED_TRANSACTION; |
| 940 | } |
| 941 | |
| 942 | if (numPending % kArbitraryOnewayCallWarnPer == 0) { |
| 943 | ALOGW("Warning: many oneway transactions built up on %p (%zu)", |
| 944 | target.get(), numPending); |
| 945 | } |
| 946 | } |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 947 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 952 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 953 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 954 | |
| 955 | if (replyStatus == OK) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 956 | Span<const uint8_t> parcelSpan = {transaction->data, |
| 957 | transactionData.size() - |
| 958 | offsetof(RpcWireTransaction, data)}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 959 | Span<const uint32_t> objectTableSpan; |
Steven Moreland | 28c8728 | 2023-04-14 21:03:01 +0000 | [diff] [blame] | 960 | if (session->getProtocolVersion().value() >= |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 961 | RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) { |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 962 | std::optional<Span<const uint8_t>> objectTableBytes = |
| 963 | parcelSpan.splitOff(transaction->parcelDataSize); |
| 964 | if (!objectTableBytes.has_value()) { |
| 965 | ALOGE("Parcel size (%" PRId32 ") greater than available bytes (%zu). Terminating!", |
| 966 | transaction->parcelDataSize, parcelSpan.byteSize()); |
| 967 | (void)session->shutdownAndWait(false); |
| 968 | return BAD_VALUE; |
| 969 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 970 | std::optional<Span<const uint32_t>> maybeSpan = |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 971 | objectTableBytes->reinterpret<const uint32_t>(); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 972 | if (!maybeSpan.has_value()) { |
| 973 | ALOGE("Bad object table size inferred from RpcWireTransaction. Saw bodySize=%zu " |
| 974 | "sizeofHeader=%zu parcelSize=%" PRId32 |
| 975 | " objectTableBytesSize=%zu. Terminating!", |
| 976 | transactionData.size(), sizeof(RpcWireTransaction), |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 977 | transaction->parcelDataSize, objectTableBytes->size); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 978 | return BAD_VALUE; |
| 979 | } |
| 980 | objectTableSpan = *maybeSpan; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 983 | Parcel data; |
| 984 | // transaction->data is owned by this function. Parcel borrows this data and |
| 985 | // only holds onto it for the duration of this function call. Parcel will be |
| 986 | // deleted before the 'transactionData' object. |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 987 | |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 988 | replyStatus = |
| 989 | data.rpcSetDataReference(session, parcelSpan.data, parcelSpan.size, |
| 990 | objectTableSpan.data, objectTableSpan.size, |
| 991 | std::move(ancillaryFds), do_nothing_to_transact_data); |
| 992 | // Reset to avoid spurious use-after-move warning from clang-tidy. |
| 993 | ancillaryFds = std::remove_reference<decltype(ancillaryFds)>::type(); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 994 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 995 | if (replyStatus == OK) { |
| 996 | if (target) { |
| 997 | bool origAllowNested = connection->allowNested; |
| 998 | connection->allowNested = !oneway; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 999 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1000 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1001 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1002 | connection->allowNested = origAllowNested; |
| 1003 | } else { |
| 1004 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1005 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1006 | switch (transaction->code) { |
| 1007 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 1008 | replyStatus = reply.writeInt32(session->getMaxIncomingThreads()); |
| 1009 | break; |
| 1010 | } |
| 1011 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 1012 | // for client connections, this should always report the value |
| 1013 | // originally returned from the server, so this is asserting |
| 1014 | // that it exists |
| 1015 | replyStatus = reply.writeByteVector(session->mId); |
| 1016 | break; |
| 1017 | } |
| 1018 | default: { |
| 1019 | sp<RpcServer> server = session->server(); |
| 1020 | if (server) { |
| 1021 | switch (transaction->code) { |
| 1022 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 1023 | sp<IBinder> root = session->mSessionSpecificRootObject |
| 1024 | ?: server->getRootObject(); |
| 1025 | replyStatus = reply.writeStrongBinder(root); |
| 1026 | break; |
| 1027 | } |
| 1028 | default: { |
| 1029 | replyStatus = UNKNOWN_TRANSACTION; |
| 1030 | } |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 1031 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1032 | } else { |
| 1033 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 1034 | } |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 1035 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1036 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1041 | if (oneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1042 | if (replyStatus != OK) { |
| 1043 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 1044 | } |
| 1045 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1046 | LOG_RPC_DETAIL("Processed async transaction %" PRIu64 " on %" PRIu64, |
| 1047 | transaction->asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Check to see if there is another asynchronous transaction to process. |
| 1050 | // This behavior differs from binder behavior, since in the binder |
| 1051 | // driver, asynchronous transactions will be processed after existing |
| 1052 | // pending binder transactions on the queue. The downside of this is |
| 1053 | // that asynchronous transactions can be drowned out by synchronous |
| 1054 | // transactions. However, we have no easy way to queue these |
| 1055 | // transactions after the synchronous transactions we may want to read |
| 1056 | // from the wire. So, in socket binder here, we have the opposite |
| 1057 | // downside: asynchronous transactions may drown out synchronous |
| 1058 | // transactions. |
| 1059 | { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 1060 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1061 | auto it = mNodeForAddress.find(addr); |
| 1062 | // last refcount dropped after this transaction happened |
| 1063 | if (it == mNodeForAddress.end()) return OK; |
| 1064 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1065 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 1066 | _l.unlock(); |
| 1067 | (void)session->shutdownAndWait(false); |
| 1068 | return DEAD_OBJECT; |
| 1069 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1070 | |
Andrei Homescu | ae5f0d1 | 2023-02-25 05:03:31 +0000 | [diff] [blame] | 1071 | if (it->second.asyncTodo.size() != 0 && |
| 1072 | it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1073 | LOG_RPC_DETAIL("Found next async transaction %" PRIu64 " on %" PRIu64, |
| 1074 | it->second.asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1075 | |
| 1076 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1077 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1078 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1079 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 1080 | |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 1081 | // reset up arguments |
| 1082 | transactionData = std::move(todo.data); |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 1083 | ancillaryFds = std::move(todo.ancillaryFds); |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 1084 | LOG_ALWAYS_FATAL_IF(target != todo.ref, |
| 1085 | "async list should be associated with a binder"); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1086 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1087 | it->second.asyncTodo.pop(); |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 1088 | goto processTransactInternalTailCall; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 1091 | |
| 1092 | // done processing all the async commands on this binder that we can, so |
| 1093 | // write decstrongs on the binder |
| 1094 | if (addr != 0 && replyStatus == OK) { |
| 1095 | return flushExcessBinderRefs(session, addr, target); |
| 1096 | } |
| 1097 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1098 | return OK; |
| 1099 | } |
| 1100 | |
Steven Moreland | 6709cf4 | 2021-09-30 15:21:54 -0700 | [diff] [blame] | 1101 | // Binder refs are flushed for oneway calls only after all calls which are |
| 1102 | // built up are executed. Otherwise, they fill up the binder buffer. |
| 1103 | if (addr != 0 && replyStatus == OK) { |
| 1104 | replyStatus = flushExcessBinderRefs(session, addr, target); |
| 1105 | } |
| 1106 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1107 | std::string errorMsg; |
| 1108 | if (status_t status = validateParcel(session, reply, &errorMsg); status != OK) { |
| 1109 | ALOGE("Reply Parcel failed validation: %s", errorMsg.c_str()); |
| 1110 | // Forward the error to the client of the transaction. |
| 1111 | reply.freeData(); |
| 1112 | reply.markForRpc(session); |
| 1113 | replyStatus = status; |
| 1114 | } |
| 1115 | |
| 1116 | auto* rpcFields = reply.maybeRpcFields(); |
| 1117 | LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); |
| 1118 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1119 | const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value()); |
| 1120 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1121 | Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(), |
| 1122 | rpcFields->mObjectPositions.size()}; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1123 | |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 1124 | uint32_t bodySize; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1125 | LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(rpcReplyWireSize, reply.dataSize(), &bodySize) || |
| 1126 | __builtin_add_overflow(objectTableSpan.byteSize(), bodySize, |
| 1127 | &bodySize), |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1128 | "Too much data for reply %zu", reply.dataSize()); |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1129 | RpcWireHeader cmdReply{ |
| 1130 | .command = RPC_COMMAND_REPLY, |
Frederick Mayle | 778c090 | 2022-05-27 01:14:57 +0000 | [diff] [blame] | 1131 | .bodySize = bodySize, |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 1132 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1133 | RpcWireReply rpcReply{ |
| 1134 | .status = replyStatus, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1135 | // NOTE: Not necessarily written to socket depending on session |
| 1136 | // version. |
| 1137 | // NOTE: bodySize didn't overflow => this cast is safe |
| 1138 | .parcelDataSize = static_cast<uint32_t>(reply.dataSize()), |
| 1139 | .reserved = {0, 0, 0}, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1140 | }; |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1141 | iovec iovs[]{ |
| 1142 | {&cmdReply, sizeof(RpcWireHeader)}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1143 | {&rpcReply, rpcReplyWireSize}, |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1144 | {const_cast<uint8_t*>(reply.data()), reply.dataSize()}, |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1145 | objectTableSpan.toIovec(), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1146 | }; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1147 | return rpcSend(connection, session, "reply", iovs, arraysize(iovs), std::nullopt, |
| 1148 | rpcFields->mFds.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 1151 | status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 1152 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1153 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 1154 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1155 | if (command.bodySize != sizeof(RpcDecStrong)) { |
| 1156 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcDecStrong. Terminating!", |
| 1157 | sizeof(RpcDecStrong), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1158 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1159 | return BAD_VALUE; |
| 1160 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1161 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1162 | RpcDecStrong body; |
| 1163 | iovec iov{&body, sizeof(RpcDecStrong)}; |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 1164 | if (status_t status = rpcRec(connection, session, "dec ref body", &iov, 1, nullptr); |
| 1165 | status != OK) |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1166 | return status; |
| 1167 | |
| 1168 | uint64_t addr = RpcWireAddress::toRaw(body.address); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 1169 | RpcMutexUniqueLock _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1170 | auto it = mNodeForAddress.find(addr); |
| 1171 | if (it == mNodeForAddress.end()) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1172 | ALOGE("Unknown binder address %" PRIu64 " for dec strong.", addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1173 | return OK; |
| 1174 | } |
| 1175 | |
| 1176 | sp<IBinder> target = it->second.binder.promote(); |
| 1177 | if (target == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1178 | ALOGE("While requesting dec strong, binder has been deleted at address %" PRIu64 |
| 1179 | ". Terminating!", |
| 1180 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1181 | _l.unlock(); |
| 1182 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1183 | return BAD_VALUE; |
| 1184 | } |
| 1185 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1186 | if (it->second.timesSent < body.amount) { |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1187 | 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] | 1188 | it->second.timesSent, addr, body.amount); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1189 | return OK; |
| 1190 | } |
| 1191 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1192 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %" PRIu64, |
| 1193 | addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1194 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1195 | 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] | 1196 | it->second.timesSent); |
| 1197 | |
Frederick Mayle | b86cda4 | 2022-06-09 23:17:45 +0000 | [diff] [blame] | 1198 | it->second.timesSent -= body.amount; |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1199 | sp<IBinder> tempHold = tryEraseNode(session, std::move(_l), it); |
| 1200 | // LOCK ALREADY RELEASED |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1201 | tempHold = nullptr; // destructor may make binder calls on this session |
| 1202 | |
| 1203 | return OK; |
| 1204 | } |
| 1205 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1206 | status_t RpcState::validateParcel(const sp<RpcSession>& session, const Parcel& parcel, |
| 1207 | std::string* errorMsg) { |
| 1208 | auto* rpcFields = parcel.maybeRpcFields(); |
| 1209 | if (rpcFields == nullptr) { |
| 1210 | *errorMsg = "Parcel not crafted for RPC call"; |
| 1211 | return BAD_TYPE; |
| 1212 | } |
| 1213 | |
| 1214 | if (rpcFields->mSession != session) { |
| 1215 | *errorMsg = "Parcel's session doesn't match"; |
| 1216 | return BAD_TYPE; |
| 1217 | } |
| 1218 | |
| 1219 | uint32_t protocolVersion = session->getProtocolVersion().value(); |
| 1220 | if (protocolVersion < RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE && |
| 1221 | !rpcFields->mObjectPositions.empty()) { |
| 1222 | *errorMsg = StringPrintf("Parcel has attached objects but the session's protocol version " |
| 1223 | "(%" PRIu32 ") is too old, must be at least %" PRIu32, |
| 1224 | protocolVersion, |
| 1225 | RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE); |
| 1226 | return BAD_VALUE; |
| 1227 | } |
| 1228 | |
| 1229 | if (rpcFields->mFds && !rpcFields->mFds->empty()) { |
| 1230 | switch (session->getFileDescriptorTransportMode()) { |
| 1231 | case RpcSession::FileDescriptorTransportMode::NONE: |
| 1232 | *errorMsg = |
| 1233 | "Parcel has file descriptors, but no file descriptor transport is enabled"; |
| 1234 | return FDS_NOT_ALLOWED; |
| 1235 | case RpcSession::FileDescriptorTransportMode::UNIX: { |
| 1236 | constexpr size_t kMaxFdsPerMsg = 253; |
| 1237 | if (rpcFields->mFds->size() > kMaxFdsPerMsg) { |
| 1238 | *errorMsg = StringPrintf("Too many file descriptors in Parcel for unix " |
| 1239 | "domain socket: %zu (max is %zu)", |
| 1240 | rpcFields->mFds->size(), kMaxFdsPerMsg); |
| 1241 | return BAD_VALUE; |
| 1242 | } |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | case RpcSession::FileDescriptorTransportMode::TRUSTY: { |
| 1246 | // Keep this in sync with trusty_ipc.h!!! |
| 1247 | // We could import that file here on Trusty, but it's not |
| 1248 | // available on Android |
| 1249 | constexpr size_t kMaxFdsPerMsg = 8; |
| 1250 | if (rpcFields->mFds->size() > kMaxFdsPerMsg) { |
| 1251 | *errorMsg = StringPrintf("Too many file descriptors in Parcel for Trusty " |
| 1252 | "IPC connection: %zu (max is %zu)", |
| 1253 | rpcFields->mFds->size(), kMaxFdsPerMsg); |
| 1254 | return BAD_VALUE; |
| 1255 | } |
| 1256 | break; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | return OK; |
| 1262 | } |
| 1263 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1264 | sp<IBinder> RpcState::tryEraseNode(const sp<RpcSession>& session, RpcMutexUniqueLock nodeLock, |
| 1265 | std::map<uint64_t, BinderNode>::iterator& it) { |
| 1266 | bool shouldShutdown = false; |
| 1267 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1268 | sp<IBinder> ref; |
| 1269 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1270 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1271 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 1274 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 1275 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1276 | mNodeForAddress.erase(it); |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (mNodeForAddress.size() == 0) { |
| 1279 | shouldShutdown = true; |
| 1280 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 1284 | // If we shutdown, prevent RpcState from being re-used. This prevents another |
| 1285 | // thread from getting the root object again. |
| 1286 | if (shouldShutdown) { |
| 1287 | clear(std::move(nodeLock)); |
| 1288 | } else { |
| 1289 | nodeLock.unlock(); // explicit |
| 1290 | } |
| 1291 | // LOCK IS RELEASED |
| 1292 | |
| 1293 | if (shouldShutdown) { |
| 1294 | ALOGI("RpcState has no binders left, so triggering shutdown..."); |
| 1295 | (void)session->shutdownAndWait(false); |
| 1296 | } |
| 1297 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1298 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1301 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1302 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 1303 | // a single binder |
| 1304 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 1305 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1306 | return false; |
| 1307 | } |
| 1308 | node->asyncNumber++; |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1312 | } // namespace android |