static { theUnsafe = new SunUnsafe(); BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class); if (theUnsafe.arrayIndexScale(byte[].class) != 1) { throw new AssertionError(); } }
public void dispose() { log.finest("Disposing"); if (should_free_memory) { log.finest("freeing memory"); unsafe.freeMemory(pData); } }
public static JavaSecurityAccess getJavaSecurityAccess() { if (javaSecurityAccess == null) { // [IKVM] OpenJDK initializes AccessController here, but that's a bug unsafe.ensureClassInitialized(ProtectionDomain.class); } return javaSecurityAccess; }
public static JavaUtilJarAccess javaUtilJarAccess() { if (javaUtilJarAccess == null) { // Ensure JarFile is initialized; we know that that class // provides the shared secret unsafe.ensureClassInitialized(JarFile.class); } return javaUtilJarAccess; }
public static JavaNioAccess getJavaNioAccess() { if (javaNioAccess == null) { // Ensure java.nio.ByteOrder is initialized; we know that // this class initializes java.nio.Bits that provides the // shared secret. unsafe.ensureClassInitialized(java.nio.ByteOrder.class); } return javaNioAccess; }
public void setCompressionThreshold(int compressionThreshold) { if (ch.getHandle().isActive() && this.compressionThreshold == -1 && getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_8) { this.compressionThreshold = compressionThreshold; unsafe.sendPacket(new SetCompression(compressionThreshold)); ch.setCompressionThreshold(compressionThreshold); } }
@Override public int compareTo( byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare(buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }
/** * Show the current stacktrace using Screen.debug. TODO that method only exist to have line * numbers : find a way to add line numbers to debugStackTrace(max) */ @KernelSpace public final void debugStackTraceWithLineNumbers(int max) { final VmThread current = VmThread.currentThread(); final VmStackFrame[] frames = (VmStackFrame[]) VmThread.getStackTrace(current); if (frames == null) { Unsafe.debug("Debug stacktrace:<no stack trace>\n"); } else { Unsafe.debug("Debug stacktrace: "); for (VmStackFrame frame : frames) { final VmStackFrame s = (VmStackFrame) frame; Unsafe.debug(s.getMethod().getDeclaringClass().getName()); Unsafe.debug("::"); Unsafe.debug(s.getMethod().getName()); Unsafe.debug(":"); Unsafe.debug(s.getLocationInfo()); Unsafe.debug('\n'); } } }
/** Show the current stacktrace using Screen.debug. */ @KernelSpace public final void debugStackTrace(int max) { Address f = VmMagic.getCurrentFrame(); Unsafe.debug("\nDebug stacktrace: "); boolean first = true; while (isValid(f) && (max > 0)) { if (first) { first = false; } else { Unsafe.debug(", "); } final VmMethod method = getMethod(f); final VmType<?> vmClass = method.getDeclaringClass(); Unsafe.debug(vmClass.getName()); Unsafe.debug("::"); Unsafe.debug(method.getName()); Unsafe.debug('\n'); f = getPrevious(f); max--; } if (isValid(f)) { Unsafe.debug("..."); } }
/** Show the stacktrace of the given thread using Screen.debug. */ @KernelSpace public final void debugStackTrace(VmThread thread) { Address f = thread.getStackFrame(); Unsafe.debug("Debug stacktrace: "); boolean first = true; int max = 20; while (isValid(f) && (max > 0)) { if (first) { first = false; } else { Unsafe.debug(", "); } final VmMethod method = getMethod(f); final VmType vmClass = method.getDeclaringClass(); Unsafe.debug(vmClass.getName()); Unsafe.debug("::"); Unsafe.debug(method.getName()); f = getPrevious(f); max--; } if (isValid(f)) { Unsafe.debug("..."); } }
public static JavaSecurityProtectionDomainAccess getJavaSecurityProtectionDomainAccess() { if (javaSecurityProtectionDomainAccess == null) unsafe.ensureClassInitialized(ProtectionDomain.class); return javaSecurityProtectionDomainAccess; }
public static JavaIOAccess getJavaIOAccess() { if (javaIOAccess == null) { unsafe.ensureClassInitialized(Console.class); } return javaIOAccess; }
static <A, B, R> F1<A, R> unsafe(Unsafe<? super A, ? super B, ? extends R> f, B b) { return a -> f.apply(a, b); }
/** * A repository of "shared secrets", which are a mechanism for calling implementation-private * methods in another package without using reflection. A package-private class implements a public * interface and provides the ability to call package-private methods within that package; the * object implementing that interface is provided through a third package to which access is * restricted. This framework avoids the primary disadvantage of using reflection for this purpose, * namely the loss of compile-time checking. */ public class SharedSecrets { private static final Unsafe unsafe = Unsafe.getUnsafe(); private static JavaUtilJarAccess javaUtilJarAccess; private static JavaLangAccess javaLangAccess; private static JavaIOAccess javaIOAccess; private static JavaNetAccess javaNetAccess; private static JavaNetHttpCookieAccess javaNetHttpCookieAccess; private static JavaNioAccess javaNioAccess; private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess; private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess; private static JavaSecurityAccess javaSecurityAccess; private static JavaxSecurityAuthKerberosAccess javaxSecurityAuthKerberosAccess; private static JavaUtilZipAccess javaUtilZipAccess; public static JavaUtilJarAccess javaUtilJarAccess() { if (javaUtilJarAccess == null) { // Ensure JarFile is initialized; we know that that class // provides the shared secret unsafe.ensureClassInitialized(JarFile.class); } return javaUtilJarAccess; } public static void setJavaUtilJarAccess(JavaUtilJarAccess access) { javaUtilJarAccess = access; } public static void setJavaLangAccess(JavaLangAccess jla) { javaLangAccess = jla; } public static JavaLangAccess getJavaLangAccess() { return javaLangAccess; } public static void setJavaNetAccess(JavaNetAccess jna) { javaNetAccess = jna; } public static JavaNetAccess getJavaNetAccess() { return javaNetAccess; } public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) { javaNetHttpCookieAccess = a; } public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() { if (javaNetHttpCookieAccess == null) unsafe.ensureClassInitialized(java.net.HttpCookie.class); return javaNetHttpCookieAccess; } public static void setJavaNioAccess(JavaNioAccess jna) { javaNioAccess = jna; } public static JavaNioAccess getJavaNioAccess() { if (javaNioAccess == null) { // Ensure java.nio.ByteOrder is initialized; we know that // this class initializes java.nio.Bits that provides the // shared secret. unsafe.ensureClassInitialized(java.nio.ByteOrder.class); } return javaNioAccess; } public static void setJavaIOAccess(JavaIOAccess jia) { javaIOAccess = jia; } public static JavaIOAccess getJavaIOAccess() { if (javaIOAccess == null) { unsafe.ensureClassInitialized(Console.class); } return javaIOAccess; } public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) { javaIOFileDescriptorAccess = jiofda; } public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() { if (javaIOFileDescriptorAccess == null) unsafe.ensureClassInitialized(FileDescriptor.class); return javaIOFileDescriptorAccess; } public static void setJavaSecurityProtectionDomainAccess( JavaSecurityProtectionDomainAccess jspda) { javaSecurityProtectionDomainAccess = jspda; } public static JavaSecurityProtectionDomainAccess getJavaSecurityProtectionDomainAccess() { if (javaSecurityProtectionDomainAccess == null) unsafe.ensureClassInitialized(ProtectionDomain.class); return javaSecurityProtectionDomainAccess; } public static void setJavaSecurityAccess(JavaSecurityAccess jsa) { javaSecurityAccess = jsa; } public static JavaSecurityAccess getJavaSecurityAccess() { if (javaSecurityAccess == null) { unsafe.ensureClassInitialized(AccessController.class); } return javaSecurityAccess; } public static void setJavaxSecurityAuthKerberosAccess(JavaxSecurityAuthKerberosAccess jsaka) { javaxSecurityAuthKerberosAccess = jsaka; } public static JavaxSecurityAuthKerberosAccess getJavaxSecurityAuthKerberosAccess() { if (javaxSecurityAuthKerberosAccess == null) unsafe.ensureClassInitialized(KeyTab.class); return javaxSecurityAuthKerberosAccess; } public static void setJavaUtilZipAccess(JavaUtilZipAccess access) { javaUtilZipAccess = access; } public static JavaUtilZipAccess getJavaUtilZipAccess() { if (javaUtilZipAccess == null) { unsafe.ensureClassInitialized(Adler32.class); } return javaUtilZipAccess; } }
public static JavaUtilZipAccess getJavaUtilZipAccess() { if (javaUtilZipAccess == null) { unsafe.ensureClassInitialized(Adler32.class); } return javaUtilZipAccess; }
public static JavaSecurityAccess getJavaSecurityAccess() { if (javaSecurityAccess == null) { unsafe.ensureClassInitialized(AccessController.class); } return javaSecurityAccess; }
public XCreateWindowEvent() { log.finest("Creating"); pData = unsafe.allocateMemory(getSize()); should_free_memory = true; }
public static JavaxSecurityAuthKerberosAccess getJavaxSecurityAuthKerberosAccess() { if (javaxSecurityAuthKerberosAccess == null) unsafe.ensureClassInitialized(KeyTab.class); return javaxSecurityAuthKerberosAccess; }
public static final <L extends Listener> LiveResourceRegistration of( final Class<L> type, final String name) { return of(type, Unsafe.cast(type, Resources.forName(name))); }
public XwcTextItem() { log.finest("Creating"); pData = unsafe.allocateMemory(getSize()); should_free_memory = true; }
/** * A repository of "shared secrets", which are a mechanism for calling implementation-private * methods in another package without using reflection. A package-private class implements a public * interface and provides the ability to call package-private methods within that package; the * object implementing that interface is provided through a third package to which access is * restricted. This framework avoids the primary disadvantage of using reflection for this purpose, * namely the loss of compile-time checking. */ public class SharedSecrets { private static final Unsafe unsafe = Unsafe.getUnsafe(); private static JavaUtilJarAccess javaUtilJarAccess; private static JavaLangAccess javaLangAccess = LangHelper.getJavaLangAccess(); private static JavaIOAccess javaIOAccess; private static JavaNetAccess javaNetAccess; private static JavaNetHttpCookieAccess javaNetHttpCookieAccess; private static JavaNioAccess javaNioAccess; private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess; private static JavaSecurityAccess javaSecurityAccess; private static JavaxSecurityAuthKerberosAccess javaxSecurityAuthKerberosAccess; private static JavaAWTAccess javaAWTAccess; public static JavaUtilJarAccess javaUtilJarAccess() { if (javaUtilJarAccess == null) { // Ensure JarFile is initialized; we know that that class // provides the shared secret unsafe.ensureClassInitialized(JarFile.class); } return javaUtilJarAccess; } public static void setJavaUtilJarAccess(JavaUtilJarAccess access) { javaUtilJarAccess = access; } public static JavaLangAccess getJavaLangAccess() { return javaLangAccess; } public static void setJavaNetAccess(JavaNetAccess jna) { javaNetAccess = jna; } public static JavaNetAccess getJavaNetAccess() { return javaNetAccess; } public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) { javaNetHttpCookieAccess = a; } public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() { if (javaNetHttpCookieAccess == null) unsafe.ensureClassInitialized(java.net.HttpCookie.class); return javaNetHttpCookieAccess; } public static void setJavaNioAccess(JavaNioAccess jna) { javaNioAccess = jna; } public static JavaNioAccess getJavaNioAccess() { if (javaNioAccess == null) { // [IKVM] OpenJDK initializes java.nio.ByteOrder here, but that doesn't work java.nio.ByteOrder.nativeOrder(); } return javaNioAccess; } public static void setJavaIOAccess(JavaIOAccess jia) { javaIOAccess = jia; } public static JavaIOAccess getJavaIOAccess() { if (javaIOAccess == null) { unsafe.ensureClassInitialized(Console.class); } return javaIOAccess; } public static void setJavaSecurityProtectionDomainAccess( JavaSecurityProtectionDomainAccess jspda) { javaSecurityProtectionDomainAccess = jspda; } public static JavaSecurityProtectionDomainAccess getJavaSecurityProtectionDomainAccess() { if (javaSecurityProtectionDomainAccess == null) unsafe.ensureClassInitialized(ProtectionDomain.class); return javaSecurityProtectionDomainAccess; } public static void setJavaSecurityAccess(JavaSecurityAccess jsa) { javaSecurityAccess = jsa; } public static JavaSecurityAccess getJavaSecurityAccess() { if (javaSecurityAccess == null) { // [IKVM] OpenJDK initializes AccessController here, but that's a bug unsafe.ensureClassInitialized(ProtectionDomain.class); } return javaSecurityAccess; } public static void setJavaxSecurityAuthKerberosAccess(JavaxSecurityAuthKerberosAccess jsaka) { javaxSecurityAuthKerberosAccess = jsaka; } public static JavaxSecurityAuthKerberosAccess getJavaxSecurityAuthKerberosAccess() { if (javaxSecurityAuthKerberosAccess == null) unsafe.ensureClassInitialized(KeyTab.class); return javaxSecurityAuthKerberosAccess; } public static void setJavaAWTAccess(JavaAWTAccess jaa) { javaAWTAccess = jaa; } public static JavaAWTAccess getJavaAWTAccess() { // this may return null in which case calling code needs to // provision for. if (javaAWTAccess == null || javaAWTAccess.getContext() == null) { return null; } return javaAWTAccess; } }
public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() { if (javaIOFileDescriptorAccess == null) unsafe.ensureClassInitialized(FileDescriptor.class); return javaIOFileDescriptorAccess; }
public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() { if (javaNetHttpCookieAccess == null) unsafe.ensureClassInitialized(java.net.HttpCookie.class); return javaNetHttpCookieAccess; }
public TypeUniverse() { Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders(); metaAccess = providers.getMetaAccess(); constantReflection = providers.getConstantReflection(); snippetReflection = Graal.getRequiredCapability(SnippetReflectionProvider.class); Unsafe theUnsafe = null; try { theUnsafe = Unsafe.getUnsafe(); } catch (Exception e) { try { Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafeField.setAccessible(true); theUnsafe = (Unsafe) theUnsafeField.get(null); } catch (Exception e1) { throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1); } } unsafe = theUnsafe; Class<?>[] initialClasses = { void.class, boolean.class, byte.class, short.class, char.class, int.class, float.class, long.class, double.class, Object.class, Class.class, ClassLoader.class, String.class, Serializable.class, Cloneable.class, Test.class, TestMetaAccessProvider.class, List.class, Collection.class, Map.class, Queue.class, HashMap.class, LinkedHashMap.class, IdentityHashMap.class, AbstractCollection.class, AbstractList.class, ArrayList.class }; for (Class<?> c : initialClasses) { addClass(c); } for (Field f : Constant.class.getDeclaredFields()) { int mods = f.getModifiers(); if (f.getType() == Constant.class && Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) { try { Constant c = (Constant) f.get(null); if (c != null) { constants.add(c); } } catch (Exception e) { } } } for (Class<?> c : classes) { if (c != void.class && !c.isArray()) { constants.add(snippetReflection.forObject(Array.newInstance(c, 42))); } } constants.add(snippetReflection.forObject(new ArrayList<>())); constants.add(snippetReflection.forObject(new IdentityHashMap<>())); constants.add(snippetReflection.forObject(new LinkedHashMap<>())); constants.add(snippetReflection.forObject(new TreeMap<>())); constants.add(snippetReflection.forObject(new ArrayDeque<>())); constants.add(snippetReflection.forObject(new LinkedList<>())); constants.add(snippetReflection.forObject("a string")); constants.add(snippetReflection.forObject(42)); constants.add(snippetReflection.forObject(String.class)); constants.add(snippetReflection.forObject(String[].class)); }