• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2017 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 "ti_search.h"
33 
34 #include "jni.h"
35 
36 #include "art_field-inl.h"
37 #include "art_jvmti.h"
38 #include "base/enums.h"
39 #include "base/macros.h"
40 #include "class_linker.h"
41 #include "dex/art_dex_file_loader.h"
42 #include "dex/dex_file.h"
43 #include "dex/dex_file_loader.h"
44 #include "jni/jni_internal.h"
45 #include "mirror/class-inl.h"
46 #include "mirror/object.h"
47 #include "mirror/string.h"
48 #include "nativehelper/scoped_local_ref.h"
49 #include "obj_ptr-inl.h"
50 #include "runtime.h"
51 #include "runtime_callbacks.h"
52 #include "scoped_thread_state_change-inl.h"
53 #include "thread-current-inl.h"
54 #include "thread_list.h"
55 #include "ti_logging.h"
56 #include "ti_phase.h"
57 #include "well_known_classes.h"
58 
59 namespace openjdkjvmti {
60 
61 static std::vector<std::string> gSystemOnloadSegments;
62 
GetSystemProperties(art::Thread * self,art::ClassLinker * class_linker)63 static art::ObjPtr<art::mirror::Object> GetSystemProperties(art::Thread* self,
64                                                             art::ClassLinker* class_linker)
65     REQUIRES_SHARED(art::Locks::mutator_lock_) {
66   art::ObjPtr<art::mirror::Class> system_class =
67       class_linker->LookupClass(self, "Ljava/lang/System;", nullptr);
68   DCHECK(system_class != nullptr);
69   DCHECK(system_class->IsInitialized());
70 
71   art::ArtField* props_field =
72       system_class->FindDeclaredStaticField("props", "Ljava/util/Properties;");
73   DCHECK(props_field != nullptr);
74 
75   art::ObjPtr<art::mirror::Object> props_obj = props_field->GetObject(system_class);
76   DCHECK(props_obj != nullptr);
77 
78   return props_obj;
79 }
80 
Update()81 static void Update() REQUIRES_SHARED(art::Locks::mutator_lock_) {
82   if (gSystemOnloadSegments.empty()) {
83     return;
84   }
85 
86   // In the on-load phase we have to modify java.class.path to influence the system classloader.
87   // As this is an unmodifiable system property, we have to access the "defaults" field.
88   art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
89   DCHECK(class_linker != nullptr);
90   art::Thread* self = art::Thread::Current();
91 
92   // Prepare: collect classes, fields and methods.
93   art::ObjPtr<art::mirror::Class> properties_class =
94       class_linker->LookupClass(self, "Ljava/util/Properties;", nullptr);
95   DCHECK(properties_class != nullptr);
96 
97   ScopedLocalRef<jobject> defaults_jobj(self->GetJniEnv(), nullptr);
98   {
99     art::ObjPtr<art::mirror::Object> props_obj = GetSystemProperties(self, class_linker);
100 
101     art::ArtField* defaults_field =
102         properties_class->FindDeclaredInstanceField("defaults", "Ljava/util/Properties;");
103     DCHECK(defaults_field != nullptr);
104 
105     art::ObjPtr<art::mirror::Object> defaults_obj = defaults_field->GetObject(props_obj);
106     DCHECK(defaults_obj != nullptr);
107     defaults_jobj.reset(self->GetJniEnv()->AddLocalReference<jobject>(defaults_obj));
108   }
109 
110   art::ArtMethod* get_property =
111       properties_class->FindClassMethod(
112           "getProperty",
113           "(Ljava/lang/String;)Ljava/lang/String;",
114           art::kRuntimePointerSize);
115   DCHECK(get_property != nullptr);
116   DCHECK(!get_property->IsDirect());
117   DCHECK(get_property->GetDeclaringClass() == properties_class);
118   art::ArtMethod* set_property =
119       properties_class->FindClassMethod(
120           "setProperty",
121           "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;",
122           art::kRuntimePointerSize);
123   DCHECK(set_property != nullptr);
124   DCHECK(!set_property->IsDirect());
125   DCHECK(set_property->GetDeclaringClass() == properties_class);
126 
127   // This is an allocation. Do this late to avoid the need for handles.
128   ScopedLocalRef<jobject> cp_jobj(self->GetJniEnv(), nullptr);
129   {
130     art::ObjPtr<art::mirror::Object> cp_key =
131         art::mirror::String::AllocFromModifiedUtf8(self, "java.class.path");
132     if (cp_key == nullptr) {
133       self->AssertPendingOOMException();
134       self->ClearException();
135       return;
136     }
137     cp_jobj.reset(self->GetJniEnv()->AddLocalReference<jobject>(cp_key));
138   }
139 
140   // OK, now get the current value.
141   std::string str_value;
142   {
143     ScopedLocalRef<jobject> old_value(self->GetJniEnv(),
144                                       self->GetJniEnv()->CallObjectMethod(
145                                           defaults_jobj.get(),
146                                           art::jni::EncodeArtMethod(get_property),
147                                           cp_jobj.get()));
148     DCHECK(old_value.get() != nullptr);
149 
150     str_value = self->DecodeJObject(old_value.get())->AsString()->ToModifiedUtf8();
151     self->GetJniEnv()->DeleteLocalRef(old_value.release());
152   }
153 
154   // Update the value by appending the new segments.
155   for (const std::string& segment : gSystemOnloadSegments) {
156     if (!str_value.empty()) {
157       str_value += ":";
158     }
159     str_value += segment;
160   }
161   gSystemOnloadSegments.clear();
162 
163   // Create the new value object.
164   ScopedLocalRef<jobject> new_val_jobj(self->GetJniEnv(), nullptr);
165   {
166     art::ObjPtr<art::mirror::Object> new_value =
167         art::mirror::String::AllocFromModifiedUtf8(self, str_value.c_str());
168     if (new_value == nullptr) {
169       self->AssertPendingOOMException();
170       self->ClearException();
171       return;
172     }
173 
174     new_val_jobj.reset(self->GetJniEnv()->AddLocalReference<jobject>(new_value));
175   }
176 
177   // Write to the defaults.
178   ScopedLocalRef<jobject> res_obj(self->GetJniEnv(),
179                                   self->GetJniEnv()->CallObjectMethod(defaults_jobj.get(),
180                                       art::jni::EncodeArtMethod(set_property),
181                                       cp_jobj.get(),
182                                       new_val_jobj.get()));
183   if (self->IsExceptionPending()) {
184     self->ClearException();
185     return;
186   }
187 }
188 
189 struct SearchCallback : public art::RuntimePhaseCallback {
NextRuntimePhaseopenjdkjvmti::SearchCallback190   void NextRuntimePhase(RuntimePhase phase) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
191     if (phase == RuntimePhase::kStart) {
192       // It's time to update the system properties.
193       Update();
194     }
195   }
196 };
197 
198 static SearchCallback gSearchCallback;
199 
Register()200 void SearchUtil::Register() {
201   art::Runtime* runtime = art::Runtime::Current();
202 
203   art::ScopedThreadStateChange stsc(art::Thread::Current(),
204                                     art::ThreadState::kWaitingForDebuggerToAttach);
205   art::ScopedSuspendAll ssa("Add search callback");
206   runtime->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gSearchCallback);
207 }
208 
Unregister()209 void SearchUtil::Unregister() {
210   art::ScopedThreadStateChange stsc(art::Thread::Current(),
211                                     art::ThreadState::kWaitingForDebuggerToAttach);
212   art::ScopedSuspendAll ssa("Remove search callback");
213   art::Runtime* runtime = art::Runtime::Current();
214   runtime->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gSearchCallback);
215 }
216 
AddToBootstrapClassLoaderSearch(jvmtiEnv * env,const char * segment)217 jvmtiError SearchUtil::AddToBootstrapClassLoaderSearch(jvmtiEnv* env,
218                                                        const char* segment) {
219   art::Runtime* current = art::Runtime::Current();
220   if (current == nullptr) {
221     return ERR(WRONG_PHASE);
222   }
223   if (current->GetClassLinker() == nullptr) {
224     return ERR(WRONG_PHASE);
225   }
226   if (segment == nullptr) {
227     return ERR(NULL_POINTER);
228   }
229 
230   std::string error_msg;
231   std::vector<std::unique_ptr<const art::DexFile>> dex_files;
232   const art::ArtDexFileLoader dex_file_loader;
233   if (!dex_file_loader.Open(segment,
234                             segment,
235                             /* verify= */ true,
236                             /* verify_checksum= */ true,
237                             &error_msg,
238                             &dex_files)) {
239     JVMTI_LOG(WARNING, env) << "Could not open " << segment << " for boot classpath extension: "
240                             << error_msg;
241     return ERR(ILLEGAL_ARGUMENT);
242   }
243 
244   art::ScopedObjectAccess soa(art::Thread::Current());
245   for (std::unique_ptr<const art::DexFile>& dex_file : dex_files) {
246     current->GetClassLinker()->AppendToBootClassPath(art::Thread::Current(), *dex_file.release());
247   }
248 
249   return ERR(NONE);
250 }
251 
AddToSystemClassLoaderSearch(jvmtiEnv * jvmti_env ATTRIBUTE_UNUSED,const char * segment)252 jvmtiError SearchUtil::AddToSystemClassLoaderSearch(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED,
253                                                     const char* segment) {
254   if (segment == nullptr) {
255     return ERR(NULL_POINTER);
256   }
257 
258   jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked();
259 
260   if (phase == jvmtiPhase::JVMTI_PHASE_ONLOAD) {
261     // We could try and see whether it is a valid path. We could also try to allocate Java
262     // objects to avoid later OOME.
263     gSystemOnloadSegments.push_back(segment);
264     return ERR(NONE);
265   } else if (phase != jvmtiPhase::JVMTI_PHASE_LIVE) {
266     return ERR(WRONG_PHASE);
267   }
268 
269   jobject sys_class_loader = art::Runtime::Current()->GetSystemClassLoader();
270   if (sys_class_loader == nullptr) {
271     // This is unexpected.
272     return ERR(INTERNAL);
273   }
274 
275   // We'll use BaseDexClassLoader.addDexPath, as it takes care of array resizing etc. As a downside,
276   // exceptions are swallowed.
277 
278   art::Thread* self = art::Thread::Current();
279   JNIEnv* env = self->GetJniEnv();
280   if (!env->IsInstanceOf(sys_class_loader,
281                          art::WellKnownClasses::dalvik_system_BaseDexClassLoader)) {
282     return ERR(INTERNAL);
283   }
284 
285   jmethodID add_dex_path_id = env->GetMethodID(
286       art::WellKnownClasses::dalvik_system_BaseDexClassLoader,
287       "addDexPath",
288       "(Ljava/lang/String;)V");
289   if (add_dex_path_id == nullptr) {
290     return ERR(INTERNAL);
291   }
292 
293   ScopedLocalRef<jstring> dex_path(env, env->NewStringUTF(segment));
294   if (dex_path.get() == nullptr) {
295     return ERR(INTERNAL);
296   }
297   env->CallVoidMethod(sys_class_loader, add_dex_path_id, dex_path.get());
298 
299   if (env->ExceptionCheck()) {
300     env->ExceptionClear();
301     return ERR(ILLEGAL_ARGUMENT);
302   }
303   return ERR(NONE);
304 }
305 
306 }  // namespace openjdkjvmti
307