blob: 64196ba30f3b925fda424a4bbebb8728e8f5c71f [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>
33
Steven Morelandcf03cf12020-12-04 02:58:40 +000034#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020035#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080036#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020037
Steven Moreland6ba5a252021-05-04 22:49:00 +000038#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070039#include "binderAbiHelper.h"
40
Riley Andrews06b01ad2014-12-18 12:10:08 -080041#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
42
43using namespace android;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070044using testing::Not;
45
46// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
47MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
48 *result_listener << statusToString(arg);
49 return expected == arg;
50}
Riley Andrews06b01ad2014-12-18 12:10:08 -080051
Sherry Yang336cdd32017-07-24 14:12:27 -070052static ::testing::AssertionResult IsPageAligned(void *buf) {
53 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
54 return ::testing::AssertionSuccess();
55 else
56 return ::testing::AssertionFailure() << buf << " is not page aligned";
57}
58
Riley Andrews06b01ad2014-12-18 12:10:08 -080059static testing::Environment* binder_env;
60static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070061static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080062static char binderserverarg[] = "--binderserver";
63
Steven Morelandbf1915b2020-07-16 22:43:02 +000064static constexpr int kSchedPolicy = SCHED_RR;
65static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000066static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000067
Riley Andrews06b01ad2014-12-18 12:10:08 -080068static String16 binderLibTestServiceName = String16("test.binderLib");
69
70enum BinderLibTestTranscationCode {
71 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
72 BINDER_LIB_TEST_REGISTER_SERVER,
73 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020074 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080075 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070076 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020077 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080078 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070079 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080080 BINDER_LIB_TEST_GET_ID_TRANSACTION,
81 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
82 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
83 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
84 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
85 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
86 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090087 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080088 BINDER_LIB_TEST_EXIT_TRANSACTION,
89 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
90 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070091 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010092 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000093 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070094 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
95 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080096 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020097 BINDER_LIB_TEST_REJECT_BUF,
Steven Moreland254e8ef2021-04-19 22:28:50 +000098 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -080099};
100
Martijn Coenen45b07b42017-08-09 12:07:45 +0200101pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800102{
103 int ret;
104 pid_t pid;
105 status_t status;
106 int pipefd[2];
107 char stri[16];
108 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200109 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800110 char *childargv[] = {
111 binderservername,
112 binderserverarg,
113 stri,
114 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200115 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700116 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700117 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800118 };
119
120 ret = pipe(pipefd);
121 if (ret < 0)
122 return ret;
123
124 snprintf(stri, sizeof(stri), "%d", arg2);
125 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200126 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800127
128 pid = fork();
129 if (pid == -1)
130 return pid;
131 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800132 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800133 close(pipefd[0]);
134 execv(binderservername, childargv);
135 status = -errno;
136 write(pipefd[1], &status, sizeof(status));
137 fprintf(stderr, "execv failed, %s\n", strerror(errno));
138 _exit(EXIT_FAILURE);
139 }
140 close(pipefd[1]);
141 ret = read(pipefd[0], &status, sizeof(status));
142 //printf("pipe read returned %d, status %d\n", ret, status);
143 close(pipefd[0]);
144 if (ret == sizeof(status)) {
145 ret = status;
146 } else {
147 kill(pid, SIGKILL);
148 if (ret >= 0) {
149 ret = NO_INIT;
150 }
151 }
152 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700153 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800154 return ret;
155 }
156 return pid;
157}
158
159class BinderLibTestEnv : public ::testing::Environment {
160 public:
161 BinderLibTestEnv() {}
162 sp<IBinder> getServer(void) {
163 return m_server;
164 }
165
166 private:
167 virtual void SetUp() {
168 m_serverpid = start_server_process(0);
169 //printf("m_serverpid %d\n", m_serverpid);
170 ASSERT_GT(m_serverpid, 0);
171
172 sp<IServiceManager> sm = defaultServiceManager();
173 //printf("%s: pid %d, get service\n", __func__, m_pid);
174 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700175 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800176 //printf("%s: pid %d, get service done\n", __func__, m_pid);
177 }
178 virtual void TearDown() {
179 status_t ret;
180 Parcel data, reply;
181 int exitStatus;
182 pid_t pid;
183
184 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700185 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800186 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
187 EXPECT_EQ(0, ret);
188 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
189 EXPECT_EQ(0, ret);
190 }
191 if (m_serverpid > 0) {
192 //printf("wait for %d\n", m_pids[i]);
193 pid = wait(&exitStatus);
194 EXPECT_EQ(m_serverpid, pid);
195 EXPECT_TRUE(WIFEXITED(exitStatus));
196 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
197 }
198 }
199
200 pid_t m_serverpid;
201 sp<IBinder> m_server;
202};
203
204class BinderLibTest : public ::testing::Test {
205 public:
206 virtual void SetUp() {
207 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000208 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 }
210 virtual void TearDown() {
211 }
212 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200213 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800215 int32_t id;
216 Parcel data, reply;
217 sp<IBinder> binder;
218
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700219 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800220
Yi Kong91635562018-06-07 14:38:36 -0700221 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800222 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700223 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700224 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800225 if (idPtr)
226 *idPtr = id;
227 return binder;
228 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200229
Yi Kong91635562018-06-07 14:38:36 -0700230 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200231 {
232 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
233 }
234
Yi Kong91635562018-06-07 14:38:36 -0700235 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200236 {
237 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
238 }
239
Riley Andrews06b01ad2014-12-18 12:10:08 -0800240 void waitForReadData(int fd, int timeout_ms) {
241 int ret;
242 pollfd pfd = pollfd();
243
244 pfd.fd = fd;
245 pfd.events = POLLIN;
246 ret = poll(&pfd, 1, timeout_ms);
247 EXPECT_EQ(1, ret);
248 }
249
250 sp<IBinder> m_server;
251};
252
253class BinderLibTestBundle : public Parcel
254{
255 public:
256 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800257 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800258 int32_t mark;
259 int32_t bundleLen;
260 size_t pos;
261
262 if (source->readInt32(&mark))
263 return;
264 if (mark != MARK_START)
265 return;
266 if (source->readInt32(&bundleLen))
267 return;
268 pos = source->dataPosition();
269 if (Parcel::appendFrom(source, pos, bundleLen))
270 return;
271 source->setDataPosition(pos + bundleLen);
272 if (source->readInt32(&mark))
273 return;
274 if (mark != MARK_END)
275 return;
276 m_isValid = true;
277 setDataPosition(0);
278 }
279 void appendTo(Parcel *dest) {
280 dest->writeInt32(MARK_START);
281 dest->writeInt32(dataSize());
282 dest->appendFrom(this, 0, dataSize());
283 dest->writeInt32(MARK_END);
284 };
285 bool isValid(void) {
286 return m_isValid;
287 }
288 private:
289 enum {
290 MARK_START = B_PACK_CHARS('B','T','B','S'),
291 MARK_END = B_PACK_CHARS('B','T','B','E'),
292 };
293 bool m_isValid;
294};
295
296class BinderLibTestEvent
297{
298 public:
299 BinderLibTestEvent(void)
300 : m_eventTriggered(false)
301 {
Yi Kong91635562018-06-07 14:38:36 -0700302 pthread_mutex_init(&m_waitMutex, nullptr);
303 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800304 }
305 int waitEvent(int timeout_s)
306 {
307 int ret;
308 pthread_mutex_lock(&m_waitMutex);
309 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800310 struct timespec ts;
311 clock_gettime(CLOCK_REALTIME, &ts);
312 ts.tv_sec += timeout_s;
313 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314 }
315 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
316 pthread_mutex_unlock(&m_waitMutex);
317 return ret;
318 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200319 pthread_t getTriggeringThread()
320 {
321 return m_triggeringThread;
322 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800323 protected:
324 void triggerEvent(void) {
325 pthread_mutex_lock(&m_waitMutex);
326 pthread_cond_signal(&m_waitCond);
327 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200328 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800329 pthread_mutex_unlock(&m_waitMutex);
330 };
331 private:
332 pthread_mutex_t m_waitMutex;
333 pthread_cond_t m_waitCond;
334 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200335 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800336};
337
338class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
339{
340 public:
341 BinderLibTestCallBack()
342 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700343 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800344 {
345 }
346 status_t getResult(void)
347 {
348 return m_result;
349 }
350
351 private:
352 virtual status_t onTransact(uint32_t code,
353 const Parcel& data, Parcel* reply,
354 uint32_t flags = 0)
355 {
356 (void)reply;
357 (void)flags;
358 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200359 case BINDER_LIB_TEST_CALL_BACK: {
360 status_t status = data.readInt32(&m_result);
361 if (status != NO_ERROR) {
362 m_result = status;
363 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800364 triggerEvent();
365 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200366 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700367 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
368 sp<IBinder> server;
369 int ret;
370 const uint8_t *buf = data.data();
371 size_t size = data.dataSize();
372 if (m_prev_end) {
373 /* 64-bit kernel needs at most 8 bytes to align buffer end */
374 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
375 } else {
376 EXPECT_TRUE(IsPageAligned((void *)buf));
377 }
378
379 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
380
381 if (size > 0) {
382 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
383 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
384 data, reply);
385 EXPECT_EQ(NO_ERROR, ret);
386 }
387 return NO_ERROR;
388 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800389 default:
390 return UNKNOWN_TRANSACTION;
391 }
392 }
393
394 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700395 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800396};
397
398class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
399{
400 private:
401 virtual void binderDied(const wp<IBinder>& who) {
402 (void)who;
403 triggerEvent();
404 };
405};
406
407TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800408 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700409 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
410 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800411}
412
Steven Moreland80844f72020-12-12 02:06:08 +0000413TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000414 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700415 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
416 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000417}
418
Steven Morelandf183fdd2020-10-27 00:12:12 +0000419TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000420 Parcel data, reply;
421 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700422 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
423 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000424}
425
Marco Ballesio7ee17572020-09-08 10:30:03 -0700426TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700427 Parcel data, reply, replypid;
Li Li60d69032021-09-10 09:59:30 -0700428 std::ifstream freezer_file("/sys/fs/cgroup/uid_0/cgroup.freeze");
Marco Ballesio7ee17572020-09-08 10:30:03 -0700429
Li Li60d69032021-09-10 09:59:30 -0700430 // Pass test on devices where the cgroup v2 freezer is not supported
Marco Ballesio7ee17572020-09-08 10:30:03 -0700431 if (freezer_file.fail()) {
432 GTEST_SKIP();
433 return;
434 }
435
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700436 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700437 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700438 for (int i = 0; i < 10; i++) {
439 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
440 }
Li Li60d69032021-09-10 09:59:30 -0700441
442 // Pass test on devices where BINDER_FREEZE ioctl is not supported
443 int ret = IPCThreadState::self()->freeze(pid, false, 0);
444 if (ret != 0) {
445 GTEST_SKIP();
446 return;
447 }
448
449 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
450 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
451 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, true, 1000));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700452 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700453
454 bool sync_received, async_received;
455
456 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
457 &async_received));
458
459 EXPECT_EQ(sync_received, 1);
460 EXPECT_EQ(async_received, 0);
461
Li Li60d69032021-09-10 09:59:30 -0700462 uint32_t sync_received2, async_received2;
463
464 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received2,
465 &async_received2));
466
467 EXPECT_EQ(sync_received2, 1);
468 EXPECT_EQ(async_received2, 0);
469
Marco Ballesio7ee17572020-09-08 10:30:03 -0700470 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
471 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
472}
473
Riley Andrews06b01ad2014-12-18 12:10:08 -0800474TEST_F(BinderLibTest, SetError) {
475 int32_t testValue[] = { 0, -123, 123 };
476 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800477 Parcel data, reply;
478 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700479 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
480 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800481 }
482}
483
484TEST_F(BinderLibTest, GetId) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800485 int32_t id;
486 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700487 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply),
488 StatusEq(NO_ERROR));
489 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800490 EXPECT_EQ(0, id);
491}
492
493TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800494 int32_t ptrsize;
495 Parcel data, reply;
496 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700497 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700498 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
499 StatusEq(NO_ERROR));
500 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800501 RecordProperty("TestPtrSize", sizeof(void *));
502 RecordProperty("ServerPtrSize", sizeof(void *));
503}
504
505TEST_F(BinderLibTest, IndirectGetId2)
506{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800507 int32_t id;
508 int32_t count;
509 Parcel data, reply;
510 int32_t serverId[3];
511
512 data.writeInt32(ARRAY_SIZE(serverId));
513 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
514 sp<IBinder> server;
515 BinderLibTestBundle datai;
516
517 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700518 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800519 data.writeStrongBinder(server);
520 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
521 datai.appendTo(&data);
522 }
523
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700524 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
525 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800526
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700527 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800528 EXPECT_EQ(0, id);
529
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700530 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700531 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800532
533 for (size_t i = 0; i < (size_t)count; i++) {
534 BinderLibTestBundle replyi(&reply);
535 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700536 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800537 EXPECT_EQ(serverId[i], id);
538 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
539 }
540
541 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
542}
543
544TEST_F(BinderLibTest, IndirectGetId3)
545{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800546 int32_t id;
547 int32_t count;
548 Parcel data, reply;
549 int32_t serverId[3];
550
551 data.writeInt32(ARRAY_SIZE(serverId));
552 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
553 sp<IBinder> server;
554 BinderLibTestBundle datai;
555 BinderLibTestBundle datai2;
556
557 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700558 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559 data.writeStrongBinder(server);
560 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
561
562 datai.writeInt32(1);
563 datai.writeStrongBinder(m_server);
564 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
565 datai2.appendTo(&datai);
566
567 datai.appendTo(&data);
568 }
569
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700570 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
571 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800572
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700573 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800574 EXPECT_EQ(0, id);
575
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700576 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700577 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800578
579 for (size_t i = 0; i < (size_t)count; i++) {
580 int32_t counti;
581
582 BinderLibTestBundle replyi(&reply);
583 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700584 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800585 EXPECT_EQ(serverId[i], id);
586
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700587 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800588 EXPECT_EQ(1, counti);
589
590 BinderLibTestBundle replyi2(&replyi);
591 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700592 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800593 EXPECT_EQ(0, id);
594 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
595
596 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
597 }
598
599 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
600}
601
602TEST_F(BinderLibTest, CallBack)
603{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800604 Parcel data, reply;
605 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
606 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700607 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
608 StatusEq(NO_ERROR));
609 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
610 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800611}
612
613TEST_F(BinderLibTest, AddServer)
614{
615 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700616 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800617}
618
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619TEST_F(BinderLibTest, DeathNotificationStrongRef)
620{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621 sp<IBinder> sbinder;
622
623 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
624
625 {
626 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700627 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700628 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800629 sbinder = binder;
630 }
631 {
632 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700633 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
634 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800635 }
636 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700637 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
638 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800639}
640
641TEST_F(BinderLibTest, DeathNotificationMultiple)
642{
643 status_t ret;
644 const int clientcount = 2;
645 sp<IBinder> target;
646 sp<IBinder> linkedclient[clientcount];
647 sp<BinderLibTestCallBack> callBack[clientcount];
648 sp<IBinder> passiveclient[clientcount];
649
650 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700651 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800652 for (int i = 0; i < clientcount; i++) {
653 {
654 Parcel data, reply;
655
656 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700657 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800658 callBack[i] = new BinderLibTestCallBack();
659 data.writeStrongBinder(target);
660 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700661 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
662 &reply, TF_ONE_WAY),
663 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800664 }
665 {
666 Parcel data, reply;
667
668 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700669 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800670 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700671 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
672 &reply, TF_ONE_WAY),
673 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800674 }
675 }
676 {
677 Parcel data, reply;
678 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
679 EXPECT_EQ(0, ret);
680 }
681
682 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700683 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
684 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685 }
686}
687
Martijn Coenenf7100e42017-07-31 12:14:09 +0200688TEST_F(BinderLibTest, DeathNotificationThread)
689{
690 status_t ret;
691 sp<BinderLibTestCallBack> callback;
692 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700693 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200694 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700695 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200696
697 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
698
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700699 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200700
701 {
702 Parcel data, reply;
703 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
704 EXPECT_EQ(0, ret);
705 }
706
707 /* Make sure it's dead */
708 testDeathRecipient->waitEvent(5);
709
710 /* Now, pass the ref to another process and ask that process to
711 * call linkToDeath() on it, and wait for a response. This tests
712 * two things:
713 * 1) You still get death notifications when calling linkToDeath()
714 * on a ref that is already dead when it was passed to you.
715 * 2) That death notifications are not directly pushed to the thread
716 * registering them, but to the threadpool (proc workqueue) instead.
717 *
718 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
719 * is blocked on a condition variable waiting for the death notification to be
720 * called; therefore, that thread is not available for handling proc work.
721 * So, if the death notification was pushed to the thread workqueue, the callback
722 * would never be called, and the test would timeout and fail.
723 *
724 * Note that we can't do this part of the test from this thread itself, because
725 * the binder driver would only push death notifications to the thread if
726 * it is a looper thread, which this thread is not.
727 *
728 * See b/23525545 for details.
729 */
730 {
731 Parcel data, reply;
732
733 callback = new BinderLibTestCallBack();
734 data.writeStrongBinder(target);
735 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700736 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
737 TF_ONE_WAY),
738 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200739 }
740
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700741 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
742 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200743}
744
Riley Andrews06b01ad2014-12-18 12:10:08 -0800745TEST_F(BinderLibTest, PassFile) {
746 int ret;
747 int pipefd[2];
748 uint8_t buf[1] = { 0 };
749 uint8_t write_value = 123;
750
751 ret = pipe2(pipefd, O_NONBLOCK);
752 ASSERT_EQ(0, ret);
753
754 {
755 Parcel data, reply;
756 uint8_t writebuf[1] = { write_value };
757
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700758 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800759
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700760 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800761
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700762 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700764 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
765 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800766 }
767
768 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700769 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800770 EXPECT_EQ(write_value, buf[0]);
771
772 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
773
774 ret = read(pipefd[0], buf, sizeof(buf));
775 EXPECT_EQ(0, ret);
776
777 close(pipefd[0]);
778}
779
Ryo Hashimotobf551892018-05-31 16:58:35 +0900780TEST_F(BinderLibTest, PassParcelFileDescriptor) {
781 const int datasize = 123;
782 std::vector<uint8_t> writebuf(datasize);
783 for (size_t i = 0; i < writebuf.size(); ++i) {
784 writebuf[i] = i;
785 }
786
787 android::base::unique_fd read_end, write_end;
788 {
789 int pipefd[2];
790 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
791 read_end.reset(pipefd[0]);
792 write_end.reset(pipefd[1]);
793 }
794 {
795 Parcel data;
796 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
797 write_end.reset();
798 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
799 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
800
801 Parcel reply;
802 EXPECT_EQ(NO_ERROR,
803 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
804 &reply));
805 }
806 std::vector<uint8_t> readbuf(datasize);
807 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
808 EXPECT_EQ(writebuf, readbuf);
809
810 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
811
812 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
813}
814
Riley Andrews06b01ad2014-12-18 12:10:08 -0800815TEST_F(BinderLibTest, PromoteLocal) {
816 sp<IBinder> strong = new BBinder();
817 wp<IBinder> weak = strong;
818 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700819 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800820 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700821 strong = nullptr;
822 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800823 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700824 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800825}
826
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700827TEST_F(BinderLibTest, LocalGetExtension) {
828 sp<BBinder> binder = new BBinder();
829 sp<IBinder> ext = new BBinder();
830 binder->setExtension(ext);
831 EXPECT_EQ(ext, binder->getExtension());
832}
833
834TEST_F(BinderLibTest, RemoteGetExtension) {
835 sp<IBinder> server = addServer();
836 ASSERT_TRUE(server != nullptr);
837
838 sp<IBinder> extension;
839 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
840 ASSERT_NE(nullptr, extension.get());
841
842 EXPECT_EQ(NO_ERROR, extension->pingBinder());
843}
844
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700845TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700846 Parcel data, reply;
847
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700848 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
849 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700850
851 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700852 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800853 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
854 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
855 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
856 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700857}
858
Connor O'Brien52be2c92016-09-20 14:18:08 -0700859TEST_F(BinderLibTest, FreedBinder) {
860 status_t ret;
861
862 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700863 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700864
865 __u32 freedHandle;
866 wp<IBinder> keepFreedBinder;
867 {
868 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700869 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
870 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700871 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
872 freedHandle = freed->handle;
873 /* Add a weak ref to the freed binder so the driver does not
874 * delete its reference to it - otherwise the transaction
875 * fails regardless of whether the driver is fixed.
876 */
Steven Morelande171d622019-07-17 16:06:01 -0700877 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700878 }
Steven Morelande171d622019-07-17 16:06:01 -0700879 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700880 {
881 Parcel data, reply;
882 data.writeStrongBinder(server);
883 /* Replace original handle with handle to the freed binder */
884 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
885 __u32 oldHandle = strong->handle;
886 strong->handle = freedHandle;
887 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
888 /* Returns DEAD_OBJECT (-32) if target crashes and
889 * FAILED_TRANSACTION if the driver rejects the invalid
890 * object.
891 */
892 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
893 /* Restore original handle so parcel destructor does not use
894 * the wrong handle.
895 */
896 strong->handle = oldHandle;
897 }
898}
899
Sherry Yang336cdd32017-07-24 14:12:27 -0700900TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700901 Parcel data, reply;
902 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
903 for (int i = 0; i < 2; i++) {
904 BinderLibTestBundle datai;
905 datai.appendFrom(&data, 0, data.dataSize());
906
907 data.freeData();
908 data.writeInt32(1);
909 data.writeStrongBinder(callBack);
910 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
911
912 datai.appendTo(&data);
913 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700914 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
915 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700916}
917
Martijn Coenen45b07b42017-08-09 12:07:45 +0200918TEST_F(BinderLibTest, OnewayQueueing)
919{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200920 Parcel data, data2;
921
922 sp<IBinder> pollServer = addPollServer();
923
924 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
925 data.writeStrongBinder(callBack);
926 data.writeInt32(500000); // delay in us before calling back
927
928 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
929 data2.writeStrongBinder(callBack2);
930 data2.writeInt32(0); // delay in us
931
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700932 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
933 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200934
935 // The delay ensures that this second transaction will end up on the async_todo list
936 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700937 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
938 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200939
940 // The server will ensure that the two transactions are handled in the expected order;
941 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700942 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
943 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200944
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700945 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
946 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200947}
948
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100949TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
950{
951 status_t ret;
952 Parcel data, reply;
953 data.writeInterfaceToken(binderLibTestServiceName);
954 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
955 EXPECT_EQ(-1, reply.readInt32());
956 EXPECT_EQ(NO_ERROR, ret);
957}
958
959TEST_F(BinderLibTest, WorkSourceSet)
960{
961 status_t ret;
962 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000963 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000964 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100965 data.writeInterfaceToken(binderLibTestServiceName);
966 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
967 EXPECT_EQ(100, reply.readInt32());
968 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000969 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
970 EXPECT_EQ(NO_ERROR, ret);
971}
972
973TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
974{
975 status_t ret;
976 Parcel data, reply;
977
978 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
979 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
980
981 data.writeInterfaceToken(binderLibTestServiceName);
982 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
983 EXPECT_EQ(-1, reply.readInt32());
984 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100985 EXPECT_EQ(NO_ERROR, ret);
986}
987
988TEST_F(BinderLibTest, WorkSourceCleared)
989{
990 status_t ret;
991 Parcel data, reply;
992
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000993 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000994 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
995 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100996 data.writeInterfaceToken(binderLibTestServiceName);
997 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
998
999 EXPECT_EQ(-1, reply.readInt32());
1000 EXPECT_EQ(100, previousWorkSource);
1001 EXPECT_EQ(NO_ERROR, ret);
1002}
1003
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001004TEST_F(BinderLibTest, WorkSourceRestored)
1005{
1006 status_t ret;
1007 Parcel data, reply;
1008
1009 IPCThreadState::self()->setCallingWorkSourceUid(100);
1010 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1011 IPCThreadState::self()->restoreCallingWorkSource(token);
1012
1013 data.writeInterfaceToken(binderLibTestServiceName);
1014 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1015
1016 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001017 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001018 EXPECT_EQ(NO_ERROR, ret);
1019}
1020
Olivier Gaillard91a04802018-11-14 17:32:41 +00001021TEST_F(BinderLibTest, PropagateFlagSet)
1022{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001023 IPCThreadState::self()->clearPropagateWorkSource();
1024 IPCThreadState::self()->setCallingWorkSourceUid(100);
1025 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1026}
1027
1028TEST_F(BinderLibTest, PropagateFlagCleared)
1029{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001030 IPCThreadState::self()->setCallingWorkSourceUid(100);
1031 IPCThreadState::self()->clearPropagateWorkSource();
1032 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1033}
1034
1035TEST_F(BinderLibTest, PropagateFlagRestored)
1036{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001037 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1038 IPCThreadState::self()->restoreCallingWorkSource(token);
1039
1040 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1041}
1042
1043TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1044{
1045 IPCThreadState::self()->setCallingWorkSourceUid(100);
1046
1047 Parcel data, reply;
1048 status_t ret;
1049 data.writeInterfaceToken(binderLibTestServiceName);
1050 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1051
1052 Parcel data2, reply2;
1053 status_t ret2;
1054 data2.writeInterfaceToken(binderLibTestServiceName);
1055 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1056 EXPECT_EQ(100, reply2.readInt32());
1057 EXPECT_EQ(NO_ERROR, ret2);
1058}
1059
Steven Morelandbf1915b2020-07-16 22:43:02 +00001060TEST_F(BinderLibTest, SchedPolicySet) {
1061 sp<IBinder> server = addServer();
1062 ASSERT_TRUE(server != nullptr);
1063
1064 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001065 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1066 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001067
1068 int policy = reply.readInt32();
1069 int priority = reply.readInt32();
1070
1071 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1072 EXPECT_EQ(kSchedPriority, priority);
1073}
1074
Steven Morelandcf03cf12020-12-04 02:58:40 +00001075TEST_F(BinderLibTest, InheritRt) {
1076 sp<IBinder> server = addServer();
1077 ASSERT_TRUE(server != nullptr);
1078
1079 const struct sched_param param {
1080 .sched_priority = kSchedPriorityMore,
1081 };
1082 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1083
1084 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001085 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1086 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001087
1088 int policy = reply.readInt32();
1089 int priority = reply.readInt32();
1090
1091 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1092 EXPECT_EQ(kSchedPriorityMore, priority);
1093}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001094
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001095TEST_F(BinderLibTest, VectorSent) {
1096 Parcel data, reply;
1097 sp<IBinder> server = addServer();
1098 ASSERT_TRUE(server != nullptr);
1099
1100 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1101 data.writeUint64Vector(testValue);
1102
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001103 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001104 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001105 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001106 EXPECT_EQ(readValue, testValue);
1107}
1108
Martijn Coenen82c75312019-07-24 15:18:30 +02001109TEST_F(BinderLibTest, BufRejected) {
1110 Parcel data, reply;
1111 uint32_t buf;
1112 sp<IBinder> server = addServer();
1113 ASSERT_TRUE(server != nullptr);
1114
1115 binder_buffer_object obj {
1116 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001117 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001118 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1119 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001120 };
1121 data.setDataCapacity(1024);
1122 // Write a bogus object at offset 0 to get an entry in the offset table
1123 data.writeFileDescriptor(0);
1124 EXPECT_EQ(data.objectsCount(), 1);
1125 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1126 // And now, overwrite it with the buffer object
1127 memcpy(parcelData, &obj, sizeof(obj));
1128 data.setDataSize(sizeof(obj));
1129
Martijn Coenen82c75312019-07-24 15:18:30 +02001130 // Either the kernel should reject this transaction (if it's correct), but
1131 // if it's not, the server implementation should return an error if it
1132 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001133 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1134 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001135}
1136
Steven Moreland254e8ef2021-04-19 22:28:50 +00001137TEST_F(BinderLibTest, GotSid) {
1138 sp<IBinder> server = addServer();
1139
1140 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001141 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001142}
1143
Riley Andrews06b01ad2014-12-18 12:10:08 -08001144class BinderLibTestService : public BBinder
1145{
1146 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001147 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001148 : m_id(id)
1149 , m_nextServerId(id + 1)
1150 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001151 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001152 {
Yi Kong91635562018-06-07 14:38:36 -07001153 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1154 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001155 }
1156 ~BinderLibTestService()
1157 {
1158 exit(EXIT_SUCCESS);
1159 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001160
1161 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001162 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001163 Parcel data;
1164 data.writeInt32(NO_ERROR);
1165 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001166 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001167 }
1168 }
1169
Riley Andrews06b01ad2014-12-18 12:10:08 -08001170 virtual status_t onTransact(uint32_t code,
1171 const Parcel& data, Parcel* reply,
1172 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001173 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1174 return PERMISSION_DENIED;
1175 }
1176 switch (code) {
1177 case BINDER_LIB_TEST_REGISTER_SERVER: {
1178 int32_t id;
1179 sp<IBinder> binder;
1180 id = data.readInt32();
1181 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001182 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001183 return BAD_VALUE;
1184 }
1185
1186 if (m_id != 0)
1187 return INVALID_OPERATION;
1188
1189 pthread_mutex_lock(&m_serverWaitMutex);
1190 if (m_serverStartRequested) {
1191 m_serverStartRequested = false;
1192 m_serverStarted = binder;
1193 pthread_cond_signal(&m_serverWaitCond);
1194 }
1195 pthread_mutex_unlock(&m_serverWaitMutex);
1196 return NO_ERROR;
1197 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001198 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 case BINDER_LIB_TEST_ADD_SERVER: {
1200 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001201 int serverid;
1202
1203 if (m_id != 0) {
1204 return INVALID_OPERATION;
1205 }
1206 pthread_mutex_lock(&m_serverWaitMutex);
1207 if (m_serverStartRequested) {
1208 ret = -EBUSY;
1209 } else {
1210 serverid = m_nextServerId++;
1211 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001212 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001213
1214 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001215 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 pthread_mutex_lock(&m_serverWaitMutex);
1217 }
1218 if (ret > 0) {
1219 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001220 struct timespec ts;
1221 clock_gettime(CLOCK_REALTIME, &ts);
1222 ts.tv_sec += 5;
1223 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001224 }
1225 if (m_serverStartRequested) {
1226 m_serverStartRequested = false;
1227 ret = -ETIMEDOUT;
1228 } else {
1229 reply->writeStrongBinder(m_serverStarted);
1230 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001231 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001232 ret = NO_ERROR;
1233 }
1234 } else if (ret >= 0) {
1235 m_serverStartRequested = false;
1236 ret = UNKNOWN_ERROR;
1237 }
1238 pthread_mutex_unlock(&m_serverWaitMutex);
1239 return ret;
1240 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001241 case BINDER_LIB_TEST_GETPID:
1242 reply->writeInt32(getpid());
1243 return NO_ERROR;
1244 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1245 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001246 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001247 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001248 // oneway error codes should be ignored
1249 if (flags & TF_ONE_WAY) {
1250 return UNKNOWN_ERROR;
1251 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001252 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001253 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1254 // Note: this transaction is only designed for use with a
1255 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001256 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001257 // A callback was already pending; this means that
1258 // we received a second call while still processing
1259 // the first one. Fail the test.
1260 sp<IBinder> callback = data.readStrongBinder();
1261 Parcel data2;
1262 data2.writeInt32(UNKNOWN_ERROR);
1263
Yi Kong91635562018-06-07 14:38:36 -07001264 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001265 } else {
1266 m_callback = data.readStrongBinder();
1267 int32_t delayUs = data.readInt32();
1268 /*
1269 * It's necessary that we sleep here, so the next
1270 * transaction the caller makes will be queued to
1271 * the async queue.
1272 */
1273 usleep(delayUs);
1274
1275 /*
1276 * Now when we return, libbinder will tell the kernel
1277 * we are done with this transaction, and the kernel
1278 * can move the queued transaction to either the
1279 * thread todo worklist (for kernels without the fix),
1280 * or the proc todo worklist. In case of the former,
1281 * the next outbound call will pick up the pending
1282 * transaction, which leads to undesired reentrant
1283 * behavior. This is caught in the if() branch above.
1284 */
1285 }
1286
1287 return NO_ERROR;
1288 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001289 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1290 Parcel data2, reply2;
1291 sp<IBinder> binder;
1292 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001293 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001294 return BAD_VALUE;
1295 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001296 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001297 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1298 return NO_ERROR;
1299 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001300 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1301 reply->writeStrongBinder(this);
1302 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001303 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1304 reply->writeInt32(m_id);
1305 return NO_ERROR;
1306 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1307 int32_t count;
1308 uint32_t indirect_code;
1309 sp<IBinder> binder;
1310
1311 count = data.readInt32();
1312 reply->writeInt32(m_id);
1313 reply->writeInt32(count);
1314 for (int i = 0; i < count; i++) {
1315 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001316 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001317 return BAD_VALUE;
1318 }
1319 indirect_code = data.readInt32();
1320 BinderLibTestBundle data2(&data);
1321 if (!data2.isValid()) {
1322 return BAD_VALUE;
1323 }
1324 BinderLibTestBundle reply2;
1325 binder->transact(indirect_code, data2, &reply2);
1326 reply2.appendTo(reply);
1327 }
1328 return NO_ERROR;
1329 }
1330 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1331 reply->setError(data.readInt32());
1332 return NO_ERROR;
1333 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1334 reply->writeInt32(sizeof(void *));
1335 return NO_ERROR;
1336 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1337 return NO_ERROR;
1338 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1339 m_strongRef = data.readStrongBinder();
1340 return NO_ERROR;
1341 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1342 int ret;
1343 Parcel data2, reply2;
1344 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1345 sp<IBinder> target;
1346 sp<IBinder> callback;
1347
1348 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001349 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001350 return BAD_VALUE;
1351 }
1352 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001353 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001354 return BAD_VALUE;
1355 }
1356 ret = target->linkToDeath(testDeathRecipient);
1357 if (ret == NO_ERROR)
1358 ret = testDeathRecipient->waitEvent(5);
1359 data2.writeInt32(ret);
1360 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1361 return NO_ERROR;
1362 }
1363 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1364 int ret;
1365 int32_t size;
1366 const void *buf;
1367 int fd;
1368
1369 fd = data.readFileDescriptor();
1370 if (fd < 0) {
1371 return BAD_VALUE;
1372 }
1373 ret = data.readInt32(&size);
1374 if (ret != NO_ERROR) {
1375 return ret;
1376 }
1377 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001378 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001379 return BAD_VALUE;
1380 }
1381 ret = write(fd, buf, size);
1382 if (ret != size)
1383 return UNKNOWN_ERROR;
1384 return NO_ERROR;
1385 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001386 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1387 int ret;
1388 int32_t size;
1389 const void *buf;
1390 android::base::unique_fd fd;
1391
1392 ret = data.readUniqueParcelFileDescriptor(&fd);
1393 if (ret != NO_ERROR) {
1394 return ret;
1395 }
1396 ret = data.readInt32(&size);
1397 if (ret != NO_ERROR) {
1398 return ret;
1399 }
1400 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001401 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001402 return BAD_VALUE;
1403 }
1404 ret = write(fd.get(), buf, size);
1405 if (ret != size) return UNKNOWN_ERROR;
1406 return NO_ERROR;
1407 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001408 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1409 alarm(10);
1410 return NO_ERROR;
1411 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001412 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001413 ;
1414 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001415 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001416 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001417 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001418 return NO_ERROR;
1419 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001420 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1421 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001422 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001423 return NO_ERROR;
1424 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001425 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1426 int policy = 0;
1427 sched_param param;
1428 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1429 return UNKNOWN_ERROR;
1430 }
1431 reply->writeInt32(policy);
1432 reply->writeInt32(param.sched_priority);
1433 return NO_ERROR;
1434 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001435 case BINDER_LIB_TEST_ECHO_VECTOR: {
1436 std::vector<uint64_t> vector;
1437 auto err = data.readUint64Vector(&vector);
1438 if (err != NO_ERROR)
1439 return err;
1440 reply->writeUint64Vector(vector);
1441 return NO_ERROR;
1442 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001443 case BINDER_LIB_TEST_REJECT_BUF: {
1444 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1445 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001446 case BINDER_LIB_TEST_CAN_GET_SID: {
1447 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1448 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001449 default:
1450 return UNKNOWN_TRANSACTION;
1451 };
1452 }
1453 private:
1454 int32_t m_id;
1455 int32_t m_nextServerId;
1456 pthread_mutex_t m_serverWaitMutex;
1457 pthread_cond_t m_serverWaitCond;
1458 bool m_serverStartRequested;
1459 sp<IBinder> m_serverStarted;
1460 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001461 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001462};
1463
Martijn Coenen45b07b42017-08-09 12:07:45 +02001464int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001465{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001466 binderLibTestServiceName += String16(binderserversuffix);
1467
Riley Andrews06b01ad2014-12-18 12:10:08 -08001468 status_t ret;
1469 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001470 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001471 {
1472 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001473
Steven Morelandbf1915b2020-07-16 22:43:02 +00001474 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1475
Steven Morelandcf03cf12020-12-04 02:58:40 +00001476 testService->setInheritRt(true);
1477
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001478 /*
1479 * Normally would also contain functionality as well, but we are only
1480 * testing the extension mechanism.
1481 */
1482 testService->setExtension(new BBinder());
1483
Martijn Coenen82c75312019-07-24 15:18:30 +02001484 // Required for test "BufRejected'
1485 testService->setRequestingSid(true);
1486
Martijn Coenen45b07b42017-08-09 12:07:45 +02001487 /*
1488 * We need this below, but can't hold a sp<> because it prevents the
1489 * node from being cleaned up automatically. It's safe in this case
1490 * because of how the tests are written.
1491 */
1492 testServicePtr = testService.get();
1493
Riley Andrews06b01ad2014-12-18 12:10:08 -08001494 if (index == 0) {
1495 ret = sm->addService(binderLibTestServiceName, testService);
1496 } else {
1497 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1498 Parcel data, reply;
1499 data.writeInt32(index);
1500 data.writeStrongBinder(testService);
1501
1502 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1503 }
1504 }
1505 write(readypipefd, &ret, sizeof(ret));
1506 close(readypipefd);
1507 //printf("%s: ret %d\n", __func__, ret);
1508 if (ret)
1509 return 1;
1510 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001511 if (usePoll) {
1512 int fd;
1513 struct epoll_event ev;
1514 int epoll_fd;
1515 IPCThreadState::self()->setupPolling(&fd);
1516 if (fd < 0) {
1517 return 1;
1518 }
1519 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1520
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001521 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001522 if (epoll_fd == -1) {
1523 return 1;
1524 }
1525
1526 ev.events = EPOLLIN;
1527 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1528 return 1;
1529 }
1530
1531 while (1) {
1532 /*
1533 * We simulate a single-threaded process using the binder poll
1534 * interface; besides handling binder commands, it can also
1535 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001536 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001537 *
1538 * processPendingCall() will then issue that transaction.
1539 */
1540 struct epoll_event events[1];
1541 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1542 if (numEvents < 0) {
1543 if (errno == EINTR) {
1544 continue;
1545 }
1546 return 1;
1547 }
1548 if (numEvents > 0) {
1549 IPCThreadState::self()->handlePolledCommands();
1550 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1551 testServicePtr->processPendingCall();
1552 }
1553 }
1554 } else {
1555 ProcessState::self()->startThreadPool();
1556 IPCThreadState::self()->joinThreadPool();
1557 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001558 //printf("%s: joinThreadPool returned\n", __func__);
1559 return 1; /* joinThreadPool should not return */
1560}
1561
1562int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001563 ExitIfWrongAbi();
1564
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001565 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001566 binderservername = argv[2];
1567 } else {
1568 binderservername = argv[0];
1569 }
1570
Martijn Coenen45b07b42017-08-09 12:07:45 +02001571 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1572 binderserversuffix = argv[5];
1573 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001574 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001575 binderserversuffix = new char[16];
1576 snprintf(binderserversuffix, 16, "%d", getpid());
1577 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001578
1579 ::testing::InitGoogleTest(&argc, argv);
1580 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1581 ProcessState::self()->startThreadPool();
1582 return RUN_ALL_TESTS();
1583}