diff --git a/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/OutputFactory.java b/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/OutputFactory.java index 55580f8e88f..aab9a6b9791 100644 --- a/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/OutputFactory.java +++ b/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/OutputFactory.java @@ -474,7 +474,7 @@ protected static MemoryLayout layoutFor(Declaration decl) { // case like `typedef struct { ... } Foo` } - private void warn(String msg) { + static void warn(String msg) { System.err.println("WARNING: " + msg); } diff --git a/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/StructBuilder.java b/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/StructBuilder.java index 0066a3b9c0c..1985e45a787 100644 --- a/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/StructBuilder.java +++ b/src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/StructBuilder.java @@ -136,6 +136,14 @@ private String findAnonymousStructName(GroupLayout parentLayout, GroupLayout lay @Override public void addVar(String javaName, String nativeName, MemoryLayout layout, Class<?> type) { + try { + structLayout.byteOffset(elementPaths(nativeName)); + } catch (UnsupportedOperationException uoe) { + // bad layout - do nothing + OutputFactory.warn("skipping '" + className() + "." + nativeName + "' : " + uoe.toString()); + return; + } + if (type.equals(MemorySegment.class)) { emitSegmentGetter(javaName, nativeName, layout); } else { diff --git a/test/jdk/tools/jextract/Test8262851.java b/test/jdk/tools/jextract/Test8262851.java new file mode 100644 index 00000000000..dee3e4a2366 --- /dev/null +++ b/test/jdk/tools/jextract/Test8262851.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import org.testng.annotations.Test; +import java.nio.file.Path; +import static org.testng.Assert.assertNotNull; + +/* + * @test + * @modules jdk.incubator.jextract + * @library /test/lib + * @build JextractToolRunner + * @bug 8262851 + * @summary jextract crashes with "Cannot compute size of a layout which is, or depends on a sequence layout with unspecified size" + * @run testng/othervm -Dforeign.restricted=permit Test8262851 + */ +public class Test8262851 extends JextractToolRunner { + @Test + public void test() { + Path output = getOutputFilePath("8262851gen"); + Path outputH = getInputFilePath("test8262851.h"); + run("-d", output.toString(), outputH.toString()).checkSuccess(); + try(Loader loader = classLoader(output)) { + assertNotNull(loader.loadClass("test8262851_h")); + assertNotNull(loader.loadClass("test8262851_h$Odd")); + assertNotNull(loader.loadClass("test8262851_h$Odd$before")); + assertNotNull(loader.loadClass("test8262851_h$Odd$after")); + } finally { + deleteDir(output); + } + } +} diff --git a/test/jdk/tools/jextract/test8262851.h b/test/jdk/tools/jextract/test8262851.h new file mode 100644 index 00000000000..fa36e9fab77 --- /dev/null +++ b/test/jdk/tools/jextract/test8262851.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +struct Odd { + struct { + int size; + char data[]; + } before; + struct { + int a; int b; + } after; +};