blob: fc8776b0d0ad599e735e684ed6b53cd89d6eb7c7 [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 Hong84bedeb2021-04-21 21:37:17 -070032#include <android-base/result.h>
Yifan Hong8b890852021-06-10 13:44:09 -070033#include <android-base/strings.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070034#include <android-base/unique_fd.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080035#include <binder/Binder.h>
Yifan Hong34823232021-06-07 17:23:00 -070036#include <binder/BpBinder.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080037#include <binder/IBinder.h>
38#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070040#include <binder/RpcServer.h>
41#include <binder/RpcSession.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080042
Steven Morelandcf03cf12020-12-04 02:58:40 +000043#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020044#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080045#include <sys/prctl.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070046#include <sys/socket.h>
47#include <sys/un.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020048
Steven Moreland6ba5a252021-05-04 22:49:00 +000049#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070050#include "binderAbiHelper.h"
51
Riley Andrews06b01ad2014-12-18 12:10:08 -080052#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
53
54using namespace android;
Yifan Hong84bedeb2021-04-21 21:37:17 -070055using namespace std::string_literals;
56using namespace std::chrono_literals;
Yifan Hong28d6c352021-06-04 17:27:35 -070057using android::base::testing::HasValue;
Yifan Hong8b890852021-06-10 13:44:09 -070058using android::base::testing::Ok;
Yifan Hong84bedeb2021-04-21 21:37:17 -070059using testing::ExplainMatchResult;
Yifan Hongbd276552022-02-28 15:28:51 -080060using testing::Matcher;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070061using testing::Not;
Yifan Hong84bedeb2021-04-21 21:37:17 -070062using testing::WithParamInterface;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070063
64// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
65MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
66 *result_listener << statusToString(arg);
67 return expected == arg;
68}
Riley Andrews06b01ad2014-12-18 12:10:08 -080069
Sherry Yang336cdd32017-07-24 14:12:27 -070070static ::testing::AssertionResult IsPageAligned(void *buf) {
71 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
72 return ::testing::AssertionSuccess();
73 else
74 return ::testing::AssertionFailure() << buf << " is not page aligned";
75}
76
Riley Andrews06b01ad2014-12-18 12:10:08 -080077static testing::Environment* binder_env;
78static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070079static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080080static char binderserverarg[] = "--binderserver";
81
Steven Morelandbf1915b2020-07-16 22:43:02 +000082static constexpr int kSchedPolicy = SCHED_RR;
83static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000084static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000085
Riley Andrews06b01ad2014-12-18 12:10:08 -080086static String16 binderLibTestServiceName = String16("test.binderLib");
87
88enum BinderLibTestTranscationCode {
89 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
90 BINDER_LIB_TEST_REGISTER_SERVER,
91 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020092 BINDER_LIB_TEST_ADD_POLL_SERVER,
Steven Moreland35626652021-05-15 01:32:04 +000093 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080094 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070095 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020096 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080097 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070098 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080099 BINDER_LIB_TEST_GET_ID_TRANSACTION,
100 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
101 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
102 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
103 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
104 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
105 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +0900106 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800107 BINDER_LIB_TEST_EXIT_TRANSACTION,
108 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
109 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -0700110 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100111 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +0000112 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -0700113 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
114 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800115 BINDER_LIB_TEST_ECHO_VECTOR,
Steven Morelandf2e0a952021-11-01 18:17:23 -0700116 BINDER_LIB_TEST_REJECT_OBJECTS,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000117 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800118};
119
Martijn Coenen45b07b42017-08-09 12:07:45 +0200120pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800121{
122 int ret;
123 pid_t pid;
124 status_t status;
125 int pipefd[2];
126 char stri[16];
127 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200128 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800129 char *childargv[] = {
130 binderservername,
131 binderserverarg,
132 stri,
133 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200134 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700135 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700136 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800137 };
138
139 ret = pipe(pipefd);
140 if (ret < 0)
141 return ret;
142
143 snprintf(stri, sizeof(stri), "%d", arg2);
144 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200145 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800146
147 pid = fork();
148 if (pid == -1)
149 return pid;
150 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800151 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800152 close(pipefd[0]);
153 execv(binderservername, childargv);
154 status = -errno;
155 write(pipefd[1], &status, sizeof(status));
156 fprintf(stderr, "execv failed, %s\n", strerror(errno));
157 _exit(EXIT_FAILURE);
158 }
159 close(pipefd[1]);
160 ret = read(pipefd[0], &status, sizeof(status));
161 //printf("pipe read returned %d, status %d\n", ret, status);
162 close(pipefd[0]);
163 if (ret == sizeof(status)) {
164 ret = status;
165 } else {
166 kill(pid, SIGKILL);
167 if (ret >= 0) {
168 ret = NO_INIT;
169 }
170 }
171 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700172 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800173 return ret;
174 }
175 return pid;
176}
177
Yifan Hong84bedeb2021-04-21 21:37:17 -0700178android::base::Result<int32_t> GetId(sp<IBinder> service) {
179 using android::base::Error;
180 Parcel data, reply;
181 data.markForBinder(service);
182 const char *prefix = data.isForRpc() ? "On RPC server, " : "On binder server, ";
183 status_t status = service->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
184 if (status != OK)
185 return Error(status) << prefix << "transact(GET_ID): " << statusToString(status);
186 int32_t result = 0;
187 status = reply.readInt32(&result);
188 if (status != OK) return Error(status) << prefix << "readInt32: " << statusToString(status);
189 return result;
190}
191
Riley Andrews06b01ad2014-12-18 12:10:08 -0800192class BinderLibTestEnv : public ::testing::Environment {
193 public:
194 BinderLibTestEnv() {}
195 sp<IBinder> getServer(void) {
196 return m_server;
197 }
198
199 private:
200 virtual void SetUp() {
201 m_serverpid = start_server_process(0);
202 //printf("m_serverpid %d\n", m_serverpid);
203 ASSERT_GT(m_serverpid, 0);
204
205 sp<IServiceManager> sm = defaultServiceManager();
206 //printf("%s: pid %d, get service\n", __func__, m_pid);
207 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700208 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 //printf("%s: pid %d, get service done\n", __func__, m_pid);
210 }
211 virtual void TearDown() {
212 status_t ret;
213 Parcel data, reply;
214 int exitStatus;
215 pid_t pid;
216
217 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700218 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800219 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
220 EXPECT_EQ(0, ret);
221 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
222 EXPECT_EQ(0, ret);
223 }
224 if (m_serverpid > 0) {
225 //printf("wait for %d\n", m_pids[i]);
226 pid = wait(&exitStatus);
227 EXPECT_EQ(m_serverpid, pid);
228 EXPECT_TRUE(WIFEXITED(exitStatus));
229 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
230 }
231 }
232
233 pid_t m_serverpid;
234 sp<IBinder> m_server;
235};
236
237class BinderLibTest : public ::testing::Test {
238 public:
239 virtual void SetUp() {
240 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000241 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800242 }
243 virtual void TearDown() {
244 }
245 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200246 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800247 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800248 int32_t id;
249 Parcel data, reply;
250 sp<IBinder> binder;
251
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700252 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800253
Yi Kong91635562018-06-07 14:38:36 -0700254 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800255 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700256 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700257 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800258 if (idPtr)
259 *idPtr = id;
260 return binder;
261 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200262
Yi Kong91635562018-06-07 14:38:36 -0700263 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200264 {
265 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
266 }
267
Yi Kong91635562018-06-07 14:38:36 -0700268 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200269 {
270 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
271 }
272
Riley Andrews06b01ad2014-12-18 12:10:08 -0800273 void waitForReadData(int fd, int timeout_ms) {
274 int ret;
275 pollfd pfd = pollfd();
276
277 pfd.fd = fd;
278 pfd.events = POLLIN;
279 ret = poll(&pfd, 1, timeout_ms);
280 EXPECT_EQ(1, ret);
281 }
282
283 sp<IBinder> m_server;
284};
285
286class BinderLibTestBundle : public Parcel
287{
288 public:
289 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800290 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800291 int32_t mark;
292 int32_t bundleLen;
293 size_t pos;
294
295 if (source->readInt32(&mark))
296 return;
297 if (mark != MARK_START)
298 return;
299 if (source->readInt32(&bundleLen))
300 return;
301 pos = source->dataPosition();
302 if (Parcel::appendFrom(source, pos, bundleLen))
303 return;
304 source->setDataPosition(pos + bundleLen);
305 if (source->readInt32(&mark))
306 return;
307 if (mark != MARK_END)
308 return;
309 m_isValid = true;
310 setDataPosition(0);
311 }
312 void appendTo(Parcel *dest) {
313 dest->writeInt32(MARK_START);
314 dest->writeInt32(dataSize());
315 dest->appendFrom(this, 0, dataSize());
316 dest->writeInt32(MARK_END);
317 };
318 bool isValid(void) {
319 return m_isValid;
320 }
321 private:
322 enum {
323 MARK_START = B_PACK_CHARS('B','T','B','S'),
324 MARK_END = B_PACK_CHARS('B','T','B','E'),
325 };
326 bool m_isValid;
327};
328
329class BinderLibTestEvent
330{
331 public:
332 BinderLibTestEvent(void)
333 : m_eventTriggered(false)
334 {
Yi Kong91635562018-06-07 14:38:36 -0700335 pthread_mutex_init(&m_waitMutex, nullptr);
336 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800337 }
338 int waitEvent(int timeout_s)
339 {
340 int ret;
341 pthread_mutex_lock(&m_waitMutex);
342 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800343 struct timespec ts;
344 clock_gettime(CLOCK_REALTIME, &ts);
345 ts.tv_sec += timeout_s;
346 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800347 }
348 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
349 pthread_mutex_unlock(&m_waitMutex);
350 return ret;
351 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200352 pthread_t getTriggeringThread()
353 {
354 return m_triggeringThread;
355 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800356 protected:
357 void triggerEvent(void) {
358 pthread_mutex_lock(&m_waitMutex);
359 pthread_cond_signal(&m_waitCond);
360 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200361 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800362 pthread_mutex_unlock(&m_waitMutex);
363 };
364 private:
365 pthread_mutex_t m_waitMutex;
366 pthread_cond_t m_waitCond;
367 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200368 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800369};
370
371class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
372{
373 public:
374 BinderLibTestCallBack()
375 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700376 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800377 {
378 }
379 status_t getResult(void)
380 {
381 return m_result;
382 }
383
384 private:
385 virtual status_t onTransact(uint32_t code,
386 const Parcel& data, Parcel* reply,
387 uint32_t flags = 0)
388 {
389 (void)reply;
390 (void)flags;
391 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200392 case BINDER_LIB_TEST_CALL_BACK: {
393 status_t status = data.readInt32(&m_result);
394 if (status != NO_ERROR) {
395 m_result = status;
396 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800397 triggerEvent();
398 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200399 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700400 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
401 sp<IBinder> server;
402 int ret;
403 const uint8_t *buf = data.data();
404 size_t size = data.dataSize();
405 if (m_prev_end) {
406 /* 64-bit kernel needs at most 8 bytes to align buffer end */
407 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
408 } else {
409 EXPECT_TRUE(IsPageAligned((void *)buf));
410 }
411
412 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
413
414 if (size > 0) {
415 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
416 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
417 data, reply);
418 EXPECT_EQ(NO_ERROR, ret);
419 }
420 return NO_ERROR;
421 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800422 default:
423 return UNKNOWN_TRANSACTION;
424 }
425 }
426
427 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700428 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800429};
430
431class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
432{
433 private:
434 virtual void binderDied(const wp<IBinder>& who) {
435 (void)who;
436 triggerEvent();
437 };
438};
439
Steven Morelandbd98e0f2021-10-14 14:24:15 -0700440TEST_F(BinderLibTest, CannotUseBinderAfterFork) {
441 // EXPECT_DEATH works by forking the process
442 EXPECT_DEATH({ ProcessState::self(); }, "libbinder ProcessState can not be used after fork");
443}
444
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500445TEST_F(BinderLibTest, WasParceled) {
446 auto binder = sp<BBinder>::make();
447 EXPECT_FALSE(binder->wasParceled());
448 Parcel data;
449 data.writeStrongBinder(binder);
450 EXPECT_TRUE(binder->wasParceled());
451}
452
Riley Andrews06b01ad2014-12-18 12:10:08 -0800453TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800454 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700455 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
456 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800457}
458
Steven Moreland80844f72020-12-12 02:06:08 +0000459TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000460 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700461 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
462 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000463}
464
Steven Morelandf183fdd2020-10-27 00:12:12 +0000465TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000466 Parcel data, reply;
467 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700468 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
469 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000470}
471
Marco Ballesio7ee17572020-09-08 10:30:03 -0700472TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700473 Parcel data, reply, replypid;
Li Li6f059292021-09-10 09:59:30 -0700474 std::ifstream freezer_file("/sys/fs/cgroup/uid_0/cgroup.freeze");
Marco Ballesio7ee17572020-09-08 10:30:03 -0700475
Li Li6f059292021-09-10 09:59:30 -0700476 // Pass test on devices where the cgroup v2 freezer is not supported
Marco Ballesio7ee17572020-09-08 10:30:03 -0700477 if (freezer_file.fail()) {
478 GTEST_SKIP();
479 return;
480 }
481
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700482 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700483 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700484 for (int i = 0; i < 10; i++) {
485 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
486 }
Li Li6f059292021-09-10 09:59:30 -0700487
488 // Pass test on devices where BINDER_FREEZE ioctl is not supported
489 int ret = IPCThreadState::self()->freeze(pid, false, 0);
490 if (ret != 0) {
491 GTEST_SKIP();
492 return;
493 }
494
495 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
496 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
497 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, true, 1000));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700498 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700499
Li Li4e678b92021-09-14 12:14:42 -0700500 uint32_t sync_received, async_received;
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700501
502 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
503 &async_received));
504
505 EXPECT_EQ(sync_received, 1);
506 EXPECT_EQ(async_received, 0);
507
Li Li4e678b92021-09-14 12:14:42 -0700508 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, false, 0));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700509 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
510}
511
Riley Andrews06b01ad2014-12-18 12:10:08 -0800512TEST_F(BinderLibTest, SetError) {
513 int32_t testValue[] = { 0, -123, 123 };
514 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800515 Parcel data, reply;
516 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700517 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
518 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800519 }
520}
521
522TEST_F(BinderLibTest, GetId) {
Yifan Hong28d6c352021-06-04 17:27:35 -0700523 EXPECT_THAT(GetId(m_server), HasValue(0));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800524}
525
526TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800527 int32_t ptrsize;
528 Parcel data, reply;
529 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700530 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700531 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
532 StatusEq(NO_ERROR));
533 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800534 RecordProperty("TestPtrSize", sizeof(void *));
535 RecordProperty("ServerPtrSize", sizeof(void *));
536}
537
538TEST_F(BinderLibTest, IndirectGetId2)
539{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800540 int32_t id;
541 int32_t count;
542 Parcel data, reply;
543 int32_t serverId[3];
544
545 data.writeInt32(ARRAY_SIZE(serverId));
546 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
547 sp<IBinder> server;
548 BinderLibTestBundle datai;
549
550 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700551 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800552 data.writeStrongBinder(server);
553 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
554 datai.appendTo(&data);
555 }
556
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700557 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
558 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700560 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800561 EXPECT_EQ(0, id);
562
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700563 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700564 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800565
566 for (size_t i = 0; i < (size_t)count; i++) {
567 BinderLibTestBundle replyi(&reply);
568 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700569 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800570 EXPECT_EQ(serverId[i], id);
571 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
572 }
573
574 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
575}
576
577TEST_F(BinderLibTest, IndirectGetId3)
578{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800579 int32_t id;
580 int32_t count;
581 Parcel data, reply;
582 int32_t serverId[3];
583
584 data.writeInt32(ARRAY_SIZE(serverId));
585 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
586 sp<IBinder> server;
587 BinderLibTestBundle datai;
588 BinderLibTestBundle datai2;
589
590 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700591 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800592 data.writeStrongBinder(server);
593 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
594
595 datai.writeInt32(1);
596 datai.writeStrongBinder(m_server);
597 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
598 datai2.appendTo(&datai);
599
600 datai.appendTo(&data);
601 }
602
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700603 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
604 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700606 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800607 EXPECT_EQ(0, id);
608
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700609 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700610 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800611
612 for (size_t i = 0; i < (size_t)count; i++) {
613 int32_t counti;
614
615 BinderLibTestBundle replyi(&reply);
616 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700617 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800618 EXPECT_EQ(serverId[i], id);
619
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700620 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621 EXPECT_EQ(1, counti);
622
623 BinderLibTestBundle replyi2(&replyi);
624 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700625 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800626 EXPECT_EQ(0, id);
627 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
628
629 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
630 }
631
632 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
633}
634
635TEST_F(BinderLibTest, CallBack)
636{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800637 Parcel data, reply;
638 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
639 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700640 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
641 StatusEq(NO_ERROR));
642 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
643 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800644}
645
Steven Moreland35626652021-05-15 01:32:04 +0000646TEST_F(BinderLibTest, BinderCallContextGuard) {
647 sp<IBinder> binder = addServer();
648 Parcel data, reply;
649 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
650 StatusEq(DEAD_OBJECT));
651}
652
Riley Andrews06b01ad2014-12-18 12:10:08 -0800653TEST_F(BinderLibTest, AddServer)
654{
655 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700656 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800657}
658
Riley Andrews06b01ad2014-12-18 12:10:08 -0800659TEST_F(BinderLibTest, DeathNotificationStrongRef)
660{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800661 sp<IBinder> sbinder;
662
663 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
664
665 {
666 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700667 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700668 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800669 sbinder = binder;
670 }
671 {
672 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700673 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
674 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800675 }
676 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700677 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
678 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800679}
680
681TEST_F(BinderLibTest, DeathNotificationMultiple)
682{
683 status_t ret;
684 const int clientcount = 2;
685 sp<IBinder> target;
686 sp<IBinder> linkedclient[clientcount];
687 sp<BinderLibTestCallBack> callBack[clientcount];
688 sp<IBinder> passiveclient[clientcount];
689
690 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700691 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800692 for (int i = 0; i < clientcount; i++) {
693 {
694 Parcel data, reply;
695
696 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700697 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800698 callBack[i] = new BinderLibTestCallBack();
699 data.writeStrongBinder(target);
700 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700701 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
702 &reply, TF_ONE_WAY),
703 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800704 }
705 {
706 Parcel data, reply;
707
708 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700709 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800710 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700711 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
712 &reply, TF_ONE_WAY),
713 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800714 }
715 }
716 {
717 Parcel data, reply;
718 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
719 EXPECT_EQ(0, ret);
720 }
721
722 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700723 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
724 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800725 }
726}
727
Martijn Coenenf7100e42017-07-31 12:14:09 +0200728TEST_F(BinderLibTest, DeathNotificationThread)
729{
730 status_t ret;
731 sp<BinderLibTestCallBack> callback;
732 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700733 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200734 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700735 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200736
737 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
738
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700739 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200740
741 {
742 Parcel data, reply;
743 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
744 EXPECT_EQ(0, ret);
745 }
746
747 /* Make sure it's dead */
748 testDeathRecipient->waitEvent(5);
749
750 /* Now, pass the ref to another process and ask that process to
751 * call linkToDeath() on it, and wait for a response. This tests
752 * two things:
753 * 1) You still get death notifications when calling linkToDeath()
754 * on a ref that is already dead when it was passed to you.
755 * 2) That death notifications are not directly pushed to the thread
756 * registering them, but to the threadpool (proc workqueue) instead.
757 *
758 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
759 * is blocked on a condition variable waiting for the death notification to be
760 * called; therefore, that thread is not available for handling proc work.
761 * So, if the death notification was pushed to the thread workqueue, the callback
762 * would never be called, and the test would timeout and fail.
763 *
764 * Note that we can't do this part of the test from this thread itself, because
765 * the binder driver would only push death notifications to the thread if
766 * it is a looper thread, which this thread is not.
767 *
768 * See b/23525545 for details.
769 */
770 {
771 Parcel data, reply;
772
773 callback = new BinderLibTestCallBack();
774 data.writeStrongBinder(target);
775 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700776 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
777 TF_ONE_WAY),
778 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200779 }
780
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700781 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
782 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200783}
784
Riley Andrews06b01ad2014-12-18 12:10:08 -0800785TEST_F(BinderLibTest, PassFile) {
786 int ret;
787 int pipefd[2];
788 uint8_t buf[1] = { 0 };
789 uint8_t write_value = 123;
790
791 ret = pipe2(pipefd, O_NONBLOCK);
792 ASSERT_EQ(0, ret);
793
794 {
795 Parcel data, reply;
796 uint8_t writebuf[1] = { write_value };
797
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700798 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800799
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700800 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800801
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700802 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800803
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700804 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
805 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800806 }
807
808 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700809 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800810 EXPECT_EQ(write_value, buf[0]);
811
812 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
813
814 ret = read(pipefd[0], buf, sizeof(buf));
815 EXPECT_EQ(0, ret);
816
817 close(pipefd[0]);
818}
819
Ryo Hashimotobf551892018-05-31 16:58:35 +0900820TEST_F(BinderLibTest, PassParcelFileDescriptor) {
821 const int datasize = 123;
822 std::vector<uint8_t> writebuf(datasize);
823 for (size_t i = 0; i < writebuf.size(); ++i) {
824 writebuf[i] = i;
825 }
826
827 android::base::unique_fd read_end, write_end;
828 {
829 int pipefd[2];
830 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
831 read_end.reset(pipefd[0]);
832 write_end.reset(pipefd[1]);
833 }
834 {
835 Parcel data;
836 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
837 write_end.reset();
838 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
839 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
840
841 Parcel reply;
842 EXPECT_EQ(NO_ERROR,
843 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
844 &reply));
845 }
846 std::vector<uint8_t> readbuf(datasize);
847 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
848 EXPECT_EQ(writebuf, readbuf);
849
850 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
851
852 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
853}
854
Riley Andrews06b01ad2014-12-18 12:10:08 -0800855TEST_F(BinderLibTest, PromoteLocal) {
856 sp<IBinder> strong = new BBinder();
857 wp<IBinder> weak = strong;
858 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700859 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800860 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700861 strong = nullptr;
862 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800863 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700864 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800865}
866
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700867TEST_F(BinderLibTest, LocalGetExtension) {
868 sp<BBinder> binder = new BBinder();
869 sp<IBinder> ext = new BBinder();
870 binder->setExtension(ext);
871 EXPECT_EQ(ext, binder->getExtension());
872}
873
874TEST_F(BinderLibTest, RemoteGetExtension) {
875 sp<IBinder> server = addServer();
876 ASSERT_TRUE(server != nullptr);
877
878 sp<IBinder> extension;
879 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
880 ASSERT_NE(nullptr, extension.get());
881
882 EXPECT_EQ(NO_ERROR, extension->pingBinder());
883}
884
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700885TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700886 Parcel data, reply;
887
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700888 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
889 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700890
891 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700892 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800893 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
894 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
895 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
896 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700897}
898
Connor O'Brien52be2c92016-09-20 14:18:08 -0700899TEST_F(BinderLibTest, FreedBinder) {
900 status_t ret;
901
902 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700903 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700904
905 __u32 freedHandle;
906 wp<IBinder> keepFreedBinder;
907 {
908 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700909 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
910 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700911 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
912 freedHandle = freed->handle;
913 /* Add a weak ref to the freed binder so the driver does not
914 * delete its reference to it - otherwise the transaction
915 * fails regardless of whether the driver is fixed.
916 */
Steven Morelande171d622019-07-17 16:06:01 -0700917 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700918 }
Steven Morelande171d622019-07-17 16:06:01 -0700919 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700920 {
921 Parcel data, reply;
922 data.writeStrongBinder(server);
923 /* Replace original handle with handle to the freed binder */
924 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
925 __u32 oldHandle = strong->handle;
926 strong->handle = freedHandle;
927 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
928 /* Returns DEAD_OBJECT (-32) if target crashes and
929 * FAILED_TRANSACTION if the driver rejects the invalid
930 * object.
931 */
932 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
933 /* Restore original handle so parcel destructor does not use
934 * the wrong handle.
935 */
936 strong->handle = oldHandle;
937 }
938}
939
Sherry Yang336cdd32017-07-24 14:12:27 -0700940TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700941 Parcel data, reply;
942 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
943 for (int i = 0; i < 2; i++) {
944 BinderLibTestBundle datai;
945 datai.appendFrom(&data, 0, data.dataSize());
946
947 data.freeData();
948 data.writeInt32(1);
949 data.writeStrongBinder(callBack);
950 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
951
952 datai.appendTo(&data);
953 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700954 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
955 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700956}
957
Martijn Coenen45b07b42017-08-09 12:07:45 +0200958TEST_F(BinderLibTest, OnewayQueueing)
959{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200960 Parcel data, data2;
961
962 sp<IBinder> pollServer = addPollServer();
963
964 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
965 data.writeStrongBinder(callBack);
966 data.writeInt32(500000); // delay in us before calling back
967
968 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
969 data2.writeStrongBinder(callBack2);
970 data2.writeInt32(0); // delay in us
971
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700972 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
973 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200974
975 // The delay ensures that this second transaction will end up on the async_todo list
976 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700977 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
978 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200979
980 // The server will ensure that the two transactions are handled in the expected order;
981 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700982 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
983 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200984
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700985 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
986 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200987}
988
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100989TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
990{
991 status_t ret;
992 Parcel data, reply;
993 data.writeInterfaceToken(binderLibTestServiceName);
994 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
995 EXPECT_EQ(-1, reply.readInt32());
996 EXPECT_EQ(NO_ERROR, ret);
997}
998
999TEST_F(BinderLibTest, WorkSourceSet)
1000{
1001 status_t ret;
1002 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001003 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001004 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001005 data.writeInterfaceToken(binderLibTestServiceName);
1006 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1007 EXPECT_EQ(100, reply.readInt32());
1008 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001009 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1010 EXPECT_EQ(NO_ERROR, ret);
1011}
1012
1013TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1014{
1015 status_t ret;
1016 Parcel data, reply;
1017
1018 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1019 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1020
1021 data.writeInterfaceToken(binderLibTestServiceName);
1022 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1023 EXPECT_EQ(-1, reply.readInt32());
1024 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001025 EXPECT_EQ(NO_ERROR, ret);
1026}
1027
1028TEST_F(BinderLibTest, WorkSourceCleared)
1029{
1030 status_t ret;
1031 Parcel data, reply;
1032
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001033 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001034 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1035 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001036 data.writeInterfaceToken(binderLibTestServiceName);
1037 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1038
1039 EXPECT_EQ(-1, reply.readInt32());
1040 EXPECT_EQ(100, previousWorkSource);
1041 EXPECT_EQ(NO_ERROR, ret);
1042}
1043
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001044TEST_F(BinderLibTest, WorkSourceRestored)
1045{
1046 status_t ret;
1047 Parcel data, reply;
1048
1049 IPCThreadState::self()->setCallingWorkSourceUid(100);
1050 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1051 IPCThreadState::self()->restoreCallingWorkSource(token);
1052
1053 data.writeInterfaceToken(binderLibTestServiceName);
1054 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1055
1056 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001057 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001058 EXPECT_EQ(NO_ERROR, ret);
1059}
1060
Olivier Gaillard91a04802018-11-14 17:32:41 +00001061TEST_F(BinderLibTest, PropagateFlagSet)
1062{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001063 IPCThreadState::self()->clearPropagateWorkSource();
1064 IPCThreadState::self()->setCallingWorkSourceUid(100);
1065 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1066}
1067
1068TEST_F(BinderLibTest, PropagateFlagCleared)
1069{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001070 IPCThreadState::self()->setCallingWorkSourceUid(100);
1071 IPCThreadState::self()->clearPropagateWorkSource();
1072 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1073}
1074
1075TEST_F(BinderLibTest, PropagateFlagRestored)
1076{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001077 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1078 IPCThreadState::self()->restoreCallingWorkSource(token);
1079
1080 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1081}
1082
1083TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1084{
1085 IPCThreadState::self()->setCallingWorkSourceUid(100);
1086
1087 Parcel data, reply;
1088 status_t ret;
1089 data.writeInterfaceToken(binderLibTestServiceName);
1090 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1091
1092 Parcel data2, reply2;
1093 status_t ret2;
1094 data2.writeInterfaceToken(binderLibTestServiceName);
1095 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1096 EXPECT_EQ(100, reply2.readInt32());
1097 EXPECT_EQ(NO_ERROR, ret2);
1098}
1099
Steven Morelandbf1915b2020-07-16 22:43:02 +00001100TEST_F(BinderLibTest, SchedPolicySet) {
1101 sp<IBinder> server = addServer();
1102 ASSERT_TRUE(server != nullptr);
1103
1104 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001105 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1106 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001107
1108 int policy = reply.readInt32();
1109 int priority = reply.readInt32();
1110
1111 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1112 EXPECT_EQ(kSchedPriority, priority);
1113}
1114
Steven Morelandcf03cf12020-12-04 02:58:40 +00001115TEST_F(BinderLibTest, InheritRt) {
1116 sp<IBinder> server = addServer();
1117 ASSERT_TRUE(server != nullptr);
1118
1119 const struct sched_param param {
1120 .sched_priority = kSchedPriorityMore,
1121 };
1122 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1123
1124 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001125 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1126 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001127
1128 int policy = reply.readInt32();
1129 int priority = reply.readInt32();
1130
1131 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1132 EXPECT_EQ(kSchedPriorityMore, priority);
1133}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001134
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001135TEST_F(BinderLibTest, VectorSent) {
1136 Parcel data, reply;
1137 sp<IBinder> server = addServer();
1138 ASSERT_TRUE(server != nullptr);
1139
1140 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1141 data.writeUint64Vector(testValue);
1142
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001143 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001144 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001145 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001146 EXPECT_EQ(readValue, testValue);
1147}
1148
Martijn Coenen82c75312019-07-24 15:18:30 +02001149TEST_F(BinderLibTest, BufRejected) {
1150 Parcel data, reply;
1151 uint32_t buf;
1152 sp<IBinder> server = addServer();
1153 ASSERT_TRUE(server != nullptr);
1154
1155 binder_buffer_object obj {
1156 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001157 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001158 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1159 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001160 };
1161 data.setDataCapacity(1024);
1162 // Write a bogus object at offset 0 to get an entry in the offset table
1163 data.writeFileDescriptor(0);
1164 EXPECT_EQ(data.objectsCount(), 1);
1165 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1166 // And now, overwrite it with the buffer object
1167 memcpy(parcelData, &obj, sizeof(obj));
1168 data.setDataSize(sizeof(obj));
1169
Steven Morelandf2e0a952021-11-01 18:17:23 -07001170 EXPECT_EQ(data.objectsCount(), 1);
1171
Martijn Coenen82c75312019-07-24 15:18:30 +02001172 // Either the kernel should reject this transaction (if it's correct), but
1173 // if it's not, the server implementation should return an error if it
1174 // finds an object in the received Parcel.
Steven Morelandf2e0a952021-11-01 18:17:23 -07001175 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001176 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001177}
1178
Steven Morelandf2e0a952021-11-01 18:17:23 -07001179TEST_F(BinderLibTest, WeakRejected) {
1180 Parcel data, reply;
1181 sp<IBinder> server = addServer();
1182 ASSERT_TRUE(server != nullptr);
1183
1184 auto binder = sp<BBinder>::make();
1185 wp<BBinder> wpBinder(binder);
1186 flat_binder_object obj{
1187 .hdr = {.type = BINDER_TYPE_WEAK_BINDER},
1188 .flags = 0,
1189 .binder = reinterpret_cast<uintptr_t>(wpBinder.get_refs()),
1190 .cookie = reinterpret_cast<uintptr_t>(wpBinder.unsafe_get()),
1191 };
1192 data.setDataCapacity(1024);
1193 // Write a bogus object at offset 0 to get an entry in the offset table
1194 data.writeFileDescriptor(0);
1195 EXPECT_EQ(data.objectsCount(), 1);
1196 uint8_t *parcelData = const_cast<uint8_t *>(data.data());
1197 // And now, overwrite it with the weak binder
1198 memcpy(parcelData, &obj, sizeof(obj));
1199 data.setDataSize(sizeof(obj));
1200
1201 // a previous bug caused other objects to be released an extra time, so we
1202 // test with an object that libbinder will actually try to release
1203 EXPECT_EQ(OK, data.writeStrongBinder(sp<BBinder>::make()));
1204
1205 EXPECT_EQ(data.objectsCount(), 2);
1206
1207 // send it many times, since previous error was memory corruption, make it
1208 // more likely that the server crashes
1209 for (size_t i = 0; i < 100; i++) {
1210 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
1211 StatusEq(BAD_VALUE));
1212 }
1213
1214 EXPECT_THAT(server->pingBinder(), StatusEq(NO_ERROR));
1215}
1216
Steven Moreland254e8ef2021-04-19 22:28:50 +00001217TEST_F(BinderLibTest, GotSid) {
1218 sp<IBinder> server = addServer();
1219
1220 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001221 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001222}
1223
Yifan Hong84bedeb2021-04-21 21:37:17 -07001224class BinderLibRpcTestBase : public BinderLibTest {
1225public:
1226 void SetUp() override {
1227 if (!base::GetBoolProperty("ro.debuggable", false)) {
1228 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1229 "non-debuggable builds.";
1230 }
1231 BinderLibTest::SetUp();
1232 }
1233
1234 std::tuple<android::base::unique_fd, unsigned int> CreateSocket() {
1235 auto rpcServer = RpcServer::make();
1236 EXPECT_NE(nullptr, rpcServer);
1237 if (rpcServer == nullptr) return {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001238 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001239 if (status_t status = rpcServer->setupInetServer("127.0.0.1", 0, &port); status != OK) {
1240 ADD_FAILURE() << "setupInetServer failed" << statusToString(status);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001241 return {};
1242 }
1243 return {rpcServer->releaseServer(), port};
1244 }
1245};
1246
Yifan Hong8b890852021-06-10 13:44:09 -07001247class BinderLibRpcTest : public BinderLibRpcTestBase {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001248
Yifan Hongbd276552022-02-28 15:28:51 -08001249// e.g. EXPECT_THAT(expr, Debuggable(StatusEq(...))
1250// If device is debuggable AND not on user builds, expects matcher.
1251// Otherwise expects INVALID_OPERATION.
1252// Debuggable + non user builds is necessary but not sufficient for setRpcClientDebug to work.
1253static Matcher<status_t> Debuggable(const Matcher<status_t> &matcher) {
1254 bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false) &&
1255 android::base::GetProperty("ro.build.type", "") != "user";
1256 return isDebuggable ? matcher : StatusEq(INVALID_OPERATION);
1257}
1258
Yifan Hong8b890852021-06-10 13:44:09 -07001259TEST_F(BinderLibRpcTest, SetRpcClientDebug) {
1260 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001261 ASSERT_TRUE(binder != nullptr);
1262 auto [socket, port] = CreateSocket();
1263 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001264 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), sp<BBinder>::make()),
1265 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001266}
1267
Yifan Hong8b890852021-06-10 13:44:09 -07001268// Tests for multiple RpcServer's on the same binder object.
1269TEST_F(BinderLibRpcTest, SetRpcClientDebugTwice) {
1270 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001271 ASSERT_TRUE(binder != nullptr);
1272
1273 auto [socket1, port1] = CreateSocket();
1274 ASSERT_TRUE(socket1.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001275 auto keepAliveBinder1 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001276 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), keepAliveBinder1),
1277 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001278
1279 auto [socket2, port2] = CreateSocket();
1280 ASSERT_TRUE(socket2.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001281 auto keepAliveBinder2 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001282 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), keepAliveBinder2),
1283 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001284}
1285
Yifan Hong8b890852021-06-10 13:44:09 -07001286// Negative tests for RPC APIs on IBinder. Call should fail in the same way on both remote and
1287// local binders.
1288class BinderLibRpcTestP : public BinderLibRpcTestBase, public WithParamInterface<bool> {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001289public:
1290 sp<IBinder> GetService() {
1291 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1292 }
1293 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1294 return info.param ? "remote" : "local";
1295 }
1296};
1297
Yifan Hong8b890852021-06-10 13:44:09 -07001298TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoFd) {
1299 auto binder = GetService();
1300 ASSERT_TRUE(binder != nullptr);
1301 EXPECT_THAT(binder->setRpcClientDebug(android::base::unique_fd(), sp<BBinder>::make()),
Yifan Hongbd276552022-02-28 15:28:51 -08001302 Debuggable(StatusEq(BAD_VALUE)));
Yifan Hong8b890852021-06-10 13:44:09 -07001303}
1304
1305TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoKeepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001306 auto binder = GetService();
1307 ASSERT_TRUE(binder != nullptr);
1308 auto [socket, port] = CreateSocket();
1309 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001310 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), nullptr),
1311 Debuggable(StatusEq(UNEXPECTED_NULL)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001312}
Yifan Hong8b890852021-06-10 13:44:09 -07001313INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTestP, testing::Bool(),
1314 BinderLibRpcTestP::ParamToString);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001315
Yifan Hong543edcd2021-05-18 19:47:30 -07001316class BinderLibTestService : public BBinder {
1317public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001318 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1319 : m_id(id),
1320 m_nextServerId(id + 1),
1321 m_serverStartRequested(false),
1322 m_callback(nullptr),
1323 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001324 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1325 pthread_cond_init(&m_serverWaitCond, nullptr);
1326 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001327 ~BinderLibTestService() {
1328 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1329 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001330
Yifan Hong543edcd2021-05-18 19:47:30 -07001331 void processPendingCall() {
1332 if (m_callback != nullptr) {
1333 Parcel data;
1334 data.writeInt32(NO_ERROR);
1335 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1336 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001337 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001338 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001339
Yifan Hong543edcd2021-05-18 19:47:30 -07001340 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1341 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001342 // TODO(b/182914638): also checks getCallingUid() for RPC
1343 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001344 return PERMISSION_DENIED;
1345 }
1346 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001347 case BINDER_LIB_TEST_REGISTER_SERVER: {
1348 int32_t id;
1349 sp<IBinder> binder;
1350 id = data.readInt32();
1351 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001352 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001353 return BAD_VALUE;
1354 }
1355
Yifan Hong543edcd2021-05-18 19:47:30 -07001356 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001357
1358 pthread_mutex_lock(&m_serverWaitMutex);
1359 if (m_serverStartRequested) {
1360 m_serverStartRequested = false;
1361 m_serverStarted = binder;
1362 pthread_cond_signal(&m_serverWaitCond);
1363 }
1364 pthread_mutex_unlock(&m_serverWaitMutex);
1365 return NO_ERROR;
1366 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001367 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001368 case BINDER_LIB_TEST_ADD_SERVER: {
1369 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001370 int serverid;
1371
1372 if (m_id != 0) {
1373 return INVALID_OPERATION;
1374 }
1375 pthread_mutex_lock(&m_serverWaitMutex);
1376 if (m_serverStartRequested) {
1377 ret = -EBUSY;
1378 } else {
1379 serverid = m_nextServerId++;
1380 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001381 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001382
1383 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001384 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001385 pthread_mutex_lock(&m_serverWaitMutex);
1386 }
1387 if (ret > 0) {
1388 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001389 struct timespec ts;
1390 clock_gettime(CLOCK_REALTIME, &ts);
1391 ts.tv_sec += 5;
1392 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001393 }
1394 if (m_serverStartRequested) {
1395 m_serverStartRequested = false;
1396 ret = -ETIMEDOUT;
1397 } else {
1398 reply->writeStrongBinder(m_serverStarted);
1399 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001400 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401 ret = NO_ERROR;
1402 }
1403 } else if (ret >= 0) {
1404 m_serverStartRequested = false;
1405 ret = UNKNOWN_ERROR;
1406 }
1407 pthread_mutex_unlock(&m_serverWaitMutex);
1408 return ret;
1409 }
Steven Moreland35626652021-05-15 01:32:04 +00001410 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1411 IPCThreadState::SpGuard spGuard{
1412 .address = __builtin_frame_address(0),
1413 .context = "GuardInBinderTransaction",
1414 };
1415 const IPCThreadState::SpGuard *origGuard =
1416 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1417
1418 // if the guard works, this should abort
1419 (void)IPCThreadState::self()->getCallingPid();
1420
1421 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1422 return NO_ERROR;
1423 }
1424
Marco Ballesio7ee17572020-09-08 10:30:03 -07001425 case BINDER_LIB_TEST_GETPID:
1426 reply->writeInt32(getpid());
1427 return NO_ERROR;
1428 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1429 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001430 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001431 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001432 // oneway error codes should be ignored
1433 if (flags & TF_ONE_WAY) {
1434 return UNKNOWN_ERROR;
1435 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001436 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001437 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1438 // Note: this transaction is only designed for use with a
1439 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001440 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001441 // A callback was already pending; this means that
1442 // we received a second call while still processing
1443 // the first one. Fail the test.
1444 sp<IBinder> callback = data.readStrongBinder();
1445 Parcel data2;
1446 data2.writeInt32(UNKNOWN_ERROR);
1447
Yi Kong91635562018-06-07 14:38:36 -07001448 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001449 } else {
1450 m_callback = data.readStrongBinder();
1451 int32_t delayUs = data.readInt32();
1452 /*
1453 * It's necessary that we sleep here, so the next
1454 * transaction the caller makes will be queued to
1455 * the async queue.
1456 */
1457 usleep(delayUs);
1458
1459 /*
1460 * Now when we return, libbinder will tell the kernel
1461 * we are done with this transaction, and the kernel
1462 * can move the queued transaction to either the
1463 * thread todo worklist (for kernels without the fix),
1464 * or the proc todo worklist. In case of the former,
1465 * the next outbound call will pick up the pending
1466 * transaction, which leads to undesired reentrant
1467 * behavior. This is caught in the if() branch above.
1468 */
1469 }
1470
1471 return NO_ERROR;
1472 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001473 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1474 Parcel data2, reply2;
1475 sp<IBinder> binder;
1476 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001477 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001478 return BAD_VALUE;
1479 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001480 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001481 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1482 return NO_ERROR;
1483 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001484 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1485 reply->writeStrongBinder(this);
1486 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001487 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1488 reply->writeInt32(m_id);
1489 return NO_ERROR;
1490 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1491 int32_t count;
1492 uint32_t indirect_code;
1493 sp<IBinder> binder;
1494
1495 count = data.readInt32();
1496 reply->writeInt32(m_id);
1497 reply->writeInt32(count);
1498 for (int i = 0; i < count; i++) {
1499 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001500 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001501 return BAD_VALUE;
1502 }
1503 indirect_code = data.readInt32();
1504 BinderLibTestBundle data2(&data);
1505 if (!data2.isValid()) {
1506 return BAD_VALUE;
1507 }
1508 BinderLibTestBundle reply2;
1509 binder->transact(indirect_code, data2, &reply2);
1510 reply2.appendTo(reply);
1511 }
1512 return NO_ERROR;
1513 }
1514 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1515 reply->setError(data.readInt32());
1516 return NO_ERROR;
1517 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1518 reply->writeInt32(sizeof(void *));
1519 return NO_ERROR;
1520 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1521 return NO_ERROR;
1522 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1523 m_strongRef = data.readStrongBinder();
1524 return NO_ERROR;
1525 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1526 int ret;
1527 Parcel data2, reply2;
1528 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1529 sp<IBinder> target;
1530 sp<IBinder> callback;
1531
1532 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001533 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001534 return BAD_VALUE;
1535 }
1536 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001537 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001538 return BAD_VALUE;
1539 }
1540 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001541 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001542 data2.writeInt32(ret);
1543 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1544 return NO_ERROR;
1545 }
1546 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1547 int ret;
1548 int32_t size;
1549 const void *buf;
1550 int fd;
1551
1552 fd = data.readFileDescriptor();
1553 if (fd < 0) {
1554 return BAD_VALUE;
1555 }
1556 ret = data.readInt32(&size);
1557 if (ret != NO_ERROR) {
1558 return ret;
1559 }
1560 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001561 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001562 return BAD_VALUE;
1563 }
1564 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001565 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001566 return NO_ERROR;
1567 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001568 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1569 int ret;
1570 int32_t size;
1571 const void *buf;
1572 android::base::unique_fd fd;
1573
1574 ret = data.readUniqueParcelFileDescriptor(&fd);
1575 if (ret != NO_ERROR) {
1576 return ret;
1577 }
1578 ret = data.readInt32(&size);
1579 if (ret != NO_ERROR) {
1580 return ret;
1581 }
1582 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001583 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001584 return BAD_VALUE;
1585 }
1586 ret = write(fd.get(), buf, size);
1587 if (ret != size) return UNKNOWN_ERROR;
1588 return NO_ERROR;
1589 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001590 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1591 alarm(10);
1592 return NO_ERROR;
1593 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001594 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001595 ;
1596 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001597 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001598 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001599 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001600 return NO_ERROR;
1601 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001602 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1603 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001604 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001605 return NO_ERROR;
1606 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001607 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1608 int policy = 0;
1609 sched_param param;
1610 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1611 return UNKNOWN_ERROR;
1612 }
1613 reply->writeInt32(policy);
1614 reply->writeInt32(param.sched_priority);
1615 return NO_ERROR;
1616 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001617 case BINDER_LIB_TEST_ECHO_VECTOR: {
1618 std::vector<uint64_t> vector;
1619 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001620 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001621 reply->writeUint64Vector(vector);
1622 return NO_ERROR;
1623 }
Steven Morelandf2e0a952021-11-01 18:17:23 -07001624 case BINDER_LIB_TEST_REJECT_OBJECTS: {
Martijn Coenen82c75312019-07-24 15:18:30 +02001625 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1626 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001627 case BINDER_LIB_TEST_CAN_GET_SID: {
1628 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1629 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001630 default:
1631 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001632 };
1633 }
1634
1635private:
1636 int32_t m_id;
1637 int32_t m_nextServerId;
1638 pthread_mutex_t m_serverWaitMutex;
1639 pthread_cond_t m_serverWaitCond;
1640 bool m_serverStartRequested;
1641 sp<IBinder> m_serverStarted;
1642 sp<IBinder> m_strongRef;
1643 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001644 bool m_exitOnDestroy;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001645};
1646
Martijn Coenen45b07b42017-08-09 12:07:45 +02001647int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001648{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001649 binderLibTestServiceName += String16(binderserversuffix);
1650
Steven Moreland35626652021-05-15 01:32:04 +00001651 // Testing to make sure that calls that we are serving can use getCallin*
1652 // even though we don't here.
1653 IPCThreadState::SpGuard spGuard{
1654 .address = __builtin_frame_address(0),
1655 .context = "main server thread",
1656 };
1657 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1658
Riley Andrews06b01ad2014-12-18 12:10:08 -08001659 status_t ret;
1660 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001661 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001662 {
1663 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001664
Steven Morelandbf1915b2020-07-16 22:43:02 +00001665 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1666
Steven Morelandcf03cf12020-12-04 02:58:40 +00001667 testService->setInheritRt(true);
1668
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001669 /*
1670 * Normally would also contain functionality as well, but we are only
1671 * testing the extension mechanism.
1672 */
1673 testService->setExtension(new BBinder());
1674
Martijn Coenen82c75312019-07-24 15:18:30 +02001675 // Required for test "BufRejected'
1676 testService->setRequestingSid(true);
1677
Martijn Coenen45b07b42017-08-09 12:07:45 +02001678 /*
1679 * We need this below, but can't hold a sp<> because it prevents the
1680 * node from being cleaned up automatically. It's safe in this case
1681 * because of how the tests are written.
1682 */
1683 testServicePtr = testService.get();
1684
Riley Andrews06b01ad2014-12-18 12:10:08 -08001685 if (index == 0) {
1686 ret = sm->addService(binderLibTestServiceName, testService);
1687 } else {
1688 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1689 Parcel data, reply;
1690 data.writeInt32(index);
1691 data.writeStrongBinder(testService);
1692
1693 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1694 }
1695 }
1696 write(readypipefd, &ret, sizeof(ret));
1697 close(readypipefd);
1698 //printf("%s: ret %d\n", __func__, ret);
1699 if (ret)
1700 return 1;
1701 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001702 if (usePoll) {
1703 int fd;
1704 struct epoll_event ev;
1705 int epoll_fd;
1706 IPCThreadState::self()->setupPolling(&fd);
1707 if (fd < 0) {
1708 return 1;
1709 }
1710 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1711
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001712 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001713 if (epoll_fd == -1) {
1714 return 1;
1715 }
1716
1717 ev.events = EPOLLIN;
1718 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1719 return 1;
1720 }
1721
1722 while (1) {
1723 /*
1724 * We simulate a single-threaded process using the binder poll
1725 * interface; besides handling binder commands, it can also
1726 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001727 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001728 *
1729 * processPendingCall() will then issue that transaction.
1730 */
1731 struct epoll_event events[1];
1732 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1733 if (numEvents < 0) {
1734 if (errno == EINTR) {
1735 continue;
1736 }
1737 return 1;
1738 }
1739 if (numEvents > 0) {
1740 IPCThreadState::self()->handlePolledCommands();
1741 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1742 testServicePtr->processPendingCall();
1743 }
1744 }
1745 } else {
1746 ProcessState::self()->startThreadPool();
1747 IPCThreadState::self()->joinThreadPool();
1748 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001749 //printf("%s: joinThreadPool returned\n", __func__);
1750 return 1; /* joinThreadPool should not return */
1751}
1752
1753int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001754 ExitIfWrongAbi();
1755
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001756 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001757 binderservername = argv[2];
1758 } else {
1759 binderservername = argv[0];
1760 }
1761
Martijn Coenen45b07b42017-08-09 12:07:45 +02001762 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1763 binderserversuffix = argv[5];
1764 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001765 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001766 binderserversuffix = new char[16];
1767 snprintf(binderserversuffix, 16, "%d", getpid());
1768 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001769
1770 ::testing::InitGoogleTest(&argc, argv);
1771 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1772 ProcessState::self()->startThreadPool();
1773 return RUN_ALL_TESTS();
1774}