/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018, Google and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ #include #include #include "ExceptionCheckingJniEnv.hpp" #include "nsk_tools.h" namespace { static const char* get_basename(const char* fullname) { const char* p; const char* base = fullname;; if (fullname == NULL) { return NULL; } for (p = fullname; *p != '\0'; p++) { if (*p == '/' || *p == '\\') { base = p + 1; } } return base; } template class JNIVerifier { public: JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg, int line, const char* file) : _env(env), _base_message(base_msg), _error_message(NULL), _line(line), _file(get_basename(file)) { } // Until C++11 is supported, we have to write multiple template constructors. template JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg, U parameter, int line, const char* file) : _env(env), _base_message(base_msg), _error_message(NULL), _line(line), _file(get_basename(file)) { PrintPreCall(parameter); } template JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg, U parameter1, V parameter2, int line, const char* file) : _env(env), _base_message(base_msg), _error_message(NULL), _line(line), _file(get_basename(file)) { PrintPreCall(parameter1, parameter2); } template JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg, U parameter1, V parameter2, W parameter3, int line, const char* file) : _env(env), _base_message(base_msg), _error_message(NULL), _line(line), _file(get_basename(file)) { PrintPreCall(parameter1, parameter2, parameter3); } ~JNIVerifier() { PrintPostCall(); JNIEnv* jni_env = _env->GetJNIEnv(); if (jni_env->ExceptionCheck()) { _error_message = "internal error"; } if (_error_message != NULL) { GenerateErrorMessage(); } } void GenerateErrorMessage() { int len = snprintf(NULL, 0, "JNI method %s : %s from %s:%d", _base_message, _error_message, _file, _line) + 1; if (len <= 0) { _env->HandleError(_error_message); return; } char* full_message = (char*) malloc(len); if (full_message == NULL) { _env->HandleError(_error_message); return; } snprintf(full_message, len, "JNI method %s : %s from %s:%d", _base_message, _error_message, _file, _line); _env->HandleError(full_message); free(full_message); } T ResultNotNull(T ptr) { if (ptr == NULL) { _error_message = "Return is NULL"; } return ptr; } T ResultIsZero(T value) { if (value != 0) { _error_message = "Return is not zero"; } return value; } void PrintPreCallHeader() { if (!nsk_getVerboseMode()) { return; } std::cout << ">> Calling JNI method " << _base_message << " from " << _file << ":" << _line << std::endl; std::cout << ">> Calling with these parameter(s):" << std::endl; } // Until C++11 is supported, we have to write multiple PrintPreCall. template void PrintPreCall(U first_parameter) { if (!nsk_getVerboseMode()) { return; } PrintPreCallHeader(); std::cout << "\t" << first_parameter << std::endl; } template void PrintPreCall(U parameter1, V parameter2) { if (!nsk_getVerboseMode()) { return; } PrintPreCallHeader(); std::cout << "\t" << parameter1 << std::endl; std::cout << "\t" << parameter2 << std::endl; } template void PrintPreCall(U parameter1, V parameter2, W parameter3) { if (!nsk_getVerboseMode()) { return; } PrintPreCallHeader(); std::cout << "\t" << parameter1 << std::endl; std::cout << "\t" << parameter2 << std::endl; std::cout << "\t" << parameter3 << std::endl; } void PrintPostCall() { if (!nsk_getVerboseMode()) { return; } std::cout << "<< Called JNI method " << _base_message << " from " << _file << ":" << _line << std::endl; } private: ExceptionCheckingJniEnv* _env; const char* const _base_message; const char* _error_message; int _line; const char* const _file; }; } jclass ExceptionCheckingJniEnv::FindClass(const char *class_name, int line, const char* file_name) { JNIVerifier marker(this, "FindClass", class_name, line, file_name); return marker.ResultNotNull(_jni_env->FindClass(class_name)); } jint ExceptionCheckingJniEnv::RegisterNatives(jclass clazz, const JNINativeMethod *methods, jint nMethods, int line, const char* file_name) { JNIVerifier marker(this, "RegisterNatives", methods, nMethods, line, file_name); return marker.ResultIsZero(_jni_env->RegisterNatives(clazz, methods, nMethods)); } jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj, int line, const char* file_name) { JNIVerifier marker(this, "GetObjectClass", obj, line, file_name); return marker.ResultNotNull(_jni_env->GetObjectClass(obj)); } jfieldID ExceptionCheckingJniEnv::GetStaticFieldID(jclass klass, const char *name, const char* type, int line, const char* file_name) { JNIVerifier marker(this, "GetStaticFieldID", klass, name, type, line, file_name); return marker.ResultNotNull(_jni_env->GetStaticFieldID(klass, name, type)); } jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name, const char* type, int line, const char* file_name) { JNIVerifier marker(this, "GetFieldID", klass, name, type, line, file_name); return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type)); } jobject ExceptionCheckingJniEnv::GetStaticObjectField(jclass klass, jfieldID field, int line, const char* file_name) { JNIVerifier marker(this, "GetStaticObjectField", klass, field, line, file_name); return marker.ResultNotNull(_jni_env->GetStaticObjectField(klass, field)); } jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field, int line, const char* file_name) { JNIVerifier marker(this, "GetObjectField", obj, field, line, file_name); return marker.ResultNotNull(_jni_env->GetObjectField(obj, field)); } void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value, int line, const char* file_name) { JNIVerifier<> marker(this, "SetObjectField", obj, field, value, line, file_name); _jni_env->SetObjectField(obj, field, value); } jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj, int line, const char* file_name) { JNIVerifier marker(this, "NewGlobalRef", obj, line, file_name); return marker.ResultNotNull(_jni_env->NewGlobalRef(obj)); } void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj, int line, const char* file_name) { JNIVerifier<> marker(this, "DeleteGlobalRef", obj, line, file_name); _jni_env->DeleteGlobalRef(obj); } jobject ExceptionCheckingJniEnv::NewLocalRef(jobject obj, int line, const char* file_name) { JNIVerifier marker(this, "NewLocalRef", obj, line, file_name); return marker.ResultNotNull(_jni_env->NewLocalRef(obj)); } void ExceptionCheckingJniEnv::DeleteLocalRef(jobject obj, int line, const char* file_name) { JNIVerifier<> marker(this, "DeleteLocalRef", obj, line, file_name); _jni_env->DeleteLocalRef(obj); } jweak ExceptionCheckingJniEnv::NewWeakGlobalRef(jobject obj, int line, const char* file_name) { JNIVerifier marker(this, "NewWeakGlobalRef", obj, line, file_name); return marker.ResultNotNull(_jni_env->NewWeakGlobalRef(obj)); } void ExceptionCheckingJniEnv::DeleteWeakGlobalRef(jweak weak_ref, int line, const char* file_name) { JNIVerifier<> marker(this, "DeleteWeakGlobalRef", weak_ref, line, file_name); _jni_env->DeleteWeakGlobalRef(weak_ref); } jsize ExceptionCheckingJniEnv::GetArrayLength(jarray array, int line, const char* file_name) { JNIVerifier<> marker(this, "GetArrayLength", array, line, file_name); return _jni_env->GetArrayLength(array); } jsize ExceptionCheckingJniEnv::GetStringLength(jstring str, int line, const char* file_name) { JNIVerifier<> marker(this, "GetStringLength", str, line, file_name); return _jni_env->GetStringLength(str); } void* ExceptionCheckingJniEnv::GetPrimitiveArrayCritical(jarray array, jboolean* is_copy, int line, const char* file_name) { JNIVerifier<> marker(this, "GetPrimitiveArrayCritical", array, is_copy, line, file_name); return marker.ResultNotNull(_jni_env->GetPrimitiveArrayCritical(array, is_copy)); } void ExceptionCheckingJniEnv::ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode, int line, const char* file_name) { JNIVerifier<> marker(this, "ReleasePrimitiveArrayCritical", array, carray, mode, line, file_name); _jni_env->ReleasePrimitiveArrayCritical(array, carray, mode); } const jchar* ExceptionCheckingJniEnv::GetStringCritical(jstring str, jboolean* is_copy, int line, const char* file_name) { JNIVerifier marker(this, "GetPrimitiveArrayCritical", str, is_copy, line, file_name); return marker.ResultNotNull(_jni_env->GetStringCritical(str, is_copy)); } void ExceptionCheckingJniEnv::ReleaseStringCritical(jstring str, const jchar* carray, int line, const char* file_name) { JNIVerifier<> marker(this, "ReleaseStringCritical", str, carray, line, file_name); _jni_env->ReleaseStringCritical(str, carray); } jbyte* ExceptionCheckingJniEnv::GetByteArrayElements(jbyteArray array, jboolean* is_copy, int line, const char* file_name) { JNIVerifier marker(this, "GetByteArrayElements", array, is_copy, line, file_name); return marker.ResultNotNull(_jni_env->GetByteArrayElements(array, is_copy)); } void ExceptionCheckingJniEnv::ReleaseByteArrayElements(jbyteArray array, jbyte* byte_array, jint mode, int line, const char* file_name) { JNIVerifier<> marker(this, "ReleaseByteArrayElements", array, byte_array, mode, line, file_name); _jni_env->ReleaseByteArrayElements(array, byte_array, mode); } jmethodID ExceptionCheckingJniEnv::GetMethodID(jclass klass, const char* name, const char* sig, int line, const char* file_name) { JNIVerifier marker(this, "GetMethodID", klass, name, sig, line, file_name); return marker.ResultNotNull(_jni_env->GetMethodID(klass, name, sig)); } jobject ExceptionCheckingJniEnv::NewObject(jclass klass, jmethodID methodID, int line, const char* file_name, ...) { // In the case of NewObject, we miss the extra arguments passed to NewObject sadly. JNIVerifier marker(this, "NewObject", klass, methodID, line, file_name); va_list args; va_start(args, file_name); jobject result = marker.ResultNotNull(_jni_env->NewObjectV(klass, methodID, args)); va_end(args); return result; }