blob: dc8c0f157e2032baefb3b36ef91faa7231a7f990 [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,
Steven Moreland254e8ef2021-04-19 22:28:50 +000091 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -080092};
93
Martijn Coenen45b07b42017-08-09 12:07:45 +020094pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080095{
96 int ret;
97 pid_t pid;
98 status_t status;
99 int pipefd[2];
100 char stri[16];
101 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200102 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800103 char *childargv[] = {
104 binderservername,
105 binderserverarg,
106 stri,
107 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200108 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700109 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700110 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800111 };
112
113 ret = pipe(pipefd);
114 if (ret < 0)
115 return ret;
116
117 snprintf(stri, sizeof(stri), "%d", arg2);
118 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200119 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800120
121 pid = fork();
122 if (pid == -1)
123 return pid;
124 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800125 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800126 close(pipefd[0]);
127 execv(binderservername, childargv);
128 status = -errno;
129 write(pipefd[1], &status, sizeof(status));
130 fprintf(stderr, "execv failed, %s\n", strerror(errno));
131 _exit(EXIT_FAILURE);
132 }
133 close(pipefd[1]);
134 ret = read(pipefd[0], &status, sizeof(status));
135 //printf("pipe read returned %d, status %d\n", ret, status);
136 close(pipefd[0]);
137 if (ret == sizeof(status)) {
138 ret = status;
139 } else {
140 kill(pid, SIGKILL);
141 if (ret >= 0) {
142 ret = NO_INIT;
143 }
144 }
145 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700146 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800147 return ret;
148 }
149 return pid;
150}
151
152class BinderLibTestEnv : public ::testing::Environment {
153 public:
154 BinderLibTestEnv() {}
155 sp<IBinder> getServer(void) {
156 return m_server;
157 }
158
159 private:
160 virtual void SetUp() {
161 m_serverpid = start_server_process(0);
162 //printf("m_serverpid %d\n", m_serverpid);
163 ASSERT_GT(m_serverpid, 0);
164
165 sp<IServiceManager> sm = defaultServiceManager();
166 //printf("%s: pid %d, get service\n", __func__, m_pid);
167 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700168 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800169 //printf("%s: pid %d, get service done\n", __func__, m_pid);
170 }
171 virtual void TearDown() {
172 status_t ret;
173 Parcel data, reply;
174 int exitStatus;
175 pid_t pid;
176
177 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700178 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800179 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
180 EXPECT_EQ(0, ret);
181 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
182 EXPECT_EQ(0, ret);
183 }
184 if (m_serverpid > 0) {
185 //printf("wait for %d\n", m_pids[i]);
186 pid = wait(&exitStatus);
187 EXPECT_EQ(m_serverpid, pid);
188 EXPECT_TRUE(WIFEXITED(exitStatus));
189 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
190 }
191 }
192
193 pid_t m_serverpid;
194 sp<IBinder> m_server;
195};
196
197class BinderLibTest : public ::testing::Test {
198 public:
199 virtual void SetUp() {
200 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000201 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 }
203 virtual void TearDown() {
204 }
205 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200206 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800207 {
208 int ret;
209 int32_t id;
210 Parcel data, reply;
211 sp<IBinder> binder;
212
Martijn Coenen45b07b42017-08-09 12:07:45 +0200213 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 EXPECT_EQ(NO_ERROR, ret);
215
Yi Kong91635562018-06-07 14:38:36 -0700216 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800217 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700218 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800219 ret = reply.readInt32(&id);
220 EXPECT_EQ(NO_ERROR, ret);
221 if (idPtr)
222 *idPtr = id;
223 return binder;
224 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200225
Yi Kong91635562018-06-07 14:38:36 -0700226 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200227 {
228 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
229 }
230
Yi Kong91635562018-06-07 14:38:36 -0700231 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200232 {
233 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
234 }
235
Riley Andrews06b01ad2014-12-18 12:10:08 -0800236 void waitForReadData(int fd, int timeout_ms) {
237 int ret;
238 pollfd pfd = pollfd();
239
240 pfd.fd = fd;
241 pfd.events = POLLIN;
242 ret = poll(&pfd, 1, timeout_ms);
243 EXPECT_EQ(1, ret);
244 }
245
246 sp<IBinder> m_server;
247};
248
249class BinderLibTestBundle : public Parcel
250{
251 public:
252 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800253 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800254 int32_t mark;
255 int32_t bundleLen;
256 size_t pos;
257
258 if (source->readInt32(&mark))
259 return;
260 if (mark != MARK_START)
261 return;
262 if (source->readInt32(&bundleLen))
263 return;
264 pos = source->dataPosition();
265 if (Parcel::appendFrom(source, pos, bundleLen))
266 return;
267 source->setDataPosition(pos + bundleLen);
268 if (source->readInt32(&mark))
269 return;
270 if (mark != MARK_END)
271 return;
272 m_isValid = true;
273 setDataPosition(0);
274 }
275 void appendTo(Parcel *dest) {
276 dest->writeInt32(MARK_START);
277 dest->writeInt32(dataSize());
278 dest->appendFrom(this, 0, dataSize());
279 dest->writeInt32(MARK_END);
280 };
281 bool isValid(void) {
282 return m_isValid;
283 }
284 private:
285 enum {
286 MARK_START = B_PACK_CHARS('B','T','B','S'),
287 MARK_END = B_PACK_CHARS('B','T','B','E'),
288 };
289 bool m_isValid;
290};
291
292class BinderLibTestEvent
293{
294 public:
295 BinderLibTestEvent(void)
296 : m_eventTriggered(false)
297 {
Yi Kong91635562018-06-07 14:38:36 -0700298 pthread_mutex_init(&m_waitMutex, nullptr);
299 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800300 }
301 int waitEvent(int timeout_s)
302 {
303 int ret;
304 pthread_mutex_lock(&m_waitMutex);
305 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800306 struct timespec ts;
307 clock_gettime(CLOCK_REALTIME, &ts);
308 ts.tv_sec += timeout_s;
309 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800310 }
311 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
312 pthread_mutex_unlock(&m_waitMutex);
313 return ret;
314 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200315 pthread_t getTriggeringThread()
316 {
317 return m_triggeringThread;
318 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800319 protected:
320 void triggerEvent(void) {
321 pthread_mutex_lock(&m_waitMutex);
322 pthread_cond_signal(&m_waitCond);
323 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200324 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800325 pthread_mutex_unlock(&m_waitMutex);
326 };
327 private:
328 pthread_mutex_t m_waitMutex;
329 pthread_cond_t m_waitCond;
330 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200331 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800332};
333
334class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
335{
336 public:
337 BinderLibTestCallBack()
338 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700339 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800340 {
341 }
342 status_t getResult(void)
343 {
344 return m_result;
345 }
346
347 private:
348 virtual status_t onTransact(uint32_t code,
349 const Parcel& data, Parcel* reply,
350 uint32_t flags = 0)
351 {
352 (void)reply;
353 (void)flags;
354 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200355 case BINDER_LIB_TEST_CALL_BACK: {
356 status_t status = data.readInt32(&m_result);
357 if (status != NO_ERROR) {
358 m_result = status;
359 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800360 triggerEvent();
361 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200362 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700363 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
364 sp<IBinder> server;
365 int ret;
366 const uint8_t *buf = data.data();
367 size_t size = data.dataSize();
368 if (m_prev_end) {
369 /* 64-bit kernel needs at most 8 bytes to align buffer end */
370 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
371 } else {
372 EXPECT_TRUE(IsPageAligned((void *)buf));
373 }
374
375 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
376
377 if (size > 0) {
378 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
379 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
380 data, reply);
381 EXPECT_EQ(NO_ERROR, ret);
382 }
383 return NO_ERROR;
384 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800385 default:
386 return UNKNOWN_TRANSACTION;
387 }
388 }
389
390 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700391 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800392};
393
394class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
395{
396 private:
397 virtual void binderDied(const wp<IBinder>& who) {
398 (void)who;
399 triggerEvent();
400 };
401};
402
403TEST_F(BinderLibTest, NopTransaction) {
404 status_t ret;
405 Parcel data, reply;
406 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
407 EXPECT_EQ(NO_ERROR, ret);
408}
409
Steven Moreland80844f72020-12-12 02:06:08 +0000410TEST_F(BinderLibTest, NopTransactionOneway) {
411 status_t ret;
412 Parcel data, reply;
413 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY);
414 EXPECT_EQ(NO_ERROR, ret);
415}
416
Steven Morelandf183fdd2020-10-27 00:12:12 +0000417TEST_F(BinderLibTest, NopTransactionClear) {
418 status_t ret;
419 Parcel data, reply;
420 // make sure it accepts the transaction flag
421 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
422 EXPECT_EQ(NO_ERROR, ret);
423}
424
Marco Ballesio7ee17572020-09-08 10:30:03 -0700425TEST_F(BinderLibTest, Freeze) {
426 status_t ret;
427 Parcel data, reply, replypid;
428 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
429
430 //Pass test on devices where the freezer is not supported
431 if (freezer_file.fail()) {
432 GTEST_SKIP();
433 return;
434 }
435
436 std::string freezer_enabled;
437 std::getline(freezer_file, freezer_enabled);
438
439 //Pass test on devices where the freezer is disabled
440 if (freezer_enabled != "1") {
441 GTEST_SKIP();
442 return;
443 }
444
445 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
446 int32_t pid = replypid.readInt32();
447 EXPECT_EQ(NO_ERROR, ret);
448 for (int i = 0; i < 10; i++) {
449 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
450 }
451 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
452 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
453 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
454 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700455
456 bool sync_received, async_received;
457
458 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
459 &async_received));
460
461 EXPECT_EQ(sync_received, 1);
462 EXPECT_EQ(async_received, 0);
463
Marco Ballesio7ee17572020-09-08 10:30:03 -0700464 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
465 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
466}
467
Riley Andrews06b01ad2014-12-18 12:10:08 -0800468TEST_F(BinderLibTest, SetError) {
469 int32_t testValue[] = { 0, -123, 123 };
470 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
471 status_t ret;
472 Parcel data, reply;
473 data.writeInt32(testValue[i]);
474 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
475 EXPECT_EQ(testValue[i], ret);
476 }
477}
478
479TEST_F(BinderLibTest, GetId) {
480 status_t ret;
481 int32_t id;
482 Parcel data, reply;
483 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
484 EXPECT_EQ(NO_ERROR, ret);
485 ret = reply.readInt32(&id);
486 EXPECT_EQ(NO_ERROR, ret);
487 EXPECT_EQ(0, id);
488}
489
490TEST_F(BinderLibTest, PtrSize) {
491 status_t ret;
492 int32_t ptrsize;
493 Parcel data, reply;
494 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700495 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800496 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
497 EXPECT_EQ(NO_ERROR, ret);
498 ret = reply.readInt32(&ptrsize);
499 EXPECT_EQ(NO_ERROR, ret);
500 RecordProperty("TestPtrSize", sizeof(void *));
501 RecordProperty("ServerPtrSize", sizeof(void *));
502}
503
504TEST_F(BinderLibTest, IndirectGetId2)
505{
506 status_t ret;
507 int32_t id;
508 int32_t count;
509 Parcel data, reply;
510 int32_t serverId[3];
511
512 data.writeInt32(ARRAY_SIZE(serverId));
513 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
514 sp<IBinder> server;
515 BinderLibTestBundle datai;
516
517 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700518 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800519 data.writeStrongBinder(server);
520 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
521 datai.appendTo(&data);
522 }
523
524 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
525 ASSERT_EQ(NO_ERROR, ret);
526
527 ret = reply.readInt32(&id);
528 ASSERT_EQ(NO_ERROR, ret);
529 EXPECT_EQ(0, id);
530
531 ret = reply.readInt32(&count);
532 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700533 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800534
535 for (size_t i = 0; i < (size_t)count; i++) {
536 BinderLibTestBundle replyi(&reply);
537 EXPECT_TRUE(replyi.isValid());
538 ret = replyi.readInt32(&id);
539 EXPECT_EQ(NO_ERROR, ret);
540 EXPECT_EQ(serverId[i], id);
541 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
542 }
543
544 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
545}
546
547TEST_F(BinderLibTest, IndirectGetId3)
548{
549 status_t ret;
550 int32_t id;
551 int32_t count;
552 Parcel data, reply;
553 int32_t serverId[3];
554
555 data.writeInt32(ARRAY_SIZE(serverId));
556 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
557 sp<IBinder> server;
558 BinderLibTestBundle datai;
559 BinderLibTestBundle datai2;
560
561 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700562 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800563 data.writeStrongBinder(server);
564 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
565
566 datai.writeInt32(1);
567 datai.writeStrongBinder(m_server);
568 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
569 datai2.appendTo(&datai);
570
571 datai.appendTo(&data);
572 }
573
574 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
575 ASSERT_EQ(NO_ERROR, ret);
576
577 ret = reply.readInt32(&id);
578 ASSERT_EQ(NO_ERROR, ret);
579 EXPECT_EQ(0, id);
580
581 ret = reply.readInt32(&count);
582 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700583 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800584
585 for (size_t i = 0; i < (size_t)count; i++) {
586 int32_t counti;
587
588 BinderLibTestBundle replyi(&reply);
589 EXPECT_TRUE(replyi.isValid());
590 ret = replyi.readInt32(&id);
591 EXPECT_EQ(NO_ERROR, ret);
592 EXPECT_EQ(serverId[i], id);
593
594 ret = replyi.readInt32(&counti);
595 ASSERT_EQ(NO_ERROR, ret);
596 EXPECT_EQ(1, counti);
597
598 BinderLibTestBundle replyi2(&replyi);
599 EXPECT_TRUE(replyi2.isValid());
600 ret = replyi2.readInt32(&id);
601 EXPECT_EQ(NO_ERROR, ret);
602 EXPECT_EQ(0, id);
603 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
604
605 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
606 }
607
608 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
609}
610
611TEST_F(BinderLibTest, CallBack)
612{
613 status_t ret;
614 Parcel data, reply;
615 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
616 data.writeStrongBinder(callBack);
617 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
618 EXPECT_EQ(NO_ERROR, ret);
619 ret = callBack->waitEvent(5);
620 EXPECT_EQ(NO_ERROR, ret);
621 ret = callBack->getResult();
622 EXPECT_EQ(NO_ERROR, ret);
623}
624
625TEST_F(BinderLibTest, AddServer)
626{
627 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700628 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800629}
630
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631TEST_F(BinderLibTest, DeathNotificationStrongRef)
632{
633 status_t ret;
634 sp<IBinder> sbinder;
635
636 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
637
638 {
639 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700640 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800641 ret = binder->linkToDeath(testDeathRecipient);
642 EXPECT_EQ(NO_ERROR, ret);
643 sbinder = binder;
644 }
645 {
646 Parcel data, reply;
647 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
648 EXPECT_EQ(0, ret);
649 }
650 IPCThreadState::self()->flushCommands();
651 ret = testDeathRecipient->waitEvent(5);
652 EXPECT_EQ(NO_ERROR, ret);
653 ret = sbinder->unlinkToDeath(testDeathRecipient);
654 EXPECT_EQ(DEAD_OBJECT, ret);
655}
656
657TEST_F(BinderLibTest, DeathNotificationMultiple)
658{
659 status_t ret;
660 const int clientcount = 2;
661 sp<IBinder> target;
662 sp<IBinder> linkedclient[clientcount];
663 sp<BinderLibTestCallBack> callBack[clientcount];
664 sp<IBinder> passiveclient[clientcount];
665
666 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700667 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800668 for (int i = 0; i < clientcount; i++) {
669 {
670 Parcel data, reply;
671
672 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700673 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800674 callBack[i] = new BinderLibTestCallBack();
675 data.writeStrongBinder(target);
676 data.writeStrongBinder(callBack[i]);
677 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
678 EXPECT_EQ(NO_ERROR, ret);
679 }
680 {
681 Parcel data, reply;
682
683 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700684 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685 data.writeStrongBinder(target);
686 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
687 EXPECT_EQ(NO_ERROR, ret);
688 }
689 }
690 {
691 Parcel data, reply;
692 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
693 EXPECT_EQ(0, ret);
694 }
695
696 for (int i = 0; i < clientcount; i++) {
697 ret = callBack[i]->waitEvent(5);
698 EXPECT_EQ(NO_ERROR, ret);
699 ret = callBack[i]->getResult();
700 EXPECT_EQ(NO_ERROR, ret);
701 }
702}
703
Martijn Coenenf7100e42017-07-31 12:14:09 +0200704TEST_F(BinderLibTest, DeathNotificationThread)
705{
706 status_t ret;
707 sp<BinderLibTestCallBack> callback;
708 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700709 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200710 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700711 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200712
713 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
714
715 ret = target->linkToDeath(testDeathRecipient);
716 EXPECT_EQ(NO_ERROR, ret);
717
718 {
719 Parcel data, reply;
720 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
721 EXPECT_EQ(0, ret);
722 }
723
724 /* Make sure it's dead */
725 testDeathRecipient->waitEvent(5);
726
727 /* Now, pass the ref to another process and ask that process to
728 * call linkToDeath() on it, and wait for a response. This tests
729 * two things:
730 * 1) You still get death notifications when calling linkToDeath()
731 * on a ref that is already dead when it was passed to you.
732 * 2) That death notifications are not directly pushed to the thread
733 * registering them, but to the threadpool (proc workqueue) instead.
734 *
735 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
736 * is blocked on a condition variable waiting for the death notification to be
737 * called; therefore, that thread is not available for handling proc work.
738 * So, if the death notification was pushed to the thread workqueue, the callback
739 * would never be called, and the test would timeout and fail.
740 *
741 * Note that we can't do this part of the test from this thread itself, because
742 * the binder driver would only push death notifications to the thread if
743 * it is a looper thread, which this thread is not.
744 *
745 * See b/23525545 for details.
746 */
747 {
748 Parcel data, reply;
749
750 callback = new BinderLibTestCallBack();
751 data.writeStrongBinder(target);
752 data.writeStrongBinder(callback);
753 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
754 EXPECT_EQ(NO_ERROR, ret);
755 }
756
757 ret = callback->waitEvent(5);
758 EXPECT_EQ(NO_ERROR, ret);
759 ret = callback->getResult();
760 EXPECT_EQ(NO_ERROR, ret);
761}
762
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763TEST_F(BinderLibTest, PassFile) {
764 int ret;
765 int pipefd[2];
766 uint8_t buf[1] = { 0 };
767 uint8_t write_value = 123;
768
769 ret = pipe2(pipefd, O_NONBLOCK);
770 ASSERT_EQ(0, ret);
771
772 {
773 Parcel data, reply;
774 uint8_t writebuf[1] = { write_value };
775
776 ret = data.writeFileDescriptor(pipefd[1], true);
777 EXPECT_EQ(NO_ERROR, ret);
778
779 ret = data.writeInt32(sizeof(writebuf));
780 EXPECT_EQ(NO_ERROR, ret);
781
782 ret = data.write(writebuf, sizeof(writebuf));
783 EXPECT_EQ(NO_ERROR, ret);
784
785 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
786 EXPECT_EQ(NO_ERROR, ret);
787 }
788
789 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700790 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800791 EXPECT_EQ(write_value, buf[0]);
792
793 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
794
795 ret = read(pipefd[0], buf, sizeof(buf));
796 EXPECT_EQ(0, ret);
797
798 close(pipefd[0]);
799}
800
Ryo Hashimotobf551892018-05-31 16:58:35 +0900801TEST_F(BinderLibTest, PassParcelFileDescriptor) {
802 const int datasize = 123;
803 std::vector<uint8_t> writebuf(datasize);
804 for (size_t i = 0; i < writebuf.size(); ++i) {
805 writebuf[i] = i;
806 }
807
808 android::base::unique_fd read_end, write_end;
809 {
810 int pipefd[2];
811 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
812 read_end.reset(pipefd[0]);
813 write_end.reset(pipefd[1]);
814 }
815 {
816 Parcel data;
817 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
818 write_end.reset();
819 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
820 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
821
822 Parcel reply;
823 EXPECT_EQ(NO_ERROR,
824 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
825 &reply));
826 }
827 std::vector<uint8_t> readbuf(datasize);
828 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
829 EXPECT_EQ(writebuf, readbuf);
830
831 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
832
833 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
834}
835
Riley Andrews06b01ad2014-12-18 12:10:08 -0800836TEST_F(BinderLibTest, PromoteLocal) {
837 sp<IBinder> strong = new BBinder();
838 wp<IBinder> weak = strong;
839 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700840 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800841 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700842 strong = nullptr;
843 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800844 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700845 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800846}
847
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700848TEST_F(BinderLibTest, LocalGetExtension) {
849 sp<BBinder> binder = new BBinder();
850 sp<IBinder> ext = new BBinder();
851 binder->setExtension(ext);
852 EXPECT_EQ(ext, binder->getExtension());
853}
854
855TEST_F(BinderLibTest, RemoteGetExtension) {
856 sp<IBinder> server = addServer();
857 ASSERT_TRUE(server != nullptr);
858
859 sp<IBinder> extension;
860 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
861 ASSERT_NE(nullptr, extension.get());
862
863 EXPECT_EQ(NO_ERROR, extension->pingBinder());
864}
865
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700866TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
867 status_t ret;
868 Parcel data, reply;
869
870 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
871 EXPECT_EQ(NO_ERROR, ret);
872
873 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700874 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800875 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
876 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
877 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
878 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700879}
880
Connor O'Brien52be2c92016-09-20 14:18:08 -0700881TEST_F(BinderLibTest, FreedBinder) {
882 status_t ret;
883
884 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700885 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700886
887 __u32 freedHandle;
888 wp<IBinder> keepFreedBinder;
889 {
890 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700891 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
892 ASSERT_EQ(NO_ERROR, ret);
893 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
894 freedHandle = freed->handle;
895 /* Add a weak ref to the freed binder so the driver does not
896 * delete its reference to it - otherwise the transaction
897 * fails regardless of whether the driver is fixed.
898 */
Steven Morelande171d622019-07-17 16:06:01 -0700899 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700900 }
Steven Morelande171d622019-07-17 16:06:01 -0700901 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700902 {
903 Parcel data, reply;
904 data.writeStrongBinder(server);
905 /* Replace original handle with handle to the freed binder */
906 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
907 __u32 oldHandle = strong->handle;
908 strong->handle = freedHandle;
909 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
910 /* Returns DEAD_OBJECT (-32) if target crashes and
911 * FAILED_TRANSACTION if the driver rejects the invalid
912 * object.
913 */
914 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
915 /* Restore original handle so parcel destructor does not use
916 * the wrong handle.
917 */
918 strong->handle = oldHandle;
919 }
920}
921
Steven Morelandd7088702021-01-13 00:27:00 +0000922TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
923 sp<IBinder> server = addServer();
924 ASSERT_TRUE(server != nullptr);
925
926 Parcel data;
927 sp<ParcelRef> reply = ParcelRef::create();
928
929 // when we have a Parcel which is deleted on another thread, if it gets
930 // deleted, it will tell the kernel this, and it will drop strong references
931 // to binder, so that we can't BR_ACQUIRE would fail
932 IPCThreadState::self()->createTransactionReference(reply.get());
933 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
934 data,
935 reply.get()));
936
937 // we have sp to binder, but it is not actually acquired by kernel, the
938 // transaction is sitting on an out buffer
939 sp<IBinder> binder = reply->readStrongBinder();
940
941 std::thread([&] {
942 // without the transaction reference, this would cause the Parcel to be
943 // deallocated before the first thread flushes BR_ACQUIRE
944 reply = nullptr;
945 IPCThreadState::self()->flushCommands();
946 }).join();
947
948 ASSERT_NE(nullptr, binder);
949 ASSERT_EQ(NO_ERROR, binder->pingBinder());
950}
951
Sherry Yang336cdd32017-07-24 14:12:27 -0700952TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
953 status_t ret;
954 Parcel data, reply;
955 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
956 for (int i = 0; i < 2; i++) {
957 BinderLibTestBundle datai;
958 datai.appendFrom(&data, 0, data.dataSize());
959
960 data.freeData();
961 data.writeInt32(1);
962 data.writeStrongBinder(callBack);
963 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
964
965 datai.appendTo(&data);
966 }
967 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
968 EXPECT_EQ(NO_ERROR, ret);
969}
970
Martijn Coenen45b07b42017-08-09 12:07:45 +0200971TEST_F(BinderLibTest, OnewayQueueing)
972{
973 status_t ret;
974 Parcel data, data2;
975
976 sp<IBinder> pollServer = addPollServer();
977
978 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
979 data.writeStrongBinder(callBack);
980 data.writeInt32(500000); // delay in us before calling back
981
982 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
983 data2.writeStrongBinder(callBack2);
984 data2.writeInt32(0); // delay in us
985
Yi Kong91635562018-06-07 14:38:36 -0700986 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200987 EXPECT_EQ(NO_ERROR, ret);
988
989 // The delay ensures that this second transaction will end up on the async_todo list
990 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700991 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200992 EXPECT_EQ(NO_ERROR, ret);
993
994 // The server will ensure that the two transactions are handled in the expected order;
995 // If the ordering is not as expected, an error will be returned through the callbacks.
996 ret = callBack->waitEvent(2);
997 EXPECT_EQ(NO_ERROR, ret);
998 ret = callBack->getResult();
999 EXPECT_EQ(NO_ERROR, ret);
1000
1001 ret = callBack2->waitEvent(2);
1002 EXPECT_EQ(NO_ERROR, ret);
1003 ret = callBack2->getResult();
1004 EXPECT_EQ(NO_ERROR, ret);
1005}
1006
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001007TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
1008{
1009 status_t ret;
1010 Parcel data, reply;
1011 data.writeInterfaceToken(binderLibTestServiceName);
1012 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1013 EXPECT_EQ(-1, reply.readInt32());
1014 EXPECT_EQ(NO_ERROR, ret);
1015}
1016
1017TEST_F(BinderLibTest, WorkSourceSet)
1018{
1019 status_t ret;
1020 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001021 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001022 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001023 data.writeInterfaceToken(binderLibTestServiceName);
1024 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1025 EXPECT_EQ(100, reply.readInt32());
1026 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001027 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1028 EXPECT_EQ(NO_ERROR, ret);
1029}
1030
1031TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1032{
1033 status_t ret;
1034 Parcel data, reply;
1035
1036 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1037 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1038
1039 data.writeInterfaceToken(binderLibTestServiceName);
1040 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1041 EXPECT_EQ(-1, reply.readInt32());
1042 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001043 EXPECT_EQ(NO_ERROR, ret);
1044}
1045
1046TEST_F(BinderLibTest, WorkSourceCleared)
1047{
1048 status_t ret;
1049 Parcel data, reply;
1050
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001051 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001052 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1053 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001054 data.writeInterfaceToken(binderLibTestServiceName);
1055 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1056
1057 EXPECT_EQ(-1, reply.readInt32());
1058 EXPECT_EQ(100, previousWorkSource);
1059 EXPECT_EQ(NO_ERROR, ret);
1060}
1061
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001062TEST_F(BinderLibTest, WorkSourceRestored)
1063{
1064 status_t ret;
1065 Parcel data, reply;
1066
1067 IPCThreadState::self()->setCallingWorkSourceUid(100);
1068 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1069 IPCThreadState::self()->restoreCallingWorkSource(token);
1070
1071 data.writeInterfaceToken(binderLibTestServiceName);
1072 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1073
1074 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001075 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001076 EXPECT_EQ(NO_ERROR, ret);
1077}
1078
Olivier Gaillard91a04802018-11-14 17:32:41 +00001079TEST_F(BinderLibTest, PropagateFlagSet)
1080{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001081 IPCThreadState::self()->clearPropagateWorkSource();
1082 IPCThreadState::self()->setCallingWorkSourceUid(100);
1083 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1084}
1085
1086TEST_F(BinderLibTest, PropagateFlagCleared)
1087{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001088 IPCThreadState::self()->setCallingWorkSourceUid(100);
1089 IPCThreadState::self()->clearPropagateWorkSource();
1090 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1091}
1092
1093TEST_F(BinderLibTest, PropagateFlagRestored)
1094{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001095 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1096 IPCThreadState::self()->restoreCallingWorkSource(token);
1097
1098 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1099}
1100
1101TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1102{
1103 IPCThreadState::self()->setCallingWorkSourceUid(100);
1104
1105 Parcel data, reply;
1106 status_t ret;
1107 data.writeInterfaceToken(binderLibTestServiceName);
1108 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1109
1110 Parcel data2, reply2;
1111 status_t ret2;
1112 data2.writeInterfaceToken(binderLibTestServiceName);
1113 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1114 EXPECT_EQ(100, reply2.readInt32());
1115 EXPECT_EQ(NO_ERROR, ret2);
1116}
1117
Steven Morelandbf1915b2020-07-16 22:43:02 +00001118TEST_F(BinderLibTest, SchedPolicySet) {
1119 sp<IBinder> server = addServer();
1120 ASSERT_TRUE(server != nullptr);
1121
1122 Parcel data, reply;
1123 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1124 EXPECT_EQ(NO_ERROR, ret);
1125
1126 int policy = reply.readInt32();
1127 int priority = reply.readInt32();
1128
1129 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1130 EXPECT_EQ(kSchedPriority, priority);
1131}
1132
Steven Morelandcf03cf12020-12-04 02:58:40 +00001133TEST_F(BinderLibTest, InheritRt) {
1134 sp<IBinder> server = addServer();
1135 ASSERT_TRUE(server != nullptr);
1136
1137 const struct sched_param param {
1138 .sched_priority = kSchedPriorityMore,
1139 };
1140 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1141
1142 Parcel data, reply;
1143 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1144 EXPECT_EQ(NO_ERROR, ret);
1145
1146 int policy = reply.readInt32();
1147 int priority = reply.readInt32();
1148
1149 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1150 EXPECT_EQ(kSchedPriorityMore, priority);
1151}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001152
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001153TEST_F(BinderLibTest, VectorSent) {
1154 Parcel data, reply;
1155 sp<IBinder> server = addServer();
1156 ASSERT_TRUE(server != nullptr);
1157
1158 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1159 data.writeUint64Vector(testValue);
1160
1161 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1162 EXPECT_EQ(NO_ERROR, ret);
1163 std::vector<uint64_t> readValue;
1164 ret = reply.readUint64Vector(&readValue);
1165 EXPECT_EQ(readValue, testValue);
1166}
1167
Martijn Coenen82c75312019-07-24 15:18:30 +02001168TEST_F(BinderLibTest, BufRejected) {
1169 Parcel data, reply;
1170 uint32_t buf;
1171 sp<IBinder> server = addServer();
1172 ASSERT_TRUE(server != nullptr);
1173
1174 binder_buffer_object obj {
1175 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001176 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001177 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1178 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001179 };
1180 data.setDataCapacity(1024);
1181 // Write a bogus object at offset 0 to get an entry in the offset table
1182 data.writeFileDescriptor(0);
1183 EXPECT_EQ(data.objectsCount(), 1);
1184 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1185 // And now, overwrite it with the buffer object
1186 memcpy(parcelData, &obj, sizeof(obj));
1187 data.setDataSize(sizeof(obj));
1188
1189 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1190 // Either the kernel should reject this transaction (if it's correct), but
1191 // if it's not, the server implementation should return an error if it
1192 // finds an object in the received Parcel.
1193 EXPECT_NE(NO_ERROR, ret);
1194}
1195
Steven Moreland254e8ef2021-04-19 22:28:50 +00001196TEST_F(BinderLibTest, GotSid) {
1197 sp<IBinder> server = addServer();
1198
1199 Parcel data;
1200 status_t ret = server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr);
1201 EXPECT_EQ(OK, ret);
1202}
1203
Riley Andrews06b01ad2014-12-18 12:10:08 -08001204class BinderLibTestService : public BBinder
1205{
1206 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001207 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001208 : m_id(id)
1209 , m_nextServerId(id + 1)
1210 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001211 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001212 {
Yi Kong91635562018-06-07 14:38:36 -07001213 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1214 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001215 }
1216 ~BinderLibTestService()
1217 {
1218 exit(EXIT_SUCCESS);
1219 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001220
1221 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001222 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001223 Parcel data;
1224 data.writeInt32(NO_ERROR);
1225 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001226 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001227 }
1228 }
1229
Riley Andrews06b01ad2014-12-18 12:10:08 -08001230 virtual status_t onTransact(uint32_t code,
1231 const Parcel& data, Parcel* reply,
1232 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001233 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1234 return PERMISSION_DENIED;
1235 }
1236 switch (code) {
1237 case BINDER_LIB_TEST_REGISTER_SERVER: {
1238 int32_t id;
1239 sp<IBinder> binder;
1240 id = data.readInt32();
1241 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001242 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001243 return BAD_VALUE;
1244 }
1245
1246 if (m_id != 0)
1247 return INVALID_OPERATION;
1248
1249 pthread_mutex_lock(&m_serverWaitMutex);
1250 if (m_serverStartRequested) {
1251 m_serverStartRequested = false;
1252 m_serverStarted = binder;
1253 pthread_cond_signal(&m_serverWaitCond);
1254 }
1255 pthread_mutex_unlock(&m_serverWaitMutex);
1256 return NO_ERROR;
1257 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001258 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001259 case BINDER_LIB_TEST_ADD_SERVER: {
1260 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001261 int serverid;
1262
1263 if (m_id != 0) {
1264 return INVALID_OPERATION;
1265 }
1266 pthread_mutex_lock(&m_serverWaitMutex);
1267 if (m_serverStartRequested) {
1268 ret = -EBUSY;
1269 } else {
1270 serverid = m_nextServerId++;
1271 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001272 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001273
1274 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001275 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 pthread_mutex_lock(&m_serverWaitMutex);
1277 }
1278 if (ret > 0) {
1279 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001280 struct timespec ts;
1281 clock_gettime(CLOCK_REALTIME, &ts);
1282 ts.tv_sec += 5;
1283 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 }
1285 if (m_serverStartRequested) {
1286 m_serverStartRequested = false;
1287 ret = -ETIMEDOUT;
1288 } else {
1289 reply->writeStrongBinder(m_serverStarted);
1290 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001291 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001292 ret = NO_ERROR;
1293 }
1294 } else if (ret >= 0) {
1295 m_serverStartRequested = false;
1296 ret = UNKNOWN_ERROR;
1297 }
1298 pthread_mutex_unlock(&m_serverWaitMutex);
1299 return ret;
1300 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001301 case BINDER_LIB_TEST_GETPID:
1302 reply->writeInt32(getpid());
1303 return NO_ERROR;
1304 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1305 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001306 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001308 // oneway error codes should be ignored
1309 if (flags & TF_ONE_WAY) {
1310 return UNKNOWN_ERROR;
1311 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001312 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001313 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1314 // Note: this transaction is only designed for use with a
1315 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001316 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001317 // A callback was already pending; this means that
1318 // we received a second call while still processing
1319 // the first one. Fail the test.
1320 sp<IBinder> callback = data.readStrongBinder();
1321 Parcel data2;
1322 data2.writeInt32(UNKNOWN_ERROR);
1323
Yi Kong91635562018-06-07 14:38:36 -07001324 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001325 } else {
1326 m_callback = data.readStrongBinder();
1327 int32_t delayUs = data.readInt32();
1328 /*
1329 * It's necessary that we sleep here, so the next
1330 * transaction the caller makes will be queued to
1331 * the async queue.
1332 */
1333 usleep(delayUs);
1334
1335 /*
1336 * Now when we return, libbinder will tell the kernel
1337 * we are done with this transaction, and the kernel
1338 * can move the queued transaction to either the
1339 * thread todo worklist (for kernels without the fix),
1340 * or the proc todo worklist. In case of the former,
1341 * the next outbound call will pick up the pending
1342 * transaction, which leads to undesired reentrant
1343 * behavior. This is caught in the if() branch above.
1344 */
1345 }
1346
1347 return NO_ERROR;
1348 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001349 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1350 Parcel data2, reply2;
1351 sp<IBinder> binder;
1352 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001353 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001354 return BAD_VALUE;
1355 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001356 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001357 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1358 return NO_ERROR;
1359 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001360 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1361 reply->writeStrongBinder(this);
1362 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001363 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1364 reply->writeInt32(m_id);
1365 return NO_ERROR;
1366 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1367 int32_t count;
1368 uint32_t indirect_code;
1369 sp<IBinder> binder;
1370
1371 count = data.readInt32();
1372 reply->writeInt32(m_id);
1373 reply->writeInt32(count);
1374 for (int i = 0; i < count; i++) {
1375 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001376 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001377 return BAD_VALUE;
1378 }
1379 indirect_code = data.readInt32();
1380 BinderLibTestBundle data2(&data);
1381 if (!data2.isValid()) {
1382 return BAD_VALUE;
1383 }
1384 BinderLibTestBundle reply2;
1385 binder->transact(indirect_code, data2, &reply2);
1386 reply2.appendTo(reply);
1387 }
1388 return NO_ERROR;
1389 }
1390 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1391 reply->setError(data.readInt32());
1392 return NO_ERROR;
1393 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1394 reply->writeInt32(sizeof(void *));
1395 return NO_ERROR;
1396 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1397 return NO_ERROR;
1398 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1399 m_strongRef = data.readStrongBinder();
1400 return NO_ERROR;
1401 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1402 int ret;
1403 Parcel data2, reply2;
1404 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1405 sp<IBinder> target;
1406 sp<IBinder> callback;
1407
1408 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001409 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001410 return BAD_VALUE;
1411 }
1412 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001413 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001414 return BAD_VALUE;
1415 }
1416 ret = target->linkToDeath(testDeathRecipient);
1417 if (ret == NO_ERROR)
1418 ret = testDeathRecipient->waitEvent(5);
1419 data2.writeInt32(ret);
1420 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1421 return NO_ERROR;
1422 }
1423 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1424 int ret;
1425 int32_t size;
1426 const void *buf;
1427 int fd;
1428
1429 fd = data.readFileDescriptor();
1430 if (fd < 0) {
1431 return BAD_VALUE;
1432 }
1433 ret = data.readInt32(&size);
1434 if (ret != NO_ERROR) {
1435 return ret;
1436 }
1437 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001438 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001439 return BAD_VALUE;
1440 }
1441 ret = write(fd, buf, size);
1442 if (ret != size)
1443 return UNKNOWN_ERROR;
1444 return NO_ERROR;
1445 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001446 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1447 int ret;
1448 int32_t size;
1449 const void *buf;
1450 android::base::unique_fd fd;
1451
1452 ret = data.readUniqueParcelFileDescriptor(&fd);
1453 if (ret != NO_ERROR) {
1454 return ret;
1455 }
1456 ret = data.readInt32(&size);
1457 if (ret != NO_ERROR) {
1458 return ret;
1459 }
1460 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001461 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001462 return BAD_VALUE;
1463 }
1464 ret = write(fd.get(), buf, size);
1465 if (ret != size) return UNKNOWN_ERROR;
1466 return NO_ERROR;
1467 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001468 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1469 alarm(10);
1470 return NO_ERROR;
1471 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001472 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001473 ;
1474 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001475 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001476 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001477 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001478 return NO_ERROR;
1479 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001480 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1481 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001482 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001483 return NO_ERROR;
1484 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001485 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1486 int policy = 0;
1487 sched_param param;
1488 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1489 return UNKNOWN_ERROR;
1490 }
1491 reply->writeInt32(policy);
1492 reply->writeInt32(param.sched_priority);
1493 return NO_ERROR;
1494 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001495 case BINDER_LIB_TEST_ECHO_VECTOR: {
1496 std::vector<uint64_t> vector;
1497 auto err = data.readUint64Vector(&vector);
1498 if (err != NO_ERROR)
1499 return err;
1500 reply->writeUint64Vector(vector);
1501 return NO_ERROR;
1502 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001503 case BINDER_LIB_TEST_REJECT_BUF: {
1504 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1505 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001506 case BINDER_LIB_TEST_CAN_GET_SID: {
1507 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1508 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001509 default:
1510 return UNKNOWN_TRANSACTION;
1511 };
1512 }
1513 private:
1514 int32_t m_id;
1515 int32_t m_nextServerId;
1516 pthread_mutex_t m_serverWaitMutex;
1517 pthread_cond_t m_serverWaitCond;
1518 bool m_serverStartRequested;
1519 sp<IBinder> m_serverStarted;
1520 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001521 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001522};
1523
Martijn Coenen45b07b42017-08-09 12:07:45 +02001524int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001525{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001526 binderLibTestServiceName += String16(binderserversuffix);
1527
Riley Andrews06b01ad2014-12-18 12:10:08 -08001528 status_t ret;
1529 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001530 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001531 {
1532 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001533
Steven Morelandbf1915b2020-07-16 22:43:02 +00001534 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1535
Steven Morelandcf03cf12020-12-04 02:58:40 +00001536 testService->setInheritRt(true);
1537
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001538 /*
1539 * Normally would also contain functionality as well, but we are only
1540 * testing the extension mechanism.
1541 */
1542 testService->setExtension(new BBinder());
1543
Martijn Coenen82c75312019-07-24 15:18:30 +02001544 // Required for test "BufRejected'
1545 testService->setRequestingSid(true);
1546
Martijn Coenen45b07b42017-08-09 12:07:45 +02001547 /*
1548 * We need this below, but can't hold a sp<> because it prevents the
1549 * node from being cleaned up automatically. It's safe in this case
1550 * because of how the tests are written.
1551 */
1552 testServicePtr = testService.get();
1553
Riley Andrews06b01ad2014-12-18 12:10:08 -08001554 if (index == 0) {
1555 ret = sm->addService(binderLibTestServiceName, testService);
1556 } else {
1557 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1558 Parcel data, reply;
1559 data.writeInt32(index);
1560 data.writeStrongBinder(testService);
1561
1562 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1563 }
1564 }
1565 write(readypipefd, &ret, sizeof(ret));
1566 close(readypipefd);
1567 //printf("%s: ret %d\n", __func__, ret);
1568 if (ret)
1569 return 1;
1570 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001571 if (usePoll) {
1572 int fd;
1573 struct epoll_event ev;
1574 int epoll_fd;
1575 IPCThreadState::self()->setupPolling(&fd);
1576 if (fd < 0) {
1577 return 1;
1578 }
1579 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1580
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001581 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001582 if (epoll_fd == -1) {
1583 return 1;
1584 }
1585
1586 ev.events = EPOLLIN;
1587 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1588 return 1;
1589 }
1590
1591 while (1) {
1592 /*
1593 * We simulate a single-threaded process using the binder poll
1594 * interface; besides handling binder commands, it can also
1595 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001596 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001597 *
1598 * processPendingCall() will then issue that transaction.
1599 */
1600 struct epoll_event events[1];
1601 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1602 if (numEvents < 0) {
1603 if (errno == EINTR) {
1604 continue;
1605 }
1606 return 1;
1607 }
1608 if (numEvents > 0) {
1609 IPCThreadState::self()->handlePolledCommands();
1610 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1611 testServicePtr->processPendingCall();
1612 }
1613 }
1614 } else {
1615 ProcessState::self()->startThreadPool();
1616 IPCThreadState::self()->joinThreadPool();
1617 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001618 //printf("%s: joinThreadPool returned\n", __func__);
1619 return 1; /* joinThreadPool should not return */
1620}
1621
1622int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001623 ExitIfWrongAbi();
1624
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001625 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001626 binderservername = argv[2];
1627 } else {
1628 binderservername = argv[0];
1629 }
1630
Martijn Coenen45b07b42017-08-09 12:07:45 +02001631 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1632 binderserversuffix = argv[5];
1633 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001634 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001635 binderserversuffix = new char[16];
1636 snprintf(binderserversuffix, 16, "%d", getpid());
1637 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001638
1639 ::testing::InitGoogleTest(&argc, argv);
1640 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1641 ProcessState::self()->startThreadPool();
1642 return RUN_ALL_TESTS();
1643}