Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 1 | /* |
| 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 | #include "ti-agent/common_helper.h" |
| 18 | |
| 19 | #include <stdio.h> |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 20 | #include <sstream> |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 21 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 22 | #include "art_method.h" |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 23 | #include "jni.h" |
| 24 | #include "openjdkjvmti/jvmti.h" |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
| 26 | #include "stack.h" |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 27 | #include "ti-agent/common_load.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | bool RuntimeIsJVM; |
| 32 | |
| 33 | bool IsJVM() { |
| 34 | return RuntimeIsJVM; |
| 35 | } |
| 36 | |
| 37 | void SetAllCapabilities(jvmtiEnv* env) { |
| 38 | jvmtiCapabilities caps; |
| 39 | env->GetPotentialCapabilities(&caps); |
| 40 | env->AddCapabilities(&caps); |
| 41 | } |
| 42 | |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame^] | 43 | bool JvmtiErrorToException(JNIEnv* env, jvmtiError error) { |
| 44 | if (error == JVMTI_ERROR_NONE) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException")); |
| 49 | if (rt_exception.get() == nullptr) { |
| 50 | // CNFE should be pending. |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | char* err; |
| 55 | jvmti_env->GetErrorName(error, &err); |
| 56 | |
| 57 | env->ThrowNew(rt_exception.get(), err); |
| 58 | |
| 59 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 60 | return true; |
| 61 | } |
| 62 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 63 | namespace common_redefine { |
| 64 | |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 65 | static void throwRedefinitionError(jvmtiEnv* jvmti, JNIEnv* env, jclass target, jvmtiError res) { |
| 66 | std::stringstream err; |
| 67 | char* signature = nullptr; |
| 68 | char* generic = nullptr; |
| 69 | jvmti->GetClassSignature(target, &signature, &generic); |
| 70 | char* error = nullptr; |
| 71 | jvmti->GetErrorName(res, &error); |
| 72 | err << "Failed to redefine class <" << signature << "> due to " << error; |
| 73 | std::string message = err.str(); |
| 74 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature)); |
| 75 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(generic)); |
| 76 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(error)); |
| 77 | env->ThrowNew(env->FindClass("java/lang/Exception"), message.c_str()); |
| 78 | } |
| 79 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 80 | using RedefineDirectFunction = jvmtiError (*)(jvmtiEnv*, jclass, jint, const unsigned char*); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 81 | static void DoClassTransformation(jvmtiEnv* jvmti_env, |
| 82 | JNIEnv* env, |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 83 | jclass target, |
| 84 | jbyteArray class_file_bytes, |
| 85 | jbyteArray dex_file_bytes) { |
| 86 | jbyteArray desired_array = IsJVM() ? class_file_bytes : dex_file_bytes; |
| 87 | jint len = static_cast<jint>(env->GetArrayLength(desired_array)); |
| 88 | const unsigned char* redef_bytes = reinterpret_cast<const unsigned char*>( |
| 89 | env->GetByteArrayElements(desired_array, nullptr)); |
| 90 | jvmtiError res; |
| 91 | if (IsJVM()) { |
| 92 | jvmtiClassDefinition def; |
| 93 | def.klass = target; |
| 94 | def.class_byte_count = static_cast<jint>(len); |
| 95 | def.class_bytes = redef_bytes; |
| 96 | res = jvmti_env->RedefineClasses(1, &def); |
| 97 | } else { |
| 98 | RedefineDirectFunction f = |
| 99 | reinterpret_cast<RedefineDirectFunction>(jvmti_env->functions->reserved3); |
| 100 | res = f(jvmti_env, target, len, redef_bytes); |
| 101 | } |
| 102 | if (res != JVMTI_ERROR_NONE) { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 103 | throwRedefinitionError(jvmti_env, env, target, res); |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | // Magic JNI export that classes can use for redefining classes. |
| 108 | // To use classes should declare this as a native function with signature (Ljava/lang/Class;[B[B)V |
| 109 | extern "C" JNIEXPORT void JNICALL Java_Main_doCommonClassRedefinition(JNIEnv* env, |
| 110 | jclass, |
| 111 | jclass target, |
| 112 | jbyteArray class_file_bytes, |
| 113 | jbyteArray dex_file_bytes) { |
| 114 | DoClassTransformation(jvmti_env, env, target, class_file_bytes, dex_file_bytes); |
| 115 | } |
| 116 | |
| 117 | // Don't do anything |
| 118 | jint OnLoad(JavaVM* vm, |
| 119 | char* options ATTRIBUTE_UNUSED, |
| 120 | void* reserved ATTRIBUTE_UNUSED) { |
| 121 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 122 | printf("Unable to get jvmti env!\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | SetAllCapabilities(jvmti_env); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | } // namespace common_redefine |
| 130 | |
| 131 | } // namespace art |