Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <android/binder_ibinder.h> |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 20 | #include <android/binder_shell.h> |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 21 | #include "ibinder_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 22 | |
| 23 | #include <atomic> |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 24 | #include <mutex> |
Andrei Homescu | 9556bb1 | 2020-09-21 16:59:45 -0700 | [diff] [blame] | 25 | #include <optional> |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 26 | #include <vector> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 27 | |
| 28 | #include <binder/Binder.h> |
| 29 | #include <binder/IBinder.h> |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 30 | #include <utils/Vector.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 31 | |
| 32 | inline bool isUserCommand(transaction_code_t code) { |
| 33 | return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION; |
| 34 | } |
| 35 | |
| 36 | struct ABBinder; |
| 37 | struct ABpBinder; |
| 38 | |
| 39 | struct AIBinder : public virtual ::android::RefBase { |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 40 | explicit AIBinder(const AIBinder_Class* clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 41 | virtual ~AIBinder(); |
| 42 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 43 | bool associateClass(const AIBinder_Class* clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 44 | const AIBinder_Class* getClass() const { return mClazz; } |
| 45 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 46 | virtual ::android::sp<::android::IBinder> getBinder() = 0; |
| 47 | virtual ABBinder* asABBinder() { return nullptr; } |
| 48 | virtual ABpBinder* asABpBinder() { return nullptr; } |
| 49 | |
Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 50 | bool isRemote() const { |
| 51 | ::android::sp<::android::IBinder> binder = const_cast<AIBinder*>(this)->getBinder(); |
| 52 | return binder->remoteBinder() != nullptr; |
| 53 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 54 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 55 | private: |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 56 | // AIBinder instance is instance of this class for a local object. In order to transact on a |
| 57 | // remote object, this also must be set for simplicity (although right now, only the |
| 58 | // interfaceDescriptor from it is used). |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 59 | // |
| 60 | // WARNING: When multiple classes exist with the same interface descriptor in different |
| 61 | // linkernamespaces, the first one to be associated with mClazz becomes the canonical one |
| 62 | // and the only requirement on this is that the interface descriptors match. If this |
| 63 | // is an ABpBinder, no other state can be referenced from mClazz. |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 64 | const AIBinder_Class* mClazz; |
Andrei Homescu | 9556bb1 | 2020-09-21 16:59:45 -0700 | [diff] [blame] | 65 | std::mutex mClazzMutex; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // This is a local AIBinder object with a known class. |
| 69 | struct ABBinder : public AIBinder, public ::android::BBinder { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 70 | virtual ~ABBinder(); |
| 71 | |
| 72 | void* getUserData() { return mUserData; } |
| 73 | |
| 74 | ::android::sp<::android::IBinder> getBinder() override { return this; } |
| 75 | ABBinder* asABBinder() override { return this; } |
| 76 | |
| 77 | const ::android::String16& getInterfaceDescriptor() const override; |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 78 | ::android::status_t dump(int fd, const ::android::Vector<::android::String16>& args) override; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 79 | ::android::status_t onTransact(uint32_t code, const ::android::Parcel& data, |
| 80 | ::android::Parcel* reply, binder_flags_t flags) override; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 81 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 82 | private: |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 83 | ABBinder(const AIBinder_Class* clazz, void* userData); |
| 84 | |
| 85 | // only thing that should create an ABBinder |
| 86 | friend AIBinder* AIBinder_new(const AIBinder_Class*, void*); |
| 87 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 88 | // Can contain implementation if this is a local binder. This can still be nullptr for a local |
| 89 | // binder. If it is nullptr, the implication is the implementation state is entirely external to |
| 90 | // this object and the functionality provided in the AIBinder_Class is sufficient. |
| 91 | void* mUserData; |
| 92 | }; |
| 93 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 94 | // This binder object may be remote or local (even though it is 'Bp'). The implication if it is |
| 95 | // local is that it is an IBinder object created outside of the domain of libbinder_ndk. |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 96 | struct ABpBinder : public AIBinder { |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 97 | // Looks up to see if this object has or is an existing ABBinder or ABpBinder object, otherwise |
| 98 | // it creates an ABpBinder object. |
| 99 | static ::android::sp<AIBinder> lookupOrCreateFromBinder( |
| 100 | const ::android::sp<::android::IBinder>& binder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 101 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 102 | virtual ~ABpBinder(); |
| 103 | |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 104 | ::android::sp<::android::IBinder> getBinder() override { return mRemote; } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 105 | ABpBinder* asABpBinder() override { return this; } |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 106 | |
Steven Moreland | 418914a | 2023-06-28 21:47:14 +0000 | [diff] [blame] | 107 | bool isServiceFuzzing() const { return mServiceFuzzing; } |
| 108 | void setServiceFuzzing() { mServiceFuzzing = true; } |
| 109 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 110 | private: |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 111 | friend android::sp<ABpBinder>; |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 112 | explicit ABpBinder(const ::android::sp<::android::IBinder>& binder); |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 113 | ::android::sp<::android::IBinder> mRemote; |
Steven Moreland | 418914a | 2023-06-28 21:47:14 +0000 | [diff] [blame] | 114 | bool mServiceFuzzing = false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct AIBinder_Class { |
| 118 | AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 119 | AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact); |
| 120 | |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 121 | const ::android::String16& getInterfaceDescriptor() const { return mWideInterfaceDescriptor; } |
| 122 | const char* getInterfaceDescriptorUtf8() const { return mInterfaceDescriptor.c_str(); } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 123 | |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 124 | // whether a transaction header should be written |
| 125 | bool writeHeader = true; |
| 126 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 127 | // required to be non-null, implemented for every class |
Steven Moreland | 8823407 | 2020-08-06 19:32:45 +0000 | [diff] [blame] | 128 | const AIBinder_Class_onCreate onCreate = nullptr; |
| 129 | const AIBinder_Class_onDestroy onDestroy = nullptr; |
| 130 | const AIBinder_Class_onTransact onTransact = nullptr; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 131 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 132 | // optional methods for a class |
Steven Moreland | 8823407 | 2020-08-06 19:32:45 +0000 | [diff] [blame] | 133 | AIBinder_onDump onDump = nullptr; |
| 134 | AIBinder_handleShellCommand handleShellCommand = nullptr; |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 135 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 136 | private: |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 137 | // Copy of the raw char string for when we don't have to return UTF-16 |
| 138 | const std::string mInterfaceDescriptor; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 139 | // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to |
| 140 | // one. |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 141 | const ::android::String16 mWideInterfaceDescriptor; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 142 | }; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 143 | |
| 144 | // Ownership is like this (when linked to death): |
| 145 | // |
| 146 | // AIBinder_DeathRecipient -sp-> TransferDeathRecipient <-wp-> IBinder |
| 147 | // |
| 148 | // When the AIBinder_DeathRecipient is dropped, so are the actual underlying death recipients. When |
| 149 | // the IBinder dies, only a wp to it is kept. |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 150 | struct AIBinder_DeathRecipient : ::android::RefBase { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 151 | // One of these is created for every linkToDeath. This is to be able to recover data when a |
| 152 | // binderDied receipt only gives us information about the IBinder. |
| 153 | struct TransferDeathRecipient : ::android::IBinder::DeathRecipient { |
| 154 | TransferDeathRecipient(const ::android::wp<::android::IBinder>& who, void* cookie, |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 155 | const ::android::wp<AIBinder_DeathRecipient>& parentRecipient, |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 156 | const AIBinder_DeathRecipient_onBinderDied onDied, |
| 157 | const AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) |
| 158 | : mWho(who), |
| 159 | mCookie(cookie), |
| 160 | mParentRecipient(parentRecipient), |
| 161 | mOnDied(onDied), |
| 162 | mOnUnlinked(onUnlinked) {} |
| 163 | ~TransferDeathRecipient(); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 164 | |
| 165 | void binderDied(const ::android::wp<::android::IBinder>& who) override; |
| 166 | |
| 167 | const ::android::wp<::android::IBinder>& getWho() { return mWho; } |
| 168 | void* getCookie() { return mCookie; } |
| 169 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 170 | private: |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 171 | ::android::wp<::android::IBinder> mWho; |
| 172 | void* mCookie; |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 173 | |
| 174 | ::android::wp<AIBinder_DeathRecipient> mParentRecipient; |
| 175 | |
| 176 | // This is kept separately from AIBinder_DeathRecipient in case the death recipient is |
| 177 | // deleted while the death notification is fired |
Steven Moreland | e88055b | 2019-03-13 09:23:18 -0700 | [diff] [blame] | 178 | const AIBinder_DeathRecipient_onBinderDied mOnDied; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 179 | const AIBinder_DeathRecipient_onBinderUnlinked mOnUnlinked; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 182 | explicit AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied); |
Jiyong Park | 4f97a8c | 2020-11-16 18:11:14 +0900 | [diff] [blame] | 183 | binder_status_t linkToDeath(const ::android::sp<::android::IBinder>&, void* cookie); |
| 184 | binder_status_t unlinkToDeath(const ::android::sp<::android::IBinder>& binder, void* cookie); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 185 | void setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 186 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 187 | private: |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 188 | // When the user of this API deletes a Bp object but not the death recipient, the |
| 189 | // TransferDeathRecipient object can't be cleaned up. This is called whenever a new |
| 190 | // TransferDeathRecipient is linked, and it ensures that mDeathRecipients can't grow unbounded. |
| 191 | void pruneDeadTransferEntriesLocked(); |
| 192 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 193 | std::mutex mDeathRecipientsMutex; |
| 194 | std::vector<::android::sp<TransferDeathRecipient>> mDeathRecipients; |
| 195 | AIBinder_DeathRecipient_onBinderDied mOnDied; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 196 | AIBinder_DeathRecipient_onBinderUnlinked mOnUnlinked; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 197 | }; |