blob: a93dc1f674be906f5dc047d15dcb5a3efb6bb40f [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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 Rastogicc7a7462020-01-31 14:29:15 -080020#include <android/binder_shell.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070021#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070022
23#include <atomic>
Steven Moreland901c7452018-09-04 16:17:28 -070024#include <mutex>
Andrei Homescu9556bb12020-09-21 16:59:45 -070025#include <optional>
Steven Moreland901c7452018-09-04 16:17:28 -070026#include <vector>
Steven Moreland2e87adc2018-08-20 19:47:00 -070027
28#include <binder/Binder.h>
29#include <binder/IBinder.h>
Steven Morelanda194c452019-03-04 16:47:07 -080030#include <utils/Vector.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070031
32inline bool isUserCommand(transaction_code_t code) {
33 return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION;
34}
35
36struct ABBinder;
37struct ABpBinder;
38
39struct AIBinder : public virtual ::android::RefBase {
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -080040 explicit AIBinder(const AIBinder_Class* clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -070041 virtual ~AIBinder();
42
Steven Moreland71cddc32018-08-30 23:39:22 -070043 bool associateClass(const AIBinder_Class* clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -070044 const AIBinder_Class* getClass() const { return mClazz; }
45
Steven Moreland2e87adc2018-08-20 19:47:00 -070046 virtual ::android::sp<::android::IBinder> getBinder() = 0;
47 virtual ABBinder* asABBinder() { return nullptr; }
48 virtual ABpBinder* asABpBinder() { return nullptr; }
49
Steven Moreland5ea54da2018-09-04 13:29:55 -070050 bool isRemote() const {
51 ::android::sp<::android::IBinder> binder = const_cast<AIBinder*>(this)->getBinder();
52 return binder->remoteBinder() != nullptr;
53 }
Devin Moore9f9d0e12024-02-29 22:07:12 +000054 virtual void addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient,
55 void* cookie) = 0;
Steven Moreland2e87adc2018-08-20 19:47:00 -070056
Steven Moreland6cf66ac2018-11-02 18:14:54 -070057 private:
Steven Moreland2e87adc2018-08-20 19:47:00 -070058 // AIBinder instance is instance of this class for a local object. In order to transact on a
59 // remote object, this also must be set for simplicity (although right now, only the
60 // interfaceDescriptor from it is used).
Steven Morelandfaf25a42022-12-21 02:21:36 +000061 //
62 // WARNING: When multiple classes exist with the same interface descriptor in different
63 // linkernamespaces, the first one to be associated with mClazz becomes the canonical one
64 // and the only requirement on this is that the interface descriptors match. If this
65 // is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -070066 const AIBinder_Class* mClazz;
Andrei Homescu9556bb12020-09-21 16:59:45 -070067 std::mutex mClazzMutex;
Steven Moreland2e87adc2018-08-20 19:47:00 -070068};
69
70// This is a local AIBinder object with a known class.
71struct ABBinder : public AIBinder, public ::android::BBinder {
Steven Moreland2e87adc2018-08-20 19:47:00 -070072 virtual ~ABBinder();
73
74 void* getUserData() { return mUserData; }
75
76 ::android::sp<::android::IBinder> getBinder() override { return this; }
77 ABBinder* asABBinder() override { return this; }
78
79 const ::android::String16& getInterfaceDescriptor() const override;
Steven Morelanda194c452019-03-04 16:47:07 -080080 ::android::status_t dump(int fd, const ::android::Vector<::android::String16>& args) override;
Steven Moreland5d62e442018-09-13 15:01:02 -070081 ::android::status_t onTransact(uint32_t code, const ::android::Parcel& data,
82 ::android::Parcel* reply, binder_flags_t flags) override;
Devin Moore9f9d0e12024-02-29 22:07:12 +000083 void addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& /* recipient */,
84 void* /* cookie */) override;
Steven Moreland2e87adc2018-08-20 19:47:00 -070085
Steven Moreland6cf66ac2018-11-02 18:14:54 -070086 private:
Steven Moreland71cddc32018-08-30 23:39:22 -070087 ABBinder(const AIBinder_Class* clazz, void* userData);
88
89 // only thing that should create an ABBinder
90 friend AIBinder* AIBinder_new(const AIBinder_Class*, void*);
91
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 // Can contain implementation if this is a local binder. This can still be nullptr for a local
93 // binder. If it is nullptr, the implication is the implementation state is entirely external to
94 // this object and the functionality provided in the AIBinder_Class is sufficient.
95 void* mUserData;
96};
97
Steven Moreland94968952018-09-05 14:42:59 -070098// This binder object may be remote or local (even though it is 'Bp'). The implication if it is
99// local is that it is an IBinder object created outside of the domain of libbinder_ndk.
Steven Moreland3acd4902022-07-26 18:29:58 +0000100struct ABpBinder : public AIBinder {
Steven Moreland94968952018-09-05 14:42:59 -0700101 // Looks up to see if this object has or is an existing ABBinder or ABpBinder object, otherwise
102 // it creates an ABpBinder object.
103 static ::android::sp<AIBinder> lookupOrCreateFromBinder(
104 const ::android::sp<::android::IBinder>& binder);
Steven Moreland71cddc32018-08-30 23:39:22 -0700105
Steven Moreland2e87adc2018-08-20 19:47:00 -0700106 virtual ~ABpBinder();
107
Steven Moreland3acd4902022-07-26 18:29:58 +0000108 ::android::sp<::android::IBinder> getBinder() override { return mRemote; }
Steven Moreland2e87adc2018-08-20 19:47:00 -0700109 ABpBinder* asABpBinder() override { return this; }
Steven Moreland71cddc32018-08-30 23:39:22 -0700110
Steven Moreland418914a2023-06-28 21:47:14 +0000111 bool isServiceFuzzing() const { return mServiceFuzzing; }
112 void setServiceFuzzing() { mServiceFuzzing = true; }
Devin Moore9f9d0e12024-02-29 22:07:12 +0000113 void addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient,
114 void* cookie) override;
Steven Moreland418914a2023-06-28 21:47:14 +0000115
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700116 private:
Steven Moreland1f1bed62021-06-25 21:50:50 +0000117 friend android::sp<ABpBinder>;
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800118 explicit ABpBinder(const ::android::sp<::android::IBinder>& binder);
Steven Moreland3acd4902022-07-26 18:29:58 +0000119 ::android::sp<::android::IBinder> mRemote;
Steven Moreland418914a2023-06-28 21:47:14 +0000120 bool mServiceFuzzing = false;
Devin Moore9f9d0e12024-02-29 22:07:12 +0000121 struct DeathRecipientInfo {
122 android::wp<AIBinder_DeathRecipient> recipient;
123 void* cookie;
124 };
125 std::mutex mDeathRecipientsMutex;
126 std::vector<DeathRecipientInfo> mDeathRecipients;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700127};
128
129struct AIBinder_Class {
130 AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
131 AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact);
132
Stephen Crane8fde87f2020-11-17 15:06:11 -0800133 const ::android::String16& getInterfaceDescriptor() const { return mWideInterfaceDescriptor; }
134 const char* getInterfaceDescriptorUtf8() const { return mInterfaceDescriptor.c_str(); }
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000135 bool setTransactionCodeMap(const char** transactionCodeMap, size_t transactionCodeMapSize);
136 const char* getFunctionName(transaction_code_t code) const;
137 size_t getTransactionCodeToFunctionLength() const { return mTransactionCodeToFunctionLength; }
Steven Moreland2e87adc2018-08-20 19:47:00 -0700138
Steven Morelandf3ed8062021-08-27 14:31:14 -0700139 // whether a transaction header should be written
140 bool writeHeader = true;
141
Steven Morelanda194c452019-03-04 16:47:07 -0800142 // required to be non-null, implemented for every class
Steven Moreland88234072020-08-06 19:32:45 +0000143 const AIBinder_Class_onCreate onCreate = nullptr;
144 const AIBinder_Class_onDestroy onDestroy = nullptr;
145 const AIBinder_Class_onTransact onTransact = nullptr;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700146
Steven Morelanda194c452019-03-04 16:47:07 -0800147 // optional methods for a class
Steven Moreland88234072020-08-06 19:32:45 +0000148 AIBinder_onDump onDump = nullptr;
149 AIBinder_handleShellCommand handleShellCommand = nullptr;
Steven Morelanda194c452019-03-04 16:47:07 -0800150
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700151 private:
Stephen Crane8fde87f2020-11-17 15:06:11 -0800152 // Copy of the raw char string for when we don't have to return UTF-16
153 const std::string mInterfaceDescriptor;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700154 // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to
155 // one.
Stephen Crane8fde87f2020-11-17 15:06:11 -0800156 const ::android::String16 mWideInterfaceDescriptor;
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000157 // Array which holds names of the functions
158 const char** mTransactionCodeToFunction = nullptr;
159 // length of mmTransactionCodeToFunctionLength array
160 size_t mTransactionCodeToFunctionLength = 0;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700161};
Steven Moreland901c7452018-09-04 16:17:28 -0700162
163// Ownership is like this (when linked to death):
164//
165// AIBinder_DeathRecipient -sp-> TransferDeathRecipient <-wp-> IBinder
166//
167// When the AIBinder_DeathRecipient is dropped, so are the actual underlying death recipients. When
168// the IBinder dies, only a wp to it is kept.
Steven Moreland64127ca2019-03-13 09:25:44 -0700169struct AIBinder_DeathRecipient : ::android::RefBase {
Steven Moreland901c7452018-09-04 16:17:28 -0700170 // One of these is created for every linkToDeath. This is to be able to recover data when a
171 // binderDied receipt only gives us information about the IBinder.
172 struct TransferDeathRecipient : ::android::IBinder::DeathRecipient {
173 TransferDeathRecipient(const ::android::wp<::android::IBinder>& who, void* cookie,
Steven Moreland64127ca2019-03-13 09:25:44 -0700174 const ::android::wp<AIBinder_DeathRecipient>& parentRecipient,
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000175 const AIBinder_DeathRecipient_onBinderDied onDied,
176 const AIBinder_DeathRecipient_onBinderUnlinked onUnlinked)
177 : mWho(who),
178 mCookie(cookie),
179 mParentRecipient(parentRecipient),
180 mOnDied(onDied),
181 mOnUnlinked(onUnlinked) {}
182 ~TransferDeathRecipient();
Steven Moreland901c7452018-09-04 16:17:28 -0700183
184 void binderDied(const ::android::wp<::android::IBinder>& who) override;
185
186 const ::android::wp<::android::IBinder>& getWho() { return mWho; }
187 void* getCookie() { return mCookie; }
188
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700189 private:
Steven Moreland901c7452018-09-04 16:17:28 -0700190 ::android::wp<::android::IBinder> mWho;
191 void* mCookie;
Steven Moreland64127ca2019-03-13 09:25:44 -0700192
193 ::android::wp<AIBinder_DeathRecipient> mParentRecipient;
194
195 // This is kept separately from AIBinder_DeathRecipient in case the death recipient is
196 // deleted while the death notification is fired
Steven Morelande88055b2019-03-13 09:23:18 -0700197 const AIBinder_DeathRecipient_onBinderDied mOnDied;
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000198 const AIBinder_DeathRecipient_onBinderUnlinked mOnUnlinked;
Steven Moreland901c7452018-09-04 16:17:28 -0700199 };
200
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800201 explicit AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied);
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900202 binder_status_t linkToDeath(const ::android::sp<::android::IBinder>&, void* cookie);
203 binder_status_t unlinkToDeath(const ::android::sp<::android::IBinder>& binder, void* cookie);
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000204 void setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked);
Devin Moore9f9d0e12024-02-29 22:07:12 +0000205 void pruneThisTransferEntry(const ::android::sp<::android::IBinder>&, void* cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700206
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700207 private:
Steven Moreland64127ca2019-03-13 09:25:44 -0700208 // When the user of this API deletes a Bp object but not the death recipient, the
209 // TransferDeathRecipient object can't be cleaned up. This is called whenever a new
210 // TransferDeathRecipient is linked, and it ensures that mDeathRecipients can't grow unbounded.
211 void pruneDeadTransferEntriesLocked();
212
Steven Moreland901c7452018-09-04 16:17:28 -0700213 std::mutex mDeathRecipientsMutex;
214 std::vector<::android::sp<TransferDeathRecipient>> mDeathRecipients;
215 AIBinder_DeathRecipient_onBinderDied mOnDied;
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000216 AIBinder_DeathRecipient_onBinderUnlinked mOnUnlinked;
Steven Moreland901c7452018-09-04 16:17:28 -0700217};