blob: eaca493357434d4761f957bd25ec2ebc4b70376c [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
Steven Morelandd63ed9d2019-09-04 18:15:25 -070031#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020032#include <sys/epoll.h>
33
Riley Andrews06b01ad2014-12-18 12:10:08 -080034#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
35
36using namespace android;
37
Sherry Yang336cdd32017-07-24 14:12:27 -070038static ::testing::AssertionResult IsPageAligned(void *buf) {
39 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
40 return ::testing::AssertionSuccess();
41 else
42 return ::testing::AssertionFailure() << buf << " is not page aligned";
43}
44
Riley Andrews06b01ad2014-12-18 12:10:08 -080045static testing::Environment* binder_env;
46static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070047static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080048static char binderserverarg[] = "--binderserver";
49
50static String16 binderLibTestServiceName = String16("test.binderLib");
51
52enum BinderLibTestTranscationCode {
53 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
54 BINDER_LIB_TEST_REGISTER_SERVER,
55 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020056 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080057 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070058 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020059 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080060 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070061 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080062 BINDER_LIB_TEST_GET_ID_TRANSACTION,
63 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
64 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
65 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
66 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
67 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
68 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090069 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080070 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
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700770TEST_F(BinderLibTest, LocalGetExtension) {
771 sp<BBinder> binder = new BBinder();
772 sp<IBinder> ext = new BBinder();
773 binder->setExtension(ext);
774 EXPECT_EQ(ext, binder->getExtension());
775}
776
777TEST_F(BinderLibTest, RemoteGetExtension) {
778 sp<IBinder> server = addServer();
779 ASSERT_TRUE(server != nullptr);
780
781 sp<IBinder> extension;
782 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
783 ASSERT_NE(nullptr, extension.get());
784
785 EXPECT_EQ(NO_ERROR, extension->pingBinder());
786}
787
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700788TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
789 status_t ret;
790 Parcel data, reply;
791
792 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
793 EXPECT_EQ(NO_ERROR, ret);
794
795 const flat_binder_object *fb = reply.readObject(false);
Yi Kongfdd8da92018-06-07 17:52:27 -0700796 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800797 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
798 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
799 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
800 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700801}
802
Connor O'Brien52be2c92016-09-20 14:18:08 -0700803TEST_F(BinderLibTest, FreedBinder) {
804 status_t ret;
805
806 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700807 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700808
809 __u32 freedHandle;
810 wp<IBinder> keepFreedBinder;
811 {
812 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700813 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
814 ASSERT_EQ(NO_ERROR, ret);
815 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
816 freedHandle = freed->handle;
817 /* Add a weak ref to the freed binder so the driver does not
818 * delete its reference to it - otherwise the transaction
819 * fails regardless of whether the driver is fixed.
820 */
Steven Morelande171d622019-07-17 16:06:01 -0700821 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700822 }
Steven Morelande171d622019-07-17 16:06:01 -0700823 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700824 {
825 Parcel data, reply;
826 data.writeStrongBinder(server);
827 /* Replace original handle with handle to the freed binder */
828 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
829 __u32 oldHandle = strong->handle;
830 strong->handle = freedHandle;
831 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
832 /* Returns DEAD_OBJECT (-32) if target crashes and
833 * FAILED_TRANSACTION if the driver rejects the invalid
834 * object.
835 */
836 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
837 /* Restore original handle so parcel destructor does not use
838 * the wrong handle.
839 */
840 strong->handle = oldHandle;
841 }
842}
843
Sherry Yang336cdd32017-07-24 14:12:27 -0700844TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
845 status_t ret;
846 Parcel data, reply;
847 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
848 for (int i = 0; i < 2; i++) {
849 BinderLibTestBundle datai;
850 datai.appendFrom(&data, 0, data.dataSize());
851
852 data.freeData();
853 data.writeInt32(1);
854 data.writeStrongBinder(callBack);
855 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
856
857 datai.appendTo(&data);
858 }
859 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
860 EXPECT_EQ(NO_ERROR, ret);
861}
862
Martijn Coenen45b07b42017-08-09 12:07:45 +0200863TEST_F(BinderLibTest, OnewayQueueing)
864{
865 status_t ret;
866 Parcel data, data2;
867
868 sp<IBinder> pollServer = addPollServer();
869
870 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
871 data.writeStrongBinder(callBack);
872 data.writeInt32(500000); // delay in us before calling back
873
874 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
875 data2.writeStrongBinder(callBack2);
876 data2.writeInt32(0); // delay in us
877
Yi Kongfdd8da92018-06-07 17:52:27 -0700878 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200879 EXPECT_EQ(NO_ERROR, ret);
880
881 // The delay ensures that this second transaction will end up on the async_todo list
882 // (for a single-threaded server)
Yi Kongfdd8da92018-06-07 17:52:27 -0700883 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200884 EXPECT_EQ(NO_ERROR, ret);
885
886 // The server will ensure that the two transactions are handled in the expected order;
887 // If the ordering is not as expected, an error will be returned through the callbacks.
888 ret = callBack->waitEvent(2);
889 EXPECT_EQ(NO_ERROR, ret);
890 ret = callBack->getResult();
891 EXPECT_EQ(NO_ERROR, ret);
892
893 ret = callBack2->waitEvent(2);
894 EXPECT_EQ(NO_ERROR, ret);
895 ret = callBack2->getResult();
896 EXPECT_EQ(NO_ERROR, ret);
897}
898
Riley Andrews06b01ad2014-12-18 12:10:08 -0800899class BinderLibTestService : public BBinder
900{
901 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800902 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800903 : m_id(id)
904 , m_nextServerId(id + 1)
905 , m_serverStartRequested(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700906 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800907 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700908 pthread_mutex_init(&m_serverWaitMutex, nullptr);
909 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800910 }
911 ~BinderLibTestService()
912 {
913 exit(EXIT_SUCCESS);
914 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200915
916 void processPendingCall() {
Yi Kongfdd8da92018-06-07 17:52:27 -0700917 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200918 Parcel data;
919 data.writeInt32(NO_ERROR);
920 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kongfdd8da92018-06-07 17:52:27 -0700921 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200922 }
923 }
924
Riley Andrews06b01ad2014-12-18 12:10:08 -0800925 virtual status_t onTransact(uint32_t code,
926 const Parcel& data, Parcel* reply,
927 uint32_t flags = 0) {
928 //printf("%s: code %d\n", __func__, code);
929 (void)flags;
930
931 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
932 return PERMISSION_DENIED;
933 }
934 switch (code) {
935 case BINDER_LIB_TEST_REGISTER_SERVER: {
936 int32_t id;
937 sp<IBinder> binder;
938 id = data.readInt32();
939 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700940 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800941 return BAD_VALUE;
942 }
943
944 if (m_id != 0)
945 return INVALID_OPERATION;
946
947 pthread_mutex_lock(&m_serverWaitMutex);
948 if (m_serverStartRequested) {
949 m_serverStartRequested = false;
950 m_serverStarted = binder;
951 pthread_cond_signal(&m_serverWaitCond);
952 }
953 pthread_mutex_unlock(&m_serverWaitMutex);
954 return NO_ERROR;
955 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200956 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -0800957 case BINDER_LIB_TEST_ADD_SERVER: {
958 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800959 int serverid;
960
961 if (m_id != 0) {
962 return INVALID_OPERATION;
963 }
964 pthread_mutex_lock(&m_serverWaitMutex);
965 if (m_serverStartRequested) {
966 ret = -EBUSY;
967 } else {
968 serverid = m_nextServerId++;
969 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200970 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800971
972 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200973 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800974 pthread_mutex_lock(&m_serverWaitMutex);
975 }
976 if (ret > 0) {
977 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800978 struct timespec ts;
979 clock_gettime(CLOCK_REALTIME, &ts);
980 ts.tv_sec += 5;
981 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800982 }
983 if (m_serverStartRequested) {
984 m_serverStartRequested = false;
985 ret = -ETIMEDOUT;
986 } else {
987 reply->writeStrongBinder(m_serverStarted);
988 reply->writeInt32(serverid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700989 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800990 ret = NO_ERROR;
991 }
992 } else if (ret >= 0) {
993 m_serverStartRequested = false;
994 ret = UNKNOWN_ERROR;
995 }
996 pthread_mutex_unlock(&m_serverWaitMutex);
997 return ret;
998 }
999 case BINDER_LIB_TEST_NOP_TRANSACTION:
1000 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001001 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1002 // Note: this transaction is only designed for use with a
1003 // poll() server. See comments around epoll_wait().
Yi Kongfdd8da92018-06-07 17:52:27 -07001004 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001005 // A callback was already pending; this means that
1006 // we received a second call while still processing
1007 // the first one. Fail the test.
1008 sp<IBinder> callback = data.readStrongBinder();
1009 Parcel data2;
1010 data2.writeInt32(UNKNOWN_ERROR);
1011
Yi Kongfdd8da92018-06-07 17:52:27 -07001012 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001013 } else {
1014 m_callback = data.readStrongBinder();
1015 int32_t delayUs = data.readInt32();
1016 /*
1017 * It's necessary that we sleep here, so the next
1018 * transaction the caller makes will be queued to
1019 * the async queue.
1020 */
1021 usleep(delayUs);
1022
1023 /*
1024 * Now when we return, libbinder will tell the kernel
1025 * we are done with this transaction, and the kernel
1026 * can move the queued transaction to either the
1027 * thread todo worklist (for kernels without the fix),
1028 * or the proc todo worklist. In case of the former,
1029 * the next outbound call will pick up the pending
1030 * transaction, which leads to undesired reentrant
1031 * behavior. This is caught in the if() branch above.
1032 */
1033 }
1034
1035 return NO_ERROR;
1036 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001037 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1038 Parcel data2, reply2;
1039 sp<IBinder> binder;
1040 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001041 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001042 return BAD_VALUE;
1043 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001044 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001045 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1046 return NO_ERROR;
1047 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001048 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1049 reply->writeStrongBinder(this);
1050 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001051 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1052 reply->writeInt32(m_id);
1053 return NO_ERROR;
1054 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1055 int32_t count;
1056 uint32_t indirect_code;
1057 sp<IBinder> binder;
1058
1059 count = data.readInt32();
1060 reply->writeInt32(m_id);
1061 reply->writeInt32(count);
1062 for (int i = 0; i < count; i++) {
1063 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001064 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001065 return BAD_VALUE;
1066 }
1067 indirect_code = data.readInt32();
1068 BinderLibTestBundle data2(&data);
1069 if (!data2.isValid()) {
1070 return BAD_VALUE;
1071 }
1072 BinderLibTestBundle reply2;
1073 binder->transact(indirect_code, data2, &reply2);
1074 reply2.appendTo(reply);
1075 }
1076 return NO_ERROR;
1077 }
1078 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1079 reply->setError(data.readInt32());
1080 return NO_ERROR;
1081 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1082 reply->writeInt32(sizeof(void *));
1083 return NO_ERROR;
1084 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1085 return NO_ERROR;
1086 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1087 m_strongRef = data.readStrongBinder();
1088 return NO_ERROR;
1089 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1090 int ret;
1091 Parcel data2, reply2;
1092 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1093 sp<IBinder> target;
1094 sp<IBinder> callback;
1095
1096 target = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001097 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001098 return BAD_VALUE;
1099 }
1100 callback = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001101 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001102 return BAD_VALUE;
1103 }
1104 ret = target->linkToDeath(testDeathRecipient);
1105 if (ret == NO_ERROR)
1106 ret = testDeathRecipient->waitEvent(5);
1107 data2.writeInt32(ret);
1108 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1109 return NO_ERROR;
1110 }
1111 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1112 int ret;
1113 int32_t size;
1114 const void *buf;
1115 int fd;
1116
1117 fd = data.readFileDescriptor();
1118 if (fd < 0) {
1119 return BAD_VALUE;
1120 }
1121 ret = data.readInt32(&size);
1122 if (ret != NO_ERROR) {
1123 return ret;
1124 }
1125 buf = data.readInplace(size);
Yi Kongfdd8da92018-06-07 17:52:27 -07001126 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001127 return BAD_VALUE;
1128 }
1129 ret = write(fd, buf, size);
1130 if (ret != size)
1131 return UNKNOWN_ERROR;
1132 return NO_ERROR;
1133 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001134 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1135 int ret;
1136 int32_t size;
1137 const void *buf;
1138 android::base::unique_fd fd;
1139
1140 ret = data.readUniqueParcelFileDescriptor(&fd);
1141 if (ret != NO_ERROR) {
1142 return ret;
1143 }
1144 ret = data.readInt32(&size);
1145 if (ret != NO_ERROR) {
1146 return ret;
1147 }
1148 buf = data.readInplace(size);
Yi Kong87d465c2018-07-24 01:14:06 -07001149 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001150 return BAD_VALUE;
1151 }
1152 ret = write(fd.get(), buf, size);
1153 if (ret != size) return UNKNOWN_ERROR;
1154 return NO_ERROR;
1155 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001156 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1157 alarm(10);
1158 return NO_ERROR;
1159 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kongfdd8da92018-06-07 17:52:27 -07001160 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001161 ;
1162 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001163 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001164 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001165 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001166 return NO_ERROR;
1167 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001168 default:
1169 return UNKNOWN_TRANSACTION;
1170 };
1171 }
1172 private:
1173 int32_t m_id;
1174 int32_t m_nextServerId;
1175 pthread_mutex_t m_serverWaitMutex;
1176 pthread_cond_t m_serverWaitCond;
1177 bool m_serverStartRequested;
1178 sp<IBinder> m_serverStarted;
1179 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001180 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001181};
1182
Martijn Coenen45b07b42017-08-09 12:07:45 +02001183int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001184{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001185 binderLibTestServiceName += String16(binderserversuffix);
1186
Riley Andrews06b01ad2014-12-18 12:10:08 -08001187 status_t ret;
1188 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001189 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001190 {
1191 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001192
1193 /*
1194 * Normally would also contain functionality as well, but we are only
1195 * testing the extension mechanism.
1196 */
1197 testService->setExtension(new BBinder());
1198
Martijn Coenen45b07b42017-08-09 12:07:45 +02001199 /*
1200 * We need this below, but can't hold a sp<> because it prevents the
1201 * node from being cleaned up automatically. It's safe in this case
1202 * because of how the tests are written.
1203 */
1204 testServicePtr = testService.get();
1205
Riley Andrews06b01ad2014-12-18 12:10:08 -08001206 if (index == 0) {
1207 ret = sm->addService(binderLibTestServiceName, testService);
1208 } else {
1209 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1210 Parcel data, reply;
1211 data.writeInt32(index);
1212 data.writeStrongBinder(testService);
1213
1214 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1215 }
1216 }
1217 write(readypipefd, &ret, sizeof(ret));
1218 close(readypipefd);
1219 //printf("%s: ret %d\n", __func__, ret);
1220 if (ret)
1221 return 1;
1222 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001223 if (usePoll) {
1224 int fd;
1225 struct epoll_event ev;
1226 int epoll_fd;
1227 IPCThreadState::self()->setupPolling(&fd);
1228 if (fd < 0) {
1229 return 1;
1230 }
1231 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1232
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001233 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001234 if (epoll_fd == -1) {
1235 return 1;
1236 }
1237
1238 ev.events = EPOLLIN;
1239 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1240 return 1;
1241 }
1242
1243 while (1) {
1244 /*
1245 * We simulate a single-threaded process using the binder poll
1246 * interface; besides handling binder commands, it can also
1247 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001248 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001249 *
1250 * processPendingCall() will then issue that transaction.
1251 */
1252 struct epoll_event events[1];
1253 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1254 if (numEvents < 0) {
1255 if (errno == EINTR) {
1256 continue;
1257 }
1258 return 1;
1259 }
1260 if (numEvents > 0) {
1261 IPCThreadState::self()->handlePolledCommands();
1262 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1263 testServicePtr->processPendingCall();
1264 }
1265 }
1266 } else {
1267 ProcessState::self()->startThreadPool();
1268 IPCThreadState::self()->joinThreadPool();
1269 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001270 //printf("%s: joinThreadPool returned\n", __func__);
1271 return 1; /* joinThreadPool should not return */
1272}
1273
1274int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001275 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 binderservername = argv[2];
1277 } else {
1278 binderservername = argv[0];
1279 }
1280
Martijn Coenen45b07b42017-08-09 12:07:45 +02001281 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1282 binderserversuffix = argv[5];
1283 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001285 binderserversuffix = new char[16];
1286 snprintf(binderserversuffix, 16, "%d", getpid());
1287 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001288
1289 ::testing::InitGoogleTest(&argc, argv);
1290 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1291 ProcessState::self()->startThreadPool();
1292 return RUN_ALL_TESTS();
1293}