blob: b1e17b789291013ab03ba5204a140cc0d198c342 [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
Jayant Chowdhary30700942022-01-31 14:12:40 -08001224TEST(ServiceNotifications, Unregister) {
1225 auto sm = defaultServiceManager();
1226 using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
1227 class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
1228 void onServiceRegistration(const String16 &, const sp<IBinder> &) override {}
1229 virtual ~LocalRegistrationCallbackImpl() {}
1230 };
1231 sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
1232
1233 EXPECT_EQ(sm->registerForNotifications(String16("RogerRafa"), cb), OK);
1234 EXPECT_EQ(sm->unregisterForNotifications(String16("RogerRafa"), cb), OK);
1235}
1236
Yifan Hong84bedeb2021-04-21 21:37:17 -07001237class BinderLibRpcTestBase : public BinderLibTest {
1238public:
1239 void SetUp() override {
1240 if (!base::GetBoolProperty("ro.debuggable", false)) {
1241 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1242 "non-debuggable builds.";
1243 }
1244 BinderLibTest::SetUp();
1245 }
1246
1247 std::tuple<android::base::unique_fd, unsigned int> CreateSocket() {
1248 auto rpcServer = RpcServer::make();
1249 EXPECT_NE(nullptr, rpcServer);
1250 if (rpcServer == nullptr) return {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001251 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001252 if (status_t status = rpcServer->setupInetServer("127.0.0.1", 0, &port); status != OK) {
1253 ADD_FAILURE() << "setupInetServer failed" << statusToString(status);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001254 return {};
1255 }
1256 return {rpcServer->releaseServer(), port};
1257 }
1258};
1259
Yifan Hong8b890852021-06-10 13:44:09 -07001260class BinderLibRpcTest : public BinderLibRpcTestBase {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001261
Yifan Hongbd276552022-02-28 15:28:51 -08001262// e.g. EXPECT_THAT(expr, Debuggable(StatusEq(...))
1263// If device is debuggable AND not on user builds, expects matcher.
1264// Otherwise expects INVALID_OPERATION.
1265// Debuggable + non user builds is necessary but not sufficient for setRpcClientDebug to work.
1266static Matcher<status_t> Debuggable(const Matcher<status_t> &matcher) {
1267 bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false) &&
1268 android::base::GetProperty("ro.build.type", "") != "user";
1269 return isDebuggable ? matcher : StatusEq(INVALID_OPERATION);
1270}
1271
Yifan Hong8b890852021-06-10 13:44:09 -07001272TEST_F(BinderLibRpcTest, SetRpcClientDebug) {
1273 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001274 ASSERT_TRUE(binder != nullptr);
1275 auto [socket, port] = CreateSocket();
1276 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001277 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), sp<BBinder>::make()),
1278 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001279}
1280
Yifan Hong8b890852021-06-10 13:44:09 -07001281// Tests for multiple RpcServer's on the same binder object.
1282TEST_F(BinderLibRpcTest, SetRpcClientDebugTwice) {
1283 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001284 ASSERT_TRUE(binder != nullptr);
1285
1286 auto [socket1, port1] = CreateSocket();
1287 ASSERT_TRUE(socket1.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001288 auto keepAliveBinder1 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001289 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), keepAliveBinder1),
1290 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001291
1292 auto [socket2, port2] = CreateSocket();
1293 ASSERT_TRUE(socket2.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001294 auto keepAliveBinder2 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001295 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), keepAliveBinder2),
1296 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001297}
1298
Yifan Hong8b890852021-06-10 13:44:09 -07001299// Negative tests for RPC APIs on IBinder. Call should fail in the same way on both remote and
1300// local binders.
1301class BinderLibRpcTestP : public BinderLibRpcTestBase, public WithParamInterface<bool> {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001302public:
1303 sp<IBinder> GetService() {
1304 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1305 }
1306 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1307 return info.param ? "remote" : "local";
1308 }
1309};
1310
Yifan Hong8b890852021-06-10 13:44:09 -07001311TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoFd) {
1312 auto binder = GetService();
1313 ASSERT_TRUE(binder != nullptr);
1314 EXPECT_THAT(binder->setRpcClientDebug(android::base::unique_fd(), sp<BBinder>::make()),
Yifan Hongbd276552022-02-28 15:28:51 -08001315 Debuggable(StatusEq(BAD_VALUE)));
Yifan Hong8b890852021-06-10 13:44:09 -07001316}
1317
1318TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoKeepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001319 auto binder = GetService();
1320 ASSERT_TRUE(binder != nullptr);
1321 auto [socket, port] = CreateSocket();
1322 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001323 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), nullptr),
1324 Debuggable(StatusEq(UNEXPECTED_NULL)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001325}
Yifan Hong8b890852021-06-10 13:44:09 -07001326INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTestP, testing::Bool(),
1327 BinderLibRpcTestP::ParamToString);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001328
Yifan Hong543edcd2021-05-18 19:47:30 -07001329class BinderLibTestService : public BBinder {
1330public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001331 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1332 : m_id(id),
1333 m_nextServerId(id + 1),
1334 m_serverStartRequested(false),
1335 m_callback(nullptr),
1336 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001337 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1338 pthread_cond_init(&m_serverWaitCond, nullptr);
1339 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001340 ~BinderLibTestService() {
1341 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1342 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001343
Yifan Hong543edcd2021-05-18 19:47:30 -07001344 void processPendingCall() {
1345 if (m_callback != nullptr) {
1346 Parcel data;
1347 data.writeInt32(NO_ERROR);
1348 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1349 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001350 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001351 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001352
Yifan Hong543edcd2021-05-18 19:47:30 -07001353 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1354 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001355 // TODO(b/182914638): also checks getCallingUid() for RPC
1356 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001357 return PERMISSION_DENIED;
1358 }
1359 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001360 case BINDER_LIB_TEST_REGISTER_SERVER: {
1361 int32_t id;
1362 sp<IBinder> binder;
1363 id = data.readInt32();
1364 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001365 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001366 return BAD_VALUE;
1367 }
1368
Yifan Hong543edcd2021-05-18 19:47:30 -07001369 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001370
1371 pthread_mutex_lock(&m_serverWaitMutex);
1372 if (m_serverStartRequested) {
1373 m_serverStartRequested = false;
1374 m_serverStarted = binder;
1375 pthread_cond_signal(&m_serverWaitCond);
1376 }
1377 pthread_mutex_unlock(&m_serverWaitMutex);
1378 return NO_ERROR;
1379 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001380 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001381 case BINDER_LIB_TEST_ADD_SERVER: {
1382 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001383 int serverid;
1384
1385 if (m_id != 0) {
1386 return INVALID_OPERATION;
1387 }
1388 pthread_mutex_lock(&m_serverWaitMutex);
1389 if (m_serverStartRequested) {
1390 ret = -EBUSY;
1391 } else {
1392 serverid = m_nextServerId++;
1393 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001394 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001395
1396 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001397 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398 pthread_mutex_lock(&m_serverWaitMutex);
1399 }
1400 if (ret > 0) {
1401 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001402 struct timespec ts;
1403 clock_gettime(CLOCK_REALTIME, &ts);
1404 ts.tv_sec += 5;
1405 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001406 }
1407 if (m_serverStartRequested) {
1408 m_serverStartRequested = false;
1409 ret = -ETIMEDOUT;
1410 } else {
1411 reply->writeStrongBinder(m_serverStarted);
1412 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001413 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001414 ret = NO_ERROR;
1415 }
1416 } else if (ret >= 0) {
1417 m_serverStartRequested = false;
1418 ret = UNKNOWN_ERROR;
1419 }
1420 pthread_mutex_unlock(&m_serverWaitMutex);
1421 return ret;
1422 }
Steven Moreland35626652021-05-15 01:32:04 +00001423 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1424 IPCThreadState::SpGuard spGuard{
1425 .address = __builtin_frame_address(0),
1426 .context = "GuardInBinderTransaction",
1427 };
1428 const IPCThreadState::SpGuard *origGuard =
1429 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1430
1431 // if the guard works, this should abort
1432 (void)IPCThreadState::self()->getCallingPid();
1433
1434 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1435 return NO_ERROR;
1436 }
1437
Marco Ballesio7ee17572020-09-08 10:30:03 -07001438 case BINDER_LIB_TEST_GETPID:
1439 reply->writeInt32(getpid());
1440 return NO_ERROR;
1441 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1442 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001443 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001444 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001445 // oneway error codes should be ignored
1446 if (flags & TF_ONE_WAY) {
1447 return UNKNOWN_ERROR;
1448 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001449 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001450 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1451 // Note: this transaction is only designed for use with a
1452 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001453 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001454 // A callback was already pending; this means that
1455 // we received a second call while still processing
1456 // the first one. Fail the test.
1457 sp<IBinder> callback = data.readStrongBinder();
1458 Parcel data2;
1459 data2.writeInt32(UNKNOWN_ERROR);
1460
Yi Kong91635562018-06-07 14:38:36 -07001461 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001462 } else {
1463 m_callback = data.readStrongBinder();
1464 int32_t delayUs = data.readInt32();
1465 /*
1466 * It's necessary that we sleep here, so the next
1467 * transaction the caller makes will be queued to
1468 * the async queue.
1469 */
1470 usleep(delayUs);
1471
1472 /*
1473 * Now when we return, libbinder will tell the kernel
1474 * we are done with this transaction, and the kernel
1475 * can move the queued transaction to either the
1476 * thread todo worklist (for kernels without the fix),
1477 * or the proc todo worklist. In case of the former,
1478 * the next outbound call will pick up the pending
1479 * transaction, which leads to undesired reentrant
1480 * behavior. This is caught in the if() branch above.
1481 */
1482 }
1483
1484 return NO_ERROR;
1485 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001486 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1487 Parcel data2, reply2;
1488 sp<IBinder> binder;
1489 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001490 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001491 return BAD_VALUE;
1492 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001493 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001494 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1495 return NO_ERROR;
1496 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001497 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1498 reply->writeStrongBinder(this);
1499 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001500 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1501 reply->writeInt32(m_id);
1502 return NO_ERROR;
1503 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1504 int32_t count;
1505 uint32_t indirect_code;
1506 sp<IBinder> binder;
1507
1508 count = data.readInt32();
1509 reply->writeInt32(m_id);
1510 reply->writeInt32(count);
1511 for (int i = 0; i < count; i++) {
1512 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001513 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001514 return BAD_VALUE;
1515 }
1516 indirect_code = data.readInt32();
1517 BinderLibTestBundle data2(&data);
1518 if (!data2.isValid()) {
1519 return BAD_VALUE;
1520 }
1521 BinderLibTestBundle reply2;
1522 binder->transact(indirect_code, data2, &reply2);
1523 reply2.appendTo(reply);
1524 }
1525 return NO_ERROR;
1526 }
1527 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1528 reply->setError(data.readInt32());
1529 return NO_ERROR;
1530 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1531 reply->writeInt32(sizeof(void *));
1532 return NO_ERROR;
1533 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1534 return NO_ERROR;
1535 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1536 m_strongRef = data.readStrongBinder();
1537 return NO_ERROR;
1538 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1539 int ret;
1540 Parcel data2, reply2;
1541 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1542 sp<IBinder> target;
1543 sp<IBinder> callback;
1544
1545 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001546 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001547 return BAD_VALUE;
1548 }
1549 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001550 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001551 return BAD_VALUE;
1552 }
1553 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001554 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001555 data2.writeInt32(ret);
1556 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1557 return NO_ERROR;
1558 }
1559 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1560 int ret;
1561 int32_t size;
1562 const void *buf;
1563 int fd;
1564
1565 fd = data.readFileDescriptor();
1566 if (fd < 0) {
1567 return BAD_VALUE;
1568 }
1569 ret = data.readInt32(&size);
1570 if (ret != NO_ERROR) {
1571 return ret;
1572 }
1573 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001574 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001575 return BAD_VALUE;
1576 }
1577 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001578 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001579 return NO_ERROR;
1580 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001581 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1582 int ret;
1583 int32_t size;
1584 const void *buf;
1585 android::base::unique_fd fd;
1586
1587 ret = data.readUniqueParcelFileDescriptor(&fd);
1588 if (ret != NO_ERROR) {
1589 return ret;
1590 }
1591 ret = data.readInt32(&size);
1592 if (ret != NO_ERROR) {
1593 return ret;
1594 }
1595 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001596 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001597 return BAD_VALUE;
1598 }
1599 ret = write(fd.get(), buf, size);
1600 if (ret != size) return UNKNOWN_ERROR;
1601 return NO_ERROR;
1602 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001603 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1604 alarm(10);
1605 return NO_ERROR;
1606 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001607 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001608 ;
1609 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001610 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001611 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001612 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001613 return NO_ERROR;
1614 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001615 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1616 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001617 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001618 return NO_ERROR;
1619 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001620 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1621 int policy = 0;
1622 sched_param param;
1623 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1624 return UNKNOWN_ERROR;
1625 }
1626 reply->writeInt32(policy);
1627 reply->writeInt32(param.sched_priority);
1628 return NO_ERROR;
1629 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001630 case BINDER_LIB_TEST_ECHO_VECTOR: {
1631 std::vector<uint64_t> vector;
1632 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001633 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001634 reply->writeUint64Vector(vector);
1635 return NO_ERROR;
1636 }
Steven Morelandf2e0a952021-11-01 18:17:23 -07001637 case BINDER_LIB_TEST_REJECT_OBJECTS: {
Martijn Coenen82c75312019-07-24 15:18:30 +02001638 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1639 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001640 case BINDER_LIB_TEST_CAN_GET_SID: {
1641 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1642 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001643 default:
1644 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001645 };
1646 }
1647
1648private:
1649 int32_t m_id;
1650 int32_t m_nextServerId;
1651 pthread_mutex_t m_serverWaitMutex;
1652 pthread_cond_t m_serverWaitCond;
1653 bool m_serverStartRequested;
1654 sp<IBinder> m_serverStarted;
1655 sp<IBinder> m_strongRef;
1656 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001657 bool m_exitOnDestroy;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001658};
1659
Martijn Coenen45b07b42017-08-09 12:07:45 +02001660int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001661{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001662 binderLibTestServiceName += String16(binderserversuffix);
1663
Steven Moreland35626652021-05-15 01:32:04 +00001664 // Testing to make sure that calls that we are serving can use getCallin*
1665 // even though we don't here.
1666 IPCThreadState::SpGuard spGuard{
1667 .address = __builtin_frame_address(0),
1668 .context = "main server thread",
1669 };
1670 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1671
Riley Andrews06b01ad2014-12-18 12:10:08 -08001672 status_t ret;
1673 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001674 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001675 {
1676 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001677
Steven Morelandbf1915b2020-07-16 22:43:02 +00001678 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1679
Steven Morelandcf03cf12020-12-04 02:58:40 +00001680 testService->setInheritRt(true);
1681
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001682 /*
1683 * Normally would also contain functionality as well, but we are only
1684 * testing the extension mechanism.
1685 */
1686 testService->setExtension(new BBinder());
1687
Martijn Coenen82c75312019-07-24 15:18:30 +02001688 // Required for test "BufRejected'
1689 testService->setRequestingSid(true);
1690
Martijn Coenen45b07b42017-08-09 12:07:45 +02001691 /*
1692 * We need this below, but can't hold a sp<> because it prevents the
1693 * node from being cleaned up automatically. It's safe in this case
1694 * because of how the tests are written.
1695 */
1696 testServicePtr = testService.get();
1697
Riley Andrews06b01ad2014-12-18 12:10:08 -08001698 if (index == 0) {
1699 ret = sm->addService(binderLibTestServiceName, testService);
1700 } else {
1701 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1702 Parcel data, reply;
1703 data.writeInt32(index);
1704 data.writeStrongBinder(testService);
1705
1706 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1707 }
1708 }
1709 write(readypipefd, &ret, sizeof(ret));
1710 close(readypipefd);
1711 //printf("%s: ret %d\n", __func__, ret);
1712 if (ret)
1713 return 1;
1714 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001715 if (usePoll) {
1716 int fd;
1717 struct epoll_event ev;
1718 int epoll_fd;
1719 IPCThreadState::self()->setupPolling(&fd);
1720 if (fd < 0) {
1721 return 1;
1722 }
1723 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1724
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001725 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001726 if (epoll_fd == -1) {
1727 return 1;
1728 }
1729
1730 ev.events = EPOLLIN;
1731 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1732 return 1;
1733 }
1734
1735 while (1) {
1736 /*
1737 * We simulate a single-threaded process using the binder poll
1738 * interface; besides handling binder commands, it can also
1739 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001740 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001741 *
1742 * processPendingCall() will then issue that transaction.
1743 */
1744 struct epoll_event events[1];
1745 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1746 if (numEvents < 0) {
1747 if (errno == EINTR) {
1748 continue;
1749 }
1750 return 1;
1751 }
1752 if (numEvents > 0) {
1753 IPCThreadState::self()->handlePolledCommands();
1754 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1755 testServicePtr->processPendingCall();
1756 }
1757 }
1758 } else {
1759 ProcessState::self()->startThreadPool();
1760 IPCThreadState::self()->joinThreadPool();
1761 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001762 //printf("%s: joinThreadPool returned\n", __func__);
1763 return 1; /* joinThreadPool should not return */
1764}
1765
1766int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001767 ExitIfWrongAbi();
1768
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001769 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001770 binderservername = argv[2];
1771 } else {
1772 binderservername = argv[0];
1773 }
1774
Martijn Coenen45b07b42017-08-09 12:07:45 +02001775 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1776 binderserversuffix = argv[5];
1777 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001778 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001779 binderserversuffix = new char[16];
1780 snprintf(binderserversuffix, 16, "%d", getpid());
1781 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001782
1783 ::testing::InitGoogleTest(&argc, argv);
1784 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1785 ProcessState::self()->startThreadPool();
1786 return RUN_ALL_TESTS();
1787}