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