blob: 7c45c775c7f19814e225d28e30985df60958a620 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
Hans Boehma997b232019-04-12 16:59:00 -070035#include <atomic>
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
Steven Morelanda4853cd2019-07-12 15:44:37 -070046#include "Static.h"
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#if LOG_NDEBUG
49
50#define IF_LOG_TRANSACTIONS() if (false)
51#define IF_LOG_COMMANDS() if (false)
52#define LOG_REMOTEREFS(...)
53#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080054
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#define LOG_THREADPOOL(...)
56#define LOG_ONEWAY(...)
57
58#else
59
Steve Block9f760152011-10-12 17:27:03 +010060#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
61#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
62#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
63#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
64#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
65#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
67#endif
68
69// ---------------------------------------------------------------------------
70
71namespace android {
72
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070073// Static const and functions will be optimized out if not used,
74// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070076 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 "BR_TRANSACTION",
79 "BR_REPLY",
80 "BR_ACQUIRE_RESULT",
81 "BR_DEAD_REPLY",
82 "BR_TRANSACTION_COMPLETE",
83 "BR_INCREFS",
84 "BR_ACQUIRE",
85 "BR_RELEASE",
86 "BR_DECREFS",
87 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 "BR_NOOP",
89 "BR_SPAWN_LOOPER",
90 "BR_FINISHED",
91 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070092 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Morelandf0212002018-12-26 13:59:23 -080093 "BR_FAILED_REPLY",
94 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095};
96
97static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 "BC_TRANSACTION",
99 "BC_REPLY",
100 "BC_ACQUIRE_RESULT",
101 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 "BC_INCREFS",
103 "BC_ACQUIRE",
104 "BC_RELEASE",
105 "BC_DECREFS",
106 "BC_INCREFS_DONE",
107 "BC_ACQUIRE_DONE",
108 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 "BC_REGISTER_LOOPER",
110 "BC_ENTER_LOOPER",
111 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 "BC_REQUEST_DEATH_NOTIFICATION",
113 "BC_CLEAR_DEATH_NOTIFICATION",
114 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115};
116
Olivier Gaillard91a04802018-11-14 17:32:41 +0000117static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100118
songjinshi73a7dde2016-10-18 21:05:56 +0800119static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120{
songjinshi8e486c62019-04-04 11:22:52 +0800121 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
123 return kReturnStrings[idx];
124 else
125 return "unknown";
126}
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128static const void* printBinderTransactionData(TextOutput& out, const void* data)
129{
130 const binder_transaction_data* btd =
131 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700132 if (btd->target.handle < 1024) {
133 /* want to print descriptors in decimal; guess based on value */
134 out << "target.desc=" << btd->target.handle;
135 } else {
136 out << "target.ptr=" << btd->target.ptr;
137 }
138 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700139 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
141 << " bytes)" << endl
142 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700143 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 return btd+1;
145}
146
147static const void* printReturnCommand(TextOutput& out, const void* _cmd)
148{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700149 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100151 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700152 size_t cmdIndex = code & 0xff;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100153 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700154 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700156 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 out << "Unknown reply: " << code << endl;
158 return cmd;
159 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700160 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 switch (code) {
163 case BR_TRANSACTION:
164 case BR_REPLY: {
165 out << ": " << indent;
166 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
167 out << dedent;
168 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 case BR_ACQUIRE_RESULT: {
171 const int32_t res = *cmd++;
172 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
173 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 case BR_INCREFS:
176 case BR_ACQUIRE:
177 case BR_RELEASE:
178 case BR_DECREFS: {
179 const int32_t b = *cmd++;
180 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700181 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 case BR_ATTEMPT_ACQUIRE: {
185 const int32_t p = *cmd++;
186 const int32_t b = *cmd++;
187 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700188 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 << "), pri=" << p;
190 } break;
191
192 case BR_DEAD_BINDER:
193 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
194 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700195 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700197
198 default:
199 // no details to show for: BR_OK, BR_DEAD_REPLY,
200 // BR_TRANSACTION_COMPLETE, BR_FINISHED
201 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 out << endl;
205 return cmd;
206}
207
208static const void* printCommand(TextOutput& out, const void* _cmd)
209{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700210 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100212 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700213 size_t cmdIndex = code & 0xff;
214
215 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 out << "Unknown command: " << code << endl;
217 return cmd;
218 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700219 out << kCommandStrings[cmdIndex];
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 switch (code) {
222 case BC_TRANSACTION:
223 case BC_REPLY: {
224 out << ": " << indent;
225 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
226 out << dedent;
227 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 case BC_ACQUIRE_RESULT: {
230 const int32_t res = *cmd++;
231 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
232 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 case BC_FREE_BUFFER: {
235 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700236 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 case BC_INCREFS:
240 case BC_ACQUIRE:
241 case BC_RELEASE:
242 case BC_DECREFS: {
243 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700244 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 case BC_INCREFS_DONE:
248 case BC_ACQUIRE_DONE: {
249 const int32_t b = *cmd++;
250 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700251 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 case BC_ATTEMPT_ACQUIRE: {
255 const int32_t p = *cmd++;
256 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700257 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 case BC_REQUEST_DEATH_NOTIFICATION:
261 case BC_CLEAR_DEATH_NOTIFICATION: {
262 const int32_t h = *cmd++;
263 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700264 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 } break;
266
267 case BC_DEAD_BINDER_DONE: {
268 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700269 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700271
272 default:
273 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
274 // BC_EXIT_LOOPER
275 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 out << endl;
279 return cmd;
280}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
282static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
Hans Boehma997b232019-04-12 16:59:00 -0700283static std::atomic<bool> gHaveTLS(false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284static pthread_key_t gTLS = 0;
Hans Boehma997b232019-04-12 16:59:00 -0700285static std::atomic<bool> gShutdown = false;
286static std::atomic<bool> gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288IPCThreadState* IPCThreadState::self()
289{
Hans Boehma997b232019-04-12 16:59:00 -0700290 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291restart:
292 const pthread_key_t k = gTLS;
293 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
294 if (st) return st;
295 return new IPCThreadState;
296 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800297
Hans Boehma997b232019-04-12 16:59:00 -0700298 // Racey, heuristic test for simultaneous shutdown.
299 if (gShutdown.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800300 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700301 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800302 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 pthread_mutex_lock(&gTLSMutex);
Hans Boehma997b232019-04-12 16:59:00 -0700305 if (!gHaveTLS.load(std::memory_order_relaxed)) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800306 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
307 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800309 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
310 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700311 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 }
Hans Boehma997b232019-04-12 16:59:00 -0700313 gHaveTLS.store(true, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 }
315 pthread_mutex_unlock(&gTLSMutex);
316 goto restart;
317}
318
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800319IPCThreadState* IPCThreadState::selfOrNull()
320{
Hans Boehma997b232019-04-12 16:59:00 -0700321 if (gHaveTLS.load(std::memory_order_acquire)) {
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800322 const pthread_key_t k = gTLS;
323 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
324 return st;
325 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700326 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800327}
328
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329void IPCThreadState::shutdown()
330{
Hans Boehma997b232019-04-12 16:59:00 -0700331 gShutdown.store(true, std::memory_order_relaxed);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800332
Hans Boehma997b232019-04-12 16:59:00 -0700333 if (gHaveTLS.load(std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 // XXX Need to wait for all thread pool threads to exit!
335 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
336 if (st) {
337 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700338 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 }
zhongjieff405782016-03-09 15:05:04 +0800340 pthread_key_delete(gTLS);
Hans Boehma997b232019-04-12 16:59:00 -0700341 gHaveTLS.store(false, std::memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 }
343}
344
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800345void IPCThreadState::disableBackgroundScheduling(bool disable)
346{
Hans Boehma997b232019-04-12 16:59:00 -0700347 gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800348}
349
Martijn Coenen2b631742017-05-05 11:16:59 -0700350bool IPCThreadState::backgroundSchedulingDisabled()
351{
Hans Boehma997b232019-04-12 16:59:00 -0700352 return gDisableBackgroundScheduling.load(std::memory_order_relaxed);
Martijn Coenen2b631742017-05-05 11:16:59 -0700353}
354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355sp<ProcessState> IPCThreadState::process()
356{
357 return mProcess;
358}
359
360status_t IPCThreadState::clearLastError()
361{
362 const status_t err = mLastError;
363 mLastError = NO_ERROR;
364 return err;
365}
366
Dan Stoza9c634fd2014-11-26 12:23:23 -0800367pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368{
369 return mCallingPid;
370}
371
Steven Morelandf0212002018-12-26 13:59:23 -0800372const char* IPCThreadState::getCallingSid() const
373{
374 return mCallingSid;
375}
376
Dan Stoza9c634fd2014-11-26 12:23:23 -0800377uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378{
379 return mCallingUid;
380}
381
382int64_t IPCThreadState::clearCallingIdentity()
383{
Steven Morelandf0212002018-12-26 13:59:23 -0800384 // ignore mCallingSid for legacy reasons
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
386 clearCaller();
387 return token;
388}
389
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700390void IPCThreadState::setStrictModePolicy(int32_t policy)
391{
392 mStrictModePolicy = policy;
393}
394
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700395int32_t IPCThreadState::getStrictModePolicy() const
396{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700397 return mStrictModePolicy;
398}
399
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000400int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100401{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000402 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
403 mPropagateWorkSource = true;
404 return token;
405}
406
407int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
408{
409 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
410 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100411 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000412 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100413}
414
Olivier Gaillard91a04802018-11-14 17:32:41 +0000415void IPCThreadState::clearPropagateWorkSource()
416{
417 mPropagateWorkSource = false;
418}
419
420bool IPCThreadState::shouldPropagateWorkSource() const
421{
422 return mPropagateWorkSource;
423}
424
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000425uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100426{
427 return mWorkSource;
428}
429
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000430int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100431{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000432 return setCallingWorkSourceUid(kUnsetWorkSource);
433}
434
435void IPCThreadState::restoreCallingWorkSource(int64_t token)
436{
437 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000438 setCallingWorkSourceUidWithoutPropagation(uid);
439 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100440}
441
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700442void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
443{
444 mLastTransactionBinderFlags = flags;
445}
446
447int32_t IPCThreadState::getLastTransactionBinderFlags() const
448{
449 return mLastTransactionBinderFlags;
450}
451
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452void IPCThreadState::restoreCallingIdentity(int64_t token)
453{
454 mCallingUid = (int)(token>>32);
Steven Morelandf0212002018-12-26 13:59:23 -0800455 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 mCallingPid = (int)token;
457}
458
459void IPCThreadState::clearCaller()
460{
Marco Nelissend43b1942009-07-17 07:59:17 -0700461 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800462 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700463 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
466void IPCThreadState::flushCommands()
467{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200468 if (mProcess->mDriverFD < 0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 return;
470 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700471 // The flush could have caused post-write refcount decrements to have
472 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
473 // being queued in mOut. So flush again, if we need to.
474 if (mOut.dataSize() > 0) {
475 talkWithDriver(false);
476 }
477 if (mOut.dataSize() > 0) {
478 ALOGW("mOut.dataSize() > 0 after flushCommands()");
479 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480}
481
Wale Ogunwale376b8222015-04-13 16:16:10 -0700482void IPCThreadState::blockUntilThreadAvailable()
483{
484 pthread_mutex_lock(&mProcess->mThreadCountLock);
485 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700486 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
487 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
488 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700489 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
490 }
491 pthread_mutex_unlock(&mProcess->mThreadCountLock);
492}
493
Todd Poynor8d96cab2013-06-25 19:12:18 -0700494status_t IPCThreadState::getAndExecuteCommand()
495{
496 status_t result;
497 int32_t cmd;
498
499 result = talkWithDriver();
500 if (result >= NO_ERROR) {
501 size_t IN = mIn.dataAvail();
502 if (IN < sizeof(int32_t)) return result;
503 cmd = mIn.readInt32();
504 IF_LOG_COMMANDS() {
505 alog << "Processing top-level Command: "
506 << getReturnString(cmd) << endl;
507 }
508
Wale Ogunwale376b8222015-04-13 16:16:10 -0700509 pthread_mutex_lock(&mProcess->mThreadCountLock);
510 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700511 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
512 mProcess->mStarvationStartTimeMs == 0) {
513 mProcess->mStarvationStartTimeMs = uptimeMillis();
514 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700515 pthread_mutex_unlock(&mProcess->mThreadCountLock);
516
Todd Poynor8d96cab2013-06-25 19:12:18 -0700517 result = executeCommand(cmd);
518
Wale Ogunwale376b8222015-04-13 16:16:10 -0700519 pthread_mutex_lock(&mProcess->mThreadCountLock);
520 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700521 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
522 mProcess->mStarvationStartTimeMs != 0) {
523 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
524 if (starvationTimeMs > 100) {
525 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
526 mProcess->mMaxThreads, starvationTimeMs);
527 }
528 mProcess->mStarvationStartTimeMs = 0;
529 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700530 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
531 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700532 }
533
534 return result;
535}
536
537// When we've cleared the incoming command queue, process any pending derefs
538void IPCThreadState::processPendingDerefs()
539{
540 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200541 /*
542 * The decWeak()/decStrong() calls may cause a destructor to run,
543 * which in turn could have initiated an outgoing transaction,
544 * which in turn could cause us to add to the pending refs
545 * vectors; so instead of simply iterating, loop until they're empty.
546 *
547 * We do this in an outer loop, because calling decStrong()
548 * may result in something being added to mPendingWeakDerefs,
549 * which could be delayed until the next incoming command
550 * from the driver if we don't process it now.
551 */
552 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
553 while (mPendingWeakDerefs.size() > 0) {
554 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
555 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700556 refs->decWeak(mProcess.get());
557 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700558
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200559 if (mPendingStrongDerefs.size() > 0) {
560 // We don't use while() here because we don't want to re-order
561 // strong and weak decs at all; if this decStrong() causes both a
562 // decWeak() and a decStrong() to be queued, we want to process
563 // the decWeak() first.
564 BBinder* obj = mPendingStrongDerefs[0];
565 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700566 obj->decStrong(mProcess.get());
567 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700568 }
569 }
570}
571
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700572void IPCThreadState::processPostWriteDerefs()
573{
574 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
575 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
576 refs->decWeak(mProcess.get());
577 }
578 mPostWriteWeakDerefs.clear();
579
580 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
581 RefBase* obj = mPostWriteStrongDerefs[i];
582 obj->decStrong(mProcess.get());
583 }
584 mPostWriteStrongDerefs.clear();
585}
586
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587void IPCThreadState::joinThreadPool(bool isMain)
588{
589 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
590
591 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800592
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 status_t result;
594 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700595 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700597 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800598
Todd Poynor8d96cab2013-06-25 19:12:18 -0700599 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
600 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700601 mProcess->mDriverFD, result);
602 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800604
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 // Let this thread exit the thread pool if it is no longer
606 // needed and it is not the main process thread.
607 if(result == TIMED_OUT && !isMain) {
608 break;
609 }
610 } while (result != -ECONNREFUSED && result != -EBADF);
611
Wei Wangc7341432016-10-19 10:23:59 -0700612 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
613 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800614
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 mOut.writeInt32(BC_EXIT_LOOPER);
616 talkWithDriver(false);
617}
618
Todd Poynor8d96cab2013-06-25 19:12:18 -0700619int IPCThreadState::setupPolling(int* fd)
620{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200621 if (mProcess->mDriverFD < 0) {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700622 return -EBADF;
623 }
624
625 mOut.writeInt32(BC_ENTER_LOOPER);
626 *fd = mProcess->mDriverFD;
627 return 0;
628}
629
630status_t IPCThreadState::handlePolledCommands()
631{
632 status_t result;
633
634 do {
635 result = getAndExecuteCommand();
636 } while (mIn.dataPosition() < mIn.dataSize());
637
638 processPendingDerefs();
639 flushCommands();
640 return result;
641}
642
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800643void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644{
Steve Blocka19954a2012-01-04 20:05:49 +0000645 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 flushCommands();
647 int fd = mProcess->mDriverFD;
648 mProcess->mDriverFD = -1;
649 close(fd);
650 //kill(getpid(), SIGKILL);
651}
652
653status_t IPCThreadState::transact(int32_t handle,
654 uint32_t code, const Parcel& data,
655 Parcel* reply, uint32_t flags)
656{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800657 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
659 flags |= TF_ACCEPT_FDS;
660
661 IF_LOG_TRANSACTIONS() {
662 TextOutput::Bundle _b(alog);
663 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
664 << handle << " / code " << TypeCode(code) << ": "
665 << indent << data << dedent << endl;
666 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800667
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800668 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
669 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700670 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 if (err != NO_ERROR) {
673 if (reply) reply->setError(err);
674 return (mLastError = err);
675 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800676
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800678 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
679 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700680 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800681 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
682 ANDROID_LOG_ERROR);
683 } else /* FATAL_IF_NOT_ONEWAY */ {
Steven Moreland8cb34fc2019-05-13 11:44:55 -0700684 LOG_ALWAYS_FATAL("Process may not make oneway calls (code: %u).", code);
Steven Moreland7732a092019-01-02 17:54:16 -0800685 }
686 }
687
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700688 #if 0
689 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000690 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700691 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000692 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700693 }
694 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 if (reply) {
696 err = waitForResponse(reply);
697 } else {
698 Parcel fakeReply;
699 err = waitForResponse(&fakeReply);
700 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700701 #if 0
702 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000703 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700704 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000705 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700706 }
707 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 IF_LOG_TRANSACTIONS() {
710 TextOutput::Bundle _b(alog);
711 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
712 << handle << ": ";
713 if (reply) alog << indent << *reply << dedent << endl;
714 else alog << "(none requested)" << endl;
715 }
716 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700717 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 return err;
721}
722
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700723void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724{
725 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
726 mOut.writeInt32(BC_ACQUIRE);
727 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700728 // Create a temp reference until the driver has handled this command.
729 proxy->incStrong(mProcess.get());
730 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731}
732
733void IPCThreadState::decStrongHandle(int32_t handle)
734{
735 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
736 mOut.writeInt32(BC_RELEASE);
737 mOut.writeInt32(handle);
738}
739
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700740void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741{
742 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
743 mOut.writeInt32(BC_INCREFS);
744 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700745 // Create a temp reference until the driver has handled this command.
746 proxy->getWeakRefs()->incWeak(mProcess.get());
747 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748}
749
750void IPCThreadState::decWeakHandle(int32_t handle)
751{
752 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
753 mOut.writeInt32(BC_DECREFS);
754 mOut.writeInt32(handle);
755}
756
757status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
758{
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800759#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700760 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
762 mOut.writeInt32(0); // xxx was thread priority
763 mOut.writeInt32(handle);
764 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800769 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
771#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 return result;
Arve HjønnevÄg11cfdcc2014-02-14 20:14:02 -0800774#else
775 (void)handle;
776 ALOGE("%s(%d): Not supported\n", __func__, handle);
777 return INVALID_OPERATION;
778#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779}
780
781void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
782{
783#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800784 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700786 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787}
788
789status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
790{
791 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
792 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000793 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 return NO_ERROR;
795}
796
797status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
798{
799 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
800 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000801 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 return NO_ERROR;
803}
804
805IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700806 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100807 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000808 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700809 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800810 mLastTransactionBinderFlags(0),
811 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812{
813 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800814 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 mIn.setDataCapacity(256);
816 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000817 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818}
819
820IPCThreadState::~IPCThreadState()
821{
822}
823
Martijn Coenenea0090a2017-11-02 18:54:40 +0000824status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
825{
826 status_t err;
827 status_t statusBuffer;
828 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
829 if (err < NO_ERROR) return err;
830
Yi Kongfdd8da92018-06-07 17:52:27 -0700831 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000832}
833
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
835{
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100836 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 int32_t err;
838
839 while (1) {
840 if ((err=talkWithDriver()) < NO_ERROR) break;
841 err = mIn.errorCheck();
842 if (err < NO_ERROR) break;
843 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800844
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +0100845 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800846
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 IF_LOG_COMMANDS() {
848 alog << "Processing waitForResponse Command: "
849 << getReturnString(cmd) << endl;
850 }
851
852 switch (cmd) {
853 case BR_TRANSACTION_COMPLETE:
854 if (!reply && !acquireResult) goto finish;
855 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800856
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 case BR_DEAD_REPLY:
858 err = DEAD_OBJECT;
859 goto finish;
860
861 case BR_FAILED_REPLY:
862 err = FAILED_TRANSACTION;
863 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800864
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 case BR_ACQUIRE_RESULT:
866 {
Steve Block67263472012-01-09 18:35:44 +0000867 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 const int32_t result = mIn.readInt32();
869 if (!acquireResult) continue;
870 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
871 }
872 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800873
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 case BR_REPLY:
875 {
876 binder_transaction_data tr;
877 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000878 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 if (err != NO_ERROR) goto finish;
880
881 if (reply) {
882 if ((tr.flags & TF_STATUS_CODE) == 0) {
883 reply->ipcSetDataReference(
884 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
885 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800886 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
887 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 freeBuffer, this);
889 } else {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800890 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700891 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
893 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800894 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
895 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 }
897 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700898 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
900 tr.data_size,
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800901 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
902 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 continue;
904 }
905 }
906 goto finish;
907
908 default:
909 err = executeCommand(cmd);
910 if (err != NO_ERROR) goto finish;
911 break;
912 }
913 }
914
915finish:
916 if (err != NO_ERROR) {
917 if (acquireResult) *acquireResult = err;
918 if (reply) reply->setError(err);
919 mLastError = err;
920 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 return err;
923}
924
925status_t IPCThreadState::talkWithDriver(bool doReceive)
926{
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200927 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100928 return -EBADF;
929 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800930
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800932
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 // Is the read buffer empty?
934 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 // We don't want to write anything if we are still reading
937 // from data left in the input buffer and the caller
938 // has requested to read the next data.
939 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800940
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 bwr.write_size = outAvail;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800942 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943
944 // This is what we'll read.
945 if (doReceive && needRead) {
946 bwr.read_size = mIn.dataCapacity();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -0800947 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 } else {
949 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800950 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 IF_LOG_COMMANDS() {
954 TextOutput::Bundle _b(alog);
955 if (outAvail != 0) {
956 alog << "Sending commands to driver: " << indent;
957 const void* cmds = (const void*)bwr.write_buffer;
958 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
959 alog << HexDump(cmds, bwr.write_size) << endl;
960 while (cmds < end) cmds = printCommand(alog, cmds);
961 alog << dedent;
962 }
963 alog << "Size of receive buffer: " << bwr.read_size
964 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
965 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 // Return immediately if there is nothing to do.
968 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700969
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 bwr.write_consumed = 0;
971 bwr.read_consumed = 0;
972 status_t err;
973 do {
974 IF_LOG_COMMANDS() {
975 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
976 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700977#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
979 err = NO_ERROR;
980 else
981 err = -errno;
982#else
983 err = INVALID_OPERATION;
984#endif
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -0200985 if (mProcess->mDriverFD < 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100986 err = -EBADF;
987 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 IF_LOG_COMMANDS() {
989 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
990 }
991 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700992
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800994 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700996 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997 }
998
999 if (err >= NO_ERROR) {
1000 if (bwr.write_consumed > 0) {
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001001 if (bwr.write_consumed < mOut.dataSize())
Steven Moreland80afc6f2019-07-15 13:28:44 -07001002 LOG_ALWAYS_FATAL("Driver did not consume write buffer");
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001003 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -07001005 processPostWriteDerefs();
1006 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 }
1008 if (bwr.read_consumed > 0) {
1009 mIn.setDataSize(bwr.read_consumed);
1010 mIn.setDataPosition(0);
1011 }
1012 IF_LOG_COMMANDS() {
1013 TextOutput::Bundle _b(alog);
1014 alog << "Remaining data size: " << mOut.dataSize() << endl;
1015 alog << "Received commands from driver: " << indent;
1016 const void* cmds = mIn.data();
1017 const void* end = mIn.data() + mIn.dataSize();
1018 alog << HexDump(cmds, mIn.dataSize()) << endl;
1019 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1020 alog << dedent;
1021 }
1022 return NO_ERROR;
1023 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 return err;
1026}
1027
1028status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1029 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1030{
1031 binder_transaction_data tr;
1032
Arve HjønnevÄg07fd0f12014-02-18 21:10:29 -08001033 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 tr.target.handle = handle;
1035 tr.code = code;
1036 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001037 tr.cookie = 0;
1038 tr.sender_pid = 0;
1039 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 const status_t err = data.errorCheck();
1042 if (err == NO_ERROR) {
1043 tr.data_size = data.ipcDataSize();
1044 tr.data.ptr.buffer = data.ipcData();
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001045 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 tr.data.ptr.offsets = data.ipcObjects();
1047 } else if (statusBuffer) {
1048 tr.flags |= TF_STATUS_CODE;
1049 *statusBuffer = err;
1050 tr.data_size = sizeof(status_t);
Arve HjønnevÄg87b30d02014-02-18 21:04:31 -08001051 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 tr.offsets_size = 0;
Arve HjønnevÄg84e625a2014-01-28 20:12:59 -08001053 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 } else {
1055 return (mLastError = err);
1056 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 mOut.writeInt32(cmd);
1059 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061 return NO_ERROR;
1062}
1063
1064sp<BBinder> the_context_object;
1065
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001066void IPCThreadState::setTheContextObject(sp<BBinder> obj)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
1068 the_context_object = obj;
1069}
1070
1071status_t IPCThreadState::executeCommand(int32_t cmd)
1072{
1073 BBinder* obj;
1074 RefBase::weakref_type* refs;
1075 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001076
Bernhard RosenkrƤnzer74debb02014-11-25 21:55:33 +01001077 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 case BR_ERROR:
1079 result = mIn.readInt32();
1080 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 case BR_OK:
1083 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001086 refs = (RefBase::weakref_type*)mIn.readPointer();
1087 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001088 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1090 refs, obj, refs->refBase());
1091 obj->incStrong(mProcess.get());
1092 IF_LOG_REMOTEREFS() {
1093 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1094 obj->printRefs();
1095 }
1096 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001097 mOut.writePointer((uintptr_t)refs);
1098 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001102 refs = (RefBase::weakref_type*)mIn.readPointer();
1103 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001104 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1106 refs, obj, refs->refBase());
1107 IF_LOG_REMOTEREFS() {
1108 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1109 obj->printRefs();
1110 }
1111 mPendingStrongDerefs.push(obj);
1112 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001115 refs = (RefBase::weakref_type*)mIn.readPointer();
1116 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 refs->incWeak(mProcess.get());
1118 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001119 mOut.writePointer((uintptr_t)refs);
1120 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001124 refs = (RefBase::weakref_type*)mIn.readPointer();
1125 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 // NOTE: This assertion is not valid, because the object may no
1127 // longer exist (thus the (BBinder*)cast above resulting in a different
1128 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001129 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1131 // refs, obj, refs->refBase());
1132 mPendingWeakDerefs.push(refs);
1133 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001136 refs = (RefBase::weakref_type*)mIn.readPointer();
1137 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 {
1140 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001141 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1143 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 mOut.writeInt32(BC_ACQUIRE_RESULT);
1146 mOut.writeInt32((int32_t)success);
1147 }
1148 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001149
Steven Morelandf0212002018-12-26 13:59:23 -08001150 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 case BR_TRANSACTION:
1152 {
Steven Morelandf0212002018-12-26 13:59:23 -08001153 binder_transaction_data_secctx tr_secctx;
1154 binder_transaction_data& tr = tr_secctx.transaction_data;
1155
1156 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1157 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1158 } else {
1159 result = mIn.read(&tr, sizeof(tr));
1160 tr_secctx.secctx = 0;
1161 }
1162
Steve Block67263472012-01-09 18:35:44 +00001163 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 "Not enough command data for brTRANSACTION");
1165 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001166
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001167 //Record the fact that we're in a binder call.
1168 mIPCThreadStateBase->pushCurrentState(
1169 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001170 Parcel buffer;
1171 buffer.ipcSetDataReference(
1172 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1173 tr.data_size,
1174 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1175 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1176
1177 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001178 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001179 const uid_t origUid = mCallingUid;
1180 const int32_t origStrictModePolicy = mStrictModePolicy;
1181 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001182 const int32_t origWorkSource = mWorkSource;
1183 const bool origPropagateWorkSet = mPropagateWorkSource;
1184 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1185 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1186 // here to never propagate it.
1187 clearCallingWorkSource();
1188 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001189
1190 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001191 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001192 mCallingUid = tr.sender_euid;
1193 mLastTransactionBinderFlags = tr.flags;
1194
Steven Morelandf0212002018-12-26 13:59:23 -08001195 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1196 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001199 status_t error;
1200 IF_LOG_TRANSACTIONS() {
1201 TextOutput::Bundle _b(alog);
1202 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1203 << " / obj " << tr.target.ptr << " / code "
1204 << TypeCode(tr.code) << ": " << indent << buffer
1205 << dedent << endl
1206 << "Data addr = "
1207 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1208 << ", offsets addr="
1209 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1210 }
1211 if (tr.target.ptr) {
1212 // We only have a weak reference on the target object, so we must first try to
1213 // safely acquire a strong reference before doing anything else with it.
1214 if (reinterpret_cast<RefBase::weakref_type*>(
1215 tr.target.ptr)->attemptIncStrong(this)) {
1216 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1217 &reply, tr.flags);
1218 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001219 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001220 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001221 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001222
Martijn Coenenea0090a2017-11-02 18:54:40 +00001223 } else {
1224 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001226
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001227 mIPCThreadStateBase->popCurrentState();
Steven Morelandf0212002018-12-26 13:59:23 -08001228 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1229 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001230
Martijn Coenenea0090a2017-11-02 18:54:40 +00001231 if ((tr.flags & TF_ONE_WAY) == 0) {
1232 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1233 if (error < NO_ERROR) reply.setError(error);
1234 sendReply(reply, 0);
1235 } else {
1236 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1237 }
1238
1239 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001240 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001241 mCallingUid = origUid;
1242 mStrictModePolicy = origStrictModePolicy;
1243 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001244 mWorkSource = origWorkSource;
1245 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 IF_LOG_TRANSACTIONS() {
1248 TextOutput::Bundle _b(alog);
1249 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1250 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1251 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 }
1254 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 case BR_DEAD_BINDER:
1257 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001258 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 proxy->sendObituary();
1260 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001261 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1265 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001266 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 proxy->getWeakRefs()->decWeak(proxy);
1268 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 case BR_FINISHED:
1271 result = TIMED_OUT;
1272 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 case BR_NOOP:
1275 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 case BR_SPAWN_LOOPER:
1278 mProcess->spawnPooledThread(false);
1279 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 default:
liangweikanga43ee152016-10-25 16:37:54 +08001282 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 result = UNKNOWN_ERROR;
1284 break;
1285 }
1286
1287 if (result != NO_ERROR) {
1288 mLastError = result;
1289 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return result;
1292}
1293
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001294bool IPCThreadState::isServingCall() const {
1295 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1296}
1297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298void IPCThreadState::threadDestructor(void *st)
1299{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001300 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1301 if (self) {
1302 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001303#if defined(__ANDROID__)
Alexandre Baiãoc60c4fc2019-07-31 12:29:31 -02001304 if (self->mProcess->mDriverFD >= 0) {
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001305 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1306 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001308 delete self;
1309 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310}
1311
1312
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001313void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1314 size_t /*dataSize*/,
1315 const binder_size_t* /*objects*/,
1316 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317{
Steve Blocka19954a2012-01-04 20:05:49 +00001318 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 IF_LOG_COMMANDS() {
1320 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1321 }
Steve Block67263472012-01-09 18:35:44 +00001322 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001323 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 IPCThreadState* state = self();
1325 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001326 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327}
1328
1329}; // namespace android