Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/Allocate/alloc001/alloc001.cpp
40951 views
/*1* Copyright (c) 2003, 2020, 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"26#include "agent_common.h"27#include "JVMTITools.h"2829extern "C" {3031#define PASSED 032#define STATUS_FAILED 233#define FAILED_NO_OOM 33435#define MAX_CHUNK 1024 * 10243637// Limit total allocations to 8Gb.38// Without this check we will loop forever if the OS does not39// limit virtual memory (this usually happens on mac).40#define MAX_CHUNK_COUNT 8 * 10244142static jvmtiEnv *jvmti = NULL;43static jint result = PASSED;4445#ifdef STATIC_BUILD46JNIEXPORT jint JNICALL Agent_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {47return Agent_Initialize(jvm, options, reserved);48}49JNIEXPORT jint JNICALL Agent_OnAttach_alloc001(JavaVM *jvm, char *options, void *reserved) {50return Agent_Initialize(jvm, options, reserved);51}52JNIEXPORT jint JNI_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {53return JNI_VERSION_1_8;54}55#endif5657jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {58jint res;5960res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);61if (res != JNI_OK || jvmti == NULL) {62printf("Wrong result of a valid call to GetEnv!\n");63return JNI_ERR;64}6566return JNI_OK;67}6869JNIEXPORT jint JNICALL70Java_nsk_jvmti_Allocate_alloc001_Test_check(JNIEnv *env, jclass cls) {71jvmtiError err;72size_t size;73void *prev = NULL;74void **mem;75int memCount = 1;7677if (jvmti == NULL) {78printf("JVMTI client was not properly loaded!\n");79return STATUS_FAILED;80}8182printf(">>> Null pointer check ...\n");83err = jvmti->Allocate((jlong)1, NULL);84if (err != JVMTI_ERROR_NULL_POINTER) {85printf("Error expected: JVMTI_ERROR_NULL_POINTER, got: %s\n",86TranslateError(err));87result = STATUS_FAILED;88}89printf(">>> ... done\n");9091printf(">>> Accessibility check ...\n");92for (size = sizeof(mem); size <= MAX_CHUNK; size <<= 1) {93err = jvmti->Allocate(size, (unsigned char **)&mem);94if (err == JVMTI_ERROR_NONE) {95memset(mem, 0, size);96*mem = prev;97prev = mem;98} else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {99break;100} else {101printf("(Allocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",102TranslateError(err));103result = STATUS_FAILED;104break;105}106}107printf(">>> ... done\n");108109printf(">>> Out of memory check ...\n");110while (err != JVMTI_ERROR_OUT_OF_MEMORY) {111err = jvmti->Allocate((jlong)MAX_CHUNK, (unsigned char **)&mem);112if (err == JVMTI_ERROR_NONE) {113*mem = prev;114prev = mem;115memCount++;116if (memCount > MAX_CHUNK_COUNT) {117printf("Allocated %dMb. Virtual memory limit too high. Quit to avoid timeout.\n", memCount);118result = FAILED_NO_OOM;119break;120}121} else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {122break;123} else {124printf("Error expected: JVMTI_ERROR_OUT_OF_MEMORY, got: %s\n",125TranslateError(err));126result = STATUS_FAILED;127break;128}129130if (memCount % 50 == 0) {131printf(">>> ... done (%dMb)\n", memCount);132}133}134printf(">>> ... done (%dMb)\n", memCount);135136printf(">>> Deallocation ...\n");137while (prev != NULL) {138mem = (void**) prev;139prev = *mem;140err = jvmti->Deallocate((unsigned char *)mem);141if (err != JVMTI_ERROR_NONE) {142printf("(Deallocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",143TranslateError(err));144result = STATUS_FAILED;145break;146}147}148printf(">>> ... done\n");149150return result;151}152153}154155156