blob: 0c3fbcd2da985f9cb08026f090a13eaff9355a33 [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
Riley Andrews06b01ad2014-12-18 12:10:08 -08001168class BinderLibTestService : public BBinder
1169{
1170 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001171 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001172 : m_id(id)
1173 , m_nextServerId(id + 1)
1174 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001175 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001176 {
Yi Kong91635562018-06-07 14:38:36 -07001177 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1178 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001179 }
1180 ~BinderLibTestService()
1181 {
1182 exit(EXIT_SUCCESS);
1183 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001184
1185 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001186 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001187 Parcel data;
1188 data.writeInt32(NO_ERROR);
1189 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001190 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001191 }
1192 }
1193
Riley Andrews06b01ad2014-12-18 12:10:08 -08001194 virtual status_t onTransact(uint32_t code,
1195 const Parcel& data, Parcel* reply,
1196 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001197 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1198 return PERMISSION_DENIED;
1199 }
1200 switch (code) {
1201 case BINDER_LIB_TEST_REGISTER_SERVER: {
1202 int32_t id;
1203 sp<IBinder> binder;
1204 id = data.readInt32();
1205 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001206 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001207 return BAD_VALUE;
1208 }
1209
1210 if (m_id != 0)
1211 return INVALID_OPERATION;
1212
1213 pthread_mutex_lock(&m_serverWaitMutex);
1214 if (m_serverStartRequested) {
1215 m_serverStartRequested = false;
1216 m_serverStarted = binder;
1217 pthread_cond_signal(&m_serverWaitCond);
1218 }
1219 pthread_mutex_unlock(&m_serverWaitMutex);
1220 return NO_ERROR;
1221 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001222 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001223 case BINDER_LIB_TEST_ADD_SERVER: {
1224 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001225 int serverid;
1226
1227 if (m_id != 0) {
1228 return INVALID_OPERATION;
1229 }
1230 pthread_mutex_lock(&m_serverWaitMutex);
1231 if (m_serverStartRequested) {
1232 ret = -EBUSY;
1233 } else {
1234 serverid = m_nextServerId++;
1235 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001236 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001237
1238 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001239 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001240 pthread_mutex_lock(&m_serverWaitMutex);
1241 }
1242 if (ret > 0) {
1243 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001244 struct timespec ts;
1245 clock_gettime(CLOCK_REALTIME, &ts);
1246 ts.tv_sec += 5;
1247 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001248 }
1249 if (m_serverStartRequested) {
1250 m_serverStartRequested = false;
1251 ret = -ETIMEDOUT;
1252 } else {
1253 reply->writeStrongBinder(m_serverStarted);
1254 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001255 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001256 ret = NO_ERROR;
1257 }
1258 } else if (ret >= 0) {
1259 m_serverStartRequested = false;
1260 ret = UNKNOWN_ERROR;
1261 }
1262 pthread_mutex_unlock(&m_serverWaitMutex);
1263 return ret;
1264 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001265 case BINDER_LIB_TEST_GETPID:
1266 reply->writeInt32(getpid());
1267 return NO_ERROR;
1268 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1269 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001270 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001271 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001272 // oneway error codes should be ignored
1273 if (flags & TF_ONE_WAY) {
1274 return UNKNOWN_ERROR;
1275 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001276 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001277 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1278 // Note: this transaction is only designed for use with a
1279 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001280 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001281 // A callback was already pending; this means that
1282 // we received a second call while still processing
1283 // the first one. Fail the test.
1284 sp<IBinder> callback = data.readStrongBinder();
1285 Parcel data2;
1286 data2.writeInt32(UNKNOWN_ERROR);
1287
Yi Kong91635562018-06-07 14:38:36 -07001288 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001289 } else {
1290 m_callback = data.readStrongBinder();
1291 int32_t delayUs = data.readInt32();
1292 /*
1293 * It's necessary that we sleep here, so the next
1294 * transaction the caller makes will be queued to
1295 * the async queue.
1296 */
1297 usleep(delayUs);
1298
1299 /*
1300 * Now when we return, libbinder will tell the kernel
1301 * we are done with this transaction, and the kernel
1302 * can move the queued transaction to either the
1303 * thread todo worklist (for kernels without the fix),
1304 * or the proc todo worklist. In case of the former,
1305 * the next outbound call will pick up the pending
1306 * transaction, which leads to undesired reentrant
1307 * behavior. This is caught in the if() branch above.
1308 */
1309 }
1310
1311 return NO_ERROR;
1312 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001313 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1314 Parcel data2, reply2;
1315 sp<IBinder> binder;
1316 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001317 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001318 return BAD_VALUE;
1319 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001320 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001321 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1322 return NO_ERROR;
1323 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001324 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1325 reply->writeStrongBinder(this);
1326 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001327 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1328 reply->writeInt32(m_id);
1329 return NO_ERROR;
1330 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1331 int32_t count;
1332 uint32_t indirect_code;
1333 sp<IBinder> binder;
1334
1335 count = data.readInt32();
1336 reply->writeInt32(m_id);
1337 reply->writeInt32(count);
1338 for (int i = 0; i < count; i++) {
1339 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001340 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001341 return BAD_VALUE;
1342 }
1343 indirect_code = data.readInt32();
1344 BinderLibTestBundle data2(&data);
1345 if (!data2.isValid()) {
1346 return BAD_VALUE;
1347 }
1348 BinderLibTestBundle reply2;
1349 binder->transact(indirect_code, data2, &reply2);
1350 reply2.appendTo(reply);
1351 }
1352 return NO_ERROR;
1353 }
1354 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1355 reply->setError(data.readInt32());
1356 return NO_ERROR;
1357 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1358 reply->writeInt32(sizeof(void *));
1359 return NO_ERROR;
1360 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1361 return NO_ERROR;
1362 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1363 m_strongRef = data.readStrongBinder();
1364 return NO_ERROR;
1365 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1366 int ret;
1367 Parcel data2, reply2;
1368 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1369 sp<IBinder> target;
1370 sp<IBinder> callback;
1371
1372 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001373 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001374 return BAD_VALUE;
1375 }
1376 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001377 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001378 return BAD_VALUE;
1379 }
1380 ret = target->linkToDeath(testDeathRecipient);
1381 if (ret == NO_ERROR)
1382 ret = testDeathRecipient->waitEvent(5);
1383 data2.writeInt32(ret);
1384 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1385 return NO_ERROR;
1386 }
1387 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1388 int ret;
1389 int32_t size;
1390 const void *buf;
1391 int fd;
1392
1393 fd = data.readFileDescriptor();
1394 if (fd < 0) {
1395 return BAD_VALUE;
1396 }
1397 ret = data.readInt32(&size);
1398 if (ret != NO_ERROR) {
1399 return ret;
1400 }
1401 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001402 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001403 return BAD_VALUE;
1404 }
1405 ret = write(fd, buf, size);
1406 if (ret != size)
1407 return UNKNOWN_ERROR;
1408 return NO_ERROR;
1409 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001410 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1411 int ret;
1412 int32_t size;
1413 const void *buf;
1414 android::base::unique_fd fd;
1415
1416 ret = data.readUniqueParcelFileDescriptor(&fd);
1417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420 ret = data.readInt32(&size);
1421 if (ret != NO_ERROR) {
1422 return ret;
1423 }
1424 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001425 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001426 return BAD_VALUE;
1427 }
1428 ret = write(fd.get(), buf, size);
1429 if (ret != size) return UNKNOWN_ERROR;
1430 return NO_ERROR;
1431 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001432 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1433 alarm(10);
1434 return NO_ERROR;
1435 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001436 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001437 ;
1438 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001439 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001440 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001441 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001442 return NO_ERROR;
1443 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001444 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1445 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001446 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001447 return NO_ERROR;
1448 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001449 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1450 int policy = 0;
1451 sched_param param;
1452 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1453 return UNKNOWN_ERROR;
1454 }
1455 reply->writeInt32(policy);
1456 reply->writeInt32(param.sched_priority);
1457 return NO_ERROR;
1458 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001459 case BINDER_LIB_TEST_ECHO_VECTOR: {
1460 std::vector<uint64_t> vector;
1461 auto err = data.readUint64Vector(&vector);
1462 if (err != NO_ERROR)
1463 return err;
1464 reply->writeUint64Vector(vector);
1465 return NO_ERROR;
1466 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001467 case BINDER_LIB_TEST_REJECT_BUF: {
1468 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1469 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001470 case BINDER_LIB_TEST_CAN_GET_SID: {
1471 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1472 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001473 default:
1474 return UNKNOWN_TRANSACTION;
1475 };
1476 }
1477 private:
1478 int32_t m_id;
1479 int32_t m_nextServerId;
1480 pthread_mutex_t m_serverWaitMutex;
1481 pthread_cond_t m_serverWaitCond;
1482 bool m_serverStartRequested;
1483 sp<IBinder> m_serverStarted;
1484 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001485 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001486};
1487
Martijn Coenen45b07b42017-08-09 12:07:45 +02001488int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001489{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001490 binderLibTestServiceName += String16(binderserversuffix);
1491
Riley Andrews06b01ad2014-12-18 12:10:08 -08001492 status_t ret;
1493 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001494 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001495 {
1496 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001497
Steven Morelandbf1915b2020-07-16 22:43:02 +00001498 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1499
Steven Morelandcf03cf12020-12-04 02:58:40 +00001500 testService->setInheritRt(true);
1501
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001502 /*
1503 * Normally would also contain functionality as well, but we are only
1504 * testing the extension mechanism.
1505 */
1506 testService->setExtension(new BBinder());
1507
Martijn Coenen82c75312019-07-24 15:18:30 +02001508 // Required for test "BufRejected'
1509 testService->setRequestingSid(true);
1510
Martijn Coenen45b07b42017-08-09 12:07:45 +02001511 /*
1512 * We need this below, but can't hold a sp<> because it prevents the
1513 * node from being cleaned up automatically. It's safe in this case
1514 * because of how the tests are written.
1515 */
1516 testServicePtr = testService.get();
1517
Riley Andrews06b01ad2014-12-18 12:10:08 -08001518 if (index == 0) {
1519 ret = sm->addService(binderLibTestServiceName, testService);
1520 } else {
1521 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1522 Parcel data, reply;
1523 data.writeInt32(index);
1524 data.writeStrongBinder(testService);
1525
1526 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1527 }
1528 }
1529 write(readypipefd, &ret, sizeof(ret));
1530 close(readypipefd);
1531 //printf("%s: ret %d\n", __func__, ret);
1532 if (ret)
1533 return 1;
1534 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001535 if (usePoll) {
1536 int fd;
1537 struct epoll_event ev;
1538 int epoll_fd;
1539 IPCThreadState::self()->setupPolling(&fd);
1540 if (fd < 0) {
1541 return 1;
1542 }
1543 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1544
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001545 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001546 if (epoll_fd == -1) {
1547 return 1;
1548 }
1549
1550 ev.events = EPOLLIN;
1551 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1552 return 1;
1553 }
1554
1555 while (1) {
1556 /*
1557 * We simulate a single-threaded process using the binder poll
1558 * interface; besides handling binder commands, it can also
1559 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001560 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001561 *
1562 * processPendingCall() will then issue that transaction.
1563 */
1564 struct epoll_event events[1];
1565 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1566 if (numEvents < 0) {
1567 if (errno == EINTR) {
1568 continue;
1569 }
1570 return 1;
1571 }
1572 if (numEvents > 0) {
1573 IPCThreadState::self()->handlePolledCommands();
1574 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1575 testServicePtr->processPendingCall();
1576 }
1577 }
1578 } else {
1579 ProcessState::self()->startThreadPool();
1580 IPCThreadState::self()->joinThreadPool();
1581 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001582 //printf("%s: joinThreadPool returned\n", __func__);
1583 return 1; /* joinThreadPool should not return */
1584}
1585
1586int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001587 ExitIfWrongAbi();
1588
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001589 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001590 binderservername = argv[2];
1591 } else {
1592 binderservername = argv[0];
1593 }
1594
Martijn Coenen45b07b42017-08-09 12:07:45 +02001595 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1596 binderserversuffix = argv[5];
1597 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001598 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001599 binderserversuffix = new char[16];
1600 snprintf(binderserversuffix, 16, "%d", getpid());
1601 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001602
1603 ::testing::InitGoogleTest(&argc, argv);
1604 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1605 ProcessState::self()->startThreadPool();
1606 return RUN_ALL_TESTS();
1607}