Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 17 | #include <hwbinder/Binder.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 18 | |
Steven Moreland | de9793d | 2020-12-15 03:07:03 +0000 | [diff] [blame] | 19 | #include <android-base/macros.h> |
| 20 | #include <cutils/android_filesystem_config.h> |
| 21 | #include <cutils/multiuser.h> |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 22 | #include <hwbinder/BpHwBinder.h> |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 23 | #include <hwbinder/IInterface.h> |
Steven Moreland | de9793d | 2020-12-15 03:07:03 +0000 | [diff] [blame] | 24 | #include <hwbinder/IPCThreadState.h> |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 25 | #include <hwbinder/Parcel.h> |
Steven Moreland | de9793d | 2020-12-15 03:07:03 +0000 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/misc.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 28 | |
Steven Moreland | d9bdb65 | 2019-09-17 15:42:45 -0700 | [diff] [blame] | 29 | #include <linux/sched.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | |
Steven Moreland | de9793d | 2020-12-15 03:07:03 +0000 | [diff] [blame] | 32 | #include <atomic> |
| 33 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 34 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 35 | namespace hardware { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 36 | |
| 37 | // --------------------------------------------------------------------------- |
| 38 | |
Mathias Agopian | a6286c3 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 39 | IBinder::IBinder() |
| 40 | : RefBase() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | IBinder::~IBinder() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | // --------------------------------------------------------------------------- |
| 49 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 50 | BHwBinder* IBinder::localBinder() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 51 | { |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 52 | return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 55 | BpHwBinder* IBinder::remoteBinder() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 56 | { |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 57 | return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool IBinder::checkSubclass(const void* /*subclassID*/) const |
| 61 | { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // --------------------------------------------------------------------------- |
| 66 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 67 | class BHwBinder::Extras |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 68 | { |
| 69 | public: |
Steven Moreland | b1a2763 | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 70 | // unlocked objects |
| 71 | bool mRequestingSid = false; |
| 72 | |
| 73 | // for below objects |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 74 | Mutex mLock; |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 75 | BpHwBinder::ObjectManager mObjects; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | |
Steven Moreland | 61d78e2 | 2019-01-09 19:49:27 +0000 | [diff] [blame] | 80 | BHwBinder::BHwBinder() : mSchedPolicy(SCHED_NORMAL), mSchedPriority(0), mExtras(nullptr) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Martijn Coenen | 3f18128 | 2017-05-03 13:45:32 -0700 | [diff] [blame] | 84 | int BHwBinder::getMinSchedulingPolicy() { |
| 85 | return mSchedPolicy; |
| 86 | } |
| 87 | |
| 88 | int BHwBinder::getMinSchedulingPriority() { |
| 89 | return mSchedPriority; |
| 90 | } |
| 91 | |
Steven Moreland | b1a2763 | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 92 | bool BHwBinder::isRequestingSid() { |
| 93 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 94 | |
| 95 | return e && e->mRequestingSid; |
| 96 | } |
| 97 | |
| 98 | void 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 Moreland | 3668e03 | 2019-02-08 17:56:59 -0800 | [diff] [blame] | 111 | e->mRequestingSid = requestingSid; |
Steven Moreland | b1a2763 | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 114 | status_t BHwBinder::transact( |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 115 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags, TransactCallback callback) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 116 | { |
| 117 | data.setDataPosition(0); |
| 118 | |
Steven Moreland | a80270c | 2020-11-19 21:08:28 +0000 | [diff] [blame] | 119 | if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) { |
| 120 | reply->markSensitive(); |
| 121 | } |
| 122 | |
Steven Moreland | de9793d | 2020-12-15 03:07:03 +0000 | [diff] [blame] | 123 | // 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 Mayer | 634f84f | 2022-02-24 15:08:51 -0800 | [diff] [blame] | 132 | return onTransact(code, data, reply, flags, [&](auto& replyParcel) { |
| 133 | replyParcel.setDataPosition(0); |
| 134 | if (callback != nullptr) { |
| 135 | callback(replyParcel); |
| 136 | } |
| 137 | }); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 140 | status_t BHwBinder::linkToDeath( |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 141 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 142 | uint32_t /*flags*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 143 | { |
| 144 | return INVALID_OPERATION; |
| 145 | } |
| 146 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 147 | status_t BHwBinder::unlinkToDeath( |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 148 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 149 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 150 | { |
| 151 | return INVALID_OPERATION; |
| 152 | } |
| 153 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 154 | void BHwBinder::attachObject( |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 155 | const void* objectID, void* object, void* cleanupCookie, |
| 156 | object_cleanup_func func) |
| 157 | { |
Steven Moreland | b1a2763 | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 158 | Extras* e = getOrCreateExtras(); |
| 159 | if (!e) return; // out of memory |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 160 | |
| 161 | AutoMutex _l(e->mLock); |
| 162 | e->mObjects.attach(objectID, object, cleanupCookie, func); |
| 163 | } |
| 164 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 165 | void* BHwBinder::findObject(const void* objectID) const |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 166 | { |
Bailey Forrest | e20d6f4 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 167 | Extras* e = mExtras.load(std::memory_order_acquire); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 168 | if (!e) return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 169 | |
| 170 | AutoMutex _l(e->mLock); |
| 171 | return e->mObjects.find(objectID); |
| 172 | } |
| 173 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 174 | void BHwBinder::detachObject(const void* objectID) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 175 | { |
Bailey Forrest | e20d6f4 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 176 | Extras* e = mExtras.load(std::memory_order_acquire); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 177 | if (!e) return; |
| 178 | |
| 179 | AutoMutex _l(e->mLock); |
| 180 | e->mObjects.detach(objectID); |
| 181 | } |
| 182 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 183 | BHwBinder* BHwBinder::localBinder() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 184 | { |
| 185 | return this; |
| 186 | } |
| 187 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 188 | BHwBinder::~BHwBinder() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 189 | { |
Bailey Forrest | e20d6f4 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 190 | Extras* e = mExtras.load(std::memory_order_relaxed); |
Hans Boehm | 99c620a | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 191 | if (e) delete e; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 195 | status_t BHwBinder::onTransact( |
Martijn Coenen | 5c6fe25 | 2017-01-18 15:33:31 +0100 | [diff] [blame] | 196 | uint32_t /*code*/, const Parcel& /*data*/, Parcel* /*reply*/, uint32_t /*flags*/, |
| 197 | TransactCallback /*callback*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 198 | { |
Martijn Coenen | 5c6fe25 | 2017-01-18 15:33:31 +0100 | [diff] [blame] | 199 | return UNKNOWN_TRANSACTION; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Steven Moreland | b1a2763 | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 202 | BHwBinder::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 Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 221 | // --------------------------------------------------------------------------- |
| 222 | |
| 223 | enum { |
| 224 | // This is used to transfer ownership of the remote binder from |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 225 | // the BpHwRefBase object holding it (when it is constructed), to the |
| 226 | // owner of the BpHwRefBase object when it first acquires that BpHwRefBase. |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 227 | kRemoteAcquired = 0x00000001 |
| 228 | }; |
| 229 | |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 230 | BpHwRefBase::BpHwRefBase(const sp<IBinder>& o) |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 231 | : mRemote(o.get()), mRefs(nullptr), mState(0) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 232 | { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 233 | if (mRemote) { |
| 234 | mRemote->incStrong(this); // Removed on first IncStrong(). |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 238 | BpHwRefBase::~BpHwRefBase() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 239 | { |
| 240 | if (mRemote) { |
Bailey Forrest | e20d6f4 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 241 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 242 | mRemote->decStrong(this); |
| 243 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 247 | void BpHwRefBase::onFirstRef() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 248 | { |
Bailey Forrest | e20d6f4 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 249 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 252 | void BpHwRefBase::onLastStrongRef(const void* /*id*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 253 | { |
| 254 | if (mRemote) { |
| 255 | mRemote->decStrong(this); |
| 256 | } |
| 257 | } |
| 258 | |
Yifan Hong | a2ce3b8 | 2017-01-12 14:49:26 -0800 | [diff] [blame] | 259 | bool BpHwRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 260 | { |
Martijn Coenen | 35a7b1e | 2017-10-09 10:35:42 +0200 | [diff] [blame] | 261 | return false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // --------------------------------------------------------------------------- |
| 265 | |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 266 | } // namespace hardware |
| 267 | } // namespace android |