blob: fdcc3822481c2443d154fcc313cc8db2e006c4b0 [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>
Yifan Hong28d6c352021-06-04 17:27:35 -070032#include <android-base/result-gmock.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <android-base/result.h>
34#include <android-base/unique_fd.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080035#include <binder/Binder.h>
36#include <binder/IBinder.h>
37#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070039#include <binder/RpcServer.h>
40#include <binder/RpcSession.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080041
Steven Morelandcf03cf12020-12-04 02:58:40 +000042#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020043#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080044#include <sys/prctl.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070045#include <sys/socket.h>
46#include <sys/un.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020047
Steven Moreland6ba5a252021-05-04 22:49:00 +000048#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070049#include "binderAbiHelper.h"
50
Riley Andrews06b01ad2014-12-18 12:10:08 -080051#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
52
53using namespace android;
Yifan Hong84bedeb2021-04-21 21:37:17 -070054using namespace std::string_literals;
55using namespace std::chrono_literals;
Yifan Hong28d6c352021-06-04 17:27:35 -070056using android::base::testing::HasValue;
Yifan Hong84bedeb2021-04-21 21:37:17 -070057using testing::ExplainMatchResult;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070058using testing::Not;
Yifan Hong84bedeb2021-04-21 21:37:17 -070059using testing::WithParamInterface;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070060
61// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
62MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
63 *result_listener << statusToString(arg);
64 return expected == arg;
65}
Riley Andrews06b01ad2014-12-18 12:10:08 -080066
Sherry Yang336cdd32017-07-24 14:12:27 -070067static ::testing::AssertionResult IsPageAligned(void *buf) {
68 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
69 return ::testing::AssertionSuccess();
70 else
71 return ::testing::AssertionFailure() << buf << " is not page aligned";
72}
73
Riley Andrews06b01ad2014-12-18 12:10:08 -080074static testing::Environment* binder_env;
75static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070076static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080077static char binderserverarg[] = "--binderserver";
78
Steven Morelandbf1915b2020-07-16 22:43:02 +000079static constexpr int kSchedPolicy = SCHED_RR;
80static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000081static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000082
Riley Andrews06b01ad2014-12-18 12:10:08 -080083static String16 binderLibTestServiceName = String16("test.binderLib");
84
85enum BinderLibTestTranscationCode {
86 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
87 BINDER_LIB_TEST_REGISTER_SERVER,
88 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020089 BINDER_LIB_TEST_ADD_POLL_SERVER,
Steven Moreland35626652021-05-15 01:32:04 +000090 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080091 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070092 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020093 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080094 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070095 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080096 BINDER_LIB_TEST_GET_ID_TRANSACTION,
97 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
98 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
99 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
100 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
101 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
102 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +0900103 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800104 BINDER_LIB_TEST_EXIT_TRANSACTION,
105 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
106 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -0700107 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100108 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +0000109 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -0700110 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
111 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800112 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +0200113 BINDER_LIB_TEST_REJECT_BUF,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000114 BINDER_LIB_TEST_CAN_GET_SID,
Yifan Hong84bedeb2021-04-21 21:37:17 -0700115 BINDER_LIB_TEST_USLEEP,
116 BINDER_LIB_TEST_CREATE_TEST_SERVICE,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800117};
118
Martijn Coenen45b07b42017-08-09 12:07:45 +0200119pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800120{
121 int ret;
122 pid_t pid;
123 status_t status;
124 int pipefd[2];
125 char stri[16];
126 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200127 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800128 char *childargv[] = {
129 binderservername,
130 binderserverarg,
131 stri,
132 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200133 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700134 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700135 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800136 };
137
138 ret = pipe(pipefd);
139 if (ret < 0)
140 return ret;
141
142 snprintf(stri, sizeof(stri), "%d", arg2);
143 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200144 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800145
146 pid = fork();
147 if (pid == -1)
148 return pid;
149 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800150 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800151 close(pipefd[0]);
152 execv(binderservername, childargv);
153 status = -errno;
154 write(pipefd[1], &status, sizeof(status));
155 fprintf(stderr, "execv failed, %s\n", strerror(errno));
156 _exit(EXIT_FAILURE);
157 }
158 close(pipefd[1]);
159 ret = read(pipefd[0], &status, sizeof(status));
160 //printf("pipe read returned %d, status %d\n", ret, status);
161 close(pipefd[0]);
162 if (ret == sizeof(status)) {
163 ret = status;
164 } else {
165 kill(pid, SIGKILL);
166 if (ret >= 0) {
167 ret = NO_INIT;
168 }
169 }
170 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700171 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800172 return ret;
173 }
174 return pid;
175}
176
Yifan Hong84bedeb2021-04-21 21:37:17 -0700177android::base::Result<int32_t> GetId(sp<IBinder> service) {
178 using android::base::Error;
179 Parcel data, reply;
180 data.markForBinder(service);
181 const char *prefix = data.isForRpc() ? "On RPC server, " : "On binder server, ";
182 status_t status = service->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
183 if (status != OK)
184 return Error(status) << prefix << "transact(GET_ID): " << statusToString(status);
185 int32_t result = 0;
186 status = reply.readInt32(&result);
187 if (status != OK) return Error(status) << prefix << "readInt32: " << statusToString(status);
188 return result;
189}
190
Riley Andrews06b01ad2014-12-18 12:10:08 -0800191class BinderLibTestEnv : public ::testing::Environment {
192 public:
193 BinderLibTestEnv() {}
194 sp<IBinder> getServer(void) {
195 return m_server;
196 }
197
198 private:
199 virtual void SetUp() {
200 m_serverpid = start_server_process(0);
201 //printf("m_serverpid %d\n", m_serverpid);
202 ASSERT_GT(m_serverpid, 0);
203
204 sp<IServiceManager> sm = defaultServiceManager();
205 //printf("%s: pid %d, get service\n", __func__, m_pid);
206 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700207 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800208 //printf("%s: pid %d, get service done\n", __func__, m_pid);
209 }
210 virtual void TearDown() {
211 status_t ret;
212 Parcel data, reply;
213 int exitStatus;
214 pid_t pid;
215
216 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700217 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800218 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
219 EXPECT_EQ(0, ret);
220 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
221 EXPECT_EQ(0, ret);
222 }
223 if (m_serverpid > 0) {
224 //printf("wait for %d\n", m_pids[i]);
225 pid = wait(&exitStatus);
226 EXPECT_EQ(m_serverpid, pid);
227 EXPECT_TRUE(WIFEXITED(exitStatus));
228 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
229 }
230 }
231
232 pid_t m_serverpid;
233 sp<IBinder> m_server;
234};
235
236class BinderLibTest : public ::testing::Test {
237 public:
238 virtual void SetUp() {
239 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000240 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800241 }
242 virtual void TearDown() {
243 }
244 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200245 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800246 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800247 int32_t id;
248 Parcel data, reply;
249 sp<IBinder> binder;
250
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700251 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800252
Yi Kong91635562018-06-07 14:38:36 -0700253 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800254 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700255 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700256 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800257 if (idPtr)
258 *idPtr = id;
259 return binder;
260 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200261
Yi Kong91635562018-06-07 14:38:36 -0700262 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200263 {
264 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
265 }
266
Yi Kong91635562018-06-07 14:38:36 -0700267 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200268 {
269 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
270 }
271
Riley Andrews06b01ad2014-12-18 12:10:08 -0800272 void waitForReadData(int fd, int timeout_ms) {
273 int ret;
274 pollfd pfd = pollfd();
275
276 pfd.fd = fd;
277 pfd.events = POLLIN;
278 ret = poll(&pfd, 1, timeout_ms);
279 EXPECT_EQ(1, ret);
280 }
281
282 sp<IBinder> m_server;
283};
284
285class BinderLibTestBundle : public Parcel
286{
287 public:
288 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800289 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800290 int32_t mark;
291 int32_t bundleLen;
292 size_t pos;
293
294 if (source->readInt32(&mark))
295 return;
296 if (mark != MARK_START)
297 return;
298 if (source->readInt32(&bundleLen))
299 return;
300 pos = source->dataPosition();
301 if (Parcel::appendFrom(source, pos, bundleLen))
302 return;
303 source->setDataPosition(pos + bundleLen);
304 if (source->readInt32(&mark))
305 return;
306 if (mark != MARK_END)
307 return;
308 m_isValid = true;
309 setDataPosition(0);
310 }
311 void appendTo(Parcel *dest) {
312 dest->writeInt32(MARK_START);
313 dest->writeInt32(dataSize());
314 dest->appendFrom(this, 0, dataSize());
315 dest->writeInt32(MARK_END);
316 };
317 bool isValid(void) {
318 return m_isValid;
319 }
320 private:
321 enum {
322 MARK_START = B_PACK_CHARS('B','T','B','S'),
323 MARK_END = B_PACK_CHARS('B','T','B','E'),
324 };
325 bool m_isValid;
326};
327
328class BinderLibTestEvent
329{
330 public:
331 BinderLibTestEvent(void)
332 : m_eventTriggered(false)
333 {
Yi Kong91635562018-06-07 14:38:36 -0700334 pthread_mutex_init(&m_waitMutex, nullptr);
335 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800336 }
337 int waitEvent(int timeout_s)
338 {
339 int ret;
340 pthread_mutex_lock(&m_waitMutex);
341 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800342 struct timespec ts;
343 clock_gettime(CLOCK_REALTIME, &ts);
344 ts.tv_sec += timeout_s;
345 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800346 }
347 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
348 pthread_mutex_unlock(&m_waitMutex);
349 return ret;
350 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200351 pthread_t getTriggeringThread()
352 {
353 return m_triggeringThread;
354 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800355 protected:
356 void triggerEvent(void) {
357 pthread_mutex_lock(&m_waitMutex);
358 pthread_cond_signal(&m_waitCond);
359 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200360 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800361 pthread_mutex_unlock(&m_waitMutex);
362 };
363 private:
364 pthread_mutex_t m_waitMutex;
365 pthread_cond_t m_waitCond;
366 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200367 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800368};
369
370class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
371{
372 public:
373 BinderLibTestCallBack()
374 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700375 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800376 {
377 }
378 status_t getResult(void)
379 {
380 return m_result;
381 }
382
383 private:
384 virtual status_t onTransact(uint32_t code,
385 const Parcel& data, Parcel* reply,
386 uint32_t flags = 0)
387 {
388 (void)reply;
389 (void)flags;
390 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200391 case BINDER_LIB_TEST_CALL_BACK: {
392 status_t status = data.readInt32(&m_result);
393 if (status != NO_ERROR) {
394 m_result = status;
395 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800396 triggerEvent();
397 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200398 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700399 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
400 sp<IBinder> server;
401 int ret;
402 const uint8_t *buf = data.data();
403 size_t size = data.dataSize();
404 if (m_prev_end) {
405 /* 64-bit kernel needs at most 8 bytes to align buffer end */
406 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
407 } else {
408 EXPECT_TRUE(IsPageAligned((void *)buf));
409 }
410
411 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
412
413 if (size > 0) {
414 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
415 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
416 data, reply);
417 EXPECT_EQ(NO_ERROR, ret);
418 }
419 return NO_ERROR;
420 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800421 default:
422 return UNKNOWN_TRANSACTION;
423 }
424 }
425
426 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700427 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800428};
429
430class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
431{
432 private:
433 virtual void binderDied(const wp<IBinder>& who) {
434 (void)who;
435 triggerEvent();
436 };
437};
438
439TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800440 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700441 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
442 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800443}
444
Steven Moreland80844f72020-12-12 02:06:08 +0000445TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000446 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700447 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
448 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000449}
450
Steven Morelandf183fdd2020-10-27 00:12:12 +0000451TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000452 Parcel data, reply;
453 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700454 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
455 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000456}
457
Marco Ballesio7ee17572020-09-08 10:30:03 -0700458TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700459 Parcel data, reply, replypid;
460 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
461
462 //Pass test on devices where the freezer is not supported
463 if (freezer_file.fail()) {
464 GTEST_SKIP();
465 return;
466 }
467
468 std::string freezer_enabled;
469 std::getline(freezer_file, freezer_enabled);
470
471 //Pass test on devices where the freezer is disabled
472 if (freezer_enabled != "1") {
473 GTEST_SKIP();
474 return;
475 }
476
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700477 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700478 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700479 for (int i = 0; i < 10; i++) {
480 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
481 }
482 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
483 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
484 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
485 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700486
487 bool sync_received, async_received;
488
489 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
490 &async_received));
491
492 EXPECT_EQ(sync_received, 1);
493 EXPECT_EQ(async_received, 0);
494
Marco Ballesio7ee17572020-09-08 10:30:03 -0700495 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
496 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
497}
498
Riley Andrews06b01ad2014-12-18 12:10:08 -0800499TEST_F(BinderLibTest, SetError) {
500 int32_t testValue[] = { 0, -123, 123 };
501 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800502 Parcel data, reply;
503 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700504 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
505 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800506 }
507}
508
509TEST_F(BinderLibTest, GetId) {
Yifan Hong28d6c352021-06-04 17:27:35 -0700510 EXPECT_THAT(GetId(m_server), HasValue(0));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800511}
512
513TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800514 int32_t ptrsize;
515 Parcel data, reply;
516 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700517 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700518 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
519 StatusEq(NO_ERROR));
520 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800521 RecordProperty("TestPtrSize", sizeof(void *));
522 RecordProperty("ServerPtrSize", sizeof(void *));
523}
524
525TEST_F(BinderLibTest, IndirectGetId2)
526{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800527 int32_t id;
528 int32_t count;
529 Parcel data, reply;
530 int32_t serverId[3];
531
532 data.writeInt32(ARRAY_SIZE(serverId));
533 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
534 sp<IBinder> server;
535 BinderLibTestBundle datai;
536
537 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700538 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800539 data.writeStrongBinder(server);
540 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
541 datai.appendTo(&data);
542 }
543
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700544 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
545 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800546
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700547 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800548 EXPECT_EQ(0, id);
549
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700550 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700551 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800552
553 for (size_t i = 0; i < (size_t)count; i++) {
554 BinderLibTestBundle replyi(&reply);
555 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700556 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800557 EXPECT_EQ(serverId[i], id);
558 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
559 }
560
561 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
562}
563
564TEST_F(BinderLibTest, IndirectGetId3)
565{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800566 int32_t id;
567 int32_t count;
568 Parcel data, reply;
569 int32_t serverId[3];
570
571 data.writeInt32(ARRAY_SIZE(serverId));
572 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
573 sp<IBinder> server;
574 BinderLibTestBundle datai;
575 BinderLibTestBundle datai2;
576
577 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700578 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800579 data.writeStrongBinder(server);
580 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
581
582 datai.writeInt32(1);
583 datai.writeStrongBinder(m_server);
584 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
585 datai2.appendTo(&datai);
586
587 datai.appendTo(&data);
588 }
589
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700590 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
591 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800592
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700593 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800594 EXPECT_EQ(0, id);
595
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700596 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700597 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800598
599 for (size_t i = 0; i < (size_t)count; i++) {
600 int32_t counti;
601
602 BinderLibTestBundle replyi(&reply);
603 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700604 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605 EXPECT_EQ(serverId[i], id);
606
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700607 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800608 EXPECT_EQ(1, counti);
609
610 BinderLibTestBundle replyi2(&replyi);
611 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700612 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800613 EXPECT_EQ(0, id);
614 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
615
616 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
617 }
618
619 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
620}
621
622TEST_F(BinderLibTest, CallBack)
623{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800624 Parcel data, reply;
625 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
626 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700627 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
628 StatusEq(NO_ERROR));
629 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
630 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631}
632
Steven Moreland35626652021-05-15 01:32:04 +0000633TEST_F(BinderLibTest, BinderCallContextGuard) {
634 sp<IBinder> binder = addServer();
635 Parcel data, reply;
636 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
637 StatusEq(DEAD_OBJECT));
638}
639
Riley Andrews06b01ad2014-12-18 12:10:08 -0800640TEST_F(BinderLibTest, AddServer)
641{
642 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700643 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800644}
645
Riley Andrews06b01ad2014-12-18 12:10:08 -0800646TEST_F(BinderLibTest, DeathNotificationStrongRef)
647{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800648 sp<IBinder> sbinder;
649
650 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
651
652 {
653 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700654 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700655 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800656 sbinder = binder;
657 }
658 {
659 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700660 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
661 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800662 }
663 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700664 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
665 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800666}
667
668TEST_F(BinderLibTest, DeathNotificationMultiple)
669{
670 status_t ret;
671 const int clientcount = 2;
672 sp<IBinder> target;
673 sp<IBinder> linkedclient[clientcount];
674 sp<BinderLibTestCallBack> callBack[clientcount];
675 sp<IBinder> passiveclient[clientcount];
676
677 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700678 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800679 for (int i = 0; i < clientcount; i++) {
680 {
681 Parcel data, reply;
682
683 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700684 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685 callBack[i] = new BinderLibTestCallBack();
686 data.writeStrongBinder(target);
687 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700688 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
689 &reply, TF_ONE_WAY),
690 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800691 }
692 {
693 Parcel data, reply;
694
695 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700696 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800697 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700698 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
699 &reply, TF_ONE_WAY),
700 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800701 }
702 }
703 {
704 Parcel data, reply;
705 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
706 EXPECT_EQ(0, ret);
707 }
708
709 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700710 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
711 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800712 }
713}
714
Martijn Coenenf7100e42017-07-31 12:14:09 +0200715TEST_F(BinderLibTest, DeathNotificationThread)
716{
717 status_t ret;
718 sp<BinderLibTestCallBack> callback;
719 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700720 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200721 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700722 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200723
724 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
725
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700726 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200727
728 {
729 Parcel data, reply;
730 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
731 EXPECT_EQ(0, ret);
732 }
733
734 /* Make sure it's dead */
735 testDeathRecipient->waitEvent(5);
736
737 /* Now, pass the ref to another process and ask that process to
738 * call linkToDeath() on it, and wait for a response. This tests
739 * two things:
740 * 1) You still get death notifications when calling linkToDeath()
741 * on a ref that is already dead when it was passed to you.
742 * 2) That death notifications are not directly pushed to the thread
743 * registering them, but to the threadpool (proc workqueue) instead.
744 *
745 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
746 * is blocked on a condition variable waiting for the death notification to be
747 * called; therefore, that thread is not available for handling proc work.
748 * So, if the death notification was pushed to the thread workqueue, the callback
749 * would never be called, and the test would timeout and fail.
750 *
751 * Note that we can't do this part of the test from this thread itself, because
752 * the binder driver would only push death notifications to the thread if
753 * it is a looper thread, which this thread is not.
754 *
755 * See b/23525545 for details.
756 */
757 {
758 Parcel data, reply;
759
760 callback = new BinderLibTestCallBack();
761 data.writeStrongBinder(target);
762 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700763 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
764 TF_ONE_WAY),
765 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200766 }
767
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700768 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
769 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200770}
771
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772TEST_F(BinderLibTest, PassFile) {
773 int ret;
774 int pipefd[2];
775 uint8_t buf[1] = { 0 };
776 uint8_t write_value = 123;
777
778 ret = pipe2(pipefd, O_NONBLOCK);
779 ASSERT_EQ(0, ret);
780
781 {
782 Parcel data, reply;
783 uint8_t writebuf[1] = { write_value };
784
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700785 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800786
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700787 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800788
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700789 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800790
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700791 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
792 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800793 }
794
795 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700796 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800797 EXPECT_EQ(write_value, buf[0]);
798
799 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
800
801 ret = read(pipefd[0], buf, sizeof(buf));
802 EXPECT_EQ(0, ret);
803
804 close(pipefd[0]);
805}
806
Ryo Hashimotobf551892018-05-31 16:58:35 +0900807TEST_F(BinderLibTest, PassParcelFileDescriptor) {
808 const int datasize = 123;
809 std::vector<uint8_t> writebuf(datasize);
810 for (size_t i = 0; i < writebuf.size(); ++i) {
811 writebuf[i] = i;
812 }
813
814 android::base::unique_fd read_end, write_end;
815 {
816 int pipefd[2];
817 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
818 read_end.reset(pipefd[0]);
819 write_end.reset(pipefd[1]);
820 }
821 {
822 Parcel data;
823 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
824 write_end.reset();
825 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
826 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
827
828 Parcel reply;
829 EXPECT_EQ(NO_ERROR,
830 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
831 &reply));
832 }
833 std::vector<uint8_t> readbuf(datasize);
834 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
835 EXPECT_EQ(writebuf, readbuf);
836
837 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
838
839 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
840}
841
Riley Andrews06b01ad2014-12-18 12:10:08 -0800842TEST_F(BinderLibTest, PromoteLocal) {
843 sp<IBinder> strong = new BBinder();
844 wp<IBinder> weak = strong;
845 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700846 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800847 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700848 strong = nullptr;
849 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800850 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700851 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800852}
853
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700854TEST_F(BinderLibTest, LocalGetExtension) {
855 sp<BBinder> binder = new BBinder();
856 sp<IBinder> ext = new BBinder();
857 binder->setExtension(ext);
858 EXPECT_EQ(ext, binder->getExtension());
859}
860
861TEST_F(BinderLibTest, RemoteGetExtension) {
862 sp<IBinder> server = addServer();
863 ASSERT_TRUE(server != nullptr);
864
865 sp<IBinder> extension;
866 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
867 ASSERT_NE(nullptr, extension.get());
868
869 EXPECT_EQ(NO_ERROR, extension->pingBinder());
870}
871
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700872TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700873 Parcel data, reply;
874
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700875 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
876 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700877
878 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700879 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800880 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
881 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
882 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
883 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700884}
885
Connor O'Brien52be2c92016-09-20 14:18:08 -0700886TEST_F(BinderLibTest, FreedBinder) {
887 status_t ret;
888
889 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700890 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700891
892 __u32 freedHandle;
893 wp<IBinder> keepFreedBinder;
894 {
895 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700896 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
897 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700898 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
899 freedHandle = freed->handle;
900 /* Add a weak ref to the freed binder so the driver does not
901 * delete its reference to it - otherwise the transaction
902 * fails regardless of whether the driver is fixed.
903 */
Steven Morelande171d622019-07-17 16:06:01 -0700904 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700905 }
Steven Morelande171d622019-07-17 16:06:01 -0700906 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700907 {
908 Parcel data, reply;
909 data.writeStrongBinder(server);
910 /* Replace original handle with handle to the freed binder */
911 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
912 __u32 oldHandle = strong->handle;
913 strong->handle = freedHandle;
914 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
915 /* Returns DEAD_OBJECT (-32) if target crashes and
916 * FAILED_TRANSACTION if the driver rejects the invalid
917 * object.
918 */
919 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
920 /* Restore original handle so parcel destructor does not use
921 * the wrong handle.
922 */
923 strong->handle = oldHandle;
924 }
925}
926
Sherry Yang336cdd32017-07-24 14:12:27 -0700927TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700928 Parcel data, reply;
929 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
930 for (int i = 0; i < 2; i++) {
931 BinderLibTestBundle datai;
932 datai.appendFrom(&data, 0, data.dataSize());
933
934 data.freeData();
935 data.writeInt32(1);
936 data.writeStrongBinder(callBack);
937 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
938
939 datai.appendTo(&data);
940 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700941 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
942 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700943}
944
Martijn Coenen45b07b42017-08-09 12:07:45 +0200945TEST_F(BinderLibTest, OnewayQueueing)
946{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200947 Parcel data, data2;
948
949 sp<IBinder> pollServer = addPollServer();
950
951 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
952 data.writeStrongBinder(callBack);
953 data.writeInt32(500000); // delay in us before calling back
954
955 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
956 data2.writeStrongBinder(callBack2);
957 data2.writeInt32(0); // delay in us
958
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700959 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
960 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200961
962 // The delay ensures that this second transaction will end up on the async_todo list
963 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700964 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
965 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200966
967 // The server will ensure that the two transactions are handled in the expected order;
968 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700969 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
970 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200971
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700972 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
973 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200974}
975
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100976TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
977{
978 status_t ret;
979 Parcel data, reply;
980 data.writeInterfaceToken(binderLibTestServiceName);
981 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
982 EXPECT_EQ(-1, reply.readInt32());
983 EXPECT_EQ(NO_ERROR, ret);
984}
985
986TEST_F(BinderLibTest, WorkSourceSet)
987{
988 status_t ret;
989 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000990 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000991 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100992 data.writeInterfaceToken(binderLibTestServiceName);
993 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
994 EXPECT_EQ(100, reply.readInt32());
995 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000996 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
997 EXPECT_EQ(NO_ERROR, ret);
998}
999
1000TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1001{
1002 status_t ret;
1003 Parcel data, reply;
1004
1005 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1006 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1007
1008 data.writeInterfaceToken(binderLibTestServiceName);
1009 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1010 EXPECT_EQ(-1, reply.readInt32());
1011 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001012 EXPECT_EQ(NO_ERROR, ret);
1013}
1014
1015TEST_F(BinderLibTest, WorkSourceCleared)
1016{
1017 status_t ret;
1018 Parcel data, reply;
1019
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001020 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001021 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1022 int32_t previousWorkSource = (int32_t)token;
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
1026 EXPECT_EQ(-1, reply.readInt32());
1027 EXPECT_EQ(100, previousWorkSource);
1028 EXPECT_EQ(NO_ERROR, ret);
1029}
1030
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001031TEST_F(BinderLibTest, WorkSourceRestored)
1032{
1033 status_t ret;
1034 Parcel data, reply;
1035
1036 IPCThreadState::self()->setCallingWorkSourceUid(100);
1037 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1038 IPCThreadState::self()->restoreCallingWorkSource(token);
1039
1040 data.writeInterfaceToken(binderLibTestServiceName);
1041 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1042
1043 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001044 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001045 EXPECT_EQ(NO_ERROR, ret);
1046}
1047
Olivier Gaillard91a04802018-11-14 17:32:41 +00001048TEST_F(BinderLibTest, PropagateFlagSet)
1049{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001050 IPCThreadState::self()->clearPropagateWorkSource();
1051 IPCThreadState::self()->setCallingWorkSourceUid(100);
1052 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1053}
1054
1055TEST_F(BinderLibTest, PropagateFlagCleared)
1056{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001057 IPCThreadState::self()->setCallingWorkSourceUid(100);
1058 IPCThreadState::self()->clearPropagateWorkSource();
1059 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1060}
1061
1062TEST_F(BinderLibTest, PropagateFlagRestored)
1063{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001064 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1065 IPCThreadState::self()->restoreCallingWorkSource(token);
1066
1067 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1068}
1069
1070TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1071{
1072 IPCThreadState::self()->setCallingWorkSourceUid(100);
1073
1074 Parcel data, reply;
1075 status_t ret;
1076 data.writeInterfaceToken(binderLibTestServiceName);
1077 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1078
1079 Parcel data2, reply2;
1080 status_t ret2;
1081 data2.writeInterfaceToken(binderLibTestServiceName);
1082 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1083 EXPECT_EQ(100, reply2.readInt32());
1084 EXPECT_EQ(NO_ERROR, ret2);
1085}
1086
Steven Morelandbf1915b2020-07-16 22:43:02 +00001087TEST_F(BinderLibTest, SchedPolicySet) {
1088 sp<IBinder> server = addServer();
1089 ASSERT_TRUE(server != nullptr);
1090
1091 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001092 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1093 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001094
1095 int policy = reply.readInt32();
1096 int priority = reply.readInt32();
1097
1098 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1099 EXPECT_EQ(kSchedPriority, priority);
1100}
1101
Steven Morelandcf03cf12020-12-04 02:58:40 +00001102TEST_F(BinderLibTest, InheritRt) {
1103 sp<IBinder> server = addServer();
1104 ASSERT_TRUE(server != nullptr);
1105
1106 const struct sched_param param {
1107 .sched_priority = kSchedPriorityMore,
1108 };
1109 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1110
1111 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001112 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1113 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001114
1115 int policy = reply.readInt32();
1116 int priority = reply.readInt32();
1117
1118 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1119 EXPECT_EQ(kSchedPriorityMore, priority);
1120}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001121
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001122TEST_F(BinderLibTest, VectorSent) {
1123 Parcel data, reply;
1124 sp<IBinder> server = addServer();
1125 ASSERT_TRUE(server != nullptr);
1126
1127 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1128 data.writeUint64Vector(testValue);
1129
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001130 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001131 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001132 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001133 EXPECT_EQ(readValue, testValue);
1134}
1135
Martijn Coenen82c75312019-07-24 15:18:30 +02001136TEST_F(BinderLibTest, BufRejected) {
1137 Parcel data, reply;
1138 uint32_t buf;
1139 sp<IBinder> server = addServer();
1140 ASSERT_TRUE(server != nullptr);
1141
1142 binder_buffer_object obj {
1143 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001144 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001145 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1146 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001147 };
1148 data.setDataCapacity(1024);
1149 // Write a bogus object at offset 0 to get an entry in the offset table
1150 data.writeFileDescriptor(0);
1151 EXPECT_EQ(data.objectsCount(), 1);
1152 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1153 // And now, overwrite it with the buffer object
1154 memcpy(parcelData, &obj, sizeof(obj));
1155 data.setDataSize(sizeof(obj));
1156
Martijn Coenen82c75312019-07-24 15:18:30 +02001157 // Either the kernel should reject this transaction (if it's correct), but
1158 // if it's not, the server implementation should return an error if it
1159 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001160 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1161 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001162}
1163
Steven Moreland254e8ef2021-04-19 22:28:50 +00001164TEST_F(BinderLibTest, GotSid) {
1165 sp<IBinder> server = addServer();
1166
1167 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001168 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001169}
1170
Yifan Hong84bedeb2021-04-21 21:37:17 -07001171class BinderLibRpcTestBase : public BinderLibTest {
1172public:
1173 void SetUp() override {
1174 if (!base::GetBoolProperty("ro.debuggable", false)) {
1175 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1176 "non-debuggable builds.";
1177 }
1178 BinderLibTest::SetUp();
1179 }
1180
1181 std::tuple<android::base::unique_fd, unsigned int> CreateSocket() {
1182 auto rpcServer = RpcServer::make();
1183 EXPECT_NE(nullptr, rpcServer);
1184 if (rpcServer == nullptr) return {};
1185 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1186 unsigned int port;
1187 if (!rpcServer->setupInetServer(0, &port)) {
1188 ADD_FAILURE() << "setupInetServer failed";
1189 return {};
1190 }
1191 return {rpcServer->releaseServer(), port};
1192 }
1193};
1194
1195class BinderLibRpcTest : public BinderLibRpcTestBase, public WithParamInterface<bool> {
1196public:
1197 sp<IBinder> GetService() {
1198 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1199 }
1200 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1201 return info.param ? "remote" : "local";
1202 }
1203};
1204
1205TEST_P(BinderLibRpcTest, SetRpcMaxThreads) {
1206 auto binder = GetService();
1207 ASSERT_TRUE(binder != nullptr);
1208 auto [socket, port] = CreateSocket();
1209 ASSERT_TRUE(socket.ok());
1210 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), 1), StatusEq(OK));
1211}
1212
1213TEST_P(BinderLibRpcTest, SetRpcClientNoFd) {
1214 auto binder = GetService();
1215 ASSERT_TRUE(binder != nullptr);
1216 EXPECT_THAT(binder->setRpcClientDebug(android::base::unique_fd(), 1), StatusEq(BAD_VALUE));
1217}
1218
1219TEST_P(BinderLibRpcTest, SetRpcMaxThreadsZero) {
1220 auto binder = GetService();
1221 ASSERT_TRUE(binder != nullptr);
1222 auto [socket, port] = CreateSocket();
1223 ASSERT_TRUE(socket.ok());
1224 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), 0), StatusEq(BAD_VALUE));
1225}
1226
1227TEST_P(BinderLibRpcTest, SetRpcMaxThreadsTwice) {
1228 auto binder = GetService();
1229 ASSERT_TRUE(binder != nullptr);
1230
1231 auto [socket1, port1] = CreateSocket();
1232 ASSERT_TRUE(socket1.ok());
1233 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), 1), StatusEq(OK));
1234
1235 auto [socket2, port2] = CreateSocket();
1236 ASSERT_TRUE(socket2.ok());
1237 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), 1), StatusEq(ALREADY_EXISTS));
1238}
1239
1240INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTest, testing::Bool(),
1241 BinderLibRpcTest::ParamToString);
1242
1243class BinderLibTestService;
1244class BinderLibRpcClientTest : public BinderLibRpcTestBase,
1245 public WithParamInterface<std::tuple<bool, uint32_t>> {
1246public:
1247 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1248 auto [isRemote, numThreads] = info.param;
1249 return (isRemote ? "remote" : "local") + "_server_with_"s + std::to_string(numThreads) +
1250 "_threads";
1251 }
1252 sp<IBinder> CreateRemoteService(int32_t id) {
1253 Parcel data, reply;
1254 status_t status = data.writeInt32(id);
1255 EXPECT_THAT(status, StatusEq(OK));
1256 if (status != OK) return nullptr;
1257 status = m_server->transact(BINDER_LIB_TEST_CREATE_TEST_SERVICE, data, &reply);
1258 EXPECT_THAT(status, StatusEq(OK));
1259 if (status != OK) return nullptr;
1260 sp<IBinder> ret;
1261 status = reply.readStrongBinder(&ret);
1262 EXPECT_THAT(status, StatusEq(OK));
1263 if (status != OK) return nullptr;
1264 return ret;
1265 }
1266};
1267
1268TEST_P(BinderLibRpcClientTest, Test) {
1269 auto [isRemote, numThreadsParam] = GetParam();
1270 uint32_t numThreads = numThreadsParam; // ... to be captured in lambda
1271 int32_t id = 0xC0FFEE00 + numThreads;
1272 sp<IBinder> server = isRemote ? sp<IBinder>(CreateRemoteService(id))
1273 : sp<IBinder>(sp<BinderLibTestService>::make(id, false));
1274 ASSERT_EQ(isRemote, !!server->remoteBinder());
Yifan Hong28d6c352021-06-04 17:27:35 -07001275 ASSERT_THAT(GetId(server), HasValue(id));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001276
1277 unsigned int port = 0;
1278 // Fake servicedispatcher.
1279 {
1280 auto [socket, socketPort] = CreateSocket();
1281 ASSERT_TRUE(socket.ok());
1282 port = socketPort;
1283 ASSERT_THAT(server->setRpcClientDebug(std::move(socket), numThreads), StatusEq(OK));
1284 }
1285
1286 auto callUsleep = [](sp<IBinder> server, uint64_t us) {
1287 Parcel data, reply;
1288 data.markForBinder(server);
1289 const char *name = data.isForRpc() ? "RPC" : "binder";
1290 EXPECT_THAT(data.writeUint64(us), StatusEq(OK));
1291 EXPECT_THAT(server->transact(BINDER_LIB_TEST_USLEEP, data, &reply), StatusEq(OK))
1292 << "for " << name << " server";
1293 };
1294
1295 auto threadFn = [&](size_t threadNum) {
1296 usleep(threadNum * 50 * 1000); // threadNum * 50ms. Need this to avoid SYN flooding.
1297 auto rpcSession = RpcSession::make();
1298 ASSERT_TRUE(rpcSession->setupInetClient("127.0.0.1", port));
1299 auto rpcServerBinder = rpcSession->getRootObject();
1300 ASSERT_NE(nullptr, rpcServerBinder);
1301
1302 EXPECT_EQ(OK, rpcServerBinder->pingBinder());
1303
1304 // Check that |rpcServerBinder| and |server| points to the same service.
Yifan Hong28d6c352021-06-04 17:27:35 -07001305 EXPECT_THAT(GetId(rpcServerBinder), HasValue(id));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001306
1307 // Occupy the server thread. The server should still have enough threads to handle
1308 // other connections.
1309 // (numThreads - threadNum) * 100ms
1310 callUsleep(rpcServerBinder, (numThreads - threadNum) * 100 * 1000);
1311 };
1312 std::vector<std::thread> threads;
1313 for (size_t i = 0; i < numThreads; ++i) threads.emplace_back(std::bind(threadFn, i));
1314 for (auto &t : threads) t.join();
1315}
1316
1317INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcClientTest,
1318 testing::Combine(testing::Bool(), testing::Range(1u, 10u)),
1319 BinderLibRpcClientTest::ParamToString);
1320
Yifan Hong543edcd2021-05-18 19:47:30 -07001321class BinderLibTestService : public BBinder {
1322public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001323 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1324 : m_id(id),
1325 m_nextServerId(id + 1),
1326 m_serverStartRequested(false),
1327 m_callback(nullptr),
1328 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001329 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1330 pthread_cond_init(&m_serverWaitCond, nullptr);
1331 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001332 ~BinderLibTestService() {
1333 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1334 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001335
Yifan Hong543edcd2021-05-18 19:47:30 -07001336 void processPendingCall() {
1337 if (m_callback != nullptr) {
1338 Parcel data;
1339 data.writeInt32(NO_ERROR);
1340 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1341 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001342 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001343 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001344
Yifan Hong543edcd2021-05-18 19:47:30 -07001345 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1346 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001347 // TODO(b/182914638): also checks getCallingUid() for RPC
1348 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001349 return PERMISSION_DENIED;
1350 }
1351 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001352 case BINDER_LIB_TEST_REGISTER_SERVER: {
1353 int32_t id;
1354 sp<IBinder> binder;
1355 id = data.readInt32();
1356 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001357 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358 return BAD_VALUE;
1359 }
1360
Yifan Hong543edcd2021-05-18 19:47:30 -07001361 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001362
1363 pthread_mutex_lock(&m_serverWaitMutex);
1364 if (m_serverStartRequested) {
1365 m_serverStartRequested = false;
1366 m_serverStarted = binder;
1367 pthread_cond_signal(&m_serverWaitCond);
1368 }
1369 pthread_mutex_unlock(&m_serverWaitMutex);
1370 return NO_ERROR;
1371 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001372 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001373 case BINDER_LIB_TEST_ADD_SERVER: {
1374 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001375 int serverid;
1376
1377 if (m_id != 0) {
1378 return INVALID_OPERATION;
1379 }
1380 pthread_mutex_lock(&m_serverWaitMutex);
1381 if (m_serverStartRequested) {
1382 ret = -EBUSY;
1383 } else {
1384 serverid = m_nextServerId++;
1385 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001386 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001387
1388 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001389 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001390 pthread_mutex_lock(&m_serverWaitMutex);
1391 }
1392 if (ret > 0) {
1393 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001394 struct timespec ts;
1395 clock_gettime(CLOCK_REALTIME, &ts);
1396 ts.tv_sec += 5;
1397 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398 }
1399 if (m_serverStartRequested) {
1400 m_serverStartRequested = false;
1401 ret = -ETIMEDOUT;
1402 } else {
1403 reply->writeStrongBinder(m_serverStarted);
1404 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001405 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001406 ret = NO_ERROR;
1407 }
1408 } else if (ret >= 0) {
1409 m_serverStartRequested = false;
1410 ret = UNKNOWN_ERROR;
1411 }
1412 pthread_mutex_unlock(&m_serverWaitMutex);
1413 return ret;
1414 }
Steven Moreland35626652021-05-15 01:32:04 +00001415 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1416 IPCThreadState::SpGuard spGuard{
1417 .address = __builtin_frame_address(0),
1418 .context = "GuardInBinderTransaction",
1419 };
1420 const IPCThreadState::SpGuard *origGuard =
1421 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1422
1423 // if the guard works, this should abort
1424 (void)IPCThreadState::self()->getCallingPid();
1425
1426 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1427 return NO_ERROR;
1428 }
1429
Marco Ballesio7ee17572020-09-08 10:30:03 -07001430 case BINDER_LIB_TEST_GETPID:
1431 reply->writeInt32(getpid());
1432 return NO_ERROR;
1433 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1434 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001435 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001436 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001437 // oneway error codes should be ignored
1438 if (flags & TF_ONE_WAY) {
1439 return UNKNOWN_ERROR;
1440 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001441 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001442 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1443 // Note: this transaction is only designed for use with a
1444 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001445 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001446 // A callback was already pending; this means that
1447 // we received a second call while still processing
1448 // the first one. Fail the test.
1449 sp<IBinder> callback = data.readStrongBinder();
1450 Parcel data2;
1451 data2.writeInt32(UNKNOWN_ERROR);
1452
Yi Kong91635562018-06-07 14:38:36 -07001453 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001454 } else {
1455 m_callback = data.readStrongBinder();
1456 int32_t delayUs = data.readInt32();
1457 /*
1458 * It's necessary that we sleep here, so the next
1459 * transaction the caller makes will be queued to
1460 * the async queue.
1461 */
1462 usleep(delayUs);
1463
1464 /*
1465 * Now when we return, libbinder will tell the kernel
1466 * we are done with this transaction, and the kernel
1467 * can move the queued transaction to either the
1468 * thread todo worklist (for kernels without the fix),
1469 * or the proc todo worklist. In case of the former,
1470 * the next outbound call will pick up the pending
1471 * transaction, which leads to undesired reentrant
1472 * behavior. This is caught in the if() branch above.
1473 */
1474 }
1475
1476 return NO_ERROR;
1477 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001478 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1479 Parcel data2, reply2;
1480 sp<IBinder> binder;
1481 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001482 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001483 return BAD_VALUE;
1484 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001485 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001486 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1487 return NO_ERROR;
1488 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001489 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1490 reply->writeStrongBinder(this);
1491 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001492 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1493 reply->writeInt32(m_id);
1494 return NO_ERROR;
1495 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1496 int32_t count;
1497 uint32_t indirect_code;
1498 sp<IBinder> binder;
1499
1500 count = data.readInt32();
1501 reply->writeInt32(m_id);
1502 reply->writeInt32(count);
1503 for (int i = 0; i < count; i++) {
1504 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001505 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001506 return BAD_VALUE;
1507 }
1508 indirect_code = data.readInt32();
1509 BinderLibTestBundle data2(&data);
1510 if (!data2.isValid()) {
1511 return BAD_VALUE;
1512 }
1513 BinderLibTestBundle reply2;
1514 binder->transact(indirect_code, data2, &reply2);
1515 reply2.appendTo(reply);
1516 }
1517 return NO_ERROR;
1518 }
1519 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1520 reply->setError(data.readInt32());
1521 return NO_ERROR;
1522 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1523 reply->writeInt32(sizeof(void *));
1524 return NO_ERROR;
1525 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1526 return NO_ERROR;
1527 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1528 m_strongRef = data.readStrongBinder();
1529 return NO_ERROR;
1530 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1531 int ret;
1532 Parcel data2, reply2;
1533 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1534 sp<IBinder> target;
1535 sp<IBinder> callback;
1536
1537 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001538 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001539 return BAD_VALUE;
1540 }
1541 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001542 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001543 return BAD_VALUE;
1544 }
1545 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001546 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001547 data2.writeInt32(ret);
1548 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1549 return NO_ERROR;
1550 }
1551 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1552 int ret;
1553 int32_t size;
1554 const void *buf;
1555 int fd;
1556
1557 fd = data.readFileDescriptor();
1558 if (fd < 0) {
1559 return BAD_VALUE;
1560 }
1561 ret = data.readInt32(&size);
1562 if (ret != NO_ERROR) {
1563 return ret;
1564 }
1565 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001566 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001567 return BAD_VALUE;
1568 }
1569 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001570 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001571 return NO_ERROR;
1572 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001573 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1574 int ret;
1575 int32_t size;
1576 const void *buf;
1577 android::base::unique_fd fd;
1578
1579 ret = data.readUniqueParcelFileDescriptor(&fd);
1580 if (ret != NO_ERROR) {
1581 return ret;
1582 }
1583 ret = data.readInt32(&size);
1584 if (ret != NO_ERROR) {
1585 return ret;
1586 }
1587 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001588 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001589 return BAD_VALUE;
1590 }
1591 ret = write(fd.get(), buf, size);
1592 if (ret != size) return UNKNOWN_ERROR;
1593 return NO_ERROR;
1594 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001595 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1596 alarm(10);
1597 return NO_ERROR;
1598 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001599 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001600 ;
1601 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001602 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001603 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001604 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001605 return NO_ERROR;
1606 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001607 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1608 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001609 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001610 return NO_ERROR;
1611 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001612 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1613 int policy = 0;
1614 sched_param param;
1615 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1616 return UNKNOWN_ERROR;
1617 }
1618 reply->writeInt32(policy);
1619 reply->writeInt32(param.sched_priority);
1620 return NO_ERROR;
1621 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001622 case BINDER_LIB_TEST_ECHO_VECTOR: {
1623 std::vector<uint64_t> vector;
1624 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001625 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001626 reply->writeUint64Vector(vector);
1627 return NO_ERROR;
1628 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001629 case BINDER_LIB_TEST_REJECT_BUF: {
1630 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1631 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001632 case BINDER_LIB_TEST_CAN_GET_SID: {
1633 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1634 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001635 case BINDER_LIB_TEST_USLEEP: {
1636 uint64_t us;
1637 if (status_t status = data.readUint64(&us); status != NO_ERROR) return status;
1638 usleep(us);
1639 return NO_ERROR;
1640 }
1641 case BINDER_LIB_TEST_CREATE_TEST_SERVICE: {
1642 int32_t id;
1643 if (status_t status = data.readInt32(&id); status != NO_ERROR) return status;
1644 reply->writeStrongBinder(sp<BinderLibTestService>::make(id, false));
1645 return NO_ERROR;
1646 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001647 default:
1648 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001649 };
1650 }
1651
1652private:
1653 int32_t m_id;
1654 int32_t m_nextServerId;
1655 pthread_mutex_t m_serverWaitMutex;
1656 pthread_cond_t m_serverWaitCond;
1657 bool m_serverStartRequested;
1658 sp<IBinder> m_serverStarted;
1659 sp<IBinder> m_strongRef;
1660 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001661 bool m_exitOnDestroy;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001662};
1663
Martijn Coenen45b07b42017-08-09 12:07:45 +02001664int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001665{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001666 binderLibTestServiceName += String16(binderserversuffix);
1667
Steven Moreland35626652021-05-15 01:32:04 +00001668 // Testing to make sure that calls that we are serving can use getCallin*
1669 // even though we don't here.
1670 IPCThreadState::SpGuard spGuard{
1671 .address = __builtin_frame_address(0),
1672 .context = "main server thread",
1673 };
1674 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1675
Riley Andrews06b01ad2014-12-18 12:10:08 -08001676 status_t ret;
1677 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001678 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001679 {
1680 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001681
Steven Morelandbf1915b2020-07-16 22:43:02 +00001682 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1683
Steven Morelandcf03cf12020-12-04 02:58:40 +00001684 testService->setInheritRt(true);
1685
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001686 /*
1687 * Normally would also contain functionality as well, but we are only
1688 * testing the extension mechanism.
1689 */
1690 testService->setExtension(new BBinder());
1691
Martijn Coenen82c75312019-07-24 15:18:30 +02001692 // Required for test "BufRejected'
1693 testService->setRequestingSid(true);
1694
Martijn Coenen45b07b42017-08-09 12:07:45 +02001695 /*
1696 * We need this below, but can't hold a sp<> because it prevents the
1697 * node from being cleaned up automatically. It's safe in this case
1698 * because of how the tests are written.
1699 */
1700 testServicePtr = testService.get();
1701
Riley Andrews06b01ad2014-12-18 12:10:08 -08001702 if (index == 0) {
1703 ret = sm->addService(binderLibTestServiceName, testService);
1704 } else {
1705 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1706 Parcel data, reply;
1707 data.writeInt32(index);
1708 data.writeStrongBinder(testService);
1709
1710 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1711 }
1712 }
1713 write(readypipefd, &ret, sizeof(ret));
1714 close(readypipefd);
1715 //printf("%s: ret %d\n", __func__, ret);
1716 if (ret)
1717 return 1;
1718 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001719 if (usePoll) {
1720 int fd;
1721 struct epoll_event ev;
1722 int epoll_fd;
1723 IPCThreadState::self()->setupPolling(&fd);
1724 if (fd < 0) {
1725 return 1;
1726 }
1727 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1728
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001729 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001730 if (epoll_fd == -1) {
1731 return 1;
1732 }
1733
1734 ev.events = EPOLLIN;
1735 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1736 return 1;
1737 }
1738
1739 while (1) {
1740 /*
1741 * We simulate a single-threaded process using the binder poll
1742 * interface; besides handling binder commands, it can also
1743 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001744 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001745 *
1746 * processPendingCall() will then issue that transaction.
1747 */
1748 struct epoll_event events[1];
1749 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1750 if (numEvents < 0) {
1751 if (errno == EINTR) {
1752 continue;
1753 }
1754 return 1;
1755 }
1756 if (numEvents > 0) {
1757 IPCThreadState::self()->handlePolledCommands();
1758 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1759 testServicePtr->processPendingCall();
1760 }
1761 }
1762 } else {
1763 ProcessState::self()->startThreadPool();
1764 IPCThreadState::self()->joinThreadPool();
1765 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001766 //printf("%s: joinThreadPool returned\n", __func__);
1767 return 1; /* joinThreadPool should not return */
1768}
1769
1770int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001771 ExitIfWrongAbi();
1772
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001773 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001774 binderservername = argv[2];
1775 } else {
1776 binderservername = argv[0];
1777 }
1778
Martijn Coenen45b07b42017-08-09 12:07:45 +02001779 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1780 binderserversuffix = argv[5];
1781 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001782 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001783 binderserversuffix = new char[16];
1784 snprintf(binderserversuffix, 16, "%d", getpid());
1785 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001786
1787 ::testing::InitGoogleTest(&argc, argv);
1788 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1789 ProcessState::self()->startThreadPool();
1790 return RUN_ALL_TESTS();
1791}