Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp
41162 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, 2019, Google and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324#include <stdint.h>25#include <stdlib.h>26#include <string.h>2728#include "ExceptionCheckingJniEnv.hpp"29#include "nsk_tools.h"3031namespace {3233static const char* get_dirname(const char* fullname) {34const char* p;35const char* base = fullname;;3637if (fullname == NULL) {38return NULL;39}4041for (p = fullname; *p != '\0'; p++) {42if (*p == '/' || *p == '\\') {43base = p + 1;44}45}46return base;47}4849template<class T = void*>50class JNIVerifier {51public:52JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_message,53int line, const char* file)54: _env(env), _base_message(base_message), _error_message(NULL),55_line(line), _file(get_dirname(file)) {56}5758// Until C++11 is supported, we have to write multiple template constructors.59template <typename U>60JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_message,61U parameter,62int line, const char* file)63: _env(env), _base_message(base_message), _error_message(NULL),64_line(line), _file(get_dirname(file)) {65PrintPreCall(parameter);66}6768template <typename U, typename V>69JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_message,70U parameter1,71V parameter2,72int line, const char* file)73: _env(env), _base_message(base_message), _error_message(NULL),74_line(line), _file(get_dirname(file)) {75PrintPreCall(parameter1, parameter2);76}7778template <typename U, typename V, typename W>79JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_message,80U parameter1, V parameter2, W parameter3,81int line, const char* file)82: _env(env), _base_message(base_message), _error_message(NULL),83_line(line), _file(get_dirname(file)) {84PrintPreCall(parameter1, parameter2, parameter3);85}8687~JNIVerifier() {88PrintPostCall();8990JNIEnv* jni_env = _env->GetJNIEnv();91if (jni_env->ExceptionCheck() && !_error_message) {92_error_message = "internal error";93}9495if (_error_message != NULL) {96GenerateErrorMessage();97}98}99100int DecimalToAsciiRec(char *str, int line) {101if (line == 0) {102return 0;103}104105int remainder = line % 10;106long quotient = line / 10;107108int pos = DecimalToAsciiRec(str, quotient);109str[pos] = '0' + remainder;110return pos + 1;111}112113// Implementing a simple version of sprintf for "%d"...114void DecimalToAscii(char *str, int line) {115if (line == 0) {116str[0] = '0';117str[1] = '\0';118return;119}120121// Special case for INT32_MIN because otherwise the *1 below will overflow122// and it won't work. Let us just be simple here due to this being for123// tests.124if (line == INT32_MIN) {125strcat(str, "-2147483648");126return;127}128129if (line < 0) {130*str = '-';131line *= -1;132str++;133}134135str[DecimalToAsciiRec(str, line)] = '\0';136}137138void GenerateErrorMessage() {139// This is error prone, but:140// - Seems like we cannot use std::string (due to windows/solaris not141// building when used, seemingly due to exception libraries not linking).142// - Seems like we cannot use sprintf due to VS2013 (JDK-8213622).143//144// We are aiming to do:145// snprintf(full_message, len, "JNI method %s : %s from %s : %d", _base_message, _error_message,146// _file, _line);147// but will use strlen + memcpy instead.148const char* pre_message = "JNI method ";149const char* between_msg = " : ";150const char* from_msg = " from ";151152const char* file_name = _file ? _file : "Unknown File";153const char* strs[] = {154pre_message,155_base_message,156between_msg,157_error_message,158from_msg,159file_name,160between_msg,161};162163size_t msg_number = sizeof(strs) / sizeof(strs[0]);164size_t len = 0;165for (size_t i = 0; i < msg_number; i++) {166len += strlen(strs[i]);167}168169// 32-bit signed means 11 characters due to the '-'.170const int MAX_INTEGER_DIGITS = 11;171// Add for the line number and 1 for the '\0'.172len += MAX_INTEGER_DIGITS + 1;173174char* full_message = (char*) malloc(len);175if (full_message == NULL) {176_env->HandleError(_error_message);177return;178}179180// Now we construct the string using strcat to not use sprintf/std::string181// instead of:182// snprintf(full_message, len, "JNI method %s : %s from %s:%d", _base_message,183// _error_message, _file, _line);184full_message[0] = '\0';185for (size_t i = 0; i < msg_number; i++) {186strcat(full_message, strs[i]);187}188189// Add line number to end of the string.190DecimalToAscii(full_message + strlen(full_message), _line);191192if (strlen(full_message) >= len) {193_env->GetJNIEnv()->FatalError("Final length of message is not what was expected");194}195196_env->HandleError(full_message);197free(full_message);198}199200T ResultNotNull(T ptr) {201if (ptr == NULL) {202_error_message = "Return is NULL";203}204return ptr;205}206207T ResultIsZero(T value) {208if (value != 0) {209_error_message = "Return is not zero";210}211return value;212}213214void PrintPreCallHeader() {215if (!nsk_getVerboseMode()) {216return;217}218219fprintf(stdout, ">> Calling JNI method %s from %s:%d\n",220_base_message, _file, _line);221fprintf(stdout, ">> Calling with these parameter(s):\n");222}223224// Until we can actually link with C++ more uniformely across architectures,225// we have to do this...226template<class U>227void PrintParameter(U* ptr) {228fprintf(stdout, "\t%p\n", ptr);229}230231void PrintParameter(int value) {232fprintf(stdout, "\t%d\n", value);233}234235// Until C++11 is supported, we have to write multiple PrintPreCall.236template<class U>237void PrintPreCall(U first_parameter) {238if (!nsk_getVerboseMode()) {239return;240}241242PrintPreCallHeader();243PrintParameter(first_parameter);244}245246template<class U, class V>247void PrintPreCall(U parameter1, V parameter2) {248if (!nsk_getVerboseMode()) {249return;250}251252PrintPreCallHeader();253PrintParameter(parameter1);254PrintParameter(parameter2);255}256257template<class U, class V, class W>258void PrintPreCall(U parameter1, V parameter2, W parameter3) {259if (!nsk_getVerboseMode()) {260return;261}262263PrintPreCallHeader();264PrintParameter(parameter1);265PrintParameter(parameter2);266PrintParameter(parameter3);267}268269void PrintPostCall() {270if (!nsk_getVerboseMode()) {271return;272}273274fprintf(stderr, "<< Called JNI method %s from %s:%d\n",275_base_message, _file, _line);276}277278private:279ExceptionCheckingJniEnv* _env;280const char* const _base_message;281const char* _error_message;282int _line;283const char* const _file;284};285286}287288jclass ExceptionCheckingJniEnv::FindClass(const char *class_name,289int line, const char* file_name) {290JNIVerifier<jclass> marker(this, "FindClass", class_name, line, file_name);291return marker.ResultNotNull(_jni_env->FindClass(class_name));292}293294jint ExceptionCheckingJniEnv::RegisterNatives(jclass clazz,295const JNINativeMethod *methods,296jint nMethods,297int line,298const char* file_name) {299JNIVerifier<jint> marker(this, "RegisterNatives", methods, nMethods, line, file_name);300return marker.ResultIsZero(_jni_env->RegisterNatives(clazz, methods, nMethods));301}302303jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj, int line,304const char* file_name) {305JNIVerifier<jclass> marker(this, "GetObjectClass", obj, line, file_name);306return marker.ResultNotNull(_jni_env->GetObjectClass(obj));307}308309jfieldID ExceptionCheckingJniEnv::GetStaticFieldID(jclass klass, const char *name,310const char* type,311int line, const char* file_name) {312JNIVerifier<jfieldID> marker(this, "GetStaticFieldID", klass, name, type,313line, file_name);314return marker.ResultNotNull(_jni_env->GetStaticFieldID(klass, name, type));315}316317jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name,318const char* type,319int line, const char* file_name) {320JNIVerifier<jfieldID> marker(this, "GetFieldID", klass, name, type, line, file_name);321return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type));322}323324jobject ExceptionCheckingJniEnv::GetStaticObjectField(jclass klass, jfieldID field,325int line, const char* file_name) {326JNIVerifier<jobject> marker(this, "GetStaticObjectField", klass, field,327line, file_name);328return marker.ResultNotNull(_jni_env->GetStaticObjectField(klass, field));329}330331jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field,332int line, const char* file_name) {333JNIVerifier<jobject> marker(this, "GetObjectField", obj, field, line, file_name);334return marker.ResultNotNull(_jni_env->GetObjectField(obj, field));335}336337void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value,338int line, const char* file_name) {339JNIVerifier<> marker(this, "SetObjectField", obj, field, value, line, file_name);340_jni_env->SetObjectField(obj, field, value);341}342343jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj, int line, const char* file_name) {344JNIVerifier<jobject> marker(this, "NewGlobalRef", obj, line, file_name);345return marker.ResultNotNull(_jni_env->NewGlobalRef(obj));346}347348void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj, int line, const char* file_name) {349JNIVerifier<> marker(this, "DeleteGlobalRef", obj, line, file_name);350_jni_env->DeleteGlobalRef(obj);351}352353jobject ExceptionCheckingJniEnv::NewLocalRef(jobject obj, int line, const char* file_name) {354JNIVerifier<jobject> marker(this, "NewLocalRef", obj, line, file_name);355return marker.ResultNotNull(_jni_env->NewLocalRef(obj));356}357358void ExceptionCheckingJniEnv::DeleteLocalRef(jobject obj, int line, const char* file_name) {359JNIVerifier<> marker(this, "DeleteLocalRef", obj, line, file_name);360_jni_env->DeleteLocalRef(obj);361}362363jweak ExceptionCheckingJniEnv::NewWeakGlobalRef(jobject obj, int line, const char* file_name) {364JNIVerifier<jweak> marker(this, "NewWeakGlobalRef", obj, line, file_name);365return marker.ResultNotNull(_jni_env->NewWeakGlobalRef(obj));366}367368void ExceptionCheckingJniEnv::DeleteWeakGlobalRef(jweak weak_ref, int line, const char* file_name) {369JNIVerifier<> marker(this, "DeleteWeakGlobalRef", weak_ref, line, file_name);370_jni_env->DeleteWeakGlobalRef(weak_ref);371}372373jsize ExceptionCheckingJniEnv::GetArrayLength(jarray array, int line, const char* file_name) {374JNIVerifier<> marker(this, "GetArrayLength", array, line, file_name);375return _jni_env->GetArrayLength(array);376}377378jsize ExceptionCheckingJniEnv::GetStringLength(jstring str, int line, const char* file_name) {379JNIVerifier<> marker(this, "GetStringLength", str, line, file_name);380return _jni_env->GetStringLength(str);381}382383void* ExceptionCheckingJniEnv::GetPrimitiveArrayCritical(jarray array, jboolean* is_copy,384int line, const char* file_name) {385JNIVerifier<> marker(this, "GetPrimitiveArrayCritical", array, is_copy, line, file_name);386return marker.ResultNotNull(_jni_env->GetPrimitiveArrayCritical(array, is_copy));387}388389void ExceptionCheckingJniEnv::ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode,390int line, const char* file_name) {391JNIVerifier<> marker(this, "ReleasePrimitiveArrayCritical", array, carray, mode,392line, file_name);393_jni_env->ReleasePrimitiveArrayCritical(array, carray, mode);394}395396const jchar* ExceptionCheckingJniEnv::GetStringCritical(jstring str, jboolean* is_copy,397int line, const char* file_name) {398JNIVerifier<const jchar*> marker(this, "GetPrimitiveArrayCritical", str, is_copy,399line, file_name);400return marker.ResultNotNull(_jni_env->GetStringCritical(str, is_copy));401}402403void ExceptionCheckingJniEnv::ReleaseStringCritical(jstring str, const jchar* carray,404int line, const char* file_name) {405JNIVerifier<> marker(this, "ReleaseStringCritical", str, carray, line, file_name);406_jni_env->ReleaseStringCritical(str, carray);407}408409jbyte* ExceptionCheckingJniEnv::GetByteArrayElements(jbyteArray array, jboolean* is_copy,410int line, const char* file_name) {411JNIVerifier<jbyte*> marker(this, "GetByteArrayElements", array, is_copy, line, file_name);412return marker.ResultNotNull(_jni_env->GetByteArrayElements(array, is_copy));413}414415void ExceptionCheckingJniEnv::ReleaseByteArrayElements(jbyteArray array, jbyte* byte_array, jint mode,416int line, const char* file_name) {417JNIVerifier<> marker(this, "ReleaseByteArrayElements", array, byte_array, mode,418line, file_name);419_jni_env->ReleaseByteArrayElements(array, byte_array, mode);420}421422jmethodID ExceptionCheckingJniEnv::GetMethodID(jclass klass, const char* name, const char* sig,423int line, const char* file_name) {424JNIVerifier<jmethodID> marker(this, "GetMethodID", klass, name, sig, line, file_name);425return marker.ResultNotNull(_jni_env->GetMethodID(klass, name, sig));426}427428jmethodID ExceptionCheckingJniEnv::GetStaticMethodID(jclass klass, const char* name, const char* sig,429int line, const char* file_name) {430JNIVerifier<jmethodID> marker(this, "GetStaticMethodID", klass, name, sig, line, file_name);431return marker.ResultNotNull(_jni_env->GetStaticMethodID(klass, name, sig));432}433434jboolean ExceptionCheckingJniEnv::IsSameObject(jobject ref1, jobject ref2, int line, const char* file_name) {435JNIVerifier<> marker(this, "IsSameObject", ref1, ref2, line, file_name);436return _jni_env->IsSameObject(ref1, ref2);437}438439jobject ExceptionCheckingJniEnv::NewObject(jclass klass, jmethodID methodID,440int line, const char* file_name, ...) {441// In the case of NewObject, we miss the extra arguments passed to NewObject sadly.442JNIVerifier<jobject> marker(this, "NewObject", klass, methodID, line, file_name);443444va_list args;445va_start(args, file_name);446jobject result = marker.ResultNotNull(_jni_env->NewObjectV(klass, methodID, args));447va_end(args);448return result;449}450451jobject ExceptionCheckingJniEnv::CallObjectMethod(jobject obj, jmethodID methodID, int line,452const char* file_name, ...) {453JNIVerifier<> marker(this, "CallObjectMethod", obj, methodID, line, file_name);454455va_list args;456va_start(args, file_name);457jobject result = _jni_env->CallObjectMethodV(obj, methodID, args);458va_end(args);459return result;460}461462void ExceptionCheckingJniEnv::CallVoidMethod(jobject obj, jmethodID methodID, int line,463const char* file_name, ...) {464JNIVerifier<> marker(this, "CallVoidMethod", obj, methodID, line, file_name);465466va_list args;467va_start(args, file_name);468_jni_env->CallVoidMethodV(obj, methodID, args);469va_end(args);470}471472473