Sub topic 10
Restricted methods and --enable-native-access
This chapter assumes you can already create a downcall handle with Linker, SymbolLookup and FunctionDescriptor, and that you know what a module is in the Java module system. It explains why the JVM sometimes prints a warning when you run FFM code and how to control it.
What a restricted method is
A restricted method is an API method that can break memory safety or crash the JVM if you misuse it. The Java compiler does not check the data you hand to native code. If a FunctionDescriptor says a function takes a long but the C function takes a pointer, the JVM cannot detect it.
The platform therefore asks you to say that you accept this risk. In Java 22 the main restricted methods are:
Linker.downcallHandleandLinker.upcallStubMemorySegment.reinterpretAddressLayout.withTargetLayoutSymbolLookup.libraryLookupSystem.load,System.loadLibraryand the matchingRuntimemethods
Segment allocation, get and set calls, and layout construction are not restricted. They are checked against bounds and lifetimes.
Granting native access
Native access is granted per module. Code on the class path belongs to the special "unnamed module", which you name with ALL-UNNAMED.
# Class-path application
java --enable-native-access=ALL-UNNAMED -cp out StrlenDemo
# Modular application: name each module that may call restricted methods
java --enable-native-access=com.example.native -p mods -m com.example.native/com.example.MainAn executable JAR can declare the same permission in its manifest with the line Enable-Native-Access: ALL-UNNAMED. This means users do not need to pass a command-line flag.
The JVM treats a restricted call in one of three ways:
| Command line | Caller module | Result |
|---|---|---|
| No `--enable-native-access` option | Any | Warning printed once per module, call proceeds |
| Option lists the caller's module | Listed | Call proceeds silently |
| Option present but does not list it | Not listed | `IllegalCallerException` |
The last row catches many people. As soon as you pass the option for one module, every unlisted module loses access. This is a strict mode that you can use on purpose to confine native access to a few modules.
Replace that block with the diagram below. Only the fenced diagram counts as part of the chapter.
A restricted call in practice
strerror returns a char *. FFM models the returned pointer as a zero-length segment, because it cannot know how long the string is. Reading it requires reinterpret, which is restricted. This example uses the Java 22 method name getUtf8String. Java 23 and later call it getString.
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
public class StrerrorDemo {
public static void main(String[] args) throws Throwable {
Linker linker = Linker.nativeLinker();
MethodHandle strerror = linker.downcallHandle(
linker.defaultLookup().find("strerror").orElseThrow(),
FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_INT));
MemorySegment msg = (MemorySegment) strerror.invokeExact(2);
System.out.println(msg.byteSize()); // 0: the JVM does not know the length
String text = msg.reinterpret(Long.MAX_VALUE).getUtf8String(0);
System.out.println(text); // "No such file or directory" on Linux
}
}Run it with --enable-native-access=ALL-UNNAMED. Both downcallHandle and reinterpret count as restricted calls, but each module gets only one warning.
Common errors
- A warning on every run. You did not pass the option. Add it to the launch command, the
JAVA_TOOL_OPTIONSvariable, or the JAR manifest. IllegalCallerExceptionin a modular app. You listed some modules but not the one that makes the call. Add that module name to the option.- The flag has no effect. It must come before the main class or
-jar, not after it.
Exercise
A modular application is started with --enable-native-access=com.example.db. A library module com.example.util calls Linker.downcallHandle. What happens, and what are two ways to fix it?
Show answer
The call throws IllegalCallerException, because the option is present and does not list com.example.util. Fix it by adding the module to the list (--enable-native-access=com.example.db,com.example.util), or by moving the native calls into com.example.db.