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_EXIT_TRANSACTION, |
| 70 | BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION, |
| 71 | BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 72 | BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 73 | BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, |
Kevin DuBois | 2f82d5b | 2018-12-05 12:56:10 -0800 | [diff] [blame] | 74 | BINDER_LIB_TEST_ECHO_VECTOR, |
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) {} |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 235 | explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 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 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 555 | TEST_F(BinderLibTest, DeathNotificationStrongRef) |
| 556 | { |
| 557 | status_t ret; |
| 558 | sp<IBinder> sbinder; |
| 559 | |
| 560 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 561 | |
| 562 | { |
| 563 | sp<IBinder> binder = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 564 | ASSERT_TRUE(binder != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 565 | ret = binder->linkToDeath(testDeathRecipient); |
| 566 | EXPECT_EQ(NO_ERROR, ret); |
| 567 | sbinder = binder; |
| 568 | } |
| 569 | { |
| 570 | Parcel data, reply; |
| 571 | ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 572 | EXPECT_EQ(0, ret); |
| 573 | } |
| 574 | IPCThreadState::self()->flushCommands(); |
| 575 | ret = testDeathRecipient->waitEvent(5); |
| 576 | EXPECT_EQ(NO_ERROR, ret); |
| 577 | ret = sbinder->unlinkToDeath(testDeathRecipient); |
| 578 | EXPECT_EQ(DEAD_OBJECT, ret); |
| 579 | } |
| 580 | |
| 581 | TEST_F(BinderLibTest, DeathNotificationMultiple) |
| 582 | { |
| 583 | status_t ret; |
| 584 | const int clientcount = 2; |
| 585 | sp<IBinder> target; |
| 586 | sp<IBinder> linkedclient[clientcount]; |
| 587 | sp<BinderLibTestCallBack> callBack[clientcount]; |
| 588 | sp<IBinder> passiveclient[clientcount]; |
| 589 | |
| 590 | target = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 591 | ASSERT_TRUE(target != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 592 | for (int i = 0; i < clientcount; i++) { |
| 593 | { |
| 594 | Parcel data, reply; |
| 595 | |
| 596 | linkedclient[i] = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 597 | ASSERT_TRUE(linkedclient[i] != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 598 | callBack[i] = new BinderLibTestCallBack(); |
| 599 | data.writeStrongBinder(target); |
| 600 | data.writeStrongBinder(callBack[i]); |
| 601 | ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 602 | EXPECT_EQ(NO_ERROR, ret); |
| 603 | } |
| 604 | { |
| 605 | Parcel data, reply; |
| 606 | |
| 607 | passiveclient[i] = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 608 | ASSERT_TRUE(passiveclient[i] != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 609 | data.writeStrongBinder(target); |
| 610 | ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 611 | EXPECT_EQ(NO_ERROR, ret); |
| 612 | } |
| 613 | } |
| 614 | { |
| 615 | Parcel data, reply; |
| 616 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 617 | EXPECT_EQ(0, ret); |
| 618 | } |
| 619 | |
| 620 | for (int i = 0; i < clientcount; i++) { |
| 621 | ret = callBack[i]->waitEvent(5); |
| 622 | EXPECT_EQ(NO_ERROR, ret); |
| 623 | ret = callBack[i]->getResult(); |
| 624 | EXPECT_EQ(NO_ERROR, ret); |
| 625 | } |
| 626 | } |
| 627 | |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 628 | TEST_F(BinderLibTest, DeathNotificationThread) |
| 629 | { |
| 630 | status_t ret; |
| 631 | sp<BinderLibTestCallBack> callback; |
| 632 | sp<IBinder> target = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 633 | ASSERT_TRUE(target != nullptr); |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 634 | sp<IBinder> client = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 635 | ASSERT_TRUE(client != nullptr); |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 636 | |
| 637 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 638 | |
| 639 | ret = target->linkToDeath(testDeathRecipient); |
| 640 | EXPECT_EQ(NO_ERROR, ret); |
| 641 | |
| 642 | { |
| 643 | Parcel data, reply; |
| 644 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 645 | EXPECT_EQ(0, ret); |
| 646 | } |
| 647 | |
| 648 | /* Make sure it's dead */ |
| 649 | testDeathRecipient->waitEvent(5); |
| 650 | |
| 651 | /* Now, pass the ref to another process and ask that process to |
| 652 | * call linkToDeath() on it, and wait for a response. This tests |
| 653 | * two things: |
| 654 | * 1) You still get death notifications when calling linkToDeath() |
| 655 | * on a ref that is already dead when it was passed to you. |
| 656 | * 2) That death notifications are not directly pushed to the thread |
| 657 | * registering them, but to the threadpool (proc workqueue) instead. |
| 658 | * |
| 659 | * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION |
| 660 | * is blocked on a condition variable waiting for the death notification to be |
| 661 | * called; therefore, that thread is not available for handling proc work. |
| 662 | * So, if the death notification was pushed to the thread workqueue, the callback |
| 663 | * would never be called, and the test would timeout and fail. |
| 664 | * |
| 665 | * Note that we can't do this part of the test from this thread itself, because |
| 666 | * the binder driver would only push death notifications to the thread if |
| 667 | * it is a looper thread, which this thread is not. |
| 668 | * |
| 669 | * See b/23525545 for details. |
| 670 | */ |
| 671 | { |
| 672 | Parcel data, reply; |
| 673 | |
| 674 | callback = new BinderLibTestCallBack(); |
| 675 | data.writeStrongBinder(target); |
| 676 | data.writeStrongBinder(callback); |
| 677 | ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 678 | EXPECT_EQ(NO_ERROR, ret); |
| 679 | } |
| 680 | |
| 681 | ret = callback->waitEvent(5); |
| 682 | EXPECT_EQ(NO_ERROR, ret); |
| 683 | ret = callback->getResult(); |
| 684 | EXPECT_EQ(NO_ERROR, ret); |
| 685 | } |
| 686 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 687 | TEST_F(BinderLibTest, PassFile) { |
| 688 | int ret; |
| 689 | int pipefd[2]; |
| 690 | uint8_t buf[1] = { 0 }; |
| 691 | uint8_t write_value = 123; |
| 692 | |
| 693 | ret = pipe2(pipefd, O_NONBLOCK); |
| 694 | ASSERT_EQ(0, ret); |
| 695 | |
| 696 | { |
| 697 | Parcel data, reply; |
| 698 | uint8_t writebuf[1] = { write_value }; |
| 699 | |
| 700 | ret = data.writeFileDescriptor(pipefd[1], true); |
| 701 | EXPECT_EQ(NO_ERROR, ret); |
| 702 | |
| 703 | ret = data.writeInt32(sizeof(writebuf)); |
| 704 | EXPECT_EQ(NO_ERROR, ret); |
| 705 | |
| 706 | ret = data.write(writebuf, sizeof(writebuf)); |
| 707 | EXPECT_EQ(NO_ERROR, ret); |
| 708 | |
| 709 | ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply); |
| 710 | EXPECT_EQ(NO_ERROR, ret); |
| 711 | } |
| 712 | |
| 713 | ret = read(pipefd[0], buf, sizeof(buf)); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 714 | EXPECT_EQ(sizeof(buf), (size_t)ret); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 715 | EXPECT_EQ(write_value, buf[0]); |
| 716 | |
| 717 | waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */ |
| 718 | |
| 719 | ret = read(pipefd[0], buf, sizeof(buf)); |
| 720 | EXPECT_EQ(0, ret); |
| 721 | |
| 722 | close(pipefd[0]); |
| 723 | } |
| 724 | |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 725 | TEST_F(BinderLibTest, PassParcelFileDescriptor) { |
| 726 | const int datasize = 123; |
| 727 | std::vector<uint8_t> writebuf(datasize); |
| 728 | for (size_t i = 0; i < writebuf.size(); ++i) { |
| 729 | writebuf[i] = i; |
| 730 | } |
| 731 | |
| 732 | android::base::unique_fd read_end, write_end; |
| 733 | { |
| 734 | int pipefd[2]; |
| 735 | ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK)); |
| 736 | read_end.reset(pipefd[0]); |
| 737 | write_end.reset(pipefd[1]); |
| 738 | } |
| 739 | { |
| 740 | Parcel data; |
| 741 | EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get())); |
| 742 | write_end.reset(); |
| 743 | EXPECT_EQ(NO_ERROR, data.writeInt32(datasize)); |
| 744 | EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize)); |
| 745 | |
| 746 | Parcel reply; |
| 747 | EXPECT_EQ(NO_ERROR, |
| 748 | m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data, |
| 749 | &reply)); |
| 750 | } |
| 751 | std::vector<uint8_t> readbuf(datasize); |
| 752 | EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize)); |
| 753 | EXPECT_EQ(writebuf, readbuf); |
| 754 | |
| 755 | waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */ |
| 756 | |
| 757 | EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize)); |
| 758 | } |
| 759 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 760 | TEST_F(BinderLibTest, PromoteLocal) { |
| 761 | sp<IBinder> strong = new BBinder(); |
| 762 | wp<IBinder> weak = strong; |
| 763 | sp<IBinder> strong_from_weak = weak.promote(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 764 | EXPECT_TRUE(strong != nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 765 | EXPECT_EQ(strong, strong_from_weak); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 766 | strong = nullptr; |
| 767 | strong_from_weak = nullptr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 768 | strong_from_weak = weak.promote(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 769 | EXPECT_TRUE(strong_from_weak == nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 772 | TEST_F(BinderLibTest, LocalGetExtension) { |
| 773 | sp<BBinder> binder = new BBinder(); |
| 774 | sp<IBinder> ext = new BBinder(); |
| 775 | binder->setExtension(ext); |
| 776 | EXPECT_EQ(ext, binder->getExtension()); |
| 777 | } |
| 778 | |
| 779 | TEST_F(BinderLibTest, RemoteGetExtension) { |
| 780 | sp<IBinder> server = addServer(); |
| 781 | ASSERT_TRUE(server != nullptr); |
| 782 | |
| 783 | sp<IBinder> extension; |
| 784 | EXPECT_EQ(NO_ERROR, server->getExtension(&extension)); |
| 785 | ASSERT_NE(nullptr, extension.get()); |
| 786 | |
| 787 | EXPECT_EQ(NO_ERROR, extension->pingBinder()); |
| 788 | } |
| 789 | |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 790 | TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) { |
| 791 | status_t ret; |
| 792 | Parcel data, reply; |
| 793 | |
| 794 | ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply); |
| 795 | EXPECT_EQ(NO_ERROR, ret); |
| 796 | |
| 797 | const flat_binder_object *fb = reply.readObject(false); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 798 | ASSERT_TRUE(fb != nullptr); |
Hsin-Yi Chen | ad6503c | 2017-07-28 11:28:52 +0800 | [diff] [blame] | 799 | EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type); |
| 800 | EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle)); |
| 801 | EXPECT_EQ((binder_uintptr_t)0, fb->cookie); |
| 802 | EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32); |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 805 | TEST_F(BinderLibTest, FreedBinder) { |
| 806 | status_t ret; |
| 807 | |
| 808 | sp<IBinder> server = addServer(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 809 | ASSERT_TRUE(server != nullptr); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 810 | |
| 811 | __u32 freedHandle; |
| 812 | wp<IBinder> keepFreedBinder; |
| 813 | { |
| 814 | Parcel data, reply; |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 815 | ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply); |
| 816 | ASSERT_EQ(NO_ERROR, ret); |
| 817 | struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data()); |
| 818 | freedHandle = freed->handle; |
| 819 | /* Add a weak ref to the freed binder so the driver does not |
| 820 | * delete its reference to it - otherwise the transaction |
| 821 | * fails regardless of whether the driver is fixed. |
| 822 | */ |
Steven Moreland | e171d62 | 2019-07-17 16:06:01 -0700 | [diff] [blame] | 823 | keepFreedBinder = reply.readStrongBinder(); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 824 | } |
Steven Moreland | e171d62 | 2019-07-17 16:06:01 -0700 | [diff] [blame] | 825 | IPCThreadState::self()->flushCommands(); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 826 | { |
| 827 | Parcel data, reply; |
| 828 | data.writeStrongBinder(server); |
| 829 | /* Replace original handle with handle to the freed binder */ |
| 830 | struct flat_binder_object *strong = (struct flat_binder_object *)(data.data()); |
| 831 | __u32 oldHandle = strong->handle; |
| 832 | strong->handle = freedHandle; |
| 833 | ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply); |
| 834 | /* Returns DEAD_OBJECT (-32) if target crashes and |
| 835 | * FAILED_TRANSACTION if the driver rejects the invalid |
| 836 | * object. |
| 837 | */ |
| 838 | EXPECT_EQ((status_t)FAILED_TRANSACTION, ret); |
| 839 | /* Restore original handle so parcel destructor does not use |
| 840 | * the wrong handle. |
| 841 | */ |
| 842 | strong->handle = oldHandle; |
| 843 | } |
| 844 | } |
| 845 | |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 846 | TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) { |
| 847 | status_t ret; |
| 848 | Parcel data, reply; |
| 849 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 850 | for (int i = 0; i < 2; i++) { |
| 851 | BinderLibTestBundle datai; |
| 852 | datai.appendFrom(&data, 0, data.dataSize()); |
| 853 | |
| 854 | data.freeData(); |
| 855 | data.writeInt32(1); |
| 856 | data.writeStrongBinder(callBack); |
| 857 | data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF); |
| 858 | |
| 859 | datai.appendTo(&data); |
| 860 | } |
| 861 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 862 | EXPECT_EQ(NO_ERROR, ret); |
| 863 | } |
| 864 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 865 | TEST_F(BinderLibTest, OnewayQueueing) |
| 866 | { |
| 867 | status_t ret; |
| 868 | Parcel data, data2; |
| 869 | |
| 870 | sp<IBinder> pollServer = addPollServer(); |
| 871 | |
| 872 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 873 | data.writeStrongBinder(callBack); |
| 874 | data.writeInt32(500000); // delay in us before calling back |
| 875 | |
| 876 | sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack(); |
| 877 | data2.writeStrongBinder(callBack2); |
| 878 | data2.writeInt32(0); // delay in us |
| 879 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 880 | 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] | 881 | EXPECT_EQ(NO_ERROR, ret); |
| 882 | |
| 883 | // The delay ensures that this second transaction will end up on the async_todo list |
| 884 | // (for a single-threaded server) |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 885 | 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] | 886 | EXPECT_EQ(NO_ERROR, ret); |
| 887 | |
| 888 | // The server will ensure that the two transactions are handled in the expected order; |
| 889 | // If the ordering is not as expected, an error will be returned through the callbacks. |
| 890 | ret = callBack->waitEvent(2); |
| 891 | EXPECT_EQ(NO_ERROR, ret); |
| 892 | ret = callBack->getResult(); |
| 893 | EXPECT_EQ(NO_ERROR, ret); |
| 894 | |
| 895 | ret = callBack2->waitEvent(2); |
| 896 | EXPECT_EQ(NO_ERROR, ret); |
| 897 | ret = callBack2->getResult(); |
| 898 | EXPECT_EQ(NO_ERROR, ret); |
| 899 | } |
| 900 | |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 901 | TEST_F(BinderLibTest, WorkSourceUnsetByDefault) |
| 902 | { |
| 903 | status_t ret; |
| 904 | Parcel data, reply; |
| 905 | data.writeInterfaceToken(binderLibTestServiceName); |
| 906 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 907 | EXPECT_EQ(-1, reply.readInt32()); |
| 908 | EXPECT_EQ(NO_ERROR, ret); |
| 909 | } |
| 910 | |
| 911 | TEST_F(BinderLibTest, WorkSourceSet) |
| 912 | { |
| 913 | status_t ret; |
| 914 | Parcel data, reply; |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 915 | IPCThreadState::self()->clearCallingWorkSource(); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 916 | int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 917 | data.writeInterfaceToken(binderLibTestServiceName); |
| 918 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 919 | EXPECT_EQ(100, reply.readInt32()); |
| 920 | EXPECT_EQ(-1, previousWorkSource); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 921 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 922 | EXPECT_EQ(NO_ERROR, ret); |
| 923 | } |
| 924 | |
| 925 | TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation) |
| 926 | { |
| 927 | status_t ret; |
| 928 | Parcel data, reply; |
| 929 | |
| 930 | IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100); |
| 931 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 932 | |
| 933 | data.writeInterfaceToken(binderLibTestServiceName); |
| 934 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 935 | EXPECT_EQ(-1, reply.readInt32()); |
| 936 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 937 | EXPECT_EQ(NO_ERROR, ret); |
| 938 | } |
| 939 | |
| 940 | TEST_F(BinderLibTest, WorkSourceCleared) |
| 941 | { |
| 942 | status_t ret; |
| 943 | Parcel data, reply; |
| 944 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 945 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 946 | int64_t token = IPCThreadState::self()->clearCallingWorkSource(); |
| 947 | int32_t previousWorkSource = (int32_t)token; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 948 | data.writeInterfaceToken(binderLibTestServiceName); |
| 949 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 950 | |
| 951 | EXPECT_EQ(-1, reply.readInt32()); |
| 952 | EXPECT_EQ(100, previousWorkSource); |
| 953 | EXPECT_EQ(NO_ERROR, ret); |
| 954 | } |
| 955 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 956 | TEST_F(BinderLibTest, WorkSourceRestored) |
| 957 | { |
| 958 | status_t ret; |
| 959 | Parcel data, reply; |
| 960 | |
| 961 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 962 | int64_t token = IPCThreadState::self()->clearCallingWorkSource(); |
| 963 | IPCThreadState::self()->restoreCallingWorkSource(token); |
| 964 | |
| 965 | data.writeInterfaceToken(binderLibTestServiceName); |
| 966 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 967 | |
| 968 | EXPECT_EQ(100, reply.readInt32()); |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 969 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 970 | EXPECT_EQ(NO_ERROR, ret); |
| 971 | } |
| 972 | |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 973 | TEST_F(BinderLibTest, PropagateFlagSet) |
| 974 | { |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 975 | IPCThreadState::self()->clearPropagateWorkSource(); |
| 976 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 977 | EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 978 | } |
| 979 | |
| 980 | TEST_F(BinderLibTest, PropagateFlagCleared) |
| 981 | { |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 982 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 983 | IPCThreadState::self()->clearPropagateWorkSource(); |
| 984 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 985 | } |
| 986 | |
| 987 | TEST_F(BinderLibTest, PropagateFlagRestored) |
| 988 | { |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 989 | int token = IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 990 | IPCThreadState::self()->restoreCallingWorkSource(token); |
| 991 | |
| 992 | EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource()); |
| 993 | } |
| 994 | |
| 995 | TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls) |
| 996 | { |
| 997 | IPCThreadState::self()->setCallingWorkSourceUid(100); |
| 998 | |
| 999 | Parcel data, reply; |
| 1000 | status_t ret; |
| 1001 | data.writeInterfaceToken(binderLibTestServiceName); |
| 1002 | ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply); |
| 1003 | |
| 1004 | Parcel data2, reply2; |
| 1005 | status_t ret2; |
| 1006 | data2.writeInterfaceToken(binderLibTestServiceName); |
| 1007 | ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2); |
| 1008 | EXPECT_EQ(100, reply2.readInt32()); |
| 1009 | EXPECT_EQ(NO_ERROR, ret2); |
| 1010 | } |
| 1011 | |
Kevin DuBois | 2f82d5b | 2018-12-05 12:56:10 -0800 | [diff] [blame] | 1012 | TEST_F(BinderLibTest, VectorSent) { |
| 1013 | Parcel data, reply; |
| 1014 | sp<IBinder> server = addServer(); |
| 1015 | ASSERT_TRUE(server != nullptr); |
| 1016 | |
| 1017 | std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 }; |
| 1018 | data.writeUint64Vector(testValue); |
| 1019 | |
| 1020 | status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply); |
| 1021 | EXPECT_EQ(NO_ERROR, ret); |
| 1022 | std::vector<uint64_t> readValue; |
| 1023 | ret = reply.readUint64Vector(&readValue); |
| 1024 | EXPECT_EQ(readValue, testValue); |
| 1025 | } |
| 1026 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1027 | class BinderLibTestService : public BBinder |
| 1028 | { |
| 1029 | public: |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 1030 | explicit BinderLibTestService(int32_t id) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1031 | : m_id(id) |
| 1032 | , m_nextServerId(id + 1) |
| 1033 | , m_serverStartRequested(false) |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1034 | , m_callback(nullptr) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1035 | { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1036 | pthread_mutex_init(&m_serverWaitMutex, nullptr); |
| 1037 | pthread_cond_init(&m_serverWaitCond, nullptr); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1038 | } |
| 1039 | ~BinderLibTestService() |
| 1040 | { |
| 1041 | exit(EXIT_SUCCESS); |
| 1042 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1043 | |
| 1044 | void processPendingCall() { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1045 | if (m_callback != nullptr) { |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1046 | Parcel data; |
| 1047 | data.writeInt32(NO_ERROR); |
| 1048 | 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] | 1049 | m_callback = nullptr; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1053 | virtual status_t onTransact(uint32_t code, |
| 1054 | const Parcel& data, Parcel* reply, |
| 1055 | uint32_t flags = 0) { |
| 1056 | //printf("%s: code %d\n", __func__, code); |
| 1057 | (void)flags; |
| 1058 | |
| 1059 | if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) { |
| 1060 | return PERMISSION_DENIED; |
| 1061 | } |
| 1062 | switch (code) { |
| 1063 | case BINDER_LIB_TEST_REGISTER_SERVER: { |
| 1064 | int32_t id; |
| 1065 | sp<IBinder> binder; |
| 1066 | id = data.readInt32(); |
| 1067 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1068 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1069 | return BAD_VALUE; |
| 1070 | } |
| 1071 | |
| 1072 | if (m_id != 0) |
| 1073 | return INVALID_OPERATION; |
| 1074 | |
| 1075 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1076 | if (m_serverStartRequested) { |
| 1077 | m_serverStartRequested = false; |
| 1078 | m_serverStarted = binder; |
| 1079 | pthread_cond_signal(&m_serverWaitCond); |
| 1080 | } |
| 1081 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 1082 | return NO_ERROR; |
| 1083 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1084 | case BINDER_LIB_TEST_ADD_POLL_SERVER: |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1085 | case BINDER_LIB_TEST_ADD_SERVER: { |
| 1086 | int ret; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1087 | int serverid; |
| 1088 | |
| 1089 | if (m_id != 0) { |
| 1090 | return INVALID_OPERATION; |
| 1091 | } |
| 1092 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1093 | if (m_serverStartRequested) { |
| 1094 | ret = -EBUSY; |
| 1095 | } else { |
| 1096 | serverid = m_nextServerId++; |
| 1097 | m_serverStartRequested = true; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1098 | bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1099 | |
| 1100 | pthread_mutex_unlock(&m_serverWaitMutex); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1101 | ret = start_server_process(serverid, usePoll); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1102 | pthread_mutex_lock(&m_serverWaitMutex); |
| 1103 | } |
| 1104 | if (ret > 0) { |
| 1105 | if (m_serverStartRequested) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1106 | struct timespec ts; |
| 1107 | clock_gettime(CLOCK_REALTIME, &ts); |
| 1108 | ts.tv_sec += 5; |
| 1109 | ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1110 | } |
| 1111 | if (m_serverStartRequested) { |
| 1112 | m_serverStartRequested = false; |
| 1113 | ret = -ETIMEDOUT; |
| 1114 | } else { |
| 1115 | reply->writeStrongBinder(m_serverStarted); |
| 1116 | reply->writeInt32(serverid); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1117 | m_serverStarted = nullptr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1118 | ret = NO_ERROR; |
| 1119 | } |
| 1120 | } else if (ret >= 0) { |
| 1121 | m_serverStartRequested = false; |
| 1122 | ret = UNKNOWN_ERROR; |
| 1123 | } |
| 1124 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | case BINDER_LIB_TEST_NOP_TRANSACTION: |
| 1128 | return NO_ERROR; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1129 | case BINDER_LIB_TEST_DELAYED_CALL_BACK: { |
| 1130 | // Note: this transaction is only designed for use with a |
| 1131 | // poll() server. See comments around epoll_wait(). |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1132 | if (m_callback != nullptr) { |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1133 | // A callback was already pending; this means that |
| 1134 | // we received a second call while still processing |
| 1135 | // the first one. Fail the test. |
| 1136 | sp<IBinder> callback = data.readStrongBinder(); |
| 1137 | Parcel data2; |
| 1138 | data2.writeInt32(UNKNOWN_ERROR); |
| 1139 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1140 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1141 | } else { |
| 1142 | m_callback = data.readStrongBinder(); |
| 1143 | int32_t delayUs = data.readInt32(); |
| 1144 | /* |
| 1145 | * It's necessary that we sleep here, so the next |
| 1146 | * transaction the caller makes will be queued to |
| 1147 | * the async queue. |
| 1148 | */ |
| 1149 | usleep(delayUs); |
| 1150 | |
| 1151 | /* |
| 1152 | * Now when we return, libbinder will tell the kernel |
| 1153 | * we are done with this transaction, and the kernel |
| 1154 | * can move the queued transaction to either the |
| 1155 | * thread todo worklist (for kernels without the fix), |
| 1156 | * or the proc todo worklist. In case of the former, |
| 1157 | * the next outbound call will pick up the pending |
| 1158 | * transaction, which leads to undesired reentrant |
| 1159 | * behavior. This is caught in the if() branch above. |
| 1160 | */ |
| 1161 | } |
| 1162 | |
| 1163 | return NO_ERROR; |
| 1164 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1165 | case BINDER_LIB_TEST_NOP_CALL_BACK: { |
| 1166 | Parcel data2, reply2; |
| 1167 | sp<IBinder> binder; |
| 1168 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1169 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1170 | return BAD_VALUE; |
| 1171 | } |
Martijn Coenen | fb368f7 | 2017-08-10 15:03:18 +0200 | [diff] [blame] | 1172 | data2.writeInt32(NO_ERROR); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1173 | binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1174 | return NO_ERROR; |
| 1175 | } |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 1176 | case BINDER_LIB_TEST_GET_SELF_TRANSACTION: |
| 1177 | reply->writeStrongBinder(this); |
| 1178 | return NO_ERROR; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1179 | case BINDER_LIB_TEST_GET_ID_TRANSACTION: |
| 1180 | reply->writeInt32(m_id); |
| 1181 | return NO_ERROR; |
| 1182 | case BINDER_LIB_TEST_INDIRECT_TRANSACTION: { |
| 1183 | int32_t count; |
| 1184 | uint32_t indirect_code; |
| 1185 | sp<IBinder> binder; |
| 1186 | |
| 1187 | count = data.readInt32(); |
| 1188 | reply->writeInt32(m_id); |
| 1189 | reply->writeInt32(count); |
| 1190 | for (int i = 0; i < count; i++) { |
| 1191 | binder = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1192 | if (binder == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1193 | return BAD_VALUE; |
| 1194 | } |
| 1195 | indirect_code = data.readInt32(); |
| 1196 | BinderLibTestBundle data2(&data); |
| 1197 | if (!data2.isValid()) { |
| 1198 | return BAD_VALUE; |
| 1199 | } |
| 1200 | BinderLibTestBundle reply2; |
| 1201 | binder->transact(indirect_code, data2, &reply2); |
| 1202 | reply2.appendTo(reply); |
| 1203 | } |
| 1204 | return NO_ERROR; |
| 1205 | } |
| 1206 | case BINDER_LIB_TEST_SET_ERROR_TRANSACTION: |
| 1207 | reply->setError(data.readInt32()); |
| 1208 | return NO_ERROR; |
| 1209 | case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION: |
| 1210 | reply->writeInt32(sizeof(void *)); |
| 1211 | return NO_ERROR; |
| 1212 | case BINDER_LIB_TEST_GET_STATUS_TRANSACTION: |
| 1213 | return NO_ERROR; |
| 1214 | case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION: |
| 1215 | m_strongRef = data.readStrongBinder(); |
| 1216 | return NO_ERROR; |
| 1217 | case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: { |
| 1218 | int ret; |
| 1219 | Parcel data2, reply2; |
| 1220 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 1221 | sp<IBinder> target; |
| 1222 | sp<IBinder> callback; |
| 1223 | |
| 1224 | target = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1225 | if (target == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1226 | return BAD_VALUE; |
| 1227 | } |
| 1228 | callback = data.readStrongBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1229 | if (callback == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1230 | return BAD_VALUE; |
| 1231 | } |
| 1232 | ret = target->linkToDeath(testDeathRecipient); |
| 1233 | if (ret == NO_ERROR) |
| 1234 | ret = testDeathRecipient->waitEvent(5); |
| 1235 | data2.writeInt32(ret); |
| 1236 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1237 | return NO_ERROR; |
| 1238 | } |
| 1239 | case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: { |
| 1240 | int ret; |
| 1241 | int32_t size; |
| 1242 | const void *buf; |
| 1243 | int fd; |
| 1244 | |
| 1245 | fd = data.readFileDescriptor(); |
| 1246 | if (fd < 0) { |
| 1247 | return BAD_VALUE; |
| 1248 | } |
| 1249 | ret = data.readInt32(&size); |
| 1250 | if (ret != NO_ERROR) { |
| 1251 | return ret; |
| 1252 | } |
| 1253 | buf = data.readInplace(size); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1254 | if (buf == nullptr) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1255 | return BAD_VALUE; |
| 1256 | } |
| 1257 | ret = write(fd, buf, size); |
| 1258 | if (ret != size) |
| 1259 | return UNKNOWN_ERROR; |
| 1260 | return NO_ERROR; |
| 1261 | } |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1262 | case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: { |
| 1263 | int ret; |
| 1264 | int32_t size; |
| 1265 | const void *buf; |
| 1266 | android::base::unique_fd fd; |
| 1267 | |
| 1268 | ret = data.readUniqueParcelFileDescriptor(&fd); |
| 1269 | if (ret != NO_ERROR) { |
| 1270 | return ret; |
| 1271 | } |
| 1272 | ret = data.readInt32(&size); |
| 1273 | if (ret != NO_ERROR) { |
| 1274 | return ret; |
| 1275 | } |
| 1276 | buf = data.readInplace(size); |
Yi Kong | 0cf7584 | 2018-07-10 11:44:36 -0700 | [diff] [blame] | 1277 | if (buf == nullptr) { |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1278 | return BAD_VALUE; |
| 1279 | } |
| 1280 | ret = write(fd.get(), buf, size); |
| 1281 | if (ret != size) return UNKNOWN_ERROR; |
| 1282 | return NO_ERROR; |
| 1283 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1284 | case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION: |
| 1285 | alarm(10); |
| 1286 | return NO_ERROR; |
| 1287 | case BINDER_LIB_TEST_EXIT_TRANSACTION: |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1288 | while (wait(nullptr) != -1 || errno != ECHILD) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1289 | ; |
| 1290 | exit(EXIT_SUCCESS); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 1291 | case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: { |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 1292 | sp<IBinder> binder = new BBinder(); |
Steven Moreland | e171d62 | 2019-07-17 16:06:01 -0700 | [diff] [blame] | 1293 | reply->writeStrongBinder(binder); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 1294 | return NO_ERROR; |
| 1295 | } |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 1296 | case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: { |
| 1297 | data.enforceInterface(binderLibTestServiceName); |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 1298 | reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid()); |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 1299 | return NO_ERROR; |
| 1300 | } |
Kevin DuBois | 2f82d5b | 2018-12-05 12:56:10 -0800 | [diff] [blame] | 1301 | case BINDER_LIB_TEST_ECHO_VECTOR: { |
| 1302 | std::vector<uint64_t> vector; |
| 1303 | auto err = data.readUint64Vector(&vector); |
| 1304 | if (err != NO_ERROR) |
| 1305 | return err; |
| 1306 | reply->writeUint64Vector(vector); |
| 1307 | return NO_ERROR; |
| 1308 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1309 | default: |
| 1310 | return UNKNOWN_TRANSACTION; |
| 1311 | }; |
| 1312 | } |
| 1313 | private: |
| 1314 | int32_t m_id; |
| 1315 | int32_t m_nextServerId; |
| 1316 | pthread_mutex_t m_serverWaitMutex; |
| 1317 | pthread_cond_t m_serverWaitCond; |
| 1318 | bool m_serverStartRequested; |
| 1319 | sp<IBinder> m_serverStarted; |
| 1320 | sp<IBinder> m_strongRef; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1321 | sp<IBinder> m_callback; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1322 | }; |
| 1323 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1324 | int run_server(int index, int readypipefd, bool usePoll) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1325 | { |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1326 | binderLibTestServiceName += String16(binderserversuffix); |
| 1327 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1328 | status_t ret; |
| 1329 | sp<IServiceManager> sm = defaultServiceManager(); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1330 | BinderLibTestService* testServicePtr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1331 | { |
| 1332 | sp<BinderLibTestService> testService = new BinderLibTestService(index); |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 1333 | |
| 1334 | /* |
| 1335 | * Normally would also contain functionality as well, but we are only |
| 1336 | * testing the extension mechanism. |
| 1337 | */ |
| 1338 | testService->setExtension(new BBinder()); |
| 1339 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1340 | /* |
| 1341 | * We need this below, but can't hold a sp<> because it prevents the |
| 1342 | * node from being cleaned up automatically. It's safe in this case |
| 1343 | * because of how the tests are written. |
| 1344 | */ |
| 1345 | testServicePtr = testService.get(); |
| 1346 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1347 | if (index == 0) { |
| 1348 | ret = sm->addService(binderLibTestServiceName, testService); |
| 1349 | } else { |
| 1350 | sp<IBinder> server = sm->getService(binderLibTestServiceName); |
| 1351 | Parcel data, reply; |
| 1352 | data.writeInt32(index); |
| 1353 | data.writeStrongBinder(testService); |
| 1354 | |
| 1355 | ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply); |
| 1356 | } |
| 1357 | } |
| 1358 | write(readypipefd, &ret, sizeof(ret)); |
| 1359 | close(readypipefd); |
| 1360 | //printf("%s: ret %d\n", __func__, ret); |
| 1361 | if (ret) |
| 1362 | return 1; |
| 1363 | //printf("%s: joinThreadPool\n", __func__); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1364 | if (usePoll) { |
| 1365 | int fd; |
| 1366 | struct epoll_event ev; |
| 1367 | int epoll_fd; |
| 1368 | IPCThreadState::self()->setupPolling(&fd); |
| 1369 | if (fd < 0) { |
| 1370 | return 1; |
| 1371 | } |
| 1372 | IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER |
| 1373 | |
Nick Kralevich | fcf1b2b | 2018-12-15 11:59:30 -0800 | [diff] [blame] | 1374 | epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1375 | if (epoll_fd == -1) { |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
| 1379 | ev.events = EPOLLIN; |
| 1380 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
| 1384 | while (1) { |
| 1385 | /* |
| 1386 | * We simulate a single-threaded process using the binder poll |
| 1387 | * interface; besides handling binder commands, it can also |
| 1388 | * issue outgoing transactions, by storing a callback in |
Steven Moreland | 573adc1 | 2019-07-17 13:29:06 -0700 | [diff] [blame] | 1389 | * m_callback. |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1390 | * |
| 1391 | * processPendingCall() will then issue that transaction. |
| 1392 | */ |
| 1393 | struct epoll_event events[1]; |
| 1394 | int numEvents = epoll_wait(epoll_fd, events, 1, 1000); |
| 1395 | if (numEvents < 0) { |
| 1396 | if (errno == EINTR) { |
| 1397 | continue; |
| 1398 | } |
| 1399 | return 1; |
| 1400 | } |
| 1401 | if (numEvents > 0) { |
| 1402 | IPCThreadState::self()->handlePolledCommands(); |
| 1403 | IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER |
| 1404 | testServicePtr->processPendingCall(); |
| 1405 | } |
| 1406 | } |
| 1407 | } else { |
| 1408 | ProcessState::self()->startThreadPool(); |
| 1409 | IPCThreadState::self()->joinThreadPool(); |
| 1410 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1411 | //printf("%s: joinThreadPool returned\n", __func__); |
| 1412 | return 1; /* joinThreadPool should not return */ |
| 1413 | } |
| 1414 | |
| 1415 | int main(int argc, char **argv) { |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1416 | if (argc == 4 && !strcmp(argv[1], "--servername")) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1417 | binderservername = argv[2]; |
| 1418 | } else { |
| 1419 | binderservername = argv[0]; |
| 1420 | } |
| 1421 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame] | 1422 | if (argc == 6 && !strcmp(argv[1], binderserverarg)) { |
| 1423 | binderserversuffix = argv[5]; |
| 1424 | 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] | 1425 | } |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1426 | binderserversuffix = new char[16]; |
| 1427 | snprintf(binderserversuffix, 16, "%d", getpid()); |
| 1428 | binderLibTestServiceName += String16(binderserversuffix); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1429 | |
| 1430 | ::testing::InitGoogleTest(&argc, argv); |
| 1431 | binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv()); |
| 1432 | ProcessState::self()->startThreadPool(); |
| 1433 | return RUN_ALL_TESTS(); |
| 1434 | } |