Loading libs/binder/RpcState.cpp +19 −0 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,9 @@ #include "RpcState.h" #include "RpcState.h" #include <android-base/scopeguard.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/RpcServer.h> #include <binder/RpcServer.h> #include "Debug.h" #include "Debug.h" Loading @@ -28,6 +30,8 @@ namespace android { namespace android { using base::ScopeGuard; RpcState::RpcState() {} RpcState::RpcState() {} RpcState::~RpcState() {} RpcState::~RpcState() {} Loading Loading @@ -470,6 +474,21 @@ status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcS status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, const RpcWireHeader& command) { const RpcWireHeader& command) { IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); IPCThreadState::SpGuard spGuard{ .address = __builtin_frame_address(0), .context = "processing binder RPC command", }; const IPCThreadState::SpGuard* origGuard; if (kernelBinderState != nullptr) { origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); } ScopeGuard guardUnguard = [&]() { if (kernelBinderState != nullptr) { kernelBinderState->restoreGetCallingSpGuard(origGuard); } }; switch (command.command) { switch (command.command) { case RPC_COMMAND_TRANSACT: case RPC_COMMAND_TRANSACT: return processTransact(fd, session, command); return processTransact(fd, session, command); Loading libs/binder/tests/IBinderRpcTest.aidl +2 −0 Original line number Original line Diff line number Diff line Loading @@ -55,4 +55,6 @@ interface IBinderRpcTest { oneway void sleepMsAsync(int ms); oneway void sleepMsAsync(int ms); void die(boolean cleanup); void die(boolean cleanup); void useKernelBinderCallingId(); } } libs/binder/tests/binderRpcTest.cpp +21 −0 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <android/binder_libbinder.h> #include <android/binder_libbinder.h> #include <binder/Binder.h> #include <binder/Binder.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binder/IServiceManager.h> #include <binder/ProcessState.h> #include <binder/ProcessState.h> #include <binder/RpcServer.h> #include <binder/RpcServer.h> Loading Loading @@ -193,6 +194,13 @@ public: _exit(1); _exit(1); } } } } 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! (void)IPCThreadState::self()->getCallingPid(); return Status::ok(); } }; }; sp<IBinder> MyBinderRpcTest::mHeldBinder; sp<IBinder> MyBinderRpcTest::mHeldBinder; Loading Loading @@ -889,6 +897,19 @@ TEST_P(BinderRpc, Die) { } } } } TEST_P(BinderRpc, UseKernelBinderCallingId) { auto proc = createRpcTestSocketServerProcess(1); // we can't allocate IPCThreadState so actually the first time should // succeed :( EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); // second time! we catch the error :) EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); proc.expectInvalid = true; } TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { auto proc = createRpcTestSocketServerProcess(1); auto proc = createRpcTestSocketServerProcess(1); Loading Loading
libs/binder/RpcState.cpp +19 −0 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,9 @@ #include "RpcState.h" #include "RpcState.h" #include <android-base/scopeguard.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/RpcServer.h> #include <binder/RpcServer.h> #include "Debug.h" #include "Debug.h" Loading @@ -28,6 +30,8 @@ namespace android { namespace android { using base::ScopeGuard; RpcState::RpcState() {} RpcState::RpcState() {} RpcState::~RpcState() {} RpcState::~RpcState() {} Loading Loading @@ -470,6 +474,21 @@ status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcS status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, const RpcWireHeader& command) { const RpcWireHeader& command) { IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); IPCThreadState::SpGuard spGuard{ .address = __builtin_frame_address(0), .context = "processing binder RPC command", }; const IPCThreadState::SpGuard* origGuard; if (kernelBinderState != nullptr) { origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); } ScopeGuard guardUnguard = [&]() { if (kernelBinderState != nullptr) { kernelBinderState->restoreGetCallingSpGuard(origGuard); } }; switch (command.command) { switch (command.command) { case RPC_COMMAND_TRANSACT: case RPC_COMMAND_TRANSACT: return processTransact(fd, session, command); return processTransact(fd, session, command); Loading
libs/binder/tests/IBinderRpcTest.aidl +2 −0 Original line number Original line Diff line number Diff line Loading @@ -55,4 +55,6 @@ interface IBinderRpcTest { oneway void sleepMsAsync(int ms); oneway void sleepMsAsync(int ms); void die(boolean cleanup); void die(boolean cleanup); void useKernelBinderCallingId(); } }
libs/binder/tests/binderRpcTest.cpp +21 −0 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <android/binder_libbinder.h> #include <android/binder_libbinder.h> #include <binder/Binder.h> #include <binder/Binder.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binder/IServiceManager.h> #include <binder/ProcessState.h> #include <binder/ProcessState.h> #include <binder/RpcServer.h> #include <binder/RpcServer.h> Loading Loading @@ -193,6 +194,13 @@ public: _exit(1); _exit(1); } } } } 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! (void)IPCThreadState::self()->getCallingPid(); return Status::ok(); } }; }; sp<IBinder> MyBinderRpcTest::mHeldBinder; sp<IBinder> MyBinderRpcTest::mHeldBinder; Loading Loading @@ -889,6 +897,19 @@ TEST_P(BinderRpc, Die) { } } } } TEST_P(BinderRpc, UseKernelBinderCallingId) { auto proc = createRpcTestSocketServerProcess(1); // we can't allocate IPCThreadState so actually the first time should // succeed :( EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); // second time! we catch the error :) EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); proc.expectInvalid = true; } TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { auto proc = createRpcTestSocketServerProcess(1); auto proc = createRpcTestSocketServerProcess(1); Loading