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