1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 import java.util.Arrays;
  26 
  27 /*
  28  * @test
  29  * @bug 8159244
  30  * @summary Verifies that no partially initialized String object escapes from
  31  *          C2's String concat optimization in a highly concurrent setting.
  32  *          This test triggers the bug in about 1 out of 10 runs.
  33  * @compile -XDstringConcat=inline TestStringObjectInitialization.java
  34  * @run main/othervm/timeout=300 -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-CompactStrings
  35  *                               -XX:-UseG1GC -XX:+UseParallelGC TestStringObjectInitialization
  36  */
  37 public class TestStringObjectInitialization {
  38 
  39     String myString;
  40 
  41     public static void main(String[] args) throws Exception {
  42         TestStringObjectInitialization t = new TestStringObjectInitialization();
  43         // Create some threads that concurrently update 'myString'
  44         for (int i = 0; i < 100; ++i) {
  45             (new Thread(new Runner(t))).start();
  46         }
  47         Thread last = new Thread(new Runner(t));
  48         last.start();
  49         last.join();
  50     }
  51 
  52     private void add(String message) {
  53         // String escapes to other threads here
  54         myString += message;
  55     }
  56 
  57     public void run(String s, String[] sArray) {
  58         // Trigger C2's string concatenation optimization
  59         add(s + Arrays.toString(sArray) + " const ");
  60     }
  61 }
  62 
  63 class Runner implements Runnable {
  64     private TestStringObjectInitialization test;
  65 
  66     public Runner(TestStringObjectInitialization t) {
  67         test = t;
  68     }
  69 
  70     public void run(){
  71         String[] array = {"a", "b", "c"};
  72         for (int i = 0; i < 10000; ++i) {
  73             test.run("a", array);
  74         }
  75     }
  76 }
  77