blob: 963d7b327163eddd1d78ac7124d84757ffc3c6e4 [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,
Steven Moreland35626652021-05-15 01:32:04 +000076 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080077 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070078 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020079 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080080 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070081 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080082 BINDER_LIB_TEST_GET_ID_TRANSACTION,
83 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
84 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
85 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
86 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
87 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
88 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090089 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080090 BINDER_LIB_TEST_EXIT_TRANSACTION,
91 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
92 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070093 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010094 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000095 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070096 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
97 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080098 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020099 BINDER_LIB_TEST_REJECT_BUF,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000100 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800101};
102
Martijn Coenen45b07b42017-08-09 12:07:45 +0200103pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800104{
105 int ret;
106 pid_t pid;
107 status_t status;
108 int pipefd[2];
109 char stri[16];
110 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200111 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800112 char *childargv[] = {
113 binderservername,
114 binderserverarg,
115 stri,
116 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200117 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700118 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700119 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800120 };
121
122 ret = pipe(pipefd);
123 if (ret < 0)
124 return ret;
125
126 snprintf(stri, sizeof(stri), "%d", arg2);
127 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200128 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800129
130 pid = fork();
131 if (pid == -1)
132 return pid;
133 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800134 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800135 close(pipefd[0]);
136 execv(binderservername, childargv);
137 status = -errno;
138 write(pipefd[1], &status, sizeof(status));
139 fprintf(stderr, "execv failed, %s\n", strerror(errno));
140 _exit(EXIT_FAILURE);
141 }
142 close(pipefd[1]);
143 ret = read(pipefd[0], &status, sizeof(status));
144 //printf("pipe read returned %d, status %d\n", ret, status);
145 close(pipefd[0]);
146 if (ret == sizeof(status)) {
147 ret = status;
148 } else {
149 kill(pid, SIGKILL);
150 if (ret >= 0) {
151 ret = NO_INIT;
152 }
153 }
154 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700155 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800156 return ret;
157 }
158 return pid;
159}
160
161class BinderLibTestEnv : public ::testing::Environment {
162 public:
163 BinderLibTestEnv() {}
164 sp<IBinder> getServer(void) {
165 return m_server;
166 }
167
168 private:
169 virtual void SetUp() {
170 m_serverpid = start_server_process(0);
171 //printf("m_serverpid %d\n", m_serverpid);
172 ASSERT_GT(m_serverpid, 0);
173
174 sp<IServiceManager> sm = defaultServiceManager();
175 //printf("%s: pid %d, get service\n", __func__, m_pid);
176 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700177 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800178 //printf("%s: pid %d, get service done\n", __func__, m_pid);
179 }
180 virtual void TearDown() {
181 status_t ret;
182 Parcel data, reply;
183 int exitStatus;
184 pid_t pid;
185
186 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700187 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800188 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
189 EXPECT_EQ(0, ret);
190 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
191 EXPECT_EQ(0, ret);
192 }
193 if (m_serverpid > 0) {
194 //printf("wait for %d\n", m_pids[i]);
195 pid = wait(&exitStatus);
196 EXPECT_EQ(m_serverpid, pid);
197 EXPECT_TRUE(WIFEXITED(exitStatus));
198 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
199 }
200 }
201
202 pid_t m_serverpid;
203 sp<IBinder> m_server;
204};
205
206class BinderLibTest : public ::testing::Test {
207 public:
208 virtual void SetUp() {
209 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000210 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800211 }
212 virtual void TearDown() {
213 }
214 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200215 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800217 int32_t id;
218 Parcel data, reply;
219 sp<IBinder> binder;
220
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700221 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800222
Yi Kong91635562018-06-07 14:38:36 -0700223 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800224 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700225 EXPECT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700226 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800227 if (idPtr)
228 *idPtr = id;
229 return binder;
230 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200231
Yi Kong91635562018-06-07 14:38:36 -0700232 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200233 {
234 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
235 }
236
Yi Kong91635562018-06-07 14:38:36 -0700237 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200238 {
239 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
240 }
241
Riley Andrews06b01ad2014-12-18 12:10:08 -0800242 void waitForReadData(int fd, int timeout_ms) {
243 int ret;
244 pollfd pfd = pollfd();
245
246 pfd.fd = fd;
247 pfd.events = POLLIN;
248 ret = poll(&pfd, 1, timeout_ms);
249 EXPECT_EQ(1, ret);
250 }
251
252 sp<IBinder> m_server;
253};
254
255class BinderLibTestBundle : public Parcel
256{
257 public:
258 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800259 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800260 int32_t mark;
261 int32_t bundleLen;
262 size_t pos;
263
264 if (source->readInt32(&mark))
265 return;
266 if (mark != MARK_START)
267 return;
268 if (source->readInt32(&bundleLen))
269 return;
270 pos = source->dataPosition();
271 if (Parcel::appendFrom(source, pos, bundleLen))
272 return;
273 source->setDataPosition(pos + bundleLen);
274 if (source->readInt32(&mark))
275 return;
276 if (mark != MARK_END)
277 return;
278 m_isValid = true;
279 setDataPosition(0);
280 }
281 void appendTo(Parcel *dest) {
282 dest->writeInt32(MARK_START);
283 dest->writeInt32(dataSize());
284 dest->appendFrom(this, 0, dataSize());
285 dest->writeInt32(MARK_END);
286 };
287 bool isValid(void) {
288 return m_isValid;
289 }
290 private:
291 enum {
292 MARK_START = B_PACK_CHARS('B','T','B','S'),
293 MARK_END = B_PACK_CHARS('B','T','B','E'),
294 };
295 bool m_isValid;
296};
297
298class BinderLibTestEvent
299{
300 public:
301 BinderLibTestEvent(void)
302 : m_eventTriggered(false)
303 {
Yi Kong91635562018-06-07 14:38:36 -0700304 pthread_mutex_init(&m_waitMutex, nullptr);
305 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800306 }
307 int waitEvent(int timeout_s)
308 {
309 int ret;
310 pthread_mutex_lock(&m_waitMutex);
311 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800312 struct timespec ts;
313 clock_gettime(CLOCK_REALTIME, &ts);
314 ts.tv_sec += timeout_s;
315 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800316 }
317 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
318 pthread_mutex_unlock(&m_waitMutex);
319 return ret;
320 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200321 pthread_t getTriggeringThread()
322 {
323 return m_triggeringThread;
324 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800325 protected:
326 void triggerEvent(void) {
327 pthread_mutex_lock(&m_waitMutex);
328 pthread_cond_signal(&m_waitCond);
329 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200330 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800331 pthread_mutex_unlock(&m_waitMutex);
332 };
333 private:
334 pthread_mutex_t m_waitMutex;
335 pthread_cond_t m_waitCond;
336 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200337 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800338};
339
340class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
341{
342 public:
343 BinderLibTestCallBack()
344 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700345 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800346 {
347 }
348 status_t getResult(void)
349 {
350 return m_result;
351 }
352
353 private:
354 virtual status_t onTransact(uint32_t code,
355 const Parcel& data, Parcel* reply,
356 uint32_t flags = 0)
357 {
358 (void)reply;
359 (void)flags;
360 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200361 case BINDER_LIB_TEST_CALL_BACK: {
362 status_t status = data.readInt32(&m_result);
363 if (status != NO_ERROR) {
364 m_result = status;
365 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800366 triggerEvent();
367 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200368 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700369 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
370 sp<IBinder> server;
371 int ret;
372 const uint8_t *buf = data.data();
373 size_t size = data.dataSize();
374 if (m_prev_end) {
375 /* 64-bit kernel needs at most 8 bytes to align buffer end */
376 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
377 } else {
378 EXPECT_TRUE(IsPageAligned((void *)buf));
379 }
380
381 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
382
383 if (size > 0) {
384 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
385 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
386 data, reply);
387 EXPECT_EQ(NO_ERROR, ret);
388 }
389 return NO_ERROR;
390 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800391 default:
392 return UNKNOWN_TRANSACTION;
393 }
394 }
395
396 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700397 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800398};
399
400class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
401{
402 private:
403 virtual void binderDied(const wp<IBinder>& who) {
404 (void)who;
405 triggerEvent();
406 };
407};
408
409TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800410 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700411 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
412 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800413}
414
Steven Moreland80844f72020-12-12 02:06:08 +0000415TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000416 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700417 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
418 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000419}
420
Steven Morelandf183fdd2020-10-27 00:12:12 +0000421TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000422 Parcel data, reply;
423 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700424 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
425 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000426}
427
Marco Ballesio7ee17572020-09-08 10:30:03 -0700428TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700429 Parcel data, reply, replypid;
430 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
431
432 //Pass test on devices where the freezer is not supported
433 if (freezer_file.fail()) {
434 GTEST_SKIP();
435 return;
436 }
437
438 std::string freezer_enabled;
439 std::getline(freezer_file, freezer_enabled);
440
441 //Pass test on devices where the freezer is disabled
442 if (freezer_enabled != "1") {
443 GTEST_SKIP();
444 return;
445 }
446
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700447 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700448 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700449 for (int i = 0; i < 10; i++) {
450 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
451 }
452 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
453 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
454 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
455 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700456
457 bool sync_received, async_received;
458
459 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
460 &async_received));
461
462 EXPECT_EQ(sync_received, 1);
463 EXPECT_EQ(async_received, 0);
464
Marco Ballesio7ee17572020-09-08 10:30:03 -0700465 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
466 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
467}
468
Riley Andrews06b01ad2014-12-18 12:10:08 -0800469TEST_F(BinderLibTest, SetError) {
470 int32_t testValue[] = { 0, -123, 123 };
471 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800472 Parcel data, reply;
473 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700474 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
475 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800476 }
477}
478
479TEST_F(BinderLibTest, GetId) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800480 int32_t id;
481 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700482 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply),
483 StatusEq(NO_ERROR));
484 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800485 EXPECT_EQ(0, id);
486}
487
488TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800489 int32_t ptrsize;
490 Parcel data, reply;
491 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700492 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700493 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
494 StatusEq(NO_ERROR));
495 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800496 RecordProperty("TestPtrSize", sizeof(void *));
497 RecordProperty("ServerPtrSize", sizeof(void *));
498}
499
500TEST_F(BinderLibTest, IndirectGetId2)
501{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800502 int32_t id;
503 int32_t count;
504 Parcel data, reply;
505 int32_t serverId[3];
506
507 data.writeInt32(ARRAY_SIZE(serverId));
508 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
509 sp<IBinder> server;
510 BinderLibTestBundle datai;
511
512 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700513 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800514 data.writeStrongBinder(server);
515 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
516 datai.appendTo(&data);
517 }
518
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700519 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
520 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800521
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700522 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800523 EXPECT_EQ(0, id);
524
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700525 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700526 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800527
528 for (size_t i = 0; i < (size_t)count; i++) {
529 BinderLibTestBundle replyi(&reply);
530 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700531 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800532 EXPECT_EQ(serverId[i], id);
533 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
534 }
535
536 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
537}
538
539TEST_F(BinderLibTest, IndirectGetId3)
540{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800541 int32_t id;
542 int32_t count;
543 Parcel data, reply;
544 int32_t serverId[3];
545
546 data.writeInt32(ARRAY_SIZE(serverId));
547 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
548 sp<IBinder> server;
549 BinderLibTestBundle datai;
550 BinderLibTestBundle datai2;
551
552 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700553 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800554 data.writeStrongBinder(server);
555 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
556
557 datai.writeInt32(1);
558 datai.writeStrongBinder(m_server);
559 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
560 datai2.appendTo(&datai);
561
562 datai.appendTo(&data);
563 }
564
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700565 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
566 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800567
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700568 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800569 EXPECT_EQ(0, id);
570
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700571 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700572 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800573
574 for (size_t i = 0; i < (size_t)count; i++) {
575 int32_t counti;
576
577 BinderLibTestBundle replyi(&reply);
578 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700579 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800580 EXPECT_EQ(serverId[i], id);
581
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700582 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800583 EXPECT_EQ(1, counti);
584
585 BinderLibTestBundle replyi2(&replyi);
586 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700587 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800588 EXPECT_EQ(0, id);
589 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
590
591 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
592 }
593
594 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
595}
596
597TEST_F(BinderLibTest, CallBack)
598{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800599 Parcel data, reply;
600 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
601 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700602 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
603 StatusEq(NO_ERROR));
604 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
605 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800606}
607
Steven Moreland35626652021-05-15 01:32:04 +0000608TEST_F(BinderLibTest, BinderCallContextGuard) {
609 sp<IBinder> binder = addServer();
610 Parcel data, reply;
611 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
612 StatusEq(DEAD_OBJECT));
613}
614
Riley Andrews06b01ad2014-12-18 12:10:08 -0800615TEST_F(BinderLibTest, AddServer)
616{
617 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700618 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619}
620
Riley Andrews06b01ad2014-12-18 12:10:08 -0800621TEST_F(BinderLibTest, DeathNotificationStrongRef)
622{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800623 sp<IBinder> sbinder;
624
625 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
626
627 {
628 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700629 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700630 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800631 sbinder = binder;
632 }
633 {
634 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700635 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
636 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800637 }
638 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700639 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
640 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800641}
642
643TEST_F(BinderLibTest, DeathNotificationMultiple)
644{
645 status_t ret;
646 const int clientcount = 2;
647 sp<IBinder> target;
648 sp<IBinder> linkedclient[clientcount];
649 sp<BinderLibTestCallBack> callBack[clientcount];
650 sp<IBinder> passiveclient[clientcount];
651
652 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700653 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800654 for (int i = 0; i < clientcount; i++) {
655 {
656 Parcel data, reply;
657
658 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700659 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800660 callBack[i] = new BinderLibTestCallBack();
661 data.writeStrongBinder(target);
662 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700663 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
664 &reply, TF_ONE_WAY),
665 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800666 }
667 {
668 Parcel data, reply;
669
670 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700671 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800672 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700673 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
674 &reply, TF_ONE_WAY),
675 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800676 }
677 }
678 {
679 Parcel data, reply;
680 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
681 EXPECT_EQ(0, ret);
682 }
683
684 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700685 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
686 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800687 }
688}
689
Martijn Coenenf7100e42017-07-31 12:14:09 +0200690TEST_F(BinderLibTest, DeathNotificationThread)
691{
692 status_t ret;
693 sp<BinderLibTestCallBack> callback;
694 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700695 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200696 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700697 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200698
699 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
700
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700701 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200702
703 {
704 Parcel data, reply;
705 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
706 EXPECT_EQ(0, ret);
707 }
708
709 /* Make sure it's dead */
710 testDeathRecipient->waitEvent(5);
711
712 /* Now, pass the ref to another process and ask that process to
713 * call linkToDeath() on it, and wait for a response. This tests
714 * two things:
715 * 1) You still get death notifications when calling linkToDeath()
716 * on a ref that is already dead when it was passed to you.
717 * 2) That death notifications are not directly pushed to the thread
718 * registering them, but to the threadpool (proc workqueue) instead.
719 *
720 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
721 * is blocked on a condition variable waiting for the death notification to be
722 * called; therefore, that thread is not available for handling proc work.
723 * So, if the death notification was pushed to the thread workqueue, the callback
724 * would never be called, and the test would timeout and fail.
725 *
726 * Note that we can't do this part of the test from this thread itself, because
727 * the binder driver would only push death notifications to the thread if
728 * it is a looper thread, which this thread is not.
729 *
730 * See b/23525545 for details.
731 */
732 {
733 Parcel data, reply;
734
735 callback = new BinderLibTestCallBack();
736 data.writeStrongBinder(target);
737 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700738 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
739 TF_ONE_WAY),
740 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200741 }
742
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700743 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
744 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200745}
746
Riley Andrews06b01ad2014-12-18 12:10:08 -0800747TEST_F(BinderLibTest, PassFile) {
748 int ret;
749 int pipefd[2];
750 uint8_t buf[1] = { 0 };
751 uint8_t write_value = 123;
752
753 ret = pipe2(pipefd, O_NONBLOCK);
754 ASSERT_EQ(0, ret);
755
756 {
757 Parcel data, reply;
758 uint8_t writebuf[1] = { write_value };
759
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700760 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800761
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700762 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700764 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800765
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700766 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
767 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768 }
769
770 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700771 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772 EXPECT_EQ(write_value, buf[0]);
773
774 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
775
776 ret = read(pipefd[0], buf, sizeof(buf));
777 EXPECT_EQ(0, ret);
778
779 close(pipefd[0]);
780}
781
Ryo Hashimotobf551892018-05-31 16:58:35 +0900782TEST_F(BinderLibTest, PassParcelFileDescriptor) {
783 const int datasize = 123;
784 std::vector<uint8_t> writebuf(datasize);
785 for (size_t i = 0; i < writebuf.size(); ++i) {
786 writebuf[i] = i;
787 }
788
789 android::base::unique_fd read_end, write_end;
790 {
791 int pipefd[2];
792 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
793 read_end.reset(pipefd[0]);
794 write_end.reset(pipefd[1]);
795 }
796 {
797 Parcel data;
798 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
799 write_end.reset();
800 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
801 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
802
803 Parcel reply;
804 EXPECT_EQ(NO_ERROR,
805 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
806 &reply));
807 }
808 std::vector<uint8_t> readbuf(datasize);
809 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
810 EXPECT_EQ(writebuf, readbuf);
811
812 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
813
814 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
815}
816
Riley Andrews06b01ad2014-12-18 12:10:08 -0800817TEST_F(BinderLibTest, PromoteLocal) {
818 sp<IBinder> strong = new BBinder();
819 wp<IBinder> weak = strong;
820 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700821 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800822 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700823 strong = nullptr;
824 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800825 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700826 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800827}
828
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700829TEST_F(BinderLibTest, LocalGetExtension) {
830 sp<BBinder> binder = new BBinder();
831 sp<IBinder> ext = new BBinder();
832 binder->setExtension(ext);
833 EXPECT_EQ(ext, binder->getExtension());
834}
835
836TEST_F(BinderLibTest, RemoteGetExtension) {
837 sp<IBinder> server = addServer();
838 ASSERT_TRUE(server != nullptr);
839
840 sp<IBinder> extension;
841 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
842 ASSERT_NE(nullptr, extension.get());
843
844 EXPECT_EQ(NO_ERROR, extension->pingBinder());
845}
846
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700847TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700848 Parcel data, reply;
849
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700850 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
851 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700852
853 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700854 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800855 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
856 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
857 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
858 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700859}
860
Connor O'Brien52be2c92016-09-20 14:18:08 -0700861TEST_F(BinderLibTest, FreedBinder) {
862 status_t ret;
863
864 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700865 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700866
867 __u32 freedHandle;
868 wp<IBinder> keepFreedBinder;
869 {
870 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700871 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
872 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700873 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
874 freedHandle = freed->handle;
875 /* Add a weak ref to the freed binder so the driver does not
876 * delete its reference to it - otherwise the transaction
877 * fails regardless of whether the driver is fixed.
878 */
Steven Morelande171d622019-07-17 16:06:01 -0700879 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700880 }
Steven Morelande171d622019-07-17 16:06:01 -0700881 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700882 {
883 Parcel data, reply;
884 data.writeStrongBinder(server);
885 /* Replace original handle with handle to the freed binder */
886 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
887 __u32 oldHandle = strong->handle;
888 strong->handle = freedHandle;
889 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
890 /* Returns DEAD_OBJECT (-32) if target crashes and
891 * FAILED_TRANSACTION if the driver rejects the invalid
892 * object.
893 */
894 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
895 /* Restore original handle so parcel destructor does not use
896 * the wrong handle.
897 */
898 strong->handle = oldHandle;
899 }
900}
901
Steven Morelandd7088702021-01-13 00:27:00 +0000902TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
903 sp<IBinder> server = addServer();
904 ASSERT_TRUE(server != nullptr);
905
906 Parcel data;
907 sp<ParcelRef> reply = ParcelRef::create();
908
909 // when we have a Parcel which is deleted on another thread, if it gets
910 // deleted, it will tell the kernel this, and it will drop strong references
911 // to binder, so that we can't BR_ACQUIRE would fail
912 IPCThreadState::self()->createTransactionReference(reply.get());
913 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
914 data,
915 reply.get()));
916
917 // we have sp to binder, but it is not actually acquired by kernel, the
918 // transaction is sitting on an out buffer
919 sp<IBinder> binder = reply->readStrongBinder();
920
921 std::thread([&] {
922 // without the transaction reference, this would cause the Parcel to be
923 // deallocated before the first thread flushes BR_ACQUIRE
924 reply = nullptr;
925 IPCThreadState::self()->flushCommands();
926 }).join();
927
928 ASSERT_NE(nullptr, binder);
929 ASSERT_EQ(NO_ERROR, binder->pingBinder());
930}
931
Sherry Yang336cdd32017-07-24 14:12:27 -0700932TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700933 Parcel data, reply;
934 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
935 for (int i = 0; i < 2; i++) {
936 BinderLibTestBundle datai;
937 datai.appendFrom(&data, 0, data.dataSize());
938
939 data.freeData();
940 data.writeInt32(1);
941 data.writeStrongBinder(callBack);
942 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
943
944 datai.appendTo(&data);
945 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700946 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
947 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700948}
949
Martijn Coenen45b07b42017-08-09 12:07:45 +0200950TEST_F(BinderLibTest, OnewayQueueing)
951{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200952 Parcel data, data2;
953
954 sp<IBinder> pollServer = addPollServer();
955
956 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
957 data.writeStrongBinder(callBack);
958 data.writeInt32(500000); // delay in us before calling back
959
960 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
961 data2.writeStrongBinder(callBack2);
962 data2.writeInt32(0); // delay in us
963
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700964 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
965 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200966
967 // The delay ensures that this second transaction will end up on the async_todo list
968 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700969 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
970 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200971
972 // The server will ensure that the two transactions are handled in the expected order;
973 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700974 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
975 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200976
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700977 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
978 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200979}
980
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100981TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
982{
983 status_t ret;
984 Parcel data, reply;
985 data.writeInterfaceToken(binderLibTestServiceName);
986 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
987 EXPECT_EQ(-1, reply.readInt32());
988 EXPECT_EQ(NO_ERROR, ret);
989}
990
991TEST_F(BinderLibTest, WorkSourceSet)
992{
993 status_t ret;
994 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000995 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000996 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100997 data.writeInterfaceToken(binderLibTestServiceName);
998 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
999 EXPECT_EQ(100, reply.readInt32());
1000 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001001 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1002 EXPECT_EQ(NO_ERROR, ret);
1003}
1004
1005TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1006{
1007 status_t ret;
1008 Parcel data, reply;
1009
1010 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1011 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1012
1013 data.writeInterfaceToken(binderLibTestServiceName);
1014 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1015 EXPECT_EQ(-1, reply.readInt32());
1016 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001017 EXPECT_EQ(NO_ERROR, ret);
1018}
1019
1020TEST_F(BinderLibTest, WorkSourceCleared)
1021{
1022 status_t ret;
1023 Parcel data, reply;
1024
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001025 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001026 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1027 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001028 data.writeInterfaceToken(binderLibTestServiceName);
1029 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1030
1031 EXPECT_EQ(-1, reply.readInt32());
1032 EXPECT_EQ(100, previousWorkSource);
1033 EXPECT_EQ(NO_ERROR, ret);
1034}
1035
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001036TEST_F(BinderLibTest, WorkSourceRestored)
1037{
1038 status_t ret;
1039 Parcel data, reply;
1040
1041 IPCThreadState::self()->setCallingWorkSourceUid(100);
1042 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1043 IPCThreadState::self()->restoreCallingWorkSource(token);
1044
1045 data.writeInterfaceToken(binderLibTestServiceName);
1046 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1047
1048 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001049 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001050 EXPECT_EQ(NO_ERROR, ret);
1051}
1052
Olivier Gaillard91a04802018-11-14 17:32:41 +00001053TEST_F(BinderLibTest, PropagateFlagSet)
1054{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001055 IPCThreadState::self()->clearPropagateWorkSource();
1056 IPCThreadState::self()->setCallingWorkSourceUid(100);
1057 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1058}
1059
1060TEST_F(BinderLibTest, PropagateFlagCleared)
1061{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001062 IPCThreadState::self()->setCallingWorkSourceUid(100);
1063 IPCThreadState::self()->clearPropagateWorkSource();
1064 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1065}
1066
1067TEST_F(BinderLibTest, PropagateFlagRestored)
1068{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001069 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1070 IPCThreadState::self()->restoreCallingWorkSource(token);
1071
1072 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1073}
1074
1075TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1076{
1077 IPCThreadState::self()->setCallingWorkSourceUid(100);
1078
1079 Parcel data, reply;
1080 status_t ret;
1081 data.writeInterfaceToken(binderLibTestServiceName);
1082 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1083
1084 Parcel data2, reply2;
1085 status_t ret2;
1086 data2.writeInterfaceToken(binderLibTestServiceName);
1087 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1088 EXPECT_EQ(100, reply2.readInt32());
1089 EXPECT_EQ(NO_ERROR, ret2);
1090}
1091
Steven Morelandbf1915b2020-07-16 22:43:02 +00001092TEST_F(BinderLibTest, SchedPolicySet) {
1093 sp<IBinder> server = addServer();
1094 ASSERT_TRUE(server != nullptr);
1095
1096 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001097 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1098 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001099
1100 int policy = reply.readInt32();
1101 int priority = reply.readInt32();
1102
1103 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1104 EXPECT_EQ(kSchedPriority, priority);
1105}
1106
Steven Morelandcf03cf12020-12-04 02:58:40 +00001107TEST_F(BinderLibTest, InheritRt) {
1108 sp<IBinder> server = addServer();
1109 ASSERT_TRUE(server != nullptr);
1110
1111 const struct sched_param param {
1112 .sched_priority = kSchedPriorityMore,
1113 };
1114 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1115
1116 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001117 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1118 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001119
1120 int policy = reply.readInt32();
1121 int priority = reply.readInt32();
1122
1123 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1124 EXPECT_EQ(kSchedPriorityMore, priority);
1125}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001126
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001127TEST_F(BinderLibTest, VectorSent) {
1128 Parcel data, reply;
1129 sp<IBinder> server = addServer();
1130 ASSERT_TRUE(server != nullptr);
1131
1132 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1133 data.writeUint64Vector(testValue);
1134
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001135 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001136 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001137 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001138 EXPECT_EQ(readValue, testValue);
1139}
1140
Martijn Coenen82c75312019-07-24 15:18:30 +02001141TEST_F(BinderLibTest, BufRejected) {
1142 Parcel data, reply;
1143 uint32_t buf;
1144 sp<IBinder> server = addServer();
1145 ASSERT_TRUE(server != nullptr);
1146
1147 binder_buffer_object obj {
1148 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001149 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001150 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1151 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001152 };
1153 data.setDataCapacity(1024);
1154 // Write a bogus object at offset 0 to get an entry in the offset table
1155 data.writeFileDescriptor(0);
1156 EXPECT_EQ(data.objectsCount(), 1);
1157 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1158 // And now, overwrite it with the buffer object
1159 memcpy(parcelData, &obj, sizeof(obj));
1160 data.setDataSize(sizeof(obj));
1161
Martijn Coenen82c75312019-07-24 15:18:30 +02001162 // Either the kernel should reject this transaction (if it's correct), but
1163 // if it's not, the server implementation should return an error if it
1164 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001165 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1166 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001167}
1168
Steven Moreland254e8ef2021-04-19 22:28:50 +00001169TEST_F(BinderLibTest, GotSid) {
1170 sp<IBinder> server = addServer();
1171
1172 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001173 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001174}
1175
Yifan Hong543edcd2021-05-18 19:47:30 -07001176class BinderLibTestService : public BBinder {
1177public:
1178 explicit BinderLibTestService(int32_t id)
1179 : m_id(id), m_nextServerId(id + 1), m_serverStartRequested(false), m_callback(nullptr) {
1180 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1181 pthread_cond_init(&m_serverWaitCond, nullptr);
1182 }
1183 ~BinderLibTestService() { exit(EXIT_SUCCESS); }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001184
Yifan Hong543edcd2021-05-18 19:47:30 -07001185 void processPendingCall() {
1186 if (m_callback != nullptr) {
1187 Parcel data;
1188 data.writeInt32(NO_ERROR);
1189 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1190 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001191 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001192 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001193
Yifan Hong543edcd2021-05-18 19:47:30 -07001194 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1195 uint32_t flags = 0) {
1196 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1197 return PERMISSION_DENIED;
1198 }
1199 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001200 case BINDER_LIB_TEST_REGISTER_SERVER: {
1201 int32_t id;
1202 sp<IBinder> binder;
1203 id = data.readInt32();
1204 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001205 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001206 return BAD_VALUE;
1207 }
1208
Yifan Hong543edcd2021-05-18 19:47:30 -07001209 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001210
1211 pthread_mutex_lock(&m_serverWaitMutex);
1212 if (m_serverStartRequested) {
1213 m_serverStartRequested = false;
1214 m_serverStarted = binder;
1215 pthread_cond_signal(&m_serverWaitCond);
1216 }
1217 pthread_mutex_unlock(&m_serverWaitMutex);
1218 return NO_ERROR;
1219 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001220 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001221 case BINDER_LIB_TEST_ADD_SERVER: {
1222 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001223 int serverid;
1224
1225 if (m_id != 0) {
1226 return INVALID_OPERATION;
1227 }
1228 pthread_mutex_lock(&m_serverWaitMutex);
1229 if (m_serverStartRequested) {
1230 ret = -EBUSY;
1231 } else {
1232 serverid = m_nextServerId++;
1233 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001234 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001235
1236 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001237 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001238 pthread_mutex_lock(&m_serverWaitMutex);
1239 }
1240 if (ret > 0) {
1241 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001242 struct timespec ts;
1243 clock_gettime(CLOCK_REALTIME, &ts);
1244 ts.tv_sec += 5;
1245 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001246 }
1247 if (m_serverStartRequested) {
1248 m_serverStartRequested = false;
1249 ret = -ETIMEDOUT;
1250 } else {
1251 reply->writeStrongBinder(m_serverStarted);
1252 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001253 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001254 ret = NO_ERROR;
1255 }
1256 } else if (ret >= 0) {
1257 m_serverStartRequested = false;
1258 ret = UNKNOWN_ERROR;
1259 }
1260 pthread_mutex_unlock(&m_serverWaitMutex);
1261 return ret;
1262 }
Steven Moreland35626652021-05-15 01:32:04 +00001263 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1264 IPCThreadState::SpGuard spGuard{
1265 .address = __builtin_frame_address(0),
1266 .context = "GuardInBinderTransaction",
1267 };
1268 const IPCThreadState::SpGuard *origGuard =
1269 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1270
1271 // if the guard works, this should abort
1272 (void)IPCThreadState::self()->getCallingPid();
1273
1274 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1275 return NO_ERROR;
1276 }
1277
Marco Ballesio7ee17572020-09-08 10:30:03 -07001278 case BINDER_LIB_TEST_GETPID:
1279 reply->writeInt32(getpid());
1280 return NO_ERROR;
1281 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1282 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001283 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001285 // oneway error codes should be ignored
1286 if (flags & TF_ONE_WAY) {
1287 return UNKNOWN_ERROR;
1288 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001289 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001290 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1291 // Note: this transaction is only designed for use with a
1292 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001293 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001294 // A callback was already pending; this means that
1295 // we received a second call while still processing
1296 // the first one. Fail the test.
1297 sp<IBinder> callback = data.readStrongBinder();
1298 Parcel data2;
1299 data2.writeInt32(UNKNOWN_ERROR);
1300
Yi Kong91635562018-06-07 14:38:36 -07001301 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001302 } else {
1303 m_callback = data.readStrongBinder();
1304 int32_t delayUs = data.readInt32();
1305 /*
1306 * It's necessary that we sleep here, so the next
1307 * transaction the caller makes will be queued to
1308 * the async queue.
1309 */
1310 usleep(delayUs);
1311
1312 /*
1313 * Now when we return, libbinder will tell the kernel
1314 * we are done with this transaction, and the kernel
1315 * can move the queued transaction to either the
1316 * thread todo worklist (for kernels without the fix),
1317 * or the proc todo worklist. In case of the former,
1318 * the next outbound call will pick up the pending
1319 * transaction, which leads to undesired reentrant
1320 * behavior. This is caught in the if() branch above.
1321 */
1322 }
1323
1324 return NO_ERROR;
1325 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001326 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1327 Parcel data2, reply2;
1328 sp<IBinder> binder;
1329 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001330 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001331 return BAD_VALUE;
1332 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001333 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001334 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1335 return NO_ERROR;
1336 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001337 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1338 reply->writeStrongBinder(this);
1339 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001340 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1341 reply->writeInt32(m_id);
1342 return NO_ERROR;
1343 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1344 int32_t count;
1345 uint32_t indirect_code;
1346 sp<IBinder> binder;
1347
1348 count = data.readInt32();
1349 reply->writeInt32(m_id);
1350 reply->writeInt32(count);
1351 for (int i = 0; i < count; i++) {
1352 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001353 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001354 return BAD_VALUE;
1355 }
1356 indirect_code = data.readInt32();
1357 BinderLibTestBundle data2(&data);
1358 if (!data2.isValid()) {
1359 return BAD_VALUE;
1360 }
1361 BinderLibTestBundle reply2;
1362 binder->transact(indirect_code, data2, &reply2);
1363 reply2.appendTo(reply);
1364 }
1365 return NO_ERROR;
1366 }
1367 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1368 reply->setError(data.readInt32());
1369 return NO_ERROR;
1370 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1371 reply->writeInt32(sizeof(void *));
1372 return NO_ERROR;
1373 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1374 return NO_ERROR;
1375 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1376 m_strongRef = data.readStrongBinder();
1377 return NO_ERROR;
1378 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1379 int ret;
1380 Parcel data2, reply2;
1381 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1382 sp<IBinder> target;
1383 sp<IBinder> callback;
1384
1385 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001386 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001387 return BAD_VALUE;
1388 }
1389 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001390 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001391 return BAD_VALUE;
1392 }
1393 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001394 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001395 data2.writeInt32(ret);
1396 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1397 return NO_ERROR;
1398 }
1399 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1400 int ret;
1401 int32_t size;
1402 const void *buf;
1403 int fd;
1404
1405 fd = data.readFileDescriptor();
1406 if (fd < 0) {
1407 return BAD_VALUE;
1408 }
1409 ret = data.readInt32(&size);
1410 if (ret != NO_ERROR) {
1411 return ret;
1412 }
1413 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001414 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001415 return BAD_VALUE;
1416 }
1417 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001418 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001419 return NO_ERROR;
1420 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001421 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1422 int ret;
1423 int32_t size;
1424 const void *buf;
1425 android::base::unique_fd fd;
1426
1427 ret = data.readUniqueParcelFileDescriptor(&fd);
1428 if (ret != NO_ERROR) {
1429 return ret;
1430 }
1431 ret = data.readInt32(&size);
1432 if (ret != NO_ERROR) {
1433 return ret;
1434 }
1435 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001436 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001437 return BAD_VALUE;
1438 }
1439 ret = write(fd.get(), buf, size);
1440 if (ret != size) return UNKNOWN_ERROR;
1441 return NO_ERROR;
1442 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001443 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1444 alarm(10);
1445 return NO_ERROR;
1446 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001447 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001448 ;
1449 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001450 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001451 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001452 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001453 return NO_ERROR;
1454 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001455 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1456 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001457 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001458 return NO_ERROR;
1459 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001460 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1461 int policy = 0;
1462 sched_param param;
1463 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1464 return UNKNOWN_ERROR;
1465 }
1466 reply->writeInt32(policy);
1467 reply->writeInt32(param.sched_priority);
1468 return NO_ERROR;
1469 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001470 case BINDER_LIB_TEST_ECHO_VECTOR: {
1471 std::vector<uint64_t> vector;
1472 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001473 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001474 reply->writeUint64Vector(vector);
1475 return NO_ERROR;
1476 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001477 case BINDER_LIB_TEST_REJECT_BUF: {
1478 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1479 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001480 case BINDER_LIB_TEST_CAN_GET_SID: {
1481 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1482 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001483 default:
1484 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001485 };
1486 }
1487
1488private:
1489 int32_t m_id;
1490 int32_t m_nextServerId;
1491 pthread_mutex_t m_serverWaitMutex;
1492 pthread_cond_t m_serverWaitCond;
1493 bool m_serverStartRequested;
1494 sp<IBinder> m_serverStarted;
1495 sp<IBinder> m_strongRef;
1496 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001497};
1498
Martijn Coenen45b07b42017-08-09 12:07:45 +02001499int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001500{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001501 binderLibTestServiceName += String16(binderserversuffix);
1502
Steven Moreland35626652021-05-15 01:32:04 +00001503 // Testing to make sure that calls that we are serving can use getCallin*
1504 // even though we don't here.
1505 IPCThreadState::SpGuard spGuard{
1506 .address = __builtin_frame_address(0),
1507 .context = "main server thread",
1508 };
1509 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1510
Riley Andrews06b01ad2014-12-18 12:10:08 -08001511 status_t ret;
1512 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001513 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001514 {
1515 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001516
Steven Morelandbf1915b2020-07-16 22:43:02 +00001517 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1518
Steven Morelandcf03cf12020-12-04 02:58:40 +00001519 testService->setInheritRt(true);
1520
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001521 /*
1522 * Normally would also contain functionality as well, but we are only
1523 * testing the extension mechanism.
1524 */
1525 testService->setExtension(new BBinder());
1526
Martijn Coenen82c75312019-07-24 15:18:30 +02001527 // Required for test "BufRejected'
1528 testService->setRequestingSid(true);
1529
Martijn Coenen45b07b42017-08-09 12:07:45 +02001530 /*
1531 * We need this below, but can't hold a sp<> because it prevents the
1532 * node from being cleaned up automatically. It's safe in this case
1533 * because of how the tests are written.
1534 */
1535 testServicePtr = testService.get();
1536
Riley Andrews06b01ad2014-12-18 12:10:08 -08001537 if (index == 0) {
1538 ret = sm->addService(binderLibTestServiceName, testService);
1539 } else {
1540 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1541 Parcel data, reply;
1542 data.writeInt32(index);
1543 data.writeStrongBinder(testService);
1544
1545 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1546 }
1547 }
1548 write(readypipefd, &ret, sizeof(ret));
1549 close(readypipefd);
1550 //printf("%s: ret %d\n", __func__, ret);
1551 if (ret)
1552 return 1;
1553 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001554 if (usePoll) {
1555 int fd;
1556 struct epoll_event ev;
1557 int epoll_fd;
1558 IPCThreadState::self()->setupPolling(&fd);
1559 if (fd < 0) {
1560 return 1;
1561 }
1562 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1563
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001564 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001565 if (epoll_fd == -1) {
1566 return 1;
1567 }
1568
1569 ev.events = EPOLLIN;
1570 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1571 return 1;
1572 }
1573
1574 while (1) {
1575 /*
1576 * We simulate a single-threaded process using the binder poll
1577 * interface; besides handling binder commands, it can also
1578 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001579 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001580 *
1581 * processPendingCall() will then issue that transaction.
1582 */
1583 struct epoll_event events[1];
1584 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1585 if (numEvents < 0) {
1586 if (errno == EINTR) {
1587 continue;
1588 }
1589 return 1;
1590 }
1591 if (numEvents > 0) {
1592 IPCThreadState::self()->handlePolledCommands();
1593 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1594 testServicePtr->processPendingCall();
1595 }
1596 }
1597 } else {
1598 ProcessState::self()->startThreadPool();
1599 IPCThreadState::self()->joinThreadPool();
1600 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001601 //printf("%s: joinThreadPool returned\n", __func__);
1602 return 1; /* joinThreadPool should not return */
1603}
1604
1605int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001606 ExitIfWrongAbi();
1607
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001608 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001609 binderservername = argv[2];
1610 } else {
1611 binderservername = argv[0];
1612 }
1613
Martijn Coenen45b07b42017-08-09 12:07:45 +02001614 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1615 binderserversuffix = argv[5];
1616 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001617 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001618 binderserversuffix = new char[16];
1619 snprintf(binderserversuffix, 16, "%d", getpid());
1620 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001621
1622 ::testing::InitGoogleTest(&argc, argv);
1623 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1624 ProcessState::self()->startThreadPool();
1625 return RUN_ALL_TESTS();
1626}