blob: f8ee32c70d40524ba515b37b3301883277cb5745 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Steven Morelandd63ed9d2019-09-04 18:15:25 -070031#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020032#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080033#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020034
Riley Andrews06b01ad2014-12-18 12:10:08 -080035#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
36
37using namespace android;
38
Sherry Yang336cdd32017-07-24 14:12:27 -070039static ::testing::AssertionResult IsPageAligned(void *buf) {
40 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
41 return ::testing::AssertionSuccess();
42 else
43 return ::testing::AssertionFailure() << buf << " is not page aligned";
44}
45
Riley Andrews06b01ad2014-12-18 12:10:08 -080046static testing::Environment* binder_env;
47static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070048static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080049static char binderserverarg[] = "--binderserver";
50
Steven Morelandbf1915b2020-07-16 22:43:02 +000051static constexpr int kSchedPolicy = SCHED_RR;
52static constexpr int kSchedPriority = 7;
53
Riley Andrews06b01ad2014-12-18 12:10:08 -080054static String16 binderLibTestServiceName = String16("test.binderLib");
55
56enum BinderLibTestTranscationCode {
57 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
58 BINDER_LIB_TEST_REGISTER_SERVER,
59 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020060 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070062 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020063 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080064 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070065 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080066 BINDER_LIB_TEST_GET_ID_TRANSACTION,
67 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
68 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
69 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
70 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
71 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
72 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090073 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080074 BINDER_LIB_TEST_EXIT_TRANSACTION,
75 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
76 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070077 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010078 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000079 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080080 BINDER_LIB_TEST_ECHO_VECTOR,
Riley Andrews06b01ad2014-12-18 12:10:08 -080081};
82
Martijn Coenen45b07b42017-08-09 12:07:45 +020083pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080084{
85 int ret;
86 pid_t pid;
87 status_t status;
88 int pipefd[2];
89 char stri[16];
90 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020091 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080092 char *childargv[] = {
93 binderservername,
94 binderserverarg,
95 stri,
96 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020097 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070098 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070099 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800100 };
101
102 ret = pipe(pipefd);
103 if (ret < 0)
104 return ret;
105
106 snprintf(stri, sizeof(stri), "%d", arg2);
107 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200108 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800109
110 pid = fork();
111 if (pid == -1)
112 return pid;
113 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800114 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800115 close(pipefd[0]);
116 execv(binderservername, childargv);
117 status = -errno;
118 write(pipefd[1], &status, sizeof(status));
119 fprintf(stderr, "execv failed, %s\n", strerror(errno));
120 _exit(EXIT_FAILURE);
121 }
122 close(pipefd[1]);
123 ret = read(pipefd[0], &status, sizeof(status));
124 //printf("pipe read returned %d, status %d\n", ret, status);
125 close(pipefd[0]);
126 if (ret == sizeof(status)) {
127 ret = status;
128 } else {
129 kill(pid, SIGKILL);
130 if (ret >= 0) {
131 ret = NO_INIT;
132 }
133 }
134 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700135 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800136 return ret;
137 }
138 return pid;
139}
140
141class BinderLibTestEnv : public ::testing::Environment {
142 public:
143 BinderLibTestEnv() {}
144 sp<IBinder> getServer(void) {
145 return m_server;
146 }
147
148 private:
149 virtual void SetUp() {
150 m_serverpid = start_server_process(0);
151 //printf("m_serverpid %d\n", m_serverpid);
152 ASSERT_GT(m_serverpid, 0);
153
154 sp<IServiceManager> sm = defaultServiceManager();
155 //printf("%s: pid %d, get service\n", __func__, m_pid);
156 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700157 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800158 //printf("%s: pid %d, get service done\n", __func__, m_pid);
159 }
160 virtual void TearDown() {
161 status_t ret;
162 Parcel data, reply;
163 int exitStatus;
164 pid_t pid;
165
166 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700167 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800168 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
169 EXPECT_EQ(0, ret);
170 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
171 EXPECT_EQ(0, ret);
172 }
173 if (m_serverpid > 0) {
174 //printf("wait for %d\n", m_pids[i]);
175 pid = wait(&exitStatus);
176 EXPECT_EQ(m_serverpid, pid);
177 EXPECT_TRUE(WIFEXITED(exitStatus));
178 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
179 }
180 }
181
182 pid_t m_serverpid;
183 sp<IBinder> m_server;
184};
185
186class BinderLibTest : public ::testing::Test {
187 public:
188 virtual void SetUp() {
189 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000190 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800191 }
192 virtual void TearDown() {
193 }
194 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200195 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800196 {
197 int ret;
198 int32_t id;
199 Parcel data, reply;
200 sp<IBinder> binder;
201
Martijn Coenen45b07b42017-08-09 12:07:45 +0200202 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800203 EXPECT_EQ(NO_ERROR, ret);
204
Yi Kong91635562018-06-07 14:38:36 -0700205 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800206 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700207 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800208 ret = reply.readInt32(&id);
209 EXPECT_EQ(NO_ERROR, ret);
210 if (idPtr)
211 *idPtr = id;
212 return binder;
213 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200214
Yi Kong91635562018-06-07 14:38:36 -0700215 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200216 {
217 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
218 }
219
Yi Kong91635562018-06-07 14:38:36 -0700220 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200221 {
222 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
223 }
224
Riley Andrews06b01ad2014-12-18 12:10:08 -0800225 void waitForReadData(int fd, int timeout_ms) {
226 int ret;
227 pollfd pfd = pollfd();
228
229 pfd.fd = fd;
230 pfd.events = POLLIN;
231 ret = poll(&pfd, 1, timeout_ms);
232 EXPECT_EQ(1, ret);
233 }
234
235 sp<IBinder> m_server;
236};
237
238class BinderLibTestBundle : public Parcel
239{
240 public:
241 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800242 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800243 int32_t mark;
244 int32_t bundleLen;
245 size_t pos;
246
247 if (source->readInt32(&mark))
248 return;
249 if (mark != MARK_START)
250 return;
251 if (source->readInt32(&bundleLen))
252 return;
253 pos = source->dataPosition();
254 if (Parcel::appendFrom(source, pos, bundleLen))
255 return;
256 source->setDataPosition(pos + bundleLen);
257 if (source->readInt32(&mark))
258 return;
259 if (mark != MARK_END)
260 return;
261 m_isValid = true;
262 setDataPosition(0);
263 }
264 void appendTo(Parcel *dest) {
265 dest->writeInt32(MARK_START);
266 dest->writeInt32(dataSize());
267 dest->appendFrom(this, 0, dataSize());
268 dest->writeInt32(MARK_END);
269 };
270 bool isValid(void) {
271 return m_isValid;
272 }
273 private:
274 enum {
275 MARK_START = B_PACK_CHARS('B','T','B','S'),
276 MARK_END = B_PACK_CHARS('B','T','B','E'),
277 };
278 bool m_isValid;
279};
280
281class BinderLibTestEvent
282{
283 public:
284 BinderLibTestEvent(void)
285 : m_eventTriggered(false)
286 {
Yi Kong91635562018-06-07 14:38:36 -0700287 pthread_mutex_init(&m_waitMutex, nullptr);
288 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800289 }
290 int waitEvent(int timeout_s)
291 {
292 int ret;
293 pthread_mutex_lock(&m_waitMutex);
294 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800295 struct timespec ts;
296 clock_gettime(CLOCK_REALTIME, &ts);
297 ts.tv_sec += timeout_s;
298 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800299 }
300 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
301 pthread_mutex_unlock(&m_waitMutex);
302 return ret;
303 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200304 pthread_t getTriggeringThread()
305 {
306 return m_triggeringThread;
307 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800308 protected:
309 void triggerEvent(void) {
310 pthread_mutex_lock(&m_waitMutex);
311 pthread_cond_signal(&m_waitCond);
312 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200313 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314 pthread_mutex_unlock(&m_waitMutex);
315 };
316 private:
317 pthread_mutex_t m_waitMutex;
318 pthread_cond_t m_waitCond;
319 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200320 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800321};
322
323class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
324{
325 public:
326 BinderLibTestCallBack()
327 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700328 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800329 {
330 }
331 status_t getResult(void)
332 {
333 return m_result;
334 }
335
336 private:
337 virtual status_t onTransact(uint32_t code,
338 const Parcel& data, Parcel* reply,
339 uint32_t flags = 0)
340 {
341 (void)reply;
342 (void)flags;
343 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200344 case BINDER_LIB_TEST_CALL_BACK: {
345 status_t status = data.readInt32(&m_result);
346 if (status != NO_ERROR) {
347 m_result = status;
348 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800349 triggerEvent();
350 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200351 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700352 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
353 sp<IBinder> server;
354 int ret;
355 const uint8_t *buf = data.data();
356 size_t size = data.dataSize();
357 if (m_prev_end) {
358 /* 64-bit kernel needs at most 8 bytes to align buffer end */
359 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
360 } else {
361 EXPECT_TRUE(IsPageAligned((void *)buf));
362 }
363
364 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
365
366 if (size > 0) {
367 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
368 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
369 data, reply);
370 EXPECT_EQ(NO_ERROR, ret);
371 }
372 return NO_ERROR;
373 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800374 default:
375 return UNKNOWN_TRANSACTION;
376 }
377 }
378
379 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700380 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800381};
382
383class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
384{
385 private:
386 virtual void binderDied(const wp<IBinder>& who) {
387 (void)who;
388 triggerEvent();
389 };
390};
391
392TEST_F(BinderLibTest, NopTransaction) {
393 status_t ret;
394 Parcel data, reply;
395 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
396 EXPECT_EQ(NO_ERROR, ret);
397}
398
399TEST_F(BinderLibTest, SetError) {
400 int32_t testValue[] = { 0, -123, 123 };
401 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
402 status_t ret;
403 Parcel data, reply;
404 data.writeInt32(testValue[i]);
405 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
406 EXPECT_EQ(testValue[i], ret);
407 }
408}
409
410TEST_F(BinderLibTest, GetId) {
411 status_t ret;
412 int32_t id;
413 Parcel data, reply;
414 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
415 EXPECT_EQ(NO_ERROR, ret);
416 ret = reply.readInt32(&id);
417 EXPECT_EQ(NO_ERROR, ret);
418 EXPECT_EQ(0, id);
419}
420
421TEST_F(BinderLibTest, PtrSize) {
422 status_t ret;
423 int32_t ptrsize;
424 Parcel data, reply;
425 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700426 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800427 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
428 EXPECT_EQ(NO_ERROR, ret);
429 ret = reply.readInt32(&ptrsize);
430 EXPECT_EQ(NO_ERROR, ret);
431 RecordProperty("TestPtrSize", sizeof(void *));
432 RecordProperty("ServerPtrSize", sizeof(void *));
433}
434
435TEST_F(BinderLibTest, IndirectGetId2)
436{
437 status_t ret;
438 int32_t id;
439 int32_t count;
440 Parcel data, reply;
441 int32_t serverId[3];
442
443 data.writeInt32(ARRAY_SIZE(serverId));
444 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
445 sp<IBinder> server;
446 BinderLibTestBundle datai;
447
448 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700449 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800450 data.writeStrongBinder(server);
451 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
452 datai.appendTo(&data);
453 }
454
455 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
456 ASSERT_EQ(NO_ERROR, ret);
457
458 ret = reply.readInt32(&id);
459 ASSERT_EQ(NO_ERROR, ret);
460 EXPECT_EQ(0, id);
461
462 ret = reply.readInt32(&count);
463 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700464 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800465
466 for (size_t i = 0; i < (size_t)count; i++) {
467 BinderLibTestBundle replyi(&reply);
468 EXPECT_TRUE(replyi.isValid());
469 ret = replyi.readInt32(&id);
470 EXPECT_EQ(NO_ERROR, ret);
471 EXPECT_EQ(serverId[i], id);
472 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
473 }
474
475 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
476}
477
478TEST_F(BinderLibTest, IndirectGetId3)
479{
480 status_t ret;
481 int32_t id;
482 int32_t count;
483 Parcel data, reply;
484 int32_t serverId[3];
485
486 data.writeInt32(ARRAY_SIZE(serverId));
487 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
488 sp<IBinder> server;
489 BinderLibTestBundle datai;
490 BinderLibTestBundle datai2;
491
492 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700493 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800494 data.writeStrongBinder(server);
495 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
496
497 datai.writeInt32(1);
498 datai.writeStrongBinder(m_server);
499 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
500 datai2.appendTo(&datai);
501
502 datai.appendTo(&data);
503 }
504
505 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
506 ASSERT_EQ(NO_ERROR, ret);
507
508 ret = reply.readInt32(&id);
509 ASSERT_EQ(NO_ERROR, ret);
510 EXPECT_EQ(0, id);
511
512 ret = reply.readInt32(&count);
513 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700514 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800515
516 for (size_t i = 0; i < (size_t)count; i++) {
517 int32_t counti;
518
519 BinderLibTestBundle replyi(&reply);
520 EXPECT_TRUE(replyi.isValid());
521 ret = replyi.readInt32(&id);
522 EXPECT_EQ(NO_ERROR, ret);
523 EXPECT_EQ(serverId[i], id);
524
525 ret = replyi.readInt32(&counti);
526 ASSERT_EQ(NO_ERROR, ret);
527 EXPECT_EQ(1, counti);
528
529 BinderLibTestBundle replyi2(&replyi);
530 EXPECT_TRUE(replyi2.isValid());
531 ret = replyi2.readInt32(&id);
532 EXPECT_EQ(NO_ERROR, ret);
533 EXPECT_EQ(0, id);
534 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
535
536 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
537 }
538
539 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
540}
541
542TEST_F(BinderLibTest, CallBack)
543{
544 status_t ret;
545 Parcel data, reply;
546 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
547 data.writeStrongBinder(callBack);
548 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
549 EXPECT_EQ(NO_ERROR, ret);
550 ret = callBack->waitEvent(5);
551 EXPECT_EQ(NO_ERROR, ret);
552 ret = callBack->getResult();
553 EXPECT_EQ(NO_ERROR, ret);
554}
555
556TEST_F(BinderLibTest, AddServer)
557{
558 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700559 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800560}
561
Riley Andrews06b01ad2014-12-18 12:10:08 -0800562TEST_F(BinderLibTest, DeathNotificationStrongRef)
563{
564 status_t ret;
565 sp<IBinder> sbinder;
566
567 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
568
569 {
570 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700571 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800572 ret = binder->linkToDeath(testDeathRecipient);
573 EXPECT_EQ(NO_ERROR, ret);
574 sbinder = binder;
575 }
576 {
577 Parcel data, reply;
578 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
579 EXPECT_EQ(0, ret);
580 }
581 IPCThreadState::self()->flushCommands();
582 ret = testDeathRecipient->waitEvent(5);
583 EXPECT_EQ(NO_ERROR, ret);
584 ret = sbinder->unlinkToDeath(testDeathRecipient);
585 EXPECT_EQ(DEAD_OBJECT, ret);
586}
587
588TEST_F(BinderLibTest, DeathNotificationMultiple)
589{
590 status_t ret;
591 const int clientcount = 2;
592 sp<IBinder> target;
593 sp<IBinder> linkedclient[clientcount];
594 sp<BinderLibTestCallBack> callBack[clientcount];
595 sp<IBinder> passiveclient[clientcount];
596
597 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700598 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800599 for (int i = 0; i < clientcount; i++) {
600 {
601 Parcel data, reply;
602
603 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700604 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605 callBack[i] = new BinderLibTestCallBack();
606 data.writeStrongBinder(target);
607 data.writeStrongBinder(callBack[i]);
608 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
609 EXPECT_EQ(NO_ERROR, ret);
610 }
611 {
612 Parcel data, reply;
613
614 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700615 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800616 data.writeStrongBinder(target);
617 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
618 EXPECT_EQ(NO_ERROR, ret);
619 }
620 }
621 {
622 Parcel data, reply;
623 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
624 EXPECT_EQ(0, ret);
625 }
626
627 for (int i = 0; i < clientcount; i++) {
628 ret = callBack[i]->waitEvent(5);
629 EXPECT_EQ(NO_ERROR, ret);
630 ret = callBack[i]->getResult();
631 EXPECT_EQ(NO_ERROR, ret);
632 }
633}
634
Martijn Coenenf7100e42017-07-31 12:14:09 +0200635TEST_F(BinderLibTest, DeathNotificationThread)
636{
637 status_t ret;
638 sp<BinderLibTestCallBack> callback;
639 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700640 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200641 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700642 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200643
644 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
645
646 ret = target->linkToDeath(testDeathRecipient);
647 EXPECT_EQ(NO_ERROR, ret);
648
649 {
650 Parcel data, reply;
651 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
652 EXPECT_EQ(0, ret);
653 }
654
655 /* Make sure it's dead */
656 testDeathRecipient->waitEvent(5);
657
658 /* Now, pass the ref to another process and ask that process to
659 * call linkToDeath() on it, and wait for a response. This tests
660 * two things:
661 * 1) You still get death notifications when calling linkToDeath()
662 * on a ref that is already dead when it was passed to you.
663 * 2) That death notifications are not directly pushed to the thread
664 * registering them, but to the threadpool (proc workqueue) instead.
665 *
666 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
667 * is blocked on a condition variable waiting for the death notification to be
668 * called; therefore, that thread is not available for handling proc work.
669 * So, if the death notification was pushed to the thread workqueue, the callback
670 * would never be called, and the test would timeout and fail.
671 *
672 * Note that we can't do this part of the test from this thread itself, because
673 * the binder driver would only push death notifications to the thread if
674 * it is a looper thread, which this thread is not.
675 *
676 * See b/23525545 for details.
677 */
678 {
679 Parcel data, reply;
680
681 callback = new BinderLibTestCallBack();
682 data.writeStrongBinder(target);
683 data.writeStrongBinder(callback);
684 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
685 EXPECT_EQ(NO_ERROR, ret);
686 }
687
688 ret = callback->waitEvent(5);
689 EXPECT_EQ(NO_ERROR, ret);
690 ret = callback->getResult();
691 EXPECT_EQ(NO_ERROR, ret);
692}
693
Riley Andrews06b01ad2014-12-18 12:10:08 -0800694TEST_F(BinderLibTest, PassFile) {
695 int ret;
696 int pipefd[2];
697 uint8_t buf[1] = { 0 };
698 uint8_t write_value = 123;
699
700 ret = pipe2(pipefd, O_NONBLOCK);
701 ASSERT_EQ(0, ret);
702
703 {
704 Parcel data, reply;
705 uint8_t writebuf[1] = { write_value };
706
707 ret = data.writeFileDescriptor(pipefd[1], true);
708 EXPECT_EQ(NO_ERROR, ret);
709
710 ret = data.writeInt32(sizeof(writebuf));
711 EXPECT_EQ(NO_ERROR, ret);
712
713 ret = data.write(writebuf, sizeof(writebuf));
714 EXPECT_EQ(NO_ERROR, ret);
715
716 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
717 EXPECT_EQ(NO_ERROR, ret);
718 }
719
720 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700721 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800722 EXPECT_EQ(write_value, buf[0]);
723
724 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
725
726 ret = read(pipefd[0], buf, sizeof(buf));
727 EXPECT_EQ(0, ret);
728
729 close(pipefd[0]);
730}
731
Ryo Hashimotobf551892018-05-31 16:58:35 +0900732TEST_F(BinderLibTest, PassParcelFileDescriptor) {
733 const int datasize = 123;
734 std::vector<uint8_t> writebuf(datasize);
735 for (size_t i = 0; i < writebuf.size(); ++i) {
736 writebuf[i] = i;
737 }
738
739 android::base::unique_fd read_end, write_end;
740 {
741 int pipefd[2];
742 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
743 read_end.reset(pipefd[0]);
744 write_end.reset(pipefd[1]);
745 }
746 {
747 Parcel data;
748 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
749 write_end.reset();
750 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
751 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
752
753 Parcel reply;
754 EXPECT_EQ(NO_ERROR,
755 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
756 &reply));
757 }
758 std::vector<uint8_t> readbuf(datasize);
759 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
760 EXPECT_EQ(writebuf, readbuf);
761
762 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
763
764 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
765}
766
Riley Andrews06b01ad2014-12-18 12:10:08 -0800767TEST_F(BinderLibTest, PromoteLocal) {
768 sp<IBinder> strong = new BBinder();
769 wp<IBinder> weak = strong;
770 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700771 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700773 strong = nullptr;
774 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800775 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700776 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800777}
778
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700779TEST_F(BinderLibTest, LocalGetExtension) {
780 sp<BBinder> binder = new BBinder();
781 sp<IBinder> ext = new BBinder();
782 binder->setExtension(ext);
783 EXPECT_EQ(ext, binder->getExtension());
784}
785
786TEST_F(BinderLibTest, RemoteGetExtension) {
787 sp<IBinder> server = addServer();
788 ASSERT_TRUE(server != nullptr);
789
790 sp<IBinder> extension;
791 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
792 ASSERT_NE(nullptr, extension.get());
793
794 EXPECT_EQ(NO_ERROR, extension->pingBinder());
795}
796
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700797TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
798 status_t ret;
799 Parcel data, reply;
800
801 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
802 EXPECT_EQ(NO_ERROR, ret);
803
804 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700805 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800806 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
807 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
808 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
809 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700810}
811
Connor O'Brien52be2c92016-09-20 14:18:08 -0700812TEST_F(BinderLibTest, FreedBinder) {
813 status_t ret;
814
815 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700816 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700817
818 __u32 freedHandle;
819 wp<IBinder> keepFreedBinder;
820 {
821 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700822 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
823 ASSERT_EQ(NO_ERROR, ret);
824 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
825 freedHandle = freed->handle;
826 /* Add a weak ref to the freed binder so the driver does not
827 * delete its reference to it - otherwise the transaction
828 * fails regardless of whether the driver is fixed.
829 */
Steven Morelande171d622019-07-17 16:06:01 -0700830 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700831 }
Steven Morelande171d622019-07-17 16:06:01 -0700832 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700833 {
834 Parcel data, reply;
835 data.writeStrongBinder(server);
836 /* Replace original handle with handle to the freed binder */
837 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
838 __u32 oldHandle = strong->handle;
839 strong->handle = freedHandle;
840 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
841 /* Returns DEAD_OBJECT (-32) if target crashes and
842 * FAILED_TRANSACTION if the driver rejects the invalid
843 * object.
844 */
845 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
846 /* Restore original handle so parcel destructor does not use
847 * the wrong handle.
848 */
849 strong->handle = oldHandle;
850 }
851}
852
Sherry Yang336cdd32017-07-24 14:12:27 -0700853TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
854 status_t ret;
855 Parcel data, reply;
856 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
857 for (int i = 0; i < 2; i++) {
858 BinderLibTestBundle datai;
859 datai.appendFrom(&data, 0, data.dataSize());
860
861 data.freeData();
862 data.writeInt32(1);
863 data.writeStrongBinder(callBack);
864 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
865
866 datai.appendTo(&data);
867 }
868 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
869 EXPECT_EQ(NO_ERROR, ret);
870}
871
Martijn Coenen45b07b42017-08-09 12:07:45 +0200872TEST_F(BinderLibTest, OnewayQueueing)
873{
874 status_t ret;
875 Parcel data, data2;
876
877 sp<IBinder> pollServer = addPollServer();
878
879 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
880 data.writeStrongBinder(callBack);
881 data.writeInt32(500000); // delay in us before calling back
882
883 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
884 data2.writeStrongBinder(callBack2);
885 data2.writeInt32(0); // delay in us
886
Yi Kong91635562018-06-07 14:38:36 -0700887 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200888 EXPECT_EQ(NO_ERROR, ret);
889
890 // The delay ensures that this second transaction will end up on the async_todo list
891 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700892 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200893 EXPECT_EQ(NO_ERROR, ret);
894
895 // The server will ensure that the two transactions are handled in the expected order;
896 // If the ordering is not as expected, an error will be returned through the callbacks.
897 ret = callBack->waitEvent(2);
898 EXPECT_EQ(NO_ERROR, ret);
899 ret = callBack->getResult();
900 EXPECT_EQ(NO_ERROR, ret);
901
902 ret = callBack2->waitEvent(2);
903 EXPECT_EQ(NO_ERROR, ret);
904 ret = callBack2->getResult();
905 EXPECT_EQ(NO_ERROR, ret);
906}
907
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100908TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
909{
910 status_t ret;
911 Parcel data, reply;
912 data.writeInterfaceToken(binderLibTestServiceName);
913 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
914 EXPECT_EQ(-1, reply.readInt32());
915 EXPECT_EQ(NO_ERROR, ret);
916}
917
918TEST_F(BinderLibTest, WorkSourceSet)
919{
920 status_t ret;
921 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000922 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000923 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100924 data.writeInterfaceToken(binderLibTestServiceName);
925 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
926 EXPECT_EQ(100, reply.readInt32());
927 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000928 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
929 EXPECT_EQ(NO_ERROR, ret);
930}
931
932TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
933{
934 status_t ret;
935 Parcel data, reply;
936
937 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
938 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
939
940 data.writeInterfaceToken(binderLibTestServiceName);
941 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
942 EXPECT_EQ(-1, reply.readInt32());
943 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100944 EXPECT_EQ(NO_ERROR, ret);
945}
946
947TEST_F(BinderLibTest, WorkSourceCleared)
948{
949 status_t ret;
950 Parcel data, reply;
951
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000952 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000953 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
954 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100955 data.writeInterfaceToken(binderLibTestServiceName);
956 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
957
958 EXPECT_EQ(-1, reply.readInt32());
959 EXPECT_EQ(100, previousWorkSource);
960 EXPECT_EQ(NO_ERROR, ret);
961}
962
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000963TEST_F(BinderLibTest, WorkSourceRestored)
964{
965 status_t ret;
966 Parcel data, reply;
967
968 IPCThreadState::self()->setCallingWorkSourceUid(100);
969 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
970 IPCThreadState::self()->restoreCallingWorkSource(token);
971
972 data.writeInterfaceToken(binderLibTestServiceName);
973 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
974
975 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000976 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000977 EXPECT_EQ(NO_ERROR, ret);
978}
979
Olivier Gaillard91a04802018-11-14 17:32:41 +0000980TEST_F(BinderLibTest, PropagateFlagSet)
981{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000982 IPCThreadState::self()->clearPropagateWorkSource();
983 IPCThreadState::self()->setCallingWorkSourceUid(100);
984 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
985}
986
987TEST_F(BinderLibTest, PropagateFlagCleared)
988{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000989 IPCThreadState::self()->setCallingWorkSourceUid(100);
990 IPCThreadState::self()->clearPropagateWorkSource();
991 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
992}
993
994TEST_F(BinderLibTest, PropagateFlagRestored)
995{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000996 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
997 IPCThreadState::self()->restoreCallingWorkSource(token);
998
999 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1000}
1001
1002TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1003{
1004 IPCThreadState::self()->setCallingWorkSourceUid(100);
1005
1006 Parcel data, reply;
1007 status_t ret;
1008 data.writeInterfaceToken(binderLibTestServiceName);
1009 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1010
1011 Parcel data2, reply2;
1012 status_t ret2;
1013 data2.writeInterfaceToken(binderLibTestServiceName);
1014 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1015 EXPECT_EQ(100, reply2.readInt32());
1016 EXPECT_EQ(NO_ERROR, ret2);
1017}
1018
Steven Morelandbf1915b2020-07-16 22:43:02 +00001019TEST_F(BinderLibTest, SchedPolicySet) {
1020 sp<IBinder> server = addServer();
1021 ASSERT_TRUE(server != nullptr);
1022
1023 Parcel data, reply;
1024 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1025 EXPECT_EQ(NO_ERROR, ret);
1026
1027 int policy = reply.readInt32();
1028 int priority = reply.readInt32();
1029
1030 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1031 EXPECT_EQ(kSchedPriority, priority);
1032}
1033
1034
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001035TEST_F(BinderLibTest, VectorSent) {
1036 Parcel data, reply;
1037 sp<IBinder> server = addServer();
1038 ASSERT_TRUE(server != nullptr);
1039
1040 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1041 data.writeUint64Vector(testValue);
1042
1043 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1044 EXPECT_EQ(NO_ERROR, ret);
1045 std::vector<uint64_t> readValue;
1046 ret = reply.readUint64Vector(&readValue);
1047 EXPECT_EQ(readValue, testValue);
1048}
1049
Riley Andrews06b01ad2014-12-18 12:10:08 -08001050class BinderLibTestService : public BBinder
1051{
1052 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001053 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001054 : m_id(id)
1055 , m_nextServerId(id + 1)
1056 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001057 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001058 {
Yi Kong91635562018-06-07 14:38:36 -07001059 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1060 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001061 }
1062 ~BinderLibTestService()
1063 {
1064 exit(EXIT_SUCCESS);
1065 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001066
1067 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001068 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001069 Parcel data;
1070 data.writeInt32(NO_ERROR);
1071 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001072 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001073 }
1074 }
1075
Riley Andrews06b01ad2014-12-18 12:10:08 -08001076 virtual status_t onTransact(uint32_t code,
1077 const Parcel& data, Parcel* reply,
1078 uint32_t flags = 0) {
1079 //printf("%s: code %d\n", __func__, code);
1080 (void)flags;
1081
1082 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1083 return PERMISSION_DENIED;
1084 }
1085 switch (code) {
1086 case BINDER_LIB_TEST_REGISTER_SERVER: {
1087 int32_t id;
1088 sp<IBinder> binder;
1089 id = data.readInt32();
1090 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001091 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001092 return BAD_VALUE;
1093 }
1094
1095 if (m_id != 0)
1096 return INVALID_OPERATION;
1097
1098 pthread_mutex_lock(&m_serverWaitMutex);
1099 if (m_serverStartRequested) {
1100 m_serverStartRequested = false;
1101 m_serverStarted = binder;
1102 pthread_cond_signal(&m_serverWaitCond);
1103 }
1104 pthread_mutex_unlock(&m_serverWaitMutex);
1105 return NO_ERROR;
1106 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001107 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001108 case BINDER_LIB_TEST_ADD_SERVER: {
1109 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001110 int serverid;
1111
1112 if (m_id != 0) {
1113 return INVALID_OPERATION;
1114 }
1115 pthread_mutex_lock(&m_serverWaitMutex);
1116 if (m_serverStartRequested) {
1117 ret = -EBUSY;
1118 } else {
1119 serverid = m_nextServerId++;
1120 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001121 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001122
1123 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001124 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001125 pthread_mutex_lock(&m_serverWaitMutex);
1126 }
1127 if (ret > 0) {
1128 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001129 struct timespec ts;
1130 clock_gettime(CLOCK_REALTIME, &ts);
1131 ts.tv_sec += 5;
1132 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001133 }
1134 if (m_serverStartRequested) {
1135 m_serverStartRequested = false;
1136 ret = -ETIMEDOUT;
1137 } else {
1138 reply->writeStrongBinder(m_serverStarted);
1139 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001140 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001141 ret = NO_ERROR;
1142 }
1143 } else if (ret >= 0) {
1144 m_serverStartRequested = false;
1145 ret = UNKNOWN_ERROR;
1146 }
1147 pthread_mutex_unlock(&m_serverWaitMutex);
1148 return ret;
1149 }
1150 case BINDER_LIB_TEST_NOP_TRANSACTION:
1151 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001152 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1153 // Note: this transaction is only designed for use with a
1154 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001155 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001156 // A callback was already pending; this means that
1157 // we received a second call while still processing
1158 // the first one. Fail the test.
1159 sp<IBinder> callback = data.readStrongBinder();
1160 Parcel data2;
1161 data2.writeInt32(UNKNOWN_ERROR);
1162
Yi Kong91635562018-06-07 14:38:36 -07001163 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001164 } else {
1165 m_callback = data.readStrongBinder();
1166 int32_t delayUs = data.readInt32();
1167 /*
1168 * It's necessary that we sleep here, so the next
1169 * transaction the caller makes will be queued to
1170 * the async queue.
1171 */
1172 usleep(delayUs);
1173
1174 /*
1175 * Now when we return, libbinder will tell the kernel
1176 * we are done with this transaction, and the kernel
1177 * can move the queued transaction to either the
1178 * thread todo worklist (for kernels without the fix),
1179 * or the proc todo worklist. In case of the former,
1180 * the next outbound call will pick up the pending
1181 * transaction, which leads to undesired reentrant
1182 * behavior. This is caught in the if() branch above.
1183 */
1184 }
1185
1186 return NO_ERROR;
1187 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001188 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1189 Parcel data2, reply2;
1190 sp<IBinder> binder;
1191 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001192 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001193 return BAD_VALUE;
1194 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001195 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001196 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1197 return NO_ERROR;
1198 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001199 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1200 reply->writeStrongBinder(this);
1201 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1203 reply->writeInt32(m_id);
1204 return NO_ERROR;
1205 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1206 int32_t count;
1207 uint32_t indirect_code;
1208 sp<IBinder> binder;
1209
1210 count = data.readInt32();
1211 reply->writeInt32(m_id);
1212 reply->writeInt32(count);
1213 for (int i = 0; i < count; i++) {
1214 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001215 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 return BAD_VALUE;
1217 }
1218 indirect_code = data.readInt32();
1219 BinderLibTestBundle data2(&data);
1220 if (!data2.isValid()) {
1221 return BAD_VALUE;
1222 }
1223 BinderLibTestBundle reply2;
1224 binder->transact(indirect_code, data2, &reply2);
1225 reply2.appendTo(reply);
1226 }
1227 return NO_ERROR;
1228 }
1229 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1230 reply->setError(data.readInt32());
1231 return NO_ERROR;
1232 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1233 reply->writeInt32(sizeof(void *));
1234 return NO_ERROR;
1235 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1236 return NO_ERROR;
1237 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1238 m_strongRef = data.readStrongBinder();
1239 return NO_ERROR;
1240 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1241 int ret;
1242 Parcel data2, reply2;
1243 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1244 sp<IBinder> target;
1245 sp<IBinder> callback;
1246
1247 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001248 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001249 return BAD_VALUE;
1250 }
1251 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001252 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001253 return BAD_VALUE;
1254 }
1255 ret = target->linkToDeath(testDeathRecipient);
1256 if (ret == NO_ERROR)
1257 ret = testDeathRecipient->waitEvent(5);
1258 data2.writeInt32(ret);
1259 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1260 return NO_ERROR;
1261 }
1262 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1263 int ret;
1264 int32_t size;
1265 const void *buf;
1266 int fd;
1267
1268 fd = data.readFileDescriptor();
1269 if (fd < 0) {
1270 return BAD_VALUE;
1271 }
1272 ret = data.readInt32(&size);
1273 if (ret != NO_ERROR) {
1274 return ret;
1275 }
1276 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001277 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001278 return BAD_VALUE;
1279 }
1280 ret = write(fd, buf, size);
1281 if (ret != size)
1282 return UNKNOWN_ERROR;
1283 return NO_ERROR;
1284 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001285 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1286 int ret;
1287 int32_t size;
1288 const void *buf;
1289 android::base::unique_fd fd;
1290
1291 ret = data.readUniqueParcelFileDescriptor(&fd);
1292 if (ret != NO_ERROR) {
1293 return ret;
1294 }
1295 ret = data.readInt32(&size);
1296 if (ret != NO_ERROR) {
1297 return ret;
1298 }
1299 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001300 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001301 return BAD_VALUE;
1302 }
1303 ret = write(fd.get(), buf, size);
1304 if (ret != size) return UNKNOWN_ERROR;
1305 return NO_ERROR;
1306 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1308 alarm(10);
1309 return NO_ERROR;
1310 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001311 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001312 ;
1313 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001314 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001315 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001316 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001317 return NO_ERROR;
1318 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001319 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1320 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001321 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001322 return NO_ERROR;
1323 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001324 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1325 int policy = 0;
1326 sched_param param;
1327 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1328 return UNKNOWN_ERROR;
1329 }
1330 reply->writeInt32(policy);
1331 reply->writeInt32(param.sched_priority);
1332 return NO_ERROR;
1333 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001334 case BINDER_LIB_TEST_ECHO_VECTOR: {
1335 std::vector<uint64_t> vector;
1336 auto err = data.readUint64Vector(&vector);
1337 if (err != NO_ERROR)
1338 return err;
1339 reply->writeUint64Vector(vector);
1340 return NO_ERROR;
1341 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001342 default:
1343 return UNKNOWN_TRANSACTION;
1344 };
1345 }
1346 private:
1347 int32_t m_id;
1348 int32_t m_nextServerId;
1349 pthread_mutex_t m_serverWaitMutex;
1350 pthread_cond_t m_serverWaitCond;
1351 bool m_serverStartRequested;
1352 sp<IBinder> m_serverStarted;
1353 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001354 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001355};
1356
Martijn Coenen45b07b42017-08-09 12:07:45 +02001357int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001359 binderLibTestServiceName += String16(binderserversuffix);
1360
Riley Andrews06b01ad2014-12-18 12:10:08 -08001361 status_t ret;
1362 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001363 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001364 {
1365 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001366
Steven Morelandbf1915b2020-07-16 22:43:02 +00001367 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1368
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001369 /*
1370 * Normally would also contain functionality as well, but we are only
1371 * testing the extension mechanism.
1372 */
1373 testService->setExtension(new BBinder());
1374
Martijn Coenen45b07b42017-08-09 12:07:45 +02001375 /*
1376 * We need this below, but can't hold a sp<> because it prevents the
1377 * node from being cleaned up automatically. It's safe in this case
1378 * because of how the tests are written.
1379 */
1380 testServicePtr = testService.get();
1381
Riley Andrews06b01ad2014-12-18 12:10:08 -08001382 if (index == 0) {
1383 ret = sm->addService(binderLibTestServiceName, testService);
1384 } else {
1385 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1386 Parcel data, reply;
1387 data.writeInt32(index);
1388 data.writeStrongBinder(testService);
1389
1390 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1391 }
1392 }
1393 write(readypipefd, &ret, sizeof(ret));
1394 close(readypipefd);
1395 //printf("%s: ret %d\n", __func__, ret);
1396 if (ret)
1397 return 1;
1398 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001399 if (usePoll) {
1400 int fd;
1401 struct epoll_event ev;
1402 int epoll_fd;
1403 IPCThreadState::self()->setupPolling(&fd);
1404 if (fd < 0) {
1405 return 1;
1406 }
1407 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1408
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001409 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001410 if (epoll_fd == -1) {
1411 return 1;
1412 }
1413
1414 ev.events = EPOLLIN;
1415 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1416 return 1;
1417 }
1418
1419 while (1) {
1420 /*
1421 * We simulate a single-threaded process using the binder poll
1422 * interface; besides handling binder commands, it can also
1423 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001424 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001425 *
1426 * processPendingCall() will then issue that transaction.
1427 */
1428 struct epoll_event events[1];
1429 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1430 if (numEvents < 0) {
1431 if (errno == EINTR) {
1432 continue;
1433 }
1434 return 1;
1435 }
1436 if (numEvents > 0) {
1437 IPCThreadState::self()->handlePolledCommands();
1438 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1439 testServicePtr->processPendingCall();
1440 }
1441 }
1442 } else {
1443 ProcessState::self()->startThreadPool();
1444 IPCThreadState::self()->joinThreadPool();
1445 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001446 //printf("%s: joinThreadPool returned\n", __func__);
1447 return 1; /* joinThreadPool should not return */
1448}
1449
1450int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001451 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001452 binderservername = argv[2];
1453 } else {
1454 binderservername = argv[0];
1455 }
1456
Martijn Coenen45b07b42017-08-09 12:07:45 +02001457 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1458 binderserversuffix = argv[5];
1459 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001460 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001461 binderserversuffix = new char[16];
1462 snprintf(binderserversuffix, 16, "%d", getpid());
1463 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001464
1465 ::testing::InitGoogleTest(&argc, argv);
1466 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1467 ProcessState::self()->startThreadPool();
1468 return RUN_ALL_TESTS();
1469}