blob: 2398e1e1ef2517764ce40d9c261cde6b5c14c7f5 [file] [log] [blame]
Steven Morelanddea3cf92019-07-16 18:06:55 -07001/*
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 Singh8b802912021-03-31 12:13:56 -040017#include <android/binder_libbinder.h>
Steven Moreland12300a02019-08-02 13:27:15 -070018#include <android/binder_manager.h>
19#include <android/binder_stability.h>
Steven Morelanddea3cf92019-07-16 18:06:55 -070020#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 Moreland12300a02019-08-02 13:27:15 -070030#include "aidl/BnBinderStabilityTest.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070031#include "BnBinderStabilityTest.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070032
33using namespace android;
Steven Moreland12300a02019-08-02 13:27:15 -070034using namespace ndk;
Steven Morelanddea3cf92019-07-16 18:06:55 -070035using android::binder::Status;
Steven Moreland12300a02019-08-02 13:27:15 -070036using android::internal::Stability; // for testing only!
Steven Morelanddea3cf92019-07-16 18:06:55 -070037
Steven Moreland43564fd2019-08-08 17:27:12 -070038const String16 kSystemStabilityServer = String16("binder_stability_test_service_system");
Steven Morelanddea3cf92019-07-16 18:06:55 -070039
Steven Moreland43564fd2019-08-08 17:27:12 -070040// 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!
43class BadStableBinder : public BBinder {
Steven Moreland12300a02019-08-02 13:27:15 -070044public:
Steven Moreland43564fd2019-08-08 17:27:12 -070045 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 Morelandc709dd82019-08-05 20:30:14 -070054 }
Steven Moreland12300a02019-08-02 13:27:15 -070055
Steven Moreland43564fd2019-08-08 17:27:12 -070056 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 Moreland12300a02019-08-02 13:27:15 -070073 return iface;
74 }
75
Steven Moreland43564fd2019-08-08 17:27:12 -070076 static sp<BadStableBinder> system() {
77 sp<BadStableBinder> iface = new BadStableBinder();
78 Stability::markCompilationUnit(iface.get()); // <- for test only
Steven Moreland12300a02019-08-02 13:27:15 -070079 return iface;
80 }
81
Steven Moreland43564fd2019-08-08 17:27:12 -070082 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 Moreland12300a02019-08-02 13:27:15 -070091 return iface;
92 }
Steven Morelandc709dd82019-08-05 20:30:14 -070093};
Steven Moreland43564fd2019-08-08 17:27:12 -070094String16 BadStableBinder::kDescriptor = String16("BadStableBinder.test");
Steven Morelandc709dd82019-08-05 20:30:14 -070095
Steven Morelanddea3cf92019-07-16 18:06:55 -070096// 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 Moreland43564fd2019-08-08 17:27:12 -070099class MyBinderStabilityTest : public BnBinderStabilityTest {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700100public:
Steven Moreland43564fd2019-08-08 17:27:12 -0700101 Status sendBinder(const sp<IBinder>& /*binder*/) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700102 return Status::ok();
103 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700104 Status sendAndCallBinder(const sp<IBinder>& binder) override {
Steven Morelande5a6a872021-05-19 22:11:37 +0000105 ALOGI("Debug log stability: %s", Stability::debugToString(binder).c_str());
Steven Moreland43564fd2019-08-08 17:27:12 -0700106 return Status::fromExceptionCode(BadStableBinder::doUserTransaction(binder));
Steven Morelandc709dd82019-08-05 20:30:14 -0700107 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700108 Status returnNoStabilityBinder(sp<IBinder>* _aidl_return) override {
109 *_aidl_return = BadStableBinder::undef();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700110 return Status::ok();
111 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700112 Status returnLocalStabilityBinder(sp<IBinder>* _aidl_return) override {
113 *_aidl_return = BadStableBinder::system();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700114 return Status::ok();
115 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700116 Status returnVintfStabilityBinder(sp<IBinder>* _aidl_return) override {
117 *_aidl_return = BadStableBinder::vintf();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700118 return Status::ok();
119 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700120 Status returnVendorStabilityBinder(sp<IBinder>* _aidl_return) override {
121 *_aidl_return = BadStableBinder::vendor();
Steven Morelandc709dd82019-08-05 20:30:14 -0700122 return Status::ok();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700123 }
124};
125
Steven Moreland86a17f82019-09-10 10:18:00 -0700126TEST(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 Singh3b9ac062021-04-05 10:07:50 -0400135TEST(BinderStability, ForceDowngradeToLocalStability) {
Steven Morelande35fef32021-03-23 01:38:24 +0000136 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 Singh8b802912021-03-31 12:13:56 -0400141 Stability::forceDowngradeToLocalStability(someBinder);
142
143 EXPECT_FALSE(Stability::requiresVintfDeclaration(someBinder));
144}
145
Kalesh Singh3b9ac062021-04-05 10:07:50 -0400146TEST(BinderStability, NdkForceDowngradeToLocalStability) {
Kalesh Singh8b802912021-03-31 12:13:56 -0400147 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 Morelande35fef32021-03-23 01:38:24 +0000153
154 EXPECT_FALSE(Stability::requiresVintfDeclaration(someBinder));
155}
156
Kalesh Singh3b9ac062021-04-05 10:07:50 -0400157TEST(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 Moreland86a17f82019-09-10 10:18:00 -0700184TEST(BinderStability, VintfStabilityServerMustBeDeclaredInManifest) {
185 sp<IBinder> vintfServer = BadStableBinder::vintf();
186
Steven Morelandb82b8f82019-10-28 10:52:34 -0700187 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;
Steven Morelandedd4e072021-04-21 00:27:29 +0000195 EXPECT_EQ(std::nullopt, android::defaultServiceManager()->updatableViaApex(instance))
196 << instance8;
Steven Morelandb82b8f82019-10-28 10:52:34 -0700197 }
Steven Moreland86a17f82019-09-10 10:18:00 -0700198}
199
Devin Moore5e4c2f12021-09-09 22:36:33 +0000200TEST(BinderStability, ConnectionInfoRequiresManifestEntries) {
201 sp<IServiceManager> sm = android::defaultServiceManager();
202 sp<IBinder> systemBinder = BadStableBinder::system();
203 EXPECT_EQ(OK, sm->addService(String16("no.connection.foo"), systemBinder));
204 std::optional<android::IServiceManager::ConnectionInfo> connectionInfo;
205 connectionInfo = sm->getConnectionInfo(String16("no.connection.foo"));
206 EXPECT_EQ(connectionInfo, std::nullopt);
207}
Steven Moreland43564fd2019-08-08 17:27:12 -0700208TEST(BinderStability, CantCallVendorBinderInSystemContext) {
209 sp<IBinder> serverBinder = android::defaultServiceManager()->getService(kSystemStabilityServer);
210 auto server = interface_cast<IBinderStabilityTest>(serverBinder);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700211
Steven Moreland43564fd2019-08-08 17:27:12 -0700212 ASSERT_NE(nullptr, server.get());
213 ASSERT_NE(nullptr, IInterface::asBinder(server)->remoteBinder());
Steven Morelandc709dd82019-08-05 20:30:14 -0700214
Steven Moreland43564fd2019-08-08 17:27:12 -0700215 EXPECT_TRUE(server->sendBinder(BadStableBinder::undef()).isOk());
216 EXPECT_TRUE(server->sendBinder(BadStableBinder::system()).isOk());
217 EXPECT_TRUE(server->sendBinder(BadStableBinder::vintf()).isOk());
218 EXPECT_TRUE(server->sendBinder(BadStableBinder::vendor()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700219
Steven Moreland43564fd2019-08-08 17:27:12 -0700220 {
221 sp<BadStableBinder> binder = BadStableBinder::undef();
222 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
223 EXPECT_TRUE(binder->gotUserTransaction);
224 }
225 {
226 sp<BadStableBinder> binder = BadStableBinder::system();
227 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
228 EXPECT_TRUE(binder->gotUserTransaction);
229 }
230 {
231 sp<BadStableBinder> binder = BadStableBinder::vintf();
232 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
233 EXPECT_TRUE(binder->gotUserTransaction);
234 }
235 {
236 // !!! user-defined transaction may not be stable for remote server !!!
237 // !!! so, it does not work !!!
238 sp<BadStableBinder> binder = BadStableBinder::vendor();
239 EXPECT_EQ(BAD_TYPE, server->sendAndCallBinder(binder).exceptionCode());
240 EXPECT_FALSE(binder->gotUserTransaction);
241 }
242
243 sp<IBinder> out;
244 EXPECT_TRUE(server->returnNoStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700245 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700246 EXPECT_EQ(OK, out->pingBinder());
247 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700248
Steven Moreland43564fd2019-08-08 17:27:12 -0700249 EXPECT_TRUE(server->returnLocalStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700250 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700251 EXPECT_EQ(OK, out->pingBinder());
252 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700253
Steven Moreland43564fd2019-08-08 17:27:12 -0700254 EXPECT_TRUE(server->returnVintfStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700255 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700256 EXPECT_EQ(OK, out->pingBinder());
257 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700258
Steven Moreland43564fd2019-08-08 17:27:12 -0700259 EXPECT_TRUE(server->returnVendorStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700260 ASSERT_NE(nullptr, out.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700261
Steven Morelandc709dd82019-08-05 20:30:14 -0700262 // !!! libbinder-defined transaction works !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700263 EXPECT_EQ(OK, out->pingBinder());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700264
Steven Morelandc709dd82019-08-05 20:30:14 -0700265 // !!! user-defined transaction may not be stable !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700266 // !!! so, it does not work !!!
267 EXPECT_EQ(BAD_TYPE, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700268}
269
Steven Moreland43564fd2019-08-08 17:27:12 -0700270// This is handwritten so that we can test different stability levels w/o having the AIDL
271// compiler assign them. Hand-writing binder interfaces is considered a bad practice
272// sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD!
Steven Morelanddea3cf92019-07-16 18:06:55 -0700273
Steven Moreland43564fd2019-08-08 17:27:12 -0700274struct NdkBinderStable_DataClass {
275 bool gotUserTransaction = false;
Steven Moreland12300a02019-08-02 13:27:15 -0700276};
Steven Moreland43564fd2019-08-08 17:27:12 -0700277void* NdkBadStableBinder_Class_onCreate(void* args) {
278 LOG_ALWAYS_FATAL_IF(args != nullptr, "Takes no args");
279 return static_cast<void*>(new NdkBinderStable_DataClass);
280}
281void NdkBadStableBinder_Class_onDestroy(void* userData) {
282 delete static_cast<NdkBinderStable_DataClass*>(userData);
283}
284NdkBinderStable_DataClass* NdkBadStableBinder_getUserData(AIBinder* binder) {
285 LOG_ALWAYS_FATAL_IF(binder == nullptr);
286 void* userData = AIBinder_getUserData(binder);
287 LOG_ALWAYS_FATAL_IF(userData == nullptr, "null data - binder is remote?");
288
289 return static_cast<NdkBinderStable_DataClass*>(userData);
290}
291binder_status_t NdkBadStableBinder_Class_onTransact(
292 AIBinder* binder, transaction_code_t code, const AParcel* /*in*/, AParcel* /*out*/) {
293
294 if (code == BadStableBinder::USER_TRANSACTION) {
295 ALOGE("ndk binder stability: Got user transaction");
296 NdkBadStableBinder_getUserData(binder)->gotUserTransaction = true;
297 return STATUS_OK;
298 }
299
300 return STATUS_UNKNOWN_TRANSACTION;
301}
302
303static AIBinder_Class* kNdkBadStableBinder =
304 AIBinder_Class_define(String8(BadStableBinder::kDescriptor).c_str(),
305 NdkBadStableBinder_Class_onCreate,
306 NdkBadStableBinder_Class_onDestroy,
307 NdkBadStableBinder_Class_onTransact);
308
Steven Moreland12300a02019-08-02 13:27:15 -0700309// for testing only to get around __ANDROID_VNDK__ guard.
310extern "C" void AIBinder_markVendorStability(AIBinder* binder); // <- BAD DO NOT COPY
311
Steven Moreland43564fd2019-08-08 17:27:12 -0700312TEST(BinderStability, NdkCantCallVendorBinderInSystemContext) {
Steven Moreland12300a02019-08-02 13:27:15 -0700313 SpAIBinder binder = SpAIBinder(AServiceManager_getService(
Steven Moreland43564fd2019-08-08 17:27:12 -0700314 String8(kSystemStabilityServer).c_str()));
Steven Moreland12300a02019-08-02 13:27:15 -0700315
316 std::shared_ptr<aidl::IBinderStabilityTest> remoteServer =
317 aidl::IBinderStabilityTest::fromBinder(binder);
318
319 ASSERT_NE(nullptr, remoteServer.get());
320
Steven Moreland43564fd2019-08-08 17:27:12 -0700321 SpAIBinder comp = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
322 EXPECT_TRUE(remoteServer->sendBinder(comp).isOk());
323 EXPECT_TRUE(remoteServer->sendAndCallBinder(comp).isOk());
324 EXPECT_TRUE(NdkBadStableBinder_getUserData(comp.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700325
Steven Moreland43564fd2019-08-08 17:27:12 -0700326 SpAIBinder vendor = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
327 AIBinder_markVendorStability(vendor.get());
Steven Moreland12300a02019-08-02 13:27:15 -0700328 EXPECT_TRUE(remoteServer->sendBinder(vendor).isOk());
329 EXPECT_FALSE(remoteServer->sendAndCallBinder(vendor).isOk());
Steven Moreland43564fd2019-08-08 17:27:12 -0700330 EXPECT_FALSE(NdkBadStableBinder_getUserData(vendor.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700331}
332
Steven Morelanddea3cf92019-07-16 18:06:55 -0700333class MarksStabilityInConstructor : public BBinder {
334public:
335 static bool gDestructed;
336
337 MarksStabilityInConstructor() {
Steven Moreland12300a02019-08-02 13:27:15 -0700338 Stability::markCompilationUnit(this);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700339 }
340 ~MarksStabilityInConstructor() {
341 gDestructed = true;
342 }
343};
344bool MarksStabilityInConstructor::gDestructed = false;
345
346TEST(BinderStability, MarkingObjectNoDestructTest) {
347 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
348
349 // best practice is to put this directly in an sp, but for this test, we
350 // want to explicitly check what happens before that happens
351 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
352 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
353
354 sp<MarksStabilityInConstructor> binderSp = binder;
355 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
356
357 binderSp = nullptr;
358 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
359}
360
Steven Morelandcfdeaf12019-08-08 15:41:01 -0700361TEST(BinderStability, RemarkDies) {
362 ASSERT_DEATH({
363 sp<IBinder> binder = new BBinder();
364 Stability::markCompilationUnit(binder.get()); // <-- only called for tests
365 Stability::markVndk(binder.get()); // <-- only called for tests
366 }, "Should only mark known object.");
367}
368
Steven Morelanddea3cf92019-07-16 18:06:55 -0700369int main(int argc, char** argv) {
370 ::testing::InitGoogleTest(&argc, argv);
371
372 if (fork() == 0) {
373 // child process
374 prctl(PR_SET_PDEATHSIG, SIGHUP);
375
Steven Moreland43564fd2019-08-08 17:27:12 -0700376 sp<IBinder> server = new MyBinderStabilityTest;
377 android::defaultServiceManager()->addService(kSystemStabilityServer, server);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700378
379 IPCThreadState::self()->joinThreadPool(true);
380 exit(1); // should not reach
381 }
382
383 // This is not racey. Just giving these services some time to register before we call
384 // getService which sleeps for much longer...
385 usleep(10000);
386
387 return RUN_ALL_TESTS();
388}