blob: 459bb2922658348d9fa0b54e461d6016bd8adbe9 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
Marco Ballesio7ee17572020-09-08 10:30:03 -070019#include <fstream>
Riley Andrews06b01ad2014-12-18 12:10:08 -080020#include <poll.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
Steven Morelandd7088702021-01-13 00:27:00 +000024#include <thread>
Riley Andrews06b01ad2014-12-18 12:10:08 -080025
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070026#include <gmock/gmock.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080027#include <gtest/gtest.h>
28
29#include <binder/Binder.h>
30#include <binder/IBinder.h>
31#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Steven Morelandd7088702021-01-13 00:27:00 +000033#include <binder/ParcelRef.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080034
Steven Morelandcf03cf12020-12-04 02:58:40 +000035#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020036#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080037#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020038
Steven Moreland6ba5a252021-05-04 22:49:00 +000039#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070040#include "binderAbiHelper.h"
41
Riley Andrews06b01ad2014-12-18 12:10:08 -080042#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
43
44using namespace android;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070045using testing::Not;
46
47// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
48MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
49 *result_listener << statusToString(arg);
50 return expected == arg;
51}
Riley Andrews06b01ad2014-12-18 12:10:08 -080052
Sherry Yang336cdd32017-07-24 14:12:27 -070053static ::testing::AssertionResult IsPageAligned(void *buf) {
54 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
55 return ::testing::AssertionSuccess();
56 else
57 return ::testing::AssertionFailure() << buf << " is not page aligned";
58}
59
Riley Andrews06b01ad2014-12-18 12:10:08 -080060static testing::Environment* binder_env;
61static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070062static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080063static char binderserverarg[] = "--binderserver";
64
Steven Morelandbf1915b2020-07-16 22:43:02 +000065static constexpr int kSchedPolicy = SCHED_RR;
66static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000067static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000068
Riley Andrews06b01ad2014-12-18 12:10:08 -080069static String16 binderLibTestServiceName = String16("test.binderLib");
70
71enum BinderLibTestTranscationCode {
72 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
73 BINDER_LIB_TEST_REGISTER_SERVER,
74 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020075 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080076 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070077 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020078 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080079 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070080 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080081 BINDER_LIB_TEST_GET_ID_TRANSACTION,
82 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
83 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
84 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
85 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
86 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
87 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090088 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080089 BINDER_LIB_TEST_EXIT_TRANSACTION,
90 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
91 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070092 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010093 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000094 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070095 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
96 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080097 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020098 BINDER_LIB_TEST_REJECT_BUF,
Steven Moreland254e8ef2021-04-19 22:28:50 +000099 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800100};
101
Martijn Coenen45b07b42017-08-09 12:07:45 +0200102pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800103{
104 int ret;
105 pid_t pid;
106 status_t status;
107 int pipefd[2];
108 char stri[16];
109 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200110 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800111 char *childargv[] = {
112 binderservername,
113 binderserverarg,
114 stri,
115 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200116 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700117 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700118 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800119 };
120
121 ret = pipe(pipefd);
122 if (ret < 0)
123 return ret;
124
125 snprintf(stri, sizeof(stri), "%d", arg2);
126 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200127 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800128
129 pid = fork();
130 if (pid == -1)
131 return pid;
132 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800133 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800134 close(pipefd[0]);
135 execv(binderservername, childargv);
136 status = -errno;
137 write(pipefd[1], &status, sizeof(status));
138 fprintf(stderr, "execv failed, %s\n", strerror(errno));
139 _exit(EXIT_FAILURE);
140 }
141 close(pipefd[1]);
142 ret = read(pipefd[0], &status, sizeof(status));
143 //printf("pipe read returned %d, status %d\n", ret, status);
144 close(pipefd[0]);
145 if (ret == sizeof(status)) {
146 ret = status;
147 } else {
148 kill(pid, SIGKILL);
149 if (ret >= 0) {
150 ret = NO_INIT;
151 }
152 }
153 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700154 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800155 return ret;
156 }
157 return pid;
158}
159
160class BinderLibTestEnv : public ::testing::Environment {
161 public:
162 BinderLibTestEnv() {}
163 sp<IBinder> getServer(void) {
164 return m_server;
165 }
166
167 private:
168 virtual void SetUp() {
169 m_serverpid = start_server_process(0);
170 //printf("m_serverpid %d\n", m_serverpid);
171 ASSERT_GT(m_serverpid, 0);
172
173 sp<IServiceManager> sm = defaultServiceManager();
174 //printf("%s: pid %d, get service\n", __func__, m_pid);
175 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700176 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800177 //printf("%s: pid %d, get service done\n", __func__, m_pid);
178 }
179 virtual void TearDown() {
180 status_t ret;
181 Parcel data, reply;
182 int exitStatus;
183 pid_t pid;
184
185 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700186 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800187 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
188 EXPECT_EQ(0, ret);
189 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
190 EXPECT_EQ(0, ret);
191 }
192 if (m_serverpid > 0) {
193 //printf("wait for %d\n", m_pids[i]);
194 pid = wait(&exitStatus);
195 EXPECT_EQ(m_serverpid, pid);
196 EXPECT_TRUE(WIFEXITED(exitStatus));
197 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
198 }
199 }
200
201 pid_t m_serverpid;
202 sp<IBinder> m_server;
203};
204
205class BinderLibTest : public ::testing::Test {
206 public:
207 virtual void SetUp() {
208 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000209 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800210 }
211 virtual void TearDown() {
212 }
213 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200214 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800215 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 int32_t id;
217 Parcel data, reply;
218 sp<IBinder> binder;
219
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700220 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800221
Yi Kong91635562018-06-07 14:38:36 -0700222 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800223 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700224 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700225 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800226 if (idPtr)
227 *idPtr = id;
228 return binder;
229 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200230
Yi Kong91635562018-06-07 14:38:36 -0700231 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200232 {
233 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
234 }
235
Yi Kong91635562018-06-07 14:38:36 -0700236 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200237 {
238 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
239 }
240
Riley Andrews06b01ad2014-12-18 12:10:08 -0800241 void waitForReadData(int fd, int timeout_ms) {
242 int ret;
243 pollfd pfd = pollfd();
244
245 pfd.fd = fd;
246 pfd.events = POLLIN;
247 ret = poll(&pfd, 1, timeout_ms);
248 EXPECT_EQ(1, ret);
249 }
250
251 sp<IBinder> m_server;
252};
253
254class BinderLibTestBundle : public Parcel
255{
256 public:
257 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800258 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800259 int32_t mark;
260 int32_t bundleLen;
261 size_t pos;
262
263 if (source->readInt32(&mark))
264 return;
265 if (mark != MARK_START)
266 return;
267 if (source->readInt32(&bundleLen))
268 return;
269 pos = source->dataPosition();
270 if (Parcel::appendFrom(source, pos, bundleLen))
271 return;
272 source->setDataPosition(pos + bundleLen);
273 if (source->readInt32(&mark))
274 return;
275 if (mark != MARK_END)
276 return;
277 m_isValid = true;
278 setDataPosition(0);
279 }
280 void appendTo(Parcel *dest) {
281 dest->writeInt32(MARK_START);
282 dest->writeInt32(dataSize());
283 dest->appendFrom(this, 0, dataSize());
284 dest->writeInt32(MARK_END);
285 };
286 bool isValid(void) {
287 return m_isValid;
288 }
289 private:
290 enum {
291 MARK_START = B_PACK_CHARS('B','T','B','S'),
292 MARK_END = B_PACK_CHARS('B','T','B','E'),
293 };
294 bool m_isValid;
295};
296
297class BinderLibTestEvent
298{
299 public:
300 BinderLibTestEvent(void)
301 : m_eventTriggered(false)
302 {
Yi Kong91635562018-06-07 14:38:36 -0700303 pthread_mutex_init(&m_waitMutex, nullptr);
304 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 }
306 int waitEvent(int timeout_s)
307 {
308 int ret;
309 pthread_mutex_lock(&m_waitMutex);
310 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800311 struct timespec ts;
312 clock_gettime(CLOCK_REALTIME, &ts);
313 ts.tv_sec += timeout_s;
314 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800315 }
316 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
317 pthread_mutex_unlock(&m_waitMutex);
318 return ret;
319 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200320 pthread_t getTriggeringThread()
321 {
322 return m_triggeringThread;
323 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800324 protected:
325 void triggerEvent(void) {
326 pthread_mutex_lock(&m_waitMutex);
327 pthread_cond_signal(&m_waitCond);
328 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200329 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800330 pthread_mutex_unlock(&m_waitMutex);
331 };
332 private:
333 pthread_mutex_t m_waitMutex;
334 pthread_cond_t m_waitCond;
335 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200336 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800337};
338
339class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
340{
341 public:
342 BinderLibTestCallBack()
343 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700344 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800345 {
346 }
347 status_t getResult(void)
348 {
349 return m_result;
350 }
351
352 private:
353 virtual status_t onTransact(uint32_t code,
354 const Parcel& data, Parcel* reply,
355 uint32_t flags = 0)
356 {
357 (void)reply;
358 (void)flags;
359 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200360 case BINDER_LIB_TEST_CALL_BACK: {
361 status_t status = data.readInt32(&m_result);
362 if (status != NO_ERROR) {
363 m_result = status;
364 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800365 triggerEvent();
366 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200367 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700368 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
369 sp<IBinder> server;
370 int ret;
371 const uint8_t *buf = data.data();
372 size_t size = data.dataSize();
373 if (m_prev_end) {
374 /* 64-bit kernel needs at most 8 bytes to align buffer end */
375 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
376 } else {
377 EXPECT_TRUE(IsPageAligned((void *)buf));
378 }
379
380 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
381
382 if (size > 0) {
383 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
384 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
385 data, reply);
386 EXPECT_EQ(NO_ERROR, ret);
387 }
388 return NO_ERROR;
389 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800390 default:
391 return UNKNOWN_TRANSACTION;
392 }
393 }
394
395 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700396 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800397};
398
399class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
400{
401 private:
402 virtual void binderDied(const wp<IBinder>& who) {
403 (void)who;
404 triggerEvent();
405 };
406};
407
408TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800409 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700410 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
411 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800412}
413
Steven Moreland80844f72020-12-12 02:06:08 +0000414TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000415 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700416 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
417 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000418}
419
Steven Morelandf183fdd2020-10-27 00:12:12 +0000420TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000421 Parcel data, reply;
422 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700423 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
424 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000425}
426
Marco Ballesio7ee17572020-09-08 10:30:03 -0700427TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700428 Parcel data, reply, replypid;
429 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
430
431 //Pass test on devices where the freezer is not supported
432 if (freezer_file.fail()) {
433 GTEST_SKIP();
434 return;
435 }
436
437 std::string freezer_enabled;
438 std::getline(freezer_file, freezer_enabled);
439
440 //Pass test on devices where the freezer is disabled
441 if (freezer_enabled != "1") {
442 GTEST_SKIP();
443 return;
444 }
445
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700446 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700447 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700448 for (int i = 0; i < 10; i++) {
449 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
450 }
451 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
452 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
453 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
454 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700455
456 bool sync_received, async_received;
457
458 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
459 &async_received));
460
461 EXPECT_EQ(sync_received, 1);
462 EXPECT_EQ(async_received, 0);
463
Marco Ballesio7ee17572020-09-08 10:30:03 -0700464 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
465 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
466}
467
Riley Andrews06b01ad2014-12-18 12:10:08 -0800468TEST_F(BinderLibTest, SetError) {
469 int32_t testValue[] = { 0, -123, 123 };
470 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800471 Parcel data, reply;
472 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700473 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
474 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800475 }
476}
477
478TEST_F(BinderLibTest, GetId) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800479 int32_t id;
480 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700481 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply),
482 StatusEq(NO_ERROR));
483 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800484 EXPECT_EQ(0, id);
485}
486
487TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800488 int32_t ptrsize;
489 Parcel data, reply;
490 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700491 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700492 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
493 StatusEq(NO_ERROR));
494 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800495 RecordProperty("TestPtrSize", sizeof(void *));
496 RecordProperty("ServerPtrSize", sizeof(void *));
497}
498
499TEST_F(BinderLibTest, IndirectGetId2)
500{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800501 int32_t id;
502 int32_t count;
503 Parcel data, reply;
504 int32_t serverId[3];
505
506 data.writeInt32(ARRAY_SIZE(serverId));
507 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
508 sp<IBinder> server;
509 BinderLibTestBundle datai;
510
511 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700512 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800513 data.writeStrongBinder(server);
514 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
515 datai.appendTo(&data);
516 }
517
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700518 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
519 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800520
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700521 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800522 EXPECT_EQ(0, id);
523
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700524 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700525 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800526
527 for (size_t i = 0; i < (size_t)count; i++) {
528 BinderLibTestBundle replyi(&reply);
529 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700530 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800531 EXPECT_EQ(serverId[i], id);
532 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
533 }
534
535 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
536}
537
538TEST_F(BinderLibTest, IndirectGetId3)
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 BinderLibTestBundle datai2;
550
551 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700552 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553 data.writeStrongBinder(server);
554 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
555
556 datai.writeInt32(1);
557 datai.writeStrongBinder(m_server);
558 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
559 datai2.appendTo(&datai);
560
561 datai.appendTo(&data);
562 }
563
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700564 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
565 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800566
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700567 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800568 EXPECT_EQ(0, id);
569
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700570 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700571 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800572
573 for (size_t i = 0; i < (size_t)count; i++) {
574 int32_t counti;
575
576 BinderLibTestBundle replyi(&reply);
577 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700578 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800579 EXPECT_EQ(serverId[i], id);
580
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700581 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800582 EXPECT_EQ(1, counti);
583
584 BinderLibTestBundle replyi2(&replyi);
585 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700586 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800587 EXPECT_EQ(0, id);
588 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
589
590 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
591 }
592
593 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
594}
595
596TEST_F(BinderLibTest, CallBack)
597{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800598 Parcel data, reply;
599 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
600 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700601 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
602 StatusEq(NO_ERROR));
603 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
604 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605}
606
607TEST_F(BinderLibTest, AddServer)
608{
609 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700610 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800611}
612
Riley Andrews06b01ad2014-12-18 12:10:08 -0800613TEST_F(BinderLibTest, DeathNotificationStrongRef)
614{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800615 sp<IBinder> sbinder;
616
617 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
618
619 {
620 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700621 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700622 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800623 sbinder = binder;
624 }
625 {
626 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700627 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
628 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800629 }
630 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700631 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
632 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800633}
634
635TEST_F(BinderLibTest, DeathNotificationMultiple)
636{
637 status_t ret;
638 const int clientcount = 2;
639 sp<IBinder> target;
640 sp<IBinder> linkedclient[clientcount];
641 sp<BinderLibTestCallBack> callBack[clientcount];
642 sp<IBinder> passiveclient[clientcount];
643
644 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700645 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800646 for (int i = 0; i < clientcount; i++) {
647 {
648 Parcel data, reply;
649
650 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700651 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800652 callBack[i] = new BinderLibTestCallBack();
653 data.writeStrongBinder(target);
654 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700655 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
656 &reply, TF_ONE_WAY),
657 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800658 }
659 {
660 Parcel data, reply;
661
662 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700663 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800664 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700665 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
666 &reply, TF_ONE_WAY),
667 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800668 }
669 }
670 {
671 Parcel data, reply;
672 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
673 EXPECT_EQ(0, ret);
674 }
675
676 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700677 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
678 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800679 }
680}
681
Martijn Coenenf7100e42017-07-31 12:14:09 +0200682TEST_F(BinderLibTest, DeathNotificationThread)
683{
684 status_t ret;
685 sp<BinderLibTestCallBack> callback;
686 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700687 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200688 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700689 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200690
691 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
692
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700693 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200694
695 {
696 Parcel data, reply;
697 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
698 EXPECT_EQ(0, ret);
699 }
700
701 /* Make sure it's dead */
702 testDeathRecipient->waitEvent(5);
703
704 /* Now, pass the ref to another process and ask that process to
705 * call linkToDeath() on it, and wait for a response. This tests
706 * two things:
707 * 1) You still get death notifications when calling linkToDeath()
708 * on a ref that is already dead when it was passed to you.
709 * 2) That death notifications are not directly pushed to the thread
710 * registering them, but to the threadpool (proc workqueue) instead.
711 *
712 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
713 * is blocked on a condition variable waiting for the death notification to be
714 * called; therefore, that thread is not available for handling proc work.
715 * So, if the death notification was pushed to the thread workqueue, the callback
716 * would never be called, and the test would timeout and fail.
717 *
718 * Note that we can't do this part of the test from this thread itself, because
719 * the binder driver would only push death notifications to the thread if
720 * it is a looper thread, which this thread is not.
721 *
722 * See b/23525545 for details.
723 */
724 {
725 Parcel data, reply;
726
727 callback = new BinderLibTestCallBack();
728 data.writeStrongBinder(target);
729 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700730 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
731 TF_ONE_WAY),
732 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200733 }
734
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700735 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
736 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200737}
738
Riley Andrews06b01ad2014-12-18 12:10:08 -0800739TEST_F(BinderLibTest, PassFile) {
740 int ret;
741 int pipefd[2];
742 uint8_t buf[1] = { 0 };
743 uint8_t write_value = 123;
744
745 ret = pipe2(pipefd, O_NONBLOCK);
746 ASSERT_EQ(0, ret);
747
748 {
749 Parcel data, reply;
750 uint8_t writebuf[1] = { write_value };
751
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700752 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800753
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700754 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800755
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700756 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800757
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700758 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
759 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800760 }
761
762 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700763 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800764 EXPECT_EQ(write_value, buf[0]);
765
766 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
767
768 ret = read(pipefd[0], buf, sizeof(buf));
769 EXPECT_EQ(0, ret);
770
771 close(pipefd[0]);
772}
773
Ryo Hashimotobf551892018-05-31 16:58:35 +0900774TEST_F(BinderLibTest, PassParcelFileDescriptor) {
775 const int datasize = 123;
776 std::vector<uint8_t> writebuf(datasize);
777 for (size_t i = 0; i < writebuf.size(); ++i) {
778 writebuf[i] = i;
779 }
780
781 android::base::unique_fd read_end, write_end;
782 {
783 int pipefd[2];
784 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
785 read_end.reset(pipefd[0]);
786 write_end.reset(pipefd[1]);
787 }
788 {
789 Parcel data;
790 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
791 write_end.reset();
792 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
793 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
794
795 Parcel reply;
796 EXPECT_EQ(NO_ERROR,
797 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
798 &reply));
799 }
800 std::vector<uint8_t> readbuf(datasize);
801 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
802 EXPECT_EQ(writebuf, readbuf);
803
804 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
805
806 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
807}
808
Riley Andrews06b01ad2014-12-18 12:10:08 -0800809TEST_F(BinderLibTest, PromoteLocal) {
810 sp<IBinder> strong = new BBinder();
811 wp<IBinder> weak = strong;
812 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700813 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800814 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700815 strong = nullptr;
816 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800817 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700818 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800819}
820
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700821TEST_F(BinderLibTest, LocalGetExtension) {
822 sp<BBinder> binder = new BBinder();
823 sp<IBinder> ext = new BBinder();
824 binder->setExtension(ext);
825 EXPECT_EQ(ext, binder->getExtension());
826}
827
828TEST_F(BinderLibTest, RemoteGetExtension) {
829 sp<IBinder> server = addServer();
830 ASSERT_TRUE(server != nullptr);
831
832 sp<IBinder> extension;
833 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
834 ASSERT_NE(nullptr, extension.get());
835
836 EXPECT_EQ(NO_ERROR, extension->pingBinder());
837}
838
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700839TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700840 Parcel data, reply;
841
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700842 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
843 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700844
845 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700846 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800847 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
848 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
849 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
850 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700851}
852
Connor O'Brien52be2c92016-09-20 14:18:08 -0700853TEST_F(BinderLibTest, FreedBinder) {
854 status_t ret;
855
856 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700857 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700858
859 __u32 freedHandle;
860 wp<IBinder> keepFreedBinder;
861 {
862 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700863 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
864 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700865 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
866 freedHandle = freed->handle;
867 /* Add a weak ref to the freed binder so the driver does not
868 * delete its reference to it - otherwise the transaction
869 * fails regardless of whether the driver is fixed.
870 */
Steven Morelande171d622019-07-17 16:06:01 -0700871 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700872 }
Steven Morelande171d622019-07-17 16:06:01 -0700873 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700874 {
875 Parcel data, reply;
876 data.writeStrongBinder(server);
877 /* Replace original handle with handle to the freed binder */
878 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
879 __u32 oldHandle = strong->handle;
880 strong->handle = freedHandle;
881 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
882 /* Returns DEAD_OBJECT (-32) if target crashes and
883 * FAILED_TRANSACTION if the driver rejects the invalid
884 * object.
885 */
886 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
887 /* Restore original handle so parcel destructor does not use
888 * the wrong handle.
889 */
890 strong->handle = oldHandle;
891 }
892}
893
Steven Morelandd7088702021-01-13 00:27:00 +0000894TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
895 sp<IBinder> server = addServer();
896 ASSERT_TRUE(server != nullptr);
897
898 Parcel data;
899 sp<ParcelRef> reply = ParcelRef::create();
900
901 // when we have a Parcel which is deleted on another thread, if it gets
902 // deleted, it will tell the kernel this, and it will drop strong references
903 // to binder, so that we can't BR_ACQUIRE would fail
904 IPCThreadState::self()->createTransactionReference(reply.get());
905 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
906 data,
907 reply.get()));
908
909 // we have sp to binder, but it is not actually acquired by kernel, the
910 // transaction is sitting on an out buffer
911 sp<IBinder> binder = reply->readStrongBinder();
912
913 std::thread([&] {
914 // without the transaction reference, this would cause the Parcel to be
915 // deallocated before the first thread flushes BR_ACQUIRE
916 reply = nullptr;
917 IPCThreadState::self()->flushCommands();
918 }).join();
919
920 ASSERT_NE(nullptr, binder);
921 ASSERT_EQ(NO_ERROR, binder->pingBinder());
922}
923
Sherry Yang336cdd32017-07-24 14:12:27 -0700924TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700925 Parcel data, reply;
926 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
927 for (int i = 0; i < 2; i++) {
928 BinderLibTestBundle datai;
929 datai.appendFrom(&data, 0, data.dataSize());
930
931 data.freeData();
932 data.writeInt32(1);
933 data.writeStrongBinder(callBack);
934 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
935
936 datai.appendTo(&data);
937 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700938 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
939 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700940}
941
Martijn Coenen45b07b42017-08-09 12:07:45 +0200942TEST_F(BinderLibTest, OnewayQueueing)
943{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200944 Parcel data, data2;
945
946 sp<IBinder> pollServer = addPollServer();
947
948 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
949 data.writeStrongBinder(callBack);
950 data.writeInt32(500000); // delay in us before calling back
951
952 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
953 data2.writeStrongBinder(callBack2);
954 data2.writeInt32(0); // delay in us
955
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700956 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
957 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200958
959 // The delay ensures that this second transaction will end up on the async_todo list
960 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700961 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
962 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200963
964 // The server will ensure that the two transactions are handled in the expected order;
965 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700966 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
967 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200968
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700969 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
970 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200971}
972
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100973TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
974{
975 status_t ret;
976 Parcel data, reply;
977 data.writeInterfaceToken(binderLibTestServiceName);
978 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
979 EXPECT_EQ(-1, reply.readInt32());
980 EXPECT_EQ(NO_ERROR, ret);
981}
982
983TEST_F(BinderLibTest, WorkSourceSet)
984{
985 status_t ret;
986 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000987 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000988 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100989 data.writeInterfaceToken(binderLibTestServiceName);
990 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
991 EXPECT_EQ(100, reply.readInt32());
992 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000993 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
994 EXPECT_EQ(NO_ERROR, ret);
995}
996
997TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
998{
999 status_t ret;
1000 Parcel data, reply;
1001
1002 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1003 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1004
1005 data.writeInterfaceToken(binderLibTestServiceName);
1006 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1007 EXPECT_EQ(-1, reply.readInt32());
1008 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001009 EXPECT_EQ(NO_ERROR, ret);
1010}
1011
1012TEST_F(BinderLibTest, WorkSourceCleared)
1013{
1014 status_t ret;
1015 Parcel data, reply;
1016
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001017 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001018 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1019 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001020 data.writeInterfaceToken(binderLibTestServiceName);
1021 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1022
1023 EXPECT_EQ(-1, reply.readInt32());
1024 EXPECT_EQ(100, previousWorkSource);
1025 EXPECT_EQ(NO_ERROR, ret);
1026}
1027
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001028TEST_F(BinderLibTest, WorkSourceRestored)
1029{
1030 status_t ret;
1031 Parcel data, reply;
1032
1033 IPCThreadState::self()->setCallingWorkSourceUid(100);
1034 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1035 IPCThreadState::self()->restoreCallingWorkSource(token);
1036
1037 data.writeInterfaceToken(binderLibTestServiceName);
1038 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1039
1040 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001041 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001042 EXPECT_EQ(NO_ERROR, ret);
1043}
1044
Olivier Gaillard91a04802018-11-14 17:32:41 +00001045TEST_F(BinderLibTest, PropagateFlagSet)
1046{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001047 IPCThreadState::self()->clearPropagateWorkSource();
1048 IPCThreadState::self()->setCallingWorkSourceUid(100);
1049 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1050}
1051
1052TEST_F(BinderLibTest, PropagateFlagCleared)
1053{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001054 IPCThreadState::self()->setCallingWorkSourceUid(100);
1055 IPCThreadState::self()->clearPropagateWorkSource();
1056 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1057}
1058
1059TEST_F(BinderLibTest, PropagateFlagRestored)
1060{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001061 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1062 IPCThreadState::self()->restoreCallingWorkSource(token);
1063
1064 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1065}
1066
1067TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1068{
1069 IPCThreadState::self()->setCallingWorkSourceUid(100);
1070
1071 Parcel data, reply;
1072 status_t ret;
1073 data.writeInterfaceToken(binderLibTestServiceName);
1074 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1075
1076 Parcel data2, reply2;
1077 status_t ret2;
1078 data2.writeInterfaceToken(binderLibTestServiceName);
1079 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1080 EXPECT_EQ(100, reply2.readInt32());
1081 EXPECT_EQ(NO_ERROR, ret2);
1082}
1083
Steven Morelandbf1915b2020-07-16 22:43:02 +00001084TEST_F(BinderLibTest, SchedPolicySet) {
1085 sp<IBinder> server = addServer();
1086 ASSERT_TRUE(server != nullptr);
1087
1088 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001089 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1090 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001091
1092 int policy = reply.readInt32();
1093 int priority = reply.readInt32();
1094
1095 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1096 EXPECT_EQ(kSchedPriority, priority);
1097}
1098
Steven Morelandcf03cf12020-12-04 02:58:40 +00001099TEST_F(BinderLibTest, InheritRt) {
1100 sp<IBinder> server = addServer();
1101 ASSERT_TRUE(server != nullptr);
1102
1103 const struct sched_param param {
1104 .sched_priority = kSchedPriorityMore,
1105 };
1106 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1107
1108 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001109 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1110 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001111
1112 int policy = reply.readInt32();
1113 int priority = reply.readInt32();
1114
1115 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1116 EXPECT_EQ(kSchedPriorityMore, priority);
1117}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001118
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001119TEST_F(BinderLibTest, VectorSent) {
1120 Parcel data, reply;
1121 sp<IBinder> server = addServer();
1122 ASSERT_TRUE(server != nullptr);
1123
1124 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1125 data.writeUint64Vector(testValue);
1126
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001127 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001128 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001129 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001130 EXPECT_EQ(readValue, testValue);
1131}
1132
Martijn Coenen82c75312019-07-24 15:18:30 +02001133TEST_F(BinderLibTest, BufRejected) {
1134 Parcel data, reply;
1135 uint32_t buf;
1136 sp<IBinder> server = addServer();
1137 ASSERT_TRUE(server != nullptr);
1138
1139 binder_buffer_object obj {
1140 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001141 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001142 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1143 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001144 };
1145 data.setDataCapacity(1024);
1146 // Write a bogus object at offset 0 to get an entry in the offset table
1147 data.writeFileDescriptor(0);
1148 EXPECT_EQ(data.objectsCount(), 1);
1149 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1150 // And now, overwrite it with the buffer object
1151 memcpy(parcelData, &obj, sizeof(obj));
1152 data.setDataSize(sizeof(obj));
1153
Martijn Coenen82c75312019-07-24 15:18:30 +02001154 // Either the kernel should reject this transaction (if it's correct), but
1155 // if it's not, the server implementation should return an error if it
1156 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001157 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1158 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001159}
1160
Steven Moreland254e8ef2021-04-19 22:28:50 +00001161TEST_F(BinderLibTest, GotSid) {
1162 sp<IBinder> server = addServer();
1163
1164 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001165 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001166}
1167
Yifan Hong543edcd2021-05-18 19:47:30 -07001168class BinderLibTestService : public BBinder {
1169public:
1170 explicit BinderLibTestService(int32_t id)
1171 : m_id(id), m_nextServerId(id + 1), m_serverStartRequested(false), m_callback(nullptr) {
1172 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1173 pthread_cond_init(&m_serverWaitCond, nullptr);
1174 }
1175 ~BinderLibTestService() { exit(EXIT_SUCCESS); }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001176
Yifan Hong543edcd2021-05-18 19:47:30 -07001177 void processPendingCall() {
1178 if (m_callback != nullptr) {
1179 Parcel data;
1180 data.writeInt32(NO_ERROR);
1181 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1182 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001183 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001184 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001185
Yifan Hong543edcd2021-05-18 19:47:30 -07001186 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1187 uint32_t flags = 0) {
1188 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1189 return PERMISSION_DENIED;
1190 }
1191 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001192 case BINDER_LIB_TEST_REGISTER_SERVER: {
1193 int32_t id;
1194 sp<IBinder> binder;
1195 id = data.readInt32();
1196 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001197 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001198 return BAD_VALUE;
1199 }
1200
Yifan Hong543edcd2021-05-18 19:47:30 -07001201 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202
1203 pthread_mutex_lock(&m_serverWaitMutex);
1204 if (m_serverStartRequested) {
1205 m_serverStartRequested = false;
1206 m_serverStarted = binder;
1207 pthread_cond_signal(&m_serverWaitCond);
1208 }
1209 pthread_mutex_unlock(&m_serverWaitMutex);
1210 return NO_ERROR;
1211 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001212 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001213 case BINDER_LIB_TEST_ADD_SERVER: {
1214 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001215 int serverid;
1216
1217 if (m_id != 0) {
1218 return INVALID_OPERATION;
1219 }
1220 pthread_mutex_lock(&m_serverWaitMutex);
1221 if (m_serverStartRequested) {
1222 ret = -EBUSY;
1223 } else {
1224 serverid = m_nextServerId++;
1225 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001226 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001227
1228 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001229 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001230 pthread_mutex_lock(&m_serverWaitMutex);
1231 }
1232 if (ret > 0) {
1233 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001234 struct timespec ts;
1235 clock_gettime(CLOCK_REALTIME, &ts);
1236 ts.tv_sec += 5;
1237 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001238 }
1239 if (m_serverStartRequested) {
1240 m_serverStartRequested = false;
1241 ret = -ETIMEDOUT;
1242 } else {
1243 reply->writeStrongBinder(m_serverStarted);
1244 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001245 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001246 ret = NO_ERROR;
1247 }
1248 } else if (ret >= 0) {
1249 m_serverStartRequested = false;
1250 ret = UNKNOWN_ERROR;
1251 }
1252 pthread_mutex_unlock(&m_serverWaitMutex);
1253 return ret;
1254 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001255 case BINDER_LIB_TEST_GETPID:
1256 reply->writeInt32(getpid());
1257 return NO_ERROR;
1258 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1259 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001260 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001261 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001262 // oneway error codes should be ignored
1263 if (flags & TF_ONE_WAY) {
1264 return UNKNOWN_ERROR;
1265 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001266 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001267 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1268 // Note: this transaction is only designed for use with a
1269 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001270 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001271 // A callback was already pending; this means that
1272 // we received a second call while still processing
1273 // the first one. Fail the test.
1274 sp<IBinder> callback = data.readStrongBinder();
1275 Parcel data2;
1276 data2.writeInt32(UNKNOWN_ERROR);
1277
Yi Kong91635562018-06-07 14:38:36 -07001278 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001279 } else {
1280 m_callback = data.readStrongBinder();
1281 int32_t delayUs = data.readInt32();
1282 /*
1283 * It's necessary that we sleep here, so the next
1284 * transaction the caller makes will be queued to
1285 * the async queue.
1286 */
1287 usleep(delayUs);
1288
1289 /*
1290 * Now when we return, libbinder will tell the kernel
1291 * we are done with this transaction, and the kernel
1292 * can move the queued transaction to either the
1293 * thread todo worklist (for kernels without the fix),
1294 * or the proc todo worklist. In case of the former,
1295 * the next outbound call will pick up the pending
1296 * transaction, which leads to undesired reentrant
1297 * behavior. This is caught in the if() branch above.
1298 */
1299 }
1300
1301 return NO_ERROR;
1302 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001303 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1304 Parcel data2, reply2;
1305 sp<IBinder> binder;
1306 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001307 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001308 return BAD_VALUE;
1309 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001310 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001311 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1312 return NO_ERROR;
1313 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001314 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1315 reply->writeStrongBinder(this);
1316 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001317 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1318 reply->writeInt32(m_id);
1319 return NO_ERROR;
1320 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1321 int32_t count;
1322 uint32_t indirect_code;
1323 sp<IBinder> binder;
1324
1325 count = data.readInt32();
1326 reply->writeInt32(m_id);
1327 reply->writeInt32(count);
1328 for (int i = 0; i < count; i++) {
1329 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001330 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001331 return BAD_VALUE;
1332 }
1333 indirect_code = data.readInt32();
1334 BinderLibTestBundle data2(&data);
1335 if (!data2.isValid()) {
1336 return BAD_VALUE;
1337 }
1338 BinderLibTestBundle reply2;
1339 binder->transact(indirect_code, data2, &reply2);
1340 reply2.appendTo(reply);
1341 }
1342 return NO_ERROR;
1343 }
1344 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1345 reply->setError(data.readInt32());
1346 return NO_ERROR;
1347 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1348 reply->writeInt32(sizeof(void *));
1349 return NO_ERROR;
1350 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1351 return NO_ERROR;
1352 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1353 m_strongRef = data.readStrongBinder();
1354 return NO_ERROR;
1355 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1356 int ret;
1357 Parcel data2, reply2;
1358 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1359 sp<IBinder> target;
1360 sp<IBinder> callback;
1361
1362 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001363 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001364 return BAD_VALUE;
1365 }
1366 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001367 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001368 return BAD_VALUE;
1369 }
1370 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001371 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001372 data2.writeInt32(ret);
1373 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1374 return NO_ERROR;
1375 }
1376 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1377 int ret;
1378 int32_t size;
1379 const void *buf;
1380 int fd;
1381
1382 fd = data.readFileDescriptor();
1383 if (fd < 0) {
1384 return BAD_VALUE;
1385 }
1386 ret = data.readInt32(&size);
1387 if (ret != NO_ERROR) {
1388 return ret;
1389 }
1390 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001391 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001392 return BAD_VALUE;
1393 }
1394 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001395 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001396 return NO_ERROR;
1397 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001398 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1399 int ret;
1400 int32_t size;
1401 const void *buf;
1402 android::base::unique_fd fd;
1403
1404 ret = data.readUniqueParcelFileDescriptor(&fd);
1405 if (ret != NO_ERROR) {
1406 return ret;
1407 }
1408 ret = data.readInt32(&size);
1409 if (ret != NO_ERROR) {
1410 return ret;
1411 }
1412 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001413 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001414 return BAD_VALUE;
1415 }
1416 ret = write(fd.get(), buf, size);
1417 if (ret != size) return UNKNOWN_ERROR;
1418 return NO_ERROR;
1419 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001420 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1421 alarm(10);
1422 return NO_ERROR;
1423 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001424 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001425 ;
1426 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001427 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001428 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001429 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001430 return NO_ERROR;
1431 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001432 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1433 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001434 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001435 return NO_ERROR;
1436 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001437 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1438 int policy = 0;
1439 sched_param param;
1440 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1441 return UNKNOWN_ERROR;
1442 }
1443 reply->writeInt32(policy);
1444 reply->writeInt32(param.sched_priority);
1445 return NO_ERROR;
1446 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001447 case BINDER_LIB_TEST_ECHO_VECTOR: {
1448 std::vector<uint64_t> vector;
1449 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001450 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001451 reply->writeUint64Vector(vector);
1452 return NO_ERROR;
1453 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001454 case BINDER_LIB_TEST_REJECT_BUF: {
1455 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1456 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001457 case BINDER_LIB_TEST_CAN_GET_SID: {
1458 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1459 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001460 default:
1461 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001462 };
1463 }
1464
1465private:
1466 int32_t m_id;
1467 int32_t m_nextServerId;
1468 pthread_mutex_t m_serverWaitMutex;
1469 pthread_cond_t m_serverWaitCond;
1470 bool m_serverStartRequested;
1471 sp<IBinder> m_serverStarted;
1472 sp<IBinder> m_strongRef;
1473 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001474};
1475
Martijn Coenen45b07b42017-08-09 12:07:45 +02001476int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001477{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001478 binderLibTestServiceName += String16(binderserversuffix);
1479
Riley Andrews06b01ad2014-12-18 12:10:08 -08001480 status_t ret;
1481 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001482 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001483 {
1484 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001485
Steven Morelandbf1915b2020-07-16 22:43:02 +00001486 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1487
Steven Morelandcf03cf12020-12-04 02:58:40 +00001488 testService->setInheritRt(true);
1489
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001490 /*
1491 * Normally would also contain functionality as well, but we are only
1492 * testing the extension mechanism.
1493 */
1494 testService->setExtension(new BBinder());
1495
Martijn Coenen82c75312019-07-24 15:18:30 +02001496 // Required for test "BufRejected'
1497 testService->setRequestingSid(true);
1498
Martijn Coenen45b07b42017-08-09 12:07:45 +02001499 /*
1500 * We need this below, but can't hold a sp<> because it prevents the
1501 * node from being cleaned up automatically. It's safe in this case
1502 * because of how the tests are written.
1503 */
1504 testServicePtr = testService.get();
1505
Riley Andrews06b01ad2014-12-18 12:10:08 -08001506 if (index == 0) {
1507 ret = sm->addService(binderLibTestServiceName, testService);
1508 } else {
1509 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1510 Parcel data, reply;
1511 data.writeInt32(index);
1512 data.writeStrongBinder(testService);
1513
1514 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1515 }
1516 }
1517 write(readypipefd, &ret, sizeof(ret));
1518 close(readypipefd);
1519 //printf("%s: ret %d\n", __func__, ret);
1520 if (ret)
1521 return 1;
1522 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001523 if (usePoll) {
1524 int fd;
1525 struct epoll_event ev;
1526 int epoll_fd;
1527 IPCThreadState::self()->setupPolling(&fd);
1528 if (fd < 0) {
1529 return 1;
1530 }
1531 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1532
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001533 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001534 if (epoll_fd == -1) {
1535 return 1;
1536 }
1537
1538 ev.events = EPOLLIN;
1539 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1540 return 1;
1541 }
1542
1543 while (1) {
1544 /*
1545 * We simulate a single-threaded process using the binder poll
1546 * interface; besides handling binder commands, it can also
1547 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001548 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001549 *
1550 * processPendingCall() will then issue that transaction.
1551 */
1552 struct epoll_event events[1];
1553 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1554 if (numEvents < 0) {
1555 if (errno == EINTR) {
1556 continue;
1557 }
1558 return 1;
1559 }
1560 if (numEvents > 0) {
1561 IPCThreadState::self()->handlePolledCommands();
1562 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1563 testServicePtr->processPendingCall();
1564 }
1565 }
1566 } else {
1567 ProcessState::self()->startThreadPool();
1568 IPCThreadState::self()->joinThreadPool();
1569 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001570 //printf("%s: joinThreadPool returned\n", __func__);
1571 return 1; /* joinThreadPool should not return */
1572}
1573
1574int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001575 ExitIfWrongAbi();
1576
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001577 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001578 binderservername = argv[2];
1579 } else {
1580 binderservername = argv[0];
1581 }
1582
Martijn Coenen45b07b42017-08-09 12:07:45 +02001583 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1584 binderserversuffix = argv[5];
1585 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001586 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001587 binderserversuffix = new char[16];
1588 snprintf(binderserversuffix, 16, "%d", getpid());
1589 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001590
1591 ::testing::InitGoogleTest(&argc, argv);
1592 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1593 ProcessState::self()->startThreadPool();
1594 return RUN_ALL_TESTS();
1595}