• 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_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
80 void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
81     EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
82 template
83 void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
84     EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
85 template
86 void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kStructuralDexFileLoadHook>(
87     EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
88 
89 template<ArtJvmtiEvent kEvent>
TransformSingleClassDirect(EventHandler * event_handler,art::Thread * self,ArtClassDefinition * def)90 void Transformer::TransformSingleClassDirect(EventHandler* event_handler,
91                                              art::Thread* self,
92                                              /*in-out*/ArtClassDefinition* def) {
93   static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable ||
94                 kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
95                 kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook,
96                 "bad event type");
97   // We don't want to do transitions between calling the event and setting the new data so change to
98   // native state early.
99   art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative);
100   jint new_len = -1;
101   unsigned char* new_data = nullptr;
102   art::ArrayRef<const unsigned char> dex_data = def->GetDexData();
103   event_handler->DispatchEvent<kEvent>(
104       self,
105       static_cast<JNIEnv*>(self->GetJniEnv()),
106       def->GetClass(),
107       def->GetLoader(),
108       def->GetName().c_str(),
109       def->GetProtectionDomain(),
110       static_cast<jint>(dex_data.size()),
111       dex_data.data(),
112       /*out*/&new_len,
113       /*out*/&new_data);
114   def->SetNewDexData(new_len, new_data, kEvent);
115 }
116 
117 template <RedefinitionType kType>
RetransformClassesDirect(art::Thread * self,std::vector<ArtClassDefinition> * definitions)118 void Transformer::RetransformClassesDirect(
119     art::Thread* self,
120     /*in-out*/ std::vector<ArtClassDefinition>* definitions) {
121   constexpr ArtJvmtiEvent kEvent = kType == RedefinitionType::kNormal
122                                        ? ArtJvmtiEvent::kClassFileLoadHookRetransformable
123                                        : ArtJvmtiEvent::kStructuralDexFileLoadHook;
124   for (ArtClassDefinition& def : *definitions) {
125     TransformSingleClassDirect<kEvent>(gEventHandler, self, &def);
126   }
127 }
128 
129 template void Transformer::RetransformClassesDirect<RedefinitionType::kNormal>(
130       art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
131 template void Transformer::RetransformClassesDirect<RedefinitionType::kStructural>(
132       art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
133 
RetransformClasses(jvmtiEnv * env,jint class_count,const jclass * classes)134 jvmtiError Transformer::RetransformClasses(jvmtiEnv* env,
135                                            jint class_count,
136                                            const jclass* classes) {
137   if (class_count < 0) {
138     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0";
139     return ERR(ILLEGAL_ARGUMENT);
140   } else if (class_count == 0) {
141     // We don't actually need to do anything. Just return OK.
142     return OK;
143   } else if (classes == nullptr) {
144     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!";
145     return ERR(NULL_POINTER);
146   }
147   art::Thread* self = art::Thread::Current();
148   art::Runtime* runtime = art::Runtime::Current();
149   // A holder that will Deallocate all the class bytes buffers on destruction.
150   std::string error_msg;
151   std::vector<ArtClassDefinition> definitions;
152   jvmtiError res = OK;
153   for (jint i = 0; i < class_count; i++) {
154     res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(classes[i], &error_msg);
155     if (res != OK) {
156       JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
157       return res;
158     }
159     ArtClassDefinition def;
160     res = def.Init(self, classes[i]);
161     if (res != OK) {
162       JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed";
163       return res;
164     }
165     definitions.push_back(std::move(def));
166   }
167   RetransformClassesDirect<RedefinitionType::kStructural>(self, &definitions);
168   RetransformClassesDirect<RedefinitionType::kNormal>(self, &definitions);
169   RedefinitionType redef_type =
170       std::any_of(definitions.cbegin(),
171                   definitions.cend(),
172                   [](const auto& it) { return it.HasStructuralChanges(); })
173           ? RedefinitionType::kStructural
174           : RedefinitionType::kNormal;
175   res = Redefiner::RedefineClassesDirect(
176       ArtJvmTiEnv::AsArtJvmTiEnv(env), runtime, self, definitions, redef_type, &error_msg);
177   if (res != OK) {
178     JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
179   }
180   return res;
181 }
182 
183 // TODO Move this somewhere else, ti_class?
GetClassLocation(ArtJvmTiEnv * env,jclass klass,std::string * location)184 jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
185   JNIEnv* jni_env = nullptr;
186   jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
187   if (ret != JNI_OK) {
188     // TODO Different error might be better?
189     return ERR(INTERNAL);
190   }
191   art::ScopedObjectAccess soa(jni_env);
192   art::StackHandleScope<1> hs(art::Thread::Current());
193   art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
194   const art::DexFile& dex = hs_klass->GetDexFile();
195   *location = dex.GetLocation();
196   return OK;
197 }
198 
199 }  // namespace openjdkjvmti
200