APIdia Launch Partner
Top Inners Fields Constructors Methods
jdk.internal.foreign.abi

public abstract sealed Class AbstractLinker

extends Object
implements Linker
permits LinuxAArch64Linker, MacOsAArch64Linker, SysVx64Linker, WindowsAArch64Linker, Windowsx64Linker, AixPPC64Linker, LinuxPPC64Linker, LinuxPPC64leLinker, LinuxRISCV64Linker, LinuxS390Linker, FallbackLinker
Class Inheritance
All Implemented Interfaces
java.lang.foreign.Linker
Known Direct Subclasses
jdk.internal.foreign.abi.aarch64.linux.LinuxAArch64Linker, jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker, jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker, jdk.internal.foreign.abi.fallback.FallbackLinker, jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker, jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64leLinker, jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker, jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker, jdk.internal.foreign.abi.s390.linux.LinuxS390Linker, jdk.internal.foreign.abi.x64.sysv.SysVx64Linker, jdk.internal.foreign.abi.x64.windows.Windowsx64Linker
Imports
jdk.internal.foreign.SystemLookup, .Utils, jdk.internal.foreign.abi.aarch64.linux.LinuxAArch64Linker, jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker, jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker, jdk.internal.foreign.abi.fallback.FallbackLinker, jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker, jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker, .LinuxPPC64leLinker, jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker, jdk.internal.foreign.abi.s390.linux.LinuxS390Linker, jdk.internal.foreign.abi.x64.sysv.SysVx64Linker, jdk.internal.foreign.abi.x64.windows.Windowsx64Linker, jdk.internal.foreign.layout.AbstractLayout, jdk.internal.reflect.CallerSensitive, .Reflection, java.lang.foreign.AddressLayout, .Arena, .FunctionDescriptor, .GroupLayout, .Linker, .MemoryLayout, .MemorySegment, .PaddingLayout, .SequenceLayout, .StructLayout, .UnionLayout, .ValueLayout, java.lang.invoke.MethodHandle, .MethodType, java.util.HashSet, .List, .Objects, .Set

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
public static interface

Field Summary

Modifier and TypeField and Description
private final Set<MemoryLayout>
private final SoftReferenceCache<AbstractLinker.LinkRequest, MethodHandle>
private final SoftReferenceCache<AbstractLinker.LinkRequest, AbstractLinker.UpcallStubFactory>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected abstract MethodHandle
arrangeDowncall(MethodType inferredMethodType, FunctionDescriptor function, LinkerOptions options)

protected abstract AbstractLinker.UpcallStubFactory
private static void
checkGroup(GroupLayout gl, long maxUnpaddedOffset)

private void
private void
private void
private void
private static void
checkMemberOffset(StructLayout parent, MemoryLayout memberLayout, long lastUnpaddedOffset, long offset)

private static void
protected void
checkStructMember(MemoryLayout member, long offset)

private void
public SystemLookup
defaultLookup()

Implements java.lang.foreign.Linker.defaultLookup.

Returns a symbol lookup for symbols in a set of commonly used libraries.

public final MethodHandle
downcallHandle(MemorySegment symbol, FunctionDescriptor function, Linker.Option... options)

Implements java.lang.foreign.Linker.downcallHandle.

Creates a method handle that is used to call a foreign function with the given signature and address.

public final MethodHandle
downcallHandle(FunctionDescriptor function, Linker.Option... options)

Implements java.lang.foreign.Linker.downcallHandle.

Creates a method handle that is used to call a foreign function with the given signature.

private MethodHandle
private static String
private static IllegalArgumentException
private static MemoryLayout
private static MemoryLayout[]
private static FunctionDescriptor
public final MemorySegment
upcallStub(MethodHandle target, FunctionDescriptor function, Arena arena, Linker.Option... options)

Implements java.lang.foreign.Linker.upcallStub.

Creates an upcall stub which can be passed to other foreign functions as a function pointer, associated with the given arena.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CANONICAL_LAYOUTS_CACHEback to summary
private final Set<MemoryLayout> CANONICAL_LAYOUTS_CACHE
DOWNCALL_CACHEback to summary
private final SoftReferenceCache<AbstractLinker.LinkRequest, MethodHandle> DOWNCALL_CACHE
UPCALL_CACHEback to summary
private final SoftReferenceCache<AbstractLinker.LinkRequest, AbstractLinker.UpcallStubFactory> UPCALL_CACHE

