blob: c0f3e3060ef6f7e3dbaa8a58d8db7a13345a39c5 [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
Steven Moreland7732a092019-01-02 17:54:16 -080025#include <android-base/macros.h>
Glenn Kastena26e1cf2012-03-16 07:15:23 -070026#include <cutils/sched_policy.h>
Steven Moreland7732a092019-01-02 17:54:16 -080027#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070029#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Hans Boehma997b232019-04-12 16:59:00 -070031#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070033#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <pthread.h>
35#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080036#include <signal.h>
37#include <stdio.h>
38#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080040#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Steven Moreland6ba5a252021-05-04 22:49:00 +000042#include "binder_module.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#if LOG_NDEBUG
45
46#define IF_LOG_TRANSACTIONS() if (false)
47#define IF_LOG_COMMANDS() if (false)
mattgilbride85897672022-10-22 17:42:44 +000048#define LOG_REMOTEREFS(...)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080050
mattgilbride85897672022-10-22 17:42:44 +000051#define LOG_THREADPOOL(...)
52#define LOG_ONEWAY(...)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#else
55
Steve Block9f760152011-10-12 17:27:03 +010056#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
57#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
58#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
59#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
60#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
61#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63#endif
64
65// ---------------------------------------------------------------------------
66
67namespace android {
68
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070069// Static const and functions will be optimized out if not used,
70// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070072 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 "BR_TRANSACTION",
75 "BR_REPLY",
76 "BR_ACQUIRE_RESULT",
77 "BR_DEAD_REPLY",
78 "BR_TRANSACTION_COMPLETE",
79 "BR_INCREFS",
80 "BR_ACQUIRE",
81 "BR_RELEASE",
82 "BR_DECREFS",
83 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 "BR_NOOP",
85 "BR_SPAWN_LOOPER",
86 "BR_FINISHED",
87 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070088 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Morelandf0212002018-12-26 13:59:23 -080089 "BR_FAILED_REPLY",
Hang Lub185ac02021-03-24 13:17:22 +080090 "BR_FROZEN_REPLY",
91 "BR_ONEWAY_SPAM_SUSPECT",
Steven Morelandf0212002018-12-26 13:59:23 -080092 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093};
94
95static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 "BC_TRANSACTION",
97 "BC_REPLY",
98 "BC_ACQUIRE_RESULT",
99 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 "BC_INCREFS",
101 "BC_ACQUIRE",
102 "BC_RELEASE",
103 "BC_DECREFS",
104 "BC_INCREFS_DONE",
105 "BC_ACQUIRE_DONE",
106 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 "BC_REGISTER_LOOPER",
108 "BC_ENTER_LOOPER",
109 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 "BC_REQUEST_DEATH_NOTIFICATION",
111 "BC_CLEAR_DEATH_NOTIFICATION",
112 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113};
114
Olivier Gaillard91a04802018-11-14 17:32:41 +0000115static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100116
songjinshi73a7dde2016-10-18 21:05:56 +0800117static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118{
songjinshi8e486c62019-04-04 11:22:52 +0800119 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
121 return kReturnStrings[idx];
122 else
123 return "unknown";
124}
125
Pawan Wagh7063b522022-09-28 18:52:26 +0000126static const void* printBinderTransactionData(std::ostream& out, const void* data) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 const binder_transaction_data* btd =
128 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700129 if (btd->target.handle < 1024) {
130 /* want to print descriptors in decimal; guess based on value */
Pawan Wagh7063b522022-09-28 18:52:26 +0000131 out << "\ttarget.desc=" << btd->target.handle;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700132 } else {
Pawan Wagh7063b522022-09-28 18:52:26 +0000133 out << "\ttarget.ptr=" << btd->target.ptr;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700134 }
Pawan Wagh7063b522022-09-28 18:52:26 +0000135 out << "\t (cookie " << btd->cookie << ")"
136 << "\n"
137 << "\tcode=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)btd->flags << "\n"
138 << "\tdata=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size << " bytes)"
139 << "\n"
140 << "\toffsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return btd+1;
142}
143
Pawan Wagh7063b522022-09-28 18:52:26 +0000144static const void* printReturnCommand(std::ostream& out, const void* _cmd) {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700145 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100147 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700148 size_t cmdIndex = code & 0xff;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100149 if (code == BR_ERROR) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000150 out << "\tBR_ERROR: " << (void*)(uint64_t)(*cmd++) << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700152 } else if (cmdIndex >= N) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000153 out << "\tUnknown reply: " << code << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 return cmd;
155 }
Pawan Wagh7063b522022-09-28 18:52:26 +0000156 out << "\t" << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 switch (code) {
159 case BR_TRANSACTION:
160 case BR_REPLY: {
Pawan Wagh7063b522022-09-28 18:52:26 +0000161 out << ": ";
162 cmd = (const int32_t*)printBinderTransactionData(out, cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 case BR_ACQUIRE_RESULT: {
166 const int32_t res = *cmd++;
167 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
168 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 case BR_INCREFS:
171 case BR_ACQUIRE:
172 case BR_RELEASE:
173 case BR_DECREFS: {
174 const int32_t b = *cmd++;
175 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900176 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 case BR_ATTEMPT_ACQUIRE: {
180 const int32_t p = *cmd++;
181 const int32_t b = *cmd++;
182 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900183 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 << "), pri=" << p;
185 } break;
186
187 case BR_DEAD_BINDER:
188 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
189 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900190 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700192
193 default:
194 // no details to show for: BR_OK, BR_DEAD_REPLY,
195 // BR_TRANSACTION_COMPLETE, BR_FINISHED
196 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800198
Pawan Wagh7063b522022-09-28 18:52:26 +0000199 out << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 return cmd;
201}
202
Pawan Wagh7063b522022-09-28 18:52:26 +0000203static const void* printCommand(std::ostream& out, const void* _cmd) {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700204 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100206 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700207 size_t cmdIndex = code & 0xff;
208
209 if (cmdIndex >= N) {
Pawan Wagh7063b522022-09-28 18:52:26 +0000210 out << "Unknown command: " << code << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 return cmd;
212 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700213 out << kCommandStrings[cmdIndex];
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 switch (code) {
216 case BC_TRANSACTION:
217 case BC_REPLY: {
Pawan Wagh7063b522022-09-28 18:52:26 +0000218 out << ": ";
219 cmd = (const int32_t*)printBinderTransactionData(out, cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 case BC_ACQUIRE_RESULT: {
223 const int32_t res = *cmd++;
224 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
225 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 case BC_FREE_BUFFER: {
228 const int32_t buf = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900229 out << ": buffer=" << (void*)(uint64_t)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 case BC_INCREFS:
233 case BC_ACQUIRE:
234 case BC_RELEASE:
235 case BC_DECREFS: {
236 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700237 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 case BC_INCREFS_DONE:
241 case BC_ACQUIRE_DONE: {
242 const int32_t b = *cmd++;
243 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900244 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 case BC_ATTEMPT_ACQUIRE: {
248 const int32_t p = *cmd++;
249 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700250 out << ": desc=" << d << ", pri=" << p;
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_REQUEST_DEATH_NOTIFICATION:
254 case BC_CLEAR_DEATH_NOTIFICATION: {
255 const int32_t h = *cmd++;
256 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900257 out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 } break;
259
260 case BC_DEAD_BINDER_DONE: {
261 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900262 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700264
265 default:
266 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
267 // BC_EXIT_LOOPER
268 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800270
Pawan Wagh7063b522022-09-28 18:52:26 +0000271 out << "\n";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 return cmd;
273}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
275static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
Hans Boehma997b232019-04-12 16:59:00 -0700276static std::atomic<bool> gHaveTLS(false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277static pthread_key_t gTLS = 0;
Hans Boehma997b232019-04-12 16:59:00 -0700278static std::atomic<bool> gShutdown = false;
279static std::atomic<bool> gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
281IPCThreadState* IPCThreadState::self()
282{
Hans Boehma997b232019-04-12 16:59:00 -0700283 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284restart:
285 const pthread_key_t k = gTLS;
286 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
287 if (st) return st;
288 return new IPCThreadState;
289 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800290
Hans Boehma997b232019-04-12 16:59:00 -0700291 // Racey, heuristic test for simultaneous shutdown.
292 if (gShutdown.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800293 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700294 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800295 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 pthread_mutex_lock(&gTLSMutex);
Hans Boehma997b232019-04-12 16:59:00 -0700298 if (!gHaveTLS.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800299 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
300 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800302 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
303 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700304 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
Hans Boehma997b232019-04-12 16:59:00 -0700306 gHaveTLS.store(true, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 }
308 pthread_mutex_unlock(&gTLSMutex);
309 goto restart;
310}
311
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800312IPCThreadState* IPCThreadState::selfOrNull()
313{
Hans Boehma997b232019-04-12 16:59:00 -0700314 if (gHaveTLS.load(std::memory_order_acquire)) {
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800315 const pthread_key_t k = gTLS;
316 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
317 return st;
318 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700319 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800320}
321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322void IPCThreadState::shutdown()
323{
Hans Boehma997b232019-04-12 16:59:00 -0700324 gShutdown.store(true, std::memory_order_relaxed);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800325
Hans Boehma997b232019-04-12 16:59:00 -0700326 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 // XXX Need to wait for all thread pool threads to exit!
328 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
329 if (st) {
330 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700331 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 }
zhongjieff405782016-03-09 15:05:04 +0800333 pthread_key_delete(gTLS);
Hans Boehma997b232019-04-12 16:59:00 -0700334 gHaveTLS.store(false, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 }
336}
337
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800338void IPCThreadState::disableBackgroundScheduling(bool disable)
339{
Hans Boehma997b232019-04-12 16:59:00 -0700340 gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800341}
342
Martijn Coenen2b631742017-05-05 11:16:59 -0700343bool IPCThreadState::backgroundSchedulingDisabled()
344{
Hans Boehma997b232019-04-12 16:59:00 -0700345 return gDisableBackgroundScheduling.load(std::memory_order_relaxed);
Martijn Coenen2b631742017-05-05 11:16:59 -0700346}
347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348status_t IPCThreadState::clearLastError()
349{
350 const status_t err = mLastError;
351 mLastError = NO_ERROR;
352 return err;
353}
354
Dan Stoza9c634fd2014-11-26 12:23:23 -0800355pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356{
Steven Moreland35626652021-05-15 01:32:04 +0000357 checkContextIsBinderForUse(__func__);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 return mCallingPid;
359}
360
Steven Morelandf0212002018-12-26 13:59:23 -0800361const char* IPCThreadState::getCallingSid() const
362{
Steven Moreland35626652021-05-15 01:32:04 +0000363 checkContextIsBinderForUse(__func__);
Steven Morelandf0212002018-12-26 13:59:23 -0800364 return mCallingSid;
365}
366
Dan Stoza9c634fd2014-11-26 12:23:23 -0800367uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368{
Steven Moreland35626652021-05-15 01:32:04 +0000369 checkContextIsBinderForUse(__func__);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 return mCallingUid;
371}
372
Steven Moreland35626652021-05-15 01:32:04 +0000373const IPCThreadState::SpGuard* IPCThreadState::pushGetCallingSpGuard(const SpGuard* guard) {
374 const SpGuard* orig = mServingStackPointerGuard;
375 mServingStackPointerGuard = guard;
376 return orig;
377}
378
379void IPCThreadState::restoreGetCallingSpGuard(const SpGuard* guard) {
380 mServingStackPointerGuard = guard;
381}
382
383void IPCThreadState::checkContextIsBinderForUse(const char* use) const {
384 if (LIKELY(mServingStackPointerGuard == nullptr)) return;
385
386 if (!mServingStackPointer || mServingStackPointerGuard->address < mServingStackPointer) {
387 LOG_ALWAYS_FATAL("In context %s, %s does not make sense (binder sp: %p, guard: %p).",
388 mServingStackPointerGuard->context, use, mServingStackPointer,
389 mServingStackPointerGuard->address);
390 }
391
392 // in the case mServingStackPointer is deeper in the stack than the guard,
393 // we must be serving a binder transaction (maybe nested). This is a binder
394 // context, so we don't abort
395}
396
mattgilbride85897672022-10-22 17:42:44 +0000397constexpr uint32_t encodeExplicitIdentity(bool hasExplicitIdentity, pid_t callingPid) {
398 uint32_t as_unsigned = static_cast<uint32_t>(callingPid);
399 if (hasExplicitIdentity) {
400 return as_unsigned | (1 << 30);
401 } else {
402 return as_unsigned & ~(1 << 30);
403 }
404}
405
406constexpr int64_t packCallingIdentity(bool hasExplicitIdentity, uid_t callingUid,
407 pid_t callingPid) {
408 // Calling PID is a 32-bit signed integer, but doesn't consume the entire 32 bit space.
409 // To future-proof this and because we have extra capacity, we decided to also support -1,
410 // since this constant is used to represent invalid UID in other places of the system.
411 // Thus, we pack hasExplicitIdentity into the 2nd bit from the left. This allows us to
412 // preserve the (left-most) bit for the sign while also encoding the value of
413 // hasExplicitIdentity.
414 // 32b | 1b | 1b | 30b
415 // token = [ calling uid | calling pid(sign) | has explicit identity | calling pid(rest) ]
416 uint64_t token = (static_cast<uint64_t>(callingUid) << 32) |
417 encodeExplicitIdentity(hasExplicitIdentity, callingPid);
418 return static_cast<int64_t>(token);
419}
420
421constexpr bool unpackHasExplicitIdentity(int64_t token) {
422 return static_cast<int32_t>(token) & (1 << 30);
423}
424
425constexpr uid_t unpackCallingUid(int64_t token) {
426 return static_cast<uid_t>(token >> 32);
427}
428
429constexpr pid_t unpackCallingPid(int64_t token) {
430 int32_t encodedPid = static_cast<int32_t>(token);
431 if (encodedPid & (1 << 31)) {
432 return encodedPid | (1 << 30);
433 } else {
434 return encodedPid & ~(1 << 30);
435 }
436}
437
438static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, 9999)) == true,
439 "pack true hasExplicit");
440
441static_assert(unpackCallingUid(packCallingIdentity(true, 1000, 9999)) == 1000, "pack true uid");
442
443static_assert(unpackCallingPid(packCallingIdentity(true, 1000, 9999)) == 9999, "pack true pid");
444
445static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, 9999)) == false,
446 "pack false hasExplicit");
447
448static_assert(unpackCallingUid(packCallingIdentity(false, 1000, 9999)) == 1000, "pack false uid");
449
450static_assert(unpackCallingPid(packCallingIdentity(false, 1000, 9999)) == 9999, "pack false pid");
451
452static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, -1)) == true,
453 "pack true (negative) hasExplicit");
454
455static_assert(unpackCallingUid(packCallingIdentity(true, 1000, -1)) == 1000,
456 "pack true (negative) uid");
457
458static_assert(unpackCallingPid(packCallingIdentity(true, 1000, -1)) == -1,
459 "pack true (negative) pid");
460
461static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, -1)) == false,
462 "pack false (negative) hasExplicit");
463
464static_assert(unpackCallingUid(packCallingIdentity(false, 1000, -1)) == 1000,
465 "pack false (negative) uid");
466
467static_assert(unpackCallingPid(packCallingIdentity(false, 1000, -1)) == -1,
468 "pack false (negative) pid");
469
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470int64_t IPCThreadState::clearCallingIdentity()
471{
Steven Morelandf0212002018-12-26 13:59:23 -0800472 // ignore mCallingSid for legacy reasons
mattgilbride85897672022-10-22 17:42:44 +0000473 int64_t token = packCallingIdentity(mHasExplicitIdentity, mCallingUid, mCallingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 clearCaller();
mattgilbride85897672022-10-22 17:42:44 +0000475 mHasExplicitIdentity = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 return token;
477}
478
mattgilbride85897672022-10-22 17:42:44 +0000479bool IPCThreadState::hasExplicitIdentity() {
480 return mHasExplicitIdentity;
481}
482
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700483void IPCThreadState::setStrictModePolicy(int32_t policy)
484{
485 mStrictModePolicy = policy;
486}
487
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700488int32_t IPCThreadState::getStrictModePolicy() const
489{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700490 return mStrictModePolicy;
491}
492
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000493int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100494{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000495 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
496 mPropagateWorkSource = true;
497 return token;
498}
499
500int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
501{
502 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
503 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100504 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000505 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100506}
507
Olivier Gaillard91a04802018-11-14 17:32:41 +0000508void IPCThreadState::clearPropagateWorkSource()
509{
510 mPropagateWorkSource = false;
511}
512
513bool IPCThreadState::shouldPropagateWorkSource() const
514{
515 return mPropagateWorkSource;
516}
517
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000518uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100519{
520 return mWorkSource;
521}
522
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000523int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100524{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000525 return setCallingWorkSourceUid(kUnsetWorkSource);
526}
527
528void IPCThreadState::restoreCallingWorkSource(int64_t token)
529{
530 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000531 setCallingWorkSourceUidWithoutPropagation(uid);
532 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100533}
534
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700535void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
536{
537 mLastTransactionBinderFlags = flags;
538}
539
540int32_t IPCThreadState::getLastTransactionBinderFlags() const
541{
542 return mLastTransactionBinderFlags;
543}
544
Steven Moreland9514b202020-09-21 18:03:27 +0000545void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) {
546 mCallRestriction = restriction;
547}
548
549ProcessState::CallRestriction IPCThreadState::getCallRestriction() const {
550 return mCallRestriction;
551}
552
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553void IPCThreadState::restoreCallingIdentity(int64_t token)
554{
mattgilbride85897672022-10-22 17:42:44 +0000555 mCallingUid = unpackCallingUid(token);
Steven Morelandf0212002018-12-26 13:59:23 -0800556 mCallingSid = nullptr; // not enough data to restore
mattgilbride85897672022-10-22 17:42:44 +0000557 mCallingPid = unpackCallingPid(token);
558 mHasExplicitIdentity = unpackHasExplicitIdentity(token);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559}
560
561void IPCThreadState::clearCaller()
562{
Marco Nelissend43b1942009-07-17 07:59:17 -0700563 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800564 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700565 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566}
567
568void IPCThreadState::flushCommands()
569{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200570 if (mProcess->mDriverFD < 0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 return;
572 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700573 // The flush could have caused post-write refcount decrements to have
574 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
575 // being queued in mOut. So flush again, if we need to.
576 if (mOut.dataSize() > 0) {
577 talkWithDriver(false);
578 }
579 if (mOut.dataSize() > 0) {
580 ALOGW("mOut.dataSize() > 0 after flushCommands()");
581 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582}
583
Martijn Coenen0442a862017-11-17 10:46:32 +0100584bool IPCThreadState::flushIfNeeded()
585{
Frankie Changf4c81372021-05-18 13:08:05 +0800586 if (mIsLooper || mServingStackPointer != nullptr || mIsFlushing) {
Martijn Coenen0442a862017-11-17 10:46:32 +0100587 return false;
588 }
Frankie Changf4c81372021-05-18 13:08:05 +0800589 mIsFlushing = true;
Martijn Coenen0442a862017-11-17 10:46:32 +0100590 // In case this thread is not a looper and is not currently serving a binder transaction,
591 // there's no guarantee that this thread will call back into the kernel driver any time
592 // soon. Therefore, flush pending commands such as BC_FREE_BUFFER, to prevent them from getting
593 // stuck in this thread's out buffer.
594 flushCommands();
Frankie Changf4c81372021-05-18 13:08:05 +0800595 mIsFlushing = false;
Martijn Coenen0442a862017-11-17 10:46:32 +0100596 return true;
597}
598
Wale Ogunwale376b8222015-04-13 16:16:10 -0700599void IPCThreadState::blockUntilThreadAvailable()
600{
601 pthread_mutex_lock(&mProcess->mThreadCountLock);
Steven Morelandc648a762021-01-16 02:39:45 +0000602 mProcess->mWaitingForThreads++;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700603 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700604 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
605 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
606 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700607 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
608 }
Steven Morelandc648a762021-01-16 02:39:45 +0000609 mProcess->mWaitingForThreads--;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700610 pthread_mutex_unlock(&mProcess->mThreadCountLock);
611}
612
Todd Poynor8d96cab2013-06-25 19:12:18 -0700613status_t IPCThreadState::getAndExecuteCommand()
614{
615 status_t result;
616 int32_t cmd;
617
618 result = talkWithDriver();
619 if (result >= NO_ERROR) {
620 size_t IN = mIn.dataAvail();
621 if (IN < sizeof(int32_t)) return result;
622 cmd = mIn.readInt32();
623 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000624 std::ostringstream logStream;
625 logStream << "Processing top-level Command: " << getReturnString(cmd) << "\n";
626 std::string message = logStream.str();
627 ALOGI("%s", message.c_str());
Todd Poynor8d96cab2013-06-25 19:12:18 -0700628 }
629
Wale Ogunwale376b8222015-04-13 16:16:10 -0700630 pthread_mutex_lock(&mProcess->mThreadCountLock);
631 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700632 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
633 mProcess->mStarvationStartTimeMs == 0) {
634 mProcess->mStarvationStartTimeMs = uptimeMillis();
635 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700636 pthread_mutex_unlock(&mProcess->mThreadCountLock);
637
Todd Poynor8d96cab2013-06-25 19:12:18 -0700638 result = executeCommand(cmd);
639
Wale Ogunwale376b8222015-04-13 16:16:10 -0700640 pthread_mutex_lock(&mProcess->mThreadCountLock);
641 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700642 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
643 mProcess->mStarvationStartTimeMs != 0) {
644 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
645 if (starvationTimeMs > 100) {
646 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
647 mProcess->mMaxThreads, starvationTimeMs);
648 }
649 mProcess->mStarvationStartTimeMs = 0;
650 }
Steven Morelandc648a762021-01-16 02:39:45 +0000651
652 // Cond broadcast can be expensive, so don't send it every time a binder
653 // call is processed. b/168806193
654 if (mProcess->mWaitingForThreads > 0) {
655 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
656 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700657 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700658 }
659
660 return result;
661}
662
663// When we've cleared the incoming command queue, process any pending derefs
664void IPCThreadState::processPendingDerefs()
665{
666 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200667 /*
668 * The decWeak()/decStrong() calls may cause a destructor to run,
669 * which in turn could have initiated an outgoing transaction,
670 * which in turn could cause us to add to the pending refs
671 * vectors; so instead of simply iterating, loop until they're empty.
672 *
673 * We do this in an outer loop, because calling decStrong()
674 * may result in something being added to mPendingWeakDerefs,
675 * which could be delayed until the next incoming command
676 * from the driver if we don't process it now.
677 */
678 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
679 while (mPendingWeakDerefs.size() > 0) {
680 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
681 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700682 refs->decWeak(mProcess.get());
683 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700684
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200685 if (mPendingStrongDerefs.size() > 0) {
686 // We don't use while() here because we don't want to re-order
687 // strong and weak decs at all; if this decStrong() causes both a
688 // decWeak() and a decStrong() to be queued, we want to process
689 // the decWeak() first.
690 BBinder* obj = mPendingStrongDerefs[0];
691 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700692 obj->decStrong(mProcess.get());
693 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700694 }
695 }
696}
697
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700698void IPCThreadState::processPostWriteDerefs()
699{
700 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
701 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
702 refs->decWeak(mProcess.get());
703 }
704 mPostWriteWeakDerefs.clear();
705
706 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
707 RefBase* obj = mPostWriteStrongDerefs[i];
708 obj->decStrong(mProcess.get());
709 }
710 mPostWriteStrongDerefs.clear();
711}
712
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713void IPCThreadState::joinThreadPool(bool isMain)
714{
715 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
Elie Kheirallah47431c12022-04-21 23:46:17 +0000716 pthread_mutex_lock(&mProcess->mThreadCountLock);
717 mProcess->mCurrentThreads++;
718 pthread_mutex_unlock(&mProcess->mThreadCountLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800720
Martijn Coenen0442a862017-11-17 10:46:32 +0100721 mIsLooper = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 status_t result;
723 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700724 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700726 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800727
Todd Poynor8d96cab2013-06-25 19:12:18 -0700728 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700729 LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700730 mProcess->mDriverFD, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 // Let this thread exit the thread pool if it is no longer
734 // needed and it is not the main process thread.
735 if(result == TIMED_OUT && !isMain) {
736 break;
737 }
738 } while (result != -ECONNREFUSED && result != -EBADF);
739
Wei Wangc7341432016-10-19 10:23:59 -0700740 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
741 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800742
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 mOut.writeInt32(BC_EXIT_LOOPER);
Martijn Coenen0442a862017-11-17 10:46:32 +0100744 mIsLooper = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 talkWithDriver(false);
Elie Kheirallah47431c12022-04-21 23:46:17 +0000746 pthread_mutex_lock(&mProcess->mThreadCountLock);
747 LOG_ALWAYS_FATAL_IF(mProcess->mCurrentThreads == 0,
748 "Threadpool thread count = 0. Thread cannot exist and exit in empty "
749 "threadpool\n"
750 "Misconfiguration. Increase threadpool max threads configuration\n");
751 mProcess->mCurrentThreads--;
752 pthread_mutex_unlock(&mProcess->mThreadCountLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
Steven Morelandd8c85672020-07-24 21:30:41 +0000755status_t IPCThreadState::setupPolling(int* fd)
Todd Poynor8d96cab2013-06-25 19:12:18 -0700756{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200757 if (mProcess->mDriverFD < 0) {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700758 return -EBADF;
759 }
760
761 mOut.writeInt32(BC_ENTER_LOOPER);
Steven Morelandf210b502021-01-15 23:40:32 +0000762 flushCommands();
Todd Poynor8d96cab2013-06-25 19:12:18 -0700763 *fd = mProcess->mDriverFD;
Elie Kheirallah47431c12022-04-21 23:46:17 +0000764 pthread_mutex_lock(&mProcess->mThreadCountLock);
765 mProcess->mCurrentThreads++;
766 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700767 return 0;
768}
769
770status_t IPCThreadState::handlePolledCommands()
771{
772 status_t result;
773
774 do {
775 result = getAndExecuteCommand();
776 } while (mIn.dataPosition() < mIn.dataSize());
777
778 processPendingDerefs();
779 flushCommands();
780 return result;
781}
782
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800783void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784{
Steve Blocka19954a2012-01-04 20:05:49 +0000785 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 flushCommands();
787 int fd = mProcess->mDriverFD;
788 mProcess->mDriverFD = -1;
789 close(fd);
790 //kill(getpid(), SIGKILL);
791}
792
793status_t IPCThreadState::transact(int32_t handle,
794 uint32_t code, const Parcel& data,
795 Parcel* reply, uint32_t flags)
796{
Steven Moreland5553ac42020-11-11 02:14:45 +0000797 LOG_ALWAYS_FATAL_IF(data.isForRpc(), "Parcel constructed for RPC, but being used with binder.");
798
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800799 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800
801 flags |= TF_ACCEPT_FDS;
802
803 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000804 std::ostringstream logStream;
805 logStream << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " << handle
806 << " / code " << TypeCode(code) << ": \t" << data << "\n";
807 std::string message = logStream.str();
808 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800810
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800811 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
812 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700813 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 if (err != NO_ERROR) {
816 if (reply) reply->setError(err);
817 return (mLastError = err);
818 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800819
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800821 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
822 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700823 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800824 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
825 ANDROID_LOG_ERROR);
826 } else /* FATAL_IF_NOT_ONEWAY */ {
Steven Morelandfcc77f12020-09-01 01:16:11 +0000827 LOG_ALWAYS_FATAL("Process may not make non-oneway calls (code: %u).", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800828 }
829 }
830
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700831 #if 0
832 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000833 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700834 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000835 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700836 }
837 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 if (reply) {
839 err = waitForResponse(reply);
840 } else {
841 Parcel fakeReply;
842 err = waitForResponse(&fakeReply);
843 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700844 #if 0
845 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000846 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700847 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000848 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700849 }
850 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +0000853 std::ostringstream logStream;
854 logStream << "BR_REPLY thr " << (void*)pthread_self() << " / hand " << handle << ": ";
855 if (reply)
856 logStream << "\t" << *reply << "\n";
857 else
858 logStream << "(none requested)"
859 << "\n";
860 std::string message = logStream.str();
861 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 }
863 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700864 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800866
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 return err;
868}
869
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700870void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871{
872 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
873 mOut.writeInt32(BC_ACQUIRE);
874 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100875 if (!flushIfNeeded()) {
876 // Create a temp reference until the driver has handled this command.
877 proxy->incStrong(mProcess.get());
878 mPostWriteStrongDerefs.push(proxy);
879 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880}
881
882void IPCThreadState::decStrongHandle(int32_t handle)
883{
884 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
885 mOut.writeInt32(BC_RELEASE);
886 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100887 flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888}
889
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700890void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891{
892 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
893 mOut.writeInt32(BC_INCREFS);
894 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100895 if (!flushIfNeeded()) {
896 // Create a temp reference until the driver has handled this command.
897 proxy->getWeakRefs()->incWeak(mProcess.get());
898 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
899 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900}
901
902void IPCThreadState::decWeakHandle(int32_t handle)
903{
904 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
905 mOut.writeInt32(BC_DECREFS);
906 mOut.writeInt32(handle);
Martijn Coenen0442a862017-11-17 10:46:32 +0100907 flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908}
909
910status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
911{
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800912#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700913 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
915 mOut.writeInt32(0); // xxx was thread priority
916 mOut.writeInt32(handle);
917 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800918
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800922 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
924#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 return result;
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800927#else
928 (void)handle;
929 ALOGE("%s(%d): Not supported\n", __func__, handle);
930 return INVALID_OPERATION;
931#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932}
933
934void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
935{
936#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800937 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700939 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940}
941
942status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
943{
944 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
945 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000946 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 return NO_ERROR;
948}
949
950status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
951{
952 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
953 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000954 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 return NO_ERROR;
956}
957
958IPCThreadState::IPCThreadState()
Steven Moreland35626652021-05-15 01:32:04 +0000959 : mProcess(ProcessState::self()),
960 mServingStackPointer(nullptr),
961 mServingStackPointerGuard(nullptr),
962 mWorkSource(kUnsetWorkSource),
963 mPropagateWorkSource(false),
964 mIsLooper(false),
Frankie Changf4c81372021-05-18 13:08:05 +0800965 mIsFlushing(false),
Steven Moreland35626652021-05-15 01:32:04 +0000966 mStrictModePolicy(0),
967 mLastTransactionBinderFlags(0),
968 mCallRestriction(mProcess->mCallRestriction) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800970 clearCaller();
mattgilbride85897672022-10-22 17:42:44 +0000971 mHasExplicitIdentity = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 mIn.setDataCapacity(256);
973 mOut.setDataCapacity(256);
974}
975
976IPCThreadState::~IPCThreadState()
977{
978}
979
Martijn Coenenea0090a2017-11-02 18:54:40 +0000980status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
981{
982 status_t err;
983 status_t statusBuffer;
984 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
985 if (err < NO_ERROR) return err;
986
Yi Kongfdd8da92018-06-07 17:52:27 -0700987 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000988}
989
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
991{
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100992 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 int32_t err;
994
995 while (1) {
996 if ((err=talkWithDriver()) < NO_ERROR) break;
997 err = mIn.errorCheck();
998 if (err < NO_ERROR) break;
999 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001000
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001001 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001002
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001004 std::ostringstream logStream;
1005 logStream << "Processing waitForResponse Command: " << getReturnString(cmd) << "\n";
1006 std::string message = logStream.str();
1007 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 }
1009
1010 switch (cmd) {
Hang Lub185ac02021-03-24 13:17:22 +08001011 case BR_ONEWAY_SPAM_SUSPECT:
1012 ALOGE("Process seems to be sending too many oneway calls.");
1013 CallStack::logStack("oneway spamming", CallStack::getCurrent().get(),
1014 ANDROID_LOG_ERROR);
1015 [[fallthrough]];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 case BR_TRANSACTION_COMPLETE:
1017 if (!reply && !acquireResult) goto finish;
1018 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001019
Li Li0e3443d2022-12-07 21:51:19 -08001020 case BR_TRANSACTION_PENDING_FROZEN:
1021 ALOGW("Sending oneway calls to frozen process.");
1022 goto finish;
1023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 case BR_DEAD_REPLY:
1025 err = DEAD_OBJECT;
1026 goto finish;
1027
1028 case BR_FAILED_REPLY:
1029 err = FAILED_TRANSACTION;
1030 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001031
Marco Ballesio7ee17572020-09-08 10:30:03 -07001032 case BR_FROZEN_REPLY:
1033 err = FAILED_TRANSACTION;
1034 goto finish;
1035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 case BR_ACQUIRE_RESULT:
1037 {
Steve Block67263472012-01-09 18:35:44 +00001038 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 const int32_t result = mIn.readInt32();
1040 if (!acquireResult) continue;
1041 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
1042 }
1043 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 case BR_REPLY:
1046 {
1047 binder_transaction_data tr;
1048 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001049 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 if (err != NO_ERROR) goto finish;
1051
1052 if (reply) {
1053 if ((tr.flags & TF_STATUS_CODE) == 0) {
1054 reply->ipcSetDataReference(
1055 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1056 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001057 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1058 tr.offsets_size/sizeof(binder_size_t),
Steven Moreland161fe122020-11-12 23:16:47 +00001059 freeBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 } else {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001061 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001062 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1063 tr.data_size,
1064 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1065 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 }
1067 } else {
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001068 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), tr.data_size,
1069 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1070 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 continue;
1072 }
1073 }
1074 goto finish;
1075
1076 default:
1077 err = executeCommand(cmd);
1078 if (err != NO_ERROR) goto finish;
1079 break;
1080 }
1081 }
1082
1083finish:
1084 if (err != NO_ERROR) {
1085 if (acquireResult) *acquireResult = err;
1086 if (reply) reply->setError(err);
1087 mLastError = err;
Carlos Llamasb235b122021-12-20 06:38:44 -08001088 logExtendedError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001090
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 return err;
1092}
1093
1094status_t IPCThreadState::talkWithDriver(bool doReceive)
1095{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001096 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001097 return -EBADF;
1098 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 // Is the read buffer empty?
1103 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 // We don't want to write anything if we are still reading
1106 // from data left in the input buffer and the caller
1107 // has requested to read the next data.
1108 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 bwr.write_size = outAvail;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001111 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112
1113 // This is what we'll read.
1114 if (doReceive && needRead) {
1115 bwr.read_size = mIn.dataCapacity();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001116 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 } else {
1118 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -08001119 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001123 std::ostringstream logStream;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 if (outAvail != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001125 logStream << "Sending commands to driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 const void* cmds = (const void*)bwr.write_buffer;
Pawan Wagh7063b522022-09-28 18:52:26 +00001127 const void* end = ((const uint8_t*)cmds) + bwr.write_size;
1128 logStream << "\t" << HexDump(cmds, bwr.write_size) << "\n";
1129 while (cmds < end) cmds = printCommand(logStream, cmds);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001131 logStream << "Size of receive buffer: " << bwr.read_size << ", needRead: " << needRead
1132 << ", doReceive: " << doReceive << "\n";
1133
1134 std::string message = logStream.str();
1135 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 // Return immediately if there is nothing to do.
1139 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 bwr.write_consumed = 0;
1142 bwr.read_consumed = 0;
1143 status_t err;
1144 do {
1145 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001146 std::ostringstream logStream;
1147 logStream << "About to read/write, write size = " << mOut.dataSize() << "\n";
1148 std::string message = logStream.str();
1149 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 }
Elliott Hughes6071da72015-08-12 15:27:47 -07001151#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
1153 err = NO_ERROR;
1154 else
1155 err = -errno;
1156#else
1157 err = INVALID_OPERATION;
1158#endif
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001159 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001160 err = -EBADF;
1161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001163 std::ostringstream logStream;
1164 logStream << "Finished read/write, write size = " << mOut.dataSize() << "\n";
1165 std::string message = logStream.str();
1166 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 }
1168 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001171 std::ostringstream logStream;
1172 logStream << "Our err: " << (void*)(intptr_t)err
1173 << ", write consumed: " << bwr.write_consumed << " (of " << mOut.dataSize()
1174 << "), read consumed: " << bwr.read_consumed << "\n";
1175 std::string message = logStream.str();
1176 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 }
1178
1179 if (err >= NO_ERROR) {
1180 if (bwr.write_consumed > 0) {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001181 if (bwr.write_consumed < mOut.dataSize())
Steven Morelandb077deb2020-04-16 16:22:52 -07001182 LOG_ALWAYS_FATAL("Driver did not consume write buffer. "
1183 "err: %s consumed: %zu of %zu",
1184 statusToString(err).c_str(),
1185 (size_t)bwr.write_consumed,
1186 mOut.dataSize());
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001187 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001189 processPostWriteDerefs();
1190 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 }
1192 if (bwr.read_consumed > 0) {
1193 mIn.setDataSize(bwr.read_consumed);
1194 mIn.setDataPosition(0);
1195 }
1196 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001197 std::ostringstream logStream;
1198 logStream << "Remaining data size: " << mOut.dataSize() << "\n";
1199 logStream << "Received commands from driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 const void* cmds = mIn.data();
1201 const void* end = mIn.data() + mIn.dataSize();
Pawan Wagh7063b522022-09-28 18:52:26 +00001202 logStream << "\t" << HexDump(cmds, mIn.dataSize()) << "\n";
1203 while (cmds < end) cmds = printReturnCommand(logStream, cmds);
1204 std::string message = logStream.str();
1205 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
1207 return NO_ERROR;
1208 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 return err;
1211}
1212
1213status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1214 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1215{
1216 binder_transaction_data tr;
1217
Arve HjønnevÄg07fd0f12014-02-18 21:10:29 -08001218 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 tr.target.handle = handle;
1220 tr.code = code;
1221 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001222 tr.cookie = 0;
1223 tr.sender_pid = 0;
1224 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 const status_t err = data.errorCheck();
1227 if (err == NO_ERROR) {
1228 tr.data_size = data.ipcDataSize();
1229 tr.data.ptr.buffer = data.ipcData();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001230 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 tr.data.ptr.offsets = data.ipcObjects();
1232 } else if (statusBuffer) {
1233 tr.flags |= TF_STATUS_CODE;
1234 *statusBuffer = err;
1235 tr.data_size = sizeof(status_t);
Arve HjønnevÄg87b30d02014-02-18 21:04:31 -08001236 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 tr.offsets_size = 0;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001238 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 } else {
1240 return (mLastError = err);
1241 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 mOut.writeInt32(cmd);
1244 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 return NO_ERROR;
1247}
1248
1249sp<BBinder> the_context_object;
1250
Jiyong Park384328e2020-11-13 17:16:48 +09001251void IPCThreadState::setTheContextObject(const sp<BBinder>& obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252{
1253 the_context_object = obj;
1254}
1255
1256status_t IPCThreadState::executeCommand(int32_t cmd)
1257{
1258 BBinder* obj;
1259 RefBase::weakref_type* refs;
1260 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001261
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001262 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 case BR_ERROR:
1264 result = mIn.readInt32();
1265 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001266
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 case BR_OK:
1268 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001271 refs = (RefBase::weakref_type*)mIn.readPointer();
1272 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001273 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1275 refs, obj, refs->refBase());
1276 obj->incStrong(mProcess.get());
1277 IF_LOG_REMOTEREFS() {
1278 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1279 obj->printRefs();
1280 }
1281 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001282 mOut.writePointer((uintptr_t)refs);
1283 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001287 refs = (RefBase::weakref_type*)mIn.readPointer();
1288 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001289 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1291 refs, obj, refs->refBase());
1292 IF_LOG_REMOTEREFS() {
1293 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1294 obj->printRefs();
1295 }
1296 mPendingStrongDerefs.push(obj);
1297 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001300 refs = (RefBase::weakref_type*)mIn.readPointer();
1301 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 refs->incWeak(mProcess.get());
1303 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001304 mOut.writePointer((uintptr_t)refs);
1305 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001309 refs = (RefBase::weakref_type*)mIn.readPointer();
Jiyong Park5970d0a2022-03-08 16:56:13 +09001310 // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
1311 obj = (BBinder*)mIn.readPointer(); // consume
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 // NOTE: This assertion is not valid, because the object may no
1313 // longer exist (thus the (BBinder*)cast above resulting in a different
1314 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001315 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1317 // refs, obj, refs->refBase());
1318 mPendingWeakDerefs.push(refs);
1319 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001322 refs = (RefBase::weakref_type*)mIn.readPointer();
1323 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 {
1326 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001327 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1329 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 mOut.writeInt32(BC_ACQUIRE_RESULT);
1332 mOut.writeInt32((int32_t)success);
1333 }
1334 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001335
Steven Morelandf0212002018-12-26 13:59:23 -08001336 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 case BR_TRANSACTION:
1338 {
Steven Morelandf0212002018-12-26 13:59:23 -08001339 binder_transaction_data_secctx tr_secctx;
1340 binder_transaction_data& tr = tr_secctx.transaction_data;
1341
1342 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1343 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1344 } else {
1345 result = mIn.read(&tr, sizeof(tr));
1346 tr_secctx.secctx = 0;
1347 }
1348
Steve Block67263472012-01-09 18:35:44 +00001349 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 "Not enough command data for brTRANSACTION");
1351 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001352
Martijn Coenenea0090a2017-11-02 18:54:40 +00001353 Parcel buffer;
1354 buffer.ipcSetDataReference(
1355 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1356 tr.data_size,
1357 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +00001358 tr.offsets_size/sizeof(binder_size_t), freeBuffer);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001359
Steven Moreland39d887d2020-01-31 14:56:45 -08001360 const void* origServingStackPointer = mServingStackPointer;
Steven Moreland35626652021-05-15 01:32:04 +00001361 mServingStackPointer = __builtin_frame_address(0);
Steven Moreland39d887d2020-01-31 14:56:45 -08001362
Martijn Coenenea0090a2017-11-02 18:54:40 +00001363 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001364 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001365 const uid_t origUid = mCallingUid;
mattgilbride85897672022-10-22 17:42:44 +00001366 const bool origHasExplicitIdentity = mHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001367 const int32_t origStrictModePolicy = mStrictModePolicy;
1368 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001369 const int32_t origWorkSource = mWorkSource;
1370 const bool origPropagateWorkSet = mPropagateWorkSource;
1371 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1372 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1373 // here to never propagate it.
1374 clearCallingWorkSource();
1375 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001376
1377 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001378 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001379 mCallingUid = tr.sender_euid;
mattgilbride85897672022-10-22 17:42:44 +00001380 mHasExplicitIdentity = false;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001381 mLastTransactionBinderFlags = tr.flags;
1382
Steven Morelandf0212002018-12-26 13:59:23 -08001383 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1384 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001387 status_t error;
1388 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001389 std::ostringstream logStream;
1390 logStream << "BR_TRANSACTION thr " << (void*)pthread_self() << " / obj "
1391 << tr.target.ptr << " / code " << TypeCode(tr.code) << ": \t" << buffer
1392 << "\n"
1393 << "Data addr = " << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1394 << ", offsets addr="
1395 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << "\n";
1396 std::string message = logStream.str();
1397 ALOGI("%s", message.c_str());
Martijn Coenenea0090a2017-11-02 18:54:40 +00001398 }
1399 if (tr.target.ptr) {
1400 // We only have a weak reference on the target object, so we must first try to
1401 // safely acquire a strong reference before doing anything else with it.
1402 if (reinterpret_cast<RefBase::weakref_type*>(
1403 tr.target.ptr)->attemptIncStrong(this)) {
1404 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1405 &reply, tr.flags);
1406 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001407 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001408 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001409 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001410
Martijn Coenenea0090a2017-11-02 18:54:40 +00001411 } else {
1412 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001414
Steven Morelandf0212002018-12-26 13:59:23 -08001415 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1416 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001417
Martijn Coenenea0090a2017-11-02 18:54:40 +00001418 if ((tr.flags & TF_ONE_WAY) == 0) {
1419 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1420 if (error < NO_ERROR) reply.setError(error);
Steven Morelandf183fdd2020-10-27 00:12:12 +00001421
Steven Morelandce15b9f2022-09-08 17:42:45 +00001422 // b/238777741: clear buffer before we send the reply.
1423 // Otherwise, there is a race where the client may
1424 // receive the reply and send another transaction
1425 // here and the space used by this transaction won't
1426 // be freed for the client.
1427 buffer.setDataSize(0);
1428
Steven Morelandf183fdd2020-10-27 00:12:12 +00001429 constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF;
1430 sendReply(reply, (tr.flags & kForwardReplyFlags));
Martijn Coenenea0090a2017-11-02 18:54:40 +00001431 } else {
Steven Moreland80844f72020-12-12 02:06:08 +00001432 if (error != OK) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001433 std::ostringstream logStream;
1434 logStream << "oneway function results for code " << tr.code << " on binder at "
1435 << reinterpret_cast<void*>(tr.target.ptr)
1436 << " will be dropped but finished with status "
1437 << statusToString(error);
Steven Moreland80844f72020-12-12 02:06:08 +00001438
1439 // ideally we could log this even when error == OK, but it
1440 // causes too much logspam because some manually-written
1441 // interfaces have clients that call methods which always
1442 // write results, sometimes as oneway methods.
1443 if (reply.dataSize() != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001444 logStream << " and reply parcel size " << reply.dataSize();
Steven Moreland80844f72020-12-12 02:06:08 +00001445 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001446 std::string message = logStream.str();
1447 ALOGI("%s", message.c_str());
Steven Morelandce66b8a2020-02-10 14:43:14 -08001448 }
Martijn Coenenea0090a2017-11-02 18:54:40 +00001449 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1450 }
1451
Steven Moreland39d887d2020-01-31 14:56:45 -08001452 mServingStackPointer = origServingStackPointer;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001453 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001454 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001455 mCallingUid = origUid;
mattgilbride85897672022-10-22 17:42:44 +00001456 mHasExplicitIdentity = origHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001457 mStrictModePolicy = origStrictModePolicy;
1458 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001459 mWorkSource = origWorkSource;
1460 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001463 std::ostringstream logStream;
1464 logStream << "BC_REPLY thr " << (void*)pthread_self() << " / obj " << tr.target.ptr
1465 << ": \t" << reply << "\n";
1466 std::string message = logStream.str();
1467 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001469
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 }
1471 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 case BR_DEAD_BINDER:
1474 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001475 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 proxy->sendObituary();
1477 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001478 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1482 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001483 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 proxy->getWeakRefs()->decWeak(proxy);
1485 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001486
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 case BR_FINISHED:
1488 result = TIMED_OUT;
1489 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 case BR_NOOP:
1492 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 case BR_SPAWN_LOOPER:
1495 mProcess->spawnPooledThread(false);
1496 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 default:
liangweikanga43ee152016-10-25 16:37:54 +08001499 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 result = UNKNOWN_ERROR;
1501 break;
1502 }
1503
1504 if (result != NO_ERROR) {
1505 mLastError = result;
1506 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 return result;
1509}
1510
Steven Moreland39d887d2020-01-31 14:56:45 -08001511const void* IPCThreadState::getServingStackPointer() const {
1512 return mServingStackPointer;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001513}
1514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515void IPCThreadState::threadDestructor(void *st)
1516{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001517 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1518 if (self) {
1519 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001520#if defined(__ANDROID__)
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001521 if (self->mProcess->mDriverFD >= 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001522 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001525 delete self;
1526 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527}
1528
Li Li6f059292021-09-10 09:59:30 -07001529status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, uint32_t *sync_received,
1530 uint32_t *async_received)
1531{
1532 int ret = 0;
Jiyong Park5970d0a2022-03-08 16:56:13 +09001533 binder_frozen_status_info info = {};
Li Li6f059292021-09-10 09:59:30 -07001534 info.pid = pid;
1535
1536#if defined(__ANDROID__)
1537 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0)
1538 ret = -errno;
1539#endif
1540 *sync_received = info.sync_recv;
1541 *async_received = info.async_recv;
1542
1543 return ret;
1544}
Li Li6f059292021-09-10 09:59:30 -07001545
Marco Ballesio7ee17572020-09-08 10:30:03 -07001546status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) {
1547 struct binder_freeze_info info;
1548 int ret = 0;
1549
1550 info.pid = pid;
1551 info.enable = enable;
1552 info.timeout_ms = timeout_ms;
1553
1554
1555#if defined(__ANDROID__)
1556 if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0)
1557 ret = -errno;
1558#endif
1559
1560 //
1561 // ret==-EAGAIN indicates that transactions have not drained.
1562 // Call again to poll for completion.
1563 //
1564 return ret;
1565}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566
Carlos Llamasb235b122021-12-20 06:38:44 -08001567void IPCThreadState::logExtendedError() {
1568 struct binder_extended_error ee = {.command = BR_OK};
1569
1570 if (!ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::EXTENDED_ERROR))
1571 return;
1572
1573#if defined(__ANDROID__)
1574 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_EXTENDED_ERROR, &ee) < 0) {
1575 ALOGE("Failed to get extended error: %s", strerror(errno));
1576 return;
1577 }
1578#endif
1579
1580 ALOGE_IF(ee.command != BR_OK, "Binder transaction failure: %d/%d/%d",
1581 ee.id, ee.command, ee.param);
1582}
1583
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001584void IPCThreadState::freeBuffer(const uint8_t* data, size_t /*dataSize*/,
1585 const binder_size_t* /*objects*/, size_t /*objectsSize*/) {
Steve Blocka19954a2012-01-04 20:05:49 +00001586 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001588 std::ostringstream logStream;
1589 logStream << "Writing BC_FREE_BUFFER for " << data << "\n";
1590 std::string message = logStream.str();
1591 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 }
Steve Block67263472012-01-09 18:35:44 +00001593 ALOG_ASSERT(data != NULL, "Called with NULL data");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 IPCThreadState* state = self();
1595 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001596 state->mOut.writePointer((uintptr_t)data);
Martijn Coenen0442a862017-11-17 10:46:32 +01001597 state->flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598}
1599
Steven Moreland61ff8492019-09-26 16:05:45 -07001600} // namespace android