blob: 0bee56c943b853b959c4ed44f2131fc0045e13e9 [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
134TEST(BinderStability, VintfStabilityServerMustBeDeclaredInManifest) {
135 sp<IBinder> vintfServer = BadStableBinder::vintf();
136
137 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
138 android::defaultServiceManager()->addService(String16("."), vintfServer));
139 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
140 android::defaultServiceManager()->addService(String16("/"), vintfServer));
141 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
142 android::defaultServiceManager()->addService(String16("/."), vintfServer));
143 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
144 android::defaultServiceManager()->addService(String16("a.d.IFoo"), vintfServer));
145 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
146 android::defaultServiceManager()->addService(String16("foo"), vintfServer));
147 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
148 android::defaultServiceManager()->addService(String16("a.d.IFoo/foo"), vintfServer));
149}
150
Steven Moreland43564fd2019-08-08 17:27:12 -0700151TEST(BinderStability, CantCallVendorBinderInSystemContext) {
152 sp<IBinder> serverBinder = android::defaultServiceManager()->getService(kSystemStabilityServer);
153 auto server = interface_cast<IBinderStabilityTest>(serverBinder);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700154
Steven Moreland43564fd2019-08-08 17:27:12 -0700155 ASSERT_NE(nullptr, server.get());
156 ASSERT_NE(nullptr, IInterface::asBinder(server)->remoteBinder());
Steven Morelandc709dd82019-08-05 20:30:14 -0700157
Steven Moreland43564fd2019-08-08 17:27:12 -0700158 EXPECT_TRUE(server->sendBinder(BadStableBinder::undef()).isOk());
159 EXPECT_TRUE(server->sendBinder(BadStableBinder::system()).isOk());
160 EXPECT_TRUE(server->sendBinder(BadStableBinder::vintf()).isOk());
161 EXPECT_TRUE(server->sendBinder(BadStableBinder::vendor()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700162
Steven Moreland43564fd2019-08-08 17:27:12 -0700163 {
164 sp<BadStableBinder> binder = BadStableBinder::undef();
165 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
166 EXPECT_TRUE(binder->gotUserTransaction);
167 }
168 {
169 sp<BadStableBinder> binder = BadStableBinder::system();
170 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
171 EXPECT_TRUE(binder->gotUserTransaction);
172 }
173 {
174 sp<BadStableBinder> binder = BadStableBinder::vintf();
175 EXPECT_TRUE(server->sendAndCallBinder(binder).isOk());
176 EXPECT_TRUE(binder->gotUserTransaction);
177 }
178 {
179 // !!! user-defined transaction may not be stable for remote server !!!
180 // !!! so, it does not work !!!
181 sp<BadStableBinder> binder = BadStableBinder::vendor();
182 EXPECT_EQ(BAD_TYPE, server->sendAndCallBinder(binder).exceptionCode());
183 EXPECT_FALSE(binder->gotUserTransaction);
184 }
185
186 sp<IBinder> out;
187 EXPECT_TRUE(server->returnNoStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700188 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700189 EXPECT_EQ(OK, out->pingBinder());
190 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700191
Steven Moreland43564fd2019-08-08 17:27:12 -0700192 EXPECT_TRUE(server->returnLocalStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700193 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700194 EXPECT_EQ(OK, out->pingBinder());
195 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700196
Steven Moreland43564fd2019-08-08 17:27:12 -0700197 EXPECT_TRUE(server->returnVintfStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700198 ASSERT_NE(nullptr, out.get());
Steven Moreland43564fd2019-08-08 17:27:12 -0700199 EXPECT_EQ(OK, out->pingBinder());
200 EXPECT_EQ(OK, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700201
Steven Moreland43564fd2019-08-08 17:27:12 -0700202 EXPECT_TRUE(server->returnVendorStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700203 ASSERT_NE(nullptr, out.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700204
Steven Morelandc709dd82019-08-05 20:30:14 -0700205 // !!! libbinder-defined transaction works !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700206 EXPECT_EQ(OK, out->pingBinder());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700207
Steven Morelandc709dd82019-08-05 20:30:14 -0700208 // !!! user-defined transaction may not be stable !!!
Steven Moreland43564fd2019-08-08 17:27:12 -0700209 // !!! so, it does not work !!!
210 EXPECT_EQ(BAD_TYPE, BadStableBinder::doUserTransaction(out));
Steven Morelanddea3cf92019-07-16 18:06:55 -0700211}
212
Steven Moreland43564fd2019-08-08 17:27:12 -0700213// This is handwritten so that we can test different stability levels w/o having the AIDL
214// compiler assign them. Hand-writing binder interfaces is considered a bad practice
215// sanity reasons. YOU SHOULD DEFINE AN AIDL INTERFACE INSTEAD!
Steven Morelanddea3cf92019-07-16 18:06:55 -0700216
Steven Moreland43564fd2019-08-08 17:27:12 -0700217struct NdkBinderStable_DataClass {
218 bool gotUserTransaction = false;
Steven Moreland12300a02019-08-02 13:27:15 -0700219};
Steven Moreland43564fd2019-08-08 17:27:12 -0700220void* NdkBadStableBinder_Class_onCreate(void* args) {
221 LOG_ALWAYS_FATAL_IF(args != nullptr, "Takes no args");
222 return static_cast<void*>(new NdkBinderStable_DataClass);
223}
224void NdkBadStableBinder_Class_onDestroy(void* userData) {
225 delete static_cast<NdkBinderStable_DataClass*>(userData);
226}
227NdkBinderStable_DataClass* NdkBadStableBinder_getUserData(AIBinder* binder) {
228 LOG_ALWAYS_FATAL_IF(binder == nullptr);
229 void* userData = AIBinder_getUserData(binder);
230 LOG_ALWAYS_FATAL_IF(userData == nullptr, "null data - binder is remote?");
231
232 return static_cast<NdkBinderStable_DataClass*>(userData);
233}
234binder_status_t NdkBadStableBinder_Class_onTransact(
235 AIBinder* binder, transaction_code_t code, const AParcel* /*in*/, AParcel* /*out*/) {
236
237 if (code == BadStableBinder::USER_TRANSACTION) {
238 ALOGE("ndk binder stability: Got user transaction");
239 NdkBadStableBinder_getUserData(binder)->gotUserTransaction = true;
240 return STATUS_OK;
241 }
242
243 return STATUS_UNKNOWN_TRANSACTION;
244}
245
246static AIBinder_Class* kNdkBadStableBinder =
247 AIBinder_Class_define(String8(BadStableBinder::kDescriptor).c_str(),
248 NdkBadStableBinder_Class_onCreate,
249 NdkBadStableBinder_Class_onDestroy,
250 NdkBadStableBinder_Class_onTransact);
251
Steven Moreland12300a02019-08-02 13:27:15 -0700252// for testing only to get around __ANDROID_VNDK__ guard.
253extern "C" void AIBinder_markVendorStability(AIBinder* binder); // <- BAD DO NOT COPY
254
Steven Moreland43564fd2019-08-08 17:27:12 -0700255TEST(BinderStability, NdkCantCallVendorBinderInSystemContext) {
Steven Moreland12300a02019-08-02 13:27:15 -0700256 SpAIBinder binder = SpAIBinder(AServiceManager_getService(
Steven Moreland43564fd2019-08-08 17:27:12 -0700257 String8(kSystemStabilityServer).c_str()));
Steven Moreland12300a02019-08-02 13:27:15 -0700258
259 std::shared_ptr<aidl::IBinderStabilityTest> remoteServer =
260 aidl::IBinderStabilityTest::fromBinder(binder);
261
262 ASSERT_NE(nullptr, remoteServer.get());
263
Steven Moreland43564fd2019-08-08 17:27:12 -0700264 SpAIBinder comp = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
265 EXPECT_TRUE(remoteServer->sendBinder(comp).isOk());
266 EXPECT_TRUE(remoteServer->sendAndCallBinder(comp).isOk());
267 EXPECT_TRUE(NdkBadStableBinder_getUserData(comp.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700268
Steven Moreland43564fd2019-08-08 17:27:12 -0700269 SpAIBinder vendor = SpAIBinder(AIBinder_new(kNdkBadStableBinder, nullptr /*args*/));
270 AIBinder_markVendorStability(vendor.get());
Steven Moreland12300a02019-08-02 13:27:15 -0700271 EXPECT_TRUE(remoteServer->sendBinder(vendor).isOk());
272 EXPECT_FALSE(remoteServer->sendAndCallBinder(vendor).isOk());
Steven Moreland43564fd2019-08-08 17:27:12 -0700273 EXPECT_FALSE(NdkBadStableBinder_getUserData(vendor.get())->gotUserTransaction);
Steven Moreland12300a02019-08-02 13:27:15 -0700274}
275
Steven Morelanddea3cf92019-07-16 18:06:55 -0700276class MarksStabilityInConstructor : public BBinder {
277public:
278 static bool gDestructed;
279
280 MarksStabilityInConstructor() {
Steven Moreland12300a02019-08-02 13:27:15 -0700281 Stability::markCompilationUnit(this);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700282 }
283 ~MarksStabilityInConstructor() {
284 gDestructed = true;
285 }
286};
287bool MarksStabilityInConstructor::gDestructed = false;
288
289TEST(BinderStability, MarkingObjectNoDestructTest) {
290 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
291
292 // best practice is to put this directly in an sp, but for this test, we
293 // want to explicitly check what happens before that happens
294 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
295 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
296
297 sp<MarksStabilityInConstructor> binderSp = binder;
298 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
299
300 binderSp = nullptr;
301 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
302}
303
Steven Morelandcfdeaf12019-08-08 15:41:01 -0700304TEST(BinderStability, RemarkDies) {
305 ASSERT_DEATH({
306 sp<IBinder> binder = new BBinder();
307 Stability::markCompilationUnit(binder.get()); // <-- only called for tests
308 Stability::markVndk(binder.get()); // <-- only called for tests
309 }, "Should only mark known object.");
310}
311
Steven Morelanddea3cf92019-07-16 18:06:55 -0700312int main(int argc, char** argv) {
313 ::testing::InitGoogleTest(&argc, argv);
314
315 if (fork() == 0) {
316 // child process
317 prctl(PR_SET_PDEATHSIG, SIGHUP);
318
Steven Moreland43564fd2019-08-08 17:27:12 -0700319 sp<IBinder> server = new MyBinderStabilityTest;
320 android::defaultServiceManager()->addService(kSystemStabilityServer, server);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700321
322 IPCThreadState::self()->joinThreadPool(true);
323 exit(1); // should not reach
324 }
325
326 // This is not racey. Just giving these services some time to register before we call
327 // getService which sleeps for much longer...
328 usleep(10000);
329
330 return RUN_ALL_TESTS();
331}