blob: 6d26414c823caba074cfdf8d00919e769b85c47c [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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
Martijn Coenen4080edc2016-05-04 14:17:02 +020017#include <hwbinder/Binder.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070018
Steven Morelandde9793d2020-12-15 03:07:03 +000019#include <android-base/macros.h>
20#include <cutils/android_filesystem_config.h>
21#include <cutils/multiuser.h>
Yifan Hong1e118d22017-01-12 14:42:28 -080022#include <hwbinder/BpHwBinder.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020023#include <hwbinder/IInterface.h>
Steven Morelandde9793d2020-12-15 03:07:03 +000024#include <hwbinder/IPCThreadState.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020025#include <hwbinder/Parcel.h>
Steven Morelandde9793d2020-12-15 03:07:03 +000026#include <utils/Log.h>
27#include <utils/misc.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070028
Steven Morelandd9bdb652019-09-17 15:42:45 -070029#include <linux/sched.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070030#include <stdio.h>
31
Steven Morelandde9793d2020-12-15 03:07:03 +000032#include <atomic>
33
Mathias Agopian7922fa22009-05-18 15:08:03 -070034namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020035namespace hardware {
Mathias Agopian7922fa22009-05-18 15:08:03 -070036
37// ---------------------------------------------------------------------------
38
Mathias Agopiana6286c32009-05-22 19:00:22 -070039IBinder::IBinder()
40 : RefBase()
41{
42}
43
44IBinder::~IBinder()
45{
46}
47
48// ---------------------------------------------------------------------------
49
Yifan Hongdde40f32017-01-12 14:22:45 -080050BHwBinder* IBinder::localBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -070051{
Yi Kong55d41072018-07-23 14:55:39 -070052 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -070053}
54
Yifan Hong1e118d22017-01-12 14:42:28 -080055BpHwBinder* IBinder::remoteBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -070056{
Yi Kong55d41072018-07-23 14:55:39 -070057 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -070058}
59
60bool IBinder::checkSubclass(const void* /*subclassID*/) const
61{
62 return false;
63}
64
65// ---------------------------------------------------------------------------
66
Yifan Hongdde40f32017-01-12 14:22:45 -080067class BHwBinder::Extras
Mathias Agopian7922fa22009-05-18 15:08:03 -070068{
69public:
Steven Morelandb1a27632019-01-09 18:01:02 -080070 // unlocked objects
71 bool mRequestingSid = false;
72
73 // for below objects
Mathias Agopian7922fa22009-05-18 15:08:03 -070074 Mutex mLock;
Yifan Hong1e118d22017-01-12 14:42:28 -080075 BpHwBinder::ObjectManager mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -070076};
77
78// ---------------------------------------------------------------------------
79
Steven Moreland61d78e22019-01-09 19:49:27 +000080BHwBinder::BHwBinder() : mSchedPolicy(SCHED_NORMAL), mSchedPriority(0), mExtras(nullptr)
Mathias Agopian7922fa22009-05-18 15:08:03 -070081{
82}
83
Martijn Coenen3f181282017-05-03 13:45:32 -070084int BHwBinder::getMinSchedulingPolicy() {
85 return mSchedPolicy;
86}
87
88int BHwBinder::getMinSchedulingPriority() {
89 return mSchedPriority;
90}
91
Steven Morelandb1a27632019-01-09 18:01:02 -080092bool BHwBinder::isRequestingSid() {
93 Extras* e = mExtras.load(std::memory_order_acquire);
94
95 return e && e->mRequestingSid;
96}
97
98void BHwBinder::setRequestingSid(bool requestingSid) {
99 Extras* e = mExtras.load(std::memory_order_acquire);
100
101 if (!e) {
102 // default is false. Most things don't need sids, so avoiding allocations when possible.
103 if (!requestingSid) {
104 return;
105 }
106
107 e = getOrCreateExtras();
108 if (!e) return; // out of memory
109 }
110
Steven Moreland3668e032019-02-08 17:56:59 -0800111 e->mRequestingSid = requestingSid;
Steven Morelandb1a27632019-01-09 18:01:02 -0800112}
113
Yifan Hongdde40f32017-01-12 14:22:45 -0800114status_t BHwBinder::transact(
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200115 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags, TransactCallback callback)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700116{
117 data.setDataPosition(0);
118
Steven Morelanda80270c2020-11-19 21:08:28 +0000119 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
120 reply->markSensitive();
121 }
122
Steven Morelandde9793d2020-12-15 03:07:03 +0000123 // extra comment to try to force running all tests
124 if (UNLIKELY(code == HIDL_DEBUG_TRANSACTION)) {
125 uid_t uid = IPCThreadState::self()->getCallingUid();
126 if (multiuser_get_app_id(uid) >= AID_APP_START) {
127 ALOGE("Can not call IBase::debug from apps");
128 return PERMISSION_DENIED;
129 }
130 }
131
Florian Mayer634f84f2022-02-24 15:08:51 -0800132 return onTransact(code, data, reply, flags, [&](auto& replyParcel) {
133 replyParcel.setDataPosition(0);
134 if (callback != nullptr) {
135 callback(replyParcel);
136 }
137 });
Mathias Agopian7922fa22009-05-18 15:08:03 -0700138}
139
Yifan Hongdde40f32017-01-12 14:22:45 -0800140status_t BHwBinder::linkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800141 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
142 uint32_t /*flags*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700143{
144 return INVALID_OPERATION;
145}
146
Yifan Hongdde40f32017-01-12 14:22:45 -0800147status_t BHwBinder::unlinkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800148 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
149 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700150{
151 return INVALID_OPERATION;
152}
153
Yifan Hongdde40f32017-01-12 14:22:45 -0800154void BHwBinder::attachObject(
Mathias Agopian7922fa22009-05-18 15:08:03 -0700155 const void* objectID, void* object, void* cleanupCookie,
156 object_cleanup_func func)
157{
Steven Morelandb1a27632019-01-09 18:01:02 -0800158 Extras* e = getOrCreateExtras();
159 if (!e) return; // out of memory
Mathias Agopian7922fa22009-05-18 15:08:03 -0700160
161 AutoMutex _l(e->mLock);
162 e->mObjects.attach(objectID, object, cleanupCookie, func);
163}
164
Yifan Hongdde40f32017-01-12 14:22:45 -0800165void* BHwBinder::findObject(const void* objectID) const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700166{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700167 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kong55d41072018-07-23 14:55:39 -0700168 if (!e) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700169
170 AutoMutex _l(e->mLock);
171 return e->mObjects.find(objectID);
172}
173
Yifan Hongdde40f32017-01-12 14:22:45 -0800174void BHwBinder::detachObject(const void* objectID)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700175{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700176 Extras* e = mExtras.load(std::memory_order_acquire);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700177 if (!e) return;
178
179 AutoMutex _l(e->mLock);
180 e->mObjects.detach(objectID);
181}
182
Yifan Hongdde40f32017-01-12 14:22:45 -0800183BHwBinder* BHwBinder::localBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700184{
185 return this;
186}
187
Yifan Hongdde40f32017-01-12 14:22:45 -0800188BHwBinder::~BHwBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700189{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700190 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm99c620a2014-08-12 22:56:00 +0000191 if (e) delete e;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700192}
193
194
Yifan Hongdde40f32017-01-12 14:22:45 -0800195status_t BHwBinder::onTransact(
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100196 uint32_t /*code*/, const Parcel& /*data*/, Parcel* /*reply*/, uint32_t /*flags*/,
197 TransactCallback /*callback*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700198{
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100199 return UNKNOWN_TRANSACTION;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700200}
201
Steven Morelandb1a27632019-01-09 18:01:02 -0800202BHwBinder::Extras* BHwBinder::getOrCreateExtras()
203{
204 Extras* e = mExtras.load(std::memory_order_acquire);
205
206 if (!e) {
207 e = new Extras;
208 Extras* expected = nullptr;
209 if (!mExtras.compare_exchange_strong(expected, e,
210 std::memory_order_release,
211 std::memory_order_acquire)) {
212 delete e;
213 e = expected; // Filled in by CAS
214 }
215 if (e == nullptr) return nullptr; // out of memory
216 }
217
218 return e;
219}
220
Mathias Agopian7922fa22009-05-18 15:08:03 -0700221// ---------------------------------------------------------------------------
222
223enum {
224 // This is used to transfer ownership of the remote binder from
Yifan Honga2ce3b82017-01-12 14:49:26 -0800225 // the BpHwRefBase object holding it (when it is constructed), to the
226 // owner of the BpHwRefBase object when it first acquires that BpHwRefBase.
Mathias Agopian7922fa22009-05-18 15:08:03 -0700227 kRemoteAcquired = 0x00000001
228};
229
Yifan Honga2ce3b82017-01-12 14:49:26 -0800230BpHwRefBase::BpHwRefBase(const sp<IBinder>& o)
Yi Kong55d41072018-07-23 14:55:39 -0700231 : mRemote(o.get()), mRefs(nullptr), mState(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700232{
Mathias Agopian7922fa22009-05-18 15:08:03 -0700233 if (mRemote) {
234 mRemote->incStrong(this); // Removed on first IncStrong().
Mathias Agopian7922fa22009-05-18 15:08:03 -0700235 }
236}
237
Yifan Honga2ce3b82017-01-12 14:49:26 -0800238BpHwRefBase::~BpHwRefBase()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700239{
240 if (mRemote) {
Bailey Forreste20d6f42015-08-18 17:15:10 -0700241 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700242 mRemote->decStrong(this);
243 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700244 }
245}
246
Yifan Honga2ce3b82017-01-12 14:49:26 -0800247void BpHwRefBase::onFirstRef()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700248{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700249 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700250}
251
Yifan Honga2ce3b82017-01-12 14:49:26 -0800252void BpHwRefBase::onLastStrongRef(const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700253{
254 if (mRemote) {
255 mRemote->decStrong(this);
256 }
257}
258
Yifan Honga2ce3b82017-01-12 14:49:26 -0800259bool BpHwRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700260{
Martijn Coenen35a7b1e2017-10-09 10:35:42 +0200261 return false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700262}
263
264// ---------------------------------------------------------------------------
265
Steven Moreland7173a4c2019-09-26 15:55:02 -0700266} // namespace hardware
267} // namespace android