blob: 301dbf6cdf7ae46c7e88cd9e0b6661608f3c3458 [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
22#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/BpBinder.h>
24#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070025#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070026#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070027#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/Parcel.h>
Fabián Cañas356a0222023-01-13 16:32:53 -050029#include <binder/RecordedTransaction.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070030#include <binder/RpcServer.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"
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +000041#include "OS.h"
Yifan Hong84bedeb2021-04-21 21:37:17 -070042#include "RpcState.h"
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044namespace android {
45
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000046constexpr uid_t kUidRoot = 0;
47
Steven Moreland90c1f9a2021-05-03 18:27:24 +000048// Service implementations inherit from BBinder and IBinder, and this is frozen
49// in prebuilts.
50#ifdef __LP64__
51static_assert(sizeof(IBinder) == 24);
52static_assert(sizeof(BBinder) == 40);
53#else
54static_assert(sizeof(IBinder) == 12);
55static_assert(sizeof(BBinder) == 20);
56#endif
57
Steven Morelandd9a74002022-06-02 00:03:18 +000058// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070059#ifdef BINDER_RPC_DEV_SERVERS
Steven Moreland5255c972023-05-19 22:53:41 +000060constexpr bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070061#else
Steven Moreland5255c972023-05-19 22:53:41 +000062constexpr bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070063#endif
64
Sahil Somani2522b072022-08-08 14:07:47 -070065#ifdef BINDER_ENABLE_RECORDING
Steven Moreland5255c972023-05-19 22:53:41 +000066constexpr bool kEnableRecording = true;
Sahil Somani2522b072022-08-08 14:07:47 -070067#else
Steven Moreland5255c972023-05-19 22:53:41 +000068constexpr bool kEnableRecording = false;
Sahil Somani2522b072022-08-08 14:07:47 -070069#endif
70
Martijn Coenen1cad19c2021-10-04 09:19:01 +020071// Log any reply transactions for which the data exceeds this size
72#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian83c04462009-05-22 19:00:22 -070075IBinder::IBinder()
76 : RefBase()
77{
78}
79
80IBinder::~IBinder()
81{
82}
83
84// ---------------------------------------------------------------------------
85
Colin Cross6f4f3ab2014-02-05 17:42:44 -080086sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087{
Yi Kongfdd8da92018-06-07 17:52:27 -070088 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089}
90
91BBinder* IBinder::localBinder()
92{
Yi Kongfdd8da92018-06-07 17:52:27 -070093 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094}
95
96BpBinder* IBinder::remoteBinder()
97{
Yi Kongfdd8da92018-06-07 17:52:27 -070098 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099}
100
101bool IBinder::checkSubclass(const void* /*subclassID*/) const
102{
103 return false;
104}
105
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700106
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700107status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700108 Vector<String16>& args, const sp<IShellCallback>& callback,
109 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700110{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700111 Parcel send;
112 Parcel reply;
113 send.writeFileDescriptor(in);
114 send.writeFileDescriptor(out);
115 send.writeFileDescriptor(err);
116 const size_t numArgs = args.size();
117 send.writeInt32(numArgs);
118 for (size_t i = 0; i < numArgs; i++) {
119 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700120 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700121 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
122 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700123 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700124}
125
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700126status_t IBinder::getExtension(sp<IBinder>* out) {
127 BBinder* local = this->localBinder();
128 if (local != nullptr) {
129 *out = local->getExtension();
130 return OK;
131 }
132
133 BpBinder* proxy = this->remoteBinder();
134 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
135
136 Parcel data;
137 Parcel reply;
138 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
139 if (status != OK) return status;
140
141 return reply.readNullableStrongBinder(out);
142}
143
Steven Moreland86080b82019-09-23 15:41:18 -0700144status_t IBinder::getDebugPid(pid_t* out) {
145 BBinder* local = this->localBinder();
146 if (local != nullptr) {
147 *out = local->getDebugPid();
148 return OK;
149 }
150
151 BpBinder* proxy = this->remoteBinder();
152 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
153
154 Parcel data;
155 Parcel reply;
156 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
157 if (status != OK) return status;
158
159 int32_t pid;
160 status = reply.readInt32(&pid);
161 if (status != OK) return status;
162
163 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
164 return BAD_VALUE;
165 }
166 *out = pid;
167 return OK;
168}
169
Yifan Hong02530ec2021-06-10 13:38:38 -0700170status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
171 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000172 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700173 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
174 return INVALID_OPERATION;
175 }
Steven Moreland32150282021-11-12 22:54:53 +0000176 if (!kEnableKernelIpc) {
177 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
178 return INVALID_OPERATION;
179 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700180
181 BBinder* local = this->localBinder();
182 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700183 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700184 }
185
186 BpBinder* proxy = this->remoteBinder();
187 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
188
189 Parcel data;
190 Parcel reply;
191 status_t status;
192 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
193 if (socketFd.ok()) {
194 // writeUniqueFileDescriptor currently makes an unnecessary dup().
195 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
196 if (status != OK) return status;
197 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700198 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700199 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
200}
201
Steven Moreland9e759e82021-06-25 21:30:23 +0000202void IBinder::withLock(const std::function<void()>& doWithLock) {
203 BBinder* local = localBinder();
204 if (local) {
205 local->withLock(doWithLock);
206 return;
207 }
208 BpBinder* proxy = this->remoteBinder();
209 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
210 proxy->withLock(doWithLock);
211}
212
Devin Moore3faaa002022-07-27 15:54:06 +0000213sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
214 const void* makeArgs) {
215 BBinder* local = localBinder();
216 if (local) {
217 return local->lookupOrCreateWeak(objectID, make, makeArgs);
218 }
219 BpBinder* proxy = this->remoteBinder();
220 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
221 return proxy->lookupOrCreateWeak(objectID, make, makeArgs);
222}
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224// ---------------------------------------------------------------------------
225
Yifan Hong8b890852021-06-10 13:44:09 -0700226class BBinder::RpcServerLink : public IBinder::DeathRecipient {
227public:
228 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
229 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
230 const wp<BBinder>& binder)
231 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000232 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700233 void binderDied(const wp<IBinder>&) override {
Steven Morelandf6957b72022-09-24 03:12:16 +0000234 auto promoted = mBinder.promote();
235 ALOGI("RpcBinder: binder died, shutting down RpcServer for %s",
236 promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>");
237
Yifan Hong8b890852021-06-10 13:44:09 -0700238 if (mRpcServer == nullptr) {
239 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
240 } else {
241 ALOGW_IF(!mRpcServer->shutdown(),
242 "RpcServerLink: RpcServer did not shut down properly. Not started?");
243 }
244 mRpcServer.clear();
245
Steven Morelandf6957b72022-09-24 03:12:16 +0000246 if (promoted) {
Yifan Hong8b890852021-06-10 13:44:09 -0700247 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
248 }
249 mBinder.clear();
250 }
251
252private:
253 sp<RpcServer> mRpcServer;
254 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
255 wp<BBinder> mBinder;
256};
Steven Morelandd9a74002022-06-02 00:03:18 +0000257BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259class BBinder::Extras
260{
261public:
Steven Morelandf0212002018-12-26 13:59:23 -0800262 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700263 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000264#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000265 int mPolicy = SCHED_NORMAL;
266 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000267#endif
268 bool mRequestingSid = false;
269 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800270
271 // for below objects
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700272 RpcMutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700273 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 BpBinder::ObjectManager mObjects;
Sahil Somani2522b072022-08-08 14:07:47 -0700275
276 android::base::unique_fd mRecordingFd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277};
278
279// ---------------------------------------------------------------------------
280
Sahil Somani2522b072022-08-08 14:07:47 -0700281BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
283bool BBinder::isBinderAlive() const
284{
285 return true;
286}
287
288status_t BBinder::pingBinder()
289{
290 return NO_ERROR;
291}
292
Sahil Somani2522b072022-08-08 14:07:47 -0700293status_t BBinder::startRecordingTransactions(const Parcel& data) {
294 if (!kEnableRecording) {
295 ALOGW("Binder recording disallowed because recording is not enabled");
296 return INVALID_OPERATION;
297 }
298 if (!kEnableKernelIpc) {
299 ALOGW("Binder recording disallowed because kernel binder is not enabled");
300 return INVALID_OPERATION;
301 }
302 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000303 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700304 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
305 return PERMISSION_DENIED;
306 }
307 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700308 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700309 if (mRecordingOn) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700310 ALOGI("Could not start Binder recording. Another is already in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700311 return INVALID_OPERATION;
312 } else {
313 status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd));
314 if (readStatus != OK) {
315 return readStatus;
316 }
317 mRecordingOn = true;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700318 ALOGI("Started Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700319 return NO_ERROR;
320 }
321}
322
323status_t BBinder::stopRecordingTransactions() {
324 if (!kEnableRecording) {
325 ALOGW("Binder recording disallowed because recording is not enabled");
326 return INVALID_OPERATION;
327 }
328 if (!kEnableKernelIpc) {
329 ALOGW("Binder recording disallowed because kernel binder is not enabled");
330 return INVALID_OPERATION;
331 }
332 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000333 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700334 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
335 return PERMISSION_DENIED;
336 }
337 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700338 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700339 if (mRecordingOn) {
340 e->mRecordingFd.reset();
341 mRecordingOn = false;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700342 ALOGI("Stopped Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700343 return NO_ERROR;
344 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700345 ALOGI("Could not stop Binder recording. One is not in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700346 return INVALID_OPERATION;
347 }
348}
349
Mathias Agopian83c04462009-05-22 19:00:22 -0700350const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351{
Steven Moreland1ef0eb62022-08-18 20:09:30 +0000352 static StaticString16 sBBinder(u"BBinder");
353 ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this);
354 return sBBinder;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355}
356
Jiyong Parkb86c8662018-10-29 23:01:57 +0900357// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358status_t BBinder::transact(
359 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
360{
361 data.setDataPosition(0);
362
Steven Morelandf183fdd2020-10-27 00:12:12 +0000363 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
364 reply->markSensitive();
365 }
366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 status_t err = NO_ERROR;
368 switch (code) {
369 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700370 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 break;
Sahil Somani2522b072022-08-08 14:07:47 -0700372 case START_RECORDING_TRANSACTION:
373 err = startRecordingTransactions(data);
374 break;
375 case STOP_RECORDING_TRANSACTION:
376 err = stopRecordingTransactions();
377 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700378 case EXTENSION_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700379 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700380 err = reply->writeStrongBinder(getExtension());
381 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700382 case DEBUG_PID_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700383 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Moreland86080b82019-09-23 15:41:18 -0700384 err = reply->writeInt32(getDebugPid());
385 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700386 case SET_RPC_CLIENT_TRANSACTION: {
387 err = setRpcClientDebug(data);
388 break;
389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 default:
391 err = onTransact(code, data, reply, flags);
392 break;
393 }
394
Steven Morelanda86a3562019-08-01 23:28:34 +0000395 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700396 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200398 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
399 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
400 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
401 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 }
403
Tomasz Wasilczyk32746ae2023-10-09 19:57:58 +0000404 if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] {
Sahil Somani2522b072022-08-08 14:07:47 -0700405 Extras* e = mExtras.load(std::memory_order_acquire);
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700406 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700407 if (mRecordingOn) {
408 Parcel emptyReply;
Fabián Cañas68dbcfe2023-01-06 15:37:39 -0500409 timespec ts;
410 timespec_get(&ts, TIME_UTC);
Fabián Cañas6c0c9632023-01-13 15:21:16 -0500411 auto transaction = android::binder::debug::RecordedTransaction::
412 fromDetails(getInterfaceDescriptor(), code, flags, ts, data,
413 reply ? *reply : emptyReply, err);
Sahil Somani2522b072022-08-08 14:07:47 -0700414 if (transaction) {
415 if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700416 ALOGI("Failed to dump RecordedTransaction to file with error %d", err);
Sahil Somani2522b072022-08-08 14:07:47 -0700417 }
418 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700419 ALOGI("Failed to create RecordedTransaction object.");
Sahil Somani2522b072022-08-08 14:07:47 -0700420 }
421 }
422 }
423
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 return err;
425}
426
Jiyong Parkb86c8662018-10-29 23:01:57 +0900427// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800429 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
430 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431{
432 return INVALID_OPERATION;
433}
434
Jiyong Parkb86c8662018-10-29 23:01:57 +0900435// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800437 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
438 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439{
440 return INVALID_OPERATION;
441}
442
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700443status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444{
445 return NO_ERROR;
446}
447
Steven Moreland63a2d512021-06-25 01:10:15 +0000448void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
449 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800450 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000451 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700453 RpcMutexUniqueLock _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000454 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455}
456
457void* BBinder::findObject(const void* objectID) const
458{
Bailey Forrest6913c462015-08-18 17:15:10 -0700459 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700460 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700462 RpcMutexUniqueLock _l(e->mLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 return e->mObjects.find(objectID);
464}
465
Steven Moreland63a2d512021-06-25 01:10:15 +0000466void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700467 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000468 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700470 RpcMutexUniqueLock _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000471 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
Steven Moreland9e759e82021-06-25 21:30:23 +0000474void BBinder::withLock(const std::function<void()>& doWithLock) {
475 Extras* e = getOrCreateExtras();
476 LOG_ALWAYS_FATAL_IF(!e, "no memory");
477
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700478 RpcMutexUniqueLock _l(e->mLock);
Steven Moreland9e759e82021-06-25 21:30:23 +0000479 doWithLock();
480}
481
Devin Moore3faaa002022-07-27 15:54:06 +0000482sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
483 const void* makeArgs) {
484 Extras* e = getOrCreateExtras();
485 LOG_ALWAYS_FATAL_IF(!e, "no memory");
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700486 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3faaa002022-07-27 15:54:06 +0000487 return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs);
488}
489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490BBinder* BBinder::localBinder()
491{
492 return this;
493}
494
Steven Morelandf0212002018-12-26 13:59:23 -0800495bool BBinder::isRequestingSid()
496{
497 Extras* e = mExtras.load(std::memory_order_acquire);
498
499 return e && e->mRequestingSid;
500}
501
502void BBinder::setRequestingSid(bool requestingSid)
503{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000504 LOG_ALWAYS_FATAL_IF(mParceled,
505 "setRequestingSid() should not be called after a binder object "
506 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500507
Steven Morelandf0212002018-12-26 13:59:23 -0800508 Extras* e = mExtras.load(std::memory_order_acquire);
509
510 if (!e) {
511 // default is false. Most things don't need sids, so avoiding allocations when possible.
512 if (!requestingSid) {
513 return;
514 }
515
516 e = getOrCreateExtras();
517 if (!e) return; // out of memory
518 }
519
Steven Moreland3668be62019-02-08 17:56:55 -0800520 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800521}
522
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700523sp<IBinder> BBinder::getExtension() {
524 Extras* e = mExtras.load(std::memory_order_acquire);
525 if (e == nullptr) return nullptr;
526 return e->mExtension;
527}
528
Andrei Homescuf55d6882022-04-30 00:50:20 +0000529#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000530void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000531 LOG_ALWAYS_FATAL_IF(mParceled,
532 "setMinSchedulerPolicy() should not be called after a binder object "
533 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500534
Steven Morelandbf1915b2020-07-16 22:43:02 +0000535 switch (policy) {
536 case SCHED_NORMAL:
537 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
538 break;
539 case SCHED_RR:
540 case SCHED_FIFO:
541 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
542 break;
543 default:
544 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
545 }
546
547 Extras* e = mExtras.load(std::memory_order_acquire);
548
549 if (e == nullptr) {
550 // Avoid allocations if called with default.
551 if (policy == SCHED_NORMAL && priority == 0) {
552 return;
553 }
554
555 e = getOrCreateExtras();
556 if (!e) return; // out of memory
557 }
558
559 e->mPolicy = policy;
560 e->mPriority = priority;
561}
562
563int BBinder::getMinSchedulerPolicy() {
564 Extras* e = mExtras.load(std::memory_order_acquire);
565 if (e == nullptr) return SCHED_NORMAL;
566 return e->mPolicy;
567}
568
569int BBinder::getMinSchedulerPriority() {
570 Extras* e = mExtras.load(std::memory_order_acquire);
571 if (e == nullptr) return 0;
572 return e->mPriority;
573}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000574#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000575
Steven Morelandcf03cf12020-12-04 02:58:40 +0000576bool BBinder::isInheritRt() {
577 Extras* e = mExtras.load(std::memory_order_acquire);
578
579 return e && e->mInheritRt;
580}
581
582void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000583 LOG_ALWAYS_FATAL_IF(mParceled,
584 "setInheritRt() should not be called after a binder object "
585 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500586
Steven Morelandcf03cf12020-12-04 02:58:40 +0000587 Extras* e = mExtras.load(std::memory_order_acquire);
588
589 if (!e) {
590 if (!inheritRt) {
591 return;
592 }
593
594 e = getOrCreateExtras();
595 if (!e) return; // out of memory
596 }
597
598 e->mInheritRt = inheritRt;
599}
600
Steven Moreland86080b82019-09-23 15:41:18 -0700601pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000602#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700603 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000604#else
605 // TODO: handle other OSes
606 return 0;
607#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700608}
609
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700610void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000611 LOG_ALWAYS_FATAL_IF(mParceled,
612 "setExtension() should not be called after a binder object "
613 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500614
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700615 Extras* e = getOrCreateExtras();
616 e->mExtension = extension;
617}
618
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500619bool BBinder::wasParceled() {
620 return mParceled;
621}
622
623void BBinder::setParceled() {
624 mParceled = true;
625}
626
Yifan Hong84bedeb2021-04-21 21:37:17 -0700627status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000628 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700629 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
630 return INVALID_OPERATION;
631 }
Steven Moreland32150282021-11-12 22:54:53 +0000632 if (!kEnableKernelIpc) {
633 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
634 return INVALID_OPERATION;
635 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700636 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000637 if (uid != kUidRoot) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700638 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
639 return PERMISSION_DENIED;
640 }
641 status_t status;
642 bool hasSocketFd;
643 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700644
645 if (status = data.readBool(&hasSocketFd); status != OK) return status;
646 if (hasSocketFd) {
647 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
648 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700649 sp<IBinder> keepAliveBinder;
650 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700651
Yifan Hong02530ec2021-06-10 13:38:38 -0700652 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700653}
654
Yifan Hong02530ec2021-06-10 13:38:38 -0700655status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
656 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000657 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700658 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
659 return INVALID_OPERATION;
660 }
Steven Moreland32150282021-11-12 22:54:53 +0000661 if (!kEnableKernelIpc) {
662 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
663 return INVALID_OPERATION;
664 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700665
666 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700667 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700668
669 if (!socketFd.ok()) {
670 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
671 return BAD_VALUE;
672 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700673
Yifan Hong02530ec2021-06-10 13:38:38 -0700674 if (keepAliveBinder == nullptr) {
675 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
676 return UNEXPECTED_NULL;
677 }
678
Elie Kheirallah47431c12022-04-21 23:46:17 +0000679 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700680 if (binderThreadPoolMaxCount <= 1) {
681 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
682 "because RPC requires the service to support multithreading.",
683 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
684 return INVALID_OPERATION;
685 }
686
Yifan Hong8b890852021-06-10 13:44:09 -0700687 // Weak ref to avoid circular dependency:
688 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
689 // `-X-> BBinder
690 auto weakThis = wp<BBinder>::fromExisting(this);
691
Yifan Hong84bedeb2021-04-21 21:37:17 -0700692 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700693 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700694 auto rpcServer = RpcServer::make();
695 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700696 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
697 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
698 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
699 statusToString(status).c_str());
700 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700701 }
Yifan Hong8b890852021-06-10 13:44:09 -0700702 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700703 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
704 return status;
705 }
Yifan Hong8b890852021-06-10 13:44:09 -0700706 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700707 ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str());
Yifan Hong8b890852021-06-10 13:44:09 -0700708 rpcServer->start();
709 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700710 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700711 return OK;
712}
713
Yifan Hong8b890852021-06-10 13:44:09 -0700714void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
715 Extras* e = mExtras.load(std::memory_order_acquire);
716 if (!e) return;
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700717 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700718 (void)e->mRpcServerLinks.erase(link);
719}
720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721BBinder::~BBinder()
722{
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000723 if (!wasParceled()) {
724 if (getExtension()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700725 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000726 }
727 if (isRequestingSid()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700728 ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000729 }
730 if (isInheritRt()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700731 ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000732 }
Andrei Homescu40525502022-08-02 01:23:22 +0000733#ifdef __linux__
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000734 if (getMinSchedulerPolicy() != SCHED_NORMAL) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700735 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000736 }
737 if (getMinSchedulerPriority() != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700738 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000739 }
Andrei Homescu40525502022-08-02 01:23:22 +0000740#endif // __linux__
Steven Moreland55a12542022-03-31 21:53:11 +0000741 }
742
Bailey Forrest6913c462015-08-18 17:15:10 -0700743 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000744 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
747
Jiyong Parkb86c8662018-10-29 23:01:57 +0900748// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800750 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751{
752 switch (code) {
753 case INTERFACE_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700754 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 reply->writeString16(getInterfaceDescriptor());
756 return NO_ERROR;
757
758 case DUMP_TRANSACTION: {
759 int fd = data.readFileDescriptor();
760 int argc = data.readInt32();
761 Vector<String16> args;
762 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
763 args.add(data.readString16());
764 }
765 return dump(fd, args);
766 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700767
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700768 case SHELL_COMMAND_TRANSACTION: {
769 int in = data.readFileDescriptor();
770 int out = data.readFileDescriptor();
771 int err = data.readFileDescriptor();
772 int argc = data.readInt32();
773 Vector<String16> args;
774 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
775 args.add(data.readString16());
776 }
Andrei Homescua543a842022-07-07 22:17:55 +0000777 sp<IBinder> shellCallbackBinder = data.readStrongBinder();
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700778 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
779 data.readStrongBinder());
780
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700781 // XXX can't add virtuals until binaries are updated.
Andrei Homescua543a842022-07-07 22:17:55 +0000782 // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
783 // shellCallbackBinder);
784 // return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700785 (void)in;
786 (void)out;
787 (void)err;
788
Yi Kongfdd8da92018-06-07 17:52:27 -0700789 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700790 resultReceiver->send(INVALID_OPERATION);
791 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200792
793 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700794 }
795
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700796 case SYSPROPS_TRANSACTION: {
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +0000797 if (!binder::os::report_sysprop_change()) return INVALID_OPERATION;
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700798 return NO_ERROR;
799 }
800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 default:
802 return UNKNOWN_TRANSACTION;
803 }
804}
805
Steven Morelandf0212002018-12-26 13:59:23 -0800806BBinder::Extras* BBinder::getOrCreateExtras()
807{
808 Extras* e = mExtras.load(std::memory_order_acquire);
809
810 if (!e) {
811 e = new Extras;
812 Extras* expected = nullptr;
813 if (!mExtras.compare_exchange_strong(expected, e,
814 std::memory_order_release,
815 std::memory_order_acquire)) {
816 delete e;
817 e = expected; // Filled in by CAS
818 }
819 if (e == nullptr) return nullptr; // out of memory
820 }
821
822 return e;
823}
824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825// ---------------------------------------------------------------------------
826
827enum {
828 // This is used to transfer ownership of the remote binder from
829 // the BpRefBase object holding it (when it is constructed), to the
830 // owner of the BpRefBase object when it first acquires that BpRefBase.
831 kRemoteAcquired = 0x00000001
832};
833
834BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700835 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836{
837 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
838
839 if (mRemote) {
840 mRemote->incStrong(this); // Removed on first IncStrong().
841 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
842 }
843}
844
845BpRefBase::~BpRefBase()
846{
847 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700848 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 mRemote->decStrong(this);
850 }
851 mRefs->decWeak(this);
852 }
853}
854
855void BpRefBase::onFirstRef()
856{
Bailey Forrest6913c462015-08-18 17:15:10 -0700857 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858}
859
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800860void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861{
862 if (mRemote) {
863 mRemote->decStrong(this);
864 }
865}
866
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800867bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868{
869 return mRemote ? mRefs->attemptIncStrong(this) : false;
870}
871
872// ---------------------------------------------------------------------------
873
Steven Moreland61ff8492019-09-26 16:05:45 -0700874} // namespace android