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 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 * @requires vm.gc == "Parallel" | vm.gc == "null" 34 * 35 * @compile -XDstringConcat=inline TestStringObjectInitialization.java 36 * @run main/othervm/timeout=300 -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-CompactStrings 37 * -XX:-UseG1GC -XX:+UseParallelGC 38 * compiler.stringopts.TestStringObjectInitialization 39 */ 40 41 package compiler.stringopts; 42 43 import java.util.Arrays; 44 45 public class TestStringObjectInitialization { 46 47 String myString; 48 49 public static void main(String[] args) throws Exception { 50 TestStringObjectInitialization t = new TestStringObjectInitialization(); 51 // Create some threads that concurrently update 'myString' 52 for (int i = 0; i < 100; ++i) { 53 (new Thread(new Runner(t))).start(); 54 } 55 Thread last = new Thread(new Runner(t)); 56 last.start(); 57 last.join(); 58 } 59 60 private void add(String message) { 61 // String escapes to other threads here 62 myString += message; 63 } 64 65 public void run(String s, String[] sArray) { 66 // Trigger C2's string concatenation optimization 67 add(s + Arrays.toString(sArray) + " const "); 68 } 69 70 private static class Runner implements Runnable { 71 private TestStringObjectInitialization test; 72 73 public Runner(TestStringObjectInitialization t) { 74 test = t; 75 } 76 77 public void run() { 78 String[] array = {"a", "b", "c"}; 79 for (int i = 0; i < 10000; ++i) { 80 test.run("a", array); 81 } 82 } 83 } 84 } 85