Skip to content
Permalink
Browse files
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
  • Loading branch information
galderz authored and jerboaa committed Sep 17, 2020
1 parent cca3a26 commit 81e2cf82d9c0bb73a664069b1317ad12b9de5b82
Showing with 14 additions and 2 deletions.
  1. +14 −2 src/java.base/share/classes/java/lang/ClassValue.java
@@ -29,6 +29,8 @@
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicInteger;

import jdk.internal.misc.Unsafe;

import static java.lang.ClassValue.ClassValueMap.probeHomeLocation;
import static java.lang.ClassValue.ClassValueMap.probeBackupLocations;

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

private static final Object CRITICAL_SECTION = new Object();
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
private static ClassValueMap initializeMap(Class<?> type) {
ClassValueMap map;
synchronized (CRITICAL_SECTION) { // private object to avoid deadlocks
// happens about once per type
if ((map = type.classValueMap) == null)
type.classValueMap = map = new ClassValueMap();
if ((map = type.classValueMap) == null) {
map = new ClassValueMap();
// Place a Store fence after construction and before publishing to emulate
// ClassValueMap containing final fields. This ensures it can be
// published safely in the non-volatile field Class.classValueMap,
// since stores to the fields of ClassValueMap will not be reordered
// to occur after the store to the field type.classValueMap
UNSAFE.storeFence();

type.classValueMap = map;
}
}
return map;
}

1 comment on commit 81e2cf8

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on 81e2cf8 Sep 17, 2020

Review

Issues

Please sign in to comment.