Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/AddModuleReads/libAddModuleReadsTest.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}79return JNI_OK;80}8182static83void throw_exc(JNIEnv *env, char *msg) {84jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));85jint rt = JNI_OK;8687if (exc_class == NULL) {88printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);89return;90}91rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);92if (rt == JNI_ERR) {93printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);94}95}9697static98jclass jlM(JNIEnv *env) {99jclass cls = NULL;100101cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, MOD_CNAME));102if (cls == NULL) {103printf(" Error in JNI FindClass: %s\n", MOD_CNAME);104}105return cls;106}107108jmethodID109get_method(JNIEnv *env, jclass clazz, const char * name, const char *sig) {110jmethodID method = NULL;111112method = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, clazz), name, sig);113if (method == NULL) {114printf(" Error in JNI GetMethodID %s with signature %s", name, sig);115}116return method;117}118119static120jboolean can_module_read(JNIEnv *env, jobject module, jobject to_module) {121static jmethodID mCanRead = NULL;122jboolean res = JNI_FALSE;123124if (mCanRead == NULL) {125const char* sign = "(Ljava/lang/Module;)Z";126mCanRead = get_method(env, jlM(env), "canRead", sign);127}128res = JNI_ENV_PTR(env)->CallBooleanMethod(JNI_ENV_ARG(env, module),129mCanRead, to_module);130return res;131}132133static134jint check_add_module_reads(JNIEnv *env,135jclass cls,136jobject unnamedModule,137jobject baseModule,138jobject instrModule) {139jvmtiError err = JVMTI_ERROR_NONE;140jboolean can = JNI_FALSE;141142// Add an invalid read edge from NULL module143printf("Check #N1:\n");144err = (*jvmti)->AddModuleReads(jvmti, NULL, baseModule);145if (err != JVMTI_ERROR_NULL_POINTER) {146printf("#N1: jvmtiError from AddModuleReads: %d\n", err);147throw_exc(env, "Check #N1: failed to return JVMTI_ERROR_NULL_POINTER for module==NULL");148return FAILED;149}150151// Add an invalid read edge to NULL module152printf("Check #N2:\n");153err = (*jvmti)->AddModuleReads(jvmti, baseModule, NULL);154if (err != JVMTI_ERROR_NULL_POINTER) {155printf("#N2: jvmtiError from AddModuleReads: %d\n", err);156throw_exc(env, "Check #N2: failed to return JVMTI_ERROR_NULL_POINTER for to_module==NULL");157return FAILED;158}159160// Add an invalid read edge from invalid module (cls)161printf("Check #I1:\n");162err = (*jvmti)->AddModuleReads(jvmti, cls, baseModule);163if (err != JVMTI_ERROR_INVALID_MODULE) {164printf("#I1: jvmtiError from AddModuleReads: %d\n", err);165throw_exc(env, "Check #I1: failed to return JVMTI_ERROR_INVALID_MODULE for module==cls");166return FAILED;167}168169// Add an invalid read edge to invalid module (cls)170printf("Check #I2:\n");171err = (*jvmti)->AddModuleReads(jvmti, baseModule, cls);172if (err != JVMTI_ERROR_INVALID_MODULE) {173printf("#I2: jvmtiError from AddModuleReads: %d\n", err);174throw_exc(env, "Check #I2: failed to return JVMTI_ERROR_INVALID_MODULE for to_module==cls");175return FAILED;176}177178// Check the edge baseModule->instrModule is absent179printf("Check #C0:\n");180can = can_module_read(env, baseModule, instrModule);181if (can != JNI_FALSE) {182throw_exc(env, "Check #C0: read edge from base to instr is unexpected");183return FAILED;184}185186// Add read edge baseModule->instrModule187printf("Check #C1:\n");188err = (*jvmti)->AddModuleReads(jvmti, baseModule, instrModule);189if (err != JVMTI_ERROR_NONE) {190printf("#C1: jvmtiError from AddModuleReads: %d\n", err);191throw_exc(env, "Check #C1: error in add reads from base to instr");192return FAILED;193}194195// Check the read edge baseModule->instrModule is present now196printf("Check #C2:\n");197can = can_module_read(env, baseModule, instrModule);198if (can == JNI_FALSE) {199throw_exc(env, "Check #C2: failed to add reads from base to instr");200return FAILED;201}202203// Check the read edge baseModule->unnamedModule is absent204printf("Check #C3:\n");205can = can_module_read(env, baseModule, unnamedModule);206if (can != JNI_FALSE) {207throw_exc(env, "Check #C3: got unexpected read edge from base to unnamed");208return FAILED;209}210211// Add read edge baseModule->unnamedModule212printf("Check #C4:\n");213err = (*jvmti)->AddModuleReads(jvmti, baseModule, unnamedModule);214if (err != JVMTI_ERROR_NONE) {215printf("#C4: jvmtiError from AddModuleReads: %d\n", err);216throw_exc(env, "Check #C4: failed to ignore adding read edge from base to unnamed");217return FAILED;218}219220// Check the read edge baseModule->unnamedModule is present now221printf("Check #C5:\n");222can = can_module_read(env, baseModule, unnamedModule);223if (can == JNI_FALSE) {224throw_exc(env, "Check #C5: did not get expected read edge from base to unnamed");225return FAILED;226}227228// Check the read edge unnamedModule->instrModule is absent229printf("Check #C6:\n");230can = can_module_read(env, unnamedModule, instrModule);231if (can == JNI_FALSE) {232throw_exc(env, "Check #C6: did not get expected read edge from unnamed to instr");233return FAILED;234}235236// Add read edge unnamedModule->instrModule237printf("Check #C7:\n");238err = (*jvmti)->AddModuleReads(jvmti, unnamedModule, instrModule);239if (err != JVMTI_ERROR_NONE) {240printf("#C7: jvmtiError from AddModuleReads: %d\n", err);241throw_exc(env, "Check #C7: failed to ignore adding read edge from unnamed to instr");242return FAILED;243}244return PASSED;245}246247JNIEXPORT jint JNICALL248Java_MyPackage_AddModuleReadsTest_check(JNIEnv *env,249jclass cls,250jobject unnamedModule,251jobject baseModule,252jobject instrModule) {253if (jvmti == NULL) {254throw_exc(env, "JVMTI client was not properly loaded!\n");255return FAILED;256}257258printf("\n*** Checks for JVMTI AddModuleReads ***\n\n");259result = check_add_module_reads(env, cls, unnamedModule, baseModule, instrModule);260if (result != PASSED) {261return result;262}263return result;264}265266#ifdef __cplusplus267}268#endif269270271