blob: 5f8887b399cddcad06e034c64f6fe435e494d5e8 [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,
Riley Andrews06b01ad2014-12-18 12:10:08 -080073};
74
Martijn Coenen45b07b42017-08-09 12:07:45 +020075pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080076{
77 int ret;
78 pid_t pid;
79 status_t status;
80 int pipefd[2];
81 char stri[16];
82 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020083 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080084 char *childargv[] = {
85 binderservername,
86 binderserverarg,
87 stri,
88 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020089 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070090 binderserversuffix,
Yi Kongfdd8da92018-06-07 17:52:27 -070091 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080092 };
93
94 ret = pipe(pipefd);
95 if (ret < 0)
96 return ret;
97
98 snprintf(stri, sizeof(stri), "%d", arg2);
99 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200100 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800101
102 pid = fork();
103 if (pid == -1)
104 return pid;
105 if (pid == 0) {
106 close(pipefd[0]);
107 execv(binderservername, childargv);
108 status = -errno;
109 write(pipefd[1], &status, sizeof(status));
110 fprintf(stderr, "execv failed, %s\n", strerror(errno));
111 _exit(EXIT_FAILURE);
112 }
113 close(pipefd[1]);
114 ret = read(pipefd[0], &status, sizeof(status));
115 //printf("pipe read returned %d, status %d\n", ret, status);
116 close(pipefd[0]);
117 if (ret == sizeof(status)) {
118 ret = status;
119 } else {
120 kill(pid, SIGKILL);
121 if (ret >= 0) {
122 ret = NO_INIT;
123 }
124 }
125 if (ret < 0) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700126 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800127 return ret;
128 }
129 return pid;
130}
131
132class BinderLibTestEnv : public ::testing::Environment {
133 public:
134 BinderLibTestEnv() {}
135 sp<IBinder> getServer(void) {
136 return m_server;
137 }
138
139 private:
140 virtual void SetUp() {
141 m_serverpid = start_server_process(0);
142 //printf("m_serverpid %d\n", m_serverpid);
143 ASSERT_GT(m_serverpid, 0);
144
145 sp<IServiceManager> sm = defaultServiceManager();
146 //printf("%s: pid %d, get service\n", __func__, m_pid);
147 m_server = sm->getService(binderLibTestServiceName);
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800149 //printf("%s: pid %d, get service done\n", __func__, m_pid);
150 }
151 virtual void TearDown() {
152 status_t ret;
153 Parcel data, reply;
154 int exitStatus;
155 pid_t pid;
156
157 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700158 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800159 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
160 EXPECT_EQ(0, ret);
161 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
162 EXPECT_EQ(0, ret);
163 }
164 if (m_serverpid > 0) {
165 //printf("wait for %d\n", m_pids[i]);
166 pid = wait(&exitStatus);
167 EXPECT_EQ(m_serverpid, pid);
168 EXPECT_TRUE(WIFEXITED(exitStatus));
169 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
170 }
171 }
172
173 pid_t m_serverpid;
174 sp<IBinder> m_server;
175};
176
177class BinderLibTest : public ::testing::Test {
178 public:
179 virtual void SetUp() {
180 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
181 }
182 virtual void TearDown() {
183 }
184 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200185 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800186 {
187 int ret;
188 int32_t id;
189 Parcel data, reply;
190 sp<IBinder> binder;
191
Martijn Coenen45b07b42017-08-09 12:07:45 +0200192 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800193 EXPECT_EQ(NO_ERROR, ret);
194
Yi Kongfdd8da92018-06-07 17:52:27 -0700195 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800196 binder = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700197 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800198 ret = reply.readInt32(&id);
199 EXPECT_EQ(NO_ERROR, ret);
200 if (idPtr)
201 *idPtr = id;
202 return binder;
203 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200204
Yi Kongfdd8da92018-06-07 17:52:27 -0700205 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200206 {
207 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
208 }
209
Yi Kongfdd8da92018-06-07 17:52:27 -0700210 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200211 {
212 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
213 }
214
Riley Andrews06b01ad2014-12-18 12:10:08 -0800215 void waitForReadData(int fd, int timeout_ms) {
216 int ret;
217 pollfd pfd = pollfd();
218
219 pfd.fd = fd;
220 pfd.events = POLLIN;
221 ret = poll(&pfd, 1, timeout_ms);
222 EXPECT_EQ(1, ret);
223 }
224
225 sp<IBinder> m_server;
226};
227
228class BinderLibTestBundle : public Parcel
229{
230 public:
231 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800232 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800233 int32_t mark;
234 int32_t bundleLen;
235 size_t pos;
236
237 if (source->readInt32(&mark))
238 return;
239 if (mark != MARK_START)
240 return;
241 if (source->readInt32(&bundleLen))
242 return;
243 pos = source->dataPosition();
244 if (Parcel::appendFrom(source, pos, bundleLen))
245 return;
246 source->setDataPosition(pos + bundleLen);
247 if (source->readInt32(&mark))
248 return;
249 if (mark != MARK_END)
250 return;
251 m_isValid = true;
252 setDataPosition(0);
253 }
254 void appendTo(Parcel *dest) {
255 dest->writeInt32(MARK_START);
256 dest->writeInt32(dataSize());
257 dest->appendFrom(this, 0, dataSize());
258 dest->writeInt32(MARK_END);
259 };
260 bool isValid(void) {
261 return m_isValid;
262 }
263 private:
264 enum {
265 MARK_START = B_PACK_CHARS('B','T','B','S'),
266 MARK_END = B_PACK_CHARS('B','T','B','E'),
267 };
268 bool m_isValid;
269};
270
271class BinderLibTestEvent
272{
273 public:
274 BinderLibTestEvent(void)
275 : m_eventTriggered(false)
276 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700277 pthread_mutex_init(&m_waitMutex, nullptr);
278 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800279 }
280 int waitEvent(int timeout_s)
281 {
282 int ret;
283 pthread_mutex_lock(&m_waitMutex);
284 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800285 struct timespec ts;
286 clock_gettime(CLOCK_REALTIME, &ts);
287 ts.tv_sec += timeout_s;
288 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800289 }
290 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
291 pthread_mutex_unlock(&m_waitMutex);
292 return ret;
293 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200294 pthread_t getTriggeringThread()
295 {
296 return m_triggeringThread;
297 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800298 protected:
299 void triggerEvent(void) {
300 pthread_mutex_lock(&m_waitMutex);
301 pthread_cond_signal(&m_waitCond);
302 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200303 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800304 pthread_mutex_unlock(&m_waitMutex);
305 };
306 private:
307 pthread_mutex_t m_waitMutex;
308 pthread_cond_t m_waitCond;
309 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800311};
312
313class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
314{
315 public:
316 BinderLibTestCallBack()
317 : m_result(NOT_ENOUGH_DATA)
Yi Kongfdd8da92018-06-07 17:52:27 -0700318 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800319 {
320 }
321 status_t getResult(void)
322 {
323 return m_result;
324 }
325
326 private:
327 virtual status_t onTransact(uint32_t code,
328 const Parcel& data, Parcel* reply,
329 uint32_t flags = 0)
330 {
331 (void)reply;
332 (void)flags;
333 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200334 case BINDER_LIB_TEST_CALL_BACK: {
335 status_t status = data.readInt32(&m_result);
336 if (status != NO_ERROR) {
337 m_result = status;
338 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800339 triggerEvent();
340 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200341 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700342 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
343 sp<IBinder> server;
344 int ret;
345 const uint8_t *buf = data.data();
346 size_t size = data.dataSize();
347 if (m_prev_end) {
348 /* 64-bit kernel needs at most 8 bytes to align buffer end */
349 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
350 } else {
351 EXPECT_TRUE(IsPageAligned((void *)buf));
352 }
353
354 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
355
356 if (size > 0) {
357 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
358 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
359 data, reply);
360 EXPECT_EQ(NO_ERROR, ret);
361 }
362 return NO_ERROR;
363 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800364 default:
365 return UNKNOWN_TRANSACTION;
366 }
367 }
368
369 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700370 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800371};
372
373class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
374{
375 private:
376 virtual void binderDied(const wp<IBinder>& who) {
377 (void)who;
378 triggerEvent();
379 };
380};
381
382TEST_F(BinderLibTest, NopTransaction) {
383 status_t ret;
384 Parcel data, reply;
385 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
386 EXPECT_EQ(NO_ERROR, ret);
387}
388
389TEST_F(BinderLibTest, SetError) {
390 int32_t testValue[] = { 0, -123, 123 };
391 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
392 status_t ret;
393 Parcel data, reply;
394 data.writeInt32(testValue[i]);
395 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
396 EXPECT_EQ(testValue[i], ret);
397 }
398}
399
400TEST_F(BinderLibTest, GetId) {
401 status_t ret;
402 int32_t id;
403 Parcel data, reply;
404 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
405 EXPECT_EQ(NO_ERROR, ret);
406 ret = reply.readInt32(&id);
407 EXPECT_EQ(NO_ERROR, ret);
408 EXPECT_EQ(0, id);
409}
410
411TEST_F(BinderLibTest, PtrSize) {
412 status_t ret;
413 int32_t ptrsize;
414 Parcel data, reply;
415 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700416 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800417 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
418 EXPECT_EQ(NO_ERROR, ret);
419 ret = reply.readInt32(&ptrsize);
420 EXPECT_EQ(NO_ERROR, ret);
421 RecordProperty("TestPtrSize", sizeof(void *));
422 RecordProperty("ServerPtrSize", sizeof(void *));
423}
424
425TEST_F(BinderLibTest, IndirectGetId2)
426{
427 status_t ret;
428 int32_t id;
429 int32_t count;
430 Parcel data, reply;
431 int32_t serverId[3];
432
433 data.writeInt32(ARRAY_SIZE(serverId));
434 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
435 sp<IBinder> server;
436 BinderLibTestBundle datai;
437
438 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700439 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800440 data.writeStrongBinder(server);
441 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
442 datai.appendTo(&data);
443 }
444
445 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
446 ASSERT_EQ(NO_ERROR, ret);
447
448 ret = reply.readInt32(&id);
449 ASSERT_EQ(NO_ERROR, ret);
450 EXPECT_EQ(0, id);
451
452 ret = reply.readInt32(&count);
453 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700454 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800455
456 for (size_t i = 0; i < (size_t)count; i++) {
457 BinderLibTestBundle replyi(&reply);
458 EXPECT_TRUE(replyi.isValid());
459 ret = replyi.readInt32(&id);
460 EXPECT_EQ(NO_ERROR, ret);
461 EXPECT_EQ(serverId[i], id);
462 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
463 }
464
465 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
466}
467
468TEST_F(BinderLibTest, IndirectGetId3)
469{
470 status_t ret;
471 int32_t id;
472 int32_t count;
473 Parcel data, reply;
474 int32_t serverId[3];
475
476 data.writeInt32(ARRAY_SIZE(serverId));
477 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
478 sp<IBinder> server;
479 BinderLibTestBundle datai;
480 BinderLibTestBundle datai2;
481
482 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700483 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800484 data.writeStrongBinder(server);
485 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
486
487 datai.writeInt32(1);
488 datai.writeStrongBinder(m_server);
489 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
490 datai2.appendTo(&datai);
491
492 datai.appendTo(&data);
493 }
494
495 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
496 ASSERT_EQ(NO_ERROR, ret);
497
498 ret = reply.readInt32(&id);
499 ASSERT_EQ(NO_ERROR, ret);
500 EXPECT_EQ(0, id);
501
502 ret = reply.readInt32(&count);
503 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700504 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800505
506 for (size_t i = 0; i < (size_t)count; i++) {
507 int32_t counti;
508
509 BinderLibTestBundle replyi(&reply);
510 EXPECT_TRUE(replyi.isValid());
511 ret = replyi.readInt32(&id);
512 EXPECT_EQ(NO_ERROR, ret);
513 EXPECT_EQ(serverId[i], id);
514
515 ret = replyi.readInt32(&counti);
516 ASSERT_EQ(NO_ERROR, ret);
517 EXPECT_EQ(1, counti);
518
519 BinderLibTestBundle replyi2(&replyi);
520 EXPECT_TRUE(replyi2.isValid());
521 ret = replyi2.readInt32(&id);
522 EXPECT_EQ(NO_ERROR, ret);
523 EXPECT_EQ(0, id);
524 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
525
526 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
527 }
528
529 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
530}
531
532TEST_F(BinderLibTest, CallBack)
533{
534 status_t ret;
535 Parcel data, reply;
536 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
537 data.writeStrongBinder(callBack);
538 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
539 EXPECT_EQ(NO_ERROR, ret);
540 ret = callBack->waitEvent(5);
541 EXPECT_EQ(NO_ERROR, ret);
542 ret = callBack->getResult();
543 EXPECT_EQ(NO_ERROR, ret);
544}
545
546TEST_F(BinderLibTest, AddServer)
547{
548 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700549 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800550}
551
Riley Andrews06b01ad2014-12-18 12:10:08 -0800552TEST_F(BinderLibTest, DeathNotificationStrongRef)
553{
554 status_t ret;
555 sp<IBinder> sbinder;
556
557 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
558
559 {
560 sp<IBinder> binder = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700561 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800562 ret = binder->linkToDeath(testDeathRecipient);
563 EXPECT_EQ(NO_ERROR, ret);
564 sbinder = binder;
565 }
566 {
567 Parcel data, reply;
568 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
569 EXPECT_EQ(0, ret);
570 }
571 IPCThreadState::self()->flushCommands();
572 ret = testDeathRecipient->waitEvent(5);
573 EXPECT_EQ(NO_ERROR, ret);
574 ret = sbinder->unlinkToDeath(testDeathRecipient);
575 EXPECT_EQ(DEAD_OBJECT, ret);
576}
577
578TEST_F(BinderLibTest, DeathNotificationMultiple)
579{
580 status_t ret;
581 const int clientcount = 2;
582 sp<IBinder> target;
583 sp<IBinder> linkedclient[clientcount];
584 sp<BinderLibTestCallBack> callBack[clientcount];
585 sp<IBinder> passiveclient[clientcount];
586
587 target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700588 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800589 for (int i = 0; i < clientcount; i++) {
590 {
591 Parcel data, reply;
592
593 linkedclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700594 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800595 callBack[i] = new BinderLibTestCallBack();
596 data.writeStrongBinder(target);
597 data.writeStrongBinder(callBack[i]);
598 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
599 EXPECT_EQ(NO_ERROR, ret);
600 }
601 {
602 Parcel data, reply;
603
604 passiveclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700605 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800606 data.writeStrongBinder(target);
607 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
608 EXPECT_EQ(NO_ERROR, ret);
609 }
610 }
611 {
612 Parcel data, reply;
613 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
614 EXPECT_EQ(0, ret);
615 }
616
617 for (int i = 0; i < clientcount; i++) {
618 ret = callBack[i]->waitEvent(5);
619 EXPECT_EQ(NO_ERROR, ret);
620 ret = callBack[i]->getResult();
621 EXPECT_EQ(NO_ERROR, ret);
622 }
623}
624
Martijn Coenenf7100e42017-07-31 12:14:09 +0200625TEST_F(BinderLibTest, DeathNotificationThread)
626{
627 status_t ret;
628 sp<BinderLibTestCallBack> callback;
629 sp<IBinder> target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700630 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200631 sp<IBinder> client = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700632 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200633
634 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
635
636 ret = target->linkToDeath(testDeathRecipient);
637 EXPECT_EQ(NO_ERROR, ret);
638
639 {
640 Parcel data, reply;
641 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
642 EXPECT_EQ(0, ret);
643 }
644
645 /* Make sure it's dead */
646 testDeathRecipient->waitEvent(5);
647
648 /* Now, pass the ref to another process and ask that process to
649 * call linkToDeath() on it, and wait for a response. This tests
650 * two things:
651 * 1) You still get death notifications when calling linkToDeath()
652 * on a ref that is already dead when it was passed to you.
653 * 2) That death notifications are not directly pushed to the thread
654 * registering them, but to the threadpool (proc workqueue) instead.
655 *
656 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
657 * is blocked on a condition variable waiting for the death notification to be
658 * called; therefore, that thread is not available for handling proc work.
659 * So, if the death notification was pushed to the thread workqueue, the callback
660 * would never be called, and the test would timeout and fail.
661 *
662 * Note that we can't do this part of the test from this thread itself, because
663 * the binder driver would only push death notifications to the thread if
664 * it is a looper thread, which this thread is not.
665 *
666 * See b/23525545 for details.
667 */
668 {
669 Parcel data, reply;
670
671 callback = new BinderLibTestCallBack();
672 data.writeStrongBinder(target);
673 data.writeStrongBinder(callback);
674 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
675 EXPECT_EQ(NO_ERROR, ret);
676 }
677
678 ret = callback->waitEvent(5);
679 EXPECT_EQ(NO_ERROR, ret);
680 ret = callback->getResult();
681 EXPECT_EQ(NO_ERROR, ret);
682}
683
Riley Andrews06b01ad2014-12-18 12:10:08 -0800684TEST_F(BinderLibTest, PassFile) {
685 int ret;
686 int pipefd[2];
687 uint8_t buf[1] = { 0 };
688 uint8_t write_value = 123;
689
690 ret = pipe2(pipefd, O_NONBLOCK);
691 ASSERT_EQ(0, ret);
692
693 {
694 Parcel data, reply;
695 uint8_t writebuf[1] = { write_value };
696
697 ret = data.writeFileDescriptor(pipefd[1], true);
698 EXPECT_EQ(NO_ERROR, ret);
699
700 ret = data.writeInt32(sizeof(writebuf));
701 EXPECT_EQ(NO_ERROR, ret);
702
703 ret = data.write(writebuf, sizeof(writebuf));
704 EXPECT_EQ(NO_ERROR, ret);
705
706 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
707 EXPECT_EQ(NO_ERROR, ret);
708 }
709
710 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700711 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800712 EXPECT_EQ(write_value, buf[0]);
713
714 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
715
716 ret = read(pipefd[0], buf, sizeof(buf));
717 EXPECT_EQ(0, ret);
718
719 close(pipefd[0]);
720}
721
Ryo Hashimotobf551892018-05-31 16:58:35 +0900722TEST_F(BinderLibTest, PassParcelFileDescriptor) {
723 const int datasize = 123;
724 std::vector<uint8_t> writebuf(datasize);
725 for (size_t i = 0; i < writebuf.size(); ++i) {
726 writebuf[i] = i;
727 }
728
729 android::base::unique_fd read_end, write_end;
730 {
731 int pipefd[2];
732 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
733 read_end.reset(pipefd[0]);
734 write_end.reset(pipefd[1]);
735 }
736 {
737 Parcel data;
738 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
739 write_end.reset();
740 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
741 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
742
743 Parcel reply;
744 EXPECT_EQ(NO_ERROR,
745 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
746 &reply));
747 }
748 std::vector<uint8_t> readbuf(datasize);
749 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
750 EXPECT_EQ(writebuf, readbuf);
751
752 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
753
754 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
755}
756
Riley Andrews06b01ad2014-12-18 12:10:08 -0800757TEST_F(BinderLibTest, PromoteLocal) {
758 sp<IBinder> strong = new BBinder();
759 wp<IBinder> weak = strong;
760 sp<IBinder> strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700761 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800762 EXPECT_EQ(strong, strong_from_weak);
Yi Kongfdd8da92018-06-07 17:52:27 -0700763 strong = nullptr;
764 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800765 strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700766 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800767}
768
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700769TEST_F(BinderLibTest, LocalGetExtension) {
770 sp<BBinder> binder = new BBinder();
771 sp<IBinder> ext = new BBinder();
772 binder->setExtension(ext);
773 EXPECT_EQ(ext, binder->getExtension());
774}
775
776TEST_F(BinderLibTest, RemoteGetExtension) {
777 sp<IBinder> server = addServer();
778 ASSERT_TRUE(server != nullptr);
779
780 sp<IBinder> extension;
781 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
782 ASSERT_NE(nullptr, extension.get());
783
784 EXPECT_EQ(NO_ERROR, extension->pingBinder());
785}
786
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700787TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
788 status_t ret;
789 Parcel data, reply;
790
791 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
792 EXPECT_EQ(NO_ERROR, ret);
793
794 const flat_binder_object *fb = reply.readObject(false);
Yi Kongfdd8da92018-06-07 17:52:27 -0700795 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800796 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
797 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
798 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
799 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700800}
801
Connor O'Brien52be2c92016-09-20 14:18:08 -0700802TEST_F(BinderLibTest, FreedBinder) {
803 status_t ret;
804
805 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700806 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700807
808 __u32 freedHandle;
809 wp<IBinder> keepFreedBinder;
810 {
811 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700812 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
813 ASSERT_EQ(NO_ERROR, ret);
814 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
815 freedHandle = freed->handle;
816 /* Add a weak ref to the freed binder so the driver does not
817 * delete its reference to it - otherwise the transaction
818 * fails regardless of whether the driver is fixed.
819 */
Steven Morelande171d622019-07-17 16:06:01 -0700820 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700821 }
Steven Morelande171d622019-07-17 16:06:01 -0700822 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700823 {
824 Parcel data, reply;
825 data.writeStrongBinder(server);
826 /* Replace original handle with handle to the freed binder */
827 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
828 __u32 oldHandle = strong->handle;
829 strong->handle = freedHandle;
830 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
831 /* Returns DEAD_OBJECT (-32) if target crashes and
832 * FAILED_TRANSACTION if the driver rejects the invalid
833 * object.
834 */
835 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
836 /* Restore original handle so parcel destructor does not use
837 * the wrong handle.
838 */
839 strong->handle = oldHandle;
840 }
841}
842
Sherry Yang336cdd32017-07-24 14:12:27 -0700843TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
844 status_t ret;
845 Parcel data, reply;
846 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
847 for (int i = 0; i < 2; i++) {
848 BinderLibTestBundle datai;
849 datai.appendFrom(&data, 0, data.dataSize());
850
851 data.freeData();
852 data.writeInt32(1);
853 data.writeStrongBinder(callBack);
854 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
855
856 datai.appendTo(&data);
857 }
858 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
859 EXPECT_EQ(NO_ERROR, ret);
860}
861
Martijn Coenen45b07b42017-08-09 12:07:45 +0200862TEST_F(BinderLibTest, OnewayQueueing)
863{
864 status_t ret;
865 Parcel data, data2;
866
867 sp<IBinder> pollServer = addPollServer();
868
869 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
870 data.writeStrongBinder(callBack);
871 data.writeInt32(500000); // delay in us before calling back
872
873 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
874 data2.writeStrongBinder(callBack2);
875 data2.writeInt32(0); // delay in us
876
Yi Kongfdd8da92018-06-07 17:52:27 -0700877 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200878 EXPECT_EQ(NO_ERROR, ret);
879
880 // The delay ensures that this second transaction will end up on the async_todo list
881 // (for a single-threaded server)
Yi Kongfdd8da92018-06-07 17:52:27 -0700882 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200883 EXPECT_EQ(NO_ERROR, ret);
884
885 // The server will ensure that the two transactions are handled in the expected order;
886 // If the ordering is not as expected, an error will be returned through the callbacks.
887 ret = callBack->waitEvent(2);
888 EXPECT_EQ(NO_ERROR, ret);
889 ret = callBack->getResult();
890 EXPECT_EQ(NO_ERROR, ret);
891
892 ret = callBack2->waitEvent(2);
893 EXPECT_EQ(NO_ERROR, ret);
894 ret = callBack2->getResult();
895 EXPECT_EQ(NO_ERROR, ret);
896}
897
Riley Andrews06b01ad2014-12-18 12:10:08 -0800898class BinderLibTestService : public BBinder
899{
900 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800901 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800902 : m_id(id)
903 , m_nextServerId(id + 1)
904 , m_serverStartRequested(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700905 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800906 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700907 pthread_mutex_init(&m_serverWaitMutex, nullptr);
908 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800909 }
910 ~BinderLibTestService()
911 {
912 exit(EXIT_SUCCESS);
913 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200914
915 void processPendingCall() {
Yi Kongfdd8da92018-06-07 17:52:27 -0700916 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200917 Parcel data;
918 data.writeInt32(NO_ERROR);
919 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kongfdd8da92018-06-07 17:52:27 -0700920 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200921 }
922 }
923
Riley Andrews06b01ad2014-12-18 12:10:08 -0800924 virtual status_t onTransact(uint32_t code,
925 const Parcel& data, Parcel* reply,
926 uint32_t flags = 0) {
927 //printf("%s: code %d\n", __func__, code);
928 (void)flags;
929
930 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
931 return PERMISSION_DENIED;
932 }
933 switch (code) {
934 case BINDER_LIB_TEST_REGISTER_SERVER: {
935 int32_t id;
936 sp<IBinder> binder;
937 id = data.readInt32();
938 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700939 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800940 return BAD_VALUE;
941 }
942
943 if (m_id != 0)
944 return INVALID_OPERATION;
945
946 pthread_mutex_lock(&m_serverWaitMutex);
947 if (m_serverStartRequested) {
948 m_serverStartRequested = false;
949 m_serverStarted = binder;
950 pthread_cond_signal(&m_serverWaitCond);
951 }
952 pthread_mutex_unlock(&m_serverWaitMutex);
953 return NO_ERROR;
954 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200955 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -0800956 case BINDER_LIB_TEST_ADD_SERVER: {
957 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800958 int serverid;
959
960 if (m_id != 0) {
961 return INVALID_OPERATION;
962 }
963 pthread_mutex_lock(&m_serverWaitMutex);
964 if (m_serverStartRequested) {
965 ret = -EBUSY;
966 } else {
967 serverid = m_nextServerId++;
968 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200969 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800970
971 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200972 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800973 pthread_mutex_lock(&m_serverWaitMutex);
974 }
975 if (ret > 0) {
976 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800977 struct timespec ts;
978 clock_gettime(CLOCK_REALTIME, &ts);
979 ts.tv_sec += 5;
980 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800981 }
982 if (m_serverStartRequested) {
983 m_serverStartRequested = false;
984 ret = -ETIMEDOUT;
985 } else {
986 reply->writeStrongBinder(m_serverStarted);
987 reply->writeInt32(serverid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700988 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800989 ret = NO_ERROR;
990 }
991 } else if (ret >= 0) {
992 m_serverStartRequested = false;
993 ret = UNKNOWN_ERROR;
994 }
995 pthread_mutex_unlock(&m_serverWaitMutex);
996 return ret;
997 }
998 case BINDER_LIB_TEST_NOP_TRANSACTION:
999 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001000 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1001 // Note: this transaction is only designed for use with a
1002 // poll() server. See comments around epoll_wait().
Yi Kongfdd8da92018-06-07 17:52:27 -07001003 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001004 // A callback was already pending; this means that
1005 // we received a second call while still processing
1006 // the first one. Fail the test.
1007 sp<IBinder> callback = data.readStrongBinder();
1008 Parcel data2;
1009 data2.writeInt32(UNKNOWN_ERROR);
1010
Yi Kongfdd8da92018-06-07 17:52:27 -07001011 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001012 } else {
1013 m_callback = data.readStrongBinder();
1014 int32_t delayUs = data.readInt32();
1015 /*
1016 * It's necessary that we sleep here, so the next
1017 * transaction the caller makes will be queued to
1018 * the async queue.
1019 */
1020 usleep(delayUs);
1021
1022 /*
1023 * Now when we return, libbinder will tell the kernel
1024 * we are done with this transaction, and the kernel
1025 * can move the queued transaction to either the
1026 * thread todo worklist (for kernels without the fix),
1027 * or the proc todo worklist. In case of the former,
1028 * the next outbound call will pick up the pending
1029 * transaction, which leads to undesired reentrant
1030 * behavior. This is caught in the if() branch above.
1031 */
1032 }
1033
1034 return NO_ERROR;
1035 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001036 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1037 Parcel data2, reply2;
1038 sp<IBinder> binder;
1039 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001040 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001041 return BAD_VALUE;
1042 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001043 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001044 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1045 return NO_ERROR;
1046 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001047 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1048 reply->writeStrongBinder(this);
1049 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001050 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1051 reply->writeInt32(m_id);
1052 return NO_ERROR;
1053 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1054 int32_t count;
1055 uint32_t indirect_code;
1056 sp<IBinder> binder;
1057
1058 count = data.readInt32();
1059 reply->writeInt32(m_id);
1060 reply->writeInt32(count);
1061 for (int i = 0; i < count; i++) {
1062 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001063 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001064 return BAD_VALUE;
1065 }
1066 indirect_code = data.readInt32();
1067 BinderLibTestBundle data2(&data);
1068 if (!data2.isValid()) {
1069 return BAD_VALUE;
1070 }
1071 BinderLibTestBundle reply2;
1072 binder->transact(indirect_code, data2, &reply2);
1073 reply2.appendTo(reply);
1074 }
1075 return NO_ERROR;
1076 }
1077 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1078 reply->setError(data.readInt32());
1079 return NO_ERROR;
1080 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1081 reply->writeInt32(sizeof(void *));
1082 return NO_ERROR;
1083 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1084 return NO_ERROR;
1085 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1086 m_strongRef = data.readStrongBinder();
1087 return NO_ERROR;
1088 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1089 int ret;
1090 Parcel data2, reply2;
1091 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1092 sp<IBinder> target;
1093 sp<IBinder> callback;
1094
1095 target = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001096 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001097 return BAD_VALUE;
1098 }
1099 callback = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001100 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001101 return BAD_VALUE;
1102 }
1103 ret = target->linkToDeath(testDeathRecipient);
1104 if (ret == NO_ERROR)
1105 ret = testDeathRecipient->waitEvent(5);
1106 data2.writeInt32(ret);
1107 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1108 return NO_ERROR;
1109 }
1110 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1111 int ret;
1112 int32_t size;
1113 const void *buf;
1114 int fd;
1115
1116 fd = data.readFileDescriptor();
1117 if (fd < 0) {
1118 return BAD_VALUE;
1119 }
1120 ret = data.readInt32(&size);
1121 if (ret != NO_ERROR) {
1122 return ret;
1123 }
1124 buf = data.readInplace(size);
Yi Kongfdd8da92018-06-07 17:52:27 -07001125 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001126 return BAD_VALUE;
1127 }
1128 ret = write(fd, buf, size);
1129 if (ret != size)
1130 return UNKNOWN_ERROR;
1131 return NO_ERROR;
1132 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001133 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1134 int ret;
1135 int32_t size;
1136 const void *buf;
1137 android::base::unique_fd fd;
1138
1139 ret = data.readUniqueParcelFileDescriptor(&fd);
1140 if (ret != NO_ERROR) {
1141 return ret;
1142 }
1143 ret = data.readInt32(&size);
1144 if (ret != NO_ERROR) {
1145 return ret;
1146 }
1147 buf = data.readInplace(size);
Yi Kong87d465c2018-07-24 01:14:06 -07001148 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001149 return BAD_VALUE;
1150 }
1151 ret = write(fd.get(), buf, size);
1152 if (ret != size) return UNKNOWN_ERROR;
1153 return NO_ERROR;
1154 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001155 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1156 alarm(10);
1157 return NO_ERROR;
1158 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kongfdd8da92018-06-07 17:52:27 -07001159 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001160 ;
1161 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001162 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001163 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001164 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001165 return NO_ERROR;
1166 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001167 default:
1168 return UNKNOWN_TRANSACTION;
1169 };
1170 }
1171 private:
1172 int32_t m_id;
1173 int32_t m_nextServerId;
1174 pthread_mutex_t m_serverWaitMutex;
1175 pthread_cond_t m_serverWaitCond;
1176 bool m_serverStartRequested;
1177 sp<IBinder> m_serverStarted;
1178 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001179 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001180};
1181
Martijn Coenen45b07b42017-08-09 12:07:45 +02001182int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001183{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001184 binderLibTestServiceName += String16(binderserversuffix);
1185
Riley Andrews06b01ad2014-12-18 12:10:08 -08001186 status_t ret;
1187 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001188 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001189 {
1190 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001191
1192 /*
1193 * Normally would also contain functionality as well, but we are only
1194 * testing the extension mechanism.
1195 */
1196 testService->setExtension(new BBinder());
1197
Martijn Coenen45b07b42017-08-09 12:07:45 +02001198 /*
1199 * We need this below, but can't hold a sp<> because it prevents the
1200 * node from being cleaned up automatically. It's safe in this case
1201 * because of how the tests are written.
1202 */
1203 testServicePtr = testService.get();
1204
Riley Andrews06b01ad2014-12-18 12:10:08 -08001205 if (index == 0) {
1206 ret = sm->addService(binderLibTestServiceName, testService);
1207 } else {
1208 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1209 Parcel data, reply;
1210 data.writeInt32(index);
1211 data.writeStrongBinder(testService);
1212
1213 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1214 }
1215 }
1216 write(readypipefd, &ret, sizeof(ret));
1217 close(readypipefd);
1218 //printf("%s: ret %d\n", __func__, ret);
1219 if (ret)
1220 return 1;
1221 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001222 if (usePoll) {
1223 int fd;
1224 struct epoll_event ev;
1225 int epoll_fd;
1226 IPCThreadState::self()->setupPolling(&fd);
1227 if (fd < 0) {
1228 return 1;
1229 }
1230 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1231
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001232 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001233 if (epoll_fd == -1) {
1234 return 1;
1235 }
1236
1237 ev.events = EPOLLIN;
1238 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1239 return 1;
1240 }
1241
1242 while (1) {
1243 /*
1244 * We simulate a single-threaded process using the binder poll
1245 * interface; besides handling binder commands, it can also
1246 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001247 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001248 *
1249 * processPendingCall() will then issue that transaction.
1250 */
1251 struct epoll_event events[1];
1252 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1253 if (numEvents < 0) {
1254 if (errno == EINTR) {
1255 continue;
1256 }
1257 return 1;
1258 }
1259 if (numEvents > 0) {
1260 IPCThreadState::self()->handlePolledCommands();
1261 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1262 testServicePtr->processPendingCall();
1263 }
1264 }
1265 } else {
1266 ProcessState::self()->startThreadPool();
1267 IPCThreadState::self()->joinThreadPool();
1268 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001269 //printf("%s: joinThreadPool returned\n", __func__);
1270 return 1; /* joinThreadPool should not return */
1271}
1272
1273int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001274 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001275 binderservername = argv[2];
1276 } else {
1277 binderservername = argv[0];
1278 }
1279
Martijn Coenen45b07b42017-08-09 12:07:45 +02001280 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1281 binderserversuffix = argv[5];
1282 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001283 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001284 binderserversuffix = new char[16];
1285 snprintf(binderserversuffix, 16, "%d", getpid());
1286 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001287
1288 ::testing::InitGoogleTest(&argc, argv);
1289 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1290 ProcessState::self()->startThreadPool();
1291 return RUN_ALL_TESTS();
1292}