blob: 936b82109a38379ef2c4cc59311c8cbde7770ab2 [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/BnBinderStabilityTestSub.h"
30#include "aidl/BnBinderStabilityTest.h"
Steven Morelandc709dd82019-08-05 20:30:14 -070031#include "BnBinderStabilityTestSub.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070032#include "BnBinderStabilityTest.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070033
34using namespace android;
Steven Moreland12300a02019-08-02 13:27:15 -070035using namespace ndk;
Steven Morelanddea3cf92019-07-16 18:06:55 -070036using android::binder::Status;
Steven Moreland12300a02019-08-02 13:27:15 -070037using android::internal::Stability; // for testing only!
Steven Morelanddea3cf92019-07-16 18:06:55 -070038
39const String16 kNoStabilityServer = String16("binder_stability_test_service_low");
40const String16 kCompilationUnitServer = String16("binder_stability_test_service_compl");
41const String16 kVintfServer = String16("binder_stability_test_service_vintf");
42
Steven Moreland12300a02019-08-02 13:27:15 -070043const String16 kCompilationUnitNdkServer = String16("binder_stability_test_service_compl");
44
Steven Morelandc709dd82019-08-05 20:30:14 -070045class BadStabilityTestSub : public BnBinderStabilityTestSub {
Steven Moreland12300a02019-08-02 13:27:15 -070046public:
Steven Morelandc709dd82019-08-05 20:30:14 -070047 Status userDefinedTransaction() {
48 return Status::ok();
49 }
Steven Moreland12300a02019-08-02 13:27:15 -070050
51 static sp<IBinderStabilityTestSub> system() {
52 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
53 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
54 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
55 Stability::markCompilationUnit(iface.get()); // <- BAD, NO! DO NOT COPY
56 return iface;
57 }
58
59 static sp<IBinderStabilityTestSub> vintf() {
60 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
61 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
62 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
63 Stability::markVintf(iface.get()); // <- BAD, NO! DO NOT COPY
64 return iface;
65 }
66
67 static sp<IBinderStabilityTestSub> vendor() {
68 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
69 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
70 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
71 Stability::markVndk(iface.get()); // <- BAD, NO! DO NOT COPY
72 return iface;
73 }
Steven Morelandc709dd82019-08-05 20:30:14 -070074};
75
Steven Morelanddea3cf92019-07-16 18:06:55 -070076// NO! NO! NO! Do not even think of doing something like this!
77// This is for testing! If a class like this was actually used in production,
78// it would ruin everything!
79class BadStabilityTester : public BnBinderStabilityTest {
80public:
Steven Morelandc709dd82019-08-05 20:30:14 -070081 Status sendBinder(const sp<IBinderStabilityTestSub>& /*binder*/) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -070082 return Status::ok();
83 }
Steven Morelandc709dd82019-08-05 20:30:14 -070084 Status sendAndCallBinder(const sp<IBinderStabilityTestSub>& binder) override {
Steven Moreland12300a02019-08-02 13:27:15 -070085 Stability::debugLogStability("sendAndCallBinder got binder", IInterface::asBinder(binder));
Steven Morelandc709dd82019-08-05 20:30:14 -070086 return binder->userDefinedTransaction();
87 }
88 Status returnNoStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
89 *_aidl_return = new BadStabilityTestSub();
Steven Morelanddea3cf92019-07-16 18:06:55 -070090 return Status::ok();
91 }
Steven Morelandc709dd82019-08-05 20:30:14 -070092 Status returnLocalStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Moreland12300a02019-08-02 13:27:15 -070093 *_aidl_return = BadStabilityTestSub::system();
Steven Morelanddea3cf92019-07-16 18:06:55 -070094 return Status::ok();
95 }
Steven Morelandc709dd82019-08-05 20:30:14 -070096 Status returnVintfStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Moreland12300a02019-08-02 13:27:15 -070097 *_aidl_return = BadStabilityTestSub::vintf();
Steven Morelanddea3cf92019-07-16 18:06:55 -070098 return Status::ok();
99 }
Steven Morelandc709dd82019-08-05 20:30:14 -0700100 Status returnVendorStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Moreland12300a02019-08-02 13:27:15 -0700101 *_aidl_return = BadStabilityTestSub::vendor();
Steven Morelandc709dd82019-08-05 20:30:14 -0700102 return Status::ok();
Steven Morelanddea3cf92019-07-16 18:06:55 -0700103 }
104};
105
Steven Morelandc709dd82019-08-05 20:30:14 -0700106void checkSystemStabilityBinder(const sp<IBinderStabilityTest>& complServer) {
107 EXPECT_TRUE(complServer->sendBinder(new BadStabilityTestSub()).isOk());
Steven Moreland12300a02019-08-02 13:27:15 -0700108 EXPECT_TRUE(complServer->sendBinder(BadStabilityTestSub::system()).isOk());
109 EXPECT_TRUE(complServer->sendBinder(BadStabilityTestSub::vintf()).isOk());
110 EXPECT_TRUE(complServer->sendBinder(BadStabilityTestSub::vendor()).isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700111
Steven Morelandc709dd82019-08-05 20:30:14 -0700112 EXPECT_TRUE(complServer->sendAndCallBinder(new BadStabilityTestSub()).isOk());
Steven Moreland12300a02019-08-02 13:27:15 -0700113 EXPECT_TRUE(complServer->sendAndCallBinder(BadStabilityTestSub::system()).isOk());
114 EXPECT_TRUE(complServer->sendAndCallBinder(BadStabilityTestSub::vintf()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700115
116 // !!! user-defined transaction may not be stable for remote server !!!
Steven Moreland12300a02019-08-02 13:27:15 -0700117 EXPECT_FALSE(complServer->sendAndCallBinder(BadStabilityTestSub::vendor()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700118
119 sp<IBinderStabilityTestSub> out;
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700120 EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700121 ASSERT_NE(nullptr, out.get());
122 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
123 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700124
125 EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700126 ASSERT_NE(nullptr, out.get());
127 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
128 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700129
130 EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700131 ASSERT_NE(nullptr, out.get());
132 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
133 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700134
Steven Morelandc709dd82019-08-05 20:30:14 -0700135 EXPECT_TRUE(complServer->returnVendorStabilityBinder(&out).isOk());
136 ASSERT_NE(nullptr, out.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700137
Steven Morelandc709dd82019-08-05 20:30:14 -0700138 // !!! libbinder-defined transaction works !!!
139 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700140
Steven Morelandc709dd82019-08-05 20:30:14 -0700141 // !!! user-defined transaction may not be stable !!!
142 EXPECT_FALSE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700143}
144
145TEST(BinderStability, RemoteNoStabilityServer) {
146 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer);
147 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
148
149 ASSERT_NE(nullptr, remoteServer.get());
150 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
151
Steven Morelandc709dd82019-08-05 20:30:14 -0700152 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700153}
154
155TEST(BinderStability, RemoteLowStabilityServer) {
156 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer);
157 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
158
159 ASSERT_NE(nullptr, remoteServer.get());
160 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
161
Steven Morelandc709dd82019-08-05 20:30:14 -0700162 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700163}
164
165TEST(BinderStability, RemoteVintfServer) {
166 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer);
167 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
168
169 ASSERT_NE(nullptr, remoteServer.get());
170 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
171
Steven Morelandc709dd82019-08-05 20:30:14 -0700172 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700173}
174
Steven Moreland12300a02019-08-02 13:27:15 -0700175class NdkBadStabilityTestSub : public aidl::BnBinderStabilityTestSub {
176 ScopedAStatus userDefinedTransaction() {
177 return ScopedAStatus::ok();
178 }
179};
180// for testing only to get around __ANDROID_VNDK__ guard.
181extern "C" void AIBinder_markVendorStability(AIBinder* binder); // <- BAD DO NOT COPY
182
183TEST(BinderStability, NdkClientOfRemoteServer) {
184 SpAIBinder binder = SpAIBinder(AServiceManager_getService(
185 String8(kCompilationUnitServer).c_str()));
186
187 std::shared_ptr<aidl::IBinderStabilityTest> remoteServer =
188 aidl::IBinderStabilityTest::fromBinder(binder);
189
190 ASSERT_NE(nullptr, remoteServer.get());
191
192 std::shared_ptr<aidl::IBinderStabilityTestSub> vendor = SharedRefBase::make<NdkBadStabilityTestSub>();
193
194 // TODO: not ideal: binder must be held once it is marked
195 SpAIBinder vendorBinder = vendor->asBinder();
196 AIBinder_markVendorStability(vendorBinder.get());
197
198 EXPECT_TRUE(remoteServer->sendBinder(vendor).isOk());
199 EXPECT_FALSE(remoteServer->sendAndCallBinder(vendor).isOk());
200}
201
Steven Morelanddea3cf92019-07-16 18:06:55 -0700202class MarksStabilityInConstructor : public BBinder {
203public:
204 static bool gDestructed;
205
206 MarksStabilityInConstructor() {
Steven Moreland12300a02019-08-02 13:27:15 -0700207 Stability::markCompilationUnit(this);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700208 }
209 ~MarksStabilityInConstructor() {
210 gDestructed = true;
211 }
212};
213bool MarksStabilityInConstructor::gDestructed = false;
214
215TEST(BinderStability, MarkingObjectNoDestructTest) {
216 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
217
218 // best practice is to put this directly in an sp, but for this test, we
219 // want to explicitly check what happens before that happens
220 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
221 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
222
223 sp<MarksStabilityInConstructor> binderSp = binder;
224 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
225
226 binderSp = nullptr;
227 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
228}
229
230int main(int argc, char** argv) {
231 ::testing::InitGoogleTest(&argc, argv);
232
233 if (fork() == 0) {
234 // child process
235 prctl(PR_SET_PDEATHSIG, SIGHUP);
236
237 sp<IBinder> noStability = new BadStabilityTester;
238 android::defaultServiceManager()->addService(kNoStabilityServer, noStability);
239
240 sp<IBinder> compil = new BadStabilityTester;
Steven Moreland12300a02019-08-02 13:27:15 -0700241 Stability::markCompilationUnit(compil.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700242 android::defaultServiceManager()->addService(kCompilationUnitServer, compil);
243
244 sp<IBinder> vintf = new BadStabilityTester;
Steven Moreland12300a02019-08-02 13:27:15 -0700245 Stability::markVintf(vintf.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700246 android::defaultServiceManager()->addService(kVintfServer, vintf);
247
248 IPCThreadState::self()->joinThreadPool(true);
249 exit(1); // should not reach
250 }
251
252 // This is not racey. Just giving these services some time to register before we call
253 // getService which sleeps for much longer...
254 usleep(10000);
255
256 return RUN_ALL_TESTS();
257}