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