blob: c9cd1022799498417beb66311d9101b53deed275 [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
Steven Moreland9f50ea62020-09-18 01:04:15 +0000602TEST_F(BinderLibTest, AddManagerToManager) {
603 sp<IServiceManager> sm = defaultServiceManager();
604 sp<IBinder> binder = IInterface::asBinder(sm);
605 EXPECT_EQ(NO_ERROR, sm->addService(String16("binderLibTest-manager"), binder));
606}
607
Riley Andrews06b01ad2014-12-18 12:10:08 -0800608TEST_F(BinderLibTest, DeathNotificationStrongRef)
609{
610 status_t ret;
611 sp<IBinder> sbinder;
612
613 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
614
615 {
616 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700617 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800618 ret = binder->linkToDeath(testDeathRecipient);
619 EXPECT_EQ(NO_ERROR, ret);
620 sbinder = binder;
621 }
622 {
623 Parcel data, reply;
624 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
625 EXPECT_EQ(0, ret);
626 }
627 IPCThreadState::self()->flushCommands();
628 ret = testDeathRecipient->waitEvent(5);
629 EXPECT_EQ(NO_ERROR, ret);
630 ret = sbinder->unlinkToDeath(testDeathRecipient);
631 EXPECT_EQ(DEAD_OBJECT, ret);
632}
633
634TEST_F(BinderLibTest, DeathNotificationMultiple)
635{
636 status_t ret;
637 const int clientcount = 2;
638 sp<IBinder> target;
639 sp<IBinder> linkedclient[clientcount];
640 sp<BinderLibTestCallBack> callBack[clientcount];
641 sp<IBinder> passiveclient[clientcount];
642
643 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700644 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800645 for (int i = 0; i < clientcount; i++) {
646 {
647 Parcel data, reply;
648
649 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700650 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800651 callBack[i] = new BinderLibTestCallBack();
652 data.writeStrongBinder(target);
653 data.writeStrongBinder(callBack[i]);
654 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
655 EXPECT_EQ(NO_ERROR, ret);
656 }
657 {
658 Parcel data, reply;
659
660 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700661 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800662 data.writeStrongBinder(target);
663 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
664 EXPECT_EQ(NO_ERROR, ret);
665 }
666 }
667 {
668 Parcel data, reply;
669 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
670 EXPECT_EQ(0, ret);
671 }
672
673 for (int i = 0; i < clientcount; i++) {
674 ret = callBack[i]->waitEvent(5);
675 EXPECT_EQ(NO_ERROR, ret);
676 ret = callBack[i]->getResult();
677 EXPECT_EQ(NO_ERROR, ret);
678 }
679}
680
Martijn Coenenf7100e42017-07-31 12:14:09 +0200681TEST_F(BinderLibTest, DeathNotificationThread)
682{
683 status_t ret;
684 sp<BinderLibTestCallBack> callback;
685 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700686 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200687 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700688 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200689
690 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
691
692 ret = target->linkToDeath(testDeathRecipient);
693 EXPECT_EQ(NO_ERROR, ret);
694
695 {
696 Parcel data, reply;
697 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
698 EXPECT_EQ(0, ret);
699 }
700
701 /* Make sure it's dead */
702 testDeathRecipient->waitEvent(5);
703
704 /* Now, pass the ref to another process and ask that process to
705 * call linkToDeath() on it, and wait for a response. This tests
706 * two things:
707 * 1) You still get death notifications when calling linkToDeath()
708 * on a ref that is already dead when it was passed to you.
709 * 2) That death notifications are not directly pushed to the thread
710 * registering them, but to the threadpool (proc workqueue) instead.
711 *
712 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
713 * is blocked on a condition variable waiting for the death notification to be
714 * called; therefore, that thread is not available for handling proc work.
715 * So, if the death notification was pushed to the thread workqueue, the callback
716 * would never be called, and the test would timeout and fail.
717 *
718 * Note that we can't do this part of the test from this thread itself, because
719 * the binder driver would only push death notifications to the thread if
720 * it is a looper thread, which this thread is not.
721 *
722 * See b/23525545 for details.
723 */
724 {
725 Parcel data, reply;
726
727 callback = new BinderLibTestCallBack();
728 data.writeStrongBinder(target);
729 data.writeStrongBinder(callback);
730 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
731 EXPECT_EQ(NO_ERROR, ret);
732 }
733
734 ret = callback->waitEvent(5);
735 EXPECT_EQ(NO_ERROR, ret);
736 ret = callback->getResult();
737 EXPECT_EQ(NO_ERROR, ret);
738}
739
Riley Andrews06b01ad2014-12-18 12:10:08 -0800740TEST_F(BinderLibTest, PassFile) {
741 int ret;
742 int pipefd[2];
743 uint8_t buf[1] = { 0 };
744 uint8_t write_value = 123;
745
746 ret = pipe2(pipefd, O_NONBLOCK);
747 ASSERT_EQ(0, ret);
748
749 {
750 Parcel data, reply;
751 uint8_t writebuf[1] = { write_value };
752
753 ret = data.writeFileDescriptor(pipefd[1], true);
754 EXPECT_EQ(NO_ERROR, ret);
755
756 ret = data.writeInt32(sizeof(writebuf));
757 EXPECT_EQ(NO_ERROR, ret);
758
759 ret = data.write(writebuf, sizeof(writebuf));
760 EXPECT_EQ(NO_ERROR, ret);
761
762 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
763 EXPECT_EQ(NO_ERROR, ret);
764 }
765
766 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700767 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768 EXPECT_EQ(write_value, buf[0]);
769
770 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
771
772 ret = read(pipefd[0], buf, sizeof(buf));
773 EXPECT_EQ(0, ret);
774
775 close(pipefd[0]);
776}
777
Ryo Hashimotobf551892018-05-31 16:58:35 +0900778TEST_F(BinderLibTest, PassParcelFileDescriptor) {
779 const int datasize = 123;
780 std::vector<uint8_t> writebuf(datasize);
781 for (size_t i = 0; i < writebuf.size(); ++i) {
782 writebuf[i] = i;
783 }
784
785 android::base::unique_fd read_end, write_end;
786 {
787 int pipefd[2];
788 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
789 read_end.reset(pipefd[0]);
790 write_end.reset(pipefd[1]);
791 }
792 {
793 Parcel data;
794 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
795 write_end.reset();
796 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
797 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
798
799 Parcel reply;
800 EXPECT_EQ(NO_ERROR,
801 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
802 &reply));
803 }
804 std::vector<uint8_t> readbuf(datasize);
805 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
806 EXPECT_EQ(writebuf, readbuf);
807
808 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
809
810 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
811}
812
Riley Andrews06b01ad2014-12-18 12:10:08 -0800813TEST_F(BinderLibTest, PromoteLocal) {
814 sp<IBinder> strong = new BBinder();
815 wp<IBinder> weak = strong;
816 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700817 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800818 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700819 strong = nullptr;
820 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800821 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700822 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800823}
824
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700825TEST_F(BinderLibTest, LocalGetExtension) {
826 sp<BBinder> binder = new BBinder();
827 sp<IBinder> ext = new BBinder();
828 binder->setExtension(ext);
829 EXPECT_EQ(ext, binder->getExtension());
830}
831
832TEST_F(BinderLibTest, RemoteGetExtension) {
833 sp<IBinder> server = addServer();
834 ASSERT_TRUE(server != nullptr);
835
836 sp<IBinder> extension;
837 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
838 ASSERT_NE(nullptr, extension.get());
839
840 EXPECT_EQ(NO_ERROR, extension->pingBinder());
841}
842
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700843TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
844 status_t ret;
845 Parcel data, reply;
846
847 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
848 EXPECT_EQ(NO_ERROR, ret);
849
850 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700851 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800852 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
853 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
854 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
855 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700856}
857
Connor O'Brien52be2c92016-09-20 14:18:08 -0700858TEST_F(BinderLibTest, FreedBinder) {
859 status_t ret;
860
861 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700862 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700863
864 __u32 freedHandle;
865 wp<IBinder> keepFreedBinder;
866 {
867 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700868 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
869 ASSERT_EQ(NO_ERROR, ret);
870 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
871 freedHandle = freed->handle;
872 /* Add a weak ref to the freed binder so the driver does not
873 * delete its reference to it - otherwise the transaction
874 * fails regardless of whether the driver is fixed.
875 */
Steven Morelande171d622019-07-17 16:06:01 -0700876 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700877 }
Steven Morelande171d622019-07-17 16:06:01 -0700878 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700879 {
880 Parcel data, reply;
881 data.writeStrongBinder(server);
882 /* Replace original handle with handle to the freed binder */
883 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
884 __u32 oldHandle = strong->handle;
885 strong->handle = freedHandle;
886 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
887 /* Returns DEAD_OBJECT (-32) if target crashes and
888 * FAILED_TRANSACTION if the driver rejects the invalid
889 * object.
890 */
891 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
892 /* Restore original handle so parcel destructor does not use
893 * the wrong handle.
894 */
895 strong->handle = oldHandle;
896 }
897}
898
Sherry Yang336cdd32017-07-24 14:12:27 -0700899TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
900 status_t ret;
901 Parcel data, reply;
902 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
903 for (int i = 0; i < 2; i++) {
904 BinderLibTestBundle datai;
905 datai.appendFrom(&data, 0, data.dataSize());
906
907 data.freeData();
908 data.writeInt32(1);
909 data.writeStrongBinder(callBack);
910 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
911
912 datai.appendTo(&data);
913 }
914 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
915 EXPECT_EQ(NO_ERROR, ret);
916}
917
Martijn Coenen45b07b42017-08-09 12:07:45 +0200918TEST_F(BinderLibTest, OnewayQueueing)
919{
920 status_t ret;
921 Parcel data, data2;
922
923 sp<IBinder> pollServer = addPollServer();
924
925 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
926 data.writeStrongBinder(callBack);
927 data.writeInt32(500000); // delay in us before calling back
928
929 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
930 data2.writeStrongBinder(callBack2);
931 data2.writeInt32(0); // delay in us
932
Yi Kong91635562018-06-07 14:38:36 -0700933 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200934 EXPECT_EQ(NO_ERROR, ret);
935
936 // The delay ensures that this second transaction will end up on the async_todo list
937 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700938 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200939 EXPECT_EQ(NO_ERROR, ret);
940
941 // The server will ensure that the two transactions are handled in the expected order;
942 // If the ordering is not as expected, an error will be returned through the callbacks.
943 ret = callBack->waitEvent(2);
944 EXPECT_EQ(NO_ERROR, ret);
945 ret = callBack->getResult();
946 EXPECT_EQ(NO_ERROR, ret);
947
948 ret = callBack2->waitEvent(2);
949 EXPECT_EQ(NO_ERROR, ret);
950 ret = callBack2->getResult();
951 EXPECT_EQ(NO_ERROR, ret);
952}
953
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100954TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
955{
956 status_t ret;
957 Parcel data, reply;
958 data.writeInterfaceToken(binderLibTestServiceName);
959 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
960 EXPECT_EQ(-1, reply.readInt32());
961 EXPECT_EQ(NO_ERROR, ret);
962}
963
964TEST_F(BinderLibTest, WorkSourceSet)
965{
966 status_t ret;
967 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000968 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000969 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100970 data.writeInterfaceToken(binderLibTestServiceName);
971 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
972 EXPECT_EQ(100, reply.readInt32());
973 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000974 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
975 EXPECT_EQ(NO_ERROR, ret);
976}
977
978TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
979{
980 status_t ret;
981 Parcel data, reply;
982
983 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
984 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
985
986 data.writeInterfaceToken(binderLibTestServiceName);
987 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
988 EXPECT_EQ(-1, reply.readInt32());
989 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100990 EXPECT_EQ(NO_ERROR, ret);
991}
992
993TEST_F(BinderLibTest, WorkSourceCleared)
994{
995 status_t ret;
996 Parcel data, reply;
997
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000998 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000999 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1000 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001001 data.writeInterfaceToken(binderLibTestServiceName);
1002 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1003
1004 EXPECT_EQ(-1, reply.readInt32());
1005 EXPECT_EQ(100, previousWorkSource);
1006 EXPECT_EQ(NO_ERROR, ret);
1007}
1008
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001009TEST_F(BinderLibTest, WorkSourceRestored)
1010{
1011 status_t ret;
1012 Parcel data, reply;
1013
1014 IPCThreadState::self()->setCallingWorkSourceUid(100);
1015 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1016 IPCThreadState::self()->restoreCallingWorkSource(token);
1017
1018 data.writeInterfaceToken(binderLibTestServiceName);
1019 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1020
1021 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001022 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001023 EXPECT_EQ(NO_ERROR, ret);
1024}
1025
Olivier Gaillard91a04802018-11-14 17:32:41 +00001026TEST_F(BinderLibTest, PropagateFlagSet)
1027{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001028 IPCThreadState::self()->clearPropagateWorkSource();
1029 IPCThreadState::self()->setCallingWorkSourceUid(100);
1030 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1031}
1032
1033TEST_F(BinderLibTest, PropagateFlagCleared)
1034{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001035 IPCThreadState::self()->setCallingWorkSourceUid(100);
1036 IPCThreadState::self()->clearPropagateWorkSource();
1037 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1038}
1039
1040TEST_F(BinderLibTest, PropagateFlagRestored)
1041{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001042 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1043 IPCThreadState::self()->restoreCallingWorkSource(token);
1044
1045 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1046}
1047
1048TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1049{
1050 IPCThreadState::self()->setCallingWorkSourceUid(100);
1051
1052 Parcel data, reply;
1053 status_t ret;
1054 data.writeInterfaceToken(binderLibTestServiceName);
1055 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1056
1057 Parcel data2, reply2;
1058 status_t ret2;
1059 data2.writeInterfaceToken(binderLibTestServiceName);
1060 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1061 EXPECT_EQ(100, reply2.readInt32());
1062 EXPECT_EQ(NO_ERROR, ret2);
1063}
1064
Steven Morelandbf1915b2020-07-16 22:43:02 +00001065TEST_F(BinderLibTest, SchedPolicySet) {
1066 sp<IBinder> server = addServer();
1067 ASSERT_TRUE(server != nullptr);
1068
1069 Parcel data, reply;
1070 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1071 EXPECT_EQ(NO_ERROR, ret);
1072
1073 int policy = reply.readInt32();
1074 int priority = reply.readInt32();
1075
1076 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1077 EXPECT_EQ(kSchedPriority, priority);
1078}
1079
1080
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001081TEST_F(BinderLibTest, VectorSent) {
1082 Parcel data, reply;
1083 sp<IBinder> server = addServer();
1084 ASSERT_TRUE(server != nullptr);
1085
1086 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1087 data.writeUint64Vector(testValue);
1088
1089 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1090 EXPECT_EQ(NO_ERROR, ret);
1091 std::vector<uint64_t> readValue;
1092 ret = reply.readUint64Vector(&readValue);
1093 EXPECT_EQ(readValue, testValue);
1094}
1095
Martijn Coenen82c75312019-07-24 15:18:30 +02001096TEST_F(BinderLibTest, BufRejected) {
1097 Parcel data, reply;
1098 uint32_t buf;
1099 sp<IBinder> server = addServer();
1100 ASSERT_TRUE(server != nullptr);
1101
1102 binder_buffer_object obj {
1103 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001104 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001105 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1106 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001107 };
1108 data.setDataCapacity(1024);
1109 // Write a bogus object at offset 0 to get an entry in the offset table
1110 data.writeFileDescriptor(0);
1111 EXPECT_EQ(data.objectsCount(), 1);
1112 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1113 // And now, overwrite it with the buffer object
1114 memcpy(parcelData, &obj, sizeof(obj));
1115 data.setDataSize(sizeof(obj));
1116
1117 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1118 // Either the kernel should reject this transaction (if it's correct), but
1119 // if it's not, the server implementation should return an error if it
1120 // finds an object in the received Parcel.
1121 EXPECT_NE(NO_ERROR, ret);
1122}
1123
Riley Andrews06b01ad2014-12-18 12:10:08 -08001124class BinderLibTestService : public BBinder
1125{
1126 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001127 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001128 : m_id(id)
1129 , m_nextServerId(id + 1)
1130 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001131 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001132 {
Yi Kong91635562018-06-07 14:38:36 -07001133 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1134 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001135 }
1136 ~BinderLibTestService()
1137 {
1138 exit(EXIT_SUCCESS);
1139 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001140
1141 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001142 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001143 Parcel data;
1144 data.writeInt32(NO_ERROR);
1145 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001146 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001147 }
1148 }
1149
Riley Andrews06b01ad2014-12-18 12:10:08 -08001150 virtual status_t onTransact(uint32_t code,
1151 const Parcel& data, Parcel* reply,
1152 uint32_t flags = 0) {
1153 //printf("%s: code %d\n", __func__, code);
1154 (void)flags;
1155
1156 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1157 return PERMISSION_DENIED;
1158 }
1159 switch (code) {
1160 case BINDER_LIB_TEST_REGISTER_SERVER: {
1161 int32_t id;
1162 sp<IBinder> binder;
1163 id = data.readInt32();
1164 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001165 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001166 return BAD_VALUE;
1167 }
1168
1169 if (m_id != 0)
1170 return INVALID_OPERATION;
1171
1172 pthread_mutex_lock(&m_serverWaitMutex);
1173 if (m_serverStartRequested) {
1174 m_serverStartRequested = false;
1175 m_serverStarted = binder;
1176 pthread_cond_signal(&m_serverWaitCond);
1177 }
1178 pthread_mutex_unlock(&m_serverWaitMutex);
1179 return NO_ERROR;
1180 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001181 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001182 case BINDER_LIB_TEST_ADD_SERVER: {
1183 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001184 int serverid;
1185
1186 if (m_id != 0) {
1187 return INVALID_OPERATION;
1188 }
1189 pthread_mutex_lock(&m_serverWaitMutex);
1190 if (m_serverStartRequested) {
1191 ret = -EBUSY;
1192 } else {
1193 serverid = m_nextServerId++;
1194 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001195 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001196
1197 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001198 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 pthread_mutex_lock(&m_serverWaitMutex);
1200 }
1201 if (ret > 0) {
1202 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001203 struct timespec ts;
1204 clock_gettime(CLOCK_REALTIME, &ts);
1205 ts.tv_sec += 5;
1206 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001207 }
1208 if (m_serverStartRequested) {
1209 m_serverStartRequested = false;
1210 ret = -ETIMEDOUT;
1211 } else {
1212 reply->writeStrongBinder(m_serverStarted);
1213 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001214 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001215 ret = NO_ERROR;
1216 }
1217 } else if (ret >= 0) {
1218 m_serverStartRequested = false;
1219 ret = UNKNOWN_ERROR;
1220 }
1221 pthread_mutex_unlock(&m_serverWaitMutex);
1222 return ret;
1223 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001224 case BINDER_LIB_TEST_GETPID:
1225 reply->writeInt32(getpid());
1226 return NO_ERROR;
1227 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1228 usleep(5000);
1229 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001230 case BINDER_LIB_TEST_NOP_TRANSACTION:
1231 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001232 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1233 // Note: this transaction is only designed for use with a
1234 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001235 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001236 // A callback was already pending; this means that
1237 // we received a second call while still processing
1238 // the first one. Fail the test.
1239 sp<IBinder> callback = data.readStrongBinder();
1240 Parcel data2;
1241 data2.writeInt32(UNKNOWN_ERROR);
1242
Yi Kong91635562018-06-07 14:38:36 -07001243 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001244 } else {
1245 m_callback = data.readStrongBinder();
1246 int32_t delayUs = data.readInt32();
1247 /*
1248 * It's necessary that we sleep here, so the next
1249 * transaction the caller makes will be queued to
1250 * the async queue.
1251 */
1252 usleep(delayUs);
1253
1254 /*
1255 * Now when we return, libbinder will tell the kernel
1256 * we are done with this transaction, and the kernel
1257 * can move the queued transaction to either the
1258 * thread todo worklist (for kernels without the fix),
1259 * or the proc todo worklist. In case of the former,
1260 * the next outbound call will pick up the pending
1261 * transaction, which leads to undesired reentrant
1262 * behavior. This is caught in the if() branch above.
1263 */
1264 }
1265
1266 return NO_ERROR;
1267 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001268 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1269 Parcel data2, reply2;
1270 sp<IBinder> binder;
1271 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001272 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001273 return BAD_VALUE;
1274 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001275 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1277 return NO_ERROR;
1278 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001279 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1280 reply->writeStrongBinder(this);
1281 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001282 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1283 reply->writeInt32(m_id);
1284 return NO_ERROR;
1285 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1286 int32_t count;
1287 uint32_t indirect_code;
1288 sp<IBinder> binder;
1289
1290 count = data.readInt32();
1291 reply->writeInt32(m_id);
1292 reply->writeInt32(count);
1293 for (int i = 0; i < count; i++) {
1294 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001295 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001296 return BAD_VALUE;
1297 }
1298 indirect_code = data.readInt32();
1299 BinderLibTestBundle data2(&data);
1300 if (!data2.isValid()) {
1301 return BAD_VALUE;
1302 }
1303 BinderLibTestBundle reply2;
1304 binder->transact(indirect_code, data2, &reply2);
1305 reply2.appendTo(reply);
1306 }
1307 return NO_ERROR;
1308 }
1309 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1310 reply->setError(data.readInt32());
1311 return NO_ERROR;
1312 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1313 reply->writeInt32(sizeof(void *));
1314 return NO_ERROR;
1315 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1316 return NO_ERROR;
1317 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1318 m_strongRef = data.readStrongBinder();
1319 return NO_ERROR;
1320 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1321 int ret;
1322 Parcel data2, reply2;
1323 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1324 sp<IBinder> target;
1325 sp<IBinder> callback;
1326
1327 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001328 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001329 return BAD_VALUE;
1330 }
1331 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001332 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001333 return BAD_VALUE;
1334 }
1335 ret = target->linkToDeath(testDeathRecipient);
1336 if (ret == NO_ERROR)
1337 ret = testDeathRecipient->waitEvent(5);
1338 data2.writeInt32(ret);
1339 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1340 return NO_ERROR;
1341 }
1342 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1343 int ret;
1344 int32_t size;
1345 const void *buf;
1346 int fd;
1347
1348 fd = data.readFileDescriptor();
1349 if (fd < 0) {
1350 return BAD_VALUE;
1351 }
1352 ret = data.readInt32(&size);
1353 if (ret != NO_ERROR) {
1354 return ret;
1355 }
1356 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001357 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358 return BAD_VALUE;
1359 }
1360 ret = write(fd, buf, size);
1361 if (ret != size)
1362 return UNKNOWN_ERROR;
1363 return NO_ERROR;
1364 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001365 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1366 int ret;
1367 int32_t size;
1368 const void *buf;
1369 android::base::unique_fd fd;
1370
1371 ret = data.readUniqueParcelFileDescriptor(&fd);
1372 if (ret != NO_ERROR) {
1373 return ret;
1374 }
1375 ret = data.readInt32(&size);
1376 if (ret != NO_ERROR) {
1377 return ret;
1378 }
1379 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001380 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001381 return BAD_VALUE;
1382 }
1383 ret = write(fd.get(), buf, size);
1384 if (ret != size) return UNKNOWN_ERROR;
1385 return NO_ERROR;
1386 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001387 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1388 alarm(10);
1389 return NO_ERROR;
1390 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001391 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001392 ;
1393 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001394 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001395 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001396 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001397 return NO_ERROR;
1398 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001399 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1400 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001401 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001402 return NO_ERROR;
1403 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001404 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1405 int policy = 0;
1406 sched_param param;
1407 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1408 return UNKNOWN_ERROR;
1409 }
1410 reply->writeInt32(policy);
1411 reply->writeInt32(param.sched_priority);
1412 return NO_ERROR;
1413 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001414 case BINDER_LIB_TEST_ECHO_VECTOR: {
1415 std::vector<uint64_t> vector;
1416 auto err = data.readUint64Vector(&vector);
1417 if (err != NO_ERROR)
1418 return err;
1419 reply->writeUint64Vector(vector);
1420 return NO_ERROR;
1421 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001422 case BINDER_LIB_TEST_REJECT_BUF: {
1423 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1424 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001425 default:
1426 return UNKNOWN_TRANSACTION;
1427 };
1428 }
1429 private:
1430 int32_t m_id;
1431 int32_t m_nextServerId;
1432 pthread_mutex_t m_serverWaitMutex;
1433 pthread_cond_t m_serverWaitCond;
1434 bool m_serverStartRequested;
1435 sp<IBinder> m_serverStarted;
1436 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001437 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001438};
1439
Martijn Coenen45b07b42017-08-09 12:07:45 +02001440int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001441{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001442 binderLibTestServiceName += String16(binderserversuffix);
1443
Riley Andrews06b01ad2014-12-18 12:10:08 -08001444 status_t ret;
1445 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001446 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001447 {
1448 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001449
Steven Morelandbf1915b2020-07-16 22:43:02 +00001450 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1451
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001452 /*
1453 * Normally would also contain functionality as well, but we are only
1454 * testing the extension mechanism.
1455 */
1456 testService->setExtension(new BBinder());
1457
Martijn Coenen82c75312019-07-24 15:18:30 +02001458 // Required for test "BufRejected'
1459 testService->setRequestingSid(true);
1460
Martijn Coenen45b07b42017-08-09 12:07:45 +02001461 /*
1462 * We need this below, but can't hold a sp<> because it prevents the
1463 * node from being cleaned up automatically. It's safe in this case
1464 * because of how the tests are written.
1465 */
1466 testServicePtr = testService.get();
1467
Riley Andrews06b01ad2014-12-18 12:10:08 -08001468 if (index == 0) {
1469 ret = sm->addService(binderLibTestServiceName, testService);
1470 } else {
1471 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1472 Parcel data, reply;
1473 data.writeInt32(index);
1474 data.writeStrongBinder(testService);
1475
1476 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1477 }
1478 }
1479 write(readypipefd, &ret, sizeof(ret));
1480 close(readypipefd);
1481 //printf("%s: ret %d\n", __func__, ret);
1482 if (ret)
1483 return 1;
1484 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001485 if (usePoll) {
1486 int fd;
1487 struct epoll_event ev;
1488 int epoll_fd;
1489 IPCThreadState::self()->setupPolling(&fd);
1490 if (fd < 0) {
1491 return 1;
1492 }
1493 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1494
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001495 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001496 if (epoll_fd == -1) {
1497 return 1;
1498 }
1499
1500 ev.events = EPOLLIN;
1501 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1502 return 1;
1503 }
1504
1505 while (1) {
1506 /*
1507 * We simulate a single-threaded process using the binder poll
1508 * interface; besides handling binder commands, it can also
1509 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001510 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001511 *
1512 * processPendingCall() will then issue that transaction.
1513 */
1514 struct epoll_event events[1];
1515 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1516 if (numEvents < 0) {
1517 if (errno == EINTR) {
1518 continue;
1519 }
1520 return 1;
1521 }
1522 if (numEvents > 0) {
1523 IPCThreadState::self()->handlePolledCommands();
1524 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1525 testServicePtr->processPendingCall();
1526 }
1527 }
1528 } else {
1529 ProcessState::self()->startThreadPool();
1530 IPCThreadState::self()->joinThreadPool();
1531 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001532 //printf("%s: joinThreadPool returned\n", __func__);
1533 return 1; /* joinThreadPool should not return */
1534}
1535
1536int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001537 ExitIfWrongAbi();
1538
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001539 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001540 binderservername = argv[2];
1541 } else {
1542 binderservername = argv[0];
1543 }
1544
Martijn Coenen45b07b42017-08-09 12:07:45 +02001545 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1546 binderserversuffix = argv[5];
1547 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001548 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001549 binderserversuffix = new char[16];
1550 snprintf(binderserversuffix, 16, "%d", getpid());
1551 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001552
1553 ::testing::InitGoogleTest(&argc, argv);
1554 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1555 ProcessState::self()->startThreadPool();
1556 return RUN_ALL_TESTS();
1557}