Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <android/os/IServiceManager.h> |
| 18 | #include <binder/Binder.h> |
| 19 | #include <binder/IBinder.h> |
| 20 | #include <binder/IPCThreadState.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/Parcel.h> |
| 23 | #include <binder/Stability.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <sys/prctl.h> |
| 27 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 28 | #include "BnBinderStabilityTestSub.h" |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 29 | #include "BnBinderStabilityTest.h" |
| 30 | #include "BpBinderStabilityTest.h" |
| 31 | |
| 32 | using namespace android; |
| 33 | using android::binder::Status; |
| 34 | using android::os::IServiceManager; |
| 35 | |
| 36 | const String16 kNoStabilityServer = String16("binder_stability_test_service_low"); |
| 37 | const String16 kCompilationUnitServer = String16("binder_stability_test_service_compl"); |
| 38 | const String16 kVintfServer = String16("binder_stability_test_service_vintf"); |
| 39 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 40 | class BadStabilityTestSub : public BnBinderStabilityTestSub { |
| 41 | Status userDefinedTransaction() { |
| 42 | return Status::ok(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | sp<IBinderStabilityTestSub> getCompilationUnitStability() { |
| 47 | sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub(); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 48 | // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS? |
| 49 | // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 50 | internal::Stability::markCompilationUnit(iface.get()); // <- BAD, NO! DO NOT COPY |
| 51 | return iface; |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 54 | sp<IBinderStabilityTestSub> getVintfStability() { |
| 55 | sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub(); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 56 | // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS? |
| 57 | // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 58 | internal::Stability::markVintf(iface.get()); // <- BAD, NO! DO NOT COPY |
| 59 | return iface; |
| 60 | } |
| 61 | |
| 62 | sp<IBinderStabilityTestSub> getVendorStability() { |
| 63 | sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub(); |
| 64 | // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS? |
| 65 | // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS |
| 66 | internal::Stability::markVndk(iface.get()); // <- BAD, NO! DO NOT COPY |
| 67 | return iface; |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // NO! NO! NO! Do not even think of doing something like this! |
| 71 | // This is for testing! If a class like this was actually used in production, |
| 72 | // it would ruin everything! |
| 73 | class BadStabilityTester : public BnBinderStabilityTest { |
| 74 | public: |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 75 | Status sendBinder(const sp<IBinderStabilityTestSub>& /*binder*/) override { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 76 | return Status::ok(); |
| 77 | } |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 78 | Status sendAndCallBinder(const sp<IBinderStabilityTestSub>& binder) override { |
| 79 | return binder->userDefinedTransaction(); |
| 80 | } |
| 81 | Status returnNoStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override { |
| 82 | *_aidl_return = new BadStabilityTestSub(); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 83 | return Status::ok(); |
| 84 | } |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 85 | Status returnLocalStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 86 | *_aidl_return = getCompilationUnitStability(); |
| 87 | return Status::ok(); |
| 88 | } |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 89 | Status returnVintfStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 90 | *_aidl_return = getVintfStability(); |
| 91 | return Status::ok(); |
| 92 | } |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 93 | Status returnVendorStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override { |
| 94 | *_aidl_return = getVendorStability(); |
| 95 | return Status::ok(); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 99 | void checkSystemStabilityBinder(const sp<IBinderStabilityTest>& complServer) { |
| 100 | EXPECT_TRUE(complServer->sendBinder(new BadStabilityTestSub()).isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 101 | EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk()); |
| 102 | EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk()); |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 103 | EXPECT_TRUE(complServer->sendBinder(getVendorStability()).isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 104 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 105 | EXPECT_TRUE(complServer->sendAndCallBinder(new BadStabilityTestSub()).isOk()); |
| 106 | EXPECT_TRUE(complServer->sendAndCallBinder(getCompilationUnitStability()).isOk()); |
| 107 | EXPECT_TRUE(complServer->sendAndCallBinder(getVintfStability()).isOk()); |
| 108 | |
| 109 | // !!! user-defined transaction may not be stable for remote server !!! |
| 110 | EXPECT_FALSE(complServer->sendAndCallBinder(getVendorStability()).isOk()); |
| 111 | |
| 112 | sp<IBinderStabilityTestSub> out; |
Steven Moreland | 0592955 | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 113 | EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk()); |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 114 | ASSERT_NE(nullptr, out.get()); |
| 115 | EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder()); |
| 116 | EXPECT_TRUE(out->userDefinedTransaction().isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 117 | |
| 118 | EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk()); |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 119 | ASSERT_NE(nullptr, out.get()); |
| 120 | EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder()); |
| 121 | EXPECT_TRUE(out->userDefinedTransaction().isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 122 | |
| 123 | EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk()); |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 124 | ASSERT_NE(nullptr, out.get()); |
| 125 | EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder()); |
| 126 | EXPECT_TRUE(out->userDefinedTransaction().isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 127 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 128 | EXPECT_TRUE(complServer->returnVendorStabilityBinder(&out).isOk()); |
| 129 | ASSERT_NE(nullptr, out.get()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 130 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 131 | // !!! libbinder-defined transaction works !!! |
| 132 | EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 133 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 134 | // !!! user-defined transaction may not be stable !!! |
| 135 | EXPECT_FALSE(out->userDefinedTransaction().isOk()); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(BinderStability, RemoteNoStabilityServer) { |
| 139 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer); |
| 140 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 141 | |
| 142 | ASSERT_NE(nullptr, remoteServer.get()); |
| 143 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 144 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 145 | checkSystemStabilityBinder(remoteServer); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(BinderStability, RemoteLowStabilityServer) { |
| 149 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer); |
| 150 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 151 | |
| 152 | ASSERT_NE(nullptr, remoteServer.get()); |
| 153 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 154 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 155 | checkSystemStabilityBinder(remoteServer); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST(BinderStability, RemoteVintfServer) { |
| 159 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer); |
| 160 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 161 | |
| 162 | ASSERT_NE(nullptr, remoteServer.get()); |
| 163 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 164 | |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame^] | 165 | checkSystemStabilityBinder(remoteServer); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | class MarksStabilityInConstructor : public BBinder { |
| 169 | public: |
| 170 | static bool gDestructed; |
| 171 | |
| 172 | MarksStabilityInConstructor() { |
| 173 | internal::Stability::markCompilationUnit(this); |
| 174 | } |
| 175 | ~MarksStabilityInConstructor() { |
| 176 | gDestructed = true; |
| 177 | } |
| 178 | }; |
| 179 | bool MarksStabilityInConstructor::gDestructed = false; |
| 180 | |
| 181 | TEST(BinderStability, MarkingObjectNoDestructTest) { |
| 182 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 183 | |
| 184 | // best practice is to put this directly in an sp, but for this test, we |
| 185 | // want to explicitly check what happens before that happens |
| 186 | MarksStabilityInConstructor* binder = new MarksStabilityInConstructor(); |
| 187 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 188 | |
| 189 | sp<MarksStabilityInConstructor> binderSp = binder; |
| 190 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 191 | |
| 192 | binderSp = nullptr; |
| 193 | ASSERT_TRUE(MarksStabilityInConstructor::gDestructed); |
| 194 | } |
| 195 | |
| 196 | int main(int argc, char** argv) { |
| 197 | ::testing::InitGoogleTest(&argc, argv); |
| 198 | |
| 199 | if (fork() == 0) { |
| 200 | // child process |
| 201 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 202 | |
| 203 | sp<IBinder> noStability = new BadStabilityTester; |
| 204 | android::defaultServiceManager()->addService(kNoStabilityServer, noStability); |
| 205 | |
| 206 | sp<IBinder> compil = new BadStabilityTester; |
| 207 | internal::Stability::markCompilationUnit(compil.get()); |
| 208 | android::defaultServiceManager()->addService(kCompilationUnitServer, compil); |
| 209 | |
| 210 | sp<IBinder> vintf = new BadStabilityTester; |
| 211 | internal::Stability::markVintf(vintf.get()); |
| 212 | android::defaultServiceManager()->addService(kVintfServer, vintf); |
| 213 | |
| 214 | IPCThreadState::self()->joinThreadPool(true); |
| 215 | exit(1); // should not reach |
| 216 | } |
| 217 | |
| 218 | // This is not racey. Just giving these services some time to register before we call |
| 219 | // getService which sleeps for much longer... |
| 220 | usleep(10000); |
| 221 | |
| 222 | return RUN_ALL_TESTS(); |
| 223 | } |