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