blob: 45b277624cbed5b29e1acd77614168cca95fb200 [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 Moreland8e5f3b42021-05-14 02:39:59 +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 Moreland8e5f3b42021-05-14 02:39:59 +0000608TEST_F(BinderLibTest, NoBinderCallContextGuard) {
609 IPCThreadState::SpGuard spGuard{"NoBinderCallContext"};
610 IPCThreadState::SpGuard *origGuard = IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
611
612 // yes, this test uses threads, but it's careful and uses fork in addServer
613 EXPECT_DEATH({ IPCThreadState::self()->getCallingPid(); },
614 "In context NoBinderCallContext, getCallingPid does not make sense.");
615
616 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
617}
618
619TEST_F(BinderLibTest, BinderCallContextGuard) {
620 sp<IBinder> binder = addServer();
621 Parcel data, reply;
622 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
623 StatusEq(DEAD_OBJECT));
624}
625
Riley Andrews06b01ad2014-12-18 12:10:08 -0800626TEST_F(BinderLibTest, AddServer)
627{
628 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700629 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800630}
631
Riley Andrews06b01ad2014-12-18 12:10:08 -0800632TEST_F(BinderLibTest, DeathNotificationStrongRef)
633{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800634 sp<IBinder> sbinder;
635
636 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
637
638 {
639 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700640 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700641 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800642 sbinder = binder;
643 }
644 {
645 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700646 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
647 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800648 }
649 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700650 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
651 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800652}
653
654TEST_F(BinderLibTest, DeathNotificationMultiple)
655{
656 status_t ret;
657 const int clientcount = 2;
658 sp<IBinder> target;
659 sp<IBinder> linkedclient[clientcount];
660 sp<BinderLibTestCallBack> callBack[clientcount];
661 sp<IBinder> passiveclient[clientcount];
662
663 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700664 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800665 for (int i = 0; i < clientcount; i++) {
666 {
667 Parcel data, reply;
668
669 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700670 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800671 callBack[i] = new BinderLibTestCallBack();
672 data.writeStrongBinder(target);
673 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700674 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
675 &reply, TF_ONE_WAY),
676 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800677 }
678 {
679 Parcel data, reply;
680
681 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700682 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800683 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700684 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
685 &reply, TF_ONE_WAY),
686 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800687 }
688 }
689 {
690 Parcel data, reply;
691 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
692 EXPECT_EQ(0, ret);
693 }
694
695 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700696 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
697 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800698 }
699}
700
Martijn Coenenf7100e42017-07-31 12:14:09 +0200701TEST_F(BinderLibTest, DeathNotificationThread)
702{
703 status_t ret;
704 sp<BinderLibTestCallBack> callback;
705 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700706 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200707 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700708 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200709
710 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
711
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700712 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200713
714 {
715 Parcel data, reply;
716 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
717 EXPECT_EQ(0, ret);
718 }
719
720 /* Make sure it's dead */
721 testDeathRecipient->waitEvent(5);
722
723 /* Now, pass the ref to another process and ask that process to
724 * call linkToDeath() on it, and wait for a response. This tests
725 * two things:
726 * 1) You still get death notifications when calling linkToDeath()
727 * on a ref that is already dead when it was passed to you.
728 * 2) That death notifications are not directly pushed to the thread
729 * registering them, but to the threadpool (proc workqueue) instead.
730 *
731 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
732 * is blocked on a condition variable waiting for the death notification to be
733 * called; therefore, that thread is not available for handling proc work.
734 * So, if the death notification was pushed to the thread workqueue, the callback
735 * would never be called, and the test would timeout and fail.
736 *
737 * Note that we can't do this part of the test from this thread itself, because
738 * the binder driver would only push death notifications to the thread if
739 * it is a looper thread, which this thread is not.
740 *
741 * See b/23525545 for details.
742 */
743 {
744 Parcel data, reply;
745
746 callback = new BinderLibTestCallBack();
747 data.writeStrongBinder(target);
748 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700749 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
750 TF_ONE_WAY),
751 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200752 }
753
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700754 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
755 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200756}
757
Riley Andrews06b01ad2014-12-18 12:10:08 -0800758TEST_F(BinderLibTest, PassFile) {
759 int ret;
760 int pipefd[2];
761 uint8_t buf[1] = { 0 };
762 uint8_t write_value = 123;
763
764 ret = pipe2(pipefd, O_NONBLOCK);
765 ASSERT_EQ(0, ret);
766
767 {
768 Parcel data, reply;
769 uint8_t writebuf[1] = { write_value };
770
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700771 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700773 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800774
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700775 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800776
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700777 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
778 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800779 }
780
781 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700782 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800783 EXPECT_EQ(write_value, buf[0]);
784
785 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
786
787 ret = read(pipefd[0], buf, sizeof(buf));
788 EXPECT_EQ(0, ret);
789
790 close(pipefd[0]);
791}
792
Ryo Hashimotobf551892018-05-31 16:58:35 +0900793TEST_F(BinderLibTest, PassParcelFileDescriptor) {
794 const int datasize = 123;
795 std::vector<uint8_t> writebuf(datasize);
796 for (size_t i = 0; i < writebuf.size(); ++i) {
797 writebuf[i] = i;
798 }
799
800 android::base::unique_fd read_end, write_end;
801 {
802 int pipefd[2];
803 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
804 read_end.reset(pipefd[0]);
805 write_end.reset(pipefd[1]);
806 }
807 {
808 Parcel data;
809 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
810 write_end.reset();
811 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
812 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
813
814 Parcel reply;
815 EXPECT_EQ(NO_ERROR,
816 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
817 &reply));
818 }
819 std::vector<uint8_t> readbuf(datasize);
820 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
821 EXPECT_EQ(writebuf, readbuf);
822
823 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
824
825 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
826}
827
Riley Andrews06b01ad2014-12-18 12:10:08 -0800828TEST_F(BinderLibTest, PromoteLocal) {
829 sp<IBinder> strong = new BBinder();
830 wp<IBinder> weak = strong;
831 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700832 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800833 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700834 strong = nullptr;
835 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800836 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700837 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800838}
839
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700840TEST_F(BinderLibTest, LocalGetExtension) {
841 sp<BBinder> binder = new BBinder();
842 sp<IBinder> ext = new BBinder();
843 binder->setExtension(ext);
844 EXPECT_EQ(ext, binder->getExtension());
845}
846
847TEST_F(BinderLibTest, RemoteGetExtension) {
848 sp<IBinder> server = addServer();
849 ASSERT_TRUE(server != nullptr);
850
851 sp<IBinder> extension;
852 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
853 ASSERT_NE(nullptr, extension.get());
854
855 EXPECT_EQ(NO_ERROR, extension->pingBinder());
856}
857
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700858TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700859 Parcel data, reply;
860
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700861 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
862 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700863
864 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700865 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800866 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
867 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
868 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
869 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700870}
871
Connor O'Brien52be2c92016-09-20 14:18:08 -0700872TEST_F(BinderLibTest, FreedBinder) {
873 status_t ret;
874
875 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700876 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700877
878 __u32 freedHandle;
879 wp<IBinder> keepFreedBinder;
880 {
881 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700882 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
883 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700884 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
885 freedHandle = freed->handle;
886 /* Add a weak ref to the freed binder so the driver does not
887 * delete its reference to it - otherwise the transaction
888 * fails regardless of whether the driver is fixed.
889 */
Steven Morelande171d622019-07-17 16:06:01 -0700890 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700891 }
Steven Morelande171d622019-07-17 16:06:01 -0700892 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700893 {
894 Parcel data, reply;
895 data.writeStrongBinder(server);
896 /* Replace original handle with handle to the freed binder */
897 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
898 __u32 oldHandle = strong->handle;
899 strong->handle = freedHandle;
900 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
901 /* Returns DEAD_OBJECT (-32) if target crashes and
902 * FAILED_TRANSACTION if the driver rejects the invalid
903 * object.
904 */
905 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
906 /* Restore original handle so parcel destructor does not use
907 * the wrong handle.
908 */
909 strong->handle = oldHandle;
910 }
911}
912
Steven Morelandd7088702021-01-13 00:27:00 +0000913TEST_F(BinderLibTest, ParcelAllocatedOnAnotherThread) {
914 sp<IBinder> server = addServer();
915 ASSERT_TRUE(server != nullptr);
916
917 Parcel data;
918 sp<ParcelRef> reply = ParcelRef::create();
919
920 // when we have a Parcel which is deleted on another thread, if it gets
921 // deleted, it will tell the kernel this, and it will drop strong references
922 // to binder, so that we can't BR_ACQUIRE would fail
923 IPCThreadState::self()->createTransactionReference(reply.get());
924 ASSERT_EQ(NO_ERROR, server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
925 data,
926 reply.get()));
927
928 // we have sp to binder, but it is not actually acquired by kernel, the
929 // transaction is sitting on an out buffer
930 sp<IBinder> binder = reply->readStrongBinder();
931
932 std::thread([&] {
933 // without the transaction reference, this would cause the Parcel to be
934 // deallocated before the first thread flushes BR_ACQUIRE
935 reply = nullptr;
936 IPCThreadState::self()->flushCommands();
937 }).join();
938
939 ASSERT_NE(nullptr, binder);
940 ASSERT_EQ(NO_ERROR, binder->pingBinder());
941}
942
Sherry Yang336cdd32017-07-24 14:12:27 -0700943TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700944 Parcel data, reply;
945 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
946 for (int i = 0; i < 2; i++) {
947 BinderLibTestBundle datai;
948 datai.appendFrom(&data, 0, data.dataSize());
949
950 data.freeData();
951 data.writeInt32(1);
952 data.writeStrongBinder(callBack);
953 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
954
955 datai.appendTo(&data);
956 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700957 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
958 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700959}
960
Martijn Coenen45b07b42017-08-09 12:07:45 +0200961TEST_F(BinderLibTest, OnewayQueueing)
962{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200963 Parcel data, data2;
964
965 sp<IBinder> pollServer = addPollServer();
966
967 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
968 data.writeStrongBinder(callBack);
969 data.writeInt32(500000); // delay in us before calling back
970
971 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
972 data2.writeStrongBinder(callBack2);
973 data2.writeInt32(0); // delay in us
974
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700975 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
976 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200977
978 // The delay ensures that this second transaction will end up on the async_todo list
979 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700980 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
981 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200982
983 // The server will ensure that the two transactions are handled in the expected order;
984 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700985 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
986 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200987
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700988 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
989 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200990}
991
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100992TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
993{
994 status_t ret;
995 Parcel data, reply;
996 data.writeInterfaceToken(binderLibTestServiceName);
997 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
998 EXPECT_EQ(-1, reply.readInt32());
999 EXPECT_EQ(NO_ERROR, ret);
1000}
1001
1002TEST_F(BinderLibTest, WorkSourceSet)
1003{
1004 status_t ret;
1005 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001006 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001007 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001008 data.writeInterfaceToken(binderLibTestServiceName);
1009 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1010 EXPECT_EQ(100, reply.readInt32());
1011 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001012 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1013 EXPECT_EQ(NO_ERROR, ret);
1014}
1015
1016TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1017{
1018 status_t ret;
1019 Parcel data, reply;
1020
1021 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1022 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1023
1024 data.writeInterfaceToken(binderLibTestServiceName);
1025 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1026 EXPECT_EQ(-1, reply.readInt32());
1027 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001028 EXPECT_EQ(NO_ERROR, ret);
1029}
1030
1031TEST_F(BinderLibTest, WorkSourceCleared)
1032{
1033 status_t ret;
1034 Parcel data, reply;
1035
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001036 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001037 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1038 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001039 data.writeInterfaceToken(binderLibTestServiceName);
1040 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1041
1042 EXPECT_EQ(-1, reply.readInt32());
1043 EXPECT_EQ(100, previousWorkSource);
1044 EXPECT_EQ(NO_ERROR, ret);
1045}
1046
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001047TEST_F(BinderLibTest, WorkSourceRestored)
1048{
1049 status_t ret;
1050 Parcel data, reply;
1051
1052 IPCThreadState::self()->setCallingWorkSourceUid(100);
1053 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1054 IPCThreadState::self()->restoreCallingWorkSource(token);
1055
1056 data.writeInterfaceToken(binderLibTestServiceName);
1057 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1058
1059 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001060 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001061 EXPECT_EQ(NO_ERROR, ret);
1062}
1063
Olivier Gaillard91a04802018-11-14 17:32:41 +00001064TEST_F(BinderLibTest, PropagateFlagSet)
1065{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001066 IPCThreadState::self()->clearPropagateWorkSource();
1067 IPCThreadState::self()->setCallingWorkSourceUid(100);
1068 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1069}
1070
1071TEST_F(BinderLibTest, PropagateFlagCleared)
1072{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001073 IPCThreadState::self()->setCallingWorkSourceUid(100);
1074 IPCThreadState::self()->clearPropagateWorkSource();
1075 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1076}
1077
1078TEST_F(BinderLibTest, PropagateFlagRestored)
1079{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001080 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1081 IPCThreadState::self()->restoreCallingWorkSource(token);
1082
1083 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1084}
1085
1086TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1087{
1088 IPCThreadState::self()->setCallingWorkSourceUid(100);
1089
1090 Parcel data, reply;
1091 status_t ret;
1092 data.writeInterfaceToken(binderLibTestServiceName);
1093 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1094
1095 Parcel data2, reply2;
1096 status_t ret2;
1097 data2.writeInterfaceToken(binderLibTestServiceName);
1098 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1099 EXPECT_EQ(100, reply2.readInt32());
1100 EXPECT_EQ(NO_ERROR, ret2);
1101}
1102
Steven Morelandbf1915b2020-07-16 22:43:02 +00001103TEST_F(BinderLibTest, SchedPolicySet) {
1104 sp<IBinder> server = addServer();
1105 ASSERT_TRUE(server != nullptr);
1106
1107 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001108 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1109 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001110
1111 int policy = reply.readInt32();
1112 int priority = reply.readInt32();
1113
1114 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1115 EXPECT_EQ(kSchedPriority, priority);
1116}
1117
Steven Morelandcf03cf12020-12-04 02:58:40 +00001118TEST_F(BinderLibTest, InheritRt) {
1119 sp<IBinder> server = addServer();
1120 ASSERT_TRUE(server != nullptr);
1121
1122 const struct sched_param param {
1123 .sched_priority = kSchedPriorityMore,
1124 };
1125 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1126
1127 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001128 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1129 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001130
1131 int policy = reply.readInt32();
1132 int priority = reply.readInt32();
1133
1134 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1135 EXPECT_EQ(kSchedPriorityMore, priority);
1136}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001137
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001138TEST_F(BinderLibTest, VectorSent) {
1139 Parcel data, reply;
1140 sp<IBinder> server = addServer();
1141 ASSERT_TRUE(server != nullptr);
1142
1143 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1144 data.writeUint64Vector(testValue);
1145
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001146 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001147 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001148 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001149 EXPECT_EQ(readValue, testValue);
1150}
1151
Martijn Coenen82c75312019-07-24 15:18:30 +02001152TEST_F(BinderLibTest, BufRejected) {
1153 Parcel data, reply;
1154 uint32_t buf;
1155 sp<IBinder> server = addServer();
1156 ASSERT_TRUE(server != nullptr);
1157
1158 binder_buffer_object obj {
1159 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001160 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001161 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1162 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001163 };
1164 data.setDataCapacity(1024);
1165 // Write a bogus object at offset 0 to get an entry in the offset table
1166 data.writeFileDescriptor(0);
1167 EXPECT_EQ(data.objectsCount(), 1);
1168 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1169 // And now, overwrite it with the buffer object
1170 memcpy(parcelData, &obj, sizeof(obj));
1171 data.setDataSize(sizeof(obj));
1172
Martijn Coenen82c75312019-07-24 15:18:30 +02001173 // Either the kernel should reject this transaction (if it's correct), but
1174 // if it's not, the server implementation should return an error if it
1175 // finds an object in the received Parcel.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001176 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply),
1177 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001178}
1179
Steven Moreland254e8ef2021-04-19 22:28:50 +00001180TEST_F(BinderLibTest, GotSid) {
1181 sp<IBinder> server = addServer();
1182
1183 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001184 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001185}
1186
Riley Andrews06b01ad2014-12-18 12:10:08 -08001187class BinderLibTestService : public BBinder
1188{
1189 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001190 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001191 : m_id(id)
1192 , m_nextServerId(id + 1)
1193 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001194 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001195 {
Yi Kong91635562018-06-07 14:38:36 -07001196 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1197 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001198 }
1199 ~BinderLibTestService()
1200 {
1201 exit(EXIT_SUCCESS);
1202 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001203
1204 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001205 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001206 Parcel data;
1207 data.writeInt32(NO_ERROR);
1208 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001209 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001210 }
1211 }
1212
Riley Andrews06b01ad2014-12-18 12:10:08 -08001213 virtual status_t onTransact(uint32_t code,
1214 const Parcel& data, Parcel* reply,
1215 uint32_t flags = 0) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001216 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1217 return PERMISSION_DENIED;
1218 }
1219 switch (code) {
1220 case BINDER_LIB_TEST_REGISTER_SERVER: {
1221 int32_t id;
1222 sp<IBinder> binder;
1223 id = data.readInt32();
1224 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001225 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001226 return BAD_VALUE;
1227 }
1228
1229 if (m_id != 0)
1230 return INVALID_OPERATION;
1231
1232 pthread_mutex_lock(&m_serverWaitMutex);
1233 if (m_serverStartRequested) {
1234 m_serverStartRequested = false;
1235 m_serverStarted = binder;
1236 pthread_cond_signal(&m_serverWaitCond);
1237 }
1238 pthread_mutex_unlock(&m_serverWaitMutex);
1239 return NO_ERROR;
1240 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001241 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001242 case BINDER_LIB_TEST_ADD_SERVER: {
1243 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001244 int serverid;
1245
1246 if (m_id != 0) {
1247 return INVALID_OPERATION;
1248 }
1249 pthread_mutex_lock(&m_serverWaitMutex);
1250 if (m_serverStartRequested) {
1251 ret = -EBUSY;
1252 } else {
1253 serverid = m_nextServerId++;
1254 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001255 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001256
1257 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001258 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001259 pthread_mutex_lock(&m_serverWaitMutex);
1260 }
1261 if (ret > 0) {
1262 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001263 struct timespec ts;
1264 clock_gettime(CLOCK_REALTIME, &ts);
1265 ts.tv_sec += 5;
1266 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001267 }
1268 if (m_serverStartRequested) {
1269 m_serverStartRequested = false;
1270 ret = -ETIMEDOUT;
1271 } else {
1272 reply->writeStrongBinder(m_serverStarted);
1273 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001274 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001275 ret = NO_ERROR;
1276 }
1277 } else if (ret >= 0) {
1278 m_serverStartRequested = false;
1279 ret = UNKNOWN_ERROR;
1280 }
1281 pthread_mutex_unlock(&m_serverWaitMutex);
1282 return ret;
1283 }
Steven Moreland8e5f3b42021-05-14 02:39:59 +00001284 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1285 IPCThreadState::SpGuard spGuard{"GuardInBinderTransaction"};
1286 IPCThreadState::SpGuard *origGuard =
1287 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1288
1289 // if the guard works, this should abort
1290 (void)IPCThreadState::self()->getCallingPid();
1291
1292 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1293 return NO_ERROR;
1294 }
1295
Marco Ballesio7ee17572020-09-08 10:30:03 -07001296 case BINDER_LIB_TEST_GETPID:
1297 reply->writeInt32(getpid());
1298 return NO_ERROR;
1299 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1300 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001301 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001302 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001303 // oneway error codes should be ignored
1304 if (flags & TF_ONE_WAY) {
1305 return UNKNOWN_ERROR;
1306 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001308 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1309 // Note: this transaction is only designed for use with a
1310 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001311 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001312 // A callback was already pending; this means that
1313 // we received a second call while still processing
1314 // the first one. Fail the test.
1315 sp<IBinder> callback = data.readStrongBinder();
1316 Parcel data2;
1317 data2.writeInt32(UNKNOWN_ERROR);
1318
Yi Kong91635562018-06-07 14:38:36 -07001319 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001320 } else {
1321 m_callback = data.readStrongBinder();
1322 int32_t delayUs = data.readInt32();
1323 /*
1324 * It's necessary that we sleep here, so the next
1325 * transaction the caller makes will be queued to
1326 * the async queue.
1327 */
1328 usleep(delayUs);
1329
1330 /*
1331 * Now when we return, libbinder will tell the kernel
1332 * we are done with this transaction, and the kernel
1333 * can move the queued transaction to either the
1334 * thread todo worklist (for kernels without the fix),
1335 * or the proc todo worklist. In case of the former,
1336 * the next outbound call will pick up the pending
1337 * transaction, which leads to undesired reentrant
1338 * behavior. This is caught in the if() branch above.
1339 */
1340 }
1341
1342 return NO_ERROR;
1343 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001344 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1345 Parcel data2, reply2;
1346 sp<IBinder> binder;
1347 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001348 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001349 return BAD_VALUE;
1350 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001351 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001352 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1353 return NO_ERROR;
1354 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001355 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1356 reply->writeStrongBinder(this);
1357 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1359 reply->writeInt32(m_id);
1360 return NO_ERROR;
1361 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1362 int32_t count;
1363 uint32_t indirect_code;
1364 sp<IBinder> binder;
1365
1366 count = data.readInt32();
1367 reply->writeInt32(m_id);
1368 reply->writeInt32(count);
1369 for (int i = 0; i < count; i++) {
1370 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001371 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001372 return BAD_VALUE;
1373 }
1374 indirect_code = data.readInt32();
1375 BinderLibTestBundle data2(&data);
1376 if (!data2.isValid()) {
1377 return BAD_VALUE;
1378 }
1379 BinderLibTestBundle reply2;
1380 binder->transact(indirect_code, data2, &reply2);
1381 reply2.appendTo(reply);
1382 }
1383 return NO_ERROR;
1384 }
1385 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1386 reply->setError(data.readInt32());
1387 return NO_ERROR;
1388 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1389 reply->writeInt32(sizeof(void *));
1390 return NO_ERROR;
1391 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1392 return NO_ERROR;
1393 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1394 m_strongRef = data.readStrongBinder();
1395 return NO_ERROR;
1396 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1397 int ret;
1398 Parcel data2, reply2;
1399 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1400 sp<IBinder> target;
1401 sp<IBinder> callback;
1402
1403 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001404 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001405 return BAD_VALUE;
1406 }
1407 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001408 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001409 return BAD_VALUE;
1410 }
1411 ret = target->linkToDeath(testDeathRecipient);
1412 if (ret == NO_ERROR)
1413 ret = testDeathRecipient->waitEvent(5);
1414 data2.writeInt32(ret);
1415 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1416 return NO_ERROR;
1417 }
1418 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1419 int ret;
1420 int32_t size;
1421 const void *buf;
1422 int fd;
1423
1424 fd = data.readFileDescriptor();
1425 if (fd < 0) {
1426 return BAD_VALUE;
1427 }
1428 ret = data.readInt32(&size);
1429 if (ret != NO_ERROR) {
1430 return ret;
1431 }
1432 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001433 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001434 return BAD_VALUE;
1435 }
1436 ret = write(fd, buf, size);
1437 if (ret != size)
1438 return UNKNOWN_ERROR;
1439 return NO_ERROR;
1440 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001441 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1442 int ret;
1443 int32_t size;
1444 const void *buf;
1445 android::base::unique_fd fd;
1446
1447 ret = data.readUniqueParcelFileDescriptor(&fd);
1448 if (ret != NO_ERROR) {
1449 return ret;
1450 }
1451 ret = data.readInt32(&size);
1452 if (ret != NO_ERROR) {
1453 return ret;
1454 }
1455 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001456 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001457 return BAD_VALUE;
1458 }
1459 ret = write(fd.get(), buf, size);
1460 if (ret != size) return UNKNOWN_ERROR;
1461 return NO_ERROR;
1462 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001463 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1464 alarm(10);
1465 return NO_ERROR;
1466 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001467 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001468 ;
1469 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001470 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001471 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001472 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001473 return NO_ERROR;
1474 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001475 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1476 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001477 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001478 return NO_ERROR;
1479 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001480 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1481 int policy = 0;
1482 sched_param param;
1483 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1484 return UNKNOWN_ERROR;
1485 }
1486 reply->writeInt32(policy);
1487 reply->writeInt32(param.sched_priority);
1488 return NO_ERROR;
1489 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001490 case BINDER_LIB_TEST_ECHO_VECTOR: {
1491 std::vector<uint64_t> vector;
1492 auto err = data.readUint64Vector(&vector);
1493 if (err != NO_ERROR)
1494 return err;
1495 reply->writeUint64Vector(vector);
1496 return NO_ERROR;
1497 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001498 case BINDER_LIB_TEST_REJECT_BUF: {
1499 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1500 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001501 case BINDER_LIB_TEST_CAN_GET_SID: {
1502 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1503 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001504 default:
1505 return UNKNOWN_TRANSACTION;
1506 };
1507 }
1508 private:
1509 int32_t m_id;
1510 int32_t m_nextServerId;
1511 pthread_mutex_t m_serverWaitMutex;
1512 pthread_cond_t m_serverWaitCond;
1513 bool m_serverStartRequested;
1514 sp<IBinder> m_serverStarted;
1515 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001516 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001517};
1518
Martijn Coenen45b07b42017-08-09 12:07:45 +02001519int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001520{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001521 binderLibTestServiceName += String16(binderserversuffix);
1522
Steven Moreland8e5f3b42021-05-14 02:39:59 +00001523 // Testing to make sure that calls that we are serving can use getCallin*
1524 // even though we don't here.
1525 IPCThreadState::SpGuard spGuard{"main server thread"};
1526 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1527
Riley Andrews06b01ad2014-12-18 12:10:08 -08001528 status_t ret;
1529 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001530 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001531 {
1532 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001533
Steven Morelandbf1915b2020-07-16 22:43:02 +00001534 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1535
Steven Morelandcf03cf12020-12-04 02:58:40 +00001536 testService->setInheritRt(true);
1537
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001538 /*
1539 * Normally would also contain functionality as well, but we are only
1540 * testing the extension mechanism.
1541 */
1542 testService->setExtension(new BBinder());
1543
Martijn Coenen82c75312019-07-24 15:18:30 +02001544 // Required for test "BufRejected'
1545 testService->setRequestingSid(true);
1546
Martijn Coenen45b07b42017-08-09 12:07:45 +02001547 /*
1548 * We need this below, but can't hold a sp<> because it prevents the
1549 * node from being cleaned up automatically. It's safe in this case
1550 * because of how the tests are written.
1551 */
1552 testServicePtr = testService.get();
1553
Riley Andrews06b01ad2014-12-18 12:10:08 -08001554 if (index == 0) {
1555 ret = sm->addService(binderLibTestServiceName, testService);
1556 } else {
1557 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1558 Parcel data, reply;
1559 data.writeInt32(index);
1560 data.writeStrongBinder(testService);
1561
1562 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1563 }
1564 }
1565 write(readypipefd, &ret, sizeof(ret));
1566 close(readypipefd);
1567 //printf("%s: ret %d\n", __func__, ret);
1568 if (ret)
1569 return 1;
1570 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001571 if (usePoll) {
1572 int fd;
1573 struct epoll_event ev;
1574 int epoll_fd;
1575 IPCThreadState::self()->setupPolling(&fd);
1576 if (fd < 0) {
1577 return 1;
1578 }
1579 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1580
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001581 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001582 if (epoll_fd == -1) {
1583 return 1;
1584 }
1585
1586 ev.events = EPOLLIN;
1587 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1588 return 1;
1589 }
1590
1591 while (1) {
1592 /*
1593 * We simulate a single-threaded process using the binder poll
1594 * interface; besides handling binder commands, it can also
1595 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001596 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001597 *
1598 * processPendingCall() will then issue that transaction.
1599 */
1600 struct epoll_event events[1];
1601 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1602 if (numEvents < 0) {
1603 if (errno == EINTR) {
1604 continue;
1605 }
1606 return 1;
1607 }
1608 if (numEvents > 0) {
1609 IPCThreadState::self()->handlePolledCommands();
1610 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1611 testServicePtr->processPendingCall();
1612 }
1613 }
1614 } else {
1615 ProcessState::self()->startThreadPool();
1616 IPCThreadState::self()->joinThreadPool();
1617 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001618 //printf("%s: joinThreadPool returned\n", __func__);
1619 return 1; /* joinThreadPool should not return */
1620}
1621
1622int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001623 ExitIfWrongAbi();
1624
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001625 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001626 binderservername = argv[2];
1627 } else {
1628 binderservername = argv[0];
1629 }
1630
Martijn Coenen45b07b42017-08-09 12:07:45 +02001631 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1632 binderserversuffix = argv[5];
1633 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001634 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001635 binderserversuffix = new char[16];
1636 snprintf(binderserversuffix, 16, "%d", getpid());
1637 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001638
1639 ::testing::InitGoogleTest(&argc, argv);
1640 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1641 ProcessState::self()->startThreadPool();
1642 return RUN_ALL_TESTS();
1643}