Debian Codegraph @

src/core/ext/transport/binder/utils/ndk_binder.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/ext/transport/binder/utils/ndk_binder.h"
18
19 #ifndef GRPC_NO_BINDER
20
21 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
22
23 #include <dlfcn.h>
24
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/gprpp/sync.h"
28
29 namespace {
30 void* GetNdkBinderHandle() {
31 // TODO(mingcl): Consider using RTLD_NOLOAD to check if it is already loaded
32 // first
33 static void* handle = dlopen("libbinder_ndk.so", RTLD_LAZY);
34 if (handle == nullptr) {
35 gpr_log(
36 GPR_ERROR,
37 "Cannot open libbinder_ndk.so. Does this device support API level 29?");
38 GPR_ASSERT(0);
39 }
40 return handle;
41 }
42
43 JavaVM* g_jvm = nullptr;
44 grpc_core::Mutex g_jvm_mu;
45
46 // Whether the thread has already attached to JVM (this is to prevent
47 // repeated attachment in `AttachJvm()`)
48 thread_local bool g_is_jvm_attached = false;
49
50 void SetJvm(JNIEnv* env) {
51 // OK to lock here since this function will only be called once for each
52 // connection.
53 grpc_core::MutexLock lock(&g_jvm_mu);
54 if (g_jvm != nullptr) {
55 return;
56 }
57 JavaVM* jvm = nullptr;
58 jint error = env->GetJavaVM(&jvm);
59 if (error != JNI_OK) {
60 gpr_log(GPR_ERROR, "Failed to get JVM");
61 }
62 g_jvm = jvm;
63 gpr_log(GPR_INFO, "JVM cached");
64 }
65
66 // `SetJvm` need to be called in the process before `AttachJvm`. This is always
67 // the case because one of `AIBinder_fromJavaBinder`/`AIBinder_toJavaBinder`
68 // will be called before we actually uses the binder. Return `false` if not able
69 // to attach to JVM. Return `true` if JVM is attached (or already attached).
70 bool AttachJvm() {
71 if (g_is_jvm_attached) {
72 return true;
73 }
74 // Note: The following code would be run at most once per thread.
75 grpc_core::MutexLock lock(&g_jvm_mu);
76 if (g_jvm == nullptr) {
77 gpr_log(GPR_ERROR, "JVM not cached yet");
78 return false;
79 }
80 JNIEnv* env_unused;
81 // Note that attach a thread that is already attached is a no-op, so it is
82 // fine to call this again if the thread has already been attached by other.
83 g_jvm->AttachCurrentThread(&env_unused, /* thr_args= */ nullptr);
84 gpr_log(GPR_INFO, "JVM attached successfully");
85 g_is_jvm_attached = true;
86 return true;
87 }
88
89 } // namespace
90
91 namespace grpc_binder {
92 namespace ndk_util {
93
94 // Helper macro to obtain the function pointer corresponding to the name
95 #define FORWARD(name) \
96 typedef decltype(&name) func_type; \
97 static func_type ptr = \
98 reinterpret_cast<func_type>(dlsym(GetNdkBinderHandle(), #name)); \
99 if (ptr == nullptr) { \
100 gpr_log(GPR_ERROR, \
101 "dlsym failed. Cannot find %s in libbinder_ndk.so. " \
102 "BinderTransport requires API level >= 33", \
103 #name); \
104 GPR_ASSERT(0); \
105 } \
106 return ptr
107
108 void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
109 FORWARD(AIBinder_Class_disableInterfaceTokenHeader)(clazz);
110 }
111
112 void* AIBinder_getUserData(AIBinder* binder) {
113 FORWARD(AIBinder_getUserData)(binder);
114 }
115
116 uid_t AIBinder_getCallingUid() { FORWARD(AIBinder_getCallingUid)(); }
117
118 AIBinder* AIBinder_fromJavaBinder(JNIEnv* env, jobject binder) {
119 SetJvm(env);
120 FORWARD(AIBinder_fromJavaBinder)(env, binder);
121 }
122
123 AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
124 AIBinder_Class_onCreate onCreate,
125 AIBinder_Class_onDestroy onDestroy,
126 AIBinder_Class_onTransact onTransact) {
127 FORWARD(AIBinder_Class_define)
128 (interfaceDescriptor, onCreate, onDestroy, onTransact);
129 }
130
131 AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
132 FORWARD(AIBinder_new)(clazz, args);
133 }
134
135 bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
136 FORWARD(AIBinder_associateClass)(binder, clazz);
137 }
138
139 void AIBinder_incStrong(AIBinder* binder) {
140 FORWARD(AIBinder_incStrong)(binder);
141 }
142
143 void AIBinder_decStrong(AIBinder* binder) {
144 FORWARD(AIBinder_decStrong)(binder);
145 }
146
147 binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code,
148 AParcel** in, AParcel** out,
149 binder_flags_t flags) {
150 if (!AttachJvm()) {
151 gpr_log(GPR_ERROR, "failed to attach JVM. AIBinder_transact might fail.");
152 }
153 FORWARD(AIBinder_transact)(binder, code, in, out, flags);
154 }
155
156 binder_status_t AParcel_readByteArray(const AParcel* parcel, void* arrayData,
157 AParcel_byteArrayAllocator allocator) {
158 FORWARD(AParcel_readByteArray)(parcel, arrayData, allocator);
159 }
160
161 void AParcel_delete(AParcel* parcel) { FORWARD(AParcel_delete)(parcel); }
162 int32_t AParcel_getDataSize(const AParcel* parcel) {
163 FORWARD(AParcel_getDataSize)(parcel);
164 }
165
166 binder_status_t AParcel_writeInt32(AParcel* parcel, int32_t value) {
167 FORWARD(AParcel_writeInt32)(parcel, value);
168 }
169
170 binder_status_t AParcel_writeInt64(AParcel* parcel, int64_t value) {
171 FORWARD(AParcel_writeInt64)(parcel, value);
172 }
173
174 binder_status_t AParcel_writeStrongBinder(AParcel* parcel, AIBinder* binder) {
175 FORWARD(AParcel_writeStrongBinder)(parcel, binder);
176 }
177
178 binder_status_t AParcel_writeString(AParcel* parcel, const char* string,
179 int32_t length) {
180 FORWARD(AParcel_writeString)(parcel, string, length);
181 }
182
183 binder_status_t AParcel_readInt32(const AParcel* parcel, int32_t* value) {
184 FORWARD(AParcel_readInt32)(parcel, value);
185 }
186
187 binder_status_t AParcel_readInt64(const AParcel* parcel, int64_t* value) {
188 FORWARD(AParcel_readInt64)(parcel, value);
189 }
190
191 binder_status_t AParcel_readString(const AParcel* parcel, void* stringData,
192 AParcel_stringAllocator allocator) {
193 FORWARD(AParcel_readString)(parcel, stringData, allocator);
194 }
195
196 binder_status_t AParcel_readStrongBinder(const AParcel* parcel,
197 AIBinder** binder) {
198 FORWARD(AParcel_readStrongBinder)(parcel, binder);
199 }
200
201 binder_status_t AParcel_writeByteArray(AParcel* parcel, const int8_t* arrayData,
202 int32_t length) {
203 FORWARD(AParcel_writeByteArray)(parcel, arrayData, length);
204 }
205
206 binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
207 FORWARD(AIBinder_prepareTransaction)(binder, in);
208 }
209
210 jobject AIBinder_toJavaBinder(JNIEnv* env, AIBinder* binder) {
211 SetJvm(env);
212 FORWARD(AIBinder_toJavaBinder)(env, binder);
213 }
214
215 } // namespace ndk_util
216 } // namespace grpc_binder
217
218 #endif
219 #endif