1 /* 2 * Copyright (C) 2018 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 18 #include <cstdio> 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "android-base/logging.h" 24 #include "android-base/stringprintf.h" 25 26 #include "jni.h" 27 #include "jvmti.h" 28 #include "scoped_local_ref.h" 29 #include "scoped_utf_chars.h" 30 31 // Test infrastructure 32 #include "jni_helper.h" 33 #include "jvmti_helper.h" 34 #include "test_env.h" 35 #include "ti_macros.h" 36 37 namespace art { 38 namespace Test1957ErrorExt { 39 40 using GetLastError = jvmtiError(*)(jvmtiEnv* env, char** msg); 41 using ClearLastError = jvmtiError(*)(jvmtiEnv* env); 42 43 template <typename T> 44 static void Dealloc(T* t) { 45 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t)); 46 } 47 48 template <typename T, typename ...Rest> 49 static void Dealloc(T* t, Rest... rs) { 50 Dealloc(t); 51 Dealloc(rs...); 52 } 53 54 static void DeallocParams(jvmtiParamInfo* params, jint n_params) { 55 for (jint i = 0; i < n_params; i++) { 56 Dealloc(params[i].name); 57 } 58 } 59 60 static jvmtiExtensionFunction FindExtensionMethod(JNIEnv* env, const std::string& name) { 61 jint n_ext; 62 jvmtiExtensionFunctionInfo* infos; 63 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) { 64 return nullptr; 65 } 66 jvmtiExtensionFunction res = nullptr; 67 for (jint i = 0; i < n_ext; i++) { 68 jvmtiExtensionFunctionInfo* cur_info = &infos[i]; 69 if (strcmp(name.c_str(), cur_info->id) == 0) { 70 res = cur_info->func; 71 } 72 // Cleanup the cur_info 73 DeallocParams(cur_info->params, cur_info->param_count); 74 Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors); 75 } 76 // Cleanup the array. 77 Dealloc(infos); 78 if (res == nullptr) { 79 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException")); 80 env->ThrowNew(rt_exception.get(), (name + " extensions not found").c_str()); 81 return nullptr; 82 } 83 return res; 84 } 85 86 extern "C" JNIEXPORT 87 jstring JNICALL Java_art_Test1957_getLastError(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { 88 GetLastError get_last_error = reinterpret_cast<GetLastError>( 89 FindExtensionMethod(env, "com.android.art.misc.get_last_error_message")); 90 if (get_last_error == nullptr) { 91 return nullptr; 92 } 93 char* msg; 94 if (JvmtiErrorToException(env, jvmti_env, get_last_error(jvmti_env, &msg))) { 95 return nullptr; 96 } 97 98 return env->NewStringUTF(msg); 99 } 100 101 extern "C" JNIEXPORT 102 void JNICALL Java_art_Test1957_clearLastError(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { 103 ClearLastError clear_last_error = reinterpret_cast<ClearLastError>( 104 FindExtensionMethod(env, "com.android.art.misc.clear_last_error_message")); 105 if (clear_last_error == nullptr) { 106 return; 107 } 108 JvmtiErrorToException(env, jvmti_env, clear_last_error(jvmti_env)); 109 } 110 111 } // namespace Test1957ErrorExt 112 } // namespace art 113