8159145: Add JVMTI function GetNamedModule

Introduce function GetNamedModule with a jtreg test coverage

Reviewed-by: alanb, ctornqvi, hseigel, jiangli, dholmes, dcubed
This commit is contained in:
Serguei Spitsyn 2016-07-09 19:20:38 -07:00
parent 5296c23c9b
commit 2cede6dbc1
7 changed files with 572 additions and 7 deletions

View File

@ -51,6 +51,7 @@ BUILD_HOTSPOT_JTREG_NATIVE_SRC := \
$(HOTSPOT_TOPDIR)/test/compiler/floatingpoint/ \
$(HOTSPOT_TOPDIR)/test/compiler/calls \
$(HOTSPOT_TOPDIR)/test/compiler/native \
$(HOTSPOT_TOPDIR)/test/serviceability/jvmti/GetNamedModule \
$(HOTSPOT_TOPDIR)/test/testlibrary/jvmti \
#
@ -64,6 +65,7 @@ endif
ifeq ($(TOOLCHAIN_TYPE), solstudio)
BUILD_HOTSPOT_JTREG_LIBRARIES_LDFLAGS_liboverflow := -lc
BUILD_HOTSPOT_JTREG_LIBRARIES_LDFLAGS_libSimpleClassFileLoadHook := -lc
BUILD_HOTSPOT_JTREG_LIBRARIES_LDFLAGS_libGetNamedModuleTest := -lc
endif
BUILD_HOTSPOT_JTREG_OUTPUT_DIR := $(BUILD_OUTPUT)/support/test/hotspot/jtreg/native

View File

