Skip to content

Commit 85bac8c

Browse files
author
Peter Levart
committedJan 5, 2021
8259021: SharedSecrets should avoid double racy reads from non-volatile fields
Reviewed-by: shade, redestad, rriggs, mchung, rrich, alanb
1 parent d5aa49d commit 85bac8c

File tree

1 file changed

+93
-45
lines changed

1 file changed

+93
-45
lines changed
 

‎src/java.base/share/classes/jdk/internal/access/SharedSecrets.java

+93-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -83,21 +83,25 @@ public static void setJavaUtilCollectionAccess(JavaUtilCollectionAccess juca) {
8383
}
8484

8585
public static JavaUtilCollectionAccess getJavaUtilCollectionAccess() {
86-
if (javaUtilCollectionAccess == null) {
86+
var access = javaUtilCollectionAccess;
87+
if (access == null) {
8788
try {
8889
Class.forName("java.util.ImmutableCollections$Access", true, null);
90+
access = javaUtilCollectionAccess;
8991
} catch (ClassNotFoundException e) {};
9092
}
91-
return javaUtilCollectionAccess;
93+
return access;
9294
}
9395

9496
public static JavaUtilJarAccess javaUtilJarAccess() {
95-
if (javaUtilJarAccess == null) {
97+
var access = javaUtilJarAccess;
98+
if (access == null) {
9699
// Ensure JarFile is initialized; we know that this class
97100
// provides the shared secret
98101
ensureClassInitialized(JarFile.class);
102+
access = javaUtilJarAccess;
99103
}
100-
return javaUtilJarAccess;
104+
return access;
101105
}
102106

103107
public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
@@ -117,23 +121,27 @@ public static void setJavaLangInvokeAccess(JavaLangInvokeAccess jlia) {
117121
}
118122

119123
public static JavaLangInvokeAccess getJavaLangInvokeAccess() {
120-
if (javaLangInvokeAccess == null) {
124+
var access = javaLangInvokeAccess;
125+
if (access == null) {
121126
try {
122127
Class.forName("java.lang.invoke.MethodHandleImpl", true, null);
128+
access = javaLangInvokeAccess;
123129
} catch (ClassNotFoundException e) {};
124130
}
125-
return javaLangInvokeAccess;
131+
return access;
126132
}
127133

128134
public static void setJavaLangModuleAccess(JavaLangModuleAccess jlrma) {
129135
javaLangModuleAccess = jlrma;
130136
}
131137

132138
public static JavaLangModuleAccess getJavaLangModuleAccess() {
133-
if (javaLangModuleAccess == null) {
139+
var access = javaLangModuleAccess;
140+
if (access == null) {
134141
ensureClassInitialized(ModuleDescriptor.class);
142+
access = javaLangModuleAccess;
135143
}
136-
return javaLangModuleAccess;
144+
return access;
137145
}
138146

139147
public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
@@ -157,102 +165,127 @@ public static void setJavaNetUriAccess(JavaNetUriAccess jnua) {
157165
}
158166

159167
public static JavaNetUriAccess getJavaNetUriAccess() {
160-
if (javaNetUriAccess == null)
168+
var access = javaNetUriAccess;
169+
if (access == null) {
161170
ensureClassInitialized(java.net.URI.class);
162-
return javaNetUriAccess;
171+
access = javaNetUriAccess;
172+
}
173+
return access;
163174
}
164175

165176
public static void setJavaNetURLAccess(JavaNetURLAccess jnua) {
166177
javaNetURLAccess = jnua;
167178
}
168179

169180
public static JavaNetURLAccess getJavaNetURLAccess() {
170-
if (javaNetURLAccess == null)
181+
var access = javaNetURLAccess;
182+
if (access == null) {
171183
ensureClassInitialized(java.net.URL.class);
172-
return javaNetURLAccess;
184+
access = javaNetURLAccess;
185+
}
186+
return access;
173187
}
174188

175189
public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
176190
javaNetInetAddressAccess = jna;
177191
}
178192

179193
public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
180-
if (javaNetInetAddressAccess == null)
194+
var access = javaNetInetAddressAccess;
195+
if (access == null) {
181196
ensureClassInitialized(java.net.InetAddress.class);
182-
return javaNetInetAddressAccess;
197+
access = javaNetInetAddressAccess;
198+
}
199+
return access;
183200
}
184201

185202
public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
186203
javaNetHttpCookieAccess = a;
187204
}
188205

189206
public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
190-
if (javaNetHttpCookieAccess == null)
207+
var access = javaNetHttpCookieAccess;
208+
if (access == null) {
191209
ensureClassInitialized(java.net.HttpCookie.class);
192-
return javaNetHttpCookieAccess;
210+
access = javaNetHttpCookieAccess;
211+
}
212+
return access;
193213
}
194214

