1 /*
   2  * Copyright (c) 2019, SAP SE. 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 import jdk.test.lib.*;
  25 import jdk.test.lib.process.*;
  26 
  27 /*
  28  * @test TestAbortVMOnSafepointTimeout
  29  * @summary Check if VM can kill thread which doesn't reach safepoint.
  30  * @bug 8219584 8227528
  31  * @requires vm.compiler2.enabled
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.misc
  34  *          java.management
  35  * @run driver TestAbortVMOnSafepointTimeout
  36  */
  37 
  38 public class TestAbortVMOnSafepointTimeout {
  39 
  40     public static void main(String[] args) throws Exception {
  41         if (args.length > 0) {
  42             int result = test_loop(3);
  43             System.out.println("This message would occur after some time with result " + result);
  44             return;
  45         }
  46 
  47         testWith(500, 500);
  48     }
  49 
  50     static int test_loop(int x) {
  51         int sum = 0;
  52         if (x != 0) {
  53             // Long running loop without safepoint.
  54             for (int y = 1; y < Integer.MAX_VALUE; ++y) {
  55                 if (y % x == 0) ++sum;
  56             }
  57         }
  58         return sum;
  59     }
  60 
  61     public static void testWith(int sfpt_interval, int timeout_delay) throws Exception {
  62         // -XX:-UseCountedLoopSafepoints - is used to prevent the loop
  63         // in test_loop() to poll for safepoints.
  64         // -XX:LoopStripMiningIter=0 and -XX:LoopUnrollLimit=0 - are
  65         // used to prevent optimizations over the loop in test_loop()
  66         // since we actually want it to provoke a safepoint timeout.
  67         // -XX:-UseBiasedLocking - is used to prevent biased locking
  68         // handshakes from changing the timing of this test.
  69         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  70                 "-XX:+UnlockDiagnosticVMOptions",
  71                 "-XX:-UseBiasedLocking",
  72                 "-XX:+SafepointTimeout",
  73                 "-XX:+SafepointALot",
  74                 "-XX:+AbortVMOnSafepointTimeout",
  75                 "-XX:SafepointTimeoutDelay=" + timeout_delay,
  76                 "-XX:GuaranteedSafepointInterval=" + sfpt_interval,
  77                 "-XX:-TieredCompilation",
  78                 "-XX:-UseCountedLoopSafepoints",
  79                 "-XX:LoopStripMiningIter=0",
  80                 "-XX:LoopUnrollLimit=0",
  81                 "-XX:CompileCommand=compileonly,TestAbortVMOnSafepointTimeout::test_loop",
  82                 "-Xcomp",
  83                 "-XX:-CreateCoredumpOnCrash",
  84                 "-Xms64m",
  85                 "TestAbortVMOnSafepointTimeout",
  86                 "runTestLoop"
  87         );
  88 
  89         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  90         if (Platform.isWindows()) {
  91             output.shouldMatch("Safepoint sync time longer than");
  92         } else {
  93             output.shouldMatch("SIGILL");
  94             if (Platform.isLinux()) {
  95                 output.shouldMatch("(sent by kill)");
  96             }
  97             output.shouldMatch("TestAbortVMOnSafepointTimeout.test_loop");
  98         }
  99         output.shouldNotHaveExitValue(0);
 100     }
 101 }