Skip to content

Commit 2b56811

Browse files
TianminShiPaul Hohensee
authored andcommitted
8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
Synchronize access to legacyMap in Provider.getService. Reviewed-by: valeriep
1 parent 3fab0b8 commit 2b56811

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

src/java.base/share/classes/java/security/Provider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,9 @@ public Service getService(String type, String algorithm) {
12551255
}
12561256
synchronized (this) {
12571257
ensureLegacyParsed();
1258-
}
1259-
if (legacyMap != null && !legacyMap.isEmpty()) {
1260-
return legacyMap.get(key);
1258+
if (legacyMap != null && !legacyMap.isEmpty()) {
1259+
return legacyMap.get(key);
1260+
}
12611261
}
12621262
return null;
12631263
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2019, 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 8231387
27+
* @library ../testlibrary
28+
* @summary make sure getService() avoids a race
29+
* @author Tianmin Shi
30+
*/
31+
32+
import java.security.Provider;
33+
34+
public class GetServiceRace {
35+
36+
private static final Provider testProvider;
37+
static {
38+
testProvider = new Provider("MyProvider", 1.0, "test") {
39+
};
40+
testProvider.put("CertificateFactory.Fixed", "MyCertificateFactory");
41+
}
42+
43+
private static final int NUMBER_OF_RETRIEVERS = 3;
44+
private static final int TEST_TIME_MS = 1000;
45+
46+
public static boolean testFailed = false;
47+
48+
public static void main(String[] args) throws Exception {
49+
Updater updater = new Updater();
50+
updater.start();
51+
Retriever [] retrievers = new Retriever[NUMBER_OF_RETRIEVERS];
52+
for (int i=0; i<retrievers.length; i++) {
53+
retrievers[i] = new Retriever();
54+
retrievers[i].start();
55+
}
56+
Thread.sleep(TEST_TIME_MS);
57+
System.out.println("Interrupt");
58+
updater.interrupt();
59+
updater.join();
60+
for (int i=0; i<retrievers.length; i++) {
61+
retrievers[i].interrupt();
62+
retrievers[i].join();
63+
}
64+
System.out.println("Done");
65+
if (testFailed) {
66+
throw new Exception("Test Failed");
67+
}
68+
System.out.println("Test Passed");
69+
}
70+
71+
private static class Updater extends Thread {
72+
@Override
73+
public void run() {
74+
while (!isInterrupted()) {
75+
testProvider.put("CertificateFactory.Added", "MyCertificateFactory");
76+
}
77+
System.out.println("Updater stopped");
78+
}
79+
}
80+
81+
private static class Retriever extends Thread {
82+
@Override
83+
public void run() {
84+
while (!isInterrupted()) {
85+
Provider.Service service = testProvider.getService("CertificateFactory", "Fixed");
86+
if (service == null) {
87+
if (!testFailed) {
88+
System.err.println("CertificateFactory.Fixed is NULL");
89+
testFailed = true;
90+
}
91+
} else {
92+
//System.out.println("CertificateFactory.Fixed is good");
93+
}
94+
}
95+
System.out.println("Retriever stopped");
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)