blob: 3424c28883fc2e0a0eb6e7f50d05d8c98b133c6d [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>
Jayant Chowdharydac6dc82018-10-01 22:52:44 +000020#include <binderthreadstate/IPCThreadStateBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/Binder.h>
23#include <binder/BpBinder.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070024#include <binder/TextOutput.h>
25
Steven Moreland7732a092019-01-02 17:54:16 -080026#include <android-base/macros.h>
Glenn Kastena26e1cf2012-03-16 07:15:23 -070027#include <cutils/sched_policy.h>
Steven Moreland7732a092019-01-02 17:54:16 -080028#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070030#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/threads.h>
32
Mathias Agopian208059f2009-05-18 15:08:03 -070033#include <private/binder/binder_module.h>
34#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Hans Boehma997b232019-04-12 16:59:00 -070036#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070038#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <pthread.h>
40#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080041#include <signal.h>
42#include <stdio.h>
43#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080045#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47#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
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700138 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)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) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700153 out << "BR_ERROR: " << (void*)(long)(*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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700180 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700187 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700194 out << ": death cookie " << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700235 out << ": buffer=" << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700250 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700263 out << ": handle=" << h << " (death cookie " << (void*)(long)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++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700268 out << ": death cookie " << (void*)(long)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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451void IPCThreadState::restoreCallingIdentity(int64_t token)
452{
453 mCallingUid = (int)(token>>32);
Steven Morelandf0212002018-12-26 13:59:23 -0800454 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 mCallingPid = (int)token;
456}
457
458void IPCThreadState::clearCaller()
459{
Marco Nelissend43b1942009-07-17 07:59:17 -0700460 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800461 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700462 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463}
464
465void IPCThreadState::flushCommands()
466{
467 if (mProcess->mDriverFD <= 0)
468 return;
469 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700470 // The flush could have caused post-write refcount decrements to have
471 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
472 // being queued in mOut. So flush again, if we need to.
473 if (mOut.dataSize() > 0) {
474 talkWithDriver(false);
475 }
476 if (mOut.dataSize() > 0) {
477 ALOGW("mOut.dataSize() > 0 after flushCommands()");
478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479}
480
Wale Ogunwale376b8222015-04-13 16:16:10 -0700481void IPCThreadState::blockUntilThreadAvailable()
482{
483 pthread_mutex_lock(&mProcess->mThreadCountLock);
484 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700485 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
486 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
487 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700488 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
489 }
490 pthread_mutex_unlock(&mProcess->mThreadCountLock);
491}
492
Todd Poynor8d96cab2013-06-25 19:12:18 -0700493status_t IPCThreadState::getAndExecuteCommand()
494{
495 status_t result;
496 int32_t cmd;
497
498 result = talkWithDriver();
499 if (result >= NO_ERROR) {
500 size_t IN = mIn.dataAvail();
501 if (IN < sizeof(int32_t)) return result;
502 cmd = mIn.readInt32();
503 IF_LOG_COMMANDS() {
504 alog << "Processing top-level Command: "
505 << getReturnString(cmd) << endl;
506 }
507
Wale Ogunwale376b8222015-04-13 16:16:10 -0700508 pthread_mutex_lock(&mProcess->mThreadCountLock);
509 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700510 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
511 mProcess->mStarvationStartTimeMs == 0) {
512 mProcess->mStarvationStartTimeMs = uptimeMillis();
513 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700514 pthread_mutex_unlock(&mProcess->mThreadCountLock);
515
Todd Poynor8d96cab2013-06-25 19:12:18 -0700516 result = executeCommand(cmd);
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 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
523 if (starvationTimeMs > 100) {
524 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
525 mProcess->mMaxThreads, starvationTimeMs);
526 }
527 mProcess->mStarvationStartTimeMs = 0;
528 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700529 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
530 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700531 }
532
533 return result;
534}
535
536// When we've cleared the incoming command queue, process any pending derefs
537void IPCThreadState::processPendingDerefs()
538{
539 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200540 /*
541 * The decWeak()/decStrong() calls may cause a destructor to run,
542 * which in turn could have initiated an outgoing transaction,
543 * which in turn could cause us to add to the pending refs
544 * vectors; so instead of simply iterating, loop until they're empty.
545 *
546 * We do this in an outer loop, because calling decStrong()
547 * may result in something being added to mPendingWeakDerefs,
548 * which could be delayed until the next incoming command
549 * from the driver if we don't process it now.
550 */
551 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
552 while (mPendingWeakDerefs.size() > 0) {
553 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
554 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700555 refs->decWeak(mProcess.get());
556 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700557
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200558 if (mPendingStrongDerefs.size() > 0) {
559 // We don't use while() here because we don't want to re-order
560 // strong and weak decs at all; if this decStrong() causes both a
561 // decWeak() and a decStrong() to be queued, we want to process
562 // the decWeak() first.
563 BBinder* obj = mPendingStrongDerefs[0];
564 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700565 obj->decStrong(mProcess.get());
566 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700567 }
568 }
569}
570
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700571void IPCThreadState::processPostWriteDerefs()
572{
573 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
574 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
575 refs->decWeak(mProcess.get());
576 }
577 mPostWriteWeakDerefs.clear();
578
579 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
580 RefBase* obj = mPostWriteStrongDerefs[i];
581 obj->decStrong(mProcess.get());
582 }
583 mPostWriteStrongDerefs.clear();
584}
585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586void IPCThreadState::joinThreadPool(bool isMain)
587{
588 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
589
590 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 status_t result;
593 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700594 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700596 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800597
Todd Poynor8d96cab2013-06-25 19:12:18 -0700598 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
599 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700600 mProcess->mDriverFD, result);
601 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800603
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 // Let this thread exit the thread pool if it is no longer
605 // needed and it is not the main process thread.
606 if(result == TIMED_OUT && !isMain) {
607 break;
608 }
609 } while (result != -ECONNREFUSED && result != -EBADF);
610
Wei Wangc7341432016-10-19 10:23:59 -0700611 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
612 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800613
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 mOut.writeInt32(BC_EXIT_LOOPER);
615 talkWithDriver(false);
616}
617
Todd Poynor8d96cab2013-06-25 19:12:18 -0700618int IPCThreadState::setupPolling(int* fd)
619{
620 if (mProcess->mDriverFD <= 0) {
621 return -EBADF;
622 }
623
624 mOut.writeInt32(BC_ENTER_LOOPER);
625 *fd = mProcess->mDriverFD;
626 return 0;
627}
628
629status_t IPCThreadState::handlePolledCommands()
630{
631 status_t result;
632
633 do {
634 result = getAndExecuteCommand();
635 } while (mIn.dataPosition() < mIn.dataSize());
636
637 processPendingDerefs();
638 flushCommands();
639 return result;
640}
641
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800642void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643{
Steve Blocka19954a2012-01-04 20:05:49 +0000644 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 flushCommands();
646 int fd = mProcess->mDriverFD;
647 mProcess->mDriverFD = -1;
648 close(fd);
649 //kill(getpid(), SIGKILL);
650}
651
652status_t IPCThreadState::transact(int32_t handle,
653 uint32_t code, const Parcel& data,
654 Parcel* reply, uint32_t flags)
655{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800656 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657
658 flags |= TF_ACCEPT_FDS;
659
660 IF_LOG_TRANSACTIONS() {
661 TextOutput::Bundle _b(alog);
662 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
663 << handle << " / code " << TypeCode(code) << ": "
664 << indent << data << dedent << endl;
665 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800666
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800667 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
668 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700669 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800670
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 if (err != NO_ERROR) {
672 if (reply) reply->setError(err);
673 return (mLastError = err);
674 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800677 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
678 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700679 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800680 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
681 ANDROID_LOG_ERROR);
682 } else /* FATAL_IF_NOT_ONEWAY */ {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700683 LOG_ALWAYS_FATAL("Process may not make oneway calls (code: %u).", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800684 }
685 }
686
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700687 #if 0
688 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000689 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700690 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000691 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700692 }
693 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 if (reply) {
695 err = waitForResponse(reply);
696 } else {
697 Parcel fakeReply;
698 err = waitForResponse(&fakeReply);
699 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700700 #if 0
701 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000702 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700703 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000704 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700705 }
706 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 IF_LOG_TRANSACTIONS() {
709 TextOutput::Bundle _b(alog);
710 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
711 << handle << ": ";
712 if (reply) alog << indent << *reply << dedent << endl;
713 else alog << "(none requested)" << endl;
714 }
715 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700716 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 return err;
720}
721
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700722void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723{
724 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
725 mOut.writeInt32(BC_ACQUIRE);
726 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700727 // Create a temp reference until the driver has handled this command.
728 proxy->incStrong(mProcess.get());
729 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730}
731
732void IPCThreadState::decStrongHandle(int32_t handle)
733{
734 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
735 mOut.writeInt32(BC_RELEASE);
736 mOut.writeInt32(handle);
737}
738
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700739void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740{
741 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
742 mOut.writeInt32(BC_INCREFS);
743 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700744 // Create a temp reference until the driver has handled this command.
745 proxy->getWeakRefs()->incWeak(mProcess.get());
746 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749void IPCThreadState::decWeakHandle(int32_t handle)
750{
751 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
752 mOut.writeInt32(BC_DECREFS);
753 mOut.writeInt32(handle);
754}
755
756status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
757{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800758#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700759 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
761 mOut.writeInt32(0); // xxx was thread priority
762 mOut.writeInt32(handle);
763 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800766
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800768 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
770#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800773#else
774 (void)handle;
775 ALOGE("%s(%d): Not supported\n", __func__, handle);
776 return INVALID_OPERATION;
777#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778}
779
780void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
781{
782#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800783 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700785 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786}
787
788status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
789{
790 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
791 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000792 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 return NO_ERROR;
794}
795
796status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
797{
798 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
799 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000800 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 return NO_ERROR;
802}
803
804IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700805 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100806 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000807 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700808 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800809 mLastTransactionBinderFlags(0),
810 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811{
812 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800813 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 mIn.setDataCapacity(256);
815 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000816 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817}
818
819IPCThreadState::~IPCThreadState()
820{
821}
822
Martijn Coenenea0090a2017-11-02 18:54:40 +0000823status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
824{
825 status_t err;
826 status_t statusBuffer;
827 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
828 if (err < NO_ERROR) return err;
829
Yi Kongfdd8da92018-06-07 17:52:27 -0700830 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000831}
832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
834{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100835 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 int32_t err;
837
838 while (1) {
839 if ((err=talkWithDriver()) < NO_ERROR) break;
840 err = mIn.errorCheck();
841 if (err < NO_ERROR) break;
842 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800843
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100844 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800845
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 IF_LOG_COMMANDS() {
847 alog << "Processing waitForResponse Command: "
848 << getReturnString(cmd) << endl;
849 }
850
851 switch (cmd) {
852 case BR_TRANSACTION_COMPLETE:
853 if (!reply && !acquireResult) goto finish;
854 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800855
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 case BR_DEAD_REPLY:
857 err = DEAD_OBJECT;
858 goto finish;
859
860 case BR_FAILED_REPLY:
861 err = FAILED_TRANSACTION;
862 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 case BR_ACQUIRE_RESULT:
865 {
Steve Block67263472012-01-09 18:35:44 +0000866 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 const int32_t result = mIn.readInt32();
868 if (!acquireResult) continue;
869 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
870 }
871 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 case BR_REPLY:
874 {
875 binder_transaction_data tr;
876 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000877 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 if (err != NO_ERROR) goto finish;
879
880 if (reply) {
881 if ((tr.flags & TF_STATUS_CODE) == 0) {
882 reply->ipcSetDataReference(
883 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
884 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800885 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
886 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 freeBuffer, this);
888 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800889 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700890 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
892 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800893 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
894 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 }
896 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700897 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
899 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800900 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
901 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 continue;
903 }
904 }
905 goto finish;
906
907 default:
908 err = executeCommand(cmd);
909 if (err != NO_ERROR) goto finish;
910 break;
911 }
912 }
913
914finish:
915 if (err != NO_ERROR) {
916 if (acquireResult) *acquireResult = err;
917 if (reply) reply->setError(err);
918 mLastError = err;
919 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 return err;
922}
923
924status_t IPCThreadState::talkWithDriver(bool doReceive)
925{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100926 if (mProcess->mDriverFD <= 0) {
927 return -EBADF;
928 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800931
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 // Is the read buffer empty?
933 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800934
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 // We don't want to write anything if we are still reading
936 // from data left in the input buffer and the caller
937 // has requested to read the next data.
938 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800939
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800941 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
943 // This is what we'll read.
944 if (doReceive && needRead) {
945 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800946 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 } else {
948 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800949 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700951
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 IF_LOG_COMMANDS() {
953 TextOutput::Bundle _b(alog);
954 if (outAvail != 0) {
955 alog << "Sending commands to driver: " << indent;
956 const void* cmds = (const void*)bwr.write_buffer;
957 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
958 alog << HexDump(cmds, bwr.write_size) << endl;
959 while (cmds < end) cmds = printCommand(alog, cmds);
960 alog << dedent;
961 }
962 alog << "Size of receive buffer: " << bwr.read_size
963 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
964 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800965
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 // Return immediately if there is nothing to do.
967 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700968
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 bwr.write_consumed = 0;
970 bwr.read_consumed = 0;
971 status_t err;
972 do {
973 IF_LOG_COMMANDS() {
974 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
975 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700976#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
978 err = NO_ERROR;
979 else
980 err = -errno;
981#else
982 err = INVALID_OPERATION;
983#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100984 if (mProcess->mDriverFD <= 0) {
985 err = -EBADF;
986 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 IF_LOG_COMMANDS() {
988 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
989 }
990 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700991
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800993 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700995 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 }
997
998 if (err >= NO_ERROR) {
999 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001000 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001002 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001004 processPostWriteDerefs();
1005 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 }
1007 if (bwr.read_consumed > 0) {
1008 mIn.setDataSize(bwr.read_consumed);
1009 mIn.setDataPosition(0);
1010 }
1011 IF_LOG_COMMANDS() {
1012 TextOutput::Bundle _b(alog);
1013 alog << "Remaining data size: " << mOut.dataSize() << endl;
1014 alog << "Received commands from driver: " << indent;
1015 const void* cmds = mIn.data();
1016 const void* end = mIn.data() + mIn.dataSize();
1017 alog << HexDump(cmds, mIn.dataSize()) << endl;
1018 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1019 alog << dedent;
1020 }
1021 return NO_ERROR;
1022 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 return err;
1025}
1026
1027status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1028 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1029{
1030 binder_transaction_data tr;
1031
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001032 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 tr.target.handle = handle;
1034 tr.code = code;
1035 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001036 tr.cookie = 0;
1037 tr.sender_pid = 0;
1038 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 const status_t err = data.errorCheck();
1041 if (err == NO_ERROR) {
1042 tr.data_size = data.ipcDataSize();
1043 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001044 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 tr.data.ptr.offsets = data.ipcObjects();
1046 } else if (statusBuffer) {
1047 tr.flags |= TF_STATUS_CODE;
1048 *statusBuffer = err;
1049 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001050 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001052 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 } else {
1054 return (mLastError = err);
1055 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 mOut.writeInt32(cmd);
1058 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 return NO_ERROR;
1061}
1062
1063sp<BBinder> the_context_object;
1064
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001065void IPCThreadState::setTheContextObject(sp<BBinder> obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
1067 the_context_object = obj;
1068}
1069
1070status_t IPCThreadState::executeCommand(int32_t cmd)
1071{
1072 BBinder* obj;
1073 RefBase::weakref_type* refs;
1074 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001075
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001076 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 case BR_ERROR:
1078 result = mIn.readInt32();
1079 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 case BR_OK:
1082 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001085 refs = (RefBase::weakref_type*)mIn.readPointer();
1086 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001087 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1089 refs, obj, refs->refBase());
1090 obj->incStrong(mProcess.get());
1091 IF_LOG_REMOTEREFS() {
1092 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1093 obj->printRefs();
1094 }
1095 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001096 mOut.writePointer((uintptr_t)refs);
1097 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001101 refs = (RefBase::weakref_type*)mIn.readPointer();
1102 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001103 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1105 refs, obj, refs->refBase());
1106 IF_LOG_REMOTEREFS() {
1107 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1108 obj->printRefs();
1109 }
1110 mPendingStrongDerefs.push(obj);
1111 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001114 refs = (RefBase::weakref_type*)mIn.readPointer();
1115 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 refs->incWeak(mProcess.get());
1117 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001118 mOut.writePointer((uintptr_t)refs);
1119 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001123 refs = (RefBase::weakref_type*)mIn.readPointer();
1124 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 // NOTE: This assertion is not valid, because the object may no
1126 // longer exist (thus the (BBinder*)cast above resulting in a different
1127 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001128 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1130 // refs, obj, refs->refBase());
1131 mPendingWeakDerefs.push(refs);
1132 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001135 refs = (RefBase::weakref_type*)mIn.readPointer();
1136 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 {
1139 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001140 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1142 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144 mOut.writeInt32(BC_ACQUIRE_RESULT);
1145 mOut.writeInt32((int32_t)success);
1146 }
1147 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001148
Steven Morelandf0212002018-12-26 13:59:23 -08001149 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 case BR_TRANSACTION:
1151 {
Steven Morelandf0212002018-12-26 13:59:23 -08001152 binder_transaction_data_secctx tr_secctx;
1153 binder_transaction_data& tr = tr_secctx.transaction_data;
1154
1155 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1156 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1157 } else {
1158 result = mIn.read(&tr, sizeof(tr));
1159 tr_secctx.secctx = 0;
1160 }
1161
Steve Block67263472012-01-09 18:35:44 +00001162 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 "Not enough command data for brTRANSACTION");
1164 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001165
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001166 //Record the fact that we're in a binder call.
1167 mIPCThreadStateBase->pushCurrentState(
1168 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001169 Parcel buffer;
1170 buffer.ipcSetDataReference(
1171 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1172 tr.data_size,
1173 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1174 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1175
1176 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001177 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001178 const uid_t origUid = mCallingUid;
1179 const int32_t origStrictModePolicy = mStrictModePolicy;
1180 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001181 const int32_t origWorkSource = mWorkSource;
1182 const bool origPropagateWorkSet = mPropagateWorkSource;
1183 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1184 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1185 // here to never propagate it.
1186 clearCallingWorkSource();
1187 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001188
1189 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001190 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001191 mCallingUid = tr.sender_euid;
1192 mLastTransactionBinderFlags = tr.flags;
1193
Steven Morelandf0212002018-12-26 13:59:23 -08001194 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1195 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001198 status_t error;
1199 IF_LOG_TRANSACTIONS() {
1200 TextOutput::Bundle _b(alog);
1201 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1202 << " / obj " << tr.target.ptr << " / code "
1203 << TypeCode(tr.code) << ": " << indent << buffer
1204 << dedent << endl
1205 << "Data addr = "
1206 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1207 << ", offsets addr="
1208 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1209 }
1210 if (tr.target.ptr) {
1211 // We only have a weak reference on the target object, so we must first try to
1212 // safely acquire a strong reference before doing anything else with it.
1213 if (reinterpret_cast<RefBase::weakref_type*>(
1214 tr.target.ptr)->attemptIncStrong(this)) {
1215 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1216 &reply, tr.flags);
1217 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001218 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001219 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001220 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001221
Martijn Coenenea0090a2017-11-02 18:54:40 +00001222 } else {
1223 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001225
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001226 mIPCThreadStateBase->popCurrentState();
Steven Morelandf0212002018-12-26 13:59:23 -08001227 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1228 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001229
Martijn Coenenea0090a2017-11-02 18:54:40 +00001230 if ((tr.flags & TF_ONE_WAY) == 0) {
1231 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1232 if (error < NO_ERROR) reply.setError(error);
1233 sendReply(reply, 0);
1234 } else {
1235 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1236 }
1237
1238 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001239 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001240 mCallingUid = origUid;
1241 mStrictModePolicy = origStrictModePolicy;
1242 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001243 mWorkSource = origWorkSource;
1244 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 IF_LOG_TRANSACTIONS() {
1247 TextOutput::Bundle _b(alog);
1248 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1249 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1250 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 }
1253 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 case BR_DEAD_BINDER:
1256 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001257 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 proxy->sendObituary();
1259 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001260 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1264 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001265 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 proxy->getWeakRefs()->decWeak(proxy);
1267 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 case BR_FINISHED:
1270 result = TIMED_OUT;
1271 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 case BR_NOOP:
1274 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 case BR_SPAWN_LOOPER:
1277 mProcess->spawnPooledThread(false);
1278 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 default:
liangweikanga43ee152016-10-25 16:37:54 +08001281 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 result = UNKNOWN_ERROR;
1283 break;
1284 }
1285
1286 if (result != NO_ERROR) {
1287 mLastError = result;
1288 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 return result;
1291}
1292
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001293bool IPCThreadState::isServingCall() const {
1294 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1295}
1296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297void IPCThreadState::threadDestructor(void *st)
1298{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001299 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1300 if (self) {
1301 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001302#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001303 if (self->mProcess->mDriverFD > 0) {
1304 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001307 delete self;
1308 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309}
1310
1311
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001312void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1313 size_t /*dataSize*/,
1314 const binder_size_t* /*objects*/,
1315 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316{
Steve Blocka19954a2012-01-04 20:05:49 +00001317 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 IF_LOG_COMMANDS() {
1319 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1320 }
Steve Block67263472012-01-09 18:35:44 +00001321 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001322 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 IPCThreadState* state = self();
1324 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001325 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326}
1327
1328}; // namespace android