blob: 08d952791ef6a8f8b71198e9f116b02eac6387da [file] [log] [blame]
Andreas Huberdab5fc62016-08-15 09:25:02 -07001/*
2 * Copyright (C) 2016 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//#define LOG_NDEBUG 0
18#define LOG_TAG "android_os_HwBinder"
19#include <android-base/logging.h>
20
21#include "android_os_HwBinder.h"
22
23#include "android_os_HwParcel.h"
24#include "android_os_HwRemoteBinder.h"
25
Steven Morelandafe95a92017-05-23 12:45:16 -070026#include <cstring>
27
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/JNIHelp.h>
Steven Morelanda8136862016-10-24 13:44:41 -070029#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hongbae1b552016-12-01 10:25:05 -080030#include <android/hidl/base/1.0/IBase.h>
Yifan Hong90b6a372017-01-09 17:58:33 -080031#include <android/hidl/base/1.0/BpHwBase.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070032#include <android_runtime/AndroidRuntime.h>
Steven Morelanda8136862016-10-24 13:44:41 -070033#include <hidl/ServiceManagement.h>
Martijn Coenenaa2c32f2016-09-01 01:37:05 +020034#include <hidl/Status.h>
Martijn Coenen12987112016-12-08 10:48:19 +010035#include <hidl/HidlTransportSupport.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070036#include <hwbinder/ProcessState.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070037#include <nativehelper/ScopedLocalRef.h>
Andreas Gampe63c01932017-10-25 13:03:24 -070038#include <nativehelper/ScopedUtfChars.h>
Yifan Hong0382be22017-02-06 12:43:08 -080039#include <vintf/parse_string.h>
Sundong Ahn28cc6e82017-07-13 16:34:01 +090040#include <utils/misc.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070041
42#include "core_jni_helpers.h"
43
44using android::AndroidRuntime;
Steven Morelandf3c53492016-11-03 15:17:04 -070045using android::hardware::hidl_vec;
46using android::hardware::hidl_string;
Timur Iskhakovb6a62832017-07-10 10:08:38 -070047using android::hardware::IPCThreadState;
48using android::hardware::ProcessState;
Steven Morelandc0631d02017-01-04 10:37:59 -080049template<typename T>
50using Return = android::hardware::Return<T>;
Andreas Huberdab5fc62016-08-15 09:25:02 -070051
52#define PACKAGE_PATH "android/os"
53#define CLASS_NAME "HwBinder"
54#define CLASS_PATH PACKAGE_PATH "/" CLASS_NAME
55
56namespace android {
57
Steven Morelande62b1f32016-12-20 15:55:48 -080058static jclass gErrorClass;
59
Andreas Huberdab5fc62016-08-15 09:25:02 -070060static struct fields_t {
61 jfieldID contextID;
62 jmethodID onTransactID;
Andreas Huberdab5fc62016-08-15 09:25:02 -070063} gFields;
64
Andreas Huber56bdb8a2017-06-16 14:56:12 -070065struct JHwBinderHolder : public RefBase {
66 JHwBinderHolder() {}
67
68 sp<JHwBinder> get(JNIEnv *env, jobject obj) {
69 Mutex::Autolock autoLock(mLock);
70
71 sp<JHwBinder> binder = mBinder.promote();
72
73 if (binder == NULL) {
74 binder = new JHwBinder(env, obj);
75 mBinder = binder;
76 }
77
78 return binder;
79 }
80
81private:
82 Mutex mLock;
83 wp<JHwBinder> mBinder;
84
85 DISALLOW_COPY_AND_ASSIGN(JHwBinderHolder);
86};
87
Andreas Huberdab5fc62016-08-15 09:25:02 -070088// static
89void JHwBinder::InitClass(JNIEnv *env) {
90 ScopedLocalRef<jclass> clazz(
91 env, FindClassOrDie(env, CLASS_PATH));
92
93 gFields.contextID =
94 GetFieldIDOrDie(env, clazz.get(), "mNativeContext", "J");
95
96 gFields.onTransactID =
97 GetMethodIDOrDie(
98 env,
99 clazz.get(),
100 "onTransact",
101 "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V");
102}
103
104// static
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700105sp<JHwBinderHolder> JHwBinder::SetNativeContext(
106 JNIEnv *env, jobject thiz, const sp<JHwBinderHolder> &context) {
107 sp<JHwBinderHolder> old =
108 (JHwBinderHolder *)env->GetLongField(thiz, gFields.contextID);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700109
110 if (context != NULL) {
111 context->incStrong(NULL /* id */);
112 }
113
114 if (old != NULL) {
115 old->decStrong(NULL /* id */);
116 }
117
118 env->SetLongField(thiz, gFields.contextID, (long)context.get());
119
120 return old;
121}
122
123// static
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700124sp<JHwBinder> JHwBinder::GetNativeBinder(
Andreas Huberdab5fc62016-08-15 09:25:02 -0700125 JNIEnv *env, jobject thiz) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700126 JHwBinderHolder *holder =
127 reinterpret_cast<JHwBinderHolder *>(
128 env->GetLongField(thiz, gFields.contextID));
129
130 return holder->get(env, thiz);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700131}
132
133JHwBinder::JHwBinder(JNIEnv *env, jobject thiz) {
134 jclass clazz = env->GetObjectClass(thiz);
135 CHECK(clazz != NULL);
136
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700137 mObject = env->NewGlobalRef(thiz);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700138}
139
140JHwBinder::~JHwBinder() {
141 JNIEnv *env = AndroidRuntime::getJNIEnv();
142
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700143 env->DeleteGlobalRef(mObject);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700144 mObject = NULL;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700145}
146
147status_t JHwBinder::onTransact(
148 uint32_t code,
149 const hardware::Parcel &data,
150 hardware::Parcel *reply,
151 uint32_t flags,
152 TransactCallback callback) {
153 JNIEnv *env = AndroidRuntime::getJNIEnv();
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100154 bool isOneway = (flags & TF_ONE_WAY) != 0;
155 ScopedLocalRef<jobject> replyObj(env, nullptr);
156 sp<JHwParcel> replyContext = nullptr;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700157
158 ScopedLocalRef<jobject> requestObj(env, JHwParcel::NewObject(env));
159 JHwParcel::GetNativeContext(env, requestObj.get())->setParcel(
160 const_cast<hardware::Parcel *>(&data), false /* assumeOwnership */);
161
Andreas Huberdab5fc62016-08-15 09:25:02 -0700162
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100163 if (!isOneway) {
164 replyObj.reset(JHwParcel::NewObject(env));
Andreas Huberdab5fc62016-08-15 09:25:02 -0700165
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100166 replyContext = JHwParcel::GetNativeContext(env, replyObj.get());
167
168 replyContext->setParcel(reply, false /* assumeOwnership */);
169 replyContext->setTransactCallback(callback);
170 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700171
172 env->CallVoidMethod(
173 mObject,
174 gFields.onTransactID,
175 code,
176 requestObj.get(),
177 replyObj.get(),
178 flags);
179
Steven Morelande62b1f32016-12-20 15:55:48 -0800180 if (env->ExceptionCheck()) {
181 jthrowable excep = env->ExceptionOccurred();
182 env->ExceptionDescribe();
Aurimas Liutikasf0aa0f12017-02-10 14:52:59 -0800183 env->ExceptionClear();
Steven Morelande62b1f32016-12-20 15:55:48 -0800184
Aurimas Liutikasf0aa0f12017-02-10 14:52:59 -0800185 // It is illegal to call IsInstanceOf if there is a pending exception.
186 // Attempting to do so results in a JniAbort which crashes the entire process.
Steven Morelande62b1f32016-12-20 15:55:48 -0800187 if (env->IsInstanceOf(excep, gErrorClass)) {
188 /* It's an error */
189 LOG(ERROR) << "Forcefully exiting";
190 exit(1);
191 } else {
Steven Morelande62b1f32016-12-20 15:55:48 -0800192 LOG(ERROR) << "Uncaught exception!";
193 }
194
195 env->DeleteLocalRef(excep);
196 }
197
Andreas Huberdab5fc62016-08-15 09:25:02 -0700198 status_t err = OK;
199
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100200 if (!isOneway) {
201 if (!replyContext->wasSent()) {
202 // The implementation never finished the transaction.
203 err = UNKNOWN_ERROR; // XXX special error code instead?
Andreas Huberdab5fc62016-08-15 09:25:02 -0700204
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100205 reply->setDataPosition(0 /* pos */);
206 }
207
208 // Release all temporary storage now that scatter-gather data
209 // has been consolidated, either by calling the TransactCallback,
210 // if wasSent() == true or clearing the reply parcel (setDataOffset above).
211 replyContext->getStorage()->release(env);
212
213 // We cannot permanently pass ownership of "data" and "reply" over to their
214 // Java object wrappers (we don't own them ourselves).
215 replyContext->setParcel(
216 NULL /* parcel */, false /* assumeOwnership */);
217
Andreas Huberdab5fc62016-08-15 09:25:02 -0700218 }
219
Andreas Huberdab5fc62016-08-15 09:25:02 -0700220 JHwParcel::GetNativeContext(env, requestObj.get())->setParcel(
221 NULL /* parcel */, false /* assumeOwnership */);
222
Andreas Huberdab5fc62016-08-15 09:25:02 -0700223 return err;
224}
225
226} // namespace android
227
228////////////////////////////////////////////////////////////////////////////////
229
230using namespace android;
231
232static void releaseNativeContext(void *nativeContext) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700233 sp<JHwBinderHolder> context = static_cast<JHwBinderHolder *>(nativeContext);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700234
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700235 if (context != NULL) {
236 context->decStrong(NULL /* id */);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700237 }
238}
239
240static jlong JHwBinder_native_init(JNIEnv *env) {
241 JHwBinder::InitClass(env);
242
243 return reinterpret_cast<jlong>(&releaseNativeContext);
244}
245
246static void JHwBinder_native_setup(JNIEnv *env, jobject thiz) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700247 sp<JHwBinderHolder> context = new JHwBinderHolder;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700248 JHwBinder::SetNativeContext(env, thiz, context);
249}
250
251static void JHwBinder_native_transact(
252 JNIEnv * /* env */,
253 jobject /* thiz */,
254 jint /* code */,
255 jobject /* requestObj */,
256 jobject /* replyObj */,
257 jint /* flags */) {
258 CHECK(!"Should not be here");
259}
260
261static void JHwBinder_native_registerService(
Andreas Huber35eb7992016-10-25 13:29:30 -0700262 JNIEnv *env,
263 jobject thiz,
Steven Morelandf3c53492016-11-03 15:17:04 -0700264 jstring serviceNameObj) {
Andreas Gampe63c01932017-10-25 13:03:24 -0700265 ScopedUtfChars str(env, serviceNameObj);
266 if (str.c_str() == nullptr) {
267 return; // NPE will be pending.
Andreas Huberdab5fc62016-08-15 09:25:02 -0700268 }
269
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700270 sp<hardware::IBinder> binder = JHwBinder::GetNativeBinder(env, thiz);
Steven Moreland520d12c2016-12-15 15:50:17 -0800271
Martijn Coenen12987112016-12-08 10:48:19 +0100272 /* TODO(b/33440494) this is not right */
Yifan Hong90b6a372017-01-09 17:58:33 -0800273 sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder);
Steven Moreland520d12c2016-12-15 15:50:17 -0800274
275 auto manager = hardware::defaultServiceManager();
276
277 if (manager == nullptr) {
278 LOG(ERROR) << "Could not get hwservicemanager.";
Steven Morelandc0631d02017-01-04 10:37:59 -0800279 signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */);
Steven Moreland520d12c2016-12-15 15:50:17 -0800280 return;
281 }
282
Andreas Gampe63c01932017-10-25 13:03:24 -0700283 Return<bool> ret = manager->add(str.c_str(), base);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700284
Steven Morelandc0631d02017-01-04 10:37:59 -0800285 bool ok = ret.isOk() && ret;
286
Steven Morelanda8136862016-10-24 13:44:41 -0700287 if (ok) {
Andreas Gampe63c01932017-10-25 13:03:24 -0700288 LOG(INFO) << "HwBinder: Starting thread pool for " << str.c_str();
Andreas Huberdab5fc62016-08-15 09:25:02 -0700289 ::android::hardware::ProcessState::self()->startThreadPool();
290 }
291
Steven Morelandc0631d02017-01-04 10:37:59 -0800292 signalExceptionForError(env, (ok ? OK : UNKNOWN_ERROR), true /* canThrowRemoteException */);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700293}
294
295static jobject JHwBinder_native_getService(
Andreas Huber35eb7992016-10-25 13:29:30 -0700296 JNIEnv *env,
297 jclass /* clazzObj */,
Steven Morelandf3c53492016-11-03 15:17:04 -0700298 jstring ifaceNameObj,
Steven Morelandec5eb7e2017-10-25 04:50:46 +0000299 jstring serviceNameObj,
300 jboolean retry) {
Steven Morelandf3c53492016-11-03 15:17:04 -0700301
Steven Morelandf8202e42017-04-06 09:28:32 -0700302 using ::android::hidl::base::V1_0::IBase;
Steven Morelandc4bfdef2017-10-18 23:19:43 -0700303 using ::android::hardware::details::getRawServiceInternal;
Yifan Hong0382be22017-02-06 12:43:08 -0800304
Andreas Gampe63c01932017-10-25 13:03:24 -0700305 std::string ifaceName;
306 {
307 ScopedUtfChars str(env, ifaceNameObj);
308 if (str.c_str() == nullptr) {
309 return nullptr; // NPE will be pending.
310 }
311 ifaceName = str.c_str();
Andreas Huberdab5fc62016-08-15 09:25:02 -0700312 }
313
Andreas Gampe63c01932017-10-25 13:03:24 -0700314 std::string serviceName;
315 {
316 ScopedUtfChars str(env, serviceNameObj);
317 if (str.c_str() == nullptr) {
318 return nullptr; // NPE will be pending.
319 }
320 serviceName = str.c_str();
Andreas Huber35eb7992016-10-25 13:29:30 -0700321 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700322
Steven Morelandec5eb7e2017-10-25 04:50:46 +0000323 sp<IBase> ret = getRawServiceInternal(ifaceName, serviceName, retry /* retry */, false /* getStub */);
Steven Moreland72be1562017-07-21 19:29:54 +0000324 sp<hardware::IBinder> service = hardware::toBinder<hidl::base::V1_0::IBase>(ret);
Steven Moreland2f379ca2017-02-01 09:58:00 -0800325
Andreas Huberdab5fc62016-08-15 09:25:02 -0700326 if (service == NULL) {
327 signalExceptionForError(env, NAME_NOT_FOUND);
328 return NULL;
329 }
330
Andreas Gampe63c01932017-10-25 13:03:24 -0700331 LOG(INFO) << "HwBinder: Starting thread pool for " << serviceName << "::" << ifaceName;
Andreas Huber781c0832016-10-05 11:10:26 -0700332 ::android::hardware::ProcessState::self()->startThreadPool();
333
Andreas Huberdab5fc62016-08-15 09:25:02 -0700334 return JHwRemoteBinder::NewObject(env, service);
335}
336
Yifan Hong029b08c2017-10-25 13:19:22 -0700337void JHwBinder_native_configureRpcThreadpool(JNIEnv *, jclass,
338 jlong maxThreads, jboolean callerWillJoin) {
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700339 CHECK(maxThreads > 0);
340 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
341}
342
343void JHwBinder_native_joinRpcThreadpool() {
344 IPCThreadState::self()->joinThreadPool();
345}
346
Yifan Hong029b08c2017-10-25 13:19:22 -0700347static void JHwBinder_report_sysprop_change(JNIEnv * /*env*/, jclass /*clazz*/)
Sundong Ahn28cc6e82017-07-13 16:34:01 +0900348{
349 report_sysprop_change();
350}
351
Andreas Huberdab5fc62016-08-15 09:25:02 -0700352static JNINativeMethod gMethods[] = {
353 { "native_init", "()J", (void *)JHwBinder_native_init },
354 { "native_setup", "()V", (void *)JHwBinder_native_setup },
355
356 { "transact",
357 "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V",
358 (void *)JHwBinder_native_transact },
359
Martijn Coenen85d12da2017-03-06 13:07:53 +0100360 { "registerService", "(Ljava/lang/String;)V",
Andreas Huberdab5fc62016-08-15 09:25:02 -0700361 (void *)JHwBinder_native_registerService },
362
Steven Morelandec5eb7e2017-10-25 04:50:46 +0000363 { "getService", "(Ljava/lang/String;Ljava/lang/String;Z)L" PACKAGE_PATH "/IHwBinder;",
Andreas Huberdab5fc62016-08-15 09:25:02 -0700364 (void *)JHwBinder_native_getService },
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700365
366 { "configureRpcThreadpool", "(JZ)V",
367 (void *)JHwBinder_native_configureRpcThreadpool },
368
369 { "joinRpcThreadpool", "()V",
370 (void *)JHwBinder_native_joinRpcThreadpool },
Sundong Ahn28cc6e82017-07-13 16:34:01 +0900371
372 { "native_report_sysprop_change", "()V",
373 (void *)JHwBinder_report_sysprop_change },
Andreas Huberdab5fc62016-08-15 09:25:02 -0700374};
375
376namespace android {
377
378int register_android_os_HwBinder(JNIEnv *env) {
Steven Morelande62b1f32016-12-20 15:55:48 -0800379 jclass errorClass = FindClassOrDie(env, "java/lang/Error");
380 gErrorClass = MakeGlobalRefOrDie(env, errorClass);
381
Andreas Huberdab5fc62016-08-15 09:25:02 -0700382 return RegisterMethodsOrDie(env, CLASS_PATH, gMethods, NELEM(gMethods));
383}
384
385} // namespace android