blob: b038feb8786f7057dc9d2bcb862908143f64b479 [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#include <utils/threads.h>
31
Mathias Agopian208059f2009-05-18 15:08:03 -070032#include <private/binder/binder_module.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Hans Boehma997b232019-04-12 16:59:00 -070034#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070036#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <pthread.h>
38#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080039#include <signal.h>
40#include <stdio.h>
41#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080043#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Steven Morelanda4853cd2019-07-12 15:44:37 -070045#include "Static.h"
46
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#if LOG_NDEBUG
48
49#define IF_LOG_TRANSACTIONS() if (false)
50#define IF_LOG_COMMANDS() if (false)
51#define LOG_REMOTEREFS(...)
52#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#define LOG_THREADPOOL(...)
55#define LOG_ONEWAY(...)
56
57#else
58
Steve Block9f760152011-10-12 17:27:03 +010059#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
60#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
61#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
62#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
63#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
64#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
66#endif
67
68// ---------------------------------------------------------------------------
69
70namespace android {
71
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070072// Static const and functions will be optimized out if not used,
73// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070075 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 "BR_TRANSACTION",
78 "BR_REPLY",
79 "BR_ACQUIRE_RESULT",
80 "BR_DEAD_REPLY",
81 "BR_TRANSACTION_COMPLETE",
82 "BR_INCREFS",
83 "BR_ACQUIRE",
84 "BR_RELEASE",
85 "BR_DECREFS",
86 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 "BR_NOOP",
88 "BR_SPAWN_LOOPER",
89 "BR_FINISHED",
90 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070091 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Morelandf0212002018-12-26 13:59:23 -080092 "BR_FAILED_REPLY",
93 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094};
95
96static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 "BC_TRANSACTION",
98 "BC_REPLY",
99 "BC_ACQUIRE_RESULT",
100 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 "BC_INCREFS",
102 "BC_ACQUIRE",
103 "BC_RELEASE",
104 "BC_DECREFS",
105 "BC_INCREFS_DONE",
106 "BC_ACQUIRE_DONE",
107 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 "BC_REGISTER_LOOPER",
109 "BC_ENTER_LOOPER",
110 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 "BC_REQUEST_DEATH_NOTIFICATION",
112 "BC_CLEAR_DEATH_NOTIFICATION",
113 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114};
115
Olivier Gaillard91a04802018-11-14 17:32:41 +0000116static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100117
songjinshi73a7dde2016-10-18 21:05:56 +0800118static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119{
songjinshi8e486c62019-04-04 11:22:52 +0800120 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
122 return kReturnStrings[idx];
123 else
124 return "unknown";
125}
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127static const void* printBinderTransactionData(TextOutput& out, const void* data)
128{
129 const binder_transaction_data* btd =
130 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700131 if (btd->target.handle < 1024) {
132 /* want to print descriptors in decimal; guess based on value */
133 out << "target.desc=" << btd->target.handle;
134 } else {
135 out << "target.ptr=" << btd->target.ptr;
136 }
137 out << " (cookie " << btd->cookie << ")" << endl
Jiyong Park16c6e702020-11-13 20:53:12 +0900138 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
140 << " bytes)" << endl
141 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700142 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 return btd+1;
144}
145
146static const void* printReturnCommand(TextOutput& out, const void* _cmd)
147{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700148 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100150 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700151 size_t cmdIndex = code & 0xff;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100152 if (code == BR_ERROR) {
Jiyong Park16c6e702020-11-13 20:53:12 +0900153 out << "BR_ERROR: " << (void*)(uint64_t)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700155 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 out << "Unknown reply: " << code << endl;
157 return cmd;
158 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700159 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 switch (code) {
162 case BR_TRANSACTION:
163 case BR_REPLY: {
164 out << ": " << indent;
165 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
166 out << dedent;
167 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 case BR_ACQUIRE_RESULT: {
170 const int32_t res = *cmd++;
171 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
172 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 case BR_INCREFS:
175 case BR_ACQUIRE:
176 case BR_RELEASE:
177 case BR_DECREFS: {
178 const int32_t b = *cmd++;
179 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900180 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 case BR_ATTEMPT_ACQUIRE: {
184 const int32_t p = *cmd++;
185 const int32_t b = *cmd++;
186 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900187 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 << "), pri=" << p;
189 } break;
190
191 case BR_DEAD_BINDER:
192 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
193 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900194 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700196
197 default:
198 // no details to show for: BR_OK, BR_DEAD_REPLY,
199 // BR_TRANSACTION_COMPLETE, BR_FINISHED
200 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 out << endl;
204 return cmd;
205}
206
207static const void* printCommand(TextOutput& out, const void* _cmd)
208{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700209 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100211 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700212 size_t cmdIndex = code & 0xff;
213
214 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 out << "Unknown command: " << code << endl;
216 return cmd;
217 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700218 out << kCommandStrings[cmdIndex];
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 switch (code) {
221 case BC_TRANSACTION:
222 case BC_REPLY: {
223 out << ": " << indent;
224 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
225 out << dedent;
226 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 case BC_ACQUIRE_RESULT: {
229 const int32_t res = *cmd++;
230 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
231 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 case BC_FREE_BUFFER: {
234 const int32_t buf = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900235 out << ": buffer=" << (void*)(uint64_t)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 case BC_INCREFS:
239 case BC_ACQUIRE:
240 case BC_RELEASE:
241 case BC_DECREFS: {
242 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700243 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 case BC_INCREFS_DONE:
247 case BC_ACQUIRE_DONE: {
248 const int32_t b = *cmd++;
249 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900250 out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
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_ATTEMPT_ACQUIRE: {
254 const int32_t p = *cmd++;
255 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700256 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 case BC_REQUEST_DEATH_NOTIFICATION:
260 case BC_CLEAR_DEATH_NOTIFICATION: {
261 const int32_t h = *cmd++;
262 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900263 out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 } break;
265
266 case BC_DEAD_BINDER_DONE: {
267 const int32_t c = *cmd++;
Jiyong Park16c6e702020-11-13 20:53:12 +0900268 out << ": death cookie " << (void*)(uint64_t)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700270
271 default:
272 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
273 // BC_EXIT_LOOPER
274 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 out << endl;
278 return cmd;
279}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
281static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
Hans Boehma997b232019-04-12 16:59:00 -0700282static std::atomic<bool> gHaveTLS(false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283static pthread_key_t gTLS = 0;
Hans Boehma997b232019-04-12 16:59:00 -0700284static std::atomic<bool> gShutdown = false;
285static std::atomic<bool> gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
287IPCThreadState* IPCThreadState::self()
288{
Hans Boehma997b232019-04-12 16:59:00 -0700289 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290restart:
291 const pthread_key_t k = gTLS;
292 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
293 if (st) return st;
294 return new IPCThreadState;
295 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800296
Hans Boehma997b232019-04-12 16:59:00 -0700297 // Racey, heuristic test for simultaneous shutdown.
298 if (gShutdown.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800299 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700300 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800301 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 pthread_mutex_lock(&gTLSMutex);
Hans Boehma997b232019-04-12 16:59:00 -0700304 if (!gHaveTLS.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800305 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
306 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800308 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
309 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700310 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 }
Hans Boehma997b232019-04-12 16:59:00 -0700312 gHaveTLS.store(true, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 }
314 pthread_mutex_unlock(&gTLSMutex);
315 goto restart;
316}
317
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800318IPCThreadState* IPCThreadState::selfOrNull()
319{
Hans Boehma997b232019-04-12 16:59:00 -0700320 if (gHaveTLS.load(std::memory_order_acquire)) {
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800321 const pthread_key_t k = gTLS;
322 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
323 return st;
324 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700325 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800326}
327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328void IPCThreadState::shutdown()
329{
Hans Boehma997b232019-04-12 16:59:00 -0700330 gShutdown.store(true, std::memory_order_relaxed);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800331
Hans Boehma997b232019-04-12 16:59:00 -0700332 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 // XXX Need to wait for all thread pool threads to exit!
334 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
335 if (st) {
336 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700337 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 }
zhongjieff405782016-03-09 15:05:04 +0800339 pthread_key_delete(gTLS);
Hans Boehma997b232019-04-12 16:59:00 -0700340 gHaveTLS.store(false, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 }
342}
343
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800344void IPCThreadState::disableBackgroundScheduling(bool disable)
345{
Hans Boehma997b232019-04-12 16:59:00 -0700346 gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800347}
348
Martijn Coenen2b631742017-05-05 11:16:59 -0700349bool IPCThreadState::backgroundSchedulingDisabled()
350{
Hans Boehma997b232019-04-12 16:59:00 -0700351 return gDisableBackgroundScheduling.load(std::memory_order_relaxed);
Martijn Coenen2b631742017-05-05 11:16:59 -0700352}
353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354sp<ProcessState> IPCThreadState::process()
355{
356 return mProcess;
357}
358
359status_t IPCThreadState::clearLastError()
360{
361 const status_t err = mLastError;
362 mLastError = NO_ERROR;
363 return err;
364}
365
Dan Stoza9c634fd2014-11-26 12:23:23 -0800366pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
368 return mCallingPid;
369}
370
Steven Morelandf0212002018-12-26 13:59:23 -0800371const char* IPCThreadState::getCallingSid() const
372{
373 return mCallingSid;
374}
375
Dan Stoza9c634fd2014-11-26 12:23:23 -0800376uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377{
378 return mCallingUid;
379}
380
381int64_t IPCThreadState::clearCallingIdentity()
382{
Steven Morelandf0212002018-12-26 13:59:23 -0800383 // ignore mCallingSid for legacy reasons
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
385 clearCaller();
386 return token;
387}
388
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700389void IPCThreadState::setStrictModePolicy(int32_t policy)
390{
391 mStrictModePolicy = policy;
392}
393
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700394int32_t IPCThreadState::getStrictModePolicy() const
395{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700396 return mStrictModePolicy;
397}
398
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000399int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100400{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000401 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
402 mPropagateWorkSource = true;
403 return token;
404}
405
406int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
407{
408 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
409 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100410 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000411 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100412}
413
Olivier Gaillard91a04802018-11-14 17:32:41 +0000414void IPCThreadState::clearPropagateWorkSource()
415{
416 mPropagateWorkSource = false;
417}
418
419bool IPCThreadState::shouldPropagateWorkSource() const
420{
421 return mPropagateWorkSource;
422}
423
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000424uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100425{
426 return mWorkSource;
427}
428
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000429int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100430{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000431 return setCallingWorkSourceUid(kUnsetWorkSource);
432}
433
434void IPCThreadState::restoreCallingWorkSource(int64_t token)
435{
436 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000437 setCallingWorkSourceUidWithoutPropagation(uid);
438 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100439}
440
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700441void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
442{
443 mLastTransactionBinderFlags = flags;
444}
445
446int32_t IPCThreadState::getLastTransactionBinderFlags() const
447{
448 return mLastTransactionBinderFlags;
449}
450
Steven Moreland9514b202020-09-21 18:03:27 +0000451void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) {
452 mCallRestriction = restriction;
453}
454
455ProcessState::CallRestriction IPCThreadState::getCallRestriction() const {
456 return mCallRestriction;
457}
458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459void IPCThreadState::restoreCallingIdentity(int64_t token)
460{
461 mCallingUid = (int)(token>>32);
Steven Morelandf0212002018-12-26 13:59:23 -0800462 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 mCallingPid = (int)token;
464}
465
466void IPCThreadState::clearCaller()
467{
Marco Nelissend43b1942009-07-17 07:59:17 -0700468 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800469 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700470 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
473void IPCThreadState::flushCommands()
474{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200475 if (mProcess->mDriverFD < 0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 return;
477 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700478 // The flush could have caused post-write refcount decrements to have
479 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
480 // being queued in mOut. So flush again, if we need to.
481 if (mOut.dataSize() > 0) {
482 talkWithDriver(false);
483 }
484 if (mOut.dataSize() > 0) {
485 ALOGW("mOut.dataSize() > 0 after flushCommands()");
486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487}
488
Wale Ogunwale376b8222015-04-13 16:16:10 -0700489void IPCThreadState::blockUntilThreadAvailable()
490{
491 pthread_mutex_lock(&mProcess->mThreadCountLock);
Steven Morelandc648a762021-01-16 02:39:45 +0000492 mProcess->mWaitingForThreads++;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700493 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700494 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
495 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
496 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700497 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
498 }
Steven Morelandc648a762021-01-16 02:39:45 +0000499 mProcess->mWaitingForThreads--;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700500 pthread_mutex_unlock(&mProcess->mThreadCountLock);
501}
502
Todd Poynor8d96cab2013-06-25 19:12:18 -0700503status_t IPCThreadState::getAndExecuteCommand()
504{
505 status_t result;
506 int32_t cmd;
507
508 result = talkWithDriver();
509 if (result >= NO_ERROR) {
510 size_t IN = mIn.dataAvail();
511 if (IN < sizeof(int32_t)) return result;
512 cmd = mIn.readInt32();
513 IF_LOG_COMMANDS() {
514 alog << "Processing top-level Command: "
515 << getReturnString(cmd) << endl;
516 }
517
Wale Ogunwale376b8222015-04-13 16:16:10 -0700518 pthread_mutex_lock(&mProcess->mThreadCountLock);
519 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700520 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
521 mProcess->mStarvationStartTimeMs == 0) {
522 mProcess->mStarvationStartTimeMs = uptimeMillis();
523 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700524 pthread_mutex_unlock(&mProcess->mThreadCountLock);
525
Todd Poynor8d96cab2013-06-25 19:12:18 -0700526 result = executeCommand(cmd);
527
Wale Ogunwale376b8222015-04-13 16:16:10 -0700528 pthread_mutex_lock(&mProcess->mThreadCountLock);
529 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700530 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
531 mProcess->mStarvationStartTimeMs != 0) {
532 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
533 if (starvationTimeMs > 100) {
534 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
535 mProcess->mMaxThreads, starvationTimeMs);
536 }
537 mProcess->mStarvationStartTimeMs = 0;
538 }
Steven Morelandc648a762021-01-16 02:39:45 +0000539
540 // Cond broadcast can be expensive, so don't send it every time a binder
541 // call is processed. b/168806193
542 if (mProcess->mWaitingForThreads > 0) {
543 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
544 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700545 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700546 }
547
548 return result;
549}
550
551// When we've cleared the incoming command queue, process any pending derefs
552void IPCThreadState::processPendingDerefs()
553{
554 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200555 /*
556 * The decWeak()/decStrong() calls may cause a destructor to run,
557 * which in turn could have initiated an outgoing transaction,
558 * which in turn could cause us to add to the pending refs
559 * vectors; so instead of simply iterating, loop until they're empty.
560 *
561 * We do this in an outer loop, because calling decStrong()
562 * may result in something being added to mPendingWeakDerefs,
563 * which could be delayed until the next incoming command
564 * from the driver if we don't process it now.
565 */
566 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
567 while (mPendingWeakDerefs.size() > 0) {
568 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
569 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700570 refs->decWeak(mProcess.get());
571 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700572
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200573 if (mPendingStrongDerefs.size() > 0) {
574 // We don't use while() here because we don't want to re-order
575 // strong and weak decs at all; if this decStrong() causes both a
576 // decWeak() and a decStrong() to be queued, we want to process
577 // the decWeak() first.
578 BBinder* obj = mPendingStrongDerefs[0];
579 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700580 obj->decStrong(mProcess.get());
581 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700582 }
583 }
584}
585
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700586void IPCThreadState::processPostWriteDerefs()
587{
588 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
589 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
590 refs->decWeak(mProcess.get());
591 }
592 mPostWriteWeakDerefs.clear();
593
594 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
595 RefBase* obj = mPostWriteStrongDerefs[i];
596 obj->decStrong(mProcess.get());
597 }
598 mPostWriteStrongDerefs.clear();
599}
600
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601void IPCThreadState::joinThreadPool(bool isMain)
602{
603 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
604
605 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800606
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 status_t result;
608 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700609 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700611 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800612
Todd Poynor8d96cab2013-06-25 19:12:18 -0700613 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700614 LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700615 mProcess->mDriverFD, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800617
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 // Let this thread exit the thread pool if it is no longer
619 // needed and it is not the main process thread.
620 if(result == TIMED_OUT && !isMain) {
621 break;
622 }
623 } while (result != -ECONNREFUSED && result != -EBADF);
624
Wei Wangc7341432016-10-19 10:23:59 -0700625 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
626 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 mOut.writeInt32(BC_EXIT_LOOPER);
629 talkWithDriver(false);
630}
631
Steven Morelandd8c85672020-07-24 21:30:41 +0000632status_t IPCThreadState::setupPolling(int* fd)
Todd Poynor8d96cab2013-06-25 19:12:18 -0700633{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200634 if (mProcess->mDriverFD < 0) {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700635 return -EBADF;
636 }
637
638 mOut.writeInt32(BC_ENTER_LOOPER);
Steven Morelandf210b502021-01-15 23:40:32 +0000639 flushCommands();
Todd Poynor8d96cab2013-06-25 19:12:18 -0700640 *fd = mProcess->mDriverFD;
641 return 0;
642}
643
644status_t IPCThreadState::handlePolledCommands()
645{
646 status_t result;
647
648 do {
649 result = getAndExecuteCommand();
650 } while (mIn.dataPosition() < mIn.dataSize());
651
652 processPendingDerefs();
653 flushCommands();
654 return result;
655}
656
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800657void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658{
Steve Blocka19954a2012-01-04 20:05:49 +0000659 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 flushCommands();
661 int fd = mProcess->mDriverFD;
662 mProcess->mDriverFD = -1;
663 close(fd);
664 //kill(getpid(), SIGKILL);
665}
666
667status_t IPCThreadState::transact(int32_t handle,
668 uint32_t code, const Parcel& data,
669 Parcel* reply, uint32_t flags)
670{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800671 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672
673 flags |= TF_ACCEPT_FDS;
674
675 IF_LOG_TRANSACTIONS() {
676 TextOutput::Bundle _b(alog);
677 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
678 << handle << " / code " << TypeCode(code) << ": "
679 << indent << data << dedent << endl;
680 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800681
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800682 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
683 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700684 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 if (err != NO_ERROR) {
687 if (reply) reply->setError(err);
688 return (mLastError = err);
689 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800692 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
693 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700694 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800695 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
696 ANDROID_LOG_ERROR);
697 } else /* FATAL_IF_NOT_ONEWAY */ {
Steven Morelandfcc77f12020-09-01 01:16:11 +0000698 LOG_ALWAYS_FATAL("Process may not make non-oneway calls (code: %u).", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800699 }
700 }
701
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700702 #if 0
703 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000704 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700705 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000706 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700707 }
708 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 if (reply) {
710 err = waitForResponse(reply);
711 } else {
712 Parcel fakeReply;
713 err = waitForResponse(&fakeReply);
714 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700715 #if 0
716 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000717 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700718 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000719 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700720 }
721 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 IF_LOG_TRANSACTIONS() {
724 TextOutput::Bundle _b(alog);
725 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
726 << handle << ": ";
727 if (reply) alog << indent << *reply << dedent << endl;
728 else alog << "(none requested)" << endl;
729 }
730 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700731 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800733
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 return err;
735}
736
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700737void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738{
739 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
740 mOut.writeInt32(BC_ACQUIRE);
741 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700742 // Create a temp reference until the driver has handled this command.
743 proxy->incStrong(mProcess.get());
744 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
747void IPCThreadState::decStrongHandle(int32_t handle)
748{
749 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
750 mOut.writeInt32(BC_RELEASE);
751 mOut.writeInt32(handle);
752}
753
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700754void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755{
756 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
757 mOut.writeInt32(BC_INCREFS);
758 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700759 // Create a temp reference until the driver has handled this command.
760 proxy->getWeakRefs()->incWeak(mProcess.get());
761 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762}
763
764void IPCThreadState::decWeakHandle(int32_t handle)
765{
766 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
767 mOut.writeInt32(BC_DECREFS);
768 mOut.writeInt32(handle);
769}
770
771status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
772{
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800773#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700774 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
776 mOut.writeInt32(0); // xxx was thread priority
777 mOut.writeInt32(handle);
778 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800779
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800783 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
785#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 return result;
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800788#else
789 (void)handle;
790 ALOGE("%s(%d): Not supported\n", __func__, handle);
791 return INVALID_OPERATION;
792#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793}
794
795void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
796{
797#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800798 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700800 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801}
802
803status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
804{
805 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
806 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000807 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 return NO_ERROR;
809}
810
811status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
812{
813 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
814 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000815 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 return NO_ERROR;
817}
818
819IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700820 : mProcess(ProcessState::self()),
Steven Moreland39d887d2020-01-31 14:56:45 -0800821 mServingStackPointer(nullptr),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100822 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000823 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700824 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800825 mLastTransactionBinderFlags(0),
826 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827{
828 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800829 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 mIn.setDataCapacity(256);
831 mOut.setDataCapacity(256);
832}
833
834IPCThreadState::~IPCThreadState()
835{
836}
837
Martijn Coenenea0090a2017-11-02 18:54:40 +0000838status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
839{
840 status_t err;
841 status_t statusBuffer;
842 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
843 if (err < NO_ERROR) return err;
844
Yi Kongfdd8da92018-06-07 17:52:27 -0700845 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000846}
847
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
849{
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100850 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 int32_t err;
852
853 while (1) {
854 if ((err=talkWithDriver()) < NO_ERROR) break;
855 err = mIn.errorCheck();
856 if (err < NO_ERROR) break;
857 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800858
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100859 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 IF_LOG_COMMANDS() {
862 alog << "Processing waitForResponse Command: "
863 << getReturnString(cmd) << endl;
864 }
865
866 switch (cmd) {
867 case BR_TRANSACTION_COMPLETE:
868 if (!reply && !acquireResult) goto finish;
869 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800870
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 case BR_DEAD_REPLY:
872 err = DEAD_OBJECT;
873 goto finish;
874
875 case BR_FAILED_REPLY:
876 err = FAILED_TRANSACTION;
877 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800878
Marco Ballesio7ee17572020-09-08 10:30:03 -0700879 case BR_FROZEN_REPLY:
880 err = FAILED_TRANSACTION;
881 goto finish;
882
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 case BR_ACQUIRE_RESULT:
884 {
Steve Block67263472012-01-09 18:35:44 +0000885 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 const int32_t result = mIn.readInt32();
887 if (!acquireResult) continue;
888 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
889 }
890 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 case BR_REPLY:
893 {
894 binder_transaction_data tr;
895 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000896 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 if (err != NO_ERROR) goto finish;
898
899 if (reply) {
900 if ((tr.flags & TF_STATUS_CODE) == 0) {
901 reply->ipcSetDataReference(
902 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
903 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800904 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
905 tr.offsets_size/sizeof(binder_size_t),
Steven Moreland161fe122020-11-12 23:16:47 +0000906 freeBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 } else {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800908 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700909 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
911 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800912 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +0000913 tr.offsets_size/sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 }
915 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700916 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
918 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800919 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +0000920 tr.offsets_size/sizeof(binder_size_t));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 continue;
922 }
923 }
924 goto finish;
925
926 default:
927 err = executeCommand(cmd);
928 if (err != NO_ERROR) goto finish;
929 break;
930 }
931 }
932
933finish:
934 if (err != NO_ERROR) {
935 if (acquireResult) *acquireResult = err;
936 if (reply) reply->setError(err);
937 mLastError = err;
938 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800939
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 return err;
941}
942
943status_t IPCThreadState::talkWithDriver(bool doReceive)
944{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200945 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100946 return -EBADF;
947 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800948
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 // Is the read buffer empty?
952 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800953
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 // We don't want to write anything if we are still reading
955 // from data left in the input buffer and the caller
956 // has requested to read the next data.
957 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800958
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 bwr.write_size = outAvail;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800960 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961
962 // This is what we'll read.
963 if (doReceive && needRead) {
964 bwr.read_size = mIn.dataCapacity();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800965 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 } else {
967 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800968 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700970
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 IF_LOG_COMMANDS() {
972 TextOutput::Bundle _b(alog);
973 if (outAvail != 0) {
974 alog << "Sending commands to driver: " << indent;
975 const void* cmds = (const void*)bwr.write_buffer;
976 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
977 alog << HexDump(cmds, bwr.write_size) << endl;
978 while (cmds < end) cmds = printCommand(alog, cmds);
979 alog << dedent;
980 }
981 alog << "Size of receive buffer: " << bwr.read_size
982 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
983 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800984
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 // Return immediately if there is nothing to do.
986 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700987
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 bwr.write_consumed = 0;
989 bwr.read_consumed = 0;
990 status_t err;
991 do {
992 IF_LOG_COMMANDS() {
993 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
994 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700995#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
997 err = NO_ERROR;
998 else
999 err = -errno;
1000#else
1001 err = INVALID_OPERATION;
1002#endif
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001003 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001004 err = -EBADF;
1005 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 IF_LOG_COMMANDS() {
1007 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
1008 }
1009 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -07001010
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001012 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -07001014 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 }
1016
1017 if (err >= NO_ERROR) {
1018 if (bwr.write_consumed > 0) {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001019 if (bwr.write_consumed < mOut.dataSize())
Steven Morelandb077deb2020-04-16 16:22:52 -07001020 LOG_ALWAYS_FATAL("Driver did not consume write buffer. "
1021 "err: %s consumed: %zu of %zu",
1022 statusToString(err).c_str(),
1023 (size_t)bwr.write_consumed,
1024 mOut.dataSize());
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001025 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001027 processPostWriteDerefs();
1028 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 }
1030 if (bwr.read_consumed > 0) {
1031 mIn.setDataSize(bwr.read_consumed);
1032 mIn.setDataPosition(0);
1033 }
1034 IF_LOG_COMMANDS() {
1035 TextOutput::Bundle _b(alog);
1036 alog << "Remaining data size: " << mOut.dataSize() << endl;
1037 alog << "Received commands from driver: " << indent;
1038 const void* cmds = mIn.data();
1039 const void* end = mIn.data() + mIn.dataSize();
1040 alog << HexDump(cmds, mIn.dataSize()) << endl;
1041 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1042 alog << dedent;
1043 }
1044 return NO_ERROR;
1045 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 return err;
1048}
1049
1050status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1051 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1052{
1053 binder_transaction_data tr;
1054
Arve HjønnevÄg07fd0f12014-02-18 21:10:29 -08001055 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 tr.target.handle = handle;
1057 tr.code = code;
1058 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001059 tr.cookie = 0;
1060 tr.sender_pid = 0;
1061 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 const status_t err = data.errorCheck();
1064 if (err == NO_ERROR) {
1065 tr.data_size = data.ipcDataSize();
1066 tr.data.ptr.buffer = data.ipcData();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001067 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 tr.data.ptr.offsets = data.ipcObjects();
1069 } else if (statusBuffer) {
1070 tr.flags |= TF_STATUS_CODE;
1071 *statusBuffer = err;
1072 tr.data_size = sizeof(status_t);
Arve HjønnevÄg87b30d02014-02-18 21:04:31 -08001073 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 tr.offsets_size = 0;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001075 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 } else {
1077 return (mLastError = err);
1078 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 mOut.writeInt32(cmd);
1081 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 return NO_ERROR;
1084}
1085
1086sp<BBinder> the_context_object;
1087
Jiyong Park384328e2020-11-13 17:16:48 +09001088void IPCThreadState::setTheContextObject(const sp<BBinder>& obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089{
1090 the_context_object = obj;
1091}
1092
1093status_t IPCThreadState::executeCommand(int32_t cmd)
1094{
1095 BBinder* obj;
1096 RefBase::weakref_type* refs;
1097 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001098
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001099 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 case BR_ERROR:
1101 result = mIn.readInt32();
1102 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 case BR_OK:
1105 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001108 refs = (RefBase::weakref_type*)mIn.readPointer();
1109 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001110 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1112 refs, obj, refs->refBase());
1113 obj->incStrong(mProcess.get());
1114 IF_LOG_REMOTEREFS() {
1115 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1116 obj->printRefs();
1117 }
1118 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001119 mOut.writePointer((uintptr_t)refs);
1120 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001124 refs = (RefBase::weakref_type*)mIn.readPointer();
1125 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001126 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1128 refs, obj, refs->refBase());
1129 IF_LOG_REMOTEREFS() {
1130 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1131 obj->printRefs();
1132 }
1133 mPendingStrongDerefs.push(obj);
1134 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001137 refs = (RefBase::weakref_type*)mIn.readPointer();
1138 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 refs->incWeak(mProcess.get());
1140 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001141 mOut.writePointer((uintptr_t)refs);
1142 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001146 refs = (RefBase::weakref_type*)mIn.readPointer();
1147 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 // NOTE: This assertion is not valid, because the object may no
1149 // longer exist (thus the (BBinder*)cast above resulting in a different
1150 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001151 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1153 // refs, obj, refs->refBase());
1154 mPendingWeakDerefs.push(refs);
1155 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001158 refs = (RefBase::weakref_type*)mIn.readPointer();
1159 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 {
1162 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001163 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1165 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 mOut.writeInt32(BC_ACQUIRE_RESULT);
1168 mOut.writeInt32((int32_t)success);
1169 }
1170 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001171
Steven Morelandf0212002018-12-26 13:59:23 -08001172 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 case BR_TRANSACTION:
1174 {
Steven Morelandf0212002018-12-26 13:59:23 -08001175 binder_transaction_data_secctx tr_secctx;
1176 binder_transaction_data& tr = tr_secctx.transaction_data;
1177
1178 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1179 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1180 } else {
1181 result = mIn.read(&tr, sizeof(tr));
1182 tr_secctx.secctx = 0;
1183 }
1184
Steve Block67263472012-01-09 18:35:44 +00001185 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 "Not enough command data for brTRANSACTION");
1187 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001188
Martijn Coenenea0090a2017-11-02 18:54:40 +00001189 Parcel buffer;
1190 buffer.ipcSetDataReference(
1191 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1192 tr.data_size,
1193 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
Steven Moreland161fe122020-11-12 23:16:47 +00001194 tr.offsets_size/sizeof(binder_size_t), freeBuffer);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001195
Steven Moreland39d887d2020-01-31 14:56:45 -08001196 const void* origServingStackPointer = mServingStackPointer;
1197 mServingStackPointer = &origServingStackPointer; // anything on the stack
1198
Martijn Coenenea0090a2017-11-02 18:54:40 +00001199 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001200 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001201 const uid_t origUid = mCallingUid;
1202 const int32_t origStrictModePolicy = mStrictModePolicy;
1203 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001204 const int32_t origWorkSource = mWorkSource;
1205 const bool origPropagateWorkSet = mPropagateWorkSource;
1206 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1207 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1208 // here to never propagate it.
1209 clearCallingWorkSource();
1210 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001211
1212 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001213 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001214 mCallingUid = tr.sender_euid;
1215 mLastTransactionBinderFlags = tr.flags;
1216
Steven Morelandf0212002018-12-26 13:59:23 -08001217 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1218 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001221 status_t error;
1222 IF_LOG_TRANSACTIONS() {
1223 TextOutput::Bundle _b(alog);
1224 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1225 << " / obj " << tr.target.ptr << " / code "
1226 << TypeCode(tr.code) << ": " << indent << buffer
1227 << dedent << endl
1228 << "Data addr = "
1229 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1230 << ", offsets addr="
1231 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1232 }
1233 if (tr.target.ptr) {
1234 // We only have a weak reference on the target object, so we must first try to
1235 // safely acquire a strong reference before doing anything else with it.
1236 if (reinterpret_cast<RefBase::weakref_type*>(
1237 tr.target.ptr)->attemptIncStrong(this)) {
1238 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1239 &reply, tr.flags);
1240 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001241 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001242 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001243 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001244
Martijn Coenenea0090a2017-11-02 18:54:40 +00001245 } else {
1246 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001248
Steven Morelandf0212002018-12-26 13:59:23 -08001249 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1250 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001251
Martijn Coenenea0090a2017-11-02 18:54:40 +00001252 if ((tr.flags & TF_ONE_WAY) == 0) {
1253 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1254 if (error < NO_ERROR) reply.setError(error);
Steven Morelandf183fdd2020-10-27 00:12:12 +00001255
1256 constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF;
1257 sendReply(reply, (tr.flags & kForwardReplyFlags));
Martijn Coenenea0090a2017-11-02 18:54:40 +00001258 } else {
Steven Moreland80844f72020-12-12 02:06:08 +00001259 if (error != OK) {
1260 alog << "oneway function results for code " << tr.code
1261 << " on binder at "
1262 << reinterpret_cast<void*>(tr.target.ptr)
1263 << " will be dropped but finished with status "
1264 << statusToString(error);
1265
1266 // ideally we could log this even when error == OK, but it
1267 // causes too much logspam because some manually-written
1268 // interfaces have clients that call methods which always
1269 // write results, sometimes as oneway methods.
1270 if (reply.dataSize() != 0) {
1271 alog << " and reply parcel size " << reply.dataSize();
1272 }
1273
1274 alog << endl;
Steven Morelandce66b8a2020-02-10 14:43:14 -08001275 }
Martijn Coenenea0090a2017-11-02 18:54:40 +00001276 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1277 }
1278
Steven Moreland39d887d2020-01-31 14:56:45 -08001279 mServingStackPointer = origServingStackPointer;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001280 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001281 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001282 mCallingUid = origUid;
1283 mStrictModePolicy = origStrictModePolicy;
1284 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001285 mWorkSource = origWorkSource;
1286 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 IF_LOG_TRANSACTIONS() {
1289 TextOutput::Bundle _b(alog);
1290 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1291 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1292 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001293
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 }
1295 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 case BR_DEAD_BINDER:
1298 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001299 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 proxy->sendObituary();
1301 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001302 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1306 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001307 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 proxy->getWeakRefs()->decWeak(proxy);
1309 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 case BR_FINISHED:
1312 result = TIMED_OUT;
1313 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 case BR_NOOP:
1316 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 case BR_SPAWN_LOOPER:
1319 mProcess->spawnPooledThread(false);
1320 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 default:
liangweikanga43ee152016-10-25 16:37:54 +08001323 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 result = UNKNOWN_ERROR;
1325 break;
1326 }
1327
1328 if (result != NO_ERROR) {
1329 mLastError = result;
1330 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 return result;
1333}
1334
Steven Moreland39d887d2020-01-31 14:56:45 -08001335const void* IPCThreadState::getServingStackPointer() const {
1336 return mServingStackPointer;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001337}
1338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339void IPCThreadState::threadDestructor(void *st)
1340{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001341 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1342 if (self) {
1343 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001344#if defined(__ANDROID__)
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001345 if (self->mProcess->mDriverFD >= 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001346 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001349 delete self;
1350 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351}
1352
Marco Ballesiob09fc4a2020-09-11 16:17:21 -07001353status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, bool *sync_received, bool *async_received)
1354{
1355 int ret = 0;
1356 binder_frozen_status_info info;
1357 info.pid = pid;
1358
1359#if defined(__ANDROID__)
1360 if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0)
1361 ret = -errno;
1362#endif
1363 *sync_received = info.sync_recv;
1364 *async_received = info.async_recv;
1365
1366 return ret;
1367}
1368
Marco Ballesio7ee17572020-09-08 10:30:03 -07001369status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) {
1370 struct binder_freeze_info info;
1371 int ret = 0;
1372
1373 info.pid = pid;
1374 info.enable = enable;
1375 info.timeout_ms = timeout_ms;
1376
1377
1378#if defined(__ANDROID__)
1379 if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0)
1380 ret = -errno;
1381#endif
1382
1383 //
1384 // ret==-EAGAIN indicates that transactions have not drained.
1385 // Call again to poll for completion.
1386 //
1387 return ret;
1388}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001390void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1391 size_t /*dataSize*/,
1392 const binder_size_t* /*objects*/,
Steven Moreland161fe122020-11-12 23:16:47 +00001393 size_t /*objectsSize*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394{
Steve Blocka19954a2012-01-04 20:05:49 +00001395 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 IF_LOG_COMMANDS() {
1397 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1398 }
Steve Block67263472012-01-09 18:35:44 +00001399 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001400 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 IPCThreadState* state = self();
1402 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001403 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404}
1405
Steven Moreland61ff8492019-09-26 16:05:45 -07001406} // namespace android