Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/IsModifiableModule/libIsModifiableModuleTest.c
41155 views
/*1* Copyright (c) 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/AssertionError";4950static jvmtiEnv *jvmti = NULL;51static jint result = PASSED;52static jboolean printdump = JNI_FALSE;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;7475if (options != NULL && strcmp(options, "printdump") == 0) {76printdump = JNI_TRUE;77}7879res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),80JVMTI_VERSION_9);81if (res != JNI_OK || jvmti == NULL) {82printf(" Error: wrong result of a valid call to GetEnv!\n");83return JNI_ERR;84}8586return JNI_OK;87}8889static90jclass find_class(JNIEnv *env, const char* cname) {91jclass cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, cname));9293if (cls == NULL) {94printf("find_class: Error: FindClass(env, \"%s\") returned NULL\n", cname);95}96return cls;97}9899static100jint throw_exc(JNIEnv *env, char *msg) {101jclass exc_class = find_class(env, EXC_CNAME);102103if (exc_class == NULL) {104printf("throw_exc: Error in find_class(env, \"%s\")\n", EXC_CNAME);105return -1;106}107return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);108}109110static jobject get_module_by_class_name(JNIEnv *env, const char* cname) {111jobject module = NULL;112jclass cls = find_class(env, cname);113114printf(">>> getting module by class name: \"%s\"\n", cname);115if (cls == NULL) {116printf("get_module_by_class_name: Error in find_class(env, \"%s\")\n", cname);117return NULL;118}119module = JNI_ENV_PTR(env)->GetModule(JNI_ENV_ARG(env, cls));120if (module == NULL) {121printf("get_module_by_class_name: Error in GetModule for class \"%s\"\n", cname);122}123return module;124}125126static127jint check_is_modifiable_error_codes(jobject module, jobject not_a_module) {128jvmtiError err = JVMTI_ERROR_NONE;129jboolean is_modifiable = JNI_FALSE;130131printf(">>> passing a bad module argument to JVMTI IsModifiableModule\n");132err = (*jvmti)->IsModifiableModule(jvmti, not_a_module, &is_modifiable);133if (err != JVMTI_ERROR_INVALID_MODULE) {134printf(" Error #EC0: Did not get expected INVALID_MODULE error code from"135" IsModifiableModule: %s (%d)\n", TranslateError(err), err);136return FAILED;137}138printf(">>> passing NULL module argument to JVMTI IsModifiableModule\n");139err = (*jvmti)->IsModifiableModule(jvmti, NULL, &is_modifiable);140if (err != JVMTI_ERROR_NULL_POINTER) {141printf(" Error #EC1: Did not get expected NULL_POINTER error code from"142" IsModifiableModule: %s (%d)\n", TranslateError(err), err);143return FAILED;144}145printf(">>> passing NULL status pointer to JVMTI IsModifiableModule\n");146err = (*jvmti)->IsModifiableModule(jvmti, module, NULL);147if (err != JVMTI_ERROR_NULL_POINTER) {148printf(" Error #EC2: Did not get expected NULL_POINTER error code from"149" IsModifiableModule: %s (%d)\n", TranslateError(err), err);150return FAILED;151}152return PASSED;153}154155static156jint check_is_modifiable(jobject module) {157jvmtiError err = JVMTI_ERROR_NONE;158jboolean is_modifiable = JNI_FALSE;159160printf(">>> checking module %p is modifiable\n", module);161err = (*jvmti)->IsModifiableModule(jvmti, module, &is_modifiable);162if (err != JVMTI_ERROR_NONE) {163printf(" Error in IsModifiableModule for module %p: %s (%d)\n",164module, TranslateError(err), err);165return FAILED;166}167if (is_modifiable == JNI_FALSE) {168printf(" unexpected non-modifiable status for module: %p\n", module);169return FAILED;170}171return PASSED;172}173174JNIEXPORT jint JNICALL175Java_MyPackage_IsModifiableModuleTest_check(JNIEnv *env, jclass cls) {176jobject module = NULL;177178if (jvmti == NULL) {179throw_exc(env, "JVMTI client was not properly loaded!\n");180return FAILED;181}182183printf("\n*** Testing IsModifiableModule ***\n\n");184185if (check_is_modifiable_error_codes(module, cls) == FAILED) {186throw_exc(env, "check #MM0: failed to return expected error code from "187"a bad call to JVMTI IsModifiableModule");188return FAILED;189}190191module = get_module_by_class_name(env, "java/lang/Class");192if (check_is_modifiable(module) == FAILED) {193throw_exc(env, "check #MM1: failed to return modifiable module status");194return FAILED;195}196197module = get_module_by_class_name(env, "com/sun/jdi/VirtualMachine");198if (check_is_modifiable(module) == FAILED) {199throw_exc(env, "check #MM2: failed to return modifiable module status");200return FAILED;201}202203module = get_module_by_class_name(env, "MyPackage/IsModifiableModuleTest");204if (check_is_modifiable(module) == FAILED) {205throw_exc(env, "check #MM3: failed to return modifiable module status");206return FAILED;207}208209return PASSED;210}211212#ifdef __cplusplus213}214#endif215216217