195215
public static void setJavaNioAccess(JavaNioAccess jna) {
196216
javaNioAccess = jna;
197217
}
198218

199219
public static JavaNioAccess getJavaNioAccess() {
200-
if (javaNioAccess == null) {
220+
var access = javaNioAccess;
221+
if (access == null) {
201222
// Ensure java.nio.Buffer is initialized, which provides the
202223
// shared secret.
203224
ensureClassInitialized(java.nio.Buffer.class);
225+
access = javaNioAccess;
204226
}
205-
return javaNioAccess;
227+
return access;
206228
}
207229

208230
public static void setJavaIOAccess(JavaIOAccess jia) {
209231
javaIOAccess = jia;
210232
}
211233

212234
public static JavaIOAccess getJavaIOAccess() {
213-
if (javaIOAccess == null) {
235+
var access = javaIOAccess;
236+
if (access == null) {
214237
ensureClassInitialized(Console.class);
238+
access = javaIOAccess;
215239
}
216-
return javaIOAccess;
240+
return access;
217241
}
218242

219243
public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
220244
javaIOFileDescriptorAccess = jiofda;
221245
}
222246

223247
public static JavaIOFilePermissionAccess getJavaIOFilePermissionAccess() {
224-
if (javaIOFilePermissionAccess == null)
248+
var access = javaIOFilePermissionAccess;
249+
if (access == null) {
225250
ensureClassInitialized(FilePermission.class);
226-
227-
return javaIOFilePermissionAccess;
251+
access = javaIOFilePermissionAccess;
252+
}
253+
return access;
228254
}
229255

230256
public static void setJavaIOFilePermissionAccess(JavaIOFilePermissionAccess jiofpa) {
231257
javaIOFilePermissionAccess = jiofpa;
232258
}
233259

234260
public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
235-
if (javaIOFileDescriptorAccess == null)
261+
var access = javaIOFileDescriptorAccess;
262+
if (access == null) {
236263
ensureClassInitialized(FileDescriptor.class);
237-
238-
return javaIOFileDescriptorAccess;
264+
access = javaIOFileDescriptorAccess;
265+
}
266+
return access;
239267
}
240268

241269
public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
242270
javaSecurityAccess = jsa;
243271
}
244272

245273
public static JavaSecurityAccess getJavaSecurityAccess() {
246-
if (javaSecurityAccess == null) {
274+
var access = javaSecurityAccess;
275+
if (access == null) {
247276
ensureClassInitialized(ProtectionDomain.class);
277+
access = javaSecurityAccess;
248278
}
249-
return javaSecurityAccess;
279+
return access;
250280
}
251281

252282
public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
253-
if (javaUtilZipFileAccess == null)
283+
var access = javaUtilZipFileAccess;
284+
if (access == null) {
254285
ensureClassInitialized(java.util.zip.ZipFile.class);
255-
return javaUtilZipFileAccess;
286+
access = javaUtilZipFileAccess;
287+
}
288+
return access;
256289
}
257290

258291
public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
@@ -288,42 +321,51 @@ public static void setJavaBeansAccess(JavaBeansAccess access) {
288321
}
289322

