blob: 81063204447f784e1fa2b3d21cde8661e473c4c1 [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>
Yifan Hong0382be22017-02-06 12:43:08 -080038#include <vintf/parse_string.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070039
40#include "core_jni_helpers.h"
41
42using android::AndroidRuntime;
Steven Morelandf3c53492016-11-03 15:17:04 -070043using android::hardware::hidl_vec;
44using android::hardware::hidl_string;
Timur Iskhakovb6a62832017-07-10 10:08:38 -070045using android::hardware::IPCThreadState;
46using android::hardware::ProcessState;
Steven Morelandc0631d02017-01-04 10:37:59 -080047template<typename T>
48using Return = android::hardware::Return<T>;
Andreas Huberdab5fc62016-08-15 09:25:02 -070049
50#define PACKAGE_PATH "android/os"
51#define CLASS_NAME "HwBinder"
52#define CLASS_PATH PACKAGE_PATH "/" CLASS_NAME
53
54namespace android {
55
Steven Morelande62b1f32016-12-20 15:55:48 -080056static jclass gErrorClass;
57
Andreas Huberdab5fc62016-08-15 09:25:02 -070058static struct fields_t {
59 jfieldID contextID;
60 jmethodID onTransactID;
Andreas Huberdab5fc62016-08-15 09:25:02 -070061} gFields;
62
Andreas Huber56bdb8a2017-06-16 14:56:12 -070063struct JHwBinderHolder : public RefBase {
64 JHwBinderHolder() {}
65
66 sp<JHwBinder> get(JNIEnv *env, jobject obj) {
67 Mutex::Autolock autoLock(mLock);
68
69 sp<JHwBinder> binder = mBinder.promote();
70
71 if (binder == NULL) {
72 binder = new JHwBinder(env, obj);
73 mBinder = binder;
74 }
75
76 return binder;
77 }
78
79private:
80 Mutex mLock;
81 wp<JHwBinder> mBinder;
82
83 DISALLOW_COPY_AND_ASSIGN(JHwBinderHolder);
84};
85
Andreas Huberdab5fc62016-08-15 09:25:02 -070086// static
87void JHwBinder::InitClass(JNIEnv *env) {
88 ScopedLocalRef<jclass> clazz(
89 env, FindClassOrDie(env, CLASS_PATH));
90
91 gFields.contextID =
92 GetFieldIDOrDie(env, clazz.get(), "mNativeContext", "J");
93
94 gFields.onTransactID =
95 GetMethodIDOrDie(
96 env,
97 clazz.get(),
98 "onTransact",
99 "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V");
100}
101
102// static
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700103sp<JHwBinderHolder> JHwBinder::SetNativeContext(
104 JNIEnv *env, jobject thiz, const sp<JHwBinderHolder> &context) {
105 sp<JHwBinderHolder> old =
106 (JHwBinderHolder *)env->GetLongField(thiz, gFields.contextID);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700107
108 if (context != NULL) {
109 context->incStrong(NULL /* id */);
110 }
111
112 if (old != NULL) {
113 old->decStrong(NULL /* id */);
114 }
115
116 env->SetLongField(thiz, gFields.contextID, (long)context.get());
117
118 return old;
119}
120
121// static
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700122sp<JHwBinder> JHwBinder::GetNativeBinder(
Andreas Huberdab5fc62016-08-15 09:25:02 -0700123 JNIEnv *env, jobject thiz) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700124 JHwBinderHolder *holder =
125 reinterpret_cast<JHwBinderHolder *>(
126 env->GetLongField(thiz, gFields.contextID));
127
128 return holder->get(env, thiz);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700129}
130
131JHwBinder::JHwBinder(JNIEnv *env, jobject thiz) {
132 jclass clazz = env->GetObjectClass(thiz);
133 CHECK(clazz != NULL);
134
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700135 mObject = env->NewGlobalRef(thiz);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700136}
137
138JHwBinder::~JHwBinder() {
139 JNIEnv *env = AndroidRuntime::getJNIEnv();
140
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700141 env->DeleteGlobalRef(mObject);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700142 mObject = NULL;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700143}
144
145status_t JHwBinder::onTransact(
146 uint32_t code,
147 const hardware::Parcel &data,
148 hardware::Parcel *reply,
149 uint32_t flags,
150 TransactCallback callback) {
151 JNIEnv *env = AndroidRuntime::getJNIEnv();
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100152 bool isOneway = (flags & TF_ONE_WAY) != 0;
153 ScopedLocalRef<jobject> replyObj(env, nullptr);
154 sp<JHwParcel> replyContext = nullptr;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700155
156 ScopedLocalRef<jobject> requestObj(env, JHwParcel::NewObject(env));
157 JHwParcel::GetNativeContext(env, requestObj.get())->setParcel(
158 const_cast<hardware::Parcel *>(&data), false /* assumeOwnership */);
159
Andreas Huberdab5fc62016-08-15 09:25:02 -0700160
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100161 if (!isOneway) {
162 replyObj.reset(JHwParcel::NewObject(env));
Andreas Huberdab5fc62016-08-15 09:25:02 -0700163
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100164 replyContext = JHwParcel::GetNativeContext(env, replyObj.get());
165
166 replyContext->setParcel(reply, false /* assumeOwnership */);
167 replyContext->setTransactCallback(callback);
168 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700169
170 env->CallVoidMethod(
171 mObject,
172 gFields.onTransactID,
173 code,
174 requestObj.get(),
175 replyObj.get(),
176 flags);
177
Steven Morelande62b1f32016-12-20 15:55:48 -0800178 if (env->ExceptionCheck()) {
179 jthrowable excep = env->ExceptionOccurred();
180 env->ExceptionDescribe();
Aurimas Liutikasf0aa0f12017-02-10 14:52:59 -0800181 env->ExceptionClear();
Steven Morelande62b1f32016-12-20 15:55:48 -0800182
Aurimas Liutikasf0aa0f12017-02-10 14:52:59 -0800183 // It is illegal to call IsInstanceOf if there is a pending exception.
184 // Attempting to do so results in a JniAbort which crashes the entire process.
Steven Morelande62b1f32016-12-20 15:55:48 -0800185 if (env->IsInstanceOf(excep, gErrorClass)) {
186 /* It's an error */
187 LOG(ERROR) << "Forcefully exiting";
188 exit(1);
189 } else {
Steven Morelande62b1f32016-12-20 15:55:48 -0800190 LOG(ERROR) << "Uncaught exception!";
191 }
192
193 env->DeleteLocalRef(excep);
194 }
195
Andreas Huberdab5fc62016-08-15 09:25:02 -0700196 status_t err = OK;
197
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100198 if (!isOneway) {
199 if (!replyContext->wasSent()) {
200 // The implementation never finished the transaction.
201 err = UNKNOWN_ERROR; // XXX special error code instead?
Andreas Huberdab5fc62016-08-15 09:25:02 -0700202
Martijn Coenen60bf84a2017-02-08 10:22:28 +0100203 reply->setDataPosition(0 /* pos */);
204 }
205
206 // Release all temporary storage now that scatter-gather data
207 // has been consolidated, either by calling the TransactCallback,
208 // if wasSent() == true or clearing the reply parcel (setDataOffset above).
209 replyContext->getStorage()->release(env);
210
211 // We cannot permanently pass ownership of "data" and "reply" over to their
212 // Java object wrappers (we don't own them ourselves).
213 replyContext->setParcel(
214 NULL /* parcel */, false /* assumeOwnership */);
215
Andreas Huberdab5fc62016-08-15 09:25:02 -0700216 }
217
Andreas Huberdab5fc62016-08-15 09:25:02 -0700218 JHwParcel::GetNativeContext(env, requestObj.get())->setParcel(
219 NULL /* parcel */, false /* assumeOwnership */);
220
Andreas Huberdab5fc62016-08-15 09:25:02 -0700221 return err;
222}
223
224} // namespace android
225
226////////////////////////////////////////////////////////////////////////////////
227
228using namespace android;
229
230static void releaseNativeContext(void *nativeContext) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700231 sp<JHwBinderHolder> context = static_cast<JHwBinderHolder *>(nativeContext);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700232
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700233 if (context != NULL) {
234 context->decStrong(NULL /* id */);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700235 }
236}
237
238static jlong JHwBinder_native_init(JNIEnv *env) {
239 JHwBinder::InitClass(env);
240
241 return reinterpret_cast<jlong>(&releaseNativeContext);
242}
243
244static void JHwBinder_native_setup(JNIEnv *env, jobject thiz) {
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700245 sp<JHwBinderHolder> context = new JHwBinderHolder;
Andreas Huberdab5fc62016-08-15 09:25:02 -0700246 JHwBinder::SetNativeContext(env, thiz, context);
247}
248
249static void JHwBinder_native_transact(
250 JNIEnv * /* env */,
251 jobject /* thiz */,
252 jint /* code */,
253 jobject /* requestObj */,
254 jobject /* replyObj */,
255 jint /* flags */) {
256 CHECK(!"Should not be here");
257}
258
259static void JHwBinder_native_registerService(
Andreas Huber35eb7992016-10-25 13:29:30 -0700260 JNIEnv *env,
261 jobject thiz,
Steven Morelandf3c53492016-11-03 15:17:04 -0700262 jstring serviceNameObj) {
Andreas Huberdab5fc62016-08-15 09:25:02 -0700263 if (serviceNameObj == NULL) {
264 jniThrowException(env, "java/lang/NullPointerException", NULL);
265 return;
266 }
267
Steven Morelandf3c53492016-11-03 15:17:04 -0700268 const char *serviceName = env->GetStringUTFChars(serviceNameObj, NULL);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700269 if (serviceName == NULL) {
270 return; // XXX exception already pending?
271 }
272
Andreas Huber56bdb8a2017-06-16 14:56:12 -0700273 sp<hardware::IBinder> binder = JHwBinder::GetNativeBinder(env, thiz);
Steven Moreland520d12c2016-12-15 15:50:17 -0800274
Martijn Coenen12987112016-12-08 10:48:19 +0100275 /* TODO(b/33440494) this is not right */
Yifan Hong90b6a372017-01-09 17:58:33 -0800276 sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder);
Steven Moreland520d12c2016-12-15 15:50:17 -0800277
278 auto manager = hardware::defaultServiceManager();
279
280 if (manager == nullptr) {
281 LOG(ERROR) << "Could not get hwservicemanager.";
Steven Morelandc0631d02017-01-04 10:37:59 -0800282 signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */);
Steven Moreland520d12c2016-12-15 15:50:17 -0800283 return;
284 }
285
Martijn Coenen85d12da2017-03-06 13:07:53 +0100286 Return<bool> ret = manager->add(serviceName, base);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700287
Steven Morelandf3c53492016-11-03 15:17:04 -0700288 env->ReleaseStringUTFChars(serviceNameObj, serviceName);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700289 serviceName = NULL;
290
Steven Morelandc0631d02017-01-04 10:37:59 -0800291 bool ok = ret.isOk() && ret;
292
Steven Morelanda8136862016-10-24 13:44:41 -0700293 if (ok) {
Andreas Huberdab5fc62016-08-15 09:25:02 -0700294 LOG(INFO) << "Starting thread pool.";
295 ::android::hardware::ProcessState::self()->startThreadPool();
296 }
297
Steven Morelandc0631d02017-01-04 10:37:59 -0800298 signalExceptionForError(env, (ok ? OK : UNKNOWN_ERROR), true /* canThrowRemoteException */);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700299}
300
301static jobject JHwBinder_native_getService(
Andreas Huber35eb7992016-10-25 13:29:30 -0700302 JNIEnv *env,
303 jclass /* clazzObj */,
Steven Morelandf3c53492016-11-03 15:17:04 -0700304 jstring ifaceNameObj,
305 jstring serviceNameObj) {
306
Steven Morelandf8202e42017-04-06 09:28:32 -0700307 using ::android::hidl::base::V1_0::IBase;
308 using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hong0382be22017-02-06 12:43:08 -0800309
Steven Morelandf3c53492016-11-03 15:17:04 -0700310 if (ifaceNameObj == NULL) {
311 jniThrowException(env, "java/lang/NullPointerException", NULL);
312 return NULL;
313 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700314 if (serviceNameObj == NULL) {
315 jniThrowException(env, "java/lang/NullPointerException", NULL);
316 return NULL;
317 }
318
Steven Moreland2f379ca2017-02-01 09:58:00 -0800319 auto manager = hardware::defaultServiceManager();
320
321 if (manager == nullptr) {
322 LOG(ERROR) << "Could not get hwservicemanager.";
323 signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */);
324 return NULL;
325 }
326
Yifan Hong0382be22017-02-06 12:43:08 -0800327 const char *ifaceNameCStr = env->GetStringUTFChars(ifaceNameObj, NULL);
328 if (ifaceNameCStr == NULL) {
Steven Morelandf3c53492016-11-03 15:17:04 -0700329 return NULL; // XXX exception already pending?
Andreas Huber35eb7992016-10-25 13:29:30 -0700330 }
Yifan Hong0382be22017-02-06 12:43:08 -0800331 std::string ifaceName(ifaceNameCStr);
332 env->ReleaseStringUTFChars(ifaceNameObj, ifaceNameCStr);
333 ::android::hardware::hidl_string ifaceNameHStr;
334 ifaceNameHStr.setToExternal(ifaceName.c_str(), ifaceName.size());
335
336 const char *serviceNameCStr = env->GetStringUTFChars(serviceNameObj, NULL);
337 if (serviceNameCStr == NULL) {
Steven Morelandf3c53492016-11-03 15:17:04 -0700338 return NULL; // XXX exception already pending?
Andreas Huberdab5fc62016-08-15 09:25:02 -0700339 }
Yifan Hong0382be22017-02-06 12:43:08 -0800340 std::string serviceName(serviceNameCStr);
341 env->ReleaseStringUTFChars(serviceNameObj, serviceNameCStr);
342 ::android::hardware::hidl_string serviceNameHStr;
343 serviceNameHStr.setToExternal(serviceName.c_str(), serviceName.size());
Andreas Huberdab5fc62016-08-15 09:25:02 -0700344
Steven Moreland2f379ca2017-02-01 09:58:00 -0800345 LOG(INFO) << "Looking for service "
346 << ifaceName
347 << "/"
348 << serviceName;
Steven Moreland520d12c2016-12-15 15:50:17 -0800349
Steven Morelandf8202e42017-04-06 09:28:32 -0700350 Return<IServiceManager::Transport> transportRet =
351 manager->getTransport(ifaceNameHStr, serviceNameHStr);
352
353 if (!transportRet.isOk()) {
354 signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */);
355 return NULL;
356 }
357
358 IServiceManager::Transport transport = transportRet;
359
Steven Morelandafe95a92017-05-23 12:45:16 -0700360#ifdef __ANDROID_TREBLE__
361#ifdef __ANDROID_DEBUGGABLE__
362 const char* testingOverride = std::getenv("TREBLE_TESTING_OVERRIDE");
363 const bool vintfLegacy = (transport == IServiceManager::Transport::EMPTY)
364 && testingOverride && !strcmp(testingOverride, "true");
365#else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__
366 const bool vintfLegacy = false;
367#endif // __ANDROID_DEBUGGABLE__
368#else // not __ANDROID_TREBLE__
369 const bool vintfLegacy = (transport == IServiceManager::Transport::EMPTY);
370#endif // __ANDROID_TREBLE__";
371
372 if (transport != IServiceManager::Transport::HWBINDER && !vintfLegacy) {
Yifan Hong0382be22017-02-06 12:43:08 -0800373 LOG(ERROR) << "service " << ifaceName << " declares transport method "
Steven Morelandf8202e42017-04-06 09:28:32 -0700374 << toString(transport) << " but framework expects hwbinder.";
Steven Moreland4c5ffe42017-06-28 10:21:00 -0700375 signalExceptionForError(env, NAME_NOT_FOUND, true /* canThrowRemoteException */);
Yifan Hong0382be22017-02-06 12:43:08 -0800376 return NULL;
377 }
Steven Morelandc0631d02017-01-04 10:37:59 -0800378
Yifan Hong0382be22017-02-06 12:43:08 -0800379 Return<sp<hidl::base::V1_0::IBase>> ret = manager->get(ifaceNameHStr, serviceNameHStr);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700380
Steven Moreland2f379ca2017-02-01 09:58:00 -0800381 if (!ret.isOk()) {
382 signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */);
383 return NULL;
384 }
385
386 sp<hardware::IBinder> service = hardware::toBinder<
387 hidl::base::V1_0::IBase, hidl::base::V1_0::BpHwBase>(ret);
388
Andreas Huberdab5fc62016-08-15 09:25:02 -0700389 if (service == NULL) {
390 signalExceptionForError(env, NAME_NOT_FOUND);
391 return NULL;
392 }
393
Andreas Huber781c0832016-10-05 11:10:26 -0700394 LOG(INFO) << "Starting thread pool.";
395 ::android::hardware::ProcessState::self()->startThreadPool();
396
Andreas Huberdab5fc62016-08-15 09:25:02 -0700397 return JHwRemoteBinder::NewObject(env, service);
398}
399
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700400void JHwBinder_native_configureRpcThreadpool(jlong maxThreads, jboolean callerWillJoin) {
401 CHECK(maxThreads > 0);
402 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
403}
404
405void JHwBinder_native_joinRpcThreadpool() {
406 IPCThreadState::self()->joinThreadPool();
407}
408
Andreas Huberdab5fc62016-08-15 09:25:02 -0700409static JNINativeMethod gMethods[] = {
410 { "native_init", "()J", (void *)JHwBinder_native_init },
411 { "native_setup", "()V", (void *)JHwBinder_native_setup },
412
413 { "transact",
414 "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V",
415 (void *)JHwBinder_native_transact },
416
Martijn Coenen85d12da2017-03-06 13:07:53 +0100417 { "registerService", "(Ljava/lang/String;)V",
Andreas Huberdab5fc62016-08-15 09:25:02 -0700418 (void *)JHwBinder_native_registerService },
419
Steven Morelandf3c53492016-11-03 15:17:04 -0700420 { "getService", "(Ljava/lang/String;Ljava/lang/String;)L" PACKAGE_PATH "/IHwBinder;",
Andreas Huberdab5fc62016-08-15 09:25:02 -0700421 (void *)JHwBinder_native_getService },
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700422
423 { "configureRpcThreadpool", "(JZ)V",
424 (void *)JHwBinder_native_configureRpcThreadpool },
425
426 { "joinRpcThreadpool", "()V",
427 (void *)JHwBinder_native_joinRpcThreadpool },
Andreas Huberdab5fc62016-08-15 09:25:02 -0700428};
429
430namespace android {
431
432int register_android_os_HwBinder(JNIEnv *env) {
Steven Morelande62b1f32016-12-20 15:55:48 -0800433 jclass errorClass = FindClassOrDie(env, "java/lang/Error");
434 gErrorClass = MakeGlobalRefOrDie(env, errorClass);
435
Andreas Huberdab5fc62016-08-15 09:25:02 -0700436 return RegisterMethodsOrDie(env, CLASS_PATH, gMethods, NELEM(gMethods));
437}
438
439} // namespace android