Sub topic 7
Describing a C signature with FunctionDescriptor
A FunctionDescriptor tells the Linker the shape of a C function: the layout of each parameter and of the return value. This chapter assumes you know the ValueLayout constants from the earlier chapters and can read a C prototype. The next chapter uses descriptors to create downcall handles.
Why the linker needs a descriptor
A native symbol is only an address. It carries no information about its parameters. The linker must know whether an argument goes in an integer register, a floating-point register, or on the stack, and how wide it is. The descriptor supplies this information. If the descriptor does not match the real C signature, the call does not fail cleanly. It can pass garbage, return garbage, or crash the JVM.
Building a descriptor
Two factory methods cover every case:
FunctionDescriptor.of(returnLayout, argLayouts...)for functions that return a value.FunctionDescriptor.ofVoid(argLayouts...)for functions that returnvoid.
The return layout always comes first, as in a C prototype. The parameter layouts follow in declaration order.
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.ValueLayout;
public class DescriptorDemo {
public static void main(String[] args) {
// size_t strlen(const char *s);
FunctionDescriptor strlen = FunctionDescriptor.of(
ValueLayout.JAVA_LONG, ValueLayout.ADDRESS);
// void srand(unsigned int seed);
FunctionDescriptor srand = FunctionDescriptor.ofVoid(ValueLayout.JAVA_INT);
// void *memcpy(void *dest, const void *src, size_t n);
FunctionDescriptor memcpy = FunctionDescriptor.of(
ValueLayout.ADDRESS,
ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG);
System.out.println(strlen.toMethodType());
System.out.println(srand.toMethodType());
System.out.println(memcpy.toMethodType());
System.out.println(srand.returnLayout().isPresent());
}
}The program prints:
(MemorySegment)long
(int)void
(MemorySegment,MemorySegment,long)MemorySegment
falsetoMethodType() shows the Java signature of the handle the linker will produce. ADDRESS becomes MemorySegment, and the other layouts become their matching Java primitives. returnLayout() returns an Optional, which is empty for a void function.
Mapping C types to layouts
Choose each layout by the size and kind of the C type on your target platform, not by the name of the type.
| C type | Layout | Note |
|---|---|---|
| `int`, `unsigned int` | `JAVA_INT` | Java has no unsigned types. The bits are the same. |
| `long long`, `int64_t` | `JAVA_LONG` | Always 64 bits. |
| `size_t` | `JAVA_LONG` | Correct on 64-bit platforms only. |
| `long` | `JAVA_LONG` or `JAVA_INT` | 64 bits on Linux and macOS, 32 bits on 64-bit Windows. |
| `double` | `JAVA_DOUBLE` | |
| `char` | `JAVA_BYTE` | A single character, not a string. |
| any pointer | `ADDRESS` | Strings, arrays, structs by pointer and callbacks. |
Pointers that carry a target layout
A plain ADDRESS returns a zero-length MemorySegment, so you cannot read through it directly. For a pointer you will dereference, such as the comparator arguments in the earlier qsort example, use ADDRESS.withTargetLayout(...). The returned segment then has the size of the target layout. Descriptors are immutable, and you can derive variants with appendArgumentLayouts, changeReturnLayout or dropReturnLayout.
Common errors
- Mismatch with the handle. Calling with
invokeExactrequires the Java argument types to matchtoMethodType()exactly. Passing anintwhere the descriptor saysJAVA_LONGthrowsWrongMethodTypeException. - Forgetting the return layout. Using
ofVoidfor a function that returns a value discards the result. Usingoffor avoidfunction reads an undefined register. - Passing structs by value. Pass the struct's
StructLayoutin the descriptor instead ofADDRESS. A pointer to a struct still usesADDRESS.
Exercise
Write the FunctionDescriptor for the C function double pow(double base, double exponent); and state the MethodType it produces.
Show answer
FunctionDescriptor.of(ValueLayout.JAVADOUBLE, ValueLayout.JAVADOUBLE, ValueLayout.JAVA_DOUBLE). The return layout comes first, followed by the two parameter layouts. toMethodType() gives (double,double)double.
Exercise
A C function is declared as int write_all(int fd, const void *buf, size_t n);. Which layouts do you use, and why is JAVA_LONG for n only safe on some platforms?
Show answer
Use JAVAINT for the return value and for fd, ADDRESS for buf, and JAVALONG for n. sizet is 64 bits on 64-bit platforms, so JAVALONG matches there. On a 32-bit platform sizet is 32 bits, and JAVALONG would be wrong.