Skip to content

Narrow unsigned 8- and 16-bit return values before converting them - #92

Open
jnbdz wants to merge 2 commits into
smallrye:mainfrom
SiteNetSoft:fix-u16-return
Open

jnbdz wants to merge 2 commits into
smallrye:mainfrom
SiteNetSoft:fix-u16-return

Conversation

@jnbdz

@jnbdz jnbdz commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

A method whose return value is an unsigned 16-bit type fails to link, whatever its Java type. For example:

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

fails at the first call with:

java.lang.IllegalArgumentException: Return type mismatch
		Expected: (MemorySegment,int)char
		Was: (MemorySegment,int)int

layoutOf gives unsigned 8- and 16-bit values an int layout, for return values as well as parameters, but outputConvOfU16's conversions take a char. For char the conversion is null, so the int returned by the downcall handle is left as it is; for the other types (c2s, c2i, c2l, ...) filterReturnValue rejects the mismatched types.

Unsigned 8-bit return values have the same problem in another form: outputConvOfU8 uses the unsigned 32-bit conversions, which on 64-bit platforms take a long, so @As(u8) int (or any other @As(u8) return) fails with target and filter types do not match: (MemorySegment,int)int, (long)byte. Signed 8- and 16-bit return values use byte and short layouts and are not affected.

I found this while answering the review on #86, which fixes the same mismatch for unsigned 16-bit parameters.

The fix

outputConvOfU8 and outputConvOfU16 now take the int from the layout and clear the upper bits inside a single conversion (new helpers ub2z, ub2s, ub2c, ub2l, ub2f, ub2d, us2z, us2i, us2l, us2f, us2d; the existing i2b, i2s, i2c and i2ub where they already do the right thing), so each return value gets at most one conversion handle. The only two-step case is NativeIntValued, which already chains to ofNativeValue on main.

Masking the value also means that the result does not depend on the upper bits of the return register, which the C ABIs do not require the callee to clear for narrow return types. wchar_t return values, which use the canonical wchar_t layout (a char on Windows), keep their conversion (outputConvOfWchar16). This PR does not depend on #86 and merges cleanly with it.

Tests

ReturnTypesTest gains tests for unsigned 16-bit returns (char, @As(u16) short, @As(stdc_unsigned_short) int, @As(u16) long), unsigned 8-bit returns (@As(u8) byte, @As(stdc_unsigned_char) short, @As(u8) int, @As(u8) long), signed 8- and 16-bit returns for comparison, and boolean, char, float, double and Errno returns with values that need the upper bits cleared. Since there is no function returning unsigned short in the default lookup on all three CI platforms (htons is not in it on Windows), they link abs, which returns its argument unchanged for these values; the int layout makes that call ABI-compatible, and values above the narrow type's range (such as 0x1fedc) check the narrowing.

On main the unsigned tests fail with the errors above; with the fix, mvn verify passes: 56 tests (3 skipped, as on main: platform- or library-specific tests).

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.
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.
@jnbdz

jnbdz commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Following your comment on #86, I've changed this PR the same way: outputConvOfU8 and outputConvOfU16 now take the int from the layout and clear the upper bits inside the one conversion (ub2s, ub2c, ub2l, us2i, us2l, ... and the existing i2b/i2s/i2c/i2ub where they already do the right thing), so there's no extra handle any more. The only remaining two-step case is NativeIntValued, which already chains to ofNativeValue on main. A 16-bit wchar_t keeps its char layout and conversion (outputConvOfWchar16). The tests now cover the remaining return types too. #86 and #92 still merge cleanly, and the merged result passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant