blob: a5261e59bffe2388b6c586bfdefe1133ca75c795 [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 Moreland80844f72020-12-12 02:06:08 +0000407TEST_F(BinderLibTest, NopTransactionOneway) {
408 status_t ret;
409 Parcel data, reply;
410 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY);
411 EXPECT_EQ(NO_ERROR, ret);
412}
413
Steven Morelandf183fdd2020-10-27 00:12:12 +0000414TEST_F(BinderLibTest, NopTransactionClear) {
415 status_t ret;
416 Parcel data, reply;
417 // make sure it accepts the transaction flag
418 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
419 EXPECT_EQ(NO_ERROR, ret);
420}
421
Marco Ballesio7ee17572020-09-08 10:30:03 -0700422TEST_F(BinderLibTest, Freeze) {
423 status_t ret;
424 Parcel data, reply, replypid;
425 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
426
427 //Pass test on devices where the freezer is not supported
428 if (freezer_file.fail()) {
429 GTEST_SKIP();
430 return;
431 }
432
433 std::string freezer_enabled;
434 std::getline(freezer_file, freezer_enabled);
435
436 //Pass test on devices where the freezer is disabled
437 if (freezer_enabled != "1") {
438 GTEST_SKIP();
439 return;
440 }
441
442 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
443 int32_t pid = replypid.readInt32();
444 EXPECT_EQ(NO_ERROR, ret);
445 for (int i = 0; i < 10; i++) {
446 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
447 }
448 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
449 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
450 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
451 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700452
453 bool sync_received, async_received;
454
455 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
456 &async_received));
457
458 EXPECT_EQ(sync_received, 1);
459 EXPECT_EQ(async_received, 0);
460
Marco Ballesio7ee17572020-09-08 10:30:03 -0700461 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
462 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
463}
464
Riley Andrews06b01ad2014-12-18 12:10:08 -0800465TEST_F(BinderLibTest, SetError) {
466 int32_t testValue[] = { 0, -123, 123 };
467 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
468 status_t ret;
469 Parcel data, reply;
470 data.writeInt32(testValue[i]);
471 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
472 EXPECT_EQ(testValue[i], ret);
473 }
474}
475
476TEST_F(BinderLibTest, GetId) {
477 status_t ret;
478 int32_t id;
479 Parcel data, reply;
480 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
481 EXPECT_EQ(NO_ERROR, ret);
482 ret = reply.readInt32(&id);
483 EXPECT_EQ(NO_ERROR, ret);
484 EXPECT_EQ(0, id);
485}
486
487TEST_F(BinderLibTest, PtrSize) {
488 status_t ret;
489 int32_t ptrsize;
490 Parcel data, reply;
491 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700492 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800493 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
494 EXPECT_EQ(NO_ERROR, ret);
495 ret = reply.readInt32(&ptrsize);
496 EXPECT_EQ(NO_ERROR, ret);
497 RecordProperty("TestPtrSize", sizeof(void *));
498 RecordProperty("ServerPtrSize", sizeof(void *));
499}
500
501TEST_F(BinderLibTest, IndirectGetId2)
502{
503 status_t ret;
504 int32_t id;
505 int32_t count;
506 Parcel data, reply;
507 int32_t serverId[3];
508
509 data.writeInt32(ARRAY_SIZE(serverId));
510 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
511 sp<IBinder> server;
512 BinderLibTestBundle datai;
513
514 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700515 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800516 data.writeStrongBinder(server);
517 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
518 datai.appendTo(&data);
519 }
520
521 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
522 ASSERT_EQ(NO_ERROR, ret);
523
524 ret = reply.readInt32(&id);
525 ASSERT_EQ(NO_ERROR, ret);
526 EXPECT_EQ(0, id);
527
528 ret = reply.readInt32(&count);
529 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700530 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800531
532 for (size_t i = 0; i < (size_t)count; i++) {
533 BinderLibTestBundle replyi(&reply);
534 EXPECT_TRUE(replyi.isValid());
535 ret = replyi.readInt32(&id);
536 EXPECT_EQ(NO_ERROR, ret);
537 EXPECT_EQ(serverId[i], id);
538 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
539 }
540
541 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
542}
543
544TEST_F(BinderLibTest, IndirectGetId3)
545{
546 status_t ret;
547 int32_t id;
548 int32_t count;
549 Parcel data, reply;
550 int32_t serverId[3];
551
552 data.writeInt32(ARRAY_SIZE(serverId));
553 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
554 sp<IBinder> server;
555 BinderLibTestBundle datai;
556 BinderLibTestBundle datai2;
557
558 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700559 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800560 data.writeStrongBinder(server);
561 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
562
563 datai.writeInt32(1);
564 datai.writeStrongBinder(m_server);
565 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
566 datai2.appendTo(&datai);
567
568 datai.appendTo(&data);
569 }
570
571 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
572 ASSERT_EQ(NO_ERROR, ret);
573
574 ret = reply.readInt32(&id);
575 ASSERT_EQ(NO_ERROR, ret);
576 EXPECT_EQ(0, id);
577
578 ret = reply.readInt32(&count);
579 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700580 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800581
582 for (size_t i = 0; i < (size_t)count; i++) {
583 int32_t counti;
584
585 BinderLibTestBundle replyi(&reply);
586 EXPECT_TRUE(replyi.isValid());
587 ret = replyi.readInt32(&id);
588 EXPECT_EQ(NO_ERROR, ret);
589 EXPECT_EQ(serverId[i], id);
590
591 ret = replyi.readInt32(&counti);
592 ASSERT_EQ(NO_ERROR, ret);
593 EXPECT_EQ(1, counti);
594
595 BinderLibTestBundle replyi2(&replyi);
596 EXPECT_TRUE(replyi2.isValid());
597 ret = replyi2.readInt32(&id);
598 EXPECT_EQ(NO_ERROR, ret);
599 EXPECT_EQ(0, id);
600 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
601
602 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
603 }
604
605 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
606}
607
608TEST_F(BinderLibTest, CallBack)
609{
610 status_t ret;
611 Parcel data, reply;
612 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
613 data.writeStrongBinder(callBack);
614 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
615 EXPECT_EQ(NO_ERROR, ret);
616 ret = callBack->waitEvent(5);
617 EXPECT_EQ(NO_ERROR, ret);
618 ret = callBack->getResult();
619 EXPECT_EQ(NO_ERROR, ret);
620}
621
622TEST_F(BinderLibTest, AddServer)
623{
624 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700625 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800626}
627
Riley Andrews06b01ad2014-12-18 12:10:08 -0800628TEST_F(BinderLibTest, DeathNotificationStrongRef)
629{
630 status_t ret;
631 sp<IBinder> sbinder;
632
633 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
634
635 {
636 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700637 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800638 ret = binder->linkToDeath(testDeathRecipient);
639 EXPECT_EQ(NO_ERROR, ret);
640 sbinder = binder;
641 }
642 {
643 Parcel data, reply;
644 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
645 EXPECT_EQ(0, ret);
646 }
647 IPCThreadState::self()->flushCommands();
648 ret = testDeathRecipient->waitEvent(5);
649 EXPECT_EQ(NO_ERROR, ret);
650 ret = sbinder->unlinkToDeath(testDeathRecipient);
651 EXPECT_EQ(DEAD_OBJECT, ret);
652}
653
654TEST_F(BinderLibTest, DeathNotificationMultiple)
655{
656 status_t ret;
657 const int clientcount = 2;
658 sp<IBinder> target;
659 sp<IBinder> linkedclient[clientcount];
660 sp<BinderLibTestCallBack> callBack[clientcount];
661 sp<IBinder> passiveclient[clientcount];
662
663 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700664 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800665 for (int i = 0; i < clientcount; i++) {
666 {
667 Parcel data, reply;
668
669 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700670 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800671 callBack[i] = new BinderLibTestCallBack();
672 data.writeStrongBinder(target);
673 data.writeStrongBinder(callBack[i]);
674 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
675 EXPECT_EQ(NO_ERROR, ret);
676 }
677 {
678 Parcel data, reply;
679
680 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700681 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800682 data.writeStrongBinder(target);
683 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
684 EXPECT_EQ(NO_ERROR, ret);
685 }
686 }
687 {
688 Parcel data, reply;
689 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
690 EXPECT_EQ(0, ret);
691 }
692
693 for (int i = 0; i < clientcount; i++) {
694 ret = callBack[i]->waitEvent(5);
695 EXPECT_EQ(NO_ERROR, ret);
696 ret = callBack[i]->getResult();
697 EXPECT_EQ(NO_ERROR, ret);
698 }
699}
700
Martijn Coenenf7100e42017-07-31 12:14:09 +0200701TEST_F(BinderLibTest, DeathNotificationThread)
702{
703 status_t ret;
704 sp<BinderLibTestCallBack> callback;
705 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700706 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200707 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700708 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200709
710 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
711
712 ret = target->linkToDeath(testDeathRecipient);
713 EXPECT_EQ(NO_ERROR, ret);
714
715 {
716 Parcel data, reply;
717 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
718 EXPECT_EQ(0, ret);
719 }
720
721 /* Make sure it's dead */
722 testDeathRecipient->waitEvent(5);
723
724 /* Now, pass the ref to another process and ask that process to
725 * call linkToDeath() on it, and wait for a response. This tests
726 * two things:
727 * 1) You still get death notifications when calling linkToDeath()
728 * on a ref that is already dead when it was passed to you.
729 * 2) That death notifications are not directly pushed to the thread
730 * registering them, but to the threadpool (proc workqueue) instead.
731 *
732 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
733 * is blocked on a condition variable waiting for the death notification to be
734 * called; therefore, that thread is not available for handling proc work.
735 * So, if the death notification was pushed to the thread workqueue, the callback
736 * would never be called, and the test would timeout and fail.
737 *
738 * Note that we can't do this part of the test from this thread itself, because
739 * the binder driver would only push death notifications to the thread if
740 * it is a looper thread, which this thread is not.
741 *
742 * See b/23525545 for details.
743 */
744 {
745 Parcel data, reply;
746
747 callback = new BinderLibTestCallBack();
748 data.writeStrongBinder(target);
749 data.writeStrongBinder(callback);
750 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
751 EXPECT_EQ(NO_ERROR, ret);
752 }
753
754 ret = callback->waitEvent(5);
755 EXPECT_EQ(NO_ERROR, ret);
756 ret = callback->getResult();
757 EXPECT_EQ(NO_ERROR, ret);
758}
759
Riley Andrews06b01ad2014-12-18 12:10:08 -0800760TEST_F(BinderLibTest, PassFile) {
761 int ret;
762 int pipefd[2];
763 uint8_t buf[1] = { 0 };
764 uint8_t write_value = 123;
765
766 ret = pipe2(pipefd, O_NONBLOCK);
767 ASSERT_EQ(0, ret);
768
769 {
770 Parcel data, reply;
771 uint8_t writebuf[1] = { write_value };
772
773 ret = data.writeFileDescriptor(pipefd[1], true);
774 EXPECT_EQ(NO_ERROR, ret);
775
776 ret = data.writeInt32(sizeof(writebuf));
777 EXPECT_EQ(NO_ERROR, ret);
778
779 ret = data.write(writebuf, sizeof(writebuf));
780 EXPECT_EQ(NO_ERROR, ret);
781
782 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
783 EXPECT_EQ(NO_ERROR, ret);
784 }
785
786 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700787 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800788 EXPECT_EQ(write_value, buf[0]);
789
790 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
791
792 ret = read(pipefd[0], buf, sizeof(buf));
793 EXPECT_EQ(0, ret);
794
795 close(pipefd[0]);
796}
797
Ryo Hashimotobf551892018-05-31 16:58:35 +0900798TEST_F(BinderLibTest, PassParcelFileDescriptor) {
799 const int datasize = 123;
800 std::vector<uint8_t> writebuf(datasize);
801 for (size_t i = 0; i < writebuf.size(); ++i) {
802 writebuf[i] = i;
803 }
804
805 android::base::unique_fd read_end, write_end;
806 {
807 int pipefd[2];
808 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
809 read_end.reset(pipefd[0]);
810 write_end.reset(pipefd[1]);
811 }
812 {
813 Parcel data;
814 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
815 write_end.reset();
816 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
817 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
818
819 Parcel reply;
820 EXPECT_EQ(NO_ERROR,
821 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
822 &reply));
823 }
824 std::vector<uint8_t> readbuf(datasize);
825 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
826 EXPECT_EQ(writebuf, readbuf);
827
828 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
829
830 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
831}
832
Riley Andrews06b01ad2014-12-18 12:10:08 -0800833TEST_F(BinderLibTest, PromoteLocal) {
834 sp<IBinder> strong = new BBinder();
835 wp<IBinder> weak = strong;
836 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700837 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800838 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700839 strong = nullptr;
840 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800841 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700842 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800843}
844
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700845TEST_F(BinderLibTest, LocalGetExtension) {
846 sp<BBinder> binder = new BBinder();
847 sp<IBinder> ext = new BBinder();
848 binder->setExtension(ext);
849 EXPECT_EQ(ext, binder->getExtension());
850}
851
852TEST_F(BinderLibTest, RemoteGetExtension) {
853 sp<IBinder> server = addServer();
854 ASSERT_TRUE(server != nullptr);
855
856 sp<IBinder> extension;
857 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
858 ASSERT_NE(nullptr, extension.get());
859
860 EXPECT_EQ(NO_ERROR, extension->pingBinder());
861}
862
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700863TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
864 status_t ret;
865 Parcel data, reply;
866
867 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
868 EXPECT_EQ(NO_ERROR, ret);
869
870 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700871 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800872 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
873 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
874 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
875 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700876}
877
Connor O'Brien52be2c92016-09-20 14:18:08 -0700878TEST_F(BinderLibTest, FreedBinder) {
879 status_t ret;
880
881 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700882 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700883
884 __u32 freedHandle;
885 wp<IBinder> keepFreedBinder;
886 {
887 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700888 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
889 ASSERT_EQ(NO_ERROR, ret);
890 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
891 freedHandle = freed->handle;
892 /* Add a weak ref to the freed binder so the driver does not
893 * delete its reference to it - otherwise the transaction
894 * fails regardless of whether the driver is fixed.
895 */
Steven Morelande171d622019-07-17 16:06:01 -0700896 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700897 }
Steven Morelande171d622019-07-17 16:06:01 -0700898 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700899 {
900 Parcel data, reply;
901 data.writeStrongBinder(server);
902 /* Replace original handle with handle to the freed binder */
903 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
904 __u32 oldHandle = strong->handle;
905 strong->handle = freedHandle;
906 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
907 /* Returns DEAD_OBJECT (-32) if target crashes and
908 * FAILED_TRANSACTION if the driver rejects the invalid
909 * object.
910 */
911 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
912 /* Restore original handle so parcel destructor does not use
913 * the wrong handle.
914 */
915 strong->handle = oldHandle;
916 }
917}
918
Sherry Yang336cdd32017-07-24 14:12:27 -0700919TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
920 status_t ret;
921 Parcel data, reply;
922 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
923 for (int i = 0; i < 2; i++) {
924 BinderLibTestBundle datai;
925 datai.appendFrom(&data, 0, data.dataSize());
926
927 data.freeData();
928 data.writeInt32(1);
929 data.writeStrongBinder(callBack);
930 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
931
932 datai.appendTo(&data);
933 }
934 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
935 EXPECT_EQ(NO_ERROR, ret);
936}
937
Martijn Coenen45b07b42017-08-09 12:07:45 +0200938TEST_F(BinderLibTest, OnewayQueueing)
939{
940 status_t ret;
941 Parcel data, data2;
942
943 sp<IBinder> pollServer = addPollServer();
944
945 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
946 data.writeStrongBinder(callBack);
947 data.writeInt32(500000); // delay in us before calling back
948
949 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
950 data2.writeStrongBinder(callBack2);
951 data2.writeInt32(0); // delay in us
952
Yi Kong91635562018-06-07 14:38:36 -0700953 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200954 EXPECT_EQ(NO_ERROR, ret);
955
956 // The delay ensures that this second transaction will end up on the async_todo list
957 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700958 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200959 EXPECT_EQ(NO_ERROR, ret);
960
961 // The server will ensure that the two transactions are handled in the expected order;
962 // If the ordering is not as expected, an error will be returned through the callbacks.
963 ret = callBack->waitEvent(2);
964 EXPECT_EQ(NO_ERROR, ret);
965 ret = callBack->getResult();
966 EXPECT_EQ(NO_ERROR, ret);
967
968 ret = callBack2->waitEvent(2);
969 EXPECT_EQ(NO_ERROR, ret);
970 ret = callBack2->getResult();
971 EXPECT_EQ(NO_ERROR, ret);
972}
973
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100974TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
975{
976 status_t ret;
977 Parcel data, reply;
978 data.writeInterfaceToken(binderLibTestServiceName);
979 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
980 EXPECT_EQ(-1, reply.readInt32());
981 EXPECT_EQ(NO_ERROR, ret);
982}
983
984TEST_F(BinderLibTest, WorkSourceSet)
985{
986 status_t ret;
987 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000988 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000989 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100990 data.writeInterfaceToken(binderLibTestServiceName);
991 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
992 EXPECT_EQ(100, reply.readInt32());
993 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000994 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
995 EXPECT_EQ(NO_ERROR, ret);
996}
997
998TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
999{
1000 status_t ret;
1001 Parcel data, reply;
1002
1003 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1004 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1005
1006 data.writeInterfaceToken(binderLibTestServiceName);
1007 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1008 EXPECT_EQ(-1, reply.readInt32());
1009 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001010 EXPECT_EQ(NO_ERROR, ret);
1011}
1012
1013TEST_F(BinderLibTest, WorkSourceCleared)
1014{
1015 status_t ret;
1016 Parcel data, reply;
1017
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001018 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001019 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1020 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001021 data.writeInterfaceToken(binderLibTestServiceName);
1022 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1023
1024 EXPECT_EQ(-1, reply.readInt32());
1025 EXPECT_EQ(100, previousWorkSource);
1026 EXPECT_EQ(NO_ERROR, ret);
1027}
1028
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001029TEST_F(BinderLibTest, WorkSourceRestored)
1030{
1031 status_t ret;
1032 Parcel data, reply;
1033
1034 IPCThreadState::self()->setCallingWorkSourceUid(100);
1035 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1036 IPCThreadState::self()->restoreCallingWorkSource(token);
1037
1038 data.writeInterfaceToken(binderLibTestServiceName);
1039 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1040
1041 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001042 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001043 EXPECT_EQ(NO_ERROR, ret);
1044}
1045
Olivier Gaillard91a04802018-11-14 17:32:41 +00001046TEST_F(BinderLibTest, PropagateFlagSet)
1047{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001048 IPCThreadState::self()->clearPropagateWorkSource();
1049 IPCThreadState::self()->setCallingWorkSourceUid(100);
1050 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1051}
1052
1053TEST_F(BinderLibTest, PropagateFlagCleared)
1054{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001055 IPCThreadState::self()->setCallingWorkSourceUid(100);
1056 IPCThreadState::self()->clearPropagateWorkSource();
1057 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1058}
1059
1060TEST_F(BinderLibTest, PropagateFlagRestored)
1061{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001062 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1063 IPCThreadState::self()->restoreCallingWorkSource(token);
1064
1065 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1066}
1067
1068TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1069{
1070 IPCThreadState::self()->setCallingWorkSourceUid(100);
1071
1072 Parcel data, reply;
1073 status_t ret;
1074 data.writeInterfaceToken(binderLibTestServiceName);
1075 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1076
1077 Parcel data2, reply2;
1078 status_t ret2;
1079 data2.writeInterfaceToken(binderLibTestServiceName);
1080 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1081 EXPECT_EQ(100, reply2.readInt32());
1082 EXPECT_EQ(NO_ERROR, ret2);
1083}
1084
Steven Morelandbf1915b2020-07-16 22:43:02 +00001085TEST_F(BinderLibTest, SchedPolicySet) {
1086 sp<IBinder> server = addServer();
1087 ASSERT_TRUE(server != nullptr);
1088
1089 Parcel data, reply;
1090 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1091 EXPECT_EQ(NO_ERROR, ret);
1092
1093 int policy = reply.readInt32();
1094 int priority = reply.readInt32();
1095
1096 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1097 EXPECT_EQ(kSchedPriority, priority);
1098}
1099
Steven Morelandcf03cf12020-12-04 02:58:40 +00001100TEST_F(BinderLibTest, InheritRt) {
1101 sp<IBinder> server = addServer();
1102 ASSERT_TRUE(server != nullptr);
1103
1104 const struct sched_param param {
1105 .sched_priority = kSchedPriorityMore,
1106 };
1107 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1108
1109 Parcel data, reply;
1110 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1111 EXPECT_EQ(NO_ERROR, ret);
1112
1113 int policy = reply.readInt32();
1114 int priority = reply.readInt32();
1115
1116 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1117 EXPECT_EQ(kSchedPriorityMore, priority);
1118}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001119
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001120TEST_F(BinderLibTest, VectorSent) {
1121 Parcel data, reply;
1122 sp<IBinder> server = addServer();
1123 ASSERT_TRUE(server != nullptr);
1124
1125 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1126 data.writeUint64Vector(testValue);
1127
1128 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1129 EXPECT_EQ(NO_ERROR, ret);
1130 std::vector<uint64_t> readValue;
1131 ret = reply.readUint64Vector(&readValue);
1132 EXPECT_EQ(readValue, testValue);
1133}
1134
Martijn Coenen82c75312019-07-24 15:18:30 +02001135TEST_F(BinderLibTest, BufRejected) {
1136 Parcel data, reply;
1137 uint32_t buf;
1138 sp<IBinder> server = addServer();
1139 ASSERT_TRUE(server != nullptr);
1140
1141 binder_buffer_object obj {
1142 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001143 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001144 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1145 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001146 };
1147 data.setDataCapacity(1024);
1148 // Write a bogus object at offset 0 to get an entry in the offset table
1149 data.writeFileDescriptor(0);
1150 EXPECT_EQ(data.objectsCount(), 1);
1151 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1152 // And now, overwrite it with the buffer object
1153 memcpy(parcelData, &obj, sizeof(obj));
1154 data.setDataSize(sizeof(obj));
1155
1156 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1157 // Either the kernel should reject this transaction (if it's correct), but
1158 // if it's not, the server implementation should return an error if it
1159 // finds an object in the received Parcel.
1160 EXPECT_NE(NO_ERROR, ret);
1161}
1162
Riley Andrews06b01ad2014-12-18 12:10:08 -08001163class BinderLibTestService : public BBinder
1164{
1165 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001166 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001167 : m_id(id)
1168 , m_nextServerId(id + 1)
1169 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001170 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001171 {
Yi Kong91635562018-06-07 14:38:36 -07001172 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1173 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001174 }
1175 ~BinderLibTestService()
1176 {
1177 exit(EXIT_SUCCESS);
1178 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001179
1180 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001181 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001182 Parcel data;
1183 data.writeInt32(NO_ERROR);
1184 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001185 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001186 }
1187 }
1188
Riley Andrews06b01ad2014-12-18 12:10:08 -08001189 virtual status_t onTransact(uint32_t code,
1190 const Parcel& data, Parcel* reply,
1191 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001192 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1193 return PERMISSION_DENIED;
1194 }
1195 switch (code) {
1196 case BINDER_LIB_TEST_REGISTER_SERVER: {
1197 int32_t id;
1198 sp<IBinder> binder;
1199 id = data.readInt32();
1200 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001201 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 return BAD_VALUE;
1203 }
1204
1205 if (m_id != 0)
1206 return INVALID_OPERATION;
1207
1208 pthread_mutex_lock(&m_serverWaitMutex);
1209 if (m_serverStartRequested) {
1210 m_serverStartRequested = false;
1211 m_serverStarted = binder;
1212 pthread_cond_signal(&m_serverWaitCond);
1213 }
1214 pthread_mutex_unlock(&m_serverWaitMutex);
1215 return NO_ERROR;
1216 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001217 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001218 case BINDER_LIB_TEST_ADD_SERVER: {
1219 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001220 int serverid;
1221
1222 if (m_id != 0) {
1223 return INVALID_OPERATION;
1224 }
1225 pthread_mutex_lock(&m_serverWaitMutex);
1226 if (m_serverStartRequested) {
1227 ret = -EBUSY;
1228 } else {
1229 serverid = m_nextServerId++;
1230 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001231 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001232
1233 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001234 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001235 pthread_mutex_lock(&m_serverWaitMutex);
1236 }
1237 if (ret > 0) {
1238 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001239 struct timespec ts;
1240 clock_gettime(CLOCK_REALTIME, &ts);
1241 ts.tv_sec += 5;
1242 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001243 }
1244 if (m_serverStartRequested) {
1245 m_serverStartRequested = false;
1246 ret = -ETIMEDOUT;
1247 } else {
1248 reply->writeStrongBinder(m_serverStarted);
1249 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001250 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001251 ret = NO_ERROR;
1252 }
1253 } else if (ret >= 0) {
1254 m_serverStartRequested = false;
1255 ret = UNKNOWN_ERROR;
1256 }
1257 pthread_mutex_unlock(&m_serverWaitMutex);
1258 return ret;
1259 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001260 case BINDER_LIB_TEST_GETPID:
1261 reply->writeInt32(getpid());
1262 return NO_ERROR;
1263 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1264 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001265 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001266 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001267 // oneway error codes should be ignored
1268 if (flags & TF_ONE_WAY) {
1269 return UNKNOWN_ERROR;
1270 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001272 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1273 // Note: this transaction is only designed for use with a
1274 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001275 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001276 // A callback was already pending; this means that
1277 // we received a second call while still processing
1278 // the first one. Fail the test.
1279 sp<IBinder> callback = data.readStrongBinder();
1280 Parcel data2;
1281 data2.writeInt32(UNKNOWN_ERROR);
1282
Yi Kong91635562018-06-07 14:38:36 -07001283 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001284 } else {
1285 m_callback = data.readStrongBinder();
1286 int32_t delayUs = data.readInt32();
1287 /*
1288 * It's necessary that we sleep here, so the next
1289 * transaction the caller makes will be queued to
1290 * the async queue.
1291 */
1292 usleep(delayUs);
1293
1294 /*
1295 * Now when we return, libbinder will tell the kernel
1296 * we are done with this transaction, and the kernel
1297 * can move the queued transaction to either the
1298 * thread todo worklist (for kernels without the fix),
1299 * or the proc todo worklist. In case of the former,
1300 * the next outbound call will pick up the pending
1301 * transaction, which leads to undesired reentrant
1302 * behavior. This is caught in the if() branch above.
1303 */
1304 }
1305
1306 return NO_ERROR;
1307 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001308 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1309 Parcel data2, reply2;
1310 sp<IBinder> binder;
1311 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001312 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001313 return BAD_VALUE;
1314 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001315 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001316 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1317 return NO_ERROR;
1318 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001319 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1320 reply->writeStrongBinder(this);
1321 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001322 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1323 reply->writeInt32(m_id);
1324 return NO_ERROR;
1325 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1326 int32_t count;
1327 uint32_t indirect_code;
1328 sp<IBinder> binder;
1329
1330 count = data.readInt32();
1331 reply->writeInt32(m_id);
1332 reply->writeInt32(count);
1333 for (int i = 0; i < count; i++) {
1334 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001335 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001336 return BAD_VALUE;
1337 }
1338 indirect_code = data.readInt32();
1339 BinderLibTestBundle data2(&data);
1340 if (!data2.isValid()) {
1341 return BAD_VALUE;
1342 }
1343 BinderLibTestBundle reply2;
1344 binder->transact(indirect_code, data2, &reply2);
1345 reply2.appendTo(reply);
1346 }
1347 return NO_ERROR;
1348 }
1349 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1350 reply->setError(data.readInt32());
1351 return NO_ERROR;
1352 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1353 reply->writeInt32(sizeof(void *));
1354 return NO_ERROR;
1355 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1356 return NO_ERROR;
1357 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1358 m_strongRef = data.readStrongBinder();
1359 return NO_ERROR;
1360 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1361 int ret;
1362 Parcel data2, reply2;
1363 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1364 sp<IBinder> target;
1365 sp<IBinder> callback;
1366
1367 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001368 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001369 return BAD_VALUE;
1370 }
1371 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001372 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001373 return BAD_VALUE;
1374 }
1375 ret = target->linkToDeath(testDeathRecipient);
1376 if (ret == NO_ERROR)
1377 ret = testDeathRecipient->waitEvent(5);
1378 data2.writeInt32(ret);
1379 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1380 return NO_ERROR;
1381 }
1382 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1383 int ret;
1384 int32_t size;
1385 const void *buf;
1386 int fd;
1387
1388 fd = data.readFileDescriptor();
1389 if (fd < 0) {
1390 return BAD_VALUE;
1391 }
1392 ret = data.readInt32(&size);
1393 if (ret != NO_ERROR) {
1394 return ret;
1395 }
1396 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001397 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398 return BAD_VALUE;
1399 }
1400 ret = write(fd, buf, size);
1401 if (ret != size)
1402 return UNKNOWN_ERROR;
1403 return NO_ERROR;
1404 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001405 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1406 int ret;
1407 int32_t size;
1408 const void *buf;
1409 android::base::unique_fd fd;
1410
1411 ret = data.readUniqueParcelFileDescriptor(&fd);
1412 if (ret != NO_ERROR) {
1413 return ret;
1414 }
1415 ret = data.readInt32(&size);
1416 if (ret != NO_ERROR) {
1417 return ret;
1418 }
1419 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001420 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001421 return BAD_VALUE;
1422 }
1423 ret = write(fd.get(), buf, size);
1424 if (ret != size) return UNKNOWN_ERROR;
1425 return NO_ERROR;
1426 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001427 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1428 alarm(10);
1429 return NO_ERROR;
1430 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001431 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001432 ;
1433 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001434 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001435 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001436 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001437 return NO_ERROR;
1438 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001439 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1440 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001441 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001442 return NO_ERROR;
1443 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001444 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1445 int policy = 0;
1446 sched_param param;
1447 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1448 return UNKNOWN_ERROR;
1449 }
1450 reply->writeInt32(policy);
1451 reply->writeInt32(param.sched_priority);
1452 return NO_ERROR;
1453 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001454 case BINDER_LIB_TEST_ECHO_VECTOR: {
1455 std::vector<uint64_t> vector;
1456 auto err = data.readUint64Vector(&vector);
1457 if (err != NO_ERROR)
1458 return err;
1459 reply->writeUint64Vector(vector);
1460 return NO_ERROR;
1461 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001462 case BINDER_LIB_TEST_REJECT_BUF: {
1463 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1464 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001465 default:
1466 return UNKNOWN_TRANSACTION;
1467 };
1468 }
1469 private:
1470 int32_t m_id;
1471 int32_t m_nextServerId;
1472 pthread_mutex_t m_serverWaitMutex;
1473 pthread_cond_t m_serverWaitCond;
1474 bool m_serverStartRequested;
1475 sp<IBinder> m_serverStarted;
1476 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001477 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001478};
1479
Martijn Coenen45b07b42017-08-09 12:07:45 +02001480int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001481{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001482 binderLibTestServiceName += String16(binderserversuffix);
1483
Riley Andrews06b01ad2014-12-18 12:10:08 -08001484 status_t ret;
1485 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001486 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001487 {
1488 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001489
Steven Morelandbf1915b2020-07-16 22:43:02 +00001490 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1491
Steven Morelandcf03cf12020-12-04 02:58:40 +00001492 testService->setInheritRt(true);
1493
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001494 /*
1495 * Normally would also contain functionality as well, but we are only
1496 * testing the extension mechanism.
1497 */
1498 testService->setExtension(new BBinder());
1499
Martijn Coenen82c75312019-07-24 15:18:30 +02001500 // Required for test "BufRejected'
1501 testService->setRequestingSid(true);
1502
Martijn Coenen45b07b42017-08-09 12:07:45 +02001503 /*
1504 * We need this below, but can't hold a sp<> because it prevents the
1505 * node from being cleaned up automatically. It's safe in this case
1506 * because of how the tests are written.
1507 */
1508 testServicePtr = testService.get();
1509
Riley Andrews06b01ad2014-12-18 12:10:08 -08001510 if (index == 0) {
1511 ret = sm->addService(binderLibTestServiceName, testService);
1512 } else {
1513 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1514 Parcel data, reply;
1515 data.writeInt32(index);
1516 data.writeStrongBinder(testService);
1517
1518 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1519 }
1520 }
1521 write(readypipefd, &ret, sizeof(ret));
1522 close(readypipefd);
1523 //printf("%s: ret %d\n", __func__, ret);
1524 if (ret)
1525 return 1;
1526 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001527 if (usePoll) {
1528 int fd;
1529 struct epoll_event ev;
1530 int epoll_fd;
1531 IPCThreadState::self()->setupPolling(&fd);
1532 if (fd < 0) {
1533 return 1;
1534 }
1535 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1536
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001537 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001538 if (epoll_fd == -1) {
1539 return 1;
1540 }
1541
1542 ev.events = EPOLLIN;
1543 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1544 return 1;
1545 }
1546
1547 while (1) {
1548 /*
1549 * We simulate a single-threaded process using the binder poll
1550 * interface; besides handling binder commands, it can also
1551 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001552 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001553 *
1554 * processPendingCall() will then issue that transaction.
1555 */
1556 struct epoll_event events[1];
1557 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1558 if (numEvents < 0) {
1559 if (errno == EINTR) {
1560 continue;
1561 }
1562 return 1;
1563 }
1564 if (numEvents > 0) {
1565 IPCThreadState::self()->handlePolledCommands();
1566 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1567 testServicePtr->processPendingCall();
1568 }
1569 }
1570 } else {
1571 ProcessState::self()->startThreadPool();
1572 IPCThreadState::self()->joinThreadPool();
1573 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001574 //printf("%s: joinThreadPool returned\n", __func__);
1575 return 1; /* joinThreadPool should not return */
1576}
1577
1578int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001579 ExitIfWrongAbi();
1580
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001581 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001582 binderservername = argv[2];
1583 } else {
1584 binderservername = argv[0];
1585 }
1586
Martijn Coenen45b07b42017-08-09 12:07:45 +02001587 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1588 binderserversuffix = argv[5];
1589 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001590 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001591 binderserversuffix = new char[16];
1592 snprintf(binderserversuffix, 16, "%d", getpid());
1593 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001594
1595 ::testing::InitGoogleTest(&argc, argv);
1596 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1597 ProcessState::self()->startThreadPool();
1598 return RUN_ALL_TESTS();
1599}