Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 19 | #include <poll.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <binder/Binder.h> |
| 27 | #include <binder/IBinder.h> |
| 28 | #include <binder/IPCThreadState.h> |
| 29 | #include <binder/IServiceManager.h> |
| 30 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 31 | #include <sys/epoll.h> |
| 32 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 33 | #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) |
| 34 | |
| 35 | using namespace android; |
| 36 | |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 37 | static ::testing::AssertionResult IsPageAligned(void *buf) { |
| 38 | if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0) |
| 39 | return ::testing::AssertionSuccess(); |
| 40 | else |
| 41 | return ::testing::AssertionFailure() << buf << " is not page aligned"; |
| 42 | } |
| 43 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 44 | static testing::Environment* binder_env; |
| 45 | static char *binderservername; |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 46 | static char *binderserversuffix; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 47 | static char binderserverarg[] = "--binderserver"; |
| 48 | |
| 49 | static String16 binderLibTestServiceName = String16("test.binderLib"); |
| 50 | |
| 51 | enum BinderLibTestTranscationCode { |
| 52 | BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION, |
| 53 | BINDER_LIB_TEST_REGISTER_SERVER, |
| 54 | BINDER_LIB_TEST_ADD_SERVER, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 55 | BINDER_LIB_TEST_ADD_POLL_SERVER, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 56 | BINDER_LIB_TEST_CALL_BACK, |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 57 | BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 58 | BINDER_LIB_TEST_DELAYED_CALL_BACK, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 59 | BINDER_LIB_TEST_NOP_CALL_BACK, |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 60 | BINDER_LIB_TEST_GET_SELF_TRANSACTION, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 61 | BINDER_LIB_TEST_GET_ID_TRANSACTION, |
| 62 | BINDER_LIB_TEST_INDIRECT_TRANSACTION, |
| 63 | BINDER_LIB_TEST_SET_ERROR_TRANSACTION, |
| 64 | BINDER_LIB_TEST_GET_STATUS_TRANSACTION, |
| 65 | BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, |
| 66 | BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, |
| 67 | BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 68 | BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 69 | BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, |
| 70 | BINDER_LIB_TEST_EXIT_TRANSACTION, |
| 71 | BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION, |
| 72 | BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 73 | BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 74 | BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 77 | pid_t start_server_process(int arg2, bool usePoll = false) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 78 | { |
| 79 | int ret; |
| 80 | pid_t pid; |
| 81 | status_t status; |
| 82 | int pipefd[2]; |
| 83 | char stri[16]; |
| 84 | char strpipefd1[16]; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 85 | char usepoll[2]; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 86 | char *childargv[] = { |
| 87 | binderservername, |
| 88 | binderserverarg, |
| 89 | stri, |
| 90 | strpipefd1, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 91 | usepoll, |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 92 | binderserversuffix, |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 93 | nullptr |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | ret = pipe(pipefd); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | snprintf(stri, sizeof(stri), "%d", arg2); |
| 101 | snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 102 | snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 103 | |
| 104 | pid = fork(); |
| 105 | if (pid == -1) |
| 106 | return pid; |
| 107 | if (pid == 0) { |
| 108 | close(pipefd[0]); |
| 109 | execv(binderservername, childargv); |
| 110 | status = -errno; |
| 111 | write(pipefd[1], &status, sizeof(status)); |
| 112 | fprintf(stderr, "execv failed, %s\n", strerror(errno)); |
| 113 | _exit(EXIT_FAILURE); |
| 114 | } |
| 115 | close(pipefd[1]); |
| 116 | ret = read(pipefd[0], &status, sizeof(status)); |
| 117 | //printf("pipe read returned %d, status %d\n", ret, status); |
| 118 | close(pipefd[0]); |
| 119 | if (ret == sizeof(status)) { |
| 120 | ret = status; |
| 121 | } else { |
| 122 | kill(pid, SIGKILL); |
| 123 | if (ret >= 0) { |
| 124 | ret = NO_INIT; |
| 125 | } |
| 126 | } |
| 127 | if (ret < 0) { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 128 | wait(nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | return pid; |
| 132 | } |
| 133 | |
| 134 | class BinderLibTestEnv : public ::testing::Environment { |
| 135 | public: |
| 136 | BinderLibTestEnv() {} |
| 137 | sp<IBinder> getServer(void) { |
| 138 | return m_server; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | virtual void SetUp() { |
| 143 | m_serverpid = start_server_process(0); |
| 144 | //printf("m_serverpid %d\n", m_serverpid); |
| 145 | ASSERT_GT(m_serverpid, 0); |
| 146 | |
| 147 | sp<IServiceManager> sm = defaultServiceManager(); |
| 148 | //printf("%s: pid %d, get service\n", __func__, m_pid); |
| 149 | m_server = sm->getService(binderLibTestServiceName); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 150 | ASSERT_TRUE(m_server != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 151 | //printf("%s: pid %d, get service done\n", __func__, m_pid); |
| 152 | } |
| 153 | virtual void TearDown() { |
| 154 | status_t ret; |
| 155 | Parcel data, reply; |
| 156 | int exitStatus; |
| 157 | pid_t pid; |
| 158 | |
| 159 | //printf("%s: pid %d\n", __func__, m_pid); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 160 | if (m_server != nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 161 | ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply); |
| 162 | EXPECT_EQ(0, ret); |
| 163 | ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 164 | EXPECT_EQ(0, ret); |
| 165 | } |
| 166 | if (m_serverpid > 0) { |
| 167 | //printf("wait for %d\n", m_pids[i]); |
| 168 | pid = wait(&exitStatus); |
| 169 | EXPECT_EQ(m_serverpid, pid); |
| 170 | EXPECT_TRUE(WIFEXITED(exitStatus)); |
| 171 | EXPECT_EQ(0, WEXITSTATUS(exitStatus)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | pid_t m_serverpid; |
| 176 | sp<IBinder> m_server; |
| 177 | }; |
| 178 | |
| 179 | class BinderLibTest : public ::testing::Test { |
| 180 | public: |
| 181 | virtual void SetUp() { |
| 182 | m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer(); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 183 | IPCThreadState::self()->restoreCallingWorkSource(0); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 184 | } |
| 185 | virtual void TearDown() { |
| 186 | } |
| 187 | protected: |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 188 | sp<IBinder> addServerEtc(int32_t *idPtr, int code) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 189 | { |
| 190 | int ret; |
| 191 | int32_t id; |
| 192 | Parcel data, reply; |
| 193 | sp<IBinder> binder; |
| 194 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 195 | ret = m_server->transact(code, data, &reply); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 196 | EXPECT_EQ(NO_ERROR, ret); |
| 197 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 198 | EXPECT_FALSE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 199 | binder = reply.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 200 | EXPECT_TRUE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 201 | ret = reply.readInt32(&id); |
| 202 | EXPECT_EQ(NO_ERROR, ret); |
| 203 | if (idPtr) |
| 204 | *idPtr = id; |
| 205 | return binder; |
| 206 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 207 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 208 | sp<IBinder> addServer(int32_t *idPtr = nullptr) |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 209 | { |
| 210 | return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER); |
| 211 | } |
| 212 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 213 | sp<IBinder> addPollServer(int32_t *idPtr = nullptr) |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 214 | { |
| 215 | return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER); |
| 216 | } |
| 217 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 218 | void waitForReadData(int fd, int timeout_ms) { |
| 219 | int ret; |
| 220 | pollfd pfd = pollfd(); |
| 221 | |
| 222 | pfd.fd = fd; |
| 223 | pfd.events = POLLIN; |
| 224 | ret = poll(&pfd, 1, timeout_ms); |
| 225 | EXPECT_EQ(1, ret); |
| 226 | } |
| 227 | |
| 228 | sp<IBinder> m_server; |
| 229 | }; |
| 230 | |
| 231 | class BinderLibTestBundle : public Parcel |
| 232 | { |
| 233 | public: |
| 234 | BinderLibTestBundle(void) {} |
| 235 | BinderLibTestBundle(const Parcel *source) : m_isValid(false) { |
| 236 | int32_t mark; |
| 237 | int32_t bundleLen; |
| 238 | size_t pos; |
| 239 | |
| 240 | if (source->readInt32(&mark)) |
| 241 | return; |
| 242 | if (mark != MARK_START) |
| 243 | return; |
| 244 | if (source->readInt32(&bundleLen)) |
| 245 | return; |
| 246 | pos = source->dataPosition(); |
| 247 | if (Parcel::appendFrom(source, pos, bundleLen)) |
| 248 | return; |
| 249 | source->setDataPosition(pos + bundleLen); |
| 250 | if (source->readInt32(&mark)) |
| 251 | return; |
| 252 | if (mark != MARK_END) |
| 253 | return; |
| 254 | m_isValid = true; |
| 255 | setDataPosition(0); |
| 256 | } |
| 257 | void appendTo(Parcel *dest) { |
| 258 | dest->writeInt32(MARK_START); |
| 259 | dest->writeInt32(dataSize()); |
| 260 | dest->appendFrom(this, 0, dataSize()); |
| 261 | dest->writeInt32(MARK_END); |
| 262 | }; |
| 263 | bool isValid(void) { |
| 264 | return m_isValid; |
| 265 | } |
| 266 | private: |
| 267 | enum { |
| 268 | MARK_START = B_PACK_CHARS('B','T','B','S'), |
| 269 | MARK_END = B_PACK_CHARS('B','T','B','E'), |
| 270 | }; |
| 271 | bool m_isValid; |
| 272 | }; |
| 273 | |
| 274 | class BinderLibTestEvent |
| 275 | { |
| 276 | public: |
| 277 | BinderLibTestEvent(void) |
| 278 | : m_eventTriggered(false) |
| 279 | { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 280 | pthread_mutex_init(&m_waitMutex, nullptr); |
| 281 | pthread_cond_init(&m_waitCond, nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 282 | } |
| 283 | int waitEvent(int timeout_s) |
| 284 | { |
| 285 | int ret; |
| 286 | pthread_mutex_lock(&m_waitMutex); |
| 287 | if (!m_eventTriggered) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 288 | struct timespec ts; |
| 289 | clock_gettime(CLOCK_REALTIME, &ts); |
| 290 | ts.tv_sec += timeout_s; |
| 291 | pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 292 | } |
| 293 | ret = m_eventTriggered ? NO_ERROR : TIMED_OUT; |
| 294 | pthread_mutex_unlock(&m_waitMutex); |
| 295 | return ret; |
| 296 | } |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 297 | pthread_t getTriggeringThread() |
| 298 | { |
| 299 | return m_triggeringThread; |
| 300 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 301 | protected: |
| 302 | void triggerEvent(void) { |
| 303 | pthread_mutex_lock(&m_waitMutex); |
| 304 | pthread_cond_signal(&m_waitCond); |
| 305 | m_eventTriggered = true; |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 306 | m_triggeringThread = pthread_self(); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 307 | pthread_mutex_unlock(&m_waitMutex); |
| 308 | }; |
| 309 | private: |
| 310 | pthread_mutex_t m_waitMutex; |
| 311 | pthread_cond_t m_waitCond; |
| 312 | bool m_eventTriggered; |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 313 | pthread_t m_triggeringThread; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent |
| 317 | { |
| 318 | public: |
| 319 | BinderLibTestCallBack() |
| 320 | : m_result(NOT_ENOUGH_DATA) |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 321 | , m_prev_end(nullptr) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 322 | { |
| 323 | } |
| 324 | status_t getResult(void) |
| 325 | { |
| 326 | return m_result; |
| 327 | } |
| 328 | |
| 329 | private: |
| 330 | virtual status_t onTransact(uint32_t code, |
| 331 | const Parcel& data, Parcel* reply, |
| 332 | uint32_t flags = 0) |
| 333 | { |
| 334 | (void)reply; |
| 335 | (void)flags; |
| 336 | switch(code) { |
Martijn Coenen | fb368f7 | 2017-08-10 15:03:18 +0200 | [diff] [blame] | 337 | case BINDER_LIB_TEST_CALL_BACK: { |
| 338 | status_t status = data.readInt32(&m_result); |
| 339 | if (status != NO_ERROR) { |
| 340 | m_result = status; |
| 341 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 342 | triggerEvent(); |
| 343 | return NO_ERROR; |
Martijn Coenen | fb368f7 | 2017-08-10 15:03:18 +0200 | [diff] [blame] | 344 | } |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 345 | case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: { |
| 346 | sp<IBinder> server; |
| 347 | int ret; |
| 348 | const uint8_t *buf = data.data(); |
| 349 | size_t size = data.dataSize(); |
| 350 | if (m_prev_end) { |
| 351 | /* 64-bit kernel needs at most 8 bytes to align buffer end */ |
| 352 | EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8); |
| 353 | } else { |
| 354 | EXPECT_TRUE(IsPageAligned((void *)buf)); |
| 355 | } |
| 356 | |
| 357 | m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t); |
| 358 | |
| 359 | if (size > 0) { |
| 360 | server = static_cast<BinderLibTestEnv *>(binder_env)->getServer(); |
| 361 | ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, |
| 362 | data, reply); |
| 363 | EXPECT_EQ(NO_ERROR, ret); |
| 364 | } |
| 365 | return NO_ERROR; |
| 366 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 367 | default: |
| 368 | return UNKNOWN_TRANSACTION; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | status_t m_result; |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 373 | const uint8_t *m_prev_end; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent |
| 377 | { |
| 378 | private: |
| 379 | virtual void binderDied(const wp<IBinder>& who) { |
| 380 | (void)who; |
| 381 | triggerEvent(); |
| 382 | }; |
| 383 | }; |
| 384 | |
| 385 | TEST_F(BinderLibTest, NopTransaction) { |
| 386 | status_t ret; |
| 387 | Parcel data, reply; |
| 388 | ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply); |
| 389 | EXPECT_EQ(NO_ERROR, ret); |
| 390 | } |
| 391 | |
| 392 | TEST_F(BinderLibTest, SetError) { |
| 393 | int32_t testValue[] = { 0, -123, 123 }; |
| 394 | for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) { |
| 395 | status_t ret; |
| 396 | Parcel data, reply; |
| 397 | data.writeInt32(testValue[i]); |
| 398 | ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply); |
| 399 | EXPECT_EQ(testValue[i], ret); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | TEST_F(BinderLibTest, GetId) { |
| 404 | status_t ret; |
| 405 | int32_t id; |
| 406 | Parcel data, reply; |
| 407 | ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply); |
| 408 | EXPECT_EQ(NO_ERROR, ret); |
| 409 | ret = reply.readInt32(&id); |
| 410 | EXPECT_EQ(NO_ERROR, ret); |
| 411 | EXPECT_EQ(0, id); |
| 412 | } |
| 413 | |
| 414 | TEST_F(BinderLibTest, PtrSize) { |
| 415 | status_t ret; |
| 416 | int32_t ptrsize; |
| 417 | Parcel data, reply; |
| 418 | sp<IBinder> server = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 419 | ASSERT_TRUE(server != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 420 | ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply); |
| 421 | EXPECT_EQ(NO_ERROR, ret); |
| 422 | ret = reply.readInt32(&ptrsize); |
| 423 | EXPECT_EQ(NO_ERROR, ret); |
| 424 | RecordProperty("TestPtrSize", sizeof(void *)); |
| 425 | RecordProperty("ServerPtrSize", sizeof(void *)); |
| 426 | } |
| 427 | |
| 428 | TEST_F(BinderLibTest, IndirectGetId2) |
| 429 | { |
| 430 | status_t ret; |
| 431 | int32_t id; |
| 432 | int32_t count; |
| 433 | Parcel data, reply; |
| 434 | int32_t serverId[3]; |
| 435 | |
| 436 | data.writeInt32(ARRAY_SIZE(serverId)); |
| 437 | for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) { |
| 438 | sp<IBinder> server; |
| 439 | BinderLibTestBundle datai; |
| 440 | |
| 441 | server = addServer(&serverId[i]); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 442 | ASSERT_TRUE(server != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 443 | data.writeStrongBinder(server); |
| 444 | data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION); |
| 445 | datai.appendTo(&data); |
| 446 | } |
| 447 | |
| 448 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 449 | ASSERT_EQ(NO_ERROR, ret); |
| 450 | |
| 451 | ret = reply.readInt32(&id); |
| 452 | ASSERT_EQ(NO_ERROR, ret); |
| 453 | EXPECT_EQ(0, id); |
| 454 | |
| 455 | ret = reply.readInt32(&count); |
| 456 | ASSERT_EQ(NO_ERROR, ret); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 457 | EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 458 | |
| 459 | for (size_t i = 0; i < (size_t)count; i++) { |
| 460 | BinderLibTestBundle replyi(&reply); |
| 461 | EXPECT_TRUE(replyi.isValid()); |
| 462 | ret = replyi.readInt32(&id); |
| 463 | EXPECT_EQ(NO_ERROR, ret); |
| 464 | EXPECT_EQ(serverId[i], id); |
| 465 | EXPECT_EQ(replyi.dataSize(), replyi.dataPosition()); |
| 466 | } |
| 467 | |
| 468 | EXPECT_EQ(reply.dataSize(), reply.dataPosition()); |
| 469 | } |
| 470 | |
| 471 | TEST_F(BinderLibTest, IndirectGetId3) |
| 472 | { |
| 473 | status_t ret; |
| 474 | int32_t id; |
| 475 | int32_t count; |
| 476 | Parcel data, reply; |
| 477 | int32_t serverId[3]; |
| 478 | |
| 479 | data.writeInt32(ARRAY_SIZE(serverId)); |
| 480 | for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) { |
| 481 | sp<IBinder> server; |
| 482 | BinderLibTestBundle datai; |
| 483 | BinderLibTestBundle datai2; |
| 484 | |
| 485 | server = addServer(&serverId[i]); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 486 | ASSERT_TRUE(server != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 487 | data.writeStrongBinder(server); |
| 488 | data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION); |
| 489 | |
| 490 | datai.writeInt32(1); |
| 491 | datai.writeStrongBinder(m_server); |
| 492 | datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION); |
| 493 | datai2.appendTo(&datai); |
| 494 | |
| 495 | datai.appendTo(&data); |
| 496 | } |
| 497 | |
| 498 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 499 | ASSERT_EQ(NO_ERROR, ret); |
| 500 | |
| 501 | ret = reply.readInt32(&id); |
| 502 | ASSERT_EQ(NO_ERROR, ret); |
| 503 | EXPECT_EQ(0, id); |
| 504 | |
| 505 | ret = reply.readInt32(&count); |
| 506 | ASSERT_EQ(NO_ERROR, ret); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 507 | EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 508 | |
| 509 | for (size_t i = 0; i < (size_t)count; i++) { |
| 510 | int32_t counti; |
| 511 | |
| 512 | BinderLibTestBundle replyi(&reply); |
| 513 | EXPECT_TRUE(replyi.isValid()); |
| 514 | ret = replyi.readInt32(&id); |
| 515 | EXPECT_EQ(NO_ERROR, ret); |
| 516 | EXPECT_EQ(serverId[i], id); |
| 517 | |
| 518 | ret = replyi.readInt32(&counti); |
| 519 | ASSERT_EQ(NO_ERROR, ret); |
| 520 | EXPECT_EQ(1, counti); |
| 521 | |
| 522 | BinderLibTestBundle replyi2(&replyi); |
| 523 | EXPECT_TRUE(replyi2.isValid()); |
| 524 | ret = replyi2.readInt32(&id); |
| 525 | EXPECT_EQ(NO_ERROR, ret); |
| 526 | EXPECT_EQ(0, id); |
| 527 | EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition()); |
| 528 | |
| 529 | EXPECT_EQ(replyi.dataSize(), replyi.dataPosition()); |
| 530 | } |
| 531 | |
| 532 | EXPECT_EQ(reply.dataSize(), reply.dataPosition()); |
| 533 | } |
| 534 | |
| 535 | TEST_F(BinderLibTest, CallBack) |
| 536 | { |
| 537 | status_t ret; |
| 538 | Parcel data, reply; |
| 539 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 540 | data.writeStrongBinder(callBack); |
| 541 | ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY); |
| 542 | EXPECT_EQ(NO_ERROR, ret); |
| 543 | ret = callBack->waitEvent(5); |
| 544 | EXPECT_EQ(NO_ERROR, ret); |
| 545 | ret = callBack->getResult(); |
| 546 | EXPECT_EQ(NO_ERROR, ret); |
| 547 | } |
| 548 | |
| 549 | TEST_F(BinderLibTest, AddServer) |
| 550 | { |
| 551 | sp<IBinder> server = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 552 | ASSERT_TRUE(server != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | TEST_F(BinderLibTest, DeathNotificationNoRefs) |
| 556 | { |
| 557 | status_t ret; |
| 558 | |
| 559 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 560 | |
| 561 | { |
| 562 | sp<IBinder> binder = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 563 | ASSERT_TRUE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 564 | ret = binder->linkToDeath(testDeathRecipient); |
| 565 | EXPECT_EQ(NO_ERROR, ret); |
| 566 | } |
| 567 | IPCThreadState::self()->flushCommands(); |
| 568 | ret = testDeathRecipient->waitEvent(5); |
| 569 | EXPECT_EQ(NO_ERROR, ret); |
| 570 | #if 0 /* Is there an unlink api that does not require a strong reference? */ |
| 571 | ret = binder->unlinkToDeath(testDeathRecipient); |
| 572 | EXPECT_EQ(NO_ERROR, ret); |
| 573 | #endif |
| 574 | } |
| 575 | |
| 576 | TEST_F(BinderLibTest, DeathNotificationWeakRef) |
| 577 | { |
| 578 | status_t ret; |
| 579 | wp<IBinder> wbinder; |
| 580 | |
| 581 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 582 | |
| 583 | { |
| 584 | sp<IBinder> binder = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 585 | ASSERT_TRUE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 586 | ret = binder->linkToDeath(testDeathRecipient); |
| 587 | EXPECT_EQ(NO_ERROR, ret); |
| 588 | wbinder = binder; |
| 589 | } |
| 590 | IPCThreadState::self()->flushCommands(); |
| 591 | ret = testDeathRecipient->waitEvent(5); |
| 592 | EXPECT_EQ(NO_ERROR, ret); |
| 593 | #if 0 /* Is there an unlink api that does not require a strong reference? */ |
| 594 | ret = binder->unlinkToDeath(testDeathRecipient); |
| 595 | EXPECT_EQ(NO_ERROR, ret); |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | TEST_F(BinderLibTest, DeathNotificationStrongRef) |
| 600 | { |
| 601 | status_t ret; |
| 602 | sp<IBinder> sbinder; |
| 603 | |
| 604 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 605 | |
| 606 | { |
| 607 | sp<IBinder> binder = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 608 | ASSERT_TRUE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 609 | ret = binder->linkToDeath(testDeathRecipient); |
| 610 | EXPECT_EQ(NO_ERROR, ret); |
| 611 | sbinder = binder; |
| 612 | } |
| 613 | { |
| 614 | Parcel data, reply; |
| 615 | ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 616 | EXPECT_EQ(0, ret); |
| 617 | } |
| 618 | IPCThreadState::self()->flushCommands(); |
| 619 | ret = testDeathRecipient->waitEvent(5); |
| 620 | EXPECT_EQ(NO_ERROR, ret); |
| 621 | ret = sbinder->unlinkToDeath(testDeathRecipient); |
| 622 | EXPECT_EQ(DEAD_OBJECT, ret); |
| 623 | } |
| 624 | |
| 625 | TEST_F(BinderLibTest, DeathNotificationMultiple) |
| 626 | { |
| 627 | status_t ret; |
| 628 | const int clientcount = 2; |
| 629 | sp<IBinder> target; |
| 630 | sp<IBinder> linkedclient[clientcount]; |
| 631 | sp<BinderLibTestCallBack> callBack[clientcount]; |
| 632 | sp<IBinder> passiveclient[clientcount]; |
| 633 | |
| 634 | target = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 635 | ASSERT_TRUE(target != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 636 | for (int i = 0; i < clientcount; i++) { |
| 637 | { |
| 638 | Parcel data, reply; |
| 639 | |
| 640 | linkedclient[i] = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 641 | ASSERT_TRUE(linkedclient[i] != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 642 | callBack[i] = new BinderLibTestCallBack(); |
| 643 | data.writeStrongBinder(target); |
| 644 | data.writeStrongBinder(callBack[i]); |
| 645 | ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 646 | EXPECT_EQ(NO_ERROR, ret); |
| 647 | } |
| 648 | { |
| 649 | Parcel data, reply; |
| 650 | |
| 651 | passiveclient[i] = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 652 | ASSERT_TRUE(passiveclient[i] != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 653 | data.writeStrongBinder(target); |
| 654 | ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 655 | EXPECT_EQ(NO_ERROR, ret); |
| 656 | } |
| 657 | } |
| 658 | { |
| 659 | Parcel data, reply; |
| 660 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 661 | EXPECT_EQ(0, ret); |
| 662 | } |
| 663 | |
| 664 | for (int i = 0; i < clientcount; i++) { |
| 665 | ret = callBack[i]->waitEvent(5); |
| 666 | EXPECT_EQ(NO_ERROR, ret); |
| 667 | ret = callBack[i]->getResult(); |
| 668 | EXPECT_EQ(NO_ERROR, ret); |
| 669 | } |
| 670 | } |
| 671 | |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 672 | TEST_F(BinderLibTest, DeathNotificationThread) |
| 673 | { |
| 674 | status_t ret; |
| 675 | sp<BinderLibTestCallBack> callback; |
| 676 | sp<IBinder> target = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 677 | ASSERT_TRUE(target != nullptr); |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 678 | sp<IBinder> client = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 679 | ASSERT_TRUE(client != nullptr); |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 680 | |
| 681 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 682 | |
| 683 | ret = target->linkToDeath(testDeathRecipient); |
| 684 | EXPECT_EQ(NO_ERROR, ret); |
| 685 | |
| 686 | { |
| 687 | Parcel data, reply; |
| 688 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 689 | EXPECT_EQ(0, ret); |
| 690 | } |
| 691 | |
| 692 | /* Make sure it's dead */ |
| 693 | testDeathRecipient->waitEvent(5); |
| 694 | |
| 695 | /* Now, pass the ref to another process and ask that process to |
| 696 | * call linkToDeath() on it, and wait for a response. This tests |
| 697 | * two things: |
| 698 | * 1) You still get death notifications when calling linkToDeath() |
| 699 | * on a ref that is already dead when it was passed to you. |
| 700 | * 2) That death notifications are not directly pushed to the thread |
| 701 | * registering them, but to the threadpool (proc workqueue) instead. |
| 702 | * |
| 703 | * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION |
| 704 | * is blocked on a condition variable waiting for the death notification to be |
| 705 | * called; therefore, that thread is not available for handling proc work. |
| 706 | * So, if the death notification was pushed to the thread workqueue, the callback |
| 707 | * would never be called, and the test would timeout and fail. |
| 708 | * |
| 709 | * Note that we can't do this part of the test from this thread itself, because |
| 710 | * the binder driver would only push death notifications to the thread if |
| 711 | * it is a looper thread, which this thread is not. |
| 712 | * |
| 713 | * See b/23525545 for details. |
| 714 | */ |
| 715 | { |
| 716 | Parcel data, reply; |
| 717 | |
| 718 | callback = new BinderLibTestCallBack(); |
| 719 | data.writeStrongBinder(target); |
| 720 | data.writeStrongBinder(callback); |
| 721 | ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 722 | EXPECT_EQ(NO_ERROR, ret); |
| 723 | } |
| 724 | |
| 725 | ret = callback->waitEvent(5); |
| 726 | EXPECT_EQ(NO_ERROR, ret); |
| 727 | ret = callback->getResult(); |
| 728 | EXPECT_EQ(NO_ERROR, ret); |
| 729 | } |
| 730 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 731 | TEST_F(BinderLibTest, PassFile) { |
| 732 | int ret; |
| 733 | int pipefd[2]; |
| 734 | uint8_t buf[1] = { 0 }; |
| 735 | uint8_t write_value = 123; |
| 736 | |
| 737 | ret = pipe2(pipefd, O_NONBLOCK); |
| 738 | ASSERT_EQ(0, ret); |
| 739 | |
| 740 | { |
| 741 | Parcel data, reply; |
| 742 | uint8_t writebuf[1] = { write_value }; |
| 743 | |
| 744 | ret = data.writeFileDescriptor(pipefd[1], true); |
| 745 | EXPECT_EQ(NO_ERROR, ret); |
| 746 | |
| 747 | ret = data.writeInt32(sizeof(writebuf)); |
| 748 | EXPECT_EQ(NO_ERROR, ret); |
| 749 | |
| 750 | ret = data.write(writebuf, sizeof(writebuf)); |
| 751 | EXPECT_EQ(NO_ERROR, ret); |
| 752 | |
| 753 | ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply); |
| 754 | EXPECT_EQ(NO_ERROR, ret); |
| 755 | } |
| 756 | |
| 757 | ret = read(pipefd[0], buf, sizeof(buf)); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 758 | EXPECT_EQ(sizeof(buf), (size_t)ret); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 759 | EXPECT_EQ(write_value, buf[0]); |
| 760 | |
| 761 | waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */ |
| 762 | |
| 763 | ret = read(pipefd[0], buf, sizeof(buf)); |
| 764 | EXPECT_EQ(0, ret); |
| 765 | |
| 766 | close(pipefd[0]); |
| 767 | } |
| 768 | |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 769 | TEST_F(BinderLibTest, PassParcelFileDescriptor) { |
| 770 | const int datasize = 123; |
| 771 | std::vector<uint8_t> writebuf(datasize); |
| 772 | for (size_t i = 0; i < writebuf.size(); ++i) { |
| 773 | writebuf[i] = i; |
| 774 | } |
| 775 | |
| 776 | android::base::unique_fd read_end, write_end; |
| 777 | { |
| 778 | int pipefd[2]; |
| 779 | ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK)); |
| 780 | read_end.reset(pipefd[0]); |
| 781 | write_end.reset(pipefd[1]); |
| 782 | } |
| 783 | { |
| 784 | Parcel data; |
| 785 | EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get())); |
| 786 | write_end.reset(); |
| 787 | EXPECT_EQ(NO_ERROR, data.writeInt32(datasize)); |
| 788 | EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize)); |
| 789 | |
| 790 | Parcel reply; |
| 791 | EXPECT_EQ(NO_ERROR, |
| 792 | m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data, |
| 793 | &reply)); |
| 794 | } |
| 795 | std::vector<uint8_t> readbuf(datasize); |
| 796 | EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize)); |
| 797 | EXPECT_EQ(writebuf, readbuf); |
| 798 | |
| 799 | waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */ |
| 800 | |
| 801 | EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize)); |
| 802 | } |
| 803 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 804 | TEST_F(BinderLibTest, PromoteLocal) { |
| 805 | sp<IBinder> strong = new BBinder(); |
| 806 | wp<IBinder> weak = strong; |
| 807 | sp<IBinder> strong_from_weak = weak.promote(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 808 | EXPECT_TRUE(strong != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 809 | EXPECT_EQ(strong, strong_from_weak); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 810 | strong = nullptr; |
| 811 | strong_from_weak = nullptr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 812 | strong_from_weak = weak.promote(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 813 | EXPECT_TRUE(strong_from_weak == nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | TEST_F(BinderLibTest, PromoteRemote) { |
| 817 | int ret; |
| 818 | Parcel data, reply; |
| 819 | sp<IBinder> strong = new BBinder(); |
| 820 | sp<IBinder> server = addServer(); |
| 821 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 822 | ASSERT_TRUE(server != nullptr); |
| 823 | ASSERT_TRUE(strong != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 824 | |
| 825 | ret = data.writeWeakBinder(strong); |
| 826 | EXPECT_EQ(NO_ERROR, ret); |
| 827 | |
| 828 | ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply); |
| 829 | EXPECT_GE(ret, 0); |
| 830 | } |
| 831 | |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 832 | TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) { |
| 833 | status_t ret; |
| 834 | Parcel data, reply; |
| 835 | |
| 836 | ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply); |
| 837 | EXPECT_EQ(NO_ERROR, ret); |
| 838 | |
| 839 | const flat_binder_object *fb = reply.readObject(false); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 840 | ASSERT_TRUE(fb != nullptr); |
Hsin-Yi Chen | ad6503c | 2017-07-28 11:28:52 +0800 | [diff] [blame] | 841 | EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type); |
| 842 | EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle)); |
| 843 | EXPECT_EQ((binder_uintptr_t)0, fb->cookie); |
| 844 | EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32); |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 847 | TEST_F(BinderLibTest, FreedBinder) { |
| 848 | status_t ret; |
| 849 | |
| 850 | sp<IBinder> server = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 851 | ASSERT_TRUE(server != nullptr); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 852 | |
| 853 | __u32 freedHandle; |
| 854 | wp<IBinder> keepFreedBinder; |
| 855 | { |
| 856 | Parcel data, reply; |
| 857 | data.writeBool(false); /* request weak reference */ |
| 858 | ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply); |
| 859 | ASSERT_EQ(NO_ERROR, ret); |
| 860 | struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data()); |
| 861 | freedHandle = freed->handle; |
| 862 | /* Add a weak ref to the freed binder so the driver does not |
| 863 | * delete its reference to it - otherwise the transaction |
| 864 | * fails regardless of whether the driver is fixed. |
| 865 | */ |
| 866 | keepFreedBinder = reply.readWeakBinder(); |
| 867 | } |
| 868 | { |
| 869 | Parcel data, reply; |
| 870 | data.writeStrongBinder(server); |
| 871 | /* Replace original handle with handle to the freed binder */ |
| 872 | struct flat_binder_object *strong = (struct flat_binder_object *)(data.data()); |
| 873 | __u32 oldHandle = strong->handle; |
| 874 | strong->handle = freedHandle; |
| 875 | ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply); |
| 876 | /* Returns DEAD_OBJECT (-32) if target crashes and |
| 877 | * FAILED_TRANSACTION if the driver rejects the invalid |
| 878 | * object. |
| 879 | */ |
| 880 | EXPECT_EQ((status_t)FAILED_TRANSACTION, ret); |
| 881 | /* Restore original handle so parcel destructor does not use |
| 882 | * the wrong handle. |
| 883 | */ |
| 884 | strong->handle = oldHandle; |
| 885 | } |
| 886 | } |
| 887 | |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 888 | TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) { |
| 889 | status_t ret; |
| 890 | Parcel data, reply; |
| 891 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 892 | for (int i = 0; i < 2; i++) { |
| 893 | BinderLibTestBundle datai; |
| 894 | datai.appendFrom(&data, 0, data.dataSize()); |
| 895 | |
| 896 | data.freeData(); |
| 897 | data.writeInt32(1); |
| 898 | data.writeStrongBinder(callBack); |
| 899 | data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF); |
| 900 | |
| 901 | datai.appendTo(&data); |
| 902 | } |
| 903 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 904 | EXPECT_EQ(NO_ERROR, ret); |
| 905 | } |
| 906 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 907 | TEST_F(BinderLibTest, OnewayQueueing) |
| 908 | { |
| 909 | status_t ret; |
| 910 | Parcel data, data2; |
| 911 | |
| 912 | sp<IBinder> pollServer = addPollServer(); |
| 913 | |
| 914 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 915 | data.writeStrongBinder(callBack); |
| 916 | data.writeInt32(500000); // delay in us before calling back |
| 917 | |
| 918 | sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack(); |
| 919 | data2.writeStrongBinder(callBack2); |
| 920 | data2.writeInt32(0); // delay in us |
| 921 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 922 | ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 923 | EXPECT_EQ(NO_ERROR, ret); |
| 924 | |
| 925 | // The delay ensures that this second transaction will end up on the async_todo list |
| 926 | // (for a single-threaded server) |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 927 | ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 928 | EXPECT_EQ(NO_ERROR, ret); |
| 929 | |
| 930 | // The server will ensure that the two transactions are handled in the expected order; |
| 931 | // If the ordering is not as expected, an error will be returned through the callbacks. |
| 932 | ret = callBack->waitEvent(2); |
| 933 | EXPECT_EQ(NO_ERROR, ret); |
| 934 | ret = callBack->getResult(); |
| 935 | EXPECT_EQ(NO_ERROR, ret); |
| 936 | |
| 937 | ret = callBack2->waitEvent(2); |
| 938 | EXPECT_EQ(NO_ERROR, ret); |
| 939 | ret = callBack2->getResult(); |
| 940 | EXPECT_EQ(NO_ERROR, ret); |
| 941 | } |
| 942 | |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 943 | TEST_F(BinderLibTest, WorkSourceUnsetByDefault) |
| 944 | { |
| 945 | status_t ret; |
| 946 | Parcel data, reply; |
| 947 | data.writeInterfaceToken(binderLibTestServiceName); |
| 948 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 949 | EXPECT_EQ(-1, reply.readInt32()); |
| 950 | EXPECT_EQ(NO_ERROR, ret); |
| 951 | } |
| 952 | |
| 953 | TEST_F(BinderLibTest, WorkSourceSet) |
| 954 | { |
| 955 | status_t ret; |
| 956 | Parcel data, reply; |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 957 | IPCThreadState::self()->clearCallingWorkSource(); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 958 | int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 959 | data.writeInterfaceToken(binderLibTestServiceName); |
| 960 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 961 | EXPECT_EQ(100, reply.readInt32()); |
| 962 | EXPECT_EQ(-1, previousWorkSource); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 963 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 964 | EXPECT_EQ(NO_ERROR, ret); |
| 965 | } |
| 966 | |
| 967 | TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation) |
| 968 | { |
| 969 | status_t ret; |
| 970 | Parcel data, reply; |
| 971 | |
| 972 | IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100); |
| 973 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 974 | |
| 975 | data.writeInterfaceToken(binderLibTestServiceName); |
| 976 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 977 | EXPECT_EQ(-1, reply.readInt32()); |
| 978 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 979 | EXPECT_EQ(NO_ERROR, ret); |
| 980 | } |
| 981 | |
| 982 | TEST_F(BinderLibTest, WorkSourceCleared) |
| 983 | { |
| 984 | status_t ret; |
| 985 | Parcel data, reply; |
| 986 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 987 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 988 | int64_t token = IPCThreadState::self()->clearCallingWorkSource(); |
| 989 | int32_t previousWorkSource = (int32_t)token; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 990 | data.writeInterfaceToken(binderLibTestServiceName); |
| 991 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 992 | |
| 993 | EXPECT_EQ(-1, reply.readInt32()); |
| 994 | EXPECT_EQ(100, previousWorkSource); |
| 995 | EXPECT_EQ(NO_ERROR, ret); |
| 996 | } |
| 997 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 998 | TEST_F(BinderLibTest, WorkSourceRestored) |
| 999 | { |
| 1000 | status_t ret; |
| 1001 | Parcel data, reply; |
| 1002 | |
| 1003 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 1004 | int64_t token = IPCThreadState::self()->clearCallingWorkSource(); |
| 1005 | IPCThreadState::self()->restoreCallingWorkSource(token); |
| 1006 | |
| 1007 | data.writeInterfaceToken(binderLibTestServiceName); |
| 1008 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 1009 | |
| 1010 | EXPECT_EQ(100, reply.readInt32()); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 1011 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 1012 | EXPECT_EQ(NO_ERROR, ret); |
| 1013 | } |
| 1014 | |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame^] | 1015 | TEST_F(BinderLibTest, PropagateFlagSet) |
| 1016 | { |
| 1017 | status_t ret; |
| 1018 | Parcel data, reply; |
| 1019 | |
| 1020 | IPCThreadState::self()->clearPropagateWorkSource(); |
| 1021 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 1022 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 1023 | } |
| 1024 | |
| 1025 | TEST_F(BinderLibTest, PropagateFlagCleared) |
| 1026 | { |
| 1027 | status_t ret; |
| 1028 | Parcel data, reply; |
| 1029 | |
| 1030 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 1031 | IPCThreadState::self()->clearPropagateWorkSource(); |
| 1032 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(BinderLibTest, PropagateFlagRestored) |
| 1036 | { |
| 1037 | status_t ret; |
| 1038 | Parcel data, reply; |
| 1039 | |
| 1040 | int token = IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 1041 | IPCThreadState::self()->restoreCallingWorkSource(token); |
| 1042 | |
| 1043 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 1044 | } |
| 1045 | |
| 1046 | TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls) |
| 1047 | { |
| 1048 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 1049 | |
| 1050 | Parcel data, reply; |
| 1051 | status_t ret; |
| 1052 | data.writeInterfaceToken(binderLibTestServiceName); |
| 1053 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 1054 | |
| 1055 | Parcel data2, reply2; |
| 1056 | status_t ret2; |
| 1057 | data2.writeInterfaceToken(binderLibTestServiceName); |
| 1058 | ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2); |
| 1059 | EXPECT_EQ(100, reply2.readInt32()); |
| 1060 | EXPECT_EQ(NO_ERROR, ret2); |
| 1061 | } |
| 1062 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1063 | class BinderLibTestService : public BBinder |
| 1064 | { |
| 1065 | public: |
| 1066 | BinderLibTestService(int32_t id) |
| 1067 | : m_id(id) |
| 1068 | , m_nextServerId(id + 1) |
| 1069 | , m_serverStartRequested(false) |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1070 | , m_callback(nullptr) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1071 | { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1072 | pthread_mutex_init(&m_serverWaitMutex, nullptr); |
| 1073 | pthread_cond_init(&m_serverWaitCond, nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1074 | } |
| 1075 | ~BinderLibTestService() |
| 1076 | { |
| 1077 | exit(EXIT_SUCCESS); |
| 1078 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1079 | |
| 1080 | void processPendingCall() { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1081 | if (m_callback != nullptr) { |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1082 | Parcel data; |
| 1083 | data.writeInt32(NO_ERROR); |
| 1084 | m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1085 | m_callback = nullptr; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1089 | virtual status_t onTransact(uint32_t code, |
| 1090 | const Parcel& data, Parcel* reply, |
| 1091 | uint32_t flags = 0) { |
| 1092 | //printf("%s: code %d\n", __func__, code); |
| 1093 | (void)flags; |
| 1094 | |
| 1095 | if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) { |
| 1096 | return PERMISSION_DENIED; |
| 1097 | } |
| 1098 | switch (code) { |
| 1099 | case BINDER_LIB_TEST_REGISTER_SERVER: { |
| 1100 | int32_t id; |
| 1101 | sp<IBinder> binder; |
| 1102 | id = data.readInt32(); |
| 1103 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1104 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1105 | return BAD_VALUE; |
| 1106 | } |
| 1107 | |
| 1108 | if (m_id != 0) |
| 1109 | return INVALID_OPERATION; |
| 1110 | |
| 1111 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1112 | if (m_serverStartRequested) { |
| 1113 | m_serverStartRequested = false; |
| 1114 | m_serverStarted = binder; |
| 1115 | pthread_cond_signal(&m_serverWaitCond); |
| 1116 | } |
| 1117 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 1118 | return NO_ERROR; |
| 1119 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1120 | case BINDER_LIB_TEST_ADD_POLL_SERVER: |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1121 | case BINDER_LIB_TEST_ADD_SERVER: { |
| 1122 | int ret; |
| 1123 | uint8_t buf[1] = { 0 }; |
| 1124 | int serverid; |
| 1125 | |
| 1126 | if (m_id != 0) { |
| 1127 | return INVALID_OPERATION; |
| 1128 | } |
| 1129 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1130 | if (m_serverStartRequested) { |
| 1131 | ret = -EBUSY; |
| 1132 | } else { |
| 1133 | serverid = m_nextServerId++; |
| 1134 | m_serverStartRequested = true; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1135 | bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1136 | |
| 1137 | pthread_mutex_unlock(&m_serverWaitMutex); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1138 | ret = start_server_process(serverid, usePoll); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1139 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1140 | } |
| 1141 | if (ret > 0) { |
| 1142 | if (m_serverStartRequested) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1143 | struct timespec ts; |
| 1144 | clock_gettime(CLOCK_REALTIME, &ts); |
| 1145 | ts.tv_sec += 5; |
| 1146 | ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1147 | } |
| 1148 | if (m_serverStartRequested) { |
| 1149 | m_serverStartRequested = false; |
| 1150 | ret = -ETIMEDOUT; |
| 1151 | } else { |
| 1152 | reply->writeStrongBinder(m_serverStarted); |
| 1153 | reply->writeInt32(serverid); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1154 | m_serverStarted = nullptr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1155 | ret = NO_ERROR; |
| 1156 | } |
| 1157 | } else if (ret >= 0) { |
| 1158 | m_serverStartRequested = false; |
| 1159 | ret = UNKNOWN_ERROR; |
| 1160 | } |
| 1161 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 1162 | return ret; |
| 1163 | } |
| 1164 | case BINDER_LIB_TEST_NOP_TRANSACTION: |
| 1165 | return NO_ERROR; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1166 | case BINDER_LIB_TEST_DELAYED_CALL_BACK: { |
| 1167 | // Note: this transaction is only designed for use with a |
| 1168 | // poll() server. See comments around epoll_wait(). |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1169 | if (m_callback != nullptr) { |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1170 | // A callback was already pending; this means that |
| 1171 | // we received a second call while still processing |
| 1172 | // the first one. Fail the test. |
| 1173 | sp<IBinder> callback = data.readStrongBinder(); |
| 1174 | Parcel data2; |
| 1175 | data2.writeInt32(UNKNOWN_ERROR); |
| 1176 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1177 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1178 | } else { |
| 1179 | m_callback = data.readStrongBinder(); |
| 1180 | int32_t delayUs = data.readInt32(); |
| 1181 | /* |
| 1182 | * It's necessary that we sleep here, so the next |
| 1183 | * transaction the caller makes will be queued to |
| 1184 | * the async queue. |
| 1185 | */ |
| 1186 | usleep(delayUs); |
| 1187 | |
| 1188 | /* |
| 1189 | * Now when we return, libbinder will tell the kernel |
| 1190 | * we are done with this transaction, and the kernel |
| 1191 | * can move the queued transaction to either the |
| 1192 | * thread todo worklist (for kernels without the fix), |
| 1193 | * or the proc todo worklist. In case of the former, |
| 1194 | * the next outbound call will pick up the pending |
| 1195 | * transaction, which leads to undesired reentrant |
| 1196 | * behavior. This is caught in the if() branch above. |
| 1197 | */ |
| 1198 | } |
| 1199 | |
| 1200 | return NO_ERROR; |
| 1201 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1202 | case BINDER_LIB_TEST_NOP_CALL_BACK: { |
| 1203 | Parcel data2, reply2; |
| 1204 | sp<IBinder> binder; |
| 1205 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1206 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1207 | return BAD_VALUE; |
| 1208 | } |
Martijn Coenen | fb368f7 | 2017-08-10 15:03:18 +0200 | [diff] [blame] | 1209 | data2.writeInt32(NO_ERROR); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1210 | binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1211 | return NO_ERROR; |
| 1212 | } |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 1213 | case BINDER_LIB_TEST_GET_SELF_TRANSACTION: |
| 1214 | reply->writeStrongBinder(this); |
| 1215 | return NO_ERROR; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1216 | case BINDER_LIB_TEST_GET_ID_TRANSACTION: |
| 1217 | reply->writeInt32(m_id); |
| 1218 | return NO_ERROR; |
| 1219 | case BINDER_LIB_TEST_INDIRECT_TRANSACTION: { |
| 1220 | int32_t count; |
| 1221 | uint32_t indirect_code; |
| 1222 | sp<IBinder> binder; |
| 1223 | |
| 1224 | count = data.readInt32(); |
| 1225 | reply->writeInt32(m_id); |
| 1226 | reply->writeInt32(count); |
| 1227 | for (int i = 0; i < count; i++) { |
| 1228 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1229 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1230 | return BAD_VALUE; |
| 1231 | } |
| 1232 | indirect_code = data.readInt32(); |
| 1233 | BinderLibTestBundle data2(&data); |
| 1234 | if (!data2.isValid()) { |
| 1235 | return BAD_VALUE; |
| 1236 | } |
| 1237 | BinderLibTestBundle reply2; |
| 1238 | binder->transact(indirect_code, data2, &reply2); |
| 1239 | reply2.appendTo(reply); |
| 1240 | } |
| 1241 | return NO_ERROR; |
| 1242 | } |
| 1243 | case BINDER_LIB_TEST_SET_ERROR_TRANSACTION: |
| 1244 | reply->setError(data.readInt32()); |
| 1245 | return NO_ERROR; |
| 1246 | case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION: |
| 1247 | reply->writeInt32(sizeof(void *)); |
| 1248 | return NO_ERROR; |
| 1249 | case BINDER_LIB_TEST_GET_STATUS_TRANSACTION: |
| 1250 | return NO_ERROR; |
| 1251 | case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION: |
| 1252 | m_strongRef = data.readStrongBinder(); |
| 1253 | return NO_ERROR; |
| 1254 | case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: { |
| 1255 | int ret; |
| 1256 | Parcel data2, reply2; |
| 1257 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 1258 | sp<IBinder> target; |
| 1259 | sp<IBinder> callback; |
| 1260 | |
| 1261 | target = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1262 | if (target == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1263 | return BAD_VALUE; |
| 1264 | } |
| 1265 | callback = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1266 | if (callback == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1267 | return BAD_VALUE; |
| 1268 | } |
| 1269 | ret = target->linkToDeath(testDeathRecipient); |
| 1270 | if (ret == NO_ERROR) |
| 1271 | ret = testDeathRecipient->waitEvent(5); |
| 1272 | data2.writeInt32(ret); |
| 1273 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1274 | return NO_ERROR; |
| 1275 | } |
| 1276 | case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: { |
| 1277 | int ret; |
| 1278 | int32_t size; |
| 1279 | const void *buf; |
| 1280 | int fd; |
| 1281 | |
| 1282 | fd = data.readFileDescriptor(); |
| 1283 | if (fd < 0) { |
| 1284 | return BAD_VALUE; |
| 1285 | } |
| 1286 | ret = data.readInt32(&size); |
| 1287 | if (ret != NO_ERROR) { |
| 1288 | return ret; |
| 1289 | } |
| 1290 | buf = data.readInplace(size); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1291 | if (buf == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1292 | return BAD_VALUE; |
| 1293 | } |
| 1294 | ret = write(fd, buf, size); |
| 1295 | if (ret != size) |
| 1296 | return UNKNOWN_ERROR; |
| 1297 | return NO_ERROR; |
| 1298 | } |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1299 | case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: { |
| 1300 | int ret; |
| 1301 | int32_t size; |
| 1302 | const void *buf; |
| 1303 | android::base::unique_fd fd; |
| 1304 | |
| 1305 | ret = data.readUniqueParcelFileDescriptor(&fd); |
| 1306 | if (ret != NO_ERROR) { |
| 1307 | return ret; |
| 1308 | } |
| 1309 | ret = data.readInt32(&size); |
| 1310 | if (ret != NO_ERROR) { |
| 1311 | return ret; |
| 1312 | } |
| 1313 | buf = data.readInplace(size); |
Yi Kong | 0cf7584 | 2018-07-10 11:44:36 -0700 | [diff] [blame] | 1314 | if (buf == nullptr) { |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1315 | return BAD_VALUE; |
| 1316 | } |
| 1317 | ret = write(fd.get(), buf, size); |
| 1318 | if (ret != size) return UNKNOWN_ERROR; |
| 1319 | return NO_ERROR; |
| 1320 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1321 | case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: { |
| 1322 | int ret; |
| 1323 | wp<IBinder> weak; |
| 1324 | sp<IBinder> strong; |
| 1325 | Parcel data2, reply2; |
| 1326 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1327 | sp<IBinder> server = sm->getService(binderLibTestServiceName); |
| 1328 | |
| 1329 | weak = data.readWeakBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1330 | if (weak == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1331 | return BAD_VALUE; |
| 1332 | } |
| 1333 | strong = weak.promote(); |
| 1334 | |
| 1335 | ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2); |
| 1336 | if (ret != NO_ERROR) |
| 1337 | exit(EXIT_FAILURE); |
| 1338 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1339 | if (strong == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1340 | reply->setError(1); |
| 1341 | } |
| 1342 | return NO_ERROR; |
| 1343 | } |
| 1344 | case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION: |
| 1345 | alarm(10); |
| 1346 | return NO_ERROR; |
| 1347 | case BINDER_LIB_TEST_EXIT_TRANSACTION: |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1348 | while (wait(nullptr) != -1 || errno != ECHILD) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1349 | ; |
| 1350 | exit(EXIT_SUCCESS); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 1351 | case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: { |
| 1352 | bool strongRef = data.readBool(); |
| 1353 | sp<IBinder> binder = new BBinder(); |
| 1354 | if (strongRef) { |
| 1355 | reply->writeStrongBinder(binder); |
| 1356 | } else { |
| 1357 | reply->writeWeakBinder(binder); |
| 1358 | } |
| 1359 | return NO_ERROR; |
| 1360 | } |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 1361 | case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: { |
| 1362 | data.enforceInterface(binderLibTestServiceName); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 1363 | reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid()); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 1364 | return NO_ERROR; |
| 1365 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1366 | default: |
| 1367 | return UNKNOWN_TRANSACTION; |
| 1368 | }; |
| 1369 | } |
| 1370 | private: |
| 1371 | int32_t m_id; |
| 1372 | int32_t m_nextServerId; |
| 1373 | pthread_mutex_t m_serverWaitMutex; |
| 1374 | pthread_cond_t m_serverWaitCond; |
| 1375 | bool m_serverStartRequested; |
| 1376 | sp<IBinder> m_serverStarted; |
| 1377 | sp<IBinder> m_strongRef; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1378 | bool m_callbackPending; |
| 1379 | sp<IBinder> m_callback; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1380 | }; |
| 1381 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1382 | int run_server(int index, int readypipefd, bool usePoll) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1383 | { |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1384 | binderLibTestServiceName += String16(binderserversuffix); |
| 1385 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1386 | status_t ret; |
| 1387 | sp<IServiceManager> sm = defaultServiceManager(); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1388 | BinderLibTestService* testServicePtr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1389 | { |
| 1390 | sp<BinderLibTestService> testService = new BinderLibTestService(index); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1391 | /* |
| 1392 | * We need this below, but can't hold a sp<> because it prevents the |
| 1393 | * node from being cleaned up automatically. It's safe in this case |
| 1394 | * because of how the tests are written. |
| 1395 | */ |
| 1396 | testServicePtr = testService.get(); |
| 1397 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1398 | if (index == 0) { |
| 1399 | ret = sm->addService(binderLibTestServiceName, testService); |
| 1400 | } else { |
| 1401 | sp<IBinder> server = sm->getService(binderLibTestServiceName); |
| 1402 | Parcel data, reply; |
| 1403 | data.writeInt32(index); |
| 1404 | data.writeStrongBinder(testService); |
| 1405 | |
| 1406 | ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply); |
| 1407 | } |
| 1408 | } |
| 1409 | write(readypipefd, &ret, sizeof(ret)); |
| 1410 | close(readypipefd); |
| 1411 | //printf("%s: ret %d\n", __func__, ret); |
| 1412 | if (ret) |
| 1413 | return 1; |
| 1414 | //printf("%s: joinThreadPool\n", __func__); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1415 | if (usePoll) { |
| 1416 | int fd; |
| 1417 | struct epoll_event ev; |
| 1418 | int epoll_fd; |
| 1419 | IPCThreadState::self()->setupPolling(&fd); |
| 1420 | if (fd < 0) { |
| 1421 | return 1; |
| 1422 | } |
| 1423 | IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER |
| 1424 | |
| 1425 | epoll_fd = epoll_create1(0); |
| 1426 | if (epoll_fd == -1) { |
| 1427 | return 1; |
| 1428 | } |
| 1429 | |
| 1430 | ev.events = EPOLLIN; |
| 1431 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | while (1) { |
| 1436 | /* |
| 1437 | * We simulate a single-threaded process using the binder poll |
| 1438 | * interface; besides handling binder commands, it can also |
| 1439 | * issue outgoing transactions, by storing a callback in |
| 1440 | * m_callback and setting m_callbackPending. |
| 1441 | * |
| 1442 | * processPendingCall() will then issue that transaction. |
| 1443 | */ |
| 1444 | struct epoll_event events[1]; |
| 1445 | int numEvents = epoll_wait(epoll_fd, events, 1, 1000); |
| 1446 | if (numEvents < 0) { |
| 1447 | if (errno == EINTR) { |
| 1448 | continue; |
| 1449 | } |
| 1450 | return 1; |
| 1451 | } |
| 1452 | if (numEvents > 0) { |
| 1453 | IPCThreadState::self()->handlePolledCommands(); |
| 1454 | IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER |
| 1455 | testServicePtr->processPendingCall(); |
| 1456 | } |
| 1457 | } |
| 1458 | } else { |
| 1459 | ProcessState::self()->startThreadPool(); |
| 1460 | IPCThreadState::self()->joinThreadPool(); |
| 1461 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1462 | //printf("%s: joinThreadPool returned\n", __func__); |
| 1463 | return 1; /* joinThreadPool should not return */ |
| 1464 | } |
| 1465 | |
| 1466 | int main(int argc, char **argv) { |
| 1467 | int ret; |
| 1468 | |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1469 | if (argc == 4 && !strcmp(argv[1], "--servername")) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1470 | binderservername = argv[2]; |
| 1471 | } else { |
| 1472 | binderservername = argv[0]; |
| 1473 | } |
| 1474 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1475 | if (argc == 6 && !strcmp(argv[1], binderserverarg)) { |
| 1476 | binderserversuffix = argv[5]; |
| 1477 | return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1478 | } |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1479 | binderserversuffix = new char[16]; |
| 1480 | snprintf(binderserversuffix, 16, "%d", getpid()); |
| 1481 | binderLibTestServiceName += String16(binderserversuffix); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1482 | |
| 1483 | ::testing::InitGoogleTest(&argc, argv); |
| 1484 | binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv()); |
| 1485 | ProcessState::self()->startThreadPool(); |
| 1486 | return RUN_ALL_TESTS(); |
| 1487 | } |