Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/Allocate/alloc001/alloc001.cpp
40951 views
1
/*
2
* Copyright (c) 2003, 2020, 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
#define PASSED 0
33
#define STATUS_FAILED 2
34
#define FAILED_NO_OOM 3
35
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
46
#ifdef STATIC_BUILD
47
JNIEXPORT jint JNICALL Agent_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {
48
return Agent_Initialize(jvm, options, reserved);
49
}
50
JNIEXPORT jint JNICALL Agent_OnAttach_alloc001(JavaVM *jvm, char *options, void *reserved) {
51
return Agent_Initialize(jvm, options, reserved);
52
}
53
JNIEXPORT jint JNI_OnLoad_alloc001(JavaVM *jvm, char *options, void *reserved) {
54
return JNI_VERSION_1_8;
55
}
56
#endif
57
58
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
59
jint res;
60
61
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
62
if (res != JNI_OK || jvmti == NULL) {
63
printf("Wrong result of a valid call to GetEnv!\n");
64
return JNI_ERR;
65
}
66
67
return JNI_OK;
68
}
69
70
JNIEXPORT jint JNICALL
71
Java_nsk_jvmti_Allocate_alloc001_Test_check(JNIEnv *env, jclass cls) {
72
jvmtiError err;
73
size_t size;
74
void *prev = NULL;
75
void **mem;
76
int memCount = 1;
77
78
if (jvmti == NULL) {
79
printf("JVMTI client was not properly loaded!\n");
80
return STATUS_FAILED;
81
}
82
83
printf(">>> Null pointer check ...\n");
84
err = jvmti->Allocate((jlong)1, NULL);
85
if (err != JVMTI_ERROR_NULL_POINTER) {
86
printf("Error expected: JVMTI_ERROR_NULL_POINTER, got: %s\n",
87
TranslateError(err));
88
result = STATUS_FAILED;
89
}
90
printf(">>> ... done\n");
91
92
printf(">>> Accessibility check ...\n");
93
for (size = sizeof(mem); size <= MAX_CHUNK; size <<= 1) {
94
err = jvmti->Allocate(size, (unsigned char **)&mem);
95
if (err == JVMTI_ERROR_NONE) {
96
memset(mem, 0, size);
97
*mem = prev;
98
prev = mem;
99
} else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {
100
break;
101
} else {
102
printf("(Allocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",
103
TranslateError(err));
104
result = STATUS_FAILED;
105
break;
106
}
107
}
108
printf(">>> ... done\n");
109
110
printf(">>> Out of memory check ...\n");
111
while (err != JVMTI_ERROR_OUT_OF_MEMORY) {
112
err = jvmti->Allocate((jlong)MAX_CHUNK, (unsigned char **)&mem);
113
if (err == JVMTI_ERROR_NONE) {
114
*mem = prev;
115
prev = mem;
116
memCount++;
117
if (memCount > MAX_CHUNK_COUNT) {
118
printf("Allocated %dMb. Virtual memory limit too high. Quit to avoid timeout.\n", memCount);
119
result = FAILED_NO_OOM;
120
break;
121
}
122
} else if (err == JVMTI_ERROR_OUT_OF_MEMORY) {
123
break;
124
} else {
125
printf("Error expected: JVMTI_ERROR_OUT_OF_MEMORY, got: %s\n",
126
TranslateError(err));
127
result = STATUS_FAILED;
128
break;
129
}
130
131
if (memCount % 50 == 0) {
132
printf(">>> ... done (%dMb)\n", memCount);
133
}
134
}
135
printf(">>> ... done (%dMb)\n", memCount);
136
137
printf(">>> Deallocation ...\n");
138
while (prev != NULL) {
139
mem = (void**) prev;
140
prev = *mem;
141
err = jvmti->Deallocate((unsigned char *)mem);
142
if (err != JVMTI_ERROR_NONE) {
143
printf("(Deallocate) Error expected: JVMTI_ERROR_NONE, got: %s\n",
144
TranslateError(err));
145
result = STATUS_FAILED;
146
break;
147
}
148
}
149
printf(">>> ... done\n");
150
151
return result;
152
}
153
154
}
155
156