Skip to content
Permalink
Browse files

8244959: Jextract's VarargsInvoker fails to link functions when passi…

…ng integer types other than long

Reviewed-by: sundar, mcimadamore, jvernee
  • Loading branch information
slowhog committed May 14, 2020
1 parent 111e5da commit f1c6b460711a4e68e97b694717af7947370cbd71
@@ -168,10 +168,20 @@ public class RuntimeHelper {
}
}

private Class<?> promote(Class<?> c) {
if (c == byte.class || c == char.class || c == short.class || c == int.class) {
return long.class;
} else if (c == float.class) {
return double.class;
} else {
return c;
}
}

private Class<?> normalize(Class<?> c) {
c = unboxIfNeeded(c);
if (c.isPrimitive()) {
return c;
return promote(c);
}
if (MemoryAddress.class.isAssignableFrom(c)) {
return MemoryAddress.class;
@@ -183,11 +193,9 @@ public class RuntimeHelper {
}

private MemoryLayout variadicLayout(Class<?> c) {
if (c == char.class || c == byte.class || c == short.class || c == int.class || c == long.class) {
//it is ok to approximate with a machine word here; numerics arguments in a prototype-less
//function call are always rounded up to a register size anyway.
if (c == long.class) {
return C_LONGLONG;
} else if (c == float.class || c == double.class) {
} else if (c == double.class) {
return C_DOUBLE;
} else if (MemoryAddress.class.isAssignableFrom(c)) {
return C_POINTER;
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020, 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 jdk.incubator.foreign.MemorySegment;

import static org.testng.Assert.assertEquals;
import static test.jextract.printf.Cstring.*;
import static test.jextract.printf.printf_h.*;

/*
* @test
* @library ..
* @modules jdk.incubator.jextract
* @run driver JtregJextract -t test.jextract.printf -- printf.h
* @run testng/othervm -Dforeign.restricted=permit Test8244959
*/
public class Test8244959 {
@Test
public void testsPrintf() {
try (MemorySegment s = MemorySegment.allocateNative(1024)) {
sprintf(s.baseAddress(),
toCString("%hhd %c %.2f %.2f %ld %ld %d %hd %d %d %lld %c").baseAddress(),
(byte) 1, 'b', -1.25f, 5.5d, -200l, Long.MAX_VALUE, (byte) -2, (short) 2, 3, (short) -4, 5l, 'a');
String str = toJavaString(s.baseAddress());
assertEquals(str, "1 b -1.25 5.50 -200 " + Long.MAX_VALUE + " -2 2 3 -4 5 a");
}
}
}
@@ -0,0 +1 @@
int sprintf(char *buf, const char *fmt, ...);

0 comments on commit f1c6b46

Please sign in to comment.
You can’t perform that action at this time.