Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8260705: jextract crash with libbart's types.h #443

Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
@@ -187,6 +187,13 @@ public String toString() {

public Declaration.Scoped createScoped(Cursor c, Declaration.Scoped.Kind scopeKind, ScopedFactoryLayout factoryLayout, ScopedFactoryNoLayout factoryNoLayout) {
List<Declaration> decls = filterNestedDeclarations(c.children()
.filter(fc -> {
if (fc.isBitField()) {
// only non-empty and named bit fields are generated
return fc.getBitFieldWidth() != 0 && !fc.spelling().isEmpty();
}
return true;
})
.map(this::createTree).collect(Collectors.toList()));
if (c.isDefinition()) {
//just a declaration AND definition, we have a layout
@@ -0,0 +1,86 @@
/*
* 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 java.lang.reflect.Method;
import java.nio.file.Path;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

/*
* @test
* @library /test/lib
* @modules jdk.incubator.jextract
* @build JextractToolRunner
* @bug 8260705
* @summary jextract crash with libbart's types.h
* @run testng/othervm -Dforeign.restricted=permit Test8260705
*/
public class Test8260705 extends JextractToolRunner {
@Test
public void testPointerFields() {
Path outputPath = getOutputFilePath("output");
Path headerFile = getInputFilePath("test8260705.h");
run("-d", outputPath.toString(), headerFile.toString()).checkSuccess();
try(Loader loader = classLoader(outputPath)) {
Class<?> FooClass = loader.loadClass("test8260705_h$Foo");
checkMethod(FooClass, "c$get", byte.class, MemorySegment.class);
checkMethod(FooClass, "c$get", byte.class, MemorySegment.class, long.class);
checkMethod(FooClass, "c$set", void.class, MemorySegment.class, byte.class);
checkMethod(FooClass, "c$set", void.class, MemorySegment.class, long.class, byte.class);

This conversation was marked as resolved by JornVernee

This comment has been minimized.

Loading
@JornVernee

JornVernee Feb 1, 2021
Member

Might be nice to check the offset of c here, to see if it's properly the second byte of the struct. (since the bitfield before it is supposed to align c to the next byte).

Class<?> Foo2Class = loader.loadClass("test8260705_h$Foo2");
checkMethod(Foo2Class, "z$get", int.class, MemorySegment.class);
checkMethod(Foo2Class, "z$get", int.class, MemorySegment.class, long.class);
checkMethod(Foo2Class, "z$set", void.class, MemorySegment.class, int.class);
checkMethod(Foo2Class, "z$set", void.class, MemorySegment.class, long.class, int.class);
checkMethod(Foo2Class, "w$get", int.class, MemorySegment.class);
checkMethod(Foo2Class, "w$get", int.class, MemorySegment.class, long.class);
checkMethod(Foo2Class, "w$set", void.class, MemorySegment.class, int.class);
checkMethod(Foo2Class, "w$set", void.class, MemorySegment.class, long.class, int.class);

assertNotNull(loader.loadClass("test8260705_h$Foo3"));

Class<?> Foo4Class = loader.loadClass("test8260705_h$Foo4");
assertTrue(sizeof(Foo4Class) == 8L);

Class<?> Foo5Class = loader.loadClass("test8260705_h$Foo5");
assertTrue(sizeof(Foo5Class) == 4L);

} finally {
deleteDir(outputPath);
}
}

private long sizeof(Class<?> cls) {
Method m = findMethod(cls, "sizeof");
try {
return (long)m.invoke(null);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}

@@ -0,0 +1,54 @@
/*
* 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 Foo {
int: 0;
char c;
};

struct Foo2 {
int z;
int: 16;
int y:16;
int w;
};

struct Foo3 {
int: 0;
int: 32;
};

struct Foo4 {
int a:3;
int b:2;
int :0; // Force alignment to next boundary.
int c:4;
int d:3;
};

struct Foo5 {
int a:3;
int b:2;
int c:4;
int d:3;
};