blob: 9a561cba6482bd4a9efb3ff113d506f708bb5f06 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070037#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <pthread.h>
39#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080040#include <signal.h>
41#include <stdio.h>
42#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080044#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#if LOG_NDEBUG
47
48#define IF_LOG_TRANSACTIONS() if (false)
49#define IF_LOG_COMMANDS() if (false)
50#define LOG_REMOTEREFS(...)
51#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#define LOG_THREADPOOL(...)
54#define LOG_ONEWAY(...)
55
56#else
57
Steve Block9f760152011-10-12 17:27:03 +010058#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
59#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
60#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
61#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
62#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
63#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65#endif
66
67// ---------------------------------------------------------------------------
68
69namespace android {
70
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070071// Static const and functions will be optimized out if not used,
72// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070074 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 "BR_TRANSACTION",
77 "BR_REPLY",
78 "BR_ACQUIRE_RESULT",
79 "BR_DEAD_REPLY",
80 "BR_TRANSACTION_COMPLETE",
81 "BR_INCREFS",
82 "BR_ACQUIRE",
83 "BR_RELEASE",
84 "BR_DECREFS",
85 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 "BR_NOOP",
87 "BR_SPAWN_LOOPER",
88 "BR_FINISHED",
89 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070090 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Morelandf0212002018-12-26 13:59:23 -080091 "BR_FAILED_REPLY",
92 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093};
94
95static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 "BC_TRANSACTION",
97 "BC_REPLY",
98 "BC_ACQUIRE_RESULT",
99 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 "BC_INCREFS",
101 "BC_ACQUIRE",
102 "BC_RELEASE",
103 "BC_DECREFS",
104 "BC_INCREFS_DONE",
105 "BC_ACQUIRE_DONE",
106 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 "BC_REGISTER_LOOPER",
108 "BC_ENTER_LOOPER",
109 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 "BC_REQUEST_DEATH_NOTIFICATION",
111 "BC_CLEAR_DEATH_NOTIFICATION",
112 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113};
114
Olivier Gaillard91a04802018-11-14 17:32:41 +0000115static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100116
songjinshi73a7dde2016-10-18 21:05:56 +0800117static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118{
songjinshi73a7dde2016-10-18 21:05:56 +0800119 size_t idx = cmd & 0xff;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
121 return kReturnStrings[idx];
122 else
123 return "unknown";
124}
125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126static const void* printBinderTransactionData(TextOutput& out, const void* data)
127{
128 const binder_transaction_data* btd =
129 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700130 if (btd->target.handle < 1024) {
131 /* want to print descriptors in decimal; guess based on value */
132 out << "target.desc=" << btd->target.handle;
133 } else {
134 out << "target.ptr=" << btd->target.ptr;
135 }
136 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700137 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
139 << " bytes)" << endl
140 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700141 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 return btd+1;
143}
144
145static const void* printReturnCommand(TextOutput& out, const void* _cmd)
146{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700147 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100149 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700150 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100151 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700152 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700154 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 out << "Unknown reply: " << code << endl;
156 return cmd;
157 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700158 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 switch (code) {
161 case BR_TRANSACTION:
162 case BR_REPLY: {
163 out << ": " << indent;
164 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
165 out << dedent;
166 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 case BR_ACQUIRE_RESULT: {
169 const int32_t res = *cmd++;
170 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
171 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 case BR_INCREFS:
174 case BR_ACQUIRE:
175 case BR_RELEASE:
176 case BR_DECREFS: {
177 const int32_t b = *cmd++;
178 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700179 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 case BR_ATTEMPT_ACQUIRE: {
183 const int32_t p = *cmd++;
184 const int32_t b = *cmd++;
185 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700186 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 << "), pri=" << p;
188 } break;
189
190 case BR_DEAD_BINDER:
191 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
192 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700193 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700195
196 default:
197 // no details to show for: BR_OK, BR_DEAD_REPLY,
198 // BR_TRANSACTION_COMPLETE, BR_FINISHED
199 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 out << endl;
203 return cmd;
204}
205
206static const void* printCommand(TextOutput& out, const void* _cmd)
207{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700208 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100210 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700211 size_t cmdIndex = code & 0xff;
212
213 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 out << "Unknown command: " << code << endl;
215 return cmd;
216 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700217 out << kCommandStrings[cmdIndex];
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 switch (code) {
220 case BC_TRANSACTION:
221 case BC_REPLY: {
222 out << ": " << indent;
223 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
224 out << dedent;
225 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 case BC_ACQUIRE_RESULT: {
228 const int32_t res = *cmd++;
229 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
230 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 case BC_FREE_BUFFER: {
233 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700234 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 case BC_INCREFS:
238 case BC_ACQUIRE:
239 case BC_RELEASE:
240 case BC_DECREFS: {
241 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700242 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 case BC_INCREFS_DONE:
246 case BC_ACQUIRE_DONE: {
247 const int32_t b = *cmd++;
248 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700249 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 case BC_ATTEMPT_ACQUIRE: {
253 const int32_t p = *cmd++;
254 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700255 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 case BC_REQUEST_DEATH_NOTIFICATION:
259 case BC_CLEAR_DEATH_NOTIFICATION: {
260 const int32_t h = *cmd++;
261 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700262 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 } break;
264
265 case BC_DEAD_BINDER_DONE: {
266 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700267 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700269
270 default:
271 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
272 // BC_EXIT_LOOPER
273 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 out << endl;
277 return cmd;
278}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
280static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
281static bool gHaveTLS = false;
282static pthread_key_t gTLS = 0;
283static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800284static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285
286IPCThreadState* IPCThreadState::self()
287{
288 if (gHaveTLS) {
289restart:
290 const pthread_key_t k = gTLS;
291 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
292 if (st) return st;
293 return new IPCThreadState;
294 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800295
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800296 if (gShutdown) {
297 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700298 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800299 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 pthread_mutex_lock(&gTLSMutex);
302 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800303 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
304 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800306 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
307 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700308 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 }
310 gHaveTLS = true;
311 }
312 pthread_mutex_unlock(&gTLSMutex);
313 goto restart;
314}
315
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800316IPCThreadState* IPCThreadState::selfOrNull()
317{
318 if (gHaveTLS) {
319 const pthread_key_t k = gTLS;
320 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
321 return st;
322 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700323 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800324}
325
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326void IPCThreadState::shutdown()
327{
328 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 if (gHaveTLS) {
331 // XXX Need to wait for all thread pool threads to exit!
332 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
333 if (st) {
334 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700335 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 }
zhongjieff405782016-03-09 15:05:04 +0800337 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 gHaveTLS = false;
339 }
340}
341
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800342void IPCThreadState::disableBackgroundScheduling(bool disable)
343{
344 gDisableBackgroundScheduling = disable;
345}
346
Martijn Coenen2b631742017-05-05 11:16:59 -0700347bool IPCThreadState::backgroundSchedulingDisabled()
348{
349 return gDisableBackgroundScheduling;
350}
351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352sp<ProcessState> IPCThreadState::process()
353{
354 return mProcess;
355}
356
357status_t IPCThreadState::clearLastError()
358{
359 const status_t err = mLastError;
360 mLastError = NO_ERROR;
361 return err;
362}
363
Dan Stoza9c634fd2014-11-26 12:23:23 -0800364pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365{
366 return mCallingPid;
367}
368
Steven Morelandf0212002018-12-26 13:59:23 -0800369const char* IPCThreadState::getCallingSid() const
370{
371 return mCallingSid;
372}
373
Dan Stoza9c634fd2014-11-26 12:23:23 -0800374uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375{
376 return mCallingUid;
377}
378
379int64_t IPCThreadState::clearCallingIdentity()
380{
Steven Morelandf0212002018-12-26 13:59:23 -0800381 // ignore mCallingSid for legacy reasons
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
383 clearCaller();
384 return token;
385}
386
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700387void IPCThreadState::setStrictModePolicy(int32_t policy)
388{
389 mStrictModePolicy = policy;
390}
391
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700392int32_t IPCThreadState::getStrictModePolicy() const
393{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700394 return mStrictModePolicy;
395}
396
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000397int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100398{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000399 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
400 mPropagateWorkSource = true;
401 return token;
402}
403
404int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
405{
406 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
407 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100408 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000409 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100410}
411
Olivier Gaillard91a04802018-11-14 17:32:41 +0000412void IPCThreadState::clearPropagateWorkSource()
413{
414 mPropagateWorkSource = false;
415}
416
417bool IPCThreadState::shouldPropagateWorkSource() const
418{
419 return mPropagateWorkSource;
420}
421
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000422uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100423{
424 return mWorkSource;
425}
426
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000427int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100428{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000429 return setCallingWorkSourceUid(kUnsetWorkSource);
430}
431
432void IPCThreadState::restoreCallingWorkSource(int64_t token)
433{
434 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000435 setCallingWorkSourceUidWithoutPropagation(uid);
436 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100437}
438
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700439void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
440{
441 mLastTransactionBinderFlags = flags;
442}
443
444int32_t IPCThreadState::getLastTransactionBinderFlags() const
445{
446 return mLastTransactionBinderFlags;
447}
448
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449void IPCThreadState::restoreCallingIdentity(int64_t token)
450{
451 mCallingUid = (int)(token>>32);
Steven Morelandf0212002018-12-26 13:59:23 -0800452 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 mCallingPid = (int)token;
454}
455
456void IPCThreadState::clearCaller()
457{
Marco Nelissend43b1942009-07-17 07:59:17 -0700458 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800459 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700460 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461}
462
463void IPCThreadState::flushCommands()
464{
465 if (mProcess->mDriverFD <= 0)
466 return;
467 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700468 // The flush could have caused post-write refcount decrements to have
469 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
470 // being queued in mOut. So flush again, if we need to.
471 if (mOut.dataSize() > 0) {
472 talkWithDriver(false);
473 }
474 if (mOut.dataSize() > 0) {
475 ALOGW("mOut.dataSize() > 0 after flushCommands()");
476 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477}
478
Wale Ogunwale376b8222015-04-13 16:16:10 -0700479void IPCThreadState::blockUntilThreadAvailable()
480{
481 pthread_mutex_lock(&mProcess->mThreadCountLock);
482 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700483 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
484 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
485 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700486 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
487 }
488 pthread_mutex_unlock(&mProcess->mThreadCountLock);
489}
490
Todd Poynor8d96cab2013-06-25 19:12:18 -0700491status_t IPCThreadState::getAndExecuteCommand()
492{
493 status_t result;
494 int32_t cmd;
495
496 result = talkWithDriver();
497 if (result >= NO_ERROR) {
498 size_t IN = mIn.dataAvail();
499 if (IN < sizeof(int32_t)) return result;
500 cmd = mIn.readInt32();
501 IF_LOG_COMMANDS() {
502 alog << "Processing top-level Command: "
503 << getReturnString(cmd) << endl;
504 }
505
Wale Ogunwale376b8222015-04-13 16:16:10 -0700506 pthread_mutex_lock(&mProcess->mThreadCountLock);
507 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700508 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
509 mProcess->mStarvationStartTimeMs == 0) {
510 mProcess->mStarvationStartTimeMs = uptimeMillis();
511 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700512 pthread_mutex_unlock(&mProcess->mThreadCountLock);
513
Todd Poynor8d96cab2013-06-25 19:12:18 -0700514 result = executeCommand(cmd);
515
Wale Ogunwale376b8222015-04-13 16:16:10 -0700516 pthread_mutex_lock(&mProcess->mThreadCountLock);
517 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700518 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
519 mProcess->mStarvationStartTimeMs != 0) {
520 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
521 if (starvationTimeMs > 100) {
522 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
523 mProcess->mMaxThreads, starvationTimeMs);
524 }
525 mProcess->mStarvationStartTimeMs = 0;
526 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700527 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
528 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700529 }
530
531 return result;
532}
533
534// When we've cleared the incoming command queue, process any pending derefs
535void IPCThreadState::processPendingDerefs()
536{
537 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200538 /*
539 * The decWeak()/decStrong() calls may cause a destructor to run,
540 * which in turn could have initiated an outgoing transaction,
541 * which in turn could cause us to add to the pending refs
542 * vectors; so instead of simply iterating, loop until they're empty.
543 *
544 * We do this in an outer loop, because calling decStrong()
545 * may result in something being added to mPendingWeakDerefs,
546 * which could be delayed until the next incoming command
547 * from the driver if we don't process it now.
548 */
549 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
550 while (mPendingWeakDerefs.size() > 0) {
551 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
552 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700553 refs->decWeak(mProcess.get());
554 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700555
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200556 if (mPendingStrongDerefs.size() > 0) {
557 // We don't use while() here because we don't want to re-order
558 // strong and weak decs at all; if this decStrong() causes both a
559 // decWeak() and a decStrong() to be queued, we want to process
560 // the decWeak() first.
561 BBinder* obj = mPendingStrongDerefs[0];
562 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700563 obj->decStrong(mProcess.get());
564 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700565 }
566 }
567}
568
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700569void IPCThreadState::processPostWriteDerefs()
570{
571 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
572 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
573 refs->decWeak(mProcess.get());
574 }
575 mPostWriteWeakDerefs.clear();
576
577 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
578 RefBase* obj = mPostWriteStrongDerefs[i];
579 obj->decStrong(mProcess.get());
580 }
581 mPostWriteStrongDerefs.clear();
582}
583
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584void IPCThreadState::joinThreadPool(bool isMain)
585{
586 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
587
588 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800589
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 status_t result;
591 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700592 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700594 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800595
Todd Poynor8d96cab2013-06-25 19:12:18 -0700596 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
597 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700598 mProcess->mDriverFD, result);
599 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800601
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 // Let this thread exit the thread pool if it is no longer
603 // needed and it is not the main process thread.
604 if(result == TIMED_OUT && !isMain) {
605 break;
606 }
607 } while (result != -ECONNREFUSED && result != -EBADF);
608
Wei Wangc7341432016-10-19 10:23:59 -0700609 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
610 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800611
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 mOut.writeInt32(BC_EXIT_LOOPER);
613 talkWithDriver(false);
614}
615
Todd Poynor8d96cab2013-06-25 19:12:18 -0700616int IPCThreadState::setupPolling(int* fd)
617{
618 if (mProcess->mDriverFD <= 0) {
619 return -EBADF;
620 }
621
622 mOut.writeInt32(BC_ENTER_LOOPER);
623 *fd = mProcess->mDriverFD;
624 return 0;
625}
626
627status_t IPCThreadState::handlePolledCommands()
628{
629 status_t result;
630
631 do {
632 result = getAndExecuteCommand();
633 } while (mIn.dataPosition() < mIn.dataSize());
634
635 processPendingDerefs();
636 flushCommands();
637 return result;
638}
639
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800640void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641{
Steve Blocka19954a2012-01-04 20:05:49 +0000642 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 flushCommands();
644 int fd = mProcess->mDriverFD;
645 mProcess->mDriverFD = -1;
646 close(fd);
647 //kill(getpid(), SIGKILL);
648}
649
650status_t IPCThreadState::transact(int32_t handle,
651 uint32_t code, const Parcel& data,
652 Parcel* reply, uint32_t flags)
653{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800654 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
656 flags |= TF_ACCEPT_FDS;
657
658 IF_LOG_TRANSACTIONS() {
659 TextOutput::Bundle _b(alog);
660 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
661 << handle << " / code " << TypeCode(code) << ": "
662 << indent << data << dedent << endl;
663 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800664
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800665 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
666 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700667 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800668
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 if (err != NO_ERROR) {
670 if (reply) reply->setError(err);
671 return (mLastError = err);
672 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800673
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800675 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
676 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
677 ALOGE("Process making non-oneway call but is restricted.");
678 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
679 ANDROID_LOG_ERROR);
680 } else /* FATAL_IF_NOT_ONEWAY */ {
681 LOG_ALWAYS_FATAL("Process may not make oneway calls.");
682 }
683 }
684
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700685 #if 0
686 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000687 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700688 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000689 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700690 }
691 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 if (reply) {
693 err = waitForResponse(reply);
694 } else {
695 Parcel fakeReply;
696 err = waitForResponse(&fakeReply);
697 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700698 #if 0
699 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000700 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700701 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000702 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700703 }
704 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 IF_LOG_TRANSACTIONS() {
707 TextOutput::Bundle _b(alog);
708 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
709 << handle << ": ";
710 if (reply) alog << indent << *reply << dedent << endl;
711 else alog << "(none requested)" << endl;
712 }
713 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700714 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717 return err;
718}
719
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700720void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721{
722 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
723 mOut.writeInt32(BC_ACQUIRE);
724 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700725 // Create a temp reference until the driver has handled this command.
726 proxy->incStrong(mProcess.get());
727 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728}
729
730void IPCThreadState::decStrongHandle(int32_t handle)
731{
732 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
733 mOut.writeInt32(BC_RELEASE);
734 mOut.writeInt32(handle);
735}
736
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700737void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738{
739 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
740 mOut.writeInt32(BC_INCREFS);
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->getWeakRefs()->incWeak(mProcess.get());
744 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
747void IPCThreadState::decWeakHandle(int32_t handle)
748{
749 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
750 mOut.writeInt32(BC_DECREFS);
751 mOut.writeInt32(handle);
752}
753
754status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
755{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800756#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700757 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
759 mOut.writeInt32(0); // xxx was thread priority
760 mOut.writeInt32(handle);
761 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800762
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800766 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
768#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800769
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800771#else
772 (void)handle;
773 ALOGE("%s(%d): Not supported\n", __func__, handle);
774 return INVALID_OPERATION;
775#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776}
777
778void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
779{
780#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800781 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700783 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784}
785
786status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
787{
788 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
789 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000790 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 return NO_ERROR;
792}
793
794status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
795{
796 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
797 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000798 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 return NO_ERROR;
800}
801
802IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700803 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100804 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000805 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700806 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800807 mLastTransactionBinderFlags(0),
808 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809{
810 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800811 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 mIn.setDataCapacity(256);
813 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000814 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815}
816
817IPCThreadState::~IPCThreadState()
818{
819}
820
Martijn Coenenea0090a2017-11-02 18:54:40 +0000821status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
822{
823 status_t err;
824 status_t statusBuffer;
825 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
826 if (err < NO_ERROR) return err;
827
Yi Kongfdd8da92018-06-07 17:52:27 -0700828 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000829}
830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
832{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100833 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 int32_t err;
835
836 while (1) {
837 if ((err=talkWithDriver()) < NO_ERROR) break;
838 err = mIn.errorCheck();
839 if (err < NO_ERROR) break;
840 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800841
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100842 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800843
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 IF_LOG_COMMANDS() {
845 alog << "Processing waitForResponse Command: "
846 << getReturnString(cmd) << endl;
847 }
848
849 switch (cmd) {
850 case BR_TRANSACTION_COMPLETE:
851 if (!reply && !acquireResult) goto finish;
852 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800853
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 case BR_DEAD_REPLY:
855 err = DEAD_OBJECT;
856 goto finish;
857
858 case BR_FAILED_REPLY:
859 err = FAILED_TRANSACTION;
860 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 case BR_ACQUIRE_RESULT:
863 {
Steve Block67263472012-01-09 18:35:44 +0000864 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 const int32_t result = mIn.readInt32();
866 if (!acquireResult) continue;
867 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
868 }
869 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800870
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 case BR_REPLY:
872 {
873 binder_transaction_data tr;
874 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000875 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 if (err != NO_ERROR) goto finish;
877
878 if (reply) {
879 if ((tr.flags & TF_STATUS_CODE) == 0) {
880 reply->ipcSetDataReference(
881 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
882 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800883 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
884 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 freeBuffer, this);
886 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800887 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700888 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
890 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800891 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
892 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 }
894 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700895 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
897 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800898 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
899 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900 continue;
901 }
902 }
903 goto finish;
904
905 default:
906 err = executeCommand(cmd);
907 if (err != NO_ERROR) goto finish;
908 break;
909 }
910 }
911
912finish:
913 if (err != NO_ERROR) {
914 if (acquireResult) *acquireResult = err;
915 if (reply) reply->setError(err);
916 mLastError = err;
917 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800918
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 return err;
920}
921
922status_t IPCThreadState::talkWithDriver(bool doReceive)
923{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100924 if (mProcess->mDriverFD <= 0) {
925 return -EBADF;
926 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 // Is the read buffer empty?
931 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800932
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 // We don't want to write anything if we are still reading
934 // from data left in the input buffer and the caller
935 // has requested to read the next data.
936 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800939 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940
941 // This is what we'll read.
942 if (doReceive && needRead) {
943 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800944 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 } else {
946 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800947 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700949
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 IF_LOG_COMMANDS() {
951 TextOutput::Bundle _b(alog);
952 if (outAvail != 0) {
953 alog << "Sending commands to driver: " << indent;
954 const void* cmds = (const void*)bwr.write_buffer;
955 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
956 alog << HexDump(cmds, bwr.write_size) << endl;
957 while (cmds < end) cmds = printCommand(alog, cmds);
958 alog << dedent;
959 }
960 alog << "Size of receive buffer: " << bwr.read_size
961 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
962 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800963
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964 // Return immediately if there is nothing to do.
965 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 bwr.write_consumed = 0;
968 bwr.read_consumed = 0;
969 status_t err;
970 do {
971 IF_LOG_COMMANDS() {
972 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
973 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700974#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
976 err = NO_ERROR;
977 else
978 err = -errno;
979#else
980 err = INVALID_OPERATION;
981#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100982 if (mProcess->mDriverFD <= 0) {
983 err = -EBADF;
984 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 IF_LOG_COMMANDS() {
986 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
987 }
988 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700989
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800991 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700993 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 }
995
996 if (err >= NO_ERROR) {
997 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800998 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001000 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001002 processPostWriteDerefs();
1003 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 }
1005 if (bwr.read_consumed > 0) {
1006 mIn.setDataSize(bwr.read_consumed);
1007 mIn.setDataPosition(0);
1008 }
1009 IF_LOG_COMMANDS() {
1010 TextOutput::Bundle _b(alog);
1011 alog << "Remaining data size: " << mOut.dataSize() << endl;
1012 alog << "Received commands from driver: " << indent;
1013 const void* cmds = mIn.data();
1014 const void* end = mIn.data() + mIn.dataSize();
1015 alog << HexDump(cmds, mIn.dataSize()) << endl;
1016 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1017 alog << dedent;
1018 }
1019 return NO_ERROR;
1020 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 return err;
1023}
1024
1025status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1026 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1027{
1028 binder_transaction_data tr;
1029
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001030 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 tr.target.handle = handle;
1032 tr.code = code;
1033 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001034 tr.cookie = 0;
1035 tr.sender_pid = 0;
1036 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 const status_t err = data.errorCheck();
1039 if (err == NO_ERROR) {
1040 tr.data_size = data.ipcDataSize();
1041 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001042 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 tr.data.ptr.offsets = data.ipcObjects();
1044 } else if (statusBuffer) {
1045 tr.flags |= TF_STATUS_CODE;
1046 *statusBuffer = err;
1047 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001048 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001050 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 } else {
1052 return (mLastError = err);
1053 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001054
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 mOut.writeInt32(cmd);
1056 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 return NO_ERROR;
1059}
1060
1061sp<BBinder> the_context_object;
1062
1063void setTheContextObject(sp<BBinder> obj)
1064{
1065 the_context_object = obj;
1066}
1067
1068status_t IPCThreadState::executeCommand(int32_t cmd)
1069{
1070 BBinder* obj;
1071 RefBase::weakref_type* refs;
1072 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001073
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001074 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 case BR_ERROR:
1076 result = mIn.readInt32();
1077 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 case BR_OK:
1080 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001083 refs = (RefBase::weakref_type*)mIn.readPointer();
1084 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001085 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1087 refs, obj, refs->refBase());
1088 obj->incStrong(mProcess.get());
1089 IF_LOG_REMOTEREFS() {
1090 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1091 obj->printRefs();
1092 }
1093 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001094 mOut.writePointer((uintptr_t)refs);
1095 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001099 refs = (RefBase::weakref_type*)mIn.readPointer();
1100 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001101 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1103 refs, obj, refs->refBase());
1104 IF_LOG_REMOTEREFS() {
1105 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1106 obj->printRefs();
1107 }
1108 mPendingStrongDerefs.push(obj);
1109 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001112 refs = (RefBase::weakref_type*)mIn.readPointer();
1113 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 refs->incWeak(mProcess.get());
1115 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001116 mOut.writePointer((uintptr_t)refs);
1117 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001121 refs = (RefBase::weakref_type*)mIn.readPointer();
1122 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 // NOTE: This assertion is not valid, because the object may no
1124 // longer exist (thus the (BBinder*)cast above resulting in a different
1125 // memory address).
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_DECREFS: object %p does not match cookie %p (expected %p)",
1128 // refs, obj, refs->refBase());
1129 mPendingWeakDerefs.push(refs);
1130 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001133 refs = (RefBase::weakref_type*)mIn.readPointer();
1134 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 {
1137 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001138 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1140 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 mOut.writeInt32(BC_ACQUIRE_RESULT);
1143 mOut.writeInt32((int32_t)success);
1144 }
1145 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001146
Steven Morelandf0212002018-12-26 13:59:23 -08001147 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 case BR_TRANSACTION:
1149 {
Steven Morelandf0212002018-12-26 13:59:23 -08001150 binder_transaction_data_secctx tr_secctx;
1151 binder_transaction_data& tr = tr_secctx.transaction_data;
1152
1153 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1154 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1155 } else {
1156 result = mIn.read(&tr, sizeof(tr));
1157 tr_secctx.secctx = 0;
1158 }
1159
Steve Block67263472012-01-09 18:35:44 +00001160 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 "Not enough command data for brTRANSACTION");
1162 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001163
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001164 //Record the fact that we're in a binder call.
1165 mIPCThreadStateBase->pushCurrentState(
1166 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001167 Parcel buffer;
1168 buffer.ipcSetDataReference(
1169 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1170 tr.data_size,
1171 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1172 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1173
1174 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001175 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001176 const uid_t origUid = mCallingUid;
1177 const int32_t origStrictModePolicy = mStrictModePolicy;
1178 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001179 const int32_t origWorkSource = mWorkSource;
1180 const bool origPropagateWorkSet = mPropagateWorkSource;
1181 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1182 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1183 // here to never propagate it.
1184 clearCallingWorkSource();
1185 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001186
1187 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001188 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001189 mCallingUid = tr.sender_euid;
1190 mLastTransactionBinderFlags = tr.flags;
1191
Steven Morelandf0212002018-12-26 13:59:23 -08001192 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1193 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001196 status_t error;
1197 IF_LOG_TRANSACTIONS() {
1198 TextOutput::Bundle _b(alog);
1199 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1200 << " / obj " << tr.target.ptr << " / code "
1201 << TypeCode(tr.code) << ": " << indent << buffer
1202 << dedent << endl
1203 << "Data addr = "
1204 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1205 << ", offsets addr="
1206 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1207 }
1208 if (tr.target.ptr) {
1209 // We only have a weak reference on the target object, so we must first try to
1210 // safely acquire a strong reference before doing anything else with it.
1211 if (reinterpret_cast<RefBase::weakref_type*>(
1212 tr.target.ptr)->attemptIncStrong(this)) {
1213 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1214 &reply, tr.flags);
1215 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001216 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001217 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001218 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001219
Martijn Coenenea0090a2017-11-02 18:54:40 +00001220 } else {
1221 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001223
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001224 mIPCThreadStateBase->popCurrentState();
Steven Morelandf0212002018-12-26 13:59:23 -08001225 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1226 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001227
Martijn Coenenea0090a2017-11-02 18:54:40 +00001228 if ((tr.flags & TF_ONE_WAY) == 0) {
1229 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1230 if (error < NO_ERROR) reply.setError(error);
1231 sendReply(reply, 0);
1232 } else {
1233 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1234 }
1235
1236 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001237 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001238 mCallingUid = origUid;
1239 mStrictModePolicy = origStrictModePolicy;
1240 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001241 mWorkSource = origWorkSource;
1242 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 IF_LOG_TRANSACTIONS() {
1245 TextOutput::Bundle _b(alog);
1246 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1247 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1248 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 }
1251 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 case BR_DEAD_BINDER:
1254 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001255 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 proxy->sendObituary();
1257 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001258 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1262 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001263 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 proxy->getWeakRefs()->decWeak(proxy);
1265 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001266
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 case BR_FINISHED:
1268 result = TIMED_OUT;
1269 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 case BR_NOOP:
1272 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 case BR_SPAWN_LOOPER:
1275 mProcess->spawnPooledThread(false);
1276 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 default:
liangweikanga43ee152016-10-25 16:37:54 +08001279 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 result = UNKNOWN_ERROR;
1281 break;
1282 }
1283
1284 if (result != NO_ERROR) {
1285 mLastError = result;
1286 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 return result;
1289}
1290
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001291bool IPCThreadState::isServingCall() const {
1292 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1293}
1294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295void IPCThreadState::threadDestructor(void *st)
1296{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001297 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1298 if (self) {
1299 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001300#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001301 if (self->mProcess->mDriverFD > 0) {
1302 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1303 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001305 delete self;
1306 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307}
1308
1309
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001310void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1311 size_t /*dataSize*/,
1312 const binder_size_t* /*objects*/,
1313 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314{
Steve Blocka19954a2012-01-04 20:05:49 +00001315 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 IF_LOG_COMMANDS() {
1317 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1318 }
Steve Block67263472012-01-09 18:35:44 +00001319 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001320 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 IPCThreadState* state = self();
1322 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001323 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324}
1325
1326}; // namespace android