blob: ebf1e4621c50c2d1ef62178bbbcb259c931d2d2a [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>
20
Alex Lightd8936da2016-11-28 16:24:32 -080021#include "art_method.h"
Alex Light1e07ca62016-12-02 11:40:56 -080022#include "jni.h"
23#include "openjdkjvmti/jvmti.h"
Alex Lightd8936da2016-11-28 16:24:32 -080024#include "scoped_thread_state_change-inl.h"
25#include "stack.h"
Alex Light1e07ca62016-12-02 11:40:56 -080026#include "ti-agent/common_load.h"
27#include "utils.h"
28
29namespace art {
30bool RuntimeIsJVM;
31
32bool IsJVM() {
33 return RuntimeIsJVM;
34}
35
36void SetAllCapabilities(jvmtiEnv* env) {
37 jvmtiCapabilities caps;
38 env->GetPotentialCapabilities(&caps);
39 env->AddCapabilities(&caps);
40}
41
42namespace common_redefine {
43
44using RedefineDirectFunction = jvmtiError (*)(jvmtiEnv*, jclass, jint, const unsigned char*);
45static void DoClassTransformation(jvmtiEnv* jvmti_env, JNIEnv* env,
46 jclass target,
47 jbyteArray class_file_bytes,
48 jbyteArray dex_file_bytes) {
49 jbyteArray desired_array = IsJVM() ? class_file_bytes : dex_file_bytes;
50 jint len = static_cast<jint>(env->GetArrayLength(desired_array));
51 const unsigned char* redef_bytes = reinterpret_cast<const unsigned char*>(
52 env->GetByteArrayElements(desired_array, nullptr));
53 jvmtiError res;
54 if (IsJVM()) {
55 jvmtiClassDefinition def;
56 def.klass = target;
57 def.class_byte_count = static_cast<jint>(len);
58 def.class_bytes = redef_bytes;
59 res = jvmti_env->RedefineClasses(1, &def);
60 } else {
61 RedefineDirectFunction f =
62 reinterpret_cast<RedefineDirectFunction>(jvmti_env->functions->reserved3);
63 res = f(jvmti_env, target, len, redef_bytes);
64 }
65 if (res != JVMTI_ERROR_NONE) {
66 printf("Redefinition failed!");
67 }
68}
69
70// Magic JNI export that classes can use for redefining classes.
71// To use classes should declare this as a native function with signature (Ljava/lang/Class;[B[B)V
72extern "C" JNIEXPORT void JNICALL Java_Main_doCommonClassRedefinition(JNIEnv* env,
73 jclass,
74 jclass target,
75 jbyteArray class_file_bytes,
76 jbyteArray dex_file_bytes) {
77 DoClassTransformation(jvmti_env, env, target, class_file_bytes, dex_file_bytes);
78}
79
80// Don't do anything
81jint OnLoad(JavaVM* vm,
82 char* options ATTRIBUTE_UNUSED,
83 void* reserved ATTRIBUTE_UNUSED) {
84 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
85 printf("Unable to get jvmti env!\n");
86 return 1;
87 }
88 SetAllCapabilities(jvmti_env);
89 return 0;
90}
91
92} // namespace common_redefine
93
94} // namespace art