blob: e2193fadab879fcae0c5dc2a8e7eeb89dcfbf546 [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 Moreland80844f72020-12-12 02:06:08 +0000409TEST_F(BinderLibTest, NopTransactionOneway) {
410 status_t ret;
411 Parcel data, reply;
412 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY);
413 EXPECT_EQ(NO_ERROR, ret);
414}
415
Steven Morelandf183fdd2020-10-27 00:12:12 +0000416TEST_F(BinderLibTest, NopTransactionClear) {
417 status_t ret;
418 Parcel data, reply;
419 // make sure it accepts the transaction flag
420 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
421 EXPECT_EQ(NO_ERROR, ret);
422}
423
Marco Ballesio7ee17572020-09-08 10:30:03 -0700424TEST_F(BinderLibTest, Freeze) {
425 status_t ret;
426 Parcel data, reply, replypid;
427 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
428
429 //Pass test on devices where the freezer is not supported
430 if (freezer_file.fail()) {
431 GTEST_SKIP();
432 return;
433 }
434
435 std::string freezer_enabled;
436 std::getline(freezer_file, freezer_enabled);
437
438 //Pass test on devices where the freezer is disabled
439 if (freezer_enabled != "1") {
440 GTEST_SKIP();
441 return;
442 }
443
444 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
445 int32_t pid = replypid.readInt32();
446 EXPECT_EQ(NO_ERROR, ret);
447 for (int i = 0; i < 10; i++) {
448 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
449 }
450 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
451 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
452 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
453 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700454
455 bool sync_received, async_received;
456
457 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
458 &async_received));
459
460 EXPECT_EQ(sync_received, 1);
461 EXPECT_EQ(async_received, 0);
462
Marco Ballesio7ee17572020-09-08 10:30:03 -0700463 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
464 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
465}
466
Riley Andrews06b01ad2014-12-18 12:10:08 -0800467TEST_F(BinderLibTest, SetError) {
468 int32_t testValue[] = { 0, -123, 123 };
469 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
470 status_t ret;
471 Parcel data, reply;
472 data.writeInt32(testValue[i]);
473 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
474 EXPECT_EQ(testValue[i], ret);
475 }
476}
477
478TEST_F(BinderLibTest, GetId) {
479 status_t ret;
480 int32_t id;
481 Parcel data, reply;
482 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
483 EXPECT_EQ(NO_ERROR, ret);
484 ret = reply.readInt32(&id);
485 EXPECT_EQ(NO_ERROR, ret);
486 EXPECT_EQ(0, id);
487}
488
489TEST_F(BinderLibTest, PtrSize) {
490 status_t ret;
491 int32_t ptrsize;
492 Parcel data, reply;
493 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700494 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800495 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
496 EXPECT_EQ(NO_ERROR, ret);
497 ret = reply.readInt32(&ptrsize);
498 EXPECT_EQ(NO_ERROR, ret);
499 RecordProperty("TestPtrSize", sizeof(void *));
500 RecordProperty("ServerPtrSize", sizeof(void *));
501}
502
503TEST_F(BinderLibTest, IndirectGetId2)
504{
505 status_t ret;
506 int32_t id;
507 int32_t count;
508 Parcel data, reply;
509 int32_t serverId[3];
510
511 data.writeInt32(ARRAY_SIZE(serverId));
512 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
513 sp<IBinder> server;
514 BinderLibTestBundle datai;
515
516 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700517 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800518 data.writeStrongBinder(server);
519 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
520 datai.appendTo(&data);
521 }
522
523 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
524 ASSERT_EQ(NO_ERROR, ret);
525
526 ret = reply.readInt32(&id);
527 ASSERT_EQ(NO_ERROR, ret);
528 EXPECT_EQ(0, id);
529
530 ret = reply.readInt32(&count);
531 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700532 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800533
534 for (size_t i = 0; i < (size_t)count; i++) {
535 BinderLibTestBundle replyi(&reply);
536 EXPECT_TRUE(replyi.isValid());
537 ret = replyi.readInt32(&id);
538 EXPECT_EQ(NO_ERROR, ret);
539 EXPECT_EQ(serverId[i], id);
540 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
541 }
542
543 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
544}
545
546TEST_F(BinderLibTest, IndirectGetId3)
547{
548 status_t ret;
549 int32_t id;
550 int32_t count;
551 Parcel data, reply;
552 int32_t serverId[3];
553
554 data.writeInt32(ARRAY_SIZE(serverId));
555 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
556 sp<IBinder> server;
557 BinderLibTestBundle datai;
558 BinderLibTestBundle datai2;
559
560 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700561 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800562 data.writeStrongBinder(server);
563 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
564
565 datai.writeInt32(1);
566 datai.writeStrongBinder(m_server);
567 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
568 datai2.appendTo(&datai);
569
570 datai.appendTo(&data);
571 }
572
573 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
574 ASSERT_EQ(NO_ERROR, ret);
575
576 ret = reply.readInt32(&id);
577 ASSERT_EQ(NO_ERROR, ret);
578 EXPECT_EQ(0, id);
579
580 ret = reply.readInt32(&count);
581 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700582 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800583
584 for (size_t i = 0; i < (size_t)count; i++) {
585 int32_t counti;
586
587 BinderLibTestBundle replyi(&reply);
588 EXPECT_TRUE(replyi.isValid());
589 ret = replyi.readInt32(&id);
590 EXPECT_EQ(NO_ERROR, ret);
591 EXPECT_EQ(serverId[i], id);
592
593 ret = replyi.readInt32(&counti);
594 ASSERT_EQ(NO_ERROR, ret);
595 EXPECT_EQ(1, counti);
596
597 BinderLibTestBundle replyi2(&replyi);
598 EXPECT_TRUE(replyi2.isValid());
599 ret = replyi2.readInt32(&id);
600 EXPECT_EQ(NO_ERROR, ret);
601 EXPECT_EQ(0, id);
602 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
603
604 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
605 }
606
607 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
608}
609
610TEST_F(BinderLibTest, CallBack)
611{
612 status_t ret;
613 Parcel data, reply;
614 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
615 data.writeStrongBinder(callBack);
616 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
617 EXPECT_EQ(NO_ERROR, ret);
618 ret = callBack->waitEvent(5);
619 EXPECT_EQ(NO_ERROR, ret);
620 ret = callBack->getResult();
621 EXPECT_EQ(NO_ERROR, ret);
622}
623
624TEST_F(BinderLibTest, AddServer)
625{
626 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700627 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800628}
629
Riley Andrews06b01ad2014-12-18 12:10:08 -0800630TEST_F(BinderLibTest, DeathNotificationStrongRef)
631{
632 status_t ret;
633 sp<IBinder> sbinder;
634
635 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
636
637 {
638 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700639 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800640 ret = binder->linkToDeath(testDeathRecipient);
641 EXPECT_EQ(NO_ERROR, ret);
642 sbinder = binder;
643 }
644 {
645 Parcel data, reply;
646 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
647 EXPECT_EQ(0, ret);
648 }
649 IPCThreadState::self()->flushCommands();
650 ret = testDeathRecipient->waitEvent(5);
651 EXPECT_EQ(NO_ERROR, ret);
652 ret = sbinder->unlinkToDeath(testDeathRecipient);
653 EXPECT_EQ(DEAD_OBJECT, ret);
654}
655
656TEST_F(BinderLibTest, DeathNotificationMultiple)
657{
658 status_t ret;
659 const int clientcount = 2;
660 sp<IBinder> target;
661 sp<IBinder> linkedclient[clientcount];
662 sp<BinderLibTestCallBack> callBack[clientcount];
663 sp<IBinder> passiveclient[clientcount];
664
665 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700666 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800667 for (int i = 0; i < clientcount; i++) {
668 {
669 Parcel data, reply;
670
671 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700672 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800673 callBack[i] = new BinderLibTestCallBack();
674 data.writeStrongBinder(target);
675 data.writeStrongBinder(callBack[i]);
676 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
677 EXPECT_EQ(NO_ERROR, ret);
678 }
679 {
680 Parcel data, reply;
681
682 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700683 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800684 data.writeStrongBinder(target);
685 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
686 EXPECT_EQ(NO_ERROR, ret);
687 }
688 }
689 {
690 Parcel data, reply;
691 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
692 EXPECT_EQ(0, ret);
693 }
694
695 for (int i = 0; i < clientcount; i++) {
696 ret = callBack[i]->waitEvent(5);
697 EXPECT_EQ(NO_ERROR, ret);
698 ret = callBack[i]->getResult();
699 EXPECT_EQ(NO_ERROR, ret);
700 }
701}
702
Martijn Coenenf7100e42017-07-31 12:14:09 +0200703TEST_F(BinderLibTest, DeathNotificationThread)
704{
705 status_t ret;
706 sp<BinderLibTestCallBack> callback;
707 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700708 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200709 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700710 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200711
712 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
713
714 ret = target->linkToDeath(testDeathRecipient);
715 EXPECT_EQ(NO_ERROR, ret);
716
717 {
718 Parcel data, reply;
719 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
720 EXPECT_EQ(0, ret);
721 }
722
723 /* Make sure it's dead */
724 testDeathRecipient->waitEvent(5);
725
726 /* Now, pass the ref to another process and ask that process to
727 * call linkToDeath() on it, and wait for a response. This tests
728 * two things:
729 * 1) You still get death notifications when calling linkToDeath()
730 * on a ref that is already dead when it was passed to you.
731 * 2) That death notifications are not directly pushed to the thread
732 * registering them, but to the threadpool (proc workqueue) instead.
733 *
734 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
735 * is blocked on a condition variable waiting for the death notification to be
736 * called; therefore, that thread is not available for handling proc work.
737 * So, if the death notification was pushed to the thread workqueue, the callback
738 * would never be called, and the test would timeout and fail.
739 *
740 * Note that we can't do this part of the test from this thread itself, because
741 * the binder driver would only push death notifications to the thread if
742 * it is a looper thread, which this thread is not.
743 *
744 * See b/23525545 for details.
745 */
746 {
747 Parcel data, reply;
748
749 callback = new BinderLibTestCallBack();
750 data.writeStrongBinder(target);
751 data.writeStrongBinder(callback);
752 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
753 EXPECT_EQ(NO_ERROR, ret);
754 }
755
756 ret = callback->waitEvent(5);
757 EXPECT_EQ(NO_ERROR, ret);
758 ret = callback->getResult();
759 EXPECT_EQ(NO_ERROR, ret);
760}
761
Riley Andrews06b01ad2014-12-18 12:10:08 -0800762TEST_F(BinderLibTest, PassFile) {
763 int ret;
764 int pipefd[2];
765 uint8_t buf[1] = { 0 };
766 uint8_t write_value = 123;
767
768 ret = pipe2(pipefd, O_NONBLOCK);
769 ASSERT_EQ(0, ret);
770
771 {
772 Parcel data, reply;
773 uint8_t writebuf[1] = { write_value };
774
775 ret = data.writeFileDescriptor(pipefd[1], true);
776 EXPECT_EQ(NO_ERROR, ret);
777
778 ret = data.writeInt32(sizeof(writebuf));
779 EXPECT_EQ(NO_ERROR, ret);
780
781 ret = data.write(writebuf, sizeof(writebuf));
782 EXPECT_EQ(NO_ERROR, ret);
783
784 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
785 EXPECT_EQ(NO_ERROR, ret);
786 }
787
788 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700789 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800790 EXPECT_EQ(write_value, buf[0]);
791
792 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
793
794 ret = read(pipefd[0], buf, sizeof(buf));
795 EXPECT_EQ(0, ret);
796
797 close(pipefd[0]);
798}
799
Ryo Hashimotobf551892018-05-31 16:58:35 +0900800TEST_F(BinderLibTest, PassParcelFileDescriptor) {
801 const int datasize = 123;
802 std::vector<uint8_t> writebuf(datasize);
803 for (size_t i = 0; i < writebuf.size(); ++i) {
804 writebuf[i] = i;
805 }
806
807 android::base::unique_fd read_end, write_end;
808 {
809 int pipefd[2];
810 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
811 read_end.reset(pipefd[0]);
812 write_end.reset(pipefd[1]);
813 }
814 {
815 Parcel data;
816 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
817 write_end.reset();
818 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
819 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
820
821 Parcel reply;
822 EXPECT_EQ(NO_ERROR,
823 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
824 &reply));
825 }
826 std::vector<uint8_t> readbuf(datasize);
827 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
828 EXPECT_EQ(writebuf, readbuf);
829
830 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
831
832 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
833}
834
Riley Andrews06b01ad2014-12-18 12:10:08 -0800835TEST_F(BinderLibTest, PromoteLocal) {
836 sp<IBinder> strong = new BBinder();
837 wp<IBinder> weak = strong;
838 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700839 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800840 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700841 strong = nullptr;
842 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800843 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700844 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800845}
846
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700847TEST_F(BinderLibTest, LocalGetExtension) {
848 sp<BBinder> binder = new BBinder();
849 sp<IBinder> ext = new BBinder();
850 binder->setExtension(ext);
851 EXPECT_EQ(ext, binder->getExtension());
852}
853
854TEST_F(BinderLibTest, RemoteGetExtension) {
855 sp<IBinder> server = addServer();
856 ASSERT_TRUE(server != nullptr);
857
858 sp<IBinder> extension;
859 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
860 ASSERT_NE(nullptr, extension.get());
861
862 EXPECT_EQ(NO_ERROR, extension->pingBinder());
863}
864
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700865TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
866 status_t ret;
867 Parcel data, reply;
868
869 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
870 EXPECT_EQ(NO_ERROR, ret);
871
872 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700873 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800874 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
875 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
876 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
877 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700878}
879
Connor O'Brien52be2c92016-09-20 14:18:08 -0700880TEST_F(BinderLibTest, FreedBinder) {
881 status_t ret;
882
883 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700884 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700885
886 __u32 freedHandle;
887 wp<IBinder> keepFreedBinder;
888 {
889 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700890 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
891 ASSERT_EQ(NO_ERROR, ret);
892 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
893 freedHandle = freed->handle;
894 /* Add a weak ref to the freed binder so the driver does not
895 * delete its reference to it - otherwise the transaction
896 * fails regardless of whether the driver is fixed.
897 */
Steven Morelande171d622019-07-17 16:06:01 -0700898 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700899 }
Steven Morelande171d622019-07-17 16:06:01 -0700900 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700901 {
902 Parcel data, reply;
903 data.writeStrongBinder(server);
904 /* Replace original handle with handle to the freed binder */
905 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
906 __u32 oldHandle = strong->handle;
907 strong->handle = freedHandle;
908 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
909 /* Returns DEAD_OBJECT (-32) if target crashes and
910 * FAILED_TRANSACTION if the driver rejects the invalid
911 * object.
912 */
913 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
914 /* Restore original handle so parcel destructor does not use
915 * the wrong handle.
916 */
917 strong->handle = oldHandle;
918 }
919}
920
Steven Morelandd7088702021-01-13 00:27:00 +0000921TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
922 sp<IBinder> server = addServer();
923 ASSERT_TRUE(server != nullptr);
924
925 Parcel data;
926 sp<ParcelRef> reply = ParcelRef::create();
927
928 // when we have a Parcel which is deleted on another thread, if it gets
929 // deleted, it will tell the kernel this, and it will drop strong references
930 // to binder, so that we can't BR_ACQUIRE would fail
931 IPCThreadState::self()->createTransactionReference(reply.get());
932 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
933 data,
934 reply.get()));
935
936 // we have sp to binder, but it is not actually acquired by kernel, the
937 // transaction is sitting on an out buffer
938 sp<IBinder> binder = reply->readStrongBinder();
939
940 std::thread([&] {
941 // without the transaction reference, this would cause the Parcel to be
942 // deallocated before the first thread flushes BR_ACQUIRE
943 reply = nullptr;
944 IPCThreadState::self()->flushCommands();
945 }).join();
946
947 ASSERT_NE(nullptr, binder);
948 ASSERT_EQ(NO_ERROR, binder->pingBinder());
949}
950
Sherry Yang336cdd32017-07-24 14:12:27 -0700951TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
952 status_t ret;
953 Parcel data, reply;
954 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
955 for (int i = 0; i < 2; i++) {
956 BinderLibTestBundle datai;
957 datai.appendFrom(&data, 0, data.dataSize());
958
959 data.freeData();
960 data.writeInt32(1);
961 data.writeStrongBinder(callBack);
962 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
963
964 datai.appendTo(&data);
965 }
966 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
967 EXPECT_EQ(NO_ERROR, ret);
968}
969
Martijn Coenen45b07b42017-08-09 12:07:45 +0200970TEST_F(BinderLibTest, OnewayQueueing)
971{
972 status_t ret;
973 Parcel data, data2;
974
975 sp<IBinder> pollServer = addPollServer();
976
977 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
978 data.writeStrongBinder(callBack);
979 data.writeInt32(500000); // delay in us before calling back
980
981 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
982 data2.writeStrongBinder(callBack2);
983 data2.writeInt32(0); // delay in us
984
Yi Kong91635562018-06-07 14:38:36 -0700985 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200986 EXPECT_EQ(NO_ERROR, ret);
987
988 // The delay ensures that this second transaction will end up on the async_todo list
989 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700990 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200991 EXPECT_EQ(NO_ERROR, ret);
992
993 // The server will ensure that the two transactions are handled in the expected order;
994 // If the ordering is not as expected, an error will be returned through the callbacks.
995 ret = callBack->waitEvent(2);
996 EXPECT_EQ(NO_ERROR, ret);
997 ret = callBack->getResult();
998 EXPECT_EQ(NO_ERROR, ret);
999
1000 ret = callBack2->waitEvent(2);
1001 EXPECT_EQ(NO_ERROR, ret);
1002 ret = callBack2->getResult();
1003 EXPECT_EQ(NO_ERROR, ret);
1004}
1005
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001006TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
1007{
1008 status_t ret;
1009 Parcel data, reply;
1010 data.writeInterfaceToken(binderLibTestServiceName);
1011 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1012 EXPECT_EQ(-1, reply.readInt32());
1013 EXPECT_EQ(NO_ERROR, ret);
1014}
1015
1016TEST_F(BinderLibTest, WorkSourceSet)
1017{
1018 status_t ret;
1019 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001020 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001021 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001022 data.writeInterfaceToken(binderLibTestServiceName);
1023 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1024 EXPECT_EQ(100, reply.readInt32());
1025 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001026 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1027 EXPECT_EQ(NO_ERROR, ret);
1028}
1029
1030TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1031{
1032 status_t ret;
1033 Parcel data, reply;
1034
1035 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1036 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1037
1038 data.writeInterfaceToken(binderLibTestServiceName);
1039 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1040 EXPECT_EQ(-1, reply.readInt32());
1041 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001042 EXPECT_EQ(NO_ERROR, ret);
1043}
1044
1045TEST_F(BinderLibTest, WorkSourceCleared)
1046{
1047 status_t ret;
1048 Parcel data, reply;
1049
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001050 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001051 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1052 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001053 data.writeInterfaceToken(binderLibTestServiceName);
1054 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1055
1056 EXPECT_EQ(-1, reply.readInt32());
1057 EXPECT_EQ(100, previousWorkSource);
1058 EXPECT_EQ(NO_ERROR, ret);
1059}
1060
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001061TEST_F(BinderLibTest, WorkSourceRestored)
1062{
1063 status_t ret;
1064 Parcel data, reply;
1065
1066 IPCThreadState::self()->setCallingWorkSourceUid(100);
1067 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1068 IPCThreadState::self()->restoreCallingWorkSource(token);
1069
1070 data.writeInterfaceToken(binderLibTestServiceName);
1071 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1072
1073 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001074 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001075 EXPECT_EQ(NO_ERROR, ret);
1076}
1077
Olivier Gaillard91a04802018-11-14 17:32:41 +00001078TEST_F(BinderLibTest, PropagateFlagSet)
1079{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001080 IPCThreadState::self()->clearPropagateWorkSource();
1081 IPCThreadState::self()->setCallingWorkSourceUid(100);
1082 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1083}
1084
1085TEST_F(BinderLibTest, PropagateFlagCleared)
1086{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001087 IPCThreadState::self()->setCallingWorkSourceUid(100);
1088 IPCThreadState::self()->clearPropagateWorkSource();
1089 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1090}
1091
1092TEST_F(BinderLibTest, PropagateFlagRestored)
1093{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001094 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1095 IPCThreadState::self()->restoreCallingWorkSource(token);
1096
1097 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1098}
1099
1100TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1101{
1102 IPCThreadState::self()->setCallingWorkSourceUid(100);
1103
1104 Parcel data, reply;
1105 status_t ret;
1106 data.writeInterfaceToken(binderLibTestServiceName);
1107 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1108
1109 Parcel data2, reply2;
1110 status_t ret2;
1111 data2.writeInterfaceToken(binderLibTestServiceName);
1112 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1113 EXPECT_EQ(100, reply2.readInt32());
1114 EXPECT_EQ(NO_ERROR, ret2);
1115}
1116
Steven Morelandbf1915b2020-07-16 22:43:02 +00001117TEST_F(BinderLibTest, SchedPolicySet) {
1118 sp<IBinder> server = addServer();
1119 ASSERT_TRUE(server != nullptr);
1120
1121 Parcel data, reply;
1122 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1123 EXPECT_EQ(NO_ERROR, ret);
1124
1125 int policy = reply.readInt32();
1126 int priority = reply.readInt32();
1127
1128 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1129 EXPECT_EQ(kSchedPriority, priority);
1130}
1131
Steven Morelandcf03cf12020-12-04 02:58:40 +00001132TEST_F(BinderLibTest, InheritRt) {
1133 sp<IBinder> server = addServer();
1134 ASSERT_TRUE(server != nullptr);
1135
1136 const struct sched_param param {
1137 .sched_priority = kSchedPriorityMore,
1138 };
1139 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1140
1141 Parcel data, reply;
1142 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1143 EXPECT_EQ(NO_ERROR, ret);
1144
1145 int policy = reply.readInt32();
1146 int priority = reply.readInt32();
1147
1148 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1149 EXPECT_EQ(kSchedPriorityMore, priority);
1150}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001151
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001152TEST_F(BinderLibTest, VectorSent) {
1153 Parcel data, reply;
1154 sp<IBinder> server = addServer();
1155 ASSERT_TRUE(server != nullptr);
1156
1157 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1158 data.writeUint64Vector(testValue);
1159
1160 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1161 EXPECT_EQ(NO_ERROR, ret);
1162 std::vector<uint64_t> readValue;
1163 ret = reply.readUint64Vector(&readValue);
1164 EXPECT_EQ(readValue, testValue);
1165}
1166
Martijn Coenen82c75312019-07-24 15:18:30 +02001167TEST_F(BinderLibTest, BufRejected) {
1168 Parcel data, reply;
1169 uint32_t buf;
1170 sp<IBinder> server = addServer();
1171 ASSERT_TRUE(server != nullptr);
1172
1173 binder_buffer_object obj {
1174 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001175 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001176 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1177 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001178 };
1179 data.setDataCapacity(1024);
1180 // Write a bogus object at offset 0 to get an entry in the offset table
1181 data.writeFileDescriptor(0);
1182 EXPECT_EQ(data.objectsCount(), 1);
1183 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1184 // And now, overwrite it with the buffer object
1185 memcpy(parcelData, &obj, sizeof(obj));
1186 data.setDataSize(sizeof(obj));
1187
1188 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1189 // Either the kernel should reject this transaction (if it's correct), but
1190 // if it's not, the server implementation should return an error if it
1191 // finds an object in the received Parcel.
1192 EXPECT_NE(NO_ERROR, ret);
1193}
1194
Riley Andrews06b01ad2014-12-18 12:10:08 -08001195class BinderLibTestService : public BBinder
1196{
1197 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001198 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 : m_id(id)
1200 , m_nextServerId(id + 1)
1201 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001202 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001203 {
Yi Kong91635562018-06-07 14:38:36 -07001204 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1205 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001206 }
1207 ~BinderLibTestService()
1208 {
1209 exit(EXIT_SUCCESS);
1210 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001211
1212 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001213 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001214 Parcel data;
1215 data.writeInt32(NO_ERROR);
1216 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001217 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001218 }
1219 }
1220
Riley Andrews06b01ad2014-12-18 12:10:08 -08001221 virtual status_t onTransact(uint32_t code,
1222 const Parcel& data, Parcel* reply,
1223 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001224 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1225 return PERMISSION_DENIED;
1226 }
1227 switch (code) {
1228 case BINDER_LIB_TEST_REGISTER_SERVER: {
1229 int32_t id;
1230 sp<IBinder> binder;
1231 id = data.readInt32();
1232 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001233 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001234 return BAD_VALUE;
1235 }
1236
1237 if (m_id != 0)
1238 return INVALID_OPERATION;
1239
1240 pthread_mutex_lock(&m_serverWaitMutex);
1241 if (m_serverStartRequested) {
1242 m_serverStartRequested = false;
1243 m_serverStarted = binder;
1244 pthread_cond_signal(&m_serverWaitCond);
1245 }
1246 pthread_mutex_unlock(&m_serverWaitMutex);
1247 return NO_ERROR;
1248 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001249 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001250 case BINDER_LIB_TEST_ADD_SERVER: {
1251 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001252 int serverid;
1253
1254 if (m_id != 0) {
1255 return INVALID_OPERATION;
1256 }
1257 pthread_mutex_lock(&m_serverWaitMutex);
1258 if (m_serverStartRequested) {
1259 ret = -EBUSY;
1260 } else {
1261 serverid = m_nextServerId++;
1262 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001263 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001264
1265 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001266 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001267 pthread_mutex_lock(&m_serverWaitMutex);
1268 }
1269 if (ret > 0) {
1270 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 struct timespec ts;
1272 clock_gettime(CLOCK_REALTIME, &ts);
1273 ts.tv_sec += 5;
1274 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001275 }
1276 if (m_serverStartRequested) {
1277 m_serverStartRequested = false;
1278 ret = -ETIMEDOUT;
1279 } else {
1280 reply->writeStrongBinder(m_serverStarted);
1281 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001282 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001283 ret = NO_ERROR;
1284 }
1285 } else if (ret >= 0) {
1286 m_serverStartRequested = false;
1287 ret = UNKNOWN_ERROR;
1288 }
1289 pthread_mutex_unlock(&m_serverWaitMutex);
1290 return ret;
1291 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001292 case BINDER_LIB_TEST_GETPID:
1293 reply->writeInt32(getpid());
1294 return NO_ERROR;
1295 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1296 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001297 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001298 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001299 // oneway error codes should be ignored
1300 if (flags & TF_ONE_WAY) {
1301 return UNKNOWN_ERROR;
1302 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001303 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001304 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1305 // Note: this transaction is only designed for use with a
1306 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001307 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001308 // A callback was already pending; this means that
1309 // we received a second call while still processing
1310 // the first one. Fail the test.
1311 sp<IBinder> callback = data.readStrongBinder();
1312 Parcel data2;
1313 data2.writeInt32(UNKNOWN_ERROR);
1314
Yi Kong91635562018-06-07 14:38:36 -07001315 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001316 } else {
1317 m_callback = data.readStrongBinder();
1318 int32_t delayUs = data.readInt32();
1319 /*
1320 * It's necessary that we sleep here, so the next
1321 * transaction the caller makes will be queued to
1322 * the async queue.
1323 */
1324 usleep(delayUs);
1325
1326 /*
1327 * Now when we return, libbinder will tell the kernel
1328 * we are done with this transaction, and the kernel
1329 * can move the queued transaction to either the
1330 * thread todo worklist (for kernels without the fix),
1331 * or the proc todo worklist. In case of the former,
1332 * the next outbound call will pick up the pending
1333 * transaction, which leads to undesired reentrant
1334 * behavior. This is caught in the if() branch above.
1335 */
1336 }
1337
1338 return NO_ERROR;
1339 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001340 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1341 Parcel data2, reply2;
1342 sp<IBinder> binder;
1343 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001344 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001345 return BAD_VALUE;
1346 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001347 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001348 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1349 return NO_ERROR;
1350 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001351 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1352 reply->writeStrongBinder(this);
1353 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001354 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1355 reply->writeInt32(m_id);
1356 return NO_ERROR;
1357 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1358 int32_t count;
1359 uint32_t indirect_code;
1360 sp<IBinder> binder;
1361
1362 count = data.readInt32();
1363 reply->writeInt32(m_id);
1364 reply->writeInt32(count);
1365 for (int i = 0; i < count; i++) {
1366 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001367 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001368 return BAD_VALUE;
1369 }
1370 indirect_code = data.readInt32();
1371 BinderLibTestBundle data2(&data);
1372 if (!data2.isValid()) {
1373 return BAD_VALUE;
1374 }
1375 BinderLibTestBundle reply2;
1376 binder->transact(indirect_code, data2, &reply2);
1377 reply2.appendTo(reply);
1378 }
1379 return NO_ERROR;
1380 }
1381 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1382 reply->setError(data.readInt32());
1383 return NO_ERROR;
1384 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1385 reply->writeInt32(sizeof(void *));
1386 return NO_ERROR;
1387 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1388 return NO_ERROR;
1389 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1390 m_strongRef = data.readStrongBinder();
1391 return NO_ERROR;
1392 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1393 int ret;
1394 Parcel data2, reply2;
1395 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1396 sp<IBinder> target;
1397 sp<IBinder> callback;
1398
1399 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001400 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401 return BAD_VALUE;
1402 }
1403 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001404 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001405 return BAD_VALUE;
1406 }
1407 ret = target->linkToDeath(testDeathRecipient);
1408 if (ret == NO_ERROR)
1409 ret = testDeathRecipient->waitEvent(5);
1410 data2.writeInt32(ret);
1411 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1412 return NO_ERROR;
1413 }
1414 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1415 int ret;
1416 int32_t size;
1417 const void *buf;
1418 int fd;
1419
1420 fd = data.readFileDescriptor();
1421 if (fd < 0) {
1422 return BAD_VALUE;
1423 }
1424 ret = data.readInt32(&size);
1425 if (ret != NO_ERROR) {
1426 return ret;
1427 }
1428 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001429 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001430 return BAD_VALUE;
1431 }
1432 ret = write(fd, buf, size);
1433 if (ret != size)
1434 return UNKNOWN_ERROR;
1435 return NO_ERROR;
1436 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001437 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1438 int ret;
1439 int32_t size;
1440 const void *buf;
1441 android::base::unique_fd fd;
1442
1443 ret = data.readUniqueParcelFileDescriptor(&fd);
1444 if (ret != NO_ERROR) {
1445 return ret;
1446 }
1447 ret = data.readInt32(&size);
1448 if (ret != NO_ERROR) {
1449 return ret;
1450 }
1451 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001452 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001453 return BAD_VALUE;
1454 }
1455 ret = write(fd.get(), buf, size);
1456 if (ret != size) return UNKNOWN_ERROR;
1457 return NO_ERROR;
1458 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001459 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1460 alarm(10);
1461 return NO_ERROR;
1462 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001463 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001464 ;
1465 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001466 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001467 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001468 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001469 return NO_ERROR;
1470 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001471 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1472 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001473 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001474 return NO_ERROR;
1475 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001476 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1477 int policy = 0;
1478 sched_param param;
1479 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1480 return UNKNOWN_ERROR;
1481 }
1482 reply->writeInt32(policy);
1483 reply->writeInt32(param.sched_priority);
1484 return NO_ERROR;
1485 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001486 case BINDER_LIB_TEST_ECHO_VECTOR: {
1487 std::vector<uint64_t> vector;
1488 auto err = data.readUint64Vector(&vector);
1489 if (err != NO_ERROR)
1490 return err;
1491 reply->writeUint64Vector(vector);
1492 return NO_ERROR;
1493 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001494 case BINDER_LIB_TEST_REJECT_BUF: {
1495 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1496 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001497 default:
1498 return UNKNOWN_TRANSACTION;
1499 };
1500 }
1501 private:
1502 int32_t m_id;
1503 int32_t m_nextServerId;
1504 pthread_mutex_t m_serverWaitMutex;
1505 pthread_cond_t m_serverWaitCond;
1506 bool m_serverStartRequested;
1507 sp<IBinder> m_serverStarted;
1508 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001509 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001510};
1511
Martijn Coenen45b07b42017-08-09 12:07:45 +02001512int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001513{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001514 binderLibTestServiceName += String16(binderserversuffix);
1515
Riley Andrews06b01ad2014-12-18 12:10:08 -08001516 status_t ret;
1517 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001518 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001519 {
1520 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001521
Steven Morelandbf1915b2020-07-16 22:43:02 +00001522 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1523
Steven Morelandcf03cf12020-12-04 02:58:40 +00001524 testService->setInheritRt(true);
1525
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001526 /*
1527 * Normally would also contain functionality as well, but we are only
1528 * testing the extension mechanism.
1529 */
1530 testService->setExtension(new BBinder());
1531
Martijn Coenen82c75312019-07-24 15:18:30 +02001532 // Required for test "BufRejected'
1533 testService->setRequestingSid(true);
1534
Martijn Coenen45b07b42017-08-09 12:07:45 +02001535 /*
1536 * We need this below, but can't hold a sp<> because it prevents the
1537 * node from being cleaned up automatically. It's safe in this case
1538 * because of how the tests are written.
1539 */
1540 testServicePtr = testService.get();
1541
Riley Andrews06b01ad2014-12-18 12:10:08 -08001542 if (index == 0) {
1543 ret = sm->addService(binderLibTestServiceName, testService);
1544 } else {
1545 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1546 Parcel data, reply;
1547 data.writeInt32(index);
1548 data.writeStrongBinder(testService);
1549
1550 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1551 }
1552 }
1553 write(readypipefd, &ret, sizeof(ret));
1554 close(readypipefd);
1555 //printf("%s: ret %d\n", __func__, ret);
1556 if (ret)
1557 return 1;
1558 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001559 if (usePoll) {
1560 int fd;
1561 struct epoll_event ev;
1562 int epoll_fd;
1563 IPCThreadState::self()->setupPolling(&fd);
1564 if (fd < 0) {
1565 return 1;
1566 }
1567 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1568
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001569 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001570 if (epoll_fd == -1) {
1571 return 1;
1572 }
1573
1574 ev.events = EPOLLIN;
1575 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1576 return 1;
1577 }
1578
1579 while (1) {
1580 /*
1581 * We simulate a single-threaded process using the binder poll
1582 * interface; besides handling binder commands, it can also
1583 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001584 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001585 *
1586 * processPendingCall() will then issue that transaction.
1587 */
1588 struct epoll_event events[1];
1589 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1590 if (numEvents < 0) {
1591 if (errno == EINTR) {
1592 continue;
1593 }
1594 return 1;
1595 }
1596 if (numEvents > 0) {
1597 IPCThreadState::self()->handlePolledCommands();
1598 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1599 testServicePtr->processPendingCall();
1600 }
1601 }
1602 } else {
1603 ProcessState::self()->startThreadPool();
1604 IPCThreadState::self()->joinThreadPool();
1605 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001606 //printf("%s: joinThreadPool returned\n", __func__);
1607 return 1; /* joinThreadPool should not return */
1608}
1609
1610int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001611 ExitIfWrongAbi();
1612
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001613 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001614 binderservername = argv[2];
1615 } else {
1616 binderservername = argv[0];
1617 }
1618
Martijn Coenen45b07b42017-08-09 12:07:45 +02001619 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1620 binderserversuffix = argv[5];
1621 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001622 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001623 binderserversuffix = new char[16];
1624 snprintf(binderserversuffix, 16, "%d", getpid());
1625 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001626
1627 ::testing::InitGoogleTest(&argc, argv);
1628 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1629 ProcessState::self()->startThreadPool();
1630 return RUN_ALL_TESTS();
1631}