RFR: 8261407: ReflectionFactory.checkInitted() is not thread-safe

David Holmes dholmes at openjdk.java.net
Sun Dec 19 06:00:22 UTC 2021


On Sun, 19 Dec 2021 03:21:55 GMT, liach <duke at openjdk.java.net> wrote:

> Upon review of [8261407](https://bugs.openjdk.java.net/browse/JDK-8261407), by design, duplicate initialization of ReflectionFactory should be safe as it performs side-effect-free property read actions, and the suggesting of making the `initted` field volatile cannot prevent concurrent initialization either; however, having `initted == true` published without the other fields' values is a possibility, which this patch addresses.
> 
> This simulates what's done in `CallSite`'s constructor for `ConstantCallSite`. Please feel free to point out the problems with this patch, as I am relatively inexperienced in this field of fences and there are relatively less available documents. (Thanks to https://shipilev.net/blog/2014/on-the-fence-with-dependencies/)

Changes requested by dholmes (Reviewer).

src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java line 695:

> 693: 
> 694:         // ensure previous fields are visible before initted is
> 695:         Unsafe.getUnsafe().storeStoreFence();

Ensuring ordering on the writer side, without also ensuring ordering on the reader side, doesn't solve an ordering problem. Just make `initted` volatile and this should be safe from a Java Memory Model perspective.

-------------

PR: https://git.openjdk.java.net/jdk/pull/6889


More information about the core-libs-dev mailing list