blob: 913d2b6a74c7c9594cab0837083756eb95a0af1f [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"
37#include "base/macros.h"
38#include "class_linker.h"
39#include "dex_file.h"
40#include "runtime.h"
41#include "scoped_thread_state_change-inl.h"
42#include "ScopedLocalRef.h"
43
44namespace openjdkjvmti {
45
46jvmtiError SearchUtil::AddToBootstrapClassLoaderSearch(jvmtiEnv* env ATTRIBUTE_UNUSED,
47 const char* segment) {
48 art::Runtime* current = art::Runtime::Current();
49 if (current == nullptr) {
50 return ERR(WRONG_PHASE);
51 }
52 if (current->GetClassLinker() == nullptr) {
53 // TODO: Support boot classpath change in OnLoad.
54 return ERR(WRONG_PHASE);
55 }
56 if (segment == nullptr) {
57 return ERR(NULL_POINTER);
58 }
59
60 std::string error_msg;
61 std::vector<std::unique_ptr<const art::DexFile>> dex_files;
62 if (!art::DexFile::Open(segment, segment, true, &error_msg, &dex_files)) {
63 LOG(WARNING) << "Could not open " << segment << " for boot classpath extension: " << error_msg;
64 return ERR(ILLEGAL_ARGUMENT);
65 }
66
67 art::ScopedObjectAccess soa(art::Thread::Current());
68 for (std::unique_ptr<const art::DexFile>& dex_file : dex_files) {
69 current->GetClassLinker()->AppendToBootClassPath(art::Thread::Current(), *dex_file.release());
70 }
71
72 return ERR(NONE);
73}
74
75jvmtiError SearchUtil::AddToSystemClassLoaderSearch(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED,
76 const char* segment) {
77 if (segment == nullptr) {
78 return ERR(NULL_POINTER);
79 }
80
81 art::Runtime* current = art::Runtime::Current();
82 if (current == nullptr) {
83 return ERR(WRONG_PHASE);
84 }
85 jobject sys_class_loader = current->GetSystemClassLoader();
86 if (sys_class_loader == nullptr) {
87 // TODO: Support classpath change in OnLoad.
88 return ERR(WRONG_PHASE);
89 }
90
91 // We'll use BaseDexClassLoader.addDexPath, as it takes care of array resizing etc. As a downside,
92 // exceptions are swallowed.
93
94 art::Thread* self = art::Thread::Current();
95 JNIEnv* env = self->GetJniEnv();
96 if (!env->IsInstanceOf(sys_class_loader,
97 art::WellKnownClasses::dalvik_system_BaseDexClassLoader)) {
98 return ERR(INTERNAL);
99 }
100
101 jmethodID add_dex_path_id = env->GetMethodID(
102 art::WellKnownClasses::dalvik_system_BaseDexClassLoader,
103 "addDexPath",
104 "(Ljava/lang/String;)V");
105 if (add_dex_path_id == nullptr) {
106 return ERR(INTERNAL);
107 }
108
109 ScopedLocalRef<jstring> dex_path(env, env->NewStringUTF(segment));
110 if (dex_path.get() == nullptr) {
111 return ERR(INTERNAL);
112 }
113 env->CallVoidMethod(sys_class_loader, add_dex_path_id, dex_path.get());
114
115 if (env->ExceptionCheck()) {
116 env->ExceptionClear();
117 return ERR(ILLEGAL_ARGUMENT);
118 }
119 return ERR(NONE);
120}
121
122} // namespace openjdkjvmti