From 0d7385ac8f2d4c4ef695aeec00df106c0cd49dda Mon Sep 17 00:00:00 2001 From: jnbdz <237008+jnbdz@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:31:53 -0500 Subject: [PATCH 1/2] Narrow unsigned 8- and 16-bit return values before converting them Unsigned 8- and 16-bit return values use an int layout, but their conversions did not take an int: the unsigned 16-bit ones take a char, so a method returning char or @As(u16) failed to link with "Return type mismatch", and the unsigned 8-bit ones went through the unsigned 32-bit ones, which take a long on 64-bit platforms, so a method returning @As(u8) failed with "target and filter types do not match". The int return value is now narrowed to char, or masked to its low 8 bits, before the existing conversion, as unsigned arguments are converted to int after it. Test unsigned 8- and 16-bit returns of every integer Java type, and signed 8- and 16-bit ones for comparison. --- .../main/java/io/smallrye/ffm/Bootstraps.java | 21 ++++- .../io/smallrye/ffm/test/ReturnTypesTest.java | 80 +++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/io/smallrye/ffm/Bootstraps.java b/api/src/main/java/io/smallrye/ffm/Bootstraps.java index d724828..1f871f7 100644 --- a/api/src/main/java/io/smallrye/ffm/Bootstraps.java +++ b/api/src/main/java/io/smallrye/ffm/Bootstraps.java @@ -1210,8 +1210,10 @@ 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 - case 's', 'S' -> outputConvOfU16(desc, offs, type, dsChar); + // downcall parameter is unsigned short: its layout is int (see layoutOf), and the unsigned short + // conversions take char, so narrow the value first + case 's', 'S' -> fromInt(staticMethod(Bootstraps.class, "i2c", "(I)C", false), + outputConvOfU16(desc, offs, type, dsChar)); // downcall parameter is unsigned int case 'I', 'i' -> outputConvOfU32(desc, offs, type, dsChar); // downcall parameter is unsigned C long @@ -1304,8 +1306,19 @@ private static MethodHandle outputConvOfS8(final String desc, final int offs, fi } 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); + // the layout is int (see layoutOf); once bits 8-31 are cleared, we can sign-extend into any type safely + return fromInt(staticMethod(Bootstraps.class, "i2ub", "(I)I", false), outputConvOfS32(desc, offs, type, dsChar)); + } + + /** + * Narrow an {@code int} return value before converting it. + * + * @param narrow the narrowing conversion (must not be {@code null}) + * @param conv the conversion of the narrowed value, or {@code null} if the return type already has the narrow type + * @return the combined conversion (not {@code null}) + */ + private static MethodHandle fromInt(final MethodHandle narrow, final MethodHandle conv) { + return conv == null ? narrow : MethodHandles.filterReturnValue(narrow, conv); } private static MethodHandle outputConvOfS16(final String desc, final int offs, final Class type, final char dsChar) { diff --git a/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java b/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java index 010eaf6..9c4d9d9 100644 --- a/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java +++ b/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java @@ -44,6 +44,50 @@ 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)); + } + + /** + * 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)); + } + + /** + * 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); @@ -55,4 +99,40 @@ 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(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(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); } From 1ef50fa6ac804e98c48e6f287d41f42379f757b4 Mon Sep 17 00:00:00 2001 From: jnbdz <237008+jnbdz@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:36:59 -0500 Subject: [PATCH 2/2] Convert unsigned 8- and 16-bit return values with a single method handle Instead of narrowing the int return value with a second method handle before the existing conversion, outputConvOfU8 and outputConvOfU16 now take the int their layout gives and clear the upper bits in the conversion itself, with new helpers (ub2s, us2i, us2l, ...) where no existing one fits. A 16-bit wchar_t keeps its char layout and conversion. Test the unsigned 8- and 16-bit return values of the remaining Java types. --- .../main/java/io/smallrye/ffm/Bootstraps.java | 127 +++++++++++++++--- .../io/smallrye/ffm/test/ReturnTypesTest.java | 38 ++++++ 2 files changed, 147 insertions(+), 18 deletions(-) diff --git a/api/src/main/java/io/smallrye/ffm/Bootstraps.java b/api/src/main/java/io/smallrye/ffm/Bootstraps.java index 1f871f7..80c6b18 100644 --- a/api/src/main/java/io/smallrye/ffm/Bootstraps.java +++ b/api/src/main/java/io/smallrye/ffm/Bootstraps.java @@ -1210,10 +1210,8 @@ 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: its layout is int (see layoutOf), and the unsigned short - // conversions take char, so narrow the value first - case 's', 'S' -> fromInt(staticMethod(Bootstraps.class, "i2c", "(I)C", false), - outputConvOfU16(desc, offs, type, dsChar)); + // 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); // downcall parameter is unsigned C long @@ -1250,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); @@ -1305,20 +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) { - // the layout is int (see layoutOf); once bits 8-31 are cleared, we can sign-extend into any type safely - return fromInt(staticMethod(Bootstraps.class, "i2ub", "(I)I", false), outputConvOfS32(desc, offs, type, dsChar)); - } - - /** - * Narrow an {@code int} return value before converting it. - * - * @param narrow the narrowing conversion (must not be {@code null}) - * @param conv the conversion of the narrowed value, or {@code null} if the return type already has the narrow type - * @return the combined conversion (not {@code null}) - */ - private static MethodHandle fromInt(final MethodHandle narrow, final MethodHandle conv) { - return conv == null ? narrow : MethodHandles.filterReturnValue(narrow, conv); + 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) { @@ -1344,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); @@ -1809,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") diff --git a/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java b/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java index 9c4d9d9..072b5fb 100644 --- a/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java +++ b/api/src/test/java/io/smallrye/ffm/test/ReturnTypesTest.java @@ -60,6 +60,11 @@ public void testUnsignedShortReturn() throws Throwable { 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())); } /** @@ -74,6 +79,12 @@ public void testUnsignedByteReturn() throws Throwable { 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())); } /** @@ -112,6 +123,18 @@ public void testSignedNarrowReturn() throws Throwable { @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); @@ -124,6 +147,21 @@ public void testSignedNarrowReturn() throws Throwable { @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);