2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2020-03-12 11:54:11 -07:00
|
|
|
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "classfile/javaClasses.hpp"
|
2019-05-09 14:28:30 +02:00
|
|
|
#include "classfile/symbolTable.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "classfile/systemDictionary.hpp"
|
|
|
|
#include "classfile/vmSymbols.hpp"
|
2019-11-11 12:11:34 -05:00
|
|
|
#include "logging/log.hpp"
|
|
|
|
#include "logging/logTag.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "memory/oopFactory.hpp"
|
|
|
|
#include "memory/resourceArea.hpp"
|
|
|
|
#include "oops/instanceKlass.hpp"
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
#include "oops/method.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "oops/oop.inline.hpp"
|
2011-01-27 16:11:27 -08:00
|
|
|
#include "oops/symbol.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "prims/jvm_misc.hpp"
|
2020-11-12 01:45:27 +00:00
|
|
|
#include "prims/jvmtiExport.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "prims/nativeLookup.hpp"
|
2016-03-03 09:33:30 -08:00
|
|
|
#include "prims/unsafe.hpp"
|
2020-11-12 16:37:23 +00:00
|
|
|
#include "prims/scopedMemoryAccess.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/arguments.hpp"
|
|
|
|
#include "runtime/handles.inline.hpp"
|
2019-08-12 10:02:25 +02:00
|
|
|
#include "runtime/interfaceSupport.inline.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/javaCalls.hpp"
|
2019-01-31 10:31:39 +01:00
|
|
|
#include "runtime/os.inline.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/sharedRuntime.hpp"
|
|
|
|
#include "runtime/signature.hpp"
|
2013-01-23 13:02:39 -05:00
|
|
|
#include "utilities/macros.hpp"
|
2019-03-30 08:26:20 -07:00
|
|
|
#include "utilities/utf8.hpp"
|
2018-05-15 20:24:34 +02:00
|
|
|
#if INCLUDE_JFR
|
|
|
|
#include "jfr/jfr.hpp"
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com>
Co-authored-by: Christian Tornqvist <christian.tornqvist@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: George Triantafillou <george.triantafillou@oracle.com>
Co-authored-by: Igor Ignatyev <igor.ignatyev@oracle.com>
Co-authored-by: Ioi Lam <ioi.lam@oracle.com>
Co-authored-by: James Laskey <james.laskey@oracle.com>
Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com>
Co-authored-by: Jiangli Zhou <jiangli.zhou@oracle.com>
Co-authored-by: Markus Gronlund <markus.gronlund@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Reviewed-by: acorn, ccheung, coleenp, ctornqvi, dholmes, dsimms, gtriantafill, iklam, jiangli, mgronlun, mseledtsov, cjplummer, sspitsyn, stefank, twisti, hseigel, lfoltan, alanb, mchung, dfazunen
2016-03-17 19:04:01 +00:00
|
|
|
#endif
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2020-05-13 03:33:21 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
The JNI specification defines the mapping from a Java native method name to
|
|
|
|
a C native library implementation function name as follows:
|
|
|
|
|
|
|
|
The mapping produces a native method name by concatenating the following components
|
|
|
|
derived from a `native` method declaration:
|
|
|
|
|
|
|
|
1. the prefix Java_
|
|
|
|
2. given the binary name, in internal form, of the class which declares the native method:
|
|
|
|
the result of escaping the name.
|
|
|
|
3. an underscore ("_")
|
|
|
|
4. the escaped method name
|
|
|
|
5. if the native method declaration is overloaded: two underscores ("__") followed by the
|
|
|
|
escaped parameter descriptor (JVMS 4.3.3) of the method declaration.
|
|
|
|
|
|
|
|
Escaping leaves every alphanumeric ASCII character (A-Za-z0-9) unchanged, and replaces each
|
|
|
|
UTF-16 code unit n the table below with the corresponding escape sequence. If the name to be
|
|
|
|
escaped contains a surrogate pair, then the high-surrogate code unit and the low-surrogate code
|
|
|
|
unit are escaped separately. The result of escaping is a string consisting only of the ASCII
|
|
|
|
characters A-Za-z0-9 and underscore.
|
|
|
|
|
|
|
|
------------------------------ ------------------------------------
|
|
|
|
UTF-16 code unit Escape sequence
|
|
|
|
------------------------------ ------------------------------------
|
|
|
|
Forward slash (/, U+002F) _
|
|
|
|
Underscore (_, U+005F) _1
|
|
|
|
Semicolon (;, U+003B) _2
|
|
|
|
Left square bracket ([, U+005B) _3
|
|
|
|
Any UTF-16 code unit \u_WXYZ_ that does not _0wxyz where w, x, y, and z are the lower-case
|
|
|
|
represent alphanumeric ASCII (A-Za-z0-9), forms of the hexadecimal digits W, X, Y, and Z.
|
|
|
|
forward slash, underscore, semicolon, (For example, U+ABCD becomes _0abcd.)
|
|
|
|
or left square bracket
|
|
|
|
------------------------------ ------------------------------------
|
|
|
|
|
|
|
|
Note that escape sequences can safely begin _0, _1, etc, because class and method
|
|
|
|
names in Java source code never begin with a number. However, that is not the case in
|
|
|
|
class files that were not generated from Java source code.
|
|
|
|
|
|
|
|
To preserve the 1:1 mapping to a native method name, the VM checks the resulting name as
|
|
|
|
follows. If the process of escaping any precursor string from the native method declaration
|
|
|
|
(class or method name, or argument type) causes a "0", "1", "2", or "3" character
|
|
|
|
from the precursor string to appear unchanged in the result *either* immediately after an
|
|
|
|
underscore *or* at the beginning of the escaped string (where it will follow an underscore
|
|
|
|
in the fully assembled name), then the escaping process is said to have "failed".
|
|
|
|
In such cases, no native library search is performed, and the attempt to link the native
|
|
|
|
method invocation will throw UnsatisfiedLinkError.
|
|
|
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
package/my_class/method
|
|
|
|
|
|
|
|
and
|
|
|
|
|
|
|
|
package/my/1class/method
|
|
|
|
|
|
|
|
both map to
|
|
|
|
|
|
|
|
Java_package_my_1class_method
|
|
|
|
|
|
|
|
To address this potential conflict we need only check if the character after
|
|
|
|
/ is a digit 0..3, or if the first character after an injected '_' seperator
|
|
|
|
is a digit 0..3. If we encounter an invalid identifier we reset the
|
|
|
|
stringStream and return false. Otherwise the stringStream contains the mapped
|
|
|
|
name and we return true.
|
|
|
|
|
|
|
|
*/
|
|
|
|
static bool map_escaped_name_on(stringStream* st, Symbol* name, int begin, int end) {
|
2007-12-01 00:00:00 +00:00
|
|
|
char* bytes = (char*)name->bytes() + begin;
|
|
|
|
char* end_bytes = (char*)name->bytes() + end;
|
2020-05-13 03:33:21 -04:00
|
|
|
bool check_escape_char = true; // initially true as first character here follows '_'
|
2007-12-01 00:00:00 +00:00
|
|
|
while (bytes < end_bytes) {
|
|
|
|
jchar c;
|
|
|
|
bytes = UTF8::next(bytes, &c);
|
|
|
|
if (c <= 0x7f && isalnum(c)) {
|
2020-05-13 03:33:21 -04:00
|
|
|
if (check_escape_char && (c >= '0' && c <= '3')) {
|
|
|
|
// This is a non-Java identifier and we won't escape it to
|
|
|
|
// ensure no name collisions with a Java identifier.
|
|
|
|
if (log_is_enabled(Debug, jni, resolve)) {
|
|
|
|
ResourceMark rm;
|
|
|
|
log_debug(jni, resolve)("[Lookup of native method with non-Java identifier rejected: %s]",
|
|
|
|
name->as_C_string());
|
|
|
|
}
|
|
|
|
st->reset(); // restore to "" on error
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
st->put((char) c);
|
2020-05-13 03:33:21 -04:00
|
|
|
check_escape_char = false;
|
2007-12-01 00:00:00 +00:00
|
|
|
} else {
|
2020-05-13 03:33:21 -04:00
|
|
|
check_escape_char = false;
|
|
|
|
if (c == '_') st->print("_1");
|
|
|
|
else if (c == '/') {
|
|
|
|
st->print("_");
|
|
|
|
// Following a / we must have non-escape character
|
|
|
|
check_escape_char = true;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
else if (c == ';') st->print("_2");
|
|
|
|
else if (c == '[') st->print("_3");
|
|
|
|
else st->print("_%.5x", c);
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 03:33:21 -04:00
|
|
|
return true;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 03:33:21 -04:00
|
|
|
static bool map_escaped_name_on(stringStream* st, Symbol* name) {
|
|
|
|
return map_escaped_name_on(st, name, 0, name->utf8_length());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
char* NativeLookup::pure_jni_name(const methodHandle& method) {
|
2007-12-01 00:00:00 +00:00
|
|
|
stringStream st;
|
|
|
|
// Prefix
|
|
|
|
st.print("Java_");
|
|
|
|
// Klass name
|
2020-05-13 03:33:21 -04:00
|
|
|
if (!map_escaped_name_on(&st, method->klass_name())) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
st.print("_");
|
|
|
|
// Method name
|
2020-05-13 03:33:21 -04:00
|
|
|
if (!map_escaped_name_on(&st, method->name())) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
return st.as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
char* NativeLookup::critical_jni_name(const methodHandle& method) {
|
2012-02-01 16:57:08 -08:00
|
|
|
stringStream st;
|
|
|
|
// Prefix
|
|
|
|
st.print("JavaCritical_");
|
|
|
|
// Klass name
|
2020-05-13 03:33:21 -04:00
|
|
|
if (!map_escaped_name_on(&st, method->klass_name())) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
st.print("_");
|
|
|
|
// Method name
|
2020-05-13 03:33:21 -04:00
|
|
|
if (!map_escaped_name_on(&st, method->name())) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
return st.as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
char* NativeLookup::long_jni_name(const methodHandle& method) {
|
2020-05-13 03:33:21 -04:00
|
|
|
// Signatures ignore the wrapping parentheses and the trailing return type
|
2007-12-01 00:00:00 +00:00
|
|
|
stringStream st;
|
2011-01-27 16:11:27 -08:00
|
|
|
Symbol* signature = method->signature();
|
2007-12-01 00:00:00 +00:00
|
|
|
st.print("__");
|
|
|
|
// find ')'
|
|
|
|
int end;
|
2019-10-21 13:13:16 -04:00
|
|
|
for (end = 0; end < signature->utf8_length() && signature->char_at(end) != JVM_SIGNATURE_ENDFUNC; end++);
|
2007-12-01 00:00:00 +00:00
|
|
|
// skip first '('
|
2020-05-13 03:33:21 -04:00
|
|
|
if (!map_escaped_name_on(&st, signature, 1, end)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
return st.as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
2009-04-08 10:56:49 -07:00
|
|
|
void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls);
|
2020-11-23 11:00:38 +00:00
|
|
|
void JNICALL JVM_RegisterReferencesMethods(JNIEnv *env, jclass unsafecls);
|
|
|
|
void JNICALL JVM_RegisterUpcallHandlerMethods(JNIEnv *env, jclass unsafecls);
|
|
|
|
void JNICALL JVM_RegisterProgrammableUpcallHandlerMethods(JNIEnv *env, jclass unsafecls);
|
|
|
|
void JNICALL JVM_RegisterProgrammableInvokerMethods(JNIEnv *env, jclass unsafecls);
|
|
|
|
void JNICALL JVM_RegisterNativeEntryPointMethods(JNIEnv *env, jclass unsafecls);
|
2007-12-01 00:00:00 +00:00
|
|
|
void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
|
2012-02-23 14:58:35 +01:00
|
|
|
void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass);
|
2020-10-14 20:02:46 +00:00
|
|
|
void JNICALL JVM_RegisterVectorSupportMethods(JNIEnv *env, jclass vsclass);
|
2015-10-08 12:49:30 -10:00
|
|
|
#if INCLUDE_JVMCI
|
|
|
|
jobject JNICALL JVM_GetJVMCIRuntime(JNIEnv *env, jclass c);
|
|
|
|
void JNICALL JVM_RegisterJVMCINatives(JNIEnv *env, jclass compilerToVMClass);
|
|
|
|
#endif
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 22:33:47 -08:00
|
|
|
#define CC (char*) /* cast a literal from (const char*) */
|
|
|
|
#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
|
|
|
|
|
|
|
|
static JNINativeMethod lookup_special_native_methods[] = {
|
2015-12-09 14:54:40 +01:00
|
|
|
{ CC"Java_jdk_internal_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterJDKInternalMiscUnsafeMethods) },
|
2011-03-11 22:34:57 -08:00
|
|
|
{ CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) },
|
2020-11-23 11:00:38 +00:00
|
|
|
{ CC"Java_jdk_internal_foreign_abi_UpcallStubs_registerNatives", NULL, FN_PTR(JVM_RegisterUpcallHandlerMethods) },
|
|
|
|
{ CC"Java_jdk_internal_foreign_abi_ProgrammableUpcallHandler_registerNatives", NULL, FN_PTR(JVM_RegisterProgrammableUpcallHandlerMethods) },
|
|
|
|
{ CC"Java_jdk_internal_foreign_abi_ProgrammableInvoker_registerNatives", NULL, FN_PTR(JVM_RegisterProgrammableInvokerMethods) },
|
|
|
|
{ CC"Java_jdk_internal_invoke_NativeEntryPoint_registerNatives", NULL, FN_PTR(JVM_RegisterNativeEntryPointMethods) },
|
2016-01-13 14:34:11 +00:00
|
|
|
{ CC"Java_jdk_internal_perf_Perf_registerNatives", NULL, FN_PTR(JVM_RegisterPerfMethods) },
|
2012-02-23 14:58:35 +01:00
|
|
|
{ CC"Java_sun_hotspot_WhiteBox_registerNatives", NULL, FN_PTR(JVM_RegisterWhiteBoxMethods) },
|
2020-10-14 20:02:46 +00:00
|
|
|
{ CC"Java_jdk_internal_vm_vector_VectorSupport_registerNatives", NULL, FN_PTR(JVM_RegisterVectorSupportMethods)},
|
2015-10-08 12:49:30 -10:00
|
|
|
#if INCLUDE_JVMCI
|
|
|
|
{ CC"Java_jdk_vm_ci_runtime_JVMCI_initializeRuntime", NULL, FN_PTR(JVM_GetJVMCIRuntime) },
|
|
|
|
{ CC"Java_jdk_vm_ci_hotspot_CompilerToVM_registerNatives", NULL, FN_PTR(JVM_RegisterJVMCINatives) },
|
|
|
|
#endif
|
2018-05-15 20:24:34 +02:00
|
|
|
#if INCLUDE_JFR
|
|
|
|
{ CC"Java_jdk_jfr_internal_JVM_registerNatives", NULL, FN_PTR(jfr_register_natives) },
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com>
Co-authored-by: Christian Tornqvist <christian.tornqvist@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: George Triantafillou <george.triantafillou@oracle.com>
Co-authored-by: Igor Ignatyev <igor.ignatyev@oracle.com>
Co-authored-by: Ioi Lam <ioi.lam@oracle.com>
Co-authored-by: James Laskey <james.laskey@oracle.com>
Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com>
Co-authored-by: Jiangli Zhou <jiangli.zhou@oracle.com>
Co-authored-by: Markus Gronlund <markus.gronlund@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Reviewed-by: acorn, ccheung, coleenp, ctornqvi, dholmes, dsimms, gtriantafill, iklam, jiangli, mgronlun, mseledtsov, cjplummer, sspitsyn, stefank, twisti, hseigel, lfoltan, alanb, mchung, dfazunen
2016-03-17 19:04:01 +00:00
|
|
|
#endif
|
2020-11-12 16:37:23 +00:00
|
|
|
{ CC"Java_jdk_internal_misc_ScopedMemoryAccess_registerNatives", NULL, FN_PTR(JVM_RegisterJDKInternalMiscScopedMemoryAccessMethods) },
|
2011-03-11 22:33:47 -08:00
|
|
|
};
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
static address lookup_special_native(const char* jni_name) {
|
2011-03-11 22:33:47 -08:00
|
|
|
int count = sizeof(lookup_special_native_methods) / sizeof(JNINativeMethod);
|
2013-10-02 09:18:56 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-11 22:33:47 -08:00
|
|
|
// NB: To ignore the jni prefix and jni postfix strstr is used matching.
|
|
|
|
if (strstr(jni_name, lookup_special_native_methods[i].name) != NULL) {
|
|
|
|
return CAST_FROM_FN_PTR(address, lookup_special_native_methods[i].fnPtr);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup_style(const methodHandle& method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
|
2007-12-01 00:00:00 +00:00
|
|
|
address entry;
|
2019-07-31 08:05:14 +02:00
|
|
|
const char* jni_name = compute_complete_jni_name(pure_name, long_name, args_size, os_style);
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// If the loader is null we have a system class, so we attempt a lookup in
|
|
|
|
// the native Java library. This takes care of any bootstrapping problems.
|
2020-03-12 11:54:11 -07:00
|
|
|
// Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_findNative
|
2007-12-01 00:00:00 +00:00
|
|
|
// gets found the first time around - otherwise an infinite loop can occure. This is
|
|
|
|
// another VM/library dependency
|
2012-11-06 15:09:37 -05:00
|
|
|
Handle loader(THREAD, method->method_holder()->class_loader());
|
2007-12-01 00:00:00 +00:00
|
|
|
if (loader.is_null()) {
|
|
|
|
entry = lookup_special_native(jni_name);
|
|
|
|
if (entry == NULL) {
|
2010-12-01 18:26:32 -05:00
|
|
|
entry = (address) os::dll_lookup(os::native_java_library(), jni_name);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
if (entry != NULL) {
|
|
|
|
in_base_library = true;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise call static method findNative in ClassLoader
|
2017-03-15 10:25:37 -04:00
|
|
|
Klass* klass = SystemDictionary::ClassLoader_klass();
|
2007-12-01 00:00:00 +00:00
|
|
|
Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
|
|
|
|
|
|
|
|
JavaValue result(T_LONG);
|
|
|
|
JavaCalls::call_static(&result,
|
|
|
|
klass,
|
2011-01-27 16:11:27 -08:00
|
|
|
vmSymbols::findNative_name(),
|
|
|
|
vmSymbols::classloader_string_long_signature(),
|
2007-12-01 00:00:00 +00:00
|
|
|
// Arguments
|
|
|
|
loader,
|
|
|
|
name_arg,
|
|
|
|
CHECK_NULL);
|
|
|
|
entry = (address) (intptr_t) result.get_jlong();
|
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
// findNative didn't find it, if there are any agent libraries look in them
|
|
|
|
AgentLibrary* agent;
|
|
|
|
for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
|
2010-12-01 18:26:32 -05:00
|
|
|
entry = (address) os::dll_lookup(agent->os_lib(), jni_name);
|
2007-12-01 00:00:00 +00:00
|
|
|
if (entry != NULL) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
const char* NativeLookup::compute_complete_jni_name(const char* pure_name, const char* long_name, int args_size, bool os_style) {
|
|
|
|
stringStream st;
|
|
|
|
if (os_style) {
|
|
|
|
os::print_jni_name_prefix_on(&st, args_size);
|
2012-02-01 16:57:08 -08:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
st.print_raw(pure_name);
|
|
|
|
st.print_raw(long_name);
|
|
|
|
if (os_style) {
|
|
|
|
os::print_jni_name_suffix_on(&st, args_size);
|
2012-02-01 16:57:08 -08:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
return st.as_string();
|
2012-02-01 16:57:08 -08:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
address NativeLookup::lookup_critical_style(void* dll, const char* pure_name, const char* long_name, int args_size, bool os_style) {
|
|
|
|
const char* jni_name = compute_complete_jni_name(pure_name, long_name, args_size, os_style);
|
|
|
|
assert(dll != NULL, "dll must be loaded");
|
|
|
|
return (address)os::dll_lookup(dll, jni_name);
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Check all the formats of native implementation name to see if there is one
|
|
|
|
// for the specified method.
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup_entry(const methodHandle& method, bool& in_base_library, TRAPS) {
|
2007-12-01 00:00:00 +00:00
|
|
|
address entry = NULL;
|
|
|
|
in_base_library = false;
|
|
|
|
// Compute pure name
|
|
|
|
char* pure_name = pure_jni_name(method);
|
2020-05-13 03:33:21 -04:00
|
|
|
if (pure_name == NULL) {
|
|
|
|
// JNI name mapping rejected this method so return
|
|
|
|
// NULL to indicate UnsatisfiedLinkError should be thrown.
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Compute argument size
|
|
|
|
int args_size = 1 // JNIEnv
|
|
|
|
+ (method->is_static() ? 1 : 0) // class for static methods
|
|
|
|
+ method->size_of_parameters(); // actual parameters
|
|
|
|
|
|
|
|
// 1) Try JNI short style
|
|
|
|
entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL);
|
|
|
|
if (entry != NULL) return entry;
|
|
|
|
|
|
|
|
// Compute long name
|
|
|
|
char* long_name = long_jni_name(method);
|
2020-05-13 03:33:21 -04:00
|
|
|
if (long_name == NULL) {
|
|
|
|
// JNI name mapping rejected this method so return
|
|
|
|
// NULL to indicate UnsatisfiedLinkError should be thrown.
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// 2) Try JNI long style
|
|
|
|
entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL);
|
|
|
|
if (entry != NULL) return entry;
|
|
|
|
|
|
|
|
// 3) Try JNI short style without os prefix/suffix
|
|
|
|
entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL);
|
|
|
|
if (entry != NULL) return entry;
|
|
|
|
|
|
|
|
// 4) Try JNI long style without os prefix/suffix
|
|
|
|
entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
|
|
|
|
|
|
|
|
return entry; // NULL indicates not found
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:57:08 -08:00
|
|
|
// Check all the formats of native implementation name to see if there is one
|
|
|
|
// for the specified method.
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup_critical_entry(const methodHandle& method) {
|
2019-08-12 10:02:25 +02:00
|
|
|
assert(CriticalJNINatives, "or should not be here");
|
2012-02-01 16:57:08 -08:00
|
|
|
|
|
|
|
if (method->is_synchronized() ||
|
|
|
|
!method->is_static()) {
|
|
|
|
// Only static non-synchronized methods are allowed
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceMark rm;
|
|
|
|
|
|
|
|
Symbol* signature = method->signature();
|
|
|
|
for (int end = 0; end < signature->utf8_length(); end++) {
|
2018-10-03 09:46:46 -04:00
|
|
|
if (signature->char_at(end) == 'L') {
|
2012-02-01 16:57:08 -08:00
|
|
|
// Don't allow object types
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute argument size
|
2017-11-06 00:30:36 -08:00
|
|
|
int args_size = method->size_of_parameters();
|
|
|
|
for (SignatureStream ss(signature); !ss.at_return_type(); ss.next()) {
|
|
|
|
if (ss.is_array()) {
|
|
|
|
args_size += T_INT_size; // array length parameter
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
2019-08-12 10:02:25 +02:00
|
|
|
// dll handling requires I/O. Don't do that while in _thread_in_vm (safepoint may get requested).
|
|
|
|
ThreadToNativeFromVM thread_in_native(JavaThread::current());
|
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
void* dll = dll_load(method);
|
|
|
|
address entry = NULL;
|
|
|
|
|
|
|
|
if (dll != NULL) {
|
|
|
|
entry = lookup_critical_style(dll, method, args_size);
|
|
|
|
// Close the handle to avoid keeping the library alive if the native method holder is unloaded.
|
|
|
|
// This is fine because the library is still kept alive by JNI (see JVM_LoadLibrary). As soon
|
|
|
|
// as the holder class and the library are unloaded (see JVM_UnloadLibrary), the native wrapper
|
|
|
|
// that calls 'critical_entry' becomes unreachable and is unloaded as well.
|
|
|
|
os::dll_unload(dll);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry; // NULL indicates not found
|
|
|
|
}
|
|
|
|
|
|
|
|
void* NativeLookup::dll_load(const methodHandle& method) {
|
|
|
|
if (method->has_native_function()) {
|
|
|
|
|
|
|
|
address current_entry = method->native_function();
|
|
|
|
|
|
|
|
char dll_name[JVM_MAXPATHLEN];
|
|
|
|
int offset;
|
|
|
|
if (os::dll_address_to_library_name(current_entry, dll_name, sizeof(dll_name), &offset)) {
|
|
|
|
char ebuf[32];
|
|
|
|
return os::dll_load(dll_name, ebuf, sizeof(ebuf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
address NativeLookup::lookup_critical_style(void* dll, const methodHandle& method, int args_size) {
|
|
|
|
address entry = NULL;
|
|
|
|
const char* critical_name = critical_jni_name(method);
|
2020-05-13 03:33:21 -04:00
|
|
|
if (critical_name == NULL) {
|
|
|
|
// JNI name mapping rejected this method so return
|
|
|
|
// NULL to indicate UnsatisfiedLinkError should be thrown.
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-31 08:05:14 +02:00
|
|
|
|
2012-02-01 16:57:08 -08:00
|
|
|
// 1) Try JNI short style
|
2019-07-31 08:05:14 +02:00
|
|
|
entry = lookup_critical_style(dll, critical_name, "", args_size, true);
|
|
|
|
if (entry != NULL) {
|
|
|
|
return entry;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
2019-07-31 08:05:14 +02:00
|
|
|
const char* long_name = long_jni_name(method);
|
2020-05-13 03:33:21 -04:00
|
|
|
if (long_name == NULL) {
|
|
|
|
// JNI name mapping rejected this method so return
|
|
|
|
// NULL to indicate UnsatisfiedLinkError should be thrown.
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
|
|
|
// 2) Try JNI long style
|
2019-07-31 08:05:14 +02:00
|
|
|
entry = lookup_critical_style(dll, critical_name, long_name, args_size, true);
|
|
|
|
if (entry != NULL) {
|
|
|
|
return entry;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
|
|
|
// 3) Try JNI short style without os prefix/suffix
|
2019-07-31 08:05:14 +02:00
|
|
|
entry = lookup_critical_style(dll, critical_name, "", args_size, false);
|
|
|
|
if (entry != NULL) {
|
|
|
|
return entry;
|
|
|
|
}
|
2012-02-01 16:57:08 -08:00
|
|
|
|
|
|
|
// 4) Try JNI long style without os prefix/suffix
|
2019-07-31 08:05:14 +02:00
|
|
|
return lookup_critical_style(dll, critical_name, long_name, args_size, false);
|
2012-02-01 16:57:08 -08:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Check if there are any JVM TI prefixes which have been applied to the native method name.
|
|
|
|
// If any are found, remove them before attemping the look up of the
|
|
|
|
// native implementation again.
|
|
|
|
// See SetNativeMethodPrefix in the JVM TI Spec for more details.
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup_entry_prefixed(const methodHandle& method, bool& in_base_library, TRAPS) {
|
2012-10-10 14:35:58 -04:00
|
|
|
#if INCLUDE_JVMTI
|
2007-12-01 00:00:00 +00:00
|
|
|
ResourceMark rm(THREAD);
|
|
|
|
|
|
|
|
int prefix_count;
|
|
|
|
char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
|
|
|
|
char* in_name = method->name()->as_C_string();
|
|
|
|
char* wrapper_name = in_name;
|
|
|
|
// last applied prefix will be first -- go backwards
|
|
|
|
for (int i = prefix_count-1; i >= 0; i--) {
|
|
|
|
char* prefix = prefixes[i];
|
|
|
|
size_t prefix_len = strlen(prefix);
|
|
|
|
if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
|
|
|
|
// has this prefix remove it
|
|
|
|
wrapper_name += prefix_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wrapper_name != in_name) {
|
|
|
|
// we have a name for a wrapping method
|
|
|
|
int wrapper_name_len = (int)strlen(wrapper_name);
|
2011-01-27 16:11:27 -08:00
|
|
|
TempNewSymbol wrapper_symbol = SymbolTable::probe(wrapper_name, wrapper_name_len);
|
|
|
|
if (wrapper_symbol != NULL) {
|
2017-03-15 10:25:37 -04:00
|
|
|
Klass* k = method->method_holder();
|
|
|
|
Method* wrapper_method = k->lookup_method(wrapper_symbol, method->signature());
|
2007-12-01 00:00:00 +00:00
|
|
|
if (wrapper_method != NULL && !wrapper_method->is_native()) {
|
|
|
|
// we found a wrapper method, use its native entry
|
|
|
|
method->set_is_prefixed_native();
|
2019-11-13 08:23:23 -05:00
|
|
|
return lookup_entry(methodHandle(THREAD, wrapper_method), in_base_library, THREAD);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-10 14:35:58 -04:00
|
|
|
#endif // INCLUDE_JVMTI
|
2007-12-01 00:00:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup_base(const methodHandle& method, bool& in_base_library, TRAPS) {
|
2007-12-01 00:00:00 +00:00
|
|
|
address entry = NULL;
|
|
|
|
ResourceMark rm(THREAD);
|
|
|
|
|
|
|
|
entry = lookup_entry(method, in_base_library, THREAD);
|
|
|
|
if (entry != NULL) return entry;
|
|
|
|
|
|
|
|
// standard native method resolution has failed. Check if there are any
|
|
|
|
// JVM TI prefixes which have been applied to the native method name.
|
|
|
|
entry = lookup_entry_prefixed(method, in_base_library, THREAD);
|
|
|
|
if (entry != NULL) return entry;
|
|
|
|
|
|
|
|
// Native function not found, throw UnsatisfiedLinkError
|
2019-04-04 09:39:44 +02:00
|
|
|
stringStream ss;
|
|
|
|
ss.print("'");
|
|
|
|
method->print_external_name(&ss);
|
|
|
|
ss.print("'");
|
|
|
|
THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(), ss.as_string());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 16:48:38 -04:00
|
|
|
address NativeLookup::lookup(const methodHandle& method, bool& in_base_library, TRAPS) {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (!method->has_native_function()) {
|
2013-05-06 13:53:13 -07:00
|
|
|
address entry = lookup_base(method, in_base_library, CHECK_NULL);
|
2007-12-01 00:00:00 +00:00
|
|
|
method->set_native_function(entry,
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
Method::native_bind_event_is_interesting);
|
2007-12-01 00:00:00 +00:00
|
|
|
// -verbose:jni printing
|
2019-11-11 12:11:34 -05:00
|
|
|
if (log_is_enabled(Debug, jni, resolve)) {
|
2007-12-01 00:00:00 +00:00
|
|
|
ResourceMark rm(THREAD);
|
2019-11-11 12:11:34 -05:00
|
|
|
log_debug(jni, resolve)("[Dynamic-linking native method %s.%s ... JNI]",
|
|
|
|
method->method_holder()->external_name(),
|
|
|
|
method->name()->as_C_string());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return method->native_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
|
|
|
|
EXCEPTION_MARK;
|
|
|
|
bool in_base_library = true; // SharedRuntime inits some math methods.
|
2019-05-14 11:29:18 -04:00
|
|
|
TempNewSymbol c_name = SymbolTable::new_symbol(class_name);
|
|
|
|
TempNewSymbol m_name = SymbolTable::new_symbol(method_name);
|
|
|
|
TempNewSymbol s_name = SymbolTable::new_symbol(signature);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Find the class
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
Klass* k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
|
2017-03-15 10:25:37 -04:00
|
|
|
InstanceKlass* klass = InstanceKlass::cast(k);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Find method and invoke standard lookup
|
|
|
|
methodHandle method (THREAD,
|
2020-08-06 18:13:56 +00:00
|
|
|
klass->uncached_lookup_method(m_name, s_name, Klass::OverpassLookupMode::find));
|
2007-12-01 00:00:00 +00:00
|
|
|
address result = lookup(method, in_base_library, CATCH);
|
|
|
|
assert(in_base_library, "must be in basic library");
|
|
|
|
guarantee(result != NULL, "must be non NULL");
|
|
|
|
return result;
|
|
|
|
}
|