Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java
38867 views
1
/*
2
* Copyright (c) 2005, 2013, 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 = 5;
57
58
private static Deque<byte []> eatupMemory() throws Exception {
59
dumpMemoryStats("Before memory allocation");
60
61
Deque<byte []> data = new ArrayDeque<byte []>();
62
boolean hasException = false;
63
while (!hasException) {
64
byte [] megaByte;
65
try {
66
megaByte = new byte [MB];
67
data.add(megaByte);
68
} catch (OutOfMemoryError e) {
69
megaByte = null; // Free memory ASAP
70
71
int size = data.size();
72
73
for (int j = 0; j < RESERVATION && !data.isEmpty(); j++) {
74
data.removeLast();
75
}
76
System.gc();
77
hasException = true;
78
System.out.println("OOME is thrown when allocating "
79
+ size + "MB memory.");
80
}
81
}
82
dumpMemoryStats("After memory allocation");
83
84
return data;
85
}
86
87
private static void dumpMemoryStats(String s) throws Exception {
88
Runtime rt = Runtime.getRuntime();
89
System.out.println(s + ":\t"
90
+ rt.freeMemory() + " bytes free");
91
}
92
93
public static void main(String [] args) throws Exception {
94
// Prepare the test
95
final SecretKeyFactory skf =
96
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
97
final PBEKeySpec pbeKS = new PBEKeySpec(
98
"passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
99
100
ExecutorService executor = Executors.newSingleThreadExecutor();
101
Callable<SecretKey> task = new Callable<SecretKey>() {
102
@Override
103
public SecretKey call() throws Exception {
104
return skf.generateSecret(pbeKS);
105
}
106
};
107
108
// Eat up memory
109
Deque<byte []> dummyData = eatupMemory();
110
assert (dummyData != null);
111
112
// Start testing iteration
113
try {
114
for (int i = 0; i <= 1000; i++) {
115
if ((i % 20) == 0) {
116
// Calling gc() isn't dependable, but doesn't hurt.
117
// Gives better output in leak cases.
118
System.gc();
119
dumpMemoryStats("Iteration " + i);
120
}
121
122
Future<SecretKey> future = executor.submit(task);
123
124
try {
125
future.get(TIME_OUT, TimeUnit.SECONDS);
126
} catch (Exception e) {
127
dumpMemoryStats("\nException seen at iteration " + i);
128
throw e;
129
}
130
}
131
} finally {
132
// JTReg will time out after two minutes. Proactively release
133
// the memory to avoid JTReg time-out situation.
134
dummyData = null;
135
System.gc();
136
dumpMemoryStats("Memory dereference");
137
executor.shutdownNow();
138
}
139
}
140
}
141
142