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