@ -820,6 +820,28 @@ jobject Modules::get_module_by_package_name(jobject loader, jstring package, TRA
}
jobject Modules::get_named_module(Handle h_loader, const char* package_str, TRAPS) {
assert(ModuleEntryTable::javabase_defined(),
"Attempt to call get_named_module before java.base is defined");
assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
"Class loader is not a subclass of java.lang.ClassLoader");
assert(package_str != NULL, "the package_str should not be NULL");
if (strlen(package_str) == 0) {
return NULL;
}
TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL);
const PackageEntry* const pkg_entry =
get_package_entry_by_name(package_sym, h_loader, THREAD);
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) {
return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
}
return NULL;
}
// This method is called by JFR and by the above method.
jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) {
const PackageEntry* const pkg_entry =

View File

@ -121,6 +121,7 @@ public:
// IllegalArgumentException is thrown if loader is neither null nor a subtype of
// java/lang/ClassLoader.
static jobject get_module_by_package_name(jobject loader, jstring package, TRAPS);
static jobject get_named_module(Handle h_loader, const char* package, TRAPS);
// If package is defined by loader, return the
// java.lang.reflect.Module object for the module in which the package is defined.

View File

@ -6509,6 +6509,59 @@ class C2 extends C1 implements I2 {
<errors>
</errors>
</function>
<function id="GetNamedModule" num="40" since="9">
<synopsis>Get Named Module</synopsis>
<description>
Return the <code>java.lang.reflect.Module</code> object for a named
module defined to a class loader that contains a given package.
The module is returned via <code>module_ptr</code>.
<p/>
If a named module is defined to the class loader and it
contains the package then that named module is returned,
otherwise <code>NULL</code> is returned.
<p/>
</description>
<origin>new</origin>
<capabilities>
</capabilities>
<parameters>
<param id="class_loader">
<ptrtype>
<jobject/>
<nullok>the bootstrap loader is assumed</nullok>
</ptrtype>
<description>
A class loader.
If the <code>class_loader</code> is not <code>NULL</code>
or a subclass of <code>java.lang.ClassLoader</code>
this function returns
<errorlink id="JVMTI_ERROR_ILLEGAL_ARGUMENT"></errorlink>.
</description>
</param>
<param id="package_name">
<inbuf><char/></inbuf>
<description>
The name of the package, encoded as a
<internallink id="mUTF">modified UTF-8</internallink> string.
The package name is in internal form (JVMS 4.2.1);
identifiers are separated by forward slashes rather than periods.
</description>
</param>
<param id="module_ptr">
<outptr><jobject/></outptr>
<description>
On return, points to a <code>java.lang.reflect.Module</code> object
or points to <code>NULL</code>.
</description>
</param>
</parameters>
<errors>
<error id="JVMTI_ERROR_ILLEGAL_ARGUMENT">
If class loader is not <code>NULL</code> and is not a class loader object.
</error>
</errors>
</function>
</category>
<category id="class" label="Class">
@ -12462,6 +12515,14 @@ myInit() {
<code>new_class_data</code> has been set, it becomes the
<code>class_data</code> for the next agent.
<p/>
When handling a class load in the live phase, then the
<functionlink id="GetNamedModule"></functionlink>
function can be used to map class loader and a package name to a module.
When a class is being redefined or retransformed then
<code>class_being_redefined</code> is non <code>NULL</code> and so
the JNI <code>GetModule</code> function can also be used
to obtain the Module.
<p/>
The order that this event is sent to each environment differs
from other events.
This event is sent to environments in the following order:
@ -14427,20 +14488,15 @@ typedef void (JNICALL *jvmtiEventVMInit)
<change date="19 June 2013" version="1.2.3">
Added support for statically linked agents.
</change>
<change date="20 January 2016" version="9.0.0">
<change date="5 July 2016" version="9.0.0">
Support for modules:
- The majorversion is 9 now
- The ClassFileLoadHook events are not sent during the primordial phase anymore.
- Add new function GetAllModules
</change>
<change date="17 February 2016" version="9.0.0">
Support for modules:
- Add new capability can_generate_early_vmstart
- Allow CompiledMethodLoad events at start phase
</change>
<change date="14 April 2016" version="9.0.0">
Support for modules:
- Add new capability can_generate_early_class_hook_events
- Add new function GetNamedModule
</change>
</changehistory>

View File

@ -24,6 +24,7 @@
#include "precompiled.hpp"
#include "classfile/classLoaderExt.hpp"
#include "classfile/modules.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/bytecodeStream.hpp"
@ -201,6 +202,28 @@ JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
} /* end GetAllModules */
// class_loader - NULL is a valid value, must be pre-checked
// package_name - pre-checked for NULL
// module_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
JavaThread* THREAD = JavaThread::current(); // pass to macros
ResourceMark rm(THREAD);
Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
// Check that loader is a subclass of java.lang.ClassLoader.
if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
jobject module = Modules::get_named_module(h_loader, package_name, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
return JVMTI_ERROR_INTERNAL; // unexpected exception
}
*module_ptr = module;
return JVMTI_ERROR_NONE;
} /* end GetNamedModule */
//
// Class functions
//

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* 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.
*
* 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.
*/
package MyPackage;
/**
* @test
* @summary Verifies the JVMTI GetNamedModule API
* @compile GetNamedModuleTest.java
* @run main/othervm/native -agentlib:GetNamedModuleTest MyPackage.GetNamedModuleTest
*/
import java.io.PrintStream;
public class GetNamedModuleTest {
static {
try {
System.loadLibrary("GetNamedModuleTest");
} catch (UnsatisfiedLinkError ule) {
System.err.println("Could not load GetNamedModuleTest library");
System.err.println("java.library.path: "
+ System.getProperty("java.library.path"));
throw ule;
}
}
native static int check();
public static void main(String args[]) {
int status = check();
if (status != 0) {
throw new RuntimeException("Non-zero status returned from the agent: " + status);
}
}
}

View File

@ -0,0 +1,405 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* 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.
*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include "jvmti.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef JNI_ENV_ARG
#ifdef __cplusplus
#define JNI_ENV_ARG(x, y) y
#define JNI_ENV_PTR(x) x
#else
#define JNI_ENV_ARG(x,y) x, y
#define JNI_ENV_PTR(x) (*x)
#endif
#endif
#define TranslateError(err) "JVMTI error"
#define PASSED 0
#define FAILED 2
static const char *EXC_CNAME = "java/lang/Exception";
static const char* MOD_CNAME = "Ljava/lang/reflect/Module;";
static jvmtiEnv *jvmti = NULL;
static jint result = PASSED;
static jboolean printdump = JNI_FALSE;
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
JNIEXPORT
jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT
jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT
jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
return JNI_VERSION_1_8;
}
static
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jint res;
if (options != NULL && strcmp(options, "printdump") == 0) {
printdump = JNI_TRUE;
}
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
JVMTI_VERSION_9);
if (res != JNI_OK || jvmti == NULL) {
printf(" Error: wrong result of a valid call to GetEnv!\n");
return JNI_ERR;
}
return JNI_OK;
}
static
jint throw_exc(JNIEnv *env, char *msg) {
jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));
if (exc_class == NULL) {
printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);
return -1;
}
return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
}
static
jobject get_class_loader(jclass cls) {
jvmtiError err = JVMTI_ERROR_NONE;
jobject loader = NULL;
if (printdump == JNI_TRUE) {
printf(">>> getting class loader ...\n");
}
err = (*jvmti)->GetClassLoader(jvmti, cls, &loader);
if (err != JVMTI_ERROR_NONE) {
printf(" Error in GetClassLoader: %s (%d)\n", TranslateError(err), err);
}
return loader;
}
static
jclass jlrM(JNIEnv *env) {
jclass cls = NULL;
cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, MOD_CNAME));
if (cls == NULL) {
printf(" Error in JNI FindClass: %s\n", MOD_CNAME);
}
return cls;
}
jmethodID
get_method(JNIEnv *env, jclass clazz, const char * name, const char *sig) {
jmethodID method = NULL;
method = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, clazz), name, sig);
if (method == NULL) {
printf(" Error in JNI GetMethodID %s with signature %s", name, sig);
}
return method;
}
static
jobject get_module_loader(JNIEnv *env, jobject module) {
static jmethodID cl_method = NULL;
jobject loader = NULL;
if (cl_method == NULL) {
cl_method = get_method(env, jlrM(env), "getClassLoader", "()Ljava/lang/ClassLoader;");
}
loader = (jobject)JNI_ENV_PTR(env)->CallObjectMethod(JNI_ENV_ARG(env, module), cl_method);
return loader;
}
static
const char* get_module_name(JNIEnv *env, jobject module) {
static jmethodID method = NULL;
jobject loader = NULL;
jstring jstr = NULL;
const char *name = NULL;
const char *nstr = NULL;
if (method == NULL) {
method = get_method(env, jlrM(env), "getName", "()Ljava/lang/String;");
}
jstr = (jstring)JNI_ENV_PTR(env)->CallObjectMethod(JNI_ENV_ARG(env, module), method);
if (jstr != NULL) {
name = JNI_ENV_PTR(env)->GetStringUTFChars(JNI_ENV_ARG(env, jstr), NULL);
}
loader = get_module_loader(env, module);
nstr = (name == NULL) ? "<UNNAMED>" : name;
printf(" loader: %p, module: %p, name: %s\n", loader, module, nstr);
return name;
}
static
jvmtiError get_module(JNIEnv *env,
jobject loader,
const char* pkg_name,
jobject* module_ptr,
const char** mod_name_ptr) {
jvmtiError err = JVMTI_ERROR_NONE;
const char* name = (pkg_name == NULL) ? "<NULL>" : pkg_name;
printf(">>> getting module by loader %p and package \"%s\"\n", loader, name);
*mod_name_ptr = NULL;
err = (*jvmti)->GetNamedModule(jvmti, loader, pkg_name, module_ptr);
if (err != JVMTI_ERROR_NONE) {
printf(" Error in GetNamedModule for package \"%s\": %s (%d)\n",
pkg_name, TranslateError(err), err);
return err;
}
printf(" returned module: %p\n", *module_ptr);
if (*module_ptr == NULL) { // named module was not found
return err;
}
*mod_name_ptr = get_module_name(env, *module_ptr);
return err;
}
static
jint get_all_modules(JNIEnv *env) {
jvmtiError err;
jint cnt = -1;
jint idx = 0;
jobject* modules;
printf(">>> Inspecting modules with GetAllModules\n");
err = (*jvmti)->GetAllModules(jvmti, &cnt, &modules);
if (err != JVMTI_ERROR_NONE) {
printf("Error in GetAllModules: %d\n", err);
return -1;
}
for (idx = 0; idx < cnt; ++idx) {
get_module_name(env, modules[idx]);
}
return cnt;
}
static
jint check_bad_loader(JNIEnv *env, jobject loader) {
jvmtiError err = JVMTI_ERROR_NONE;
jobject module = NULL;
const char* mod_name = NULL;
err = get_module(env, loader, "", &module, &mod_name);
if (err != JVMTI_ERROR_ILLEGAL_ARGUMENT) {
return FAILED;
}
printf(" got expected JVMTI_ERROR_ILLEGAL_ARGUMENT for bad loader\n");
return PASSED;
}
static
jint check_system_loader(JNIEnv *env, jobject loader) {
jvmtiError err = JVMTI_ERROR_NONE;
jobject module = NULL;
const char* exp_name = NULL;
const char* mod_name = NULL;
// NULL pointer for package name
err = get_module(env, loader, NULL, &module, &mod_name);
if (err != JVMTI_ERROR_NULL_POINTER) {
throw_exc(env, "check #SN1: failed to return JVMTI_ERROR_NULL_POINTER for NULL package");
return FAILED;
}
// NULL pointer for module_ptr
err = (*jvmti)->GetNamedModule(jvmti, loader, "", NULL);
if (err != JVMTI_ERROR_NULL_POINTER) {
throw_exc(env, "check #SN2: failed to return JVMTI_ERROR_NULL_POINTER for NULL module_ptr");
return FAILED;
}
// Unnamed/default package ""
err = get_module(env, loader, "", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #S1: failed to return JVMTI_ERROR_NONE for default package");
return FAILED;
}
if (module != NULL || mod_name != NULL) {
throw_exc(env, "check #S2: failed to return NULL-module for default package");
return FAILED;
}
// Test package: MyPackage
err = get_module(env, loader, "MyPackage", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #S3: failed to return JVMTI_ERROR_NONE for MyPackage");
return FAILED;
}
if (module != NULL || mod_name != NULL) {
throw_exc(env, "check #S4: failed to return NULL-module for MyPackage");
return FAILED;
}
// Package: com/sun/jdi
exp_name = "jdk.jdi";
err = get_module(env, loader, "com/sun/jdi", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #S5: failed to return JVMTI_ERROR_NONE for test package");
return FAILED;
}
if (module == NULL || mod_name == NULL) {
throw_exc(env, "check #S6: failed to return named module for com/sun/jdi package");
return FAILED;
}
if (strcmp(mod_name, exp_name) != 0) {
printf("check #S7: failed to return right module, expected: %s, returned: %s\n",
exp_name, mod_name);
throw_exc(env, "check #S7: failed to return jdk.jdi module for com/sun/jdi package");
return FAILED;
}
// Non-existing package: "bad/package/name"
err = get_module(env, loader, "bad/package/name", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #S8: failed to return JVMTI_ERROR_NONE for bad package");
return FAILED;
}
if (module != NULL || mod_name != NULL) {
throw_exc(env, "check #S9: failed to return NULL-module for bad package");
return FAILED;
}
return PASSED;
}
static
jint check_bootstrap_loader(JNIEnv *env, jobject loader) {
jvmtiError err = JVMTI_ERROR_NONE;
jobject module = NULL;
const char* exp_name = NULL;
const char* mod_name = NULL;
// NULL pointer for package name
err = get_module(env, loader, NULL, &module, &mod_name);
if (err != JVMTI_ERROR_NULL_POINTER) {
throw_exc(env, "check #BN1: failed to return JVMTI_ERROR_NULL_POINTER for NULL package");
return FAILED;
}
// NULL pointer for module_ptr
err = (*jvmti)->GetNamedModule(jvmti, loader, "", NULL);
if (err != JVMTI_ERROR_NULL_POINTER) {
throw_exc(env, "check #BN2: failed to return JVMTI_ERROR_NULL_POINTER for NULL module_ptr");
return FAILED;
}
// Unnamed/default package ""
err = get_module(env, loader, "", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #B1: failed to return JVMTI_ERROR_NONE for default package");
return FAILED;
}
if (module != NULL || mod_name != NULL) {
throw_exc(env, "check #B2: failed to return NULL-module for default package");
return FAILED;
}
// Normal package from java.base module: "java/lang"
exp_name = "java.base";
err = get_module(env, loader, "java/lang", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #B3: failed to return JVMTI_ERROR_NONE for java/lang package");
return FAILED;
}
if (module == NULL || mod_name == NULL) {
throw_exc(env, "check #B4: failed to return named module for java/lang package");
return FAILED;
}
if (strcmp(exp_name, mod_name) != 0) {
printf("check #B5: failed to return right module, expected: %s, returned: %s\n",
exp_name, mod_name);
throw_exc(env, "check #B5: failed to return expected module for java/lang package");
return FAILED;
}
// Non-existing package: "bad/package/name"
err = get_module(env, loader, "bad/package/name", &module, &mod_name);
if (err != JVMTI_ERROR_NONE) {
throw_exc(env, "check #B6: failed to return JVMTI_ERROR_NONE for bad package");
return FAILED;
}
if (module != NULL || mod_name != NULL) {
throw_exc(env, "check #B7: failed to return NULL-module for bad package");
return FAILED;
}
return PASSED;
}
JNIEXPORT jint JNICALL
Java_MyPackage_GetNamedModuleTest_check(JNIEnv *env, jclass cls) {
jobject loader = NULL;
if (jvmti == NULL) {
throw_exc(env, "JVMTI client was not properly loaded!\n");
return FAILED;
}
get_all_modules(env);
printf("\n*** Check for bad ClassLoader ***\n\n");
result = check_bad_loader(env, (jobject)cls);
if (result != PASSED) {
throw_exc(env, "check #L1: failed to return JVMTI_ERROR_ILLEGAL_ARGUMENT for bad loader");
return result;
}
loader = get_class_loader(cls);
if (loader == NULL) {
throw_exc(env, "check #L2: failed to return non-NULL loader for valid test class");
return FAILED;
}
printf("\n*** Checks for System ClassLoader ***\n\n");
result = check_system_loader(env, loader);
if (result != PASSED) {
return result;
}
printf("\n*** Checks for Bootstrap ClassLoader ***\n\n");
result = check_bootstrap_loader(env, NULL);
return result;
}
#ifdef __cplusplus
}
#endif