blob: 40de2db2cbcf988045de691f5cc637deebbe7b41 [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
Steven Morelandf9f3de22020-05-06 17:14:39 -070035#include "binderAbiHelper.h"
36
Riley Andrews06b01ad2014-12-18 12:10:08 -080037#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
38
39using namespace android;
40
Sherry Yang336cdd32017-07-24 14:12:27 -070041static ::testing::AssertionResult IsPageAligned(void *buf) {
42 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
43 return ::testing::AssertionSuccess();
44 else
45 return ::testing::AssertionFailure() << buf << " is not page aligned";
46}
47
Riley Andrews06b01ad2014-12-18 12:10:08 -080048static testing::Environment* binder_env;
49static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070050static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080051static char binderserverarg[] = "--binderserver";
52
53static String16 binderLibTestServiceName = String16("test.binderLib");
54
55enum BinderLibTestTranscationCode {
56 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
57 BINDER_LIB_TEST_REGISTER_SERVER,
58 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020059 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080060 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070061 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020062 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080063 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070064 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080065 BINDER_LIB_TEST_GET_ID_TRANSACTION,
66 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
67 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
68 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
69 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
70 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
71 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090072 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080073 BINDER_LIB_TEST_EXIT_TRANSACTION,
74 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
75 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070076 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010077 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080078 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020079 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080080};
81
Martijn Coenen45b07b42017-08-09 12:07:45 +020082pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080083{
84 int ret;
85 pid_t pid;
86 status_t status;
87 int pipefd[2];
88 char stri[16];
89 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020090 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080091 char *childargv[] = {
92 binderservername,
93 binderserverarg,
94 stri,
95 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020096 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070097 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070098 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080099 };
100
101 ret = pipe(pipefd);
102 if (ret < 0)
103 return ret;
104
105 snprintf(stri, sizeof(stri), "%d", arg2);
106 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200107 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800108
109 pid = fork();
110 if (pid == -1)
111 return pid;
112 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800113 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800114 close(pipefd[0]);
115 execv(binderservername, childargv);
116 status = -errno;
117 write(pipefd[1], &status, sizeof(status));
118 fprintf(stderr, "execv failed, %s\n", strerror(errno));
119 _exit(EXIT_FAILURE);
120 }
121 close(pipefd[1]);
122 ret = read(pipefd[0], &status, sizeof(status));
123 //printf("pipe read returned %d, status %d\n", ret, status);
124 close(pipefd[0]);
125 if (ret == sizeof(status)) {
126 ret = status;
127 } else {
128 kill(pid, SIGKILL);
129 if (ret >= 0) {
130 ret = NO_INIT;
131 }
132 }
133 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700134 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800135 return ret;
136 }
137 return pid;
138}
139
140class BinderLibTestEnv : public ::testing::Environment {
141 public:
142 BinderLibTestEnv() {}
143 sp<IBinder> getServer(void) {
144 return m_server;
145 }
146
147 private:
148 virtual void SetUp() {
149 m_serverpid = start_server_process(0);
150 //printf("m_serverpid %d\n", m_serverpid);
151 ASSERT_GT(m_serverpid, 0);
152
153 sp<IServiceManager> sm = defaultServiceManager();
154 //printf("%s: pid %d, get service\n", __func__, m_pid);
155 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700156 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800157 //printf("%s: pid %d, get service done\n", __func__, m_pid);
158 }
159 virtual void TearDown() {
160 status_t ret;
161 Parcel data, reply;
162 int exitStatus;
163 pid_t pid;
164
165 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700166 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800167 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
168 EXPECT_EQ(0, ret);
169 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
170 EXPECT_EQ(0, ret);
171 }
172 if (m_serverpid > 0) {
173 //printf("wait for %d\n", m_pids[i]);
174 pid = wait(&exitStatus);
175 EXPECT_EQ(m_serverpid, pid);
176 EXPECT_TRUE(WIFEXITED(exitStatus));
177 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
178 }
179 }
180
181 pid_t m_serverpid;
182 sp<IBinder> m_server;
183};
184
185class BinderLibTest : public ::testing::Test {
186 public:
187 virtual void SetUp() {
188 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000189 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800190 }
191 virtual void TearDown() {
192 }
193 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200194 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800195 {
196 int ret;
197 int32_t id;
198 Parcel data, reply;
199 sp<IBinder> binder;
200
Martijn Coenen45b07b42017-08-09 12:07:45 +0200201 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 EXPECT_EQ(NO_ERROR, ret);
203
Yi Kong91635562018-06-07 14:38:36 -0700204 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800205 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700206 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800207 ret = reply.readInt32(&id);
208 EXPECT_EQ(NO_ERROR, ret);
209 if (idPtr)
210 *idPtr = id;
211 return binder;
212 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200213
Yi Kong91635562018-06-07 14:38:36 -0700214 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200215 {
216 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
217 }
218
Yi Kong91635562018-06-07 14:38:36 -0700219 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200220 {
221 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
222 }
223
Riley Andrews06b01ad2014-12-18 12:10:08 -0800224 void waitForReadData(int fd, int timeout_ms) {
225 int ret;
226 pollfd pfd = pollfd();
227
228 pfd.fd = fd;
229 pfd.events = POLLIN;
230 ret = poll(&pfd, 1, timeout_ms);
231 EXPECT_EQ(1, ret);
232 }
233
234 sp<IBinder> m_server;
235};
236
237class BinderLibTestBundle : public Parcel
238{
239 public:
240 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800241 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800242 int32_t mark;
243 int32_t bundleLen;
244 size_t pos;
245
246 if (source->readInt32(&mark))
247 return;
248 if (mark != MARK_START)
249 return;
250 if (source->readInt32(&bundleLen))
251 return;
252 pos = source->dataPosition();
253 if (Parcel::appendFrom(source, pos, bundleLen))
254 return;
255 source->setDataPosition(pos + bundleLen);
256 if (source->readInt32(&mark))
257 return;
258 if (mark != MARK_END)
259 return;
260 m_isValid = true;
261 setDataPosition(0);
262 }
263 void appendTo(Parcel *dest) {
264 dest->writeInt32(MARK_START);
265 dest->writeInt32(dataSize());
266 dest->appendFrom(this, 0, dataSize());
267 dest->writeInt32(MARK_END);
268 };
269 bool isValid(void) {
270 return m_isValid;
271 }
272 private:
273 enum {
274 MARK_START = B_PACK_CHARS('B','T','B','S'),
275 MARK_END = B_PACK_CHARS('B','T','B','E'),
276 };
277 bool m_isValid;
278};
279
280class BinderLibTestEvent
281{
282 public:
283 BinderLibTestEvent(void)
284 : m_eventTriggered(false)
285 {
Yi Kong91635562018-06-07 14:38:36 -0700286 pthread_mutex_init(&m_waitMutex, nullptr);
287 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800288 }
289 int waitEvent(int timeout_s)
290 {
291 int ret;
292 pthread_mutex_lock(&m_waitMutex);
293 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800294 struct timespec ts;
295 clock_gettime(CLOCK_REALTIME, &ts);
296 ts.tv_sec += timeout_s;
297 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800298 }
299 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
300 pthread_mutex_unlock(&m_waitMutex);
301 return ret;
302 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200303 pthread_t getTriggeringThread()
304 {
305 return m_triggeringThread;
306 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800307 protected:
308 void triggerEvent(void) {
309 pthread_mutex_lock(&m_waitMutex);
310 pthread_cond_signal(&m_waitCond);
311 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200312 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800313 pthread_mutex_unlock(&m_waitMutex);
314 };
315 private:
316 pthread_mutex_t m_waitMutex;
317 pthread_cond_t m_waitCond;
318 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200319 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320};
321
322class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
323{
324 public:
325 BinderLibTestCallBack()
326 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700327 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800328 {
329 }
330 status_t getResult(void)
331 {
332 return m_result;
333 }
334
335 private:
336 virtual status_t onTransact(uint32_t code,
337 const Parcel& data, Parcel* reply,
338 uint32_t flags = 0)
339 {
340 (void)reply;
341 (void)flags;
342 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200343 case BINDER_LIB_TEST_CALL_BACK: {
344 status_t status = data.readInt32(&m_result);
345 if (status != NO_ERROR) {
346 m_result = status;
347 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800348 triggerEvent();
349 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200350 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700351 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
352 sp<IBinder> server;
353 int ret;
354 const uint8_t *buf = data.data();
355 size_t size = data.dataSize();
356 if (m_prev_end) {
357 /* 64-bit kernel needs at most 8 bytes to align buffer end */
358 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
359 } else {
360 EXPECT_TRUE(IsPageAligned((void *)buf));
361 }
362
363 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
364
365 if (size > 0) {
366 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
367 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
368 data, reply);
369 EXPECT_EQ(NO_ERROR, ret);
370 }
371 return NO_ERROR;
372 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800373 default:
374 return UNKNOWN_TRANSACTION;
375 }
376 }
377
378 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700379 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800380};
381
382class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
383{
384 private:
385 virtual void binderDied(const wp<IBinder>& who) {
386 (void)who;
387 triggerEvent();
388 };
389};
390
391TEST_F(BinderLibTest, NopTransaction) {
392 status_t ret;
393 Parcel data, reply;
394 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
395 EXPECT_EQ(NO_ERROR, ret);
396}
397
398TEST_F(BinderLibTest, SetError) {
399 int32_t testValue[] = { 0, -123, 123 };
400 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
401 status_t ret;
402 Parcel data, reply;
403 data.writeInt32(testValue[i]);
404 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
405 EXPECT_EQ(testValue[i], ret);
406 }
407}
408
409TEST_F(BinderLibTest, GetId) {
410 status_t ret;
411 int32_t id;
412 Parcel data, reply;
413 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
414 EXPECT_EQ(NO_ERROR, ret);
415 ret = reply.readInt32(&id);
416 EXPECT_EQ(NO_ERROR, ret);
417 EXPECT_EQ(0, id);
418}
419
420TEST_F(BinderLibTest, PtrSize) {
421 status_t ret;
422 int32_t ptrsize;
423 Parcel data, reply;
424 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700425 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800426 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
427 EXPECT_EQ(NO_ERROR, ret);
428 ret = reply.readInt32(&ptrsize);
429 EXPECT_EQ(NO_ERROR, ret);
430 RecordProperty("TestPtrSize", sizeof(void *));
431 RecordProperty("ServerPtrSize", sizeof(void *));
432}
433
434TEST_F(BinderLibTest, IndirectGetId2)
435{
436 status_t ret;
437 int32_t id;
438 int32_t count;
439 Parcel data, reply;
440 int32_t serverId[3];
441
442 data.writeInt32(ARRAY_SIZE(serverId));
443 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
444 sp<IBinder> server;
445 BinderLibTestBundle datai;
446
447 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700448 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800449 data.writeStrongBinder(server);
450 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
451 datai.appendTo(&data);
452 }
453
454 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
455 ASSERT_EQ(NO_ERROR, ret);
456
457 ret = reply.readInt32(&id);
458 ASSERT_EQ(NO_ERROR, ret);
459 EXPECT_EQ(0, id);
460
461 ret = reply.readInt32(&count);
462 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700463 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800464
465 for (size_t i = 0; i < (size_t)count; i++) {
466 BinderLibTestBundle replyi(&reply);
467 EXPECT_TRUE(replyi.isValid());
468 ret = replyi.readInt32(&id);
469 EXPECT_EQ(NO_ERROR, ret);
470 EXPECT_EQ(serverId[i], id);
471 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
472 }
473
474 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
475}
476
477TEST_F(BinderLibTest, IndirectGetId3)
478{
479 status_t ret;
480 int32_t id;
481 int32_t count;
482 Parcel data, reply;
483 int32_t serverId[3];
484
485 data.writeInt32(ARRAY_SIZE(serverId));
486 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
487 sp<IBinder> server;
488 BinderLibTestBundle datai;
489 BinderLibTestBundle datai2;
490
491 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700492 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800493 data.writeStrongBinder(server);
494 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
495
496 datai.writeInt32(1);
497 datai.writeStrongBinder(m_server);
498 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
499 datai2.appendTo(&datai);
500
501 datai.appendTo(&data);
502 }
503
504 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
505 ASSERT_EQ(NO_ERROR, ret);
506
507 ret = reply.readInt32(&id);
508 ASSERT_EQ(NO_ERROR, ret);
509 EXPECT_EQ(0, id);
510
511 ret = reply.readInt32(&count);
512 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700513 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800514
515 for (size_t i = 0; i < (size_t)count; i++) {
516 int32_t counti;
517
518 BinderLibTestBundle replyi(&reply);
519 EXPECT_TRUE(replyi.isValid());
520 ret = replyi.readInt32(&id);
521 EXPECT_EQ(NO_ERROR, ret);
522 EXPECT_EQ(serverId[i], id);
523
524 ret = replyi.readInt32(&counti);
525 ASSERT_EQ(NO_ERROR, ret);
526 EXPECT_EQ(1, counti);
527
528 BinderLibTestBundle replyi2(&replyi);
529 EXPECT_TRUE(replyi2.isValid());
530 ret = replyi2.readInt32(&id);
531 EXPECT_EQ(NO_ERROR, ret);
532 EXPECT_EQ(0, id);
533 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
534
535 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
536 }
537
538 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
539}
540
541TEST_F(BinderLibTest, CallBack)
542{
543 status_t ret;
544 Parcel data, reply;
545 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
546 data.writeStrongBinder(callBack);
547 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
548 EXPECT_EQ(NO_ERROR, ret);
549 ret = callBack->waitEvent(5);
550 EXPECT_EQ(NO_ERROR, ret);
551 ret = callBack->getResult();
552 EXPECT_EQ(NO_ERROR, ret);
553}
554
555TEST_F(BinderLibTest, AddServer)
556{
557 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700558 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559}
560
Riley Andrews06b01ad2014-12-18 12:10:08 -0800561TEST_F(BinderLibTest, DeathNotificationStrongRef)
562{
563 status_t ret;
564 sp<IBinder> sbinder;
565
566 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
567
568 {
569 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700570 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800571 ret = binder->linkToDeath(testDeathRecipient);
572 EXPECT_EQ(NO_ERROR, ret);
573 sbinder = binder;
574 }
575 {
576 Parcel data, reply;
577 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
578 EXPECT_EQ(0, ret);
579 }
580 IPCThreadState::self()->flushCommands();
581 ret = testDeathRecipient->waitEvent(5);
582 EXPECT_EQ(NO_ERROR, ret);
583 ret = sbinder->unlinkToDeath(testDeathRecipient);
584 EXPECT_EQ(DEAD_OBJECT, ret);
585}
586
587TEST_F(BinderLibTest, DeathNotificationMultiple)
588{
589 status_t ret;
590 const int clientcount = 2;
591 sp<IBinder> target;
592 sp<IBinder> linkedclient[clientcount];
593 sp<BinderLibTestCallBack> callBack[clientcount];
594 sp<IBinder> passiveclient[clientcount];
595
596 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700597 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800598 for (int i = 0; i < clientcount; i++) {
599 {
600 Parcel data, reply;
601
602 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700603 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800604 callBack[i] = new BinderLibTestCallBack();
605 data.writeStrongBinder(target);
606 data.writeStrongBinder(callBack[i]);
607 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
608 EXPECT_EQ(NO_ERROR, ret);
609 }
610 {
611 Parcel data, reply;
612
613 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700614 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800615 data.writeStrongBinder(target);
616 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
617 EXPECT_EQ(NO_ERROR, ret);
618 }
619 }
620 {
621 Parcel data, reply;
622 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
623 EXPECT_EQ(0, ret);
624 }
625
626 for (int i = 0; i < clientcount; i++) {
627 ret = callBack[i]->waitEvent(5);
628 EXPECT_EQ(NO_ERROR, ret);
629 ret = callBack[i]->getResult();
630 EXPECT_EQ(NO_ERROR, ret);
631 }
632}
633
Martijn Coenenf7100e42017-07-31 12:14:09 +0200634TEST_F(BinderLibTest, DeathNotificationThread)
635{
636 status_t ret;
637 sp<BinderLibTestCallBack> callback;
638 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700639 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200640 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700641 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200642
643 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
644
645 ret = target->linkToDeath(testDeathRecipient);
646 EXPECT_EQ(NO_ERROR, ret);
647
648 {
649 Parcel data, reply;
650 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
651 EXPECT_EQ(0, ret);
652 }
653
654 /* Make sure it's dead */
655 testDeathRecipient->waitEvent(5);
656
657 /* Now, pass the ref to another process and ask that process to
658 * call linkToDeath() on it, and wait for a response. This tests
659 * two things:
660 * 1) You still get death notifications when calling linkToDeath()
661 * on a ref that is already dead when it was passed to you.
662 * 2) That death notifications are not directly pushed to the thread
663 * registering them, but to the threadpool (proc workqueue) instead.
664 *
665 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
666 * is blocked on a condition variable waiting for the death notification to be
667 * called; therefore, that thread is not available for handling proc work.
668 * So, if the death notification was pushed to the thread workqueue, the callback
669 * would never be called, and the test would timeout and fail.
670 *
671 * Note that we can't do this part of the test from this thread itself, because
672 * the binder driver would only push death notifications to the thread if
673 * it is a looper thread, which this thread is not.
674 *
675 * See b/23525545 for details.
676 */
677 {
678 Parcel data, reply;
679
680 callback = new BinderLibTestCallBack();
681 data.writeStrongBinder(target);
682 data.writeStrongBinder(callback);
683 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
684 EXPECT_EQ(NO_ERROR, ret);
685 }
686
687 ret = callback->waitEvent(5);
688 EXPECT_EQ(NO_ERROR, ret);
689 ret = callback->getResult();
690 EXPECT_EQ(NO_ERROR, ret);
691}
692
Riley Andrews06b01ad2014-12-18 12:10:08 -0800693TEST_F(BinderLibTest, PassFile) {
694 int ret;
695 int pipefd[2];
696 uint8_t buf[1] = { 0 };
697 uint8_t write_value = 123;
698
699 ret = pipe2(pipefd, O_NONBLOCK);
700 ASSERT_EQ(0, ret);
701
702 {
703 Parcel data, reply;
704 uint8_t writebuf[1] = { write_value };
705
706 ret = data.writeFileDescriptor(pipefd[1], true);
707 EXPECT_EQ(NO_ERROR, ret);
708
709 ret = data.writeInt32(sizeof(writebuf));
710 EXPECT_EQ(NO_ERROR, ret);
711
712 ret = data.write(writebuf, sizeof(writebuf));
713 EXPECT_EQ(NO_ERROR, ret);
714
715 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
716 EXPECT_EQ(NO_ERROR, ret);
717 }
718
719 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700720 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800721 EXPECT_EQ(write_value, buf[0]);
722
723 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
724
725 ret = read(pipefd[0], buf, sizeof(buf));
726 EXPECT_EQ(0, ret);
727
728 close(pipefd[0]);
729}
730
Ryo Hashimotobf551892018-05-31 16:58:35 +0900731TEST_F(BinderLibTest, PassParcelFileDescriptor) {
732 const int datasize = 123;
733 std::vector<uint8_t> writebuf(datasize);
734 for (size_t i = 0; i < writebuf.size(); ++i) {
735 writebuf[i] = i;
736 }
737
738 android::base::unique_fd read_end, write_end;
739 {
740 int pipefd[2];
741 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
742 read_end.reset(pipefd[0]);
743 write_end.reset(pipefd[1]);
744 }
745 {
746 Parcel data;
747 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
748 write_end.reset();
749 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
750 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
751
752 Parcel reply;
753 EXPECT_EQ(NO_ERROR,
754 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
755 &reply));
756 }
757 std::vector<uint8_t> readbuf(datasize);
758 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
759 EXPECT_EQ(writebuf, readbuf);
760
761 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
762
763 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
764}
765
Riley Andrews06b01ad2014-12-18 12:10:08 -0800766TEST_F(BinderLibTest, PromoteLocal) {
767 sp<IBinder> strong = new BBinder();
768 wp<IBinder> weak = strong;
769 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700770 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800771 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700772 strong = nullptr;
773 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800774 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700775 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800776}
777
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700778TEST_F(BinderLibTest, LocalGetExtension) {
779 sp<BBinder> binder = new BBinder();
780 sp<IBinder> ext = new BBinder();
781 binder->setExtension(ext);
782 EXPECT_EQ(ext, binder->getExtension());
783}
784
785TEST_F(BinderLibTest, RemoteGetExtension) {
786 sp<IBinder> server = addServer();
787 ASSERT_TRUE(server != nullptr);
788
789 sp<IBinder> extension;
790 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
791 ASSERT_NE(nullptr, extension.get());
792
793 EXPECT_EQ(NO_ERROR, extension->pingBinder());
794}
795
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700796TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
797 status_t ret;
798 Parcel data, reply;
799
800 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
801 EXPECT_EQ(NO_ERROR, ret);
802
803 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700804 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800805 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
806 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
807 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
808 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700809}
810
Connor O'Brien52be2c92016-09-20 14:18:08 -0700811TEST_F(BinderLibTest, FreedBinder) {
812 status_t ret;
813
814 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700815 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700816
817 __u32 freedHandle;
818 wp<IBinder> keepFreedBinder;
819 {
820 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700821 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
822 ASSERT_EQ(NO_ERROR, ret);
823 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
824 freedHandle = freed->handle;
825 /* Add a weak ref to the freed binder so the driver does not
826 * delete its reference to it - otherwise the transaction
827 * fails regardless of whether the driver is fixed.
828 */
Steven Morelande171d622019-07-17 16:06:01 -0700829 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700830 }
Steven Morelande171d622019-07-17 16:06:01 -0700831 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700832 {
833 Parcel data, reply;
834 data.writeStrongBinder(server);
835 /* Replace original handle with handle to the freed binder */
836 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
837 __u32 oldHandle = strong->handle;
838 strong->handle = freedHandle;
839 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
840 /* Returns DEAD_OBJECT (-32) if target crashes and
841 * FAILED_TRANSACTION if the driver rejects the invalid
842 * object.
843 */
844 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
845 /* Restore original handle so parcel destructor does not use
846 * the wrong handle.
847 */
848 strong->handle = oldHandle;
849 }
850}
851
Sherry Yang336cdd32017-07-24 14:12:27 -0700852TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
853 status_t ret;
854 Parcel data, reply;
855 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
856 for (int i = 0; i < 2; i++) {
857 BinderLibTestBundle datai;
858 datai.appendFrom(&data, 0, data.dataSize());
859
860 data.freeData();
861 data.writeInt32(1);
862 data.writeStrongBinder(callBack);
863 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
864
865 datai.appendTo(&data);
866 }
867 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
868 EXPECT_EQ(NO_ERROR, ret);
869}
870
Martijn Coenen45b07b42017-08-09 12:07:45 +0200871TEST_F(BinderLibTest, OnewayQueueing)
872{
873 status_t ret;
874 Parcel data, data2;
875
876 sp<IBinder> pollServer = addPollServer();
877
878 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
879 data.writeStrongBinder(callBack);
880 data.writeInt32(500000); // delay in us before calling back
881
882 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
883 data2.writeStrongBinder(callBack2);
884 data2.writeInt32(0); // delay in us
885
Yi Kong91635562018-06-07 14:38:36 -0700886 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200887 EXPECT_EQ(NO_ERROR, ret);
888
889 // The delay ensures that this second transaction will end up on the async_todo list
890 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700891 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200892 EXPECT_EQ(NO_ERROR, ret);
893
894 // The server will ensure that the two transactions are handled in the expected order;
895 // If the ordering is not as expected, an error will be returned through the callbacks.
896 ret = callBack->waitEvent(2);
897 EXPECT_EQ(NO_ERROR, ret);
898 ret = callBack->getResult();
899 EXPECT_EQ(NO_ERROR, ret);
900
901 ret = callBack2->waitEvent(2);
902 EXPECT_EQ(NO_ERROR, ret);
903 ret = callBack2->getResult();
904 EXPECT_EQ(NO_ERROR, ret);
905}
906
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100907TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
908{
909 status_t ret;
910 Parcel data, reply;
911 data.writeInterfaceToken(binderLibTestServiceName);
912 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
913 EXPECT_EQ(-1, reply.readInt32());
914 EXPECT_EQ(NO_ERROR, ret);
915}
916
917TEST_F(BinderLibTest, WorkSourceSet)
918{
919 status_t ret;
920 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000921 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000922 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100923 data.writeInterfaceToken(binderLibTestServiceName);
924 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
925 EXPECT_EQ(100, reply.readInt32());
926 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000927 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
928 EXPECT_EQ(NO_ERROR, ret);
929}
930
931TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
932{
933 status_t ret;
934 Parcel data, reply;
935
936 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
937 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
938
939 data.writeInterfaceToken(binderLibTestServiceName);
940 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
941 EXPECT_EQ(-1, reply.readInt32());
942 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100943 EXPECT_EQ(NO_ERROR, ret);
944}
945
946TEST_F(BinderLibTest, WorkSourceCleared)
947{
948 status_t ret;
949 Parcel data, reply;
950
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000951 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000952 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
953 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100954 data.writeInterfaceToken(binderLibTestServiceName);
955 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
956
957 EXPECT_EQ(-1, reply.readInt32());
958 EXPECT_EQ(100, previousWorkSource);
959 EXPECT_EQ(NO_ERROR, ret);
960}
961
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000962TEST_F(BinderLibTest, WorkSourceRestored)
963{
964 status_t ret;
965 Parcel data, reply;
966
967 IPCThreadState::self()->setCallingWorkSourceUid(100);
968 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
969 IPCThreadState::self()->restoreCallingWorkSource(token);
970
971 data.writeInterfaceToken(binderLibTestServiceName);
972 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
973
974 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000975 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000976 EXPECT_EQ(NO_ERROR, ret);
977}
978
Olivier Gaillard91a04802018-11-14 17:32:41 +0000979TEST_F(BinderLibTest, PropagateFlagSet)
980{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000981 IPCThreadState::self()->clearPropagateWorkSource();
982 IPCThreadState::self()->setCallingWorkSourceUid(100);
983 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
984}
985
986TEST_F(BinderLibTest, PropagateFlagCleared)
987{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000988 IPCThreadState::self()->setCallingWorkSourceUid(100);
989 IPCThreadState::self()->clearPropagateWorkSource();
990 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
991}
992
993TEST_F(BinderLibTest, PropagateFlagRestored)
994{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000995 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
996 IPCThreadState::self()->restoreCallingWorkSource(token);
997
998 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
999}
1000
1001TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1002{
1003 IPCThreadState::self()->setCallingWorkSourceUid(100);
1004
1005 Parcel data, reply;
1006 status_t ret;
1007 data.writeInterfaceToken(binderLibTestServiceName);
1008 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1009
1010 Parcel data2, reply2;
1011 status_t ret2;
1012 data2.writeInterfaceToken(binderLibTestServiceName);
1013 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1014 EXPECT_EQ(100, reply2.readInt32());
1015 EXPECT_EQ(NO_ERROR, ret2);
1016}
1017
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001018TEST_F(BinderLibTest, VectorSent) {
1019 Parcel data, reply;
1020 sp<IBinder> server = addServer();
1021 ASSERT_TRUE(server != nullptr);
1022
1023 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1024 data.writeUint64Vector(testValue);
1025
1026 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1027 EXPECT_EQ(NO_ERROR, ret);
1028 std::vector<uint64_t> readValue;
1029 ret = reply.readUint64Vector(&readValue);
1030 EXPECT_EQ(readValue, testValue);
1031}
1032
Martijn Coenen82c75312019-07-24 15:18:30 +02001033TEST_F(BinderLibTest, BufRejected) {
1034 Parcel data, reply;
1035 uint32_t buf;
1036 sp<IBinder> server = addServer();
1037 ASSERT_TRUE(server != nullptr);
1038
1039 binder_buffer_object obj {
1040 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001041 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001042 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1043 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001044 };
1045 data.setDataCapacity(1024);
1046 // Write a bogus object at offset 0 to get an entry in the offset table
1047 data.writeFileDescriptor(0);
1048 EXPECT_EQ(data.objectsCount(), 1);
1049 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1050 // And now, overwrite it with the buffer object
1051 memcpy(parcelData, &obj, sizeof(obj));
1052 data.setDataSize(sizeof(obj));
1053
1054 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1055 // Either the kernel should reject this transaction (if it's correct), but
1056 // if it's not, the server implementation should return an error if it
1057 // finds an object in the received Parcel.
1058 EXPECT_NE(NO_ERROR, ret);
1059}
1060
Riley Andrews06b01ad2014-12-18 12:10:08 -08001061class BinderLibTestService : public BBinder
1062{
1063 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001064 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001065 : m_id(id)
1066 , m_nextServerId(id + 1)
1067 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001068 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001069 {
Yi Kong91635562018-06-07 14:38:36 -07001070 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1071 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001072 }
1073 ~BinderLibTestService()
1074 {
1075 exit(EXIT_SUCCESS);
1076 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001077
1078 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001079 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001080 Parcel data;
1081 data.writeInt32(NO_ERROR);
1082 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001083 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001084 }
1085 }
1086
Riley Andrews06b01ad2014-12-18 12:10:08 -08001087 virtual status_t onTransact(uint32_t code,
1088 const Parcel& data, Parcel* reply,
1089 uint32_t flags = 0) {
1090 //printf("%s: code %d\n", __func__, code);
1091 (void)flags;
1092
1093 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1094 return PERMISSION_DENIED;
1095 }
1096 switch (code) {
1097 case BINDER_LIB_TEST_REGISTER_SERVER: {
1098 int32_t id;
1099 sp<IBinder> binder;
1100 id = data.readInt32();
1101 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001102 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001103 return BAD_VALUE;
1104 }
1105
1106 if (m_id != 0)
1107 return INVALID_OPERATION;
1108
1109 pthread_mutex_lock(&m_serverWaitMutex);
1110 if (m_serverStartRequested) {
1111 m_serverStartRequested = false;
1112 m_serverStarted = binder;
1113 pthread_cond_signal(&m_serverWaitCond);
1114 }
1115 pthread_mutex_unlock(&m_serverWaitMutex);
1116 return NO_ERROR;
1117 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001118 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001119 case BINDER_LIB_TEST_ADD_SERVER: {
1120 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001121 int serverid;
1122
1123 if (m_id != 0) {
1124 return INVALID_OPERATION;
1125 }
1126 pthread_mutex_lock(&m_serverWaitMutex);
1127 if (m_serverStartRequested) {
1128 ret = -EBUSY;
1129 } else {
1130 serverid = m_nextServerId++;
1131 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001132 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001133
1134 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001135 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001136 pthread_mutex_lock(&m_serverWaitMutex);
1137 }
1138 if (ret > 0) {
1139 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001140 struct timespec ts;
1141 clock_gettime(CLOCK_REALTIME, &ts);
1142 ts.tv_sec += 5;
1143 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001144 }
1145 if (m_serverStartRequested) {
1146 m_serverStartRequested = false;
1147 ret = -ETIMEDOUT;
1148 } else {
1149 reply->writeStrongBinder(m_serverStarted);
1150 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001151 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001152 ret = NO_ERROR;
1153 }
1154 } else if (ret >= 0) {
1155 m_serverStartRequested = false;
1156 ret = UNKNOWN_ERROR;
1157 }
1158 pthread_mutex_unlock(&m_serverWaitMutex);
1159 return ret;
1160 }
1161 case BINDER_LIB_TEST_NOP_TRANSACTION:
1162 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001163 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1164 // Note: this transaction is only designed for use with a
1165 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001166 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001167 // A callback was already pending; this means that
1168 // we received a second call while still processing
1169 // the first one. Fail the test.
1170 sp<IBinder> callback = data.readStrongBinder();
1171 Parcel data2;
1172 data2.writeInt32(UNKNOWN_ERROR);
1173
Yi Kong91635562018-06-07 14:38:36 -07001174 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001175 } else {
1176 m_callback = data.readStrongBinder();
1177 int32_t delayUs = data.readInt32();
1178 /*
1179 * It's necessary that we sleep here, so the next
1180 * transaction the caller makes will be queued to
1181 * the async queue.
1182 */
1183 usleep(delayUs);
1184
1185 /*
1186 * Now when we return, libbinder will tell the kernel
1187 * we are done with this transaction, and the kernel
1188 * can move the queued transaction to either the
1189 * thread todo worklist (for kernels without the fix),
1190 * or the proc todo worklist. In case of the former,
1191 * the next outbound call will pick up the pending
1192 * transaction, which leads to undesired reentrant
1193 * behavior. This is caught in the if() branch above.
1194 */
1195 }
1196
1197 return NO_ERROR;
1198 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1200 Parcel data2, reply2;
1201 sp<IBinder> binder;
1202 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001203 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001204 return BAD_VALUE;
1205 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001206 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001207 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1208 return NO_ERROR;
1209 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001210 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1211 reply->writeStrongBinder(this);
1212 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001213 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1214 reply->writeInt32(m_id);
1215 return NO_ERROR;
1216 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1217 int32_t count;
1218 uint32_t indirect_code;
1219 sp<IBinder> binder;
1220
1221 count = data.readInt32();
1222 reply->writeInt32(m_id);
1223 reply->writeInt32(count);
1224 for (int i = 0; i < count; i++) {
1225 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001226 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001227 return BAD_VALUE;
1228 }
1229 indirect_code = data.readInt32();
1230 BinderLibTestBundle data2(&data);
1231 if (!data2.isValid()) {
1232 return BAD_VALUE;
1233 }
1234 BinderLibTestBundle reply2;
1235 binder->transact(indirect_code, data2, &reply2);
1236 reply2.appendTo(reply);
1237 }
1238 return NO_ERROR;
1239 }
1240 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1241 reply->setError(data.readInt32());
1242 return NO_ERROR;
1243 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1244 reply->writeInt32(sizeof(void *));
1245 return NO_ERROR;
1246 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1247 return NO_ERROR;
1248 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1249 m_strongRef = data.readStrongBinder();
1250 return NO_ERROR;
1251 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1252 int ret;
1253 Parcel data2, reply2;
1254 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1255 sp<IBinder> target;
1256 sp<IBinder> callback;
1257
1258 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001259 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001260 return BAD_VALUE;
1261 }
1262 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001263 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001264 return BAD_VALUE;
1265 }
1266 ret = target->linkToDeath(testDeathRecipient);
1267 if (ret == NO_ERROR)
1268 ret = testDeathRecipient->waitEvent(5);
1269 data2.writeInt32(ret);
1270 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1271 return NO_ERROR;
1272 }
1273 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1274 int ret;
1275 int32_t size;
1276 const void *buf;
1277 int fd;
1278
1279 fd = data.readFileDescriptor();
1280 if (fd < 0) {
1281 return BAD_VALUE;
1282 }
1283 ret = data.readInt32(&size);
1284 if (ret != NO_ERROR) {
1285 return ret;
1286 }
1287 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001288 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001289 return BAD_VALUE;
1290 }
1291 ret = write(fd, buf, size);
1292 if (ret != size)
1293 return UNKNOWN_ERROR;
1294 return NO_ERROR;
1295 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001296 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1297 int ret;
1298 int32_t size;
1299 const void *buf;
1300 android::base::unique_fd fd;
1301
1302 ret = data.readUniqueParcelFileDescriptor(&fd);
1303 if (ret != NO_ERROR) {
1304 return ret;
1305 }
1306 ret = data.readInt32(&size);
1307 if (ret != NO_ERROR) {
1308 return ret;
1309 }
1310 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001311 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001312 return BAD_VALUE;
1313 }
1314 ret = write(fd.get(), buf, size);
1315 if (ret != size) return UNKNOWN_ERROR;
1316 return NO_ERROR;
1317 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001318 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1319 alarm(10);
1320 return NO_ERROR;
1321 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001322 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001323 ;
1324 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001325 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001326 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001327 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001328 return NO_ERROR;
1329 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001330 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1331 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001332 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001333 return NO_ERROR;
1334 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001335 case BINDER_LIB_TEST_ECHO_VECTOR: {
1336 std::vector<uint64_t> vector;
1337 auto err = data.readUint64Vector(&vector);
1338 if (err != NO_ERROR)
1339 return err;
1340 reply->writeUint64Vector(vector);
1341 return NO_ERROR;
1342 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001343 case BINDER_LIB_TEST_REJECT_BUF: {
1344 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1345 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001346 default:
1347 return UNKNOWN_TRANSACTION;
1348 };
1349 }
1350 private:
1351 int32_t m_id;
1352 int32_t m_nextServerId;
1353 pthread_mutex_t m_serverWaitMutex;
1354 pthread_cond_t m_serverWaitCond;
1355 bool m_serverStartRequested;
1356 sp<IBinder> m_serverStarted;
1357 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001358 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001359};
1360
Martijn Coenen45b07b42017-08-09 12:07:45 +02001361int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001362{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001363 binderLibTestServiceName += String16(binderserversuffix);
1364
Riley Andrews06b01ad2014-12-18 12:10:08 -08001365 status_t ret;
1366 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001367 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001368 {
1369 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001370
1371 /*
1372 * Normally would also contain functionality as well, but we are only
1373 * testing the extension mechanism.
1374 */
1375 testService->setExtension(new BBinder());
1376
Martijn Coenen82c75312019-07-24 15:18:30 +02001377 // Required for test "BufRejected'
1378 testService->setRequestingSid(true);
1379
Martijn Coenen45b07b42017-08-09 12:07:45 +02001380 /*
1381 * We need this below, but can't hold a sp<> because it prevents the
1382 * node from being cleaned up automatically. It's safe in this case
1383 * because of how the tests are written.
1384 */
1385 testServicePtr = testService.get();
1386
Riley Andrews06b01ad2014-12-18 12:10:08 -08001387 if (index == 0) {
1388 ret = sm->addService(binderLibTestServiceName, testService);
1389 } else {
1390 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1391 Parcel data, reply;
1392 data.writeInt32(index);
1393 data.writeStrongBinder(testService);
1394
1395 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1396 }
1397 }
1398 write(readypipefd, &ret, sizeof(ret));
1399 close(readypipefd);
1400 //printf("%s: ret %d\n", __func__, ret);
1401 if (ret)
1402 return 1;
1403 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001404 if (usePoll) {
1405 int fd;
1406 struct epoll_event ev;
1407 int epoll_fd;
1408 IPCThreadState::self()->setupPolling(&fd);
1409 if (fd < 0) {
1410 return 1;
1411 }
1412 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1413
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001414 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001415 if (epoll_fd == -1) {
1416 return 1;
1417 }
1418
1419 ev.events = EPOLLIN;
1420 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1421 return 1;
1422 }
1423
1424 while (1) {
1425 /*
1426 * We simulate a single-threaded process using the binder poll
1427 * interface; besides handling binder commands, it can also
1428 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001429 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001430 *
1431 * processPendingCall() will then issue that transaction.
1432 */
1433 struct epoll_event events[1];
1434 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1435 if (numEvents < 0) {
1436 if (errno == EINTR) {
1437 continue;
1438 }
1439 return 1;
1440 }
1441 if (numEvents > 0) {
1442 IPCThreadState::self()->handlePolledCommands();
1443 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1444 testServicePtr->processPendingCall();
1445 }
1446 }
1447 } else {
1448 ProcessState::self()->startThreadPool();
1449 IPCThreadState::self()->joinThreadPool();
1450 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001451 //printf("%s: joinThreadPool returned\n", __func__);
1452 return 1; /* joinThreadPool should not return */
1453}
1454
1455int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001456 ExitIfWrongAbi();
1457
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001458 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001459 binderservername = argv[2];
1460 } else {
1461 binderservername = argv[0];
1462 }
1463
Martijn Coenen45b07b42017-08-09 12:07:45 +02001464 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1465 binderserversuffix = argv[5];
1466 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001467 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001468 binderserversuffix = new char[16];
1469 snprintf(binderserversuffix, 16, "%d", getpid());
1470 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001471
1472 ::testing::InitGoogleTest(&argc, argv);
1473 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1474 ProcessState::self()->startThreadPool();
1475 return RUN_ALL_TESTS();
1476}