1 /*
 2  * Copyright (c) 2003, 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 import java.io.*;
25 
26 /*
27  * @test
28  *
29  * @summary converted from VM Testbase nsk/jvmti/NativeMethodBind/nativemethbind004.
30  * VM Testbase keywords: [quick, jpda, jvmti, noras]
31  * VM Testbase readme:
32  * DESCRIPTION
33  *     This test exercises the JVMTI event NativeMethodBind.
34  *     It verifies that binding native method can be redirected during
35  *     the event callback.
36  *     The test works as follows. An agent part enables the NativeMethodBind
37  *     generation. Then the java part invokes the native method 'nativeMethod()'
38  *     which leads to the NativeMethodBind generation. In NativeMethodBind
39  *     callback incoming address of the nativeMethod() is changed to the
40  *     address of another native method 'redirNativeMethod()'. Both functions
41  *     nativeMethod() and redirNativeMethod() count their calls.
42  *     In accordance with the spec, the nativeMethod() should not be invoked
43  *     and the redirNativeMethod() should be invoked once.
44  * COMMENTS
45  *     The test has been fixed due to the bug 4967116.
46  *     Fixed the 4995867 bug.
47  *
48  * @library /test/lib
49  * @run main/othervm/native -agentlib:nativemethbind04 nativemethbind04
50  */
51 
52 
53 public class nativemethbind04 {
54     static {
55         try {
56             System.loadLibrary("nativemethbind04");
57         } catch (UnsatisfiedLinkError ule) {
58             System.err.println("Could not load \"nativemethbind04\" library");
59             System.err.println("java.library.path:"
60                 + System.getProperty("java.library.path"));
61             throw ule;
62         }
63     }
64 
65     native void nativeMethod();
66     native int check();
67 
68     public static void main(String[] argv) {
69         new nativemethbind04().runThis();
70     }
71 
72     private void runThis() {
73         // invoke native method to be redirected
74         nativeMethod();
75 
76         int res = check();
77         if (res != 0) {
78             throw new RuntimeException("Check() returned " + res);
79         }
80     }
81 }