blob: bc7ae37ff055513bf23b8c3b7a80d93065e51d25 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Yifan Hong8b890852021-06-10 13:44:09 -070020#include <set>
Yifan Hong84bedeb2021-04-21 21:37:17 -070021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/BpBinder.h>
23#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070024#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070025#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070026#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/Parcel.h>
Fabián Cañas356a0222023-01-13 16:32:53 -050028#include <binder/RecordedTransaction.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070029#include <binder/RpcServer.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070030#include <binder/unique_fd.h>
Sahil Somani2522b072022-08-08 14:07:47 -070031#include <pthread.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <stdio.h>
35
Andrei Homescuf55d6882022-04-30 00:50:20 +000036#ifdef __linux__
37#include <linux/sched.h>
38#endif
39
Steven Moreland32150282021-11-12 22:54:53 +000040#include "BuildFlags.h"
Steven Moreland54fa6c72025-01-28 01:02:15 +000041#include "Constants.h"
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +000042#include "OS.h"
Yifan Hong84bedeb2021-04-21 21:37:17 -070043#include "RpcState.h"
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070047using android::binder::unique_fd;
48
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000049constexpr uid_t kUidRoot = 0;
50
Steven Moreland90c1f9a2021-05-03 18:27:24 +000051// Service implementations inherit from BBinder and IBinder, and this is frozen
52// in prebuilts.
53#ifdef __LP64__
54static_assert(sizeof(IBinder) == 24);
55static_assert(sizeof(BBinder) == 40);
56#else
57static_assert(sizeof(IBinder) == 12);
58static_assert(sizeof(BBinder) == 20);
59#endif
60
Steven Morelandd9a74002022-06-02 00:03:18 +000061// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070062#ifdef BINDER_RPC_DEV_SERVERS
Steven Moreland5255c972023-05-19 22:53:41 +000063constexpr bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070064#else
Steven Moreland5255c972023-05-19 22:53:41 +000065constexpr bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070066#endif
67
Sahil Somani2522b072022-08-08 14:07:47 -070068#ifdef BINDER_ENABLE_RECORDING
Steven Moreland5255c972023-05-19 22:53:41 +000069constexpr bool kEnableRecording = true;
Sahil Somani2522b072022-08-08 14:07:47 -070070#else
Steven Moreland5255c972023-05-19 22:53:41 +000071constexpr bool kEnableRecording = false;
Sahil Somani2522b072022-08-08 14:07:47 -070072#endif
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian83c04462009-05-22 19:00:22 -070076IBinder::IBinder()
77 : RefBase()
78{
79}
80
81IBinder::~IBinder()
82{
83}
84
85// ---------------------------------------------------------------------------
86
Colin Cross6f4f3ab2014-02-05 17:42:44 -080087sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
Yi Kongfdd8da92018-06-07 17:52:27 -070089 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090}
91
92BBinder* IBinder::localBinder()
93{
Yi Kongfdd8da92018-06-07 17:52:27 -070094 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97BpBinder* IBinder::remoteBinder()
98{
Yi Kongfdd8da92018-06-07 17:52:27 -070099 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100}
101
102bool IBinder::checkSubclass(const void* /*subclassID*/) const
103{
104 return false;
105}
106
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700107
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700108status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700109 Vector<String16>& args, const sp<IShellCallback>& callback,
110 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700111{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700112 Parcel send;
113 Parcel reply;
114 send.writeFileDescriptor(in);
115 send.writeFileDescriptor(out);
116 send.writeFileDescriptor(err);
117 const size_t numArgs = args.size();
118 send.writeInt32(numArgs);
119 for (size_t i = 0; i < numArgs; i++) {
120 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700121 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700122 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
123 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700124 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700125}
126
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700127status_t IBinder::getExtension(sp<IBinder>* out) {
128 BBinder* local = this->localBinder();
129 if (local != nullptr) {
130 *out = local->getExtension();
131 return OK;
132 }
133
134 BpBinder* proxy = this->remoteBinder();
135 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
136
137 Parcel data;
138 Parcel reply;
139 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
140 if (status != OK) return status;
141
142 return reply.readNullableStrongBinder(out);
143}
144
Yu-Ting Tsengd5fc4462024-04-30 15:07:13 -0700145status_t IBinder::addFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
146 BpBinder* proxy = this->remoteBinder();
147 if (proxy != nullptr) {
148 return proxy->addFrozenStateChangeCallback(callback);
149 }
150 return INVALID_OPERATION;
151}
152
153status_t IBinder::removeFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
154 BpBinder* proxy = this->remoteBinder();
155 if (proxy != nullptr) {
156 return proxy->removeFrozenStateChangeCallback(callback);
157 }
158 return INVALID_OPERATION;
159}
160
Steven Moreland86080b82019-09-23 15:41:18 -0700161status_t IBinder::getDebugPid(pid_t* out) {
162 BBinder* local = this->localBinder();
163 if (local != nullptr) {
164 *out = local->getDebugPid();
165 return OK;
166 }
167
168 BpBinder* proxy = this->remoteBinder();
169 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
170
171 Parcel data;
172 Parcel reply;
173 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
174 if (status != OK) return status;
175
176 int32_t pid;
177 status = reply.readInt32(&pid);
178 if (status != OK) return status;
179
180 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
181 return BAD_VALUE;
182 }
183 *out = pid;
184 return OK;
185}
186
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700187status_t IBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000188 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700189 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
190 return INVALID_OPERATION;
191 }
Steven Moreland32150282021-11-12 22:54:53 +0000192 if (!kEnableKernelIpc) {
193 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
194 return INVALID_OPERATION;
195 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700196
197 BBinder* local = this->localBinder();
198 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700199 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700200 }
201
202 BpBinder* proxy = this->remoteBinder();
203 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
204
205 Parcel data;
206 Parcel reply;
207 status_t status;
208 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
209 if (socketFd.ok()) {
210 // writeUniqueFileDescriptor currently makes an unnecessary dup().
211 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
212 if (status != OK) return status;
213 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700214 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700215 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
216}
217
Steven Moreland9e759e82021-06-25 21:30:23 +0000218void IBinder::withLock(const std::function<void()>& doWithLock) {
219 BBinder* local = localBinder();
220 if (local) {
221 local->withLock(doWithLock);
222 return;
223 }
224 BpBinder* proxy = this->remoteBinder();
225 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
226 proxy->withLock(doWithLock);
227}
228
Devin Moore3faaa002022-07-27 15:54:06 +0000229sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
230 const void* makeArgs) {
231 BBinder* local = localBinder();
232 if (local) {
233 return local->lookupOrCreateWeak(objectID, make, makeArgs);
234 }
235 BpBinder* proxy = this->remoteBinder();
236 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
237 return proxy->lookupOrCreateWeak(objectID, make, makeArgs);
238}
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240// ---------------------------------------------------------------------------
241
Yifan Hong8b890852021-06-10 13:44:09 -0700242class BBinder::RpcServerLink : public IBinder::DeathRecipient {
243public:
244 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
245 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
246 const wp<BBinder>& binder)
247 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000248 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700249 void binderDied(const wp<IBinder>&) override {
Steven Morelandf6957b72022-09-24 03:12:16 +0000250 auto promoted = mBinder.promote();
251 ALOGI("RpcBinder: binder died, shutting down RpcServer for %s",
252 promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>");
253
Yifan Hong8b890852021-06-10 13:44:09 -0700254 if (mRpcServer == nullptr) {
255 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
256 } else {
257 ALOGW_IF(!mRpcServer->shutdown(),
258 "RpcServerLink: RpcServer did not shut down properly. Not started?");
259 }
260 mRpcServer.clear();
261
Steven Morelandf6957b72022-09-24 03:12:16 +0000262 if (promoted) {
Yifan Hong8b890852021-06-10 13:44:09 -0700263 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
264 }
265 mBinder.clear();
266 }
267
268private:
269 sp<RpcServer> mRpcServer;
270 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
271 wp<BBinder> mBinder;
272};
Steven Morelandd9a74002022-06-02 00:03:18 +0000273BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275class BBinder::Extras
276{
277public:
Steven Morelandf0212002018-12-26 13:59:23 -0800278 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700279 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000280#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000281 int mPolicy = SCHED_NORMAL;
282 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000283#endif
284 bool mRequestingSid = false;
285 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800286
287 // for below objects
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700288 RpcMutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700289 std::set<sp<RpcServerLink>> mRpcServerLinks;
Devin Moore3fea46f2025-01-09 00:17:12 +0000290 BpBinder::ObjectManager mObjectMgr;
Sahil Somani2522b072022-08-08 14:07:47 -0700291
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700292 unique_fd mRecordingFd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293};
294
295// ---------------------------------------------------------------------------
296
Sahil Somani2522b072022-08-08 14:07:47 -0700297BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
299bool BBinder::isBinderAlive() const
300{
301 return true;
302}
303
304status_t BBinder::pingBinder()
305{
306 return NO_ERROR;
307}
308
Sahil Somani2522b072022-08-08 14:07:47 -0700309status_t BBinder::startRecordingTransactions(const Parcel& data) {
310 if (!kEnableRecording) {
311 ALOGW("Binder recording disallowed because recording is not enabled");
312 return INVALID_OPERATION;
313 }
314 if (!kEnableKernelIpc) {
315 ALOGW("Binder recording disallowed because kernel binder is not enabled");
316 return INVALID_OPERATION;
317 }
318 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000319 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700320 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
321 return PERMISSION_DENIED;
322 }
323 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700324 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700325 if (mRecordingOn) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700326 ALOGI("Could not start Binder recording. Another is already in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700327 return INVALID_OPERATION;
328 } else {
329 status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd));
330 if (readStatus != OK) {
331 return readStatus;
332 }
333 mRecordingOn = true;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700334 ALOGI("Started Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700335 return NO_ERROR;
336 }
337}
338
339status_t BBinder::stopRecordingTransactions() {
340 if (!kEnableRecording) {
341 ALOGW("Binder recording disallowed because recording is not enabled");
342 return INVALID_OPERATION;
343 }
344 if (!kEnableKernelIpc) {
345 ALOGW("Binder recording disallowed because kernel binder is not enabled");
346 return INVALID_OPERATION;
347 }
348 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000349 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700350 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
351 return PERMISSION_DENIED;
352 }
353 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700354 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700355 if (mRecordingOn) {
356 e->mRecordingFd.reset();
357 mRecordingOn = false;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700358 ALOGI("Stopped Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700359 return NO_ERROR;
360 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700361 ALOGI("Could not stop Binder recording. One is not in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700362 return INVALID_OPERATION;
363 }
364}
365
Mathias Agopian83c04462009-05-22 19:00:22 -0700366const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
Steven Moreland1ef0eb62022-08-18 20:09:30 +0000368 static StaticString16 sBBinder(u"BBinder");
369 ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this);
370 return sBBinder;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371}
372
Jiyong Parkb86c8662018-10-29 23:01:57 +0900373// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374status_t BBinder::transact(
375 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
376{
377 data.setDataPosition(0);
378
Steven Morelandf183fdd2020-10-27 00:12:12 +0000379 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
380 reply->markSensitive();
381 }
382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 status_t err = NO_ERROR;
384 switch (code) {
385 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700386 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 break;
Sahil Somani2522b072022-08-08 14:07:47 -0700388 case START_RECORDING_TRANSACTION:
389 err = startRecordingTransactions(data);
390 break;
391 case STOP_RECORDING_TRANSACTION:
392 err = stopRecordingTransactions();
393 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700394 case EXTENSION_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700395 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700396 err = reply->writeStrongBinder(getExtension());
397 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700398 case DEBUG_PID_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700399 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Moreland86080b82019-09-23 15:41:18 -0700400 err = reply->writeInt32(getDebugPid());
401 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700402 case SET_RPC_CLIENT_TRANSACTION: {
403 err = setRpcClientDebug(data);
404 break;
405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 default:
407 err = onTransact(code, data, reply, flags);
408 break;
409 }
410
Steven Morelanda86a3562019-08-01 23:28:34 +0000411 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700412 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 reply->setDataPosition(0);
Steven Moreland54fa6c72025-01-28 01:02:15 +0000414 if (reply->dataSize() > binder::kLogTransactionsOverBytes) {
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200415 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
416 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
417 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
419
Tomasz Wasilczyk32746ae2023-10-09 19:57:58 +0000420 if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] {
Sahil Somani2522b072022-08-08 14:07:47 -0700421 Extras* e = mExtras.load(std::memory_order_acquire);
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700422 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700423 if (mRecordingOn) {
424 Parcel emptyReply;
Fabián Cañas68dbcfe2023-01-06 15:37:39 -0500425 timespec ts;
426 timespec_get(&ts, TIME_UTC);
Fabián Cañas6c0c9632023-01-13 15:21:16 -0500427 auto transaction = android::binder::debug::RecordedTransaction::
428 fromDetails(getInterfaceDescriptor(), code, flags, ts, data,
429 reply ? *reply : emptyReply, err);
Sahil Somani2522b072022-08-08 14:07:47 -0700430 if (transaction) {
431 if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700432 ALOGI("Failed to dump RecordedTransaction to file with error %d", err);
Sahil Somani2522b072022-08-08 14:07:47 -0700433 }
434 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700435 ALOGI("Failed to create RecordedTransaction object.");
Sahil Somani2522b072022-08-08 14:07:47 -0700436 }
437 }
438 }
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 return err;
441}
442
Jiyong Parkb86c8662018-10-29 23:01:57 +0900443// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800445 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
446 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447{
448 return INVALID_OPERATION;
449}
450
Jiyong Parkb86c8662018-10-29 23:01:57 +0900451// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800453 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
454 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455{
456 return INVALID_OPERATION;
457}
458
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700459status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460{
461 return NO_ERROR;
462}
463
Steven Moreland63a2d512021-06-25 01:10:15 +0000464void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
465 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800466 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000467 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700469 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000470 return e->mObjectMgr.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
473void* BBinder::findObject(const void* objectID) const
474{
Bailey Forrest6913c462015-08-18 17:15:10 -0700475 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700476 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700478 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000479 return e->mObjectMgr.find(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480}
481
Steven Moreland63a2d512021-06-25 01:10:15 +0000482void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700483 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000484 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700486 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000487 return e->mObjectMgr.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488}
489
Steven Moreland9e759e82021-06-25 21:30:23 +0000490void BBinder::withLock(const std::function<void()>& doWithLock) {
491 Extras* e = getOrCreateExtras();
492 LOG_ALWAYS_FATAL_IF(!e, "no memory");
493
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700494 RpcMutexUniqueLock _l(e->mLock);
Steven Moreland9e759e82021-06-25 21:30:23 +0000495 doWithLock();
496}
497
Devin Moore3faaa002022-07-27 15:54:06 +0000498sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
499 const void* makeArgs) {
500 Extras* e = getOrCreateExtras();
501 LOG_ALWAYS_FATAL_IF(!e, "no memory");
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700502 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000503 return e->mObjectMgr.lookupOrCreateWeak(objectID, make, makeArgs);
Devin Moore3faaa002022-07-27 15:54:06 +0000504}
505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506BBinder* BBinder::localBinder()
507{
508 return this;
509}
510
Steven Morelandf0212002018-12-26 13:59:23 -0800511bool BBinder::isRequestingSid()
512{
513 Extras* e = mExtras.load(std::memory_order_acquire);
514
515 return e && e->mRequestingSid;
516}
517
518void BBinder::setRequestingSid(bool requestingSid)
519{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000520 LOG_ALWAYS_FATAL_IF(mParceled,
521 "setRequestingSid() should not be called after a binder object "
522 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500523
Steven Morelandf0212002018-12-26 13:59:23 -0800524 Extras* e = mExtras.load(std::memory_order_acquire);
525
526 if (!e) {
527 // default is false. Most things don't need sids, so avoiding allocations when possible.
528 if (!requestingSid) {
529 return;
530 }
531
532 e = getOrCreateExtras();
533 if (!e) return; // out of memory
534 }
535
Steven Moreland3668be62019-02-08 17:56:55 -0800536 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800537}
538
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700539sp<IBinder> BBinder::getExtension() {
540 Extras* e = mExtras.load(std::memory_order_acquire);
541 if (e == nullptr) return nullptr;
542 return e->mExtension;
543}
544
Andrei Homescuf55d6882022-04-30 00:50:20 +0000545#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000546void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000547 LOG_ALWAYS_FATAL_IF(mParceled,
548 "setMinSchedulerPolicy() should not be called after a binder object "
549 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500550
Steven Morelandbf1915b2020-07-16 22:43:02 +0000551 switch (policy) {
552 case SCHED_NORMAL:
553 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
554 break;
555 case SCHED_RR:
556 case SCHED_FIFO:
557 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
558 break;
559 default:
560 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
561 }
562
563 Extras* e = mExtras.load(std::memory_order_acquire);
564
565 if (e == nullptr) {
566 // Avoid allocations if called with default.
567 if (policy == SCHED_NORMAL && priority == 0) {
568 return;
569 }
570
571 e = getOrCreateExtras();
572 if (!e) return; // out of memory
573 }
574
575 e->mPolicy = policy;
576 e->mPriority = priority;
577}
578
579int BBinder::getMinSchedulerPolicy() {
580 Extras* e = mExtras.load(std::memory_order_acquire);
581 if (e == nullptr) return SCHED_NORMAL;
582 return e->mPolicy;
583}
584
585int BBinder::getMinSchedulerPriority() {
586 Extras* e = mExtras.load(std::memory_order_acquire);
587 if (e == nullptr) return 0;
588 return e->mPriority;
589}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000590#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000591
Steven Morelandcf03cf12020-12-04 02:58:40 +0000592bool BBinder::isInheritRt() {
593 Extras* e = mExtras.load(std::memory_order_acquire);
594
595 return e && e->mInheritRt;
596}
597
598void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000599 LOG_ALWAYS_FATAL_IF(mParceled,
600 "setInheritRt() should not be called after a binder object "
601 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500602
Steven Morelandcf03cf12020-12-04 02:58:40 +0000603 Extras* e = mExtras.load(std::memory_order_acquire);
604
605 if (!e) {
606 if (!inheritRt) {
607 return;
608 }
609
610 e = getOrCreateExtras();
611 if (!e) return; // out of memory
612 }
613
614 e->mInheritRt = inheritRt;
615}
616
Steven Moreland86080b82019-09-23 15:41:18 -0700617pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000618#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700619 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000620#else
621 // TODO: handle other OSes
622 return 0;
623#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700624}
625
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700626void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000627 LOG_ALWAYS_FATAL_IF(mParceled,
628 "setExtension() should not be called after a binder object "
629 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500630
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700631 Extras* e = getOrCreateExtras();
632 e->mExtension = extension;
633}
634
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500635bool BBinder::wasParceled() {
636 return mParceled;
637}
638
639void BBinder::setParceled() {
640 mParceled = true;
641}
642
Yifan Hong84bedeb2021-04-21 21:37:17 -0700643status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000644 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700645 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
646 return INVALID_OPERATION;
647 }
Steven Moreland32150282021-11-12 22:54:53 +0000648 if (!kEnableKernelIpc) {
649 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
650 return INVALID_OPERATION;
651 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700652 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000653 if (uid != kUidRoot) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700654 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
655 return PERMISSION_DENIED;
656 }
657 status_t status;
658 bool hasSocketFd;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700659 unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700660
661 if (status = data.readBool(&hasSocketFd); status != OK) return status;
662 if (hasSocketFd) {
663 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
664 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700665 sp<IBinder> keepAliveBinder;
666 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700667
Yifan Hong02530ec2021-06-10 13:38:38 -0700668 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700669}
670
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700671status_t BBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000672 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700673 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
674 return INVALID_OPERATION;
675 }
Steven Moreland32150282021-11-12 22:54:53 +0000676 if (!kEnableKernelIpc) {
677 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
678 return INVALID_OPERATION;
679 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700680
681 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700682 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700683
684 if (!socketFd.ok()) {
685 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
686 return BAD_VALUE;
687 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700688
Yifan Hong02530ec2021-06-10 13:38:38 -0700689 if (keepAliveBinder == nullptr) {
690 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
691 return UNEXPECTED_NULL;
692 }
693
Elie Kheirallah47431c12022-04-21 23:46:17 +0000694 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700695 if (binderThreadPoolMaxCount <= 1) {
696 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
697 "because RPC requires the service to support multithreading.",
698 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
699 return INVALID_OPERATION;
700 }
701
Yifan Hong8b890852021-06-10 13:44:09 -0700702 // Weak ref to avoid circular dependency:
703 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
704 // `-X-> BBinder
705 auto weakThis = wp<BBinder>::fromExisting(this);
706
Yifan Hong84bedeb2021-04-21 21:37:17 -0700707 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700708 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700709 auto rpcServer = RpcServer::make();
710 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700711 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
712 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
713 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
714 statusToString(status).c_str());
715 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700716 }
Yifan Hong8b890852021-06-10 13:44:09 -0700717 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700718 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
719 return status;
720 }
Yifan Hong8b890852021-06-10 13:44:09 -0700721 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700722 ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str());
Yifan Hong8b890852021-06-10 13:44:09 -0700723 rpcServer->start();
724 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700725 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700726 return OK;
727}
728
Yifan Hong8b890852021-06-10 13:44:09 -0700729void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
730 Extras* e = mExtras.load(std::memory_order_acquire);
731 if (!e) return;
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700732 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700733 (void)e->mRpcServerLinks.erase(link);
734}
735
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736BBinder::~BBinder()
737{
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000738 if (!wasParceled()) {
739 if (getExtension()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700740 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000741 }
742 if (isRequestingSid()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700743 ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000744 }
745 if (isInheritRt()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700746 ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000747 }
Andrei Homescu40525502022-08-02 01:23:22 +0000748#ifdef __linux__
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000749 if (getMinSchedulerPolicy() != SCHED_NORMAL) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700750 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000751 }
752 if (getMinSchedulerPriority() != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700753 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000754 }
Andrei Homescu40525502022-08-02 01:23:22 +0000755#endif // __linux__
Steven Moreland55a12542022-03-31 21:53:11 +0000756 }
757
Bailey Forrest6913c462015-08-18 17:15:10 -0700758 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000759 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760}
761
762
Jiyong Parkb86c8662018-10-29 23:01:57 +0900763// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800765 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766{
767 switch (code) {
768 case INTERFACE_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700769 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 reply->writeString16(getInterfaceDescriptor());
771 return NO_ERROR;
772
773 case DUMP_TRANSACTION: {
774 int fd = data.readFileDescriptor();
775 int argc = data.readInt32();
776 Vector<String16> args;
777 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
778 args.add(data.readString16());
779 }
780 return dump(fd, args);
781 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700782
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700783 case SHELL_COMMAND_TRANSACTION: {
784 int in = data.readFileDescriptor();
785 int out = data.readFileDescriptor();
786 int err = data.readFileDescriptor();
787 int argc = data.readInt32();
788 Vector<String16> args;
789 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
790 args.add(data.readString16());
791 }
Andrei Homescua543a842022-07-07 22:17:55 +0000792 sp<IBinder> shellCallbackBinder = data.readStrongBinder();
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700793 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
794 data.readStrongBinder());
795
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700796 // XXX can't add virtuals until binaries are updated.
Andrei Homescua543a842022-07-07 22:17:55 +0000797 // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
798 // shellCallbackBinder);
799 // return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700800 (void)in;
801 (void)out;
802 (void)err;
803
Yi Kongfdd8da92018-06-07 17:52:27 -0700804 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700805 resultReceiver->send(INVALID_OPERATION);
806 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200807
808 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700809 }
810
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700811 case SYSPROPS_TRANSACTION: {
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +0000812 if (!binder::os::report_sysprop_change()) return INVALID_OPERATION;
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700813 return NO_ERROR;
814 }
815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 default:
817 return UNKNOWN_TRANSACTION;
818 }
819}
820
Steven Morelandf0212002018-12-26 13:59:23 -0800821BBinder::Extras* BBinder::getOrCreateExtras()
822{
823 Extras* e = mExtras.load(std::memory_order_acquire);
824
825 if (!e) {
826 e = new Extras;
827 Extras* expected = nullptr;
828 if (!mExtras.compare_exchange_strong(expected, e,
829 std::memory_order_release,
830 std::memory_order_acquire)) {
831 delete e;
832 e = expected; // Filled in by CAS
833 }
834 if (e == nullptr) return nullptr; // out of memory
835 }
836
837 return e;
838}
839
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840// ---------------------------------------------------------------------------
841
842enum {
843 // This is used to transfer ownership of the remote binder from
844 // the BpRefBase object holding it (when it is constructed), to the
845 // owner of the BpRefBase object when it first acquires that BpRefBase.
846 kRemoteAcquired = 0x00000001
847};
848
849BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700850 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851{
852 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
853
854 if (mRemote) {
855 mRemote->incStrong(this); // Removed on first IncStrong().
856 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
857 }
858}
859
860BpRefBase::~BpRefBase()
861{
862 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700863 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 mRemote->decStrong(this);
865 }
866 mRefs->decWeak(this);
867 }
868}
869
870void BpRefBase::onFirstRef()
871{
Bailey Forrest6913c462015-08-18 17:15:10 -0700872 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873}
874
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800875void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876{
877 if (mRemote) {
878 mRemote->decStrong(this);
879 }
880}
881
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800882bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883{
884 return mRemote ? mRefs->attemptIncStrong(this) : false;
885}
886
887// ---------------------------------------------------------------------------
888
Steven Moreland61ff8492019-09-26 16:05:45 -0700889} // namespace android