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> |
| 25 | #include <aidl/IBinderRpcTest.h> |
| 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/properties.h> |
| 29 | #include <android/binder_auto_utils.h> |
| 30 | #include <android/binder_libbinder.h> |
| 31 | #include <binder/Binder.h> |
| 32 | #include <binder/BpBinder.h> |
| 33 | #include <binder/IPCThreadState.h> |
| 34 | #include <binder/IServiceManager.h> |
| 35 | #include <binder/ProcessState.h> |
| 36 | #include <binder/RpcServer.h> |
| 37 | #include <binder/RpcSession.h> |
| 38 | #include <binder/RpcThreads.h> |
| 39 | #include <binder/RpcTlsTestUtils.h> |
| 40 | #include <binder/RpcTlsUtils.h> |
| 41 | #include <binder/RpcTransport.h> |
| 42 | #include <binder/RpcTransportRaw.h> |
| 43 | #include <binder/RpcTransportTls.h> |
| 44 | #include <unistd.h> |
| 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | #include <signal.h> |
| 49 | |
| 50 | #include "../BuildFlags.h" |
| 51 | #include "../FdTrigger.h" |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 52 | #include "../OS.h" // for testing UnixBootstrap clients |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 53 | #include "../RpcSocketAddress.h" // for testing preconnected clients |
| 54 | #include "../RpcState.h" // for debugging |
| 55 | #include "../vm_sockets.h" // for VMADDR_* |
| 56 | #include "utils/Errors.h" |
| 57 | |
| 58 | namespace android { |
| 59 | |
| 60 | constexpr char kLocalInetAddress[] = "127.0.0.1"; |
| 61 | |
| 62 | enum class RpcSecurity { RAW, TLS }; |
| 63 | |
| 64 | static inline std::vector<RpcSecurity> RpcSecurityValues() { |
| 65 | return {RpcSecurity::RAW, RpcSecurity::TLS}; |
| 66 | } |
| 67 | |
| 68 | enum class SocketType { |
| 69 | PRECONNECTED, |
| 70 | UNIX, |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 71 | UNIX_BOOTSTRAP, |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 72 | VSOCK, |
| 73 | INET, |
| 74 | }; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 75 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 76 | static inline std::string PrintToString(SocketType socketType) { |
| 77 | switch (socketType) { |
| 78 | case SocketType::PRECONNECTED: |
| 79 | return "preconnected_uds"; |
| 80 | case SocketType::UNIX: |
| 81 | return "unix_domain_socket"; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 82 | case SocketType::UNIX_BOOTSTRAP: |
| 83 | return "unix_domain_socket_bootstrap"; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 84 | case SocketType::VSOCK: |
| 85 | return "vm_socket"; |
| 86 | case SocketType::INET: |
| 87 | return "inet_socket"; |
| 88 | default: |
| 89 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 90 | return ""; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | struct BinderRpcOptions { |
| 95 | size_t numThreads = 1; |
| 96 | size_t numSessions = 1; |
| 97 | size_t numIncomingConnections = 0; |
| 98 | size_t numOutgoingConnections = SIZE_MAX; |
| 99 | RpcSession::FileDescriptorTransportMode clientFileDescriptorTransportMode = |
| 100 | RpcSession::FileDescriptorTransportMode::NONE; |
| 101 | std::vector<RpcSession::FileDescriptorTransportMode> |
| 102 | serverSupportedFileDescriptorTransportModes = { |
| 103 | RpcSession::FileDescriptorTransportMode::NONE}; |
| 104 | |
| 105 | // If true, connection failures will result in `ProcessSession::sessions` being empty |
| 106 | // instead of a fatal error. |
| 107 | bool allowConnectFailure = false; |
| 108 | }; |
| 109 | |
| 110 | static inline void writeString(android::base::borrowed_fd fd, std::string_view str) { |
| 111 | uint64_t length = str.length(); |
| 112 | CHECK(android::base::WriteFully(fd, &length, sizeof(length))); |
| 113 | CHECK(android::base::WriteFully(fd, str.data(), str.length())); |
| 114 | } |
| 115 | |
| 116 | static inline std::string readString(android::base::borrowed_fd fd) { |
| 117 | uint64_t length; |
| 118 | CHECK(android::base::ReadFully(fd, &length, sizeof(length))); |
| 119 | std::string ret(length, '\0'); |
| 120 | CHECK(android::base::ReadFully(fd, ret.data(), length)); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static inline void writeToFd(android::base::borrowed_fd fd, const Parcelable& parcelable) { |
| 125 | Parcel parcel; |
| 126 | CHECK_EQ(OK, parcelable.writeToParcel(&parcel)); |
| 127 | writeString(fd, std::string(reinterpret_cast<const char*>(parcel.data()), parcel.dataSize())); |
| 128 | } |
| 129 | |
| 130 | template <typename T> |
| 131 | static inline T readFromFd(android::base::borrowed_fd fd) { |
| 132 | std::string data = readString(fd); |
| 133 | Parcel parcel; |
| 134 | CHECK_EQ(OK, parcel.setData(reinterpret_cast<const uint8_t*>(data.data()), data.size())); |
| 135 | T object; |
| 136 | CHECK_EQ(OK, object.readFromParcel(&parcel)); |
| 137 | return object; |
| 138 | } |
| 139 | |
| 140 | static inline std::unique_ptr<RpcTransportCtxFactory> newFactory( |
| 141 | RpcSecurity rpcSecurity, std::shared_ptr<RpcCertificateVerifier> verifier = nullptr, |
| 142 | std::unique_ptr<RpcAuth> auth = nullptr) { |
| 143 | switch (rpcSecurity) { |
| 144 | case RpcSecurity::RAW: |
| 145 | return RpcTransportCtxFactoryRaw::make(); |
| 146 | case RpcSecurity::TLS: { |
| 147 | if (verifier == nullptr) { |
| 148 | verifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 149 | } |
| 150 | if (auth == nullptr) { |
| 151 | auth = std::make_unique<RpcAuthSelfSigned>(); |
| 152 | } |
| 153 | return RpcTransportCtxFactoryTls::make(std::move(verifier), std::move(auth)); |
| 154 | } |
| 155 | default: |
| 156 | LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Create an FD that returns `contents` when read. |
| 161 | static inline base::unique_fd mockFileDescriptor(std::string contents) { |
| 162 | android::base::unique_fd readFd, writeFd; |
| 163 | CHECK(android::base::Pipe(&readFd, &writeFd)) << strerror(errno); |
| 164 | RpcMaybeThread([writeFd = std::move(writeFd), contents = std::move(contents)]() { |
| 165 | signal(SIGPIPE, SIG_IGN); // ignore possible SIGPIPE from the write |
| 166 | if (!WriteStringToFd(contents, writeFd)) { |
| 167 | int savedErrno = errno; |
| 168 | LOG_ALWAYS_FATAL_IF(EPIPE != savedErrno, "mockFileDescriptor write failed: %s", |
| 169 | strerror(savedErrno)); |
| 170 | } |
| 171 | }).detach(); |
| 172 | return readFd; |
| 173 | } |
| 174 | |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 175 | // A threadsafe channel where writes block until the value is read. |
| 176 | template <typename T> |
| 177 | class HandoffChannel { |
| 178 | public: |
| 179 | void write(T v) { |
| 180 | { |
| 181 | RpcMutexUniqueLock lock(mMutex); |
| 182 | // Wait for space to send. |
| 183 | mCvEmpty.wait(lock, [&]() { return !mValue.has_value(); }); |
| 184 | mValue.emplace(std::move(v)); |
| 185 | } |
| 186 | mCvFull.notify_all(); |
| 187 | RpcMutexUniqueLock lock(mMutex); |
| 188 | // Wait for it to be taken. |
| 189 | mCvEmpty.wait(lock, [&]() { return !mValue.has_value(); }); |
| 190 | } |
| 191 | |
| 192 | T read() { |
| 193 | RpcMutexUniqueLock lock(mMutex); |
| 194 | if (!mValue.has_value()) { |
| 195 | mCvFull.wait(lock, [&]() { return mValue.has_value(); }); |
| 196 | } |
| 197 | T v = std::move(mValue.value()); |
| 198 | mValue.reset(); |
| 199 | lock.unlock(); |
| 200 | mCvEmpty.notify_all(); |
| 201 | return std::move(v); |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | RpcMutex mMutex; |
| 206 | RpcConditionVariable mCvEmpty; |
| 207 | RpcConditionVariable mCvFull; |
| 208 | std::optional<T> mValue; |
| 209 | }; |
| 210 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 211 | using android::binder::Status; |
| 212 | |
| 213 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 214 | public: |
| 215 | static std::atomic<int32_t> gNum; |
| 216 | |
| 217 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 218 | Status getName(std::string* name) override { |
| 219 | *name = mName; |
| 220 | return Status::ok(); |
| 221 | } |
| 222 | ~MyBinderRpcSession() { gNum--; } |
| 223 | |
| 224 | private: |
| 225 | std::string mName; |
| 226 | }; |
| 227 | |
| 228 | class MyBinderRpcCallback : public BnBinderRpcCallback { |
| 229 | Status sendCallback(const std::string& value) { |
| 230 | RpcMutexUniqueLock _l(mMutex); |
| 231 | mValues.push_back(value); |
| 232 | _l.unlock(); |
| 233 | mCv.notify_one(); |
| 234 | return Status::ok(); |
| 235 | } |
| 236 | Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } |
| 237 | |
| 238 | public: |
| 239 | RpcMutex mMutex; |
| 240 | RpcConditionVariable mCv; |
| 241 | std::vector<std::string> mValues; |
| 242 | }; |
| 243 | |
| 244 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 245 | public: |
| 246 | wp<RpcServer> server; |
| 247 | int port = 0; |
| 248 | |
| 249 | Status sendString(const std::string& str) override { |
| 250 | (void)str; |
| 251 | return Status::ok(); |
| 252 | } |
| 253 | Status doubleString(const std::string& str, std::string* strstr) override { |
| 254 | *strstr = str + str; |
| 255 | return Status::ok(); |
| 256 | } |
| 257 | Status getClientPort(int* out) override { |
| 258 | *out = port; |
| 259 | return Status::ok(); |
| 260 | } |
| 261 | Status countBinders(std::vector<int32_t>* out) override { |
| 262 | sp<RpcServer> spServer = server.promote(); |
| 263 | if (spServer == nullptr) { |
| 264 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 265 | } |
| 266 | out->clear(); |
| 267 | for (auto session : spServer->listSessions()) { |
| 268 | size_t count = session->state()->countBinders(); |
| 269 | out->push_back(count); |
| 270 | } |
| 271 | return Status::ok(); |
| 272 | } |
| 273 | Status getNullBinder(sp<IBinder>* out) override { |
| 274 | out->clear(); |
| 275 | return Status::ok(); |
| 276 | } |
| 277 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 278 | if (binder == nullptr) { |
| 279 | std::cout << "Received null binder!" << std::endl; |
| 280 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 281 | } |
| 282 | *out = binder->pingBinder(); |
| 283 | return Status::ok(); |
| 284 | } |
| 285 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 286 | *out = binder; |
| 287 | return Status::ok(); |
| 288 | } |
| 289 | static sp<IBinder> mHeldBinder; |
| 290 | Status holdBinder(const sp<IBinder>& binder) override { |
| 291 | mHeldBinder = binder; |
| 292 | return Status::ok(); |
| 293 | } |
| 294 | Status getHeldBinder(sp<IBinder>* held) override { |
| 295 | *held = mHeldBinder; |
| 296 | return Status::ok(); |
| 297 | } |
| 298 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 299 | if (count <= 0) return Status::ok(); |
| 300 | return binder->nestMe(this, count - 1); |
| 301 | } |
| 302 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 303 | static sp<IBinder> binder = new BBinder; |
| 304 | *out = binder; |
| 305 | return Status::ok(); |
| 306 | } |
| 307 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 308 | *out = new MyBinderRpcSession(name); |
| 309 | return Status::ok(); |
| 310 | } |
| 311 | Status getNumOpenSessions(int32_t* out) override { |
| 312 | *out = MyBinderRpcSession::gNum; |
| 313 | return Status::ok(); |
| 314 | } |
| 315 | |
| 316 | RpcMutex blockMutex; |
| 317 | Status lock() override { |
| 318 | blockMutex.lock(); |
| 319 | return Status::ok(); |
| 320 | } |
| 321 | Status unlockInMsAsync(int32_t ms) override { |
| 322 | usleep(ms * 1000); |
| 323 | blockMutex.unlock(); |
| 324 | return Status::ok(); |
| 325 | } |
| 326 | Status lockUnlock() override { |
| 327 | RpcMutexLockGuard _l(blockMutex); |
| 328 | return Status::ok(); |
| 329 | } |
| 330 | |
| 331 | Status sleepMs(int32_t ms) override { |
| 332 | usleep(ms * 1000); |
| 333 | return Status::ok(); |
| 334 | } |
| 335 | |
| 336 | Status sleepMsAsync(int32_t ms) override { |
| 337 | // In-process binder calls are asynchronous, but the call to this method |
| 338 | // is synchronous wrt its client. This in/out-process threading model |
| 339 | // diffentiation is a classic binder leaky abstraction (for better or |
| 340 | // worse) and is preserved here the way binder sockets plugs itself |
| 341 | // into BpBinder, as nothing is changed at the higher levels |
| 342 | // (IInterface) which result in this behavior. |
| 343 | return sleepMs(ms); |
| 344 | } |
| 345 | |
| 346 | Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 347 | const std::string& value) override { |
| 348 | if (callback == nullptr) { |
| 349 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 350 | } |
| 351 | |
| 352 | if (delayed) { |
| 353 | RpcMaybeThread([=]() { |
| 354 | ALOGE("Executing delayed callback: '%s'", value.c_str()); |
| 355 | Status status = doCallback(callback, oneway, false, value); |
| 356 | ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); |
| 357 | }).detach(); |
| 358 | return Status::ok(); |
| 359 | } |
| 360 | |
| 361 | if (oneway) { |
| 362 | return callback->sendOnewayCallback(value); |
| 363 | } |
| 364 | |
| 365 | return callback->sendCallback(value); |
| 366 | } |
| 367 | |
| 368 | Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 369 | const std::string& value) override { |
| 370 | return doCallback(callback, oneway, delayed, value); |
| 371 | } |
| 372 | |
| 373 | Status die(bool cleanup) override { |
| 374 | if (cleanup) { |
| 375 | exit(1); |
| 376 | } else { |
| 377 | _exit(1); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | Status scheduleShutdown() override { |
| 382 | sp<RpcServer> strongServer = server.promote(); |
| 383 | if (strongServer == nullptr) { |
| 384 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 385 | } |
| 386 | RpcMaybeThread([=] { |
| 387 | LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); |
| 388 | }).detach(); |
| 389 | return Status::ok(); |
| 390 | } |
| 391 | |
| 392 | Status useKernelBinderCallingId() override { |
| 393 | // this is WRONG! It does not make sense when using RPC binder, and |
| 394 | // because it is SO wrong, and so much code calls this, it should abort! |
| 395 | |
| 396 | if constexpr (kEnableKernelIpc) { |
| 397 | (void)IPCThreadState::self()->getCallingPid(); |
| 398 | } |
| 399 | return Status::ok(); |
| 400 | } |
| 401 | |
| 402 | Status echoAsFile(const std::string& content, android::os::ParcelFileDescriptor* out) override { |
| 403 | out->reset(mockFileDescriptor(content)); |
| 404 | return Status::ok(); |
| 405 | } |
| 406 | |
| 407 | Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& files, |
| 408 | android::os::ParcelFileDescriptor* out) override { |
| 409 | std::string acc; |
| 410 | for (const auto& file : files) { |
| 411 | std::string result; |
| 412 | CHECK(android::base::ReadFdToString(file.get(), &result)); |
| 413 | acc.append(result); |
| 414 | } |
| 415 | out->reset(mockFileDescriptor(acc)); |
| 416 | return Status::ok(); |
| 417 | } |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 418 | |
| 419 | HandoffChannel<android::base::unique_fd> mFdChannel; |
| 420 | |
| 421 | Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& fd) override { |
| 422 | mFdChannel.write(android::base::unique_fd(fcntl(fd.get(), F_DUPFD_CLOEXEC, 0))); |
| 423 | return Status::ok(); |
| 424 | } |
| 425 | |
| 426 | Status blockingRecvFd(android::os::ParcelFileDescriptor* fd) override { |
| 427 | fd->reset(mFdChannel.read()); |
| 428 | return Status::ok(); |
| 429 | } |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | } // namespace android |