1 /*
 2  * Copyright (c) 2021, 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.lang.reflect.Method;
25 import java.nio.file.Path;
26 import jdk.incubator.foreign.MemoryAddress;
27 import jdk.incubator.foreign.MemorySegment;
28 import org.testng.annotations.Test;
29 import static org.testng.Assert.assertNotNull;
30 import static org.testng.Assert.assertTrue;
31 
32 /*
33  * @test
34  * @library /test/lib
35  * @modules jdk.incubator.jextract
36  * @build JextractToolRunner
37  * @bug 8260705
38  * @summary jextract crash with libbart's types.h
39  * @run testng/othervm --enable-native-access=jdk.incubator.jextract Test8260705
40  */
41 public class Test8260705 extends JextractToolRunner {
42     @Test
43     public void test() {
44         Path outputPath = getOutputFilePath("output");
45         Path headerFile = getInputFilePath("test8260705.h");
46         run("-d", outputPath.toString(), headerFile.toString()).checkSuccess();
47         try(Loader loader = classLoader(outputPath)) {
48             Class<?> FooClass = loader.loadClass("Foo");
49             checkMethod(FooClass, "c$get", byte.class, MemorySegment.class);
50             checkMethod(FooClass, "c$get", byte.class, MemorySegment.class, long.class);
51             checkMethod(FooClass, "c$set", void.class, MemorySegment.class, byte.class);
52             checkMethod(FooClass, "c$set", void.class, MemorySegment.class, long.class, byte.class);
53 
54             Class<?> Foo2Class = loader.loadClass("Foo2");
55             checkMethod(Foo2Class, "z$get", int.class, MemorySegment.class);
56             checkMethod(Foo2Class, "z$get", int.class, MemorySegment.class, long.class);
57             checkMethod(Foo2Class, "z$set", void.class, MemorySegment.class, int.class);
58             checkMethod(Foo2Class, "z$set", void.class, MemorySegment.class, long.class, int.class);
59             checkMethod(Foo2Class, "w$get", int.class, MemorySegment.class);
60             checkMethod(Foo2Class, "w$get", int.class, MemorySegment.class, long.class);
61             checkMethod(Foo2Class, "w$set", void.class, MemorySegment.class, int.class);
62             checkMethod(Foo2Class, "w$set", void.class, MemorySegment.class, long.class, int.class);
63 
64             assertNotNull(loader.loadClass("Foo3"));
65 
66             Class<?> Foo4Class = loader.loadClass("Foo4");
67             assertTrue(sizeof(Foo4Class) == 8L);
68 
69             Class<?> Foo5Class = loader.loadClass("Foo5");
70             assertTrue(sizeof(Foo5Class) == 4L);
71 
72         } finally {
73             deleteDir(outputPath);
74         }
75     }
76 
77     private long sizeof(Class<?> cls) {
78         Method m = findMethod(cls, "sizeof");
79         try {
80             return (long)m.invoke(null);
81         } catch (Exception ex) {
82             throw new RuntimeException(ex);
83         }
84     }
85 }
86