8041998: RegExp implementation is not thread-safe
authorhannesw
Tue, 06 May 2014 12:38:12 +0200
changeset 24280 4420eb3f86a4
parent 24279 33b0fbd03872
child 24281 58ed42a1ebc6
8041998: RegExp implementation is not thread-safe Reviewed-by: lagergren, sundar, attila
nashorn/src/jdk/nashorn/internal/runtime/regexp/JdkRegExp.java
nashorn/src/jdk/nashorn/internal/runtime/regexp/JoniRegExp.java
nashorn/src/jdk/nashorn/internal/runtime/regexp/joni/Regex.java
nashorn/test/script/basic/JDK-8041998.js
nashorn/test/script/basic/JDK-8041998.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/runtime/regexp/JdkRegExp.java	Fri May 02 19:15:59 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/regexp/JdkRegExp.java	Tue May 06 12:38:12 2014 +0200
@@ -46,9 +46,6 @@
     /** Java regexp pattern to use for match. We compile to one of these */
     private Pattern pattern;
 
-    /** The matcher */
-    private RegExpMatcher matcher;
-
     /**
      * Construct a Regular expression from the given {@code source} and {@code flags} strings.
      *
@@ -95,14 +92,7 @@
             return null; // never matches or similar, e.g. a[]
         }
 
-        RegExpMatcher currentMatcher = this.matcher;
-
-        if (currentMatcher == null || matcher.getInput() != str) {
-            currentMatcher = new DefaultMatcher(str);
-            this.matcher  = currentMatcher;
-        }
-
-        return currentMatcher;
+        return new DefaultMatcher(str);
     }
 
     class DefaultMatcher implements RegExpMatcher {
--- a/nashorn/src/jdk/nashorn/internal/runtime/regexp/JoniRegExp.java	Fri May 02 19:15:59 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/regexp/JoniRegExp.java	Tue May 06 12:38:12 2014 +0200
@@ -44,9 +44,6 @@
     /** Compiled Joni Regex */
     private Regex regex;
 
-    /** Matcher */
-    private RegExpMatcher matcher;
-
     /**
      * Construct a Regular expression from the given {@code pattern} and {@code flags} strings.
      *
@@ -95,14 +92,7 @@
             return null;
         }
 
-        RegExpMatcher currentMatcher = this.matcher;
-
-        if (currentMatcher == null || input != currentMatcher.getInput()) {
-            currentMatcher = new JoniMatcher(input);
-            this.matcher   = currentMatcher;
-        }
-
-        return currentMatcher;
+        return new JoniMatcher(input);
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/regexp/joni/Regex.java	Fri May 02 19:15:59 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/regexp/joni/Regex.java	Tue May 06 12:38:12 2014 +0200
@@ -131,12 +131,13 @@
         this.warnings = null;
     }
 
-    public void compile() {
+    public synchronized MatcherFactory compile() {
         if (factory == null && analyser != null) {
-            Compiler compiler = new ArrayCompiler(analyser);
+            new ArrayCompiler(analyser).compile();
             analyser = null; // only do this once
-            compiler.compile();
         }
+        assert factory != null;
+        return factory;
     }
 
     public Matcher matcher(char[] chars) {
@@ -144,8 +145,11 @@
     }
 
     public Matcher matcher(char[] chars, int p, int end) {
-        compile();
-        return factory.create(this, chars, p, end);
+        MatcherFactory matcherFactory = factory;
+        if (matcherFactory == null) {
+            matcherFactory = compile();
+        }
+        return matcherFactory.create(this, chars, p, end);
     }
 
     public WarnCallback getWarnings() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8041998.js	Tue May 06 12:38:12 2014 +0200
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8041998: RegExp implementation is not thread-safe
+ *
+ * @test
+ * @run
+ */
+
+var Thread = java.lang.Thread;
+
+function run() {
+    var line = 'content-type: text/html';
+    for (var i = 0; i < 300; i++) {
+        Thread.sleep(1);
+        line.split(/: /);
+    }
+    print("done");
+}
+
+var threads = [];
+
+for (var i = 0; i < 4; i++) {
+    var thread = new Thread(run);
+    thread.start();
+    threads.push(thread);
+}
+
+for (var i = 0; i < 4; i++) {
+    threads[i].join();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8041998.js.EXPECTED	Tue May 06 12:38:12 2014 +0200
@@ -0,0 +1,4 @@
+done
+done
+done
+done