Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java
41159 views
1
/*
2
* Copyright (c) 2005, 2016, 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
/*
25
* @test
26
* @bug 6578538 8027624
27
* @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and
28
* LoginContext
29
* @author Brad Wetmore
30
*
31
* @run main/othervm -Xmx20m TestProviderLeak
32
*
33
*/
34
35
/*
36
* We force the leak to become a problem by eating up most JVM free memory.
37
* In current runs on a server and client machine, it took roughly 50-150
38
* iterations to have the memory leak or time-out shut down other operations.
39
* It complained about "JCE cannot authenticate the provider SunJCE" or timed
40
* out.
41
*/
42
43
import javax.crypto.*;
44
import javax.crypto.spec.*;
45
46
import java.util.*;
47
import java.util.concurrent.*;
48
49
public class TestProviderLeak {
50
private static final int MB = 1024 * 1024;
51
// Currently, 3MB heap size is reserved for running testing iterations.
52
// It is tweaked to make sure the test quickly triggers the memory leak
53
// or throws out TimeoutException.
54
private static final int RESERVATION = 3;
55
// The maximum time, 5 seconds, to wait for each iteration.
56
private static final int TIME_OUT;
57
static {
58
int timeout = 5;
59
try {
60
double timeoutFactor = Double.parseDouble(
61
System.getProperty("test.timeout.factor", "1.0"));
62
timeout = (int) (timeout * timeoutFactor);
63
} catch (Exception e) {
64
System.out.println("Warning: " + e);
65
}
66
TIME_OUT = timeout;
67
System.out.println("Timeout for each iteration is "
68
+ TIME_OUT + " seconds");
69
}
70
71
private static Deque<byte []> eatupMemory() throws Exception {
72
dumpMemoryStats("Before memory allocation");
73
74
Deque<byte []> data = new ArrayDeque<byte []>();
75
boolean hasException = false;
76
while (!hasException) {
77
byte [] megaByte;
78
try {
79
megaByte = new byte [MB];
80
data.add(megaByte);
81
} catch (OutOfMemoryError e) {
82
megaByte = null; // Free memory ASAP
83
84
int size = data.size();
85
86
for (int j = 0; j < RESERVATION && !data.isEmpty(); j++) {
87
data.removeLast();
88
}
89
System.gc();
90
hasException = true;
91
System.out.println("OOME is thrown when allocating "
92
+ size + "MB memory.");
93
}
94
}
95
dumpMemoryStats("After memory allocation");
96
97
return data;
98
}
99
100
private static void dumpMemoryStats(String s) throws Exception {
101
Runtime rt = Runtime.getRuntime();
102
System.out.println(s + ":\t"
103
+ rt.freeMemory() + " bytes free");
104
}
105
106
public static void main(String [] args) throws Exception {
107
// Prepare the test
108
final SecretKeyFactory skf =
109
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
110
final PBEKeySpec pbeKS = new PBEKeySpec(
111
"passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
112
113
ExecutorService executor = Executors.newSingleThreadExecutor();
114
Callable<SecretKey> task = new Callable<SecretKey>() {
115
@Override
116
public SecretKey call() throws Exception {
117
return skf.generateSecret(pbeKS);
118
}
119
};
120
121
// Eat up memory
122
Deque<byte []> dummyData = eatupMemory();
123
assert (dummyData != null);
124
125
// Start testing iteration
126
try {
127
for (int i = 0; i <= 1000; i++) {
128
if ((i % 20) == 0) {
129
// Calling gc() isn't dependable, but doesn't hurt.
130
// Gives better output in leak cases.
131
System.gc();
132
dumpMemoryStats("Iteration " + i);
133
}
134
135
Future<SecretKey> future = executor.submit(task);
136
137
try {
138
future.get(TIME_OUT, TimeUnit.SECONDS);
139
} catch (Exception e) {
140
dumpMemoryStats("\nException seen at iteration " + i);
141
throw e;
142
}
143
}
144
} finally {
145
// JTReg will time out after two minutes. Proactively release
146
// the memory to avoid JTReg time-out situation.
147
dummyData = null;
148
System.gc();
149
dumpMemoryStats("Memory dereference");
150
executor.shutdownNow();
151
}
152
}
153
}
154
155