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