1 /*
 2  * Copyright (c) 2020, 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 that specific JVMTI functions returns JVMTI_ERROR_INVALID_THREAD if called with virtual threads.
27  * @compile --enable-preview -source ${jdk.version} VThreadUnsupportedTest.java
28  * @run main/othervm/native --enable-preview -agentlib:VThreadUnsupportedTest VThreadUnsupportedTest
29  */
30 
31 import java.util.concurrent.atomic.AtomicBoolean;
32 
33 public class VThreadUnsupportedTest {
34     private static final String AGENT_LIB = "VThreadUnsupportedTest";
35     final Object lock = new Object();
36     final AtomicBoolean isJNITestingCompleted = new AtomicBoolean(false);
37     
38     native boolean isCompletedTestInEvent(); 
39     native boolean testJvmtiFunctionsInJNICall(Thread vthread);
40 
41     final Runnable pinnedTask = () -> {
42         synchronized (lock) {
43             do {
44                 try { 
45                     lock.wait(10);
46                 } catch (InterruptedException ie) {}
47             } while (!isCompletedTestInEvent() || !isJNITestingCompleted.get());
48         }
49     };
50 
51     void runTest() throws Exception { 
52         Thread vthread = Thread.ofVirtual().name("VThread").start(pinnedTask);
53         testJvmtiFunctionsInJNICall(vthread);
54         isJNITestingCompleted.set(true);
55         vthread.join();
56     }
57 
58     public static void main(String[] args) throws Exception {
59         try {
60             System.loadLibrary(AGENT_LIB);
61         } catch (UnsatisfiedLinkError ex) {
62             System.err.println("Failed to load " + AGENT_LIB + " lib");
63             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
64             throw ex;
65         } 
66         VThreadUnsupportedTest t = new VThreadUnsupportedTest();
67         t.runTest();
68     }
69 }