blob: b90639f1b723962f6f12a8bfd2992382b8605ea8 [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
Mathias Agopian7922fa22009-05-18 15:08:03 -0700132 status_t err = NO_ERROR;
133 switch (code) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700134 default:
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200135 err = onTransact(code, data, reply, flags,
136 [&](auto &replyParcel) {
137 replyParcel.setDataPosition(0);
Yi Kong55d41072018-07-23 14:55:39 -0700138 if (callback != nullptr) {
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200139 callback(replyParcel);
140 }
141 });
Mathias Agopian7922fa22009-05-18 15:08:03 -0700142 break;
143 }
144
Mathias Agopian7922fa22009-05-18 15:08:03 -0700145 return err;
146}
147
Yifan Hongdde40f32017-01-12 14:22:45 -0800148status_t BHwBinder::linkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800149 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
150 uint32_t /*flags*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700151{
152 return INVALID_OPERATION;
153}
154
Yifan Hongdde40f32017-01-12 14:22:45 -0800155status_t BHwBinder::unlinkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800156 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
157 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700158{
159 return INVALID_OPERATION;
160}
161
Yifan Hongdde40f32017-01-12 14:22:45 -0800162void BHwBinder::attachObject(
Mathias Agopian7922fa22009-05-18 15:08:03 -0700163 const void* objectID, void* object, void* cleanupCookie,
164 object_cleanup_func func)
165{
Steven Morelandb1a27632019-01-09 18:01:02 -0800166 Extras* e = getOrCreateExtras();
167 if (!e) return; // out of memory
Mathias Agopian7922fa22009-05-18 15:08:03 -0700168
169 AutoMutex _l(e->mLock);
170 e->mObjects.attach(objectID, object, cleanupCookie, func);
171}
172
Yifan Hongdde40f32017-01-12 14:22:45 -0800173void* BHwBinder::findObject(const void* objectID) const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700174{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700175 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kong55d41072018-07-23 14:55:39 -0700176 if (!e) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700177
178 AutoMutex _l(e->mLock);
179 return e->mObjects.find(objectID);
180}
181
Yifan Hongdde40f32017-01-12 14:22:45 -0800182void BHwBinder::detachObject(const void* objectID)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700183{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700184 Extras* e = mExtras.load(std::memory_order_acquire);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700185 if (!e) return;
186
187 AutoMutex _l(e->mLock);
188 e->mObjects.detach(objectID);
189}
190
Yifan Hongdde40f32017-01-12 14:22:45 -0800191BHwBinder* BHwBinder::localBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700192{
193 return this;
194}
195
Yifan Hongdde40f32017-01-12 14:22:45 -0800196BHwBinder::~BHwBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700197{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700198 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm99c620a2014-08-12 22:56:00 +0000199 if (e) delete e;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700200}
201
202
Yifan Hongdde40f32017-01-12 14:22:45 -0800203status_t BHwBinder::onTransact(
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100204 uint32_t /*code*/, const Parcel& /*data*/, Parcel* /*reply*/, uint32_t /*flags*/,
205 TransactCallback /*callback*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700206{
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100207 return UNKNOWN_TRANSACTION;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700208}
209
Steven Morelandb1a27632019-01-09 18:01:02 -0800210BHwBinder::Extras* BHwBinder::getOrCreateExtras()
211{
212 Extras* e = mExtras.load(std::memory_order_acquire);
213
214 if (!e) {
215 e = new Extras;
216 Extras* expected = nullptr;
217 if (!mExtras.compare_exchange_strong(expected, e,
218 std::memory_order_release,
219 std::memory_order_acquire)) {
220 delete e;
221 e = expected; // Filled in by CAS
222 }
223 if (e == nullptr) return nullptr; // out of memory
224 }
225
226 return e;
227}
228
Mathias Agopian7922fa22009-05-18 15:08:03 -0700229// ---------------------------------------------------------------------------
230
231enum {
232 // This is used to transfer ownership of the remote binder from
Yifan Honga2ce3b82017-01-12 14:49:26 -0800233 // the BpHwRefBase object holding it (when it is constructed), to the
234 // owner of the BpHwRefBase object when it first acquires that BpHwRefBase.
Mathias Agopian7922fa22009-05-18 15:08:03 -0700235 kRemoteAcquired = 0x00000001
236};
237
Yifan Honga2ce3b82017-01-12 14:49:26 -0800238BpHwRefBase::BpHwRefBase(const sp<IBinder>& o)
Yi Kong55d41072018-07-23 14:55:39 -0700239 : mRemote(o.get()), mRefs(nullptr), mState(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700240{
Mathias Agopian7922fa22009-05-18 15:08:03 -0700241 if (mRemote) {
242 mRemote->incStrong(this); // Removed on first IncStrong().
Mathias Agopian7922fa22009-05-18 15:08:03 -0700243 }
244}
245
Yifan Honga2ce3b82017-01-12 14:49:26 -0800246BpHwRefBase::~BpHwRefBase()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700247{
248 if (mRemote) {
Bailey Forreste20d6f42015-08-18 17:15:10 -0700249 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700250 mRemote->decStrong(this);
251 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700252 }
253}
254
Yifan Honga2ce3b82017-01-12 14:49:26 -0800255void BpHwRefBase::onFirstRef()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700256{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700257 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700258}
259
Yifan Honga2ce3b82017-01-12 14:49:26 -0800260void BpHwRefBase::onLastStrongRef(const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700261{
262 if (mRemote) {
263 mRemote->decStrong(this);
264 }
265}
266
Yifan Honga2ce3b82017-01-12 14:49:26 -0800267bool BpHwRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700268{
Martijn Coenen35a7b1e2017-10-09 10:35:42 +0200269 return false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700270}
271
272// ---------------------------------------------------------------------------
273
Steven Moreland7173a4c2019-09-26 15:55:02 -0700274} // namespace hardware
275} // namespace android