Skip to content

Commit 81e2cf8

Browse files
galderzjerboaa
authored andcommitted
8251397: NPE on ClassValue.ClassValueMap.cacheArray
Add release fence to ClassValueMap constructor. * Release fence guarantees that cacheArray field will published with a non-null value. * Without this fix, CacheValueMap.cacheArray can sometimes be seen as null. Reviewed-by: shade, psandoz
1 parent cca3a26 commit 81e2cf8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/java.base/share/classes/java/lang/ClassValue.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.lang.ref.WeakReference;
3030
import java.util.concurrent.atomic.AtomicInteger;
3131

32+
import jdk.internal.misc.Unsafe;
33+
3234
import static java.lang.ClassValue.ClassValueMap.probeHomeLocation;
3335
import static java.lang.ClassValue.ClassValueMap.probeBackupLocations;
3436

@@ -369,12 +371,22 @@ private static ClassValueMap getMap(Class<?> type) {
369371
}
370372

371373
private static final Object CRITICAL_SECTION = new Object();
374+
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
372375
private static ClassValueMap initializeMap(Class<?> type) {
373376
ClassValueMap map;
374377
synchronized (CRITICAL_SECTION) { // private object to avoid deadlocks
375378
// happens about once per type
376-
if ((map = type.classValueMap) == null)
377-
type.classValueMap = map = new ClassValueMap();
379+
if ((map = type.classValueMap) == null) {
380+
map = new ClassValueMap();
381+
// Place a Store fence after construction and before publishing to emulate
382+
// ClassValueMap containing final fields. This ensures it can be
383+
// published safely in the non-volatile field Class.classValueMap,
384+
// since stores to the fields of ClassValueMap will not be reordered
385+
// to occur after the store to the field type.classValueMap
386+
UNSAFE.storeFence();
387+
388+
type.classValueMap = map;
389+
}
378390
}
379391
return map;
380392
}

0 commit comments

Comments
 (0)