1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2017 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker * is licensed under the same terms as the file jvmti.h. The
6*795d594fSAndroid Build Coastguard Worker * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker *
11*795d594fSAndroid Build Coastguard Worker * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker * published by the Free Software Foundation. Oracle designates this
14*795d594fSAndroid Build Coastguard Worker * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker *
17*795d594fSAndroid Build Coastguard Worker * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker *
23*795d594fSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker *
27*795d594fSAndroid Build Coastguard Worker * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker * questions.
30*795d594fSAndroid Build Coastguard Worker */
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker #include "ti_jni.h"
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker #include "jni.h"
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker #include "art_jvmti.h"
37*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
38*795d594fSAndroid Build Coastguard Worker #include "jni/java_vm_ext.h"
39*795d594fSAndroid Build Coastguard Worker #include "jni/jni_env_ext.h"
40*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
41*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
42*795d594fSAndroid Build Coastguard Worker
43*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
44*795d594fSAndroid Build Coastguard Worker
SetJNIFunctionTable(jvmtiEnv * env,const jniNativeInterface * function_table)45*795d594fSAndroid Build Coastguard Worker jvmtiError JNIUtil::SetJNIFunctionTable([[maybe_unused]] jvmtiEnv* env,
46*795d594fSAndroid Build Coastguard Worker const jniNativeInterface* function_table) {
47*795d594fSAndroid Build Coastguard Worker // While we supporting setting null (which will reset the table), the spec says no.
48*795d594fSAndroid Build Coastguard Worker if (function_table == nullptr) {
49*795d594fSAndroid Build Coastguard Worker return ERR(NULL_POINTER);
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker art::JNIEnvExt::SetTableOverride(function_table);
53*795d594fSAndroid Build Coastguard Worker return ERR(NONE);
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker
GetJNIFunctionTable(jvmtiEnv * env,jniNativeInterface ** function_table)56*795d594fSAndroid Build Coastguard Worker jvmtiError JNIUtil::GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
57*795d594fSAndroid Build Coastguard Worker if (function_table == nullptr) {
58*795d594fSAndroid Build Coastguard Worker return ERR(NULL_POINTER);
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker
61*795d594fSAndroid Build Coastguard Worker // We use the generic JNIEnvExt::GetFunctionTable instead of querying a specific JNIEnv, as
62*795d594fSAndroid Build Coastguard Worker // this has to work in the start phase.
63*795d594fSAndroid Build Coastguard Worker
64*795d594fSAndroid Build Coastguard Worker // Figure out which table is current. Conservatively assume check-jni is off.
65*795d594fSAndroid Build Coastguard Worker bool check_jni = false;
66*795d594fSAndroid Build Coastguard Worker art::Runtime* runtime = art::Runtime::Current();
67*795d594fSAndroid Build Coastguard Worker if (runtime != nullptr && runtime->GetJavaVM() != nullptr) {
68*795d594fSAndroid Build Coastguard Worker check_jni = runtime->GetJavaVM()->IsCheckJniEnabled();
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker // Get that table.
72*795d594fSAndroid Build Coastguard Worker const JNINativeInterface* current_table;
73*795d594fSAndroid Build Coastguard Worker {
74*795d594fSAndroid Build Coastguard Worker art::MutexLock mu(art::Thread::Current(), *art::Locks::jni_function_table_lock_);
75*795d594fSAndroid Build Coastguard Worker current_table = art::JNIEnvExt::GetFunctionTable(check_jni);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker
78*795d594fSAndroid Build Coastguard Worker // Allocate memory and copy the table.
79*795d594fSAndroid Build Coastguard Worker unsigned char* data;
80*795d594fSAndroid Build Coastguard Worker jvmtiError data_result = env->Allocate(sizeof(JNINativeInterface), &data);
81*795d594fSAndroid Build Coastguard Worker if (data_result != ERR(NONE)) {
82*795d594fSAndroid Build Coastguard Worker return data_result;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker memcpy(data, current_table, sizeof(JNINativeInterface));
85*795d594fSAndroid Build Coastguard Worker
86*795d594fSAndroid Build Coastguard Worker *function_table = reinterpret_cast<JNINativeInterface*>(data);
87*795d594fSAndroid Build Coastguard Worker
88*795d594fSAndroid Build Coastguard Worker return ERR(NONE);
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker
91*795d594fSAndroid Build Coastguard Worker } // namespace openjdkjvmti
92