Loading libs/binder/Android.bp +36 −16 Original line number Diff line number Diff line Loading @@ -195,18 +195,25 @@ cc_defaults { ], } cc_library_shared { name: "libbinder_on_trusty_mock", defaults: ["libbinder_common_defaults"], cc_library_headers { name: "trusty_mock_headers", host_supported: true, srcs: [ // Trusty-specific files "trusty/logging.cpp", "trusty/OS.cpp", "trusty/RpcServerTrusty.cpp", "trusty/RpcTransportTipcTrusty.cpp", "trusty/TrustyStatus.cpp", "trusty/socket.cpp", export_include_dirs: [ "trusty/include", "trusty/include_mock", ], visibility: [ ":__subpackages__", ], } cc_defaults { name: "trusty_mock_defaults", header_libs: [ "trusty_mock_headers", ], cflags: [ Loading @@ -227,16 +234,29 @@ cc_library_shared { ], rtti: false, local_include_dirs: [ "trusty/include", "trusty/include_mock", ], visibility: [ ":__subpackages__", ], } cc_library_shared { name: "libbinder_on_trusty_mock", defaults: [ "libbinder_common_defaults", "trusty_mock_defaults", ], srcs: [ // Trusty-specific files "trusty/logging.cpp", "trusty/OS.cpp", "trusty/RpcServerTrusty.cpp", "trusty/RpcTransportTipcTrusty.cpp", "trusty/TrustyStatus.cpp", "trusty/socket.cpp", ], } cc_defaults { name: "libbinder_kernel_defaults", srcs: [ Loading libs/binder/tests/Android.bp +24 −0 Original line number Diff line number Diff line Loading @@ -336,6 +336,29 @@ cc_test { ], } cc_binary { name: "binderRpcTestService_on_trusty_mock", defaults: [ "trusty_mock_defaults", ], srcs: [ "binderRpcTestCommon.cpp", "binderRpcTestServiceTrusty.cpp", ], shared_libs: [ "libbinder_on_trusty_mock", "libbase", "libutils", "libcutils", ], static_libs: [ "binderRpcTestIface-cpp", ], } cc_test { name: "binderRpcTest", defaults: [ Loading @@ -347,6 +370,7 @@ cc_test { // Add the Trusty mock library as a fake dependency so it gets built required: [ "libbinder_on_trusty_mock", "binderRpcTestService_on_trusty_mock", ], } Loading libs/binder/tests/binderRpcTest.cpp +1 −9 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ * limitations under the License. */ #include <aidl/IBinderRpcTest.h> #include <android-base/stringprintf.h> #include <chrono> Loading Loading @@ -1100,15 +1101,6 @@ static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) { return ret; } static std::vector<uint32_t> testVersions() { std::vector<uint32_t> versions; for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { versions.push_back(i); } versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); return versions; } INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, ::testing::Combine(::testing::ValuesIn(testSocketTypes()), ::testing::ValuesIn(RpcSecurityValues()), Loading libs/binder/tests/binderRpcTestCommon.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -19,6 +19,6 @@ namespace android { std::atomic<int32_t> MyBinderRpcSession::gNum; sp<IBinder> MyBinderRpcTest::mHeldBinder; sp<IBinder> MyBinderRpcTestBase::mHeldBinder; } // namespace android libs/binder/tests/binderRpcTestCommon.h +74 −70 Original line number Diff line number Diff line Loading @@ -22,37 +22,42 @@ #include <BnBinderRpcCallback.h> #include <BnBinderRpcSession.h> #include <BnBinderRpcTest.h> #include <aidl/IBinderRpcTest.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android/binder_auto_utils.h> #include <android/binder_libbinder.h> #include <android-base/stringprintf.h> #include <binder/Binder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #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> #include <binder/RpcTransportRaw.h> #include <binder/RpcTransportTls.h> #include <unistd.h> #include <cinttypes> #include <string> #include <vector> #ifndef __TRUSTY__ #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android/binder_auto_utils.h> #include <android/binder_libbinder.h> #include <binder/ProcessState.h> #include <binder/RpcTlsTestUtils.h> #include <binder/RpcTlsUtils.h> #include <binder/RpcTransportTls.h> #include <signal.h> #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../OS.h" // for testing UnixBootstrap clients #include "../RpcSocketAddress.h" // for testing preconnected clients #include "../RpcState.h" // for debugging #include "../vm_sockets.h" // for VMADDR_* #endif // __TRUSTY__ #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../RpcState.h" // for debugging #include "utils/Errors.h" namespace android { Loading @@ -65,6 +70,19 @@ static inline std::vector<RpcSecurity> RpcSecurityValues() { return {RpcSecurity::RAW, RpcSecurity::TLS}; } static inline std::vector<uint32_t> testVersions() { std::vector<uint32_t> versions; for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { versions.push_back(i); } versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); return versions; } static inline std::string trustyIpcPort(uint32_t serverVersion) { return base::StringPrintf("com.android.trusty.binderRpcTestService.V%" PRIu32, serverVersion); } enum class SocketType { PRECONNECTED, UNIX, Loading Loading @@ -118,6 +136,7 @@ struct BinderRpcOptions { bool allowConnectFailure = false; }; #ifndef __TRUSTY__ static inline void writeString(android::base::borrowed_fd fd, std::string_view str) { uint64_t length = str.length(); CHECK(android::base::WriteFully(fd, &length, sizeof(length))); Loading Loading @@ -182,6 +201,7 @@ static inline base::unique_fd mockFileDescriptor(std::string contents) { }).detach(); return readFd; } #endif // __TRUSTY__ // A threadsafe channel where writes block until the value is read. template <typename T> Loading Loading @@ -252,9 +272,12 @@ public: std::vector<std::string> mValues; }; class MyBinderRpcTest : public BnBinderRpcTest { // Base class for all concrete implementations of MyBinderRpcTest. // Sub-classes that want to provide a full implementation should derive // from this class instead of MyBinderRpcTestDefault below so the compiler // checks that all methods are implemented. class MyBinderRpcTestBase : public BnBinderRpcTest { public: wp<RpcServer> server; int port = 0; Status sendString(const std::string& str) override { Loading @@ -269,18 +292,6 @@ public: *out = port; return Status::ok(); } Status countBinders(std::vector<int32_t>* out) override { sp<RpcServer> spServer = server.promote(); if (spServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } out->clear(); for (auto session : spServer->listSessions()) { size_t count = session->state()->countBinders(); out->push_back(count); } return Status::ok(); } Status getNullBinder(sp<IBinder>* out) override { out->clear(); return Status::ok(); Loading Loading @@ -381,62 +392,55 @@ public: return doCallback(callback, oneway, delayed, value); } Status die(bool cleanup) override { if (cleanup) { exit(1); } else { _exit(1); } } Status scheduleShutdown() override { sp<RpcServer> strongServer = server.promote(); if (strongServer == nullptr) { protected: // Generic version of countBinders that works with both // RpcServer and RpcServerTrusty template <typename T> Status countBindersImpl(const wp<T>& server, std::vector<int32_t>* out) { sp<T> spServer = server.promote(); if (spServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } RpcMaybeThread([=] { LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); }).detach(); return Status::ok(); } Status useKernelBinderCallingId() override { // 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(); out->clear(); for (auto session : spServer->listSessions()) { size_t count = session->state()->countBinders(); out->push_back(count); } return Status::ok(); } }; Status echoAsFile(const std::string& content, android::os::ParcelFileDescriptor* out) override { out->reset(mockFileDescriptor(content)); return Status::ok(); // Default implementation of MyBinderRpcTest that can be used as-is // or derived from by classes that only want to implement a subset of // the unimplemented methods class MyBinderRpcTestDefault : public MyBinderRpcTestBase { public: Status countBinders(std::vector<int32_t>* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& files, android::os::ParcelFileDescriptor* out) override { std::string acc; for (const auto& file : files) { std::string result; CHECK(android::base::ReadFdToString(file.get(), &result)); acc.append(result); } out->reset(mockFileDescriptor(acc)); return Status::ok(); Status die(bool /*cleanup*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status scheduleShutdown() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status useKernelBinderCallingId() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status echoAsFile(const std::string& /*content*/, android::os::ParcelFileDescriptor* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } HandoffChannel<android::base::unique_fd> mFdChannel; Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& /*files*/, android::os::ParcelFileDescriptor* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& fd) override { mFdChannel.write(android::base::unique_fd(fcntl(fd.get(), F_DUPFD_CLOEXEC, 0))); return Status::ok(); Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& /*fd*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status blockingRecvFd(android::os::ParcelFileDescriptor* fd) override { fd->reset(mFdChannel.read()); return Status::ok(); Status blockingRecvFd(android::os::ParcelFileDescriptor* /*fd*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } }; Loading Loading
libs/binder/Android.bp +36 −16 Original line number Diff line number Diff line Loading @@ -195,18 +195,25 @@ cc_defaults { ], } cc_library_shared { name: "libbinder_on_trusty_mock", defaults: ["libbinder_common_defaults"], cc_library_headers { name: "trusty_mock_headers", host_supported: true, srcs: [ // Trusty-specific files "trusty/logging.cpp", "trusty/OS.cpp", "trusty/RpcServerTrusty.cpp", "trusty/RpcTransportTipcTrusty.cpp", "trusty/TrustyStatus.cpp", "trusty/socket.cpp", export_include_dirs: [ "trusty/include", "trusty/include_mock", ], visibility: [ ":__subpackages__", ], } cc_defaults { name: "trusty_mock_defaults", header_libs: [ "trusty_mock_headers", ], cflags: [ Loading @@ -227,16 +234,29 @@ cc_library_shared { ], rtti: false, local_include_dirs: [ "trusty/include", "trusty/include_mock", ], visibility: [ ":__subpackages__", ], } cc_library_shared { name: "libbinder_on_trusty_mock", defaults: [ "libbinder_common_defaults", "trusty_mock_defaults", ], srcs: [ // Trusty-specific files "trusty/logging.cpp", "trusty/OS.cpp", "trusty/RpcServerTrusty.cpp", "trusty/RpcTransportTipcTrusty.cpp", "trusty/TrustyStatus.cpp", "trusty/socket.cpp", ], } cc_defaults { name: "libbinder_kernel_defaults", srcs: [ Loading
libs/binder/tests/Android.bp +24 −0 Original line number Diff line number Diff line Loading @@ -336,6 +336,29 @@ cc_test { ], } cc_binary { name: "binderRpcTestService_on_trusty_mock", defaults: [ "trusty_mock_defaults", ], srcs: [ "binderRpcTestCommon.cpp", "binderRpcTestServiceTrusty.cpp", ], shared_libs: [ "libbinder_on_trusty_mock", "libbase", "libutils", "libcutils", ], static_libs: [ "binderRpcTestIface-cpp", ], } cc_test { name: "binderRpcTest", defaults: [ Loading @@ -347,6 +370,7 @@ cc_test { // Add the Trusty mock library as a fake dependency so it gets built required: [ "libbinder_on_trusty_mock", "binderRpcTestService_on_trusty_mock", ], } Loading
libs/binder/tests/binderRpcTest.cpp +1 −9 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ * limitations under the License. */ #include <aidl/IBinderRpcTest.h> #include <android-base/stringprintf.h> #include <chrono> Loading Loading @@ -1100,15 +1101,6 @@ static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) { return ret; } static std::vector<uint32_t> testVersions() { std::vector<uint32_t> versions; for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { versions.push_back(i); } versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); return versions; } INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, ::testing::Combine(::testing::ValuesIn(testSocketTypes()), ::testing::ValuesIn(RpcSecurityValues()), Loading
libs/binder/tests/binderRpcTestCommon.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -19,6 +19,6 @@ namespace android { std::atomic<int32_t> MyBinderRpcSession::gNum; sp<IBinder> MyBinderRpcTest::mHeldBinder; sp<IBinder> MyBinderRpcTestBase::mHeldBinder; } // namespace android
libs/binder/tests/binderRpcTestCommon.h +74 −70 Original line number Diff line number Diff line Loading @@ -22,37 +22,42 @@ #include <BnBinderRpcCallback.h> #include <BnBinderRpcSession.h> #include <BnBinderRpcTest.h> #include <aidl/IBinderRpcTest.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android/binder_auto_utils.h> #include <android/binder_libbinder.h> #include <android-base/stringprintf.h> #include <binder/Binder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #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> #include <binder/RpcTransportRaw.h> #include <binder/RpcTransportTls.h> #include <unistd.h> #include <cinttypes> #include <string> #include <vector> #ifndef __TRUSTY__ #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android/binder_auto_utils.h> #include <android/binder_libbinder.h> #include <binder/ProcessState.h> #include <binder/RpcTlsTestUtils.h> #include <binder/RpcTlsUtils.h> #include <binder/RpcTransportTls.h> #include <signal.h> #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../OS.h" // for testing UnixBootstrap clients #include "../RpcSocketAddress.h" // for testing preconnected clients #include "../RpcState.h" // for debugging #include "../vm_sockets.h" // for VMADDR_* #endif // __TRUSTY__ #include "../BuildFlags.h" #include "../FdTrigger.h" #include "../RpcState.h" // for debugging #include "utils/Errors.h" namespace android { Loading @@ -65,6 +70,19 @@ static inline std::vector<RpcSecurity> RpcSecurityValues() { return {RpcSecurity::RAW, RpcSecurity::TLS}; } static inline std::vector<uint32_t> testVersions() { std::vector<uint32_t> versions; for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { versions.push_back(i); } versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); return versions; } static inline std::string trustyIpcPort(uint32_t serverVersion) { return base::StringPrintf("com.android.trusty.binderRpcTestService.V%" PRIu32, serverVersion); } enum class SocketType { PRECONNECTED, UNIX, Loading Loading @@ -118,6 +136,7 @@ struct BinderRpcOptions { bool allowConnectFailure = false; }; #ifndef __TRUSTY__ static inline void writeString(android::base::borrowed_fd fd, std::string_view str) { uint64_t length = str.length(); CHECK(android::base::WriteFully(fd, &length, sizeof(length))); Loading Loading @@ -182,6 +201,7 @@ static inline base::unique_fd mockFileDescriptor(std::string contents) { }).detach(); return readFd; } #endif // __TRUSTY__ // A threadsafe channel where writes block until the value is read. template <typename T> Loading Loading @@ -252,9 +272,12 @@ public: std::vector<std::string> mValues; }; class MyBinderRpcTest : public BnBinderRpcTest { // Base class for all concrete implementations of MyBinderRpcTest. // Sub-classes that want to provide a full implementation should derive // from this class instead of MyBinderRpcTestDefault below so the compiler // checks that all methods are implemented. class MyBinderRpcTestBase : public BnBinderRpcTest { public: wp<RpcServer> server; int port = 0; Status sendString(const std::string& str) override { Loading @@ -269,18 +292,6 @@ public: *out = port; return Status::ok(); } Status countBinders(std::vector<int32_t>* out) override { sp<RpcServer> spServer = server.promote(); if (spServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } out->clear(); for (auto session : spServer->listSessions()) { size_t count = session->state()->countBinders(); out->push_back(count); } return Status::ok(); } Status getNullBinder(sp<IBinder>* out) override { out->clear(); return Status::ok(); Loading Loading @@ -381,62 +392,55 @@ public: return doCallback(callback, oneway, delayed, value); } Status die(bool cleanup) override { if (cleanup) { exit(1); } else { _exit(1); } } Status scheduleShutdown() override { sp<RpcServer> strongServer = server.promote(); if (strongServer == nullptr) { protected: // Generic version of countBinders that works with both // RpcServer and RpcServerTrusty template <typename T> Status countBindersImpl(const wp<T>& server, std::vector<int32_t>* out) { sp<T> spServer = server.promote(); if (spServer == nullptr) { return Status::fromExceptionCode(Status::EX_NULL_POINTER); } RpcMaybeThread([=] { LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); }).detach(); return Status::ok(); } Status useKernelBinderCallingId() override { // 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(); out->clear(); for (auto session : spServer->listSessions()) { size_t count = session->state()->countBinders(); out->push_back(count); } return Status::ok(); } }; Status echoAsFile(const std::string& content, android::os::ParcelFileDescriptor* out) override { out->reset(mockFileDescriptor(content)); return Status::ok(); // Default implementation of MyBinderRpcTest that can be used as-is // or derived from by classes that only want to implement a subset of // the unimplemented methods class MyBinderRpcTestDefault : public MyBinderRpcTestBase { public: Status countBinders(std::vector<int32_t>* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& files, android::os::ParcelFileDescriptor* out) override { std::string acc; for (const auto& file : files) { std::string result; CHECK(android::base::ReadFdToString(file.get(), &result)); acc.append(result); } out->reset(mockFileDescriptor(acc)); return Status::ok(); Status die(bool /*cleanup*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status scheduleShutdown() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status useKernelBinderCallingId() override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status echoAsFile(const std::string& /*content*/, android::os::ParcelFileDescriptor* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } HandoffChannel<android::base::unique_fd> mFdChannel; Status concatFiles(const std::vector<android::os::ParcelFileDescriptor>& /*files*/, android::os::ParcelFileDescriptor* /*out*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& fd) override { mFdChannel.write(android::base::unique_fd(fcntl(fd.get(), F_DUPFD_CLOEXEC, 0))); return Status::ok(); Status blockingSendFdOneway(const android::os::ParcelFileDescriptor& /*fd*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } Status blockingRecvFd(android::os::ParcelFileDescriptor* fd) override { fd->reset(mFdChannel.read()); return Status::ok(); Status blockingRecvFd(android::os::ParcelFileDescriptor* /*fd*/) override { return Status::fromStatusT(UNKNOWN_TRANSACTION); } }; Loading