Search in sources :

Example 76 with Unsafe

use of sun.misc.Unsafe in project tutorials by eugenp.

the class OffHeapArray method getUnsafe.

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

Example 77 with Unsafe

use of sun.misc.Unsafe in project java-interview by MelloChan.

the class DirectMemoryOOM method main.

public static void main(String[] args) throws IllegalAccessException {
    Field unsafe = Unsafe.class.getDeclaredFields()[0];
    unsafe.setAccessible(true);
    Unsafe unsafe1 = (Unsafe) unsafe.get(null);
    while (true) {
        unsafe1.allocateMemory(_1MB);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 78 with Unsafe

use of sun.misc.Unsafe in project symja_android_library by axkr.

the class UnsafeUtil method fetchUnsafe.

private static Unsafe fetchUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        LOG.error("Could not get unsafe: " + e, e);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 79 with Unsafe

use of sun.misc.Unsafe in project groovy-core by groovy.

the class FastStringUtils method loadUnsafe.

/**
 * @return Unsafe
 */
private static Unsafe loadUnsafe() {
    try {
        Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    } catch (Exception e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 80 with Unsafe

use of sun.misc.Unsafe in project jboss-modules by jboss-modules.

the class JAXPModuleTest method testMain.

/*
     * This test is slightly dangerous. If it causes problems, just add @Ignore
     * and/or let me know.
     *   -Jason
     */
@Test
public void testMain() throws Throwable {
    final java.lang.reflect.Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
    unsafeField.setAccessible(true);
    Unsafe unsafe = (Unsafe) unsafeField.get(null);
    java.lang.reflect.Field field = DefaultBootModuleLoaderHolder.class.getDeclaredField("INSTANCE");
    ModuleLoader oldMl = (ModuleLoader) field.get(null);
    final Object fieldBase = unsafe.staticFieldBase(field);
    final long fieldOffset = unsafe.staticFieldOffset(field);
    unsafe.putObjectVolatile(fieldBase, fieldOffset, moduleLoader);
    Main.main(new String[] { "-jaxpmodule", "fake-jaxp", "test-jaxp" });
    ModuleClassLoader cl = moduleLoader.loadModule(ModuleIdentifier.fromString("test-jaxp")).getClassLoader();
    Class<?> clazz = cl.loadClass("org.jboss.modules.test.JAXPCaller");
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(cl);
        checkDom(clazz, false, true);
        checkSax(clazz, false, true);
        checkTransformer(clazz, false, true);
        checkSAXTransformer(clazz, false, true);
        checkXmlEvent(clazz, false, true);
        checkXPath(clazz, false, true);
        checkXmlInput(clazz, false, true);
        checkXmlOutput(clazz, false, true);
        checkDatatype(clazz, false, true);
        checkSchema(clazz, false, true);
        checkXMLReader(clazz, false, true);
    } finally {
        unsafe.putObjectVolatile(fieldBase, fieldOffset, oldMl);
        Thread.currentThread().setContextClassLoader(old);
        __JAXPRedirected.restorePlatformFactory();
    }
}
Also used : TestModuleLoader(org.jboss.modules.util.TestModuleLoader) Unsafe(sun.misc.Unsafe) Test(org.junit.Test)

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