JDK-8242263 : Diagnose synchronization on primitive wrappers
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 16
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-04-07
  • Updated: 2024-02-06
  • Resolved: 2020-08-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 16
16 b13Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
"5.1.7. Boxing Conversion" specifies all boxing conversions be identity equivalent for some primitive types in the range -127 to 127. In the Hotspot JVM this is implemented by way of a "primitive cache", whose size may actually changed via an option making this range arbitrary.

Programmers may be at times surprised when they discover synchronization on primitive wrappers deadlocks because of identity aliasing that may have been introduced at deployment time (via tuning cache size options), yet tested fine at development time.

Proposed here, a diagnostic option(s) suitable for both development, testing and production environments. Said option should detect the use of the "synchronized" keyword on a primitive wrapper instance and take one of three actions:

1) Exit the process with a fatal "VirtualMachineError". Suitable for development environments whereby the user wishes to inspect the site/call stack, amend, recompile and retest immediately.

2) Log warning with stack trace. Suitable for coverage testing, whereby the user wishes to identify as many problems as possible over a test run.

3) Emit a "Java Flight Recorder" (JFR) event. Suitable for production environments, given JFR's production-friendly low overhead. Also suitable for cases whereby option 2 (log warning) is extremely verbose. 

Said mechanism may be extended by way of further API and/or options to apply to other cases whereby unintended identity aliasing may cause correctness or deadlock problems.
URL: https://hg.openjdk.java.net/jdk/jdk/rev/39e485236ad7 User: pchilanomate Date: 2020-08-21 15:08:47 +0000

[~briangoetz] Sorry I should have updated the issue. There was an offline discussion which explained the difficulty of compile-time static analysis in relation to the problem at hand (though I'm sure more sophisticated tools like Parfait should be able to track the potential sources of all targets of a synchronized statement.) Patricio has come up with a simple and non-intrusive way to detect the problem which alleviates my immediate concerns.

[~dholmes] Static analysis is effectively useless for answering the question we need to answer, which is: how disruptive would it be if primitive wrappers become unlockable. The cases where this is statically detectable is rare; we are worried about wrapper objects hidden behind a static type of `Object` or `<T extends Comparable<T>>` buried deeply in tools like Web{Logic,Sphere}.

I would like to see some discussion around this issue before we launch into it. While I understand why valhalla would like this diagnostic warning it is not clear to me that the VM is the best place to provide this (a javac Xlint option could detect static usesites), nor that we can implement this is a non-disruptive and performant way.

For complete coverage you would need to inject code into the biased-locking, basic-locking (aka stack locking) and ObjectMonitor inflation execution paths.