blob: 3289b5f765e2764f2a305f273b7a4f163afc234b [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>
Yifan Hong84bedeb2021-04-21 21:37:17 -070024
25#include <chrono>
Steven Morelandd7088702021-01-13 00:27:00 +000026#include <thread>
Riley Andrews06b01ad2014-12-18 12:10:08 -080027
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070028#include <gmock/gmock.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080029#include <gtest/gtest.h>
30
Yifan Hong84bedeb2021-04-21 21:37:17 -070031#include <android-base/properties.h>
32#include <android-base/result.h>
33#include <android-base/unique_fd.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080034#include <binder/Binder.h>
35#include <binder/IBinder.h>
36#include <binder/IPCThreadState.h>
37#include <binder/IServiceManager.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070038#include <binder/RpcServer.h>
39#include <binder/RpcSession.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080040
Steven Morelandcf03cf12020-12-04 02:58:40 +000041#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020042#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080043#include <sys/prctl.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070044#include <sys/socket.h>
45#include <sys/un.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020046
Steven Moreland6ba5a252021-05-04 22:49:00 +000047#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070048#include "binderAbiHelper.h"
49
Riley Andrews06b01ad2014-12-18 12:10:08 -080050#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
51
52using namespace android;
Yifan Hong84bedeb2021-04-21 21:37:17 -070053using namespace std::string_literals;
54using namespace std::chrono_literals;
55using testing::ExplainMatchResult;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070056using testing::Not;
Yifan Hong84bedeb2021-04-21 21:37:17 -070057using testing::WithParamInterface;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070058
59// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
60MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
61 *result_listener << statusToString(arg);
62 return expected == arg;
63}
Riley Andrews06b01ad2014-12-18 12:10:08 -080064
Yifan Hong84bedeb2021-04-21 21:37:17 -070065// e.g. Result<int32_t> v = 0; EXPECT_THAT(result, ResultHasValue(0));
66MATCHER_P(ResultHasValue, resultMatcher, "") {
67 if (!arg.ok()) {
68 *result_listener << "contains error " << arg.error();
69 return false;
70 }
71 return ExplainMatchResult(resultMatcher, arg.value(), result_listener);
72}
73
Sherry Yang336cdd32017-07-24 14:12:27 -070074static ::testing::AssertionResult IsPageAligned(void *buf) {
75 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
76 return ::testing::AssertionSuccess();
77 else
78 return ::testing::AssertionFailure() << buf << " is not page aligned";
79}
80
Riley Andrews06b01ad2014-12-18 12:10:08 -080081static testing::Environment* binder_env;
82static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070083static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080084static char binderserverarg[] = "--binderserver";
85
Steven Morelandbf1915b2020-07-16 22:43:02 +000086static constexpr int kSchedPolicy = SCHED_RR;
87static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000088static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000089
Riley Andrews06b01ad2014-12-18 12:10:08 -080090static String16 binderLibTestServiceName = String16("test.binderLib");
91
92enum BinderLibTestTranscationCode {
93 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
94 BINDER_LIB_TEST_REGISTER_SERVER,
95 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020096 BINDER_LIB_TEST_ADD_POLL_SERVER,
Steven Moreland35626652021-05-15 01:32:04 +000097 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080098 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070099 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200100 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800101 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700102 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800103 BINDER_LIB_TEST_GET_ID_TRANSACTION,
104 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
105 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
106 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
107 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
108 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
109 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +0900110 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800111 BINDER_LIB_TEST_EXIT_TRANSACTION,
112 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
113 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -0700114 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100115 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +0000116 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -0700117 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
118 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800119 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +0200120 BINDER_LIB_TEST_REJECT_BUF,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000121 BINDER_LIB_TEST_CAN_GET_SID,
Yifan Hong84bedeb2021-04-21 21:37:17 -0700122 BINDER_LIB_TEST_USLEEP,
123 BINDER_LIB_TEST_CREATE_TEST_SERVICE,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800124};
125
Martijn Coenen45b07b42017-08-09 12:07:45 +0200126pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800127{
128 int ret;
129 pid_t pid;
130 status_t status;
131 int pipefd[2];
132 char stri[16];
133 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200134 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800135 char *childargv[] = {
136 binderservername,
137 binderserverarg,
138 stri,
139 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200140 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700141 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700142 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800143 };
144
145 ret = pipe(pipefd);
146 if (ret < 0)
147 return ret;
148
149 snprintf(stri, sizeof(stri), "%d", arg2);
150 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200151 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800152
153 pid = fork();
154 if (pid == -1)
155 return pid;
156 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800157 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800158 close(pipefd[0]);
159 execv(binderservername, childargv);
160 status = -errno;
161 write(pipefd[1], &status, sizeof(status));
162 fprintf(stderr, "execv failed, %s\n", strerror(errno));
163 _exit(EXIT_FAILURE);
164 }
165 close(pipefd[1]);
166 ret = read(pipefd[0], &status, sizeof(status));
167 //printf("pipe read returned %d, status %d\n", ret, status);
168 close(pipefd[0]);
169 if (ret == sizeof(status)) {
170 ret = status;
171 } else {
172 kill(pid, SIGKILL);
173 if (ret >= 0) {
174 ret = NO_INIT;
175 }
176 }
177 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700178 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800179 return ret;
180 }
181 return pid;
182}
183
Yifan Hong84bedeb2021-04-21 21:37:17 -0700184android::base::Result<int32_t> GetId(sp<IBinder> service) {
185 using android::base::Error;
186 Parcel data, reply;
187 data.markForBinder(service);
188 const char *prefix = data.isForRpc() ? "On RPC server, " : "On binder server, ";
189 status_t status = service->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
190 if (status != OK)
191 return Error(status) << prefix << "transact(GET_ID): " << statusToString(status);
192 int32_t result = 0;
193 status = reply.readInt32(&result);
194 if (status != OK) return Error(status) << prefix << "readInt32: " << statusToString(status);
195 return result;
196}
197
Riley Andrews06b01ad2014-12-18 12:10:08 -0800198class BinderLibTestEnv : public ::testing::Environment {
199 public:
200 BinderLibTestEnv() {}
201 sp<IBinder> getServer(void) {
202 return m_server;
203 }
204
205 private:
206 virtual void SetUp() {
207 m_serverpid = start_server_process(0);
208 //printf("m_serverpid %d\n", m_serverpid);
209 ASSERT_GT(m_serverpid, 0);
210
211 sp<IServiceManager> sm = defaultServiceManager();
212 //printf("%s: pid %d, get service\n", __func__, m_pid);
213 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700214 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800215 //printf("%s: pid %d, get service done\n", __func__, m_pid);
216 }
217 virtual void TearDown() {
218 status_t ret;
219 Parcel data, reply;
220 int exitStatus;
221 pid_t pid;
222
223 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700224 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800225 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
226 EXPECT_EQ(0, ret);
227 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
228 EXPECT_EQ(0, ret);
229 }
230 if (m_serverpid > 0) {
231 //printf("wait for %d\n", m_pids[i]);
232 pid = wait(&exitStatus);
233 EXPECT_EQ(m_serverpid, pid);
234 EXPECT_TRUE(WIFEXITED(exitStatus));
235 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
236 }
237 }
238
239 pid_t m_serverpid;
240 sp<IBinder> m_server;
241};
242
243class BinderLibTest : public ::testing::Test {
244 public:
245 virtual void SetUp() {
246 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000247 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800248 }
249 virtual void TearDown() {
250 }
251 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200252 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800253 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800254 int32_t id;
255 Parcel data, reply;
256 sp<IBinder> binder;
257
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700258 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800259
Yi Kong91635562018-06-07 14:38:36 -0700260 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800261 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700262 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700263 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800264 if (idPtr)
265 *idPtr = id;
266 return binder;
267 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200268
Yi Kong91635562018-06-07 14:38:36 -0700269 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200270 {
271 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
272 }
273
Yi Kong91635562018-06-07 14:38:36 -0700274 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200275 {
276 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
277 }
278
Riley Andrews06b01ad2014-12-18 12:10:08 -0800279 void waitForReadData(int fd, int timeout_ms) {
280 int ret;
281 pollfd pfd = pollfd();
282
283 pfd.fd = fd;
284 pfd.events = POLLIN;
285 ret = poll(&pfd, 1, timeout_ms);
286 EXPECT_EQ(1, ret);
287 }
288
289 sp<IBinder> m_server;
290};
291
292class BinderLibTestBundle : public Parcel
293{
294 public:
295 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800296 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800297 int32_t mark;
298 int32_t bundleLen;
299 size_t pos;
300
301 if (source->readInt32(&mark))
302 return;
303 if (mark != MARK_START)
304 return;
305 if (source->readInt32(&bundleLen))
306 return;
307 pos = source->dataPosition();
308 if (Parcel::appendFrom(source, pos, bundleLen))
309 return;
310 source->setDataPosition(pos + bundleLen);
311 if (source->readInt32(&mark))
312 return;
313 if (mark != MARK_END)
314 return;
315 m_isValid = true;
316 setDataPosition(0);
317 }
318 void appendTo(Parcel *dest) {
319 dest->writeInt32(MARK_START);
320 dest->writeInt32(dataSize());
321 dest->appendFrom(this, 0, dataSize());
322 dest->writeInt32(MARK_END);
323 };
324 bool isValid(void) {
325 return m_isValid;
326 }
327 private:
328 enum {
329 MARK_START = B_PACK_CHARS('B','T','B','S'),
330 MARK_END = B_PACK_CHARS('B','T','B','E'),
331 };
332 bool m_isValid;
333};
334
335class BinderLibTestEvent
336{
337 public:
338 BinderLibTestEvent(void)
339 : m_eventTriggered(false)
340 {
Yi Kong91635562018-06-07 14:38:36 -0700341 pthread_mutex_init(&m_waitMutex, nullptr);
342 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800343 }
344 int waitEvent(int timeout_s)
345 {
346 int ret;
347 pthread_mutex_lock(&m_waitMutex);
348 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800349 struct timespec ts;
350 clock_gettime(CLOCK_REALTIME, &ts);
351 ts.tv_sec += timeout_s;
352 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800353 }
354 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
355 pthread_mutex_unlock(&m_waitMutex);
356 return ret;
357 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200358 pthread_t getTriggeringThread()
359 {
360 return m_triggeringThread;
361 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800362 protected:
363 void triggerEvent(void) {
364 pthread_mutex_lock(&m_waitMutex);
365 pthread_cond_signal(&m_waitCond);
366 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200367 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800368 pthread_mutex_unlock(&m_waitMutex);
369 };
370 private:
371 pthread_mutex_t m_waitMutex;
372 pthread_cond_t m_waitCond;
373 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200374 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800375};
376
377class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
378{
379 public:
380 BinderLibTestCallBack()
381 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700382 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800383 {
384 }
385 status_t getResult(void)
386 {
387 return m_result;
388 }
389
390 private:
391 virtual status_t onTransact(uint32_t code,
392 const Parcel& data, Parcel* reply,
393 uint32_t flags = 0)
394 {
395 (void)reply;
396 (void)flags;
397 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200398 case BINDER_LIB_TEST_CALL_BACK: {
399 status_t status = data.readInt32(&m_result);
400 if (status != NO_ERROR) {
401 m_result = status;
402 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800403 triggerEvent();
404 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200405 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700406 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
407 sp<IBinder> server;
408 int ret;
409 const uint8_t *buf = data.data();
410 size_t size = data.dataSize();
411 if (m_prev_end) {
412 /* 64-bit kernel needs at most 8 bytes to align buffer end */
413 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
414 } else {
415 EXPECT_TRUE(IsPageAligned((void *)buf));
416 }
417
418 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
419
420 if (size > 0) {
421 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
422 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
423 data, reply);
424 EXPECT_EQ(NO_ERROR, ret);
425 }
426 return NO_ERROR;
427 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800428 default:
429 return UNKNOWN_TRANSACTION;
430 }
431 }
432
433 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700434 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800435};
436
437class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
438{
439 private:
440 virtual void binderDied(const wp<IBinder>& who) {
441 (void)who;
442 triggerEvent();
443 };
444};
445
446TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800447 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700448 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
449 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800450}
451
Steven Moreland80844f72020-12-12 02:06:08 +0000452TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000453 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700454 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
455 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000456}
457
Steven Morelandf183fdd2020-10-27 00:12:12 +0000458TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000459 Parcel data, reply;
460 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700461 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
462 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000463}
464
Marco Ballesio7ee17572020-09-08 10:30:03 -0700465TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700466 Parcel data, reply, replypid;
467 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
468
469 //Pass test on devices where the freezer is not supported
470 if (freezer_file.fail()) {
471 GTEST_SKIP();
472 return;
473 }
474
475 std::string freezer_enabled;
476 std::getline(freezer_file, freezer_enabled);
477
478 //Pass test on devices where the freezer is disabled
479 if (freezer_enabled != "1") {
480 GTEST_SKIP();
481 return;
482 }
483
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700484 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700485 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700486 for (int i = 0; i < 10; i++) {
487 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
488 }
489 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
490 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
491 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
492 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700493
494 bool sync_received, async_received;
495
496 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
497 &async_received));
498
499 EXPECT_EQ(sync_received, 1);
500 EXPECT_EQ(async_received, 0);
501
Marco Ballesio7ee17572020-09-08 10:30:03 -0700502 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
503 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
504}
505
Riley Andrews06b01ad2014-12-18 12:10:08 -0800506TEST_F(BinderLibTest, SetError) {
507 int32_t testValue[] = { 0, -123, 123 };
508 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800509 Parcel data, reply;
510 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700511 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
512 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800513 }
514}
515
516TEST_F(BinderLibTest, GetId) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700517 EXPECT_THAT(GetId(m_server), ResultHasValue(0));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800518}
519
520TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800521 int32_t ptrsize;
522 Parcel data, reply;
523 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700524 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700525 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
526 StatusEq(NO_ERROR));
527 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800528 RecordProperty("TestPtrSize", sizeof(void *));
529 RecordProperty("ServerPtrSize", sizeof(void *));
530}
531
532TEST_F(BinderLibTest, IndirectGetId2)
533{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800534 int32_t id;
535 int32_t count;
536 Parcel data, reply;
537 int32_t serverId[3];
538
539 data.writeInt32(ARRAY_SIZE(serverId));
540 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
541 sp<IBinder> server;
542 BinderLibTestBundle datai;
543
544 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700545 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800546 data.writeStrongBinder(server);
547 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
548 datai.appendTo(&data);
549 }
550
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700551 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
552 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700554 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800555 EXPECT_EQ(0, id);
556
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700557 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700558 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559
560 for (size_t i = 0; i < (size_t)count; i++) {
561 BinderLibTestBundle replyi(&reply);
562 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700563 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800564 EXPECT_EQ(serverId[i], id);
565 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
566 }
567
568 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
569}
570
571TEST_F(BinderLibTest, IndirectGetId3)
572{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800573 int32_t id;
574 int32_t count;
575 Parcel data, reply;
576 int32_t serverId[3];
577
578 data.writeInt32(ARRAY_SIZE(serverId));
579 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
580 sp<IBinder> server;
581 BinderLibTestBundle datai;
582 BinderLibTestBundle datai2;
583
584 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700585 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800586 data.writeStrongBinder(server);
587 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
588
589 datai.writeInt32(1);
590 datai.writeStrongBinder(m_server);
591 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
592 datai2.appendTo(&datai);
593
594 datai.appendTo(&data);
595 }
596
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700597 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
598 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800599
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700600 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800601 EXPECT_EQ(0, id);
602
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700603 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700604 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605
606 for (size_t i = 0; i < (size_t)count; i++) {
607 int32_t counti;
608
609 BinderLibTestBundle replyi(&reply);
610 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700611 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800612 EXPECT_EQ(serverId[i], id);
613
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700614 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800615 EXPECT_EQ(1, counti);
616
617 BinderLibTestBundle replyi2(&replyi);
618 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700619 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800620 EXPECT_EQ(0, id);
621 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
622
623 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
624 }
625
626 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
627}
628
629TEST_F(BinderLibTest, CallBack)
630{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631 Parcel data, reply;
632 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
633 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700634 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
635 StatusEq(NO_ERROR));
636 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
637 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800638}
639
Steven Moreland35626652021-05-15 01:32:04 +0000640TEST_F(BinderLibTest, BinderCallContextGuard) {
641 sp<IBinder> binder = addServer();
642 Parcel data, reply;
643 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
644 StatusEq(DEAD_OBJECT));
645}
646
Riley Andrews06b01ad2014-12-18 12:10:08 -0800647TEST_F(BinderLibTest, AddServer)
648{
649 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700650 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800651}
652
Riley Andrews06b01ad2014-12-18 12:10:08 -0800653TEST_F(BinderLibTest, DeathNotificationStrongRef)
654{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800655 sp<IBinder> sbinder;
656
657 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
658
659 {
660 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700661 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700662 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800663 sbinder = binder;
664 }
665 {
666 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700667 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
668 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800669 }
670 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700671 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
672 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800673}
674
675TEST_F(BinderLibTest, DeathNotificationMultiple)
676{
677 status_t ret;
678 const int clientcount = 2;
679 sp<IBinder> target;
680 sp<IBinder> linkedclient[clientcount];
681 sp<BinderLibTestCallBack> callBack[clientcount];
682 sp<IBinder> passiveclient[clientcount];
683
684 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700685 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800686 for (int i = 0; i < clientcount; i++) {
687 {
688 Parcel data, reply;
689
690 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700691 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800692 callBack[i] = new BinderLibTestCallBack();
693 data.writeStrongBinder(target);
694 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700695 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
696 &reply, TF_ONE_WAY),
697 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800698 }
699 {
700 Parcel data, reply;
701
702 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700703 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800704 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700705 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
706 &reply, TF_ONE_WAY),
707 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800708 }
709 }
710 {
711 Parcel data, reply;
712 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
713 EXPECT_EQ(0, ret);
714 }
715
716 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700717 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
718 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800719 }
720}
721
Martijn Coenenf7100e42017-07-31 12:14:09 +0200722TEST_F(BinderLibTest, DeathNotificationThread)
723{
724 status_t ret;
725 sp<BinderLibTestCallBack> callback;
726 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700727 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200728 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700729 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200730
731 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
732
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700733 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200734
735 {
736 Parcel data, reply;
737 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
738 EXPECT_EQ(0, ret);
739 }
740
741 /* Make sure it's dead */
742 testDeathRecipient->waitEvent(5);
743
744 /* Now, pass the ref to another process and ask that process to
745 * call linkToDeath() on it, and wait for a response. This tests
746 * two things:
747 * 1) You still get death notifications when calling linkToDeath()
748 * on a ref that is already dead when it was passed to you.
749 * 2) That death notifications are not directly pushed to the thread
750 * registering them, but to the threadpool (proc workqueue) instead.
751 *
752 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
753 * is blocked on a condition variable waiting for the death notification to be
754 * called; therefore, that thread is not available for handling proc work.
755 * So, if the death notification was pushed to the thread workqueue, the callback
756 * would never be called, and the test would timeout and fail.
757 *
758 * Note that we can't do this part of the test from this thread itself, because
759 * the binder driver would only push death notifications to the thread if
760 * it is a looper thread, which this thread is not.
761 *
762 * See b/23525545 for details.
763 */
764 {
765 Parcel data, reply;
766
767 callback = new BinderLibTestCallBack();
768 data.writeStrongBinder(target);
769 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700770 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
771 TF_ONE_WAY),
772 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200773 }
774
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700775 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
776 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200777}
778
Riley Andrews06b01ad2014-12-18 12:10:08 -0800779TEST_F(BinderLibTest, PassFile) {
780 int ret;
781 int pipefd[2];
782 uint8_t buf[1] = { 0 };
783 uint8_t write_value = 123;
784
785 ret = pipe2(pipefd, O_NONBLOCK);
786 ASSERT_EQ(0, ret);
787
788 {
789 Parcel data, reply;
790 uint8_t writebuf[1] = { write_value };
791
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700792 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800793
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700794 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800795
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700796 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800797
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700798 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
799 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800800 }
801
802 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700803 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800804 EXPECT_EQ(write_value, buf[0]);
805
806 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
807
808 ret = read(pipefd[0], buf, sizeof(buf));
809 EXPECT_EQ(0, ret);
810
811 close(pipefd[0]);
812}
813
Ryo Hashimotobf551892018-05-31 16:58:35 +0900814TEST_F(BinderLibTest, PassParcelFileDescriptor) {
815 const int datasize = 123;
816 std::vector<uint8_t> writebuf(datasize);
817 for (size_t i = 0; i < writebuf.size(); ++i) {
818 writebuf[i] = i;
819 }
820
821 android::base::unique_fd read_end, write_end;
822 {
823 int pipefd[2];
824 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
825 read_end.reset(pipefd[0]);
826 write_end.reset(pipefd[1]);
827 }
828 {
829 Parcel data;
830 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
831 write_end.reset();
832 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
833 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
834
835 Parcel reply;
836 EXPECT_EQ(NO_ERROR,
837 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
838 &reply));
839 }
840 std::vector<uint8_t> readbuf(datasize);
841 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
842 EXPECT_EQ(writebuf, readbuf);
843
844 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
845
846 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
847}
848
Riley Andrews06b01ad2014-12-18 12:10:08 -0800849TEST_F(BinderLibTest, PromoteLocal) {
850 sp<IBinder> strong = new BBinder();
851 wp<IBinder> weak = strong;
852 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700853 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800854 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700855 strong = nullptr;
856 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800857 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700858 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800859}
860
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700861TEST_F(BinderLibTest, LocalGetExtension) {
862 sp<BBinder> binder = new BBinder();
863 sp<IBinder> ext = new BBinder();
864 binder->setExtension(ext);
865 EXPECT_EQ(ext, binder->getExtension());
866}
867
868TEST_F(BinderLibTest, RemoteGetExtension) {
869 sp<IBinder> server = addServer();
870 ASSERT_TRUE(server != nullptr);
871
872 sp<IBinder> extension;
873 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
874 ASSERT_NE(nullptr, extension.get());
875
876 EXPECT_EQ(NO_ERROR, extension->pingBinder());
877}
878
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700879TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700880 Parcel data, reply;
881
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700882 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
883 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700884
885 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700886 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800887 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
888 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
889 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
890 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700891}
892
Connor O'Brien52be2c92016-09-20 14:18:08 -0700893TEST_F(BinderLibTest, FreedBinder) {
894 status_t ret;
895
896 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700897 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700898
899 __u32 freedHandle;
900 wp<IBinder> keepFreedBinder;
901 {
902 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700903 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
904 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700905 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
906 freedHandle = freed->handle;
907 /* Add a weak ref to the freed binder so the driver does not
908 * delete its reference to it - otherwise the transaction
909 * fails regardless of whether the driver is fixed.
910 */
Steven Morelande171d622019-07-17 16:06:01 -0700911 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700912 }
Steven Morelande171d622019-07-17 16:06:01 -0700913 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700914 {
915 Parcel data, reply;
916 data.writeStrongBinder(server);
917 /* Replace original handle with handle to the freed binder */
918 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
919 __u32 oldHandle = strong->handle;
920 strong->handle = freedHandle;
921 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
922 /* Returns DEAD_OBJECT (-32) if target crashes and
923 * FAILED_TRANSACTION if the driver rejects the invalid
924 * object.
925 */
926 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
927 /* Restore original handle so parcel destructor does not use
928 * the wrong handle.
929 */
930 strong->handle = oldHandle;
931 }
932}
933
Sherry Yang336cdd32017-07-24 14:12:27 -0700934TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700935 Parcel data, reply;
936 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
937 for (int i = 0; i < 2; i++) {
938 BinderLibTestBundle datai;
939 datai.appendFrom(&data, 0, data.dataSize());
940
941 data.freeData();
942 data.writeInt32(1);
943 data.writeStrongBinder(callBack);
944 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
945
946 datai.appendTo(&data);
947 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700948 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
949 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700950}
951
Martijn Coenen45b07b42017-08-09 12:07:45 +0200952TEST_F(BinderLibTest, OnewayQueueing)
953{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200954 Parcel data, data2;
955
956 sp<IBinder> pollServer = addPollServer();
957
958 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
959 data.writeStrongBinder(callBack);
960 data.writeInt32(500000); // delay in us before calling back
961
962 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
963 data2.writeStrongBinder(callBack2);
964 data2.writeInt32(0); // delay in us
965
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700966 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
967 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200968
969 // The delay ensures that this second transaction will end up on the async_todo list
970 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700971 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
972 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200973
974 // The server will ensure that the two transactions are handled in the expected order;
975 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700976 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
977 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200978
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700979 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
980 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200981}
982
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100983TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
984{
985 status_t ret;
986 Parcel data, reply;
987 data.writeInterfaceToken(binderLibTestServiceName);
988 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
989 EXPECT_EQ(-1, reply.readInt32());
990 EXPECT_EQ(NO_ERROR, ret);
991}
992
993TEST_F(BinderLibTest, WorkSourceSet)
994{
995 status_t ret;
996 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000997 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000998 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100999 data.writeInterfaceToken(binderLibTestServiceName);
1000 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1001 EXPECT_EQ(100, reply.readInt32());
1002 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001003 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1004 EXPECT_EQ(NO_ERROR, ret);
1005}
1006
1007TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1008{
1009 status_t ret;
1010 Parcel data, reply;
1011
1012 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1013 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1014
1015 data.writeInterfaceToken(binderLibTestServiceName);
1016 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1017 EXPECT_EQ(-1, reply.readInt32());
1018 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001019 EXPECT_EQ(NO_ERROR, ret);
1020}
1021
1022TEST_F(BinderLibTest, WorkSourceCleared)
1023{
1024 status_t ret;
1025 Parcel data, reply;
1026
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001027 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001028 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1029 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001030 data.writeInterfaceToken(binderLibTestServiceName);
1031 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1032
1033 EXPECT_EQ(-1, reply.readInt32());
1034 EXPECT_EQ(100, previousWorkSource);
1035 EXPECT_EQ(NO_ERROR, ret);
1036}
1037
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001038TEST_F(BinderLibTest, WorkSourceRestored)
1039{
1040 status_t ret;
1041 Parcel data, reply;
1042
1043 IPCThreadState::self()->setCallingWorkSourceUid(100);
1044 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1045 IPCThreadState::self()->restoreCallingWorkSource(token);
1046
1047 data.writeInterfaceToken(binderLibTestServiceName);
1048 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1049
1050 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001051 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001052 EXPECT_EQ(NO_ERROR, ret);
1053}
1054
Olivier Gaillard91a04802018-11-14 17:32:41 +00001055TEST_F(BinderLibTest, PropagateFlagSet)
1056{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001057 IPCThreadState::self()->clearPropagateWorkSource();
1058 IPCThreadState::self()->setCallingWorkSourceUid(100);
1059 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1060}
1061
1062TEST_F(BinderLibTest, PropagateFlagCleared)
1063{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001064 IPCThreadState::self()->setCallingWorkSourceUid(100);
1065 IPCThreadState::self()->clearPropagateWorkSource();
1066 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1067}
1068
1069TEST_F(BinderLibTest, PropagateFlagRestored)
1070{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001071 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1072 IPCThreadState::self()->restoreCallingWorkSource(token);
1073
1074 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1075}
1076
1077TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1078{
1079 IPCThreadState::self()->setCallingWorkSourceUid(100);
1080
1081 Parcel data, reply;
1082 status_t ret;
1083 data.writeInterfaceToken(binderLibTestServiceName);
1084 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1085
1086 Parcel data2, reply2;
1087 status_t ret2;
1088 data2.writeInterfaceToken(binderLibTestServiceName);
1089 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1090 EXPECT_EQ(100, reply2.readInt32());
1091 EXPECT_EQ(NO_ERROR, ret2);
1092}
1093
Steven Morelandbf1915b2020-07-16 22:43:02 +00001094TEST_F(BinderLibTest, SchedPolicySet) {
1095 sp<IBinder> server = addServer();
1096 ASSERT_TRUE(server != nullptr);
1097
1098 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001099 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1100 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001101
1102 int policy = reply.readInt32();
1103 int priority = reply.readInt32();
1104
1105 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1106 EXPECT_EQ(kSchedPriority, priority);
1107}
1108
Steven Morelandcf03cf12020-12-04 02:58:40 +00001109TEST_F(BinderLibTest, InheritRt) {
1110 sp<IBinder> server = addServer();
1111 ASSERT_TRUE(server != nullptr);
1112
1113 const struct sched_param param {
1114 .sched_priority = kSchedPriorityMore,
1115 };
1116 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1117
1118 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001119 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1120 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001121
1122 int policy = reply.readInt32();
1123 int priority = reply.readInt32();
1124
1125 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1126 EXPECT_EQ(kSchedPriorityMore, priority);
1127}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001128
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001129TEST_F(BinderLibTest, VectorSent) {
1130 Parcel data, reply;
1131 sp<IBinder> server = addServer();
1132 ASSERT_TRUE(server != nullptr);
1133
1134 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1135 data.writeUint64Vector(testValue);
1136
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001137 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001138 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001139 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001140 EXPECT_EQ(readValue, testValue);
1141}
1142
Martijn Coenen82c75312019-07-24 15:18:30 +02001143TEST_F(BinderLibTest, BufRejected) {
1144 Parcel data, reply;
1145 uint32_t buf;
1146 sp<IBinder> server = addServer();
1147 ASSERT_TRUE(server != nullptr);
1148
1149 binder_buffer_object obj {
1150 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001151 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001152 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1153 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001154 };
1155 data.setDataCapacity(1024);
1156 // Write a bogus object at offset 0 to get an entry in the offset table
1157 data.writeFileDescriptor(0);
1158 EXPECT_EQ(data.objectsCount(), 1);
1159 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1160 // And now, overwrite it with the buffer object
1161 memcpy(parcelData, &obj, sizeof(obj));
1162 data.setDataSize(sizeof(obj));
1163
Martijn Coenen82c75312019-07-24 15:18:30 +02001164 // Either the kernel should reject this transaction (if it's correct), but
1165 // if it's not, the server implementation should return an error if it
1166 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001167 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1168 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001169}
1170
Steven Moreland254e8ef2021-04-19 22:28:50 +00001171TEST_F(BinderLibTest, GotSid) {
1172 sp<IBinder> server = addServer();
1173
1174 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001175 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001176}
1177
Yifan Hong84bedeb2021-04-21 21:37:17 -07001178class BinderLibRpcTestBase : public BinderLibTest {
1179public:
1180 void SetUp() override {
1181 if (!base::GetBoolProperty("ro.debuggable", false)) {
1182 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1183 "non-debuggable builds.";
1184 }
1185 BinderLibTest::SetUp();
1186 }
1187
1188 std::tuple<android::base::unique_fd, unsigned int> CreateSocket() {
1189 auto rpcServer = RpcServer::make();
1190 EXPECT_NE(nullptr, rpcServer);
1191 if (rpcServer == nullptr) return {};
1192 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1193 unsigned int port;
1194 if (!rpcServer->setupInetServer(0, &port)) {
1195 ADD_FAILURE() << "setupInetServer failed";
1196 return {};
1197 }
1198 return {rpcServer->releaseServer(), port};
1199 }
1200};
1201
1202class BinderLibRpcTest : public BinderLibRpcTestBase, public WithParamInterface<bool> {
1203public:
1204 sp<IBinder> GetService() {
1205 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1206 }
1207 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1208 return info.param ? "remote" : "local";
1209 }
1210};
1211
1212TEST_P(BinderLibRpcTest, SetRpcMaxThreads) {
1213 auto binder = GetService();
1214 ASSERT_TRUE(binder != nullptr);
1215 auto [socket, port] = CreateSocket();
1216 ASSERT_TRUE(socket.ok());
1217 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), 1), StatusEq(OK));
1218}
1219
1220TEST_P(BinderLibRpcTest, SetRpcClientNoFd) {
1221 auto binder = GetService();
1222 ASSERT_TRUE(binder != nullptr);
1223 EXPECT_THAT(binder->setRpcClientDebug(android::base::unique_fd(), 1), StatusEq(BAD_VALUE));
1224}
1225
1226TEST_P(BinderLibRpcTest, SetRpcMaxThreadsZero) {
1227 auto binder = GetService();
1228 ASSERT_TRUE(binder != nullptr);
1229 auto [socket, port] = CreateSocket();
1230 ASSERT_TRUE(socket.ok());
1231 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), 0), StatusEq(BAD_VALUE));
1232}
1233
1234TEST_P(BinderLibRpcTest, SetRpcMaxThreadsTwice) {
1235 auto binder = GetService();
1236 ASSERT_TRUE(binder != nullptr);
1237
1238 auto [socket1, port1] = CreateSocket();
1239 ASSERT_TRUE(socket1.ok());
1240 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), 1), StatusEq(OK));
1241
1242 auto [socket2, port2] = CreateSocket();
1243 ASSERT_TRUE(socket2.ok());
1244 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), 1), StatusEq(ALREADY_EXISTS));
1245}
1246
1247INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTest, testing::Bool(),
1248 BinderLibRpcTest::ParamToString);
1249
1250class BinderLibTestService;
1251class BinderLibRpcClientTest : public BinderLibRpcTestBase,
1252 public WithParamInterface<std::tuple<bool, uint32_t>> {
1253public:
1254 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1255 auto [isRemote, numThreads] = info.param;
1256 return (isRemote ? "remote" : "local") + "_server_with_"s + std::to_string(numThreads) +
1257 "_threads";
1258 }
1259 sp<IBinder> CreateRemoteService(int32_t id) {
1260 Parcel data, reply;
1261 status_t status = data.writeInt32(id);
1262 EXPECT_THAT(status, StatusEq(OK));
1263 if (status != OK) return nullptr;
1264 status = m_server->transact(BINDER_LIB_TEST_CREATE_TEST_SERVICE, data, &reply);
1265 EXPECT_THAT(status, StatusEq(OK));
1266 if (status != OK) return nullptr;
1267 sp<IBinder> ret;
1268 status = reply.readStrongBinder(&ret);
1269 EXPECT_THAT(status, StatusEq(OK));
1270 if (status != OK) return nullptr;
1271 return ret;
1272 }
1273};
1274
1275TEST_P(BinderLibRpcClientTest, Test) {
1276 auto [isRemote, numThreadsParam] = GetParam();
1277 uint32_t numThreads = numThreadsParam; // ... to be captured in lambda
1278 int32_t id = 0xC0FFEE00 + numThreads;
1279 sp<IBinder> server = isRemote ? sp<IBinder>(CreateRemoteService(id))
1280 : sp<IBinder>(sp<BinderLibTestService>::make(id, false));
1281 ASSERT_EQ(isRemote, !!server->remoteBinder());
1282 ASSERT_THAT(GetId(server), ResultHasValue(id));
1283
1284 unsigned int port = 0;
1285 // Fake servicedispatcher.
1286 {
1287 auto [socket, socketPort] = CreateSocket();
1288 ASSERT_TRUE(socket.ok());
1289 port = socketPort;
1290 ASSERT_THAT(server->setRpcClientDebug(std::move(socket), numThreads), StatusEq(OK));
1291 }
1292
1293 auto callUsleep = [](sp<IBinder> server, uint64_t us) {
1294 Parcel data, reply;
1295 data.markForBinder(server);
1296 const char *name = data.isForRpc() ? "RPC" : "binder";
1297 EXPECT_THAT(data.writeUint64(us), StatusEq(OK));
1298 EXPECT_THAT(server->transact(BINDER_LIB_TEST_USLEEP, data, &reply), StatusEq(OK))
1299 << "for " << name << " server";
1300 };
1301
1302 auto threadFn = [&](size_t threadNum) {
1303 usleep(threadNum * 50 * 1000); // threadNum * 50ms. Need this to avoid SYN flooding.
1304 auto rpcSession = RpcSession::make();
1305 ASSERT_TRUE(rpcSession->setupInetClient("127.0.0.1", port));
1306 auto rpcServerBinder = rpcSession->getRootObject();
1307 ASSERT_NE(nullptr, rpcServerBinder);
1308
1309 EXPECT_EQ(OK, rpcServerBinder->pingBinder());
1310
1311 // Check that |rpcServerBinder| and |server| points to the same service.
1312 EXPECT_THAT(GetId(rpcServerBinder), ResultHasValue(id));
1313
1314 // Occupy the server thread. The server should still have enough threads to handle
1315 // other connections.
1316 // (numThreads - threadNum) * 100ms
1317 callUsleep(rpcServerBinder, (numThreads - threadNum) * 100 * 1000);
1318 };
1319 std::vector<std::thread> threads;
1320 for (size_t i = 0; i < numThreads; ++i) threads.emplace_back(std::bind(threadFn, i));
1321 for (auto &t : threads) t.join();
1322}
1323
1324INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcClientTest,
1325 testing::Combine(testing::Bool(), testing::Range(1u, 10u)),
1326 BinderLibRpcClientTest::ParamToString);
1327
Yifan Hong543edcd2021-05-18 19:47:30 -07001328class BinderLibTestService : public BBinder {
1329public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001330 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1331 : m_id(id),
1332 m_nextServerId(id + 1),
1333 m_serverStartRequested(false),
1334 m_callback(nullptr),
1335 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001336 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1337 pthread_cond_init(&m_serverWaitCond, nullptr);
1338 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001339 ~BinderLibTestService() {
1340 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1341 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001342
Yifan Hong543edcd2021-05-18 19:47:30 -07001343 void processPendingCall() {
1344 if (m_callback != nullptr) {
1345 Parcel data;
1346 data.writeInt32(NO_ERROR);
1347 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1348 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001349 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001350 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001351
Yifan Hong543edcd2021-05-18 19:47:30 -07001352 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1353 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001354 // TODO(b/182914638): also checks getCallingUid() for RPC
1355 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001356 return PERMISSION_DENIED;
1357 }
1358 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001359 case BINDER_LIB_TEST_REGISTER_SERVER: {
1360 int32_t id;
1361 sp<IBinder> binder;
1362 id = data.readInt32();
1363 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001364 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001365 return BAD_VALUE;
1366 }
1367
Yifan Hong543edcd2021-05-18 19:47:30 -07001368 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001369
1370 pthread_mutex_lock(&m_serverWaitMutex);
1371 if (m_serverStartRequested) {
1372 m_serverStartRequested = false;
1373 m_serverStarted = binder;
1374 pthread_cond_signal(&m_serverWaitCond);
1375 }
1376 pthread_mutex_unlock(&m_serverWaitMutex);
1377 return NO_ERROR;
1378 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001379 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001380 case BINDER_LIB_TEST_ADD_SERVER: {
1381 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001382 int serverid;
1383
1384 if (m_id != 0) {
1385 return INVALID_OPERATION;
1386 }
1387 pthread_mutex_lock(&m_serverWaitMutex);
1388 if (m_serverStartRequested) {
1389 ret = -EBUSY;
1390 } else {
1391 serverid = m_nextServerId++;
1392 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001393 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001394
1395 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001396 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001397 pthread_mutex_lock(&m_serverWaitMutex);
1398 }
1399 if (ret > 0) {
1400 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401 struct timespec ts;
1402 clock_gettime(CLOCK_REALTIME, &ts);
1403 ts.tv_sec += 5;
1404 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001405 }
1406 if (m_serverStartRequested) {
1407 m_serverStartRequested = false;
1408 ret = -ETIMEDOUT;
1409 } else {
1410 reply->writeStrongBinder(m_serverStarted);
1411 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001412 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001413 ret = NO_ERROR;
1414 }
1415 } else if (ret >= 0) {
1416 m_serverStartRequested = false;
1417 ret = UNKNOWN_ERROR;
1418 }
1419 pthread_mutex_unlock(&m_serverWaitMutex);
1420 return ret;
1421 }
Steven Moreland35626652021-05-15 01:32:04 +00001422 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1423 IPCThreadState::SpGuard spGuard{
1424 .address = __builtin_frame_address(0),
1425 .context = "GuardInBinderTransaction",
1426 };
1427 const IPCThreadState::SpGuard *origGuard =
1428 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1429
1430 // if the guard works, this should abort
1431 (void)IPCThreadState::self()->getCallingPid();
1432
1433 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1434 return NO_ERROR;
1435 }
1436
Marco Ballesio7ee17572020-09-08 10:30:03 -07001437 case BINDER_LIB_TEST_GETPID:
1438 reply->writeInt32(getpid());
1439 return NO_ERROR;
1440 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1441 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001442 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001443 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001444 // oneway error codes should be ignored
1445 if (flags & TF_ONE_WAY) {
1446 return UNKNOWN_ERROR;
1447 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001448 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001449 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1450 // Note: this transaction is only designed for use with a
1451 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001452 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001453 // A callback was already pending; this means that
1454 // we received a second call while still processing
1455 // the first one. Fail the test.
1456 sp<IBinder> callback = data.readStrongBinder();
1457 Parcel data2;
1458 data2.writeInt32(UNKNOWN_ERROR);
1459
Yi Kong91635562018-06-07 14:38:36 -07001460 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001461 } else {
1462 m_callback = data.readStrongBinder();
1463 int32_t delayUs = data.readInt32();
1464 /*
1465 * It's necessary that we sleep here, so the next
1466 * transaction the caller makes will be queued to
1467 * the async queue.
1468 */
1469 usleep(delayUs);
1470
1471 /*
1472 * Now when we return, libbinder will tell the kernel
1473 * we are done with this transaction, and the kernel
1474 * can move the queued transaction to either the
1475 * thread todo worklist (for kernels without the fix),
1476 * or the proc todo worklist. In case of the former,
1477 * the next outbound call will pick up the pending
1478 * transaction, which leads to undesired reentrant
1479 * behavior. This is caught in the if() branch above.
1480 */
1481 }
1482
1483 return NO_ERROR;
1484 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001485 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1486 Parcel data2, reply2;
1487 sp<IBinder> binder;
1488 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001489 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001490 return BAD_VALUE;
1491 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001492 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001493 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1494 return NO_ERROR;
1495 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001496 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1497 reply->writeStrongBinder(this);
1498 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001499 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1500 reply->writeInt32(m_id);
1501 return NO_ERROR;
1502 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1503 int32_t count;
1504 uint32_t indirect_code;
1505 sp<IBinder> binder;
1506
1507 count = data.readInt32();
1508 reply->writeInt32(m_id);
1509 reply->writeInt32(count);
1510 for (int i = 0; i < count; i++) {
1511 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001512 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001513 return BAD_VALUE;
1514 }
1515 indirect_code = data.readInt32();
1516 BinderLibTestBundle data2(&data);
1517 if (!data2.isValid()) {
1518 return BAD_VALUE;
1519 }
1520 BinderLibTestBundle reply2;
1521 binder->transact(indirect_code, data2, &reply2);
1522 reply2.appendTo(reply);
1523 }
1524 return NO_ERROR;
1525 }
1526 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1527 reply->setError(data.readInt32());
1528 return NO_ERROR;
1529 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1530 reply->writeInt32(sizeof(void *));
1531 return NO_ERROR;
1532 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1533 return NO_ERROR;
1534 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1535 m_strongRef = data.readStrongBinder();
1536 return NO_ERROR;
1537 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1538 int ret;
1539 Parcel data2, reply2;
1540 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1541 sp<IBinder> target;
1542 sp<IBinder> callback;
1543
1544 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001545 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001546 return BAD_VALUE;
1547 }
1548 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001549 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001550 return BAD_VALUE;
1551 }
1552 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001553 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001554 data2.writeInt32(ret);
1555 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1556 return NO_ERROR;
1557 }
1558 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1559 int ret;
1560 int32_t size;
1561 const void *buf;
1562 int fd;
1563
1564 fd = data.readFileDescriptor();
1565 if (fd < 0) {
1566 return BAD_VALUE;
1567 }
1568 ret = data.readInt32(&size);
1569 if (ret != NO_ERROR) {
1570 return ret;
1571 }
1572 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001573 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001574 return BAD_VALUE;
1575 }
1576 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001577 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001578 return NO_ERROR;
1579 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001580 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1581 int ret;
1582 int32_t size;
1583 const void *buf;
1584 android::base::unique_fd fd;
1585
1586 ret = data.readUniqueParcelFileDescriptor(&fd);
1587 if (ret != NO_ERROR) {
1588 return ret;
1589 }
1590 ret = data.readInt32(&size);
1591 if (ret != NO_ERROR) {
1592 return ret;
1593 }
1594 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001595 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001596 return BAD_VALUE;
1597 }
1598 ret = write(fd.get(), buf, size);
1599 if (ret != size) return UNKNOWN_ERROR;
1600 return NO_ERROR;
1601 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001602 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1603 alarm(10);
1604 return NO_ERROR;
1605 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001606 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001607 ;
1608 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001609 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001610 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001611 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001612 return NO_ERROR;
1613 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001614 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1615 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001616 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001617 return NO_ERROR;
1618 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001619 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1620 int policy = 0;
1621 sched_param param;
1622 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1623 return UNKNOWN_ERROR;
1624 }
1625 reply->writeInt32(policy);
1626 reply->writeInt32(param.sched_priority);
1627 return NO_ERROR;
1628 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001629 case BINDER_LIB_TEST_ECHO_VECTOR: {
1630 std::vector<uint64_t> vector;
1631 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001632 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001633 reply->writeUint64Vector(vector);
1634 return NO_ERROR;
1635 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001636 case BINDER_LIB_TEST_REJECT_BUF: {
1637 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1638 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001639 case BINDER_LIB_TEST_CAN_GET_SID: {
1640 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1641 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001642 case BINDER_LIB_TEST_USLEEP: {
1643 uint64_t us;
1644 if (status_t status = data.readUint64(&us); status != NO_ERROR) return status;
1645 usleep(us);
1646 return NO_ERROR;
1647 }
1648 case BINDER_LIB_TEST_CREATE_TEST_SERVICE: {
1649 int32_t id;
1650 if (status_t status = data.readInt32(&id); status != NO_ERROR) return status;
1651 reply->writeStrongBinder(sp<BinderLibTestService>::make(id, false));
1652 return NO_ERROR;
1653 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001654 default:
1655 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001656 };
1657 }
1658
1659private:
1660 int32_t m_id;
1661 int32_t m_nextServerId;
1662 pthread_mutex_t m_serverWaitMutex;
1663 pthread_cond_t m_serverWaitCond;
1664 bool m_serverStartRequested;
1665 sp<IBinder> m_serverStarted;
1666 sp<IBinder> m_strongRef;
1667 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001668 bool m_exitOnDestroy;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001669};
1670
Martijn Coenen45b07b42017-08-09 12:07:45 +02001671int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001672{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001673 binderLibTestServiceName += String16(binderserversuffix);
1674
Steven Moreland35626652021-05-15 01:32:04 +00001675 // Testing to make sure that calls that we are serving can use getCallin*
1676 // even though we don't here.
1677 IPCThreadState::SpGuard spGuard{
1678 .address = __builtin_frame_address(0),
1679 .context = "main server thread",
1680 };
1681 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1682
Riley Andrews06b01ad2014-12-18 12:10:08 -08001683 status_t ret;
1684 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001685 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001686 {
1687 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001688
Steven Morelandbf1915b2020-07-16 22:43:02 +00001689 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1690
Steven Morelandcf03cf12020-12-04 02:58:40 +00001691 testService->setInheritRt(true);
1692
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001693 /*
1694 * Normally would also contain functionality as well, but we are only
1695 * testing the extension mechanism.
1696 */
1697 testService->setExtension(new BBinder());
1698
Martijn Coenen82c75312019-07-24 15:18:30 +02001699 // Required for test "BufRejected'
1700 testService->setRequestingSid(true);
1701
Martijn Coenen45b07b42017-08-09 12:07:45 +02001702 /*
1703 * We need this below, but can't hold a sp<> because it prevents the
1704 * node from being cleaned up automatically. It's safe in this case
1705 * because of how the tests are written.
1706 */
1707 testServicePtr = testService.get();
1708
Riley Andrews06b01ad2014-12-18 12:10:08 -08001709 if (index == 0) {
1710 ret = sm->addService(binderLibTestServiceName, testService);
1711 } else {
1712 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1713 Parcel data, reply;
1714 data.writeInt32(index);
1715 data.writeStrongBinder(testService);
1716
1717 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1718 }
1719 }
1720 write(readypipefd, &ret, sizeof(ret));
1721 close(readypipefd);
1722 //printf("%s: ret %d\n", __func__, ret);
1723 if (ret)
1724 return 1;
1725 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001726 if (usePoll) {
1727 int fd;
1728 struct epoll_event ev;
1729 int epoll_fd;
1730 IPCThreadState::self()->setupPolling(&fd);
1731 if (fd < 0) {
1732 return 1;
1733 }
1734 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1735
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001736 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001737 if (epoll_fd == -1) {
1738 return 1;
1739 }
1740
1741 ev.events = EPOLLIN;
1742 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1743 return 1;
1744 }
1745
1746 while (1) {
1747 /*
1748 * We simulate a single-threaded process using the binder poll
1749 * interface; besides handling binder commands, it can also
1750 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001751 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001752 *
1753 * processPendingCall() will then issue that transaction.
1754 */
1755 struct epoll_event events[1];
1756 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1757 if (numEvents < 0) {
1758 if (errno == EINTR) {
1759 continue;
1760 }
1761 return 1;
1762 }
1763 if (numEvents > 0) {
1764 IPCThreadState::self()->handlePolledCommands();
1765 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1766 testServicePtr->processPendingCall();
1767 }
1768 }
1769 } else {
1770 ProcessState::self()->startThreadPool();
1771 IPCThreadState::self()->joinThreadPool();
1772 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001773 //printf("%s: joinThreadPool returned\n", __func__);
1774 return 1; /* joinThreadPool should not return */
1775}
1776
1777int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001778 ExitIfWrongAbi();
1779
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001780 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001781 binderservername = argv[2];
1782 } else {
1783 binderservername = argv[0];
1784 }
1785
Martijn Coenen45b07b42017-08-09 12:07:45 +02001786 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1787 binderserversuffix = argv[5];
1788 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001789 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001790 binderserversuffix = new char[16];
1791 snprintf(binderserversuffix, 16, "%d", getpid());
1792 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001793
1794 ::testing::InitGoogleTest(&argc, argv);
1795 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1796 ProcessState::self()->startThreadPool();
1797 return RUN_ALL_TESTS();
1798}