blob: fb51f984578c2a9492ce3fa05f1afa51c1022d66 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 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
17#include <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Martijn Coenen45b07b42017-08-09 12:07:45 +020031#include <sys/epoll.h>
32
Riley Andrews06b01ad2014-12-18 12:10:08 -080033#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
34
35using namespace android;
36
Sherry Yang336cdd32017-07-24 14:12:27 -070037static ::testing::AssertionResult IsPageAligned(void *buf) {
38 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
39 return ::testing::AssertionSuccess();
40 else
41 return ::testing::AssertionFailure() << buf << " is not page aligned";
42}
43
Riley Andrews06b01ad2014-12-18 12:10:08 -080044static testing::Environment* binder_env;
45static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070046static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080047static char binderserverarg[] = "--binderserver";
48
49static String16 binderLibTestServiceName = String16("test.binderLib");
50
51enum BinderLibTestTranscationCode {
52 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
53 BINDER_LIB_TEST_REGISTER_SERVER,
54 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020055 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080056 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070057 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020058 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080059 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070060 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_GET_ID_TRANSACTION,
62 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
63 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
64 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
65 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
66 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
67 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090068 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
70 BINDER_LIB_TEST_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
72 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070073 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080074};
75
Martijn Coenen45b07b42017-08-09 12:07:45 +020076pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080077{
78 int ret;
79 pid_t pid;
80 status_t status;
81 int pipefd[2];
82 char stri[16];
83 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020084 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080085 char *childargv[] = {
86 binderservername,
87 binderserverarg,
88 stri,
89 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020090 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070091 binderserversuffix,
Yi Kongfdd8da92018-06-07 17:52:27 -070092 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080093 };
94
95 ret = pipe(pipefd);
96 if (ret < 0)
97 return ret;
98
99 snprintf(stri, sizeof(stri), "%d", arg2);
100 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200101 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800102
103 pid = fork();
104 if (pid == -1)
105 return pid;
106 if (pid == 0) {
107 close(pipefd[0]);
108 execv(binderservername, childargv);
109 status = -errno;
110 write(pipefd[1], &status, sizeof(status));
111 fprintf(stderr, "execv failed, %s\n", strerror(errno));
112 _exit(EXIT_FAILURE);
113 }
114 close(pipefd[1]);
115 ret = read(pipefd[0], &status, sizeof(status));
116 //printf("pipe read returned %d, status %d\n", ret, status);
117 close(pipefd[0]);
118 if (ret == sizeof(status)) {
119 ret = status;
120 } else {
121 kill(pid, SIGKILL);
122 if (ret >= 0) {
123 ret = NO_INIT;
124 }
125 }
126 if (ret < 0) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700127 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800128 return ret;
129 }
130 return pid;
131}
132
133class BinderLibTestEnv : public ::testing::Environment {
134 public:
135 BinderLibTestEnv() {}
136 sp<IBinder> getServer(void) {
137 return m_server;
138 }
139
140 private:
141 virtual void SetUp() {
142 m_serverpid = start_server_process(0);
143 //printf("m_serverpid %d\n", m_serverpid);
144 ASSERT_GT(m_serverpid, 0);
145
146 sp<IServiceManager> sm = defaultServiceManager();
147 //printf("%s: pid %d, get service\n", __func__, m_pid);
148 m_server = sm->getService(binderLibTestServiceName);
Yi Kongfdd8da92018-06-07 17:52:27 -0700149 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800150 //printf("%s: pid %d, get service done\n", __func__, m_pid);
151 }
152 virtual void TearDown() {
153 status_t ret;
154 Parcel data, reply;
155 int exitStatus;
156 pid_t pid;
157
158 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700159 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800160 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
161 EXPECT_EQ(0, ret);
162 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
163 EXPECT_EQ(0, ret);
164 }
165 if (m_serverpid > 0) {
166 //printf("wait for %d\n", m_pids[i]);
167 pid = wait(&exitStatus);
168 EXPECT_EQ(m_serverpid, pid);
169 EXPECT_TRUE(WIFEXITED(exitStatus));
170 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
171 }
172 }
173
174 pid_t m_serverpid;
175 sp<IBinder> m_server;
176};
177
178class BinderLibTest : public ::testing::Test {
179 public:
180 virtual void SetUp() {
181 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
182 }
183 virtual void TearDown() {
184 }
185 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200186 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800187 {
188 int ret;
189 int32_t id;
190 Parcel data, reply;
191 sp<IBinder> binder;
192
Martijn Coenen45b07b42017-08-09 12:07:45 +0200193 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800194 EXPECT_EQ(NO_ERROR, ret);
195
Yi Kongfdd8da92018-06-07 17:52:27 -0700196 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 binder = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700198 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 ret = reply.readInt32(&id);
200 EXPECT_EQ(NO_ERROR, ret);
201 if (idPtr)
202 *idPtr = id;
203 return binder;
204 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200205
Yi Kongfdd8da92018-06-07 17:52:27 -0700206 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200207 {
208 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
209 }
210
Yi Kongfdd8da92018-06-07 17:52:27 -0700211 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200212 {
213 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
214 }
215
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 void waitForReadData(int fd, int timeout_ms) {
217 int ret;
218 pollfd pfd = pollfd();
219
220 pfd.fd = fd;
221 pfd.events = POLLIN;
222 ret = poll(&pfd, 1, timeout_ms);
223 EXPECT_EQ(1, ret);
224 }
225
226 sp<IBinder> m_server;
227};
228
229class BinderLibTestBundle : public Parcel
230{
231 public:
232 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800233 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800234 int32_t mark;
235 int32_t bundleLen;
236 size_t pos;
237
238 if (source->readInt32(&mark))
239 return;
240 if (mark != MARK_START)
241 return;
242 if (source->readInt32(&bundleLen))
243 return;
244 pos = source->dataPosition();
245 if (Parcel::appendFrom(source, pos, bundleLen))
246 return;
247 source->setDataPosition(pos + bundleLen);
248 if (source->readInt32(&mark))
249 return;
250 if (mark != MARK_END)
251 return;
252 m_isValid = true;
253 setDataPosition(0);
254 }
255 void appendTo(Parcel *dest) {
256 dest->writeInt32(MARK_START);
257 dest->writeInt32(dataSize());
258 dest->appendFrom(this, 0, dataSize());
259 dest->writeInt32(MARK_END);
260 };
261 bool isValid(void) {
262 return m_isValid;
263 }
264 private:
265 enum {
266 MARK_START = B_PACK_CHARS('B','T','B','S'),
267 MARK_END = B_PACK_CHARS('B','T','B','E'),
268 };
269 bool m_isValid;
270};
271
272class BinderLibTestEvent
273{
274 public:
275 BinderLibTestEvent(void)
276 : m_eventTriggered(false)
277 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700278 pthread_mutex_init(&m_waitMutex, nullptr);
279 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800280 }
281 int waitEvent(int timeout_s)
282 {
283 int ret;
284 pthread_mutex_lock(&m_waitMutex);
285 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800286 struct timespec ts;
287 clock_gettime(CLOCK_REALTIME, &ts);
288 ts.tv_sec += timeout_s;
289 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800290 }
291 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
292 pthread_mutex_unlock(&m_waitMutex);
293 return ret;
294 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200295 pthread_t getTriggeringThread()
296 {
297 return m_triggeringThread;
298 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800299 protected:
300 void triggerEvent(void) {
301 pthread_mutex_lock(&m_waitMutex);
302 pthread_cond_signal(&m_waitCond);
303 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200304 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 pthread_mutex_unlock(&m_waitMutex);
306 };
307 private:
308 pthread_mutex_t m_waitMutex;
309 pthread_cond_t m_waitCond;
310 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200311 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800312};
313
314class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
315{
316 public:
317 BinderLibTestCallBack()
318 : m_result(NOT_ENOUGH_DATA)
Yi Kongfdd8da92018-06-07 17:52:27 -0700319 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320 {
321 }
322 status_t getResult(void)
323 {
324 return m_result;
325 }
326
327 private:
328 virtual status_t onTransact(uint32_t code,
329 const Parcel& data, Parcel* reply,
330 uint32_t flags = 0)
331 {
332 (void)reply;
333 (void)flags;
334 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200335 case BINDER_LIB_TEST_CALL_BACK: {
336 status_t status = data.readInt32(&m_result);
337 if (status != NO_ERROR) {
338 m_result = status;
339 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800340 triggerEvent();
341 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200342 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700343 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
344 sp<IBinder> server;
345 int ret;
346 const uint8_t *buf = data.data();
347 size_t size = data.dataSize();
348 if (m_prev_end) {
349 /* 64-bit kernel needs at most 8 bytes to align buffer end */
350 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
351 } else {
352 EXPECT_TRUE(IsPageAligned((void *)buf));
353 }
354
355 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
356
357 if (size > 0) {
358 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
359 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
360 data, reply);
361 EXPECT_EQ(NO_ERROR, ret);
362 }
363 return NO_ERROR;
364 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800365 default:
366 return UNKNOWN_TRANSACTION;
367 }
368 }
369
370 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700371 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800372};
373
374class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
375{
376 private:
377 virtual void binderDied(const wp<IBinder>& who) {
378 (void)who;
379 triggerEvent();
380 };
381};
382
383TEST_F(BinderLibTest, NopTransaction) {
384 status_t ret;
385 Parcel data, reply;
386 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
387 EXPECT_EQ(NO_ERROR, ret);
388}
389
390TEST_F(BinderLibTest, SetError) {
391 int32_t testValue[] = { 0, -123, 123 };
392 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
393 status_t ret;
394 Parcel data, reply;
395 data.writeInt32(testValue[i]);
396 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
397 EXPECT_EQ(testValue[i], ret);
398 }
399}
400
401TEST_F(BinderLibTest, GetId) {
402 status_t ret;
403 int32_t id;
404 Parcel data, reply;
405 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
406 EXPECT_EQ(NO_ERROR, ret);
407 ret = reply.readInt32(&id);
408 EXPECT_EQ(NO_ERROR, ret);
409 EXPECT_EQ(0, id);
410}
411
412TEST_F(BinderLibTest, PtrSize) {
413 status_t ret;
414 int32_t ptrsize;
415 Parcel data, reply;
416 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700417 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800418 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
419 EXPECT_EQ(NO_ERROR, ret);
420 ret = reply.readInt32(&ptrsize);
421 EXPECT_EQ(NO_ERROR, ret);
422 RecordProperty("TestPtrSize", sizeof(void *));
423 RecordProperty("ServerPtrSize", sizeof(void *));
424}
425
426TEST_F(BinderLibTest, IndirectGetId2)
427{
428 status_t ret;
429 int32_t id;
430 int32_t count;
431 Parcel data, reply;
432 int32_t serverId[3];
433
434 data.writeInt32(ARRAY_SIZE(serverId));
435 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
436 sp<IBinder> server;
437 BinderLibTestBundle datai;
438
439 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700440 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800441 data.writeStrongBinder(server);
442 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
443 datai.appendTo(&data);
444 }
445
446 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
447 ASSERT_EQ(NO_ERROR, ret);
448
449 ret = reply.readInt32(&id);
450 ASSERT_EQ(NO_ERROR, ret);
451 EXPECT_EQ(0, id);
452
453 ret = reply.readInt32(&count);
454 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700455 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800456
457 for (size_t i = 0; i < (size_t)count; i++) {
458 BinderLibTestBundle replyi(&reply);
459 EXPECT_TRUE(replyi.isValid());
460 ret = replyi.readInt32(&id);
461 EXPECT_EQ(NO_ERROR, ret);
462 EXPECT_EQ(serverId[i], id);
463 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
464 }
465
466 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
467}
468
469TEST_F(BinderLibTest, IndirectGetId3)
470{
471 status_t ret;
472 int32_t id;
473 int32_t count;
474 Parcel data, reply;
475 int32_t serverId[3];
476
477 data.writeInt32(ARRAY_SIZE(serverId));
478 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
479 sp<IBinder> server;
480 BinderLibTestBundle datai;
481 BinderLibTestBundle datai2;
482
483 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700484 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800485 data.writeStrongBinder(server);
486 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
487
488 datai.writeInt32(1);
489 datai.writeStrongBinder(m_server);
490 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
491 datai2.appendTo(&datai);
492
493 datai.appendTo(&data);
494 }
495
496 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
497 ASSERT_EQ(NO_ERROR, ret);
498
499 ret = reply.readInt32(&id);
500 ASSERT_EQ(NO_ERROR, ret);
501 EXPECT_EQ(0, id);
502
503 ret = reply.readInt32(&count);
504 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700505 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800506
507 for (size_t i = 0; i < (size_t)count; i++) {
508 int32_t counti;
509
510 BinderLibTestBundle replyi(&reply);
511 EXPECT_TRUE(replyi.isValid());
512 ret = replyi.readInt32(&id);
513 EXPECT_EQ(NO_ERROR, ret);
514 EXPECT_EQ(serverId[i], id);
515
516 ret = replyi.readInt32(&counti);
517 ASSERT_EQ(NO_ERROR, ret);
518 EXPECT_EQ(1, counti);
519
520 BinderLibTestBundle replyi2(&replyi);
521 EXPECT_TRUE(replyi2.isValid());
522 ret = replyi2.readInt32(&id);
523 EXPECT_EQ(NO_ERROR, ret);
524 EXPECT_EQ(0, id);
525 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
526
527 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
528 }
529
530 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
531}
532
533TEST_F(BinderLibTest, CallBack)
534{
535 status_t ret;
536 Parcel data, reply;
537 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
538 data.writeStrongBinder(callBack);
539 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
540 EXPECT_EQ(NO_ERROR, ret);
541 ret = callBack->waitEvent(5);
542 EXPECT_EQ(NO_ERROR, ret);
543 ret = callBack->getResult();
544 EXPECT_EQ(NO_ERROR, ret);
545}
546
547TEST_F(BinderLibTest, AddServer)
548{
549 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700550 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800551}
552
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553TEST_F(BinderLibTest, DeathNotificationStrongRef)
554{
555 status_t ret;
556 sp<IBinder> sbinder;
557
558 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
559
560 {
561 sp<IBinder> binder = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700562 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800563 ret = binder->linkToDeath(testDeathRecipient);
564 EXPECT_EQ(NO_ERROR, ret);
565 sbinder = binder;
566 }
567 {
568 Parcel data, reply;
569 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
570 EXPECT_EQ(0, ret);
571 }
572 IPCThreadState::self()->flushCommands();
573 ret = testDeathRecipient->waitEvent(5);
574 EXPECT_EQ(NO_ERROR, ret);
575 ret = sbinder->unlinkToDeath(testDeathRecipient);
576 EXPECT_EQ(DEAD_OBJECT, ret);
577}
578
579TEST_F(BinderLibTest, DeathNotificationMultiple)
580{
581 status_t ret;
582 const int clientcount = 2;
583 sp<IBinder> target;
584 sp<IBinder> linkedclient[clientcount];
585 sp<BinderLibTestCallBack> callBack[clientcount];
586 sp<IBinder> passiveclient[clientcount];
587
588 target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700589 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800590 for (int i = 0; i < clientcount; i++) {
591 {
592 Parcel data, reply;
593
594 linkedclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700595 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800596 callBack[i] = new BinderLibTestCallBack();
597 data.writeStrongBinder(target);
598 data.writeStrongBinder(callBack[i]);
599 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
600 EXPECT_EQ(NO_ERROR, ret);
601 }
602 {
603 Parcel data, reply;
604
605 passiveclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700606 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800607 data.writeStrongBinder(target);
608 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
609 EXPECT_EQ(NO_ERROR, ret);
610 }
611 }
612 {
613 Parcel data, reply;
614 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
615 EXPECT_EQ(0, ret);
616 }
617
618 for (int i = 0; i < clientcount; i++) {
619 ret = callBack[i]->waitEvent(5);
620 EXPECT_EQ(NO_ERROR, ret);
621 ret = callBack[i]->getResult();
622 EXPECT_EQ(NO_ERROR, ret);
623 }
624}
625
Martijn Coenenf7100e42017-07-31 12:14:09 +0200626TEST_F(BinderLibTest, DeathNotificationThread)
627{
628 status_t ret;
629 sp<BinderLibTestCallBack> callback;
630 sp<IBinder> target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700631 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200632 sp<IBinder> client = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700633 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200634
635 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
636
637 ret = target->linkToDeath(testDeathRecipient);
638 EXPECT_EQ(NO_ERROR, ret);
639
640 {
641 Parcel data, reply;
642 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
643 EXPECT_EQ(0, ret);
644 }
645
646 /* Make sure it's dead */
647 testDeathRecipient->waitEvent(5);
648
649 /* Now, pass the ref to another process and ask that process to
650 * call linkToDeath() on it, and wait for a response. This tests
651 * two things:
652 * 1) You still get death notifications when calling linkToDeath()
653 * on a ref that is already dead when it was passed to you.
654 * 2) That death notifications are not directly pushed to the thread
655 * registering them, but to the threadpool (proc workqueue) instead.
656 *
657 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
658 * is blocked on a condition variable waiting for the death notification to be
659 * called; therefore, that thread is not available for handling proc work.
660 * So, if the death notification was pushed to the thread workqueue, the callback
661 * would never be called, and the test would timeout and fail.
662 *
663 * Note that we can't do this part of the test from this thread itself, because
664 * the binder driver would only push death notifications to the thread if
665 * it is a looper thread, which this thread is not.
666 *
667 * See b/23525545 for details.
668 */
669 {
670 Parcel data, reply;
671
672 callback = new BinderLibTestCallBack();
673 data.writeStrongBinder(target);
674 data.writeStrongBinder(callback);
675 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
676 EXPECT_EQ(NO_ERROR, ret);
677 }
678
679 ret = callback->waitEvent(5);
680 EXPECT_EQ(NO_ERROR, ret);
681 ret = callback->getResult();
682 EXPECT_EQ(NO_ERROR, ret);
683}
684
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685TEST_F(BinderLibTest, PassFile) {
686 int ret;
687 int pipefd[2];
688 uint8_t buf[1] = { 0 };
689 uint8_t write_value = 123;
690
691 ret = pipe2(pipefd, O_NONBLOCK);
692 ASSERT_EQ(0, ret);
693
694 {
695 Parcel data, reply;
696 uint8_t writebuf[1] = { write_value };
697
698 ret = data.writeFileDescriptor(pipefd[1], true);
699 EXPECT_EQ(NO_ERROR, ret);
700
701 ret = data.writeInt32(sizeof(writebuf));
702 EXPECT_EQ(NO_ERROR, ret);
703
704 ret = data.write(writebuf, sizeof(writebuf));
705 EXPECT_EQ(NO_ERROR, ret);
706
707 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
708 EXPECT_EQ(NO_ERROR, ret);
709 }
710
711 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700712 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800713 EXPECT_EQ(write_value, buf[0]);
714
715 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
716
717 ret = read(pipefd[0], buf, sizeof(buf));
718 EXPECT_EQ(0, ret);
719
720 close(pipefd[0]);
721}
722
Ryo Hashimotobf551892018-05-31 16:58:35 +0900723TEST_F(BinderLibTest, PassParcelFileDescriptor) {
724 const int datasize = 123;
725 std::vector<uint8_t> writebuf(datasize);
726 for (size_t i = 0; i < writebuf.size(); ++i) {
727 writebuf[i] = i;
728 }
729
730 android::base::unique_fd read_end, write_end;
731 {
732 int pipefd[2];
733 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
734 read_end.reset(pipefd[0]);
735 write_end.reset(pipefd[1]);
736 }
737 {
738 Parcel data;
739 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
740 write_end.reset();
741 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
742 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
743
744 Parcel reply;
745 EXPECT_EQ(NO_ERROR,
746 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
747 &reply));
748 }
749 std::vector<uint8_t> readbuf(datasize);
750 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
751 EXPECT_EQ(writebuf, readbuf);
752
753 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
754
755 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
756}
757
Riley Andrews06b01ad2014-12-18 12:10:08 -0800758TEST_F(BinderLibTest, PromoteLocal) {
759 sp<IBinder> strong = new BBinder();
760 wp<IBinder> weak = strong;
761 sp<IBinder> strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700762 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763 EXPECT_EQ(strong, strong_from_weak);
Yi Kongfdd8da92018-06-07 17:52:27 -0700764 strong = nullptr;
765 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800766 strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700767 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768}
769
770TEST_F(BinderLibTest, PromoteRemote) {
771 int ret;
772 Parcel data, reply;
773 sp<IBinder> strong = new BBinder();
774 sp<IBinder> server = addServer();
775
Yi Kongfdd8da92018-06-07 17:52:27 -0700776 ASSERT_TRUE(server != nullptr);
777 ASSERT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800778
779 ret = data.writeWeakBinder(strong);
780 EXPECT_EQ(NO_ERROR, ret);
781
782 ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
783 EXPECT_GE(ret, 0);
784}
785
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700786TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
787 status_t ret;
788 Parcel data, reply;
789
790 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
791 EXPECT_EQ(NO_ERROR, ret);
792
793 const flat_binder_object *fb = reply.readObject(false);
Yi Kongfdd8da92018-06-07 17:52:27 -0700794 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800795 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
796 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
797 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
798 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700799}
800
Connor O'Brien52be2c92016-09-20 14:18:08 -0700801TEST_F(BinderLibTest, FreedBinder) {
802 status_t ret;
803
804 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700805 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700806
807 __u32 freedHandle;
808 wp<IBinder> keepFreedBinder;
809 {
810 Parcel data, reply;
811 data.writeBool(false); /* request weak reference */
812 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
813 ASSERT_EQ(NO_ERROR, ret);
814 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
815 freedHandle = freed->handle;
816 /* Add a weak ref to the freed binder so the driver does not
817 * delete its reference to it - otherwise the transaction
818 * fails regardless of whether the driver is fixed.
819 */
820 keepFreedBinder = reply.readWeakBinder();
821 }
822 {
823 Parcel data, reply;
824 data.writeStrongBinder(server);
825 /* Replace original handle with handle to the freed binder */
826 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
827 __u32 oldHandle = strong->handle;
828 strong->handle = freedHandle;
829 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
830 /* Returns DEAD_OBJECT (-32) if target crashes and
831 * FAILED_TRANSACTION if the driver rejects the invalid
832 * object.
833 */
834 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
835 /* Restore original handle so parcel destructor does not use
836 * the wrong handle.
837 */
838 strong->handle = oldHandle;
839 }
840}
841
Sherry Yang336cdd32017-07-24 14:12:27 -0700842TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
843 status_t ret;
844 Parcel data, reply;
845 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
846 for (int i = 0; i < 2; i++) {
847 BinderLibTestBundle datai;
848 datai.appendFrom(&data, 0, data.dataSize());
849
850 data.freeData();
851 data.writeInt32(1);
852 data.writeStrongBinder(callBack);
853 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
854
855 datai.appendTo(&data);
856 }
857 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
858 EXPECT_EQ(NO_ERROR, ret);
859}
860
Martijn Coenen45b07b42017-08-09 12:07:45 +0200861TEST_F(BinderLibTest, OnewayQueueing)
862{
863 status_t ret;
864 Parcel data, data2;
865
866 sp<IBinder> pollServer = addPollServer();
867
868 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
869 data.writeStrongBinder(callBack);
870 data.writeInt32(500000); // delay in us before calling back
871
872 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
873 data2.writeStrongBinder(callBack2);
874 data2.writeInt32(0); // delay in us
875
Yi Kongfdd8da92018-06-07 17:52:27 -0700876 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200877 EXPECT_EQ(NO_ERROR, ret);
878
879 // The delay ensures that this second transaction will end up on the async_todo list
880 // (for a single-threaded server)
Yi Kongfdd8da92018-06-07 17:52:27 -0700881 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200882 EXPECT_EQ(NO_ERROR, ret);
883
884 // The server will ensure that the two transactions are handled in the expected order;
885 // If the ordering is not as expected, an error will be returned through the callbacks.
886 ret = callBack->waitEvent(2);
887 EXPECT_EQ(NO_ERROR, ret);
888 ret = callBack->getResult();
889 EXPECT_EQ(NO_ERROR, ret);
890
891 ret = callBack2->waitEvent(2);
892 EXPECT_EQ(NO_ERROR, ret);
893 ret = callBack2->getResult();
894 EXPECT_EQ(NO_ERROR, ret);
895}
896
Riley Andrews06b01ad2014-12-18 12:10:08 -0800897class BinderLibTestService : public BBinder
898{
899 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800900 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800901 : m_id(id)
902 , m_nextServerId(id + 1)
903 , m_serverStartRequested(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700904 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800905 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700906 pthread_mutex_init(&m_serverWaitMutex, nullptr);
907 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800908 }
909 ~BinderLibTestService()
910 {
911 exit(EXIT_SUCCESS);
912 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200913
914 void processPendingCall() {
Yi Kongfdd8da92018-06-07 17:52:27 -0700915 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200916 Parcel data;
917 data.writeInt32(NO_ERROR);
918 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kongfdd8da92018-06-07 17:52:27 -0700919 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200920 }
921 }
922
Riley Andrews06b01ad2014-12-18 12:10:08 -0800923 virtual status_t onTransact(uint32_t code,
924 const Parcel& data, Parcel* reply,
925 uint32_t flags = 0) {
926 //printf("%s: code %d\n", __func__, code);
927 (void)flags;
928
929 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
930 return PERMISSION_DENIED;
931 }
932 switch (code) {
933 case BINDER_LIB_TEST_REGISTER_SERVER: {
934 int32_t id;
935 sp<IBinder> binder;
936 id = data.readInt32();
937 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700938 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800939 return BAD_VALUE;
940 }
941
942 if (m_id != 0)
943 return INVALID_OPERATION;
944
945 pthread_mutex_lock(&m_serverWaitMutex);
946 if (m_serverStartRequested) {
947 m_serverStartRequested = false;
948 m_serverStarted = binder;
949 pthread_cond_signal(&m_serverWaitCond);
950 }
951 pthread_mutex_unlock(&m_serverWaitMutex);
952 return NO_ERROR;
953 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200954 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -0800955 case BINDER_LIB_TEST_ADD_SERVER: {
956 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800957 int serverid;
958
959 if (m_id != 0) {
960 return INVALID_OPERATION;
961 }
962 pthread_mutex_lock(&m_serverWaitMutex);
963 if (m_serverStartRequested) {
964 ret = -EBUSY;
965 } else {
966 serverid = m_nextServerId++;
967 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200968 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800969
970 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200971 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800972 pthread_mutex_lock(&m_serverWaitMutex);
973 }
974 if (ret > 0) {
975 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800976 struct timespec ts;
977 clock_gettime(CLOCK_REALTIME, &ts);
978 ts.tv_sec += 5;
979 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800980 }
981 if (m_serverStartRequested) {
982 m_serverStartRequested = false;
983 ret = -ETIMEDOUT;
984 } else {
985 reply->writeStrongBinder(m_serverStarted);
986 reply->writeInt32(serverid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700987 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800988 ret = NO_ERROR;
989 }
990 } else if (ret >= 0) {
991 m_serverStartRequested = false;
992 ret = UNKNOWN_ERROR;
993 }
994 pthread_mutex_unlock(&m_serverWaitMutex);
995 return ret;
996 }
997 case BINDER_LIB_TEST_NOP_TRANSACTION:
998 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200999 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1000 // Note: this transaction is only designed for use with a
1001 // poll() server. See comments around epoll_wait().
Yi Kongfdd8da92018-06-07 17:52:27 -07001002 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001003 // A callback was already pending; this means that
1004 // we received a second call while still processing
1005 // the first one. Fail the test.
1006 sp<IBinder> callback = data.readStrongBinder();
1007 Parcel data2;
1008 data2.writeInt32(UNKNOWN_ERROR);
1009
Yi Kongfdd8da92018-06-07 17:52:27 -07001010 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001011 } else {
1012 m_callback = data.readStrongBinder();
1013 int32_t delayUs = data.readInt32();
1014 /*
1015 * It's necessary that we sleep here, so the next
1016 * transaction the caller makes will be queued to
1017 * the async queue.
1018 */
1019 usleep(delayUs);
1020
1021 /*
1022 * Now when we return, libbinder will tell the kernel
1023 * we are done with this transaction, and the kernel
1024 * can move the queued transaction to either the
1025 * thread todo worklist (for kernels without the fix),
1026 * or the proc todo worklist. In case of the former,
1027 * the next outbound call will pick up the pending
1028 * transaction, which leads to undesired reentrant
1029 * behavior. This is caught in the if() branch above.
1030 */
1031 }
1032
1033 return NO_ERROR;
1034 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001035 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1036 Parcel data2, reply2;
1037 sp<IBinder> binder;
1038 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001039 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001040 return BAD_VALUE;
1041 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001042 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001043 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1044 return NO_ERROR;
1045 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001046 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1047 reply->writeStrongBinder(this);
1048 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001049 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1050 reply->writeInt32(m_id);
1051 return NO_ERROR;
1052 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1053 int32_t count;
1054 uint32_t indirect_code;
1055 sp<IBinder> binder;
1056
1057 count = data.readInt32();
1058 reply->writeInt32(m_id);
1059 reply->writeInt32(count);
1060 for (int i = 0; i < count; i++) {
1061 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001062 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001063 return BAD_VALUE;
1064 }
1065 indirect_code = data.readInt32();
1066 BinderLibTestBundle data2(&data);
1067 if (!data2.isValid()) {
1068 return BAD_VALUE;
1069 }
1070 BinderLibTestBundle reply2;
1071 binder->transact(indirect_code, data2, &reply2);
1072 reply2.appendTo(reply);
1073 }
1074 return NO_ERROR;
1075 }
1076 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1077 reply->setError(data.readInt32());
1078 return NO_ERROR;
1079 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1080 reply->writeInt32(sizeof(void *));
1081 return NO_ERROR;
1082 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1083 return NO_ERROR;
1084 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1085 m_strongRef = data.readStrongBinder();
1086 return NO_ERROR;
1087 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1088 int ret;
1089 Parcel data2, reply2;
1090 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1091 sp<IBinder> target;
1092 sp<IBinder> callback;
1093
1094 target = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001095 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001096 return BAD_VALUE;
1097 }
1098 callback = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001099 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001100 return BAD_VALUE;
1101 }
1102 ret = target->linkToDeath(testDeathRecipient);
1103 if (ret == NO_ERROR)
1104 ret = testDeathRecipient->waitEvent(5);
1105 data2.writeInt32(ret);
1106 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1107 return NO_ERROR;
1108 }
1109 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1110 int ret;
1111 int32_t size;
1112 const void *buf;
1113 int fd;
1114
1115 fd = data.readFileDescriptor();
1116 if (fd < 0) {
1117 return BAD_VALUE;
1118 }
1119 ret = data.readInt32(&size);
1120 if (ret != NO_ERROR) {
1121 return ret;
1122 }
1123 buf = data.readInplace(size);
Yi Kongfdd8da92018-06-07 17:52:27 -07001124 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001125 return BAD_VALUE;
1126 }
1127 ret = write(fd, buf, size);
1128 if (ret != size)
1129 return UNKNOWN_ERROR;
1130 return NO_ERROR;
1131 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001132 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1133 int ret;
1134 int32_t size;
1135 const void *buf;
1136 android::base::unique_fd fd;
1137
1138 ret = data.readUniqueParcelFileDescriptor(&fd);
1139 if (ret != NO_ERROR) {
1140 return ret;
1141 }
1142 ret = data.readInt32(&size);
1143 if (ret != NO_ERROR) {
1144 return ret;
1145 }
1146 buf = data.readInplace(size);
Yi Kong87d465c2018-07-24 01:14:06 -07001147 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001148 return BAD_VALUE;
1149 }
1150 ret = write(fd.get(), buf, size);
1151 if (ret != size) return UNKNOWN_ERROR;
1152 return NO_ERROR;
1153 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001154 case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
1155 int ret;
1156 wp<IBinder> weak;
1157 sp<IBinder> strong;
1158 Parcel data2, reply2;
1159 sp<IServiceManager> sm = defaultServiceManager();
1160 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1161
1162 weak = data.readWeakBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001163 if (weak == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001164 return BAD_VALUE;
1165 }
1166 strong = weak.promote();
1167
1168 ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
1169 if (ret != NO_ERROR)
1170 exit(EXIT_FAILURE);
1171
Yi Kongfdd8da92018-06-07 17:52:27 -07001172 if (strong == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001173 reply->setError(1);
1174 }
1175 return NO_ERROR;
1176 }
1177 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1178 alarm(10);
1179 return NO_ERROR;
1180 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kongfdd8da92018-06-07 17:52:27 -07001181 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001182 ;
1183 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001184 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
1185 bool strongRef = data.readBool();
1186 sp<IBinder> binder = new BBinder();
1187 if (strongRef) {
1188 reply->writeStrongBinder(binder);
1189 } else {
1190 reply->writeWeakBinder(binder);
1191 }
1192 return NO_ERROR;
1193 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001194 default:
1195 return UNKNOWN_TRANSACTION;
1196 };
1197 }
1198 private:
1199 int32_t m_id;
1200 int32_t m_nextServerId;
1201 pthread_mutex_t m_serverWaitMutex;
1202 pthread_cond_t m_serverWaitCond;
1203 bool m_serverStartRequested;
1204 sp<IBinder> m_serverStarted;
1205 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001206 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001207};
1208
Martijn Coenen45b07b42017-08-09 12:07:45 +02001209int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001210{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001211 binderLibTestServiceName += String16(binderserversuffix);
1212
Riley Andrews06b01ad2014-12-18 12:10:08 -08001213 status_t ret;
1214 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001215 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 {
1217 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001218 /*
1219 * We need this below, but can't hold a sp<> because it prevents the
1220 * node from being cleaned up automatically. It's safe in this case
1221 * because of how the tests are written.
1222 */
1223 testServicePtr = testService.get();
1224
Riley Andrews06b01ad2014-12-18 12:10:08 -08001225 if (index == 0) {
1226 ret = sm->addService(binderLibTestServiceName, testService);
1227 } else {
1228 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1229 Parcel data, reply;
1230 data.writeInt32(index);
1231 data.writeStrongBinder(testService);
1232
1233 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1234 }
1235 }
1236 write(readypipefd, &ret, sizeof(ret));
1237 close(readypipefd);
1238 //printf("%s: ret %d\n", __func__, ret);
1239 if (ret)
1240 return 1;
1241 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001242 if (usePoll) {
1243 int fd;
1244 struct epoll_event ev;
1245 int epoll_fd;
1246 IPCThreadState::self()->setupPolling(&fd);
1247 if (fd < 0) {
1248 return 1;
1249 }
1250 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1251
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001252 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001253 if (epoll_fd == -1) {
1254 return 1;
1255 }
1256
1257 ev.events = EPOLLIN;
1258 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1259 return 1;
1260 }
1261
1262 while (1) {
1263 /*
1264 * We simulate a single-threaded process using the binder poll
1265 * interface; besides handling binder commands, it can also
1266 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001267 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001268 *
1269 * processPendingCall() will then issue that transaction.
1270 */
1271 struct epoll_event events[1];
1272 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1273 if (numEvents < 0) {
1274 if (errno == EINTR) {
1275 continue;
1276 }
1277 return 1;
1278 }
1279 if (numEvents > 0) {
1280 IPCThreadState::self()->handlePolledCommands();
1281 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1282 testServicePtr->processPendingCall();
1283 }
1284 }
1285 } else {
1286 ProcessState::self()->startThreadPool();
1287 IPCThreadState::self()->joinThreadPool();
1288 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001289 //printf("%s: joinThreadPool returned\n", __func__);
1290 return 1; /* joinThreadPool should not return */
1291}
1292
1293int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001294 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001295 binderservername = argv[2];
1296 } else {
1297 binderservername = argv[0];
1298 }
1299
Martijn Coenen45b07b42017-08-09 12:07:45 +02001300 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1301 binderserversuffix = argv[5];
1302 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001303 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001304 binderserversuffix = new char[16];
1305 snprintf(binderserversuffix, 16, "%d", getpid());
1306 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307
1308 ::testing::InitGoogleTest(&argc, argv);
1309 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1310 ProcessState::self()->startThreadPool();
1311 return RUN_ALL_TESTS();
1312}