blob: 01b6c989cc4e72fe37a7f9fbc33675c1ad6ea200 [file] [log] [blame]
Alex Light1e07ca62016-12-02 11:40:56 -08001/*
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 Light10f02fb2017-01-06 16:21:48 -080020#include <sstream>
Alex Light1e07ca62016-12-02 11:40:56 -080021
Alex Lightdba61482016-12-21 08:20:29 -080022#include "art_method.h"
Alex Light1e07ca62016-12-02 11:40:56 -080023#include "jni.h"
24#include "openjdkjvmti/jvmti.h"
Alex Lightdba61482016-12-21 08:20:29 -080025#include "scoped_thread_state_change-inl.h"
26#include "stack.h"
Alex Light1e07ca62016-12-02 11:40:56 -080027#include "ti-agent/common_load.h"
28#include "utils.h"
29
30namespace art {
31bool RuntimeIsJVM;
32
33bool IsJVM() {
34 return RuntimeIsJVM;
35}
36
37void SetAllCapabilities(jvmtiEnv* env) {
38 jvmtiCapabilities caps;
39 env->GetPotentialCapabilities(&caps);
40 env->AddCapabilities(&caps);
41}
42
43namespace common_redefine {
44
Alex Light10f02fb2017-01-06 16:21:48 -080045static 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 Light1e07ca62016-12-02 11:40:56 -080060using RedefineDirectFunction = jvmtiError (*)(jvmtiEnv*, jclass, jint, const unsigned char*);
Alex Light10f02fb2017-01-06 16:21:48 -080061static void DoClassTransformation(jvmtiEnv* jvmti_env,
62 JNIEnv* env,
Alex Light1e07ca62016-12-02 11:40:56 -080063 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 Light10f02fb2017-01-06 16:21:48 -080083 throwRedefinitionError(jvmti_env, env, target, res);
Alex Light1e07ca62016-12-02 11:40:56 -080084 }
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
89extern "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
98jint 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