blob: 7679b46c4ecf6df69463b5cc31e855ad33a65c9b [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
Riley Andrews06b01ad2014-12-18 12:10:08 -08001176class BinderLibTestService : public BBinder
1177{
1178 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001179 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001180 : m_id(id)
1181 , m_nextServerId(id + 1)
1182 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001183 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001184 {
Yi Kong91635562018-06-07 14:38:36 -07001185 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1186 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001187 }
1188 ~BinderLibTestService()
1189 {
1190 exit(EXIT_SUCCESS);
1191 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001192
1193 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001194 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001195 Parcel data;
1196 data.writeInt32(NO_ERROR);
1197 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001198 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001199 }
1200 }
1201
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 virtual status_t onTransact(uint32_t code,
1203 const Parcel& data, Parcel* reply,
1204 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001205 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1206 return PERMISSION_DENIED;
1207 }
1208 switch (code) {
1209 case BINDER_LIB_TEST_REGISTER_SERVER: {
1210 int32_t id;
1211 sp<IBinder> binder;
1212 id = data.readInt32();
1213 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001214 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001215 return BAD_VALUE;
1216 }
1217
1218 if (m_id != 0)
1219 return INVALID_OPERATION;
1220
1221 pthread_mutex_lock(&m_serverWaitMutex);
1222 if (m_serverStartRequested) {
1223 m_serverStartRequested = false;
1224 m_serverStarted = binder;
1225 pthread_cond_signal(&m_serverWaitCond);
1226 }
1227 pthread_mutex_unlock(&m_serverWaitMutex);
1228 return NO_ERROR;
1229 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001230 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001231 case BINDER_LIB_TEST_ADD_SERVER: {
1232 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001233 int serverid;
1234
1235 if (m_id != 0) {
1236 return INVALID_OPERATION;
1237 }
1238 pthread_mutex_lock(&m_serverWaitMutex);
1239 if (m_serverStartRequested) {
1240 ret = -EBUSY;
1241 } else {
1242 serverid = m_nextServerId++;
1243 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001244 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001245
1246 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001247 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001248 pthread_mutex_lock(&m_serverWaitMutex);
1249 }
1250 if (ret > 0) {
1251 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001252 struct timespec ts;
1253 clock_gettime(CLOCK_REALTIME, &ts);
1254 ts.tv_sec += 5;
1255 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001256 }
1257 if (m_serverStartRequested) {
1258 m_serverStartRequested = false;
1259 ret = -ETIMEDOUT;
1260 } else {
1261 reply->writeStrongBinder(m_serverStarted);
1262 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001263 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001264 ret = NO_ERROR;
1265 }
1266 } else if (ret >= 0) {
1267 m_serverStartRequested = false;
1268 ret = UNKNOWN_ERROR;
1269 }
1270 pthread_mutex_unlock(&m_serverWaitMutex);
1271 return ret;
1272 }
Steven Moreland35626652021-05-15 01:32:04 +00001273 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1274 IPCThreadState::SpGuard spGuard{
1275 .address = __builtin_frame_address(0),
1276 .context = "GuardInBinderTransaction",
1277 };
1278 const IPCThreadState::SpGuard *origGuard =
1279 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1280
1281 // if the guard works, this should abort
1282 (void)IPCThreadState::self()->getCallingPid();
1283
1284 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1285 return NO_ERROR;
1286 }
1287
Marco Ballesio7ee17572020-09-08 10:30:03 -07001288 case BINDER_LIB_TEST_GETPID:
1289 reply->writeInt32(getpid());
1290 return NO_ERROR;
1291 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1292 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001293 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001294 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001295 // oneway error codes should be ignored
1296 if (flags & TF_ONE_WAY) {
1297 return UNKNOWN_ERROR;
1298 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001299 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001300 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1301 // Note: this transaction is only designed for use with a
1302 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001303 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001304 // A callback was already pending; this means that
1305 // we received a second call while still processing
1306 // the first one. Fail the test.
1307 sp<IBinder> callback = data.readStrongBinder();
1308 Parcel data2;
1309 data2.writeInt32(UNKNOWN_ERROR);
1310
Yi Kong91635562018-06-07 14:38:36 -07001311 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001312 } else {
1313 m_callback = data.readStrongBinder();
1314 int32_t delayUs = data.readInt32();
1315 /*
1316 * It's necessary that we sleep here, so the next
1317 * transaction the caller makes will be queued to
1318 * the async queue.
1319 */
1320 usleep(delayUs);
1321
1322 /*
1323 * Now when we return, libbinder will tell the kernel
1324 * we are done with this transaction, and the kernel
1325 * can move the queued transaction to either the
1326 * thread todo worklist (for kernels without the fix),
1327 * or the proc todo worklist. In case of the former,
1328 * the next outbound call will pick up the pending
1329 * transaction, which leads to undesired reentrant
1330 * behavior. This is caught in the if() branch above.
1331 */
1332 }
1333
1334 return NO_ERROR;
1335 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001336 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1337 Parcel data2, reply2;
1338 sp<IBinder> binder;
1339 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001340 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001341 return BAD_VALUE;
1342 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001343 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001344 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1345 return NO_ERROR;
1346 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001347 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1348 reply->writeStrongBinder(this);
1349 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001350 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1351 reply->writeInt32(m_id);
1352 return NO_ERROR;
1353 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1354 int32_t count;
1355 uint32_t indirect_code;
1356 sp<IBinder> binder;
1357
1358 count = data.readInt32();
1359 reply->writeInt32(m_id);
1360 reply->writeInt32(count);
1361 for (int i = 0; i < count; i++) {
1362 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001363 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001364 return BAD_VALUE;
1365 }
1366 indirect_code = data.readInt32();
1367 BinderLibTestBundle data2(&data);
1368 if (!data2.isValid()) {
1369 return BAD_VALUE;
1370 }
1371 BinderLibTestBundle reply2;
1372 binder->transact(indirect_code, data2, &reply2);
1373 reply2.appendTo(reply);
1374 }
1375 return NO_ERROR;
1376 }
1377 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1378 reply->setError(data.readInt32());
1379 return NO_ERROR;
1380 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1381 reply->writeInt32(sizeof(void *));
1382 return NO_ERROR;
1383 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1384 return NO_ERROR;
1385 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1386 m_strongRef = data.readStrongBinder();
1387 return NO_ERROR;
1388 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1389 int ret;
1390 Parcel data2, reply2;
1391 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1392 sp<IBinder> target;
1393 sp<IBinder> callback;
1394
1395 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001396 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001397 return BAD_VALUE;
1398 }
1399 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001400 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401 return BAD_VALUE;
1402 }
1403 ret = target->linkToDeath(testDeathRecipient);
1404 if (ret == NO_ERROR)
1405 ret = testDeathRecipient->waitEvent(5);
1406 data2.writeInt32(ret);
1407 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1408 return NO_ERROR;
1409 }
1410 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1411 int ret;
1412 int32_t size;
1413 const void *buf;
1414 int fd;
1415
1416 fd = data.readFileDescriptor();
1417 if (fd < 0) {
1418 return BAD_VALUE;
1419 }
1420 ret = data.readInt32(&size);
1421 if (ret != NO_ERROR) {
1422 return ret;
1423 }
1424 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001425 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001426 return BAD_VALUE;
1427 }
1428 ret = write(fd, buf, size);
1429 if (ret != size)
1430 return UNKNOWN_ERROR;
1431 return NO_ERROR;
1432 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001433 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1434 int ret;
1435 int32_t size;
1436 const void *buf;
1437 android::base::unique_fd fd;
1438
1439 ret = data.readUniqueParcelFileDescriptor(&fd);
1440 if (ret != NO_ERROR) {
1441 return ret;
1442 }
1443 ret = data.readInt32(&size);
1444 if (ret != NO_ERROR) {
1445 return ret;
1446 }
1447 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001448 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001449 return BAD_VALUE;
1450 }
1451 ret = write(fd.get(), buf, size);
1452 if (ret != size) return UNKNOWN_ERROR;
1453 return NO_ERROR;
1454 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001455 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1456 alarm(10);
1457 return NO_ERROR;
1458 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001459 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001460 ;
1461 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001462 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001463 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001464 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001465 return NO_ERROR;
1466 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001467 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1468 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001469 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001470 return NO_ERROR;
1471 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001472 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1473 int policy = 0;
1474 sched_param param;
1475 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1476 return UNKNOWN_ERROR;
1477 }
1478 reply->writeInt32(policy);
1479 reply->writeInt32(param.sched_priority);
1480 return NO_ERROR;
1481 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001482 case BINDER_LIB_TEST_ECHO_VECTOR: {
1483 std::vector<uint64_t> vector;
1484 auto err = data.readUint64Vector(&vector);
1485 if (err != NO_ERROR)
1486 return err;
1487 reply->writeUint64Vector(vector);
1488 return NO_ERROR;
1489 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001490 case BINDER_LIB_TEST_REJECT_BUF: {
1491 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1492 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001493 case BINDER_LIB_TEST_CAN_GET_SID: {
1494 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1495 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001496 default:
1497 return UNKNOWN_TRANSACTION;
1498 };
1499 }
1500 private:
1501 int32_t m_id;
1502 int32_t m_nextServerId;
1503 pthread_mutex_t m_serverWaitMutex;
1504 pthread_cond_t m_serverWaitCond;
1505 bool m_serverStartRequested;
1506 sp<IBinder> m_serverStarted;
1507 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001508 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001509};
1510
Martijn Coenen45b07b42017-08-09 12:07:45 +02001511int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001512{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001513 binderLibTestServiceName += String16(binderserversuffix);
1514
Steven Moreland35626652021-05-15 01:32:04 +00001515 // Testing to make sure that calls that we are serving can use getCallin*
1516 // even though we don't here.
1517 IPCThreadState::SpGuard spGuard{
1518 .address = __builtin_frame_address(0),
1519 .context = "main server thread",
1520 };
1521 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1522
Riley Andrews06b01ad2014-12-18 12:10:08 -08001523 status_t ret;
1524 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001525 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001526 {
1527 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001528
Steven Morelandbf1915b2020-07-16 22:43:02 +00001529 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1530
Steven Morelandcf03cf12020-12-04 02:58:40 +00001531 testService->setInheritRt(true);
1532
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001533 /*
1534 * Normally would also contain functionality as well, but we are only
1535 * testing the extension mechanism.
1536 */
1537 testService->setExtension(new BBinder());
1538
Martijn Coenen82c75312019-07-24 15:18:30 +02001539 // Required for test "BufRejected'
1540 testService->setRequestingSid(true);
1541
Martijn Coenen45b07b42017-08-09 12:07:45 +02001542 /*
1543 * We need this below, but can't hold a sp<> because it prevents the
1544 * node from being cleaned up automatically. It's safe in this case
1545 * because of how the tests are written.
1546 */
1547 testServicePtr = testService.get();
1548
Riley Andrews06b01ad2014-12-18 12:10:08 -08001549 if (index == 0) {
1550 ret = sm->addService(binderLibTestServiceName, testService);
1551 } else {
1552 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1553 Parcel data, reply;
1554 data.writeInt32(index);
1555 data.writeStrongBinder(testService);
1556
1557 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1558 }
1559 }
1560 write(readypipefd, &ret, sizeof(ret));
1561 close(readypipefd);
1562 //printf("%s: ret %d\n", __func__, ret);
1563 if (ret)
1564 return 1;
1565 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001566 if (usePoll) {
1567 int fd;
1568 struct epoll_event ev;
1569 int epoll_fd;
1570 IPCThreadState::self()->setupPolling(&fd);
1571 if (fd < 0) {
1572 return 1;
1573 }
1574 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1575
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001576 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001577 if (epoll_fd == -1) {
1578 return 1;
1579 }
1580
1581 ev.events = EPOLLIN;
1582 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1583 return 1;
1584 }
1585
1586 while (1) {
1587 /*
1588 * We simulate a single-threaded process using the binder poll
1589 * interface; besides handling binder commands, it can also
1590 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001591 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001592 *
1593 * processPendingCall() will then issue that transaction.
1594 */
1595 struct epoll_event events[1];
1596 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1597 if (numEvents < 0) {
1598 if (errno == EINTR) {
1599 continue;
1600 }
1601 return 1;
1602 }
1603 if (numEvents > 0) {
1604 IPCThreadState::self()->handlePolledCommands();
1605 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1606 testServicePtr->processPendingCall();
1607 }
1608 }
1609 } else {
1610 ProcessState::self()->startThreadPool();
1611 IPCThreadState::self()->joinThreadPool();
1612 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001613 //printf("%s: joinThreadPool returned\n", __func__);
1614 return 1; /* joinThreadPool should not return */
1615}
1616
1617int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001618 ExitIfWrongAbi();
1619
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001620 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001621 binderservername = argv[2];
1622 } else {
1623 binderservername = argv[0];
1624 }
1625
Martijn Coenen45b07b42017-08-09 12:07:45 +02001626 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1627 binderserversuffix = argv[5];
1628 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001629 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001630 binderserversuffix = new char[16];
1631 snprintf(binderserversuffix, 16, "%d", getpid());
1632 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001633
1634 ::testing::InitGoogleTest(&argc, argv);
1635 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1636 ProcessState::self()->startThreadPool();
1637 return RUN_ALL_TESTS();
1638}