Search in sources :

Example 61 with Unsafe

use of sun.misc.Unsafe in project Universal-Pointer-Searcher by BullyWiiPlaza.

the class UnsafeTesting method runTest.

private void runTest(boolean useUnsafeMethod) throws Exception {
    Unsafe unsafe = getUnsafeInstance();
    long destinationByteBufferAddress = ((DirectBuffer) destinationByteBuffer).address();
    long sourceByteBufferAddress = ((DirectBuffer) sourceByteBuffer).address();
    int executionsCount = 0;
    if (useUnsafeMethod) {
        for (int sourceBufferIndex = 0; sourceBufferIndex < destinationByteBuffer.remaining() - 3; sourceBufferIndex += 4) {
            long sourceOffset = sourceByteBufferAddress + sourceBufferIndex;
            int value = unsafe.getInt(sourceOffset);
            long targetOffset = destinationByteBufferAddress + sourceBufferIndex;
            unsafe.putInt(targetOffset, value);
            executionsCount++;
        }
    } else {
        while (sourceByteBuffer.remaining() > 3) {
            int value = destinationByteBuffer.getInt();
            sourceByteBuffer.putInt(value);
            executionsCount++;
        }
    }
    /*sourceByteBuffer.position(0);
		destinationByteBuffer.position(0);
		boolean equal = sourceByteBuffer.equals(destinationByteBuffer);

		if (!equal)
		{
			throw new IllegalStateException("Buffers not equal!");
		}*/
    System.out.println("Executions: " + executionsCount);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) Unsafe(sun.misc.Unsafe)

Example 62 with Unsafe

use of sun.misc.Unsafe in project graal by oracle.

the class AMD64GraphBuilderPlugins method registerUnsafePlugins.

private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) {
    Registration r;
    if (Java8OrEarlier) {
        r = new Registration(plugins, Unsafe.class);
    } else {
        r = new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider);
    }
    for (JavaKind kind : new JavaKind[] { JavaKind.Int, JavaKind.Long, JavaKind.Object }) {
        Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass();
        r.register4("getAndSet" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {

            @Override
            public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) {
                // Emits a null-check for the otherwise unused receiver
                unsafe.get();
                b.addPush(kind, new AtomicReadAndWriteNode(object, offset, value, kind, LocationIdentity.any()));
                b.getGraph().markUnsafeAccess();
                return true;
            }
        });
        if (kind != JavaKind.Object) {
            r.register4("getAndAdd" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {

                @Override
                public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) {
                    // Emits a null-check for the otherwise unused receiver
                    unsafe.get();
                    AddressNode address = b.add(new OffsetAddressNode(object, offset));
                    b.addPush(kind, new AtomicReadAndAddNode(address, delta, LocationIdentity.any()));
                    b.getGraph().markUnsafeAccess();
                    return true;
                }
            });
        }
    }
    for (JavaKind kind : new JavaKind[] { JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long }) {
        Class<?> javaClass = kind.toJavaClass();
        r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, false));
        r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, false));
    }
}
Also used : Unsafe(sun.misc.Unsafe) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) UnsafePutPlugin(org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafePutPlugin) OffsetAddressNode(org.graalvm.compiler.nodes.memory.address.OffsetAddressNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) OffsetAddressNode(org.graalvm.compiler.nodes.memory.address.OffsetAddressNode) AddressNode(org.graalvm.compiler.nodes.memory.address.AddressNode) AtomicReadAndWriteNode(org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) JavaKind(jdk.vm.ci.meta.JavaKind) AtomicReadAndAddNode(org.graalvm.compiler.nodes.java.AtomicReadAndAddNode) UnsafeGetPlugin(org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeGetPlugin)

Example 63 with Unsafe

use of sun.misc.Unsafe in project openj9 by eclipse.

the class FrameIteratorTestGenerator method testFrameIteratorSkipAnnontationForNonBootstrapClass.

@Test
public void testFrameIteratorSkipAnnontationForNonBootstrapClass() throws Throwable {
    final byte[] classBytes = FrameIteratorTestGenerator.dump();
    /* Use reflect to get Unsafe so we can avoid the 'must be bootstrap' check */
    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    theUnsafe.setAccessible(true);
    Unsafe unsafe = (Unsafe) theUnsafe.get(null);
    /* Force the class to be loaded in the Extension Loader so we can use sun.reflect.Reflection.getCallerClass() */
    ClassLoader extLoader = Cmvc194781.class.getClassLoader().getParent();
    Class<?> test = unsafe.defineClass("FrameIteratorTest", classBytes, 0, classBytes.length, extLoader, Cmvc194781.class.getProtectionDomain());
    PrivilegedAction<Class<?>> instance = (PrivilegedAction<Class<?>>) test.newInstance();
    Class<?> caller = (Class<?>) instance.run();
    AssertJUnit.assertEquals(Cmvc194781.class, caller);
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Unsafe(sun.misc.Unsafe) Test(org.testng.annotations.Test)

Example 64 with Unsafe

use of sun.misc.Unsafe in project hpcourse by cscenter.

the class MyAtomicInteger method getUnsafe.

// @SuppressWarnings("restriction")
private static Unsafe getUnsafe() {
    try {
        Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
        singleoneInstanceField.setAccessible(true);
        return (Unsafe) singleoneInstanceField.get(null);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new IllegalStateException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 65 with Unsafe

use of sun.misc.Unsafe in project incubator-crail by apache.

the class RdmaStorageLocalEndpoint method getUnsafe.

private Unsafe getUnsafe() throws Exception {
    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    theUnsafe.setAccessible(true);
    Unsafe unsafe = (Unsafe) theUnsafe.get(null);
    return unsafe;
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Aggregations

Unsafe (sun.misc.Unsafe)90 Field (java.lang.reflect.Field)62 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)7 Test (org.testng.annotations.Test)7 PrivilegedActionException (java.security.PrivilegedActionException)5 PrivilegedAction (java.security.PrivilegedAction)4 IOException (java.io.IOException)3 JavaKind (jdk.vm.ci.meta.JavaKind)3 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)3 ValueNode (org.graalvm.compiler.nodes.ValueNode)3 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)3 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)3 Test (org.junit.Test)3 LazyArrayIntTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayIntTags)2 LazyArrayObjTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayObjTags)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)2 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)2 FrameSlotTypeException (com.oracle.truffle.api.frame.FrameSlotTypeException)1 DataSize (io.airlift.units.DataSize)1