RFR: 8295486: Inconsistent constant field values observed during compilation
Jatin Bhateja
jbhateja at openjdk.org
Thu Jan 12 07:18:16 UTC 2023
On Mon, 9 Jan 2023 13:20:04 GMT, Tobias Hartmann <thartmann at openjdk.org> wrote:
> Vladimirs, Jatin, thanks for the reviews and discussion!
>
> The question is what we want to achieve with this patch:
>
> 1. Prevent C2 from crashing / asserting when observing inconsistent field values
> 2. Prevent C2 from applying optimizations based on inconsistent field values
> 3. Prevent C2 from constant folding stale values
>
> I think my proposed patch achieves 1) which is also the only issue we ever observed in real code: Racy initialization of method handles for indy string concat in core libraries code. The verification code proposed by ([JDK-8024042](https://bugs.openjdk.org/browse/JDK-8024042)) would catch this. Constant folding a stale value does not matter here, semantically, we can use any of the equivalent method handles that we observe. It just confuses CCP verification. Convergence of CCP is also not an issue because we don't update a node's type indefinitely but only if the type of a relevant input node changed.
>
> Regarding 2): Couldn't any such wrong/undesired behaviour happen with execution in the interpreter as well? The only difference would be that optimized C2 compiled code would **always** behave that way. But according to the specification for `@Stable`, that's okay:
>
> https://github.com/openjdk/jdk/blob/5c8c67c523279de728248f54382c40fb20d0ab63/src/java.base/share/classes/jdk/internal/vm/annotation/Stable.java#L74-L80
>
> Also, doesn't the above specification mean that all non-synchronized initializations of stable fields can lead to undefined behavior?
>
> > Imaging you read same stable field on separate paths and then compare constants.
>
> That will also happen with interpreted code when the stable field is written between the two reads.
>
> The only complete solution would be 3), which, as Vladimir I. already pointed out, does not seem feasible given that we would need to intercept all writes to stable fields and array elements.
>
> I'm not against caching but I'm wondering how much sense it makes to apply an expensive and complex (partial) fix to C2 while C1 and the interpreter are still affected. Shouldn't the remaining issues be fixed in Java code if undefined/unexpected behavior is ever observed?
Thanks @TobiHartmann for explanations. Caching looks correct. First intercept of constant field folding is dring parsing when a field access is made and later on gvn folds the constant field loads. Similar change could be done for C1 https://github.com/openjdk/jdk/blob/af8d3fb21ab59104d49bd664f634399fb72ecbd2/src/hotspot/share/c1/c1_GraphBuilder.cpp#L1797
But given that problem is only seen during aggressive optimization like CCP I agree with @iwanowww's suggestion of restricting the scope of fix to C2.
-------------
PR: https://git.openjdk.org/jdk/pull/11861
More information about the hotspot-compiler-dev
mailing list