blob: 145c09940b8faeea853f79df1134c9ec8e15f6c4 [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>
Marco Ballesio7ee17572020-09-08 10:30:03 -070019#include <fstream>
Riley Andrews06b01ad2014-12-18 12:10:08 -080020#include <poll.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <gtest/gtest.h>
26
27#include <binder/Binder.h>
28#include <binder/IBinder.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31
Steven Morelandd63ed9d2019-09-04 18:15:25 -070032#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020033#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080034#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020035
Steven Morelandf9f3de22020-05-06 17:14:39 -070036#include "binderAbiHelper.h"
37
Riley Andrews06b01ad2014-12-18 12:10:08 -080038#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
39
40using namespace android;
41
Sherry Yang336cdd32017-07-24 14:12:27 -070042static ::testing::AssertionResult IsPageAligned(void *buf) {
43 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
44 return ::testing::AssertionSuccess();
45 else
46 return ::testing::AssertionFailure() << buf << " is not page aligned";
47}
48
Riley Andrews06b01ad2014-12-18 12:10:08 -080049static testing::Environment* binder_env;
50static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070051static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080052static char binderserverarg[] = "--binderserver";
53
Steven Morelandbf1915b2020-07-16 22:43:02 +000054static constexpr int kSchedPolicy = SCHED_RR;
55static constexpr int kSchedPriority = 7;
56
Riley Andrews06b01ad2014-12-18 12:10:08 -080057static String16 binderLibTestServiceName = String16("test.binderLib");
58
59enum BinderLibTestTranscationCode {
60 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
61 BINDER_LIB_TEST_REGISTER_SERVER,
62 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020063 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080064 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070065 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020066 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080067 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070068 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_GET_ID_TRANSACTION,
70 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
71 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
72 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
73 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
74 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
75 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090076 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080077 BINDER_LIB_TEST_EXIT_TRANSACTION,
78 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
79 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070080 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010081 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000082 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070083 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
84 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080085 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020086 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080087};
88
Martijn Coenen45b07b42017-08-09 12:07:45 +020089pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080090{
91 int ret;
92 pid_t pid;
93 status_t status;
94 int pipefd[2];
95 char stri[16];
96 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020097 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080098 char *childargv[] = {
99 binderservername,
100 binderserverarg,
101 stri,
102 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200103 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700104 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700105 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800106 };
107
108 ret = pipe(pipefd);
109 if (ret < 0)
110 return ret;
111
112 snprintf(stri, sizeof(stri), "%d", arg2);
113 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200114 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800115
116 pid = fork();
117 if (pid == -1)
118 return pid;
119 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800120 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800121 close(pipefd[0]);
122 execv(binderservername, childargv);
123 status = -errno;
124 write(pipefd[1], &status, sizeof(status));
125 fprintf(stderr, "execv failed, %s\n", strerror(errno));
126 _exit(EXIT_FAILURE);
127 }
128 close(pipefd[1]);
129 ret = read(pipefd[0], &status, sizeof(status));
130 //printf("pipe read returned %d, status %d\n", ret, status);
131 close(pipefd[0]);
132 if (ret == sizeof(status)) {
133 ret = status;
134 } else {
135 kill(pid, SIGKILL);
136 if (ret >= 0) {
137 ret = NO_INIT;
138 }
139 }
140 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700141 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800142 return ret;
143 }
144 return pid;
145}
146
147class BinderLibTestEnv : public ::testing::Environment {
148 public:
149 BinderLibTestEnv() {}
150 sp<IBinder> getServer(void) {
151 return m_server;
152 }
153
154 private:
155 virtual void SetUp() {
156 m_serverpid = start_server_process(0);
157 //printf("m_serverpid %d\n", m_serverpid);
158 ASSERT_GT(m_serverpid, 0);
159
160 sp<IServiceManager> sm = defaultServiceManager();
161 //printf("%s: pid %d, get service\n", __func__, m_pid);
162 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700163 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800164 //printf("%s: pid %d, get service done\n", __func__, m_pid);
165 }
166 virtual void TearDown() {
167 status_t ret;
168 Parcel data, reply;
169 int exitStatus;
170 pid_t pid;
171
172 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700173 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800174 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
175 EXPECT_EQ(0, ret);
176 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
177 EXPECT_EQ(0, ret);
178 }
179 if (m_serverpid > 0) {
180 //printf("wait for %d\n", m_pids[i]);
181 pid = wait(&exitStatus);
182 EXPECT_EQ(m_serverpid, pid);
183 EXPECT_TRUE(WIFEXITED(exitStatus));
184 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
185 }
186 }
187
188 pid_t m_serverpid;
189 sp<IBinder> m_server;
190};
191
192class BinderLibTest : public ::testing::Test {
193 public:
194 virtual void SetUp() {
195 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000196 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 }
198 virtual void TearDown() {
199 }
200 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200201 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 {
203 int ret;
204 int32_t id;
205 Parcel data, reply;
206 sp<IBinder> binder;
207
Martijn Coenen45b07b42017-08-09 12:07:45 +0200208 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 EXPECT_EQ(NO_ERROR, ret);
210
Yi Kong91635562018-06-07 14:38:36 -0700211 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800212 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700213 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 ret = reply.readInt32(&id);
215 EXPECT_EQ(NO_ERROR, ret);
216 if (idPtr)
217 *idPtr = id;
218 return binder;
219 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200220
Yi Kong91635562018-06-07 14:38:36 -0700221 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200222 {
223 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
224 }
225
Yi Kong91635562018-06-07 14:38:36 -0700226 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200227 {
228 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
229 }
230
Riley Andrews06b01ad2014-12-18 12:10:08 -0800231 void waitForReadData(int fd, int timeout_ms) {
232 int ret;
233 pollfd pfd = pollfd();
234
235 pfd.fd = fd;
236 pfd.events = POLLIN;
237 ret = poll(&pfd, 1, timeout_ms);
238 EXPECT_EQ(1, ret);
239 }
240
241 sp<IBinder> m_server;
242};
243
244class BinderLibTestBundle : public Parcel
245{
246 public:
247 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800248 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800249 int32_t mark;
250 int32_t bundleLen;
251 size_t pos;
252
253 if (source->readInt32(&mark))
254 return;
255 if (mark != MARK_START)
256 return;
257 if (source->readInt32(&bundleLen))
258 return;
259 pos = source->dataPosition();
260 if (Parcel::appendFrom(source, pos, bundleLen))
261 return;
262 source->setDataPosition(pos + bundleLen);
263 if (source->readInt32(&mark))
264 return;
265 if (mark != MARK_END)
266 return;
267 m_isValid = true;
268 setDataPosition(0);
269 }
270 void appendTo(Parcel *dest) {
271 dest->writeInt32(MARK_START);
272 dest->writeInt32(dataSize());
273 dest->appendFrom(this, 0, dataSize());
274 dest->writeInt32(MARK_END);
275 };
276 bool isValid(void) {
277 return m_isValid;
278 }
279 private:
280 enum {
281 MARK_START = B_PACK_CHARS('B','T','B','S'),
282 MARK_END = B_PACK_CHARS('B','T','B','E'),
283 };
284 bool m_isValid;
285};
286
287class BinderLibTestEvent
288{
289 public:
290 BinderLibTestEvent(void)
291 : m_eventTriggered(false)
292 {
Yi Kong91635562018-06-07 14:38:36 -0700293 pthread_mutex_init(&m_waitMutex, nullptr);
294 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800295 }
296 int waitEvent(int timeout_s)
297 {
298 int ret;
299 pthread_mutex_lock(&m_waitMutex);
300 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800301 struct timespec ts;
302 clock_gettime(CLOCK_REALTIME, &ts);
303 ts.tv_sec += timeout_s;
304 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 }
306 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
307 pthread_mutex_unlock(&m_waitMutex);
308 return ret;
309 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 pthread_t getTriggeringThread()
311 {
312 return m_triggeringThread;
313 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314 protected:
315 void triggerEvent(void) {
316 pthread_mutex_lock(&m_waitMutex);
317 pthread_cond_signal(&m_waitCond);
318 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200319 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320 pthread_mutex_unlock(&m_waitMutex);
321 };
322 private:
323 pthread_mutex_t m_waitMutex;
324 pthread_cond_t m_waitCond;
325 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200326 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800327};
328
329class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
330{
331 public:
332 BinderLibTestCallBack()
333 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700334 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800335 {
336 }
337 status_t getResult(void)
338 {
339 return m_result;
340 }
341
342 private:
343 virtual status_t onTransact(uint32_t code,
344 const Parcel& data, Parcel* reply,
345 uint32_t flags = 0)
346 {
347 (void)reply;
348 (void)flags;
349 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200350 case BINDER_LIB_TEST_CALL_BACK: {
351 status_t status = data.readInt32(&m_result);
352 if (status != NO_ERROR) {
353 m_result = status;
354 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800355 triggerEvent();
356 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200357 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700358 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
359 sp<IBinder> server;
360 int ret;
361 const uint8_t *buf = data.data();
362 size_t size = data.dataSize();
363 if (m_prev_end) {
364 /* 64-bit kernel needs at most 8 bytes to align buffer end */
365 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
366 } else {
367 EXPECT_TRUE(IsPageAligned((void *)buf));
368 }
369
370 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
371
372 if (size > 0) {
373 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
374 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
375 data, reply);
376 EXPECT_EQ(NO_ERROR, ret);
377 }
378 return NO_ERROR;
379 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800380 default:
381 return UNKNOWN_TRANSACTION;
382 }
383 }
384
385 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700386 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800387};
388
389class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
390{
391 private:
392 virtual void binderDied(const wp<IBinder>& who) {
393 (void)who;
394 triggerEvent();
395 };
396};
397
398TEST_F(BinderLibTest, NopTransaction) {
399 status_t ret;
400 Parcel data, reply;
401 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
402 EXPECT_EQ(NO_ERROR, ret);
403}
404
Marco Ballesio7ee17572020-09-08 10:30:03 -0700405TEST_F(BinderLibTest, Freeze) {
406 status_t ret;
407 Parcel data, reply, replypid;
408 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
409
410 //Pass test on devices where the freezer is not supported
411 if (freezer_file.fail()) {
412 GTEST_SKIP();
413 return;
414 }
415
416 std::string freezer_enabled;
417 std::getline(freezer_file, freezer_enabled);
418
419 //Pass test on devices where the freezer is disabled
420 if (freezer_enabled != "1") {
421 GTEST_SKIP();
422 return;
423 }
424
425 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
426 int32_t pid = replypid.readInt32();
427 EXPECT_EQ(NO_ERROR, ret);
428 for (int i = 0; i < 10; i++) {
429 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
430 }
431 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
432 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
433 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
434 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
435 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
436 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
437}
438
Riley Andrews06b01ad2014-12-18 12:10:08 -0800439TEST_F(BinderLibTest, SetError) {
440 int32_t testValue[] = { 0, -123, 123 };
441 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
442 status_t ret;
443 Parcel data, reply;
444 data.writeInt32(testValue[i]);
445 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
446 EXPECT_EQ(testValue[i], ret);
447 }
448}
449
450TEST_F(BinderLibTest, GetId) {
451 status_t ret;
452 int32_t id;
453 Parcel data, reply;
454 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
455 EXPECT_EQ(NO_ERROR, ret);
456 ret = reply.readInt32(&id);
457 EXPECT_EQ(NO_ERROR, ret);
458 EXPECT_EQ(0, id);
459}
460
461TEST_F(BinderLibTest, PtrSize) {
462 status_t ret;
463 int32_t ptrsize;
464 Parcel data, reply;
465 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700466 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800467 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
468 EXPECT_EQ(NO_ERROR, ret);
469 ret = reply.readInt32(&ptrsize);
470 EXPECT_EQ(NO_ERROR, ret);
471 RecordProperty("TestPtrSize", sizeof(void *));
472 RecordProperty("ServerPtrSize", sizeof(void *));
473}
474
475TEST_F(BinderLibTest, IndirectGetId2)
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
488 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700489 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800490 data.writeStrongBinder(server);
491 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
492 datai.appendTo(&data);
493 }
494
495 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
496 ASSERT_EQ(NO_ERROR, ret);
497
498 ret = reply.readInt32(&id);
499 ASSERT_EQ(NO_ERROR, ret);
500 EXPECT_EQ(0, id);
501
502 ret = reply.readInt32(&count);
503 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700504 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800505
506 for (size_t i = 0; i < (size_t)count; i++) {
507 BinderLibTestBundle replyi(&reply);
508 EXPECT_TRUE(replyi.isValid());
509 ret = replyi.readInt32(&id);
510 EXPECT_EQ(NO_ERROR, ret);
511 EXPECT_EQ(serverId[i], id);
512 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
513 }
514
515 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
516}
517
518TEST_F(BinderLibTest, IndirectGetId3)
519{
520 status_t ret;
521 int32_t id;
522 int32_t count;
523 Parcel data, reply;
524 int32_t serverId[3];
525
526 data.writeInt32(ARRAY_SIZE(serverId));
527 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
528 sp<IBinder> server;
529 BinderLibTestBundle datai;
530 BinderLibTestBundle datai2;
531
532 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700533 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800534 data.writeStrongBinder(server);
535 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
536
537 datai.writeInt32(1);
538 datai.writeStrongBinder(m_server);
539 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
540 datai2.appendTo(&datai);
541
542 datai.appendTo(&data);
543 }
544
545 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
546 ASSERT_EQ(NO_ERROR, ret);
547
548 ret = reply.readInt32(&id);
549 ASSERT_EQ(NO_ERROR, ret);
550 EXPECT_EQ(0, id);
551
552 ret = reply.readInt32(&count);
553 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700554 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800555
556 for (size_t i = 0; i < (size_t)count; i++) {
557 int32_t counti;
558
559 BinderLibTestBundle replyi(&reply);
560 EXPECT_TRUE(replyi.isValid());
561 ret = replyi.readInt32(&id);
562 EXPECT_EQ(NO_ERROR, ret);
563 EXPECT_EQ(serverId[i], id);
564
565 ret = replyi.readInt32(&counti);
566 ASSERT_EQ(NO_ERROR, ret);
567 EXPECT_EQ(1, counti);
568
569 BinderLibTestBundle replyi2(&replyi);
570 EXPECT_TRUE(replyi2.isValid());
571 ret = replyi2.readInt32(&id);
572 EXPECT_EQ(NO_ERROR, ret);
573 EXPECT_EQ(0, id);
574 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
575
576 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
577 }
578
579 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
580}
581
582TEST_F(BinderLibTest, CallBack)
583{
584 status_t ret;
585 Parcel data, reply;
586 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
587 data.writeStrongBinder(callBack);
588 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
589 EXPECT_EQ(NO_ERROR, ret);
590 ret = callBack->waitEvent(5);
591 EXPECT_EQ(NO_ERROR, ret);
592 ret = callBack->getResult();
593 EXPECT_EQ(NO_ERROR, ret);
594}
595
596TEST_F(BinderLibTest, AddServer)
597{
598 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700599 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800600}
601
Riley Andrews06b01ad2014-12-18 12:10:08 -0800602TEST_F(BinderLibTest, DeathNotificationStrongRef)
603{
604 status_t ret;
605 sp<IBinder> sbinder;
606
607 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
608
609 {
610 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700611 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800612 ret = binder->linkToDeath(testDeathRecipient);
613 EXPECT_EQ(NO_ERROR, ret);
614 sbinder = binder;
615 }
616 {
617 Parcel data, reply;
618 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
619 EXPECT_EQ(0, ret);
620 }
621 IPCThreadState::self()->flushCommands();
622 ret = testDeathRecipient->waitEvent(5);
623 EXPECT_EQ(NO_ERROR, ret);
624 ret = sbinder->unlinkToDeath(testDeathRecipient);
625 EXPECT_EQ(DEAD_OBJECT, ret);
626}
627
628TEST_F(BinderLibTest, DeathNotificationMultiple)
629{
630 status_t ret;
631 const int clientcount = 2;
632 sp<IBinder> target;
633 sp<IBinder> linkedclient[clientcount];
634 sp<BinderLibTestCallBack> callBack[clientcount];
635 sp<IBinder> passiveclient[clientcount];
636
637 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700638 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800639 for (int i = 0; i < clientcount; i++) {
640 {
641 Parcel data, reply;
642
643 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700644 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800645 callBack[i] = new BinderLibTestCallBack();
646 data.writeStrongBinder(target);
647 data.writeStrongBinder(callBack[i]);
648 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
649 EXPECT_EQ(NO_ERROR, ret);
650 }
651 {
652 Parcel data, reply;
653
654 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700655 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800656 data.writeStrongBinder(target);
657 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
658 EXPECT_EQ(NO_ERROR, ret);
659 }
660 }
661 {
662 Parcel data, reply;
663 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
664 EXPECT_EQ(0, ret);
665 }
666
667 for (int i = 0; i < clientcount; i++) {
668 ret = callBack[i]->waitEvent(5);
669 EXPECT_EQ(NO_ERROR, ret);
670 ret = callBack[i]->getResult();
671 EXPECT_EQ(NO_ERROR, ret);
672 }
673}
674
Martijn Coenenf7100e42017-07-31 12:14:09 +0200675TEST_F(BinderLibTest, DeathNotificationThread)
676{
677 status_t ret;
678 sp<BinderLibTestCallBack> callback;
679 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700680 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200681 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700682 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200683
684 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
685
686 ret = target->linkToDeath(testDeathRecipient);
687 EXPECT_EQ(NO_ERROR, ret);
688
689 {
690 Parcel data, reply;
691 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
692 EXPECT_EQ(0, ret);
693 }
694
695 /* Make sure it's dead */
696 testDeathRecipient->waitEvent(5);
697
698 /* Now, pass the ref to another process and ask that process to
699 * call linkToDeath() on it, and wait for a response. This tests
700 * two things:
701 * 1) You still get death notifications when calling linkToDeath()
702 * on a ref that is already dead when it was passed to you.
703 * 2) That death notifications are not directly pushed to the thread
704 * registering them, but to the threadpool (proc workqueue) instead.
705 *
706 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
707 * is blocked on a condition variable waiting for the death notification to be
708 * called; therefore, that thread is not available for handling proc work.
709 * So, if the death notification was pushed to the thread workqueue, the callback
710 * would never be called, and the test would timeout and fail.
711 *
712 * Note that we can't do this part of the test from this thread itself, because
713 * the binder driver would only push death notifications to the thread if
714 * it is a looper thread, which this thread is not.
715 *
716 * See b/23525545 for details.
717 */
718 {
719 Parcel data, reply;
720
721 callback = new BinderLibTestCallBack();
722 data.writeStrongBinder(target);
723 data.writeStrongBinder(callback);
724 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
725 EXPECT_EQ(NO_ERROR, ret);
726 }
727
728 ret = callback->waitEvent(5);
729 EXPECT_EQ(NO_ERROR, ret);
730 ret = callback->getResult();
731 EXPECT_EQ(NO_ERROR, ret);
732}
733
Riley Andrews06b01ad2014-12-18 12:10:08 -0800734TEST_F(BinderLibTest, PassFile) {
735 int ret;
736 int pipefd[2];
737 uint8_t buf[1] = { 0 };
738 uint8_t write_value = 123;
739
740 ret = pipe2(pipefd, O_NONBLOCK);
741 ASSERT_EQ(0, ret);
742
743 {
744 Parcel data, reply;
745 uint8_t writebuf[1] = { write_value };
746
747 ret = data.writeFileDescriptor(pipefd[1], true);
748 EXPECT_EQ(NO_ERROR, ret);
749
750 ret = data.writeInt32(sizeof(writebuf));
751 EXPECT_EQ(NO_ERROR, ret);
752
753 ret = data.write(writebuf, sizeof(writebuf));
754 EXPECT_EQ(NO_ERROR, ret);
755
756 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
757 EXPECT_EQ(NO_ERROR, ret);
758 }
759
760 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700761 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800762 EXPECT_EQ(write_value, buf[0]);
763
764 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
765
766 ret = read(pipefd[0], buf, sizeof(buf));
767 EXPECT_EQ(0, ret);
768
769 close(pipefd[0]);
770}
771
Ryo Hashimotobf551892018-05-31 16:58:35 +0900772TEST_F(BinderLibTest, PassParcelFileDescriptor) {
773 const int datasize = 123;
774 std::vector<uint8_t> writebuf(datasize);
775 for (size_t i = 0; i < writebuf.size(); ++i) {
776 writebuf[i] = i;
777 }
778
779 android::base::unique_fd read_end, write_end;
780 {
781 int pipefd[2];
782 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
783 read_end.reset(pipefd[0]);
784 write_end.reset(pipefd[1]);
785 }
786 {
787 Parcel data;
788 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
789 write_end.reset();
790 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
791 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
792
793 Parcel reply;
794 EXPECT_EQ(NO_ERROR,
795 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
796 &reply));
797 }
798 std::vector<uint8_t> readbuf(datasize);
799 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
800 EXPECT_EQ(writebuf, readbuf);
801
802 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
803
804 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
805}
806
Riley Andrews06b01ad2014-12-18 12:10:08 -0800807TEST_F(BinderLibTest, PromoteLocal) {
808 sp<IBinder> strong = new BBinder();
809 wp<IBinder> weak = strong;
810 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700811 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800812 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700813 strong = nullptr;
814 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800815 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700816 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800817}
818
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700819TEST_F(BinderLibTest, LocalGetExtension) {
820 sp<BBinder> binder = new BBinder();
821 sp<IBinder> ext = new BBinder();
822 binder->setExtension(ext);
823 EXPECT_EQ(ext, binder->getExtension());
824}
825
826TEST_F(BinderLibTest, RemoteGetExtension) {
827 sp<IBinder> server = addServer();
828 ASSERT_TRUE(server != nullptr);
829
830 sp<IBinder> extension;
831 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
832 ASSERT_NE(nullptr, extension.get());
833
834 EXPECT_EQ(NO_ERROR, extension->pingBinder());
835}
836
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700837TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
838 status_t ret;
839 Parcel data, reply;
840
841 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
842 EXPECT_EQ(NO_ERROR, ret);
843
844 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700845 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800846 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
847 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
848 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
849 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700850}
851
Connor O'Brien52be2c92016-09-20 14:18:08 -0700852TEST_F(BinderLibTest, FreedBinder) {
853 status_t ret;
854
855 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700856 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700857
858 __u32 freedHandle;
859 wp<IBinder> keepFreedBinder;
860 {
861 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700862 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
863 ASSERT_EQ(NO_ERROR, ret);
864 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
865 freedHandle = freed->handle;
866 /* Add a weak ref to the freed binder so the driver does not
867 * delete its reference to it - otherwise the transaction
868 * fails regardless of whether the driver is fixed.
869 */
Steven Morelande171d622019-07-17 16:06:01 -0700870 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700871 }
Steven Morelande171d622019-07-17 16:06:01 -0700872 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700873 {
874 Parcel data, reply;
875 data.writeStrongBinder(server);
876 /* Replace original handle with handle to the freed binder */
877 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
878 __u32 oldHandle = strong->handle;
879 strong->handle = freedHandle;
880 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
881 /* Returns DEAD_OBJECT (-32) if target crashes and
882 * FAILED_TRANSACTION if the driver rejects the invalid
883 * object.
884 */
885 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
886 /* Restore original handle so parcel destructor does not use
887 * the wrong handle.
888 */
889 strong->handle = oldHandle;
890 }
891}
892
Sherry Yang336cdd32017-07-24 14:12:27 -0700893TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
894 status_t ret;
895 Parcel data, reply;
896 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
897 for (int i = 0; i < 2; i++) {
898 BinderLibTestBundle datai;
899 datai.appendFrom(&data, 0, data.dataSize());
900
901 data.freeData();
902 data.writeInt32(1);
903 data.writeStrongBinder(callBack);
904 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
905
906 datai.appendTo(&data);
907 }
908 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
909 EXPECT_EQ(NO_ERROR, ret);
910}
911
Martijn Coenen45b07b42017-08-09 12:07:45 +0200912TEST_F(BinderLibTest, OnewayQueueing)
913{
914 status_t ret;
915 Parcel data, data2;
916
917 sp<IBinder> pollServer = addPollServer();
918
919 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
920 data.writeStrongBinder(callBack);
921 data.writeInt32(500000); // delay in us before calling back
922
923 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
924 data2.writeStrongBinder(callBack2);
925 data2.writeInt32(0); // delay in us
926
Yi Kong91635562018-06-07 14:38:36 -0700927 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200928 EXPECT_EQ(NO_ERROR, ret);
929
930 // The delay ensures that this second transaction will end up on the async_todo list
931 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700932 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200933 EXPECT_EQ(NO_ERROR, ret);
934
935 // The server will ensure that the two transactions are handled in the expected order;
936 // If the ordering is not as expected, an error will be returned through the callbacks.
937 ret = callBack->waitEvent(2);
938 EXPECT_EQ(NO_ERROR, ret);
939 ret = callBack->getResult();
940 EXPECT_EQ(NO_ERROR, ret);
941
942 ret = callBack2->waitEvent(2);
943 EXPECT_EQ(NO_ERROR, ret);
944 ret = callBack2->getResult();
945 EXPECT_EQ(NO_ERROR, ret);
946}
947
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100948TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
949{
950 status_t ret;
951 Parcel data, reply;
952 data.writeInterfaceToken(binderLibTestServiceName);
953 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
954 EXPECT_EQ(-1, reply.readInt32());
955 EXPECT_EQ(NO_ERROR, ret);
956}
957
958TEST_F(BinderLibTest, WorkSourceSet)
959{
960 status_t ret;
961 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000962 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000963 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100964 data.writeInterfaceToken(binderLibTestServiceName);
965 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
966 EXPECT_EQ(100, reply.readInt32());
967 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000968 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
969 EXPECT_EQ(NO_ERROR, ret);
970}
971
972TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
973{
974 status_t ret;
975 Parcel data, reply;
976
977 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
978 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
979
980 data.writeInterfaceToken(binderLibTestServiceName);
981 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
982 EXPECT_EQ(-1, reply.readInt32());
983 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100984 EXPECT_EQ(NO_ERROR, ret);
985}
986
987TEST_F(BinderLibTest, WorkSourceCleared)
988{
989 status_t ret;
990 Parcel data, reply;
991
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000992 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000993 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
994 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100995 data.writeInterfaceToken(binderLibTestServiceName);
996 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
997
998 EXPECT_EQ(-1, reply.readInt32());
999 EXPECT_EQ(100, previousWorkSource);
1000 EXPECT_EQ(NO_ERROR, ret);
1001}
1002
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001003TEST_F(BinderLibTest, WorkSourceRestored)
1004{
1005 status_t ret;
1006 Parcel data, reply;
1007
1008 IPCThreadState::self()->setCallingWorkSourceUid(100);
1009 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1010 IPCThreadState::self()->restoreCallingWorkSource(token);
1011
1012 data.writeInterfaceToken(binderLibTestServiceName);
1013 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1014
1015 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001016 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001017 EXPECT_EQ(NO_ERROR, ret);
1018}
1019
Olivier Gaillard91a04802018-11-14 17:32:41 +00001020TEST_F(BinderLibTest, PropagateFlagSet)
1021{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001022 IPCThreadState::self()->clearPropagateWorkSource();
1023 IPCThreadState::self()->setCallingWorkSourceUid(100);
1024 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1025}
1026
1027TEST_F(BinderLibTest, PropagateFlagCleared)
1028{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001029 IPCThreadState::self()->setCallingWorkSourceUid(100);
1030 IPCThreadState::self()->clearPropagateWorkSource();
1031 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1032}
1033
1034TEST_F(BinderLibTest, PropagateFlagRestored)
1035{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001036 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1037 IPCThreadState::self()->restoreCallingWorkSource(token);
1038
1039 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1040}
1041
1042TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1043{
1044 IPCThreadState::self()->setCallingWorkSourceUid(100);
1045
1046 Parcel data, reply;
1047 status_t ret;
1048 data.writeInterfaceToken(binderLibTestServiceName);
1049 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1050
1051 Parcel data2, reply2;
1052 status_t ret2;
1053 data2.writeInterfaceToken(binderLibTestServiceName);
1054 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1055 EXPECT_EQ(100, reply2.readInt32());
1056 EXPECT_EQ(NO_ERROR, ret2);
1057}
1058
Steven Morelandbf1915b2020-07-16 22:43:02 +00001059TEST_F(BinderLibTest, SchedPolicySet) {
1060 sp<IBinder> server = addServer();
1061 ASSERT_TRUE(server != nullptr);
1062
1063 Parcel data, reply;
1064 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1065 EXPECT_EQ(NO_ERROR, ret);
1066
1067 int policy = reply.readInt32();
1068 int priority = reply.readInt32();
1069
1070 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1071 EXPECT_EQ(kSchedPriority, priority);
1072}
1073
1074
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001075TEST_F(BinderLibTest, VectorSent) {
1076 Parcel data, reply;
1077 sp<IBinder> server = addServer();
1078 ASSERT_TRUE(server != nullptr);
1079
1080 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1081 data.writeUint64Vector(testValue);
1082
1083 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1084 EXPECT_EQ(NO_ERROR, ret);
1085 std::vector<uint64_t> readValue;
1086 ret = reply.readUint64Vector(&readValue);
1087 EXPECT_EQ(readValue, testValue);
1088}
1089
Martijn Coenen82c75312019-07-24 15:18:30 +02001090TEST_F(BinderLibTest, BufRejected) {
1091 Parcel data, reply;
1092 uint32_t buf;
1093 sp<IBinder> server = addServer();
1094 ASSERT_TRUE(server != nullptr);
1095
1096 binder_buffer_object obj {
1097 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001098 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001099 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1100 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001101 };
1102 data.setDataCapacity(1024);
1103 // Write a bogus object at offset 0 to get an entry in the offset table
1104 data.writeFileDescriptor(0);
1105 EXPECT_EQ(data.objectsCount(), 1);
1106 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1107 // And now, overwrite it with the buffer object
1108 memcpy(parcelData, &obj, sizeof(obj));
1109 data.setDataSize(sizeof(obj));
1110
1111 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1112 // Either the kernel should reject this transaction (if it's correct), but
1113 // if it's not, the server implementation should return an error if it
1114 // finds an object in the received Parcel.
1115 EXPECT_NE(NO_ERROR, ret);
1116}
1117
Riley Andrews06b01ad2014-12-18 12:10:08 -08001118class BinderLibTestService : public BBinder
1119{
1120 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001121 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001122 : m_id(id)
1123 , m_nextServerId(id + 1)
1124 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001125 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001126 {
Yi Kong91635562018-06-07 14:38:36 -07001127 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1128 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001129 }
1130 ~BinderLibTestService()
1131 {
1132 exit(EXIT_SUCCESS);
1133 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001134
1135 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001136 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001137 Parcel data;
1138 data.writeInt32(NO_ERROR);
1139 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001140 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001141 }
1142 }
1143
Riley Andrews06b01ad2014-12-18 12:10:08 -08001144 virtual status_t onTransact(uint32_t code,
1145 const Parcel& data, Parcel* reply,
1146 uint32_t flags = 0) {
1147 //printf("%s: code %d\n", __func__, code);
1148 (void)flags;
1149
1150 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1151 return PERMISSION_DENIED;
1152 }
1153 switch (code) {
1154 case BINDER_LIB_TEST_REGISTER_SERVER: {
1155 int32_t id;
1156 sp<IBinder> binder;
1157 id = data.readInt32();
1158 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001159 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001160 return BAD_VALUE;
1161 }
1162
1163 if (m_id != 0)
1164 return INVALID_OPERATION;
1165
1166 pthread_mutex_lock(&m_serverWaitMutex);
1167 if (m_serverStartRequested) {
1168 m_serverStartRequested = false;
1169 m_serverStarted = binder;
1170 pthread_cond_signal(&m_serverWaitCond);
1171 }
1172 pthread_mutex_unlock(&m_serverWaitMutex);
1173 return NO_ERROR;
1174 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001175 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001176 case BINDER_LIB_TEST_ADD_SERVER: {
1177 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001178 int serverid;
1179
1180 if (m_id != 0) {
1181 return INVALID_OPERATION;
1182 }
1183 pthread_mutex_lock(&m_serverWaitMutex);
1184 if (m_serverStartRequested) {
1185 ret = -EBUSY;
1186 } else {
1187 serverid = m_nextServerId++;
1188 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001189 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001190
1191 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001192 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001193 pthread_mutex_lock(&m_serverWaitMutex);
1194 }
1195 if (ret > 0) {
1196 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001197 struct timespec ts;
1198 clock_gettime(CLOCK_REALTIME, &ts);
1199 ts.tv_sec += 5;
1200 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001201 }
1202 if (m_serverStartRequested) {
1203 m_serverStartRequested = false;
1204 ret = -ETIMEDOUT;
1205 } else {
1206 reply->writeStrongBinder(m_serverStarted);
1207 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001208 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001209 ret = NO_ERROR;
1210 }
1211 } else if (ret >= 0) {
1212 m_serverStartRequested = false;
1213 ret = UNKNOWN_ERROR;
1214 }
1215 pthread_mutex_unlock(&m_serverWaitMutex);
1216 return ret;
1217 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001218 case BINDER_LIB_TEST_GETPID:
1219 reply->writeInt32(getpid());
1220 return NO_ERROR;
1221 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1222 usleep(5000);
1223 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001224 case BINDER_LIB_TEST_NOP_TRANSACTION:
1225 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001226 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1227 // Note: this transaction is only designed for use with a
1228 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001229 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001230 // A callback was already pending; this means that
1231 // we received a second call while still processing
1232 // the first one. Fail the test.
1233 sp<IBinder> callback = data.readStrongBinder();
1234 Parcel data2;
1235 data2.writeInt32(UNKNOWN_ERROR);
1236
Yi Kong91635562018-06-07 14:38:36 -07001237 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001238 } else {
1239 m_callback = data.readStrongBinder();
1240 int32_t delayUs = data.readInt32();
1241 /*
1242 * It's necessary that we sleep here, so the next
1243 * transaction the caller makes will be queued to
1244 * the async queue.
1245 */
1246 usleep(delayUs);
1247
1248 /*
1249 * Now when we return, libbinder will tell the kernel
1250 * we are done with this transaction, and the kernel
1251 * can move the queued transaction to either the
1252 * thread todo worklist (for kernels without the fix),
1253 * or the proc todo worklist. In case of the former,
1254 * the next outbound call will pick up the pending
1255 * transaction, which leads to undesired reentrant
1256 * behavior. This is caught in the if() branch above.
1257 */
1258 }
1259
1260 return NO_ERROR;
1261 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001262 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1263 Parcel data2, reply2;
1264 sp<IBinder> binder;
1265 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001266 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001267 return BAD_VALUE;
1268 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001269 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001270 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1271 return NO_ERROR;
1272 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001273 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1274 reply->writeStrongBinder(this);
1275 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1277 reply->writeInt32(m_id);
1278 return NO_ERROR;
1279 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1280 int32_t count;
1281 uint32_t indirect_code;
1282 sp<IBinder> binder;
1283
1284 count = data.readInt32();
1285 reply->writeInt32(m_id);
1286 reply->writeInt32(count);
1287 for (int i = 0; i < count; i++) {
1288 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001289 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001290 return BAD_VALUE;
1291 }
1292 indirect_code = data.readInt32();
1293 BinderLibTestBundle data2(&data);
1294 if (!data2.isValid()) {
1295 return BAD_VALUE;
1296 }
1297 BinderLibTestBundle reply2;
1298 binder->transact(indirect_code, data2, &reply2);
1299 reply2.appendTo(reply);
1300 }
1301 return NO_ERROR;
1302 }
1303 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1304 reply->setError(data.readInt32());
1305 return NO_ERROR;
1306 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1307 reply->writeInt32(sizeof(void *));
1308 return NO_ERROR;
1309 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1310 return NO_ERROR;
1311 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1312 m_strongRef = data.readStrongBinder();
1313 return NO_ERROR;
1314 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1315 int ret;
1316 Parcel data2, reply2;
1317 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1318 sp<IBinder> target;
1319 sp<IBinder> callback;
1320
1321 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001322 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001323 return BAD_VALUE;
1324 }
1325 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001326 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001327 return BAD_VALUE;
1328 }
1329 ret = target->linkToDeath(testDeathRecipient);
1330 if (ret == NO_ERROR)
1331 ret = testDeathRecipient->waitEvent(5);
1332 data2.writeInt32(ret);
1333 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1334 return NO_ERROR;
1335 }
1336 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1337 int ret;
1338 int32_t size;
1339 const void *buf;
1340 int fd;
1341
1342 fd = data.readFileDescriptor();
1343 if (fd < 0) {
1344 return BAD_VALUE;
1345 }
1346 ret = data.readInt32(&size);
1347 if (ret != NO_ERROR) {
1348 return ret;
1349 }
1350 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001351 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001352 return BAD_VALUE;
1353 }
1354 ret = write(fd, buf, size);
1355 if (ret != size)
1356 return UNKNOWN_ERROR;
1357 return NO_ERROR;
1358 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001359 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1360 int ret;
1361 int32_t size;
1362 const void *buf;
1363 android::base::unique_fd fd;
1364
1365 ret = data.readUniqueParcelFileDescriptor(&fd);
1366 if (ret != NO_ERROR) {
1367 return ret;
1368 }
1369 ret = data.readInt32(&size);
1370 if (ret != NO_ERROR) {
1371 return ret;
1372 }
1373 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001374 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001375 return BAD_VALUE;
1376 }
1377 ret = write(fd.get(), buf, size);
1378 if (ret != size) return UNKNOWN_ERROR;
1379 return NO_ERROR;
1380 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001381 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1382 alarm(10);
1383 return NO_ERROR;
1384 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001385 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001386 ;
1387 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001388 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001389 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001390 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001391 return NO_ERROR;
1392 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001393 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1394 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001395 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001396 return NO_ERROR;
1397 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001398 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1399 int policy = 0;
1400 sched_param param;
1401 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1402 return UNKNOWN_ERROR;
1403 }
1404 reply->writeInt32(policy);
1405 reply->writeInt32(param.sched_priority);
1406 return NO_ERROR;
1407 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001408 case BINDER_LIB_TEST_ECHO_VECTOR: {
1409 std::vector<uint64_t> vector;
1410 auto err = data.readUint64Vector(&vector);
1411 if (err != NO_ERROR)
1412 return err;
1413 reply->writeUint64Vector(vector);
1414 return NO_ERROR;
1415 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001416 case BINDER_LIB_TEST_REJECT_BUF: {
1417 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1418 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001419 default:
1420 return UNKNOWN_TRANSACTION;
1421 };
1422 }
1423 private:
1424 int32_t m_id;
1425 int32_t m_nextServerId;
1426 pthread_mutex_t m_serverWaitMutex;
1427 pthread_cond_t m_serverWaitCond;
1428 bool m_serverStartRequested;
1429 sp<IBinder> m_serverStarted;
1430 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001431 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001432};
1433
Martijn Coenen45b07b42017-08-09 12:07:45 +02001434int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001435{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001436 binderLibTestServiceName += String16(binderserversuffix);
1437
Riley Andrews06b01ad2014-12-18 12:10:08 -08001438 status_t ret;
1439 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001440 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001441 {
1442 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001443
Steven Morelandbf1915b2020-07-16 22:43:02 +00001444 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1445
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001446 /*
1447 * Normally would also contain functionality as well, but we are only
1448 * testing the extension mechanism.
1449 */
1450 testService->setExtension(new BBinder());
1451
Martijn Coenen82c75312019-07-24 15:18:30 +02001452 // Required for test "BufRejected'
1453 testService->setRequestingSid(true);
1454
Martijn Coenen45b07b42017-08-09 12:07:45 +02001455 /*
1456 * We need this below, but can't hold a sp<> because it prevents the
1457 * node from being cleaned up automatically. It's safe in this case
1458 * because of how the tests are written.
1459 */
1460 testServicePtr = testService.get();
1461
Riley Andrews06b01ad2014-12-18 12:10:08 -08001462 if (index == 0) {
1463 ret = sm->addService(binderLibTestServiceName, testService);
1464 } else {
1465 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1466 Parcel data, reply;
1467 data.writeInt32(index);
1468 data.writeStrongBinder(testService);
1469
1470 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1471 }
1472 }
1473 write(readypipefd, &ret, sizeof(ret));
1474 close(readypipefd);
1475 //printf("%s: ret %d\n", __func__, ret);
1476 if (ret)
1477 return 1;
1478 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001479 if (usePoll) {
1480 int fd;
1481 struct epoll_event ev;
1482 int epoll_fd;
1483 IPCThreadState::self()->setupPolling(&fd);
1484 if (fd < 0) {
1485 return 1;
1486 }
1487 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1488
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001489 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001490 if (epoll_fd == -1) {
1491 return 1;
1492 }
1493
1494 ev.events = EPOLLIN;
1495 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1496 return 1;
1497 }
1498
1499 while (1) {
1500 /*
1501 * We simulate a single-threaded process using the binder poll
1502 * interface; besides handling binder commands, it can also
1503 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001504 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001505 *
1506 * processPendingCall() will then issue that transaction.
1507 */
1508 struct epoll_event events[1];
1509 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1510 if (numEvents < 0) {
1511 if (errno == EINTR) {
1512 continue;
1513 }
1514 return 1;
1515 }
1516 if (numEvents > 0) {
1517 IPCThreadState::self()->handlePolledCommands();
1518 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1519 testServicePtr->processPendingCall();
1520 }
1521 }
1522 } else {
1523 ProcessState::self()->startThreadPool();
1524 IPCThreadState::self()->joinThreadPool();
1525 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001526 //printf("%s: joinThreadPool returned\n", __func__);
1527 return 1; /* joinThreadPool should not return */
1528}
1529
1530int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001531 ExitIfWrongAbi();
1532
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001533 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001534 binderservername = argv[2];
1535 } else {
1536 binderservername = argv[0];
1537 }
1538
Martijn Coenen45b07b42017-08-09 12:07:45 +02001539 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1540 binderserversuffix = argv[5];
1541 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001542 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001543 binderserversuffix = new char[16];
1544 snprintf(binderserversuffix, 16, "%d", getpid());
1545 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001546
1547 ::testing::InitGoogleTest(&argc, argv);
1548 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1549 ProcessState::self()->startThreadPool();
1550 return RUN_ALL_TESTS();
1551}