blob: cb1a1ee443cd8e554e92c72249ddda2910dde279 [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>
Riley Andrews06b01ad2014-12-18 12:10:08 -080018#include <poll.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070022
23#include <chrono>
Yifan Hong8b890852021-06-10 13:44:09 -070024#include <fstream>
Steven Morelandd7088702021-01-13 00:27:00 +000025#include <thread>
Riley Andrews06b01ad2014-12-18 12:10:08 -080026
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070027#include <gmock/gmock.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080028#include <gtest/gtest.h>
29
Yifan Hong84bedeb2021-04-21 21:37:17 -070030#include <android-base/properties.h>
Yifan Hong28d6c352021-06-04 17:27:35 -070031#include <android-base/result-gmock.h>
Yifan Hong8b890852021-06-10 13:44:09 -070032#include <android-base/strings.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080033#include <binder/Binder.h>
Yifan Hong34823232021-06-07 17:23:00 -070034#include <binder/BpBinder.h>
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000035#include <binder/Functional.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080036#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>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070041#include <binder/unique_fd.h>
Tomasz Wasilczyk300aa132023-10-26 15:00:04 -070042#include <utils/Flattenable.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080043
Steven Morelandcf03cf12020-12-04 02:58:40 +000044#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020045#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080046#include <sys/prctl.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070047#include <sys/socket.h>
48#include <sys/un.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020049
Steven Moreland6ba5a252021-05-04 22:49:00 +000050#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070051
Riley Andrews06b01ad2014-12-18 12:10:08 -080052#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
53
54using namespace android;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000055using namespace android::binder::impl;
Yifan Hong84bedeb2021-04-21 21:37:17 -070056using namespace std::string_literals;
57using namespace std::chrono_literals;
Yifan Hong28d6c352021-06-04 17:27:35 -070058using android::base::testing::HasValue;
Yifan Hong8b890852021-06-10 13:44:09 -070059using android::base::testing::Ok;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070060using android::binder::unique_fd;
Yifan Hong84bedeb2021-04-21 21:37:17 -070061using testing::ExplainMatchResult;
Yifan Hongbd276552022-02-28 15:28:51 -080062using testing::Matcher;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070063using testing::Not;
Yifan Hong84bedeb2021-04-21 21:37:17 -070064using testing::WithParamInterface;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070065
66// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
67MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
68 *result_listener << statusToString(arg);
69 return expected == arg;
70}
Riley Andrews06b01ad2014-12-18 12:10:08 -080071
Sherry Yang336cdd32017-07-24 14:12:27 -070072static ::testing::AssertionResult IsPageAligned(void *buf) {
73 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
74 return ::testing::AssertionSuccess();
75 else
76 return ::testing::AssertionFailure() << buf << " is not page aligned";
77}
78
Riley Andrews06b01ad2014-12-18 12:10:08 -080079static testing::Environment* binder_env;
80static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070081static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080082static char binderserverarg[] = "--binderserver";
83
Steven Morelandbf1915b2020-07-16 22:43:02 +000084static constexpr int kSchedPolicy = SCHED_RR;
85static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000086static constexpr int kSchedPriorityMore = 8;
Steven Moreland3e9debc2023-06-15 00:35:29 +000087static constexpr int kKernelThreads = 17; // anything different than the default
Steven Morelandbf1915b2020-07-16 22:43:02 +000088
Riley Andrews06b01ad2014-12-18 12:10:08 -080089static String16 binderLibTestServiceName = String16("test.binderLib");
90
91enum BinderLibTestTranscationCode {
92 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
93 BINDER_LIB_TEST_REGISTER_SERVER,
94 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020095 BINDER_LIB_TEST_ADD_POLL_SERVER,
Steven Moreland35626652021-05-15 01:32:04 +000096 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080097 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070098 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020099 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800100 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700101 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800102 BINDER_LIB_TEST_GET_ID_TRANSACTION,
103 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
104 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
105 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
106 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
107 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
108 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +0900109 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800110 BINDER_LIB_TEST_EXIT_TRANSACTION,
111 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
112 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -0700113 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100114 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +0000115 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -0700116 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
117 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800118 BINDER_LIB_TEST_ECHO_VECTOR,
Siarhei Vishniakou116f6b82022-10-03 13:43:15 -0700119 BINDER_LIB_TEST_GET_NON_BLOCKING_FD,
Steven Morelandf2e0a952021-11-01 18:17:23 -0700120 BINDER_LIB_TEST_REJECT_OBJECTS,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000121 BINDER_LIB_TEST_CAN_GET_SID,
Elie Kheirallah47431c12022-04-21 23:46:17 +0000122 BINDER_LIB_TEST_GET_MAX_THREAD_COUNT,
123 BINDER_LIB_TEST_SET_MAX_THREAD_COUNT,
Devin Moore4354f712022-12-08 01:44:46 +0000124 BINDER_LIB_TEST_IS_THREADPOOL_STARTED,
Elie Kheirallah47431c12022-04-21 23:46:17 +0000125 BINDER_LIB_TEST_LOCK_UNLOCK,
126 BINDER_LIB_TEST_PROCESS_LOCK,
127 BINDER_LIB_TEST_UNLOCK_AFTER_MS,
128 BINDER_LIB_TEST_PROCESS_TEMPORARY_LOCK
Riley Andrews06b01ad2014-12-18 12:10:08 -0800129};
130
Martijn Coenen45b07b42017-08-09 12:07:45 +0200131pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800132{
133 int ret;
134 pid_t pid;
135 status_t status;
136 int pipefd[2];
137 char stri[16];
138 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200139 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800140 char *childargv[] = {
141 binderservername,
142 binderserverarg,
143 stri,
144 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200145 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700146 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700147 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800148 };
149
150 ret = pipe(pipefd);
151 if (ret < 0)
152 return ret;
153
154 snprintf(stri, sizeof(stri), "%d", arg2);
155 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200156 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800157
158 pid = fork();
159 if (pid == -1)
160 return pid;
161 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800162 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800163 close(pipefd[0]);
164 execv(binderservername, childargv);
165 status = -errno;
166 write(pipefd[1], &status, sizeof(status));
167 fprintf(stderr, "execv failed, %s\n", strerror(errno));
168 _exit(EXIT_FAILURE);
169 }
170 close(pipefd[1]);
171 ret = read(pipefd[0], &status, sizeof(status));
172 //printf("pipe read returned %d, status %d\n", ret, status);
173 close(pipefd[0]);
174 if (ret == sizeof(status)) {
175 ret = status;
176 } else {
177 kill(pid, SIGKILL);
178 if (ret >= 0) {
179 ret = NO_INIT;
180 }
181 }
182 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700183 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800184 return ret;
185 }
186 return pid;
187}
188
Yifan Hong84bedeb2021-04-21 21:37:17 -0700189android::base::Result<int32_t> GetId(sp<IBinder> service) {
190 using android::base::Error;
191 Parcel data, reply;
192 data.markForBinder(service);
193 const char *prefix = data.isForRpc() ? "On RPC server, " : "On binder server, ";
194 status_t status = service->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
195 if (status != OK)
196 return Error(status) << prefix << "transact(GET_ID): " << statusToString(status);
197 int32_t result = 0;
198 status = reply.readInt32(&result);
199 if (status != OK) return Error(status) << prefix << "readInt32: " << statusToString(status);
200 return result;
201}
202
Riley Andrews06b01ad2014-12-18 12:10:08 -0800203class BinderLibTestEnv : public ::testing::Environment {
204 public:
205 BinderLibTestEnv() {}
206 sp<IBinder> getServer(void) {
207 return m_server;
208 }
209
210 private:
211 virtual void SetUp() {
212 m_serverpid = start_server_process(0);
213 //printf("m_serverpid %d\n", m_serverpid);
214 ASSERT_GT(m_serverpid, 0);
215
216 sp<IServiceManager> sm = defaultServiceManager();
217 //printf("%s: pid %d, get service\n", __func__, m_pid);
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +0000218#pragma clang diagnostic push
219#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Riley Andrews06b01ad2014-12-18 12:10:08 -0800220 m_server = sm->getService(binderLibTestServiceName);
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +0000221#pragma clang diagnostic pop
Yi Kong91635562018-06-07 14:38:36 -0700222 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800223 //printf("%s: pid %d, get service done\n", __func__, m_pid);
224 }
225 virtual void TearDown() {
226 status_t ret;
227 Parcel data, reply;
228 int exitStatus;
229 pid_t pid;
230
231 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700232 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800233 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
234 EXPECT_EQ(0, ret);
235 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
236 EXPECT_EQ(0, ret);
237 }
238 if (m_serverpid > 0) {
239 //printf("wait for %d\n", m_pids[i]);
240 pid = wait(&exitStatus);
241 EXPECT_EQ(m_serverpid, pid);
242 EXPECT_TRUE(WIFEXITED(exitStatus));
243 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
244 }
245 }
246
247 pid_t m_serverpid;
248 sp<IBinder> m_server;
249};
250
251class BinderLibTest : public ::testing::Test {
252 public:
253 virtual void SetUp() {
254 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000255 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800256 }
257 virtual void TearDown() {
258 }
259 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200260 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800261 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800262 int32_t id;
263 Parcel data, reply;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800264
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700265 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800266
Elie Kheirallahb7246642022-05-03 18:01:43 +0000267 sp<IBinder> binder = reply.readStrongBinder();
268 EXPECT_NE(nullptr, binder);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700269 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800270 if (idPtr)
271 *idPtr = id;
272 return binder;
273 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200274
Yi Kong91635562018-06-07 14:38:36 -0700275 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200276 {
277 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
278 }
279
Yi Kong91635562018-06-07 14:38:36 -0700280 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200281 {
282 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
283 }
284
Riley Andrews06b01ad2014-12-18 12:10:08 -0800285 void waitForReadData(int fd, int timeout_ms) {
286 int ret;
287 pollfd pfd = pollfd();
288
289 pfd.fd = fd;
290 pfd.events = POLLIN;
291 ret = poll(&pfd, 1, timeout_ms);
292 EXPECT_EQ(1, ret);
293 }
294
295 sp<IBinder> m_server;
296};
297
298class BinderLibTestBundle : public Parcel
299{
300 public:
301 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800302 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800303 int32_t mark;
304 int32_t bundleLen;
305 size_t pos;
306
307 if (source->readInt32(&mark))
308 return;
309 if (mark != MARK_START)
310 return;
311 if (source->readInt32(&bundleLen))
312 return;
313 pos = source->dataPosition();
314 if (Parcel::appendFrom(source, pos, bundleLen))
315 return;
316 source->setDataPosition(pos + bundleLen);
317 if (source->readInt32(&mark))
318 return;
319 if (mark != MARK_END)
320 return;
321 m_isValid = true;
322 setDataPosition(0);
323 }
324 void appendTo(Parcel *dest) {
325 dest->writeInt32(MARK_START);
326 dest->writeInt32(dataSize());
327 dest->appendFrom(this, 0, dataSize());
328 dest->writeInt32(MARK_END);
329 };
330 bool isValid(void) {
331 return m_isValid;
332 }
333 private:
334 enum {
335 MARK_START = B_PACK_CHARS('B','T','B','S'),
336 MARK_END = B_PACK_CHARS('B','T','B','E'),
337 };
338 bool m_isValid;
339};
340
341class BinderLibTestEvent
342{
343 public:
344 BinderLibTestEvent(void)
345 : m_eventTriggered(false)
346 {
Yi Kong91635562018-06-07 14:38:36 -0700347 pthread_mutex_init(&m_waitMutex, nullptr);
348 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800349 }
350 int waitEvent(int timeout_s)
351 {
352 int ret;
353 pthread_mutex_lock(&m_waitMutex);
354 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800355 struct timespec ts;
356 clock_gettime(CLOCK_REALTIME, &ts);
357 ts.tv_sec += timeout_s;
358 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800359 }
360 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
361 pthread_mutex_unlock(&m_waitMutex);
362 return ret;
363 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200364 pthread_t getTriggeringThread()
365 {
366 return m_triggeringThread;
367 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800368 protected:
369 void triggerEvent(void) {
370 pthread_mutex_lock(&m_waitMutex);
371 pthread_cond_signal(&m_waitCond);
372 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200373 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800374 pthread_mutex_unlock(&m_waitMutex);
375 };
376 private:
377 pthread_mutex_t m_waitMutex;
378 pthread_cond_t m_waitCond;
379 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200380 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800381};
382
383class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
384{
385 public:
386 BinderLibTestCallBack()
387 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700388 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800389 {
390 }
391 status_t getResult(void)
392 {
393 return m_result;
394 }
395
396 private:
397 virtual status_t onTransact(uint32_t code,
398 const Parcel& data, Parcel* reply,
399 uint32_t flags = 0)
400 {
401 (void)reply;
402 (void)flags;
403 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200404 case BINDER_LIB_TEST_CALL_BACK: {
405 status_t status = data.readInt32(&m_result);
406 if (status != NO_ERROR) {
407 m_result = status;
408 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800409 triggerEvent();
410 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200411 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700412 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
413 sp<IBinder> server;
414 int ret;
415 const uint8_t *buf = data.data();
416 size_t size = data.dataSize();
417 if (m_prev_end) {
418 /* 64-bit kernel needs at most 8 bytes to align buffer end */
419 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
420 } else {
421 EXPECT_TRUE(IsPageAligned((void *)buf));
422 }
423
424 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
425
426 if (size > 0) {
427 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
428 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
429 data, reply);
430 EXPECT_EQ(NO_ERROR, ret);
431 }
432 return NO_ERROR;
433 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800434 default:
435 return UNKNOWN_TRANSACTION;
436 }
437 }
438
439 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700440 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800441};
442
443class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
444{
445 private:
446 virtual void binderDied(const wp<IBinder>& who) {
447 (void)who;
448 triggerEvent();
449 };
450};
451
Steven Morelandbd98e0f2021-10-14 14:24:15 -0700452TEST_F(BinderLibTest, CannotUseBinderAfterFork) {
453 // EXPECT_DEATH works by forking the process
454 EXPECT_DEATH({ ProcessState::self(); }, "libbinder ProcessState can not be used after fork");
455}
456
Steven Moreland5c75a5a2022-05-11 22:15:10 +0000457TEST_F(BinderLibTest, AddManagerToManager) {
458 sp<IServiceManager> sm = defaultServiceManager();
459 sp<IBinder> binder = IInterface::asBinder(sm);
460 EXPECT_EQ(NO_ERROR, sm->addService(String16("binderLibTest-manager"), binder));
461}
462
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500463TEST_F(BinderLibTest, WasParceled) {
464 auto binder = sp<BBinder>::make();
465 EXPECT_FALSE(binder->wasParceled());
466 Parcel data;
467 data.writeStrongBinder(binder);
468 EXPECT_TRUE(binder->wasParceled());
469}
470
Riley Andrews06b01ad2014-12-18 12:10:08 -0800471TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800472 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700473 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
474 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800475}
476
Steven Moreland80844f72020-12-12 02:06:08 +0000477TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000478 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700479 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
480 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000481}
482
Steven Morelandf183fdd2020-10-27 00:12:12 +0000483TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000484 Parcel data, reply;
485 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700486 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
487 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000488}
489
Marco Ballesio7ee17572020-09-08 10:30:03 -0700490TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700491 Parcel data, reply, replypid;
Li Li6f059292021-09-10 09:59:30 -0700492 std::ifstream freezer_file("/sys/fs/cgroup/uid_0/cgroup.freeze");
Marco Ballesio7ee17572020-09-08 10:30:03 -0700493
Li Li6f059292021-09-10 09:59:30 -0700494 // Pass test on devices where the cgroup v2 freezer is not supported
Marco Ballesio7ee17572020-09-08 10:30:03 -0700495 if (freezer_file.fail()) {
496 GTEST_SKIP();
497 return;
498 }
499
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700500 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700501 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700502 for (int i = 0; i < 10; i++) {
503 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
504 }
Li Li6f059292021-09-10 09:59:30 -0700505
506 // Pass test on devices where BINDER_FREEZE ioctl is not supported
507 int ret = IPCThreadState::self()->freeze(pid, false, 0);
508 if (ret != 0) {
509 GTEST_SKIP();
510 return;
511 }
512
513 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
Steven Morelandee739eb2023-02-13 21:03:49 +0000514
515 // b/268232063 - succeeds ~0.08% of the time
516 {
517 auto ret = IPCThreadState::self()->freeze(pid, true, 0);
518 EXPECT_TRUE(ret == -EAGAIN || ret == OK);
519 }
520
Li Li6f059292021-09-10 09:59:30 -0700521 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, true, 1000));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700522 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700523
Li Li4e678b92021-09-14 12:14:42 -0700524 uint32_t sync_received, async_received;
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700525
526 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
527 &async_received));
528
529 EXPECT_EQ(sync_received, 1);
530 EXPECT_EQ(async_received, 0);
531
Li Li4e678b92021-09-14 12:14:42 -0700532 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, false, 0));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700533 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
534}
535
Riley Andrews06b01ad2014-12-18 12:10:08 -0800536TEST_F(BinderLibTest, SetError) {
537 int32_t testValue[] = { 0, -123, 123 };
538 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800539 Parcel data, reply;
540 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700541 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
542 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800543 }
544}
545
546TEST_F(BinderLibTest, GetId) {
Yifan Hong28d6c352021-06-04 17:27:35 -0700547 EXPECT_THAT(GetId(m_server), HasValue(0));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800548}
549
550TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800551 int32_t ptrsize;
552 Parcel data, reply;
553 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700554 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700555 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
556 StatusEq(NO_ERROR));
557 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800558 RecordProperty("TestPtrSize", sizeof(void *));
559 RecordProperty("ServerPtrSize", sizeof(void *));
560}
561
562TEST_F(BinderLibTest, IndirectGetId2)
563{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800564 int32_t id;
565 int32_t count;
566 Parcel data, reply;
567 int32_t serverId[3];
568
569 data.writeInt32(ARRAY_SIZE(serverId));
570 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
571 sp<IBinder> server;
572 BinderLibTestBundle datai;
573
574 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700575 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800576 data.writeStrongBinder(server);
577 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
578 datai.appendTo(&data);
579 }
580
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700581 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
582 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800583
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700584 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800585 EXPECT_EQ(0, id);
586
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700587 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700588 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800589
590 for (size_t i = 0; i < (size_t)count; i++) {
591 BinderLibTestBundle replyi(&reply);
592 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700593 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800594 EXPECT_EQ(serverId[i], id);
595 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
596 }
597
598 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
599}
600
601TEST_F(BinderLibTest, IndirectGetId3)
602{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800603 int32_t id;
604 int32_t count;
605 Parcel data, reply;
606 int32_t serverId[3];
607
608 data.writeInt32(ARRAY_SIZE(serverId));
609 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
610 sp<IBinder> server;
611 BinderLibTestBundle datai;
612 BinderLibTestBundle datai2;
613
614 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700615 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800616 data.writeStrongBinder(server);
617 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
618
619 datai.writeInt32(1);
620 datai.writeStrongBinder(m_server);
621 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
622 datai2.appendTo(&datai);
623
624 datai.appendTo(&data);
625 }
626
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700627 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
628 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800629
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700630 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631 EXPECT_EQ(0, id);
632
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700633 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700634 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800635
636 for (size_t i = 0; i < (size_t)count; i++) {
637 int32_t counti;
638
639 BinderLibTestBundle replyi(&reply);
640 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700641 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800642 EXPECT_EQ(serverId[i], id);
643
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700644 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800645 EXPECT_EQ(1, counti);
646
647 BinderLibTestBundle replyi2(&replyi);
648 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700649 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800650 EXPECT_EQ(0, id);
651 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
652
653 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
654 }
655
656 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
657}
658
659TEST_F(BinderLibTest, CallBack)
660{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800661 Parcel data, reply;
662 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
663 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700664 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
665 StatusEq(NO_ERROR));
666 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
667 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800668}
669
Steven Moreland35626652021-05-15 01:32:04 +0000670TEST_F(BinderLibTest, BinderCallContextGuard) {
671 sp<IBinder> binder = addServer();
672 Parcel data, reply;
673 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
674 StatusEq(DEAD_OBJECT));
675}
676
Riley Andrews06b01ad2014-12-18 12:10:08 -0800677TEST_F(BinderLibTest, AddServer)
678{
679 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700680 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800681}
682
Riley Andrews06b01ad2014-12-18 12:10:08 -0800683TEST_F(BinderLibTest, DeathNotificationStrongRef)
684{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685 sp<IBinder> sbinder;
686
687 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
688
689 {
690 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700691 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700692 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800693 sbinder = binder;
694 }
695 {
696 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700697 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
698 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800699 }
700 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700701 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
702 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800703}
704
705TEST_F(BinderLibTest, DeathNotificationMultiple)
706{
707 status_t ret;
708 const int clientcount = 2;
709 sp<IBinder> target;
710 sp<IBinder> linkedclient[clientcount];
711 sp<BinderLibTestCallBack> callBack[clientcount];
712 sp<IBinder> passiveclient[clientcount];
713
714 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700715 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800716 for (int i = 0; i < clientcount; i++) {
717 {
718 Parcel data, reply;
719
720 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700721 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800722 callBack[i] = new BinderLibTestCallBack();
723 data.writeStrongBinder(target);
724 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700725 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
726 &reply, TF_ONE_WAY),
727 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800728 }
729 {
730 Parcel data, reply;
731
732 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700733 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800734 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700735 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
736 &reply, TF_ONE_WAY),
737 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800738 }
739 }
740 {
741 Parcel data, reply;
742 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
743 EXPECT_EQ(0, ret);
744 }
745
746 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700747 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
748 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800749 }
750}
751
Martijn Coenenf7100e42017-07-31 12:14:09 +0200752TEST_F(BinderLibTest, DeathNotificationThread)
753{
754 status_t ret;
755 sp<BinderLibTestCallBack> callback;
756 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700757 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200758 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700759 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200760
761 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
762
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700763 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200764
765 {
766 Parcel data, reply;
767 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
768 EXPECT_EQ(0, ret);
769 }
770
771 /* Make sure it's dead */
772 testDeathRecipient->waitEvent(5);
773
774 /* Now, pass the ref to another process and ask that process to
775 * call linkToDeath() on it, and wait for a response. This tests
776 * two things:
777 * 1) You still get death notifications when calling linkToDeath()
778 * on a ref that is already dead when it was passed to you.
779 * 2) That death notifications are not directly pushed to the thread
780 * registering them, but to the threadpool (proc workqueue) instead.
781 *
782 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
783 * is blocked on a condition variable waiting for the death notification to be
784 * called; therefore, that thread is not available for handling proc work.
785 * So, if the death notification was pushed to the thread workqueue, the callback
786 * would never be called, and the test would timeout and fail.
787 *
788 * Note that we can't do this part of the test from this thread itself, because
789 * the binder driver would only push death notifications to the thread if
790 * it is a looper thread, which this thread is not.
791 *
792 * See b/23525545 for details.
793 */
794 {
795 Parcel data, reply;
796
797 callback = new BinderLibTestCallBack();
798 data.writeStrongBinder(target);
799 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700800 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
801 TF_ONE_WAY),
802 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200803 }
804
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700805 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
806 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200807}
808
Riley Andrews06b01ad2014-12-18 12:10:08 -0800809TEST_F(BinderLibTest, PassFile) {
810 int ret;
811 int pipefd[2];
812 uint8_t buf[1] = { 0 };
813 uint8_t write_value = 123;
814
815 ret = pipe2(pipefd, O_NONBLOCK);
816 ASSERT_EQ(0, ret);
817
818 {
819 Parcel data, reply;
820 uint8_t writebuf[1] = { write_value };
821
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700822 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800823
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700824 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800825
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700826 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800827
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700828 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
829 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800830 }
831
832 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700833 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800834 EXPECT_EQ(write_value, buf[0]);
835
836 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
837
838 ret = read(pipefd[0], buf, sizeof(buf));
839 EXPECT_EQ(0, ret);
840
841 close(pipefd[0]);
842}
843
Ryo Hashimotobf551892018-05-31 16:58:35 +0900844TEST_F(BinderLibTest, PassParcelFileDescriptor) {
845 const int datasize = 123;
846 std::vector<uint8_t> writebuf(datasize);
847 for (size_t i = 0; i < writebuf.size(); ++i) {
848 writebuf[i] = i;
849 }
850
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700851 unique_fd read_end, write_end;
Ryo Hashimotobf551892018-05-31 16:58:35 +0900852 {
853 int pipefd[2];
854 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
855 read_end.reset(pipefd[0]);
856 write_end.reset(pipefd[1]);
857 }
858 {
859 Parcel data;
860 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
861 write_end.reset();
862 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
863 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
864
865 Parcel reply;
866 EXPECT_EQ(NO_ERROR,
867 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
868 &reply));
869 }
870 std::vector<uint8_t> readbuf(datasize);
871 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
872 EXPECT_EQ(writebuf, readbuf);
873
874 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
875
876 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
877}
878
Riley Andrews06b01ad2014-12-18 12:10:08 -0800879TEST_F(BinderLibTest, PromoteLocal) {
880 sp<IBinder> strong = new BBinder();
881 wp<IBinder> weak = strong;
882 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700883 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800884 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700885 strong = nullptr;
886 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800887 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700888 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800889}
890
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700891TEST_F(BinderLibTest, LocalGetExtension) {
892 sp<BBinder> binder = new BBinder();
893 sp<IBinder> ext = new BBinder();
894 binder->setExtension(ext);
895 EXPECT_EQ(ext, binder->getExtension());
896}
897
898TEST_F(BinderLibTest, RemoteGetExtension) {
899 sp<IBinder> server = addServer();
900 ASSERT_TRUE(server != nullptr);
901
902 sp<IBinder> extension;
903 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
904 ASSERT_NE(nullptr, extension.get());
905
906 EXPECT_EQ(NO_ERROR, extension->pingBinder());
907}
908
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700909TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700910 Parcel data, reply;
911
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700912 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
913 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700914
915 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700916 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800917 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
918 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
919 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
920 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700921}
922
Connor O'Brien52be2c92016-09-20 14:18:08 -0700923TEST_F(BinderLibTest, FreedBinder) {
924 status_t ret;
925
926 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700927 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700928
929 __u32 freedHandle;
930 wp<IBinder> keepFreedBinder;
931 {
932 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700933 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
934 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700935 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
936 freedHandle = freed->handle;
937 /* Add a weak ref to the freed binder so the driver does not
938 * delete its reference to it - otherwise the transaction
939 * fails regardless of whether the driver is fixed.
940 */
Steven Morelande171d622019-07-17 16:06:01 -0700941 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700942 }
Steven Morelande171d622019-07-17 16:06:01 -0700943 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700944 {
945 Parcel data, reply;
946 data.writeStrongBinder(server);
947 /* Replace original handle with handle to the freed binder */
948 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
949 __u32 oldHandle = strong->handle;
950 strong->handle = freedHandle;
951 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
952 /* Returns DEAD_OBJECT (-32) if target crashes and
953 * FAILED_TRANSACTION if the driver rejects the invalid
954 * object.
955 */
956 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
957 /* Restore original handle so parcel destructor does not use
958 * the wrong handle.
959 */
960 strong->handle = oldHandle;
961 }
962}
963
Sherry Yang336cdd32017-07-24 14:12:27 -0700964TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700965 Parcel data, reply;
966 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
967 for (int i = 0; i < 2; i++) {
968 BinderLibTestBundle datai;
969 datai.appendFrom(&data, 0, data.dataSize());
970
971 data.freeData();
972 data.writeInt32(1);
973 data.writeStrongBinder(callBack);
974 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
975
976 datai.appendTo(&data);
977 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700978 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
979 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700980}
981
Martijn Coenen45b07b42017-08-09 12:07:45 +0200982TEST_F(BinderLibTest, OnewayQueueing)
983{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200984 Parcel data, data2;
985
986 sp<IBinder> pollServer = addPollServer();
987
988 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
989 data.writeStrongBinder(callBack);
990 data.writeInt32(500000); // delay in us before calling back
991
992 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
993 data2.writeStrongBinder(callBack2);
994 data2.writeInt32(0); // delay in us
995
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700996 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
997 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200998
999 // The delay ensures that this second transaction will end up on the async_todo list
1000 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001001 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
1002 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +02001003
1004 // The server will ensure that the two transactions are handled in the expected order;
1005 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001006 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
1007 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +02001008
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001009 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
1010 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +02001011}
1012
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001013TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
1014{
1015 status_t ret;
1016 Parcel data, reply;
1017 data.writeInterfaceToken(binderLibTestServiceName);
1018 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1019 EXPECT_EQ(-1, reply.readInt32());
1020 EXPECT_EQ(NO_ERROR, ret);
1021}
1022
1023TEST_F(BinderLibTest, WorkSourceSet)
1024{
1025 status_t ret;
1026 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001027 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001028 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001029 data.writeInterfaceToken(binderLibTestServiceName);
1030 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1031 EXPECT_EQ(100, reply.readInt32());
1032 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001033 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1034 EXPECT_EQ(NO_ERROR, ret);
1035}
1036
1037TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1038{
1039 status_t ret;
1040 Parcel data, reply;
1041
1042 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1043 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1044
1045 data.writeInterfaceToken(binderLibTestServiceName);
1046 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1047 EXPECT_EQ(-1, reply.readInt32());
1048 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001049 EXPECT_EQ(NO_ERROR, ret);
1050}
1051
1052TEST_F(BinderLibTest, WorkSourceCleared)
1053{
1054 status_t ret;
1055 Parcel data, reply;
1056
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001057 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001058 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1059 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001060 data.writeInterfaceToken(binderLibTestServiceName);
1061 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1062
1063 EXPECT_EQ(-1, reply.readInt32());
1064 EXPECT_EQ(100, previousWorkSource);
1065 EXPECT_EQ(NO_ERROR, ret);
1066}
1067
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001068TEST_F(BinderLibTest, WorkSourceRestored)
1069{
1070 status_t ret;
1071 Parcel data, reply;
1072
1073 IPCThreadState::self()->setCallingWorkSourceUid(100);
1074 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1075 IPCThreadState::self()->restoreCallingWorkSource(token);
1076
1077 data.writeInterfaceToken(binderLibTestServiceName);
1078 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1079
1080 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001081 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001082 EXPECT_EQ(NO_ERROR, ret);
1083}
1084
Olivier Gaillard91a04802018-11-14 17:32:41 +00001085TEST_F(BinderLibTest, PropagateFlagSet)
1086{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001087 IPCThreadState::self()->clearPropagateWorkSource();
1088 IPCThreadState::self()->setCallingWorkSourceUid(100);
1089 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1090}
1091
1092TEST_F(BinderLibTest, PropagateFlagCleared)
1093{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001094 IPCThreadState::self()->setCallingWorkSourceUid(100);
1095 IPCThreadState::self()->clearPropagateWorkSource();
1096 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1097}
1098
1099TEST_F(BinderLibTest, PropagateFlagRestored)
1100{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001101 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1102 IPCThreadState::self()->restoreCallingWorkSource(token);
1103
1104 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1105}
1106
1107TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1108{
1109 IPCThreadState::self()->setCallingWorkSourceUid(100);
1110
1111 Parcel data, reply;
1112 status_t ret;
1113 data.writeInterfaceToken(binderLibTestServiceName);
1114 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +00001115 EXPECT_EQ(NO_ERROR, ret);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001116
1117 Parcel data2, reply2;
1118 status_t ret2;
1119 data2.writeInterfaceToken(binderLibTestServiceName);
1120 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1121 EXPECT_EQ(100, reply2.readInt32());
1122 EXPECT_EQ(NO_ERROR, ret2);
1123}
1124
Steven Morelandbf1915b2020-07-16 22:43:02 +00001125TEST_F(BinderLibTest, SchedPolicySet) {
1126 sp<IBinder> server = addServer();
1127 ASSERT_TRUE(server != nullptr);
1128
1129 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001130 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1131 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001132
1133 int policy = reply.readInt32();
1134 int priority = reply.readInt32();
1135
1136 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1137 EXPECT_EQ(kSchedPriority, priority);
1138}
1139
Steven Morelandcf03cf12020-12-04 02:58:40 +00001140TEST_F(BinderLibTest, InheritRt) {
1141 sp<IBinder> server = addServer();
1142 ASSERT_TRUE(server != nullptr);
1143
1144 const struct sched_param param {
1145 .sched_priority = kSchedPriorityMore,
1146 };
1147 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1148
1149 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001150 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1151 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001152
1153 int policy = reply.readInt32();
1154 int priority = reply.readInt32();
1155
1156 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1157 EXPECT_EQ(kSchedPriorityMore, priority);
1158}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001159
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001160TEST_F(BinderLibTest, VectorSent) {
1161 Parcel data, reply;
1162 sp<IBinder> server = addServer();
1163 ASSERT_TRUE(server != nullptr);
1164
1165 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1166 data.writeUint64Vector(testValue);
1167
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001168 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001169 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001170 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001171 EXPECT_EQ(readValue, testValue);
1172}
1173
Siarhei Vishniakou116f6b82022-10-03 13:43:15 -07001174TEST_F(BinderLibTest, FileDescriptorRemainsNonBlocking) {
1175 sp<IBinder> server = addServer();
1176 ASSERT_TRUE(server != nullptr);
1177
1178 Parcel reply;
1179 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_NON_BLOCKING_FD, {} /*data*/, &reply),
1180 StatusEq(NO_ERROR));
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001181 unique_fd fd;
Siarhei Vishniakou116f6b82022-10-03 13:43:15 -07001182 EXPECT_THAT(reply.readUniqueFileDescriptor(&fd), StatusEq(OK));
1183
1184 const int result = fcntl(fd.get(), F_GETFL);
1185 ASSERT_NE(result, -1);
1186 EXPECT_EQ(result & O_NONBLOCK, O_NONBLOCK);
1187}
1188
Steven Moreland59b84442022-07-12 18:32:44 +00001189// see ProcessState.cpp BINDER_VM_SIZE = 1MB.
1190// This value is not exposed, but some code in the framework relies on being able to use
1191// buffers near the cap size.
Steven Morelandce15b9f2022-09-08 17:42:45 +00001192constexpr size_t kSizeBytesAlmostFull = 950'000;
Steven Moreland59b84442022-07-12 18:32:44 +00001193constexpr size_t kSizeBytesOverFull = 1'050'000;
1194
1195TEST_F(BinderLibTest, GargantuanVectorSent) {
1196 sp<IBinder> server = addServer();
1197 ASSERT_TRUE(server != nullptr);
1198
1199 for (size_t i = 0; i < 10; i++) {
1200 // a slight variation in size is used to consider certain possible caching implementations
1201 const std::vector<uint64_t> testValue((kSizeBytesAlmostFull + i) / sizeof(uint64_t), 42);
1202
1203 Parcel data, reply;
1204 data.writeUint64Vector(testValue);
1205 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR))
1206 << i;
1207 std::vector<uint64_t> readValue;
1208 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
1209 EXPECT_EQ(readValue, testValue);
1210 }
1211}
1212
1213TEST_F(BinderLibTest, LimitExceededVectorSent) {
1214 sp<IBinder> server = addServer();
1215 ASSERT_TRUE(server != nullptr);
1216 const std::vector<uint64_t> testValue(kSizeBytesOverFull / sizeof(uint64_t), 42);
1217
1218 Parcel data, reply;
1219 data.writeUint64Vector(testValue);
1220 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply),
1221 StatusEq(FAILED_TRANSACTION));
1222}
1223
Martijn Coenen82c75312019-07-24 15:18:30 +02001224TEST_F(BinderLibTest, BufRejected) {
1225 Parcel data, reply;
1226 uint32_t buf;
1227 sp<IBinder> server = addServer();
1228 ASSERT_TRUE(server != nullptr);
1229
1230 binder_buffer_object obj {
1231 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001232 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001233 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1234 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001235 };
1236 data.setDataCapacity(1024);
1237 // Write a bogus object at offset 0 to get an entry in the offset table
1238 data.writeFileDescriptor(0);
1239 EXPECT_EQ(data.objectsCount(), 1);
1240 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1241 // And now, overwrite it with the buffer object
1242 memcpy(parcelData, &obj, sizeof(obj));
1243 data.setDataSize(sizeof(obj));
1244
Steven Morelandf2e0a952021-11-01 18:17:23 -07001245 EXPECT_EQ(data.objectsCount(), 1);
1246
Martijn Coenen82c75312019-07-24 15:18:30 +02001247 // Either the kernel should reject this transaction (if it's correct), but
1248 // if it's not, the server implementation should return an error if it
1249 // finds an object in the received Parcel.
Steven Morelandf2e0a952021-11-01 18:17:23 -07001250 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001251 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001252}
1253
Steven Morelandf2e0a952021-11-01 18:17:23 -07001254TEST_F(BinderLibTest, WeakRejected) {
1255 Parcel data, reply;
1256 sp<IBinder> server = addServer();
1257 ASSERT_TRUE(server != nullptr);
1258
1259 auto binder = sp<BBinder>::make();
1260 wp<BBinder> wpBinder(binder);
1261 flat_binder_object obj{
1262 .hdr = {.type = BINDER_TYPE_WEAK_BINDER},
1263 .flags = 0,
1264 .binder = reinterpret_cast<uintptr_t>(wpBinder.get_refs()),
1265 .cookie = reinterpret_cast<uintptr_t>(wpBinder.unsafe_get()),
1266 };
1267 data.setDataCapacity(1024);
1268 // Write a bogus object at offset 0 to get an entry in the offset table
1269 data.writeFileDescriptor(0);
1270 EXPECT_EQ(data.objectsCount(), 1);
1271 uint8_t *parcelData = const_cast<uint8_t *>(data.data());
1272 // And now, overwrite it with the weak binder
1273 memcpy(parcelData, &obj, sizeof(obj));
1274 data.setDataSize(sizeof(obj));
1275
1276 // a previous bug caused other objects to be released an extra time, so we
1277 // test with an object that libbinder will actually try to release
1278 EXPECT_EQ(OK, data.writeStrongBinder(sp<BBinder>::make()));
1279
1280 EXPECT_EQ(data.objectsCount(), 2);
1281
1282 // send it many times, since previous error was memory corruption, make it
1283 // more likely that the server crashes
1284 for (size_t i = 0; i < 100; i++) {
1285 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
1286 StatusEq(BAD_VALUE));
1287 }
1288
1289 EXPECT_THAT(server->pingBinder(), StatusEq(NO_ERROR));
1290}
1291
Steven Moreland254e8ef2021-04-19 22:28:50 +00001292TEST_F(BinderLibTest, GotSid) {
1293 sp<IBinder> server = addServer();
1294
1295 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001296 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001297}
1298
Andrei Homescu1519b982022-06-09 02:04:44 +00001299struct TooManyFdsFlattenable : Flattenable<TooManyFdsFlattenable> {
1300 TooManyFdsFlattenable(size_t fdCount) : mFdCount(fdCount) {}
1301
1302 // Flattenable protocol
1303 size_t getFlattenedSize() const {
1304 // Return a valid non-zero size here so we don't get an unintended
1305 // BAD_VALUE from Parcel::write
1306 return 16;
1307 }
1308 size_t getFdCount() const { return mFdCount; }
1309 status_t flatten(void *& /*buffer*/, size_t & /*size*/, int *&fds, size_t &count) const {
1310 for (size_t i = 0; i < count; i++) {
1311 fds[i] = STDIN_FILENO;
1312 }
1313 return NO_ERROR;
1314 }
1315 status_t unflatten(void const *& /*buffer*/, size_t & /*size*/, int const *& /*fds*/,
1316 size_t & /*count*/) {
1317 /* This doesn't get called */
1318 return NO_ERROR;
1319 }
1320
1321 size_t mFdCount;
1322};
1323
1324TEST_F(BinderLibTest, TooManyFdsFlattenable) {
1325 rlimit origNofile;
1326 int ret = getrlimit(RLIMIT_NOFILE, &origNofile);
1327 ASSERT_EQ(0, ret);
1328
1329 // Restore the original file limits when the test finishes
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +00001330 auto guardUnguard = make_scope_guard([&]() { setrlimit(RLIMIT_NOFILE, &origNofile); });
Andrei Homescu1519b982022-06-09 02:04:44 +00001331
1332 rlimit testNofile = {1024, 1024};
1333 ret = setrlimit(RLIMIT_NOFILE, &testNofile);
1334 ASSERT_EQ(0, ret);
1335
1336 Parcel parcel;
1337 // Try to write more file descriptors than supported by the OS
1338 TooManyFdsFlattenable tooManyFds1(1024);
1339 EXPECT_THAT(parcel.write(tooManyFds1), StatusEq(-EMFILE));
1340
1341 // Try to write more file descriptors than the internal limit
1342 TooManyFdsFlattenable tooManyFds2(1025);
1343 EXPECT_THAT(parcel.write(tooManyFds2), StatusEq(BAD_VALUE));
1344}
1345
Jayant Chowdhary30700942022-01-31 14:12:40 -08001346TEST(ServiceNotifications, Unregister) {
1347 auto sm = defaultServiceManager();
1348 using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
1349 class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
1350 void onServiceRegistration(const String16 &, const sp<IBinder> &) override {}
1351 virtual ~LocalRegistrationCallbackImpl() {}
1352 };
1353 sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
1354
1355 EXPECT_EQ(sm->registerForNotifications(String16("RogerRafa"), cb), OK);
1356 EXPECT_EQ(sm->unregisterForNotifications(String16("RogerRafa"), cb), OK);
1357}
1358
Elie Kheirallah47431c12022-04-21 23:46:17 +00001359TEST_F(BinderLibTest, ThreadPoolAvailableThreads) {
1360 Parcel data, reply;
1361 sp<IBinder> server = addServer();
1362 ASSERT_TRUE(server != nullptr);
1363 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_MAX_THREAD_COUNT, data, &reply),
1364 StatusEq(NO_ERROR));
1365 int32_t replyi = reply.readInt32();
Steven Moreland3e9debc2023-06-15 00:35:29 +00001366 // see getThreadPoolMaxTotalThreadCount for why there is a race
1367 EXPECT_TRUE(replyi == kKernelThreads + 1 || replyi == kKernelThreads + 2) << replyi;
1368
Elie Kheirallah47431c12022-04-21 23:46:17 +00001369 EXPECT_THAT(server->transact(BINDER_LIB_TEST_PROCESS_LOCK, data, &reply), NO_ERROR);
1370
1371 /*
Steven Moreland3e9debc2023-06-15 00:35:29 +00001372 * This will use all threads in the pool but one. There are actually kKernelThreads+2
1373 * available in the other process (startThreadPool, joinThreadPool, + the kernel-
1374 * started threads from setThreadPoolMaxThreadCount
1375 *
1376 * Adding one more will cause it to deadlock.
Elie Kheirallah47431c12022-04-21 23:46:17 +00001377 */
1378 std::vector<std::thread> ts;
Steven Moreland3e9debc2023-06-15 00:35:29 +00001379 for (size_t i = 0; i < kKernelThreads + 1; i++) {
Elie Kheirallah47431c12022-04-21 23:46:17 +00001380 ts.push_back(std::thread([&] {
Elie Kheirallah59f60fd2022-06-09 23:59:04 +00001381 Parcel local_reply;
1382 EXPECT_THAT(server->transact(BINDER_LIB_TEST_LOCK_UNLOCK, data, &local_reply),
1383 NO_ERROR);
Elie Kheirallah47431c12022-04-21 23:46:17 +00001384 }));
1385 }
1386
Steven Moreland3e9debc2023-06-15 00:35:29 +00001387 // make sure all of the above calls will be queued in parallel. Otherwise, most of
1388 // the time, the below call will pre-empt them (presumably because we have the
1389 // scheduler timeslice already + scheduler hint).
1390 sleep(1);
1391
1392 data.writeInt32(1000);
1393 // Give a chance for all threads to be used (kKernelThreads + 1 thread in use)
Elie Kheirallah47431c12022-04-21 23:46:17 +00001394 EXPECT_THAT(server->transact(BINDER_LIB_TEST_UNLOCK_AFTER_MS, data, &reply), NO_ERROR);
1395
1396 for (auto &t : ts) {
1397 t.join();
1398 }
1399
1400 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_MAX_THREAD_COUNT, data, &reply),
1401 StatusEq(NO_ERROR));
1402 replyi = reply.readInt32();
Steven Moreland3e9debc2023-06-15 00:35:29 +00001403 EXPECT_EQ(replyi, kKernelThreads + 2);
Elie Kheirallah47431c12022-04-21 23:46:17 +00001404}
1405
Devin Moore4354f712022-12-08 01:44:46 +00001406TEST_F(BinderLibTest, ThreadPoolStarted) {
1407 Parcel data, reply;
1408 sp<IBinder> server = addServer();
1409 ASSERT_TRUE(server != nullptr);
1410 EXPECT_THAT(server->transact(BINDER_LIB_TEST_IS_THREADPOOL_STARTED, data, &reply), NO_ERROR);
1411 EXPECT_TRUE(reply.readBool());
1412}
1413
Elie Kheirallah47431c12022-04-21 23:46:17 +00001414size_t epochMillis() {
1415 using std::chrono::duration_cast;
1416 using std::chrono::milliseconds;
1417 using std::chrono::seconds;
1418 using std::chrono::system_clock;
1419 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
1420}
1421
1422TEST_F(BinderLibTest, HangingServices) {
1423 Parcel data, reply;
1424 sp<IBinder> server = addServer();
1425 ASSERT_TRUE(server != nullptr);
1426 int32_t delay = 1000; // ms
1427 data.writeInt32(delay);
Steven Moreland436a1102023-01-24 21:48:11 +00001428 // b/266537959 - must take before taking lock, since countdown is started in the remote
1429 // process there.
1430 size_t epochMsBefore = epochMillis();
Elie Kheirallah47431c12022-04-21 23:46:17 +00001431 EXPECT_THAT(server->transact(BINDER_LIB_TEST_PROCESS_TEMPORARY_LOCK, data, &reply), NO_ERROR);
1432 std::vector<std::thread> ts;
Elie Kheirallah47431c12022-04-21 23:46:17 +00001433 for (size_t i = 0; i < kKernelThreads + 1; i++) {
1434 ts.push_back(std::thread([&] {
Elie Kheirallah59f60fd2022-06-09 23:59:04 +00001435 Parcel local_reply;
1436 EXPECT_THAT(server->transact(BINDER_LIB_TEST_LOCK_UNLOCK, data, &local_reply),
1437 NO_ERROR);
Elie Kheirallah47431c12022-04-21 23:46:17 +00001438 }));
1439 }
1440
1441 for (auto &t : ts) {
1442 t.join();
1443 }
1444 size_t epochMsAfter = epochMillis();
1445
1446 // deadlock occurred and threads only finished after 1s passed.
1447 EXPECT_GE(epochMsAfter, epochMsBefore + delay);
1448}
1449
Jing Jibbe9ae62023-10-07 15:26:02 -07001450TEST_F(BinderLibTest, BinderProxyCount) {
1451 Parcel data, reply;
1452 sp<IBinder> server = addServer();
1453 ASSERT_NE(server, nullptr);
1454
1455 uint32_t initialCount = BpBinder::getBinderProxyCount();
1456 size_t iterations = 100;
1457 {
1458 uint32_t count = initialCount;
1459 std::vector<sp<IBinder> > proxies;
1460 sp<IBinder> proxy;
1461 // Create binder proxies and verify the count.
1462 for (size_t i = 0; i < iterations; i++) {
1463 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
1464 StatusEq(NO_ERROR));
1465 proxies.push_back(reply.readStrongBinder());
1466 EXPECT_EQ(BpBinder::getBinderProxyCount(), ++count);
1467 }
1468 // Remove every other one and verify the count.
1469 auto it = proxies.begin();
1470 for (size_t i = 0; it != proxies.end(); i++) {
1471 if (i % 2 == 0) {
1472 it = proxies.erase(it);
1473 EXPECT_EQ(BpBinder::getBinderProxyCount(), --count);
1474 }
1475 }
1476 }
1477 EXPECT_EQ(BpBinder::getBinderProxyCount(), initialCount);
1478}
1479
Yifan Hong84bedeb2021-04-21 21:37:17 -07001480class BinderLibRpcTestBase : public BinderLibTest {
1481public:
1482 void SetUp() override {
1483 if (!base::GetBoolProperty("ro.debuggable", false)) {
1484 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1485 "non-debuggable builds.";
1486 }
1487 BinderLibTest::SetUp();
1488 }
1489
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001490 std::tuple<unique_fd, unsigned int> CreateSocket() {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001491 auto rpcServer = RpcServer::make();
1492 EXPECT_NE(nullptr, rpcServer);
1493 if (rpcServer == nullptr) return {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001494 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001495 if (status_t status = rpcServer->setupInetServer("127.0.0.1", 0, &port); status != OK) {
1496 ADD_FAILURE() << "setupInetServer failed" << statusToString(status);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001497 return {};
1498 }
1499 return {rpcServer->releaseServer(), port};
1500 }
1501};
1502
Yifan Hong8b890852021-06-10 13:44:09 -07001503class BinderLibRpcTest : public BinderLibRpcTestBase {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001504
Yifan Hongbd276552022-02-28 15:28:51 -08001505// e.g. EXPECT_THAT(expr, Debuggable(StatusEq(...))
1506// If device is debuggable AND not on user builds, expects matcher.
1507// Otherwise expects INVALID_OPERATION.
1508// Debuggable + non user builds is necessary but not sufficient for setRpcClientDebug to work.
1509static Matcher<status_t> Debuggable(const Matcher<status_t> &matcher) {
1510 bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false) &&
1511 android::base::GetProperty("ro.build.type", "") != "user";
1512 return isDebuggable ? matcher : StatusEq(INVALID_OPERATION);
1513}
1514
Yifan Hong8b890852021-06-10 13:44:09 -07001515TEST_F(BinderLibRpcTest, SetRpcClientDebug) {
1516 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001517 ASSERT_TRUE(binder != nullptr);
1518 auto [socket, port] = CreateSocket();
1519 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001520 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), sp<BBinder>::make()),
1521 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001522}
1523
Yifan Hong8b890852021-06-10 13:44:09 -07001524// Tests for multiple RpcServer's on the same binder object.
1525TEST_F(BinderLibRpcTest, SetRpcClientDebugTwice) {
1526 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001527 ASSERT_TRUE(binder != nullptr);
1528
1529 auto [socket1, port1] = CreateSocket();
1530 ASSERT_TRUE(socket1.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001531 auto keepAliveBinder1 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001532 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), keepAliveBinder1),
1533 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001534
1535 auto [socket2, port2] = CreateSocket();
1536 ASSERT_TRUE(socket2.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001537 auto keepAliveBinder2 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001538 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), keepAliveBinder2),
1539 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001540}
1541
Yifan Hong8b890852021-06-10 13:44:09 -07001542// Negative tests for RPC APIs on IBinder. Call should fail in the same way on both remote and
1543// local binders.
1544class BinderLibRpcTestP : public BinderLibRpcTestBase, public WithParamInterface<bool> {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001545public:
1546 sp<IBinder> GetService() {
1547 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1548 }
1549 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1550 return info.param ? "remote" : "local";
1551 }
1552};
1553
Yifan Hong8b890852021-06-10 13:44:09 -07001554TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoFd) {
1555 auto binder = GetService();
1556 ASSERT_TRUE(binder != nullptr);
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001557 EXPECT_THAT(binder->setRpcClientDebug(unique_fd(), sp<BBinder>::make()),
Yifan Hongbd276552022-02-28 15:28:51 -08001558 Debuggable(StatusEq(BAD_VALUE)));
Yifan Hong8b890852021-06-10 13:44:09 -07001559}
1560
1561TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoKeepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001562 auto binder = GetService();
1563 ASSERT_TRUE(binder != nullptr);
1564 auto [socket, port] = CreateSocket();
1565 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001566 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), nullptr),
1567 Debuggable(StatusEq(UNEXPECTED_NULL)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001568}
Yifan Hong8b890852021-06-10 13:44:09 -07001569INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTestP, testing::Bool(),
1570 BinderLibRpcTestP::ParamToString);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001571
Yifan Hong543edcd2021-05-18 19:47:30 -07001572class BinderLibTestService : public BBinder {
1573public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001574 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1575 : m_id(id),
1576 m_nextServerId(id + 1),
1577 m_serverStartRequested(false),
1578 m_callback(nullptr),
1579 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001580 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1581 pthread_cond_init(&m_serverWaitCond, nullptr);
1582 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001583 ~BinderLibTestService() {
1584 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1585 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001586
Yifan Hong543edcd2021-05-18 19:47:30 -07001587 void processPendingCall() {
1588 if (m_callback != nullptr) {
1589 Parcel data;
1590 data.writeInt32(NO_ERROR);
1591 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1592 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001593 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001594 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001595
Yifan Hong543edcd2021-05-18 19:47:30 -07001596 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1597 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001598 // TODO(b/182914638): also checks getCallingUid() for RPC
1599 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001600 return PERMISSION_DENIED;
1601 }
1602 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001603 case BINDER_LIB_TEST_REGISTER_SERVER: {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001604 sp<IBinder> binder;
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +00001605 /*id =*/data.readInt32();
Riley Andrews06b01ad2014-12-18 12:10:08 -08001606 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001607 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001608 return BAD_VALUE;
1609 }
1610
Yifan Hong543edcd2021-05-18 19:47:30 -07001611 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001612
1613 pthread_mutex_lock(&m_serverWaitMutex);
1614 if (m_serverStartRequested) {
1615 m_serverStartRequested = false;
1616 m_serverStarted = binder;
1617 pthread_cond_signal(&m_serverWaitCond);
1618 }
1619 pthread_mutex_unlock(&m_serverWaitMutex);
1620 return NO_ERROR;
1621 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001622 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001623 case BINDER_LIB_TEST_ADD_SERVER: {
1624 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001625 int serverid;
1626
1627 if (m_id != 0) {
1628 return INVALID_OPERATION;
1629 }
1630 pthread_mutex_lock(&m_serverWaitMutex);
1631 if (m_serverStartRequested) {
1632 ret = -EBUSY;
1633 } else {
1634 serverid = m_nextServerId++;
1635 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001636 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001637
1638 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001639 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001640 pthread_mutex_lock(&m_serverWaitMutex);
1641 }
1642 if (ret > 0) {
1643 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001644 struct timespec ts;
1645 clock_gettime(CLOCK_REALTIME, &ts);
1646 ts.tv_sec += 5;
1647 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001648 }
1649 if (m_serverStartRequested) {
1650 m_serverStartRequested = false;
1651 ret = -ETIMEDOUT;
1652 } else {
1653 reply->writeStrongBinder(m_serverStarted);
1654 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001655 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001656 ret = NO_ERROR;
1657 }
1658 } else if (ret >= 0) {
1659 m_serverStartRequested = false;
1660 ret = UNKNOWN_ERROR;
1661 }
1662 pthread_mutex_unlock(&m_serverWaitMutex);
1663 return ret;
1664 }
Steven Moreland35626652021-05-15 01:32:04 +00001665 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1666 IPCThreadState::SpGuard spGuard{
1667 .address = __builtin_frame_address(0),
1668 .context = "GuardInBinderTransaction",
1669 };
1670 const IPCThreadState::SpGuard *origGuard =
1671 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1672
1673 // if the guard works, this should abort
1674 (void)IPCThreadState::self()->getCallingPid();
1675
1676 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1677 return NO_ERROR;
1678 }
1679
Marco Ballesio7ee17572020-09-08 10:30:03 -07001680 case BINDER_LIB_TEST_GETPID:
1681 reply->writeInt32(getpid());
1682 return NO_ERROR;
1683 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1684 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001685 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001686 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001687 // oneway error codes should be ignored
1688 if (flags & TF_ONE_WAY) {
1689 return UNKNOWN_ERROR;
1690 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001691 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001692 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1693 // Note: this transaction is only designed for use with a
1694 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001695 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001696 // A callback was already pending; this means that
1697 // we received a second call while still processing
1698 // the first one. Fail the test.
1699 sp<IBinder> callback = data.readStrongBinder();
1700 Parcel data2;
1701 data2.writeInt32(UNKNOWN_ERROR);
1702
Yi Kong91635562018-06-07 14:38:36 -07001703 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001704 } else {
1705 m_callback = data.readStrongBinder();
1706 int32_t delayUs = data.readInt32();
1707 /*
1708 * It's necessary that we sleep here, so the next
1709 * transaction the caller makes will be queued to
1710 * the async queue.
1711 */
1712 usleep(delayUs);
1713
1714 /*
1715 * Now when we return, libbinder will tell the kernel
1716 * we are done with this transaction, and the kernel
1717 * can move the queued transaction to either the
1718 * thread todo worklist (for kernels without the fix),
1719 * or the proc todo worklist. In case of the former,
1720 * the next outbound call will pick up the pending
1721 * transaction, which leads to undesired reentrant
1722 * behavior. This is caught in the if() branch above.
1723 */
1724 }
1725
1726 return NO_ERROR;
1727 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001728 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1729 Parcel data2, reply2;
1730 sp<IBinder> binder;
1731 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001732 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001733 return BAD_VALUE;
1734 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001735 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001736 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1737 return NO_ERROR;
1738 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001739 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1740 reply->writeStrongBinder(this);
1741 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001742 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1743 reply->writeInt32(m_id);
1744 return NO_ERROR;
1745 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1746 int32_t count;
1747 uint32_t indirect_code;
1748 sp<IBinder> binder;
1749
1750 count = data.readInt32();
1751 reply->writeInt32(m_id);
1752 reply->writeInt32(count);
1753 for (int i = 0; i < count; i++) {
1754 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001755 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001756 return BAD_VALUE;
1757 }
1758 indirect_code = data.readInt32();
1759 BinderLibTestBundle data2(&data);
1760 if (!data2.isValid()) {
1761 return BAD_VALUE;
1762 }
1763 BinderLibTestBundle reply2;
1764 binder->transact(indirect_code, data2, &reply2);
1765 reply2.appendTo(reply);
1766 }
1767 return NO_ERROR;
1768 }
1769 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1770 reply->setError(data.readInt32());
1771 return NO_ERROR;
1772 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1773 reply->writeInt32(sizeof(void *));
1774 return NO_ERROR;
1775 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1776 return NO_ERROR;
1777 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1778 m_strongRef = data.readStrongBinder();
1779 return NO_ERROR;
1780 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1781 int ret;
1782 Parcel data2, reply2;
1783 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1784 sp<IBinder> target;
1785 sp<IBinder> callback;
1786
1787 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001788 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001789 return BAD_VALUE;
1790 }
1791 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001792 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001793 return BAD_VALUE;
1794 }
1795 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001796 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001797 data2.writeInt32(ret);
1798 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1799 return NO_ERROR;
1800 }
1801 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1802 int ret;
1803 int32_t size;
1804 const void *buf;
1805 int fd;
1806
1807 fd = data.readFileDescriptor();
1808 if (fd < 0) {
1809 return BAD_VALUE;
1810 }
1811 ret = data.readInt32(&size);
1812 if (ret != NO_ERROR) {
1813 return ret;
1814 }
1815 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001816 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001817 return BAD_VALUE;
1818 }
1819 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001820 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001821 return NO_ERROR;
1822 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001823 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1824 int ret;
1825 int32_t size;
1826 const void *buf;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001827 unique_fd fd;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001828
1829 ret = data.readUniqueParcelFileDescriptor(&fd);
1830 if (ret != NO_ERROR) {
1831 return ret;
1832 }
1833 ret = data.readInt32(&size);
1834 if (ret != NO_ERROR) {
1835 return ret;
1836 }
1837 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001838 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001839 return BAD_VALUE;
1840 }
1841 ret = write(fd.get(), buf, size);
1842 if (ret != size) return UNKNOWN_ERROR;
1843 return NO_ERROR;
1844 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001845 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1846 alarm(10);
1847 return NO_ERROR;
1848 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001849 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001850 ;
1851 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001852 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001853 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001854 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001855 return NO_ERROR;
1856 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001857 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1858 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001859 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001860 return NO_ERROR;
1861 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001862 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1863 int policy = 0;
1864 sched_param param;
1865 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1866 return UNKNOWN_ERROR;
1867 }
1868 reply->writeInt32(policy);
1869 reply->writeInt32(param.sched_priority);
1870 return NO_ERROR;
1871 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001872 case BINDER_LIB_TEST_ECHO_VECTOR: {
1873 std::vector<uint64_t> vector;
1874 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001875 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001876 reply->writeUint64Vector(vector);
1877 return NO_ERROR;
1878 }
Siarhei Vishniakou116f6b82022-10-03 13:43:15 -07001879 case BINDER_LIB_TEST_GET_NON_BLOCKING_FD: {
1880 std::array<int, 2> sockets;
1881 const bool created = socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets.data()) == 0;
1882 if (!created) {
1883 ALOGE("Could not create socket pair");
1884 return UNKNOWN_ERROR;
1885 }
1886
1887 const int result = fcntl(sockets[0], F_SETFL, O_NONBLOCK);
1888 if (result != 0) {
1889 ALOGE("Could not make socket non-blocking: %s", strerror(errno));
1890 return UNKNOWN_ERROR;
1891 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001892 unique_fd out(sockets[0]);
Siarhei Vishniakou116f6b82022-10-03 13:43:15 -07001893 status_t writeResult = reply->writeUniqueFileDescriptor(out);
1894 if (writeResult != NO_ERROR) {
1895 ALOGE("Could not write unique_fd");
1896 return writeResult;
1897 }
1898 close(sockets[1]); // we don't need the other side of the fd
1899 return NO_ERROR;
1900 }
Steven Morelandf2e0a952021-11-01 18:17:23 -07001901 case BINDER_LIB_TEST_REJECT_OBJECTS: {
Martijn Coenen82c75312019-07-24 15:18:30 +02001902 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1903 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001904 case BINDER_LIB_TEST_CAN_GET_SID: {
1905 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1906 }
Elie Kheirallah47431c12022-04-21 23:46:17 +00001907 case BINDER_LIB_TEST_GET_MAX_THREAD_COUNT: {
1908 reply->writeInt32(ProcessState::self()->getThreadPoolMaxTotalThreadCount());
1909 return NO_ERROR;
1910 }
Devin Moore4354f712022-12-08 01:44:46 +00001911 case BINDER_LIB_TEST_IS_THREADPOOL_STARTED: {
1912 reply->writeBool(ProcessState::self()->isThreadPoolStarted());
1913 return NO_ERROR;
1914 }
Elie Kheirallah47431c12022-04-21 23:46:17 +00001915 case BINDER_LIB_TEST_PROCESS_LOCK: {
Elie Kheirallahc2f5a7e2022-05-27 22:43:40 +00001916 m_blockMutex.lock();
Elie Kheirallah47431c12022-04-21 23:46:17 +00001917 return NO_ERROR;
1918 }
1919 case BINDER_LIB_TEST_LOCK_UNLOCK: {
Elie Kheirallahc2f5a7e2022-05-27 22:43:40 +00001920 std::lock_guard<std::mutex> _l(m_blockMutex);
Elie Kheirallah47431c12022-04-21 23:46:17 +00001921 return NO_ERROR;
1922 }
1923 case BINDER_LIB_TEST_UNLOCK_AFTER_MS: {
1924 int32_t ms = data.readInt32();
1925 return unlockInMs(ms);
1926 }
1927 case BINDER_LIB_TEST_PROCESS_TEMPORARY_LOCK: {
Elie Kheirallahc2f5a7e2022-05-27 22:43:40 +00001928 m_blockMutex.lock();
1929 sp<BinderLibTestService> thisService = this;
1930 int32_t value = data.readInt32();
1931 // start local thread to unlock in 1s
1932 std::thread t([=] { thisService->unlockInMs(value); });
Elie Kheirallah47431c12022-04-21 23:46:17 +00001933 t.detach();
1934 return NO_ERROR;
1935 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001936 default:
1937 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001938 };
1939 }
1940
Elie Kheirallah47431c12022-04-21 23:46:17 +00001941 status_t unlockInMs(int32_t ms) {
1942 usleep(ms * 1000);
Elie Kheirallahc2f5a7e2022-05-27 22:43:40 +00001943 m_blockMutex.unlock();
Elie Kheirallah47431c12022-04-21 23:46:17 +00001944 return NO_ERROR;
1945 }
1946
Yifan Hong543edcd2021-05-18 19:47:30 -07001947private:
1948 int32_t m_id;
1949 int32_t m_nextServerId;
1950 pthread_mutex_t m_serverWaitMutex;
1951 pthread_cond_t m_serverWaitCond;
1952 bool m_serverStartRequested;
1953 sp<IBinder> m_serverStarted;
1954 sp<IBinder> m_strongRef;
1955 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001956 bool m_exitOnDestroy;
Elie Kheirallahc2f5a7e2022-05-27 22:43:40 +00001957 std::mutex m_blockMutex;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001958};
1959
Martijn Coenen45b07b42017-08-09 12:07:45 +02001960int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001961{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001962 binderLibTestServiceName += String16(binderserversuffix);
1963
Steven Moreland35626652021-05-15 01:32:04 +00001964 // Testing to make sure that calls that we are serving can use getCallin*
1965 // even though we don't here.
1966 IPCThreadState::SpGuard spGuard{
1967 .address = __builtin_frame_address(0),
1968 .context = "main server thread",
1969 };
1970 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1971
Riley Andrews06b01ad2014-12-18 12:10:08 -08001972 status_t ret;
1973 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001974 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001975 {
1976 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001977
Steven Morelandbf1915b2020-07-16 22:43:02 +00001978 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1979
Steven Morelandcf03cf12020-12-04 02:58:40 +00001980 testService->setInheritRt(true);
1981
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001982 /*
1983 * Normally would also contain functionality as well, but we are only
1984 * testing the extension mechanism.
1985 */
1986 testService->setExtension(new BBinder());
1987
Martijn Coenen82c75312019-07-24 15:18:30 +02001988 // Required for test "BufRejected'
1989 testService->setRequestingSid(true);
1990
Martijn Coenen45b07b42017-08-09 12:07:45 +02001991 /*
1992 * We need this below, but can't hold a sp<> because it prevents the
1993 * node from being cleaned up automatically. It's safe in this case
1994 * because of how the tests are written.
1995 */
1996 testServicePtr = testService.get();
1997
Riley Andrews06b01ad2014-12-18 12:10:08 -08001998 if (index == 0) {
1999 ret = sm->addService(binderLibTestServiceName, testService);
2000 } else {
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +00002001#pragma clang diagnostic push
2002#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Riley Andrews06b01ad2014-12-18 12:10:08 -08002003 sp<IBinder> server = sm->getService(binderLibTestServiceName);
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +00002004#pragma clang diagnostic pop
Riley Andrews06b01ad2014-12-18 12:10:08 -08002005 Parcel data, reply;
2006 data.writeInt32(index);
2007 data.writeStrongBinder(testService);
2008
2009 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
2010 }
2011 }
2012 write(readypipefd, &ret, sizeof(ret));
2013 close(readypipefd);
2014 //printf("%s: ret %d\n", __func__, ret);
2015 if (ret)
2016 return 1;
2017 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02002018 if (usePoll) {
2019 int fd;
2020 struct epoll_event ev;
2021 int epoll_fd;
2022 IPCThreadState::self()->setupPolling(&fd);
2023 if (fd < 0) {
2024 return 1;
2025 }
2026 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
2027
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08002028 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02002029 if (epoll_fd == -1) {
2030 return 1;
2031 }
2032
2033 ev.events = EPOLLIN;
2034 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
2035 return 1;
2036 }
2037
2038 while (1) {
2039 /*
2040 * We simulate a single-threaded process using the binder poll
2041 * interface; besides handling binder commands, it can also
2042 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07002043 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02002044 *
2045 * processPendingCall() will then issue that transaction.
2046 */
2047 struct epoll_event events[1];
2048 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
2049 if (numEvents < 0) {
2050 if (errno == EINTR) {
2051 continue;
2052 }
2053 return 1;
2054 }
2055 if (numEvents > 0) {
2056 IPCThreadState::self()->handlePolledCommands();
2057 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
2058 testServicePtr->processPendingCall();
2059 }
2060 }
2061 } else {
Elie Kheirallah47431c12022-04-21 23:46:17 +00002062 ProcessState::self()->setThreadPoolMaxThreadCount(kKernelThreads);
Martijn Coenen45b07b42017-08-09 12:07:45 +02002063 ProcessState::self()->startThreadPool();
2064 IPCThreadState::self()->joinThreadPool();
2065 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08002066 //printf("%s: joinThreadPool returned\n", __func__);
2067 return 1; /* joinThreadPool should not return */
2068}
2069
Steven Moreland68275d72023-04-21 22:12:45 +00002070int main(int argc, char** argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07002071 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08002072 binderservername = argv[2];
2073 } else {
2074 binderservername = argv[0];
2075 }
2076
Martijn Coenen45b07b42017-08-09 12:07:45 +02002077 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
2078 binderserversuffix = argv[5];
2079 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08002080 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07002081 binderserversuffix = new char[16];
2082 snprintf(binderserversuffix, 16, "%d", getpid());
2083 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08002084
2085 ::testing::InitGoogleTest(&argc, argv);
2086 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
2087 ProcessState::self()->startThreadPool();
2088 return RUN_ALL_TESTS();
2089}