1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 #include "jvmti.h"
  27 #include "agent_common.h"
  28 #include "JVMTITools.h"
  29 
  30 extern "C" {
  31 
  32 
  33 #define PASSED 0
  34 #define STATUS_FAILED 2
  35 #define FAILED_NO_OOM 3
  36 #define MAX_CHUNK 1024*1024
  37 
  38 // Limit total allocations to 8Gb.
  39 // Without this check we will loop forever if the OS does not
  40 // limit virtual memory (this usually happens on mac).
  41 #define MAX_CHUNK_COUNT 8*1024
  42 
  43 static jvmtiEnv *jvmti = NULL;
  44 static jint result = PASSED;
  45 static jboolean printdump = JNI_FALSE;
  46 
  47 #ifdef STATIC_BUILD
  48 JNIEXPORT jint JNICALL Agent_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {
  49     return Agent_Initialize(jvm, options, reserved);
  50 }
  51 JNIEXPORT jint JNICALL Agent_OnAttach_alloc001(JavaVM *jvm, char *options, void *reserved) {
  52     return Agent_Initialize(jvm, options, reserved);
  53 }
  54 JNIEXPORT jint JNI_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {
  55     return JNI_VERSION_1_8;
  56 }
  57 #endif
  58 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  59     jint res;
  60 
  61     if (options != NULL && strcmp(options, "printdump") == 0) {
  62         printdump = JNI_TRUE;
  63     }
  64 
  65     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
  66     if (res != JNI_OK || jvmti == NULL) {
  67         printf("Wrong result of a valid call to GetEnv!\n");
  68         return JNI_ERR;
  69     }
  70 
  71     return JNI_OK;
  72 }
  73 
  74 JNIEXPORT jint JNICALL
  75 Java_nsk_jvmti_Allocate_alloc001_check(JNIEnv *env, jclass cls) {
  76     jvmtiError err;
  77     size_t size;
  78     void *prev = NULL;
  79     void **mem;
  80     int memCount = 1;
  81 
  82     if (jvmti == NULL) {
  83         printf("JVMTI client was not properly loaded!\n");
  84         return STATUS_FAILED;
  85     }
  86 
  87     if (printdump == JNI_TRUE) {
  88         printf(">>> Null pointer check ...\n");
  89     }
  90     err = jvmti->Allocate((jlong)1, NULL);
  91     if (err != JVMTI_ERROR_NULL_POINTER) {
  92         printf("Error expected: JVMTI_ERROR_NULL_POINTER, got: %s\n",
  93                TranslateError(err));
  94         result = STATUS_FAILED;
  95     }
  96     if (printdump == JNI_TRUE) {
  97         printf(">>> ... done\n");
  98     }
  99 
 100     if (printdump == JNI_TRUE) {
 101         printf(">>> Accessibility check ...\n");
 102     }
 103     for (size = sizeof(mem); size <= MAX_CHUNK; size <<= 1) {
 104         err = jvmti->Allocate(size, (unsigned char **)&mem);
 105         if (err == JVMTI_ERROR_NONE) {
 106             memset(mem, 0, size);
 107             *mem = prev;
 108             prev = mem;
 109         } else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {
 110             break;
 111         } else {
 112             printf("(Allocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",
 113                    TranslateError(err));
 114             result = STATUS_FAILED;
 115             break;
 116         }
 117     }
 118     if (printdump == JNI_TRUE) {
 119         printf(">>> ... done\n");
 120     }
 121 
 122     if (printdump == JNI_TRUE) {
 123         printf(">>> Out of memory check ...\n");
 124     }
 125     while (err != JVMTI_ERROR_OUT_OF_MEMORY) {
 126         err = jvmti->Allocate((jlong)MAX_CHUNK, (unsigned char **)&mem);
 127         if (err == JVMTI_ERROR_NONE) {
 128             *mem = prev;
 129             prev = mem;
 130             memCount++;
 131             if (memCount > MAX_CHUNK_COUNT) {
 132                 printf("Allocated %dMb. Virtual memory limit too high. Quit to avoid timeout.\n", memCount);
 133                 result = FAILED_NO_OOM;
 134                 break;
 135             }
 136         } else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {
 137             break;
 138         } else {
 139             printf("Error expected: JVMTI_ERROR_OUT_OF_MEMORY, got: %s\n",
 140                    TranslateError(err));
 141             result = STATUS_FAILED;
 142             break;
 143         }
 144 
 145         if (printdump == JNI_TRUE && (memCount % 50 == 0)) {
 146            printf(">>> ... done (%dMb)\n", memCount);
 147         }
 148     }
 149     if (printdump == JNI_TRUE) {
 150         printf(">>> ... done (%dMb)\n", memCount);
 151     }
 152 
 153     if (printdump == JNI_TRUE) {
 154         printf(">>> Deallocation ...\n");
 155     }
 156     while (prev != NULL) {
 157         mem = (void**) prev;
 158         prev = *mem;
 159         err = jvmti->Deallocate((unsigned char *)mem);
 160         if (err != JVMTI_ERROR_NONE) {
 161             printf("(Deallocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",
 162                    TranslateError(err));
 163             result = STATUS_FAILED;
 164             break;
 165         }
 166     }
 167     if (printdump == JNI_TRUE) {
 168         printf(">>> ... done\n");
 169     }
 170 
 171     return result;
 172 }
 173 
 174 }