Uploaded image for project: 'Harmony'
  1. Harmony
  2. HARMONY-6651

Broken double checked locking in DragSourceContext.java

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 6.0M1
    • Fix Version/s: 5.0M16
    • Component/s: Classlib
    • Labels:
      None
    • Environment:
      Windows Xp

      Description

      I found broken double-checked locking in modules/nio_char/src/main/java/java/nio/charset/Charset.java

      public abstract class Charset implements Comparable<Charset>{
      ...
      public static SortedMap<String, Charset> availableCharsets() {
      // Initialize the built-in charsets map cache if necessary
      if (null == _builtInCharsets) {
      synchronized (Charset.class) {
      if (null == _builtInCharsets)

      { _builtInCharsets = new TreeMap<String, Charset>( IgnoreCaseComparator.getInstance()); _builtInProvider.putCharsets(_builtInCharsets); }

      }
      }
      ...
      }
      ...
      }

      The double checked locking idiom is broken in the code above. In current Java memory model (JMM), the writes initializing the TreeMap object and the write to the _builtInCharesets field could be reordered by the compiler, processor or memory system. It means the write to _builtInCharesets can happen before the writes in TreeMap object initialization.

      Consequence:
      When multiple threads access the code simultaneously, threads can see a non-null _builtInCharesets field, but the TreeMap can be partially constructed (e.g. The elementData array is not created yet). Then these threads invoke clone() method on the partially initialized TreeMap, which will result in undesired behavior, e.g. Throwing NullPointerException. The error doesn't manifest all the time due to the non-deterministic nature of multi-threading, but it would be too bad to leave the code depend on the undefined behavior.

      In JDK 5 or above, declaring the _builtInCharesets field as volatile can fix the problem.

        Attachments

          Activity

            People

            • Assignee:
              regis_xu Regis Xu
              Reporter:
              cashcrop Wendy Feng
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 3h
                3h
                Remaining:
                Remaining Estimate - 3h
                3h
                Logged:
                Time Spent - Not Specified
                Not Specified