xref: /aosp_15_r20/frameworks/native/libs/binder/IPCThreadState.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2005 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "IPCThreadState"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <binder/Binder.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/BpBinder.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/TextOutput.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/CallStack.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include <atomic>
28*38e8c45fSAndroid Build Coastguard Worker #include <errno.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <inttypes.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <pthread.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <sched.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <signal.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <sys/resource.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #include "Utils.h"
39*38e8c45fSAndroid Build Coastguard Worker #include "binder_module.h"
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker #if LOG_NDEBUG
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_TRANSACTIONS() if (false)
44*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_COMMANDS() if (false)
45*38e8c45fSAndroid Build Coastguard Worker #define LOG_REMOTEREFS(...)
46*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_REMOTEREFS() if (false)
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker #define LOG_THREADPOOL(...)
49*38e8c45fSAndroid Build Coastguard Worker #define LOG_ONEWAY(...)
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker #else
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
54*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
55*38e8c45fSAndroid Build Coastguard Worker #define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
56*38e8c45fSAndroid Build Coastguard Worker #define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
57*38e8c45fSAndroid Build Coastguard Worker #define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
58*38e8c45fSAndroid Build Coastguard Worker #define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker #endif
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker namespace android {
65*38e8c45fSAndroid Build Coastguard Worker 
66*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker // Static const and functions will be optimized out if not used,
69*38e8c45fSAndroid Build Coastguard Worker // when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
70*38e8c45fSAndroid Build Coastguard Worker static const char* kReturnStrings[] = {
71*38e8c45fSAndroid Build Coastguard Worker         "BR_ERROR",
72*38e8c45fSAndroid Build Coastguard Worker         "BR_OK",
73*38e8c45fSAndroid Build Coastguard Worker         "BR_TRANSACTION/BR_TRANSACTION_SEC_CTX",
74*38e8c45fSAndroid Build Coastguard Worker         "BR_REPLY",
75*38e8c45fSAndroid Build Coastguard Worker         "BR_ACQUIRE_RESULT",
76*38e8c45fSAndroid Build Coastguard Worker         "BR_DEAD_REPLY",
77*38e8c45fSAndroid Build Coastguard Worker         "BR_TRANSACTION_COMPLETE",
78*38e8c45fSAndroid Build Coastguard Worker         "BR_INCREFS",
79*38e8c45fSAndroid Build Coastguard Worker         "BR_ACQUIRE",
80*38e8c45fSAndroid Build Coastguard Worker         "BR_RELEASE",
81*38e8c45fSAndroid Build Coastguard Worker         "BR_DECREFS",
82*38e8c45fSAndroid Build Coastguard Worker         "BR_ATTEMPT_ACQUIRE",
83*38e8c45fSAndroid Build Coastguard Worker         "BR_NOOP",
84*38e8c45fSAndroid Build Coastguard Worker         "BR_SPAWN_LOOPER",
85*38e8c45fSAndroid Build Coastguard Worker         "BR_FINISHED",
86*38e8c45fSAndroid Build Coastguard Worker         "BR_DEAD_BINDER",
87*38e8c45fSAndroid Build Coastguard Worker         "BR_CLEAR_DEATH_NOTIFICATION_DONE",
88*38e8c45fSAndroid Build Coastguard Worker         "BR_FAILED_REPLY",
89*38e8c45fSAndroid Build Coastguard Worker         "BR_FROZEN_REPLY",
90*38e8c45fSAndroid Build Coastguard Worker         "BR_ONEWAY_SPAM_SUSPECT",
91*38e8c45fSAndroid Build Coastguard Worker         "BR_TRANSACTION_PENDING_FROZEN",
92*38e8c45fSAndroid Build Coastguard Worker         "BR_FROZEN_BINDER",
93*38e8c45fSAndroid Build Coastguard Worker         "BR_CLEAR_FREEZE_NOTIFICATION_DONE",
94*38e8c45fSAndroid Build Coastguard Worker };
95*38e8c45fSAndroid Build Coastguard Worker 
96*38e8c45fSAndroid Build Coastguard Worker static const char* kCommandStrings[] = {
97*38e8c45fSAndroid Build Coastguard Worker         "BC_TRANSACTION",
98*38e8c45fSAndroid Build Coastguard Worker         "BC_REPLY",
99*38e8c45fSAndroid Build Coastguard Worker         "BC_ACQUIRE_RESULT",
100*38e8c45fSAndroid Build Coastguard Worker         "BC_FREE_BUFFER",
101*38e8c45fSAndroid Build Coastguard Worker         "BC_INCREFS",
102*38e8c45fSAndroid Build Coastguard Worker         "BC_ACQUIRE",
103*38e8c45fSAndroid Build Coastguard Worker         "BC_RELEASE",
104*38e8c45fSAndroid Build Coastguard Worker         "BC_DECREFS",
105*38e8c45fSAndroid Build Coastguard Worker         "BC_INCREFS_DONE",
106*38e8c45fSAndroid Build Coastguard Worker         "BC_ACQUIRE_DONE",
107*38e8c45fSAndroid Build Coastguard Worker         "BC_ATTEMPT_ACQUIRE",
108*38e8c45fSAndroid Build Coastguard Worker         "BC_REGISTER_LOOPER",
109*38e8c45fSAndroid Build Coastguard Worker         "BC_ENTER_LOOPER",
110*38e8c45fSAndroid Build Coastguard Worker         "BC_EXIT_LOOPER",
111*38e8c45fSAndroid Build Coastguard Worker         "BC_REQUEST_DEATH_NOTIFICATION",
112*38e8c45fSAndroid Build Coastguard Worker         "BC_CLEAR_DEATH_NOTIFICATION",
113*38e8c45fSAndroid Build Coastguard Worker         "BC_DEAD_BINDER_DONE",
114*38e8c45fSAndroid Build Coastguard Worker         "BC_TRANSACTION_SG",
115*38e8c45fSAndroid Build Coastguard Worker         "BC_REPLY_SG",
116*38e8c45fSAndroid Build Coastguard Worker         "BC_REQUEST_FREEZE_NOTIFICATION",
117*38e8c45fSAndroid Build Coastguard Worker         "BC_CLEAR_FREEZE_NOTIFICATION",
118*38e8c45fSAndroid Build Coastguard Worker         "BC_FREEZE_NOTIFICATION_DONE",
119*38e8c45fSAndroid Build Coastguard Worker };
120*38e8c45fSAndroid Build Coastguard Worker 
121*38e8c45fSAndroid Build Coastguard Worker static const int64_t kWorkSourcePropagatedBitIndex = 32;
122*38e8c45fSAndroid Build Coastguard Worker 
getReturnString(uint32_t cmd)123*38e8c45fSAndroid Build Coastguard Worker static const char* getReturnString(uint32_t cmd)
124*38e8c45fSAndroid Build Coastguard Worker {
125*38e8c45fSAndroid Build Coastguard Worker     size_t idx = cmd & _IOC_NRMASK;
126*38e8c45fSAndroid Build Coastguard Worker     if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
127*38e8c45fSAndroid Build Coastguard Worker         return kReturnStrings[idx];
128*38e8c45fSAndroid Build Coastguard Worker     else
129*38e8c45fSAndroid Build Coastguard Worker         return "unknown";
130*38e8c45fSAndroid Build Coastguard Worker }
131*38e8c45fSAndroid Build Coastguard Worker 
printBinderTransactionData(std::ostream & out,const void * data)132*38e8c45fSAndroid Build Coastguard Worker static const void* printBinderTransactionData(std::ostream& out, const void* data) {
133*38e8c45fSAndroid Build Coastguard Worker     const binder_transaction_data* btd =
134*38e8c45fSAndroid Build Coastguard Worker         (const binder_transaction_data*)data;
135*38e8c45fSAndroid Build Coastguard Worker     if (btd->target.handle < 1024) {
136*38e8c45fSAndroid Build Coastguard Worker         /* want to print descriptors in decimal; guess based on value */
137*38e8c45fSAndroid Build Coastguard Worker         out << "\ttarget.desc=" << btd->target.handle;
138*38e8c45fSAndroid Build Coastguard Worker     } else {
139*38e8c45fSAndroid Build Coastguard Worker         out << "\ttarget.ptr=" << btd->target.ptr;
140*38e8c45fSAndroid Build Coastguard Worker     }
141*38e8c45fSAndroid Build Coastguard Worker     out << "\t (cookie " << btd->cookie << ")\n"
142*38e8c45fSAndroid Build Coastguard Worker         << "\tcode=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)btd->flags << "\n"
143*38e8c45fSAndroid Build Coastguard Worker         << "\tdata=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size << " bytes)\n"
144*38e8c45fSAndroid Build Coastguard Worker         << "\toffsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size << " bytes)\n";
145*38e8c45fSAndroid Build Coastguard Worker     return btd + 1;
146*38e8c45fSAndroid Build Coastguard Worker }
147*38e8c45fSAndroid Build Coastguard Worker 
printBinderTransactionDataSecCtx(std::ostream & out,const void * data)148*38e8c45fSAndroid Build Coastguard Worker static const void* printBinderTransactionDataSecCtx(std::ostream& out, const void* data) {
149*38e8c45fSAndroid Build Coastguard Worker     const binder_transaction_data_secctx* btd = (const binder_transaction_data_secctx*)data;
150*38e8c45fSAndroid Build Coastguard Worker 
151*38e8c45fSAndroid Build Coastguard Worker     printBinderTransactionData(out, &btd->transaction_data);
152*38e8c45fSAndroid Build Coastguard Worker 
153*38e8c45fSAndroid Build Coastguard Worker     char* secctx = (char*)btd->secctx;
154*38e8c45fSAndroid Build Coastguard Worker     out << "\tsecctx=" << secctx << "\n";
155*38e8c45fSAndroid Build Coastguard Worker 
156*38e8c45fSAndroid Build Coastguard Worker     return btd+1;
157*38e8c45fSAndroid Build Coastguard Worker }
158*38e8c45fSAndroid Build Coastguard Worker 
printReturnCommand(std::ostream & out,const void * _cmd)159*38e8c45fSAndroid Build Coastguard Worker static const void* printReturnCommand(std::ostream& out, const void* _cmd) {
160*38e8c45fSAndroid Build Coastguard Worker     static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
161*38e8c45fSAndroid Build Coastguard Worker     const int32_t* cmd = (const int32_t*)_cmd;
162*38e8c45fSAndroid Build Coastguard Worker     uint32_t code = (uint32_t)*cmd++;
163*38e8c45fSAndroid Build Coastguard Worker     size_t cmdIndex = code & 0xff;
164*38e8c45fSAndroid Build Coastguard Worker     if (code == BR_ERROR) {
165*38e8c45fSAndroid Build Coastguard Worker         out << "\tBR_ERROR: " << (void*)(uint64_t)(*cmd++) << "\n";
166*38e8c45fSAndroid Build Coastguard Worker         return cmd;
167*38e8c45fSAndroid Build Coastguard Worker     } else if (cmdIndex >= N) {
168*38e8c45fSAndroid Build Coastguard Worker         out << "\tUnknown reply: " << code << "\n";
169*38e8c45fSAndroid Build Coastguard Worker         return cmd;
170*38e8c45fSAndroid Build Coastguard Worker     }
171*38e8c45fSAndroid Build Coastguard Worker     out << "\t" << kReturnStrings[cmdIndex];
172*38e8c45fSAndroid Build Coastguard Worker 
173*38e8c45fSAndroid Build Coastguard Worker     switch (code) {
174*38e8c45fSAndroid Build Coastguard Worker         case BR_TRANSACTION_SEC_CTX: {
175*38e8c45fSAndroid Build Coastguard Worker             out << ": ";
176*38e8c45fSAndroid Build Coastguard Worker             cmd = (const int32_t*)printBinderTransactionDataSecCtx(out, cmd);
177*38e8c45fSAndroid Build Coastguard Worker         } break;
178*38e8c45fSAndroid Build Coastguard Worker 
179*38e8c45fSAndroid Build Coastguard Worker         case BR_TRANSACTION:
180*38e8c45fSAndroid Build Coastguard Worker         case BR_REPLY: {
181*38e8c45fSAndroid Build Coastguard Worker             out << ": ";
182*38e8c45fSAndroid Build Coastguard Worker             cmd = (const int32_t*)printBinderTransactionData(out, cmd);
183*38e8c45fSAndroid Build Coastguard Worker         } break;
184*38e8c45fSAndroid Build Coastguard Worker 
185*38e8c45fSAndroid Build Coastguard Worker         case BR_ACQUIRE_RESULT: {
186*38e8c45fSAndroid Build Coastguard Worker             const int32_t res = *cmd++;
187*38e8c45fSAndroid Build Coastguard Worker             out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
188*38e8c45fSAndroid Build Coastguard Worker         } break;
189*38e8c45fSAndroid Build Coastguard Worker 
190*38e8c45fSAndroid Build Coastguard Worker         case BR_INCREFS:
191*38e8c45fSAndroid Build Coastguard Worker         case BR_ACQUIRE:
192*38e8c45fSAndroid Build Coastguard Worker         case BR_RELEASE:
193*38e8c45fSAndroid Build Coastguard Worker         case BR_DECREFS: {
194*38e8c45fSAndroid Build Coastguard Worker             const int32_t b = *cmd++;
195*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
196*38e8c45fSAndroid Build Coastguard Worker             out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
197*38e8c45fSAndroid Build Coastguard Worker         } break;
198*38e8c45fSAndroid Build Coastguard Worker 
199*38e8c45fSAndroid Build Coastguard Worker         case BR_ATTEMPT_ACQUIRE: {
200*38e8c45fSAndroid Build Coastguard Worker             const int32_t p = *cmd++;
201*38e8c45fSAndroid Build Coastguard Worker             const int32_t b = *cmd++;
202*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
203*38e8c45fSAndroid Build Coastguard Worker             out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c
204*38e8c45fSAndroid Build Coastguard Worker                 << "), pri=" << p;
205*38e8c45fSAndroid Build Coastguard Worker         } break;
206*38e8c45fSAndroid Build Coastguard Worker 
207*38e8c45fSAndroid Build Coastguard Worker         case BR_DEAD_BINDER:
208*38e8c45fSAndroid Build Coastguard Worker         case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
209*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
210*38e8c45fSAndroid Build Coastguard Worker             out << ": death cookie " << (void*)(uint64_t)c;
211*38e8c45fSAndroid Build Coastguard Worker         } break;
212*38e8c45fSAndroid Build Coastguard Worker 
213*38e8c45fSAndroid Build Coastguard Worker         case BR_FROZEN_BINDER: {
214*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
215*38e8c45fSAndroid Build Coastguard Worker             const int32_t h = *cmd++;
216*38e8c45fSAndroid Build Coastguard Worker             const int32_t isFrozen = *cmd++;
217*38e8c45fSAndroid Build Coastguard Worker             out << ": freeze cookie " << (void*)(uint64_t)c << " isFrozen: " << isFrozen;
218*38e8c45fSAndroid Build Coastguard Worker         } break;
219*38e8c45fSAndroid Build Coastguard Worker 
220*38e8c45fSAndroid Build Coastguard Worker         case BR_CLEAR_FREEZE_NOTIFICATION_DONE: {
221*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
222*38e8c45fSAndroid Build Coastguard Worker             out << ": freeze cookie " << (void*)(uint64_t)c;
223*38e8c45fSAndroid Build Coastguard Worker         } break;
224*38e8c45fSAndroid Build Coastguard Worker 
225*38e8c45fSAndroid Build Coastguard Worker         default:
226*38e8c45fSAndroid Build Coastguard Worker             // no details to show for: BR_OK, BR_DEAD_REPLY,
227*38e8c45fSAndroid Build Coastguard Worker             // BR_TRANSACTION_COMPLETE, BR_FINISHED
228*38e8c45fSAndroid Build Coastguard Worker             break;
229*38e8c45fSAndroid Build Coastguard Worker     }
230*38e8c45fSAndroid Build Coastguard Worker 
231*38e8c45fSAndroid Build Coastguard Worker     out << "\n";
232*38e8c45fSAndroid Build Coastguard Worker     return cmd;
233*38e8c45fSAndroid Build Coastguard Worker }
234*38e8c45fSAndroid Build Coastguard Worker 
printReturnCommandParcel(std::ostream & out,const Parcel & parcel)235*38e8c45fSAndroid Build Coastguard Worker static void printReturnCommandParcel(std::ostream& out, const Parcel& parcel) {
236*38e8c45fSAndroid Build Coastguard Worker     const void* cmds = parcel.data();
237*38e8c45fSAndroid Build Coastguard Worker     out << "\t" << HexDump(cmds, parcel.dataSize()) << "\n";
238*38e8c45fSAndroid Build Coastguard Worker     IF_LOG_COMMANDS() {
239*38e8c45fSAndroid Build Coastguard Worker         const void* end = parcel.data() + parcel.dataSize();
240*38e8c45fSAndroid Build Coastguard Worker         while (cmds < end) cmds = printReturnCommand(out, cmds);
241*38e8c45fSAndroid Build Coastguard Worker     }
242*38e8c45fSAndroid Build Coastguard Worker }
243*38e8c45fSAndroid Build Coastguard Worker 
printCommand(std::ostream & out,const void * _cmd)244*38e8c45fSAndroid Build Coastguard Worker static const void* printCommand(std::ostream& out, const void* _cmd) {
245*38e8c45fSAndroid Build Coastguard Worker     static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
246*38e8c45fSAndroid Build Coastguard Worker     const int32_t* cmd = (const int32_t*)_cmd;
247*38e8c45fSAndroid Build Coastguard Worker     uint32_t code = (uint32_t)*cmd++;
248*38e8c45fSAndroid Build Coastguard Worker     size_t cmdIndex = code & 0xff;
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker     if (cmdIndex >= N) {
251*38e8c45fSAndroid Build Coastguard Worker         out << "Unknown command: " << code << "\n";
252*38e8c45fSAndroid Build Coastguard Worker         return cmd;
253*38e8c45fSAndroid Build Coastguard Worker     }
254*38e8c45fSAndroid Build Coastguard Worker     out << kCommandStrings[cmdIndex];
255*38e8c45fSAndroid Build Coastguard Worker 
256*38e8c45fSAndroid Build Coastguard Worker     switch (code) {
257*38e8c45fSAndroid Build Coastguard Worker         case BC_TRANSACTION:
258*38e8c45fSAndroid Build Coastguard Worker         case BC_REPLY: {
259*38e8c45fSAndroid Build Coastguard Worker             out << ": ";
260*38e8c45fSAndroid Build Coastguard Worker             cmd = (const int32_t*)printBinderTransactionData(out, cmd);
261*38e8c45fSAndroid Build Coastguard Worker         } break;
262*38e8c45fSAndroid Build Coastguard Worker 
263*38e8c45fSAndroid Build Coastguard Worker         case BC_ACQUIRE_RESULT: {
264*38e8c45fSAndroid Build Coastguard Worker             const int32_t res = *cmd++;
265*38e8c45fSAndroid Build Coastguard Worker             out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
266*38e8c45fSAndroid Build Coastguard Worker         } break;
267*38e8c45fSAndroid Build Coastguard Worker 
268*38e8c45fSAndroid Build Coastguard Worker         case BC_FREE_BUFFER: {
269*38e8c45fSAndroid Build Coastguard Worker             const int32_t buf = *cmd++;
270*38e8c45fSAndroid Build Coastguard Worker             out << ": buffer=" << (void*)(uint64_t)buf;
271*38e8c45fSAndroid Build Coastguard Worker         } break;
272*38e8c45fSAndroid Build Coastguard Worker 
273*38e8c45fSAndroid Build Coastguard Worker         case BC_INCREFS:
274*38e8c45fSAndroid Build Coastguard Worker         case BC_ACQUIRE:
275*38e8c45fSAndroid Build Coastguard Worker         case BC_RELEASE:
276*38e8c45fSAndroid Build Coastguard Worker         case BC_DECREFS: {
277*38e8c45fSAndroid Build Coastguard Worker             const int32_t d = *cmd++;
278*38e8c45fSAndroid Build Coastguard Worker             out << ": desc=" << d;
279*38e8c45fSAndroid Build Coastguard Worker         } break;
280*38e8c45fSAndroid Build Coastguard Worker 
281*38e8c45fSAndroid Build Coastguard Worker         case BC_INCREFS_DONE:
282*38e8c45fSAndroid Build Coastguard Worker         case BC_ACQUIRE_DONE: {
283*38e8c45fSAndroid Build Coastguard Worker             const int32_t b = *cmd++;
284*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
285*38e8c45fSAndroid Build Coastguard Worker             out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")";
286*38e8c45fSAndroid Build Coastguard Worker         } break;
287*38e8c45fSAndroid Build Coastguard Worker 
288*38e8c45fSAndroid Build Coastguard Worker         case BC_ATTEMPT_ACQUIRE: {
289*38e8c45fSAndroid Build Coastguard Worker             const int32_t p = *cmd++;
290*38e8c45fSAndroid Build Coastguard Worker             const int32_t d = *cmd++;
291*38e8c45fSAndroid Build Coastguard Worker             out << ": desc=" << d << ", pri=" << p;
292*38e8c45fSAndroid Build Coastguard Worker         } break;
293*38e8c45fSAndroid Build Coastguard Worker 
294*38e8c45fSAndroid Build Coastguard Worker         case BC_REQUEST_DEATH_NOTIFICATION:
295*38e8c45fSAndroid Build Coastguard Worker         case BC_CLEAR_DEATH_NOTIFICATION: {
296*38e8c45fSAndroid Build Coastguard Worker             const int32_t h = *cmd++;
297*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
298*38e8c45fSAndroid Build Coastguard Worker             out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)c << ")";
299*38e8c45fSAndroid Build Coastguard Worker         } break;
300*38e8c45fSAndroid Build Coastguard Worker 
301*38e8c45fSAndroid Build Coastguard Worker         case BC_REQUEST_FREEZE_NOTIFICATION:
302*38e8c45fSAndroid Build Coastguard Worker         case BC_CLEAR_FREEZE_NOTIFICATION: {
303*38e8c45fSAndroid Build Coastguard Worker             const int32_t h = *cmd++;
304*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
305*38e8c45fSAndroid Build Coastguard Worker             out << ": handle=" << h << " (freeze cookie " << (void*)(uint64_t)c << ")";
306*38e8c45fSAndroid Build Coastguard Worker         } break;
307*38e8c45fSAndroid Build Coastguard Worker 
308*38e8c45fSAndroid Build Coastguard Worker         case BC_DEAD_BINDER_DONE: {
309*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
310*38e8c45fSAndroid Build Coastguard Worker             out << ": death cookie " << (void*)(uint64_t)c;
311*38e8c45fSAndroid Build Coastguard Worker         } break;
312*38e8c45fSAndroid Build Coastguard Worker 
313*38e8c45fSAndroid Build Coastguard Worker         case BC_FREEZE_NOTIFICATION_DONE: {
314*38e8c45fSAndroid Build Coastguard Worker             const int32_t c = *cmd++;
315*38e8c45fSAndroid Build Coastguard Worker             out << ": freeze cookie " << (void*)(uint64_t)c;
316*38e8c45fSAndroid Build Coastguard Worker         } break;
317*38e8c45fSAndroid Build Coastguard Worker 
318*38e8c45fSAndroid Build Coastguard Worker         default:
319*38e8c45fSAndroid Build Coastguard Worker             // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
320*38e8c45fSAndroid Build Coastguard Worker             // BC_EXIT_LOOPER
321*38e8c45fSAndroid Build Coastguard Worker             break;
322*38e8c45fSAndroid Build Coastguard Worker     }
323*38e8c45fSAndroid Build Coastguard Worker 
324*38e8c45fSAndroid Build Coastguard Worker     out << "\n";
325*38e8c45fSAndroid Build Coastguard Worker     return cmd;
326*38e8c45fSAndroid Build Coastguard Worker }
327*38e8c45fSAndroid Build Coastguard Worker 
328*38e8c45fSAndroid Build Coastguard Worker LIBBINDER_IGNORE("-Wzero-as-null-pointer-constant")
329*38e8c45fSAndroid Build Coastguard Worker static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
330*38e8c45fSAndroid Build Coastguard Worker LIBBINDER_IGNORE_END()
331*38e8c45fSAndroid Build Coastguard Worker static std::atomic<bool> gHaveTLS(false);
332*38e8c45fSAndroid Build Coastguard Worker static pthread_key_t gTLS = 0;
333*38e8c45fSAndroid Build Coastguard Worker static std::atomic<bool> gShutdown = false;
334*38e8c45fSAndroid Build Coastguard Worker static std::atomic<bool> gDisableBackgroundScheduling = false;
335*38e8c45fSAndroid Build Coastguard Worker 
self()336*38e8c45fSAndroid Build Coastguard Worker IPCThreadState* IPCThreadState::self()
337*38e8c45fSAndroid Build Coastguard Worker {
338*38e8c45fSAndroid Build Coastguard Worker     if (gHaveTLS.load(std::memory_order_acquire)) {
339*38e8c45fSAndroid Build Coastguard Worker restart:
340*38e8c45fSAndroid Build Coastguard Worker         const pthread_key_t k = gTLS;
341*38e8c45fSAndroid Build Coastguard Worker         IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
342*38e8c45fSAndroid Build Coastguard Worker         if (st) return st;
343*38e8c45fSAndroid Build Coastguard Worker         return new IPCThreadState;
344*38e8c45fSAndroid Build Coastguard Worker     }
345*38e8c45fSAndroid Build Coastguard Worker 
346*38e8c45fSAndroid Build Coastguard Worker     // Racey, heuristic test for simultaneous shutdown.
347*38e8c45fSAndroid Build Coastguard Worker     if (gShutdown.load(std::memory_order_relaxed)) {
348*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
349*38e8c45fSAndroid Build Coastguard Worker         return nullptr;
350*38e8c45fSAndroid Build Coastguard Worker     }
351*38e8c45fSAndroid Build Coastguard Worker 
352*38e8c45fSAndroid Build Coastguard Worker     pthread_mutex_lock(&gTLSMutex);
353*38e8c45fSAndroid Build Coastguard Worker     if (!gHaveTLS.load(std::memory_order_relaxed)) {
354*38e8c45fSAndroid Build Coastguard Worker         int key_create_value = pthread_key_create(&gTLS, threadDestructor);
355*38e8c45fSAndroid Build Coastguard Worker         if (key_create_value != 0) {
356*38e8c45fSAndroid Build Coastguard Worker             pthread_mutex_unlock(&gTLSMutex);
357*38e8c45fSAndroid Build Coastguard Worker             ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
358*38e8c45fSAndroid Build Coastguard Worker                     strerror(key_create_value));
359*38e8c45fSAndroid Build Coastguard Worker             return nullptr;
360*38e8c45fSAndroid Build Coastguard Worker         }
361*38e8c45fSAndroid Build Coastguard Worker         gHaveTLS.store(true, std::memory_order_release);
362*38e8c45fSAndroid Build Coastguard Worker     }
363*38e8c45fSAndroid Build Coastguard Worker     pthread_mutex_unlock(&gTLSMutex);
364*38e8c45fSAndroid Build Coastguard Worker     goto restart;
365*38e8c45fSAndroid Build Coastguard Worker }
366*38e8c45fSAndroid Build Coastguard Worker 
selfOrNull()367*38e8c45fSAndroid Build Coastguard Worker IPCThreadState* IPCThreadState::selfOrNull()
368*38e8c45fSAndroid Build Coastguard Worker {
369*38e8c45fSAndroid Build Coastguard Worker     if (gHaveTLS.load(std::memory_order_acquire)) {
370*38e8c45fSAndroid Build Coastguard Worker         const pthread_key_t k = gTLS;
371*38e8c45fSAndroid Build Coastguard Worker         IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
372*38e8c45fSAndroid Build Coastguard Worker         return st;
373*38e8c45fSAndroid Build Coastguard Worker     }
374*38e8c45fSAndroid Build Coastguard Worker     return nullptr;
375*38e8c45fSAndroid Build Coastguard Worker }
376*38e8c45fSAndroid Build Coastguard Worker 
shutdown()377*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::shutdown()
378*38e8c45fSAndroid Build Coastguard Worker {
379*38e8c45fSAndroid Build Coastguard Worker     gShutdown.store(true, std::memory_order_relaxed);
380*38e8c45fSAndroid Build Coastguard Worker 
381*38e8c45fSAndroid Build Coastguard Worker     if (gHaveTLS.load(std::memory_order_acquire)) {
382*38e8c45fSAndroid Build Coastguard Worker         // XXX Need to wait for all thread pool threads to exit!
383*38e8c45fSAndroid Build Coastguard Worker         IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
384*38e8c45fSAndroid Build Coastguard Worker         if (st) {
385*38e8c45fSAndroid Build Coastguard Worker             delete st;
386*38e8c45fSAndroid Build Coastguard Worker             pthread_setspecific(gTLS, nullptr);
387*38e8c45fSAndroid Build Coastguard Worker         }
388*38e8c45fSAndroid Build Coastguard Worker         pthread_key_delete(gTLS);
389*38e8c45fSAndroid Build Coastguard Worker         gHaveTLS.store(false, std::memory_order_release);
390*38e8c45fSAndroid Build Coastguard Worker     }
391*38e8c45fSAndroid Build Coastguard Worker }
392*38e8c45fSAndroid Build Coastguard Worker 
disableBackgroundScheduling(bool disable)393*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::disableBackgroundScheduling(bool disable)
394*38e8c45fSAndroid Build Coastguard Worker {
395*38e8c45fSAndroid Build Coastguard Worker     gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed);
396*38e8c45fSAndroid Build Coastguard Worker }
397*38e8c45fSAndroid Build Coastguard Worker 
backgroundSchedulingDisabled()398*38e8c45fSAndroid Build Coastguard Worker bool IPCThreadState::backgroundSchedulingDisabled()
399*38e8c45fSAndroid Build Coastguard Worker {
400*38e8c45fSAndroid Build Coastguard Worker     return gDisableBackgroundScheduling.load(std::memory_order_relaxed);
401*38e8c45fSAndroid Build Coastguard Worker }
402*38e8c45fSAndroid Build Coastguard Worker 
clearLastError()403*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::clearLastError()
404*38e8c45fSAndroid Build Coastguard Worker {
405*38e8c45fSAndroid Build Coastguard Worker     const status_t err = mLastError;
406*38e8c45fSAndroid Build Coastguard Worker     mLastError = NO_ERROR;
407*38e8c45fSAndroid Build Coastguard Worker     return err;
408*38e8c45fSAndroid Build Coastguard Worker }
409*38e8c45fSAndroid Build Coastguard Worker 
getCallingPid() const410*38e8c45fSAndroid Build Coastguard Worker pid_t IPCThreadState::getCallingPid() const
411*38e8c45fSAndroid Build Coastguard Worker {
412*38e8c45fSAndroid Build Coastguard Worker     checkContextIsBinderForUse(__func__);
413*38e8c45fSAndroid Build Coastguard Worker     return mCallingPid;
414*38e8c45fSAndroid Build Coastguard Worker }
415*38e8c45fSAndroid Build Coastguard Worker 
getCallingSid() const416*38e8c45fSAndroid Build Coastguard Worker const char* IPCThreadState::getCallingSid() const
417*38e8c45fSAndroid Build Coastguard Worker {
418*38e8c45fSAndroid Build Coastguard Worker     checkContextIsBinderForUse(__func__);
419*38e8c45fSAndroid Build Coastguard Worker     return mCallingSid;
420*38e8c45fSAndroid Build Coastguard Worker }
421*38e8c45fSAndroid Build Coastguard Worker 
getCallingUid() const422*38e8c45fSAndroid Build Coastguard Worker uid_t IPCThreadState::getCallingUid() const
423*38e8c45fSAndroid Build Coastguard Worker {
424*38e8c45fSAndroid Build Coastguard Worker     checkContextIsBinderForUse(__func__);
425*38e8c45fSAndroid Build Coastguard Worker     return mCallingUid;
426*38e8c45fSAndroid Build Coastguard Worker }
427*38e8c45fSAndroid Build Coastguard Worker 
pushGetCallingSpGuard(const SpGuard * guard)428*38e8c45fSAndroid Build Coastguard Worker const IPCThreadState::SpGuard* IPCThreadState::pushGetCallingSpGuard(const SpGuard* guard) {
429*38e8c45fSAndroid Build Coastguard Worker     const SpGuard* orig = mServingStackPointerGuard;
430*38e8c45fSAndroid Build Coastguard Worker     mServingStackPointerGuard = guard;
431*38e8c45fSAndroid Build Coastguard Worker     return orig;
432*38e8c45fSAndroid Build Coastguard Worker }
433*38e8c45fSAndroid Build Coastguard Worker 
restoreGetCallingSpGuard(const SpGuard * guard)434*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::restoreGetCallingSpGuard(const SpGuard* guard) {
435*38e8c45fSAndroid Build Coastguard Worker     mServingStackPointerGuard = guard;
436*38e8c45fSAndroid Build Coastguard Worker }
437*38e8c45fSAndroid Build Coastguard Worker 
checkContextIsBinderForUse(const char * use) const438*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::checkContextIsBinderForUse(const char* use) const {
439*38e8c45fSAndroid Build Coastguard Worker     if (mServingStackPointerGuard == nullptr) [[likely]] {
440*38e8c45fSAndroid Build Coastguard Worker         return;
441*38e8c45fSAndroid Build Coastguard Worker     }
442*38e8c45fSAndroid Build Coastguard Worker 
443*38e8c45fSAndroid Build Coastguard Worker     if (!mServingStackPointer || mServingStackPointerGuard->address < mServingStackPointer) {
444*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("In context %s, %s does not make sense (binder sp: %p, guard: %p).",
445*38e8c45fSAndroid Build Coastguard Worker                          mServingStackPointerGuard->context, use, mServingStackPointer,
446*38e8c45fSAndroid Build Coastguard Worker                          mServingStackPointerGuard->address);
447*38e8c45fSAndroid Build Coastguard Worker     }
448*38e8c45fSAndroid Build Coastguard Worker 
449*38e8c45fSAndroid Build Coastguard Worker     // in the case mServingStackPointer is deeper in the stack than the guard,
450*38e8c45fSAndroid Build Coastguard Worker     // we must be serving a binder transaction (maybe nested). This is a binder
451*38e8c45fSAndroid Build Coastguard Worker     // context, so we don't abort
452*38e8c45fSAndroid Build Coastguard Worker }
453*38e8c45fSAndroid Build Coastguard Worker 
encodeExplicitIdentity(bool hasExplicitIdentity,pid_t callingPid)454*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t encodeExplicitIdentity(bool hasExplicitIdentity, pid_t callingPid) {
455*38e8c45fSAndroid Build Coastguard Worker     uint32_t as_unsigned = static_cast<uint32_t>(callingPid);
456*38e8c45fSAndroid Build Coastguard Worker     if (hasExplicitIdentity) {
457*38e8c45fSAndroid Build Coastguard Worker         return as_unsigned | (1 << 30);
458*38e8c45fSAndroid Build Coastguard Worker     } else {
459*38e8c45fSAndroid Build Coastguard Worker         return as_unsigned & ~(1 << 30);
460*38e8c45fSAndroid Build Coastguard Worker     }
461*38e8c45fSAndroid Build Coastguard Worker }
462*38e8c45fSAndroid Build Coastguard Worker 
packCallingIdentity(bool hasExplicitIdentity,uid_t callingUid,pid_t callingPid)463*38e8c45fSAndroid Build Coastguard Worker constexpr int64_t packCallingIdentity(bool hasExplicitIdentity, uid_t callingUid,
464*38e8c45fSAndroid Build Coastguard Worker                                       pid_t callingPid) {
465*38e8c45fSAndroid Build Coastguard Worker     // Calling PID is a 32-bit signed integer, but doesn't consume the entire 32 bit space.
466*38e8c45fSAndroid Build Coastguard Worker     // To future-proof this and because we have extra capacity, we decided to also support -1,
467*38e8c45fSAndroid Build Coastguard Worker     // since this constant is used to represent invalid UID in other places of the system.
468*38e8c45fSAndroid Build Coastguard Worker     // Thus, we pack hasExplicitIdentity into the 2nd bit from the left.  This allows us to
469*38e8c45fSAndroid Build Coastguard Worker     // preserve the (left-most) bit for the sign while also encoding the value of
470*38e8c45fSAndroid Build Coastguard Worker     // hasExplicitIdentity.
471*38e8c45fSAndroid Build Coastguard Worker     //               32b     |        1b         |         1b            |        30b
472*38e8c45fSAndroid Build Coastguard Worker     // token = [ calling uid | calling pid(sign) | has explicit identity | calling pid(rest) ]
473*38e8c45fSAndroid Build Coastguard Worker     uint64_t token = (static_cast<uint64_t>(callingUid) << 32) |
474*38e8c45fSAndroid Build Coastguard Worker             encodeExplicitIdentity(hasExplicitIdentity, callingPid);
475*38e8c45fSAndroid Build Coastguard Worker     return static_cast<int64_t>(token);
476*38e8c45fSAndroid Build Coastguard Worker }
477*38e8c45fSAndroid Build Coastguard Worker 
unpackHasExplicitIdentity(int64_t token)478*38e8c45fSAndroid Build Coastguard Worker constexpr bool unpackHasExplicitIdentity(int64_t token) {
479*38e8c45fSAndroid Build Coastguard Worker     return static_cast<int32_t>(token) & (1 << 30);
480*38e8c45fSAndroid Build Coastguard Worker }
481*38e8c45fSAndroid Build Coastguard Worker 
unpackCallingUid(int64_t token)482*38e8c45fSAndroid Build Coastguard Worker constexpr uid_t unpackCallingUid(int64_t token) {
483*38e8c45fSAndroid Build Coastguard Worker     return static_cast<uid_t>(token >> 32);
484*38e8c45fSAndroid Build Coastguard Worker }
485*38e8c45fSAndroid Build Coastguard Worker 
unpackCallingPid(int64_t token)486*38e8c45fSAndroid Build Coastguard Worker constexpr pid_t unpackCallingPid(int64_t token) {
487*38e8c45fSAndroid Build Coastguard Worker     int32_t encodedPid = static_cast<int32_t>(token);
488*38e8c45fSAndroid Build Coastguard Worker     if (encodedPid & (1 << 31)) {
489*38e8c45fSAndroid Build Coastguard Worker         return encodedPid | (1 << 30);
490*38e8c45fSAndroid Build Coastguard Worker     } else {
491*38e8c45fSAndroid Build Coastguard Worker         return encodedPid & ~(1 << 30);
492*38e8c45fSAndroid Build Coastguard Worker     }
493*38e8c45fSAndroid Build Coastguard Worker }
494*38e8c45fSAndroid Build Coastguard Worker 
495*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, 9999)) == true,
496*38e8c45fSAndroid Build Coastguard Worker               "pack true hasExplicit");
497*38e8c45fSAndroid Build Coastguard Worker 
498*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingUid(packCallingIdentity(true, 1000, 9999)) == 1000, "pack true uid");
499*38e8c45fSAndroid Build Coastguard Worker 
500*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingPid(packCallingIdentity(true, 1000, 9999)) == 9999, "pack true pid");
501*38e8c45fSAndroid Build Coastguard Worker 
502*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, 9999)) == false,
503*38e8c45fSAndroid Build Coastguard Worker               "pack false hasExplicit");
504*38e8c45fSAndroid Build Coastguard Worker 
505*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingUid(packCallingIdentity(false, 1000, 9999)) == 1000, "pack false uid");
506*38e8c45fSAndroid Build Coastguard Worker 
507*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingPid(packCallingIdentity(false, 1000, 9999)) == 9999, "pack false pid");
508*38e8c45fSAndroid Build Coastguard Worker 
509*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackHasExplicitIdentity(packCallingIdentity(true, 1000, -1)) == true,
510*38e8c45fSAndroid Build Coastguard Worker               "pack true (negative) hasExplicit");
511*38e8c45fSAndroid Build Coastguard Worker 
512*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingUid(packCallingIdentity(true, 1000, -1)) == 1000,
513*38e8c45fSAndroid Build Coastguard Worker               "pack true (negative) uid");
514*38e8c45fSAndroid Build Coastguard Worker 
515*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingPid(packCallingIdentity(true, 1000, -1)) == -1,
516*38e8c45fSAndroid Build Coastguard Worker               "pack true (negative) pid");
517*38e8c45fSAndroid Build Coastguard Worker 
518*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackHasExplicitIdentity(packCallingIdentity(false, 1000, -1)) == false,
519*38e8c45fSAndroid Build Coastguard Worker               "pack false (negative) hasExplicit");
520*38e8c45fSAndroid Build Coastguard Worker 
521*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingUid(packCallingIdentity(false, 1000, -1)) == 1000,
522*38e8c45fSAndroid Build Coastguard Worker               "pack false (negative) uid");
523*38e8c45fSAndroid Build Coastguard Worker 
524*38e8c45fSAndroid Build Coastguard Worker static_assert(unpackCallingPid(packCallingIdentity(false, 1000, -1)) == -1,
525*38e8c45fSAndroid Build Coastguard Worker               "pack false (negative) pid");
526*38e8c45fSAndroid Build Coastguard Worker 
clearCallingIdentity()527*38e8c45fSAndroid Build Coastguard Worker int64_t IPCThreadState::clearCallingIdentity()
528*38e8c45fSAndroid Build Coastguard Worker {
529*38e8c45fSAndroid Build Coastguard Worker     // ignore mCallingSid for legacy reasons
530*38e8c45fSAndroid Build Coastguard Worker     int64_t token = packCallingIdentity(mHasExplicitIdentity, mCallingUid, mCallingPid);
531*38e8c45fSAndroid Build Coastguard Worker     clearCaller();
532*38e8c45fSAndroid Build Coastguard Worker     mHasExplicitIdentity = true;
533*38e8c45fSAndroid Build Coastguard Worker     return token;
534*38e8c45fSAndroid Build Coastguard Worker }
535*38e8c45fSAndroid Build Coastguard Worker 
hasExplicitIdentity()536*38e8c45fSAndroid Build Coastguard Worker bool IPCThreadState::hasExplicitIdentity() {
537*38e8c45fSAndroid Build Coastguard Worker     return mHasExplicitIdentity;
538*38e8c45fSAndroid Build Coastguard Worker }
539*38e8c45fSAndroid Build Coastguard Worker 
setStrictModePolicy(int32_t policy)540*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::setStrictModePolicy(int32_t policy)
541*38e8c45fSAndroid Build Coastguard Worker {
542*38e8c45fSAndroid Build Coastguard Worker     mStrictModePolicy = policy;
543*38e8c45fSAndroid Build Coastguard Worker }
544*38e8c45fSAndroid Build Coastguard Worker 
getStrictModePolicy() const545*38e8c45fSAndroid Build Coastguard Worker int32_t IPCThreadState::getStrictModePolicy() const
546*38e8c45fSAndroid Build Coastguard Worker {
547*38e8c45fSAndroid Build Coastguard Worker     return mStrictModePolicy;
548*38e8c45fSAndroid Build Coastguard Worker }
549*38e8c45fSAndroid Build Coastguard Worker 
setCallingWorkSourceUid(uid_t uid)550*38e8c45fSAndroid Build Coastguard Worker int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
551*38e8c45fSAndroid Build Coastguard Worker {
552*38e8c45fSAndroid Build Coastguard Worker     int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
553*38e8c45fSAndroid Build Coastguard Worker     mPropagateWorkSource = true;
554*38e8c45fSAndroid Build Coastguard Worker     return token;
555*38e8c45fSAndroid Build Coastguard Worker }
556*38e8c45fSAndroid Build Coastguard Worker 
setCallingWorkSourceUidWithoutPropagation(uid_t uid)557*38e8c45fSAndroid Build Coastguard Worker int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
558*38e8c45fSAndroid Build Coastguard Worker {
559*38e8c45fSAndroid Build Coastguard Worker     const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
560*38e8c45fSAndroid Build Coastguard Worker     int64_t token = propagatedBit | mWorkSource;
561*38e8c45fSAndroid Build Coastguard Worker     mWorkSource = uid;
562*38e8c45fSAndroid Build Coastguard Worker     return token;
563*38e8c45fSAndroid Build Coastguard Worker }
564*38e8c45fSAndroid Build Coastguard Worker 
clearPropagateWorkSource()565*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::clearPropagateWorkSource()
566*38e8c45fSAndroid Build Coastguard Worker {
567*38e8c45fSAndroid Build Coastguard Worker     mPropagateWorkSource = false;
568*38e8c45fSAndroid Build Coastguard Worker }
569*38e8c45fSAndroid Build Coastguard Worker 
shouldPropagateWorkSource() const570*38e8c45fSAndroid Build Coastguard Worker bool IPCThreadState::shouldPropagateWorkSource() const
571*38e8c45fSAndroid Build Coastguard Worker {
572*38e8c45fSAndroid Build Coastguard Worker     return mPropagateWorkSource;
573*38e8c45fSAndroid Build Coastguard Worker }
574*38e8c45fSAndroid Build Coastguard Worker 
getCallingWorkSourceUid() const575*38e8c45fSAndroid Build Coastguard Worker uid_t IPCThreadState::getCallingWorkSourceUid() const
576*38e8c45fSAndroid Build Coastguard Worker {
577*38e8c45fSAndroid Build Coastguard Worker     return mWorkSource;
578*38e8c45fSAndroid Build Coastguard Worker }
579*38e8c45fSAndroid Build Coastguard Worker 
clearCallingWorkSource()580*38e8c45fSAndroid Build Coastguard Worker int64_t IPCThreadState::clearCallingWorkSource()
581*38e8c45fSAndroid Build Coastguard Worker {
582*38e8c45fSAndroid Build Coastguard Worker     return setCallingWorkSourceUid(kUnsetWorkSource);
583*38e8c45fSAndroid Build Coastguard Worker }
584*38e8c45fSAndroid Build Coastguard Worker 
restoreCallingWorkSource(int64_t token)585*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::restoreCallingWorkSource(int64_t token)
586*38e8c45fSAndroid Build Coastguard Worker {
587*38e8c45fSAndroid Build Coastguard Worker     uid_t uid = (int)token;
588*38e8c45fSAndroid Build Coastguard Worker     setCallingWorkSourceUidWithoutPropagation(uid);
589*38e8c45fSAndroid Build Coastguard Worker     mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
590*38e8c45fSAndroid Build Coastguard Worker }
591*38e8c45fSAndroid Build Coastguard Worker 
setLastTransactionBinderFlags(int32_t flags)592*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
593*38e8c45fSAndroid Build Coastguard Worker {
594*38e8c45fSAndroid Build Coastguard Worker     mLastTransactionBinderFlags = flags;
595*38e8c45fSAndroid Build Coastguard Worker }
596*38e8c45fSAndroid Build Coastguard Worker 
getLastTransactionBinderFlags() const597*38e8c45fSAndroid Build Coastguard Worker int32_t IPCThreadState::getLastTransactionBinderFlags() const
598*38e8c45fSAndroid Build Coastguard Worker {
599*38e8c45fSAndroid Build Coastguard Worker     return mLastTransactionBinderFlags;
600*38e8c45fSAndroid Build Coastguard Worker }
601*38e8c45fSAndroid Build Coastguard Worker 
setCallRestriction(ProcessState::CallRestriction restriction)602*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) {
603*38e8c45fSAndroid Build Coastguard Worker     mCallRestriction = restriction;
604*38e8c45fSAndroid Build Coastguard Worker }
605*38e8c45fSAndroid Build Coastguard Worker 
getCallRestriction() const606*38e8c45fSAndroid Build Coastguard Worker ProcessState::CallRestriction IPCThreadState::getCallRestriction() const {
607*38e8c45fSAndroid Build Coastguard Worker     return mCallRestriction;
608*38e8c45fSAndroid Build Coastguard Worker }
609*38e8c45fSAndroid Build Coastguard Worker 
restoreCallingIdentity(int64_t token)610*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::restoreCallingIdentity(int64_t token)
611*38e8c45fSAndroid Build Coastguard Worker {
612*38e8c45fSAndroid Build Coastguard Worker     mCallingUid = unpackCallingUid(token);
613*38e8c45fSAndroid Build Coastguard Worker     mCallingSid = nullptr;  // not enough data to restore
614*38e8c45fSAndroid Build Coastguard Worker     mCallingPid = unpackCallingPid(token);
615*38e8c45fSAndroid Build Coastguard Worker     mHasExplicitIdentity = unpackHasExplicitIdentity(token);
616*38e8c45fSAndroid Build Coastguard Worker }
617*38e8c45fSAndroid Build Coastguard Worker 
clearCaller()618*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::clearCaller()
619*38e8c45fSAndroid Build Coastguard Worker {
620*38e8c45fSAndroid Build Coastguard Worker     mCallingPid = getpid();
621*38e8c45fSAndroid Build Coastguard Worker     mCallingSid = nullptr;  // expensive to lookup
622*38e8c45fSAndroid Build Coastguard Worker     mCallingUid = getuid();
623*38e8c45fSAndroid Build Coastguard Worker }
624*38e8c45fSAndroid Build Coastguard Worker 
flushCommands()625*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::flushCommands()
626*38e8c45fSAndroid Build Coastguard Worker {
627*38e8c45fSAndroid Build Coastguard Worker     if (mProcess->mDriverFD < 0)
628*38e8c45fSAndroid Build Coastguard Worker         return;
629*38e8c45fSAndroid Build Coastguard Worker     talkWithDriver(false);
630*38e8c45fSAndroid Build Coastguard Worker     // The flush could have caused post-write refcount decrements to have
631*38e8c45fSAndroid Build Coastguard Worker     // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
632*38e8c45fSAndroid Build Coastguard Worker     // being queued in mOut. So flush again, if we need to.
633*38e8c45fSAndroid Build Coastguard Worker     if (mOut.dataSize() > 0) {
634*38e8c45fSAndroid Build Coastguard Worker         talkWithDriver(false);
635*38e8c45fSAndroid Build Coastguard Worker     }
636*38e8c45fSAndroid Build Coastguard Worker     if (mOut.dataSize() > 0) {
637*38e8c45fSAndroid Build Coastguard Worker         ALOGW("mOut.dataSize() > 0 after flushCommands()");
638*38e8c45fSAndroid Build Coastguard Worker     }
639*38e8c45fSAndroid Build Coastguard Worker }
640*38e8c45fSAndroid Build Coastguard Worker 
flushIfNeeded()641*38e8c45fSAndroid Build Coastguard Worker bool IPCThreadState::flushIfNeeded()
642*38e8c45fSAndroid Build Coastguard Worker {
643*38e8c45fSAndroid Build Coastguard Worker     if (mIsLooper || mServingStackPointer != nullptr || mIsFlushing) {
644*38e8c45fSAndroid Build Coastguard Worker         return false;
645*38e8c45fSAndroid Build Coastguard Worker     }
646*38e8c45fSAndroid Build Coastguard Worker     mIsFlushing = true;
647*38e8c45fSAndroid Build Coastguard Worker     // In case this thread is not a looper and is not currently serving a binder transaction,
648*38e8c45fSAndroid Build Coastguard Worker     // there's no guarantee that this thread will call back into the kernel driver any time
649*38e8c45fSAndroid Build Coastguard Worker     // soon. Therefore, flush pending commands such as BC_FREE_BUFFER, to prevent them from getting
650*38e8c45fSAndroid Build Coastguard Worker     // stuck in this thread's out buffer.
651*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
652*38e8c45fSAndroid Build Coastguard Worker     mIsFlushing = false;
653*38e8c45fSAndroid Build Coastguard Worker     return true;
654*38e8c45fSAndroid Build Coastguard Worker }
655*38e8c45fSAndroid Build Coastguard Worker 
blockUntilThreadAvailable()656*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::blockUntilThreadAvailable()
657*38e8c45fSAndroid Build Coastguard Worker {
658*38e8c45fSAndroid Build Coastguard Worker     std::unique_lock lock_guard_(mProcess->mOnThreadAvailableLock);
659*38e8c45fSAndroid Build Coastguard Worker     mProcess->mOnThreadAvailableWaiting++;
660*38e8c45fSAndroid Build Coastguard Worker     mProcess->mOnThreadAvailableCondVar.wait(lock_guard_, [&] {
661*38e8c45fSAndroid Build Coastguard Worker         size_t max = mProcess->mMaxThreads;
662*38e8c45fSAndroid Build Coastguard Worker         size_t cur = mProcess->mExecutingThreadsCount;
663*38e8c45fSAndroid Build Coastguard Worker         if (cur < max) {
664*38e8c45fSAndroid Build Coastguard Worker             return true;
665*38e8c45fSAndroid Build Coastguard Worker         }
666*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%zu mMaxThreads=%zu\n", cur,
667*38e8c45fSAndroid Build Coastguard Worker               max);
668*38e8c45fSAndroid Build Coastguard Worker         return false;
669*38e8c45fSAndroid Build Coastguard Worker     });
670*38e8c45fSAndroid Build Coastguard Worker     mProcess->mOnThreadAvailableWaiting--;
671*38e8c45fSAndroid Build Coastguard Worker }
672*38e8c45fSAndroid Build Coastguard Worker 
getAndExecuteCommand()673*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::getAndExecuteCommand()
674*38e8c45fSAndroid Build Coastguard Worker {
675*38e8c45fSAndroid Build Coastguard Worker     status_t result;
676*38e8c45fSAndroid Build Coastguard Worker     int32_t cmd;
677*38e8c45fSAndroid Build Coastguard Worker 
678*38e8c45fSAndroid Build Coastguard Worker     result = talkWithDriver();
679*38e8c45fSAndroid Build Coastguard Worker     if (result >= NO_ERROR) {
680*38e8c45fSAndroid Build Coastguard Worker         size_t IN = mIn.dataAvail();
681*38e8c45fSAndroid Build Coastguard Worker         if (IN < sizeof(int32_t)) return result;
682*38e8c45fSAndroid Build Coastguard Worker         cmd = mIn.readInt32();
683*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_COMMANDS() {
684*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
685*38e8c45fSAndroid Build Coastguard Worker             logStream << "Processing top-level Command: " << getReturnString(cmd) << "\n";
686*38e8c45fSAndroid Build Coastguard Worker             std::string message = logStream.str();
687*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", message.c_str());
688*38e8c45fSAndroid Build Coastguard Worker         }
689*38e8c45fSAndroid Build Coastguard Worker 
690*38e8c45fSAndroid Build Coastguard Worker         size_t newThreadsCount = mProcess->mExecutingThreadsCount.fetch_add(1) + 1;
691*38e8c45fSAndroid Build Coastguard Worker         if (newThreadsCount >= mProcess->mMaxThreads) {
692*38e8c45fSAndroid Build Coastguard Worker             auto expected = ProcessState::never();
693*38e8c45fSAndroid Build Coastguard Worker             mProcess->mStarvationStartTime
694*38e8c45fSAndroid Build Coastguard Worker                     .compare_exchange_strong(expected, std::chrono::steady_clock::now());
695*38e8c45fSAndroid Build Coastguard Worker         }
696*38e8c45fSAndroid Build Coastguard Worker 
697*38e8c45fSAndroid Build Coastguard Worker         result = executeCommand(cmd);
698*38e8c45fSAndroid Build Coastguard Worker 
699*38e8c45fSAndroid Build Coastguard Worker         size_t maxThreads = mProcess->mMaxThreads;
700*38e8c45fSAndroid Build Coastguard Worker         newThreadsCount = mProcess->mExecutingThreadsCount.fetch_sub(1) - 1;
701*38e8c45fSAndroid Build Coastguard Worker         if (newThreadsCount < maxThreads) {
702*38e8c45fSAndroid Build Coastguard Worker             auto starvationStartTime =
703*38e8c45fSAndroid Build Coastguard Worker                     mProcess->mStarvationStartTime.exchange(ProcessState::never());
704*38e8c45fSAndroid Build Coastguard Worker             if (starvationStartTime != ProcessState::never()) {
705*38e8c45fSAndroid Build Coastguard Worker                 auto starvationTime = std::chrono::steady_clock::now() - starvationStartTime;
706*38e8c45fSAndroid Build Coastguard Worker                 if (starvationTime > 100ms) {
707*38e8c45fSAndroid Build Coastguard Worker                     ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", maxThreads,
708*38e8c45fSAndroid Build Coastguard Worker                           to_ms(starvationTime));
709*38e8c45fSAndroid Build Coastguard Worker                 }
710*38e8c45fSAndroid Build Coastguard Worker             }
711*38e8c45fSAndroid Build Coastguard Worker         }
712*38e8c45fSAndroid Build Coastguard Worker 
713*38e8c45fSAndroid Build Coastguard Worker         // Cond broadcast can be expensive, so don't send it every time a binder
714*38e8c45fSAndroid Build Coastguard Worker         // call is processed. b/168806193
715*38e8c45fSAndroid Build Coastguard Worker         if (mProcess->mOnThreadAvailableWaiting > 0) {
716*38e8c45fSAndroid Build Coastguard Worker             std::lock_guard lock_guard_(mProcess->mOnThreadAvailableLock);
717*38e8c45fSAndroid Build Coastguard Worker             mProcess->mOnThreadAvailableCondVar.notify_all();
718*38e8c45fSAndroid Build Coastguard Worker         }
719*38e8c45fSAndroid Build Coastguard Worker     }
720*38e8c45fSAndroid Build Coastguard Worker 
721*38e8c45fSAndroid Build Coastguard Worker     return result;
722*38e8c45fSAndroid Build Coastguard Worker }
723*38e8c45fSAndroid Build Coastguard Worker 
724*38e8c45fSAndroid Build Coastguard Worker // When we've cleared the incoming command queue, process any pending derefs
processPendingDerefs()725*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::processPendingDerefs()
726*38e8c45fSAndroid Build Coastguard Worker {
727*38e8c45fSAndroid Build Coastguard Worker     if (mIn.dataPosition() >= mIn.dataSize()) {
728*38e8c45fSAndroid Build Coastguard Worker         /*
729*38e8c45fSAndroid Build Coastguard Worker          * The decWeak()/decStrong() calls may cause a destructor to run,
730*38e8c45fSAndroid Build Coastguard Worker          * which in turn could have initiated an outgoing transaction,
731*38e8c45fSAndroid Build Coastguard Worker          * which in turn could cause us to add to the pending refs
732*38e8c45fSAndroid Build Coastguard Worker          * vectors; so instead of simply iterating, loop until they're empty.
733*38e8c45fSAndroid Build Coastguard Worker          *
734*38e8c45fSAndroid Build Coastguard Worker          * We do this in an outer loop, because calling decStrong()
735*38e8c45fSAndroid Build Coastguard Worker          * may result in something being added to mPendingWeakDerefs,
736*38e8c45fSAndroid Build Coastguard Worker          * which could be delayed until the next incoming command
737*38e8c45fSAndroid Build Coastguard Worker          * from the driver if we don't process it now.
738*38e8c45fSAndroid Build Coastguard Worker          */
739*38e8c45fSAndroid Build Coastguard Worker         while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
740*38e8c45fSAndroid Build Coastguard Worker             while (mPendingWeakDerefs.size() > 0) {
741*38e8c45fSAndroid Build Coastguard Worker                 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
742*38e8c45fSAndroid Build Coastguard Worker                 mPendingWeakDerefs.removeAt(0);
743*38e8c45fSAndroid Build Coastguard Worker                 refs->decWeak(mProcess.get());
744*38e8c45fSAndroid Build Coastguard Worker             }
745*38e8c45fSAndroid Build Coastguard Worker 
746*38e8c45fSAndroid Build Coastguard Worker             if (mPendingStrongDerefs.size() > 0) {
747*38e8c45fSAndroid Build Coastguard Worker                 // We don't use while() here because we don't want to re-order
748*38e8c45fSAndroid Build Coastguard Worker                 // strong and weak decs at all; if this decStrong() causes both a
749*38e8c45fSAndroid Build Coastguard Worker                 // decWeak() and a decStrong() to be queued, we want to process
750*38e8c45fSAndroid Build Coastguard Worker                 // the decWeak() first.
751*38e8c45fSAndroid Build Coastguard Worker                 BBinder* obj = mPendingStrongDerefs[0];
752*38e8c45fSAndroid Build Coastguard Worker                 mPendingStrongDerefs.removeAt(0);
753*38e8c45fSAndroid Build Coastguard Worker                 obj->decStrong(mProcess.get());
754*38e8c45fSAndroid Build Coastguard Worker             }
755*38e8c45fSAndroid Build Coastguard Worker         }
756*38e8c45fSAndroid Build Coastguard Worker     }
757*38e8c45fSAndroid Build Coastguard Worker }
758*38e8c45fSAndroid Build Coastguard Worker 
processPostWriteDerefs()759*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::processPostWriteDerefs()
760*38e8c45fSAndroid Build Coastguard Worker {
761*38e8c45fSAndroid Build Coastguard Worker     for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
762*38e8c45fSAndroid Build Coastguard Worker         RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
763*38e8c45fSAndroid Build Coastguard Worker         refs->decWeak(mProcess.get());
764*38e8c45fSAndroid Build Coastguard Worker     }
765*38e8c45fSAndroid Build Coastguard Worker     mPostWriteWeakDerefs.clear();
766*38e8c45fSAndroid Build Coastguard Worker 
767*38e8c45fSAndroid Build Coastguard Worker     for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
768*38e8c45fSAndroid Build Coastguard Worker         RefBase* obj = mPostWriteStrongDerefs[i];
769*38e8c45fSAndroid Build Coastguard Worker         obj->decStrong(mProcess.get());
770*38e8c45fSAndroid Build Coastguard Worker     }
771*38e8c45fSAndroid Build Coastguard Worker     mPostWriteStrongDerefs.clear();
772*38e8c45fSAndroid Build Coastguard Worker }
773*38e8c45fSAndroid Build Coastguard Worker 
joinThreadPool(bool isMain)774*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::joinThreadPool(bool isMain)
775*38e8c45fSAndroid Build Coastguard Worker {
776*38e8c45fSAndroid Build Coastguard Worker     LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(),
777*38e8c45fSAndroid Build Coastguard Worker                    getpid());
778*38e8c45fSAndroid Build Coastguard Worker     mProcess->mCurrentThreads++;
779*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
780*38e8c45fSAndroid Build Coastguard Worker 
781*38e8c45fSAndroid Build Coastguard Worker     mIsLooper = true;
782*38e8c45fSAndroid Build Coastguard Worker     status_t result;
783*38e8c45fSAndroid Build Coastguard Worker     do {
784*38e8c45fSAndroid Build Coastguard Worker         processPendingDerefs();
785*38e8c45fSAndroid Build Coastguard Worker         // now get the next command to be processed, waiting if necessary
786*38e8c45fSAndroid Build Coastguard Worker         result = getAndExecuteCommand();
787*38e8c45fSAndroid Build Coastguard Worker 
788*38e8c45fSAndroid Build Coastguard Worker         if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
789*38e8c45fSAndroid Build Coastguard Worker             LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
790*38e8c45fSAndroid Build Coastguard Worker                   mProcess->mDriverFD, result);
791*38e8c45fSAndroid Build Coastguard Worker         }
792*38e8c45fSAndroid Build Coastguard Worker 
793*38e8c45fSAndroid Build Coastguard Worker         // Let this thread exit the thread pool if it is no longer
794*38e8c45fSAndroid Build Coastguard Worker         // needed and it is not the main process thread.
795*38e8c45fSAndroid Build Coastguard Worker         if(result == TIMED_OUT && !isMain) {
796*38e8c45fSAndroid Build Coastguard Worker             break;
797*38e8c45fSAndroid Build Coastguard Worker         }
798*38e8c45fSAndroid Build Coastguard Worker     } while (result != -ECONNREFUSED && result != -EBADF);
799*38e8c45fSAndroid Build Coastguard Worker 
800*38e8c45fSAndroid Build Coastguard Worker     LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
801*38e8c45fSAndroid Build Coastguard Worker         (void*)pthread_self(), getpid(), result);
802*38e8c45fSAndroid Build Coastguard Worker 
803*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_EXIT_LOOPER);
804*38e8c45fSAndroid Build Coastguard Worker     mIsLooper = false;
805*38e8c45fSAndroid Build Coastguard Worker     talkWithDriver(false);
806*38e8c45fSAndroid Build Coastguard Worker     size_t oldCount = mProcess->mCurrentThreads.fetch_sub(1);
807*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(oldCount == 0,
808*38e8c45fSAndroid Build Coastguard Worker                         "Threadpool thread count underflowed. Thread cannot exist and exit in "
809*38e8c45fSAndroid Build Coastguard Worker                         "empty threadpool\n"
810*38e8c45fSAndroid Build Coastguard Worker                         "Misconfiguration. Increase threadpool max threads configuration\n");
811*38e8c45fSAndroid Build Coastguard Worker }
812*38e8c45fSAndroid Build Coastguard Worker 
setupPolling(int * fd)813*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::setupPolling(int* fd)
814*38e8c45fSAndroid Build Coastguard Worker {
815*38e8c45fSAndroid Build Coastguard Worker     if (mProcess->mDriverFD < 0) {
816*38e8c45fSAndroid Build Coastguard Worker         return -EBADF;
817*38e8c45fSAndroid Build Coastguard Worker     }
818*38e8c45fSAndroid Build Coastguard Worker 
819*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_ENTER_LOOPER);
820*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
821*38e8c45fSAndroid Build Coastguard Worker     *fd = mProcess->mDriverFD;
822*38e8c45fSAndroid Build Coastguard Worker     mProcess->mCurrentThreads++;
823*38e8c45fSAndroid Build Coastguard Worker     return 0;
824*38e8c45fSAndroid Build Coastguard Worker }
825*38e8c45fSAndroid Build Coastguard Worker 
handlePolledCommands()826*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::handlePolledCommands()
827*38e8c45fSAndroid Build Coastguard Worker {
828*38e8c45fSAndroid Build Coastguard Worker     status_t result;
829*38e8c45fSAndroid Build Coastguard Worker 
830*38e8c45fSAndroid Build Coastguard Worker     do {
831*38e8c45fSAndroid Build Coastguard Worker         result = getAndExecuteCommand();
832*38e8c45fSAndroid Build Coastguard Worker     } while (mIn.dataPosition() < mIn.dataSize());
833*38e8c45fSAndroid Build Coastguard Worker 
834*38e8c45fSAndroid Build Coastguard Worker     processPendingDerefs();
835*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
836*38e8c45fSAndroid Build Coastguard Worker     return result;
837*38e8c45fSAndroid Build Coastguard Worker }
838*38e8c45fSAndroid Build Coastguard Worker 
stopProcess(bool)839*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::stopProcess(bool /*immediate*/)
840*38e8c45fSAndroid Build Coastguard Worker {
841*38e8c45fSAndroid Build Coastguard Worker     //ALOGI("**** STOPPING PROCESS");
842*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
843*38e8c45fSAndroid Build Coastguard Worker     int fd = mProcess->mDriverFD;
844*38e8c45fSAndroid Build Coastguard Worker     mProcess->mDriverFD = -1;
845*38e8c45fSAndroid Build Coastguard Worker     close(fd);
846*38e8c45fSAndroid Build Coastguard Worker     //kill(getpid(), SIGKILL);
847*38e8c45fSAndroid Build Coastguard Worker }
848*38e8c45fSAndroid Build Coastguard Worker 
transact(int32_t handle,uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)849*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::transact(int32_t handle,
850*38e8c45fSAndroid Build Coastguard Worker                                   uint32_t code, const Parcel& data,
851*38e8c45fSAndroid Build Coastguard Worker                                   Parcel* reply, uint32_t flags)
852*38e8c45fSAndroid Build Coastguard Worker {
853*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(data.isForRpc(), "Parcel constructed for RPC, but being used with binder.");
854*38e8c45fSAndroid Build Coastguard Worker 
855*38e8c45fSAndroid Build Coastguard Worker     status_t err;
856*38e8c45fSAndroid Build Coastguard Worker 
857*38e8c45fSAndroid Build Coastguard Worker     flags |= TF_ACCEPT_FDS;
858*38e8c45fSAndroid Build Coastguard Worker 
859*38e8c45fSAndroid Build Coastguard Worker     IF_LOG_TRANSACTIONS() {
860*38e8c45fSAndroid Build Coastguard Worker         std::ostringstream logStream;
861*38e8c45fSAndroid Build Coastguard Worker         logStream << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " << handle
862*38e8c45fSAndroid Build Coastguard Worker                   << " / code " << TypeCode(code) << ": \t" << data << "\n";
863*38e8c45fSAndroid Build Coastguard Worker         std::string message = logStream.str();
864*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s", message.c_str());
865*38e8c45fSAndroid Build Coastguard Worker     }
866*38e8c45fSAndroid Build Coastguard Worker 
867*38e8c45fSAndroid Build Coastguard Worker     LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
868*38e8c45fSAndroid Build Coastguard Worker         (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
869*38e8c45fSAndroid Build Coastguard Worker     err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
870*38e8c45fSAndroid Build Coastguard Worker 
871*38e8c45fSAndroid Build Coastguard Worker     if (err != NO_ERROR) {
872*38e8c45fSAndroid Build Coastguard Worker         if (reply) reply->setError(err);
873*38e8c45fSAndroid Build Coastguard Worker         return (mLastError = err);
874*38e8c45fSAndroid Build Coastguard Worker     }
875*38e8c45fSAndroid Build Coastguard Worker 
876*38e8c45fSAndroid Build Coastguard Worker     if ((flags & TF_ONE_WAY) == 0) {
877*38e8c45fSAndroid Build Coastguard Worker         if (mCallRestriction != ProcessState::CallRestriction::NONE) [[unlikely]] {
878*38e8c45fSAndroid Build Coastguard Worker             if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
879*38e8c45fSAndroid Build Coastguard Worker                 ALOGE("Process making non-oneway call (code: %u) but is restricted.", code);
880*38e8c45fSAndroid Build Coastguard Worker                 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
881*38e8c45fSAndroid Build Coastguard Worker                     ANDROID_LOG_ERROR);
882*38e8c45fSAndroid Build Coastguard Worker             } else /* FATAL_IF_NOT_ONEWAY */ {
883*38e8c45fSAndroid Build Coastguard Worker                 LOG_ALWAYS_FATAL("Process may not make non-oneway calls (code: %u).", code);
884*38e8c45fSAndroid Build Coastguard Worker             }
885*38e8c45fSAndroid Build Coastguard Worker         }
886*38e8c45fSAndroid Build Coastguard Worker 
887*38e8c45fSAndroid Build Coastguard Worker #if 0
888*38e8c45fSAndroid Build Coastguard Worker         if (code == 4) { // relayout
889*38e8c45fSAndroid Build Coastguard Worker             ALOGI(">>>>>> CALLING transaction 4");
890*38e8c45fSAndroid Build Coastguard Worker         } else {
891*38e8c45fSAndroid Build Coastguard Worker             ALOGI(">>>>>> CALLING transaction %d", code);
892*38e8c45fSAndroid Build Coastguard Worker         }
893*38e8c45fSAndroid Build Coastguard Worker #endif
894*38e8c45fSAndroid Build Coastguard Worker         if (reply) {
895*38e8c45fSAndroid Build Coastguard Worker             err = waitForResponse(reply);
896*38e8c45fSAndroid Build Coastguard Worker         } else {
897*38e8c45fSAndroid Build Coastguard Worker             Parcel fakeReply;
898*38e8c45fSAndroid Build Coastguard Worker             err = waitForResponse(&fakeReply);
899*38e8c45fSAndroid Build Coastguard Worker         }
900*38e8c45fSAndroid Build Coastguard Worker         #if 0
901*38e8c45fSAndroid Build Coastguard Worker         if (code == 4) { // relayout
902*38e8c45fSAndroid Build Coastguard Worker             ALOGI("<<<<<< RETURNING transaction 4");
903*38e8c45fSAndroid Build Coastguard Worker         } else {
904*38e8c45fSAndroid Build Coastguard Worker             ALOGI("<<<<<< RETURNING transaction %d", code);
905*38e8c45fSAndroid Build Coastguard Worker         }
906*38e8c45fSAndroid Build Coastguard Worker         #endif
907*38e8c45fSAndroid Build Coastguard Worker 
908*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_TRANSACTIONS() {
909*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
910*38e8c45fSAndroid Build Coastguard Worker             logStream << "BR_REPLY thr " << (void*)pthread_self() << " / hand " << handle << ": ";
911*38e8c45fSAndroid Build Coastguard Worker             if (reply)
912*38e8c45fSAndroid Build Coastguard Worker                 logStream << "\t" << *reply << "\n";
913*38e8c45fSAndroid Build Coastguard Worker             else
914*38e8c45fSAndroid Build Coastguard Worker                 logStream << "(none requested)"
915*38e8c45fSAndroid Build Coastguard Worker                           << "\n";
916*38e8c45fSAndroid Build Coastguard Worker             std::string message = logStream.str();
917*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", message.c_str());
918*38e8c45fSAndroid Build Coastguard Worker         }
919*38e8c45fSAndroid Build Coastguard Worker     } else {
920*38e8c45fSAndroid Build Coastguard Worker         err = waitForResponse(nullptr, nullptr);
921*38e8c45fSAndroid Build Coastguard Worker     }
922*38e8c45fSAndroid Build Coastguard Worker 
923*38e8c45fSAndroid Build Coastguard Worker     return err;
924*38e8c45fSAndroid Build Coastguard Worker }
925*38e8c45fSAndroid Build Coastguard Worker 
incStrongHandle(int32_t handle,BpBinder * proxy)926*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
927*38e8c45fSAndroid Build Coastguard Worker {
928*38e8c45fSAndroid Build Coastguard Worker     LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
929*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_ACQUIRE);
930*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(handle);
931*38e8c45fSAndroid Build Coastguard Worker     if (!flushIfNeeded()) {
932*38e8c45fSAndroid Build Coastguard Worker         // Create a temp reference until the driver has handled this command.
933*38e8c45fSAndroid Build Coastguard Worker         proxy->incStrong(mProcess.get());
934*38e8c45fSAndroid Build Coastguard Worker         mPostWriteStrongDerefs.push(proxy);
935*38e8c45fSAndroid Build Coastguard Worker     }
936*38e8c45fSAndroid Build Coastguard Worker }
937*38e8c45fSAndroid Build Coastguard Worker 
decStrongHandle(int32_t handle)938*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::decStrongHandle(int32_t handle)
939*38e8c45fSAndroid Build Coastguard Worker {
940*38e8c45fSAndroid Build Coastguard Worker     LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
941*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_RELEASE);
942*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(handle);
943*38e8c45fSAndroid Build Coastguard Worker     flushIfNeeded();
944*38e8c45fSAndroid Build Coastguard Worker }
945*38e8c45fSAndroid Build Coastguard Worker 
incWeakHandle(int32_t handle,BpBinder * proxy)946*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
947*38e8c45fSAndroid Build Coastguard Worker {
948*38e8c45fSAndroid Build Coastguard Worker     LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
949*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_INCREFS);
950*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(handle);
951*38e8c45fSAndroid Build Coastguard Worker     if (!flushIfNeeded()) {
952*38e8c45fSAndroid Build Coastguard Worker         // Create a temp reference until the driver has handled this command.
953*38e8c45fSAndroid Build Coastguard Worker         proxy->getWeakRefs()->incWeak(mProcess.get());
954*38e8c45fSAndroid Build Coastguard Worker         mPostWriteWeakDerefs.push(proxy->getWeakRefs());
955*38e8c45fSAndroid Build Coastguard Worker     }
956*38e8c45fSAndroid Build Coastguard Worker }
957*38e8c45fSAndroid Build Coastguard Worker 
decWeakHandle(int32_t handle)958*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::decWeakHandle(int32_t handle)
959*38e8c45fSAndroid Build Coastguard Worker {
960*38e8c45fSAndroid Build Coastguard Worker     LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
961*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_DECREFS);
962*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(handle);
963*38e8c45fSAndroid Build Coastguard Worker     flushIfNeeded();
964*38e8c45fSAndroid Build Coastguard Worker }
965*38e8c45fSAndroid Build Coastguard Worker 
attemptIncStrongHandle(int32_t handle)966*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) {
967*38e8c45fSAndroid Build Coastguard Worker     (void)handle;
968*38e8c45fSAndroid Build Coastguard Worker     ALOGE("%s(%d): Not supported\n", __func__, handle);
969*38e8c45fSAndroid Build Coastguard Worker     return INVALID_OPERATION;
970*38e8c45fSAndroid Build Coastguard Worker }
971*38e8c45fSAndroid Build Coastguard Worker 
expungeHandle(int32_t handle,IBinder * binder)972*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
973*38e8c45fSAndroid Build Coastguard Worker {
974*38e8c45fSAndroid Build Coastguard Worker #if LOG_REFCOUNTS
975*38e8c45fSAndroid Build Coastguard Worker     ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
976*38e8c45fSAndroid Build Coastguard Worker #endif
977*38e8c45fSAndroid Build Coastguard Worker     self()->mProcess->expungeHandle(handle, binder); // NOLINT
978*38e8c45fSAndroid Build Coastguard Worker }
979*38e8c45fSAndroid Build Coastguard Worker 
requestDeathNotification(int32_t handle,BpBinder * proxy)980*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
981*38e8c45fSAndroid Build Coastguard Worker {
982*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
983*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32((int32_t)handle);
984*38e8c45fSAndroid Build Coastguard Worker     mOut.writePointer((uintptr_t)proxy);
985*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
986*38e8c45fSAndroid Build Coastguard Worker }
987*38e8c45fSAndroid Build Coastguard Worker 
clearDeathNotification(int32_t handle,BpBinder * proxy)988*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
989*38e8c45fSAndroid Build Coastguard Worker {
990*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
991*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32((int32_t)handle);
992*38e8c45fSAndroid Build Coastguard Worker     mOut.writePointer((uintptr_t)proxy);
993*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
994*38e8c45fSAndroid Build Coastguard Worker }
995*38e8c45fSAndroid Build Coastguard Worker 
addFrozenStateChangeCallback(int32_t handle,BpBinder * proxy)996*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::addFrozenStateChangeCallback(int32_t handle, BpBinder* proxy) {
997*38e8c45fSAndroid Build Coastguard Worker     static bool isSupported =
998*38e8c45fSAndroid Build Coastguard Worker             ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::FREEZE_NOTIFICATION);
999*38e8c45fSAndroid Build Coastguard Worker     if (!isSupported) {
1000*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
1001*38e8c45fSAndroid Build Coastguard Worker     }
1002*38e8c45fSAndroid Build Coastguard Worker     proxy->getWeakRefs()->incWeak(proxy);
1003*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_REQUEST_FREEZE_NOTIFICATION);
1004*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32((int32_t)handle);
1005*38e8c45fSAndroid Build Coastguard Worker     mOut.writePointer((uintptr_t)proxy);
1006*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
1007*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
1008*38e8c45fSAndroid Build Coastguard Worker }
1009*38e8c45fSAndroid Build Coastguard Worker 
removeFrozenStateChangeCallback(int32_t handle,BpBinder * proxy)1010*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::removeFrozenStateChangeCallback(int32_t handle, BpBinder* proxy) {
1011*38e8c45fSAndroid Build Coastguard Worker     static bool isSupported =
1012*38e8c45fSAndroid Build Coastguard Worker             ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::FREEZE_NOTIFICATION);
1013*38e8c45fSAndroid Build Coastguard Worker     if (!isSupported) {
1014*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
1015*38e8c45fSAndroid Build Coastguard Worker     }
1016*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(BC_CLEAR_FREEZE_NOTIFICATION);
1017*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32((int32_t)handle);
1018*38e8c45fSAndroid Build Coastguard Worker     mOut.writePointer((uintptr_t)proxy);
1019*38e8c45fSAndroid Build Coastguard Worker     flushCommands();
1020*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
1021*38e8c45fSAndroid Build Coastguard Worker }
1022*38e8c45fSAndroid Build Coastguard Worker 
IPCThreadState()1023*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::IPCThreadState()
1024*38e8c45fSAndroid Build Coastguard Worker       : mProcess(ProcessState::self()),
1025*38e8c45fSAndroid Build Coastguard Worker         mServingStackPointer(nullptr),
1026*38e8c45fSAndroid Build Coastguard Worker         mServingStackPointerGuard(nullptr),
1027*38e8c45fSAndroid Build Coastguard Worker         mWorkSource(kUnsetWorkSource),
1028*38e8c45fSAndroid Build Coastguard Worker         mPropagateWorkSource(false),
1029*38e8c45fSAndroid Build Coastguard Worker         mIsLooper(false),
1030*38e8c45fSAndroid Build Coastguard Worker         mIsFlushing(false),
1031*38e8c45fSAndroid Build Coastguard Worker         mStrictModePolicy(0),
1032*38e8c45fSAndroid Build Coastguard Worker         mLastTransactionBinderFlags(0),
1033*38e8c45fSAndroid Build Coastguard Worker         mCallRestriction(mProcess->mCallRestriction) {
1034*38e8c45fSAndroid Build Coastguard Worker     pthread_setspecific(gTLS, this);
1035*38e8c45fSAndroid Build Coastguard Worker     clearCaller();
1036*38e8c45fSAndroid Build Coastguard Worker     mHasExplicitIdentity = false;
1037*38e8c45fSAndroid Build Coastguard Worker     mIn.setDataCapacity(256);
1038*38e8c45fSAndroid Build Coastguard Worker     mOut.setDataCapacity(256);
1039*38e8c45fSAndroid Build Coastguard Worker }
1040*38e8c45fSAndroid Build Coastguard Worker 
~IPCThreadState()1041*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::~IPCThreadState()
1042*38e8c45fSAndroid Build Coastguard Worker {
1043*38e8c45fSAndroid Build Coastguard Worker }
1044*38e8c45fSAndroid Build Coastguard Worker 
sendReply(const Parcel & reply,uint32_t flags)1045*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
1046*38e8c45fSAndroid Build Coastguard Worker {
1047*38e8c45fSAndroid Build Coastguard Worker     status_t err;
1048*38e8c45fSAndroid Build Coastguard Worker     status_t statusBuffer;
1049*38e8c45fSAndroid Build Coastguard Worker     err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
1050*38e8c45fSAndroid Build Coastguard Worker     if (err < NO_ERROR) return err;
1051*38e8c45fSAndroid Build Coastguard Worker 
1052*38e8c45fSAndroid Build Coastguard Worker     return waitForResponse(nullptr, nullptr);
1053*38e8c45fSAndroid Build Coastguard Worker }
1054*38e8c45fSAndroid Build Coastguard Worker 
waitForResponse(Parcel * reply,status_t * acquireResult)1055*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
1056*38e8c45fSAndroid Build Coastguard Worker {
1057*38e8c45fSAndroid Build Coastguard Worker     uint32_t cmd;
1058*38e8c45fSAndroid Build Coastguard Worker     int32_t err;
1059*38e8c45fSAndroid Build Coastguard Worker 
1060*38e8c45fSAndroid Build Coastguard Worker     while (1) {
1061*38e8c45fSAndroid Build Coastguard Worker         if ((err=talkWithDriver()) < NO_ERROR) break;
1062*38e8c45fSAndroid Build Coastguard Worker         err = mIn.errorCheck();
1063*38e8c45fSAndroid Build Coastguard Worker         if (err < NO_ERROR) break;
1064*38e8c45fSAndroid Build Coastguard Worker         if (mIn.dataAvail() == 0) continue;
1065*38e8c45fSAndroid Build Coastguard Worker 
1066*38e8c45fSAndroid Build Coastguard Worker         cmd = (uint32_t)mIn.readInt32();
1067*38e8c45fSAndroid Build Coastguard Worker 
1068*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_COMMANDS() {
1069*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
1070*38e8c45fSAndroid Build Coastguard Worker             logStream << "Processing waitForResponse Command: " << getReturnString(cmd) << "\n";
1071*38e8c45fSAndroid Build Coastguard Worker             std::string message = logStream.str();
1072*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", message.c_str());
1073*38e8c45fSAndroid Build Coastguard Worker         }
1074*38e8c45fSAndroid Build Coastguard Worker 
1075*38e8c45fSAndroid Build Coastguard Worker         switch (cmd) {
1076*38e8c45fSAndroid Build Coastguard Worker         case BR_ONEWAY_SPAM_SUSPECT:
1077*38e8c45fSAndroid Build Coastguard Worker             ALOGE("Process seems to be sending too many oneway calls.");
1078*38e8c45fSAndroid Build Coastguard Worker             CallStack::logStack("oneway spamming", CallStack::getCurrent().get(),
1079*38e8c45fSAndroid Build Coastguard Worker                     ANDROID_LOG_ERROR);
1080*38e8c45fSAndroid Build Coastguard Worker             [[fallthrough]];
1081*38e8c45fSAndroid Build Coastguard Worker         case BR_TRANSACTION_COMPLETE:
1082*38e8c45fSAndroid Build Coastguard Worker             if (!reply && !acquireResult) goto finish;
1083*38e8c45fSAndroid Build Coastguard Worker             break;
1084*38e8c45fSAndroid Build Coastguard Worker 
1085*38e8c45fSAndroid Build Coastguard Worker         case BR_TRANSACTION_PENDING_FROZEN:
1086*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Sending oneway calls to frozen process.");
1087*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1088*38e8c45fSAndroid Build Coastguard Worker 
1089*38e8c45fSAndroid Build Coastguard Worker         case BR_DEAD_REPLY:
1090*38e8c45fSAndroid Build Coastguard Worker             err = DEAD_OBJECT;
1091*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1092*38e8c45fSAndroid Build Coastguard Worker 
1093*38e8c45fSAndroid Build Coastguard Worker         case BR_FAILED_REPLY:
1094*38e8c45fSAndroid Build Coastguard Worker             err = FAILED_TRANSACTION;
1095*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1096*38e8c45fSAndroid Build Coastguard Worker 
1097*38e8c45fSAndroid Build Coastguard Worker         case BR_FROZEN_REPLY:
1098*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Transaction failed because process frozen.");
1099*38e8c45fSAndroid Build Coastguard Worker             err = FAILED_TRANSACTION;
1100*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1101*38e8c45fSAndroid Build Coastguard Worker 
1102*38e8c45fSAndroid Build Coastguard Worker         case BR_ACQUIRE_RESULT:
1103*38e8c45fSAndroid Build Coastguard Worker             {
1104*38e8c45fSAndroid Build Coastguard Worker                 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
1105*38e8c45fSAndroid Build Coastguard Worker                 const int32_t result = mIn.readInt32();
1106*38e8c45fSAndroid Build Coastguard Worker                 if (!acquireResult) continue;
1107*38e8c45fSAndroid Build Coastguard Worker                 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
1108*38e8c45fSAndroid Build Coastguard Worker             }
1109*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1110*38e8c45fSAndroid Build Coastguard Worker 
1111*38e8c45fSAndroid Build Coastguard Worker         case BR_REPLY:
1112*38e8c45fSAndroid Build Coastguard Worker             {
1113*38e8c45fSAndroid Build Coastguard Worker                 binder_transaction_data tr;
1114*38e8c45fSAndroid Build Coastguard Worker                 err = mIn.read(&tr, sizeof(tr));
1115*38e8c45fSAndroid Build Coastguard Worker                 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
1116*38e8c45fSAndroid Build Coastguard Worker                 if (err != NO_ERROR) goto finish;
1117*38e8c45fSAndroid Build Coastguard Worker 
1118*38e8c45fSAndroid Build Coastguard Worker                 if (reply) {
1119*38e8c45fSAndroid Build Coastguard Worker                     if ((tr.flags & TF_STATUS_CODE) == 0) {
1120*38e8c45fSAndroid Build Coastguard Worker                         reply->ipcSetDataReference(
1121*38e8c45fSAndroid Build Coastguard Worker                             reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1122*38e8c45fSAndroid Build Coastguard Worker                             tr.data_size,
1123*38e8c45fSAndroid Build Coastguard Worker                             reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1124*38e8c45fSAndroid Build Coastguard Worker                             tr.offsets_size/sizeof(binder_size_t),
1125*38e8c45fSAndroid Build Coastguard Worker                             freeBuffer);
1126*38e8c45fSAndroid Build Coastguard Worker                     } else {
1127*38e8c45fSAndroid Build Coastguard Worker                         err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
1128*38e8c45fSAndroid Build Coastguard Worker                         freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1129*38e8c45fSAndroid Build Coastguard Worker                                    tr.data_size,
1130*38e8c45fSAndroid Build Coastguard Worker                                    reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1131*38e8c45fSAndroid Build Coastguard Worker                                    tr.offsets_size / sizeof(binder_size_t));
1132*38e8c45fSAndroid Build Coastguard Worker                     }
1133*38e8c45fSAndroid Build Coastguard Worker                 } else {
1134*38e8c45fSAndroid Build Coastguard Worker                     freeBuffer(reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), tr.data_size,
1135*38e8c45fSAndroid Build Coastguard Worker                                reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1136*38e8c45fSAndroid Build Coastguard Worker                                tr.offsets_size / sizeof(binder_size_t));
1137*38e8c45fSAndroid Build Coastguard Worker                     continue;
1138*38e8c45fSAndroid Build Coastguard Worker                 }
1139*38e8c45fSAndroid Build Coastguard Worker             }
1140*38e8c45fSAndroid Build Coastguard Worker             goto finish;
1141*38e8c45fSAndroid Build Coastguard Worker 
1142*38e8c45fSAndroid Build Coastguard Worker         default:
1143*38e8c45fSAndroid Build Coastguard Worker             err = executeCommand(cmd);
1144*38e8c45fSAndroid Build Coastguard Worker             if (err != NO_ERROR) goto finish;
1145*38e8c45fSAndroid Build Coastguard Worker             break;
1146*38e8c45fSAndroid Build Coastguard Worker         }
1147*38e8c45fSAndroid Build Coastguard Worker     }
1148*38e8c45fSAndroid Build Coastguard Worker 
1149*38e8c45fSAndroid Build Coastguard Worker finish:
1150*38e8c45fSAndroid Build Coastguard Worker     if (err != NO_ERROR) {
1151*38e8c45fSAndroid Build Coastguard Worker         if (acquireResult) *acquireResult = err;
1152*38e8c45fSAndroid Build Coastguard Worker         if (reply) reply->setError(err);
1153*38e8c45fSAndroid Build Coastguard Worker         mLastError = err;
1154*38e8c45fSAndroid Build Coastguard Worker         logExtendedError();
1155*38e8c45fSAndroid Build Coastguard Worker     }
1156*38e8c45fSAndroid Build Coastguard Worker 
1157*38e8c45fSAndroid Build Coastguard Worker     return err;
1158*38e8c45fSAndroid Build Coastguard Worker }
1159*38e8c45fSAndroid Build Coastguard Worker 
talkWithDriver(bool doReceive)1160*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::talkWithDriver(bool doReceive)
1161*38e8c45fSAndroid Build Coastguard Worker {
1162*38e8c45fSAndroid Build Coastguard Worker     if (mProcess->mDriverFD < 0) {
1163*38e8c45fSAndroid Build Coastguard Worker         return -EBADF;
1164*38e8c45fSAndroid Build Coastguard Worker     }
1165*38e8c45fSAndroid Build Coastguard Worker 
1166*38e8c45fSAndroid Build Coastguard Worker     binder_write_read bwr;
1167*38e8c45fSAndroid Build Coastguard Worker 
1168*38e8c45fSAndroid Build Coastguard Worker     // Is the read buffer empty?
1169*38e8c45fSAndroid Build Coastguard Worker     const bool needRead = mIn.dataPosition() >= mIn.dataSize();
1170*38e8c45fSAndroid Build Coastguard Worker 
1171*38e8c45fSAndroid Build Coastguard Worker     // We don't want to write anything if we are still reading
1172*38e8c45fSAndroid Build Coastguard Worker     // from data left in the input buffer and the caller
1173*38e8c45fSAndroid Build Coastguard Worker     // has requested to read the next data.
1174*38e8c45fSAndroid Build Coastguard Worker     const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
1175*38e8c45fSAndroid Build Coastguard Worker 
1176*38e8c45fSAndroid Build Coastguard Worker     bwr.write_size = outAvail;
1177*38e8c45fSAndroid Build Coastguard Worker     bwr.write_buffer = (uintptr_t)mOut.data();
1178*38e8c45fSAndroid Build Coastguard Worker 
1179*38e8c45fSAndroid Build Coastguard Worker     // This is what we'll read.
1180*38e8c45fSAndroid Build Coastguard Worker     if (doReceive && needRead) {
1181*38e8c45fSAndroid Build Coastguard Worker         bwr.read_size = mIn.dataCapacity();
1182*38e8c45fSAndroid Build Coastguard Worker         bwr.read_buffer = (uintptr_t)mIn.data();
1183*38e8c45fSAndroid Build Coastguard Worker     } else {
1184*38e8c45fSAndroid Build Coastguard Worker         bwr.read_size = 0;
1185*38e8c45fSAndroid Build Coastguard Worker         bwr.read_buffer = 0;
1186*38e8c45fSAndroid Build Coastguard Worker     }
1187*38e8c45fSAndroid Build Coastguard Worker 
1188*38e8c45fSAndroid Build Coastguard Worker     IF_LOG_COMMANDS() {
1189*38e8c45fSAndroid Build Coastguard Worker         std::ostringstream logStream;
1190*38e8c45fSAndroid Build Coastguard Worker         if (outAvail != 0) {
1191*38e8c45fSAndroid Build Coastguard Worker             logStream << "Sending commands to driver: ";
1192*38e8c45fSAndroid Build Coastguard Worker             const void* cmds = (const void*)bwr.write_buffer;
1193*38e8c45fSAndroid Build Coastguard Worker             const void* end = ((const uint8_t*)cmds) + bwr.write_size;
1194*38e8c45fSAndroid Build Coastguard Worker             logStream << "\t" << HexDump(cmds, bwr.write_size) << "\n";
1195*38e8c45fSAndroid Build Coastguard Worker             while (cmds < end) cmds = printCommand(logStream, cmds);
1196*38e8c45fSAndroid Build Coastguard Worker         }
1197*38e8c45fSAndroid Build Coastguard Worker         logStream << "Size of receive buffer: " << bwr.read_size << ", needRead: " << needRead
1198*38e8c45fSAndroid Build Coastguard Worker                   << ", doReceive: " << doReceive << "\n";
1199*38e8c45fSAndroid Build Coastguard Worker 
1200*38e8c45fSAndroid Build Coastguard Worker         std::string message = logStream.str();
1201*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s", message.c_str());
1202*38e8c45fSAndroid Build Coastguard Worker     }
1203*38e8c45fSAndroid Build Coastguard Worker 
1204*38e8c45fSAndroid Build Coastguard Worker     // Return immediately if there is nothing to do.
1205*38e8c45fSAndroid Build Coastguard Worker     if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
1206*38e8c45fSAndroid Build Coastguard Worker 
1207*38e8c45fSAndroid Build Coastguard Worker     bwr.write_consumed = 0;
1208*38e8c45fSAndroid Build Coastguard Worker     bwr.read_consumed = 0;
1209*38e8c45fSAndroid Build Coastguard Worker     status_t err;
1210*38e8c45fSAndroid Build Coastguard Worker     do {
1211*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_COMMANDS() {
1212*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
1213*38e8c45fSAndroid Build Coastguard Worker             logStream << "About to read/write, write size = " << mOut.dataSize() << "\n";
1214*38e8c45fSAndroid Build Coastguard Worker             std::string message = logStream.str();
1215*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", message.c_str());
1216*38e8c45fSAndroid Build Coastguard Worker         }
1217*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
1218*38e8c45fSAndroid Build Coastguard Worker         if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
1219*38e8c45fSAndroid Build Coastguard Worker             err = NO_ERROR;
1220*38e8c45fSAndroid Build Coastguard Worker         else
1221*38e8c45fSAndroid Build Coastguard Worker             err = -errno;
1222*38e8c45fSAndroid Build Coastguard Worker #else
1223*38e8c45fSAndroid Build Coastguard Worker         err = INVALID_OPERATION;
1224*38e8c45fSAndroid Build Coastguard Worker #endif
1225*38e8c45fSAndroid Build Coastguard Worker         if (mProcess->mDriverFD < 0) {
1226*38e8c45fSAndroid Build Coastguard Worker             err = -EBADF;
1227*38e8c45fSAndroid Build Coastguard Worker         }
1228*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_COMMANDS() {
1229*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
1230*38e8c45fSAndroid Build Coastguard Worker             logStream << "Finished read/write, write size = " << mOut.dataSize() << "\n";
1231*38e8c45fSAndroid Build Coastguard Worker             std::string message = logStream.str();
1232*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", message.c_str());
1233*38e8c45fSAndroid Build Coastguard Worker         }
1234*38e8c45fSAndroid Build Coastguard Worker     } while (err == -EINTR);
1235*38e8c45fSAndroid Build Coastguard Worker 
1236*38e8c45fSAndroid Build Coastguard Worker     IF_LOG_COMMANDS() {
1237*38e8c45fSAndroid Build Coastguard Worker         std::ostringstream logStream;
1238*38e8c45fSAndroid Build Coastguard Worker         logStream << "Our err: " << (void*)(intptr_t)err
1239*38e8c45fSAndroid Build Coastguard Worker                   << ", write consumed: " << bwr.write_consumed << " (of " << mOut.dataSize()
1240*38e8c45fSAndroid Build Coastguard Worker                   << "), read consumed: " << bwr.read_consumed << "\n";
1241*38e8c45fSAndroid Build Coastguard Worker         std::string message = logStream.str();
1242*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s", message.c_str());
1243*38e8c45fSAndroid Build Coastguard Worker     }
1244*38e8c45fSAndroid Build Coastguard Worker 
1245*38e8c45fSAndroid Build Coastguard Worker     if (err >= NO_ERROR) {
1246*38e8c45fSAndroid Build Coastguard Worker         if (bwr.write_consumed > 0) {
1247*38e8c45fSAndroid Build Coastguard Worker             if (bwr.write_consumed < mOut.dataSize()) {
1248*38e8c45fSAndroid Build Coastguard Worker                 std::ostringstream logStream;
1249*38e8c45fSAndroid Build Coastguard Worker                 printReturnCommandParcel(logStream, mIn);
1250*38e8c45fSAndroid Build Coastguard Worker                 LOG_ALWAYS_FATAL("Driver did not consume write buffer. "
1251*38e8c45fSAndroid Build Coastguard Worker                                  "err: %s consumed: %zu of %zu.\n"
1252*38e8c45fSAndroid Build Coastguard Worker                                  "Return command: %s",
1253*38e8c45fSAndroid Build Coastguard Worker                                  statusToString(err).c_str(), (size_t)bwr.write_consumed,
1254*38e8c45fSAndroid Build Coastguard Worker                                  mOut.dataSize(), logStream.str().c_str());
1255*38e8c45fSAndroid Build Coastguard Worker             } else {
1256*38e8c45fSAndroid Build Coastguard Worker                 mOut.setDataSize(0);
1257*38e8c45fSAndroid Build Coastguard Worker                 processPostWriteDerefs();
1258*38e8c45fSAndroid Build Coastguard Worker             }
1259*38e8c45fSAndroid Build Coastguard Worker         }
1260*38e8c45fSAndroid Build Coastguard Worker         if (bwr.read_consumed > 0) {
1261*38e8c45fSAndroid Build Coastguard Worker             mIn.setDataSize(bwr.read_consumed);
1262*38e8c45fSAndroid Build Coastguard Worker             mIn.setDataPosition(0);
1263*38e8c45fSAndroid Build Coastguard Worker         }
1264*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_COMMANDS() {
1265*38e8c45fSAndroid Build Coastguard Worker             std::ostringstream logStream;
1266*38e8c45fSAndroid Build Coastguard Worker             printReturnCommandParcel(logStream, mIn);
1267*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s", logStream.str().c_str());
1268*38e8c45fSAndroid Build Coastguard Worker         }
1269*38e8c45fSAndroid Build Coastguard Worker         return NO_ERROR;
1270*38e8c45fSAndroid Build Coastguard Worker     }
1271*38e8c45fSAndroid Build Coastguard Worker 
1272*38e8c45fSAndroid Build Coastguard Worker     ALOGE_IF(mProcess->mDriverFD >= 0,
1273*38e8c45fSAndroid Build Coastguard Worker              "Driver returned error (%s). This is a bug in either libbinder or the driver. This "
1274*38e8c45fSAndroid Build Coastguard Worker              "thread's connection to %s will no longer work.",
1275*38e8c45fSAndroid Build Coastguard Worker              statusToString(err).c_str(), mProcess->mDriverName.c_str());
1276*38e8c45fSAndroid Build Coastguard Worker     return err;
1277*38e8c45fSAndroid Build Coastguard Worker }
1278*38e8c45fSAndroid Build Coastguard Worker 
writeTransactionData(int32_t cmd,uint32_t binderFlags,int32_t handle,uint32_t code,const Parcel & data,status_t * statusBuffer)1279*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1280*38e8c45fSAndroid Build Coastguard Worker     int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1281*38e8c45fSAndroid Build Coastguard Worker {
1282*38e8c45fSAndroid Build Coastguard Worker     binder_transaction_data tr;
1283*38e8c45fSAndroid Build Coastguard Worker 
1284*38e8c45fSAndroid Build Coastguard Worker     tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
1285*38e8c45fSAndroid Build Coastguard Worker     tr.target.handle = handle;
1286*38e8c45fSAndroid Build Coastguard Worker     tr.code = code;
1287*38e8c45fSAndroid Build Coastguard Worker     tr.flags = binderFlags;
1288*38e8c45fSAndroid Build Coastguard Worker     tr.cookie = 0;
1289*38e8c45fSAndroid Build Coastguard Worker     tr.sender_pid = 0;
1290*38e8c45fSAndroid Build Coastguard Worker     tr.sender_euid = 0;
1291*38e8c45fSAndroid Build Coastguard Worker 
1292*38e8c45fSAndroid Build Coastguard Worker     const status_t err = data.errorCheck();
1293*38e8c45fSAndroid Build Coastguard Worker     if (err == NO_ERROR) {
1294*38e8c45fSAndroid Build Coastguard Worker         tr.data_size = data.ipcDataSize();
1295*38e8c45fSAndroid Build Coastguard Worker         tr.data.ptr.buffer = data.ipcData();
1296*38e8c45fSAndroid Build Coastguard Worker         tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
1297*38e8c45fSAndroid Build Coastguard Worker         tr.data.ptr.offsets = data.ipcObjects();
1298*38e8c45fSAndroid Build Coastguard Worker     } else if (statusBuffer) {
1299*38e8c45fSAndroid Build Coastguard Worker         tr.flags |= TF_STATUS_CODE;
1300*38e8c45fSAndroid Build Coastguard Worker         *statusBuffer = err;
1301*38e8c45fSAndroid Build Coastguard Worker         tr.data_size = sizeof(status_t);
1302*38e8c45fSAndroid Build Coastguard Worker         tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
1303*38e8c45fSAndroid Build Coastguard Worker         tr.offsets_size = 0;
1304*38e8c45fSAndroid Build Coastguard Worker         tr.data.ptr.offsets = 0;
1305*38e8c45fSAndroid Build Coastguard Worker     } else {
1306*38e8c45fSAndroid Build Coastguard Worker         return (mLastError = err);
1307*38e8c45fSAndroid Build Coastguard Worker     }
1308*38e8c45fSAndroid Build Coastguard Worker 
1309*38e8c45fSAndroid Build Coastguard Worker     mOut.writeInt32(cmd);
1310*38e8c45fSAndroid Build Coastguard Worker     mOut.write(&tr, sizeof(tr));
1311*38e8c45fSAndroid Build Coastguard Worker 
1312*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
1313*38e8c45fSAndroid Build Coastguard Worker }
1314*38e8c45fSAndroid Build Coastguard Worker 
1315*38e8c45fSAndroid Build Coastguard Worker sp<BBinder> the_context_object;
1316*38e8c45fSAndroid Build Coastguard Worker 
setTheContextObject(const sp<BBinder> & obj)1317*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::setTheContextObject(const sp<BBinder>& obj)
1318*38e8c45fSAndroid Build Coastguard Worker {
1319*38e8c45fSAndroid Build Coastguard Worker     the_context_object = obj;
1320*38e8c45fSAndroid Build Coastguard Worker }
1321*38e8c45fSAndroid Build Coastguard Worker 
executeCommand(int32_t cmd)1322*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::executeCommand(int32_t cmd)
1323*38e8c45fSAndroid Build Coastguard Worker {
1324*38e8c45fSAndroid Build Coastguard Worker     BBinder* obj;
1325*38e8c45fSAndroid Build Coastguard Worker     RefBase::weakref_type* refs;
1326*38e8c45fSAndroid Build Coastguard Worker     status_t result = NO_ERROR;
1327*38e8c45fSAndroid Build Coastguard Worker 
1328*38e8c45fSAndroid Build Coastguard Worker     switch ((uint32_t)cmd) {
1329*38e8c45fSAndroid Build Coastguard Worker     case BR_ERROR:
1330*38e8c45fSAndroid Build Coastguard Worker         result = mIn.readInt32();
1331*38e8c45fSAndroid Build Coastguard Worker         break;
1332*38e8c45fSAndroid Build Coastguard Worker 
1333*38e8c45fSAndroid Build Coastguard Worker     case BR_OK:
1334*38e8c45fSAndroid Build Coastguard Worker         break;
1335*38e8c45fSAndroid Build Coastguard Worker 
1336*38e8c45fSAndroid Build Coastguard Worker     case BR_ACQUIRE:
1337*38e8c45fSAndroid Build Coastguard Worker         refs = (RefBase::weakref_type*)mIn.readPointer();
1338*38e8c45fSAndroid Build Coastguard Worker         obj = (BBinder*)mIn.readPointer();
1339*38e8c45fSAndroid Build Coastguard Worker         ALOG_ASSERT(refs->refBase() == obj,
1340*38e8c45fSAndroid Build Coastguard Worker                    "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1341*38e8c45fSAndroid Build Coastguard Worker                    refs, obj, refs->refBase());
1342*38e8c45fSAndroid Build Coastguard Worker         obj->incStrong(mProcess.get());
1343*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_REMOTEREFS() {
1344*38e8c45fSAndroid Build Coastguard Worker             LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1345*38e8c45fSAndroid Build Coastguard Worker             obj->printRefs();
1346*38e8c45fSAndroid Build Coastguard Worker         }
1347*38e8c45fSAndroid Build Coastguard Worker         mOut.writeInt32(BC_ACQUIRE_DONE);
1348*38e8c45fSAndroid Build Coastguard Worker         mOut.writePointer((uintptr_t)refs);
1349*38e8c45fSAndroid Build Coastguard Worker         mOut.writePointer((uintptr_t)obj);
1350*38e8c45fSAndroid Build Coastguard Worker         break;
1351*38e8c45fSAndroid Build Coastguard Worker 
1352*38e8c45fSAndroid Build Coastguard Worker     case BR_RELEASE:
1353*38e8c45fSAndroid Build Coastguard Worker         refs = (RefBase::weakref_type*)mIn.readPointer();
1354*38e8c45fSAndroid Build Coastguard Worker         obj = (BBinder*)mIn.readPointer();
1355*38e8c45fSAndroid Build Coastguard Worker         ALOG_ASSERT(refs->refBase() == obj,
1356*38e8c45fSAndroid Build Coastguard Worker                    "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1357*38e8c45fSAndroid Build Coastguard Worker                    refs, obj, refs->refBase());
1358*38e8c45fSAndroid Build Coastguard Worker         IF_LOG_REMOTEREFS() {
1359*38e8c45fSAndroid Build Coastguard Worker             LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1360*38e8c45fSAndroid Build Coastguard Worker             obj->printRefs();
1361*38e8c45fSAndroid Build Coastguard Worker         }
1362*38e8c45fSAndroid Build Coastguard Worker         mPendingStrongDerefs.push(obj);
1363*38e8c45fSAndroid Build Coastguard Worker         break;
1364*38e8c45fSAndroid Build Coastguard Worker 
1365*38e8c45fSAndroid Build Coastguard Worker     case BR_INCREFS:
1366*38e8c45fSAndroid Build Coastguard Worker         refs = (RefBase::weakref_type*)mIn.readPointer();
1367*38e8c45fSAndroid Build Coastguard Worker         obj = (BBinder*)mIn.readPointer();
1368*38e8c45fSAndroid Build Coastguard Worker         refs->incWeak(mProcess.get());
1369*38e8c45fSAndroid Build Coastguard Worker         mOut.writeInt32(BC_INCREFS_DONE);
1370*38e8c45fSAndroid Build Coastguard Worker         mOut.writePointer((uintptr_t)refs);
1371*38e8c45fSAndroid Build Coastguard Worker         mOut.writePointer((uintptr_t)obj);
1372*38e8c45fSAndroid Build Coastguard Worker         break;
1373*38e8c45fSAndroid Build Coastguard Worker 
1374*38e8c45fSAndroid Build Coastguard Worker     case BR_DECREFS:
1375*38e8c45fSAndroid Build Coastguard Worker         refs = (RefBase::weakref_type*)mIn.readPointer();
1376*38e8c45fSAndroid Build Coastguard Worker         // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
1377*38e8c45fSAndroid Build Coastguard Worker         obj = (BBinder*)mIn.readPointer(); // consume
1378*38e8c45fSAndroid Build Coastguard Worker         // NOTE: This assertion is not valid, because the object may no
1379*38e8c45fSAndroid Build Coastguard Worker         // longer exist (thus the (BBinder*)cast above resulting in a different
1380*38e8c45fSAndroid Build Coastguard Worker         // memory address).
1381*38e8c45fSAndroid Build Coastguard Worker         //ALOG_ASSERT(refs->refBase() == obj,
1382*38e8c45fSAndroid Build Coastguard Worker         //           "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1383*38e8c45fSAndroid Build Coastguard Worker         //           refs, obj, refs->refBase());
1384*38e8c45fSAndroid Build Coastguard Worker         mPendingWeakDerefs.push(refs);
1385*38e8c45fSAndroid Build Coastguard Worker         break;
1386*38e8c45fSAndroid Build Coastguard Worker 
1387*38e8c45fSAndroid Build Coastguard Worker     case BR_ATTEMPT_ACQUIRE:
1388*38e8c45fSAndroid Build Coastguard Worker         refs = (RefBase::weakref_type*)mIn.readPointer();
1389*38e8c45fSAndroid Build Coastguard Worker         obj = (BBinder*)mIn.readPointer();
1390*38e8c45fSAndroid Build Coastguard Worker 
1391*38e8c45fSAndroid Build Coastguard Worker         {
1392*38e8c45fSAndroid Build Coastguard Worker             const bool success = refs->attemptIncStrong(mProcess.get());
1393*38e8c45fSAndroid Build Coastguard Worker             ALOG_ASSERT(success && refs->refBase() == obj,
1394*38e8c45fSAndroid Build Coastguard Worker                        "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1395*38e8c45fSAndroid Build Coastguard Worker                        refs, obj, refs->refBase());
1396*38e8c45fSAndroid Build Coastguard Worker 
1397*38e8c45fSAndroid Build Coastguard Worker             mOut.writeInt32(BC_ACQUIRE_RESULT);
1398*38e8c45fSAndroid Build Coastguard Worker             mOut.writeInt32((int32_t)success);
1399*38e8c45fSAndroid Build Coastguard Worker         }
1400*38e8c45fSAndroid Build Coastguard Worker         break;
1401*38e8c45fSAndroid Build Coastguard Worker 
1402*38e8c45fSAndroid Build Coastguard Worker     case BR_TRANSACTION_SEC_CTX:
1403*38e8c45fSAndroid Build Coastguard Worker     case BR_TRANSACTION:
1404*38e8c45fSAndroid Build Coastguard Worker         {
1405*38e8c45fSAndroid Build Coastguard Worker             binder_transaction_data_secctx tr_secctx;
1406*38e8c45fSAndroid Build Coastguard Worker             binder_transaction_data& tr = tr_secctx.transaction_data;
1407*38e8c45fSAndroid Build Coastguard Worker 
1408*38e8c45fSAndroid Build Coastguard Worker             if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1409*38e8c45fSAndroid Build Coastguard Worker                 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1410*38e8c45fSAndroid Build Coastguard Worker             } else {
1411*38e8c45fSAndroid Build Coastguard Worker                 result = mIn.read(&tr, sizeof(tr));
1412*38e8c45fSAndroid Build Coastguard Worker                 tr_secctx.secctx = 0;
1413*38e8c45fSAndroid Build Coastguard Worker             }
1414*38e8c45fSAndroid Build Coastguard Worker 
1415*38e8c45fSAndroid Build Coastguard Worker             ALOG_ASSERT(result == NO_ERROR,
1416*38e8c45fSAndroid Build Coastguard Worker                 "Not enough command data for brTRANSACTION");
1417*38e8c45fSAndroid Build Coastguard Worker             if (result != NO_ERROR) break;
1418*38e8c45fSAndroid Build Coastguard Worker 
1419*38e8c45fSAndroid Build Coastguard Worker             Parcel buffer;
1420*38e8c45fSAndroid Build Coastguard Worker             buffer.ipcSetDataReference(
1421*38e8c45fSAndroid Build Coastguard Worker                 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1422*38e8c45fSAndroid Build Coastguard Worker                 tr.data_size,
1423*38e8c45fSAndroid Build Coastguard Worker                 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1424*38e8c45fSAndroid Build Coastguard Worker                 tr.offsets_size/sizeof(binder_size_t), freeBuffer);
1425*38e8c45fSAndroid Build Coastguard Worker 
1426*38e8c45fSAndroid Build Coastguard Worker             const void* origServingStackPointer = mServingStackPointer;
1427*38e8c45fSAndroid Build Coastguard Worker             mServingStackPointer = __builtin_frame_address(0);
1428*38e8c45fSAndroid Build Coastguard Worker 
1429*38e8c45fSAndroid Build Coastguard Worker             const pid_t origPid = mCallingPid;
1430*38e8c45fSAndroid Build Coastguard Worker             const char* origSid = mCallingSid;
1431*38e8c45fSAndroid Build Coastguard Worker             const uid_t origUid = mCallingUid;
1432*38e8c45fSAndroid Build Coastguard Worker             const bool origHasExplicitIdentity = mHasExplicitIdentity;
1433*38e8c45fSAndroid Build Coastguard Worker             const int32_t origStrictModePolicy = mStrictModePolicy;
1434*38e8c45fSAndroid Build Coastguard Worker             const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1435*38e8c45fSAndroid Build Coastguard Worker             const int32_t origWorkSource = mWorkSource;
1436*38e8c45fSAndroid Build Coastguard Worker             const bool origPropagateWorkSet = mPropagateWorkSource;
1437*38e8c45fSAndroid Build Coastguard Worker             // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1438*38e8c45fSAndroid Build Coastguard Worker             // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1439*38e8c45fSAndroid Build Coastguard Worker             // here to never propagate it.
1440*38e8c45fSAndroid Build Coastguard Worker             clearCallingWorkSource();
1441*38e8c45fSAndroid Build Coastguard Worker             clearPropagateWorkSource();
1442*38e8c45fSAndroid Build Coastguard Worker 
1443*38e8c45fSAndroid Build Coastguard Worker             mCallingPid = tr.sender_pid;
1444*38e8c45fSAndroid Build Coastguard Worker             mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
1445*38e8c45fSAndroid Build Coastguard Worker             mCallingUid = tr.sender_euid;
1446*38e8c45fSAndroid Build Coastguard Worker             mHasExplicitIdentity = false;
1447*38e8c45fSAndroid Build Coastguard Worker             mLastTransactionBinderFlags = tr.flags;
1448*38e8c45fSAndroid Build Coastguard Worker 
1449*38e8c45fSAndroid Build Coastguard Worker             // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1450*38e8c45fSAndroid Build Coastguard Worker             //    (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
1451*38e8c45fSAndroid Build Coastguard Worker 
1452*38e8c45fSAndroid Build Coastguard Worker             Parcel reply;
1453*38e8c45fSAndroid Build Coastguard Worker             status_t error;
1454*38e8c45fSAndroid Build Coastguard Worker             IF_LOG_TRANSACTIONS() {
1455*38e8c45fSAndroid Build Coastguard Worker                 std::ostringstream logStream;
1456*38e8c45fSAndroid Build Coastguard Worker                 logStream << "BR_TRANSACTION thr " << (void*)pthread_self() << " / obj "
1457*38e8c45fSAndroid Build Coastguard Worker                           << tr.target.ptr << " / code " << TypeCode(tr.code) << ": \t" << buffer
1458*38e8c45fSAndroid Build Coastguard Worker                           << "\n"
1459*38e8c45fSAndroid Build Coastguard Worker                           << "Data addr = " << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1460*38e8c45fSAndroid Build Coastguard Worker                           << ", offsets addr="
1461*38e8c45fSAndroid Build Coastguard Worker                           << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << "\n";
1462*38e8c45fSAndroid Build Coastguard Worker                 std::string message = logStream.str();
1463*38e8c45fSAndroid Build Coastguard Worker                 ALOGI("%s", message.c_str());
1464*38e8c45fSAndroid Build Coastguard Worker             }
1465*38e8c45fSAndroid Build Coastguard Worker             if (tr.target.ptr) {
1466*38e8c45fSAndroid Build Coastguard Worker                 // We only have a weak reference on the target object, so we must first try to
1467*38e8c45fSAndroid Build Coastguard Worker                 // safely acquire a strong reference before doing anything else with it.
1468*38e8c45fSAndroid Build Coastguard Worker                 if (reinterpret_cast<RefBase::weakref_type*>(
1469*38e8c45fSAndroid Build Coastguard Worker                         tr.target.ptr)->attemptIncStrong(this)) {
1470*38e8c45fSAndroid Build Coastguard Worker                     error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1471*38e8c45fSAndroid Build Coastguard Worker                             &reply, tr.flags);
1472*38e8c45fSAndroid Build Coastguard Worker                     reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
1473*38e8c45fSAndroid Build Coastguard Worker                 } else {
1474*38e8c45fSAndroid Build Coastguard Worker                     error = UNKNOWN_TRANSACTION;
1475*38e8c45fSAndroid Build Coastguard Worker                 }
1476*38e8c45fSAndroid Build Coastguard Worker 
1477*38e8c45fSAndroid Build Coastguard Worker             } else {
1478*38e8c45fSAndroid Build Coastguard Worker                 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
1479*38e8c45fSAndroid Build Coastguard Worker             }
1480*38e8c45fSAndroid Build Coastguard Worker 
1481*38e8c45fSAndroid Build Coastguard Worker             //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1482*38e8c45fSAndroid Build Coastguard Worker             //     mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
1483*38e8c45fSAndroid Build Coastguard Worker 
1484*38e8c45fSAndroid Build Coastguard Worker             if ((tr.flags & TF_ONE_WAY) == 0) {
1485*38e8c45fSAndroid Build Coastguard Worker                 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1486*38e8c45fSAndroid Build Coastguard Worker                 if (error < NO_ERROR) reply.setError(error);
1487*38e8c45fSAndroid Build Coastguard Worker 
1488*38e8c45fSAndroid Build Coastguard Worker                 // b/238777741: clear buffer before we send the reply.
1489*38e8c45fSAndroid Build Coastguard Worker                 // Otherwise, there is a race where the client may
1490*38e8c45fSAndroid Build Coastguard Worker                 // receive the reply and send another transaction
1491*38e8c45fSAndroid Build Coastguard Worker                 // here and the space used by this transaction won't
1492*38e8c45fSAndroid Build Coastguard Worker                 // be freed for the client.
1493*38e8c45fSAndroid Build Coastguard Worker                 buffer.setDataSize(0);
1494*38e8c45fSAndroid Build Coastguard Worker 
1495*38e8c45fSAndroid Build Coastguard Worker                 constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF;
1496*38e8c45fSAndroid Build Coastguard Worker                 sendReply(reply, (tr.flags & kForwardReplyFlags));
1497*38e8c45fSAndroid Build Coastguard Worker             } else {
1498*38e8c45fSAndroid Build Coastguard Worker                 if (error != OK) {
1499*38e8c45fSAndroid Build Coastguard Worker                     std::ostringstream logStream;
1500*38e8c45fSAndroid Build Coastguard Worker                     logStream << "oneway function results for code " << tr.code << " on binder at "
1501*38e8c45fSAndroid Build Coastguard Worker                               << reinterpret_cast<void*>(tr.target.ptr)
1502*38e8c45fSAndroid Build Coastguard Worker                               << " will be dropped but finished with status "
1503*38e8c45fSAndroid Build Coastguard Worker                               << statusToString(error);
1504*38e8c45fSAndroid Build Coastguard Worker 
1505*38e8c45fSAndroid Build Coastguard Worker                     // ideally we could log this even when error == OK, but it
1506*38e8c45fSAndroid Build Coastguard Worker                     // causes too much logspam because some manually-written
1507*38e8c45fSAndroid Build Coastguard Worker                     // interfaces have clients that call methods which always
1508*38e8c45fSAndroid Build Coastguard Worker                     // write results, sometimes as oneway methods.
1509*38e8c45fSAndroid Build Coastguard Worker                     if (reply.dataSize() != 0) {
1510*38e8c45fSAndroid Build Coastguard Worker                         logStream << " and reply parcel size " << reply.dataSize();
1511*38e8c45fSAndroid Build Coastguard Worker                     }
1512*38e8c45fSAndroid Build Coastguard Worker                     std::string message = logStream.str();
1513*38e8c45fSAndroid Build Coastguard Worker                     ALOGI("%s", message.c_str());
1514*38e8c45fSAndroid Build Coastguard Worker                 }
1515*38e8c45fSAndroid Build Coastguard Worker                 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1516*38e8c45fSAndroid Build Coastguard Worker             }
1517*38e8c45fSAndroid Build Coastguard Worker 
1518*38e8c45fSAndroid Build Coastguard Worker             mServingStackPointer = origServingStackPointer;
1519*38e8c45fSAndroid Build Coastguard Worker             mCallingPid = origPid;
1520*38e8c45fSAndroid Build Coastguard Worker             mCallingSid = origSid;
1521*38e8c45fSAndroid Build Coastguard Worker             mCallingUid = origUid;
1522*38e8c45fSAndroid Build Coastguard Worker             mHasExplicitIdentity = origHasExplicitIdentity;
1523*38e8c45fSAndroid Build Coastguard Worker             mStrictModePolicy = origStrictModePolicy;
1524*38e8c45fSAndroid Build Coastguard Worker             mLastTransactionBinderFlags = origTransactionBinderFlags;
1525*38e8c45fSAndroid Build Coastguard Worker             mWorkSource = origWorkSource;
1526*38e8c45fSAndroid Build Coastguard Worker             mPropagateWorkSource = origPropagateWorkSet;
1527*38e8c45fSAndroid Build Coastguard Worker 
1528*38e8c45fSAndroid Build Coastguard Worker             IF_LOG_TRANSACTIONS() {
1529*38e8c45fSAndroid Build Coastguard Worker                 std::ostringstream logStream;
1530*38e8c45fSAndroid Build Coastguard Worker                 logStream << "BC_REPLY thr " << (void*)pthread_self() << " / obj " << tr.target.ptr
1531*38e8c45fSAndroid Build Coastguard Worker                           << ": \t" << reply << "\n";
1532*38e8c45fSAndroid Build Coastguard Worker                 std::string message = logStream.str();
1533*38e8c45fSAndroid Build Coastguard Worker                 ALOGI("%s", message.c_str());
1534*38e8c45fSAndroid Build Coastguard Worker             }
1535*38e8c45fSAndroid Build Coastguard Worker 
1536*38e8c45fSAndroid Build Coastguard Worker         }
1537*38e8c45fSAndroid Build Coastguard Worker         break;
1538*38e8c45fSAndroid Build Coastguard Worker 
1539*38e8c45fSAndroid Build Coastguard Worker     case BR_DEAD_BINDER:
1540*38e8c45fSAndroid Build Coastguard Worker         {
1541*38e8c45fSAndroid Build Coastguard Worker             BpBinder *proxy = (BpBinder*)mIn.readPointer();
1542*38e8c45fSAndroid Build Coastguard Worker             proxy->sendObituary();
1543*38e8c45fSAndroid Build Coastguard Worker             mOut.writeInt32(BC_DEAD_BINDER_DONE);
1544*38e8c45fSAndroid Build Coastguard Worker             mOut.writePointer((uintptr_t)proxy);
1545*38e8c45fSAndroid Build Coastguard Worker         } break;
1546*38e8c45fSAndroid Build Coastguard Worker 
1547*38e8c45fSAndroid Build Coastguard Worker     case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1548*38e8c45fSAndroid Build Coastguard Worker         {
1549*38e8c45fSAndroid Build Coastguard Worker             BpBinder *proxy = (BpBinder*)mIn.readPointer();
1550*38e8c45fSAndroid Build Coastguard Worker             proxy->getWeakRefs()->decWeak(proxy);
1551*38e8c45fSAndroid Build Coastguard Worker         } break;
1552*38e8c45fSAndroid Build Coastguard Worker 
1553*38e8c45fSAndroid Build Coastguard Worker         case BR_FROZEN_BINDER: {
1554*38e8c45fSAndroid Build Coastguard Worker             const struct binder_frozen_state_info* data =
1555*38e8c45fSAndroid Build Coastguard Worker                     reinterpret_cast<const struct binder_frozen_state_info*>(
1556*38e8c45fSAndroid Build Coastguard Worker                             mIn.readInplace(sizeof(struct binder_frozen_state_info)));
1557*38e8c45fSAndroid Build Coastguard Worker             if (data == nullptr) {
1558*38e8c45fSAndroid Build Coastguard Worker                 result = UNKNOWN_ERROR;
1559*38e8c45fSAndroid Build Coastguard Worker                 break;
1560*38e8c45fSAndroid Build Coastguard Worker             }
1561*38e8c45fSAndroid Build Coastguard Worker             BpBinder* proxy = (BpBinder*)data->cookie;
1562*38e8c45fSAndroid Build Coastguard Worker             bool isFrozen = mIn.readInt32() > 0;
1563*38e8c45fSAndroid Build Coastguard Worker             proxy->getPrivateAccessor().onFrozenStateChanged(data->is_frozen);
1564*38e8c45fSAndroid Build Coastguard Worker             mOut.writeInt32(BC_FREEZE_NOTIFICATION_DONE);
1565*38e8c45fSAndroid Build Coastguard Worker             mOut.writePointer(data->cookie);
1566*38e8c45fSAndroid Build Coastguard Worker         } break;
1567*38e8c45fSAndroid Build Coastguard Worker 
1568*38e8c45fSAndroid Build Coastguard Worker         case BR_CLEAR_FREEZE_NOTIFICATION_DONE: {
1569*38e8c45fSAndroid Build Coastguard Worker             BpBinder* proxy = (BpBinder*)mIn.readPointer();
1570*38e8c45fSAndroid Build Coastguard Worker             proxy->getWeakRefs()->decWeak(proxy);
1571*38e8c45fSAndroid Build Coastguard Worker         } break;
1572*38e8c45fSAndroid Build Coastguard Worker 
1573*38e8c45fSAndroid Build Coastguard Worker     case BR_FINISHED:
1574*38e8c45fSAndroid Build Coastguard Worker         result = TIMED_OUT;
1575*38e8c45fSAndroid Build Coastguard Worker         break;
1576*38e8c45fSAndroid Build Coastguard Worker 
1577*38e8c45fSAndroid Build Coastguard Worker     case BR_NOOP:
1578*38e8c45fSAndroid Build Coastguard Worker         break;
1579*38e8c45fSAndroid Build Coastguard Worker 
1580*38e8c45fSAndroid Build Coastguard Worker     case BR_SPAWN_LOOPER:
1581*38e8c45fSAndroid Build Coastguard Worker         mProcess->spawnPooledThread(false);
1582*38e8c45fSAndroid Build Coastguard Worker         break;
1583*38e8c45fSAndroid Build Coastguard Worker 
1584*38e8c45fSAndroid Build Coastguard Worker     default:
1585*38e8c45fSAndroid Build Coastguard Worker         ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
1586*38e8c45fSAndroid Build Coastguard Worker         result = UNKNOWN_ERROR;
1587*38e8c45fSAndroid Build Coastguard Worker         break;
1588*38e8c45fSAndroid Build Coastguard Worker     }
1589*38e8c45fSAndroid Build Coastguard Worker 
1590*38e8c45fSAndroid Build Coastguard Worker     if (result != NO_ERROR) {
1591*38e8c45fSAndroid Build Coastguard Worker         mLastError = result;
1592*38e8c45fSAndroid Build Coastguard Worker     }
1593*38e8c45fSAndroid Build Coastguard Worker 
1594*38e8c45fSAndroid Build Coastguard Worker     return result;
1595*38e8c45fSAndroid Build Coastguard Worker }
1596*38e8c45fSAndroid Build Coastguard Worker 
getServingStackPointer() const1597*38e8c45fSAndroid Build Coastguard Worker const void* IPCThreadState::getServingStackPointer() const {
1598*38e8c45fSAndroid Build Coastguard Worker      return mServingStackPointer;
1599*38e8c45fSAndroid Build Coastguard Worker }
1600*38e8c45fSAndroid Build Coastguard Worker 
threadDestructor(void * st)1601*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::threadDestructor(void *st)
1602*38e8c45fSAndroid Build Coastguard Worker {
1603*38e8c45fSAndroid Build Coastguard Worker         IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1604*38e8c45fSAndroid Build Coastguard Worker         if (self) {
1605*38e8c45fSAndroid Build Coastguard Worker                 self->flushCommands();
1606*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
1607*38e8c45fSAndroid Build Coastguard Worker         if (self->mProcess->mDriverFD >= 0) {
1608*38e8c45fSAndroid Build Coastguard Worker             ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1609*38e8c45fSAndroid Build Coastguard Worker         }
1610*38e8c45fSAndroid Build Coastguard Worker #endif
1611*38e8c45fSAndroid Build Coastguard Worker                 delete self;
1612*38e8c45fSAndroid Build Coastguard Worker         }
1613*38e8c45fSAndroid Build Coastguard Worker }
1614*38e8c45fSAndroid Build Coastguard Worker 
getProcessFreezeInfo(pid_t pid,uint32_t * sync_received,uint32_t * async_received)1615*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, uint32_t *sync_received,
1616*38e8c45fSAndroid Build Coastguard Worker                                               uint32_t *async_received)
1617*38e8c45fSAndroid Build Coastguard Worker {
1618*38e8c45fSAndroid Build Coastguard Worker     int ret = 0;
1619*38e8c45fSAndroid Build Coastguard Worker     binder_frozen_status_info info = {};
1620*38e8c45fSAndroid Build Coastguard Worker     info.pid = pid;
1621*38e8c45fSAndroid Build Coastguard Worker 
1622*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
1623*38e8c45fSAndroid Build Coastguard Worker     if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0)
1624*38e8c45fSAndroid Build Coastguard Worker         ret = -errno;
1625*38e8c45fSAndroid Build Coastguard Worker #endif
1626*38e8c45fSAndroid Build Coastguard Worker     *sync_received = info.sync_recv;
1627*38e8c45fSAndroid Build Coastguard Worker     *async_received = info.async_recv;
1628*38e8c45fSAndroid Build Coastguard Worker 
1629*38e8c45fSAndroid Build Coastguard Worker     return ret;
1630*38e8c45fSAndroid Build Coastguard Worker }
1631*38e8c45fSAndroid Build Coastguard Worker 
freeze(pid_t pid,bool enable,uint32_t timeout_ms)1632*38e8c45fSAndroid Build Coastguard Worker status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) {
1633*38e8c45fSAndroid Build Coastguard Worker     struct binder_freeze_info info;
1634*38e8c45fSAndroid Build Coastguard Worker     int ret = 0;
1635*38e8c45fSAndroid Build Coastguard Worker 
1636*38e8c45fSAndroid Build Coastguard Worker     info.pid = pid;
1637*38e8c45fSAndroid Build Coastguard Worker     info.enable = enable;
1638*38e8c45fSAndroid Build Coastguard Worker     info.timeout_ms = timeout_ms;
1639*38e8c45fSAndroid Build Coastguard Worker 
1640*38e8c45fSAndroid Build Coastguard Worker 
1641*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
1642*38e8c45fSAndroid Build Coastguard Worker     if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0)
1643*38e8c45fSAndroid Build Coastguard Worker         ret = -errno;
1644*38e8c45fSAndroid Build Coastguard Worker #endif
1645*38e8c45fSAndroid Build Coastguard Worker 
1646*38e8c45fSAndroid Build Coastguard Worker     //
1647*38e8c45fSAndroid Build Coastguard Worker     // ret==-EAGAIN indicates that transactions have not drained.
1648*38e8c45fSAndroid Build Coastguard Worker     // Call again to poll for completion.
1649*38e8c45fSAndroid Build Coastguard Worker     //
1650*38e8c45fSAndroid Build Coastguard Worker     return ret;
1651*38e8c45fSAndroid Build Coastguard Worker }
1652*38e8c45fSAndroid Build Coastguard Worker 
logExtendedError()1653*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::logExtendedError() {
1654*38e8c45fSAndroid Build Coastguard Worker     struct binder_extended_error ee = {.command = BR_OK};
1655*38e8c45fSAndroid Build Coastguard Worker 
1656*38e8c45fSAndroid Build Coastguard Worker     if (!ProcessState::isDriverFeatureEnabled(ProcessState::DriverFeature::EXTENDED_ERROR))
1657*38e8c45fSAndroid Build Coastguard Worker         return;
1658*38e8c45fSAndroid Build Coastguard Worker 
1659*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
1660*38e8c45fSAndroid Build Coastguard Worker     if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_EXTENDED_ERROR, &ee) < 0) {
1661*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Failed to get extended error: %s", strerror(errno));
1662*38e8c45fSAndroid Build Coastguard Worker         return;
1663*38e8c45fSAndroid Build Coastguard Worker     }
1664*38e8c45fSAndroid Build Coastguard Worker #endif
1665*38e8c45fSAndroid Build Coastguard Worker 
1666*38e8c45fSAndroid Build Coastguard Worker     ALOGE_IF(ee.command != BR_OK, "Binder transaction failure. id: %d, BR_*: %d, error: %d (%s)",
1667*38e8c45fSAndroid Build Coastguard Worker              ee.id, ee.command, ee.param, strerror(-ee.param));
1668*38e8c45fSAndroid Build Coastguard Worker }
1669*38e8c45fSAndroid Build Coastguard Worker 
freeBuffer(const uint8_t * data,size_t,const binder_size_t *,size_t)1670*38e8c45fSAndroid Build Coastguard Worker void IPCThreadState::freeBuffer(const uint8_t* data, size_t /*dataSize*/,
1671*38e8c45fSAndroid Build Coastguard Worker                                 const binder_size_t* /*objects*/, size_t /*objectsSize*/) {
1672*38e8c45fSAndroid Build Coastguard Worker     //ALOGI("Freeing parcel %p", &parcel);
1673*38e8c45fSAndroid Build Coastguard Worker     IF_LOG_COMMANDS() {
1674*38e8c45fSAndroid Build Coastguard Worker         std::ostringstream logStream;
1675*38e8c45fSAndroid Build Coastguard Worker         logStream << "Writing BC_FREE_BUFFER for " << data << "\n";
1676*38e8c45fSAndroid Build Coastguard Worker         std::string message = logStream.str();
1677*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s", message.c_str());
1678*38e8c45fSAndroid Build Coastguard Worker     }
1679*38e8c45fSAndroid Build Coastguard Worker     ALOG_ASSERT(data != NULL, "Called with NULL data");
1680*38e8c45fSAndroid Build Coastguard Worker     IPCThreadState* state = self();
1681*38e8c45fSAndroid Build Coastguard Worker     state->mOut.writeInt32(BC_FREE_BUFFER);
1682*38e8c45fSAndroid Build Coastguard Worker     state->mOut.writePointer((uintptr_t)data);
1683*38e8c45fSAndroid Build Coastguard Worker     state->flushIfNeeded();
1684*38e8c45fSAndroid Build Coastguard Worker }
1685*38e8c45fSAndroid Build Coastguard Worker 
1686*38e8c45fSAndroid Build Coastguard Worker } // namespace android
1687