Loading libs/binderthreadstate/include/binderthreadstate/CallerUtils.h +6 −2 Original line number Diff line number Diff line Loading @@ -36,8 +36,12 @@ enum class BinderCallType { // Based on where we are in recursion of nested binder/hwbinder calls, determine // which one we are closer to. inline static BinderCallType getCurrentServingCall() { const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer(); const void* binderSp = android::IPCThreadState::self()->getServingStackPointer(); auto* hwState = android::hardware::IPCThreadState::selfOrNull(); auto* state = android::IPCThreadState::selfOrNull(); // getServingStackPointer can also return nullptr const void* hwbinderSp = hwState ? hwState->getServingStackPointer() : nullptr; const void* binderSp = state ? state->getServingStackPointer() : nullptr; if (hwbinderSp == nullptr && binderSp == nullptr) return BinderCallType::NONE; if (hwbinderSp == nullptr) return BinderCallType::BINDER; Loading libs/binderthreadstate/test.cpp +19 −0 Original line number Diff line number Diff line Loading @@ -16,11 +16,16 @@ #include <BnAidlStuff.h> #include <android-base/logging.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binderthreadstate/CallerUtils.h> #include <binderthreadstateutilstest/1.0/IHidlStuff.h> #include <gtest/gtest.h> #include <hidl/HidlTransportSupport.h> #include <hwbinder/IPCThreadState.h> #include <thread> #include <linux/prctl.h> #include <sys/prctl.h> Loading Loading @@ -154,6 +159,20 @@ TEST(BinderThreadState, LocalAidlCall) { EXPECT_TRUE(server->callLocal().isOk()); } TEST(BinderThreadState, DoesntInitializeBinderDriver) { // this is on another thread, because it's testing thread-specific // state and we expect it not to be initialized. std::thread([&] { EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); (void)getCurrentServingCall(); EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); }).join(); } TEST(BindThreadState, RemoteHidlCall) { auto stuff = IHidlStuff::getService(id2name(kP1Id)); ASSERT_NE(nullptr, stuff); Loading Loading
libs/binderthreadstate/include/binderthreadstate/CallerUtils.h +6 −2 Original line number Diff line number Diff line Loading @@ -36,8 +36,12 @@ enum class BinderCallType { // Based on where we are in recursion of nested binder/hwbinder calls, determine // which one we are closer to. inline static BinderCallType getCurrentServingCall() { const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer(); const void* binderSp = android::IPCThreadState::self()->getServingStackPointer(); auto* hwState = android::hardware::IPCThreadState::selfOrNull(); auto* state = android::IPCThreadState::selfOrNull(); // getServingStackPointer can also return nullptr const void* hwbinderSp = hwState ? hwState->getServingStackPointer() : nullptr; const void* binderSp = state ? state->getServingStackPointer() : nullptr; if (hwbinderSp == nullptr && binderSp == nullptr) return BinderCallType::NONE; if (hwbinderSp == nullptr) return BinderCallType::BINDER; Loading
libs/binderthreadstate/test.cpp +19 −0 Original line number Diff line number Diff line Loading @@ -16,11 +16,16 @@ #include <BnAidlStuff.h> #include <android-base/logging.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binderthreadstate/CallerUtils.h> #include <binderthreadstateutilstest/1.0/IHidlStuff.h> #include <gtest/gtest.h> #include <hidl/HidlTransportSupport.h> #include <hwbinder/IPCThreadState.h> #include <thread> #include <linux/prctl.h> #include <sys/prctl.h> Loading Loading @@ -154,6 +159,20 @@ TEST(BinderThreadState, LocalAidlCall) { EXPECT_TRUE(server->callLocal().isOk()); } TEST(BinderThreadState, DoesntInitializeBinderDriver) { // this is on another thread, because it's testing thread-specific // state and we expect it not to be initialized. std::thread([&] { EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); (void)getCurrentServingCall(); EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); }).join(); } TEST(BindThreadState, RemoteHidlCall) { auto stuff = IHidlStuff::getService(id2name(kP1Id)); ASSERT_NE(nullptr, stuff); Loading