Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/AddModuleExportsAndOpens/libAddModuleExportsAndOpensTest.c
41153 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x, y) y35#define JNI_ENV_PTR(x) x36#else37#define JNI_ENV_ARG(x,y) x, y38#define JNI_ENV_PTR(x) (*x)39#endif4041#endif4243#define TranslateError(err) "JVMTI error"4445#define PASSED 046#define FAILED 24748static const char *EXC_CNAME = "java/lang/Exception";49static const char* MOD_CNAME = "Ljava/lang/Module;";5051static jvmtiEnv *jvmti = NULL;52static jint result = PASSED;5354static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5556JNIEXPORT57jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {58return Agent_Initialize(jvm, options, reserved);59}6061JNIEXPORT62jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {63return Agent_Initialize(jvm, options, reserved);64}6566JNIEXPORT67jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {68return JNI_VERSION_1_8;69}7071static72jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {73jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),74JVMTI_VERSION_9);75if (res != JNI_OK || jvmti == NULL) {76printf(" Error: wrong result of a valid call to GetEnv!\n");77return JNI_ERR;78}7980return JNI_OK;81}8283static84void throw_exc(JNIEnv *env, char *msg) {85jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));86jint rt = JNI_OK;8788if (exc_class == NULL) {89printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);90return;91}92rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);93if (rt == JNI_ERR) {94printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);95}96}9798static99jclass jlM(JNIEnv *env) {100jclass cls = NULL;101102cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, MOD_CNAME));103if (cls == NULL) {104printf(" Error in JNI FindClass: %s\n", MOD_CNAME);105}106return cls;107}108109jmethodID110get_method(JNIEnv *env, jclass clazz, const char * name, const char *sig) {111jmethodID method = NULL;112113method = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, clazz), name, sig);114if (method == NULL) {115printf(" Error in JNI GetMethodID %s with signature %s", name, sig);116}117return method;118}119120static121jboolean is_exported(JNIEnv *env, jobject module, const char* pkg, jboolean open) {122static jmethodID mIsExported = NULL;123jstring jstr = NULL;124jboolean res = JNI_FALSE;125126if (mIsExported == NULL) {127const char* sign = "(Ljava/lang/String;)Z";128const char* name = open ? "isOpen" : "isExported";129mIsExported = get_method(env, jlM(env), name, sign);130}131jstr = JNI_ENV_PTR(env)->NewStringUTF(JNI_ENV_ARG(env, pkg));132res = JNI_ENV_PTR(env)->CallBooleanMethod(JNI_ENV_ARG(env, module),133mIsExported, jstr);134return res;135}136137static138jboolean is_exported_to(JNIEnv *env, jobject module, const char* pkg, jobject to_module,139jboolean open) {140static jmethodID mIsExportedTo = NULL;141jstring jstr = NULL;142jboolean res = JNI_FALSE;143144if (mIsExportedTo == NULL) {145const char* sign = "(Ljava/lang/String;Ljava/lang/Module;)Z";146const char* name = open ? "isOpen" : "isExported";147mIsExportedTo = get_method(env, jlM(env), name, sign);148}149jstr = JNI_ENV_PTR(env)->NewStringUTF(JNI_ENV_ARG(env, pkg));150res = JNI_ENV_PTR(env)->CallBooleanMethod(JNI_ENV_ARG(env, module),151mIsExportedTo, jstr, to_module);152return res;153}154155static156jvmtiError add_module_exports(jobject baseModule, const char* pkg, jobject thisModule,157jboolean open) {158jvmtiError err = JVMTI_ERROR_NONE;159if (open) {160err = (*jvmti)->AddModuleOpens(jvmti, baseModule, pkg, thisModule);161} else {162err = (*jvmti)->AddModuleExports(jvmti, baseModule, pkg, thisModule);163}164return err;165}166167static168jint check_add_module_exports(JNIEnv *env,169jclass cls,170jobject baseModule,171jobject thisModule,172jboolean open) {173static char strbuf[128] = { '\0' };174jvmtiError err = JVMTI_ERROR_NONE;175const char* pkg = open ? "jdk.internal.math"176: "jdk.internal.misc";177const char* bad_pkg = "my.bad.pkg";178const char* jvmti_fn = open ? "AddModuleOpens"179: "AddModuleExports";180jboolean exported = JNI_FALSE;181182// Export from NULL module183printf("Check #N1:\n");184err = add_module_exports(NULL, pkg, thisModule, open);185if (err != JVMTI_ERROR_NULL_POINTER) {186printf("#N1: jvmtiError from %s: %d\n", jvmti_fn, err);187throw_exc(env, "Check #N1: failed to return JVMTI_ERROR_NULL_POINTER for module==NULL");188return FAILED;189}190191// Export NULL package192printf("Check #N2:\n");193err = add_module_exports(baseModule, NULL, thisModule, open);194if (err != JVMTI_ERROR_NULL_POINTER) {195printf("#N2: jvmtiError from %s: %d\n", jvmti_fn, err);196throw_exc(env, "Check #N2: failed to return JVMTI_ERROR_NULL_POINTER for pkg==NULL");197return FAILED;198}199200// Export to NULL module201printf("Check #N3:\n");202err = add_module_exports(baseModule, pkg, NULL, open);203if (err != JVMTI_ERROR_NULL_POINTER) {204printf("#N3: jvmtiError from %s: %d\n", jvmti_fn, err);205throw_exc(env, "Check #N3: failed to return JVMTI_ERROR_NULL_POINTER for to_module==NULL");206return FAILED;207}208209// Export a bad package210printf("Check #I0:\n");211err = add_module_exports(baseModule, bad_pkg, thisModule, open);212if (err != JVMTI_ERROR_ILLEGAL_ARGUMENT) {213printf("#I0: jvmtiError from %s: %d\n", jvmti_fn, err);214throw_exc(env, "Check #I0: did not get expected JVMTI_ERROR_ILLEGAL_ARGUMENT for invalid package");215return FAILED;216}217218// Export from invalid module (cls)219printf("Check #I1:\n");220err = add_module_exports((jobject)cls, pkg, thisModule, open);221if (err != JVMTI_ERROR_INVALID_MODULE) {222printf("#I1: jvmtiError from %s: %d\n", jvmti_fn, err);223throw_exc(env, "Check #I1: did not get expected JVMTI_ERROR_INVALID_MODULE for invalid module");224return FAILED;225}226227// Export to invalid module (cls)228printf("Check #I2:\n");229err = add_module_exports(baseModule, pkg, (jobject)cls, open);230if (err != JVMTI_ERROR_INVALID_MODULE) {231printf("#I2: jvmtiError from %s: %d\n", jvmti_fn, err);232throw_exc(env, "Check #I2: did not get expected JVMTI_ERROR_INVALID_MODULE for invalid to_module");233return FAILED;234}235236// Check the pkg is not exported from baseModule to thisModule237printf("Check #C0:\n");238exported = is_exported_to(env, baseModule, pkg, thisModule, open);239if (exported != JNI_FALSE) {240sprintf(strbuf, "Check #C0: unexpected export of %s from base to this", pkg);241throw_exc(env, strbuf);242return FAILED;243}244245// Add export of the pkg from baseModule to thisModule246printf("Check #C1:\n");247err = add_module_exports(baseModule, pkg, thisModule, open);248if (err != JVMTI_ERROR_NONE) {249printf("#C1: jvmtiError from %s: %d\n", jvmti_fn, err);250sprintf(strbuf, "Check #C1: error in add export of %s from base to this", pkg);251throw_exc(env, strbuf);252return FAILED;253}254255// Check the pkg is exported from baseModule to thisModule256printf("Check #C2:\n");257exported = is_exported_to(env, baseModule, pkg, thisModule, open);258if (exported == JNI_FALSE) {259sprintf(strbuf, "Check #C2: failed to export %s from base to this", pkg);260throw_exc(env, strbuf);261return FAILED;262}263264// Check the pkg is not exported to all modules265printf("Check #C3:\n");266exported = is_exported(env, baseModule, pkg, open);267if (exported != JNI_FALSE) {268sprintf(strbuf, "Check #C3: unexpected export of %s from base to all modules", pkg);269throw_exc(env, strbuf);270return FAILED;271}272return PASSED;273}274275JNIEXPORT jint JNICALL276Java_MyPackage_AddModuleExportsAndOpensTest_check(JNIEnv *env,277jclass cls,278jobject baseModule,279jobject thisModule) {280if (jvmti == NULL) {281throw_exc(env, "JVMTI client was not properly loaded!\n");282return FAILED;283}284285printf("\n*** Checks for JVMTI AddModuleExports ***\n\n");286result = check_add_module_exports(env, cls, baseModule, thisModule, JNI_FALSE);287if (result != PASSED) {288return result;289}290291printf("\n*** Checks for JVMTI AddModuleOpens ***\n\n");292result = check_add_module_exports(env, cls, baseModule, thisModule, JNI_TRUE);293if (result != PASSED) {294return result;295}296return result;297}298299#ifdef __cplusplus300}301#endif302303304