Loading libs/binder/Android.bp +117 −57 Original line number Diff line number Diff line Loading @@ -70,21 +70,9 @@ filegroup { ], } cc_library { name: "libbinder", version_script: "libbinder.map", // for vndbinder vendor_available: true, vndk: { enabled: true, }, recovery_available: true, double_loadable: true, cc_defaults { name: "libbinder_defaults", host_supported: true, // TODO(b/153609531): remove when no longer needed. native_bridge_supported: true, // TODO(b/31559095): get headers from bionic on host include_dirs: [ Loading @@ -92,73 +80,32 @@ cc_library { "bionic/libc/kernel/uapi/", ], // libbinder does not offer a stable wire protocol. // if a second copy of it is installed, then it may break after security // or dessert updates. Instead, apex users should use libbinder_ndk. apex_available: [ "//apex_available:platform", ], srcs: [ "Binder.cpp", "BpBinder.cpp", "BufferedTextOutput.cpp", "Debug.cpp", "FdTrigger.cpp", "IInterface.cpp", "IMemory.cpp", "IPCThreadState.cpp", "IResultReceiver.cpp", "IServiceManager.cpp", "IShellCallback.cpp", "LazyServiceRegistrar.cpp", "MemoryBase.cpp", "MemoryDealer.cpp", "MemoryHeapBase.cpp", "Parcel.cpp", "ParcelableHolder.cpp", "ParcelFileDescriptor.cpp", "PersistableBundle.cpp", "ProcessState.cpp", "RpcSession.cpp", "RpcServer.cpp", "RpcState.cpp", "RpcTransportRaw.cpp", "Static.cpp", "Stability.cpp", "Status.cpp", "TextOutput.cpp", "Utils.cpp", ":libbinder_aidl", ":libbinder_device_interface_sources", ], target: { android: { // NOT static to keep the wire protocol unfrozen static: { enabled: false, }, }, vendor: { exclude_srcs: [ ":libbinder_device_interface_sources", ], }, darwin: { enabled: false, }, host: { srcs: [ "ServiceManagerHost.cpp", "UtilsHost.cpp", ], }, recovery: { exclude_header_libs: [ "libandroid_runtime_vm_headers", ], }, }, aidl: { Loading @@ -166,7 +113,6 @@ cc_library { }, cflags: [ "-Wall", "-Wextra", "-Wextra-semi", "-Werror", Loading @@ -174,7 +120,6 @@ cc_library { "-Wreorder-init-list", "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION", "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION", "-DBINDER_WITH_KERNEL_IPC", ], product_variables: { binder32bit: { Loading Loading @@ -230,10 +175,125 @@ cc_library { "performance*", "portability*", ], } cc_defaults { name: "libbinder_kernel_defaults", srcs: [ "BufferedTextOutput.cpp", "IPCThreadState.cpp", "IServiceManager.cpp", "ProcessState.cpp", "Static.cpp", ":libbinder_aidl", ":libbinder_device_interface_sources", ], target: { vendor: { exclude_srcs: [ ":libbinder_device_interface_sources", ], }, host: { srcs: [ "ServiceManagerHost.cpp", ], }, }, cflags: [ "-DBINDER_WITH_KERNEL_IPC", ], } cc_library { name: "libbinder", defaults: [ "libbinder_defaults", "libbinder_kernel_defaults", ], version_script: "libbinder.map", // for vndbinder vendor_available: true, vndk: { enabled: true, }, recovery_available: true, double_loadable: true, // TODO(b/153609531): remove when no longer needed. native_bridge_supported: true, // libbinder does not offer a stable wire protocol. // if a second copy of it is installed, then it may break after security // or dessert updates. Instead, apex users should use libbinder_ndk. apex_available: [ "//apex_available:platform", ], srcs: [ "IMemory.cpp", "LazyServiceRegistrar.cpp", "MemoryBase.cpp", "MemoryDealer.cpp", "MemoryHeapBase.cpp", "ParcelableHolder.cpp", "PersistableBundle.cpp", ], target: { android: { // NOT static to keep the wire protocol unfrozen static: { enabled: false, }, }, darwin: { enabled: false, }, recovery: { exclude_header_libs: [ "libandroid_runtime_vm_headers", ], }, }, afdo: true, } cc_library_static { name: "libbinder_rpc_no_kernel", defaults: ["libbinder_defaults"], visibility: [ ":__subpackages__", ], } cc_library_static { name: "libbinder_rpc_single_threaded", defaults: [ "libbinder_defaults", "libbinder_kernel_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], visibility: [ ":__subpackages__", ], } cc_library_static { name: "libbinder_rpc_single_threaded_no_kernel", defaults: ["libbinder_defaults"], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], visibility: [ ":__subpackages__", ], } cc_defaults { name: "libbinder_tls_shared_deps", shared_libs: [ Loading libs/binder/TEST_MAPPING +9 −0 Original line number Diff line number Diff line Loading @@ -84,6 +84,15 @@ "presubmit-large": [ { "name": "binderRpcTest" }, { "name": "binderRpcTestNoKernel" }, { "name": "binderRpcTestSingleThreaded" }, { "name": "binderRpcTestSingleThreadedNoKernel" } ], "hwasan-presubmit": [ Loading libs/binder/tests/Android.bp +85 −10 Original line number Diff line number Diff line Loading @@ -167,7 +167,6 @@ cc_library_static { "libbinder_tls_shared_deps", ], shared_libs: [ "libbinder", "libbase", "liblog", ], Loading @@ -185,8 +184,8 @@ cc_library_static { ], } cc_test { name: "binderRpcTest", cc_defaults { name: "binderRpcTest_defaults", host_supported: true, target: { darwin: { Loading @@ -198,12 +197,34 @@ cc_test { }, defaults: [ "binder_test_defaults", "libbinder_tls_shared_deps", ], srcs: [ "binderRpcTest.cpp", ], static_libs: [ "libbinder_tls_static", "libbinder_tls_test_utils", "binderRpcTestIface-cpp", "binderRpcTestIface-ndk", ], test_suites: ["general-tests"], require_root: true, } cc_test { name: "binderRpcTest", defaults: [ "binderRpcTest_defaults", "libbinder_tls_shared_deps", ], cflags: [ "-DBINDER_WITH_KERNEL_IPC", ], shared_libs: [ "libbinder", "libbinder_ndk", Loading @@ -212,14 +233,68 @@ cc_test { "libcutils", "liblog", ], } cc_defaults { name: "binderRpcTest_static_defaults", shared_libs: [ "libutils", // libcrypto_static is not visible to this module "libcrypto", ], static_libs: [ "libbinder_tls_static", "libbinder_tls_test_utils", "binderRpcTestIface-cpp", "binderRpcTestIface-ndk", "libbase", "libcutils", "liblog", "libssl", ], cflags: [ // Disable tests that require shared libraries, // e.g., libbinder.so or libbinder_ndk.so "-DBINDER_TEST_NO_SHARED_LIBS", ], } cc_test { name: "binderRpcTestNoKernel", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], static_libs: [ "libbinder_rpc_no_kernel", ], } cc_test { name: "binderRpcTestSingleThreaded", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", "-DBINDER_WITH_KERNEL_IPC", ], static_libs: [ "libbinder_rpc_single_threaded", ], } cc_test { name: "binderRpcTestSingleThreadedNoKernel", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], static_libs: [ "libbinder_rpc_single_threaded_no_kernel", ], test_suites: ["general-tests"], require_root: true, } cc_test { Loading libs/binder/tests/binderRpcTest.cpp +99 −22 Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <binder/ProcessState.h> #include <binder/RpcServer.h> #include <binder/RpcSession.h> #include <binder/RpcThreads.h> #include <binder/RpcTlsTestUtils.h> #include <binder/RpcTlsUtils.h> #include <binder/RpcTransport.h> Loading @@ -51,6 +52,7 @@ #include <sys/socket.h> #include <unistd.h> #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../RpcSocketAddress.h" // for testing preconnected clients #include "../RpcState.h" // for debugging Loading @@ -65,6 +67,12 @@ using testing::AssertionSuccess; namespace android { #ifdef BINDER_TEST_NO_SHARED_LIBS constexpr bool kEnableSharedLibs = false; #else constexpr bool kEnableSharedLibs = true; #endif static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT || RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); const char* kLocalInetAddress = "127.0.0.1"; Loading Loading @@ -99,7 +107,7 @@ static inline std::unique_ptr<RpcTransportCtxFactory> newFactory( static base::unique_fd mockFileDescriptor(std::string contents) { android::base::unique_fd readFd, writeFd; CHECK(android::base::Pipe(&readFd, &writeFd)) << strerror(errno); std::thread([writeFd = std::move(writeFd), contents = std::move(contents)]() { RpcMaybeThread([writeFd = std::move(writeFd), contents = std::move(contents)]() { signal(SIGPIPE, SIG_IGN); // ignore possible SIGPIPE from the write if (!WriteStringToFd(contents, writeFd)) { int savedErrno = errno; Loading Loading @@ -177,7 +185,7 @@ std::atomic<int32_t> MyBinderRpcSession::gNum; class MyBinderRpcCallback : public BnBinderRpcCallback { Status sendCallback(const std::string& value) { std::unique_lock _l(mMutex); RpcMutexUniqueLock _l(mMutex); mValues.push_back(value); _l.unlock(); mCv.notify_one(); Loading @@ -186,8 +194,8 @@ class MyBinderRpcCallback : public BnBinderRpcCallback { Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } public: std::mutex mMutex; std::condition_variable mCv; RpcMutex mMutex; RpcConditionVariable mCv; std::vector<std::string> mValues; }; Loading Loading @@ -263,7 +271,7 @@ public: return Status::ok(); } std::mutex blockMutex; RpcMutex blockMutex; Status lock() override { blockMutex.lock(); return Status::ok(); Loading @@ -274,7 +282,7 @@ public: return Status::ok(); } Status lockUnlock() override { std::lock_guard<std::mutex> _l(blockMutex); RpcMutexLockGuard _l(blockMutex); return Status::ok(); } Loading @@ -300,7 +308,7 @@ public: } if (delayed) { std::thread([=]() { RpcMaybeThread([=]() { ALOGE("Executing delayed callback: '%s'", value.c_str()); Status status = doCallback(callback, oneway, false, value); ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); Loading Loading @@ -333,7 +341,7 @@ public: if (strongServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } std::thread([=] { RpcMaybeThread([=] { LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); }).detach(); return Status::ok(); Loading @@ -343,7 +351,9 @@ public: // this is WRONG! It does not make sense when using RPC binder, and // because it is SO wrong, and so much code calls this, it should abort! if constexpr (kEnableKernelIpc) { (void)IPCThreadState::self()->getCallingPid(); } return Status::ok(); } Loading Loading @@ -796,6 +806,18 @@ public: size_t sleepMs = 500); }; // Test fixture for tests that start multiple threads. // This includes tests with one thread but multiple sessions, // since a server uses one thread per session. class BinderRpcThreads : public BinderRpc { public: void SetUp() override { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } } }; TEST_P(BinderRpc, Ping) { auto proc = createRpcTestSocketServerProcess({}); ASSERT_NE(proc.rootBinder, nullptr); Loading @@ -808,7 +830,7 @@ TEST_P(BinderRpc, GetInterfaceDescriptor) { EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); } TEST_P(BinderRpc, MultipleSessions) { TEST_P(BinderRpcThreads, MultipleSessions) { auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5}); for (auto session : proc.proc.sessions) { ASSERT_NE(nullptr, session.root); Loading @@ -816,7 +838,7 @@ TEST_P(BinderRpc, MultipleSessions) { } } TEST_P(BinderRpc, SeparateRootObject) { TEST_P(BinderRpcThreads, SeparateRootObject) { SocketType type = std::get<0>(GetParam()); if (type == SocketType::PRECONNECTED || type == SocketType::UNIX) { // we can't get port numbers for unix sockets Loading Loading @@ -999,7 +1021,7 @@ TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); } TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { TEST_P(BinderRpcThreads, CannotMixBindersBetweenTwoSessionsToTheSameServer) { auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2}); sp<IBinder> outBinder; Loading @@ -1009,6 +1031,11 @@ TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { } TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } auto proc = createRpcTestSocketServerProcess({}); sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); Loading @@ -1018,6 +1045,11 @@ TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { } TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } auto proc = createRpcTestSocketServerProcess({}); // for historical reasons, IServiceManager interface only returns the Loading Loading @@ -1145,7 +1177,7 @@ size_t epochMillis() { return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); } TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { TEST_P(BinderRpcThreads, ThreadPoolGreaterThanEqualRequested) { constexpr size_t kNumThreads = 10; auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); Loading Loading @@ -1196,14 +1228,14 @@ void BinderRpc::testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t num EXPECT_LE(epochMsAfter, epochMsBefore + 3 * sleepMs); } TEST_P(BinderRpc, ThreadPoolOverSaturated) { TEST_P(BinderRpcThreads, ThreadPoolOverSaturated) { constexpr size_t kNumThreads = 10; constexpr size_t kNumCalls = kNumThreads + 3; auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); testThreadPoolOverSaturated(proc.rootIface, kNumCalls); } TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { TEST_P(BinderRpcThreads, ThreadPoolLimitOutgoing) { constexpr size_t kNumThreads = 20; constexpr size_t kNumOutgoingConnections = 10; constexpr size_t kNumCalls = kNumOutgoingConnections + 3; Loading @@ -1212,7 +1244,7 @@ TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { testThreadPoolOverSaturated(proc.rootIface, kNumCalls); } TEST_P(BinderRpc, ThreadingStressTest) { TEST_P(BinderRpcThreads, ThreadingStressTest) { constexpr size_t kNumClientThreads = 10; constexpr size_t kNumServerThreads = 10; constexpr size_t kNumCalls = 100; Loading Loading @@ -1241,7 +1273,7 @@ static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& ifa for (auto& t : threads) t.join(); } TEST_P(BinderRpc, OnewayStressTest) { TEST_P(BinderRpcThreads, OnewayStressTest) { constexpr size_t kNumClientThreads = 10; constexpr size_t kNumServerThreads = 10; constexpr size_t kNumCalls = 1000; Loading Loading @@ -1276,7 +1308,7 @@ TEST_P(BinderRpc, OnewayCallDoesNotWait) { EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); } TEST_P(BinderRpc, OnewayCallQueueing) { TEST_P(BinderRpcThreads, OnewayCallQueueing) { constexpr size_t kNumSleeps = 10; constexpr size_t kNumExtraServerThreads = 4; constexpr size_t kSleepMs = 50; Loading Loading @@ -1305,7 +1337,7 @@ TEST_P(BinderRpc, OnewayCallQueueing) { saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface); } TEST_P(BinderRpc, OnewayCallExhaustion) { TEST_P(BinderRpcThreads, OnewayCallExhaustion) { constexpr size_t kNumClients = 2; constexpr size_t kTooLongMs = 1000; Loading Loading @@ -1351,8 +1383,15 @@ TEST_P(BinderRpc, Callbacks) { for (bool callIsOneway : {true, false}) { for (bool callbackIsOneway : {true, false}) { for (bool delayed : {true, false}) { if (!kEnableRpcThreads && (callIsOneway || callbackIsOneway || delayed)) { // we have no incoming connections to receive the callback continue; } auto proc = createRpcTestSocketServerProcess( {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); {.numThreads = 1, .numSessions = 1, .numIncomingConnections = kEnableRpcThreads ? 1 : 0}); auto cb = sp<MyBinderRpcCallback>::make(); if (callIsOneway) { Loading @@ -1368,7 +1407,7 @@ TEST_P(BinderRpc, Callbacks) { // the callback will be processed on another thread. if (callIsOneway || callbackIsOneway || delayed) { using std::literals::chrono_literals::operator""s; std::unique_lock<std::mutex> _l(cb->mMutex); RpcMutexUniqueLock _l(cb->mMutex); cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); }); } Loading Loading @@ -1434,6 +1473,11 @@ TEST_P(BinderRpc, Die) { } TEST_P(BinderRpc, UseKernelBinderCallingId) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } bool okToFork = ProcessState::selfOrNull() == nullptr; auto proc = createRpcTestSocketServerProcess({}); Loading Loading @@ -1597,6 +1641,10 @@ TEST_P(BinderRpc, SendTooManyFiles) { } TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { if constexpr (!kEnableSharedLibs) { GTEST_SKIP() << "Test disabled because Binder was built as a static library"; } auto proc = createRpcTestSocketServerProcess({}); ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); Loading @@ -1606,6 +1654,10 @@ TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { } TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { if constexpr (!kEnableSharedLibs) { GTEST_SKIP() << "Test disabled because Binder was built as a static library"; } auto proc = createRpcTestSocketServerProcess({}); ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); Loading @@ -1630,7 +1682,7 @@ ssize_t countFds() { return ret; } TEST_P(BinderRpc, Fds) { TEST_P(BinderRpcThreads, Fds) { ssize_t beforeFds = countFds(); ASSERT_GE(beforeFds, 0); { Loading Loading @@ -1769,6 +1821,13 @@ INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, ::testing::ValuesIn(testVersions())), BinderRpc::PrintParamInfo); INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpcThreads, ::testing::Combine(::testing::ValuesIn(testSocketTypes()), ::testing::ValuesIn(RpcSecurityValues()), ::testing::ValuesIn(testVersions()), ::testing::ValuesIn(testVersions())), BinderRpc::PrintParamInfo); class BinderRpcServerRootObject : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {}; Loading Loading @@ -1821,6 +1880,10 @@ private: }; TEST_P(BinderRpcServerOnly, Shutdown) { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } auto addr = allocateSocketAddress(); auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); server->setProtocolVersion(std::get<1>(GetParam())); Loading Loading @@ -1852,6 +1915,11 @@ TEST(BinderRpc, Java) { "createRpcDelegateServiceManager() with a device attached, such test belongs " "to binderHostDeviceTest. Hence, just disable this test on host."; #endif // !__ANDROID__ if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } sp<IServiceManager> sm = defaultServiceManager(); ASSERT_NE(nullptr, sm); // Any Java service with non-empty getInterfaceDescriptor() would do. Loading Loading @@ -2152,6 +2220,11 @@ public: (void)serverVersion; return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b); } void SetUp() override { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } } }; TEST_P(RpcTransportTest, GoodCertificate) { Loading Loading @@ -2356,6 +2429,10 @@ public: }; TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); std::vector<uint8_t> pkeyData, certData; Loading Loading
libs/binder/Android.bp +117 −57 Original line number Diff line number Diff line Loading @@ -70,21 +70,9 @@ filegroup { ], } cc_library { name: "libbinder", version_script: "libbinder.map", // for vndbinder vendor_available: true, vndk: { enabled: true, }, recovery_available: true, double_loadable: true, cc_defaults { name: "libbinder_defaults", host_supported: true, // TODO(b/153609531): remove when no longer needed. native_bridge_supported: true, // TODO(b/31559095): get headers from bionic on host include_dirs: [ Loading @@ -92,73 +80,32 @@ cc_library { "bionic/libc/kernel/uapi/", ], // libbinder does not offer a stable wire protocol. // if a second copy of it is installed, then it may break after security // or dessert updates. Instead, apex users should use libbinder_ndk. apex_available: [ "//apex_available:platform", ], srcs: [ "Binder.cpp", "BpBinder.cpp", "BufferedTextOutput.cpp", "Debug.cpp", "FdTrigger.cpp", "IInterface.cpp", "IMemory.cpp", "IPCThreadState.cpp", "IResultReceiver.cpp", "IServiceManager.cpp", "IShellCallback.cpp", "LazyServiceRegistrar.cpp", "MemoryBase.cpp", "MemoryDealer.cpp", "MemoryHeapBase.cpp", "Parcel.cpp", "ParcelableHolder.cpp", "ParcelFileDescriptor.cpp", "PersistableBundle.cpp", "ProcessState.cpp", "RpcSession.cpp", "RpcServer.cpp", "RpcState.cpp", "RpcTransportRaw.cpp", "Static.cpp", "Stability.cpp", "Status.cpp", "TextOutput.cpp", "Utils.cpp", ":libbinder_aidl", ":libbinder_device_interface_sources", ], target: { android: { // NOT static to keep the wire protocol unfrozen static: { enabled: false, }, }, vendor: { exclude_srcs: [ ":libbinder_device_interface_sources", ], }, darwin: { enabled: false, }, host: { srcs: [ "ServiceManagerHost.cpp", "UtilsHost.cpp", ], }, recovery: { exclude_header_libs: [ "libandroid_runtime_vm_headers", ], }, }, aidl: { Loading @@ -166,7 +113,6 @@ cc_library { }, cflags: [ "-Wall", "-Wextra", "-Wextra-semi", "-Werror", Loading @@ -174,7 +120,6 @@ cc_library { "-Wreorder-init-list", "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION", "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION", "-DBINDER_WITH_KERNEL_IPC", ], product_variables: { binder32bit: { Loading Loading @@ -230,10 +175,125 @@ cc_library { "performance*", "portability*", ], } cc_defaults { name: "libbinder_kernel_defaults", srcs: [ "BufferedTextOutput.cpp", "IPCThreadState.cpp", "IServiceManager.cpp", "ProcessState.cpp", "Static.cpp", ":libbinder_aidl", ":libbinder_device_interface_sources", ], target: { vendor: { exclude_srcs: [ ":libbinder_device_interface_sources", ], }, host: { srcs: [ "ServiceManagerHost.cpp", ], }, }, cflags: [ "-DBINDER_WITH_KERNEL_IPC", ], } cc_library { name: "libbinder", defaults: [ "libbinder_defaults", "libbinder_kernel_defaults", ], version_script: "libbinder.map", // for vndbinder vendor_available: true, vndk: { enabled: true, }, recovery_available: true, double_loadable: true, // TODO(b/153609531): remove when no longer needed. native_bridge_supported: true, // libbinder does not offer a stable wire protocol. // if a second copy of it is installed, then it may break after security // or dessert updates. Instead, apex users should use libbinder_ndk. apex_available: [ "//apex_available:platform", ], srcs: [ "IMemory.cpp", "LazyServiceRegistrar.cpp", "MemoryBase.cpp", "MemoryDealer.cpp", "MemoryHeapBase.cpp", "ParcelableHolder.cpp", "PersistableBundle.cpp", ], target: { android: { // NOT static to keep the wire protocol unfrozen static: { enabled: false, }, }, darwin: { enabled: false, }, recovery: { exclude_header_libs: [ "libandroid_runtime_vm_headers", ], }, }, afdo: true, } cc_library_static { name: "libbinder_rpc_no_kernel", defaults: ["libbinder_defaults"], visibility: [ ":__subpackages__", ], } cc_library_static { name: "libbinder_rpc_single_threaded", defaults: [ "libbinder_defaults", "libbinder_kernel_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], visibility: [ ":__subpackages__", ], } cc_library_static { name: "libbinder_rpc_single_threaded_no_kernel", defaults: ["libbinder_defaults"], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], visibility: [ ":__subpackages__", ], } cc_defaults { name: "libbinder_tls_shared_deps", shared_libs: [ Loading
libs/binder/TEST_MAPPING +9 −0 Original line number Diff line number Diff line Loading @@ -84,6 +84,15 @@ "presubmit-large": [ { "name": "binderRpcTest" }, { "name": "binderRpcTestNoKernel" }, { "name": "binderRpcTestSingleThreaded" }, { "name": "binderRpcTestSingleThreadedNoKernel" } ], "hwasan-presubmit": [ Loading
libs/binder/tests/Android.bp +85 −10 Original line number Diff line number Diff line Loading @@ -167,7 +167,6 @@ cc_library_static { "libbinder_tls_shared_deps", ], shared_libs: [ "libbinder", "libbase", "liblog", ], Loading @@ -185,8 +184,8 @@ cc_library_static { ], } cc_test { name: "binderRpcTest", cc_defaults { name: "binderRpcTest_defaults", host_supported: true, target: { darwin: { Loading @@ -198,12 +197,34 @@ cc_test { }, defaults: [ "binder_test_defaults", "libbinder_tls_shared_deps", ], srcs: [ "binderRpcTest.cpp", ], static_libs: [ "libbinder_tls_static", "libbinder_tls_test_utils", "binderRpcTestIface-cpp", "binderRpcTestIface-ndk", ], test_suites: ["general-tests"], require_root: true, } cc_test { name: "binderRpcTest", defaults: [ "binderRpcTest_defaults", "libbinder_tls_shared_deps", ], cflags: [ "-DBINDER_WITH_KERNEL_IPC", ], shared_libs: [ "libbinder", "libbinder_ndk", Loading @@ -212,14 +233,68 @@ cc_test { "libcutils", "liblog", ], } cc_defaults { name: "binderRpcTest_static_defaults", shared_libs: [ "libutils", // libcrypto_static is not visible to this module "libcrypto", ], static_libs: [ "libbinder_tls_static", "libbinder_tls_test_utils", "binderRpcTestIface-cpp", "binderRpcTestIface-ndk", "libbase", "libcutils", "liblog", "libssl", ], cflags: [ // Disable tests that require shared libraries, // e.g., libbinder.so or libbinder_ndk.so "-DBINDER_TEST_NO_SHARED_LIBS", ], } cc_test { name: "binderRpcTestNoKernel", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], static_libs: [ "libbinder_rpc_no_kernel", ], } cc_test { name: "binderRpcTestSingleThreaded", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", "-DBINDER_WITH_KERNEL_IPC", ], static_libs: [ "libbinder_rpc_single_threaded", ], } cc_test { name: "binderRpcTestSingleThreadedNoKernel", defaults: [ "binderRpcTest_defaults", "binderRpcTest_static_defaults", ], cflags: [ "-DBINDER_RPC_SINGLE_THREADED", ], static_libs: [ "libbinder_rpc_single_threaded_no_kernel", ], test_suites: ["general-tests"], require_root: true, } cc_test { Loading
libs/binder/tests/binderRpcTest.cpp +99 −22 Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <binder/ProcessState.h> #include <binder/RpcServer.h> #include <binder/RpcSession.h> #include <binder/RpcThreads.h> #include <binder/RpcTlsTestUtils.h> #include <binder/RpcTlsUtils.h> #include <binder/RpcTransport.h> Loading @@ -51,6 +52,7 @@ #include <sys/socket.h> #include <unistd.h> #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../RpcSocketAddress.h" // for testing preconnected clients #include "../RpcState.h" // for debugging Loading @@ -65,6 +67,12 @@ using testing::AssertionSuccess; namespace android { #ifdef BINDER_TEST_NO_SHARED_LIBS constexpr bool kEnableSharedLibs = false; #else constexpr bool kEnableSharedLibs = true; #endif static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT || RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); const char* kLocalInetAddress = "127.0.0.1"; Loading Loading @@ -99,7 +107,7 @@ static inline std::unique_ptr<RpcTransportCtxFactory> newFactory( static base::unique_fd mockFileDescriptor(std::string contents) { android::base::unique_fd readFd, writeFd; CHECK(android::base::Pipe(&readFd, &writeFd)) << strerror(errno); std::thread([writeFd = std::move(writeFd), contents = std::move(contents)]() { RpcMaybeThread([writeFd = std::move(writeFd), contents = std::move(contents)]() { signal(SIGPIPE, SIG_IGN); // ignore possible SIGPIPE from the write if (!WriteStringToFd(contents, writeFd)) { int savedErrno = errno; Loading Loading @@ -177,7 +185,7 @@ std::atomic<int32_t> MyBinderRpcSession::gNum; class MyBinderRpcCallback : public BnBinderRpcCallback { Status sendCallback(const std::string& value) { std::unique_lock _l(mMutex); RpcMutexUniqueLock _l(mMutex); mValues.push_back(value); _l.unlock(); mCv.notify_one(); Loading @@ -186,8 +194,8 @@ class MyBinderRpcCallback : public BnBinderRpcCallback { Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } public: std::mutex mMutex; std::condition_variable mCv; RpcMutex mMutex; RpcConditionVariable mCv; std::vector<std::string> mValues; }; Loading Loading @@ -263,7 +271,7 @@ public: return Status::ok(); } std::mutex blockMutex; RpcMutex blockMutex; Status lock() override { blockMutex.lock(); return Status::ok(); Loading @@ -274,7 +282,7 @@ public: return Status::ok(); } Status lockUnlock() override { std::lock_guard<std::mutex> _l(blockMutex); RpcMutexLockGuard _l(blockMutex); return Status::ok(); } Loading @@ -300,7 +308,7 @@ public: } if (delayed) { std::thread([=]() { RpcMaybeThread([=]() { ALOGE("Executing delayed callback: '%s'", value.c_str()); Status status = doCallback(callback, oneway, false, value); ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); Loading Loading @@ -333,7 +341,7 @@ public: if (strongServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } std::thread([=] { RpcMaybeThread([=] { LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); }).detach(); return Status::ok(); Loading @@ -343,7 +351,9 @@ public: // this is WRONG! It does not make sense when using RPC binder, and // because it is SO wrong, and so much code calls this, it should abort! if constexpr (kEnableKernelIpc) { (void)IPCThreadState::self()->getCallingPid(); } return Status::ok(); } Loading Loading @@ -796,6 +806,18 @@ public: size_t sleepMs = 500); }; // Test fixture for tests that start multiple threads. // This includes tests with one thread but multiple sessions, // since a server uses one thread per session. class BinderRpcThreads : public BinderRpc { public: void SetUp() override { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } } }; TEST_P(BinderRpc, Ping) { auto proc = createRpcTestSocketServerProcess({}); ASSERT_NE(proc.rootBinder, nullptr); Loading @@ -808,7 +830,7 @@ TEST_P(BinderRpc, GetInterfaceDescriptor) { EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); } TEST_P(BinderRpc, MultipleSessions) { TEST_P(BinderRpcThreads, MultipleSessions) { auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5}); for (auto session : proc.proc.sessions) { ASSERT_NE(nullptr, session.root); Loading @@ -816,7 +838,7 @@ TEST_P(BinderRpc, MultipleSessions) { } } TEST_P(BinderRpc, SeparateRootObject) { TEST_P(BinderRpcThreads, SeparateRootObject) { SocketType type = std::get<0>(GetParam()); if (type == SocketType::PRECONNECTED || type == SocketType::UNIX) { // we can't get port numbers for unix sockets Loading Loading @@ -999,7 +1021,7 @@ TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); } TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { TEST_P(BinderRpcThreads, CannotMixBindersBetweenTwoSessionsToTheSameServer) { auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2}); sp<IBinder> outBinder; Loading @@ -1009,6 +1031,11 @@ TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { } TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } auto proc = createRpcTestSocketServerProcess({}); sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); Loading @@ -1018,6 +1045,11 @@ TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { } TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } auto proc = createRpcTestSocketServerProcess({}); // for historical reasons, IServiceManager interface only returns the Loading Loading @@ -1145,7 +1177,7 @@ size_t epochMillis() { return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); } TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { TEST_P(BinderRpcThreads, ThreadPoolGreaterThanEqualRequested) { constexpr size_t kNumThreads = 10; auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); Loading Loading @@ -1196,14 +1228,14 @@ void BinderRpc::testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t num EXPECT_LE(epochMsAfter, epochMsBefore + 3 * sleepMs); } TEST_P(BinderRpc, ThreadPoolOverSaturated) { TEST_P(BinderRpcThreads, ThreadPoolOverSaturated) { constexpr size_t kNumThreads = 10; constexpr size_t kNumCalls = kNumThreads + 3; auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); testThreadPoolOverSaturated(proc.rootIface, kNumCalls); } TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { TEST_P(BinderRpcThreads, ThreadPoolLimitOutgoing) { constexpr size_t kNumThreads = 20; constexpr size_t kNumOutgoingConnections = 10; constexpr size_t kNumCalls = kNumOutgoingConnections + 3; Loading @@ -1212,7 +1244,7 @@ TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { testThreadPoolOverSaturated(proc.rootIface, kNumCalls); } TEST_P(BinderRpc, ThreadingStressTest) { TEST_P(BinderRpcThreads, ThreadingStressTest) { constexpr size_t kNumClientThreads = 10; constexpr size_t kNumServerThreads = 10; constexpr size_t kNumCalls = 100; Loading Loading @@ -1241,7 +1273,7 @@ static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& ifa for (auto& t : threads) t.join(); } TEST_P(BinderRpc, OnewayStressTest) { TEST_P(BinderRpcThreads, OnewayStressTest) { constexpr size_t kNumClientThreads = 10; constexpr size_t kNumServerThreads = 10; constexpr size_t kNumCalls = 1000; Loading Loading @@ -1276,7 +1308,7 @@ TEST_P(BinderRpc, OnewayCallDoesNotWait) { EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); } TEST_P(BinderRpc, OnewayCallQueueing) { TEST_P(BinderRpcThreads, OnewayCallQueueing) { constexpr size_t kNumSleeps = 10; constexpr size_t kNumExtraServerThreads = 4; constexpr size_t kSleepMs = 50; Loading Loading @@ -1305,7 +1337,7 @@ TEST_P(BinderRpc, OnewayCallQueueing) { saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface); } TEST_P(BinderRpc, OnewayCallExhaustion) { TEST_P(BinderRpcThreads, OnewayCallExhaustion) { constexpr size_t kNumClients = 2; constexpr size_t kTooLongMs = 1000; Loading Loading @@ -1351,8 +1383,15 @@ TEST_P(BinderRpc, Callbacks) { for (bool callIsOneway : {true, false}) { for (bool callbackIsOneway : {true, false}) { for (bool delayed : {true, false}) { if (!kEnableRpcThreads && (callIsOneway || callbackIsOneway || delayed)) { // we have no incoming connections to receive the callback continue; } auto proc = createRpcTestSocketServerProcess( {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); {.numThreads = 1, .numSessions = 1, .numIncomingConnections = kEnableRpcThreads ? 1 : 0}); auto cb = sp<MyBinderRpcCallback>::make(); if (callIsOneway) { Loading @@ -1368,7 +1407,7 @@ TEST_P(BinderRpc, Callbacks) { // the callback will be processed on another thread. if (callIsOneway || callbackIsOneway || delayed) { using std::literals::chrono_literals::operator""s; std::unique_lock<std::mutex> _l(cb->mMutex); RpcMutexUniqueLock _l(cb->mMutex); cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); }); } Loading Loading @@ -1434,6 +1473,11 @@ TEST_P(BinderRpc, Die) { } TEST_P(BinderRpc, UseKernelBinderCallingId) { if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } bool okToFork = ProcessState::selfOrNull() == nullptr; auto proc = createRpcTestSocketServerProcess({}); Loading Loading @@ -1597,6 +1641,10 @@ TEST_P(BinderRpc, SendTooManyFiles) { } TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { if constexpr (!kEnableSharedLibs) { GTEST_SKIP() << "Test disabled because Binder was built as a static library"; } auto proc = createRpcTestSocketServerProcess({}); ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); Loading @@ -1606,6 +1654,10 @@ TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { } TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { if constexpr (!kEnableSharedLibs) { GTEST_SKIP() << "Test disabled because Binder was built as a static library"; } auto proc = createRpcTestSocketServerProcess({}); ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); Loading @@ -1630,7 +1682,7 @@ ssize_t countFds() { return ret; } TEST_P(BinderRpc, Fds) { TEST_P(BinderRpcThreads, Fds) { ssize_t beforeFds = countFds(); ASSERT_GE(beforeFds, 0); { Loading Loading @@ -1769,6 +1821,13 @@ INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, ::testing::ValuesIn(testVersions())), BinderRpc::PrintParamInfo); INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpcThreads, ::testing::Combine(::testing::ValuesIn(testSocketTypes()), ::testing::ValuesIn(RpcSecurityValues()), ::testing::ValuesIn(testVersions()), ::testing::ValuesIn(testVersions())), BinderRpc::PrintParamInfo); class BinderRpcServerRootObject : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {}; Loading Loading @@ -1821,6 +1880,10 @@ private: }; TEST_P(BinderRpcServerOnly, Shutdown) { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } auto addr = allocateSocketAddress(); auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); server->setProtocolVersion(std::get<1>(GetParam())); Loading Loading @@ -1852,6 +1915,11 @@ TEST(BinderRpc, Java) { "createRpcDelegateServiceManager() with a device attached, such test belongs " "to binderHostDeviceTest. Hence, just disable this test on host."; #endif // !__ANDROID__ if constexpr (!kEnableKernelIpc) { GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " "at build time."; } sp<IServiceManager> sm = defaultServiceManager(); ASSERT_NE(nullptr, sm); // Any Java service with non-empty getInterfaceDescriptor() would do. Loading Loading @@ -2152,6 +2220,11 @@ public: (void)serverVersion; return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b); } void SetUp() override { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } } }; TEST_P(RpcTransportTest, GoodCertificate) { Loading Loading @@ -2356,6 +2429,10 @@ public: }; TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) { if constexpr (!kEnableRpcThreads) { GTEST_SKIP() << "Test skipped because threads were disabled at build time"; } auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); std::vector<uint8_t> pkeyData, certData; Loading