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 | 10f02fb | 2017-01-06 16:21:48 -0800 | [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 | |
| 43 | namespace common_redefine { |
| 44 | |
Alex Light | 10f02fb | 2017-01-06 16:21:48 -0800 | [diff] [blame^] | 45 | static void throwRedefinitionError(jvmtiEnv* jvmti, JNIEnv* env, jclass target, jvmtiError res) { |
| 46 | std::stringstream err; |
| 47 | char* signature = nullptr; |
| 48 | char* generic = nullptr; |
| 49 | jvmti->GetClassSignature(target, &signature, &generic); |
| 50 | char* error = nullptr; |
| 51 | jvmti->GetErrorName(res, &error); |
| 52 | err << "Failed to redefine class <" << signature << "> due to " << error; |
| 53 | std::string message = err.str(); |
| 54 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature)); |
| 55 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(generic)); |
| 56 | jvmti->Deallocate(reinterpret_cast<unsigned char*>(error)); |
| 57 | env->ThrowNew(env->FindClass("java/lang/Exception"), message.c_str()); |
| 58 | } |
| 59 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 60 | using RedefineDirectFunction = jvmtiError (*)(jvmtiEnv*, jclass, jint, const unsigned char*); |
Alex Light | 10f02fb | 2017-01-06 16:21:48 -0800 | [diff] [blame^] | 61 | static void DoClassTransformation(jvmtiEnv* jvmti_env, |
| 62 | JNIEnv* env, |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 63 | jclass target, |
| 64 | jbyteArray class_file_bytes, |
| 65 | jbyteArray dex_file_bytes) { |
| 66 | jbyteArray desired_array = IsJVM() ? class_file_bytes : dex_file_bytes; |
| 67 | jint len = static_cast<jint>(env->GetArrayLength(desired_array)); |
| 68 | const unsigned char* redef_bytes = reinterpret_cast<const unsigned char*>( |
| 69 | env->GetByteArrayElements(desired_array, nullptr)); |
| 70 | jvmtiError res; |
| 71 | if (IsJVM()) { |
| 72 | jvmtiClassDefinition def; |
| 73 | def.klass = target; |
| 74 | def.class_byte_count = static_cast<jint>(len); |
| 75 | def.class_bytes = redef_bytes; |
| 76 | res = jvmti_env->RedefineClasses(1, &def); |
| 77 | } else { |
| 78 | RedefineDirectFunction f = |
| 79 | reinterpret_cast<RedefineDirectFunction>(jvmti_env->functions->reserved3); |
| 80 | res = f(jvmti_env, target, len, redef_bytes); |
| 81 | } |
| 82 | if (res != JVMTI_ERROR_NONE) { |
Alex Light | 10f02fb | 2017-01-06 16:21:48 -0800 | [diff] [blame^] | 83 | throwRedefinitionError(jvmti_env, env, target, res); |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | // Magic JNI export that classes can use for redefining classes. |
| 88 | // To use classes should declare this as a native function with signature (Ljava/lang/Class;[B[B)V |
| 89 | extern "C" JNIEXPORT void JNICALL Java_Main_doCommonClassRedefinition(JNIEnv* env, |
| 90 | jclass, |
| 91 | jclass target, |
| 92 | jbyteArray class_file_bytes, |
| 93 | jbyteArray dex_file_bytes) { |
| 94 | DoClassTransformation(jvmti_env, env, target, class_file_bytes, dex_file_bytes); |
| 95 | } |
| 96 | |
| 97 | // Don't do anything |
| 98 | jint OnLoad(JavaVM* vm, |
| 99 | char* options ATTRIBUTE_UNUSED, |
| 100 | void* reserved ATTRIBUTE_UNUSED) { |
| 101 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 102 | printf("Unable to get jvmti env!\n"); |
| 103 | return 1; |
| 104 | } |
| 105 | SetAllCapabilities(jvmti_env); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | } // namespace common_redefine |
| 110 | |
| 111 | } // namespace art |