blob: b92e504a9a342f6f34e632c7a87d7e119328b57d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Jason Parksdcd39582009-11-03 12:14:38 -080017#define LOG_TAG "IPCThreadState"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/Binder.h>
22#include <binder/BpBinder.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070023#include <binder/TextOutput.h>
24
Glenn Kastena26e1cf2012-03-16 07:15:23 -070025#include <cutils/sched_policy.h>
Steven Moreland7732a092019-01-02 17:54:16 -080026#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070028#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Hans Boehma997b232019-04-12 16:59:00 -070030#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070032#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <pthread.h>
34#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080035#include <signal.h>
36#include <stdio.h>
37#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080039#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Steven Moreland6ba5a252021-05-04 22:49:00 +000041#include "binder_module.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070042
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#if LOG_NDEBUG
44
45#define IF_LOG_TRANSACTIONS() if (false)
46#define IF_LOG_COMMANDS() if (false)
mattgilbride85897672022-10-22 17:42:44 +000047#define LOG_REMOTEREFS(...)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080049
mattgilbride85897672022-10-22 17:42:44 +000050#define LOG_THREADPOOL(...)
51#define LOG_ONEWAY(...)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#else
54
Steve Block9f760152011-10-12 17:27:03 +010055#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
56#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
57#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
58#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
59#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
60#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
62#endif
63
64// ---------------------------------------------------------------------------
65
66namespace android {
67
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070068// Static const and functions will be optimized out if not used,
69// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
Devin Moorea7499132023-12-15 18:48:39 +000070static const char* kReturnStrings[] = {
71 "BR_ERROR",
72 "BR_OK",
Devin Moore58029202023-12-15 18:58:48 +000073 "BR_TRANSACTION/BR_TRANSACTION_SEC_CTX",
Devin Moorea7499132023-12-15 18:48:39 +000074 "BR_REPLY",
75 "BR_ACQUIRE_RESULT",
76 "BR_DEAD_REPLY",
77 "BR_TRANSACTION_COMPLETE",
78 "BR_INCREFS",
79 "BR_ACQUIRE",
80 "BR_RELEASE",
81 "BR_DECREFS",
82 "BR_ATTEMPT_ACQUIRE",
83 "BR_NOOP",
84 "BR_SPAWN_LOOPER",
85 "BR_FINISHED",
86 "BR_DEAD_BINDER",
87 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
88 "BR_FAILED_REPLY",
89 "BR_FROZEN_REPLY",
90 "BR_ONEWAY_SPAM_SUSPECT",
91 "BR_TRANSACTION_PENDING_FROZEN",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092};
93
94static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "BC_TRANSACTION",
96 "BC_REPLY",
97 "BC_ACQUIRE_RESULT",
98 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 "BC_INCREFS",
100 "BC_ACQUIRE",
101 "BC_RELEASE",
102 "BC_DECREFS",
103 "BC_INCREFS_DONE",
104 "BC_ACQUIRE_DONE",
105 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 "BC_REGISTER_LOOPER",
107 "BC_ENTER_LOOPER",
108 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 "BC_REQUEST_DEATH_NOTIFICATION",
110 "BC_CLEAR_DEATH_NOTIFICATION",
111 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112};
113
Olivier Gaillard91a04802018-11-14 17:32:41 +0000114static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100115
songjinshi73a7dde2016-10-18 21:05:56 +0800116static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117{
songjinshi8e486c62019-04-04 11:22:52 +0800118 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
120 return kReturnStrings[idx];
121 else
122 return "unknown";
123}
124
Pawan Wagh7063b522022-09-28 18:52:26 +0000125static const void* printBinderTransactionData(std::ostream& out, const void* data) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 const binder_transaction_data* btd =
127 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700128 if (btd->target.handle < 1024) {
129 /* want to print descriptors in decimal; guess based on value */
Pawan Wagh7063b522022-09-28 18:52:26 +0000130 out << "\ttarget.desc=" << btd->target.handle;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700131 } else {
Pawan Wagh7063b522022-09-28 18:52:26 +0000132 out << "\ttarget.ptr=" << btd->target.ptr;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700133 }
Alice Ryhlc268ccb2022-11-17 10:18:38 +0000134 out << "\t (cookie " << btd->cookie << ")\n"
Pawan Wagh7063b522022-09-28 18:52:26 +0000135 << "\tcode=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)btd->flags << "\n"
Alice Ryhlc268ccb2022-11-17 10:18:38 +0000136 << "\tdata=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size << " bytes)\n"
137 << "\toffsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size << " bytes)\n";
138 return btd + 1;
139}
140
141static const void* printBinderTransactionDataSecCtx(std::ostream& out, const void* data) {
142 const binder_transaction_data_secctx* btd = (const binder_transaction_data_secctx*)data;
143
144 printBinderTransactionData(out, &btd->transaction_data);
145
146 char* secctx = (char*)btd->secctx;
147 out << "\tsecctx=" << secctx << "\n";
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return btd+1;
150}
151
Pawan Wagh7063b522022-09-28 18:52:26 +0000152static const void* printReturnCommand(std::ostream& out, const void* _cmd) {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700153 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100155 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700156 size_t cmdIndex = code & 0xff;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100157 if (code == BR_ERROR) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000158 out << "\tBR_ERROR: " << (void*)(uint64_t)(*cmd++) << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700160 } else if (cmdIndex >= N) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000161 out << "\tUnknown reply: " << code << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return cmd;
163 }
Pawan Wagh7063b522022-09-28 18:52:26 +0000164 out << "\t" << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 switch (code) {
Alice Ryhlc268ccb2022-11-17 10:18:38 +0000167 case BR_TRANSACTION_SEC_CTX: {
168 out << ": ";
169 cmd = (const int32_t*)printBinderTransactionDataSecCtx(out, cmd);
170 } break;
171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 case BR_TRANSACTION:
173 case BR_REPLY: {
Pawan Wagh7063b522022-09-28 18:52:26 +0000174 out << ": ";
175 cmd = (const int32_t*)printBinderTransactionData(out, cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 case BR_ACQUIRE_RESULT: {
179 const int32_t res = *cmd++;
180 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
181 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 case BR_INCREFS:
184 case BR_ACQUIRE:
185 case BR_RELEASE:
186 case BR_DECREFS: {
187 const int32_t b = *cmd++;
188 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900189 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 case BR_ATTEMPT_ACQUIRE: {
193 const int32_t p = *cmd++;
194 const int32_t b = *cmd++;
195 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900196 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 << "), pri=" << p;
198 } break;
199
200 case BR_DEAD_BINDER:
201 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
202 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900203 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700205
206 default:
207 // no details to show for: BR_OK, BR_DEAD_REPLY,
208 // BR_TRANSACTION_COMPLETE, BR_FINISHED
209 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800211
Pawan Wagh7063b522022-09-28 18:52:26 +0000212 out << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 return cmd;
214}
215
Pawan Wagh7063b522022-09-28 18:52:26 +0000216static const void* printCommand(std::ostream& out, const void* _cmd) {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700217 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100219 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700220 size_t cmdIndex = code & 0xff;
221
222 if (cmdIndex >= N) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000223 out << "Unknown command: " << code << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 return cmd;
225 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700226 out << kCommandStrings[cmdIndex];
227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 switch (code) {
229 case BC_TRANSACTION:
230 case BC_REPLY: {
Pawan Wagh7063b522022-09-28 18:52:26 +0000231 out << ": ";
232 cmd = (const int32_t*)printBinderTransactionData(out, cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 case BC_ACQUIRE_RESULT: {
236 const int32_t res = *cmd++;
237 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
238 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 case BC_FREE_BUFFER: {
241 const int32_t buf = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900242 out << ": buffer=" << (void*)(uint64_t)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 case BC_INCREFS:
246 case BC_ACQUIRE:
247 case BC_RELEASE:
248 case BC_DECREFS: {
249 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700250 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 case BC_INCREFS_DONE:
254 case BC_ACQUIRE_DONE: {
255 const int32_t b = *cmd++;
256 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900257 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 case BC_ATTEMPT_ACQUIRE: {
261 const int32_t p = *cmd++;
262 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700263 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 case BC_REQUEST_DEATH_NOTIFICATION:
267 case BC_CLEAR_DEATH_NOTIFICATION: {
268 const int32_t h = *cmd++;
269 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900270 out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 } break;
272
273 case BC_DEAD_BINDER_DONE: {
274 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900275 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700277
278 default:
279 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
280 // BC_EXIT_LOOPER
281 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800283
Pawan Wagh7063b522022-09-28 18:52:26 +0000284 out << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 return cmd;
286}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
Hans Boehma997b232019-04-12 16:59:00 -0700289static std::atomic<bool> gHaveTLS(false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290static pthread_key_t gTLS = 0;
Hans Boehma997b232019-04-12 16:59:00 -0700291static std::atomic<bool> gShutdown = false;
292static std::atomic<bool> gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293
294IPCThreadState* IPCThreadState::self()
295{
Hans Boehma997b232019-04-12 16:59:00 -0700296 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297restart:
298 const pthread_key_t k = gTLS;
299 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
300 if (st) return st;
301 return new IPCThreadState;
302 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800303
Hans Boehma997b232019-04-12 16:59:00 -0700304 // Racey, heuristic test for simultaneous shutdown.
305 if (gShutdown.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800306 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700307 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800308 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 pthread_mutex_lock(&gTLSMutex);
Hans Boehma997b232019-04-12 16:59:00 -0700311 if (!gHaveTLS.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800312 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
313 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800315 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
316 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700317 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
Hans Boehma997b232019-04-12 16:59:00 -0700319 gHaveTLS.store(true, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 }
321 pthread_mutex_unlock(&gTLSMutex);
322 goto restart;
323}
324
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800325IPCThreadState* IPCThreadState::selfOrNull()
326{
Hans Boehma997b232019-04-12 16:59:00 -0700327 if (gHaveTLS.load(std::memory_order_acquire)) {
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800328 const pthread_key_t k = gTLS;
329 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
330 return st;
331 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700332 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800333}
334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335void IPCThreadState::shutdown()
336{
Hans Boehma997b232019-04-12 16:59:00 -0700337 gShutdown.store(true, std::memory_order_relaxed);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800338
Hans Boehma997b232019-04-12 16:59:00 -0700339 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 // XXX Need to wait for all thread pool threads to exit!
341 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
342 if (st) {
343 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700344 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 }
zhongjieff405782016-03-09 15:05:04 +0800346 pthread_key_delete(gTLS);
Hans Boehma997b232019-04-12 16:59:00 -0700347 gHaveTLS.store(false, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 }
349}
350
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800351void IPCThreadState::disableBackgroundScheduling(bool disable)
352{
Hans Boehma997b232019-04-12 16:59:00 -0700353 gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800354}
355
Martijn Coenen2b631742017-05-05 11:16:59 -0700356bool IPCThreadState::backgroundSchedulingDisabled()
357{
Hans Boehma997b232019-04-12 16:59:00 -0700358 return gDisableBackgroundScheduling.load(std::memory_order_relaxed);
Martijn Coenen2b631742017-05-05 11:16:59 -0700359}
360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361status_t IPCThreadState::clearLastError()
362{
363 const status_t err = mLastError;
364 mLastError = NO_ERROR;
365 return err;
366}
367
Dan Stoza9c634fd2014-11-26 12:23:23 -0800368pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369{
Steven Moreland35626652021-05-15 01:32:04 +0000370 checkContextIsBinderForUse(__func__);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 return mCallingPid;
372}
373
Steven Morelandf0212002018-12-26 13:59:23 -0800374const char* IPCThreadState::getCallingSid() const
375{
Steven Moreland35626652021-05-15 01:32:04 +0000376 checkContextIsBinderForUse(__func__);
Steven Morelandf0212002018-12-26 13:59:23 -0800377 return mCallingSid;
378}
379
Dan Stoza9c634fd2014-11-26 12:23:23 -0800380uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381{
Steven Moreland35626652021-05-15 01:32:04 +0000382 checkContextIsBinderForUse(__func__);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 return mCallingUid;
384}
385
Steven Moreland35626652021-05-15 01:32:04 +0000386const IPCThreadState::SpGuard* IPCThreadState::pushGetCallingSpGuard(const SpGuard* guard) {
387 const SpGuard* orig = mServingStackPointerGuard;
388 mServingStackPointerGuard = guard;
389 return orig;
390}
391
392void IPCThreadState::restoreGetCallingSpGuard(const SpGuard* guard) {
393 mServingStackPointerGuard = guard;
394}
395
396void IPCThreadState::checkContextIsBinderForUse(const char* use) const {
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -0700397 if (mServingStackPointerGuard == nullptr) [[likely]] {
398 return;
399 }
Steven Moreland35626652021-05-15 01:32:04 +0000400
401 if (!mServingStackPointer || mServingStackPointerGuard->address < mServingStackPointer) {
402 LOG_ALWAYS_FATAL("In context %s, %s does not make sense (binder sp: %p, guard: %p).",
403 mServingStackPointerGuard->context, use, mServingStackPointer,
404 mServingStackPointerGuard->address);
405 }
406
407 // in the case mServingStackPointer is deeper in the stack than the guard,
408 // we must be serving a binder transaction (maybe nested). This is a binder
409 // context, so we don't abort
410}
411
mattgilbride85897672022-10-22 17:42:44 +0000412constexpr uint32_t encodeExplicitIdentity(bool hasExplicitIdentity, pid_t callingPid) {
413 uint32_t as_unsigned = static_cast<uint32_t>(callingPid);
414 if (hasExplicitIdentity) {
415 return as_unsigned | (1 << 30);
416 } else {
417 return as_unsigned & ~(1 << 30);
418 }
419}
420
421constexpr int64_t packCallingIdentity(bool hasExplicitIdentity, uid_t callingUid,
422 pid_t callingPid) {
423 // Calling PID is a 32-bit signed integer, but doesn't consume the entire 32 bit space.
424 // To future-proof this and because we have extra capacity, we decided to also support -1,
425 // since this constant is used to represent invalid UID in other places of the system.
426 // Thus, we pack hasExplicitIdentity into the 2nd bit from the left. This allows us to
427 // preserve the (left-most) bit for the sign while also encoding the value of
428 // hasExplicitIdentity.
429 // 32b | 1b | 1b | 30b
430 // token = [ calling uid | calling pid(sign) | has explicit identity | calling pid(rest) ]
431 uint64_t token = (static_cast<uint64_t>(callingUid) << 32) |
432 encodeExplicitIdentity(hasExplicitIdentity, callingPid);
433 return static_cast<int64_t>(token);
434}
435
436constexpr bool unpackHasExplicitIdentity(int64_t token) {
437 return static_cast<int32_t>(token) & (1 << 30);
438}
439
440constexpr uid_t unpackCallingUid(int64_t token) {
441 return static_cast<uid_t>(token >> 32);
442}
443
444constexpr pid_t unpackCallingPid(int64_t token) {
445 int32_t encodedPid = static_cast<int32_t>(token);
446 if (encodedPid & (1 << 31)) {
447 return encodedPid | (1 << 30);
448 } else {
449 return encodedPid & ~(1 << 30);
450 }
451}
452
453static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, 9999)) == true,
454 "pack true hasExplicit");
455
456static_assert(unpackCallingUid(packCallingIdentity(true, 1000, 9999)) == 1000, "pack true uid");
457
458static_assert(unpackCallingPid(packCallingIdentity(true, 1000, 9999)) == 9999, "pack true pid");
459
460static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, 9999)) == false,
461 "pack false hasExplicit");
462
463static_assert(unpackCallingUid(packCallingIdentity(false, 1000, 9999)) == 1000, "pack false uid");
464
465static_assert(unpackCallingPid(packCallingIdentity(false, 1000, 9999)) == 9999, "pack false pid");
466
467static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, -1)) == true,
468 "pack true (negative) hasExplicit");
469
470static_assert(unpackCallingUid(packCallingIdentity(true, 1000, -1)) == 1000,
471 "pack true (negative) uid");
472
473static_assert(unpackCallingPid(packCallingIdentity(true, 1000, -1)) == -1,
474 "pack true (negative) pid");
475
476static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, -1)) == false,
477 "pack false (negative) hasExplicit");
478
479static_assert(unpackCallingUid(packCallingIdentity(false, 1000, -1)) == 1000,
480 "pack false (negative) uid");
481
482static_assert(unpackCallingPid(packCallingIdentity(false, 1000, -1)) == -1,
483 "pack false (negative) pid");
484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485int64_t IPCThreadState::clearCallingIdentity()
486{
Steven Morelandf0212002018-12-26 13:59:23 -0800487 // ignore mCallingSid for legacy reasons
mattgilbride85897672022-10-22 17:42:44 +0000488 int64_t token = packCallingIdentity(mHasExplicitIdentity, mCallingUid, mCallingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 clearCaller();
mattgilbride85897672022-10-22 17:42:44 +0000490 mHasExplicitIdentity = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 return token;
492}
493
mattgilbride85897672022-10-22 17:42:44 +0000494bool IPCThreadState::hasExplicitIdentity() {
495 return mHasExplicitIdentity;
496}
497
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700498void IPCThreadState::setStrictModePolicy(int32_t policy)
499{
500 mStrictModePolicy = policy;
501}
502
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700503int32_t IPCThreadState::getStrictModePolicy() const
504{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700505 return mStrictModePolicy;
506}
507
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000508int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100509{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000510 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
511 mPropagateWorkSource = true;
512 return token;
513}
514
515int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
516{
517 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
518 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100519 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000520 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100521}
522
Olivier Gaillard91a04802018-11-14 17:32:41 +0000523void IPCThreadState::clearPropagateWorkSource()
524{
525 mPropagateWorkSource = false;
526}
527
528bool IPCThreadState::shouldPropagateWorkSource() const
529{
530 return mPropagateWorkSource;
531}
532
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000533uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100534{
535 return mWorkSource;
536}
537
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000538int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100539{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000540 return setCallingWorkSourceUid(kUnsetWorkSource);
541}
542
543void IPCThreadState::restoreCallingWorkSource(int64_t token)
544{
545 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000546 setCallingWorkSourceUidWithoutPropagation(uid);
547 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100548}
549
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700550void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
551{
552 mLastTransactionBinderFlags = flags;
553}
554
555int32_t IPCThreadState::getLastTransactionBinderFlags() const
556{
557 return mLastTransactionBinderFlags;
558}
559
Steven Moreland9514b202020-09-21 18:03:27 +0000560void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) {
561 mCallRestriction = restriction;
562}
563
564ProcessState::CallRestriction IPCThreadState::getCallRestriction() const {
565 return mCallRestriction;
566}
567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568void IPCThreadState::restoreCallingIdentity(int64_t token)
569{
mattgilbride85897672022-10-22 17:42:44 +0000570 mCallingUid = unpackCallingUid(token);
Steven Morelandf0212002018-12-26 13:59:23 -0800571 mCallingSid = nullptr; // not enough data to restore
mattgilbride85897672022-10-22 17:42:44 +0000572 mCallingPid = unpackCallingPid(token);
573 mHasExplicitIdentity = unpackHasExplicitIdentity(token);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574}
575
576void IPCThreadState::clearCaller()
577{
Marco Nelissend43b1942009-07-17 07:59:17 -0700578 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800579 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700580 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581}
582
583void IPCThreadState::flushCommands()
584{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200585 if (mProcess->mDriverFD < 0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 return;
587 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700588 // The flush could have caused post-write refcount decrements to have
589 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
590 // being queued in mOut. So flush again, if we need to.
591 if (mOut.dataSize() > 0) {
592 talkWithDriver(false);
593 }
594 if (mOut.dataSize() > 0) {
595 ALOGW("mOut.dataSize() > 0 after flushCommands()");
596 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597}
598
Martijn Coenen0442a862017-11-17 10:46:32 +0100599bool IPCThreadState::flushIfNeeded()
600{
Frankie Changf4c81372021-05-18 13:08:05 +0800601 if (mIsLooper || mServingStackPointer != nullptr || mIsFlushing) {
Martijn Coenen0442a862017-11-17 10:46:32 +0100602 return false;
603 }
Frankie Changf4c81372021-05-18 13:08:05 +0800604 mIsFlushing = true;
Martijn Coenen0442a862017-11-17 10:46:32 +0100605 // In case this thread is not a looper and is not currently serving a binder transaction,
606 // there's no guarantee that this thread will call back into the kernel driver any time
607 // soon. Therefore, flush pending commands such as BC_FREE_BUFFER, to prevent them from getting
608 // stuck in this thread's out buffer.
609 flushCommands();
Frankie Changf4c81372021-05-18 13:08:05 +0800610 mIsFlushing = false;
Martijn Coenen0442a862017-11-17 10:46:32 +0100611 return true;
612}
613
Wale Ogunwale376b8222015-04-13 16:16:10 -0700614void IPCThreadState::blockUntilThreadAvailable()
615{
616 pthread_mutex_lock(&mProcess->mThreadCountLock);
Steven Morelandc648a762021-01-16 02:39:45 +0000617 mProcess->mWaitingForThreads++;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700618 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700619 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
620 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
621 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700622 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
623 }
Steven Morelandc648a762021-01-16 02:39:45 +0000624 mProcess->mWaitingForThreads--;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700625 pthread_mutex_unlock(&mProcess->mThreadCountLock);
626}
627
Todd Poynor8d96cab2013-06-25 19:12:18 -0700628status_t IPCThreadState::getAndExecuteCommand()
629{
630 status_t result;
631 int32_t cmd;
632
633 result = talkWithDriver();
634 if (result >= NO_ERROR) {
635 size_t IN = mIn.dataAvail();
636 if (IN < sizeof(int32_t)) return result;
637 cmd = mIn.readInt32();
638 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000639 std::ostringstream logStream;
640 logStream << "Processing top-level Command: " << getReturnString(cmd) << "\n";
641 std::string message = logStream.str();
642 ALOGI("%s", message.c_str());
Todd Poynor8d96cab2013-06-25 19:12:18 -0700643 }
644
Wale Ogunwale376b8222015-04-13 16:16:10 -0700645 pthread_mutex_lock(&mProcess->mThreadCountLock);
646 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700647 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
648 mProcess->mStarvationStartTimeMs == 0) {
649 mProcess->mStarvationStartTimeMs = uptimeMillis();
650 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700651 pthread_mutex_unlock(&mProcess->mThreadCountLock);
652
Todd Poynor8d96cab2013-06-25 19:12:18 -0700653 result = executeCommand(cmd);
654
Wale Ogunwale376b8222015-04-13 16:16:10 -0700655 pthread_mutex_lock(&mProcess->mThreadCountLock);
656 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700657 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
658 mProcess->mStarvationStartTimeMs != 0) {
659 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
660 if (starvationTimeMs > 100) {
661 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
662 mProcess->mMaxThreads, starvationTimeMs);
663 }
664 mProcess->mStarvationStartTimeMs = 0;
665 }
Steven Morelandc648a762021-01-16 02:39:45 +0000666
667 // Cond broadcast can be expensive, so don't send it every time a binder
668 // call is processed. b/168806193
669 if (mProcess->mWaitingForThreads > 0) {
670 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
671 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700672 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700673 }
674
675 return result;
676}
677
678// When we've cleared the incoming command queue, process any pending derefs
679void IPCThreadState::processPendingDerefs()
680{
681 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200682 /*
683 * The decWeak()/decStrong() calls may cause a destructor to run,
684 * which in turn could have initiated an outgoing transaction,
685 * which in turn could cause us to add to the pending refs
686 * vectors; so instead of simply iterating, loop until they're empty.
687 *
688 * We do this in an outer loop, because calling decStrong()
689 * may result in something being added to mPendingWeakDerefs,
690 * which could be delayed until the next incoming command
691 * from the driver if we don't process it now.
692 */
693 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
694 while (mPendingWeakDerefs.size() > 0) {
695 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
696 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700697 refs->decWeak(mProcess.get());
698 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700699
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200700 if (mPendingStrongDerefs.size() > 0) {
701 // We don't use while() here because we don't want to re-order
702 // strong and weak decs at all; if this decStrong() causes both a
703 // decWeak() and a decStrong() to be queued, we want to process
704 // the decWeak() first.
705 BBinder* obj = mPendingStrongDerefs[0];
706 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700707 obj->decStrong(mProcess.get());
708 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700709 }
710 }
711}
712
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700713void IPCThreadState::processPostWriteDerefs()
714{
715 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
716 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
717 refs->decWeak(mProcess.get());
718 }
719 mPostWriteWeakDerefs.clear();
720
721 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
722 RefBase* obj = mPostWriteStrongDerefs[i];
723 obj->decStrong(mProcess.get());
724 }
725 mPostWriteStrongDerefs.clear();
726}
727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728void IPCThreadState::joinThreadPool(bool isMain)
729{
730 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
Elie Kheirallah47431c12022-04-21 23:46:17 +0000731 pthread_mutex_lock(&mProcess->mThreadCountLock);
732 mProcess->mCurrentThreads++;
733 pthread_mutex_unlock(&mProcess->mThreadCountLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800735
Martijn Coenen0442a862017-11-17 10:46:32 +0100736 mIsLooper = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 status_t result;
738 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700739 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700741 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800742
Todd Poynor8d96cab2013-06-25 19:12:18 -0700743 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700744 LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700745 mProcess->mDriverFD, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800747
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 // Let this thread exit the thread pool if it is no longer
749 // needed and it is not the main process thread.
750 if(result == TIMED_OUT && !isMain) {
751 break;
752 }
753 } while (result != -ECONNREFUSED && result != -EBADF);
754
Wei Wangc7341432016-10-19 10:23:59 -0700755 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
756 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 mOut.writeInt32(BC_EXIT_LOOPER);
Martijn Coenen0442a862017-11-17 10:46:32 +0100759 mIsLooper = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 talkWithDriver(false);
Elie Kheirallah47431c12022-04-21 23:46:17 +0000761 pthread_mutex_lock(&mProcess->mThreadCountLock);
762 LOG_ALWAYS_FATAL_IF(mProcess->mCurrentThreads == 0,
763 "Threadpool thread count = 0. Thread cannot exist and exit in empty "
764 "threadpool\n"
765 "Misconfiguration. Increase threadpool max threads configuration\n");
766 mProcess->mCurrentThreads--;
767 pthread_mutex_unlock(&mProcess->mThreadCountLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768}
769
Steven Morelandd8c85672020-07-24 21:30:41 +0000770status_t IPCThreadState::setupPolling(int* fd)
Todd Poynor8d96cab2013-06-25 19:12:18 -0700771{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200772 if (mProcess->mDriverFD < 0) {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700773 return -EBADF;
774 }
775
776 mOut.writeInt32(BC_ENTER_LOOPER);
Steven Morelandf210b502021-01-15 23:40:32 +0000777 flushCommands();
Todd Poynor8d96cab2013-06-25 19:12:18 -0700778 *fd = mProcess->mDriverFD;
Elie Kheirallah47431c12022-04-21 23:46:17 +0000779 pthread_mutex_lock(&mProcess->mThreadCountLock);
780 mProcess->mCurrentThreads++;
781 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700782 return 0;
783}
784
785status_t IPCThreadState::handlePolledCommands()
786{
787 status_t result;
788
789 do {
790 result = getAndExecuteCommand();
791 } while (mIn.dataPosition() < mIn.dataSize());
792
793 processPendingDerefs();
794 flushCommands();
795 return result;
796}
797
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800798void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799{
Steve Blocka19954a2012-01-04 20:05:49 +0000800 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 flushCommands();
802 int fd = mProcess->mDriverFD;
803 mProcess->mDriverFD = -1;
804 close(fd);
805 //kill(getpid(), SIGKILL);
806}
807
808status_t IPCThreadState::transact(int32_t handle,
809 uint32_t code, const Parcel& data,
810 Parcel* reply, uint32_t flags)
811{
Steven Moreland5553ac42020-11-11 02:14:45 +0000812 LOG_ALWAYS_FATAL_IF(data.isForRpc(), "Parcel constructed for RPC, but being used with binder.");
813
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800814 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815
816 flags |= TF_ACCEPT_FDS;
817
818 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000819 std::ostringstream logStream;
820 logStream << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " << handle
821 << " / code " << TypeCode(code) << ": \t" << data << "\n";
822 std::string message = logStream.str();
823 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800825
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800826 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
827 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700828 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 if (err != NO_ERROR) {
831 if (reply) reply->setError(err);
832 return (mLastError = err);
833 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 if ((flags & TF_ONE_WAY) == 0) {
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -0700836 if (mCallRestriction != ProcessState::CallRestriction::NONE) [[unlikely]] {
Steven Moreland7732a092019-01-02 17:54:16 -0800837 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700838 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800839 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
840 ANDROID_LOG_ERROR);
841 } else /* FATAL_IF_NOT_ONEWAY */ {
Steven Morelandfcc77f12020-09-01 01:16:11 +0000842 LOG_ALWAYS_FATAL("Process may not make non-oneway calls (code: %u).", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800843 }
844 }
845
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -0700846#if 0
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700847 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000848 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700849 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000850 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700851 }
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -0700852#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 if (reply) {
854 err = waitForResponse(reply);
855 } else {
856 Parcel fakeReply;
857 err = waitForResponse(&fakeReply);
858 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700859 #if 0
860 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000861 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700862 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000863 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700864 }
865 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800866
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000868 std::ostringstream logStream;
869 logStream << "BR_REPLY thr " << (void*)pthread_self() << " / hand " << handle << ": ";
870 if (reply)
871 logStream << "\t" << *reply << "\n";
872 else
873 logStream << "(none requested)"
874 << "\n";
875 std::string message = logStream.str();
876 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 }
878 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700879 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 return err;
883}
884
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700885void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886{
887 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
888 mOut.writeInt32(BC_ACQUIRE);
889 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100890 if (!flushIfNeeded()) {
891 // Create a temp reference until the driver has handled this command.
892 proxy->incStrong(mProcess.get());
893 mPostWriteStrongDerefs.push(proxy);
894 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895}
896
897void IPCThreadState::decStrongHandle(int32_t handle)
898{
899 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
900 mOut.writeInt32(BC_RELEASE);
901 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100902 flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903}
904
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700905void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906{
907 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
908 mOut.writeInt32(BC_INCREFS);
909 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100910 if (!flushIfNeeded()) {
911 // Create a temp reference until the driver has handled this command.
912 proxy->getWeakRefs()->incWeak(mProcess.get());
913 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
914 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915}
916
917void IPCThreadState::decWeakHandle(int32_t handle)
918{
919 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
920 mOut.writeInt32(BC_DECREFS);
921 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100922 flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923}
924
925status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
926{
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800927#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700928 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
930 mOut.writeInt32(0); // xxx was thread priority
931 mOut.writeInt32(handle);
932 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800933
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800937 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
939#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800940
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 return result;
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800942#else
943 (void)handle;
944 ALOGE("%s(%d): Not supported\n", __func__, handle);
945 return INVALID_OPERATION;
946#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947}
948
949void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
950{
951#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800952 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700954 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955}
956
957status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
958{
959 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
960 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000961 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 return NO_ERROR;
963}
964
965status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
966{
967 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
968 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000969 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 return NO_ERROR;
971}
972
973IPCThreadState::IPCThreadState()
Steven Moreland35626652021-05-15 01:32:04 +0000974 : mProcess(ProcessState::self()),
975 mServingStackPointer(nullptr),
976 mServingStackPointerGuard(nullptr),
977 mWorkSource(kUnsetWorkSource),
978 mPropagateWorkSource(false),
979 mIsLooper(false),
Frankie Changf4c81372021-05-18 13:08:05 +0800980 mIsFlushing(false),
Steven Moreland35626652021-05-15 01:32:04 +0000981 mStrictModePolicy(0),
982 mLastTransactionBinderFlags(0),
983 mCallRestriction(mProcess->mCallRestriction) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800985 clearCaller();
mattgilbride85897672022-10-22 17:42:44 +0000986 mHasExplicitIdentity = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 mIn.setDataCapacity(256);
988 mOut.setDataCapacity(256);
989}
990
991IPCThreadState::~IPCThreadState()
992{
993}
994
Martijn Coenenea0090a2017-11-02 18:54:40 +0000995status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
996{
997 status_t err;
998 status_t statusBuffer;
999 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
1000 if (err < NO_ERROR) return err;
1001
Yi Kongfdd8da92018-06-07 17:52:27 -07001002 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001003}
1004
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
1006{
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001007 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 int32_t err;
1009
1010 while (1) {
1011 if ((err=talkWithDriver()) < NO_ERROR) break;
1012 err = mIn.errorCheck();
1013 if (err < NO_ERROR) break;
1014 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001015
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001016 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001017
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001019 std::ostringstream logStream;
1020 logStream << "Processing waitForResponse Command: " << getReturnString(cmd) << "\n";
1021 std::string message = logStream.str();
1022 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 }
1024
1025 switch (cmd) {
Hang Lub185ac02021-03-24 13:17:22 +08001026 case BR_ONEWAY_SPAM_SUSPECT:
1027 ALOGE("Process seems to be sending too many oneway calls.");
1028 CallStack::logStack("oneway spamming", CallStack::getCurrent().get(),
1029 ANDROID_LOG_ERROR);
1030 [[fallthrough]];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 case BR_TRANSACTION_COMPLETE:
1032 if (!reply && !acquireResult) goto finish;
1033 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001034
Li Li0e3443d2022-12-07 21:51:19 -08001035 case BR_TRANSACTION_PENDING_FROZEN:
1036 ALOGW("Sending oneway calls to frozen process.");
1037 goto finish;
1038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 case BR_DEAD_REPLY:
1040 err = DEAD_OBJECT;
1041 goto finish;
1042
1043 case BR_FAILED_REPLY:
1044 err = FAILED_TRANSACTION;
1045 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001046
Marco Ballesio7ee17572020-09-08 10:30:03 -07001047 case BR_FROZEN_REPLY:
1048 err = FAILED_TRANSACTION;
1049 goto finish;
1050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 case BR_ACQUIRE_RESULT:
1052 {
Steve Block67263472012-01-09 18:35:44 +00001053 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 const int32_t result = mIn.readInt32();
1055 if (!acquireResult) continue;
1056 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
1057 }
1058 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 case BR_REPLY:
1061 {
1062 binder_transaction_data tr;
1063 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001064 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 if (err != NO_ERROR) goto finish;
1066
1067 if (reply) {
1068 if ((tr.flags & TF_STATUS_CODE) == 0) {
1069 reply->ipcSetDataReference(
1070 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1071 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001072 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1073 tr.offsets_size/sizeof(binder_size_t),
Steven Moreland161fe122020-11-12 23:16:47 +00001074 freeBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 } else {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001076 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001077 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1078 tr.data_size,
1079 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1080 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 }
1082 } else {
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001083 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), tr.data_size,
1084 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1085 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 continue;
1087 }
1088 }
1089 goto finish;
1090
1091 default:
1092 err = executeCommand(cmd);
1093 if (err != NO_ERROR) goto finish;
1094 break;
1095 }
1096 }
1097
1098finish:
1099 if (err != NO_ERROR) {
1100 if (acquireResult) *acquireResult = err;
1101 if (reply) reply->setError(err);
1102 mLastError = err;
Carlos Llamasb235b122021-12-20 06:38:44 -08001103 logExtendedError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 return err;
1107}
1108
1109status_t IPCThreadState::talkWithDriver(bool doReceive)
1110{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001111 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001112 return -EBADF;
1113 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 // Is the read buffer empty?
1118 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 // We don't want to write anything if we are still reading
1121 // from data left in the input buffer and the caller
1122 // has requested to read the next data.
1123 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 bwr.write_size = outAvail;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001126 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127
1128 // This is what we'll read.
1129 if (doReceive && needRead) {
1130 bwr.read_size = mIn.dataCapacity();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001131 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 } else {
1133 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -08001134 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001138 std::ostringstream logStream;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 if (outAvail != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001140 logStream << "Sending commands to driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 const void* cmds = (const void*)bwr.write_buffer;
Pawan Wagh7063b522022-09-28 18:52:26 +00001142 const void* end = ((const uint8_t*)cmds) + bwr.write_size;
1143 logStream << "\t" << HexDump(cmds, bwr.write_size) << "\n";
1144 while (cmds < end) cmds = printCommand(logStream, cmds);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001146 logStream << "Size of receive buffer: " << bwr.read_size << ", needRead: " << needRead
1147 << ", doReceive: " << doReceive << "\n";
1148
1149 std::string message = logStream.str();
1150 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 // Return immediately if there is nothing to do.
1154 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 bwr.write_consumed = 0;
1157 bwr.read_consumed = 0;
1158 status_t err;
1159 do {
1160 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001161 std::ostringstream logStream;
1162 logStream << "About to read/write, write size = " << mOut.dataSize() << "\n";
1163 std::string message = logStream.str();
1164 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 }
Elliott Hughes6071da72015-08-12 15:27:47 -07001166#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
1168 err = NO_ERROR;
1169 else
1170 err = -errno;
1171#else
1172 err = INVALID_OPERATION;
1173#endif
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001174 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001175 err = -EBADF;
1176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001178 std::ostringstream logStream;
1179 logStream << "Finished read/write, write size = " << mOut.dataSize() << "\n";
1180 std::string message = logStream.str();
1181 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 }
1183 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001186 std::ostringstream logStream;
1187 logStream << "Our err: " << (void*)(intptr_t)err
1188 << ", write consumed: " << bwr.write_consumed << " (of " << mOut.dataSize()
1189 << "), read consumed: " << bwr.read_consumed << "\n";
1190 std::string message = logStream.str();
1191 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 }
1193
1194 if (err >= NO_ERROR) {
1195 if (bwr.write_consumed > 0) {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001196 if (bwr.write_consumed < mOut.dataSize())
Steven Morelandb077deb2020-04-16 16:22:52 -07001197 LOG_ALWAYS_FATAL("Driver did not consume write buffer. "
1198 "err: %s consumed: %zu of %zu",
1199 statusToString(err).c_str(),
1200 (size_t)bwr.write_consumed,
1201 mOut.dataSize());
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001202 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001204 processPostWriteDerefs();
1205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
1207 if (bwr.read_consumed > 0) {
1208 mIn.setDataSize(bwr.read_consumed);
1209 mIn.setDataPosition(0);
1210 }
1211 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001212 std::ostringstream logStream;
1213 logStream << "Remaining data size: " << mOut.dataSize() << "\n";
1214 logStream << "Received commands from driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 const void* cmds = mIn.data();
1216 const void* end = mIn.data() + mIn.dataSize();
Pawan Wagh7063b522022-09-28 18:52:26 +00001217 logStream << "\t" << HexDump(cmds, mIn.dataSize()) << "\n";
1218 while (cmds < end) cmds = printReturnCommand(logStream, cmds);
1219 std::string message = logStream.str();
1220 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 }
1222 return NO_ERROR;
1223 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001224
Theodore Duboisbf144632023-01-09 15:36:49 -08001225 ALOGE_IF(mProcess->mDriverFD >= 0,
1226 "Driver returned error (%s). This is a bug in either libbinder or the driver. This "
1227 "thread's connection to %s will no longer work.",
1228 statusToString(err).c_str(), mProcess->mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 return err;
1230}
1231
1232status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1233 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1234{
1235 binder_transaction_data tr;
1236
Arve HjønnevÄg07fd0f12014-02-18 21:10:29 -08001237 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 tr.target.handle = handle;
1239 tr.code = code;
1240 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001241 tr.cookie = 0;
1242 tr.sender_pid = 0;
1243 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 const status_t err = data.errorCheck();
1246 if (err == NO_ERROR) {
1247 tr.data_size = data.ipcDataSize();
1248 tr.data.ptr.buffer = data.ipcData();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001249 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 tr.data.ptr.offsets = data.ipcObjects();
1251 } else if (statusBuffer) {
1252 tr.flags |= TF_STATUS_CODE;
1253 *statusBuffer = err;
1254 tr.data_size = sizeof(status_t);
Arve HjønnevÄg87b30d02014-02-18 21:04:31 -08001255 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 tr.offsets_size = 0;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001257 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 } else {
1259 return (mLastError = err);
1260 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 mOut.writeInt32(cmd);
1263 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 return NO_ERROR;
1266}
1267
1268sp<BBinder> the_context_object;
1269
Jiyong Park384328e2020-11-13 17:16:48 +09001270void IPCThreadState::setTheContextObject(const sp<BBinder>& obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271{
1272 the_context_object = obj;
1273}
1274
1275status_t IPCThreadState::executeCommand(int32_t cmd)
1276{
1277 BBinder* obj;
1278 RefBase::weakref_type* refs;
1279 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001280
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001281 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 case BR_ERROR:
1283 result = mIn.readInt32();
1284 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 case BR_OK:
1287 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001290 refs = (RefBase::weakref_type*)mIn.readPointer();
1291 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001292 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1294 refs, obj, refs->refBase());
1295 obj->incStrong(mProcess.get());
1296 IF_LOG_REMOTEREFS() {
1297 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1298 obj->printRefs();
1299 }
1300 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001301 mOut.writePointer((uintptr_t)refs);
1302 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001306 refs = (RefBase::weakref_type*)mIn.readPointer();
1307 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001308 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1310 refs, obj, refs->refBase());
1311 IF_LOG_REMOTEREFS() {
1312 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1313 obj->printRefs();
1314 }
1315 mPendingStrongDerefs.push(obj);
1316 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001319 refs = (RefBase::weakref_type*)mIn.readPointer();
1320 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 refs->incWeak(mProcess.get());
1322 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001323 mOut.writePointer((uintptr_t)refs);
1324 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001328 refs = (RefBase::weakref_type*)mIn.readPointer();
Jiyong Park5970d0a2022-03-08 16:56:13 +09001329 // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
1330 obj = (BBinder*)mIn.readPointer(); // consume
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 // NOTE: This assertion is not valid, because the object may no
1332 // longer exist (thus the (BBinder*)cast above resulting in a different
1333 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001334 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1336 // refs, obj, refs->refBase());
1337 mPendingWeakDerefs.push(refs);
1338 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001341 refs = (RefBase::weakref_type*)mIn.readPointer();
1342 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001343
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 {
1345 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001346 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1348 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001349
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 mOut.writeInt32(BC_ACQUIRE_RESULT);
1351 mOut.writeInt32((int32_t)success);
1352 }
1353 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001354
Steven Morelandf0212002018-12-26 13:59:23 -08001355 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356 case BR_TRANSACTION:
1357 {
Steven Morelandf0212002018-12-26 13:59:23 -08001358 binder_transaction_data_secctx tr_secctx;
1359 binder_transaction_data& tr = tr_secctx.transaction_data;
1360
1361 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1362 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1363 } else {
1364 result = mIn.read(&tr, sizeof(tr));
1365 tr_secctx.secctx = 0;
1366 }
1367
Steve Block67263472012-01-09 18:35:44 +00001368 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 "Not enough command data for brTRANSACTION");
1370 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001371
Martijn Coenenea0090a2017-11-02 18:54:40 +00001372 Parcel buffer;
1373 buffer.ipcSetDataReference(
1374 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1375 tr.data_size,
1376 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +00001377 tr.offsets_size/sizeof(binder_size_t), freeBuffer);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001378
Steven Moreland39d887d2020-01-31 14:56:45 -08001379 const void* origServingStackPointer = mServingStackPointer;
Steven Moreland35626652021-05-15 01:32:04 +00001380 mServingStackPointer = __builtin_frame_address(0);
Steven Moreland39d887d2020-01-31 14:56:45 -08001381
Martijn Coenenea0090a2017-11-02 18:54:40 +00001382 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001383 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001384 const uid_t origUid = mCallingUid;
mattgilbride85897672022-10-22 17:42:44 +00001385 const bool origHasExplicitIdentity = mHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001386 const int32_t origStrictModePolicy = mStrictModePolicy;
1387 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001388 const int32_t origWorkSource = mWorkSource;
1389 const bool origPropagateWorkSet = mPropagateWorkSource;
1390 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1391 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1392 // here to never propagate it.
1393 clearCallingWorkSource();
1394 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001395
1396 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001397 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001398 mCallingUid = tr.sender_euid;
mattgilbride85897672022-10-22 17:42:44 +00001399 mHasExplicitIdentity = false;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001400 mLastTransactionBinderFlags = tr.flags;
1401
Steven Morelandf0212002018-12-26 13:59:23 -08001402 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1403 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001406 status_t error;
1407 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001408 std::ostringstream logStream;
1409 logStream << "BR_TRANSACTION thr " << (void*)pthread_self() << " / obj "
1410 << tr.target.ptr << " / code " << TypeCode(tr.code) << ": \t" << buffer
1411 << "\n"
1412 << "Data addr = " << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1413 << ", offsets addr="
1414 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << "\n";
1415 std::string message = logStream.str();
1416 ALOGI("%s", message.c_str());
Martijn Coenenea0090a2017-11-02 18:54:40 +00001417 }
1418 if (tr.target.ptr) {
1419 // We only have a weak reference on the target object, so we must first try to
1420 // safely acquire a strong reference before doing anything else with it.
1421 if (reinterpret_cast<RefBase::weakref_type*>(
1422 tr.target.ptr)->attemptIncStrong(this)) {
1423 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1424 &reply, tr.flags);
1425 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001426 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001427 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001428 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001429
Martijn Coenenea0090a2017-11-02 18:54:40 +00001430 } else {
1431 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001433
Steven Morelandf0212002018-12-26 13:59:23 -08001434 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1435 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001436
Martijn Coenenea0090a2017-11-02 18:54:40 +00001437 if ((tr.flags & TF_ONE_WAY) == 0) {
1438 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1439 if (error < NO_ERROR) reply.setError(error);
Steven Morelandf183fdd2020-10-27 00:12:12 +00001440
Steven Morelandce15b9f2022-09-08 17:42:45 +00001441 // b/238777741: clear buffer before we send the reply.
1442 // Otherwise, there is a race where the client may
1443 // receive the reply and send another transaction
1444 // here and the space used by this transaction won't
1445 // be freed for the client.
1446 buffer.setDataSize(0);
1447
Steven Morelandf183fdd2020-10-27 00:12:12 +00001448 constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF;
1449 sendReply(reply, (tr.flags & kForwardReplyFlags));
Martijn Coenenea0090a2017-11-02 18:54:40 +00001450 } else {
Steven Moreland80844f72020-12-12 02:06:08 +00001451 if (error != OK) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001452 std::ostringstream logStream;
1453 logStream << "oneway function results for code " << tr.code << " on binder at "
1454 << reinterpret_cast<void*>(tr.target.ptr)
1455 << " will be dropped but finished with status "
1456 << statusToString(error);
Steven Moreland80844f72020-12-12 02:06:08 +00001457
1458 // ideally we could log this even when error == OK, but it
1459 // causes too much logspam because some manually-written
1460 // interfaces have clients that call methods which always
1461 // write results, sometimes as oneway methods.
1462 if (reply.dataSize() != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001463 logStream << " and reply parcel size " << reply.dataSize();
Steven Moreland80844f72020-12-12 02:06:08 +00001464 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001465 std::string message = logStream.str();
1466 ALOGI("%s", message.c_str());
Steven Morelandce66b8a2020-02-10 14:43:14 -08001467 }
Martijn Coenenea0090a2017-11-02 18:54:40 +00001468 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1469 }
1470
Steven Moreland39d887d2020-01-31 14:56:45 -08001471 mServingStackPointer = origServingStackPointer;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001472 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001473 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001474 mCallingUid = origUid;
mattgilbride85897672022-10-22 17:42:44 +00001475 mHasExplicitIdentity = origHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001476 mStrictModePolicy = origStrictModePolicy;
1477 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001478 mWorkSource = origWorkSource;
1479 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001482 std::ostringstream logStream;
1483 logStream << "BC_REPLY thr " << (void*)pthread_self() << " / obj " << tr.target.ptr
1484 << ": \t" << reply << "\n";
1485 std::string message = logStream.str();
1486 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
1490 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 case BR_DEAD_BINDER:
1493 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001494 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 proxy->sendObituary();
1496 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001497 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1501 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001502 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 proxy->getWeakRefs()->decWeak(proxy);
1504 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 case BR_FINISHED:
1507 result = TIMED_OUT;
1508 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 case BR_NOOP:
1511 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 case BR_SPAWN_LOOPER:
1514 mProcess->spawnPooledThread(false);
1515 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 default:
liangweikanga43ee152016-10-25 16:37:54 +08001518 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 result = UNKNOWN_ERROR;
1520 break;
1521 }
1522
1523 if (result != NO_ERROR) {
1524 mLastError = result;
1525 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 return result;
1528}
1529
Steven Moreland39d887d2020-01-31 14:56:45 -08001530const void* IPCThreadState::getServingStackPointer() const {
1531 return mServingStackPointer;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001532}
1533
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534void IPCThreadState::threadDestructor(void *st)
1535{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001536 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1537 if (self) {
1538 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001539#if defined(__ANDROID__)
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001540 if (self->mProcess->mDriverFD >= 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001541 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1542 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001544 delete self;
1545 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546}
1547
Li Li6f059292021-09-10 09:59:30 -07001548status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, uint32_t *sync_received,
1549 uint32_t *async_received)
1550{
1551 int ret = 0;
Jiyong Park5970d0a2022-03-08 16:56:13 +09001552 binder_frozen_status_info info = {};
Li Li6f059292021-09-10 09:59:30 -07001553 info.pid = pid;
1554
1555#if defined(__ANDROID__)
1556 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0)
1557 ret = -errno;
1558#endif
1559 *sync_received = info.sync_recv;
1560 *async_received = info.async_recv;
1561
1562 return ret;
1563}
Li Li6f059292021-09-10 09:59:30 -07001564
Marco Ballesio7ee17572020-09-08 10:30:03 -07001565status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) {
1566 struct binder_freeze_info info;
1567 int ret = 0;
1568
1569 info.pid = pid;
1570 info.enable = enable;
1571 info.timeout_ms = timeout_ms;
1572
1573
1574#if defined(__ANDROID__)
1575 if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0)
1576 ret = -errno;
1577#endif
1578
1579 //
1580 // ret==-EAGAIN indicates that transactions have not drained.
1581 // Call again to poll for completion.
1582 //
1583 return ret;
1584}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585
Carlos Llamasb235b122021-12-20 06:38:44 -08001586void IPCThreadState::logExtendedError() {
1587 struct binder_extended_error ee = {.command = BR_OK};
1588
1589 if (!ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::EXTENDED_ERROR))
1590 return;
1591
1592#if defined(__ANDROID__)
1593 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_EXTENDED_ERROR, &ee) < 0) {
1594 ALOGE("Failed to get extended error: %s", strerror(errno));
1595 return;
1596 }
1597#endif
1598
1599 ALOGE_IF(ee.command != BR_OK, "Binder transaction failure: %d/%d/%d",
1600 ee.id, ee.command, ee.param);
1601}
1602
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001603void IPCThreadState::freeBuffer(const uint8_t* data, size_t /*dataSize*/,
1604 const binder_size_t* /*objects*/, size_t /*objectsSize*/) {
Steve Blocka19954a2012-01-04 20:05:49 +00001605 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001607 std::ostringstream logStream;
1608 logStream << "Writing BC_FREE_BUFFER for " << data << "\n";
1609 std::string message = logStream.str();
1610 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 }
Steve Block67263472012-01-09 18:35:44 +00001612 ALOG_ASSERT(data != NULL, "Called with NULL data");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 IPCThreadState* state = self();
1614 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001615 state->mOut.writePointer((uintptr_t)data);
Martijn Coenen0442a862017-11-17 10:46:32 +01001616 state->flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Steven Moreland61ff8492019-09-26 16:05:45 -07001619} // namespace android