Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/ThreadSafety.java
38828 views
1
/*
2
* Copyright 2014 Google Inc. 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 8062771 8016236
27
* @summary Test publication of Class objects via a data race
28
* @run testng ThreadSafety
29
*/
30
31
import java.net.URL;
32
import java.net.URLClassLoader;
33
import java.util.Collections;
34
import java.util.concurrent.BrokenBarrierException;
35
import java.util.concurrent.Callable;
36
import java.util.concurrent.CyclicBarrier;
37
import java.util.concurrent.ExecutionException;
38
import java.util.concurrent.ExecutorService;
39
import java.util.concurrent.Executors;
40
import java.util.concurrent.Future;
41
import java.util.concurrent.TimeoutException;
42
import static java.util.concurrent.TimeUnit.SECONDS;
43
import static org.testng.Assert.*;
44
import org.testng.annotations.Test;
45
46
/**
47
* A test resulting from an attempt to repro this failure (in guice):
48
*
49
* java.lang.NullPointerException
50
* at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
51
* at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
52
* at sun.reflect.generics.repository.ClassRepository.getSuperclass(ClassRepository.java:84)
53
* at java.lang.Class.getGenericSuperclass(Class.java:692)
54
* at com.google.inject.TypeLiteral.getSuperclassTypeParameter(TypeLiteral.java:99)
55
* at com.google.inject.TypeLiteral.<init>(TypeLiteral.java:79)
56
*
57
* However, as one would expect with thread safety problems in reflection, these
58
* are very hard to reproduce. This very test has never been observed to fail,
59
* but a similar test has been observed to fail about once in 2000 executions
60
* (about once every 6 CPU-hours), in jdk7 only. It appears to be fixed in jdk8+ by:
61
*
62
* 8016236: Class.getGenericInterfaces performance improvement.
63
* (by making Class.genericInfo volatile)
64
*/
65
public class ThreadSafety {
66
public static class EmptyClass {
67
public static class EmptyGenericSuperclass<T> {}
68
public static class EmptyGenericSubclass<T> extends EmptyGenericSuperclass<T> {}
69
}
70
71
/** published via data race */
72
private Class<?> racyClass = Object.class;
73
74
private URL[] urls = ((URLClassLoader) ThreadSafety.class.getClassLoader()).getURLs();
75
76
private Class<?> createNewEmptyGenericSubclassClass() throws Exception {
77
URLClassLoader ucl = new URLClassLoader(urls, null);
78
return Class.forName("ThreadSafety$EmptyClass$EmptyGenericSubclass", true, ucl);
79
}
80
81
@Test
82
public void testRacy_getGenericSuperclass() throws Exception {
83
final int nThreads = 10;
84
final int iterations = 30;
85
final int timeout = 10;
86
final CyclicBarrier newCycle = new CyclicBarrier(nThreads);
87
final Callable<Void> task = new Callable<Void>() {
88
public Void call() throws Exception {
89
for (int i = 0; i < iterations; i++) {
90
final int threadId;
91
try {
92
threadId = newCycle.await(timeout, SECONDS);
93
} catch (BrokenBarrierException e) {
94
return null;
95
}
96
for (int j = 0; j < iterations; j++) {
97
// one thread publishes the class object via a data
98
// race, for the other threads to consume.
99
if (threadId == 0) {
100
racyClass = createNewEmptyGenericSubclassClass();
101
} else {
102
racyClass.getGenericSuperclass();
103
}
104
}
105
}
106
return null;
107
}};
108
109
final ExecutorService pool = Executors.newFixedThreadPool(nThreads);
110
try {
111
for (Future<Void> future :
112
pool.invokeAll(Collections.nCopies(nThreads, task))) {
113
try {
114
future.get(iterations * timeout, SECONDS);
115
} catch (ExecutionException e) {
116
// ignore "collateral damage"
117
if (!(e.getCause() instanceof BrokenBarrierException)
118
&&
119
!(e.getCause() instanceof TimeoutException)) {
120
throw e;
121
}
122
}
123
}
124
} finally {
125
pool.shutdownNow();
126
assertTrue(pool.awaitTermination(2 * timeout, SECONDS));
127
}
128
}
129
}
130
131