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