| 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 |  | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 17 | #include <android/binder_libbinder.h> | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 18 | #include <android/binder_manager.h> | 
|  | 19 | #include <android/binder_stability.h> | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 20 | #include <binder/Binder.h> | 
|  | 21 | #include <binder/IBinder.h> | 
|  | 22 | #include <binder/IPCThreadState.h> | 
|  | 23 | #include <binder/IServiceManager.h> | 
|  | 24 | #include <binder/Parcel.h> | 
|  | 25 | #include <binder/Stability.h> | 
|  | 26 | #include <gtest/gtest.h> | 
|  | 27 |  | 
|  | 28 | #include <sys/prctl.h> | 
|  | 29 |  | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 30 | #include "aidl/BnBinderStabilityTest.h" | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 31 | #include "BnBinderStabilityTest.h" | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | using namespace android; | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 34 | using namespace ndk; | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 35 | using android::binder::Status; | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 36 | using android::internal::Stability; // for testing only! | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 37 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 38 | const String16 kSystemStabilityServer = String16("binder_stability_test_service_system"); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 39 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 40 | // This is handwritten so that we can test different stability levels w/o having the AIDL | 
|  | 41 | // compiler assign them. Hand-writing binder interfaces is considered a bad practice | 
|  | 42 | // sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD! | 
|  | 43 | class BadStableBinder : public BBinder { | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 44 | public: | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 45 | static constexpr uint32_t USER_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION; | 
|  | 46 | static String16 kDescriptor; | 
|  | 47 |  | 
|  | 48 | bool gotUserTransaction = false; | 
|  | 49 |  | 
|  | 50 | static status_t doUserTransaction(const sp<IBinder>& binder) { | 
|  | 51 | Parcel data, reply; | 
|  | 52 | data.writeInterfaceToken(kDescriptor); | 
|  | 53 | return binder->transact(USER_TRANSACTION, data, &reply, 0/*flags*/); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 54 | } | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 55 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 56 | status_t onTransact(uint32_t code, | 
|  | 57 | const Parcel& data, Parcel* reply, uint32_t flags) override { | 
|  | 58 | if (code == USER_TRANSACTION) { | 
|  | 59 | // not interested in this kind of stability. Make sure | 
|  | 60 | // we have a test failure | 
|  | 61 | LOG_ALWAYS_FATAL_IF(!data.enforceInterface(kDescriptor)); | 
|  | 62 |  | 
|  | 63 | gotUserTransaction = true; | 
|  | 64 |  | 
|  | 65 | ALOGE("binder stability: Got user transaction"); | 
|  | 66 | return OK; | 
|  | 67 | } | 
|  | 68 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | static sp<BadStableBinder> undef() { | 
|  | 72 | sp<BadStableBinder> iface = new BadStableBinder(); | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 73 | return iface; | 
|  | 74 | } | 
|  | 75 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 76 | static sp<BadStableBinder> system() { | 
|  | 77 | sp<BadStableBinder> iface = new BadStableBinder(); | 
|  | 78 | Stability::markCompilationUnit(iface.get()); // <- for test only | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 79 | return iface; | 
|  | 80 | } | 
|  | 81 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 82 | static sp<BadStableBinder> vintf() { | 
|  | 83 | sp<BadStableBinder> iface = new BadStableBinder(); | 
|  | 84 | Stability::markVintf(iface.get()); // <- for test only | 
|  | 85 | return iface; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static sp<BadStableBinder> vendor() { | 
|  | 89 | sp<BadStableBinder> iface = new BadStableBinder(); | 
|  | 90 | Stability::markVndk(iface.get()); // <- for test only | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 91 | return iface; | 
|  | 92 | } | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 93 | }; | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 94 | String16 BadStableBinder::kDescriptor = String16("BadStableBinder.test"); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 95 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 96 | // NO! NO! NO! Do not even think of doing something like this! | 
|  | 97 | // This is for testing! If a class like this was actually used in production, | 
|  | 98 | // it would ruin everything! | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 99 | class MyBinderStabilityTest : public BnBinderStabilityTest { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 100 | public: | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 101 | Status sendBinder(const sp<IBinder>& /*binder*/) override { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 102 | return Status::ok(); | 
|  | 103 | } | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 104 | Status sendAndCallBinder(const sp<IBinder>& binder) override { | 
|  | 105 | Stability::debugLogStability("sendAndCallBinder got binder", binder); | 
|  | 106 | return Status::fromExceptionCode(BadStableBinder::doUserTransaction(binder)); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 107 | } | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 108 | Status returnNoStabilityBinder(sp<IBinder>* _aidl_return) override { | 
|  | 109 | *_aidl_return = BadStableBinder::undef(); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 110 | return Status::ok(); | 
|  | 111 | } | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 112 | Status returnLocalStabilityBinder(sp<IBinder>* _aidl_return) override { | 
|  | 113 | *_aidl_return = BadStableBinder::system(); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 114 | return Status::ok(); | 
|  | 115 | } | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 116 | Status returnVintfStabilityBinder(sp<IBinder>* _aidl_return) override { | 
|  | 117 | *_aidl_return = BadStableBinder::vintf(); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 118 | return Status::ok(); | 
|  | 119 | } | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 120 | Status returnVendorStabilityBinder(sp<IBinder>* _aidl_return) override { | 
|  | 121 | *_aidl_return = BadStableBinder::vendor(); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 122 | return Status::ok(); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 123 | } | 
|  | 124 | }; | 
|  | 125 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 126 | TEST(BinderStability, OnlyVintfStabilityBinderNeedsVintfDeclaration) { | 
|  | 127 | EXPECT_FALSE(Stability::requiresVintfDeclaration(nullptr)); | 
|  | 128 | EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::undef())); | 
|  | 129 | EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::system())); | 
|  | 130 | EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::vendor())); | 
|  | 131 |  | 
|  | 132 | EXPECT_TRUE(Stability::requiresVintfDeclaration(BadStableBinder::vintf())); | 
|  | 133 | } | 
|  | 134 |  | 
| Kalesh Singh | 3b9ac06 | 2021-04-05 10:07:50 -0400 | [diff] [blame] | 135 | TEST(BinderStability, ForceDowngradeToLocalStability) { | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 136 | sp<IBinder> someBinder = BadStableBinder::vintf(); | 
|  | 137 |  | 
|  | 138 | EXPECT_TRUE(Stability::requiresVintfDeclaration(someBinder)); | 
|  | 139 |  | 
|  | 140 | // silly to do this after already using the binder, but it's for the test | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 141 | Stability::forceDowngradeToLocalStability(someBinder); | 
|  | 142 |  | 
|  | 143 | EXPECT_FALSE(Stability::requiresVintfDeclaration(someBinder)); | 
|  | 144 | } | 
|  | 145 |  | 
| Kalesh Singh | 3b9ac06 | 2021-04-05 10:07:50 -0400 | [diff] [blame] | 146 | TEST(BinderStability, NdkForceDowngradeToLocalStability) { | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 147 | sp<IBinder> someBinder = BadStableBinder::vintf(); | 
|  | 148 |  | 
|  | 149 | EXPECT_TRUE(Stability::requiresVintfDeclaration(someBinder)); | 
|  | 150 |  | 
|  | 151 | // silly to do this after already using the binder, but it's for the test | 
|  | 152 | AIBinder_forceDowngradeToLocalStability(AIBinder_fromPlatformBinder(someBinder)); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 153 |  | 
|  | 154 | EXPECT_FALSE(Stability::requiresVintfDeclaration(someBinder)); | 
|  | 155 | } | 
|  | 156 |  | 
| Kalesh Singh | 3b9ac06 | 2021-04-05 10:07:50 -0400 | [diff] [blame] | 157 | TEST(BinderStability, ForceDowngradeToVendorStability) { | 
|  | 158 | sp<IBinder> serverBinder = android::defaultServiceManager()->getService(kSystemStabilityServer); | 
|  | 159 | auto server = interface_cast<IBinderStabilityTest>(serverBinder); | 
|  | 160 |  | 
|  | 161 | ASSERT_NE(nullptr, server.get()); | 
|  | 162 | ASSERT_NE(nullptr, IInterface::asBinder(server)->remoteBinder()); | 
|  | 163 |  | 
|  | 164 | { | 
|  | 165 | sp<BadStableBinder> binder = BadStableBinder::vintf(); | 
|  | 166 |  | 
|  | 167 | EXPECT_TRUE(Stability::requiresVintfDeclaration(binder)); | 
|  | 168 | EXPECT_TRUE(server->sendAndCallBinder(binder).isOk()); | 
|  | 169 | EXPECT_TRUE(binder->gotUserTransaction); | 
|  | 170 | } | 
|  | 171 | { | 
|  | 172 | sp<BadStableBinder> binder = BadStableBinder::vintf(); | 
|  | 173 |  | 
|  | 174 | // This method should never be called directly. This is done only for the test. | 
|  | 175 | Stability::forceDowngradeToVendorStability(binder); | 
|  | 176 |  | 
|  | 177 | // Binder downgraded to vendor stability, cannot be called from system context | 
|  | 178 | EXPECT_FALSE(Stability::requiresVintfDeclaration(binder)); | 
|  | 179 | EXPECT_EQ(BAD_TYPE, server->sendAndCallBinder(binder).exceptionCode()); | 
|  | 180 | EXPECT_FALSE(binder->gotUserTransaction); | 
|  | 181 | } | 
|  | 182 | } | 
|  | 183 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 184 | TEST(BinderStability, VintfStabilityServerMustBeDeclaredInManifest) { | 
|  | 185 | sp<IBinder> vintfServer = BadStableBinder::vintf(); | 
|  | 186 |  | 
| Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 187 | for (const char* instance8 : { | 
|  | 188 | ".", "/", "/.", "a.d.IFoo", "foo", "a.d.IFoo/foo" | 
|  | 189 | }) { | 
|  | 190 | String16 instance (instance8); | 
|  | 191 |  | 
|  | 192 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, | 
|  | 193 | android::defaultServiceManager()->addService(String16("."), vintfServer)) << instance8; | 
|  | 194 | EXPECT_FALSE(android::defaultServiceManager()->isDeclared(instance)) << instance8; | 
|  | 195 | } | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 198 | TEST(BinderStability, CantCallVendorBinderInSystemContext) { | 
|  | 199 | sp<IBinder> serverBinder = android::defaultServiceManager()->getService(kSystemStabilityServer); | 
|  | 200 | auto server = interface_cast<IBinderStabilityTest>(serverBinder); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 201 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 202 | ASSERT_NE(nullptr, server.get()); | 
|  | 203 | ASSERT_NE(nullptr, IInterface::asBinder(server)->remoteBinder()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 204 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 205 | EXPECT_TRUE(server->sendBinder(BadStableBinder::undef()).isOk()); | 
|  | 206 | EXPECT_TRUE(server->sendBinder(BadStableBinder::system()).isOk()); | 
|  | 207 | EXPECT_TRUE(server->sendBinder(BadStableBinder::vintf()).isOk()); | 
|  | 208 | EXPECT_TRUE(server->sendBinder(BadStableBinder::vendor()).isOk()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 209 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 210 | { | 
|  | 211 | sp<BadStableBinder> binder = BadStableBinder::undef(); | 
|  | 212 | EXPECT_TRUE(server->sendAndCallBinder(binder).isOk()); | 
|  | 213 | EXPECT_TRUE(binder->gotUserTransaction); | 
|  | 214 | } | 
|  | 215 | { | 
|  | 216 | sp<BadStableBinder> binder = BadStableBinder::system(); | 
|  | 217 | EXPECT_TRUE(server->sendAndCallBinder(binder).isOk()); | 
|  | 218 | EXPECT_TRUE(binder->gotUserTransaction); | 
|  | 219 | } | 
|  | 220 | { | 
|  | 221 | sp<BadStableBinder> binder = BadStableBinder::vintf(); | 
|  | 222 | EXPECT_TRUE(server->sendAndCallBinder(binder).isOk()); | 
|  | 223 | EXPECT_TRUE(binder->gotUserTransaction); | 
|  | 224 | } | 
|  | 225 | { | 
|  | 226 | // !!! user-defined transaction may not be stable for remote server !!! | 
|  | 227 | // !!! so, it does not work !!! | 
|  | 228 | sp<BadStableBinder> binder = BadStableBinder::vendor(); | 
|  | 229 | EXPECT_EQ(BAD_TYPE, server->sendAndCallBinder(binder).exceptionCode()); | 
|  | 230 | EXPECT_FALSE(binder->gotUserTransaction); | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | sp<IBinder> out; | 
|  | 234 | EXPECT_TRUE(server->returnNoStabilityBinder(&out).isOk()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 235 | ASSERT_NE(nullptr, out.get()); | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 236 | EXPECT_EQ(OK, out->pingBinder()); | 
|  | 237 | EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out)); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 238 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 239 | EXPECT_TRUE(server->returnLocalStabilityBinder(&out).isOk()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 240 | ASSERT_NE(nullptr, out.get()); | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 241 | EXPECT_EQ(OK, out->pingBinder()); | 
|  | 242 | EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out)); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 243 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 244 | EXPECT_TRUE(server->returnVintfStabilityBinder(&out).isOk()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 245 | ASSERT_NE(nullptr, out.get()); | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 246 | EXPECT_EQ(OK, out->pingBinder()); | 
|  | 247 | EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out)); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 248 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(server->returnVendorStabilityBinder(&out).isOk()); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 250 | ASSERT_NE(nullptr, out.get()); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 251 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 252 | // !!! libbinder-defined transaction works !!! | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 253 | EXPECT_EQ(OK, out->pingBinder()); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 254 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 255 | // !!! user-defined transaction may not be stable !!! | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 256 | // !!! so, it does not work !!! | 
|  | 257 | EXPECT_EQ(BAD_TYPE, BadStableBinder::doUserTransaction(out)); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 260 | // This is handwritten so that we can test different stability levels w/o having the AIDL | 
|  | 261 | // compiler assign them. Hand-writing binder interfaces is considered a bad practice | 
|  | 262 | // sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD! | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 263 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 264 | struct NdkBinderStable_DataClass { | 
|  | 265 | bool gotUserTransaction = false; | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 266 | }; | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 267 | void* NdkBadStableBinder_Class_onCreate(void* args) { | 
|  | 268 | LOG_ALWAYS_FATAL_IF(args != nullptr, "Takes no args"); | 
|  | 269 | return static_cast<void*>(new NdkBinderStable_DataClass); | 
|  | 270 | } | 
|  | 271 | void NdkBadStableBinder_Class_onDestroy(void* userData) { | 
|  | 272 | delete static_cast<NdkBinderStable_DataClass*>(userData); | 
|  | 273 | } | 
|  | 274 | NdkBinderStable_DataClass* NdkBadStableBinder_getUserData(AIBinder* binder) { | 
|  | 275 | LOG_ALWAYS_FATAL_IF(binder == nullptr); | 
|  | 276 | void* userData = AIBinder_getUserData(binder); | 
|  | 277 | LOG_ALWAYS_FATAL_IF(userData == nullptr, "null data - binder is remote?"); | 
|  | 278 |  | 
|  | 279 | return static_cast<NdkBinderStable_DataClass*>(userData); | 
|  | 280 | } | 
|  | 281 | binder_status_t NdkBadStableBinder_Class_onTransact( | 
|  | 282 | AIBinder* binder, transaction_code_t code, const AParcel* /*in*/, AParcel* /*out*/) { | 
|  | 283 |  | 
|  | 284 | if (code == BadStableBinder::USER_TRANSACTION) { | 
|  | 285 | ALOGE("ndk binder stability: Got user transaction"); | 
|  | 286 | NdkBadStableBinder_getUserData(binder)->gotUserTransaction = true; | 
|  | 287 | return STATUS_OK; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | return STATUS_UNKNOWN_TRANSACTION; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | static AIBinder_Class* kNdkBadStableBinder = | 
|  | 294 | AIBinder_Class_define(String8(BadStableBinder::kDescriptor).c_str(), | 
|  | 295 | NdkBadStableBinder_Class_onCreate, | 
|  | 296 | NdkBadStableBinder_Class_onDestroy, | 
|  | 297 | NdkBadStableBinder_Class_onTransact); | 
|  | 298 |  | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 299 | // for testing only to get around __ANDROID_VNDK__ guard. | 
|  | 300 | extern "C" void AIBinder_markVendorStability(AIBinder* binder); // <- BAD DO NOT COPY | 
|  | 301 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 302 | TEST(BinderStability, NdkCantCallVendorBinderInSystemContext) { | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 303 | SpAIBinder binder = SpAIBinder(AServiceManager_getService( | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 304 | String8(kSystemStabilityServer).c_str())); | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 305 |  | 
|  | 306 | std::shared_ptr<aidl::IBinderStabilityTest> remoteServer = | 
|  | 307 | aidl::IBinderStabilityTest::fromBinder(binder); | 
|  | 308 |  | 
|  | 309 | ASSERT_NE(nullptr, remoteServer.get()); | 
|  | 310 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 311 | SpAIBinder comp = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/)); | 
|  | 312 | EXPECT_TRUE(remoteServer->sendBinder(comp).isOk()); | 
|  | 313 | EXPECT_TRUE(remoteServer->sendAndCallBinder(comp).isOk()); | 
|  | 314 | EXPECT_TRUE(NdkBadStableBinder_getUserData(comp.get())->gotUserTransaction); | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 315 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 316 | SpAIBinder vendor = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/)); | 
|  | 317 | AIBinder_markVendorStability(vendor.get()); | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 318 | EXPECT_TRUE(remoteServer->sendBinder(vendor).isOk()); | 
|  | 319 | EXPECT_FALSE(remoteServer->sendAndCallBinder(vendor).isOk()); | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 320 | EXPECT_FALSE(NdkBadStableBinder_getUserData(vendor.get())->gotUserTransaction); | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 323 | class MarksStabilityInConstructor : public BBinder { | 
|  | 324 | public: | 
|  | 325 | static bool gDestructed; | 
|  | 326 |  | 
|  | 327 | MarksStabilityInConstructor() { | 
| Steven Moreland | 12300a0 | 2019-08-02 13:27:15 -0700 | [diff] [blame] | 328 | Stability::markCompilationUnit(this); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 329 | } | 
|  | 330 | ~MarksStabilityInConstructor() { | 
|  | 331 | gDestructed = true; | 
|  | 332 | } | 
|  | 333 | }; | 
|  | 334 | bool MarksStabilityInConstructor::gDestructed = false; | 
|  | 335 |  | 
|  | 336 | TEST(BinderStability, MarkingObjectNoDestructTest) { | 
|  | 337 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); | 
|  | 338 |  | 
|  | 339 | // best practice is to put this directly in an sp, but for this test, we | 
|  | 340 | // want to explicitly check what happens before that happens | 
|  | 341 | MarksStabilityInConstructor* binder = new MarksStabilityInConstructor(); | 
|  | 342 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); | 
|  | 343 |  | 
|  | 344 | sp<MarksStabilityInConstructor> binderSp = binder; | 
|  | 345 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); | 
|  | 346 |  | 
|  | 347 | binderSp = nullptr; | 
|  | 348 | ASSERT_TRUE(MarksStabilityInConstructor::gDestructed); | 
|  | 349 | } | 
|  | 350 |  | 
| Steven Moreland | cfdeaf1 | 2019-08-08 15:41:01 -0700 | [diff] [blame] | 351 | TEST(BinderStability, RemarkDies) { | 
|  | 352 | ASSERT_DEATH({ | 
|  | 353 | sp<IBinder> binder = new BBinder(); | 
|  | 354 | Stability::markCompilationUnit(binder.get()); // <-- only called for tests | 
|  | 355 | Stability::markVndk(binder.get()); // <-- only called for tests | 
|  | 356 | }, "Should only mark known object."); | 
|  | 357 | } | 
|  | 358 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 359 | int main(int argc, char** argv) { | 
|  | 360 | ::testing::InitGoogleTest(&argc, argv); | 
|  | 361 |  | 
|  | 362 | if (fork() == 0) { | 
|  | 363 | // child process | 
|  | 364 | prctl(PR_SET_PDEATHSIG, SIGHUP); | 
|  | 365 |  | 
| Steven Moreland | 43564fd | 2019-08-08 17:27:12 -0700 | [diff] [blame] | 366 | sp<IBinder> server = new MyBinderStabilityTest; | 
|  | 367 | android::defaultServiceManager()->addService(kSystemStabilityServer, server); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 368 |  | 
|  | 369 | IPCThreadState::self()->joinThreadPool(true); | 
|  | 370 | exit(1);  // should not reach | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | // This is not racey. Just giving these services some time to register before we call | 
|  | 374 | // getService which sleeps for much longer... | 
|  | 375 | usleep(10000); | 
|  | 376 |  | 
|  | 377 | return RUN_ALL_TESTS(); | 
|  | 378 | } |