[OpenJDK 2D-Dev] Concurrent execution of SunLayoutEngine.getEngine(..) may cause corrupt HashMap

Thorsten Jungblut ulli at netcorner.org
Mon Aug 23 15:48:54 UTC 2010


Hi,

if java.awt.font.TextLayout is used concurrently within multiple threads, 
threads can become stuck in an endless loop caused by a corrupt HashMap.

This problem seems to caused by calls to
sun.font.SunLayoutEngine.getEngine(LayoutEngineKey key)
during initilization of TextLayout at
java.awt.font.TextLayout(String string, Font font, FontRenderContext frc)

SunLayoutEngine.getEngine(LayoutEngineKey) accesses a SoftReference and a 
HashMap without proper synchronisation. This may lead to the HashMap being 
corrupted. Any subsequent call to this method causes an endless loop.

Imho the problem is solved by synchronize access to the used SoftReference 
and HashMap.

I created a patch (using latest OpenJDK7 sources from mercurial):

diff -r fd28003bc1d6 src/share/classes/sun/font/SunLayoutEngine.java
--- a/src/share/classes/sun/font/SunLayoutEngine.java	Mon Aug 23 
14:35:22 2010 +0100
+++ b/src/share/classes/sun/font/SunLayoutEngine.java	Mon Aug 23 
17:38:59 2010 +0200
@@ -128,7 +128,7 @@
      }

    // !!! don't need this unless we have more than one sun layout 
engine...
-    public LayoutEngine getEngine(LayoutEngineKey key) {
+    public synchronized LayoutEngine getEngine(LayoutEngineKey key) {
          HashMap cache = (HashMap)cacheref.get();
          if (cache == null) {
              cache = new HashMap();



This will cause access to the hole method to be synchronized so that 
neither 'cache' nor 'cacheref' are accessed concurrently and so nothing 
can get corrupted.

Any comments are welcome.

Best regards
Thorsten



More information about the 2d-dev mailing list