16f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe/* Copyright (C) 2017 The Android Open Source Project
26f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
46f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * This file implements interfaces from the file jvmti.h. This implementation
56f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * is licensed under the same terms as the file jvmti.h.  The
66f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * copyright and license information for the file jvmti.h follows.
76f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
86f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
96f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
106f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
116f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * This code is free software; you can redistribute it and/or modify it
126f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * under the terms of the GNU General Public License version 2 only, as
136f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * published by the Free Software Foundation.  Oracle designates this
146f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * particular file as subject to the "Classpath" exception as provided
156f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * by Oracle in the LICENSE file that accompanied this code.
166f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
176f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * This code is distributed in the hope that it will be useful, but WITHOUT
186f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
196f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
206f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * version 2 for more details (a copy is included in the LICENSE file that
216f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * accompanied this code).
226f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
236f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * You should have received a copy of the GNU General Public License version
246f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * 2 along with this work; if not, write to the Free Software Foundation,
256f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
266f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe *
276f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * or visit www.oracle.com if you need additional information or have any
296f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe * questions.
306f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe */
316f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
326f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "ti_jni.h"
336f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
346f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "jni.h"
356f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
366f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "art_jvmti.h"
376f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "base/mutex.h"
386f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "java_vm_ext.h"
396f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "jni_env_ext.h"
406f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "runtime.h"
416f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe#include "thread-inl.h"
426f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
436f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampenamespace openjdkjvmti {
446f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
456f8e4f0fc378b49b8203db9a64777be3c773556aAndreas GampejvmtiError JNIUtil::SetJNIFunctionTable(jvmtiEnv* env ATTRIBUTE_UNUSED,
466f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe                                        const jniNativeInterface* function_table) {
476f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // While we supporting setting null (which will reset the table), the spec says no.
486f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  if (function_table == nullptr) {
496f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    return ERR(NULL_POINTER);
506f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  }
516f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
526f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  art::JNIEnvExt::SetTableOverride(function_table);
536f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  return ERR(NONE);
546f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe}
556f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
566f8e4f0fc378b49b8203db9a64777be3c773556aAndreas GampejvmtiError JNIUtil::GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
576f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  if (function_table == nullptr) {
586f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    return ERR(NULL_POINTER);
596f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  }
606f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
616f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // We use the generic JNIEnvExt::GetFunctionTable instead of querying a specific JNIEnv, as
626f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // this has to work in the start phase.
636f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
646f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // Figure out which table is current. Conservatively assume check-jni is off.
656f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  bool check_jni = false;
666f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  art::Runtime* runtime = art::Runtime::Current();
676f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  if (runtime != nullptr && runtime->GetJavaVM() != nullptr) {
686f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    check_jni = runtime->GetJavaVM()->IsCheckJniEnabled();
696f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  }
706f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
716f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // Get that table.
726f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  const JNINativeInterface* current_table;
736f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  {
746f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    art::MutexLock mu(art::Thread::Current(), *art::Locks::jni_function_table_lock_);
756f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    current_table = art::JNIEnvExt::GetFunctionTable(check_jni);
766f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  }
776f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
786f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  // Allocate memory and copy the table.
796f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  unsigned char* data;
806f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  jvmtiError data_result = env->Allocate(sizeof(JNINativeInterface), &data);
816f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  if (data_result != ERR(NONE)) {
826f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe    return data_result;
836f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  }
846f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  memcpy(data, current_table, sizeof(JNINativeInterface));
856f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
866f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  *function_table = reinterpret_cast<JNINativeInterface*>(data);
876f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
886f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe  return ERR(NONE);
896f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe}
906f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe
916f8e4f0fc378b49b8203db9a64777be3c773556aAndreas Gampe}  // namespace openjdkjvmti
92