8153978: New test to verify the modules info as returned by the JVMTI

A new JVMTI test

Reviewed-by: ctornqvi, sspitsyn
This commit is contained in:
Alexander Kulyakhtin 2016-07-22 16:55:55 +03:00
parent c076e4284c
commit 1a81918931
3 changed files with 198 additions and 0 deletions
hotspot
make/test
test/serviceability/jvmti/GetModulesInfo

@ -53,6 +53,7 @@ BUILD_HOTSPOT_JTREG_NATIVE_SRC := \
$(HOTSPOT_TOPDIR)/test/compiler/native \
$(HOTSPOT_TOPDIR)/test/serviceability/jvmti/GetNamedModule \
$(HOTSPOT_TOPDIR)/test/testlibrary/jvmti \
$(HOTSPOT_TOPDIR)/test/serviceability/jvmti/GetModulesInfo \
#
# Add conditional directories here when needed.

@ -0,0 +1,130 @@
/*
* 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.
*/
/**
* @test
* @summary Verifies the JVMTI GetAllModules API
* @library /testlibrary
* @run main/othervm -agentlib:JvmtiGetAllModulesTest JvmtiGetAllModulesTest
*
*/
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.lang.module.ModuleReference;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleDescriptor;
import java.lang.module.Configuration;
import java.util.Arrays;
import java.util.Set;
import java.util.Map;
import java.util.function.Supplier;
import java.util.Objects;
import java.util.Optional;
import java.net.URI;
import java.util.HashSet;
import java.util.HashMap;
import java.util.stream.Collectors;
import jdk.test.lib.Asserts;
public class JvmtiGetAllModulesTest {
private static native Module[] getModulesNative();
private static Set<Module> getModulesJVMTI() {
Set<Module> modules = Arrays.stream(getModulesNative()).collect(Collectors.toSet());
// JVMTI reports unnamed modules, Java API does not
// remove the unnamed modules here, so the resulting report can be expected
// to be equal to what Java reports
modules.removeIf(mod -> !mod.isNamed());
return modules;
}
public static void main(String[] args) throws Exception {
final String MY_MODULE_NAME = "myModule";
// Verify that JVMTI reports exactly the same info as Java regarding the named modules
Asserts.assertEquals(Layer.boot().modules(), getModulesJVMTI());
// Load a new named module
ModuleDescriptor descriptor
= new ModuleDescriptor.Builder(MY_MODULE_NAME)
.build();
ModuleFinder finder = finderOf(descriptor);
ClassLoader loader = new ClassLoader() {};
Configuration parent = Layer.boot().configuration();
Configuration cf = parent.resolveRequires(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
Layer my = Layer.boot().defineModules(cf, m -> loader);
// Verify that the loaded module is indeed reported by JVMTI
Set<Module> jvmtiModules = getModulesJVMTI();
for (Module mod : my.modules()) {
if (!jvmtiModules.contains(mod)) {
throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName());
}
}
}
/**
* Returns a ModuleFinder that finds modules with the given module
* descriptors.
*/
static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
// Create a ModuleReference for each module
Map<String, ModuleReference> namesToReference = new HashMap<>();
for (ModuleDescriptor descriptor : descriptors) {
String name = descriptor.name();
URI uri = URI.create("module:/" + name);
Supplier<ModuleReader> supplier = () -> {
throw new UnsupportedOperationException();
};
ModuleReference mref = new ModuleReference(descriptor, uri, supplier);
namesToReference.put(name, mref);
}
return new ModuleFinder() {
@Override
public Optional<ModuleReference> find(String name) {
Objects.requireNonNull(name);
return Optional.ofNullable(namesToReference.get(name));
}
@Override
public Set<ModuleReference> findAll() {
return new HashSet<>(namesToReference.values());
}
};
}
}

@ -0,0 +1,67 @@
/*
* 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
static jvmtiEnv *jvmti = NULL;
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
int err = (*jvm)->GetEnv(jvm, (void**) &jvmti, JVMTI_VERSION_9);
if (err != JNI_OK) {
return JNI_ERR;
}
return JNI_OK;
}
JNIEXPORT jobjectArray JNICALL
Java_JvmtiGetAllModulesTest_getModulesNative(JNIEnv *env, jclass cls) {
jvmtiError err;
jint modules_count = -1;
jobject* modules_ptr;
jobjectArray array = NULL;
int i = 0;
err = (*jvmti)->GetAllModules(jvmti, &modules_count, &modules_ptr);
if (err != JVMTI_ERROR_NONE) {
return NULL;
}
array = (*env)->NewObjectArray(env, modules_count, (*env)->FindClass(env, "java/lang/reflect/Module"), NULL);
for (i = 0; i < modules_count; ++i) {
(*env)->SetObjectArrayElement(env, array, i, modules_ptr[i]);
}
return array;
}
#ifdef __cplusplus
}
#endif