1 /* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32 #include <unordered_map>
33 #include <unordered_set>
34
35 #include "transform.h"
36
37 #include "art_method.h"
38 #include "class_linker.h"
39 #include "dex_file.h"
40 #include "dex_file_types.h"
41 #include "events-inl.h"
42 #include "gc_root-inl.h"
43 #include "globals.h"
44 #include "jni_env_ext-inl.h"
45 #include "jvalue.h"
46 #include "jvmti.h"
47 #include "linear_alloc.h"
48 #include "mem_map.h"
49 #include "mirror/array.h"
50 #include "mirror/class-inl.h"
51 #include "mirror/class_ext.h"
52 #include "mirror/class_loader-inl.h"
53 #include "mirror/string-inl.h"
54 #include "oat_file.h"
55 #include "scoped_thread_state_change-inl.h"
56 #include "stack.h"
57 #include "thread_list.h"
58 #include "ti_redefine.h"
59 #include "transform.h"
60 #include "utf.h"
61 #include "utils/dex_cache_arrays_layout-inl.h"
62
63 namespace openjdkjvmti {
64
RetransformClassesDirect(ArtJvmTiEnv * env,EventHandler * event_handler,art::Thread * self,std::vector<ArtClassDefinition> * definitions)65 jvmtiError Transformer::RetransformClassesDirect(
66 ArtJvmTiEnv* env,
67 EventHandler* event_handler,
68 art::Thread* self,
69 /*in-out*/std::vector<ArtClassDefinition>* definitions) {
70 for (ArtClassDefinition& def : *definitions) {
71 jint new_len = -1;
72 unsigned char* new_data = nullptr;
73 art::ArraySlice<const unsigned char> dex_data = def.GetDexData();
74 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
75 self,
76 GetJniEnv(env),
77 def.GetClass(),
78 def.GetLoader(),
79 def.GetName().c_str(),
80 def.GetProtectionDomain(),
81 static_cast<jint>(dex_data.size()),
82 &dex_data.At(0),
83 /*out*/&new_len,
84 /*out*/&new_data);
85 def.SetNewDexData(env, new_len, new_data);
86 }
87 return OK;
88 }
89
RetransformClasses(ArtJvmTiEnv * env,EventHandler * event_handler,art::Runtime * runtime,art::Thread * self,jint class_count,const jclass * classes,std::string * error_msg)90 jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env,
91 EventHandler* event_handler,
92 art::Runtime* runtime,
93 art::Thread* self,
94 jint class_count,
95 const jclass* classes,
96 /*out*/std::string* error_msg) {
97 if (env == nullptr) {
98 *error_msg = "env was null!";
99 return ERR(INVALID_ENVIRONMENT);
100 } else if (class_count < 0) {
101 *error_msg = "class_count was less then 0";
102 return ERR(ILLEGAL_ARGUMENT);
103 } else if (class_count == 0) {
104 // We don't actually need to do anything. Just return OK.
105 return OK;
106 } else if (classes == nullptr) {
107 *error_msg = "null classes!";
108 return ERR(NULL_POINTER);
109 }
110 // A holder that will Deallocate all the class bytes buffers on destruction.
111 std::vector<ArtClassDefinition> definitions;
112 jvmtiError res = OK;
113 for (jint i = 0; i < class_count; i++) {
114 jboolean is_modifiable = JNI_FALSE;
115 res = env->IsModifiableClass(classes[i], &is_modifiable);
116 if (res != OK) {
117 return res;
118 } else if (!is_modifiable) {
119 return ERR(UNMODIFIABLE_CLASS);
120 }
121 ArtClassDefinition def;
122 res = def.Init(env, classes[i]);
123 if (res != OK) {
124 return res;
125 }
126 definitions.push_back(std::move(def));
127 }
128 res = RetransformClassesDirect(env, event_handler, self, &definitions);
129 if (res != OK) {
130 return res;
131 }
132 return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg);
133 }
134
135 // TODO Move this somewhere else, ti_class?
GetClassLocation(ArtJvmTiEnv * env,jclass klass,std::string * location)136 jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
137 JNIEnv* jni_env = nullptr;
138 jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
139 if (ret != JNI_OK) {
140 // TODO Different error might be better?
141 return ERR(INTERNAL);
142 }
143 art::ScopedObjectAccess soa(jni_env);
144 art::StackHandleScope<1> hs(art::Thread::Current());
145 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
146 const art::DexFile& dex = hs_klass->GetDexFile();
147 *location = dex.GetLocation();
148 return OK;
149 }
150
151 } // namespace openjdkjvmti
152