package jdk.internal.foreign;
import jdk.incubator.foreign.*;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.VM;
import sun.invoke.util.Wrapper;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.util.Optional;
import java.util.function.Supplier;
import static sun.security.action.GetPropertyAction.*;
public final class Utils {
private static final boolean SHOULD_ADAPT_HANDLES
= Boolean.parseBoolean(privilegedGetProperty("jdk.internal.foreign.SHOULD_ADAPT_HANDLES", "true"));
private static final String foreignRestrictedAccess = Optional.ofNullable(VM.getSavedProperty("foreign.restricted"))
.orElse("deny");
private static final MethodHandle SEGMENT_FILTER;
public static final MethodHandle MH_bitsToBytesOrThrowForOffset;
public static final Supplier<RuntimeException> bitsToBytesThrowOffset
= () -> new UnsupportedOperationException("Cannot compute byte offset; bit offset is not a multiple of 8");
static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
SEGMENT_FILTER = lookup.findStatic(Utils.class, "filterSegment",
MethodType.methodType(MemorySegmentProxy.class, MemorySegment.class));
MH_bitsToBytesOrThrowForOffset = MethodHandles.insertArguments(
lookup.findStatic(Utils.class, "bitsToBytesOrThrow",
MethodType.methodType(long.class, long.class, Supplier.class)),
1,
bitsToBytesThrowOffset);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static long alignUp(long n, long alignment) {
return (n + alignment - 1) & -alignment;
}
public static MemoryAddress alignUp(MemoryAddress ma, long alignment) {
long offset = ma.toRawLongValue();
return ma.addOffset(alignUp(offset, alignment) - offset);
}
public static MemorySegment alignUp(MemorySegment ms, long alignment) {
long offset = ms.address().toRawLongValue();
return ms.asSlice(alignUp(offset, alignment) - offset);
}
public static long bitsToBytesOrThrow(long bits, Supplier<RuntimeException> exFactory) {
if (bits % 8 == 0) {
return bits / 8;
} else {
throw exFactory.get();
}
}
public static VarHandle fixUpVarHandle(VarHandle handle) {
return SHOULD_ADAPT_HANDLES
? MemoryHandles.filterCoordinates(handle, 0, SEGMENT_FILTER)
: handle;
}
private static MemorySegmentProxy filterSegment(MemorySegment segment) {
return (AbstractMemorySegmentImpl)segment;
}
public static void checkRestrictedAccess(String method) {
switch (foreignRestrictedAccess) {
case "deny" -> throwIllegalAccessError(foreignRestrictedAccess, method);
case "warn" -> System.err.println("WARNING: Accessing restricted foreign method: " + method);
case "debug" -> {
StringBuilder sb = new StringBuilder("DEBUG: restricted foreign method: \" + method");
StackWalker.getInstance().forEach(f -> sb.append(System.lineSeparator())
.append("\tat ")
.append(f));
System.err.println(sb.toString());
}
case "permit" -> {}
default -> throwIllegalAccessError(foreignRestrictedAccess, method);
}
}
private static void throwIllegalAccessError(String value, String method) {
throw new IllegalAccessError("Illegal access to restricted foreign method: " + method +
" ; system property 'foreign.restricted' is set to '" + value + "'");
}
public static void checkPrimitiveCarrierCompat(Class<?> carrier, MemoryLayout layout) {
checkLayoutType(layout, ValueLayout.class);
if (!isValidPrimitiveCarrier(carrier))
throw new IllegalArgumentException("Unsupported carrier: " + carrier);
if (Wrapper.forPrimitiveType(carrier).bitWidth() != layout.bitSize())
throw new IllegalArgumentException("Carrier size mismatch: " + carrier + " != " + layout);
}
public static boolean isValidPrimitiveCarrier(Class<?> carrier) {
return carrier == byte.class
|| carrier == short.class
|| carrier == char.class
|| carrier == int.class
|| carrier == long.class
|| carrier == float.class
|| carrier == double.class;
}
public static void checkLayoutType(MemoryLayout layout, Class<? extends MemoryLayout> layoutType) {
if (!layoutType.isInstance(layout))
throw new IllegalArgumentException("Expected a " + layoutType.getSimpleName() + ": " + layout);
}
}