Skip to content

Commit

Permalink
8251397: NPE on ClassValue.ClassValueMap.cacheArray
Browse files Browse the repository at this point in the history
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 81e2cf8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/java.base/share/classes/java/lang/ClassValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down

1 comment on commit 81e2cf8

@bridgekeeper
Copy link

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Issues

Please sign in to comment.