Constructor Detail

AbstractLinkerback to summary
public AbstractLinker()

Method Detail

arrangeDowncallback to summary
protected abstract MethodHandle arrangeDowncall(MethodType inferredMethodType, FunctionDescriptor function, LinkerOptions options)
arrangeUpcallback to summary
protected abstract AbstractLinker.UpcallStubFactory arrangeUpcall(MethodType targetType, FunctionDescriptor function, LinkerOptions options)
checkGroupback to summary
private static void checkGroup(GroupLayout gl, long maxUnpaddedOffset)
checkHasNaturalAlignmentback to summary
private void checkHasNaturalAlignment(MemoryLayout layout)
checkLayoutback to summary
private void checkLayout(MemoryLayout layout)
checkLayoutRecursiveback to summary
private void checkLayoutRecursive(MemoryLayout layout)
checkLayoutsback to summary
private void checkLayouts(FunctionDescriptor descriptor)
checkMemberOffsetback to summary
private static void checkMemberOffset(StructLayout parent, MemoryLayout memberLayout, long lastUnpaddedOffset, long offset)
checkNotAllPaddingback to summary
private static void checkNotAllPadding(StructLayout sl)
checkStructMemberback to summary
protected void checkStructMember(MemoryLayout member, long offset)
checkSupportedback to summary
private void checkSupported(ValueLayout valueLayout)
defaultLookupback to summary
public SystemLookup defaultLookup()

Implements java.lang.foreign.Linker.defaultLookup.

Doc from java.lang.foreign.Linker.defaultLookup.

Returns a symbol lookup for symbols in a set of commonly used libraries.

Each Linker is responsible for choosing libraries that are widely recognized as useful on the OS and processor combination supported by the Linker. Accordingly, the precise set of symbols exposed by the symbol lookup is unspecified; it varies from one Linker to another.

Annotations
@Override
downcallHandleback to summary
public final MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function, Linker.Option... options)

Implements java.lang.foreign.Linker.downcallHandle.

Doc from java.lang.foreign.Linker.downcallHandle.

Creates a method handle that is used to call a foreign function with the given signature and address.

Calling this method is equivalent to the following code:

linker.downcallHandle(function, options).bindTo(address);
linker.downcallHandle(function, options).bindTo(address);
Annotations
@Override
@CallerSensitive
downcallHandleback to summary
public final MethodHandle downcallHandle(FunctionDescriptor function, Linker.Option... options)

Implements java.lang.foreign.Linker.downcallHandle.

Doc from java.lang.foreign.Linker.downcallHandle.

Creates a method handle that is used to call a foreign function with the given signature.

The Java method type associated with the returned method handle is derived from the argument and return layouts in the function descriptor, but features an additional leading parameter of type MemorySegment, from which the address of the target foreign function is derived. Moreover, if the function descriptor's return layout is a group layout, the resulting downcall method handle accepts an additional leading parameter of type SegmentAllocator, which is used by the linker runtime to allocate the memory region associated with the struct returned by the downcall method handle.

Upon invoking a downcall method handle, the linker provides the following guarantees for any argument A of type MemorySegment whose corresponding layout is an address layout:

  • A.scope().isAlive() == true. Otherwise, the invocation throws IllegalStateException;
  • The invocation occurs in a thread T such that A.isAccessibleBy(T) == true. Otherwise, the invocation throws WrongThreadException; and
  • A is kept alive during the invocation. For instance, if A has been obtained using a shared arena, any attempt to close the arena while the downcall method handle is still executing will result in an IllegalStateException.

Moreover, if the provided function descriptor's return layout is an address layout, invoking the returned method handle will return a native segment associated with the global scope. Under normal conditions, the size of the returned segment is 0. However, if the function descriptor's return layout has a target layout T, then the size of the returned segment is set to T.byteSize().

