blob: 54b6428ef13ac65126d6e393276b0bff662351c1 [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>
Steven Morelandd7088702021-01-13 00:27:00 +000024#include <thread>
Riley Andrews06b01ad2014-12-18 12:10:08 -080025
26#include <gtest/gtest.h>
27
28#include <binder/Binder.h>
29#include <binder/IBinder.h>
30#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Steven Morelandd7088702021-01-13 00:27:00 +000032#include <binder/ParcelRef.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080033
Steven Morelandd63ed9d2019-09-04 18:15:25 -070034#include <private/binder/binder_module.h>
Steven Morelandcf03cf12020-12-04 02:58:40 +000035#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020036#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080037#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020038
Steven Morelandf9f3de22020-05-06 17:14:39 -070039#include "binderAbiHelper.h"
40
Riley Andrews06b01ad2014-12-18 12:10:08 -080041#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
42
43using namespace android;
44
Sherry Yang336cdd32017-07-24 14:12:27 -070045static ::testing::AssertionResult IsPageAligned(void *buf) {
46 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
47 return ::testing::AssertionSuccess();
48 else
49 return ::testing::AssertionFailure() << buf << " is not page aligned";
50}
51
Riley Andrews06b01ad2014-12-18 12:10:08 -080052static testing::Environment* binder_env;
53static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070054static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080055static char binderserverarg[] = "--binderserver";
56
Steven Morelandbf1915b2020-07-16 22:43:02 +000057static constexpr int kSchedPolicy = SCHED_RR;
58static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000059static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000060
Riley Andrews06b01ad2014-12-18 12:10:08 -080061static String16 binderLibTestServiceName = String16("test.binderLib");
62
63enum BinderLibTestTranscationCode {
64 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
65 BINDER_LIB_TEST_REGISTER_SERVER,
66 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020067 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080068 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070069 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020070 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080071 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070072 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080073 BINDER_LIB_TEST_GET_ID_TRANSACTION,
74 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
75 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
76 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
77 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
78 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
79 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090080 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080081 BINDER_LIB_TEST_EXIT_TRANSACTION,
82 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
83 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070084 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010085 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000086 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070087 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
88 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080089 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020090 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080091};
92
Martijn Coenen45b07b42017-08-09 12:07:45 +020093pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080094{
95 int ret;
96 pid_t pid;
97 status_t status;
98 int pipefd[2];
99 char stri[16];
100 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200101 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800102 char *childargv[] = {
103 binderservername,
104 binderserverarg,
105 stri,
106 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200107 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700108 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700109 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800110 };
111
112 ret = pipe(pipefd);
113 if (ret < 0)
114 return ret;
115
116 snprintf(stri, sizeof(stri), "%d", arg2);
117 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200118 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800119
120 pid = fork();
121 if (pid == -1)
122 return pid;
123 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800124 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800125 close(pipefd[0]);
126 execv(binderservername, childargv);
127 status = -errno;
128 write(pipefd[1], &status, sizeof(status));
129 fprintf(stderr, "execv failed, %s\n", strerror(errno));
130 _exit(EXIT_FAILURE);
131 }
132 close(pipefd[1]);
133 ret = read(pipefd[0], &status, sizeof(status));
134 //printf("pipe read returned %d, status %d\n", ret, status);
135 close(pipefd[0]);
136 if (ret == sizeof(status)) {
137 ret = status;
138 } else {
139 kill(pid, SIGKILL);
140 if (ret >= 0) {
141 ret = NO_INIT;
142 }
143 }
144 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700145 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800146 return ret;
147 }
148 return pid;
149}
150
151class BinderLibTestEnv : public ::testing::Environment {
152 public:
153 BinderLibTestEnv() {}
154 sp<IBinder> getServer(void) {
155 return m_server;
156 }
157
158 private:
159 virtual void SetUp() {
160 m_serverpid = start_server_process(0);
161 //printf("m_serverpid %d\n", m_serverpid);
162 ASSERT_GT(m_serverpid, 0);
163
164 sp<IServiceManager> sm = defaultServiceManager();
165 //printf("%s: pid %d, get service\n", __func__, m_pid);
166 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700167 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800168 //printf("%s: pid %d, get service done\n", __func__, m_pid);
169 }
170 virtual void TearDown() {
171 status_t ret;
172 Parcel data, reply;
173 int exitStatus;
174 pid_t pid;
175
176 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700177 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800178 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
179 EXPECT_EQ(0, ret);
180 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
181 EXPECT_EQ(0, ret);
182 }
183 if (m_serverpid > 0) {
184 //printf("wait for %d\n", m_pids[i]);
185 pid = wait(&exitStatus);
186 EXPECT_EQ(m_serverpid, pid);
187 EXPECT_TRUE(WIFEXITED(exitStatus));
188 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
189 }
190 }
191
192 pid_t m_serverpid;
193 sp<IBinder> m_server;
194};
195
196class BinderLibTest : public ::testing::Test {
197 public:
198 virtual void SetUp() {
199 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000200 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800201 }
202 virtual void TearDown() {
203 }
204 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200205 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800206 {
207 int ret;
208 int32_t id;
209 Parcel data, reply;
210 sp<IBinder> binder;
211
Martijn Coenen45b07b42017-08-09 12:07:45 +0200212 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800213 EXPECT_EQ(NO_ERROR, ret);
214
Yi Kong91635562018-06-07 14:38:36 -0700215 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700217 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800218 ret = reply.readInt32(&id);
219 EXPECT_EQ(NO_ERROR, ret);
220 if (idPtr)
221 *idPtr = id;
222 return binder;
223 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200224
Yi Kong91635562018-06-07 14:38:36 -0700225 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200226 {
227 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
228 }
229
Yi Kong91635562018-06-07 14:38:36 -0700230 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200231 {
232 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
233 }
234
Riley Andrews06b01ad2014-12-18 12:10:08 -0800235 void waitForReadData(int fd, int timeout_ms) {
236 int ret;
237 pollfd pfd = pollfd();
238
239 pfd.fd = fd;
240 pfd.events = POLLIN;
241 ret = poll(&pfd, 1, timeout_ms);
242 EXPECT_EQ(1, ret);
243 }
244
245 sp<IBinder> m_server;
246};
247
248class BinderLibTestBundle : public Parcel
249{
250 public:
251 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800252 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800253 int32_t mark;
254 int32_t bundleLen;
255 size_t pos;
256
257 if (source->readInt32(&mark))
258 return;
259 if (mark != MARK_START)
260 return;
261 if (source->readInt32(&bundleLen))
262 return;
263 pos = source->dataPosition();
264 if (Parcel::appendFrom(source, pos, bundleLen))
265 return;
266 source->setDataPosition(pos + bundleLen);
267 if (source->readInt32(&mark))
268 return;
269 if (mark != MARK_END)
270 return;
271 m_isValid = true;
272 setDataPosition(0);
273 }
274 void appendTo(Parcel *dest) {
275 dest->writeInt32(MARK_START);
276 dest->writeInt32(dataSize());
277 dest->appendFrom(this, 0, dataSize());
278 dest->writeInt32(MARK_END);
279 };
280 bool isValid(void) {
281 return m_isValid;
282 }
283 private:
284 enum {
285 MARK_START = B_PACK_CHARS('B','T','B','S'),
286 MARK_END = B_PACK_CHARS('B','T','B','E'),
287 };
288 bool m_isValid;
289};
290
291class BinderLibTestEvent
292{
293 public:
294 BinderLibTestEvent(void)
295 : m_eventTriggered(false)
296 {
Yi Kong91635562018-06-07 14:38:36 -0700297 pthread_mutex_init(&m_waitMutex, nullptr);
298 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800299 }
300 int waitEvent(int timeout_s)
301 {
302 int ret;
303 pthread_mutex_lock(&m_waitMutex);
304 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 struct timespec ts;
306 clock_gettime(CLOCK_REALTIME, &ts);
307 ts.tv_sec += timeout_s;
308 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800309 }
310 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
311 pthread_mutex_unlock(&m_waitMutex);
312 return ret;
313 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200314 pthread_t getTriggeringThread()
315 {
316 return m_triggeringThread;
317 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800318 protected:
319 void triggerEvent(void) {
320 pthread_mutex_lock(&m_waitMutex);
321 pthread_cond_signal(&m_waitCond);
322 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200323 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800324 pthread_mutex_unlock(&m_waitMutex);
325 };
326 private:
327 pthread_mutex_t m_waitMutex;
328 pthread_cond_t m_waitCond;
329 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200330 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800331};
332
333class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
334{
335 public:
336 BinderLibTestCallBack()
337 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700338 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800339 {
340 }
341 status_t getResult(void)
342 {
343 return m_result;
344 }
345
346 private:
347 virtual status_t onTransact(uint32_t code,
348 const Parcel& data, Parcel* reply,
349 uint32_t flags = 0)
350 {
351 (void)reply;
352 (void)flags;
353 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200354 case BINDER_LIB_TEST_CALL_BACK: {
355 status_t status = data.readInt32(&m_result);
356 if (status != NO_ERROR) {
357 m_result = status;
358 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800359 triggerEvent();
360 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200361 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700362 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
363 sp<IBinder> server;
364 int ret;
365 const uint8_t *buf = data.data();
366 size_t size = data.dataSize();
367 if (m_prev_end) {
368 /* 64-bit kernel needs at most 8 bytes to align buffer end */
369 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
370 } else {
371 EXPECT_TRUE(IsPageAligned((void *)buf));
372 }
373
374 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
375
376 if (size > 0) {
377 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
378 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
379 data, reply);
380 EXPECT_EQ(NO_ERROR, ret);
381 }
382 return NO_ERROR;
383 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800384 default:
385 return UNKNOWN_TRANSACTION;
386 }
387 }
388
389 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700390 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800391};
392
393class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
394{
395 private:
396 virtual void binderDied(const wp<IBinder>& who) {
397 (void)who;
398 triggerEvent();
399 };
400};
401
402TEST_F(BinderLibTest, NopTransaction) {
403 status_t ret;
404 Parcel data, reply;
405 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
406 EXPECT_EQ(NO_ERROR, ret);
407}
408
Steven Morelandf183fdd2020-10-27 00:12:12 +0000409TEST_F(BinderLibTest, NopTransactionClear) {
410 status_t ret;
411 Parcel data, reply;
412 // make sure it accepts the transaction flag
413 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
414 EXPECT_EQ(NO_ERROR, ret);
415}
416
Marco Ballesio7ee17572020-09-08 10:30:03 -0700417TEST_F(BinderLibTest, Freeze) {
418 status_t ret;
419 Parcel data, reply, replypid;
420 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
421
422 //Pass test on devices where the freezer is not supported
423 if (freezer_file.fail()) {
424 GTEST_SKIP();
425 return;
426 }
427
428 std::string freezer_enabled;
429 std::getline(freezer_file, freezer_enabled);
430
431 //Pass test on devices where the freezer is disabled
432 if (freezer_enabled != "1") {
433 GTEST_SKIP();
434 return;
435 }
436
437 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
438 int32_t pid = replypid.readInt32();
439 EXPECT_EQ(NO_ERROR, ret);
440 for (int i = 0; i < 10; i++) {
441 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
442 }
443 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
444 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
445 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
446 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700447
448 bool sync_received, async_received;
449
450 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
451 &async_received));
452
453 EXPECT_EQ(sync_received, 1);
454 EXPECT_EQ(async_received, 0);
455
Marco Ballesio7ee17572020-09-08 10:30:03 -0700456 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
457 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
458}
459
Riley Andrews06b01ad2014-12-18 12:10:08 -0800460TEST_F(BinderLibTest, SetError) {
461 int32_t testValue[] = { 0, -123, 123 };
462 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
463 status_t ret;
464 Parcel data, reply;
465 data.writeInt32(testValue[i]);
466 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
467 EXPECT_EQ(testValue[i], ret);
468 }
469}
470
471TEST_F(BinderLibTest, GetId) {
472 status_t ret;
473 int32_t id;
474 Parcel data, reply;
475 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
476 EXPECT_EQ(NO_ERROR, ret);
477 ret = reply.readInt32(&id);
478 EXPECT_EQ(NO_ERROR, ret);
479 EXPECT_EQ(0, id);
480}
481
482TEST_F(BinderLibTest, PtrSize) {
483 status_t ret;
484 int32_t ptrsize;
485 Parcel data, reply;
486 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700487 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800488 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
489 EXPECT_EQ(NO_ERROR, ret);
490 ret = reply.readInt32(&ptrsize);
491 EXPECT_EQ(NO_ERROR, ret);
492 RecordProperty("TestPtrSize", sizeof(void *));
493 RecordProperty("ServerPtrSize", sizeof(void *));
494}
495
496TEST_F(BinderLibTest, IndirectGetId2)
497{
498 status_t ret;
499 int32_t id;
500 int32_t count;
501 Parcel data, reply;
502 int32_t serverId[3];
503
504 data.writeInt32(ARRAY_SIZE(serverId));
505 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
506 sp<IBinder> server;
507 BinderLibTestBundle datai;
508
509 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700510 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800511 data.writeStrongBinder(server);
512 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
513 datai.appendTo(&data);
514 }
515
516 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
517 ASSERT_EQ(NO_ERROR, ret);
518
519 ret = reply.readInt32(&id);
520 ASSERT_EQ(NO_ERROR, ret);
521 EXPECT_EQ(0, id);
522
523 ret = reply.readInt32(&count);
524 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700525 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800526
527 for (size_t i = 0; i < (size_t)count; i++) {
528 BinderLibTestBundle replyi(&reply);
529 EXPECT_TRUE(replyi.isValid());
530 ret = replyi.readInt32(&id);
531 EXPECT_EQ(NO_ERROR, ret);
532 EXPECT_EQ(serverId[i], id);
533 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
534 }
535
536 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
537}
538
539TEST_F(BinderLibTest, IndirectGetId3)
540{
541 status_t ret;
542 int32_t id;
543 int32_t count;
544 Parcel data, reply;
545 int32_t serverId[3];
546
547 data.writeInt32(ARRAY_SIZE(serverId));
548 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
549 sp<IBinder> server;
550 BinderLibTestBundle datai;
551 BinderLibTestBundle datai2;
552
553 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700554 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800555 data.writeStrongBinder(server);
556 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
557
558 datai.writeInt32(1);
559 datai.writeStrongBinder(m_server);
560 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
561 datai2.appendTo(&datai);
562
563 datai.appendTo(&data);
564 }
565
566 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
567 ASSERT_EQ(NO_ERROR, ret);
568
569 ret = reply.readInt32(&id);
570 ASSERT_EQ(NO_ERROR, ret);
571 EXPECT_EQ(0, id);
572
573 ret = reply.readInt32(&count);
574 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700575 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800576
577 for (size_t i = 0; i < (size_t)count; i++) {
578 int32_t counti;
579
580 BinderLibTestBundle replyi(&reply);
581 EXPECT_TRUE(replyi.isValid());
582 ret = replyi.readInt32(&id);
583 EXPECT_EQ(NO_ERROR, ret);
584 EXPECT_EQ(serverId[i], id);
585
586 ret = replyi.readInt32(&counti);
587 ASSERT_EQ(NO_ERROR, ret);
588 EXPECT_EQ(1, counti);
589
590 BinderLibTestBundle replyi2(&replyi);
591 EXPECT_TRUE(replyi2.isValid());
592 ret = replyi2.readInt32(&id);
593 EXPECT_EQ(NO_ERROR, ret);
594 EXPECT_EQ(0, id);
595 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
596
597 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
598 }
599
600 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
601}
602
603TEST_F(BinderLibTest, CallBack)
604{
605 status_t ret;
606 Parcel data, reply;
607 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
608 data.writeStrongBinder(callBack);
609 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
610 EXPECT_EQ(NO_ERROR, ret);
611 ret = callBack->waitEvent(5);
612 EXPECT_EQ(NO_ERROR, ret);
613 ret = callBack->getResult();
614 EXPECT_EQ(NO_ERROR, ret);
615}
616
617TEST_F(BinderLibTest, AddServer)
618{
619 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700620 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621}
622
Riley Andrews06b01ad2014-12-18 12:10:08 -0800623TEST_F(BinderLibTest, DeathNotificationStrongRef)
624{
625 status_t ret;
626 sp<IBinder> sbinder;
627
628 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
629
630 {
631 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700632 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800633 ret = binder->linkToDeath(testDeathRecipient);
634 EXPECT_EQ(NO_ERROR, ret);
635 sbinder = binder;
636 }
637 {
638 Parcel data, reply;
639 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
640 EXPECT_EQ(0, ret);
641 }
642 IPCThreadState::self()->flushCommands();
643 ret = testDeathRecipient->waitEvent(5);
644 EXPECT_EQ(NO_ERROR, ret);
645 ret = sbinder->unlinkToDeath(testDeathRecipient);
646 EXPECT_EQ(DEAD_OBJECT, ret);
647}
648
649TEST_F(BinderLibTest, DeathNotificationMultiple)
650{
651 status_t ret;
652 const int clientcount = 2;
653 sp<IBinder> target;
654 sp<IBinder> linkedclient[clientcount];
655 sp<BinderLibTestCallBack> callBack[clientcount];
656 sp<IBinder> passiveclient[clientcount];
657
658 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700659 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800660 for (int i = 0; i < clientcount; i++) {
661 {
662 Parcel data, reply;
663
664 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700665 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800666 callBack[i] = new BinderLibTestCallBack();
667 data.writeStrongBinder(target);
668 data.writeStrongBinder(callBack[i]);
669 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
670 EXPECT_EQ(NO_ERROR, ret);
671 }
672 {
673 Parcel data, reply;
674
675 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700676 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800677 data.writeStrongBinder(target);
678 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
679 EXPECT_EQ(NO_ERROR, ret);
680 }
681 }
682 {
683 Parcel data, reply;
684 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
685 EXPECT_EQ(0, ret);
686 }
687
688 for (int i = 0; i < clientcount; i++) {
689 ret = callBack[i]->waitEvent(5);
690 EXPECT_EQ(NO_ERROR, ret);
691 ret = callBack[i]->getResult();
692 EXPECT_EQ(NO_ERROR, ret);
693 }
694}
695
Martijn Coenenf7100e42017-07-31 12:14:09 +0200696TEST_F(BinderLibTest, DeathNotificationThread)
697{
698 status_t ret;
699 sp<BinderLibTestCallBack> callback;
700 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700701 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200702 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700703 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200704
705 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
706
707 ret = target->linkToDeath(testDeathRecipient);
708 EXPECT_EQ(NO_ERROR, ret);
709
710 {
711 Parcel data, reply;
712 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
713 EXPECT_EQ(0, ret);
714 }
715
716 /* Make sure it's dead */
717 testDeathRecipient->waitEvent(5);
718
719 /* Now, pass the ref to another process and ask that process to
720 * call linkToDeath() on it, and wait for a response. This tests
721 * two things:
722 * 1) You still get death notifications when calling linkToDeath()
723 * on a ref that is already dead when it was passed to you.
724 * 2) That death notifications are not directly pushed to the thread
725 * registering them, but to the threadpool (proc workqueue) instead.
726 *
727 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
728 * is blocked on a condition variable waiting for the death notification to be
729 * called; therefore, that thread is not available for handling proc work.
730 * So, if the death notification was pushed to the thread workqueue, the callback
731 * would never be called, and the test would timeout and fail.
732 *
733 * Note that we can't do this part of the test from this thread itself, because
734 * the binder driver would only push death notifications to the thread if
735 * it is a looper thread, which this thread is not.
736 *
737 * See b/23525545 for details.
738 */
739 {
740 Parcel data, reply;
741
742 callback = new BinderLibTestCallBack();
743 data.writeStrongBinder(target);
744 data.writeStrongBinder(callback);
745 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
746 EXPECT_EQ(NO_ERROR, ret);
747 }
748
749 ret = callback->waitEvent(5);
750 EXPECT_EQ(NO_ERROR, ret);
751 ret = callback->getResult();
752 EXPECT_EQ(NO_ERROR, ret);
753}
754
Riley Andrews06b01ad2014-12-18 12:10:08 -0800755TEST_F(BinderLibTest, PassFile) {
756 int ret;
757 int pipefd[2];
758 uint8_t buf[1] = { 0 };
759 uint8_t write_value = 123;
760
761 ret = pipe2(pipefd, O_NONBLOCK);
762 ASSERT_EQ(0, ret);
763
764 {
765 Parcel data, reply;
766 uint8_t writebuf[1] = { write_value };
767
768 ret = data.writeFileDescriptor(pipefd[1], true);
769 EXPECT_EQ(NO_ERROR, ret);
770
771 ret = data.writeInt32(sizeof(writebuf));
772 EXPECT_EQ(NO_ERROR, ret);
773
774 ret = data.write(writebuf, sizeof(writebuf));
775 EXPECT_EQ(NO_ERROR, ret);
776
777 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
778 EXPECT_EQ(NO_ERROR, ret);
779 }
780
781 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700782 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800783 EXPECT_EQ(write_value, buf[0]);
784
785 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
786
787 ret = read(pipefd[0], buf, sizeof(buf));
788 EXPECT_EQ(0, ret);
789
790 close(pipefd[0]);
791}
792
Ryo Hashimotobf551892018-05-31 16:58:35 +0900793TEST_F(BinderLibTest, PassParcelFileDescriptor) {
794 const int datasize = 123;
795 std::vector<uint8_t> writebuf(datasize);
796 for (size_t i = 0; i < writebuf.size(); ++i) {
797 writebuf[i] = i;
798 }
799
800 android::base::unique_fd read_end, write_end;
801 {
802 int pipefd[2];
803 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
804 read_end.reset(pipefd[0]);
805 write_end.reset(pipefd[1]);
806 }
807 {
808 Parcel data;
809 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
810 write_end.reset();
811 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
812 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
813
814 Parcel reply;
815 EXPECT_EQ(NO_ERROR,
816 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
817 &reply));
818 }
819 std::vector<uint8_t> readbuf(datasize);
820 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
821 EXPECT_EQ(writebuf, readbuf);
822
823 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
824
825 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
826}
827
Riley Andrews06b01ad2014-12-18 12:10:08 -0800828TEST_F(BinderLibTest, PromoteLocal) {
829 sp<IBinder> strong = new BBinder();
830 wp<IBinder> weak = strong;
831 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700832 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800833 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700834 strong = nullptr;
835 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800836 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700837 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800838}
839
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700840TEST_F(BinderLibTest, LocalGetExtension) {
841 sp<BBinder> binder = new BBinder();
842 sp<IBinder> ext = new BBinder();
843 binder->setExtension(ext);
844 EXPECT_EQ(ext, binder->getExtension());
845}
846
847TEST_F(BinderLibTest, RemoteGetExtension) {
848 sp<IBinder> server = addServer();
849 ASSERT_TRUE(server != nullptr);
850
851 sp<IBinder> extension;
852 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
853 ASSERT_NE(nullptr, extension.get());
854
855 EXPECT_EQ(NO_ERROR, extension->pingBinder());
856}
857
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700858TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
859 status_t ret;
860 Parcel data, reply;
861
862 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
863 EXPECT_EQ(NO_ERROR, ret);
864
865 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700866 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800867 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
868 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
869 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
870 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700871}
872
Connor O'Brien52be2c92016-09-20 14:18:08 -0700873TEST_F(BinderLibTest, FreedBinder) {
874 status_t ret;
875
876 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700877 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700878
879 __u32 freedHandle;
880 wp<IBinder> keepFreedBinder;
881 {
882 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700883 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
884 ASSERT_EQ(NO_ERROR, ret);
885 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
886 freedHandle = freed->handle;
887 /* Add a weak ref to the freed binder so the driver does not
888 * delete its reference to it - otherwise the transaction
889 * fails regardless of whether the driver is fixed.
890 */
Steven Morelande171d622019-07-17 16:06:01 -0700891 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700892 }
Steven Morelande171d622019-07-17 16:06:01 -0700893 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700894 {
895 Parcel data, reply;
896 data.writeStrongBinder(server);
897 /* Replace original handle with handle to the freed binder */
898 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
899 __u32 oldHandle = strong->handle;
900 strong->handle = freedHandle;
901 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
902 /* Returns DEAD_OBJECT (-32) if target crashes and
903 * FAILED_TRANSACTION if the driver rejects the invalid
904 * object.
905 */
906 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
907 /* Restore original handle so parcel destructor does not use
908 * the wrong handle.
909 */
910 strong->handle = oldHandle;
911 }
912}
913
Steven Morelandd7088702021-01-13 00:27:00 +0000914TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
915 sp<IBinder> server = addServer();
916 ASSERT_TRUE(server != nullptr);
917
918 Parcel data;
919 sp<ParcelRef> reply = ParcelRef::create();
920
921 // when we have a Parcel which is deleted on another thread, if it gets
922 // deleted, it will tell the kernel this, and it will drop strong references
923 // to binder, so that we can't BR_ACQUIRE would fail
924 IPCThreadState::self()->createTransactionReference(reply.get());
925 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
926 data,
927 reply.get()));
928
929 // we have sp to binder, but it is not actually acquired by kernel, the
930 // transaction is sitting on an out buffer
931 sp<IBinder> binder = reply->readStrongBinder();
932
933 std::thread([&] {
934 // without the transaction reference, this would cause the Parcel to be
935 // deallocated before the first thread flushes BR_ACQUIRE
936 reply = nullptr;
937 IPCThreadState::self()->flushCommands();
938 }).join();
939
940 ASSERT_NE(nullptr, binder);
941 ASSERT_EQ(NO_ERROR, binder->pingBinder());
942}
943
Sherry Yang336cdd32017-07-24 14:12:27 -0700944TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
945 status_t ret;
946 Parcel data, reply;
947 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
948 for (int i = 0; i < 2; i++) {
949 BinderLibTestBundle datai;
950 datai.appendFrom(&data, 0, data.dataSize());
951
952 data.freeData();
953 data.writeInt32(1);
954 data.writeStrongBinder(callBack);
955 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
956
957 datai.appendTo(&data);
958 }
959 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
960 EXPECT_EQ(NO_ERROR, ret);
961}
962
Martijn Coenen45b07b42017-08-09 12:07:45 +0200963TEST_F(BinderLibTest, OnewayQueueing)
964{
965 status_t ret;
966 Parcel data, data2;
967
968 sp<IBinder> pollServer = addPollServer();
969
970 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
971 data.writeStrongBinder(callBack);
972 data.writeInt32(500000); // delay in us before calling back
973
974 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
975 data2.writeStrongBinder(callBack2);
976 data2.writeInt32(0); // delay in us
977
Yi Kong91635562018-06-07 14:38:36 -0700978 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200979 EXPECT_EQ(NO_ERROR, ret);
980
981 // The delay ensures that this second transaction will end up on the async_todo list
982 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700983 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200984 EXPECT_EQ(NO_ERROR, ret);
985
986 // The server will ensure that the two transactions are handled in the expected order;
987 // If the ordering is not as expected, an error will be returned through the callbacks.
988 ret = callBack->waitEvent(2);
989 EXPECT_EQ(NO_ERROR, ret);
990 ret = callBack->getResult();
991 EXPECT_EQ(NO_ERROR, ret);
992
993 ret = callBack2->waitEvent(2);
994 EXPECT_EQ(NO_ERROR, ret);
995 ret = callBack2->getResult();
996 EXPECT_EQ(NO_ERROR, ret);
997}
998
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100999TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
1000{
1001 status_t ret;
1002 Parcel data, reply;
1003 data.writeInterfaceToken(binderLibTestServiceName);
1004 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1005 EXPECT_EQ(-1, reply.readInt32());
1006 EXPECT_EQ(NO_ERROR, ret);
1007}
1008
1009TEST_F(BinderLibTest, WorkSourceSet)
1010{
1011 status_t ret;
1012 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001013 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001014 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001015 data.writeInterfaceToken(binderLibTestServiceName);
1016 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1017 EXPECT_EQ(100, reply.readInt32());
1018 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001019 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1020 EXPECT_EQ(NO_ERROR, ret);
1021}
1022
1023TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1024{
1025 status_t ret;
1026 Parcel data, reply;
1027
1028 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1029 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1030
1031 data.writeInterfaceToken(binderLibTestServiceName);
1032 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1033 EXPECT_EQ(-1, reply.readInt32());
1034 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001035 EXPECT_EQ(NO_ERROR, ret);
1036}
1037
1038TEST_F(BinderLibTest, WorkSourceCleared)
1039{
1040 status_t ret;
1041 Parcel data, reply;
1042
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001043 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001044 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1045 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001046 data.writeInterfaceToken(binderLibTestServiceName);
1047 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1048
1049 EXPECT_EQ(-1, reply.readInt32());
1050 EXPECT_EQ(100, previousWorkSource);
1051 EXPECT_EQ(NO_ERROR, ret);
1052}
1053
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001054TEST_F(BinderLibTest, WorkSourceRestored)
1055{
1056 status_t ret;
1057 Parcel data, reply;
1058
1059 IPCThreadState::self()->setCallingWorkSourceUid(100);
1060 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1061 IPCThreadState::self()->restoreCallingWorkSource(token);
1062
1063 data.writeInterfaceToken(binderLibTestServiceName);
1064 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1065
1066 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001067 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001068 EXPECT_EQ(NO_ERROR, ret);
1069}
1070
Olivier Gaillard91a04802018-11-14 17:32:41 +00001071TEST_F(BinderLibTest, PropagateFlagSet)
1072{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001073 IPCThreadState::self()->clearPropagateWorkSource();
1074 IPCThreadState::self()->setCallingWorkSourceUid(100);
1075 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1076}
1077
1078TEST_F(BinderLibTest, PropagateFlagCleared)
1079{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001080 IPCThreadState::self()->setCallingWorkSourceUid(100);
1081 IPCThreadState::self()->clearPropagateWorkSource();
1082 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1083}
1084
1085TEST_F(BinderLibTest, PropagateFlagRestored)
1086{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001087 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1088 IPCThreadState::self()->restoreCallingWorkSource(token);
1089
1090 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1091}
1092
1093TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1094{
1095 IPCThreadState::self()->setCallingWorkSourceUid(100);
1096
1097 Parcel data, reply;
1098 status_t ret;
1099 data.writeInterfaceToken(binderLibTestServiceName);
1100 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1101
1102 Parcel data2, reply2;
1103 status_t ret2;
1104 data2.writeInterfaceToken(binderLibTestServiceName);
1105 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1106 EXPECT_EQ(100, reply2.readInt32());
1107 EXPECT_EQ(NO_ERROR, ret2);
1108}
1109
Steven Morelandbf1915b2020-07-16 22:43:02 +00001110TEST_F(BinderLibTest, SchedPolicySet) {
1111 sp<IBinder> server = addServer();
1112 ASSERT_TRUE(server != nullptr);
1113
1114 Parcel data, reply;
1115 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1116 EXPECT_EQ(NO_ERROR, ret);
1117
1118 int policy = reply.readInt32();
1119 int priority = reply.readInt32();
1120
1121 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1122 EXPECT_EQ(kSchedPriority, priority);
1123}
1124
Steven Morelandcf03cf12020-12-04 02:58:40 +00001125TEST_F(BinderLibTest, InheritRt) {
1126 sp<IBinder> server = addServer();
1127 ASSERT_TRUE(server != nullptr);
1128
1129 const struct sched_param param {
1130 .sched_priority = kSchedPriorityMore,
1131 };
1132 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1133
1134 Parcel data, reply;
1135 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1136 EXPECT_EQ(NO_ERROR, ret);
1137
1138 int policy = reply.readInt32();
1139 int priority = reply.readInt32();
1140
1141 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1142 EXPECT_EQ(kSchedPriorityMore, priority);
1143}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001144
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001145TEST_F(BinderLibTest, VectorSent) {
1146 Parcel data, reply;
1147 sp<IBinder> server = addServer();
1148 ASSERT_TRUE(server != nullptr);
1149
1150 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1151 data.writeUint64Vector(testValue);
1152
1153 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1154 EXPECT_EQ(NO_ERROR, ret);
1155 std::vector<uint64_t> readValue;
1156 ret = reply.readUint64Vector(&readValue);
1157 EXPECT_EQ(readValue, testValue);
1158}
1159
Martijn Coenen82c75312019-07-24 15:18:30 +02001160TEST_F(BinderLibTest, BufRejected) {
1161 Parcel data, reply;
1162 uint32_t buf;
1163 sp<IBinder> server = addServer();
1164 ASSERT_TRUE(server != nullptr);
1165
1166 binder_buffer_object obj {
1167 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001168 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001169 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1170 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001171 };
1172 data.setDataCapacity(1024);
1173 // Write a bogus object at offset 0 to get an entry in the offset table
1174 data.writeFileDescriptor(0);
1175 EXPECT_EQ(data.objectsCount(), 1);
1176 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1177 // And now, overwrite it with the buffer object
1178 memcpy(parcelData, &obj, sizeof(obj));
1179 data.setDataSize(sizeof(obj));
1180
1181 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1182 // Either the kernel should reject this transaction (if it's correct), but
1183 // if it's not, the server implementation should return an error if it
1184 // finds an object in the received Parcel.
1185 EXPECT_NE(NO_ERROR, ret);
1186}
1187
Riley Andrews06b01ad2014-12-18 12:10:08 -08001188class BinderLibTestService : public BBinder
1189{
1190 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001191 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001192 : m_id(id)
1193 , m_nextServerId(id + 1)
1194 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001195 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001196 {
Yi Kong91635562018-06-07 14:38:36 -07001197 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1198 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 }
1200 ~BinderLibTestService()
1201 {
1202 exit(EXIT_SUCCESS);
1203 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001204
1205 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001206 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001207 Parcel data;
1208 data.writeInt32(NO_ERROR);
1209 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001210 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001211 }
1212 }
1213
Riley Andrews06b01ad2014-12-18 12:10:08 -08001214 virtual status_t onTransact(uint32_t code,
1215 const Parcel& data, Parcel* reply,
1216 uint32_t flags = 0) {
1217 //printf("%s: code %d\n", __func__, code);
1218 (void)flags;
1219
1220 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1221 return PERMISSION_DENIED;
1222 }
1223 switch (code) {
1224 case BINDER_LIB_TEST_REGISTER_SERVER: {
1225 int32_t id;
1226 sp<IBinder> binder;
1227 id = data.readInt32();
1228 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001229 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001230 return BAD_VALUE;
1231 }
1232
1233 if (m_id != 0)
1234 return INVALID_OPERATION;
1235
1236 pthread_mutex_lock(&m_serverWaitMutex);
1237 if (m_serverStartRequested) {
1238 m_serverStartRequested = false;
1239 m_serverStarted = binder;
1240 pthread_cond_signal(&m_serverWaitCond);
1241 }
1242 pthread_mutex_unlock(&m_serverWaitMutex);
1243 return NO_ERROR;
1244 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001245 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001246 case BINDER_LIB_TEST_ADD_SERVER: {
1247 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001248 int serverid;
1249
1250 if (m_id != 0) {
1251 return INVALID_OPERATION;
1252 }
1253 pthread_mutex_lock(&m_serverWaitMutex);
1254 if (m_serverStartRequested) {
1255 ret = -EBUSY;
1256 } else {
1257 serverid = m_nextServerId++;
1258 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001259 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001260
1261 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001262 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001263 pthread_mutex_lock(&m_serverWaitMutex);
1264 }
1265 if (ret > 0) {
1266 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001267 struct timespec ts;
1268 clock_gettime(CLOCK_REALTIME, &ts);
1269 ts.tv_sec += 5;
1270 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 }
1272 if (m_serverStartRequested) {
1273 m_serverStartRequested = false;
1274 ret = -ETIMEDOUT;
1275 } else {
1276 reply->writeStrongBinder(m_serverStarted);
1277 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001278 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001279 ret = NO_ERROR;
1280 }
1281 } else if (ret >= 0) {
1282 m_serverStartRequested = false;
1283 ret = UNKNOWN_ERROR;
1284 }
1285 pthread_mutex_unlock(&m_serverWaitMutex);
1286 return ret;
1287 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001288 case BINDER_LIB_TEST_GETPID:
1289 reply->writeInt32(getpid());
1290 return NO_ERROR;
1291 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1292 usleep(5000);
1293 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001294 case BINDER_LIB_TEST_NOP_TRANSACTION:
1295 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001296 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1297 // Note: this transaction is only designed for use with a
1298 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001299 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001300 // A callback was already pending; this means that
1301 // we received a second call while still processing
1302 // the first one. Fail the test.
1303 sp<IBinder> callback = data.readStrongBinder();
1304 Parcel data2;
1305 data2.writeInt32(UNKNOWN_ERROR);
1306
Yi Kong91635562018-06-07 14:38:36 -07001307 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001308 } else {
1309 m_callback = data.readStrongBinder();
1310 int32_t delayUs = data.readInt32();
1311 /*
1312 * It's necessary that we sleep here, so the next
1313 * transaction the caller makes will be queued to
1314 * the async queue.
1315 */
1316 usleep(delayUs);
1317
1318 /*
1319 * Now when we return, libbinder will tell the kernel
1320 * we are done with this transaction, and the kernel
1321 * can move the queued transaction to either the
1322 * thread todo worklist (for kernels without the fix),
1323 * or the proc todo worklist. In case of the former,
1324 * the next outbound call will pick up the pending
1325 * transaction, which leads to undesired reentrant
1326 * behavior. This is caught in the if() branch above.
1327 */
1328 }
1329
1330 return NO_ERROR;
1331 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001332 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1333 Parcel data2, reply2;
1334 sp<IBinder> binder;
1335 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001336 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001337 return BAD_VALUE;
1338 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001339 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001340 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1341 return NO_ERROR;
1342 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001343 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1344 reply->writeStrongBinder(this);
1345 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001346 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1347 reply->writeInt32(m_id);
1348 return NO_ERROR;
1349 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1350 int32_t count;
1351 uint32_t indirect_code;
1352 sp<IBinder> binder;
1353
1354 count = data.readInt32();
1355 reply->writeInt32(m_id);
1356 reply->writeInt32(count);
1357 for (int i = 0; i < count; i++) {
1358 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001359 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001360 return BAD_VALUE;
1361 }
1362 indirect_code = data.readInt32();
1363 BinderLibTestBundle data2(&data);
1364 if (!data2.isValid()) {
1365 return BAD_VALUE;
1366 }
1367 BinderLibTestBundle reply2;
1368 binder->transact(indirect_code, data2, &reply2);
1369 reply2.appendTo(reply);
1370 }
1371 return NO_ERROR;
1372 }
1373 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1374 reply->setError(data.readInt32());
1375 return NO_ERROR;
1376 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1377 reply->writeInt32(sizeof(void *));
1378 return NO_ERROR;
1379 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1380 return NO_ERROR;
1381 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1382 m_strongRef = data.readStrongBinder();
1383 return NO_ERROR;
1384 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1385 int ret;
1386 Parcel data2, reply2;
1387 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1388 sp<IBinder> target;
1389 sp<IBinder> callback;
1390
1391 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001392 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001393 return BAD_VALUE;
1394 }
1395 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001396 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001397 return BAD_VALUE;
1398 }
1399 ret = target->linkToDeath(testDeathRecipient);
1400 if (ret == NO_ERROR)
1401 ret = testDeathRecipient->waitEvent(5);
1402 data2.writeInt32(ret);
1403 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1404 return NO_ERROR;
1405 }
1406 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1407 int ret;
1408 int32_t size;
1409 const void *buf;
1410 int fd;
1411
1412 fd = data.readFileDescriptor();
1413 if (fd < 0) {
1414 return BAD_VALUE;
1415 }
1416 ret = data.readInt32(&size);
1417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001421 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001422 return BAD_VALUE;
1423 }
1424 ret = write(fd, buf, size);
1425 if (ret != size)
1426 return UNKNOWN_ERROR;
1427 return NO_ERROR;
1428 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001429 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1430 int ret;
1431 int32_t size;
1432 const void *buf;
1433 android::base::unique_fd fd;
1434
1435 ret = data.readUniqueParcelFileDescriptor(&fd);
1436 if (ret != NO_ERROR) {
1437 return ret;
1438 }
1439 ret = data.readInt32(&size);
1440 if (ret != NO_ERROR) {
1441 return ret;
1442 }
1443 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001444 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001445 return BAD_VALUE;
1446 }
1447 ret = write(fd.get(), buf, size);
1448 if (ret != size) return UNKNOWN_ERROR;
1449 return NO_ERROR;
1450 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001451 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1452 alarm(10);
1453 return NO_ERROR;
1454 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001455 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001456 ;
1457 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001458 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001459 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001460 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001461 return NO_ERROR;
1462 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001463 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1464 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001465 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001466 return NO_ERROR;
1467 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001468 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1469 int policy = 0;
1470 sched_param param;
1471 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1472 return UNKNOWN_ERROR;
1473 }
1474 reply->writeInt32(policy);
1475 reply->writeInt32(param.sched_priority);
1476 return NO_ERROR;
1477 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001478 case BINDER_LIB_TEST_ECHO_VECTOR: {
1479 std::vector<uint64_t> vector;
1480 auto err = data.readUint64Vector(&vector);
1481 if (err != NO_ERROR)
1482 return err;
1483 reply->writeUint64Vector(vector);
1484 return NO_ERROR;
1485 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001486 case BINDER_LIB_TEST_REJECT_BUF: {
1487 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1488 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001489 default:
1490 return UNKNOWN_TRANSACTION;
1491 };
1492 }
1493 private:
1494 int32_t m_id;
1495 int32_t m_nextServerId;
1496 pthread_mutex_t m_serverWaitMutex;
1497 pthread_cond_t m_serverWaitCond;
1498 bool m_serverStartRequested;
1499 sp<IBinder> m_serverStarted;
1500 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001501 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001502};
1503
Martijn Coenen45b07b42017-08-09 12:07:45 +02001504int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001505{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001506 binderLibTestServiceName += String16(binderserversuffix);
1507
Riley Andrews06b01ad2014-12-18 12:10:08 -08001508 status_t ret;
1509 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001510 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001511 {
1512 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001513
Steven Morelandbf1915b2020-07-16 22:43:02 +00001514 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1515
Steven Morelandcf03cf12020-12-04 02:58:40 +00001516 testService->setInheritRt(true);
1517
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001518 /*
1519 * Normally would also contain functionality as well, but we are only
1520 * testing the extension mechanism.
1521 */
1522 testService->setExtension(new BBinder());
1523
Martijn Coenen82c75312019-07-24 15:18:30 +02001524 // Required for test "BufRejected'
1525 testService->setRequestingSid(true);
1526
Martijn Coenen45b07b42017-08-09 12:07:45 +02001527 /*
1528 * We need this below, but can't hold a sp<> because it prevents the
1529 * node from being cleaned up automatically. It's safe in this case
1530 * because of how the tests are written.
1531 */
1532 testServicePtr = testService.get();
1533
Riley Andrews06b01ad2014-12-18 12:10:08 -08001534 if (index == 0) {
1535 ret = sm->addService(binderLibTestServiceName, testService);
1536 } else {
1537 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1538 Parcel data, reply;
1539 data.writeInt32(index);
1540 data.writeStrongBinder(testService);
1541
1542 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1543 }
1544 }
1545 write(readypipefd, &ret, sizeof(ret));
1546 close(readypipefd);
1547 //printf("%s: ret %d\n", __func__, ret);
1548 if (ret)
1549 return 1;
1550 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001551 if (usePoll) {
1552 int fd;
1553 struct epoll_event ev;
1554 int epoll_fd;
1555 IPCThreadState::self()->setupPolling(&fd);
1556 if (fd < 0) {
1557 return 1;
1558 }
1559 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1560
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001561 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001562 if (epoll_fd == -1) {
1563 return 1;
1564 }
1565
1566 ev.events = EPOLLIN;
1567 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1568 return 1;
1569 }
1570
1571 while (1) {
1572 /*
1573 * We simulate a single-threaded process using the binder poll
1574 * interface; besides handling binder commands, it can also
1575 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001576 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001577 *
1578 * processPendingCall() will then issue that transaction.
1579 */
1580 struct epoll_event events[1];
1581 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1582 if (numEvents < 0) {
1583 if (errno == EINTR) {
1584 continue;
1585 }
1586 return 1;
1587 }
1588 if (numEvents > 0) {
1589 IPCThreadState::self()->handlePolledCommands();
1590 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1591 testServicePtr->processPendingCall();
1592 }
1593 }
1594 } else {
1595 ProcessState::self()->startThreadPool();
1596 IPCThreadState::self()->joinThreadPool();
1597 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001598 //printf("%s: joinThreadPool returned\n", __func__);
1599 return 1; /* joinThreadPool should not return */
1600}
1601
1602int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001603 ExitIfWrongAbi();
1604
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001605 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001606 binderservername = argv[2];
1607 } else {
1608 binderservername = argv[0];
1609 }
1610
Martijn Coenen45b07b42017-08-09 12:07:45 +02001611 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1612 binderserversuffix = argv[5];
1613 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001614 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001615 binderserversuffix = new char[16];
1616 snprintf(binderserversuffix, 16, "%d", getpid());
1617 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001618
1619 ::testing::InitGoogleTest(&argc, argv);
1620 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1621 ProcessState::self()->startThreadPool();
1622 return RUN_ALL_TESTS();
1623}