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