• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "transform.h"
33 
34 #include <stddef.h>
35 #include <sys/types.h>
36 
37 #include <unordered_map>
38 #include <unordered_set>
39 
40 #include "art_method.h"
41 #include "base/array_ref.h"
42 #include "base/globals.h"
43 #include "base/logging.h"
44 #include "base/mem_map.h"
45 #include "class_linker.h"
46 #include "dex/dex_file.h"
47 #include "dex/dex_file_types.h"
48 #include "dex/utf.h"
49 #include "events-inl.h"
50 #include "events.h"
51 #include "fault_handler.h"
52 #include "gc_root-inl.h"
53 #include "handle_scope-inl.h"
54 #include "jni/jni_env_ext-inl.h"
55 #include "jvalue.h"
56 #include "jvmti.h"
57 #include "linear_alloc.h"
58 #include "mirror/array.h"
59 #include "mirror/class-inl.h"
60 #include "mirror/class_ext.h"
61 #include "mirror/class_loader-inl.h"
62 #include "mirror/string-inl.h"
63 #include "oat/oat_file.h"
64 #include "scoped_thread_state_change-inl.h"
65 #include "stack.h"
66 #include "thread_list.h"
67 #include "ti_logging.h"
68 #include "ti_redefine.h"
69 
70 namespace openjdkjvmti {
71 
72 static EventHandler* gEventHandler = nullptr;
73 
Register(EventHandler * eh)74 void Transformer::Register(EventHandler* eh) {
75   gEventHandler = eh;
76 }
77 
78 // Initialize templates.
79 template void Transformer::CallClassFileLoadHooksSingleClass<
80     ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(EventHandler* event_handler,
81                                                          art::Thread* self,
82                                                          /*in-out*/ ArtClassDefinition* def);
83 template void Transformer::CallClassFileLoadHooksSingleClass<
84     ArtJvmtiEvent::kClassFileLoadHookRetransformable>(EventHandler* event_handler,
85                                                       art::Thread* self,
86                                                       /*in-out*/ ArtClassDefinition* def);
87 template void Transformer::CallClassFileLoadHooksSingleClass<
88     ArtJvmtiEvent::kStructuralDexFileLoadHook>(EventHandler* event_handler,
89                                                art::Thread* self,
90                                                /*in-out*/ ArtClassDefinition* def);
91 
92 template <ArtJvmtiEvent kEvent>
CallClassFileLoadHooksSingleClass(EventHandler * event_handler,art::Thread * self,ArtClassDefinition * def)93 void Transformer::CallClassFileLoadHooksSingleClass(EventHandler* event_handler,
94                                                     art::Thread* self,
95                                                     /*in-out*/ ArtClassDefinition* def) {
96   static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable ||
97                 kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
98                 kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook,
99                 "bad event type");
100   // We don't want to do transitions between calling the event and setting the new data so change to
101   // native state early.
102   art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative);
103   jint new_len = -1;
104   unsigned char* new_data = nullptr;
105   art::ArrayRef<const unsigned char> dex_data = def->GetDexData();
106   event_handler->DispatchEvent<kEvent>(
107       self,
108       static_cast<JNIEnv*>(self->GetJniEnv()),
109       def->GetClass(),
110       def->GetLoader(),
111       def->GetName().c_str(),
112       def->GetProtectionDomain(),
113       static_cast<jint>(dex_data.size()),
114       dex_data.data(),
115       /*out*/&new_len,
116       /*out*/&new_data);
117   def->SetNewDexData(new_len, new_data, kEvent);
118 }
119 
120 template <RedefinitionType kType>
CallClassFileLoadHooks(art::Thread * self,std::vector<ArtClassDefinition> * definitions)121 void Transformer::CallClassFileLoadHooks(art::Thread* self,
122                                          /*in-out*/ std::vector<ArtClassDefinition>* definitions) {
123   if (kType == RedefinitionType::kNormal) {
124     // For normal redefinition we have to call ClassFileLoadHook according to the spec. We use an
125     // internal event "ClassFileLoadHookRetransformable" for agents that can redefine and a
126     // "ClassFileLoadHookNonRetransformable" for agents that cannot redefine. When an agent is
127     // attached to a non-debuggable environment, we cannot redefine any classes. Splitting the
128     // ClassFileLoadHooks allows us to differentiate between these two cases. This method is only
129     // called when redefinition is allowed so just run ClassFileLoadHookRetransformable hooks.
130     for (ArtClassDefinition& def : *definitions) {
131       CallClassFileLoadHooksSingleClass<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
132           gEventHandler, self, &def);
133     }
134   } else {
135     // For structural redefinition we call StructualDexFileLoadHook in addition to the
136     // ClassFileLoadHooks. This let's us specify if structural modifications are allowed.
137     // TODO(mythria): The spec only specifies we need to call ClassFileLoadHooks, the
138     // StructuralDexFileLoadHooks is internal to ART. It is not clear if we need to run all
139     // StructuralDexFileHooks before ClassFileLoadHooks. Doing it this way to keep the existing
140     // behaviour.
141     for (ArtClassDefinition& def : *definitions) {
142       CallClassFileLoadHooksSingleClass<ArtJvmtiEvent::kStructuralDexFileLoadHook>(
143           gEventHandler, self, &def);
144     }
145     for (ArtClassDefinition& def : *definitions) {
146       CallClassFileLoadHooksSingleClass<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
147           gEventHandler, self, &def);
148     }
149   }
150 }
151 
152 template void Transformer::CallClassFileLoadHooks<RedefinitionType::kNormal>(
153     art::Thread* self, /*in-out*/ std::vector<ArtClassDefinition>* definitions);
154 template void Transformer::CallClassFileLoadHooks<RedefinitionType::kStructural>(
155     art::Thread* self, /*in-out*/ std::vector<ArtClassDefinition>* definitions);
156 
RetransformClasses(jvmtiEnv * env,jint class_count,const jclass * classes)157 jvmtiError Transformer::RetransformClasses(jvmtiEnv* env,
158                                            jint class_count,
159                                            const jclass* classes) {
160   if (class_count < 0) {
161     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0";
162     return ERR(ILLEGAL_ARGUMENT);
163   } else if (class_count == 0) {
164     // We don't actually need to do anything. Just return OK.
165     return OK;
166   } else if (classes == nullptr) {
167     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!";
168     return ERR(NULL_POINTER);
169   }
170   art::Thread* self = art::Thread::Current();
171   art::Runtime* runtime = art::Runtime::Current();
172   // A holder that will Deallocate all the class bytes buffers on destruction.
173   std::string error_msg;
174   std::vector<ArtClassDefinition> definitions;
175   jvmtiError res = OK;
176   for (jint i = 0; i < class_count; i++) {
177     res = Redefiner::CanRedefineClass<RedefinitionType::kNormal>(classes[i], &error_msg);
178     if (res != OK) {
179       JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
180       return res;
181     }
182     ArtClassDefinition def;
183     res = def.Init(self, classes[i]);
184     if (res != OK) {
185       JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed";
186       return res;
187     }
188     definitions.push_back(std::move(def));
189   }
190 
191   CallClassFileLoadHooks<RedefinitionType::kStructural>(self, &definitions);
192   RedefinitionType redef_type =
193       std::any_of(definitions.cbegin(),
194                   definitions.cend(),
195                   [](const auto& it) { return it.HasStructuralChanges(); })
196           ? RedefinitionType::kStructural
197           : RedefinitionType::kNormal;
198   res = Redefiner::RedefineClassesDirect(
199       ArtJvmTiEnv::AsArtJvmTiEnv(env), runtime, self, definitions, redef_type, &error_msg);
200   if (res != OK) {
201     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
202   }
203   return res;
204 }
205 
206 // TODO Move this somewhere else, ti_class?
GetClassLocation(ArtJvmTiEnv * env,jclass klass,std::string * location)207 jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
208   JNIEnv* jni_env = nullptr;
209   jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
210   if (ret != JNI_OK) {
211     // TODO Different error might be better?
212     return ERR(INTERNAL);
213   }
214   art::ScopedObjectAccess soa(jni_env);
215   art::StackHandleScope<1> hs(art::Thread::Current());
216   art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
217   const art::DexFile& dex = hs_klass->GetDexFile();
218   *location = dex.GetLocation();
219   return OK;
220 }
221 
222 }  // namespace openjdkjvmti
223