Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #pragma once |
| 18 | |
| 19 | #include <BinderRpcTestClientInfo.h> |
| 20 | #include <BinderRpcTestServerConfig.h> |
| 21 | #include <BinderRpcTestServerInfo.h> |
| 22 | #include <BnBinderRpcCallback.h> |
| 23 | #include <BnBinderRpcSession.h> |
| 24 | #include <BnBinderRpcTest.h> |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 25 | #include <binder/Binder.h> |
| 26 | #include <binder/BpBinder.h> |
| 27 | #include <binder/IPCThreadState.h> |
| 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/RpcServer.h> |
| 30 | #include <binder/RpcSession.h> |
| 31 | #include <binder/RpcThreads.h> |
| 32 | #include <binder/RpcTransport.h> |
| 33 | #include <binder/RpcTransportRaw.h> |
| 34 | #include <unistd.h> |
| 35 | #include <cinttypes> |
| 36 | #include <string> |
| 37 | #include <vector> |
| 38 | |
Tomasz Wasilczyk | c2b71d5 | 2023-11-06 16:32:12 -0800 | [diff] [blame] | 39 | #ifdef __ANDROID__ |
| 40 | #include <android-base/properties.h> |
| 41 | #endif |
| 42 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 43 | #ifndef __TRUSTY__ |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 44 | #include <android/binder_auto_utils.h> |
| 45 | #include <android/binder_libbinder.h> |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 46 | #include <binder/ProcessState.h> |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 47 | #include <binder/RpcTlsTestUtils.h> |
| 48 | #include <binder/RpcTlsUtils.h> |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 49 | #include <binder/RpcTransportTls.h> |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 50 | |
| 51 | #include <signal.h> |
| 52 | |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 53 | #include "../OS.h" // for testing UnixBootstrap clients |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 54 | #include "../RpcSocketAddress.h" // for testing preconnected clients |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 55 | #include "../vm_sockets.h" // for VMADDR_* |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 56 | #endif // __TRUSTY__ |
| 57 | |
| 58 | #include "../BuildFlags.h" |
| 59 | #include "../FdTrigger.h" |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 60 | #include "../FdUtils.h" |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 61 | #include "../RpcState.h" // for debugging |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 62 | #include "FileUtils.h" |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 63 | #include "utils/Errors.h" |
| 64 | |
| 65 | namespace android { |
| 66 | |
Tomasz Wasilczyk | 2265ad9 | 2024-05-01 12:54:46 -0700 | [diff] [blame] | 67 | #ifdef BINDER_NO_KERNEL_IPC_TESTING |
| 68 | constexpr bool kEnableKernelIpcTesting = false; |
| 69 | #else |
| 70 | constexpr bool kEnableKernelIpcTesting = true; |
| 71 | #endif |
| 72 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 73 | constexpr char kLocalInetAddress[] = "127.0.0.1"; |
| 74 | |
| 75 | enum class RpcSecurity { RAW, TLS }; |
| 76 | |
| 77 | static inline std::vector<RpcSecurity> RpcSecurityValues() { |
| 78 | return {RpcSecurity::RAW, RpcSecurity::TLS}; |
| 79 | } |
| 80 | |
Tomasz Wasilczyk | 2265ad9 | 2024-05-01 12:54:46 -0700 | [diff] [blame] | 81 | static inline std::vector<bool> noKernelValues() { |
| 82 | std::vector<bool> values = {true}; |
| 83 | if (kEnableKernelIpcTesting) { |
| 84 | values.push_back(false); |
| 85 | } |
| 86 | return values; |
| 87 | } |
| 88 | |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 89 | static inline bool hasExperimentalRpc() { |
Andrei Homescu | 331c67e | 2024-03-27 23:07:01 +0000 | [diff] [blame] | 90 | #ifdef BINDER_RPC_TO_TRUSTY_TEST |
| 91 | // Trusty services do not support the experimental version, |
| 92 | // so that we can update the prebuilts separately. |
| 93 | // This covers the binderRpcToTrustyTest case on Android. |
| 94 | return false; |
| 95 | #endif |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 96 | #ifdef __ANDROID__ |
| 97 | return base::GetProperty("ro.build.version.codename", "") != "REL"; |
| 98 | #else |
Steven Moreland | 687728e | 2023-10-28 01:07:40 +0000 | [diff] [blame] | 99 | return false; |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 100 | #endif |
| 101 | } |
| 102 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 103 | static inline std::vector<uint32_t> testVersions() { |
| 104 | std::vector<uint32_t> versions; |
| 105 | for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { |
| 106 | versions.push_back(i); |
| 107 | } |
Steven Moreland | 0884c55 | 2023-10-17 18:23:31 +0000 | [diff] [blame] | 108 | if (hasExperimentalRpc()) { |
| 109 | versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); |
| 110 | } |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 111 | return versions; |
| 112 | } |
| 113 | |
| 114 | static inline std::string trustyIpcPort(uint32_t serverVersion) { |
Tomasz Wasilczyk | 68c4208 | 2024-05-20 11:59:35 -0700 | [diff] [blame] | 115 | return "com.android.trusty.binderRpcTestService.V" + std::to_string(serverVersion); |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 118 | enum class SocketType { |
| 119 | PRECONNECTED, |
| 120 | UNIX, |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 121 | UNIX_BOOTSTRAP, |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 122 | UNIX_RAW, |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 123 | VSOCK, |
| 124 | INET, |
Andrei Homescu | 68a5561 | 2022-08-02 01:25:15 +0000 | [diff] [blame] | 125 | TIPC, |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 126 | }; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 127 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 128 | static inline std::string PrintToString(SocketType socketType) { |
| 129 | switch (socketType) { |
| 130 | case SocketType::PRECONNECTED: |
| 131 | return "preconnected_uds"; |
| 132 | case SocketType::UNIX: |
| 133 | return "unix_domain_socket"; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 134 | case SocketType::UNIX_BOOTSTRAP: |
| 135 | return "unix_domain_socket_bootstrap"; |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 136 | case SocketType::UNIX_RAW: |
| 137 | return "raw_uds"; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 138 | case SocketType::VSOCK: |
| 139 | return "vm_socket"; |
| 140 | case SocketType::INET: |
| 141 | return "inet_socket"; |
Andrei Homescu | 68a5561 | 2022-08-02 01:25:15 +0000 | [diff] [blame] | 142 | case SocketType::TIPC: |
| 143 | return "trusty_ipc"; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 144 | default: |
| 145 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 146 | return ""; |
| 147 | } |
| 148 | } |
| 149 | |
Andrei Homescu | 9a9b1b4 | 2022-10-14 01:40:59 +0000 | [diff] [blame] | 150 | static inline size_t epochMillis() { |
| 151 | using std::chrono::duration_cast; |
| 152 | using std::chrono::milliseconds; |
| 153 | using std::chrono::seconds; |
| 154 | using std::chrono::system_clock; |
| 155 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 156 | } |
| 157 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 158 | struct BinderRpcOptions { |
| 159 | size_t numThreads = 1; |
| 160 | size_t numSessions = 1; |
Steven Moreland | 67f8590 | 2023-03-15 01:13:49 +0000 | [diff] [blame] | 161 | // right now, this can be empty, or length numSessions, where each value |
| 162 | // represents the info for the corresponding session, but we should |
| 163 | // probably switch this to be a list of sessions options so that other |
| 164 | // options can all be specified per session |
| 165 | std::vector<size_t> numIncomingConnectionsBySession = {}; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 166 | size_t numOutgoingConnections = SIZE_MAX; |
| 167 | RpcSession::FileDescriptorTransportMode clientFileDescriptorTransportMode = |
| 168 | RpcSession::FileDescriptorTransportMode::NONE; |
| 169 | std::vector<RpcSession::FileDescriptorTransportMode> |
| 170 | serverSupportedFileDescriptorTransportModes = { |
| 171 | RpcSession::FileDescriptorTransportMode::NONE}; |
| 172 | |
| 173 | // If true, connection failures will result in `ProcessSession::sessions` being empty |
| 174 | // instead of a fatal error. |
| 175 | bool allowConnectFailure = false; |
| 176 | }; |
| 177 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 178 | #ifndef __TRUSTY__ |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 179 | static inline void writeString(binder::borrowed_fd fd, std::string_view str) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 180 | uint64_t length = str.length(); |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 181 | LOG_ALWAYS_FATAL_IF(!android::binder::WriteFully(fd, &length, sizeof(length))); |
| 182 | LOG_ALWAYS_FATAL_IF(!android::binder::WriteFully(fd, str.data(), str.length())); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 185 | static inline std::string readString(binder::borrowed_fd fd) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 186 | uint64_t length; |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 187 | LOG_ALWAYS_FATAL_IF(!android::binder::ReadFully(fd, &length, sizeof(length))); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 188 | std::string ret(length, '\0'); |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 189 | LOG_ALWAYS_FATAL_IF(!android::binder::ReadFully(fd, ret.data(), length)); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 190 | return ret; |
| 191 | } |
| 192 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 193 | static inline void writeToFd(binder::borrowed_fd fd, const Parcelable& parcelable) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 194 | Parcel parcel; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 195 | LOG_ALWAYS_FATAL_IF(OK != parcelable.writeToParcel(&parcel)); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 196 | writeString(fd, std::string(reinterpret_cast<const char*>(parcel.data()), parcel.dataSize())); |
| 197 | } |
| 198 | |
| 199 | template <typename T> |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 200 | static inline T readFromFd(binder::borrowed_fd fd) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 201 | std::string data = readString(fd); |
| 202 | Parcel parcel; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 203 | LOG_ALWAYS_FATAL_IF(OK != |
| 204 | parcel.setData(reinterpret_cast<const uint8_t*>(data.data()), data.size())); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 205 | T object; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 206 | LOG_ALWAYS_FATAL_IF(OK != object.readFromParcel(&parcel)); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 207 | return object; |
| 208 | } |
| 209 | |
Andrei Homescu | f30148c | 2023-03-10 00:31:45 +0000 | [diff] [blame] | 210 | static inline std::unique_ptr<RpcTransportCtxFactory> newTlsFactory( |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 211 | RpcSecurity rpcSecurity, std::shared_ptr<RpcCertificateVerifier> verifier = nullptr, |
| 212 | std::unique_ptr<RpcAuth> auth = nullptr) { |
| 213 | switch (rpcSecurity) { |
| 214 | case RpcSecurity::RAW: |
| 215 | return RpcTransportCtxFactoryRaw::make(); |
| 216 | case RpcSecurity::TLS: { |
| 217 | if (verifier == nullptr) { |
| 218 | verifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 219 | } |
| 220 | if (auth == nullptr) { |
| 221 | auth = std::make_unique<RpcAuthSelfSigned>(); |
| 222 | } |
| 223 | return RpcTransportCtxFactoryTls::make(std::move(verifier), std::move(auth)); |
| 224 | } |
| 225 | default: |
Tomasz Wasilczyk | e97f3a8 | 2024-04-30 10:37:32 -0700 | [diff] [blame] | 226 | LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", static_cast<int>(rpcSecurity)); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | // Create an FD that returns `contents` when read. |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 231 | static inline binder::unique_fd mockFileDescriptor(std::string contents) { |
| 232 | binder::unique_fd readFd, writeFd; |
| 233 | LOG_ALWAYS_FATAL_IF(!binder::Pipe(&readFd, &writeFd), "%s", strerror(errno)); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 234 | RpcMaybeThread([writeFd = std::move(writeFd), contents = std::move(contents)]() { |
| 235 | signal(SIGPIPE, SIG_IGN); // ignore possible SIGPIPE from the write |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 236 | if (!android::binder::WriteStringToFd(contents, writeFd)) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 237 | int savedErrno = errno; |
| 238 | LOG_ALWAYS_FATAL_IF(EPIPE != savedErrno, "mockFileDescriptor write failed: %s", |
| 239 | strerror(savedErrno)); |
| 240 | } |
| 241 | }).detach(); |
| 242 | return readFd; |
| 243 | } |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 244 | #endif // __TRUSTY__ |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 245 | |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 246 | // A threadsafe channel where writes block until the value is read. |
| 247 | template <typename T> |
| 248 | class HandoffChannel { |
| 249 | public: |
| 250 | void write(T v) { |
| 251 | { |
| 252 | RpcMutexUniqueLock lock(mMutex); |
| 253 | // Wait for space to send. |
| 254 | mCvEmpty.wait(lock, [&]() { return !mValue.has_value(); }); |
| 255 | mValue.emplace(std::move(v)); |
| 256 | } |
| 257 | mCvFull.notify_all(); |
| 258 | RpcMutexUniqueLock lock(mMutex); |
| 259 | // Wait for it to be taken. |
| 260 | mCvEmpty.wait(lock, [&]() { return !mValue.has_value(); }); |
| 261 | } |
| 262 | |
| 263 | T read() { |
| 264 | RpcMutexUniqueLock lock(mMutex); |
| 265 | if (!mValue.has_value()) { |
| 266 | mCvFull.wait(lock, [&]() { return mValue.has_value(); }); |
| 267 | } |
| 268 | T v = std::move(mValue.value()); |
| 269 | mValue.reset(); |
| 270 | lock.unlock(); |
| 271 | mCvEmpty.notify_all(); |
Tomasz Wasilczyk | e97f3a8 | 2024-04-30 10:37:32 -0700 | [diff] [blame] | 272 | return v; |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private: |
| 276 | RpcMutex mMutex; |
| 277 | RpcConditionVariable mCvEmpty; |
| 278 | RpcConditionVariable mCvFull; |
| 279 | std::optional<T> mValue; |
| 280 | }; |
| 281 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 282 | using android::binder::Status; |
| 283 | |
| 284 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 285 | public: |
| 286 | static std::atomic<int32_t> gNum; |
| 287 | |
| 288 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 289 | Status getName(std::string* name) override { |
| 290 | *name = mName; |
| 291 | return Status::ok(); |
| 292 | } |
| 293 | ~MyBinderRpcSession() { gNum--; } |
| 294 | |
| 295 | private: |
| 296 | std::string mName; |
| 297 | }; |
| 298 | |
| 299 | class MyBinderRpcCallback : public BnBinderRpcCallback { |
| 300 | Status sendCallback(const std::string& value) { |
| 301 | RpcMutexUniqueLock _l(mMutex); |
| 302 | mValues.push_back(value); |
| 303 | _l.unlock(); |
| 304 | mCv.notify_one(); |
| 305 | return Status::ok(); |
| 306 | } |
| 307 | Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } |
| 308 | |
| 309 | public: |
| 310 | RpcMutex mMutex; |
| 311 | RpcConditionVariable mCv; |
| 312 | std::vector<std::string> mValues; |
| 313 | }; |
| 314 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 315 | // Base class for all concrete implementations of MyBinderRpcTest. |
| 316 | // Sub-classes that want to provide a full implementation should derive |
| 317 | // from this class instead of MyBinderRpcTestDefault below so the compiler |
| 318 | // checks that all methods are implemented. |
| 319 | class MyBinderRpcTestBase : public BnBinderRpcTest { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 320 | public: |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 321 | int port = 0; |
| 322 | |
| 323 | Status sendString(const std::string& str) override { |
| 324 | (void)str; |
| 325 | return Status::ok(); |
| 326 | } |
| 327 | Status doubleString(const std::string& str, std::string* strstr) override { |
| 328 | *strstr = str + str; |
| 329 | return Status::ok(); |
| 330 | } |
| 331 | Status getClientPort(int* out) override { |
| 332 | *out = port; |
| 333 | return Status::ok(); |
| 334 | } |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 335 | Status getNullBinder(sp<IBinder>* out) override { |
| 336 | out->clear(); |
| 337 | return Status::ok(); |
| 338 | } |
| 339 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 340 | if (binder == nullptr) { |
| 341 | std::cout << "Received null binder!" << std::endl; |
| 342 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 343 | } |
| 344 | *out = binder->pingBinder(); |
| 345 | return Status::ok(); |
| 346 | } |
| 347 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 348 | *out = binder; |
| 349 | return Status::ok(); |
| 350 | } |
| 351 | static sp<IBinder> mHeldBinder; |
| 352 | Status holdBinder(const sp<IBinder>& binder) override { |
| 353 | mHeldBinder = binder; |
| 354 | return Status::ok(); |
| 355 | } |
| 356 | Status getHeldBinder(sp<IBinder>* held) override { |
| 357 | *held = mHeldBinder; |
| 358 | return Status::ok(); |
| 359 | } |
| 360 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 361 | if (count <= 0) return Status::ok(); |
| 362 | return binder->nestMe(this, count - 1); |
| 363 | } |
| 364 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 365 | static sp<IBinder> binder = new BBinder; |
| 366 | *out = binder; |
| 367 | return Status::ok(); |
| 368 | } |
| 369 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 370 | *out = new MyBinderRpcSession(name); |
| 371 | return Status::ok(); |
| 372 | } |
| 373 | Status getNumOpenSessions(int32_t* out) override { |
| 374 | *out = MyBinderRpcSession::gNum; |
| 375 | return Status::ok(); |
| 376 | } |
| 377 | |
| 378 | RpcMutex blockMutex; |
| 379 | Status lock() override { |
| 380 | blockMutex.lock(); |
| 381 | return Status::ok(); |
| 382 | } |
| 383 | Status unlockInMsAsync(int32_t ms) override { |
| 384 | usleep(ms * 1000); |
| 385 | blockMutex.unlock(); |
| 386 | return Status::ok(); |
| 387 | } |
| 388 | Status lockUnlock() override { |
| 389 | RpcMutexLockGuard _l(blockMutex); |
| 390 | return Status::ok(); |
| 391 | } |
| 392 | |
| 393 | Status sleepMs(int32_t ms) override { |
| 394 | usleep(ms * 1000); |
| 395 | return Status::ok(); |
| 396 | } |
| 397 | |
| 398 | Status sleepMsAsync(int32_t ms) override { |
| 399 | // In-process binder calls are asynchronous, but the call to this method |
| 400 | // is synchronous wrt its client. This in/out-process threading model |
| 401 | // diffentiation is a classic binder leaky abstraction (for better or |
| 402 | // worse) and is preserved here the way binder sockets plugs itself |
| 403 | // into BpBinder, as nothing is changed at the higher levels |
| 404 | // (IInterface) which result in this behavior. |
| 405 | return sleepMs(ms); |
| 406 | } |
| 407 | |
| 408 | Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 409 | const std::string& value) override { |
| 410 | if (callback == nullptr) { |
| 411 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 412 | } |
| 413 | |
| 414 | if (delayed) { |
Tomasz Wasilczyk | e9c93a7 | 2023-12-04 11:53:26 -0800 | [diff] [blame] | 415 | RpcMaybeThread([=, this]() { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 416 | ALOGE("Executing delayed callback: '%s'", value.c_str()); |
| 417 | Status status = doCallback(callback, oneway, false, value); |
| 418 | ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); |
| 419 | }).detach(); |
| 420 | return Status::ok(); |
| 421 | } |
| 422 | |
| 423 | if (oneway) { |
| 424 | return callback->sendOnewayCallback(value); |
| 425 | } |
| 426 | |
| 427 | return callback->sendCallback(value); |
| 428 | } |
| 429 | |
| 430 | Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 431 | const std::string& value) override { |
| 432 | return doCallback(callback, oneway, delayed, value); |
| 433 | } |
| 434 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 435 | protected: |
| 436 | // Generic version of countBinders that works with both |
| 437 | // RpcServer and RpcServerTrusty |
| 438 | template <typename T> |
| 439 | Status countBindersImpl(const wp<T>& server, std::vector<int32_t>* out) { |
| 440 | sp<T> spServer = server.promote(); |
| 441 | if (spServer == nullptr) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 442 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 443 | } |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 444 | out->clear(); |
| 445 | for (auto session : spServer->listSessions()) { |
| 446 | size_t count = session->state()->countBinders(); |
| 447 | out->push_back(count); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 448 | } |
| 449 | return Status::ok(); |
| 450 | } |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 451 | }; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 452 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 453 | // Default implementation of MyBinderRpcTest that can be used as-is |
| 454 | // or derived from by classes that only want to implement a subset of |
| 455 | // the unimplemented methods |
| 456 | class MyBinderRpcTestDefault : public MyBinderRpcTestBase { |
| 457 | public: |
| 458 | Status countBinders(std::vector<int32_t>* /*out*/) override { |
| 459 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 462 | Status die(bool /*cleanup*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } |
| 463 | |
| 464 | Status scheduleShutdown() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } |
| 465 | |
| 466 | Status useKernelBinderCallingId() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } |
| 467 | |
| 468 | Status echoAsFile(const std::string& /*content*/, |
| 469 | android::os::ParcelFileDescriptor* /*out*/) override { |
| 470 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 471 | } |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 472 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 473 | Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& /*files*/, |
| 474 | android::os::ParcelFileDescriptor* /*out*/) override { |
| 475 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Andrei Homescu | 9d8adb1 | 2022-08-02 04:38:30 +0000 | [diff] [blame] | 478 | Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& /*fd*/) override { |
| 479 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
| 480 | } |
| 481 | |
| 482 | Status blockingRecvFd(android::os::ParcelFileDescriptor* /*fd*/) override { |
| 483 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 484 | } |
Frederick Mayle | 9687259 | 2023-03-07 14:56:15 -0800 | [diff] [blame] | 485 | |
| 486 | Status blockingSendIntOneway(int /*n*/) override { |
| 487 | return Status::fromStatusT(UNKNOWN_TRANSACTION); |
| 488 | } |
| 489 | |
| 490 | Status blockingRecvInt(int* /*n*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | } // namespace android |