blob: 77703749a1d5a6b41b2b2479d45d17beb82179ff [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 case BR_DEAD_REPLY:
1021 err = DEAD_OBJECT;
1022 goto finish;
1023
1024 case BR_FAILED_REPLY:
1025 err = FAILED_TRANSACTION;
1026 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001027
Marco Ballesio7ee17572020-09-08 10:30:03 -07001028 case BR_FROZEN_REPLY:
1029 err = FAILED_TRANSACTION;
1030 goto finish;
1031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 case BR_ACQUIRE_RESULT:
1033 {
Steve Block67263472012-01-09 18:35:44 +00001034 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 const int32_t result = mIn.readInt32();
1036 if (!acquireResult) continue;
1037 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
1038 }
1039 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 case BR_REPLY:
1042 {
1043 binder_transaction_data tr;
1044 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001045 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 if (err != NO_ERROR) goto finish;
1047
1048 if (reply) {
1049 if ((tr.flags & TF_STATUS_CODE) == 0) {
1050 reply->ipcSetDataReference(
1051 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1052 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001053 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1054 tr.offsets_size/sizeof(binder_size_t),
Steven Moreland161fe122020-11-12 23:16:47 +00001055 freeBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 } else {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001057 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001058 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1059 tr.data_size,
1060 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1061 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 }
1063 } else {
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001064 freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), tr.data_size,
1065 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1066 tr.offsets_size / sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 continue;
1068 }
1069 }
1070 goto finish;
1071
1072 default:
1073 err = executeCommand(cmd);
1074 if (err != NO_ERROR) goto finish;
1075 break;
1076 }
1077 }
1078
1079finish:
1080 if (err != NO_ERROR) {
1081 if (acquireResult) *acquireResult = err;
1082 if (reply) reply->setError(err);
1083 mLastError = err;
Carlos Llamasb235b122021-12-20 06:38:44 -08001084 logExtendedError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 return err;
1088}
1089
1090status_t IPCThreadState::talkWithDriver(bool doReceive)
1091{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001092 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001093 return -EBADF;
1094 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 // Is the read buffer empty?
1099 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 // We don't want to write anything if we are still reading
1102 // from data left in the input buffer and the caller
1103 // has requested to read the next data.
1104 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 bwr.write_size = outAvail;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001107 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108
1109 // This is what we'll read.
1110 if (doReceive && needRead) {
1111 bwr.read_size = mIn.dataCapacity();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001112 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 } else {
1114 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -08001115 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001119 std::ostringstream logStream;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 if (outAvail != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001121 logStream << "Sending commands to driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 const void* cmds = (const void*)bwr.write_buffer;
Pawan Wagh7063b522022-09-28 18:52:26 +00001123 const void* end = ((const uint8_t*)cmds) + bwr.write_size;
1124 logStream << "\t" << HexDump(cmds, bwr.write_size) << "\n";
1125 while (cmds < end) cmds = printCommand(logStream, cmds);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001127 logStream << "Size of receive buffer: " << bwr.read_size << ", needRead: " << needRead
1128 << ", doReceive: " << doReceive << "\n";
1129
1130 std::string message = logStream.str();
1131 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 // Return immediately if there is nothing to do.
1135 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 bwr.write_consumed = 0;
1138 bwr.read_consumed = 0;
1139 status_t err;
1140 do {
1141 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001142 std::ostringstream logStream;
1143 logStream << "About to read/write, write size = " << mOut.dataSize() << "\n";
1144 std::string message = logStream.str();
1145 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
Elliott Hughes6071da72015-08-12 15:27:47 -07001147#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
1149 err = NO_ERROR;
1150 else
1151 err = -errno;
1152#else
1153 err = INVALID_OPERATION;
1154#endif
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001155 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001156 err = -EBADF;
1157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001159 std::ostringstream logStream;
1160 logStream << "Finished read/write, write size = " << mOut.dataSize() << "\n";
1161 std::string message = logStream.str();
1162 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 }
1164 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001167 std::ostringstream logStream;
1168 logStream << "Our err: " << (void*)(intptr_t)err
1169 << ", write consumed: " << bwr.write_consumed << " (of " << mOut.dataSize()
1170 << "), read consumed: " << bwr.read_consumed << "\n";
1171 std::string message = logStream.str();
1172 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 }
1174
1175 if (err >= NO_ERROR) {
1176 if (bwr.write_consumed > 0) {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001177 if (bwr.write_consumed < mOut.dataSize())
Steven Morelandb077deb2020-04-16 16:22:52 -07001178 LOG_ALWAYS_FATAL("Driver did not consume write buffer. "
1179 "err: %s consumed: %zu of %zu",
1180 statusToString(err).c_str(),
1181 (size_t)bwr.write_consumed,
1182 mOut.dataSize());
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001183 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001185 processPostWriteDerefs();
1186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
1188 if (bwr.read_consumed > 0) {
1189 mIn.setDataSize(bwr.read_consumed);
1190 mIn.setDataPosition(0);
1191 }
1192 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001193 std::ostringstream logStream;
1194 logStream << "Remaining data size: " << mOut.dataSize() << "\n";
1195 logStream << "Received commands from driver: ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 const void* cmds = mIn.data();
1197 const void* end = mIn.data() + mIn.dataSize();
Pawan Wagh7063b522022-09-28 18:52:26 +00001198 logStream << "\t" << HexDump(cmds, mIn.dataSize()) << "\n";
1199 while (cmds < end) cmds = printReturnCommand(logStream, cmds);
1200 std::string message = logStream.str();
1201 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 }
1203 return NO_ERROR;
1204 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 return err;
1207}
1208
1209status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1210 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1211{
1212 binder_transaction_data tr;
1213
Arve HjønnevÄg07fd0f12014-02-18 21:10:29 -08001214 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 tr.target.handle = handle;
1216 tr.code = code;
1217 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001218 tr.cookie = 0;
1219 tr.sender_pid = 0;
1220 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 const status_t err = data.errorCheck();
1223 if (err == NO_ERROR) {
1224 tr.data_size = data.ipcDataSize();
1225 tr.data.ptr.buffer = data.ipcData();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001226 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 tr.data.ptr.offsets = data.ipcObjects();
1228 } else if (statusBuffer) {
1229 tr.flags |= TF_STATUS_CODE;
1230 *statusBuffer = err;
1231 tr.data_size = sizeof(status_t);
Arve HjønnevÄg87b30d02014-02-18 21:04:31 -08001232 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 tr.offsets_size = 0;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001234 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 } else {
1236 return (mLastError = err);
1237 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 mOut.writeInt32(cmd);
1240 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 return NO_ERROR;
1243}
1244
1245sp<BBinder> the_context_object;
1246
Jiyong Park384328e2020-11-13 17:16:48 +09001247void IPCThreadState::setTheContextObject(const sp<BBinder>& obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248{
1249 the_context_object = obj;
1250}
1251
1252status_t IPCThreadState::executeCommand(int32_t cmd)
1253{
1254 BBinder* obj;
1255 RefBase::weakref_type* refs;
1256 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001257
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001258 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 case BR_ERROR:
1260 result = mIn.readInt32();
1261 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 case BR_OK:
1264 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001267 refs = (RefBase::weakref_type*)mIn.readPointer();
1268 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001269 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1271 refs, obj, refs->refBase());
1272 obj->incStrong(mProcess.get());
1273 IF_LOG_REMOTEREFS() {
1274 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1275 obj->printRefs();
1276 }
1277 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001278 mOut.writePointer((uintptr_t)refs);
1279 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001283 refs = (RefBase::weakref_type*)mIn.readPointer();
1284 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001285 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1287 refs, obj, refs->refBase());
1288 IF_LOG_REMOTEREFS() {
1289 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1290 obj->printRefs();
1291 }
1292 mPendingStrongDerefs.push(obj);
1293 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001296 refs = (RefBase::weakref_type*)mIn.readPointer();
1297 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 refs->incWeak(mProcess.get());
1299 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001300 mOut.writePointer((uintptr_t)refs);
1301 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001305 refs = (RefBase::weakref_type*)mIn.readPointer();
Jiyong Park5970d0a2022-03-08 16:56:13 +09001306 // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
1307 obj = (BBinder*)mIn.readPointer(); // consume
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 // NOTE: This assertion is not valid, because the object may no
1309 // longer exist (thus the (BBinder*)cast above resulting in a different
1310 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001311 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1313 // refs, obj, refs->refBase());
1314 mPendingWeakDerefs.push(refs);
1315 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001318 refs = (RefBase::weakref_type*)mIn.readPointer();
1319 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 {
1322 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001323 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1325 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 mOut.writeInt32(BC_ACQUIRE_RESULT);
1328 mOut.writeInt32((int32_t)success);
1329 }
1330 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001331
Steven Morelandf0212002018-12-26 13:59:23 -08001332 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 case BR_TRANSACTION:
1334 {
Steven Morelandf0212002018-12-26 13:59:23 -08001335 binder_transaction_data_secctx tr_secctx;
1336 binder_transaction_data& tr = tr_secctx.transaction_data;
1337
1338 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1339 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1340 } else {
1341 result = mIn.read(&tr, sizeof(tr));
1342 tr_secctx.secctx = 0;
1343 }
1344
Steve Block67263472012-01-09 18:35:44 +00001345 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 "Not enough command data for brTRANSACTION");
1347 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001348
Martijn Coenenea0090a2017-11-02 18:54:40 +00001349 Parcel buffer;
1350 buffer.ipcSetDataReference(
1351 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1352 tr.data_size,
1353 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +00001354 tr.offsets_size/sizeof(binder_size_t), freeBuffer);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001355
Steven Moreland39d887d2020-01-31 14:56:45 -08001356 const void* origServingStackPointer = mServingStackPointer;
Steven Moreland35626652021-05-15 01:32:04 +00001357 mServingStackPointer = __builtin_frame_address(0);
Steven Moreland39d887d2020-01-31 14:56:45 -08001358
Martijn Coenenea0090a2017-11-02 18:54:40 +00001359 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001360 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001361 const uid_t origUid = mCallingUid;
mattgilbride85897672022-10-22 17:42:44 +00001362 const bool origHasExplicitIdentity = mHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001363 const int32_t origStrictModePolicy = mStrictModePolicy;
1364 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001365 const int32_t origWorkSource = mWorkSource;
1366 const bool origPropagateWorkSet = mPropagateWorkSource;
1367 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1368 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1369 // here to never propagate it.
1370 clearCallingWorkSource();
1371 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001372
1373 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001374 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001375 mCallingUid = tr.sender_euid;
mattgilbride85897672022-10-22 17:42:44 +00001376 mHasExplicitIdentity = false;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001377 mLastTransactionBinderFlags = tr.flags;
1378
Steven Morelandf0212002018-12-26 13:59:23 -08001379 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1380 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001383 status_t error;
1384 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001385 std::ostringstream logStream;
1386 logStream << "BR_TRANSACTION thr " << (void*)pthread_self() << " / obj "
1387 << tr.target.ptr << " / code " << TypeCode(tr.code) << ": \t" << buffer
1388 << "\n"
1389 << "Data addr = " << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1390 << ", offsets addr="
1391 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << "\n";
1392 std::string message = logStream.str();
1393 ALOGI("%s", message.c_str());
Martijn Coenenea0090a2017-11-02 18:54:40 +00001394 }
1395 if (tr.target.ptr) {
1396 // We only have a weak reference on the target object, so we must first try to
1397 // safely acquire a strong reference before doing anything else with it.
1398 if (reinterpret_cast<RefBase::weakref_type*>(
1399 tr.target.ptr)->attemptIncStrong(this)) {
1400 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1401 &reply, tr.flags);
1402 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001403 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001404 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001405 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001406
Martijn Coenenea0090a2017-11-02 18:54:40 +00001407 } else {
1408 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001410
Steven Morelandf0212002018-12-26 13:59:23 -08001411 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1412 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001413
Martijn Coenenea0090a2017-11-02 18:54:40 +00001414 if ((tr.flags & TF_ONE_WAY) == 0) {
1415 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1416 if (error < NO_ERROR) reply.setError(error);
Steven Morelandf183fdd2020-10-27 00:12:12 +00001417
Steven Morelandce15b9f2022-09-08 17:42:45 +00001418 // b/238777741: clear buffer before we send the reply.
1419 // Otherwise, there is a race where the client may
1420 // receive the reply and send another transaction
1421 // here and the space used by this transaction won't
1422 // be freed for the client.
1423 buffer.setDataSize(0);
1424
Steven Morelandf183fdd2020-10-27 00:12:12 +00001425 constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF;
1426 sendReply(reply, (tr.flags & kForwardReplyFlags));
Martijn Coenenea0090a2017-11-02 18:54:40 +00001427 } else {
Steven Moreland80844f72020-12-12 02:06:08 +00001428 if (error != OK) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001429 std::ostringstream logStream;
1430 logStream << "oneway function results for code " << tr.code << " on binder at "
1431 << reinterpret_cast<void*>(tr.target.ptr)
1432 << " will be dropped but finished with status "
1433 << statusToString(error);
Steven Moreland80844f72020-12-12 02:06:08 +00001434
1435 // ideally we could log this even when error == OK, but it
1436 // causes too much logspam because some manually-written
1437 // interfaces have clients that call methods which always
1438 // write results, sometimes as oneway methods.
1439 if (reply.dataSize() != 0) {
Pawan Wagh7063b522022-09-28 18:52:26 +00001440 logStream << " and reply parcel size " << reply.dataSize();
Steven Moreland80844f72020-12-12 02:06:08 +00001441 }
Pawan Wagh7063b522022-09-28 18:52:26 +00001442 std::string message = logStream.str();
1443 ALOGI("%s", message.c_str());
Steven Morelandce66b8a2020-02-10 14:43:14 -08001444 }
Martijn Coenenea0090a2017-11-02 18:54:40 +00001445 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1446 }
1447
Steven Moreland39d887d2020-01-31 14:56:45 -08001448 mServingStackPointer = origServingStackPointer;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001449 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001450 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001451 mCallingUid = origUid;
mattgilbride85897672022-10-22 17:42:44 +00001452 mHasExplicitIdentity = origHasExplicitIdentity;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001453 mStrictModePolicy = origStrictModePolicy;
1454 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001455 mWorkSource = origWorkSource;
1456 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 IF_LOG_TRANSACTIONS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001459 std::ostringstream logStream;
1460 logStream << "BC_REPLY thr " << (void*)pthread_self() << " / obj " << tr.target.ptr
1461 << ": \t" << reply << "\n";
1462 std::string message = logStream.str();
1463 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 }
1467 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469 case BR_DEAD_BINDER:
1470 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001471 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 proxy->sendObituary();
1473 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001474 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1478 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001479 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 proxy->getWeakRefs()->decWeak(proxy);
1481 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 case BR_FINISHED:
1484 result = TIMED_OUT;
1485 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001486
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 case BR_NOOP:
1488 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 case BR_SPAWN_LOOPER:
1491 mProcess->spawnPooledThread(false);
1492 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 default:
liangweikanga43ee152016-10-25 16:37:54 +08001495 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 result = UNKNOWN_ERROR;
1497 break;
1498 }
1499
1500 if (result != NO_ERROR) {
1501 mLastError = result;
1502 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 return result;
1505}
1506
Steven Moreland39d887d2020-01-31 14:56:45 -08001507const void* IPCThreadState::getServingStackPointer() const {
1508 return mServingStackPointer;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001509}
1510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511void IPCThreadState::threadDestructor(void *st)
1512{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001513 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1514 if (self) {
1515 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001516#if defined(__ANDROID__)
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001517 if (self->mProcess->mDriverFD >= 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001518 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1519 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001521 delete self;
1522 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001523}
1524
Li Li6f059292021-09-10 09:59:30 -07001525status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, uint32_t *sync_received,
1526 uint32_t *async_received)
1527{
1528 int ret = 0;
Jiyong Park5970d0a2022-03-08 16:56:13 +09001529 binder_frozen_status_info info = {};
Li Li6f059292021-09-10 09:59:30 -07001530 info.pid = pid;
1531
1532#if defined(__ANDROID__)
1533 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0)
1534 ret = -errno;
1535#endif
1536 *sync_received = info.sync_recv;
1537 *async_received = info.async_recv;
1538
1539 return ret;
1540}
Li Li6f059292021-09-10 09:59:30 -07001541
Marco Ballesio7ee17572020-09-08 10:30:03 -07001542status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) {
1543 struct binder_freeze_info info;
1544 int ret = 0;
1545
1546 info.pid = pid;
1547 info.enable = enable;
1548 info.timeout_ms = timeout_ms;
1549
1550
1551#if defined(__ANDROID__)
1552 if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0)
1553 ret = -errno;
1554#endif
1555
1556 //
1557 // ret==-EAGAIN indicates that transactions have not drained.
1558 // Call again to poll for completion.
1559 //
1560 return ret;
1561}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562
Carlos Llamasb235b122021-12-20 06:38:44 -08001563void IPCThreadState::logExtendedError() {
1564 struct binder_extended_error ee = {.command = BR_OK};
1565
1566 if (!ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::EXTENDED_ERROR))
1567 return;
1568
1569#if defined(__ANDROID__)
1570 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_EXTENDED_ERROR, &ee) < 0) {
1571 ALOGE("Failed to get extended error: %s", strerror(errno));
1572 return;
1573 }
1574#endif
1575
1576 ALOGE_IF(ee.command != BR_OK, "Binder transaction failure: %d/%d/%d",
1577 ee.id, ee.command, ee.param);
1578}
1579
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00001580void IPCThreadState::freeBuffer(const uint8_t* data, size_t /*dataSize*/,
1581 const binder_size_t* /*objects*/, size_t /*objectsSize*/) {
Steve Blocka19954a2012-01-04 20:05:49 +00001582 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 IF_LOG_COMMANDS() {
Pawan Wagh7063b522022-09-28 18:52:26 +00001584 std::ostringstream logStream;
1585 logStream << "Writing BC_FREE_BUFFER for " << data << "\n";
1586 std::string message = logStream.str();
1587 ALOGI("%s", message.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 }
Steve Block67263472012-01-09 18:35:44 +00001589 ALOG_ASSERT(data != NULL, "Called with NULL data");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 IPCThreadState* state = self();
1591 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001592 state->mOut.writePointer((uintptr_t)data);
Martijn Coenen0442a862017-11-17 10:46:32 +01001593 state->flushIfNeeded();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594}
1595
Steven Moreland61ff8492019-09-26 16:05:45 -07001596} // namespace android