290323
public static JavaUtilResourceBundleAccess getJavaUtilResourceBundleAccess() {
291-
if (javaUtilResourceBundleAccess == null)
324+
var access = javaUtilResourceBundleAccess;
325+
if (access == null) {
292326
ensureClassInitialized(ResourceBundle.class);
293-
return javaUtilResourceBundleAccess;
327+
access = javaUtilResourceBundleAccess;
328+
}
329+
return access;
294330
}
295331

296332
public static void setJavaUtilResourceBundleAccess(JavaUtilResourceBundleAccess access) {
297333
javaUtilResourceBundleAccess = access;
298334
}
299335

300336
public static JavaObjectInputStreamReadString getJavaObjectInputStreamReadString() {
301-
if (javaObjectInputStreamReadString == null) {
337+
var access = javaObjectInputStreamReadString;
338+
if (access == null) {
302339
ensureClassInitialized(ObjectInputStream.class);
340+
access = javaObjectInputStreamReadString;
303341
}
304-
return javaObjectInputStreamReadString;
342+
return access;
305343
}
306344

307345
public static void setJavaObjectInputStreamReadString(JavaObjectInputStreamReadString access) {
308346
javaObjectInputStreamReadString = access;
309347
}
310348

311349
public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
312-
if (javaObjectInputStreamAccess == null) {
350+
var access = javaObjectInputStreamAccess;
351+
if (access == null) {
313352
ensureClassInitialized(ObjectInputStream.class);
353+
access = javaObjectInputStreamAccess;
314354
}
315-
return javaObjectInputStreamAccess;
355+
return access;
316356
}
317357

318358
public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
319359
javaObjectInputStreamAccess = access;
320360
}
321361

322362
public static JavaObjectInputFilterAccess getJavaObjectInputFilterAccess() {
323-
if (javaObjectInputFilterAccess == null) {
363+
var access = javaObjectInputFilterAccess;
364+
if (access == null) {
324365
ensureClassInitialized(ObjectInputFilter.Config.class);
366+
access = javaObjectInputFilterAccess;
325367
}
326-
return javaObjectInputFilterAccess;
368+
return access;
327369
}
328370

329371
public static void setJavaObjectInputFilterAccess(JavaObjectInputFilterAccess access) {
@@ -335,32 +377,38 @@ public static void setJavaIORandomAccessFileAccess(JavaIORandomAccessFileAccess
335377
}
336378

337379
public static JavaIORandomAccessFileAccess getJavaIORandomAccessFileAccess() {
338-
if (javaIORandomAccessFileAccess == null) {
380+
var access = javaIORandomAccessFileAccess;
381+
if (access == null) {
339382
ensureClassInitialized(RandomAccessFile.class);
383+
access = javaIORandomAccessFileAccess;
340384
}
341-
return javaIORandomAccessFileAccess;
385+
return access;
342386
}
343387

344388
public static void setJavaSecuritySignatureAccess(JavaSecuritySignatureAccess jssa) {
345389
javaSecuritySignatureAccess = jssa;
346390
}
347391

348392
public static JavaSecuritySignatureAccess getJavaSecuritySignatureAccess() {
349-
if (javaSecuritySignatureAccess == null) {
393+
var access = javaSecuritySignatureAccess;
394+
if (access == null) {
350395
ensureClassInitialized(Signature.class);
396+
access = javaSecuritySignatureAccess;
351397
}
352-
return javaSecuritySignatureAccess;
398+
return access;
353399
}
354400

355401
public static void setJavaxCryptoSealedObjectAccess(JavaxCryptoSealedObjectAccess jcsoa) {
356402
javaxCryptoSealedObjectAccess = jcsoa;
357403
}
358404

359405
public static JavaxCryptoSealedObjectAccess getJavaxCryptoSealedObjectAccess() {
360-
if (javaxCryptoSealedObjectAccess == null) {
406+
var access = javaxCryptoSealedObjectAccess;
407+
if (access == null) {
361408
ensureClassInitialized(SealedObject.class);
409+
access = javaxCryptoSealedObjectAccess;
362410
}
363-
return javaxCryptoSealedObjectAccess;
411+
return access;
364412
}
365413

366414
private static void ensureClassInitialized(Class<?> c) {

0 commit comments

Comments
 (0)
Please sign in to comment.