blob: 0f7d15989a49a7402e1972deb92353e1a00bea12 [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>
Steven Morelandcf03cf12020-12-04 02:58:40 +000033#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020034#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080035#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020036
Steven Morelandf9f3de22020-05-06 17:14:39 -070037#include "binderAbiHelper.h"
38
Riley Andrews06b01ad2014-12-18 12:10:08 -080039#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
40
41using namespace android;
42
Sherry Yang336cdd32017-07-24 14:12:27 -070043static ::testing::AssertionResult IsPageAligned(void *buf) {
44 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
45 return ::testing::AssertionSuccess();
46 else
47 return ::testing::AssertionFailure() << buf << " is not page aligned";
48}
49
Riley Andrews06b01ad2014-12-18 12:10:08 -080050static testing::Environment* binder_env;
51static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070052static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080053static char binderserverarg[] = "--binderserver";
54
Steven Morelandbf1915b2020-07-16 22:43:02 +000055static constexpr int kSchedPolicy = SCHED_RR;
56static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000057static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000058
Riley Andrews06b01ad2014-12-18 12:10:08 -080059static String16 binderLibTestServiceName = String16("test.binderLib");
60
61enum BinderLibTestTranscationCode {
62 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
63 BINDER_LIB_TEST_REGISTER_SERVER,
64 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020065 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080066 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070067 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020068 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070070 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080071 BINDER_LIB_TEST_GET_ID_TRANSACTION,
72 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
73 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
74 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
75 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
76 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
77 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090078 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080079 BINDER_LIB_TEST_EXIT_TRANSACTION,
80 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
81 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070082 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010083 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000084 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070085 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
86 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080087 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020088 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080089};
90
Martijn Coenen45b07b42017-08-09 12:07:45 +020091pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080092{
93 int ret;
94 pid_t pid;
95 status_t status;
96 int pipefd[2];
97 char stri[16];
98 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020099 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800100 char *childargv[] = {
101 binderservername,
102 binderserverarg,
103 stri,
104 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200105 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700106 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700107 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800108 };
109
110 ret = pipe(pipefd);
111 if (ret < 0)
112 return ret;
113
114 snprintf(stri, sizeof(stri), "%d", arg2);
115 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200116 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800117
118 pid = fork();
119 if (pid == -1)
120 return pid;
121 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800122 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800123 close(pipefd[0]);
124 execv(binderservername, childargv);
125 status = -errno;
126 write(pipefd[1], &status, sizeof(status));
127 fprintf(stderr, "execv failed, %s\n", strerror(errno));
128 _exit(EXIT_FAILURE);
129 }
130 close(pipefd[1]);
131 ret = read(pipefd[0], &status, sizeof(status));
132 //printf("pipe read returned %d, status %d\n", ret, status);
133 close(pipefd[0]);
134 if (ret == sizeof(status)) {
135 ret = status;
136 } else {
137 kill(pid, SIGKILL);
138 if (ret >= 0) {
139 ret = NO_INIT;
140 }
141 }
142 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700143 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800144 return ret;
145 }
146 return pid;
147}
148
149class BinderLibTestEnv : public ::testing::Environment {
150 public:
151 BinderLibTestEnv() {}
152 sp<IBinder> getServer(void) {
153 return m_server;
154 }
155
156 private:
157 virtual void SetUp() {
158 m_serverpid = start_server_process(0);
159 //printf("m_serverpid %d\n", m_serverpid);
160 ASSERT_GT(m_serverpid, 0);
161
162 sp<IServiceManager> sm = defaultServiceManager();
163 //printf("%s: pid %d, get service\n", __func__, m_pid);
164 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700165 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800166 //printf("%s: pid %d, get service done\n", __func__, m_pid);
167 }
168 virtual void TearDown() {
169 status_t ret;
170 Parcel data, reply;
171 int exitStatus;
172 pid_t pid;
173
174 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700175 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800176 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
177 EXPECT_EQ(0, ret);
178 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
179 EXPECT_EQ(0, ret);
180 }
181 if (m_serverpid > 0) {
182 //printf("wait for %d\n", m_pids[i]);
183 pid = wait(&exitStatus);
184 EXPECT_EQ(m_serverpid, pid);
185 EXPECT_TRUE(WIFEXITED(exitStatus));
186 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
187 }
188 }
189
190 pid_t m_serverpid;
191 sp<IBinder> m_server;
192};
193
194class BinderLibTest : public ::testing::Test {
195 public:
196 virtual void SetUp() {
197 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000198 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 }
200 virtual void TearDown() {
201 }
202 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200203 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800204 {
205 int ret;
206 int32_t id;
207 Parcel data, reply;
208 sp<IBinder> binder;
209
Martijn Coenen45b07b42017-08-09 12:07:45 +0200210 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800211 EXPECT_EQ(NO_ERROR, ret);
212
Yi Kong91635562018-06-07 14:38:36 -0700213 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700215 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 ret = reply.readInt32(&id);
217 EXPECT_EQ(NO_ERROR, ret);
218 if (idPtr)
219 *idPtr = id;
220 return binder;
221 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200222
Yi Kong91635562018-06-07 14:38:36 -0700223 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200224 {
225 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
226 }
227
Yi Kong91635562018-06-07 14:38:36 -0700228 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200229 {
230 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
231 }
232
Riley Andrews06b01ad2014-12-18 12:10:08 -0800233 void waitForReadData(int fd, int timeout_ms) {
234 int ret;
235 pollfd pfd = pollfd();
236
237 pfd.fd = fd;
238 pfd.events = POLLIN;
239 ret = poll(&pfd, 1, timeout_ms);
240 EXPECT_EQ(1, ret);
241 }
242
243 sp<IBinder> m_server;
244};
245
246class BinderLibTestBundle : public Parcel
247{
248 public:
249 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800250 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800251 int32_t mark;
252 int32_t bundleLen;
253 size_t pos;
254
255 if (source->readInt32(&mark))
256 return;
257 if (mark != MARK_START)
258 return;
259 if (source->readInt32(&bundleLen))
260 return;
261 pos = source->dataPosition();
262 if (Parcel::appendFrom(source, pos, bundleLen))
263 return;
264 source->setDataPosition(pos + bundleLen);
265 if (source->readInt32(&mark))
266 return;
267 if (mark != MARK_END)
268 return;
269 m_isValid = true;
270 setDataPosition(0);
271 }
272 void appendTo(Parcel *dest) {
273 dest->writeInt32(MARK_START);
274 dest->writeInt32(dataSize());
275 dest->appendFrom(this, 0, dataSize());
276 dest->writeInt32(MARK_END);
277 };
278 bool isValid(void) {
279 return m_isValid;
280 }
281 private:
282 enum {
283 MARK_START = B_PACK_CHARS('B','T','B','S'),
284 MARK_END = B_PACK_CHARS('B','T','B','E'),
285 };
286 bool m_isValid;
287};
288
289class BinderLibTestEvent
290{
291 public:
292 BinderLibTestEvent(void)
293 : m_eventTriggered(false)
294 {
Yi Kong91635562018-06-07 14:38:36 -0700295 pthread_mutex_init(&m_waitMutex, nullptr);
296 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800297 }
298 int waitEvent(int timeout_s)
299 {
300 int ret;
301 pthread_mutex_lock(&m_waitMutex);
302 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800303 struct timespec ts;
304 clock_gettime(CLOCK_REALTIME, &ts);
305 ts.tv_sec += timeout_s;
306 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800307 }
308 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
309 pthread_mutex_unlock(&m_waitMutex);
310 return ret;
311 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200312 pthread_t getTriggeringThread()
313 {
314 return m_triggeringThread;
315 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800316 protected:
317 void triggerEvent(void) {
318 pthread_mutex_lock(&m_waitMutex);
319 pthread_cond_signal(&m_waitCond);
320 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200321 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800322 pthread_mutex_unlock(&m_waitMutex);
323 };
324 private:
325 pthread_mutex_t m_waitMutex;
326 pthread_cond_t m_waitCond;
327 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200328 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800329};
330
331class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
332{
333 public:
334 BinderLibTestCallBack()
335 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700336 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800337 {
338 }
339 status_t getResult(void)
340 {
341 return m_result;
342 }
343
344 private:
345 virtual status_t onTransact(uint32_t code,
346 const Parcel& data, Parcel* reply,
347 uint32_t flags = 0)
348 {
349 (void)reply;
350 (void)flags;
351 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200352 case BINDER_LIB_TEST_CALL_BACK: {
353 status_t status = data.readInt32(&m_result);
354 if (status != NO_ERROR) {
355 m_result = status;
356 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800357 triggerEvent();
358 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200359 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700360 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
361 sp<IBinder> server;
362 int ret;
363 const uint8_t *buf = data.data();
364 size_t size = data.dataSize();
365 if (m_prev_end) {
366 /* 64-bit kernel needs at most 8 bytes to align buffer end */
367 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
368 } else {
369 EXPECT_TRUE(IsPageAligned((void *)buf));
370 }
371
372 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
373
374 if (size > 0) {
375 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
376 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
377 data, reply);
378 EXPECT_EQ(NO_ERROR, ret);
379 }
380 return NO_ERROR;
381 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800382 default:
383 return UNKNOWN_TRANSACTION;
384 }
385 }
386
387 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700388 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800389};
390
391class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
392{
393 private:
394 virtual void binderDied(const wp<IBinder>& who) {
395 (void)who;
396 triggerEvent();
397 };
398};
399
400TEST_F(BinderLibTest, NopTransaction) {
401 status_t ret;
402 Parcel data, reply;
403 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
404 EXPECT_EQ(NO_ERROR, ret);
405}
406
Steven Morelandf183fdd2020-10-27 00:12:12 +0000407TEST_F(BinderLibTest, NopTransactionClear) {
408 status_t ret;
409 Parcel data, reply;
410 // make sure it accepts the transaction flag
411 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
412 EXPECT_EQ(NO_ERROR, ret);
413}
414
Marco Ballesio7ee17572020-09-08 10:30:03 -0700415TEST_F(BinderLibTest, Freeze) {
416 status_t ret;
417 Parcel data, reply, replypid;
418 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
419
420 //Pass test on devices where the freezer is not supported
421 if (freezer_file.fail()) {
422 GTEST_SKIP();
423 return;
424 }
425
426 std::string freezer_enabled;
427 std::getline(freezer_file, freezer_enabled);
428
429 //Pass test on devices where the freezer is disabled
430 if (freezer_enabled != "1") {
431 GTEST_SKIP();
432 return;
433 }
434
435 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
436 int32_t pid = replypid.readInt32();
437 EXPECT_EQ(NO_ERROR, ret);
438 for (int i = 0; i < 10; i++) {
439 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
440 }
441 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
442 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
443 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
444 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700445
446 bool sync_received, async_received;
447
448 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
449 &async_received));
450
451 EXPECT_EQ(sync_received, 1);
452 EXPECT_EQ(async_received, 0);
453
Marco Ballesio7ee17572020-09-08 10:30:03 -0700454 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
455 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
456}
457
Riley Andrews06b01ad2014-12-18 12:10:08 -0800458TEST_F(BinderLibTest, SetError) {
459 int32_t testValue[] = { 0, -123, 123 };
460 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
461 status_t ret;
462 Parcel data, reply;
463 data.writeInt32(testValue[i]);
464 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
465 EXPECT_EQ(testValue[i], ret);
466 }
467}
468
469TEST_F(BinderLibTest, GetId) {
470 status_t ret;
471 int32_t id;
472 Parcel data, reply;
473 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
474 EXPECT_EQ(NO_ERROR, ret);
475 ret = reply.readInt32(&id);
476 EXPECT_EQ(NO_ERROR, ret);
477 EXPECT_EQ(0, id);
478}
479
480TEST_F(BinderLibTest, PtrSize) {
481 status_t ret;
482 int32_t ptrsize;
483 Parcel data, reply;
484 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700485 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800486 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
487 EXPECT_EQ(NO_ERROR, ret);
488 ret = reply.readInt32(&ptrsize);
489 EXPECT_EQ(NO_ERROR, ret);
490 RecordProperty("TestPtrSize", sizeof(void *));
491 RecordProperty("ServerPtrSize", sizeof(void *));
492}
493
494TEST_F(BinderLibTest, IndirectGetId2)
495{
496 status_t ret;
497 int32_t id;
498 int32_t count;
499 Parcel data, reply;
500 int32_t serverId[3];
501
502 data.writeInt32(ARRAY_SIZE(serverId));
503 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
504 sp<IBinder> server;
505 BinderLibTestBundle datai;
506
507 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700508 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800509 data.writeStrongBinder(server);
510 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
511 datai.appendTo(&data);
512 }
513
514 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
515 ASSERT_EQ(NO_ERROR, ret);
516
517 ret = reply.readInt32(&id);
518 ASSERT_EQ(NO_ERROR, ret);
519 EXPECT_EQ(0, id);
520
521 ret = reply.readInt32(&count);
522 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700523 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800524
525 for (size_t i = 0; i < (size_t)count; i++) {
526 BinderLibTestBundle replyi(&reply);
527 EXPECT_TRUE(replyi.isValid());
528 ret = replyi.readInt32(&id);
529 EXPECT_EQ(NO_ERROR, ret);
530 EXPECT_EQ(serverId[i], id);
531 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
532 }
533
534 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
535}
536
537TEST_F(BinderLibTest, IndirectGetId3)
538{
539 status_t ret;
540 int32_t id;
541 int32_t count;
542 Parcel data, reply;
543 int32_t serverId[3];
544
545 data.writeInt32(ARRAY_SIZE(serverId));
546 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
547 sp<IBinder> server;
548 BinderLibTestBundle datai;
549 BinderLibTestBundle datai2;
550
551 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700552 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553 data.writeStrongBinder(server);
554 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
555
556 datai.writeInt32(1);
557 datai.writeStrongBinder(m_server);
558 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
559 datai2.appendTo(&datai);
560
561 datai.appendTo(&data);
562 }
563
564 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
565 ASSERT_EQ(NO_ERROR, ret);
566
567 ret = reply.readInt32(&id);
568 ASSERT_EQ(NO_ERROR, ret);
569 EXPECT_EQ(0, id);
570
571 ret = reply.readInt32(&count);
572 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700573 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800574
575 for (size_t i = 0; i < (size_t)count; i++) {
576 int32_t counti;
577
578 BinderLibTestBundle replyi(&reply);
579 EXPECT_TRUE(replyi.isValid());
580 ret = replyi.readInt32(&id);
581 EXPECT_EQ(NO_ERROR, ret);
582 EXPECT_EQ(serverId[i], id);
583
584 ret = replyi.readInt32(&counti);
585 ASSERT_EQ(NO_ERROR, ret);
586 EXPECT_EQ(1, counti);
587
588 BinderLibTestBundle replyi2(&replyi);
589 EXPECT_TRUE(replyi2.isValid());
590 ret = replyi2.readInt32(&id);
591 EXPECT_EQ(NO_ERROR, ret);
592 EXPECT_EQ(0, id);
593 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
594
595 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
596 }
597
598 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
599}
600
601TEST_F(BinderLibTest, CallBack)
602{
603 status_t ret;
604 Parcel data, reply;
605 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
606 data.writeStrongBinder(callBack);
607 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
608 EXPECT_EQ(NO_ERROR, ret);
609 ret = callBack->waitEvent(5);
610 EXPECT_EQ(NO_ERROR, ret);
611 ret = callBack->getResult();
612 EXPECT_EQ(NO_ERROR, ret);
613}
614
615TEST_F(BinderLibTest, AddServer)
616{
617 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700618 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619}
620
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621TEST_F(BinderLibTest, DeathNotificationStrongRef)
622{
623 status_t ret;
624 sp<IBinder> sbinder;
625
626 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
627
628 {
629 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700630 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631 ret = binder->linkToDeath(testDeathRecipient);
632 EXPECT_EQ(NO_ERROR, ret);
633 sbinder = binder;
634 }
635 {
636 Parcel data, reply;
637 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
638 EXPECT_EQ(0, ret);
639 }
640 IPCThreadState::self()->flushCommands();
641 ret = testDeathRecipient->waitEvent(5);
642 EXPECT_EQ(NO_ERROR, ret);
643 ret = sbinder->unlinkToDeath(testDeathRecipient);
644 EXPECT_EQ(DEAD_OBJECT, ret);
645}
646
647TEST_F(BinderLibTest, DeathNotificationMultiple)
648{
649 status_t ret;
650 const int clientcount = 2;
651 sp<IBinder> target;
652 sp<IBinder> linkedclient[clientcount];
653 sp<BinderLibTestCallBack> callBack[clientcount];
654 sp<IBinder> passiveclient[clientcount];
655
656 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700657 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800658 for (int i = 0; i < clientcount; i++) {
659 {
660 Parcel data, reply;
661
662 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700663 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800664 callBack[i] = new BinderLibTestCallBack();
665 data.writeStrongBinder(target);
666 data.writeStrongBinder(callBack[i]);
667 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
668 EXPECT_EQ(NO_ERROR, ret);
669 }
670 {
671 Parcel data, reply;
672
673 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700674 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800675 data.writeStrongBinder(target);
676 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
677 EXPECT_EQ(NO_ERROR, ret);
678 }
679 }
680 {
681 Parcel data, reply;
682 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
683 EXPECT_EQ(0, ret);
684 }
685
686 for (int i = 0; i < clientcount; i++) {
687 ret = callBack[i]->waitEvent(5);
688 EXPECT_EQ(NO_ERROR, ret);
689 ret = callBack[i]->getResult();
690 EXPECT_EQ(NO_ERROR, ret);
691 }
692}
693
Martijn Coenenf7100e42017-07-31 12:14:09 +0200694TEST_F(BinderLibTest, DeathNotificationThread)
695{
696 status_t ret;
697 sp<BinderLibTestCallBack> callback;
698 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700699 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200700 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700701 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200702
703 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
704
705 ret = target->linkToDeath(testDeathRecipient);
706 EXPECT_EQ(NO_ERROR, ret);
707
708 {
709 Parcel data, reply;
710 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
711 EXPECT_EQ(0, ret);
712 }
713
714 /* Make sure it's dead */
715 testDeathRecipient->waitEvent(5);
716
717 /* Now, pass the ref to another process and ask that process to
718 * call linkToDeath() on it, and wait for a response. This tests
719 * two things:
720 * 1) You still get death notifications when calling linkToDeath()
721 * on a ref that is already dead when it was passed to you.
722 * 2) That death notifications are not directly pushed to the thread
723 * registering them, but to the threadpool (proc workqueue) instead.
724 *
725 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
726 * is blocked on a condition variable waiting for the death notification to be
727 * called; therefore, that thread is not available for handling proc work.
728 * So, if the death notification was pushed to the thread workqueue, the callback
729 * would never be called, and the test would timeout and fail.
730 *
731 * Note that we can't do this part of the test from this thread itself, because
732 * the binder driver would only push death notifications to the thread if
733 * it is a looper thread, which this thread is not.
734 *
735 * See b/23525545 for details.
736 */
737 {
738 Parcel data, reply;
739
740 callback = new BinderLibTestCallBack();
741 data.writeStrongBinder(target);
742 data.writeStrongBinder(callback);
743 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
744 EXPECT_EQ(NO_ERROR, ret);
745 }
746
747 ret = callback->waitEvent(5);
748 EXPECT_EQ(NO_ERROR, ret);
749 ret = callback->getResult();
750 EXPECT_EQ(NO_ERROR, ret);
751}
752
Riley Andrews06b01ad2014-12-18 12:10:08 -0800753TEST_F(BinderLibTest, PassFile) {
754 int ret;
755 int pipefd[2];
756 uint8_t buf[1] = { 0 };
757 uint8_t write_value = 123;
758
759 ret = pipe2(pipefd, O_NONBLOCK);
760 ASSERT_EQ(0, ret);
761
762 {
763 Parcel data, reply;
764 uint8_t writebuf[1] = { write_value };
765
766 ret = data.writeFileDescriptor(pipefd[1], true);
767 EXPECT_EQ(NO_ERROR, ret);
768
769 ret = data.writeInt32(sizeof(writebuf));
770 EXPECT_EQ(NO_ERROR, ret);
771
772 ret = data.write(writebuf, sizeof(writebuf));
773 EXPECT_EQ(NO_ERROR, ret);
774
775 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
776 EXPECT_EQ(NO_ERROR, ret);
777 }
778
779 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700780 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800781 EXPECT_EQ(write_value, buf[0]);
782
783 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
784
785 ret = read(pipefd[0], buf, sizeof(buf));
786 EXPECT_EQ(0, ret);
787
788 close(pipefd[0]);
789}
790
Ryo Hashimotobf551892018-05-31 16:58:35 +0900791TEST_F(BinderLibTest, PassParcelFileDescriptor) {
792 const int datasize = 123;
793 std::vector<uint8_t> writebuf(datasize);
794 for (size_t i = 0; i < writebuf.size(); ++i) {
795 writebuf[i] = i;
796 }
797
798 android::base::unique_fd read_end, write_end;
799 {
800 int pipefd[2];
801 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
802 read_end.reset(pipefd[0]);
803 write_end.reset(pipefd[1]);
804 }
805 {
806 Parcel data;
807 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
808 write_end.reset();
809 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
810 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
811
812 Parcel reply;
813 EXPECT_EQ(NO_ERROR,
814 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
815 &reply));
816 }
817 std::vector<uint8_t> readbuf(datasize);
818 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
819 EXPECT_EQ(writebuf, readbuf);
820
821 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
822
823 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
824}
825
Riley Andrews06b01ad2014-12-18 12:10:08 -0800826TEST_F(BinderLibTest, PromoteLocal) {
827 sp<IBinder> strong = new BBinder();
828 wp<IBinder> weak = strong;
829 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700830 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800831 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700832 strong = nullptr;
833 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800834 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700835 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800836}
837
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700838TEST_F(BinderLibTest, LocalGetExtension) {
839 sp<BBinder> binder = new BBinder();
840 sp<IBinder> ext = new BBinder();
841 binder->setExtension(ext);
842 EXPECT_EQ(ext, binder->getExtension());
843}
844
845TEST_F(BinderLibTest, RemoteGetExtension) {
846 sp<IBinder> server = addServer();
847 ASSERT_TRUE(server != nullptr);
848
849 sp<IBinder> extension;
850 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
851 ASSERT_NE(nullptr, extension.get());
852
853 EXPECT_EQ(NO_ERROR, extension->pingBinder());
854}
855
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700856TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
857 status_t ret;
858 Parcel data, reply;
859
860 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
861 EXPECT_EQ(NO_ERROR, ret);
862
863 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700864 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800865 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
866 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
867 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
868 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700869}
870
Connor O'Brien52be2c92016-09-20 14:18:08 -0700871TEST_F(BinderLibTest, FreedBinder) {
872 status_t ret;
873
874 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700875 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700876
877 __u32 freedHandle;
878 wp<IBinder> keepFreedBinder;
879 {
880 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700881 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
882 ASSERT_EQ(NO_ERROR, ret);
883 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
884 freedHandle = freed->handle;
885 /* Add a weak ref to the freed binder so the driver does not
886 * delete its reference to it - otherwise the transaction
887 * fails regardless of whether the driver is fixed.
888 */
Steven Morelande171d622019-07-17 16:06:01 -0700889 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700890 }
Steven Morelande171d622019-07-17 16:06:01 -0700891 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700892 {
893 Parcel data, reply;
894 data.writeStrongBinder(server);
895 /* Replace original handle with handle to the freed binder */
896 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
897 __u32 oldHandle = strong->handle;
898 strong->handle = freedHandle;
899 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
900 /* Returns DEAD_OBJECT (-32) if target crashes and
901 * FAILED_TRANSACTION if the driver rejects the invalid
902 * object.
903 */
904 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
905 /* Restore original handle so parcel destructor does not use
906 * the wrong handle.
907 */
908 strong->handle = oldHandle;
909 }
910}
911
Sherry Yang336cdd32017-07-24 14:12:27 -0700912TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
913 status_t ret;
914 Parcel data, reply;
915 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
916 for (int i = 0; i < 2; i++) {
917 BinderLibTestBundle datai;
918 datai.appendFrom(&data, 0, data.dataSize());
919
920 data.freeData();
921 data.writeInt32(1);
922 data.writeStrongBinder(callBack);
923 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
924
925 datai.appendTo(&data);
926 }
927 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
928 EXPECT_EQ(NO_ERROR, ret);
929}
930
Martijn Coenen45b07b42017-08-09 12:07:45 +0200931TEST_F(BinderLibTest, OnewayQueueing)
932{
933 status_t ret;
934 Parcel data, data2;
935
936 sp<IBinder> pollServer = addPollServer();
937
938 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
939 data.writeStrongBinder(callBack);
940 data.writeInt32(500000); // delay in us before calling back
941
942 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
943 data2.writeStrongBinder(callBack2);
944 data2.writeInt32(0); // delay in us
945
Yi Kong91635562018-06-07 14:38:36 -0700946 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200947 EXPECT_EQ(NO_ERROR, ret);
948
949 // The delay ensures that this second transaction will end up on the async_todo list
950 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700951 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200952 EXPECT_EQ(NO_ERROR, ret);
953
954 // The server will ensure that the two transactions are handled in the expected order;
955 // If the ordering is not as expected, an error will be returned through the callbacks.
956 ret = callBack->waitEvent(2);
957 EXPECT_EQ(NO_ERROR, ret);
958 ret = callBack->getResult();
959 EXPECT_EQ(NO_ERROR, ret);
960
961 ret = callBack2->waitEvent(2);
962 EXPECT_EQ(NO_ERROR, ret);
963 ret = callBack2->getResult();
964 EXPECT_EQ(NO_ERROR, ret);
965}
966
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100967TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
968{
969 status_t ret;
970 Parcel data, reply;
971 data.writeInterfaceToken(binderLibTestServiceName);
972 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
973 EXPECT_EQ(-1, reply.readInt32());
974 EXPECT_EQ(NO_ERROR, ret);
975}
976
977TEST_F(BinderLibTest, WorkSourceSet)
978{
979 status_t ret;
980 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000981 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000982 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100983 data.writeInterfaceToken(binderLibTestServiceName);
984 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
985 EXPECT_EQ(100, reply.readInt32());
986 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000987 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
988 EXPECT_EQ(NO_ERROR, ret);
989}
990
991TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
992{
993 status_t ret;
994 Parcel data, reply;
995
996 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
997 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
998
999 data.writeInterfaceToken(binderLibTestServiceName);
1000 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1001 EXPECT_EQ(-1, reply.readInt32());
1002 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001003 EXPECT_EQ(NO_ERROR, ret);
1004}
1005
1006TEST_F(BinderLibTest, WorkSourceCleared)
1007{
1008 status_t ret;
1009 Parcel data, reply;
1010
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001011 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001012 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1013 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001014 data.writeInterfaceToken(binderLibTestServiceName);
1015 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1016
1017 EXPECT_EQ(-1, reply.readInt32());
1018 EXPECT_EQ(100, previousWorkSource);
1019 EXPECT_EQ(NO_ERROR, ret);
1020}
1021
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001022TEST_F(BinderLibTest, WorkSourceRestored)
1023{
1024 status_t ret;
1025 Parcel data, reply;
1026
1027 IPCThreadState::self()->setCallingWorkSourceUid(100);
1028 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1029 IPCThreadState::self()->restoreCallingWorkSource(token);
1030
1031 data.writeInterfaceToken(binderLibTestServiceName);
1032 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1033
1034 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001035 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001036 EXPECT_EQ(NO_ERROR, ret);
1037}
1038
Olivier Gaillard91a04802018-11-14 17:32:41 +00001039TEST_F(BinderLibTest, PropagateFlagSet)
1040{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001041 IPCThreadState::self()->clearPropagateWorkSource();
1042 IPCThreadState::self()->setCallingWorkSourceUid(100);
1043 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1044}
1045
1046TEST_F(BinderLibTest, PropagateFlagCleared)
1047{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001048 IPCThreadState::self()->setCallingWorkSourceUid(100);
1049 IPCThreadState::self()->clearPropagateWorkSource();
1050 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1051}
1052
1053TEST_F(BinderLibTest, PropagateFlagRestored)
1054{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001055 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1056 IPCThreadState::self()->restoreCallingWorkSource(token);
1057
1058 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1059}
1060
1061TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1062{
1063 IPCThreadState::self()->setCallingWorkSourceUid(100);
1064
1065 Parcel data, reply;
1066 status_t ret;
1067 data.writeInterfaceToken(binderLibTestServiceName);
1068 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1069
1070 Parcel data2, reply2;
1071 status_t ret2;
1072 data2.writeInterfaceToken(binderLibTestServiceName);
1073 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1074 EXPECT_EQ(100, reply2.readInt32());
1075 EXPECT_EQ(NO_ERROR, ret2);
1076}
1077
Steven Morelandbf1915b2020-07-16 22:43:02 +00001078TEST_F(BinderLibTest, SchedPolicySet) {
1079 sp<IBinder> server = addServer();
1080 ASSERT_TRUE(server != nullptr);
1081
1082 Parcel data, reply;
1083 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1084 EXPECT_EQ(NO_ERROR, ret);
1085
1086 int policy = reply.readInt32();
1087 int priority = reply.readInt32();
1088
1089 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1090 EXPECT_EQ(kSchedPriority, priority);
1091}
1092
Steven Morelandcf03cf12020-12-04 02:58:40 +00001093TEST_F(BinderLibTest, InheritRt) {
1094 sp<IBinder> server = addServer();
1095 ASSERT_TRUE(server != nullptr);
1096
1097 const struct sched_param param {
1098 .sched_priority = kSchedPriorityMore,
1099 };
1100 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1101
1102 Parcel data, reply;
1103 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1104 EXPECT_EQ(NO_ERROR, ret);
1105
1106 int policy = reply.readInt32();
1107 int priority = reply.readInt32();
1108
1109 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1110 EXPECT_EQ(kSchedPriorityMore, priority);
1111}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001112
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001113TEST_F(BinderLibTest, VectorSent) {
1114 Parcel data, reply;
1115 sp<IBinder> server = addServer();
1116 ASSERT_TRUE(server != nullptr);
1117
1118 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1119 data.writeUint64Vector(testValue);
1120
1121 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1122 EXPECT_EQ(NO_ERROR, ret);
1123 std::vector<uint64_t> readValue;
1124 ret = reply.readUint64Vector(&readValue);
1125 EXPECT_EQ(readValue, testValue);
1126}
1127
Martijn Coenen82c75312019-07-24 15:18:30 +02001128TEST_F(BinderLibTest, BufRejected) {
1129 Parcel data, reply;
1130 uint32_t buf;
1131 sp<IBinder> server = addServer();
1132 ASSERT_TRUE(server != nullptr);
1133
1134 binder_buffer_object obj {
1135 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001136 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001137 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1138 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001139 };
1140 data.setDataCapacity(1024);
1141 // Write a bogus object at offset 0 to get an entry in the offset table
1142 data.writeFileDescriptor(0);
1143 EXPECT_EQ(data.objectsCount(), 1);
1144 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1145 // And now, overwrite it with the buffer object
1146 memcpy(parcelData, &obj, sizeof(obj));
1147 data.setDataSize(sizeof(obj));
1148
1149 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1150 // Either the kernel should reject this transaction (if it's correct), but
1151 // if it's not, the server implementation should return an error if it
1152 // finds an object in the received Parcel.
1153 EXPECT_NE(NO_ERROR, ret);
1154}
1155
Riley Andrews06b01ad2014-12-18 12:10:08 -08001156class BinderLibTestService : public BBinder
1157{
1158 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001159 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001160 : m_id(id)
1161 , m_nextServerId(id + 1)
1162 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001163 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001164 {
Yi Kong91635562018-06-07 14:38:36 -07001165 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1166 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001167 }
1168 ~BinderLibTestService()
1169 {
1170 exit(EXIT_SUCCESS);
1171 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001172
1173 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001174 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001175 Parcel data;
1176 data.writeInt32(NO_ERROR);
1177 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001178 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001179 }
1180 }
1181
Riley Andrews06b01ad2014-12-18 12:10:08 -08001182 virtual status_t onTransact(uint32_t code,
1183 const Parcel& data, Parcel* reply,
1184 uint32_t flags = 0) {
1185 //printf("%s: code %d\n", __func__, code);
1186 (void)flags;
1187
1188 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1189 return PERMISSION_DENIED;
1190 }
1191 switch (code) {
1192 case BINDER_LIB_TEST_REGISTER_SERVER: {
1193 int32_t id;
1194 sp<IBinder> binder;
1195 id = data.readInt32();
1196 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001197 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001198 return BAD_VALUE;
1199 }
1200
1201 if (m_id != 0)
1202 return INVALID_OPERATION;
1203
1204 pthread_mutex_lock(&m_serverWaitMutex);
1205 if (m_serverStartRequested) {
1206 m_serverStartRequested = false;
1207 m_serverStarted = binder;
1208 pthread_cond_signal(&m_serverWaitCond);
1209 }
1210 pthread_mutex_unlock(&m_serverWaitMutex);
1211 return NO_ERROR;
1212 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001213 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001214 case BINDER_LIB_TEST_ADD_SERVER: {
1215 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 int serverid;
1217
1218 if (m_id != 0) {
1219 return INVALID_OPERATION;
1220 }
1221 pthread_mutex_lock(&m_serverWaitMutex);
1222 if (m_serverStartRequested) {
1223 ret = -EBUSY;
1224 } else {
1225 serverid = m_nextServerId++;
1226 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001227 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001228
1229 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001230 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001231 pthread_mutex_lock(&m_serverWaitMutex);
1232 }
1233 if (ret > 0) {
1234 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001235 struct timespec ts;
1236 clock_gettime(CLOCK_REALTIME, &ts);
1237 ts.tv_sec += 5;
1238 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001239 }
1240 if (m_serverStartRequested) {
1241 m_serverStartRequested = false;
1242 ret = -ETIMEDOUT;
1243 } else {
1244 reply->writeStrongBinder(m_serverStarted);
1245 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001246 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001247 ret = NO_ERROR;
1248 }
1249 } else if (ret >= 0) {
1250 m_serverStartRequested = false;
1251 ret = UNKNOWN_ERROR;
1252 }
1253 pthread_mutex_unlock(&m_serverWaitMutex);
1254 return ret;
1255 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001256 case BINDER_LIB_TEST_GETPID:
1257 reply->writeInt32(getpid());
1258 return NO_ERROR;
1259 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1260 usleep(5000);
1261 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001262 case BINDER_LIB_TEST_NOP_TRANSACTION:
1263 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001264 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1265 // Note: this transaction is only designed for use with a
1266 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001267 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001268 // A callback was already pending; this means that
1269 // we received a second call while still processing
1270 // the first one. Fail the test.
1271 sp<IBinder> callback = data.readStrongBinder();
1272 Parcel data2;
1273 data2.writeInt32(UNKNOWN_ERROR);
1274
Yi Kong91635562018-06-07 14:38:36 -07001275 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001276 } else {
1277 m_callback = data.readStrongBinder();
1278 int32_t delayUs = data.readInt32();
1279 /*
1280 * It's necessary that we sleep here, so the next
1281 * transaction the caller makes will be queued to
1282 * the async queue.
1283 */
1284 usleep(delayUs);
1285
1286 /*
1287 * Now when we return, libbinder will tell the kernel
1288 * we are done with this transaction, and the kernel
1289 * can move the queued transaction to either the
1290 * thread todo worklist (for kernels without the fix),
1291 * or the proc todo worklist. In case of the former,
1292 * the next outbound call will pick up the pending
1293 * transaction, which leads to undesired reentrant
1294 * behavior. This is caught in the if() branch above.
1295 */
1296 }
1297
1298 return NO_ERROR;
1299 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001300 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1301 Parcel data2, reply2;
1302 sp<IBinder> binder;
1303 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001304 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001305 return BAD_VALUE;
1306 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001307 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001308 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1309 return NO_ERROR;
1310 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001311 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1312 reply->writeStrongBinder(this);
1313 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001314 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1315 reply->writeInt32(m_id);
1316 return NO_ERROR;
1317 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1318 int32_t count;
1319 uint32_t indirect_code;
1320 sp<IBinder> binder;
1321
1322 count = data.readInt32();
1323 reply->writeInt32(m_id);
1324 reply->writeInt32(count);
1325 for (int i = 0; i < count; i++) {
1326 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001327 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001328 return BAD_VALUE;
1329 }
1330 indirect_code = data.readInt32();
1331 BinderLibTestBundle data2(&data);
1332 if (!data2.isValid()) {
1333 return BAD_VALUE;
1334 }
1335 BinderLibTestBundle reply2;
1336 binder->transact(indirect_code, data2, &reply2);
1337 reply2.appendTo(reply);
1338 }
1339 return NO_ERROR;
1340 }
1341 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1342 reply->setError(data.readInt32());
1343 return NO_ERROR;
1344 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1345 reply->writeInt32(sizeof(void *));
1346 return NO_ERROR;
1347 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1348 return NO_ERROR;
1349 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1350 m_strongRef = data.readStrongBinder();
1351 return NO_ERROR;
1352 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1353 int ret;
1354 Parcel data2, reply2;
1355 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1356 sp<IBinder> target;
1357 sp<IBinder> callback;
1358
1359 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001360 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001361 return BAD_VALUE;
1362 }
1363 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001364 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001365 return BAD_VALUE;
1366 }
1367 ret = target->linkToDeath(testDeathRecipient);
1368 if (ret == NO_ERROR)
1369 ret = testDeathRecipient->waitEvent(5);
1370 data2.writeInt32(ret);
1371 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1372 return NO_ERROR;
1373 }
1374 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1375 int ret;
1376 int32_t size;
1377 const void *buf;
1378 int fd;
1379
1380 fd = data.readFileDescriptor();
1381 if (fd < 0) {
1382 return BAD_VALUE;
1383 }
1384 ret = data.readInt32(&size);
1385 if (ret != NO_ERROR) {
1386 return ret;
1387 }
1388 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001389 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001390 return BAD_VALUE;
1391 }
1392 ret = write(fd, buf, size);
1393 if (ret != size)
1394 return UNKNOWN_ERROR;
1395 return NO_ERROR;
1396 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001397 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1398 int ret;
1399 int32_t size;
1400 const void *buf;
1401 android::base::unique_fd fd;
1402
1403 ret = data.readUniqueParcelFileDescriptor(&fd);
1404 if (ret != NO_ERROR) {
1405 return ret;
1406 }
1407 ret = data.readInt32(&size);
1408 if (ret != NO_ERROR) {
1409 return ret;
1410 }
1411 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001412 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001413 return BAD_VALUE;
1414 }
1415 ret = write(fd.get(), buf, size);
1416 if (ret != size) return UNKNOWN_ERROR;
1417 return NO_ERROR;
1418 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001419 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1420 alarm(10);
1421 return NO_ERROR;
1422 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001423 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001424 ;
1425 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001426 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001427 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001428 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001429 return NO_ERROR;
1430 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001431 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1432 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001433 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001434 return NO_ERROR;
1435 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001436 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1437 int policy = 0;
1438 sched_param param;
1439 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1440 return UNKNOWN_ERROR;
1441 }
1442 reply->writeInt32(policy);
1443 reply->writeInt32(param.sched_priority);
1444 return NO_ERROR;
1445 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001446 case BINDER_LIB_TEST_ECHO_VECTOR: {
1447 std::vector<uint64_t> vector;
1448 auto err = data.readUint64Vector(&vector);
1449 if (err != NO_ERROR)
1450 return err;
1451 reply->writeUint64Vector(vector);
1452 return NO_ERROR;
1453 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001454 case BINDER_LIB_TEST_REJECT_BUF: {
1455 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1456 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001457 default:
1458 return UNKNOWN_TRANSACTION;
1459 };
1460 }
1461 private:
1462 int32_t m_id;
1463 int32_t m_nextServerId;
1464 pthread_mutex_t m_serverWaitMutex;
1465 pthread_cond_t m_serverWaitCond;
1466 bool m_serverStartRequested;
1467 sp<IBinder> m_serverStarted;
1468 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001469 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001470};
1471
Martijn Coenen45b07b42017-08-09 12:07:45 +02001472int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001473{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001474 binderLibTestServiceName += String16(binderserversuffix);
1475
Riley Andrews06b01ad2014-12-18 12:10:08 -08001476 status_t ret;
1477 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001478 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001479 {
1480 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001481
Steven Morelandbf1915b2020-07-16 22:43:02 +00001482 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1483
Steven Morelandcf03cf12020-12-04 02:58:40 +00001484 testService->setInheritRt(true);
1485
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001486 /*
1487 * Normally would also contain functionality as well, but we are only
1488 * testing the extension mechanism.
1489 */
1490 testService->setExtension(new BBinder());
1491
Martijn Coenen82c75312019-07-24 15:18:30 +02001492 // Required for test "BufRejected'
1493 testService->setRequestingSid(true);
1494
Martijn Coenen45b07b42017-08-09 12:07:45 +02001495 /*
1496 * We need this below, but can't hold a sp<> because it prevents the
1497 * node from being cleaned up automatically. It's safe in this case
1498 * because of how the tests are written.
1499 */
1500 testServicePtr = testService.get();
1501
Riley Andrews06b01ad2014-12-18 12:10:08 -08001502 if (index == 0) {
1503 ret = sm->addService(binderLibTestServiceName, testService);
1504 } else {
1505 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1506 Parcel data, reply;
1507 data.writeInt32(index);
1508 data.writeStrongBinder(testService);
1509
1510 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1511 }
1512 }
1513 write(readypipefd, &ret, sizeof(ret));
1514 close(readypipefd);
1515 //printf("%s: ret %d\n", __func__, ret);
1516 if (ret)
1517 return 1;
1518 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001519 if (usePoll) {
1520 int fd;
1521 struct epoll_event ev;
1522 int epoll_fd;
1523 IPCThreadState::self()->setupPolling(&fd);
1524 if (fd < 0) {
1525 return 1;
1526 }
1527 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1528
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001529 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001530 if (epoll_fd == -1) {
1531 return 1;
1532 }
1533
1534 ev.events = EPOLLIN;
1535 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1536 return 1;
1537 }
1538
1539 while (1) {
1540 /*
1541 * We simulate a single-threaded process using the binder poll
1542 * interface; besides handling binder commands, it can also
1543 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001544 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001545 *
1546 * processPendingCall() will then issue that transaction.
1547 */
1548 struct epoll_event events[1];
1549 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1550 if (numEvents < 0) {
1551 if (errno == EINTR) {
1552 continue;
1553 }
1554 return 1;
1555 }
1556 if (numEvents > 0) {
1557 IPCThreadState::self()->handlePolledCommands();
1558 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1559 testServicePtr->processPendingCall();
1560 }
1561 }
1562 } else {
1563 ProcessState::self()->startThreadPool();
1564 IPCThreadState::self()->joinThreadPool();
1565 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001566 //printf("%s: joinThreadPool returned\n", __func__);
1567 return 1; /* joinThreadPool should not return */
1568}
1569
1570int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001571 ExitIfWrongAbi();
1572
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001573 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001574 binderservername = argv[2];
1575 } else {
1576 binderservername = argv[0];
1577 }
1578
Martijn Coenen45b07b42017-08-09 12:07:45 +02001579 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1580 binderserversuffix = argv[5];
1581 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001582 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001583 binderserversuffix = new char[16];
1584 snprintf(binderserversuffix, 16, "%d", getpid());
1585 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001586
1587 ::testing::InitGoogleTest(&argc, argv);
1588 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1589 ProcessState::self()->startThreadPool();
1590 return RUN_ALL_TESTS();
1591}