blob: 9de346054d18192ea13cfade52fcb593e9dd5d2b [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_EXIT_TRANSACTION,
70 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070072 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010073 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080074 BINDER_LIB_TEST_ECHO_VECTOR,
Riley Andrews06b01ad2014-12-18 12:10:08 -080075};
76
Martijn Coenen45b07b42017-08-09 12:07:45 +020077pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080078{
79 int ret;
80 pid_t pid;
81 status_t status;
82 int pipefd[2];
83 char stri[16];
84 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020085 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080086 char *childargv[] = {
87 binderservername,
88 binderserverarg,
89 stri,
90 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020091 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070092 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070093 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080094 };
95
96 ret = pipe(pipefd);
97 if (ret < 0)
98 return ret;
99
100 snprintf(stri, sizeof(stri), "%d", arg2);
101 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200102 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800103
104 pid = fork();
105 if (pid == -1)
106 return pid;
107 if (pid == 0) {
108 close(pipefd[0]);
109 execv(binderservername, childargv);
110 status = -errno;
111 write(pipefd[1], &status, sizeof(status));
112 fprintf(stderr, "execv failed, %s\n", strerror(errno));
113 _exit(EXIT_FAILURE);
114 }
115 close(pipefd[1]);
116 ret = read(pipefd[0], &status, sizeof(status));
117 //printf("pipe read returned %d, status %d\n", ret, status);
118 close(pipefd[0]);
119 if (ret == sizeof(status)) {
120 ret = status;
121 } else {
122 kill(pid, SIGKILL);
123 if (ret >= 0) {
124 ret = NO_INIT;
125 }
126 }
127 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700128 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800129 return ret;
130 }
131 return pid;
132}
133
134class BinderLibTestEnv : public ::testing::Environment {
135 public:
136 BinderLibTestEnv() {}
137 sp<IBinder> getServer(void) {
138 return m_server;
139 }
140
141 private:
142 virtual void SetUp() {
143 m_serverpid = start_server_process(0);
144 //printf("m_serverpid %d\n", m_serverpid);
145 ASSERT_GT(m_serverpid, 0);
146
147 sp<IServiceManager> sm = defaultServiceManager();
148 //printf("%s: pid %d, get service\n", __func__, m_pid);
149 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700150 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800151 //printf("%s: pid %d, get service done\n", __func__, m_pid);
152 }
153 virtual void TearDown() {
154 status_t ret;
155 Parcel data, reply;
156 int exitStatus;
157 pid_t pid;
158
159 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700160 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800161 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
162 EXPECT_EQ(0, ret);
163 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
164 EXPECT_EQ(0, ret);
165 }
166 if (m_serverpid > 0) {
167 //printf("wait for %d\n", m_pids[i]);
168 pid = wait(&exitStatus);
169 EXPECT_EQ(m_serverpid, pid);
170 EXPECT_TRUE(WIFEXITED(exitStatus));
171 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
172 }
173 }
174
175 pid_t m_serverpid;
176 sp<IBinder> m_server;
177};
178
179class BinderLibTest : public ::testing::Test {
180 public:
181 virtual void SetUp() {
182 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000183 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800184 }
185 virtual void TearDown() {
186 }
187 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200188 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800189 {
190 int ret;
191 int32_t id;
192 Parcel data, reply;
193 sp<IBinder> binder;
194
Martijn Coenen45b07b42017-08-09 12:07:45 +0200195 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800196 EXPECT_EQ(NO_ERROR, ret);
197
Yi Kong91635562018-06-07 14:38:36 -0700198 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700200 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800201 ret = reply.readInt32(&id);
202 EXPECT_EQ(NO_ERROR, ret);
203 if (idPtr)
204 *idPtr = id;
205 return binder;
206 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200207
Yi Kong91635562018-06-07 14:38:36 -0700208 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200209 {
210 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
211 }
212
Yi Kong91635562018-06-07 14:38:36 -0700213 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200214 {
215 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
216 }
217
Riley Andrews06b01ad2014-12-18 12:10:08 -0800218 void waitForReadData(int fd, int timeout_ms) {
219 int ret;
220 pollfd pfd = pollfd();
221
222 pfd.fd = fd;
223 pfd.events = POLLIN;
224 ret = poll(&pfd, 1, timeout_ms);
225 EXPECT_EQ(1, ret);
226 }
227
228 sp<IBinder> m_server;
229};
230
231class BinderLibTestBundle : public Parcel
232{
233 public:
234 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800235 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800236 int32_t mark;
237 int32_t bundleLen;
238 size_t pos;
239
240 if (source->readInt32(&mark))
241 return;
242 if (mark != MARK_START)
243 return;
244 if (source->readInt32(&bundleLen))
245 return;
246 pos = source->dataPosition();
247 if (Parcel::appendFrom(source, pos, bundleLen))
248 return;
249 source->setDataPosition(pos + bundleLen);
250 if (source->readInt32(&mark))
251 return;
252 if (mark != MARK_END)
253 return;
254 m_isValid = true;
255 setDataPosition(0);
256 }
257 void appendTo(Parcel *dest) {
258 dest->writeInt32(MARK_START);
259 dest->writeInt32(dataSize());
260 dest->appendFrom(this, 0, dataSize());
261 dest->writeInt32(MARK_END);
262 };
263 bool isValid(void) {
264 return m_isValid;
265 }
266 private:
267 enum {
268 MARK_START = B_PACK_CHARS('B','T','B','S'),
269 MARK_END = B_PACK_CHARS('B','T','B','E'),
270 };
271 bool m_isValid;
272};
273
274class BinderLibTestEvent
275{
276 public:
277 BinderLibTestEvent(void)
278 : m_eventTriggered(false)
279 {
Yi Kong91635562018-06-07 14:38:36 -0700280 pthread_mutex_init(&m_waitMutex, nullptr);
281 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800282 }
283 int waitEvent(int timeout_s)
284 {
285 int ret;
286 pthread_mutex_lock(&m_waitMutex);
287 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800288 struct timespec ts;
289 clock_gettime(CLOCK_REALTIME, &ts);
290 ts.tv_sec += timeout_s;
291 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800292 }
293 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
294 pthread_mutex_unlock(&m_waitMutex);
295 return ret;
296 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200297 pthread_t getTriggeringThread()
298 {
299 return m_triggeringThread;
300 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800301 protected:
302 void triggerEvent(void) {
303 pthread_mutex_lock(&m_waitMutex);
304 pthread_cond_signal(&m_waitCond);
305 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200306 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800307 pthread_mutex_unlock(&m_waitMutex);
308 };
309 private:
310 pthread_mutex_t m_waitMutex;
311 pthread_cond_t m_waitCond;
312 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200313 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314};
315
316class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
317{
318 public:
319 BinderLibTestCallBack()
320 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700321 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800322 {
323 }
324 status_t getResult(void)
325 {
326 return m_result;
327 }
328
329 private:
330 virtual status_t onTransact(uint32_t code,
331 const Parcel& data, Parcel* reply,
332 uint32_t flags = 0)
333 {
334 (void)reply;
335 (void)flags;
336 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200337 case BINDER_LIB_TEST_CALL_BACK: {
338 status_t status = data.readInt32(&m_result);
339 if (status != NO_ERROR) {
340 m_result = status;
341 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800342 triggerEvent();
343 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200344 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700345 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
346 sp<IBinder> server;
347 int ret;
348 const uint8_t *buf = data.data();
349 size_t size = data.dataSize();
350 if (m_prev_end) {
351 /* 64-bit kernel needs at most 8 bytes to align buffer end */
352 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
353 } else {
354 EXPECT_TRUE(IsPageAligned((void *)buf));
355 }
356
357 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
358
359 if (size > 0) {
360 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
361 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
362 data, reply);
363 EXPECT_EQ(NO_ERROR, ret);
364 }
365 return NO_ERROR;
366 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800367 default:
368 return UNKNOWN_TRANSACTION;
369 }
370 }
371
372 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700373 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800374};
375
376class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
377{
378 private:
379 virtual void binderDied(const wp<IBinder>& who) {
380 (void)who;
381 triggerEvent();
382 };
383};
384
385TEST_F(BinderLibTest, NopTransaction) {
386 status_t ret;
387 Parcel data, reply;
388 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
389 EXPECT_EQ(NO_ERROR, ret);
390}
391
392TEST_F(BinderLibTest, SetError) {
393 int32_t testValue[] = { 0, -123, 123 };
394 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
395 status_t ret;
396 Parcel data, reply;
397 data.writeInt32(testValue[i]);
398 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
399 EXPECT_EQ(testValue[i], ret);
400 }
401}
402
403TEST_F(BinderLibTest, GetId) {
404 status_t ret;
405 int32_t id;
406 Parcel data, reply;
407 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
408 EXPECT_EQ(NO_ERROR, ret);
409 ret = reply.readInt32(&id);
410 EXPECT_EQ(NO_ERROR, ret);
411 EXPECT_EQ(0, id);
412}
413
414TEST_F(BinderLibTest, PtrSize) {
415 status_t ret;
416 int32_t ptrsize;
417 Parcel data, reply;
418 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700419 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800420 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
421 EXPECT_EQ(NO_ERROR, ret);
422 ret = reply.readInt32(&ptrsize);
423 EXPECT_EQ(NO_ERROR, ret);
424 RecordProperty("TestPtrSize", sizeof(void *));
425 RecordProperty("ServerPtrSize", sizeof(void *));
426}
427
428TEST_F(BinderLibTest, IndirectGetId2)
429{
430 status_t ret;
431 int32_t id;
432 int32_t count;
433 Parcel data, reply;
434 int32_t serverId[3];
435
436 data.writeInt32(ARRAY_SIZE(serverId));
437 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
438 sp<IBinder> server;
439 BinderLibTestBundle datai;
440
441 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700442 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800443 data.writeStrongBinder(server);
444 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
445 datai.appendTo(&data);
446 }
447
448 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
449 ASSERT_EQ(NO_ERROR, ret);
450
451 ret = reply.readInt32(&id);
452 ASSERT_EQ(NO_ERROR, ret);
453 EXPECT_EQ(0, id);
454
455 ret = reply.readInt32(&count);
456 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700457 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800458
459 for (size_t i = 0; i < (size_t)count; i++) {
460 BinderLibTestBundle replyi(&reply);
461 EXPECT_TRUE(replyi.isValid());
462 ret = replyi.readInt32(&id);
463 EXPECT_EQ(NO_ERROR, ret);
464 EXPECT_EQ(serverId[i], id);
465 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
466 }
467
468 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
469}
470
471TEST_F(BinderLibTest, IndirectGetId3)
472{
473 status_t ret;
474 int32_t id;
475 int32_t count;
476 Parcel data, reply;
477 int32_t serverId[3];
478
479 data.writeInt32(ARRAY_SIZE(serverId));
480 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
481 sp<IBinder> server;
482 BinderLibTestBundle datai;
483 BinderLibTestBundle datai2;
484
485 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700486 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800487 data.writeStrongBinder(server);
488 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
489
490 datai.writeInt32(1);
491 datai.writeStrongBinder(m_server);
492 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
493 datai2.appendTo(&datai);
494
495 datai.appendTo(&data);
496 }
497
498 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
499 ASSERT_EQ(NO_ERROR, ret);
500
501 ret = reply.readInt32(&id);
502 ASSERT_EQ(NO_ERROR, ret);
503 EXPECT_EQ(0, id);
504
505 ret = reply.readInt32(&count);
506 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700507 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800508
509 for (size_t i = 0; i < (size_t)count; i++) {
510 int32_t counti;
511
512 BinderLibTestBundle replyi(&reply);
513 EXPECT_TRUE(replyi.isValid());
514 ret = replyi.readInt32(&id);
515 EXPECT_EQ(NO_ERROR, ret);
516 EXPECT_EQ(serverId[i], id);
517
518 ret = replyi.readInt32(&counti);
519 ASSERT_EQ(NO_ERROR, ret);
520 EXPECT_EQ(1, counti);
521
522 BinderLibTestBundle replyi2(&replyi);
523 EXPECT_TRUE(replyi2.isValid());
524 ret = replyi2.readInt32(&id);
525 EXPECT_EQ(NO_ERROR, ret);
526 EXPECT_EQ(0, id);
527 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
528
529 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
530 }
531
532 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
533}
534
535TEST_F(BinderLibTest, CallBack)
536{
537 status_t ret;
538 Parcel data, reply;
539 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
540 data.writeStrongBinder(callBack);
541 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
542 EXPECT_EQ(NO_ERROR, ret);
543 ret = callBack->waitEvent(5);
544 EXPECT_EQ(NO_ERROR, ret);
545 ret = callBack->getResult();
546 EXPECT_EQ(NO_ERROR, ret);
547}
548
549TEST_F(BinderLibTest, AddServer)
550{
551 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700552 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553}
554
Riley Andrews06b01ad2014-12-18 12:10:08 -0800555TEST_F(BinderLibTest, DeathNotificationStrongRef)
556{
557 status_t ret;
558 sp<IBinder> sbinder;
559
560 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
561
562 {
563 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700564 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800565 ret = binder->linkToDeath(testDeathRecipient);
566 EXPECT_EQ(NO_ERROR, ret);
567 sbinder = binder;
568 }
569 {
570 Parcel data, reply;
571 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
572 EXPECT_EQ(0, ret);
573 }
574 IPCThreadState::self()->flushCommands();
575 ret = testDeathRecipient->waitEvent(5);
576 EXPECT_EQ(NO_ERROR, ret);
577 ret = sbinder->unlinkToDeath(testDeathRecipient);
578 EXPECT_EQ(DEAD_OBJECT, ret);
579}
580
581TEST_F(BinderLibTest, DeathNotificationMultiple)
582{
583 status_t ret;
584 const int clientcount = 2;
585 sp<IBinder> target;
586 sp<IBinder> linkedclient[clientcount];
587 sp<BinderLibTestCallBack> callBack[clientcount];
588 sp<IBinder> passiveclient[clientcount];
589
590 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700591 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800592 for (int i = 0; i < clientcount; i++) {
593 {
594 Parcel data, reply;
595
596 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700597 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800598 callBack[i] = new BinderLibTestCallBack();
599 data.writeStrongBinder(target);
600 data.writeStrongBinder(callBack[i]);
601 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
602 EXPECT_EQ(NO_ERROR, ret);
603 }
604 {
605 Parcel data, reply;
606
607 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700608 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800609 data.writeStrongBinder(target);
610 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
611 EXPECT_EQ(NO_ERROR, ret);
612 }
613 }
614 {
615 Parcel data, reply;
616 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
617 EXPECT_EQ(0, ret);
618 }
619
620 for (int i = 0; i < clientcount; i++) {
621 ret = callBack[i]->waitEvent(5);
622 EXPECT_EQ(NO_ERROR, ret);
623 ret = callBack[i]->getResult();
624 EXPECT_EQ(NO_ERROR, ret);
625 }
626}
627
Martijn Coenenf7100e42017-07-31 12:14:09 +0200628TEST_F(BinderLibTest, DeathNotificationThread)
629{
630 status_t ret;
631 sp<BinderLibTestCallBack> callback;
632 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700633 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200634 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700635 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200636
637 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
638
639 ret = target->linkToDeath(testDeathRecipient);
640 EXPECT_EQ(NO_ERROR, ret);
641
642 {
643 Parcel data, reply;
644 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
645 EXPECT_EQ(0, ret);
646 }
647
648 /* Make sure it's dead */
649 testDeathRecipient->waitEvent(5);
650
651 /* Now, pass the ref to another process and ask that process to
652 * call linkToDeath() on it, and wait for a response. This tests
653 * two things:
654 * 1) You still get death notifications when calling linkToDeath()
655 * on a ref that is already dead when it was passed to you.
656 * 2) That death notifications are not directly pushed to the thread
657 * registering them, but to the threadpool (proc workqueue) instead.
658 *
659 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
660 * is blocked on a condition variable waiting for the death notification to be
661 * called; therefore, that thread is not available for handling proc work.
662 * So, if the death notification was pushed to the thread workqueue, the callback
663 * would never be called, and the test would timeout and fail.
664 *
665 * Note that we can't do this part of the test from this thread itself, because
666 * the binder driver would only push death notifications to the thread if
667 * it is a looper thread, which this thread is not.
668 *
669 * See b/23525545 for details.
670 */
671 {
672 Parcel data, reply;
673
674 callback = new BinderLibTestCallBack();
675 data.writeStrongBinder(target);
676 data.writeStrongBinder(callback);
677 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
678 EXPECT_EQ(NO_ERROR, ret);
679 }
680
681 ret = callback->waitEvent(5);
682 EXPECT_EQ(NO_ERROR, ret);
683 ret = callback->getResult();
684 EXPECT_EQ(NO_ERROR, ret);
685}
686
Riley Andrews06b01ad2014-12-18 12:10:08 -0800687TEST_F(BinderLibTest, PassFile) {
688 int ret;
689 int pipefd[2];
690 uint8_t buf[1] = { 0 };
691 uint8_t write_value = 123;
692
693 ret = pipe2(pipefd, O_NONBLOCK);
694 ASSERT_EQ(0, ret);
695
696 {
697 Parcel data, reply;
698 uint8_t writebuf[1] = { write_value };
699
700 ret = data.writeFileDescriptor(pipefd[1], true);
701 EXPECT_EQ(NO_ERROR, ret);
702
703 ret = data.writeInt32(sizeof(writebuf));
704 EXPECT_EQ(NO_ERROR, ret);
705
706 ret = data.write(writebuf, sizeof(writebuf));
707 EXPECT_EQ(NO_ERROR, ret);
708
709 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
710 EXPECT_EQ(NO_ERROR, ret);
711 }
712
713 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700714 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800715 EXPECT_EQ(write_value, buf[0]);
716
717 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
718
719 ret = read(pipefd[0], buf, sizeof(buf));
720 EXPECT_EQ(0, ret);
721
722 close(pipefd[0]);
723}
724
Ryo Hashimotobf551892018-05-31 16:58:35 +0900725TEST_F(BinderLibTest, PassParcelFileDescriptor) {
726 const int datasize = 123;
727 std::vector<uint8_t> writebuf(datasize);
728 for (size_t i = 0; i < writebuf.size(); ++i) {
729 writebuf[i] = i;
730 }
731
732 android::base::unique_fd read_end, write_end;
733 {
734 int pipefd[2];
735 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
736 read_end.reset(pipefd[0]);
737 write_end.reset(pipefd[1]);
738 }
739 {
740 Parcel data;
741 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
742 write_end.reset();
743 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
744 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
745
746 Parcel reply;
747 EXPECT_EQ(NO_ERROR,
748 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
749 &reply));
750 }
751 std::vector<uint8_t> readbuf(datasize);
752 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
753 EXPECT_EQ(writebuf, readbuf);
754
755 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
756
757 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
758}
759
Riley Andrews06b01ad2014-12-18 12:10:08 -0800760TEST_F(BinderLibTest, PromoteLocal) {
761 sp<IBinder> strong = new BBinder();
762 wp<IBinder> weak = strong;
763 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700764 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800765 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700766 strong = nullptr;
767 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700769 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800770}
771
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700772TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
773 status_t ret;
774 Parcel data, reply;
775
776 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
777 EXPECT_EQ(NO_ERROR, ret);
778
779 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700780 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800781 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
782 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
783 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
784 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700785}
786
Connor O'Brien52be2c92016-09-20 14:18:08 -0700787TEST_F(BinderLibTest, FreedBinder) {
788 status_t ret;
789
790 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700791 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700792
793 __u32 freedHandle;
794 wp<IBinder> keepFreedBinder;
795 {
796 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700797 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
798 ASSERT_EQ(NO_ERROR, ret);
799 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
800 freedHandle = freed->handle;
801 /* Add a weak ref to the freed binder so the driver does not
802 * delete its reference to it - otherwise the transaction
803 * fails regardless of whether the driver is fixed.
804 */
Steven Morelande171d622019-07-17 16:06:01 -0700805 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700806 }
Steven Morelande171d622019-07-17 16:06:01 -0700807 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700808 {
809 Parcel data, reply;
810 data.writeStrongBinder(server);
811 /* Replace original handle with handle to the freed binder */
812 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
813 __u32 oldHandle = strong->handle;
814 strong->handle = freedHandle;
815 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
816 /* Returns DEAD_OBJECT (-32) if target crashes and
817 * FAILED_TRANSACTION if the driver rejects the invalid
818 * object.
819 */
820 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
821 /* Restore original handle so parcel destructor does not use
822 * the wrong handle.
823 */
824 strong->handle = oldHandle;
825 }
826}
827
Sherry Yang336cdd32017-07-24 14:12:27 -0700828TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
829 status_t ret;
830 Parcel data, reply;
831 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
832 for (int i = 0; i < 2; i++) {
833 BinderLibTestBundle datai;
834 datai.appendFrom(&data, 0, data.dataSize());
835
836 data.freeData();
837 data.writeInt32(1);
838 data.writeStrongBinder(callBack);
839 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
840
841 datai.appendTo(&data);
842 }
843 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
844 EXPECT_EQ(NO_ERROR, ret);
845}
846
Martijn Coenen45b07b42017-08-09 12:07:45 +0200847TEST_F(BinderLibTest, OnewayQueueing)
848{
849 status_t ret;
850 Parcel data, data2;
851
852 sp<IBinder> pollServer = addPollServer();
853
854 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
855 data.writeStrongBinder(callBack);
856 data.writeInt32(500000); // delay in us before calling back
857
858 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
859 data2.writeStrongBinder(callBack2);
860 data2.writeInt32(0); // delay in us
861
Yi Kong91635562018-06-07 14:38:36 -0700862 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200863 EXPECT_EQ(NO_ERROR, ret);
864
865 // The delay ensures that this second transaction will end up on the async_todo list
866 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700867 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200868 EXPECT_EQ(NO_ERROR, ret);
869
870 // The server will ensure that the two transactions are handled in the expected order;
871 // If the ordering is not as expected, an error will be returned through the callbacks.
872 ret = callBack->waitEvent(2);
873 EXPECT_EQ(NO_ERROR, ret);
874 ret = callBack->getResult();
875 EXPECT_EQ(NO_ERROR, ret);
876
877 ret = callBack2->waitEvent(2);
878 EXPECT_EQ(NO_ERROR, ret);
879 ret = callBack2->getResult();
880 EXPECT_EQ(NO_ERROR, ret);
881}
882
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100883TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
884{
885 status_t ret;
886 Parcel data, reply;
887 data.writeInterfaceToken(binderLibTestServiceName);
888 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
889 EXPECT_EQ(-1, reply.readInt32());
890 EXPECT_EQ(NO_ERROR, ret);
891}
892
893TEST_F(BinderLibTest, WorkSourceSet)
894{
895 status_t ret;
896 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000897 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000898 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100899 data.writeInterfaceToken(binderLibTestServiceName);
900 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
901 EXPECT_EQ(100, reply.readInt32());
902 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000903 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
904 EXPECT_EQ(NO_ERROR, ret);
905}
906
907TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
908{
909 status_t ret;
910 Parcel data, reply;
911
912 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
913 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
914
915 data.writeInterfaceToken(binderLibTestServiceName);
916 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
917 EXPECT_EQ(-1, reply.readInt32());
918 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100919 EXPECT_EQ(NO_ERROR, ret);
920}
921
922TEST_F(BinderLibTest, WorkSourceCleared)
923{
924 status_t ret;
925 Parcel data, reply;
926
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000927 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000928 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
929 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100930 data.writeInterfaceToken(binderLibTestServiceName);
931 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
932
933 EXPECT_EQ(-1, reply.readInt32());
934 EXPECT_EQ(100, previousWorkSource);
935 EXPECT_EQ(NO_ERROR, ret);
936}
937
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000938TEST_F(BinderLibTest, WorkSourceRestored)
939{
940 status_t ret;
941 Parcel data, reply;
942
943 IPCThreadState::self()->setCallingWorkSourceUid(100);
944 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
945 IPCThreadState::self()->restoreCallingWorkSource(token);
946
947 data.writeInterfaceToken(binderLibTestServiceName);
948 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
949
950 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000951 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000952 EXPECT_EQ(NO_ERROR, ret);
953}
954
Olivier Gaillard91a04802018-11-14 17:32:41 +0000955TEST_F(BinderLibTest, PropagateFlagSet)
956{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000957 IPCThreadState::self()->clearPropagateWorkSource();
958 IPCThreadState::self()->setCallingWorkSourceUid(100);
959 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
960}
961
962TEST_F(BinderLibTest, PropagateFlagCleared)
963{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000964 IPCThreadState::self()->setCallingWorkSourceUid(100);
965 IPCThreadState::self()->clearPropagateWorkSource();
966 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
967}
968
969TEST_F(BinderLibTest, PropagateFlagRestored)
970{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000971 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
972 IPCThreadState::self()->restoreCallingWorkSource(token);
973
974 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
975}
976
977TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
978{
979 IPCThreadState::self()->setCallingWorkSourceUid(100);
980
981 Parcel data, reply;
982 status_t ret;
983 data.writeInterfaceToken(binderLibTestServiceName);
984 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
985
986 Parcel data2, reply2;
987 status_t ret2;
988 data2.writeInterfaceToken(binderLibTestServiceName);
989 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
990 EXPECT_EQ(100, reply2.readInt32());
991 EXPECT_EQ(NO_ERROR, ret2);
992}
993
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800994TEST_F(BinderLibTest, VectorSent) {
995 Parcel data, reply;
996 sp<IBinder> server = addServer();
997 ASSERT_TRUE(server != nullptr);
998
999 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1000 data.writeUint64Vector(testValue);
1001
1002 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1003 EXPECT_EQ(NO_ERROR, ret);
1004 std::vector<uint64_t> readValue;
1005 ret = reply.readUint64Vector(&readValue);
1006 EXPECT_EQ(readValue, testValue);
1007}
1008
Riley Andrews06b01ad2014-12-18 12:10:08 -08001009class BinderLibTestService : public BBinder
1010{
1011 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001012 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001013 : m_id(id)
1014 , m_nextServerId(id + 1)
1015 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001016 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001017 {
Yi Kong91635562018-06-07 14:38:36 -07001018 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1019 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001020 }
1021 ~BinderLibTestService()
1022 {
1023 exit(EXIT_SUCCESS);
1024 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001025
1026 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001027 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001028 Parcel data;
1029 data.writeInt32(NO_ERROR);
1030 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001031 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001032 }
1033 }
1034
Riley Andrews06b01ad2014-12-18 12:10:08 -08001035 virtual status_t onTransact(uint32_t code,
1036 const Parcel& data, Parcel* reply,
1037 uint32_t flags = 0) {
1038 //printf("%s: code %d\n", __func__, code);
1039 (void)flags;
1040
1041 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1042 return PERMISSION_DENIED;
1043 }
1044 switch (code) {
1045 case BINDER_LIB_TEST_REGISTER_SERVER: {
1046 int32_t id;
1047 sp<IBinder> binder;
1048 id = data.readInt32();
1049 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001050 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001051 return BAD_VALUE;
1052 }
1053
1054 if (m_id != 0)
1055 return INVALID_OPERATION;
1056
1057 pthread_mutex_lock(&m_serverWaitMutex);
1058 if (m_serverStartRequested) {
1059 m_serverStartRequested = false;
1060 m_serverStarted = binder;
1061 pthread_cond_signal(&m_serverWaitCond);
1062 }
1063 pthread_mutex_unlock(&m_serverWaitMutex);
1064 return NO_ERROR;
1065 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001066 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001067 case BINDER_LIB_TEST_ADD_SERVER: {
1068 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001069 int serverid;
1070
1071 if (m_id != 0) {
1072 return INVALID_OPERATION;
1073 }
1074 pthread_mutex_lock(&m_serverWaitMutex);
1075 if (m_serverStartRequested) {
1076 ret = -EBUSY;
1077 } else {
1078 serverid = m_nextServerId++;
1079 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001080 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001081
1082 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001083 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001084 pthread_mutex_lock(&m_serverWaitMutex);
1085 }
1086 if (ret > 0) {
1087 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001088 struct timespec ts;
1089 clock_gettime(CLOCK_REALTIME, &ts);
1090 ts.tv_sec += 5;
1091 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001092 }
1093 if (m_serverStartRequested) {
1094 m_serverStartRequested = false;
1095 ret = -ETIMEDOUT;
1096 } else {
1097 reply->writeStrongBinder(m_serverStarted);
1098 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001099 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001100 ret = NO_ERROR;
1101 }
1102 } else if (ret >= 0) {
1103 m_serverStartRequested = false;
1104 ret = UNKNOWN_ERROR;
1105 }
1106 pthread_mutex_unlock(&m_serverWaitMutex);
1107 return ret;
1108 }
1109 case BINDER_LIB_TEST_NOP_TRANSACTION:
1110 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001111 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1112 // Note: this transaction is only designed for use with a
1113 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001114 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001115 // A callback was already pending; this means that
1116 // we received a second call while still processing
1117 // the first one. Fail the test.
1118 sp<IBinder> callback = data.readStrongBinder();
1119 Parcel data2;
1120 data2.writeInt32(UNKNOWN_ERROR);
1121
Yi Kong91635562018-06-07 14:38:36 -07001122 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001123 } else {
1124 m_callback = data.readStrongBinder();
1125 int32_t delayUs = data.readInt32();
1126 /*
1127 * It's necessary that we sleep here, so the next
1128 * transaction the caller makes will be queued to
1129 * the async queue.
1130 */
1131 usleep(delayUs);
1132
1133 /*
1134 * Now when we return, libbinder will tell the kernel
1135 * we are done with this transaction, and the kernel
1136 * can move the queued transaction to either the
1137 * thread todo worklist (for kernels without the fix),
1138 * or the proc todo worklist. In case of the former,
1139 * the next outbound call will pick up the pending
1140 * transaction, which leads to undesired reentrant
1141 * behavior. This is caught in the if() branch above.
1142 */
1143 }
1144
1145 return NO_ERROR;
1146 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001147 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1148 Parcel data2, reply2;
1149 sp<IBinder> binder;
1150 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001151 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001152 return BAD_VALUE;
1153 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001154 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001155 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1156 return NO_ERROR;
1157 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001158 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1159 reply->writeStrongBinder(this);
1160 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001161 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1162 reply->writeInt32(m_id);
1163 return NO_ERROR;
1164 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1165 int32_t count;
1166 uint32_t indirect_code;
1167 sp<IBinder> binder;
1168
1169 count = data.readInt32();
1170 reply->writeInt32(m_id);
1171 reply->writeInt32(count);
1172 for (int i = 0; i < count; i++) {
1173 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001174 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001175 return BAD_VALUE;
1176 }
1177 indirect_code = data.readInt32();
1178 BinderLibTestBundle data2(&data);
1179 if (!data2.isValid()) {
1180 return BAD_VALUE;
1181 }
1182 BinderLibTestBundle reply2;
1183 binder->transact(indirect_code, data2, &reply2);
1184 reply2.appendTo(reply);
1185 }
1186 return NO_ERROR;
1187 }
1188 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1189 reply->setError(data.readInt32());
1190 return NO_ERROR;
1191 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1192 reply->writeInt32(sizeof(void *));
1193 return NO_ERROR;
1194 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1195 return NO_ERROR;
1196 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1197 m_strongRef = data.readStrongBinder();
1198 return NO_ERROR;
1199 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1200 int ret;
1201 Parcel data2, reply2;
1202 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1203 sp<IBinder> target;
1204 sp<IBinder> callback;
1205
1206 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001207 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001208 return BAD_VALUE;
1209 }
1210 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001211 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001212 return BAD_VALUE;
1213 }
1214 ret = target->linkToDeath(testDeathRecipient);
1215 if (ret == NO_ERROR)
1216 ret = testDeathRecipient->waitEvent(5);
1217 data2.writeInt32(ret);
1218 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1219 return NO_ERROR;
1220 }
1221 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1222 int ret;
1223 int32_t size;
1224 const void *buf;
1225 int fd;
1226
1227 fd = data.readFileDescriptor();
1228 if (fd < 0) {
1229 return BAD_VALUE;
1230 }
1231 ret = data.readInt32(&size);
1232 if (ret != NO_ERROR) {
1233 return ret;
1234 }
1235 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001236 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001237 return BAD_VALUE;
1238 }
1239 ret = write(fd, buf, size);
1240 if (ret != size)
1241 return UNKNOWN_ERROR;
1242 return NO_ERROR;
1243 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001244 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1245 int ret;
1246 int32_t size;
1247 const void *buf;
1248 android::base::unique_fd fd;
1249
1250 ret = data.readUniqueParcelFileDescriptor(&fd);
1251 if (ret != NO_ERROR) {
1252 return ret;
1253 }
1254 ret = data.readInt32(&size);
1255 if (ret != NO_ERROR) {
1256 return ret;
1257 }
1258 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001259 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001260 return BAD_VALUE;
1261 }
1262 ret = write(fd.get(), buf, size);
1263 if (ret != size) return UNKNOWN_ERROR;
1264 return NO_ERROR;
1265 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001266 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1267 alarm(10);
1268 return NO_ERROR;
1269 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001270 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 ;
1272 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001273 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001274 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001275 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001276 return NO_ERROR;
1277 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001278 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1279 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001280 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001281 return NO_ERROR;
1282 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001283 case BINDER_LIB_TEST_ECHO_VECTOR: {
1284 std::vector<uint64_t> vector;
1285 auto err = data.readUint64Vector(&vector);
1286 if (err != NO_ERROR)
1287 return err;
1288 reply->writeUint64Vector(vector);
1289 return NO_ERROR;
1290 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001291 default:
1292 return UNKNOWN_TRANSACTION;
1293 };
1294 }
1295 private:
1296 int32_t m_id;
1297 int32_t m_nextServerId;
1298 pthread_mutex_t m_serverWaitMutex;
1299 pthread_cond_t m_serverWaitCond;
1300 bool m_serverStartRequested;
1301 sp<IBinder> m_serverStarted;
1302 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001303 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001304};
1305
Martijn Coenen45b07b42017-08-09 12:07:45 +02001306int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001308 binderLibTestServiceName += String16(binderserversuffix);
1309
Riley Andrews06b01ad2014-12-18 12:10:08 -08001310 status_t ret;
1311 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001312 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001313 {
1314 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001315 /*
1316 * We need this below, but can't hold a sp<> because it prevents the
1317 * node from being cleaned up automatically. It's safe in this case
1318 * because of how the tests are written.
1319 */
1320 testServicePtr = testService.get();
1321
Riley Andrews06b01ad2014-12-18 12:10:08 -08001322 if (index == 0) {
1323 ret = sm->addService(binderLibTestServiceName, testService);
1324 } else {
1325 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1326 Parcel data, reply;
1327 data.writeInt32(index);
1328 data.writeStrongBinder(testService);
1329
1330 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1331 }
1332 }
1333 write(readypipefd, &ret, sizeof(ret));
1334 close(readypipefd);
1335 //printf("%s: ret %d\n", __func__, ret);
1336 if (ret)
1337 return 1;
1338 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001339 if (usePoll) {
1340 int fd;
1341 struct epoll_event ev;
1342 int epoll_fd;
1343 IPCThreadState::self()->setupPolling(&fd);
1344 if (fd < 0) {
1345 return 1;
1346 }
1347 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1348
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001349 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001350 if (epoll_fd == -1) {
1351 return 1;
1352 }
1353
1354 ev.events = EPOLLIN;
1355 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1356 return 1;
1357 }
1358
1359 while (1) {
1360 /*
1361 * We simulate a single-threaded process using the binder poll
1362 * interface; besides handling binder commands, it can also
1363 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001364 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001365 *
1366 * processPendingCall() will then issue that transaction.
1367 */
1368 struct epoll_event events[1];
1369 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1370 if (numEvents < 0) {
1371 if (errno == EINTR) {
1372 continue;
1373 }
1374 return 1;
1375 }
1376 if (numEvents > 0) {
1377 IPCThreadState::self()->handlePolledCommands();
1378 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1379 testServicePtr->processPendingCall();
1380 }
1381 }
1382 } else {
1383 ProcessState::self()->startThreadPool();
1384 IPCThreadState::self()->joinThreadPool();
1385 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001386 //printf("%s: joinThreadPool returned\n", __func__);
1387 return 1; /* joinThreadPool should not return */
1388}
1389
1390int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001391 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001392 binderservername = argv[2];
1393 } else {
1394 binderservername = argv[0];
1395 }
1396
Martijn Coenen45b07b42017-08-09 12:07:45 +02001397 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1398 binderserversuffix = argv[5];
1399 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001400 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001401 binderserversuffix = new char[16];
1402 snprintf(binderserversuffix, 16, "%d", getpid());
1403 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001404
1405 ::testing::InitGoogleTest(&argc, argv);
1406 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1407 ProcessState::self()->startThreadPool();
1408 return RUN_ALL_TESTS();
1409}