blob: 64ed7040c0707c8f691babccd888df7d8c970155 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
Marco Ballesio7ee17572020-09-08 10:30:03 -070019#include <fstream>
Riley Andrews06b01ad2014-12-18 12:10:08 -080020#include <poll.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <gtest/gtest.h>
26
27#include <binder/Binder.h>
28#include <binder/IBinder.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31
Steven Morelandd63ed9d2019-09-04 18:15:25 -070032#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020033#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080034#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020035
Steven Morelandf9f3de22020-05-06 17:14:39 -070036#include "binderAbiHelper.h"
37
Riley Andrews06b01ad2014-12-18 12:10:08 -080038#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
39
40using namespace android;
41
Sherry Yang336cdd32017-07-24 14:12:27 -070042static ::testing::AssertionResult IsPageAligned(void *buf) {
43 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
44 return ::testing::AssertionSuccess();
45 else
46 return ::testing::AssertionFailure() << buf << " is not page aligned";
47}
48
Riley Andrews06b01ad2014-12-18 12:10:08 -080049static testing::Environment* binder_env;
50static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070051static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080052static char binderserverarg[] = "--binderserver";
53
Steven Morelandbf1915b2020-07-16 22:43:02 +000054static constexpr int kSchedPolicy = SCHED_RR;
55static constexpr int kSchedPriority = 7;
56
Riley Andrews06b01ad2014-12-18 12:10:08 -080057static String16 binderLibTestServiceName = String16("test.binderLib");
58
59enum BinderLibTestTranscationCode {
60 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
61 BINDER_LIB_TEST_REGISTER_SERVER,
62 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020063 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080064 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070065 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020066 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080067 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070068 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_GET_ID_TRANSACTION,
70 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
71 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
72 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
73 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
74 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
75 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090076 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080077 BINDER_LIB_TEST_EXIT_TRANSACTION,
78 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
79 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070080 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010081 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000082 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070083 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
84 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080085 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020086 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080087};
88
Martijn Coenen45b07b42017-08-09 12:07:45 +020089pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080090{
91 int ret;
92 pid_t pid;
93 status_t status;
94 int pipefd[2];
95 char stri[16];
96 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020097 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080098 char *childargv[] = {
99 binderservername,
100 binderserverarg,
101 stri,
102 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200103 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700104 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700105 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800106 };
107
108 ret = pipe(pipefd);
109 if (ret < 0)
110 return ret;
111
112 snprintf(stri, sizeof(stri), "%d", arg2);
113 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200114 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800115
116 pid = fork();
117 if (pid == -1)
118 return pid;
119 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800120 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800121 close(pipefd[0]);
122 execv(binderservername, childargv);
123 status = -errno;
124 write(pipefd[1], &status, sizeof(status));
125 fprintf(stderr, "execv failed, %s\n", strerror(errno));
126 _exit(EXIT_FAILURE);
127 }
128 close(pipefd[1]);
129 ret = read(pipefd[0], &status, sizeof(status));
130 //printf("pipe read returned %d, status %d\n", ret, status);
131 close(pipefd[0]);
132 if (ret == sizeof(status)) {
133 ret = status;
134 } else {
135 kill(pid, SIGKILL);
136 if (ret >= 0) {
137 ret = NO_INIT;
138 }
139 }
140 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700141 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800142 return ret;
143 }
144 return pid;
145}
146
147class BinderLibTestEnv : public ::testing::Environment {
148 public:
149 BinderLibTestEnv() {}
150 sp<IBinder> getServer(void) {
151 return m_server;
152 }
153
154 private:
155 virtual void SetUp() {
156 m_serverpid = start_server_process(0);
157 //printf("m_serverpid %d\n", m_serverpid);
158 ASSERT_GT(m_serverpid, 0);
159
160 sp<IServiceManager> sm = defaultServiceManager();
161 //printf("%s: pid %d, get service\n", __func__, m_pid);
162 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700163 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800164 //printf("%s: pid %d, get service done\n", __func__, m_pid);
165 }
166 virtual void TearDown() {
167 status_t ret;
168 Parcel data, reply;
169 int exitStatus;
170 pid_t pid;
171
172 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700173 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800174 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
175 EXPECT_EQ(0, ret);
176 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
177 EXPECT_EQ(0, ret);
178 }
179 if (m_serverpid > 0) {
180 //printf("wait for %d\n", m_pids[i]);
181 pid = wait(&exitStatus);
182 EXPECT_EQ(m_serverpid, pid);
183 EXPECT_TRUE(WIFEXITED(exitStatus));
184 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
185 }
186 }
187
188 pid_t m_serverpid;
189 sp<IBinder> m_server;
190};
191
192class BinderLibTest : public ::testing::Test {
193 public:
194 virtual void SetUp() {
195 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000196 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 }
198 virtual void TearDown() {
199 }
200 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200201 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 {
203 int ret;
204 int32_t id;
205 Parcel data, reply;
206 sp<IBinder> binder;
207
Martijn Coenen45b07b42017-08-09 12:07:45 +0200208 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 EXPECT_EQ(NO_ERROR, ret);
210
Yi Kong91635562018-06-07 14:38:36 -0700211 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800212 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700213 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 ret = reply.readInt32(&id);
215 EXPECT_EQ(NO_ERROR, ret);
216 if (idPtr)
217 *idPtr = id;
218 return binder;
219 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200220
Yi Kong91635562018-06-07 14:38:36 -0700221 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200222 {
223 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
224 }
225
Yi Kong91635562018-06-07 14:38:36 -0700226 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200227 {
228 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
229 }
230
Riley Andrews06b01ad2014-12-18 12:10:08 -0800231 void waitForReadData(int fd, int timeout_ms) {
232 int ret;
233 pollfd pfd = pollfd();
234
235 pfd.fd = fd;
236 pfd.events = POLLIN;
237 ret = poll(&pfd, 1, timeout_ms);
238 EXPECT_EQ(1, ret);
239 }
240
241 sp<IBinder> m_server;
242};
243
244class BinderLibTestBundle : public Parcel
245{
246 public:
247 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800248 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800249 int32_t mark;
250 int32_t bundleLen;
251 size_t pos;
252
253 if (source->readInt32(&mark))
254 return;
255 if (mark != MARK_START)
256 return;
257 if (source->readInt32(&bundleLen))
258 return;
259 pos = source->dataPosition();
260 if (Parcel::appendFrom(source, pos, bundleLen))
261 return;
262 source->setDataPosition(pos + bundleLen);
263 if (source->readInt32(&mark))
264 return;
265 if (mark != MARK_END)
266 return;
267 m_isValid = true;
268 setDataPosition(0);
269 }
270 void appendTo(Parcel *dest) {
271 dest->writeInt32(MARK_START);
272 dest->writeInt32(dataSize());
273 dest->appendFrom(this, 0, dataSize());
274 dest->writeInt32(MARK_END);
275 };
276 bool isValid(void) {
277 return m_isValid;
278 }
279 private:
280 enum {
281 MARK_START = B_PACK_CHARS('B','T','B','S'),
282 MARK_END = B_PACK_CHARS('B','T','B','E'),
283 };
284 bool m_isValid;
285};
286
287class BinderLibTestEvent
288{
289 public:
290 BinderLibTestEvent(void)
291 : m_eventTriggered(false)
292 {
Yi Kong91635562018-06-07 14:38:36 -0700293 pthread_mutex_init(&m_waitMutex, nullptr);
294 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800295 }
296 int waitEvent(int timeout_s)
297 {
298 int ret;
299 pthread_mutex_lock(&m_waitMutex);
300 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800301 struct timespec ts;
302 clock_gettime(CLOCK_REALTIME, &ts);
303 ts.tv_sec += timeout_s;
304 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 }
306 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
307 pthread_mutex_unlock(&m_waitMutex);
308 return ret;
309 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 pthread_t getTriggeringThread()
311 {
312 return m_triggeringThread;
313 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314 protected:
315 void triggerEvent(void) {
316 pthread_mutex_lock(&m_waitMutex);
317 pthread_cond_signal(&m_waitCond);
318 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200319 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320 pthread_mutex_unlock(&m_waitMutex);
321 };
322 private:
323 pthread_mutex_t m_waitMutex;
324 pthread_cond_t m_waitCond;
325 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200326 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800327};
328
329class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
330{
331 public:
332 BinderLibTestCallBack()
333 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700334 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800335 {
336 }
337 status_t getResult(void)
338 {
339 return m_result;
340 }
341
342 private:
343 virtual status_t onTransact(uint32_t code,
344 const Parcel& data, Parcel* reply,
345 uint32_t flags = 0)
346 {
347 (void)reply;
348 (void)flags;
349 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200350 case BINDER_LIB_TEST_CALL_BACK: {
351 status_t status = data.readInt32(&m_result);
352 if (status != NO_ERROR) {
353 m_result = status;
354 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800355 triggerEvent();
356 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200357 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700358 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
359 sp<IBinder> server;
360 int ret;
361 const uint8_t *buf = data.data();
362 size_t size = data.dataSize();
363 if (m_prev_end) {
364 /* 64-bit kernel needs at most 8 bytes to align buffer end */
365 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
366 } else {
367 EXPECT_TRUE(IsPageAligned((void *)buf));
368 }
369
370 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
371
372 if (size > 0) {
373 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
374 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
375 data, reply);
376 EXPECT_EQ(NO_ERROR, ret);
377 }
378 return NO_ERROR;
379 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800380 default:
381 return UNKNOWN_TRANSACTION;
382 }
383 }
384
385 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700386 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800387};
388
389class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
390{
391 private:
392 virtual void binderDied(const wp<IBinder>& who) {
393 (void)who;
394 triggerEvent();
395 };
396};
397
398TEST_F(BinderLibTest, NopTransaction) {
399 status_t ret;
400 Parcel data, reply;
401 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
402 EXPECT_EQ(NO_ERROR, ret);
403}
404
Marco Ballesio7ee17572020-09-08 10:30:03 -0700405TEST_F(BinderLibTest, Freeze) {
406 status_t ret;
407 Parcel data, reply, replypid;
408 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
409
410 //Pass test on devices where the freezer is not supported
411 if (freezer_file.fail()) {
412 GTEST_SKIP();
413 return;
414 }
415
416 std::string freezer_enabled;
417 std::getline(freezer_file, freezer_enabled);
418
419 //Pass test on devices where the freezer is disabled
420 if (freezer_enabled != "1") {
421 GTEST_SKIP();
422 return;
423 }
424
425 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
426 int32_t pid = replypid.readInt32();
427 EXPECT_EQ(NO_ERROR, ret);
428 for (int i = 0; i < 10; i++) {
429 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
430 }
431 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
432 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
433 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
434 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700435
436 bool sync_received, async_received;
437
438 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
439 &async_received));
440
441 EXPECT_EQ(sync_received, 1);
442 EXPECT_EQ(async_received, 0);
443
Marco Ballesio7ee17572020-09-08 10:30:03 -0700444 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
445 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
446}
447
Riley Andrews06b01ad2014-12-18 12:10:08 -0800448TEST_F(BinderLibTest, SetError) {
449 int32_t testValue[] = { 0, -123, 123 };
450 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
451 status_t ret;
452 Parcel data, reply;
453 data.writeInt32(testValue[i]);
454 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
455 EXPECT_EQ(testValue[i], ret);
456 }
457}
458
459TEST_F(BinderLibTest, GetId) {
460 status_t ret;
461 int32_t id;
462 Parcel data, reply;
463 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
464 EXPECT_EQ(NO_ERROR, ret);
465 ret = reply.readInt32(&id);
466 EXPECT_EQ(NO_ERROR, ret);
467 EXPECT_EQ(0, id);
468}
469
470TEST_F(BinderLibTest, PtrSize) {
471 status_t ret;
472 int32_t ptrsize;
473 Parcel data, reply;
474 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700475 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800476 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
477 EXPECT_EQ(NO_ERROR, ret);
478 ret = reply.readInt32(&ptrsize);
479 EXPECT_EQ(NO_ERROR, ret);
480 RecordProperty("TestPtrSize", sizeof(void *));
481 RecordProperty("ServerPtrSize", sizeof(void *));
482}
483
484TEST_F(BinderLibTest, IndirectGetId2)
485{
486 status_t ret;
487 int32_t id;
488 int32_t count;
489 Parcel data, reply;
490 int32_t serverId[3];
491
492 data.writeInt32(ARRAY_SIZE(serverId));
493 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
494 sp<IBinder> server;
495 BinderLibTestBundle datai;
496
497 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700498 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800499 data.writeStrongBinder(server);
500 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
501 datai.appendTo(&data);
502 }
503
504 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
505 ASSERT_EQ(NO_ERROR, ret);
506
507 ret = reply.readInt32(&id);
508 ASSERT_EQ(NO_ERROR, ret);
509 EXPECT_EQ(0, id);
510
511 ret = reply.readInt32(&count);
512 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700513 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800514
515 for (size_t i = 0; i < (size_t)count; i++) {
516 BinderLibTestBundle replyi(&reply);
517 EXPECT_TRUE(replyi.isValid());
518 ret = replyi.readInt32(&id);
519 EXPECT_EQ(NO_ERROR, ret);
520 EXPECT_EQ(serverId[i], id);
521 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
522 }
523
524 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
525}
526
527TEST_F(BinderLibTest, IndirectGetId3)
528{
529 status_t ret;
530 int32_t id;
531 int32_t count;
532 Parcel data, reply;
533 int32_t serverId[3];
534
535 data.writeInt32(ARRAY_SIZE(serverId));
536 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
537 sp<IBinder> server;
538 BinderLibTestBundle datai;
539 BinderLibTestBundle datai2;
540
541 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700542 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800543 data.writeStrongBinder(server);
544 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
545
546 datai.writeInt32(1);
547 datai.writeStrongBinder(m_server);
548 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
549 datai2.appendTo(&datai);
550
551 datai.appendTo(&data);
552 }
553
554 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
555 ASSERT_EQ(NO_ERROR, ret);
556
557 ret = reply.readInt32(&id);
558 ASSERT_EQ(NO_ERROR, ret);
559 EXPECT_EQ(0, id);
560
561 ret = reply.readInt32(&count);
562 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700563 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800564
565 for (size_t i = 0; i < (size_t)count; i++) {
566 int32_t counti;
567
568 BinderLibTestBundle replyi(&reply);
569 EXPECT_TRUE(replyi.isValid());
570 ret = replyi.readInt32(&id);
571 EXPECT_EQ(NO_ERROR, ret);
572 EXPECT_EQ(serverId[i], id);
573
574 ret = replyi.readInt32(&counti);
575 ASSERT_EQ(NO_ERROR, ret);
576 EXPECT_EQ(1, counti);
577
578 BinderLibTestBundle replyi2(&replyi);
579 EXPECT_TRUE(replyi2.isValid());
580 ret = replyi2.readInt32(&id);
581 EXPECT_EQ(NO_ERROR, ret);
582 EXPECT_EQ(0, id);
583 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
584
585 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
586 }
587
588 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
589}
590
591TEST_F(BinderLibTest, CallBack)
592{
593 status_t ret;
594 Parcel data, reply;
595 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
596 data.writeStrongBinder(callBack);
597 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
598 EXPECT_EQ(NO_ERROR, ret);
599 ret = callBack->waitEvent(5);
600 EXPECT_EQ(NO_ERROR, ret);
601 ret = callBack->getResult();
602 EXPECT_EQ(NO_ERROR, ret);
603}
604
605TEST_F(BinderLibTest, AddServer)
606{
607 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700608 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800609}
610
Steven Moreland9f50ea62020-09-18 01:04:15 +0000611TEST_F(BinderLibTest, AddManagerToManager) {
612 sp<IServiceManager> sm = defaultServiceManager();
613 sp<IBinder> binder = IInterface::asBinder(sm);
614 EXPECT_EQ(NO_ERROR, sm->addService(String16("binderLibTest-manager"), binder));
615}
616
Riley Andrews06b01ad2014-12-18 12:10:08 -0800617TEST_F(BinderLibTest, DeathNotificationStrongRef)
618{
619 status_t ret;
620 sp<IBinder> sbinder;
621
622 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
623
624 {
625 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700626 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800627 ret = binder->linkToDeath(testDeathRecipient);
628 EXPECT_EQ(NO_ERROR, ret);
629 sbinder = binder;
630 }
631 {
632 Parcel data, reply;
633 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
634 EXPECT_EQ(0, ret);
635 }
636 IPCThreadState::self()->flushCommands();
637 ret = testDeathRecipient->waitEvent(5);
638 EXPECT_EQ(NO_ERROR, ret);
639 ret = sbinder->unlinkToDeath(testDeathRecipient);
640 EXPECT_EQ(DEAD_OBJECT, ret);
641}
642
643TEST_F(BinderLibTest, DeathNotificationMultiple)
644{
645 status_t ret;
646 const int clientcount = 2;
647 sp<IBinder> target;
648 sp<IBinder> linkedclient[clientcount];
649 sp<BinderLibTestCallBack> callBack[clientcount];
650 sp<IBinder> passiveclient[clientcount];
651
652 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700653 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800654 for (int i = 0; i < clientcount; i++) {
655 {
656 Parcel data, reply;
657
658 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700659 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800660 callBack[i] = new BinderLibTestCallBack();
661 data.writeStrongBinder(target);
662 data.writeStrongBinder(callBack[i]);
663 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
664 EXPECT_EQ(NO_ERROR, ret);
665 }
666 {
667 Parcel data, reply;
668
669 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700670 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800671 data.writeStrongBinder(target);
672 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
673 EXPECT_EQ(NO_ERROR, ret);
674 }
675 }
676 {
677 Parcel data, reply;
678 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
679 EXPECT_EQ(0, ret);
680 }
681
682 for (int i = 0; i < clientcount; i++) {
683 ret = callBack[i]->waitEvent(5);
684 EXPECT_EQ(NO_ERROR, ret);
685 ret = callBack[i]->getResult();
686 EXPECT_EQ(NO_ERROR, ret);
687 }
688}
689
Martijn Coenenf7100e42017-07-31 12:14:09 +0200690TEST_F(BinderLibTest, DeathNotificationThread)
691{
692 status_t ret;
693 sp<BinderLibTestCallBack> callback;
694 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700695 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200696 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700697 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200698
699 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
700
701 ret = target->linkToDeath(testDeathRecipient);
702 EXPECT_EQ(NO_ERROR, ret);
703
704 {
705 Parcel data, reply;
706 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
707 EXPECT_EQ(0, ret);
708 }
709
710 /* Make sure it's dead */
711 testDeathRecipient->waitEvent(5);
712
713 /* Now, pass the ref to another process and ask that process to
714 * call linkToDeath() on it, and wait for a response. This tests
715 * two things:
716 * 1) You still get death notifications when calling linkToDeath()
717 * on a ref that is already dead when it was passed to you.
718 * 2) That death notifications are not directly pushed to the thread
719 * registering them, but to the threadpool (proc workqueue) instead.
720 *
721 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
722 * is blocked on a condition variable waiting for the death notification to be
723 * called; therefore, that thread is not available for handling proc work.
724 * So, if the death notification was pushed to the thread workqueue, the callback
725 * would never be called, and the test would timeout and fail.
726 *
727 * Note that we can't do this part of the test from this thread itself, because
728 * the binder driver would only push death notifications to the thread if
729 * it is a looper thread, which this thread is not.
730 *
731 * See b/23525545 for details.
732 */
733 {
734 Parcel data, reply;
735
736 callback = new BinderLibTestCallBack();
737 data.writeStrongBinder(target);
738 data.writeStrongBinder(callback);
739 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
740 EXPECT_EQ(NO_ERROR, ret);
741 }
742
743 ret = callback->waitEvent(5);
744 EXPECT_EQ(NO_ERROR, ret);
745 ret = callback->getResult();
746 EXPECT_EQ(NO_ERROR, ret);
747}
748
Riley Andrews06b01ad2014-12-18 12:10:08 -0800749TEST_F(BinderLibTest, PassFile) {
750 int ret;
751 int pipefd[2];
752 uint8_t buf[1] = { 0 };
753 uint8_t write_value = 123;
754
755 ret = pipe2(pipefd, O_NONBLOCK);
756 ASSERT_EQ(0, ret);
757
758 {
759 Parcel data, reply;
760 uint8_t writebuf[1] = { write_value };
761
762 ret = data.writeFileDescriptor(pipefd[1], true);
763 EXPECT_EQ(NO_ERROR, ret);
764
765 ret = data.writeInt32(sizeof(writebuf));
766 EXPECT_EQ(NO_ERROR, ret);
767
768 ret = data.write(writebuf, sizeof(writebuf));
769 EXPECT_EQ(NO_ERROR, ret);
770
771 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
772 EXPECT_EQ(NO_ERROR, ret);
773 }
774
775 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700776 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800777 EXPECT_EQ(write_value, buf[0]);
778
779 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
780
781 ret = read(pipefd[0], buf, sizeof(buf));
782 EXPECT_EQ(0, ret);
783
784 close(pipefd[0]);
785}
786
Ryo Hashimotobf551892018-05-31 16:58:35 +0900787TEST_F(BinderLibTest, PassParcelFileDescriptor) {
788 const int datasize = 123;
789 std::vector<uint8_t> writebuf(datasize);
790 for (size_t i = 0; i < writebuf.size(); ++i) {
791 writebuf[i] = i;
792 }
793
794 android::base::unique_fd read_end, write_end;
795 {
796 int pipefd[2];
797 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
798 read_end.reset(pipefd[0]);
799 write_end.reset(pipefd[1]);
800 }
801 {
802 Parcel data;
803 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
804 write_end.reset();
805 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
806 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
807
808 Parcel reply;
809 EXPECT_EQ(NO_ERROR,
810 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
811 &reply));
812 }
813 std::vector<uint8_t> readbuf(datasize);
814 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
815 EXPECT_EQ(writebuf, readbuf);
816
817 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
818
819 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
820}
821
Riley Andrews06b01ad2014-12-18 12:10:08 -0800822TEST_F(BinderLibTest, PromoteLocal) {
823 sp<IBinder> strong = new BBinder();
824 wp<IBinder> weak = strong;
825 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700826 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800827 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700828 strong = nullptr;
829 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800830 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700831 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800832}
833
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700834TEST_F(BinderLibTest, LocalGetExtension) {
835 sp<BBinder> binder = new BBinder();
836 sp<IBinder> ext = new BBinder();
837 binder->setExtension(ext);
838 EXPECT_EQ(ext, binder->getExtension());
839}
840
841TEST_F(BinderLibTest, RemoteGetExtension) {
842 sp<IBinder> server = addServer();
843 ASSERT_TRUE(server != nullptr);
844
845 sp<IBinder> extension;
846 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
847 ASSERT_NE(nullptr, extension.get());
848
849 EXPECT_EQ(NO_ERROR, extension->pingBinder());
850}
851
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700852TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
853 status_t ret;
854 Parcel data, reply;
855
856 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
857 EXPECT_EQ(NO_ERROR, ret);
858
859 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700860 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800861 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
862 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
863 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
864 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700865}
866
Connor O'Brien52be2c92016-09-20 14:18:08 -0700867TEST_F(BinderLibTest, FreedBinder) {
868 status_t ret;
869
870 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700871 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700872
873 __u32 freedHandle;
874 wp<IBinder> keepFreedBinder;
875 {
876 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700877 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
878 ASSERT_EQ(NO_ERROR, ret);
879 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
880 freedHandle = freed->handle;
881 /* Add a weak ref to the freed binder so the driver does not
882 * delete its reference to it - otherwise the transaction
883 * fails regardless of whether the driver is fixed.
884 */
Steven Morelande171d622019-07-17 16:06:01 -0700885 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700886 }
Steven Morelande171d622019-07-17 16:06:01 -0700887 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700888 {
889 Parcel data, reply;
890 data.writeStrongBinder(server);
891 /* Replace original handle with handle to the freed binder */
892 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
893 __u32 oldHandle = strong->handle;
894 strong->handle = freedHandle;
895 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
896 /* Returns DEAD_OBJECT (-32) if target crashes and
897 * FAILED_TRANSACTION if the driver rejects the invalid
898 * object.
899 */
900 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
901 /* Restore original handle so parcel destructor does not use
902 * the wrong handle.
903 */
904 strong->handle = oldHandle;
905 }
906}
907
Sherry Yang336cdd32017-07-24 14:12:27 -0700908TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
909 status_t ret;
910 Parcel data, reply;
911 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
912 for (int i = 0; i < 2; i++) {
913 BinderLibTestBundle datai;
914 datai.appendFrom(&data, 0, data.dataSize());
915
916 data.freeData();
917 data.writeInt32(1);
918 data.writeStrongBinder(callBack);
919 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
920
921 datai.appendTo(&data);
922 }
923 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
924 EXPECT_EQ(NO_ERROR, ret);
925}
926
Martijn Coenen45b07b42017-08-09 12:07:45 +0200927TEST_F(BinderLibTest, OnewayQueueing)
928{
929 status_t ret;
930 Parcel data, data2;
931
932 sp<IBinder> pollServer = addPollServer();
933
934 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
935 data.writeStrongBinder(callBack);
936 data.writeInt32(500000); // delay in us before calling back
937
938 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
939 data2.writeStrongBinder(callBack2);
940 data2.writeInt32(0); // delay in us
941
Yi Kong91635562018-06-07 14:38:36 -0700942 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200943 EXPECT_EQ(NO_ERROR, ret);
944
945 // The delay ensures that this second transaction will end up on the async_todo list
946 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700947 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200948 EXPECT_EQ(NO_ERROR, ret);
949
950 // The server will ensure that the two transactions are handled in the expected order;
951 // If the ordering is not as expected, an error will be returned through the callbacks.
952 ret = callBack->waitEvent(2);
953 EXPECT_EQ(NO_ERROR, ret);
954 ret = callBack->getResult();
955 EXPECT_EQ(NO_ERROR, ret);
956
957 ret = callBack2->waitEvent(2);
958 EXPECT_EQ(NO_ERROR, ret);
959 ret = callBack2->getResult();
960 EXPECT_EQ(NO_ERROR, ret);
961}
962
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100963TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
964{
965 status_t ret;
966 Parcel data, reply;
967 data.writeInterfaceToken(binderLibTestServiceName);
968 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
969 EXPECT_EQ(-1, reply.readInt32());
970 EXPECT_EQ(NO_ERROR, ret);
971}
972
973TEST_F(BinderLibTest, WorkSourceSet)
974{
975 status_t ret;
976 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000977 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000978 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100979 data.writeInterfaceToken(binderLibTestServiceName);
980 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
981 EXPECT_EQ(100, reply.readInt32());
982 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000983 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
984 EXPECT_EQ(NO_ERROR, ret);
985}
986
987TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
988{
989 status_t ret;
990 Parcel data, reply;
991
992 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
993 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
994
995 data.writeInterfaceToken(binderLibTestServiceName);
996 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
997 EXPECT_EQ(-1, reply.readInt32());
998 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100999 EXPECT_EQ(NO_ERROR, ret);
1000}
1001
1002TEST_F(BinderLibTest, WorkSourceCleared)
1003{
1004 status_t ret;
1005 Parcel data, reply;
1006
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001007 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001008 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1009 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001010 data.writeInterfaceToken(binderLibTestServiceName);
1011 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1012
1013 EXPECT_EQ(-1, reply.readInt32());
1014 EXPECT_EQ(100, previousWorkSource);
1015 EXPECT_EQ(NO_ERROR, ret);
1016}
1017
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001018TEST_F(BinderLibTest, WorkSourceRestored)
1019{
1020 status_t ret;
1021 Parcel data, reply;
1022
1023 IPCThreadState::self()->setCallingWorkSourceUid(100);
1024 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1025 IPCThreadState::self()->restoreCallingWorkSource(token);
1026
1027 data.writeInterfaceToken(binderLibTestServiceName);
1028 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1029
1030 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001031 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001032 EXPECT_EQ(NO_ERROR, ret);
1033}
1034
Olivier Gaillard91a04802018-11-14 17:32:41 +00001035TEST_F(BinderLibTest, PropagateFlagSet)
1036{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001037 IPCThreadState::self()->clearPropagateWorkSource();
1038 IPCThreadState::self()->setCallingWorkSourceUid(100);
1039 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1040}
1041
1042TEST_F(BinderLibTest, PropagateFlagCleared)
1043{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001044 IPCThreadState::self()->setCallingWorkSourceUid(100);
1045 IPCThreadState::self()->clearPropagateWorkSource();
1046 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1047}
1048
1049TEST_F(BinderLibTest, PropagateFlagRestored)
1050{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001051 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1052 IPCThreadState::self()->restoreCallingWorkSource(token);
1053
1054 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1055}
1056
1057TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1058{
1059 IPCThreadState::self()->setCallingWorkSourceUid(100);
1060
1061 Parcel data, reply;
1062 status_t ret;
1063 data.writeInterfaceToken(binderLibTestServiceName);
1064 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1065
1066 Parcel data2, reply2;
1067 status_t ret2;
1068 data2.writeInterfaceToken(binderLibTestServiceName);
1069 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1070 EXPECT_EQ(100, reply2.readInt32());
1071 EXPECT_EQ(NO_ERROR, ret2);
1072}
1073
Steven Morelandbf1915b2020-07-16 22:43:02 +00001074TEST_F(BinderLibTest, SchedPolicySet) {
1075 sp<IBinder> server = addServer();
1076 ASSERT_TRUE(server != nullptr);
1077
1078 Parcel data, reply;
1079 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1080 EXPECT_EQ(NO_ERROR, ret);
1081
1082 int policy = reply.readInt32();
1083 int priority = reply.readInt32();
1084
1085 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1086 EXPECT_EQ(kSchedPriority, priority);
1087}
1088
1089
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001090TEST_F(BinderLibTest, VectorSent) {
1091 Parcel data, reply;
1092 sp<IBinder> server = addServer();
1093 ASSERT_TRUE(server != nullptr);
1094
1095 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1096 data.writeUint64Vector(testValue);
1097
1098 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1099 EXPECT_EQ(NO_ERROR, ret);
1100 std::vector<uint64_t> readValue;
1101 ret = reply.readUint64Vector(&readValue);
1102 EXPECT_EQ(readValue, testValue);
1103}
1104
Martijn Coenen82c75312019-07-24 15:18:30 +02001105TEST_F(BinderLibTest, BufRejected) {
1106 Parcel data, reply;
1107 uint32_t buf;
1108 sp<IBinder> server = addServer();
1109 ASSERT_TRUE(server != nullptr);
1110
1111 binder_buffer_object obj {
1112 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001113 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001114 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1115 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001116 };
1117 data.setDataCapacity(1024);
1118 // Write a bogus object at offset 0 to get an entry in the offset table
1119 data.writeFileDescriptor(0);
1120 EXPECT_EQ(data.objectsCount(), 1);
1121 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1122 // And now, overwrite it with the buffer object
1123 memcpy(parcelData, &obj, sizeof(obj));
1124 data.setDataSize(sizeof(obj));
1125
1126 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1127 // Either the kernel should reject this transaction (if it's correct), but
1128 // if it's not, the server implementation should return an error if it
1129 // finds an object in the received Parcel.
1130 EXPECT_NE(NO_ERROR, ret);
1131}
1132
Riley Andrews06b01ad2014-12-18 12:10:08 -08001133class BinderLibTestService : public BBinder
1134{
1135 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001136 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001137 : m_id(id)
1138 , m_nextServerId(id + 1)
1139 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001140 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001141 {
Yi Kong91635562018-06-07 14:38:36 -07001142 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1143 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001144 }
1145 ~BinderLibTestService()
1146 {
1147 exit(EXIT_SUCCESS);
1148 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001149
1150 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001151 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001152 Parcel data;
1153 data.writeInt32(NO_ERROR);
1154 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001155 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001156 }
1157 }
1158
Riley Andrews06b01ad2014-12-18 12:10:08 -08001159 virtual status_t onTransact(uint32_t code,
1160 const Parcel& data, Parcel* reply,
1161 uint32_t flags = 0) {
1162 //printf("%s: code %d\n", __func__, code);
1163 (void)flags;
1164
1165 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1166 return PERMISSION_DENIED;
1167 }
1168 switch (code) {
1169 case BINDER_LIB_TEST_REGISTER_SERVER: {
1170 int32_t id;
1171 sp<IBinder> binder;
1172 id = data.readInt32();
1173 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001174 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001175 return BAD_VALUE;
1176 }
1177
1178 if (m_id != 0)
1179 return INVALID_OPERATION;
1180
1181 pthread_mutex_lock(&m_serverWaitMutex);
1182 if (m_serverStartRequested) {
1183 m_serverStartRequested = false;
1184 m_serverStarted = binder;
1185 pthread_cond_signal(&m_serverWaitCond);
1186 }
1187 pthread_mutex_unlock(&m_serverWaitMutex);
1188 return NO_ERROR;
1189 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001190 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001191 case BINDER_LIB_TEST_ADD_SERVER: {
1192 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001193 int serverid;
1194
1195 if (m_id != 0) {
1196 return INVALID_OPERATION;
1197 }
1198 pthread_mutex_lock(&m_serverWaitMutex);
1199 if (m_serverStartRequested) {
1200 ret = -EBUSY;
1201 } else {
1202 serverid = m_nextServerId++;
1203 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001204 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001205
1206 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001207 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001208 pthread_mutex_lock(&m_serverWaitMutex);
1209 }
1210 if (ret > 0) {
1211 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001212 struct timespec ts;
1213 clock_gettime(CLOCK_REALTIME, &ts);
1214 ts.tv_sec += 5;
1215 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 }
1217 if (m_serverStartRequested) {
1218 m_serverStartRequested = false;
1219 ret = -ETIMEDOUT;
1220 } else {
1221 reply->writeStrongBinder(m_serverStarted);
1222 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001223 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001224 ret = NO_ERROR;
1225 }
1226 } else if (ret >= 0) {
1227 m_serverStartRequested = false;
1228 ret = UNKNOWN_ERROR;
1229 }
1230 pthread_mutex_unlock(&m_serverWaitMutex);
1231 return ret;
1232 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001233 case BINDER_LIB_TEST_GETPID:
1234 reply->writeInt32(getpid());
1235 return NO_ERROR;
1236 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1237 usleep(5000);
1238 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001239 case BINDER_LIB_TEST_NOP_TRANSACTION:
1240 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001241 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1242 // Note: this transaction is only designed for use with a
1243 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001244 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001245 // A callback was already pending; this means that
1246 // we received a second call while still processing
1247 // the first one. Fail the test.
1248 sp<IBinder> callback = data.readStrongBinder();
1249 Parcel data2;
1250 data2.writeInt32(UNKNOWN_ERROR);
1251
Yi Kong91635562018-06-07 14:38:36 -07001252 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001253 } else {
1254 m_callback = data.readStrongBinder();
1255 int32_t delayUs = data.readInt32();
1256 /*
1257 * It's necessary that we sleep here, so the next
1258 * transaction the caller makes will be queued to
1259 * the async queue.
1260 */
1261 usleep(delayUs);
1262
1263 /*
1264 * Now when we return, libbinder will tell the kernel
1265 * we are done with this transaction, and the kernel
1266 * can move the queued transaction to either the
1267 * thread todo worklist (for kernels without the fix),
1268 * or the proc todo worklist. In case of the former,
1269 * the next outbound call will pick up the pending
1270 * transaction, which leads to undesired reentrant
1271 * behavior. This is caught in the if() branch above.
1272 */
1273 }
1274
1275 return NO_ERROR;
1276 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001277 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1278 Parcel data2, reply2;
1279 sp<IBinder> binder;
1280 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001281 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001282 return BAD_VALUE;
1283 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001284 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001285 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1286 return NO_ERROR;
1287 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001288 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1289 reply->writeStrongBinder(this);
1290 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001291 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1292 reply->writeInt32(m_id);
1293 return NO_ERROR;
1294 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1295 int32_t count;
1296 uint32_t indirect_code;
1297 sp<IBinder> binder;
1298
1299 count = data.readInt32();
1300 reply->writeInt32(m_id);
1301 reply->writeInt32(count);
1302 for (int i = 0; i < count; i++) {
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 }
1307 indirect_code = data.readInt32();
1308 BinderLibTestBundle data2(&data);
1309 if (!data2.isValid()) {
1310 return BAD_VALUE;
1311 }
1312 BinderLibTestBundle reply2;
1313 binder->transact(indirect_code, data2, &reply2);
1314 reply2.appendTo(reply);
1315 }
1316 return NO_ERROR;
1317 }
1318 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1319 reply->setError(data.readInt32());
1320 return NO_ERROR;
1321 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1322 reply->writeInt32(sizeof(void *));
1323 return NO_ERROR;
1324 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1325 return NO_ERROR;
1326 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1327 m_strongRef = data.readStrongBinder();
1328 return NO_ERROR;
1329 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1330 int ret;
1331 Parcel data2, reply2;
1332 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1333 sp<IBinder> target;
1334 sp<IBinder> callback;
1335
1336 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001337 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001338 return BAD_VALUE;
1339 }
1340 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001341 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001342 return BAD_VALUE;
1343 }
1344 ret = target->linkToDeath(testDeathRecipient);
1345 if (ret == NO_ERROR)
1346 ret = testDeathRecipient->waitEvent(5);
1347 data2.writeInt32(ret);
1348 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1349 return NO_ERROR;
1350 }
1351 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1352 int ret;
1353 int32_t size;
1354 const void *buf;
1355 int fd;
1356
1357 fd = data.readFileDescriptor();
1358 if (fd < 0) {
1359 return BAD_VALUE;
1360 }
1361 ret = data.readInt32(&size);
1362 if (ret != NO_ERROR) {
1363 return ret;
1364 }
1365 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001366 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001367 return BAD_VALUE;
1368 }
1369 ret = write(fd, buf, size);
1370 if (ret != size)
1371 return UNKNOWN_ERROR;
1372 return NO_ERROR;
1373 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001374 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1375 int ret;
1376 int32_t size;
1377 const void *buf;
1378 android::base::unique_fd fd;
1379
1380 ret = data.readUniqueParcelFileDescriptor(&fd);
1381 if (ret != NO_ERROR) {
1382 return ret;
1383 }
1384 ret = data.readInt32(&size);
1385 if (ret != NO_ERROR) {
1386 return ret;
1387 }
1388 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001389 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001390 return BAD_VALUE;
1391 }
1392 ret = write(fd.get(), buf, size);
1393 if (ret != size) return UNKNOWN_ERROR;
1394 return NO_ERROR;
1395 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001396 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1397 alarm(10);
1398 return NO_ERROR;
1399 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001400 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401 ;
1402 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001403 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001404 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001405 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001406 return NO_ERROR;
1407 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001408 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1409 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001410 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001411 return NO_ERROR;
1412 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001413 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1414 int policy = 0;
1415 sched_param param;
1416 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1417 return UNKNOWN_ERROR;
1418 }
1419 reply->writeInt32(policy);
1420 reply->writeInt32(param.sched_priority);
1421 return NO_ERROR;
1422 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001423 case BINDER_LIB_TEST_ECHO_VECTOR: {
1424 std::vector<uint64_t> vector;
1425 auto err = data.readUint64Vector(&vector);
1426 if (err != NO_ERROR)
1427 return err;
1428 reply->writeUint64Vector(vector);
1429 return NO_ERROR;
1430 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001431 case BINDER_LIB_TEST_REJECT_BUF: {
1432 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1433 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001434 default:
1435 return UNKNOWN_TRANSACTION;
1436 };
1437 }
1438 private:
1439 int32_t m_id;
1440 int32_t m_nextServerId;
1441 pthread_mutex_t m_serverWaitMutex;
1442 pthread_cond_t m_serverWaitCond;
1443 bool m_serverStartRequested;
1444 sp<IBinder> m_serverStarted;
1445 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001446 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001447};
1448
Martijn Coenen45b07b42017-08-09 12:07:45 +02001449int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001450{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001451 binderLibTestServiceName += String16(binderserversuffix);
1452
Riley Andrews06b01ad2014-12-18 12:10:08 -08001453 status_t ret;
1454 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001455 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001456 {
1457 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001458
Steven Morelandbf1915b2020-07-16 22:43:02 +00001459 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1460
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001461 /*
1462 * Normally would also contain functionality as well, but we are only
1463 * testing the extension mechanism.
1464 */
1465 testService->setExtension(new BBinder());
1466
Martijn Coenen82c75312019-07-24 15:18:30 +02001467 // Required for test "BufRejected'
1468 testService->setRequestingSid(true);
1469
Martijn Coenen45b07b42017-08-09 12:07:45 +02001470 /*
1471 * We need this below, but can't hold a sp<> because it prevents the
1472 * node from being cleaned up automatically. It's safe in this case
1473 * because of how the tests are written.
1474 */
1475 testServicePtr = testService.get();
1476
Riley Andrews06b01ad2014-12-18 12:10:08 -08001477 if (index == 0) {
1478 ret = sm->addService(binderLibTestServiceName, testService);
1479 } else {
1480 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1481 Parcel data, reply;
1482 data.writeInt32(index);
1483 data.writeStrongBinder(testService);
1484
1485 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1486 }
1487 }
1488 write(readypipefd, &ret, sizeof(ret));
1489 close(readypipefd);
1490 //printf("%s: ret %d\n", __func__, ret);
1491 if (ret)
1492 return 1;
1493 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001494 if (usePoll) {
1495 int fd;
1496 struct epoll_event ev;
1497 int epoll_fd;
1498 IPCThreadState::self()->setupPolling(&fd);
1499 if (fd < 0) {
1500 return 1;
1501 }
1502 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1503
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001504 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001505 if (epoll_fd == -1) {
1506 return 1;
1507 }
1508
1509 ev.events = EPOLLIN;
1510 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1511 return 1;
1512 }
1513
1514 while (1) {
1515 /*
1516 * We simulate a single-threaded process using the binder poll
1517 * interface; besides handling binder commands, it can also
1518 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001519 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001520 *
1521 * processPendingCall() will then issue that transaction.
1522 */
1523 struct epoll_event events[1];
1524 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1525 if (numEvents < 0) {
1526 if (errno == EINTR) {
1527 continue;
1528 }
1529 return 1;
1530 }
1531 if (numEvents > 0) {
1532 IPCThreadState::self()->handlePolledCommands();
1533 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1534 testServicePtr->processPendingCall();
1535 }
1536 }
1537 } else {
1538 ProcessState::self()->startThreadPool();
1539 IPCThreadState::self()->joinThreadPool();
1540 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001541 //printf("%s: joinThreadPool returned\n", __func__);
1542 return 1; /* joinThreadPool should not return */
1543}
1544
1545int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001546 ExitIfWrongAbi();
1547
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001548 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001549 binderservername = argv[2];
1550 } else {
1551 binderservername = argv[0];
1552 }
1553
Martijn Coenen45b07b42017-08-09 12:07:45 +02001554 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1555 binderserversuffix = argv[5];
1556 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001557 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001558 binderserversuffix = new char[16];
1559 snprintf(binderserversuffix, 16, "%d", getpid());
1560 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001561
1562 ::testing::InitGoogleTest(&argc, argv);
1563 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1564 ProcessState::self()->startThreadPool();
1565 return RUN_ALL_TESTS();
1566}