Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 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_jvmti.h" |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 37 | #include "base/enums.h" |
Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 38 | #include "base/macros.h" |
| 39 | #include "class_linker.h" |
| 40 | #include "dex_file.h" |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 41 | #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 Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 46 | #include "runtime.h" |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 47 | #include "runtime_callbacks.h" |
Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 48 | #include "scoped_thread_state_change-inl.h" |
| 49 | #include "ScopedLocalRef.h" |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 50 | #include "ti_phase.h" |
| 51 | #include "thread-inl.h" |
| 52 | #include "thread_list.h" |
Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 53 | |
| 54 | namespace openjdkjvmti { |
| 55 | |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 56 | static std::vector<std::string> gSystemOnloadSegments; |
| 57 | |
| 58 | static 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 | |
| 76 | static 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 | |
| 180 | struct 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 | |
| 189 | static SearchCallback gSearchCallback; |
| 190 | |
| 191 | void 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 | |
| 200 | void 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 Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 208 | jvmtiError 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 | |
| 237 | jvmtiError SearchUtil::AddToSystemClassLoaderSearch(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, |
| 238 | const char* segment) { |
| 239 | if (segment == nullptr) { |
| 240 | return ERR(NULL_POINTER); |
| 241 | } |
| 242 | |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 243 | 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 Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 251 | return ERR(WRONG_PHASE); |
| 252 | } |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 253 | |
| 254 | jobject sys_class_loader = art::Runtime::Current()->GetSystemClassLoader(); |
Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 255 | if (sys_class_loader == nullptr) { |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame^] | 256 | // This is unexpected. |
| 257 | return ERR(INTERNAL); |
Andreas Gampe | ce7732b | 2017-01-17 15:50:26 -0800 | [diff] [blame] | 258 | } |
| 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 |