blob: 42705401da401fd8dec3a374f773935781c97352 [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
Steven Moreland12300a02019-08-02 13:27:15 -070017#include <android/binder_manager.h>
18#include <android/binder_stability.h>
Steven Morelanddea3cf92019-07-16 18:06:55 -070019#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 Moreland12300a02019-08-02 13:27:15 -070029#include "aidl/BnBinderStabilityTest.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070030#include "BnBinderStabilityTest.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070031
32using namespace android;
Steven Moreland12300a02019-08-02 13:27:15 -070033using namespace ndk;
Steven Morelanddea3cf92019-07-16 18:06:55 -070034using android::binder::Status;
Steven Moreland12300a02019-08-02 13:27:15 -070035using android::internal::Stability; // for testing only!
Steven Morelanddea3cf92019-07-16 18:06:55 -070036
Steven Moreland43564fd2019-08-08 17:27:12 -070037const String16 kSystemStabilityServer = String16("binder_stability_test_service_system");
Steven Morelanddea3cf92019-07-16 18:06:55 -070038
Steven Moreland43564fd2019-08-08 17:27:12 -070039// This is handwritten so that we can test different stability levels w/o having the AIDL
40// compiler assign them. Hand-writing binder interfaces is considered a bad practice
41// sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD!
42class BadStableBinder : public BBinder {
Steven Moreland12300a02019-08-02 13:27:15 -070043public:
Steven Moreland43564fd2019-08-08 17:27:12 -070044 static constexpr uint32_t USER_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION;
45 static String16 kDescriptor;
46
47 bool gotUserTransaction = false;
48
49 static status_t doUserTransaction(const sp<IBinder>& binder) {
50 Parcel data, reply;
51 data.writeInterfaceToken(kDescriptor);
52 return binder->transact(USER_TRANSACTION, data, &reply, 0/*flags*/);
Steven Morelandc709dd82019-08-05 20:30:14 -070053 }
Steven Moreland12300a02019-08-02 13:27:15 -070054
Steven Moreland43564fd2019-08-08 17:27:12 -070055 status_t onTransact(uint32_t code,
56 const Parcel& data, Parcel* reply, uint32_t flags) override {
57 if (code == USER_TRANSACTION) {
58 // not interested in this kind of stability. Make sure
59 // we have a test failure
60 LOG_ALWAYS_FATAL_IF(!data.enforceInterface(kDescriptor));
61
62 gotUserTransaction = true;
63
64 ALOGE("binder stability: Got user transaction");
65 return OK;
66 }
67 return BBinder::onTransact(code, data, reply, flags);
68 }
69
70 static sp<BadStableBinder> undef() {
71 sp<BadStableBinder> iface = new BadStableBinder();
Steven Moreland12300a02019-08-02 13:27:15 -070072 return iface;
73 }
74
Steven Moreland43564fd2019-08-08 17:27:12 -070075 static sp<BadStableBinder> system() {
76 sp<BadStableBinder> iface = new BadStableBinder();
77 Stability::markCompilationUnit(iface.get()); // <- for test only
Steven Moreland12300a02019-08-02 13:27:15 -070078 return iface;
79 }
80
Steven Moreland43564fd2019-08-08 17:27:12 -070081 static sp<BadStableBinder> vintf() {
82 sp<BadStableBinder> iface = new BadStableBinder();
83 Stability::markVintf(iface.get()); // <- for test only
84 return iface;
85 }
86
87 static sp<BadStableBinder> vendor() {
88 sp<BadStableBinder> iface = new BadStableBinder();
89 Stability::markVndk(iface.get()); // <- for test only
Steven Moreland12300a02019-08-02 13:27:15 -070090 return iface;
91 }
Steven Morelandc709dd82019-08-05 20:30:14 -070092};
Steven Moreland43564fd2019-08-08 17:27:12 -070093String16 BadStableBinder::kDescriptor = String16("BadStableBinder.test");
Steven Morelandc709dd82019-08-05 20:30:14 -070094
Steven Morelanddea3cf92019-07-16 18:06:55 -070095// NO! NO! NO! Do not even think of doing something like this!
96// This is for testing! If a class like this was actually used in production,
97// it would ruin everything!
Steven Moreland43564fd2019-08-08 17:27:12 -070098class MyBinderStabilityTest : public BnBinderStabilityTest {
Steven Morelanddea3cf92019-07-16 18:06:55 -070099public:
Steven Moreland43564fd2019-08-08 17:27:12 -0700100 Status sendBinder(const sp<IBinder>& /*binder*/) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700101 return Status::ok();
102 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700103 Status sendAndCallBinder(const sp<IBinder>& binder) override {
104 Stability::debugLogStability("sendAndCallBinder got binder", binder);
105 return Status::fromExceptionCode(BadStableBinder::doUserTransaction(binder));
Steven Morelandc709dd82019-08-05 20:30:14 -0700106 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700107 Status returnNoStabilityBinder(sp<IBinder>* _aidl_return) override {
108 *_aidl_return = BadStableBinder::undef();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700109 return Status::ok();
110 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700111 Status returnLocalStabilityBinder(sp<IBinder>* _aidl_return) override {
112 *_aidl_return = BadStableBinder::system();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700113 return Status::ok();
114 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700115 Status returnVintfStabilityBinder(sp<IBinder>* _aidl_return) override {
116 *_aidl_return = BadStableBinder::vintf();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700117 return Status::ok();
118 }
Steven Moreland43564fd2019-08-08 17:27:12 -0700119 Status returnVendorStabilityBinder(sp<IBinder>* _aidl_return) override {
120 *_aidl_return = BadStableBinder::vendor();
Steven Morelandc709dd82019-08-05 20:30:14 -0700121 return Status::ok();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700122 }
123};
124
Steven Moreland86a17f82019-09-10 10:18:00 -0700125TEST(BinderStability, OnlyVintfStabilityBinderNeedsVintfDeclaration) {
126 EXPECT_FALSE(Stability::requiresVintfDeclaration(nullptr));
127 EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::undef()));
128 EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::system()));
129 EXPECT_FALSE(Stability::requiresVintfDeclaration(BadStableBinder::vendor()));
130
131 EXPECT_TRUE(Stability::requiresVintfDeclaration(BadStableBinder::vintf()));
132}
133
Steven Morelande35fef32021-03-23 01:38:24 +0000134TEST(BinderStability, ForceDowngradeStability) {
135 sp<IBinder> someBinder = BadStableBinder::vintf();
136
137 EXPECT_TRUE(Stability::requiresVintfDeclaration(someBinder));
138
139 // silly to do this after already using the binder, but it's for the test
140 Stability::forceDowngradeCompilationUnit(someBinder);
141
142 EXPECT_FALSE(Stability::requiresVintfDeclaration(someBinder));
143}
144
Steven Moreland86a17f82019-09-10 10:18:00 -0700145TEST(BinderStability, VintfStabilityServerMustBeDeclaredInManifest) {
146 sp<IBinder> vintfServer = BadStableBinder::vintf();
147
Steven Morelandb82b8f82019-10-28 10:52:34 -0700148 for (const char* instance8 : {
149 ".", "/", "/.", "a.d.IFoo", "foo", "a.d.IFoo/foo"
150 }) {
151 String16 instance (instance8);
152
153 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
154 android::defaultServiceManager()->addService(String16("."), vintfServer)) << instance8;
155 EXPECT_FALSE(android::defaultServiceManager()->isDeclared(instance)) << instance8;
156 }
Steven Moreland86a17f82019-09-10 10:18:00 -0700157}
158
Steven Moreland43564fd2019-08-08 17:27:12 -0700159TEST(BinderStability, CantCallVendorBinderInSystemContext) {
160 sp<IBinder> serverBinder = android::defaultServiceManager()->getService(kSystemStabilityServer);
161 auto server = interface_cast<IBinderStabilityTest>(serverBinder);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700162
Steven Moreland43564fd2019-08-08 17:27:12 -0700163 ASSERT_NE(nullptr, server.get());
164 ASSERT_NE(nullptr, IInterface::asBinder(server)->remoteBinder());
Steven Morelandc709dd82019-08-05 20:30:14 -0700165
Steven Moreland43564fd2019-08-08 17:27:12 -0700166 EXPECT_TRUE(server->sendBinder(BadStableBinder::undef()).isOk());
167 EXPECT_TRUE(server->sendBinder(BadStableBinder::system()).isOk());
168 EXPECT_TRUE(server->sendBinder(BadStableBinder::vintf()).isOk());
169 EXPECT_TRUE(server->sendBinder(BadStableBinder::vendor()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700170
Steven Moreland43564fd2019-08-08 17:27:12 -0700171 {
172 sp<BadStableBinder> binder = BadStableBinder::undef();
173 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
174 EXPECT_TRUE(binder->gotUserTransaction);
175 }
176 {
177 sp<BadStableBinder> binder = BadStableBinder::system();
178 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
179 EXPECT_TRUE(binder->gotUserTransaction);
180 }
181 {
182 sp<BadStableBinder> binder = BadStableBinder::vintf();
183 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
184 EXPECT_TRUE(binder->gotUserTransaction);
185 }
186 {
187 // !!! user-defined transaction may not be stable for remote server !!!
188 // !!! so, it does not work !!!
189 sp<BadStableBinder> binder = BadStableBinder::vendor();
190 EXPECT_EQ(BAD_TYPE, server->sendAndCallBinder(binder).exceptionCode());
191 EXPECT_FALSE(binder->gotUserTransaction);
192 }
193
194 sp<IBinder> out;
195 EXPECT_TRUE(server->returnNoStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700196 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700197 EXPECT_EQ(OK, out->pingBinder());
198 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700199
Steven Moreland43564fd2019-08-08 17:27:12 -0700200 EXPECT_TRUE(server->returnLocalStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700201 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700202 EXPECT_EQ(OK, out->pingBinder());
203 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700204
Steven Moreland43564fd2019-08-08 17:27:12 -0700205 EXPECT_TRUE(server->returnVintfStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700206 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700207 EXPECT_EQ(OK, out->pingBinder());
208 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700209
Steven Moreland43564fd2019-08-08 17:27:12 -0700210 EXPECT_TRUE(server->returnVendorStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700211 ASSERT_NE(nullptr, out.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700212
Steven Morelandc709dd82019-08-05 20:30:14 -0700213 // !!! libbinder-defined transaction works !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700214 EXPECT_EQ(OK, out->pingBinder());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700215
Steven Morelandc709dd82019-08-05 20:30:14 -0700216 // !!! user-defined transaction may not be stable !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700217 // !!! so, it does not work !!!
218 EXPECT_EQ(BAD_TYPE, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700219}
220
Steven Moreland43564fd2019-08-08 17:27:12 -0700221// This is handwritten so that we can test different stability levels w/o having the AIDL
222// compiler assign them. Hand-writing binder interfaces is considered a bad practice
223// sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD!
Steven Morelanddea3cf92019-07-16 18:06:55 -0700224
Steven Moreland43564fd2019-08-08 17:27:12 -0700225struct NdkBinderStable_DataClass {
226 bool gotUserTransaction = false;
Steven Moreland12300a02019-08-02 13:27:15 -0700227};
Steven Moreland43564fd2019-08-08 17:27:12 -0700228void* NdkBadStableBinder_Class_onCreate(void* args) {
229 LOG_ALWAYS_FATAL_IF(args != nullptr, "Takes no args");
230 return static_cast<void*>(new NdkBinderStable_DataClass);
231}
232void NdkBadStableBinder_Class_onDestroy(void* userData) {
233 delete static_cast<NdkBinderStable_DataClass*>(userData);
234}
235NdkBinderStable_DataClass* NdkBadStableBinder_getUserData(AIBinder* binder) {
236 LOG_ALWAYS_FATAL_IF(binder == nullptr);
237 void* userData = AIBinder_getUserData(binder);
238 LOG_ALWAYS_FATAL_IF(userData == nullptr, "null data - binder is remote?");
239
240 return static_cast<NdkBinderStable_DataClass*>(userData);
241}
242binder_status_t NdkBadStableBinder_Class_onTransact(
243 AIBinder* binder, transaction_code_t code, const AParcel* /*in*/, AParcel* /*out*/) {
244
245 if (code == BadStableBinder::USER_TRANSACTION) {
246 ALOGE("ndk binder stability: Got user transaction");
247 NdkBadStableBinder_getUserData(binder)->gotUserTransaction = true;
248 return STATUS_OK;
249 }
250
251 return STATUS_UNKNOWN_TRANSACTION;
252}
253
254static AIBinder_Class* kNdkBadStableBinder =
255 AIBinder_Class_define(String8(BadStableBinder::kDescriptor).c_str(),
256 NdkBadStableBinder_Class_onCreate,
257 NdkBadStableBinder_Class_onDestroy,
258 NdkBadStableBinder_Class_onTransact);
259
Steven Moreland12300a02019-08-02 13:27:15 -0700260// for testing only to get around __ANDROID_VNDK__ guard.
261extern "C" void AIBinder_markVendorStability(AIBinder* binder); // <- BAD DO NOT COPY
262
Steven Moreland43564fd2019-08-08 17:27:12 -0700263TEST(BinderStability, NdkCantCallVendorBinderInSystemContext) {
Steven Moreland12300a02019-08-02 13:27:15 -0700264 SpAIBinder binder = SpAIBinder(AServiceManager_getService(
Steven Moreland43564fd2019-08-08 17:27:12 -0700265 String8(kSystemStabilityServer).c_str()));
Steven Moreland12300a02019-08-02 13:27:15 -0700266
267 std::shared_ptr<aidl::IBinderStabilityTest> remoteServer =
268 aidl::IBinderStabilityTest::fromBinder(binder);
269
270 ASSERT_NE(nullptr, remoteServer.get());
271
Steven Moreland43564fd2019-08-08 17:27:12 -0700272 SpAIBinder comp = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
273 EXPECT_TRUE(remoteServer->sendBinder(comp).isOk());
274 EXPECT_TRUE(remoteServer->sendAndCallBinder(comp).isOk());
275 EXPECT_TRUE(NdkBadStableBinder_getUserData(comp.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700276
Steven Moreland43564fd2019-08-08 17:27:12 -0700277 SpAIBinder vendor = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
278 AIBinder_markVendorStability(vendor.get());
Steven Moreland12300a02019-08-02 13:27:15 -0700279 EXPECT_TRUE(remoteServer->sendBinder(vendor).isOk());
280 EXPECT_FALSE(remoteServer->sendAndCallBinder(vendor).isOk());
Steven Moreland43564fd2019-08-08 17:27:12 -0700281 EXPECT_FALSE(NdkBadStableBinder_getUserData(vendor.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700282}
283
Steven Morelanddea3cf92019-07-16 18:06:55 -0700284class MarksStabilityInConstructor : public BBinder {
285public:
286 static bool gDestructed;
287
288 MarksStabilityInConstructor() {
Steven Moreland12300a02019-08-02 13:27:15 -0700289 Stability::markCompilationUnit(this);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700290 }
291 ~MarksStabilityInConstructor() {
292 gDestructed = true;
293 }
294};
295bool MarksStabilityInConstructor::gDestructed = false;
296
297TEST(BinderStability, MarkingObjectNoDestructTest) {
298 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
299
300 // best practice is to put this directly in an sp, but for this test, we
301 // want to explicitly check what happens before that happens
302 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
303 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
304
305 sp<MarksStabilityInConstructor> binderSp = binder;
306 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
307
308 binderSp = nullptr;
309 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
310}
311
Steven Morelandcfdeaf12019-08-08 15:41:01 -0700312TEST(BinderStability, RemarkDies) {
313 ASSERT_DEATH({
314 sp<IBinder> binder = new BBinder();
315 Stability::markCompilationUnit(binder.get()); // <-- only called for tests
316 Stability::markVndk(binder.get()); // <-- only called for tests
317 }, "Should only mark known object.");
318}
319
Steven Morelanddea3cf92019-07-16 18:06:55 -0700320int main(int argc, char** argv) {
321 ::testing::InitGoogleTest(&argc, argv);
322
323 if (fork() == 0) {
324 // child process
325 prctl(PR_SET_PDEATHSIG, SIGHUP);
326
Steven Moreland43564fd2019-08-08 17:27:12 -0700327 sp<IBinder> server = new MyBinderStabilityTest;
328 android::defaultServiceManager()->addService(kSystemStabilityServer, server);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700329
330 IPCThreadState::self()->joinThreadPool(true);
331 exit(1); // should not reach
332 }
333
334 // This is not racey. Just giving these services some time to register before we call
335 // getService which sleeps for much longer...
336 usleep(10000);
337
338 return RUN_ALL_TESTS();
339}