The returned method handle will throw an IllegalArgumentException if the MemorySegment representing the target address of the foreign function is the MemorySegment#NULL address. If an argument is a MemorySegment, whose corresponding layout is a group layout, the linker might attempt to access the contents of the segment. As such, one of the exceptions specified by the MemorySegment#get(ValueLayout.OfByte, long) or the MemorySegment#copy(MemorySegment, long, MemorySegment, long, long) methods may be thrown. If an argument is a MemorySegment whose corresponding layout is an address layout, the linker will throw an IllegalArgumentException if the segment is a heap memory segment, unless heap memory segments are explicitly allowed through the Linker.Option#critical(boolean) linker option. The returned method handle will additionally throw NullPointerException if any argument passed to it is null.

Annotations
@Override
@CallerSensitive
downcallHandle0back to summary
private MethodHandle downcallHandle0(FunctionDescriptor function, Linker.Option... options)
inMessageback to summary
private static String inMessage(GroupLayout gl)
memberExceptionback to summary
private static IllegalArgumentException memberException(MemoryLayout parent, MemoryLayout member, String info)
stripNamesback to summary
private static MemoryLayout stripNames(MemoryLayout ml)
Annotations
@SuppressWarnings:restricted
stripNamesback to summary
private static MemoryLayout[] stripNames(List<MemoryLayout> layouts)
stripNamesback to summary
private static FunctionDescriptor stripNames(FunctionDescriptor function)
upcallStubback to summary
public final MemorySegment upcallStub(MethodHandle target, FunctionDescriptor function, Arena arena, Linker.Option... options)

Implements java.lang.foreign.Linker.upcallStub.

Doc from java.lang.foreign.Linker.upcallStub.

Creates an upcall stub which can be passed to other foreign functions as a function pointer, associated with the given arena. Calling such a function pointer from foreign code will result in the execution of the provided method handle.

The returned memory segment's address points to the newly allocated upcall stub, and is associated with the provided arena. As such, the lifetime of the returned upcall stub segment is controlled by the provided arena. For instance, if the provided arena is a confined arena, the returned upcall stub segment will be deallocated when the provided confined arena is closed.

An upcall stub argument whose corresponding layout is an address layout is a native segment associated with the global scope. Under normal conditions, the size of this segment argument is 0. However, if the address layout has a target layout T, then the size of the segment argument is set to T.byteSize().

The target method handle should not throw any exceptions. If the target method handle does throw an exception, the JVM will terminate abruptly. To avoid this, clients should wrap the code in the target method handle in a try/catch block to catch any unexpected exceptions. This can be done using the java.lang.invoke.MethodHandles#catchException(MethodHandle, Class, MethodHandle) method handle combinator, and handle exceptions as desired in the corresponding catch block.

Annotations
@Override
@CallerSensitive
validateVariadicLayoutsback to summary
private void validateVariadicLayouts(FunctionDescriptor function, LinkerOptions optionSet)
jdk.internal.foreign.abi back to summary

private final Record AbstractLinker.LinkRequest

extends Record
Class Inheritance
Record Components
descriptor:FunctionDescriptor 
options:LinkerOptions 

Field Summary

Modifier and TypeField and Description
private final FunctionDescriptor
descriptor

Record Component accessed by descriptor().

private final LinkerOptions
options

Record Component accessed by options().

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public FunctionDescriptor
descriptor()

Record Component getter of descriptor.

public boolean
equals(Object obj)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public LinkerOptions
options()

Record Component getter of options.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

descriptorback to summary
private final FunctionDescriptor descriptor

Record Component accessed by descriptor().

optionsback to summary
private final LinkerOptions options

Record Component accessed by options().

Constructor Detail

LinkRequestback to summary
private LinkRequest(FunctionDescriptor descriptor, LinkerOptions options)

Method Detail

descriptorback to summary
public FunctionDescriptor descriptor()

Record Component getter of descriptor.

equalsback to summary
public boolean equals(Object obj)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Annotations
@Override
hashCodeback to summary
public int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Annotations
@Override
optionsback to summary
public LinkerOptions options()

Record Component getter of options.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

jdk.internal.foreign.abi back to summary

public Interface AbstractLinker.UpcallStubFactory


Method Summary

Modifier and TypeMethod and Description
public MemorySegment
makeStub(MethodHandle target, Arena arena)

Method Detail

makeStubback to summary
public MemorySegment makeStub(MethodHandle target, Arena arena)