Rust just fixed a bug with booleans returned across language boundaries.

I initially thought Java handled this situation safely because its Foreign Function & Memory API did not provide a Boolean layout. That explanation was wrong. Java provides ValueLayout.JAVA_BOOLEAN. Java does handle the returned value differently, but it can still give the wrong answer.

Using booleans in Foreign Function Interfaces (FFI) requires agreement on the native type and the application binary interface (ABI). The ABI specifies how values are passed, including which bits of a return register must be defined. Two languages agreeing on what true and false mean is not enough if the generated machine code breaks that contract.

The Rust bug report gives a small example:

pub unsafe extern "C" fn rust_fn_returning_bool(flags: u8) -> bool {
    (flags & 0x1) == 0x1
}

This function tests the lowest bit. With input 0x10, or decimal 16, that bit is zero, so the answer should be false.

However, Rust 1.99.0 generated code that copied the input into the return register without masking it. The caller consequently received 16. The x8664 ABI requires bits 1 through 7 of a Boolean return to be zero, but the compiler had omitted the zero-extension marking needed to preserve that requirement. Rust 1.99.1 restored it.

This was not C allowing 16 as a valid Boolean representation. C conditionals treat zero as false and nonzero as true, but converting a scalar value to C's _Bool type produces 0 or 1. C has had that built-in type since C99. C++ also has a built-in bool type. A compiler can rely on the Boolean return contract rather than defensively normalizing every result from another language.

Rust's Boolean validity rules permit only the byte representations 0 and 1. An invalid representation entering a Rust bool violates those rules and causes undefined behavior. But the bug here originated in the compiler's handling of an otherwise valid Rust function, not in the programmer choosing an invalid Boolean value.

Java 22's Foreign Function & Memory API lets Java call native functions. It provides ValueLayout.JAVA_BOOLEAN to describe a Boolean value passed between Java and native code. On Linux/x64, Java's native linker uses this layout for C bool.

I checked how OpenJDK 22 reads a Boolean returned by a native function. It takes the lowest byte of the returned value and checks whether it is zero. Zero becomes false; any other byte value becomes true. This conversion is visible in the FFM code and its conversion helper.

So what happens when the broken Rust function returns 16? Java turns it into true because 16 is not zero. But the Rust function was checking the lowest bit, which is zero in 16. The intended answer was false.

I tested this on OpenJDK 22.0.2 on Linux/x64, using a small native library with the broken and corrected machine instructions from the Rust bug report. This tested the same returned values without requiring the affected Rust compiler.

Input Expected answer Java with broken native code Java with corrected native code
0x00 false false false
0x01 true true true
0x10 false true false
0x11 true true true

Across all 256 possible byte inputs, the broken code caused Java to return true for 127 inputs that should have produced false. These results describe the OpenJDK version and platform tested.

Java therefore handles the returned value differently, but that does not make the answer correct. Rust allows only 0 and 1 as valid bool representations. OpenJDK's FFM code converts the returned byte into a Java true or false. It can turn an unexpected value into a valid Boolean, but it cannot know what the native function meant to return.

The Java declaration must still match the native function. Use the appropriate Boolean layout for a native Boolean. If the function returns an integer flag, use the matching integer layout and follow the library's rules for interpreting it. For example, Win32 BOOL is an int, so it uses ValueLayout.JAVA_INT on Windows. Switching a Boolean layout to a byte layout does not fix the underlying compiler bug.

I used to think boolean was the simplest data type. But this bug changed my views. A value can be valid for the receiving language and still be wrong for the program.