Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 108 additions & 4 deletions api/src/main/java/io/smallrye/ffm/Bootstraps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ private static MethodHandle outputConvOf(String desc, int offs, Class<?> type) {
case 'u' -> switch (desc.charAt(offs + 1)) {
// downcall parameter is unsigned byte
case 'B', 'c' -> outputConvOfU8(desc, offs, type, dsChar);
// downcall parameter is unsigned short
// downcall parameter is unsigned short (its layout is int, as for unsigned byte)
case 's', 'S' -> outputConvOfU16(desc, offs, type, dsChar);
// downcall parameter is unsigned int
case 'I', 'i' -> outputConvOfU32(desc, offs, type, dsChar);
Expand Down Expand Up @@ -1248,7 +1248,7 @@ private static MethodHandle outputConvOf(String desc, int offs, Class<?> type) {
? stdc_wchar_is_32bit ? outputConvOfS32(desc, offs, type, dsChar)
: outputConvOfS16(desc, offs, type, dsChar)
: stdc_wchar_is_32bit ? outputConvOfU32(desc, offs, type, dsChar)
: outputConvOfU16(desc, offs, type, dsChar);
: outputConvOfWchar16(desc, offs, type, dsChar);
case '*' -> outputConvOfPtr(desc, offs, type);
case 'Z' -> outputConvOfBool(desc, offs, type, dsChar);
case 'F' -> outputConvOfF32(desc, offs, type, dsChar);
Expand Down Expand Up @@ -1303,9 +1303,28 @@ private static MethodHandle outputConvOfS8(final String desc, final int offs, fi
};
}

// the layout is int (see layoutOf); bits 8-31 are not guaranteed to be zero, so each conversion clears them
private static MethodHandle outputConvOfU8(final String desc, final int offs, final Class<?> type, final char dsChar) {
// bits 8-31 are zero, so we can sign-extend into any type safely
return outputConvOfU32(desc, offs, type, dsChar);
return switch (dsChar) {
case 'Z' -> staticMethod(Bootstraps.class, "ub2z", "(I)Z", false);
case 'B' -> staticMethod(Bootstraps.class, "i2b", "(I)B", false);
case 'S' -> staticMethod(Bootstraps.class, "ub2s", "(I)S", false);
case 'C' -> staticMethod(Bootstraps.class, "ub2c", "(I)C", false);
case 'I' -> staticMethod(Bootstraps.class, "i2ub", "(I)I", false);
case 'J' -> staticMethod(Bootstraps.class, "ub2l", "(I)J", false);
case 'D' -> staticMethod(Bootstraps.class, "ub2d", "(I)D", false);
case 'F' -> staticMethod(Bootstraps.class, "ub2f", "(I)F", false);
case 'L' -> {
if (NativeIntValued.class.isAssignableFrom(type)) {
yield MethodHandles.filterReturnValue(
staticMethod(Bootstraps.class, "i2ub", "(I)I", false),
staticMethod(type, "ofNativeValue", "(I)" + type.descriptorString(), false));
} else {
throw invalidTypeForDescType(desc, offs, type);
}
}
default -> throw invalidTypeForDescType(desc, offs, type);
};
}

private static MethodHandle outputConvOfS16(final String desc, final int offs, final Class<?> type, final char dsChar) {
Expand All @@ -1331,7 +1350,33 @@ private static MethodHandle outputConvOfS16(final String desc, final int offs, f
};
}

// the layout is int (see layoutOf); bits 16-31 are not guaranteed to be zero, so each conversion clears them
private static MethodHandle outputConvOfU16(final String desc, final int offs, final Class<?> type, final char dsChar) {
return switch (dsChar) {
case 'Z' -> staticMethod(Bootstraps.class, "us2z", "(I)Z", false);
case 'B' -> staticMethod(Bootstraps.class, "i2b", "(I)B", false);
case 'S' -> staticMethod(Bootstraps.class, "i2s", "(I)S", false);
case 'C' -> staticMethod(Bootstraps.class, "i2c", "(I)C", false);
case 'I' -> staticMethod(Bootstraps.class, "us2i", "(I)I", false);
case 'J' -> staticMethod(Bootstraps.class, "us2l", "(I)J", false);
case 'D' -> staticMethod(Bootstraps.class, "us2d", "(I)D", false);
case 'F' -> staticMethod(Bootstraps.class, "us2f", "(I)F", false);
case 'L' -> {
if (NativeIntValued.class.isAssignableFrom(type)) {
yield MethodHandles.filterReturnValue(
staticMethod(Bootstraps.class, "us2i", "(I)I", false),
staticMethod(type, "ofNativeValue", "(I)" + type.descriptorString(), false));
} else {
throw invalidTypeForDescType(desc, offs, type);
}
}
default -> throw invalidTypeForDescType(desc, offs, type);
};
}

// a 16-bit wchar_t uses the canonical layout, which is char
private static MethodHandle outputConvOfWchar16(final String desc, final int offs, final Class<?> type,
final char dsChar) {
return switch (dsChar) {
case 'Z' -> staticMethod(Bootstraps.class, "c2z", "(C)Z", false);
case 'B' -> staticMethod(Bootstraps.class, "c2b", "(C)B", false);
Expand Down Expand Up @@ -1796,6 +1841,65 @@ private static double ul2d(long val) {
return (double) ((val >>> 1) | (val & 1)) * 2.0;
}

// unsigned byte (in an int) to...

@SuppressWarnings("unused")
private static boolean ub2z(int val) {
return (val & 0xff) != 0;
}

@SuppressWarnings("unused")
private static short ub2s(int val) {
return (short) (val & 0xff);
}

@SuppressWarnings("unused")
private static char ub2c(int val) {
return (char) (val & 0xff);
}

@SuppressWarnings("unused")
private static long ub2l(int val) {
return val & 0xff;
}

@SuppressWarnings("unused")
private static float ub2f(int val) {
return val & 0xff;
}

@SuppressWarnings("unused")
private static double ub2d(int val) {
return val & 0xff;
}

// unsigned short (in an int) to...

@SuppressWarnings("unused")
private static boolean us2z(int val) {
return (val & 0xffff) != 0;
}

@SuppressWarnings("unused")
private static int us2i(int val) {
return val & 0xffff;
}

@SuppressWarnings("unused")
private static long us2l(int val) {
return val & 0xffff;
}

@SuppressWarnings("unused")
private static float us2f(int val) {
return val & 0xffff;
}

@SuppressWarnings("unused")
private static double us2d(int val) {
return val & 0xffff;
}

// float to...

@SuppressWarnings("unused")
Expand Down
118 changes: 118 additions & 0 deletions api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,61 @@ public void testErrnoReturn() throws Throwable {
assertEquals(Errno.EDOM, atoi_as_errno(String.valueOf(Errno.EDOM.nativeValue())));
}

/**
* Test that unsigned 16-bit return values are linked and converted correctly. Their layout is {@code int}, so the
* value must be narrowed to 16 bits before it is converted to the Java return type. {@code abs} is used because it
* is available everywhere and returns its argument unchanged for these values.
*/
@Test
public void testUnsignedShortReturn() throws Throwable {
assumeTrue(ReturnTypesTest.class.getModule().isNativeAccessEnabled());
assertEquals('A', absChar(65));
assertEquals('\uffff', absChar(0xffff));
assertEquals('\ufedc', absChar(0x1fedc));
assertEquals((short) 0xfedc, absU16Short(0xfedc));
assertEquals(0xfedc, absU16Int(0xfedc));
assertEquals(0xfedc, absU16Int(0x1fedc));
assertEquals(0xfedcL, absU16Long(0xfedc));
assertEquals(0xfedcL, absU16Long(0x1fedc));
assertTrue(absU16Boolean(1));
assertFalse(absU16Boolean(0x10000));
assertEquals(65244.0f, absU16Float(0x1fedc));
assertEquals(65244.0, absU16Double(0x1fedc));
assertEquals(Errno.EDOM, absU16Errno(0x10000 + Errno.EDOM.nativeValue()));
}

/**
* Test that unsigned 8-bit return values are linked and converted correctly. Their layout is {@code int} too.
*/
@Test
public void testUnsignedByteReturn() throws Throwable {
assumeTrue(ReturnTypesTest.class.getModule().isNativeAccessEnabled());
assertEquals((byte) 200, absU8Byte(200));
assertEquals((short) 200, absU8Short(200));
assertEquals(200, absU8Int(200));
assertEquals(200, absU8Int(0x1c8));
assertEquals(200L, absU8Long(200));
assertEquals(200L, absU8Long(0x1c8));
assertTrue(absU8Boolean(1));
assertFalse(absU8Boolean(0x100));
assertEquals((char) 200, absU8Char(0x1c8));
assertEquals(200.0f, absU8Float(0x1c8));
assertEquals(200.0, absU8Double(0x1c8));
assertEquals(Errno.EDOM, absU8Errno(0x100 + Errno.EDOM.nativeValue()));
}

/**
* Test that signed 8- and 16-bit return values are sign-extended.
*/
@Test
public void testSignedNarrowReturn() throws Throwable {
assumeTrue(ReturnTypesTest.class.getModule().isNativeAccessEnabled());
assertEquals(-56, absS8Int(200));
assertEquals(-56L, absS8Long(200));
assertEquals(-292, absS16Int(0xfedc));
assertEquals(-292L, absS16Long(0xfedc));
}

@Link
private static native @As(stdc_int) long atoi(String numberString);

Expand All @@ -55,4 +110,67 @@ public void testErrnoReturn() throws Throwable {

@Link(name = "atoi")
private static native Errno atoi_as_errno(String numberString);

@Link(name = "abs")
private static native char absChar(int value);

@Link(name = "abs")
private static native @As(u16) short absU16Short(int value);

@Link(name = "abs")
private static native @As(stdc_unsigned_short) int absU16Int(int value);

@Link(name = "abs")
private static native @As(u16) long absU16Long(int value);

@Link(name = "abs")
private static native @As(u16) boolean absU16Boolean(int value);

@Link(name = "abs")
private static native @As(u16) float absU16Float(int value);

@Link(name = "abs")
private static native @As(u16) double absU16Double(int value);

@Link(name = "abs")
private static native @As(u16) Errno absU16Errno(int value);

@Link(name = "abs")
private static native @As(u8) byte absU8Byte(int value);

@Link(name = "abs")
private static native @As(stdc_unsigned_char) short absU8Short(int value);

@Link(name = "abs")
private static native @As(u8) int absU8Int(int value);

@Link(name = "abs")
private static native @As(u8) long absU8Long(int value);

@Link(name = "abs")
private static native @As(u8) boolean absU8Boolean(int value);

@Link(name = "abs")
private static native @As(u8) char absU8Char(int value);

@Link(name = "abs")
private static native @As(u8) float absU8Float(int value);

@Link(name = "abs")
private static native @As(u8) double absU8Double(int value);

@Link(name = "abs")
private static native @As(u8) Errno absU8Errno(int value);

@Link(name = "abs")
private static native @As(s8) int absS8Int(int value);

@Link(name = "abs")
private static native @As(s8) long absS8Long(int value);

@Link(name = "abs")
private static native @As(s16) int absS16Int(int value);

@Link(name = "abs")
private static native @As(s16) long absS16Long(int value);
}
Loading