blob: e343df7e50c11c471cf925bc74647337d1dc1936 [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
51static String16 binderLibTestServiceName = String16("test.binderLib");
52
53enum BinderLibTestTranscationCode {
54 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
55 BINDER_LIB_TEST_REGISTER_SERVER,
56 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020057 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080058 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070059 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020060 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070062 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080063 BINDER_LIB_TEST_GET_ID_TRANSACTION,
64 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
65 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
66 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
67 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
68 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
69 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090070 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080071 BINDER_LIB_TEST_EXIT_TRANSACTION,
72 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
73 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070074 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010075 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080076 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020077 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080078};
79
Martijn Coenen45b07b42017-08-09 12:07:45 +020080pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080081{
82 int ret;
83 pid_t pid;
84 status_t status;
85 int pipefd[2];
86 char stri[16];
87 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020088 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080089 char *childargv[] = {
90 binderservername,
91 binderserverarg,
92 stri,
93 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020094 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070095 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070096 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080097 };
98
99 ret = pipe(pipefd);
100 if (ret < 0)
101 return ret;
102
103 snprintf(stri, sizeof(stri), "%d", arg2);
104 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200105 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800106
107 pid = fork();
108 if (pid == -1)
109 return pid;
110 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800111 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800112 close(pipefd[0]);
113 execv(binderservername, childargv);
114 status = -errno;
115 write(pipefd[1], &status, sizeof(status));
116 fprintf(stderr, "execv failed, %s\n", strerror(errno));
117 _exit(EXIT_FAILURE);
118 }
119 close(pipefd[1]);
120 ret = read(pipefd[0], &status, sizeof(status));
121 //printf("pipe read returned %d, status %d\n", ret, status);
122 close(pipefd[0]);
123 if (ret == sizeof(status)) {
124 ret = status;
125 } else {
126 kill(pid, SIGKILL);
127 if (ret >= 0) {
128 ret = NO_INIT;
129 }
130 }
131 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700132 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800133 return ret;
134 }
135 return pid;
136}
137
138class BinderLibTestEnv : public ::testing::Environment {
139 public:
140 BinderLibTestEnv() {}
141 sp<IBinder> getServer(void) {
142 return m_server;
143 }
144
145 private:
146 virtual void SetUp() {
147 m_serverpid = start_server_process(0);
148 //printf("m_serverpid %d\n", m_serverpid);
149 ASSERT_GT(m_serverpid, 0);
150
151 sp<IServiceManager> sm = defaultServiceManager();
152 //printf("%s: pid %d, get service\n", __func__, m_pid);
153 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700154 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800155 //printf("%s: pid %d, get service done\n", __func__, m_pid);
156 }
157 virtual void TearDown() {
158 status_t ret;
159 Parcel data, reply;
160 int exitStatus;
161 pid_t pid;
162
163 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700164 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800165 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
166 EXPECT_EQ(0, ret);
167 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
168 EXPECT_EQ(0, ret);
169 }
170 if (m_serverpid > 0) {
171 //printf("wait for %d\n", m_pids[i]);
172 pid = wait(&exitStatus);
173 EXPECT_EQ(m_serverpid, pid);
174 EXPECT_TRUE(WIFEXITED(exitStatus));
175 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
176 }
177 }
178
179 pid_t m_serverpid;
180 sp<IBinder> m_server;
181};
182
183class BinderLibTest : public ::testing::Test {
184 public:
185 virtual void SetUp() {
186 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000187 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800188 }
189 virtual void TearDown() {
190 }
191 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200192 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800193 {
194 int ret;
195 int32_t id;
196 Parcel data, reply;
197 sp<IBinder> binder;
198
Martijn Coenen45b07b42017-08-09 12:07:45 +0200199 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800200 EXPECT_EQ(NO_ERROR, ret);
201
Yi Kong91635562018-06-07 14:38:36 -0700202 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800203 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700204 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800205 ret = reply.readInt32(&id);
206 EXPECT_EQ(NO_ERROR, ret);
207 if (idPtr)
208 *idPtr = id;
209 return binder;
210 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200211
Yi Kong91635562018-06-07 14:38:36 -0700212 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200213 {
214 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
215 }
216
Yi Kong91635562018-06-07 14:38:36 -0700217 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200218 {
219 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
220 }
221
Riley Andrews06b01ad2014-12-18 12:10:08 -0800222 void waitForReadData(int fd, int timeout_ms) {
223 int ret;
224 pollfd pfd = pollfd();
225
226 pfd.fd = fd;
227 pfd.events = POLLIN;
228 ret = poll(&pfd, 1, timeout_ms);
229 EXPECT_EQ(1, ret);
230 }
231
232 sp<IBinder> m_server;
233};
234
235class BinderLibTestBundle : public Parcel
236{
237 public:
238 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800239 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800240 int32_t mark;
241 int32_t bundleLen;
242 size_t pos;
243
244 if (source->readInt32(&mark))
245 return;
246 if (mark != MARK_START)
247 return;
248 if (source->readInt32(&bundleLen))
249 return;
250 pos = source->dataPosition();
251 if (Parcel::appendFrom(source, pos, bundleLen))
252 return;
253 source->setDataPosition(pos + bundleLen);
254 if (source->readInt32(&mark))
255 return;
256 if (mark != MARK_END)
257 return;
258 m_isValid = true;
259 setDataPosition(0);
260 }
261 void appendTo(Parcel *dest) {
262 dest->writeInt32(MARK_START);
263 dest->writeInt32(dataSize());
264 dest->appendFrom(this, 0, dataSize());
265 dest->writeInt32(MARK_END);
266 };
267 bool isValid(void) {
268 return m_isValid;
269 }
270 private:
271 enum {
272 MARK_START = B_PACK_CHARS('B','T','B','S'),
273 MARK_END = B_PACK_CHARS('B','T','B','E'),
274 };
275 bool m_isValid;
276};
277
278class BinderLibTestEvent
279{
280 public:
281 BinderLibTestEvent(void)
282 : m_eventTriggered(false)
283 {
Yi Kong91635562018-06-07 14:38:36 -0700284 pthread_mutex_init(&m_waitMutex, nullptr);
285 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800286 }
287 int waitEvent(int timeout_s)
288 {
289 int ret;
290 pthread_mutex_lock(&m_waitMutex);
291 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800292 struct timespec ts;
293 clock_gettime(CLOCK_REALTIME, &ts);
294 ts.tv_sec += timeout_s;
295 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800296 }
297 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
298 pthread_mutex_unlock(&m_waitMutex);
299 return ret;
300 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200301 pthread_t getTriggeringThread()
302 {
303 return m_triggeringThread;
304 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 protected:
306 void triggerEvent(void) {
307 pthread_mutex_lock(&m_waitMutex);
308 pthread_cond_signal(&m_waitCond);
309 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800311 pthread_mutex_unlock(&m_waitMutex);
312 };
313 private:
314 pthread_mutex_t m_waitMutex;
315 pthread_cond_t m_waitCond;
316 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200317 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800318};
319
320class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
321{
322 public:
323 BinderLibTestCallBack()
324 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700325 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800326 {
327 }
328 status_t getResult(void)
329 {
330 return m_result;
331 }
332
333 private:
334 virtual status_t onTransact(uint32_t code,
335 const Parcel& data, Parcel* reply,
336 uint32_t flags = 0)
337 {
338 (void)reply;
339 (void)flags;
340 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200341 case BINDER_LIB_TEST_CALL_BACK: {
342 status_t status = data.readInt32(&m_result);
343 if (status != NO_ERROR) {
344 m_result = status;
345 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800346 triggerEvent();
347 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200348 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700349 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
350 sp<IBinder> server;
351 int ret;
352 const uint8_t *buf = data.data();
353 size_t size = data.dataSize();
354 if (m_prev_end) {
355 /* 64-bit kernel needs at most 8 bytes to align buffer end */
356 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
357 } else {
358 EXPECT_TRUE(IsPageAligned((void *)buf));
359 }
360
361 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
362
363 if (size > 0) {
364 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
365 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
366 data, reply);
367 EXPECT_EQ(NO_ERROR, ret);
368 }
369 return NO_ERROR;
370 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800371 default:
372 return UNKNOWN_TRANSACTION;
373 }
374 }
375
376 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700377 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800378};
379
380class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
381{
382 private:
383 virtual void binderDied(const wp<IBinder>& who) {
384 (void)who;
385 triggerEvent();
386 };
387};
388
389TEST_F(BinderLibTest, NopTransaction) {
390 status_t ret;
391 Parcel data, reply;
392 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
393 EXPECT_EQ(NO_ERROR, ret);
394}
395
396TEST_F(BinderLibTest, SetError) {
397 int32_t testValue[] = { 0, -123, 123 };
398 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
399 status_t ret;
400 Parcel data, reply;
401 data.writeInt32(testValue[i]);
402 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
403 EXPECT_EQ(testValue[i], ret);
404 }
405}
406
407TEST_F(BinderLibTest, GetId) {
408 status_t ret;
409 int32_t id;
410 Parcel data, reply;
411 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
412 EXPECT_EQ(NO_ERROR, ret);
413 ret = reply.readInt32(&id);
414 EXPECT_EQ(NO_ERROR, ret);
415 EXPECT_EQ(0, id);
416}
417
418TEST_F(BinderLibTest, PtrSize) {
419 status_t ret;
420 int32_t ptrsize;
421 Parcel data, reply;
422 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700423 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800424 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
425 EXPECT_EQ(NO_ERROR, ret);
426 ret = reply.readInt32(&ptrsize);
427 EXPECT_EQ(NO_ERROR, ret);
428 RecordProperty("TestPtrSize", sizeof(void *));
429 RecordProperty("ServerPtrSize", sizeof(void *));
430}
431
432TEST_F(BinderLibTest, IndirectGetId2)
433{
434 status_t ret;
435 int32_t id;
436 int32_t count;
437 Parcel data, reply;
438 int32_t serverId[3];
439
440 data.writeInt32(ARRAY_SIZE(serverId));
441 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
442 sp<IBinder> server;
443 BinderLibTestBundle datai;
444
445 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700446 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800447 data.writeStrongBinder(server);
448 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
449 datai.appendTo(&data);
450 }
451
452 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
453 ASSERT_EQ(NO_ERROR, ret);
454
455 ret = reply.readInt32(&id);
456 ASSERT_EQ(NO_ERROR, ret);
457 EXPECT_EQ(0, id);
458
459 ret = reply.readInt32(&count);
460 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700461 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800462
463 for (size_t i = 0; i < (size_t)count; i++) {
464 BinderLibTestBundle replyi(&reply);
465 EXPECT_TRUE(replyi.isValid());
466 ret = replyi.readInt32(&id);
467 EXPECT_EQ(NO_ERROR, ret);
468 EXPECT_EQ(serverId[i], id);
469 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
470 }
471
472 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
473}
474
475TEST_F(BinderLibTest, IndirectGetId3)
476{
477 status_t ret;
478 int32_t id;
479 int32_t count;
480 Parcel data, reply;
481 int32_t serverId[3];
482
483 data.writeInt32(ARRAY_SIZE(serverId));
484 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
485 sp<IBinder> server;
486 BinderLibTestBundle datai;
487 BinderLibTestBundle datai2;
488
489 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700490 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800491 data.writeStrongBinder(server);
492 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
493
494 datai.writeInt32(1);
495 datai.writeStrongBinder(m_server);
496 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
497 datai2.appendTo(&datai);
498
499 datai.appendTo(&data);
500 }
501
502 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
503 ASSERT_EQ(NO_ERROR, ret);
504
505 ret = reply.readInt32(&id);
506 ASSERT_EQ(NO_ERROR, ret);
507 EXPECT_EQ(0, id);
508
509 ret = reply.readInt32(&count);
510 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700511 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800512
513 for (size_t i = 0; i < (size_t)count; i++) {
514 int32_t counti;
515
516 BinderLibTestBundle replyi(&reply);
517 EXPECT_TRUE(replyi.isValid());
518 ret = replyi.readInt32(&id);
519 EXPECT_EQ(NO_ERROR, ret);
520 EXPECT_EQ(serverId[i], id);
521
522 ret = replyi.readInt32(&counti);
523 ASSERT_EQ(NO_ERROR, ret);
524 EXPECT_EQ(1, counti);
525
526 BinderLibTestBundle replyi2(&replyi);
527 EXPECT_TRUE(replyi2.isValid());
528 ret = replyi2.readInt32(&id);
529 EXPECT_EQ(NO_ERROR, ret);
530 EXPECT_EQ(0, id);
531 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
532
533 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
534 }
535
536 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
537}
538
539TEST_F(BinderLibTest, CallBack)
540{
541 status_t ret;
542 Parcel data, reply;
543 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
544 data.writeStrongBinder(callBack);
545 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
546 EXPECT_EQ(NO_ERROR, ret);
547 ret = callBack->waitEvent(5);
548 EXPECT_EQ(NO_ERROR, ret);
549 ret = callBack->getResult();
550 EXPECT_EQ(NO_ERROR, ret);
551}
552
553TEST_F(BinderLibTest, AddServer)
554{
555 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700556 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800557}
558
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559TEST_F(BinderLibTest, DeathNotificationStrongRef)
560{
561 status_t ret;
562 sp<IBinder> sbinder;
563
564 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
565
566 {
567 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700568 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800569 ret = binder->linkToDeath(testDeathRecipient);
570 EXPECT_EQ(NO_ERROR, ret);
571 sbinder = binder;
572 }
573 {
574 Parcel data, reply;
575 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
576 EXPECT_EQ(0, ret);
577 }
578 IPCThreadState::self()->flushCommands();
579 ret = testDeathRecipient->waitEvent(5);
580 EXPECT_EQ(NO_ERROR, ret);
581 ret = sbinder->unlinkToDeath(testDeathRecipient);
582 EXPECT_EQ(DEAD_OBJECT, ret);
583}
584
585TEST_F(BinderLibTest, DeathNotificationMultiple)
586{
587 status_t ret;
588 const int clientcount = 2;
589 sp<IBinder> target;
590 sp<IBinder> linkedclient[clientcount];
591 sp<BinderLibTestCallBack> callBack[clientcount];
592 sp<IBinder> passiveclient[clientcount];
593
594 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700595 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800596 for (int i = 0; i < clientcount; i++) {
597 {
598 Parcel data, reply;
599
600 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700601 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800602 callBack[i] = new BinderLibTestCallBack();
603 data.writeStrongBinder(target);
604 data.writeStrongBinder(callBack[i]);
605 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
606 EXPECT_EQ(NO_ERROR, ret);
607 }
608 {
609 Parcel data, reply;
610
611 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700612 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800613 data.writeStrongBinder(target);
614 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
615 EXPECT_EQ(NO_ERROR, ret);
616 }
617 }
618 {
619 Parcel data, reply;
620 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
621 EXPECT_EQ(0, ret);
622 }
623
624 for (int i = 0; i < clientcount; i++) {
625 ret = callBack[i]->waitEvent(5);
626 EXPECT_EQ(NO_ERROR, ret);
627 ret = callBack[i]->getResult();
628 EXPECT_EQ(NO_ERROR, ret);
629 }
630}
631
Martijn Coenenf7100e42017-07-31 12:14:09 +0200632TEST_F(BinderLibTest, DeathNotificationThread)
633{
634 status_t ret;
635 sp<BinderLibTestCallBack> callback;
636 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700637 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200638 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700639 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200640
641 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
642
643 ret = target->linkToDeath(testDeathRecipient);
644 EXPECT_EQ(NO_ERROR, ret);
645
646 {
647 Parcel data, reply;
648 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
649 EXPECT_EQ(0, ret);
650 }
651
652 /* Make sure it's dead */
653 testDeathRecipient->waitEvent(5);
654
655 /* Now, pass the ref to another process and ask that process to
656 * call linkToDeath() on it, and wait for a response. This tests
657 * two things:
658 * 1) You still get death notifications when calling linkToDeath()
659 * on a ref that is already dead when it was passed to you.
660 * 2) That death notifications are not directly pushed to the thread
661 * registering them, but to the threadpool (proc workqueue) instead.
662 *
663 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
664 * is blocked on a condition variable waiting for the death notification to be
665 * called; therefore, that thread is not available for handling proc work.
666 * So, if the death notification was pushed to the thread workqueue, the callback
667 * would never be called, and the test would timeout and fail.
668 *
669 * Note that we can't do this part of the test from this thread itself, because
670 * the binder driver would only push death notifications to the thread if
671 * it is a looper thread, which this thread is not.
672 *
673 * See b/23525545 for details.
674 */
675 {
676 Parcel data, reply;
677
678 callback = new BinderLibTestCallBack();
679 data.writeStrongBinder(target);
680 data.writeStrongBinder(callback);
681 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
682 EXPECT_EQ(NO_ERROR, ret);
683 }
684
685 ret = callback->waitEvent(5);
686 EXPECT_EQ(NO_ERROR, ret);
687 ret = callback->getResult();
688 EXPECT_EQ(NO_ERROR, ret);
689}
690
Riley Andrews06b01ad2014-12-18 12:10:08 -0800691TEST_F(BinderLibTest, PassFile) {
692 int ret;
693 int pipefd[2];
694 uint8_t buf[1] = { 0 };
695 uint8_t write_value = 123;
696
697 ret = pipe2(pipefd, O_NONBLOCK);
698 ASSERT_EQ(0, ret);
699
700 {
701 Parcel data, reply;
702 uint8_t writebuf[1] = { write_value };
703
704 ret = data.writeFileDescriptor(pipefd[1], true);
705 EXPECT_EQ(NO_ERROR, ret);
706
707 ret = data.writeInt32(sizeof(writebuf));
708 EXPECT_EQ(NO_ERROR, ret);
709
710 ret = data.write(writebuf, sizeof(writebuf));
711 EXPECT_EQ(NO_ERROR, ret);
712
713 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
714 EXPECT_EQ(NO_ERROR, ret);
715 }
716
717 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700718 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800719 EXPECT_EQ(write_value, buf[0]);
720
721 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
722
723 ret = read(pipefd[0], buf, sizeof(buf));
724 EXPECT_EQ(0, ret);
725
726 close(pipefd[0]);
727}
728
Ryo Hashimotobf551892018-05-31 16:58:35 +0900729TEST_F(BinderLibTest, PassParcelFileDescriptor) {
730 const int datasize = 123;
731 std::vector<uint8_t> writebuf(datasize);
732 for (size_t i = 0; i < writebuf.size(); ++i) {
733 writebuf[i] = i;
734 }
735
736 android::base::unique_fd read_end, write_end;
737 {
738 int pipefd[2];
739 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
740 read_end.reset(pipefd[0]);
741 write_end.reset(pipefd[1]);
742 }
743 {
744 Parcel data;
745 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
746 write_end.reset();
747 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
748 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
749
750 Parcel reply;
751 EXPECT_EQ(NO_ERROR,
752 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
753 &reply));
754 }
755 std::vector<uint8_t> readbuf(datasize);
756 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
757 EXPECT_EQ(writebuf, readbuf);
758
759 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
760
761 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
762}
763
Riley Andrews06b01ad2014-12-18 12:10:08 -0800764TEST_F(BinderLibTest, PromoteLocal) {
765 sp<IBinder> strong = new BBinder();
766 wp<IBinder> weak = strong;
767 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700768 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800769 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700770 strong = nullptr;
771 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700773 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800774}
775
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700776TEST_F(BinderLibTest, LocalGetExtension) {
777 sp<BBinder> binder = new BBinder();
778 sp<IBinder> ext = new BBinder();
779 binder->setExtension(ext);
780 EXPECT_EQ(ext, binder->getExtension());
781}
782
783TEST_F(BinderLibTest, RemoteGetExtension) {
784 sp<IBinder> server = addServer();
785 ASSERT_TRUE(server != nullptr);
786
787 sp<IBinder> extension;
788 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
789 ASSERT_NE(nullptr, extension.get());
790
791 EXPECT_EQ(NO_ERROR, extension->pingBinder());
792}
793
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700794TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
795 status_t ret;
796 Parcel data, reply;
797
798 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
799 EXPECT_EQ(NO_ERROR, ret);
800
801 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700802 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800803 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
804 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
805 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
806 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700807}
808
Connor O'Brien52be2c92016-09-20 14:18:08 -0700809TEST_F(BinderLibTest, FreedBinder) {
810 status_t ret;
811
812 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700813 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700814
815 __u32 freedHandle;
816 wp<IBinder> keepFreedBinder;
817 {
818 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700819 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
820 ASSERT_EQ(NO_ERROR, ret);
821 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
822 freedHandle = freed->handle;
823 /* Add a weak ref to the freed binder so the driver does not
824 * delete its reference to it - otherwise the transaction
825 * fails regardless of whether the driver is fixed.
826 */
Steven Morelande171d622019-07-17 16:06:01 -0700827 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700828 }
Steven Morelande171d622019-07-17 16:06:01 -0700829 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700830 {
831 Parcel data, reply;
832 data.writeStrongBinder(server);
833 /* Replace original handle with handle to the freed binder */
834 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
835 __u32 oldHandle = strong->handle;
836 strong->handle = freedHandle;
837 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
838 /* Returns DEAD_OBJECT (-32) if target crashes and
839 * FAILED_TRANSACTION if the driver rejects the invalid
840 * object.
841 */
842 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
843 /* Restore original handle so parcel destructor does not use
844 * the wrong handle.
845 */
846 strong->handle = oldHandle;
847 }
848}
849
Sherry Yang336cdd32017-07-24 14:12:27 -0700850TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
851 status_t ret;
852 Parcel data, reply;
853 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
854 for (int i = 0; i < 2; i++) {
855 BinderLibTestBundle datai;
856 datai.appendFrom(&data, 0, data.dataSize());
857
858 data.freeData();
859 data.writeInt32(1);
860 data.writeStrongBinder(callBack);
861 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
862
863 datai.appendTo(&data);
864 }
865 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
866 EXPECT_EQ(NO_ERROR, ret);
867}
868
Martijn Coenen45b07b42017-08-09 12:07:45 +0200869TEST_F(BinderLibTest, OnewayQueueing)
870{
871 status_t ret;
872 Parcel data, data2;
873
874 sp<IBinder> pollServer = addPollServer();
875
876 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
877 data.writeStrongBinder(callBack);
878 data.writeInt32(500000); // delay in us before calling back
879
880 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
881 data2.writeStrongBinder(callBack2);
882 data2.writeInt32(0); // delay in us
883
Yi Kong91635562018-06-07 14:38:36 -0700884 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200885 EXPECT_EQ(NO_ERROR, ret);
886
887 // The delay ensures that this second transaction will end up on the async_todo list
888 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700889 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200890 EXPECT_EQ(NO_ERROR, ret);
891
892 // The server will ensure that the two transactions are handled in the expected order;
893 // If the ordering is not as expected, an error will be returned through the callbacks.
894 ret = callBack->waitEvent(2);
895 EXPECT_EQ(NO_ERROR, ret);
896 ret = callBack->getResult();
897 EXPECT_EQ(NO_ERROR, ret);
898
899 ret = callBack2->waitEvent(2);
900 EXPECT_EQ(NO_ERROR, ret);
901 ret = callBack2->getResult();
902 EXPECT_EQ(NO_ERROR, ret);
903}
904
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100905TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
906{
907 status_t ret;
908 Parcel data, reply;
909 data.writeInterfaceToken(binderLibTestServiceName);
910 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
911 EXPECT_EQ(-1, reply.readInt32());
912 EXPECT_EQ(NO_ERROR, ret);
913}
914
915TEST_F(BinderLibTest, WorkSourceSet)
916{
917 status_t ret;
918 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000919 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000920 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100921 data.writeInterfaceToken(binderLibTestServiceName);
922 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
923 EXPECT_EQ(100, reply.readInt32());
924 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000925 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
926 EXPECT_EQ(NO_ERROR, ret);
927}
928
929TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
930{
931 status_t ret;
932 Parcel data, reply;
933
934 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
935 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
936
937 data.writeInterfaceToken(binderLibTestServiceName);
938 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
939 EXPECT_EQ(-1, reply.readInt32());
940 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100941 EXPECT_EQ(NO_ERROR, ret);
942}
943
944TEST_F(BinderLibTest, WorkSourceCleared)
945{
946 status_t ret;
947 Parcel data, reply;
948
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000949 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000950 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
951 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100952 data.writeInterfaceToken(binderLibTestServiceName);
953 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
954
955 EXPECT_EQ(-1, reply.readInt32());
956 EXPECT_EQ(100, previousWorkSource);
957 EXPECT_EQ(NO_ERROR, ret);
958}
959
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000960TEST_F(BinderLibTest, WorkSourceRestored)
961{
962 status_t ret;
963 Parcel data, reply;
964
965 IPCThreadState::self()->setCallingWorkSourceUid(100);
966 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
967 IPCThreadState::self()->restoreCallingWorkSource(token);
968
969 data.writeInterfaceToken(binderLibTestServiceName);
970 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
971
972 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000973 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000974 EXPECT_EQ(NO_ERROR, ret);
975}
976
Olivier Gaillard91a04802018-11-14 17:32:41 +0000977TEST_F(BinderLibTest, PropagateFlagSet)
978{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000979 IPCThreadState::self()->clearPropagateWorkSource();
980 IPCThreadState::self()->setCallingWorkSourceUid(100);
981 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
982}
983
984TEST_F(BinderLibTest, PropagateFlagCleared)
985{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000986 IPCThreadState::self()->setCallingWorkSourceUid(100);
987 IPCThreadState::self()->clearPropagateWorkSource();
988 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
989}
990
991TEST_F(BinderLibTest, PropagateFlagRestored)
992{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000993 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
994 IPCThreadState::self()->restoreCallingWorkSource(token);
995
996 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
997}
998
999TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1000{
1001 IPCThreadState::self()->setCallingWorkSourceUid(100);
1002
1003 Parcel data, reply;
1004 status_t ret;
1005 data.writeInterfaceToken(binderLibTestServiceName);
1006 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1007
1008 Parcel data2, reply2;
1009 status_t ret2;
1010 data2.writeInterfaceToken(binderLibTestServiceName);
1011 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1012 EXPECT_EQ(100, reply2.readInt32());
1013 EXPECT_EQ(NO_ERROR, ret2);
1014}
1015
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001016TEST_F(BinderLibTest, VectorSent) {
1017 Parcel data, reply;
1018 sp<IBinder> server = addServer();
1019 ASSERT_TRUE(server != nullptr);
1020
1021 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1022 data.writeUint64Vector(testValue);
1023
1024 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1025 EXPECT_EQ(NO_ERROR, ret);
1026 std::vector<uint64_t> readValue;
1027 ret = reply.readUint64Vector(&readValue);
1028 EXPECT_EQ(readValue, testValue);
1029}
1030
Martijn Coenen82c75312019-07-24 15:18:30 +02001031TEST_F(BinderLibTest, BufRejected) {
1032 Parcel data, reply;
1033 uint32_t buf;
1034 sp<IBinder> server = addServer();
1035 ASSERT_TRUE(server != nullptr);
1036
1037 binder_buffer_object obj {
1038 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001039 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001040 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1041 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001042 };
1043 data.setDataCapacity(1024);
1044 // Write a bogus object at offset 0 to get an entry in the offset table
1045 data.writeFileDescriptor(0);
1046 EXPECT_EQ(data.objectsCount(), 1);
1047 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1048 // And now, overwrite it with the buffer object
1049 memcpy(parcelData, &obj, sizeof(obj));
1050 data.setDataSize(sizeof(obj));
1051
1052 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1053 // Either the kernel should reject this transaction (if it's correct), but
1054 // if it's not, the server implementation should return an error if it
1055 // finds an object in the received Parcel.
1056 EXPECT_NE(NO_ERROR, ret);
1057}
1058
Riley Andrews06b01ad2014-12-18 12:10:08 -08001059class BinderLibTestService : public BBinder
1060{
1061 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001062 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001063 : m_id(id)
1064 , m_nextServerId(id + 1)
1065 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001066 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001067 {
Yi Kong91635562018-06-07 14:38:36 -07001068 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1069 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001070 }
1071 ~BinderLibTestService()
1072 {
1073 exit(EXIT_SUCCESS);
1074 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001075
1076 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001077 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001078 Parcel data;
1079 data.writeInt32(NO_ERROR);
1080 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001081 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001082 }
1083 }
1084
Riley Andrews06b01ad2014-12-18 12:10:08 -08001085 virtual status_t onTransact(uint32_t code,
1086 const Parcel& data, Parcel* reply,
1087 uint32_t flags = 0) {
1088 //printf("%s: code %d\n", __func__, code);
1089 (void)flags;
1090
1091 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1092 return PERMISSION_DENIED;
1093 }
1094 switch (code) {
1095 case BINDER_LIB_TEST_REGISTER_SERVER: {
1096 int32_t id;
1097 sp<IBinder> binder;
1098 id = data.readInt32();
1099 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001100 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001101 return BAD_VALUE;
1102 }
1103
1104 if (m_id != 0)
1105 return INVALID_OPERATION;
1106
1107 pthread_mutex_lock(&m_serverWaitMutex);
1108 if (m_serverStartRequested) {
1109 m_serverStartRequested = false;
1110 m_serverStarted = binder;
1111 pthread_cond_signal(&m_serverWaitCond);
1112 }
1113 pthread_mutex_unlock(&m_serverWaitMutex);
1114 return NO_ERROR;
1115 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001116 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001117 case BINDER_LIB_TEST_ADD_SERVER: {
1118 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001119 int serverid;
1120
1121 if (m_id != 0) {
1122 return INVALID_OPERATION;
1123 }
1124 pthread_mutex_lock(&m_serverWaitMutex);
1125 if (m_serverStartRequested) {
1126 ret = -EBUSY;
1127 } else {
1128 serverid = m_nextServerId++;
1129 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001130 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001131
1132 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001133 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001134 pthread_mutex_lock(&m_serverWaitMutex);
1135 }
1136 if (ret > 0) {
1137 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001138 struct timespec ts;
1139 clock_gettime(CLOCK_REALTIME, &ts);
1140 ts.tv_sec += 5;
1141 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001142 }
1143 if (m_serverStartRequested) {
1144 m_serverStartRequested = false;
1145 ret = -ETIMEDOUT;
1146 } else {
1147 reply->writeStrongBinder(m_serverStarted);
1148 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001149 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001150 ret = NO_ERROR;
1151 }
1152 } else if (ret >= 0) {
1153 m_serverStartRequested = false;
1154 ret = UNKNOWN_ERROR;
1155 }
1156 pthread_mutex_unlock(&m_serverWaitMutex);
1157 return ret;
1158 }
1159 case BINDER_LIB_TEST_NOP_TRANSACTION:
1160 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001161 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1162 // Note: this transaction is only designed for use with a
1163 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001164 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001165 // A callback was already pending; this means that
1166 // we received a second call while still processing
1167 // the first one. Fail the test.
1168 sp<IBinder> callback = data.readStrongBinder();
1169 Parcel data2;
1170 data2.writeInt32(UNKNOWN_ERROR);
1171
Yi Kong91635562018-06-07 14:38:36 -07001172 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001173 } else {
1174 m_callback = data.readStrongBinder();
1175 int32_t delayUs = data.readInt32();
1176 /*
1177 * It's necessary that we sleep here, so the next
1178 * transaction the caller makes will be queued to
1179 * the async queue.
1180 */
1181 usleep(delayUs);
1182
1183 /*
1184 * Now when we return, libbinder will tell the kernel
1185 * we are done with this transaction, and the kernel
1186 * can move the queued transaction to either the
1187 * thread todo worklist (for kernels without the fix),
1188 * or the proc todo worklist. In case of the former,
1189 * the next outbound call will pick up the pending
1190 * transaction, which leads to undesired reentrant
1191 * behavior. This is caught in the if() branch above.
1192 */
1193 }
1194
1195 return NO_ERROR;
1196 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001197 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1198 Parcel data2, reply2;
1199 sp<IBinder> binder;
1200 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001201 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 return BAD_VALUE;
1203 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001204 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001205 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1206 return NO_ERROR;
1207 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001208 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1209 reply->writeStrongBinder(this);
1210 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001211 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1212 reply->writeInt32(m_id);
1213 return NO_ERROR;
1214 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1215 int32_t count;
1216 uint32_t indirect_code;
1217 sp<IBinder> binder;
1218
1219 count = data.readInt32();
1220 reply->writeInt32(m_id);
1221 reply->writeInt32(count);
1222 for (int i = 0; i < count; i++) {
1223 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001224 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001225 return BAD_VALUE;
1226 }
1227 indirect_code = data.readInt32();
1228 BinderLibTestBundle data2(&data);
1229 if (!data2.isValid()) {
1230 return BAD_VALUE;
1231 }
1232 BinderLibTestBundle reply2;
1233 binder->transact(indirect_code, data2, &reply2);
1234 reply2.appendTo(reply);
1235 }
1236 return NO_ERROR;
1237 }
1238 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1239 reply->setError(data.readInt32());
1240 return NO_ERROR;
1241 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1242 reply->writeInt32(sizeof(void *));
1243 return NO_ERROR;
1244 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1245 return NO_ERROR;
1246 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1247 m_strongRef = data.readStrongBinder();
1248 return NO_ERROR;
1249 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1250 int ret;
1251 Parcel data2, reply2;
1252 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1253 sp<IBinder> target;
1254 sp<IBinder> callback;
1255
1256 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001257 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001258 return BAD_VALUE;
1259 }
1260 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001261 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001262 return BAD_VALUE;
1263 }
1264 ret = target->linkToDeath(testDeathRecipient);
1265 if (ret == NO_ERROR)
1266 ret = testDeathRecipient->waitEvent(5);
1267 data2.writeInt32(ret);
1268 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1269 return NO_ERROR;
1270 }
1271 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1272 int ret;
1273 int32_t size;
1274 const void *buf;
1275 int fd;
1276
1277 fd = data.readFileDescriptor();
1278 if (fd < 0) {
1279 return BAD_VALUE;
1280 }
1281 ret = data.readInt32(&size);
1282 if (ret != NO_ERROR) {
1283 return ret;
1284 }
1285 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001286 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001287 return BAD_VALUE;
1288 }
1289 ret = write(fd, buf, size);
1290 if (ret != size)
1291 return UNKNOWN_ERROR;
1292 return NO_ERROR;
1293 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001294 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1295 int ret;
1296 int32_t size;
1297 const void *buf;
1298 android::base::unique_fd fd;
1299
1300 ret = data.readUniqueParcelFileDescriptor(&fd);
1301 if (ret != NO_ERROR) {
1302 return ret;
1303 }
1304 ret = data.readInt32(&size);
1305 if (ret != NO_ERROR) {
1306 return ret;
1307 }
1308 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001309 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001310 return BAD_VALUE;
1311 }
1312 ret = write(fd.get(), buf, size);
1313 if (ret != size) return UNKNOWN_ERROR;
1314 return NO_ERROR;
1315 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001316 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1317 alarm(10);
1318 return NO_ERROR;
1319 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001320 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001321 ;
1322 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001323 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001324 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001325 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001326 return NO_ERROR;
1327 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001328 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1329 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001330 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001331 return NO_ERROR;
1332 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001333 case BINDER_LIB_TEST_ECHO_VECTOR: {
1334 std::vector<uint64_t> vector;
1335 auto err = data.readUint64Vector(&vector);
1336 if (err != NO_ERROR)
1337 return err;
1338 reply->writeUint64Vector(vector);
1339 return NO_ERROR;
1340 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001341 case BINDER_LIB_TEST_REJECT_BUF: {
1342 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1343 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001344 default:
1345 return UNKNOWN_TRANSACTION;
1346 };
1347 }
1348 private:
1349 int32_t m_id;
1350 int32_t m_nextServerId;
1351 pthread_mutex_t m_serverWaitMutex;
1352 pthread_cond_t m_serverWaitCond;
1353 bool m_serverStartRequested;
1354 sp<IBinder> m_serverStarted;
1355 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001356 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001357};
1358
Martijn Coenen45b07b42017-08-09 12:07:45 +02001359int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001360{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001361 binderLibTestServiceName += String16(binderserversuffix);
1362
Riley Andrews06b01ad2014-12-18 12:10:08 -08001363 status_t ret;
1364 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001365 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001366 {
1367 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001368
1369 /*
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 Coenen82c75312019-07-24 15:18:30 +02001375 // Required for test "BufRejected'
1376 testService->setRequestingSid(true);
1377
Martijn Coenen45b07b42017-08-09 12:07:45 +02001378 /*
1379 * We need this below, but can't hold a sp<> because it prevents the
1380 * node from being cleaned up automatically. It's safe in this case
1381 * because of how the tests are written.
1382 */
1383 testServicePtr = testService.get();
1384
Riley Andrews06b01ad2014-12-18 12:10:08 -08001385 if (index == 0) {
1386 ret = sm->addService(binderLibTestServiceName, testService);
1387 } else {
1388 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1389 Parcel data, reply;
1390 data.writeInt32(index);
1391 data.writeStrongBinder(testService);
1392
1393 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1394 }
1395 }
1396 write(readypipefd, &ret, sizeof(ret));
1397 close(readypipefd);
1398 //printf("%s: ret %d\n", __func__, ret);
1399 if (ret)
1400 return 1;
1401 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001402 if (usePoll) {
1403 int fd;
1404 struct epoll_event ev;
1405 int epoll_fd;
1406 IPCThreadState::self()->setupPolling(&fd);
1407 if (fd < 0) {
1408 return 1;
1409 }
1410 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1411
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001412 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001413 if (epoll_fd == -1) {
1414 return 1;
1415 }
1416
1417 ev.events = EPOLLIN;
1418 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1419 return 1;
1420 }
1421
1422 while (1) {
1423 /*
1424 * We simulate a single-threaded process using the binder poll
1425 * interface; besides handling binder commands, it can also
1426 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001427 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001428 *
1429 * processPendingCall() will then issue that transaction.
1430 */
1431 struct epoll_event events[1];
1432 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1433 if (numEvents < 0) {
1434 if (errno == EINTR) {
1435 continue;
1436 }
1437 return 1;
1438 }
1439 if (numEvents > 0) {
1440 IPCThreadState::self()->handlePolledCommands();
1441 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1442 testServicePtr->processPendingCall();
1443 }
1444 }
1445 } else {
1446 ProcessState::self()->startThreadPool();
1447 IPCThreadState::self()->joinThreadPool();
1448 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001449 //printf("%s: joinThreadPool returned\n", __func__);
1450 return 1; /* joinThreadPool should not return */
1451}
1452
1453int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001454 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001455 binderservername = argv[2];
1456 } else {
1457 binderservername = argv[0];
1458 }
1459
Martijn Coenen45b07b42017-08-09 12:07:45 +02001460 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1461 binderserversuffix = argv[5];
1462 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001463 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001464 binderserversuffix = new char[16];
1465 snprintf(binderserversuffix, 16, "%d", getpid());
1466 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001467
1468 ::testing::InitGoogleTest(&argc, argv);
1469 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1470 ProcessState::self()->startThreadPool();
1471 return RUN_ALL_TESTS();
1472}