1 /*
  2  * Copyright (c) 2018, 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  * @test
 26  * @summary Verifies JVMTI support for Continuations
 27  * @compile ContStackDepthTest.java
 28  * @modules java.base/jdk.internal.vm
 29  * @run main/othervm/native -agentlib:ContStackDepthTest ContStackDepthTest
 30  */
 31 
 32 import java.math.BigInteger;
 33 import java.math.BigInteger.*;
 34 import jdk.internal.vm.Continuation;
 35 import jdk.internal.vm.ContinuationScope;
 36 
 37 public class ContStackDepthTest {
 38     private static final String agentLib = "ContStackDepthTest";
 39     private static ContinuationScope scope = new ContinuationScope("Fibonacci") {};
 40     private static Continuation cont;
 41     private static BigInteger value = BigInteger.ONE;
 42     private static boolean done = false;
 43 
 44     static void log(String str) { System.out.println(str); }
 45 
 46     static native void enableEvents(Thread thread);
 47     static native boolean check();
 48 
 49     public static void main(String[] args) throws Exception {
 50         try {
 51             System.loadLibrary(agentLib);
 52         } catch (UnsatisfiedLinkError ex) {
 53             log("Failed to load " + agentLib + " lib");
 54             log("java.library.path: " + System.getProperty("java.library.path"));
 55             throw ex;
 56         }
 57         log("\n######   main: started   #####\n");
 58         enableEvents(Thread.currentThread());
 59 
 60         ContStackDepthTest obj = new ContStackDepthTest();
 61         obj.runTest();
 62 
 63         if (!check()) {
 64             throw new RuntimeException(
 65                 "ContStackDepthTest failed: miscounted FramePop, MethodEnter or MethodExit events!");
 66         }
 67         log("ContStackDepthTest passed\n");
 68         log("\n#####   main: finished  #####\n");
 69     }
 70 
 71     public void runTest() {
 72         log("\n####  runTest: started  ####\n");
 73         fibTest();
 74         log("\n####  runTest: finished ####\n");
 75     }
 76 
 77     static final Runnable FIB = () -> {
 78         Continuation.yield(scope);
 79         var cur = value;
 80         var next = BigInteger.ONE;
 81         int iter = 0;
 82 
 83         while (true) {
 84             log("\n  ##  FIB iteration: " + (++iter) + "  ##\n");
 85             if (done) return;
 86 
 87             value = next;
 88             Continuation.yield(scope);
 89             var tmp = cur.add(next);
 90             cur = next;
 91             next = tmp;
 92         }
 93     };
 94 
 95     public static void fibTest() {
 96         cont = new Continuation(scope, FIB);
 97         log("\n####  fibTest: started  ####\n");
 98 
 99         System.out.println("getNextFib returned value: " + getNextFib());
100         System.out.println("getNextFib returned value: " + getNextFib());
101         System.out.println("getNextFib returned value: " + getNextFib());
102 
103         done = true;
104 
105         System.out.println("getNextFib returned value: " + getNextFib());
106 
107         log("\n####  fibTest: finished ####\n");
108     }
109 
110     public static BigInteger getNextFib() {
111         cont.run();
112         return value;
113     }
114 }