blob: 98f0868bcab0391d4a7c06ff50d93f731c7ecdf0 [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));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700435
436 bool sync_received, async_received;
437
438 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
439 &async_received));
440
441 EXPECT_EQ(sync_received, 1);
442 EXPECT_EQ(async_received, 0);
443
Marco Ballesio7ee17572020-09-08 10:30:03 -0700444 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
445 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
446}
447
Riley Andrews06b01ad2014-12-18 12:10:08 -0800448TEST_F(BinderLibTest, SetError) {
449 int32_t testValue[] = { 0, -123, 123 };
450 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
451 status_t ret;
452 Parcel data, reply;
453 data.writeInt32(testValue[i]);
454 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
455 EXPECT_EQ(testValue[i], ret);
456 }
457}
458
459TEST_F(BinderLibTest, GetId) {
460 status_t ret;
461 int32_t id;
462 Parcel data, reply;
463 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
464 EXPECT_EQ(NO_ERROR, ret);
465 ret = reply.readInt32(&id);
466 EXPECT_EQ(NO_ERROR, ret);
467 EXPECT_EQ(0, id);
468}
469
470TEST_F(BinderLibTest, PtrSize) {
471 status_t ret;
472 int32_t ptrsize;
473 Parcel data, reply;
474 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700475 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800476 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
477 EXPECT_EQ(NO_ERROR, ret);
478 ret = reply.readInt32(&ptrsize);
479 EXPECT_EQ(NO_ERROR, ret);
480 RecordProperty("TestPtrSize", sizeof(void *));
481 RecordProperty("ServerPtrSize", sizeof(void *));
482}
483
484TEST_F(BinderLibTest, IndirectGetId2)
485{
486 status_t ret;
487 int32_t id;
488 int32_t count;
489 Parcel data, reply;
490 int32_t serverId[3];
491
492 data.writeInt32(ARRAY_SIZE(serverId));
493 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
494 sp<IBinder> server;
495 BinderLibTestBundle datai;
496
497 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700498 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800499 data.writeStrongBinder(server);
500 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
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 BinderLibTestBundle replyi(&reply);
517 EXPECT_TRUE(replyi.isValid());
518 ret = replyi.readInt32(&id);
519 EXPECT_EQ(NO_ERROR, ret);
520 EXPECT_EQ(serverId[i], id);
521 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
522 }
523
524 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
525}
526
527TEST_F(BinderLibTest, IndirectGetId3)
528{
529 status_t ret;
530 int32_t id;
531 int32_t count;
532 Parcel data, reply;
533 int32_t serverId[3];
534
535 data.writeInt32(ARRAY_SIZE(serverId));
536 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
537 sp<IBinder> server;
538 BinderLibTestBundle datai;
539 BinderLibTestBundle datai2;
540
541 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700542 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800543 data.writeStrongBinder(server);
544 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
545
546 datai.writeInt32(1);
547 datai.writeStrongBinder(m_server);
548 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
549 datai2.appendTo(&datai);
550
551 datai.appendTo(&data);
552 }
553
554 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
555 ASSERT_EQ(NO_ERROR, ret);
556
557 ret = reply.readInt32(&id);
558 ASSERT_EQ(NO_ERROR, ret);
559 EXPECT_EQ(0, id);
560
561 ret = reply.readInt32(&count);
562 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700563 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800564
565 for (size_t i = 0; i < (size_t)count; i++) {
566 int32_t counti;
567
568 BinderLibTestBundle replyi(&reply);
569 EXPECT_TRUE(replyi.isValid());
570 ret = replyi.readInt32(&id);
571 EXPECT_EQ(NO_ERROR, ret);
572 EXPECT_EQ(serverId[i], id);
573
574 ret = replyi.readInt32(&counti);
575 ASSERT_EQ(NO_ERROR, ret);
576 EXPECT_EQ(1, counti);
577
578 BinderLibTestBundle replyi2(&replyi);
579 EXPECT_TRUE(replyi2.isValid());
580 ret = replyi2.readInt32(&id);
581 EXPECT_EQ(NO_ERROR, ret);
582 EXPECT_EQ(0, id);
583 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
584
585 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
586 }
587
588 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
589}
590
591TEST_F(BinderLibTest, CallBack)
592{
593 status_t ret;
594 Parcel data, reply;
595 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
596 data.writeStrongBinder(callBack);
597 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
598 EXPECT_EQ(NO_ERROR, ret);
599 ret = callBack->waitEvent(5);
600 EXPECT_EQ(NO_ERROR, ret);
601 ret = callBack->getResult();
602 EXPECT_EQ(NO_ERROR, ret);
603}
604
605TEST_F(BinderLibTest, AddServer)
606{
607 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700608 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800609}
610
Riley Andrews06b01ad2014-12-18 12:10:08 -0800611TEST_F(BinderLibTest, DeathNotificationStrongRef)
612{
613 status_t ret;
614 sp<IBinder> sbinder;
615
616 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
617
618 {
619 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700620 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621 ret = binder->linkToDeath(testDeathRecipient);
622 EXPECT_EQ(NO_ERROR, ret);
623 sbinder = binder;
624 }
625 {
626 Parcel data, reply;
627 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
628 EXPECT_EQ(0, ret);
629 }
630 IPCThreadState::self()->flushCommands();
631 ret = testDeathRecipient->waitEvent(5);
632 EXPECT_EQ(NO_ERROR, ret);
633 ret = sbinder->unlinkToDeath(testDeathRecipient);
634 EXPECT_EQ(DEAD_OBJECT, ret);
635}
636
637TEST_F(BinderLibTest, DeathNotificationMultiple)
638{
639 status_t ret;
640 const int clientcount = 2;
641 sp<IBinder> target;
642 sp<IBinder> linkedclient[clientcount];
643 sp<BinderLibTestCallBack> callBack[clientcount];
644 sp<IBinder> passiveclient[clientcount];
645
646 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700647 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800648 for (int i = 0; i < clientcount; i++) {
649 {
650 Parcel data, reply;
651
652 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700653 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800654 callBack[i] = new BinderLibTestCallBack();
655 data.writeStrongBinder(target);
656 data.writeStrongBinder(callBack[i]);
657 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
658 EXPECT_EQ(NO_ERROR, ret);
659 }
660 {
661 Parcel data, reply;
662
663 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700664 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800665 data.writeStrongBinder(target);
666 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
667 EXPECT_EQ(NO_ERROR, ret);
668 }
669 }
670 {
671 Parcel data, reply;
672 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
673 EXPECT_EQ(0, ret);
674 }
675
676 for (int i = 0; i < clientcount; i++) {
677 ret = callBack[i]->waitEvent(5);
678 EXPECT_EQ(NO_ERROR, ret);
679 ret = callBack[i]->getResult();
680 EXPECT_EQ(NO_ERROR, ret);
681 }
682}
683
Martijn Coenenf7100e42017-07-31 12:14:09 +0200684TEST_F(BinderLibTest, DeathNotificationThread)
685{
686 status_t ret;
687 sp<BinderLibTestCallBack> callback;
688 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700689 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200690 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700691 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200692
693 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
694
695 ret = target->linkToDeath(testDeathRecipient);
696 EXPECT_EQ(NO_ERROR, ret);
697
698 {
699 Parcel data, reply;
700 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
701 EXPECT_EQ(0, ret);
702 }
703
704 /* Make sure it's dead */
705 testDeathRecipient->waitEvent(5);
706
707 /* Now, pass the ref to another process and ask that process to
708 * call linkToDeath() on it, and wait for a response. This tests
709 * two things:
710 * 1) You still get death notifications when calling linkToDeath()
711 * on a ref that is already dead when it was passed to you.
712 * 2) That death notifications are not directly pushed to the thread
713 * registering them, but to the threadpool (proc workqueue) instead.
714 *
715 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
716 * is blocked on a condition variable waiting for the death notification to be
717 * called; therefore, that thread is not available for handling proc work.
718 * So, if the death notification was pushed to the thread workqueue, the callback
719 * would never be called, and the test would timeout and fail.
720 *
721 * Note that we can't do this part of the test from this thread itself, because
722 * the binder driver would only push death notifications to the thread if
723 * it is a looper thread, which this thread is not.
724 *
725 * See b/23525545 for details.
726 */
727 {
728 Parcel data, reply;
729
730 callback = new BinderLibTestCallBack();
731 data.writeStrongBinder(target);
732 data.writeStrongBinder(callback);
733 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
734 EXPECT_EQ(NO_ERROR, ret);
735 }
736
737 ret = callback->waitEvent(5);
738 EXPECT_EQ(NO_ERROR, ret);
739 ret = callback->getResult();
740 EXPECT_EQ(NO_ERROR, ret);
741}
742
Riley Andrews06b01ad2014-12-18 12:10:08 -0800743TEST_F(BinderLibTest, PassFile) {
744 int ret;
745 int pipefd[2];
746 uint8_t buf[1] = { 0 };
747 uint8_t write_value = 123;
748
749 ret = pipe2(pipefd, O_NONBLOCK);
750 ASSERT_EQ(0, ret);
751
752 {
753 Parcel data, reply;
754 uint8_t writebuf[1] = { write_value };
755
756 ret = data.writeFileDescriptor(pipefd[1], true);
757 EXPECT_EQ(NO_ERROR, ret);
758
759 ret = data.writeInt32(sizeof(writebuf));
760 EXPECT_EQ(NO_ERROR, ret);
761
762 ret = data.write(writebuf, sizeof(writebuf));
763 EXPECT_EQ(NO_ERROR, ret);
764
765 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
766 EXPECT_EQ(NO_ERROR, ret);
767 }
768
769 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700770 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800771 EXPECT_EQ(write_value, buf[0]);
772
773 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
774
775 ret = read(pipefd[0], buf, sizeof(buf));
776 EXPECT_EQ(0, ret);
777
778 close(pipefd[0]);
779}
780
Ryo Hashimotobf551892018-05-31 16:58:35 +0900781TEST_F(BinderLibTest, PassParcelFileDescriptor) {
782 const int datasize = 123;
783 std::vector<uint8_t> writebuf(datasize);
784 for (size_t i = 0; i < writebuf.size(); ++i) {
785 writebuf[i] = i;
786 }
787
788 android::base::unique_fd read_end, write_end;
789 {
790 int pipefd[2];
791 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
792 read_end.reset(pipefd[0]);
793 write_end.reset(pipefd[1]);
794 }
795 {
796 Parcel data;
797 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
798 write_end.reset();
799 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
800 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
801
802 Parcel reply;
803 EXPECT_EQ(NO_ERROR,
804 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
805 &reply));
806 }
807 std::vector<uint8_t> readbuf(datasize);
808 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
809 EXPECT_EQ(writebuf, readbuf);
810
811 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
812
813 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
814}
815
Riley Andrews06b01ad2014-12-18 12:10:08 -0800816TEST_F(BinderLibTest, PromoteLocal) {
817 sp<IBinder> strong = new BBinder();
818 wp<IBinder> weak = strong;
819 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700820 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800821 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700822 strong = nullptr;
823 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800824 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700825 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800826}
827
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700828TEST_F(BinderLibTest, LocalGetExtension) {
829 sp<BBinder> binder = new BBinder();
830 sp<IBinder> ext = new BBinder();
831 binder->setExtension(ext);
832 EXPECT_EQ(ext, binder->getExtension());
833}
834
835TEST_F(BinderLibTest, RemoteGetExtension) {
836 sp<IBinder> server = addServer();
837 ASSERT_TRUE(server != nullptr);
838
839 sp<IBinder> extension;
840 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
841 ASSERT_NE(nullptr, extension.get());
842
843 EXPECT_EQ(NO_ERROR, extension->pingBinder());
844}
845
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700846TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
847 status_t ret;
848 Parcel data, reply;
849
850 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
851 EXPECT_EQ(NO_ERROR, ret);
852
853 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700854 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800855 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
856 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
857 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
858 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700859}
860
Connor O'Brien52be2c92016-09-20 14:18:08 -0700861TEST_F(BinderLibTest, FreedBinder) {
862 status_t ret;
863
864 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700865 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700866
867 __u32 freedHandle;
868 wp<IBinder> keepFreedBinder;
869 {
870 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700871 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
872 ASSERT_EQ(NO_ERROR, ret);
873 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
874 freedHandle = freed->handle;
875 /* Add a weak ref to the freed binder so the driver does not
876 * delete its reference to it - otherwise the transaction
877 * fails regardless of whether the driver is fixed.
878 */
Steven Morelande171d622019-07-17 16:06:01 -0700879 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700880 }
Steven Morelande171d622019-07-17 16:06:01 -0700881 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700882 {
883 Parcel data, reply;
884 data.writeStrongBinder(server);
885 /* Replace original handle with handle to the freed binder */
886 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
887 __u32 oldHandle = strong->handle;
888 strong->handle = freedHandle;
889 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
890 /* Returns DEAD_OBJECT (-32) if target crashes and
891 * FAILED_TRANSACTION if the driver rejects the invalid
892 * object.
893 */
894 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
895 /* Restore original handle so parcel destructor does not use
896 * the wrong handle.
897 */
898 strong->handle = oldHandle;
899 }
900}
901
Sherry Yang336cdd32017-07-24 14:12:27 -0700902TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
903 status_t ret;
904 Parcel data, reply;
905 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
906 for (int i = 0; i < 2; i++) {
907 BinderLibTestBundle datai;
908 datai.appendFrom(&data, 0, data.dataSize());
909
910 data.freeData();
911 data.writeInt32(1);
912 data.writeStrongBinder(callBack);
913 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
914
915 datai.appendTo(&data);
916 }
917 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
918 EXPECT_EQ(NO_ERROR, ret);
919}
920
Martijn Coenen45b07b42017-08-09 12:07:45 +0200921TEST_F(BinderLibTest, OnewayQueueing)
922{
923 status_t ret;
924 Parcel data, data2;
925
926 sp<IBinder> pollServer = addPollServer();
927
928 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
929 data.writeStrongBinder(callBack);
930 data.writeInt32(500000); // delay in us before calling back
931
932 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
933 data2.writeStrongBinder(callBack2);
934 data2.writeInt32(0); // delay in us
935
Yi Kong91635562018-06-07 14:38:36 -0700936 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200937 EXPECT_EQ(NO_ERROR, ret);
938
939 // The delay ensures that this second transaction will end up on the async_todo list
940 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700941 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200942 EXPECT_EQ(NO_ERROR, ret);
943
944 // The server will ensure that the two transactions are handled in the expected order;
945 // If the ordering is not as expected, an error will be returned through the callbacks.
946 ret = callBack->waitEvent(2);
947 EXPECT_EQ(NO_ERROR, ret);
948 ret = callBack->getResult();
949 EXPECT_EQ(NO_ERROR, ret);
950
951 ret = callBack2->waitEvent(2);
952 EXPECT_EQ(NO_ERROR, ret);
953 ret = callBack2->getResult();
954 EXPECT_EQ(NO_ERROR, ret);
955}
956
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100957TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
958{
959 status_t ret;
960 Parcel data, reply;
961 data.writeInterfaceToken(binderLibTestServiceName);
962 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
963 EXPECT_EQ(-1, reply.readInt32());
964 EXPECT_EQ(NO_ERROR, ret);
965}
966
967TEST_F(BinderLibTest, WorkSourceSet)
968{
969 status_t ret;
970 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000971 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000972 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100973 data.writeInterfaceToken(binderLibTestServiceName);
974 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
975 EXPECT_EQ(100, reply.readInt32());
976 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000977 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
978 EXPECT_EQ(NO_ERROR, ret);
979}
980
981TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
982{
983 status_t ret;
984 Parcel data, reply;
985
986 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
987 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
988
989 data.writeInterfaceToken(binderLibTestServiceName);
990 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
991 EXPECT_EQ(-1, reply.readInt32());
992 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100993 EXPECT_EQ(NO_ERROR, ret);
994}
995
996TEST_F(BinderLibTest, WorkSourceCleared)
997{
998 status_t ret;
999 Parcel data, reply;
1000
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001001 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001002 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1003 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001004 data.writeInterfaceToken(binderLibTestServiceName);
1005 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1006
1007 EXPECT_EQ(-1, reply.readInt32());
1008 EXPECT_EQ(100, previousWorkSource);
1009 EXPECT_EQ(NO_ERROR, ret);
1010}
1011
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001012TEST_F(BinderLibTest, WorkSourceRestored)
1013{
1014 status_t ret;
1015 Parcel data, reply;
1016
1017 IPCThreadState::self()->setCallingWorkSourceUid(100);
1018 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1019 IPCThreadState::self()->restoreCallingWorkSource(token);
1020
1021 data.writeInterfaceToken(binderLibTestServiceName);
1022 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1023
1024 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001025 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001026 EXPECT_EQ(NO_ERROR, ret);
1027}
1028
Olivier Gaillard91a04802018-11-14 17:32:41 +00001029TEST_F(BinderLibTest, PropagateFlagSet)
1030{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001031 IPCThreadState::self()->clearPropagateWorkSource();
1032 IPCThreadState::self()->setCallingWorkSourceUid(100);
1033 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1034}
1035
1036TEST_F(BinderLibTest, PropagateFlagCleared)
1037{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001038 IPCThreadState::self()->setCallingWorkSourceUid(100);
1039 IPCThreadState::self()->clearPropagateWorkSource();
1040 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1041}
1042
1043TEST_F(BinderLibTest, PropagateFlagRestored)
1044{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001045 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1046 IPCThreadState::self()->restoreCallingWorkSource(token);
1047
1048 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1049}
1050
1051TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1052{
1053 IPCThreadState::self()->setCallingWorkSourceUid(100);
1054
1055 Parcel data, reply;
1056 status_t ret;
1057 data.writeInterfaceToken(binderLibTestServiceName);
1058 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1059
1060 Parcel data2, reply2;
1061 status_t ret2;
1062 data2.writeInterfaceToken(binderLibTestServiceName);
1063 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1064 EXPECT_EQ(100, reply2.readInt32());
1065 EXPECT_EQ(NO_ERROR, ret2);
1066}
1067
Steven Morelandbf1915b2020-07-16 22:43:02 +00001068TEST_F(BinderLibTest, SchedPolicySet) {
1069 sp<IBinder> server = addServer();
1070 ASSERT_TRUE(server != nullptr);
1071
1072 Parcel data, reply;
1073 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1074 EXPECT_EQ(NO_ERROR, ret);
1075
1076 int policy = reply.readInt32();
1077 int priority = reply.readInt32();
1078
1079 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1080 EXPECT_EQ(kSchedPriority, priority);
1081}
1082
1083
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001084TEST_F(BinderLibTest, VectorSent) {
1085 Parcel data, reply;
1086 sp<IBinder> server = addServer();
1087 ASSERT_TRUE(server != nullptr);
1088
1089 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1090 data.writeUint64Vector(testValue);
1091
1092 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1093 EXPECT_EQ(NO_ERROR, ret);
1094 std::vector<uint64_t> readValue;
1095 ret = reply.readUint64Vector(&readValue);
1096 EXPECT_EQ(readValue, testValue);
1097}
1098
Martijn Coenen82c75312019-07-24 15:18:30 +02001099TEST_F(BinderLibTest, BufRejected) {
1100 Parcel data, reply;
1101 uint32_t buf;
1102 sp<IBinder> server = addServer();
1103 ASSERT_TRUE(server != nullptr);
1104
1105 binder_buffer_object obj {
1106 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001107 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001108 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1109 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001110 };
1111 data.setDataCapacity(1024);
1112 // Write a bogus object at offset 0 to get an entry in the offset table
1113 data.writeFileDescriptor(0);
1114 EXPECT_EQ(data.objectsCount(), 1);
1115 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1116 // And now, overwrite it with the buffer object
1117 memcpy(parcelData, &obj, sizeof(obj));
1118 data.setDataSize(sizeof(obj));
1119
1120 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1121 // Either the kernel should reject this transaction (if it's correct), but
1122 // if it's not, the server implementation should return an error if it
1123 // finds an object in the received Parcel.
1124 EXPECT_NE(NO_ERROR, ret);
1125}
1126
Riley Andrews06b01ad2014-12-18 12:10:08 -08001127class BinderLibTestService : public BBinder
1128{
1129 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001130 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001131 : m_id(id)
1132 , m_nextServerId(id + 1)
1133 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001134 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001135 {
Yi Kong91635562018-06-07 14:38:36 -07001136 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1137 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001138 }
1139 ~BinderLibTestService()
1140 {
1141 exit(EXIT_SUCCESS);
1142 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001143
1144 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001145 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001146 Parcel data;
1147 data.writeInt32(NO_ERROR);
1148 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001149 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001150 }
1151 }
1152
Riley Andrews06b01ad2014-12-18 12:10:08 -08001153 virtual status_t onTransact(uint32_t code,
1154 const Parcel& data, Parcel* reply,
1155 uint32_t flags = 0) {
1156 //printf("%s: code %d\n", __func__, code);
1157 (void)flags;
1158
1159 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1160 return PERMISSION_DENIED;
1161 }
1162 switch (code) {
1163 case BINDER_LIB_TEST_REGISTER_SERVER: {
1164 int32_t id;
1165 sp<IBinder> binder;
1166 id = data.readInt32();
1167 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001168 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001169 return BAD_VALUE;
1170 }
1171
1172 if (m_id != 0)
1173 return INVALID_OPERATION;
1174
1175 pthread_mutex_lock(&m_serverWaitMutex);
1176 if (m_serverStartRequested) {
1177 m_serverStartRequested = false;
1178 m_serverStarted = binder;
1179 pthread_cond_signal(&m_serverWaitCond);
1180 }
1181 pthread_mutex_unlock(&m_serverWaitMutex);
1182 return NO_ERROR;
1183 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001184 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001185 case BINDER_LIB_TEST_ADD_SERVER: {
1186 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001187 int serverid;
1188
1189 if (m_id != 0) {
1190 return INVALID_OPERATION;
1191 }
1192 pthread_mutex_lock(&m_serverWaitMutex);
1193 if (m_serverStartRequested) {
1194 ret = -EBUSY;
1195 } else {
1196 serverid = m_nextServerId++;
1197 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001198 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199
1200 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001201 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 pthread_mutex_lock(&m_serverWaitMutex);
1203 }
1204 if (ret > 0) {
1205 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001206 struct timespec ts;
1207 clock_gettime(CLOCK_REALTIME, &ts);
1208 ts.tv_sec += 5;
1209 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001210 }
1211 if (m_serverStartRequested) {
1212 m_serverStartRequested = false;
1213 ret = -ETIMEDOUT;
1214 } else {
1215 reply->writeStrongBinder(m_serverStarted);
1216 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001217 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001218 ret = NO_ERROR;
1219 }
1220 } else if (ret >= 0) {
1221 m_serverStartRequested = false;
1222 ret = UNKNOWN_ERROR;
1223 }
1224 pthread_mutex_unlock(&m_serverWaitMutex);
1225 return ret;
1226 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001227 case BINDER_LIB_TEST_GETPID:
1228 reply->writeInt32(getpid());
1229 return NO_ERROR;
1230 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1231 usleep(5000);
1232 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001233 case BINDER_LIB_TEST_NOP_TRANSACTION:
1234 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001235 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1236 // Note: this transaction is only designed for use with a
1237 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001238 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001239 // A callback was already pending; this means that
1240 // we received a second call while still processing
1241 // the first one. Fail the test.
1242 sp<IBinder> callback = data.readStrongBinder();
1243 Parcel data2;
1244 data2.writeInt32(UNKNOWN_ERROR);
1245
Yi Kong91635562018-06-07 14:38:36 -07001246 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001247 } else {
1248 m_callback = data.readStrongBinder();
1249 int32_t delayUs = data.readInt32();
1250 /*
1251 * It's necessary that we sleep here, so the next
1252 * transaction the caller makes will be queued to
1253 * the async queue.
1254 */
1255 usleep(delayUs);
1256
1257 /*
1258 * Now when we return, libbinder will tell the kernel
1259 * we are done with this transaction, and the kernel
1260 * can move the queued transaction to either the
1261 * thread todo worklist (for kernels without the fix),
1262 * or the proc todo worklist. In case of the former,
1263 * the next outbound call will pick up the pending
1264 * transaction, which leads to undesired reentrant
1265 * behavior. This is caught in the if() branch above.
1266 */
1267 }
1268
1269 return NO_ERROR;
1270 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1272 Parcel data2, reply2;
1273 sp<IBinder> binder;
1274 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001275 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 return BAD_VALUE;
1277 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001278 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001279 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1280 return NO_ERROR;
1281 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001282 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1283 reply->writeStrongBinder(this);
1284 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001285 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1286 reply->writeInt32(m_id);
1287 return NO_ERROR;
1288 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1289 int32_t count;
1290 uint32_t indirect_code;
1291 sp<IBinder> binder;
1292
1293 count = data.readInt32();
1294 reply->writeInt32(m_id);
1295 reply->writeInt32(count);
1296 for (int i = 0; i < count; i++) {
1297 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001298 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001299 return BAD_VALUE;
1300 }
1301 indirect_code = data.readInt32();
1302 BinderLibTestBundle data2(&data);
1303 if (!data2.isValid()) {
1304 return BAD_VALUE;
1305 }
1306 BinderLibTestBundle reply2;
1307 binder->transact(indirect_code, data2, &reply2);
1308 reply2.appendTo(reply);
1309 }
1310 return NO_ERROR;
1311 }
1312 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1313 reply->setError(data.readInt32());
1314 return NO_ERROR;
1315 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1316 reply->writeInt32(sizeof(void *));
1317 return NO_ERROR;
1318 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1319 return NO_ERROR;
1320 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1321 m_strongRef = data.readStrongBinder();
1322 return NO_ERROR;
1323 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1324 int ret;
1325 Parcel data2, reply2;
1326 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1327 sp<IBinder> target;
1328 sp<IBinder> callback;
1329
1330 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001331 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001332 return BAD_VALUE;
1333 }
1334 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001335 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001336 return BAD_VALUE;
1337 }
1338 ret = target->linkToDeath(testDeathRecipient);
1339 if (ret == NO_ERROR)
1340 ret = testDeathRecipient->waitEvent(5);
1341 data2.writeInt32(ret);
1342 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1343 return NO_ERROR;
1344 }
1345 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1346 int ret;
1347 int32_t size;
1348 const void *buf;
1349 int fd;
1350
1351 fd = data.readFileDescriptor();
1352 if (fd < 0) {
1353 return BAD_VALUE;
1354 }
1355 ret = data.readInt32(&size);
1356 if (ret != NO_ERROR) {
1357 return ret;
1358 }
1359 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001360 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001361 return BAD_VALUE;
1362 }
1363 ret = write(fd, buf, size);
1364 if (ret != size)
1365 return UNKNOWN_ERROR;
1366 return NO_ERROR;
1367 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001368 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1369 int ret;
1370 int32_t size;
1371 const void *buf;
1372 android::base::unique_fd fd;
1373
1374 ret = data.readUniqueParcelFileDescriptor(&fd);
1375 if (ret != NO_ERROR) {
1376 return ret;
1377 }
1378 ret = data.readInt32(&size);
1379 if (ret != NO_ERROR) {
1380 return ret;
1381 }
1382 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001383 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001384 return BAD_VALUE;
1385 }
1386 ret = write(fd.get(), buf, size);
1387 if (ret != size) return UNKNOWN_ERROR;
1388 return NO_ERROR;
1389 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001390 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1391 alarm(10);
1392 return NO_ERROR;
1393 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001394 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001395 ;
1396 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001397 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001398 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001399 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001400 return NO_ERROR;
1401 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001402 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1403 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001404 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001405 return NO_ERROR;
1406 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001407 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1408 int policy = 0;
1409 sched_param param;
1410 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1411 return UNKNOWN_ERROR;
1412 }
1413 reply->writeInt32(policy);
1414 reply->writeInt32(param.sched_priority);
1415 return NO_ERROR;
1416 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001417 case BINDER_LIB_TEST_ECHO_VECTOR: {
1418 std::vector<uint64_t> vector;
1419 auto err = data.readUint64Vector(&vector);
1420 if (err != NO_ERROR)
1421 return err;
1422 reply->writeUint64Vector(vector);
1423 return NO_ERROR;
1424 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001425 case BINDER_LIB_TEST_REJECT_BUF: {
1426 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1427 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001428 default:
1429 return UNKNOWN_TRANSACTION;
1430 };
1431 }
1432 private:
1433 int32_t m_id;
1434 int32_t m_nextServerId;
1435 pthread_mutex_t m_serverWaitMutex;
1436 pthread_cond_t m_serverWaitCond;
1437 bool m_serverStartRequested;
1438 sp<IBinder> m_serverStarted;
1439 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001440 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001441};
1442
Martijn Coenen45b07b42017-08-09 12:07:45 +02001443int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001444{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001445 binderLibTestServiceName += String16(binderserversuffix);
1446
Riley Andrews06b01ad2014-12-18 12:10:08 -08001447 status_t ret;
1448 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001449 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001450 {
1451 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001452
Steven Morelandbf1915b2020-07-16 22:43:02 +00001453 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1454
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001455 /*
1456 * Normally would also contain functionality as well, but we are only
1457 * testing the extension mechanism.
1458 */
1459 testService->setExtension(new BBinder());
1460
Martijn Coenen82c75312019-07-24 15:18:30 +02001461 // Required for test "BufRejected'
1462 testService->setRequestingSid(true);
1463
Martijn Coenen45b07b42017-08-09 12:07:45 +02001464 /*
1465 * We need this below, but can't hold a sp<> because it prevents the
1466 * node from being cleaned up automatically. It's safe in this case
1467 * because of how the tests are written.
1468 */
1469 testServicePtr = testService.get();
1470
Riley Andrews06b01ad2014-12-18 12:10:08 -08001471 if (index == 0) {
1472 ret = sm->addService(binderLibTestServiceName, testService);
1473 } else {
1474 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1475 Parcel data, reply;
1476 data.writeInt32(index);
1477 data.writeStrongBinder(testService);
1478
1479 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1480 }
1481 }
1482 write(readypipefd, &ret, sizeof(ret));
1483 close(readypipefd);
1484 //printf("%s: ret %d\n", __func__, ret);
1485 if (ret)
1486 return 1;
1487 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001488 if (usePoll) {
1489 int fd;
1490 struct epoll_event ev;
1491 int epoll_fd;
1492 IPCThreadState::self()->setupPolling(&fd);
1493 if (fd < 0) {
1494 return 1;
1495 }
1496 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1497
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001498 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001499 if (epoll_fd == -1) {
1500 return 1;
1501 }
1502
1503 ev.events = EPOLLIN;
1504 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1505 return 1;
1506 }
1507
1508 while (1) {
1509 /*
1510 * We simulate a single-threaded process using the binder poll
1511 * interface; besides handling binder commands, it can also
1512 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001513 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001514 *
1515 * processPendingCall() will then issue that transaction.
1516 */
1517 struct epoll_event events[1];
1518 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1519 if (numEvents < 0) {
1520 if (errno == EINTR) {
1521 continue;
1522 }
1523 return 1;
1524 }
1525 if (numEvents > 0) {
1526 IPCThreadState::self()->handlePolledCommands();
1527 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1528 testServicePtr->processPendingCall();
1529 }
1530 }
1531 } else {
1532 ProcessState::self()->startThreadPool();
1533 IPCThreadState::self()->joinThreadPool();
1534 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001535 //printf("%s: joinThreadPool returned\n", __func__);
1536 return 1; /* joinThreadPool should not return */
1537}
1538
1539int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001540 ExitIfWrongAbi();
1541
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001542 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001543 binderservername = argv[2];
1544 } else {
1545 binderservername = argv[0];
1546 }
1547
Martijn Coenen45b07b42017-08-09 12:07:45 +02001548 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1549 binderserversuffix = argv[5];
1550 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001551 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001552 binderserversuffix = new char[16];
1553 snprintf(binderserversuffix, 16, "%d", getpid());
1554 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001555
1556 ::testing::InitGoogleTest(&argc, argv);
1557 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1558 ProcessState::self()->startThreadPool();
1559 return RUN_ALL_TESTS();
1560}