Search in sources :

Example 86 with Unsafe

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

the class Test_UnsafeFence method testLongFence.

/**
 * @tests sun.misc.Unsafe.getAndAddLong()
 * @tests sun.misc.Unsafe.loadFence()
 * @tests sun.misc.Unsafe.storeFence()
 * @tests sun.misc.Unsafe.fullFence()
 */
@Test
public void testLongFence() {
    Unsafe unsafe = getUnsafe();
    Field intFieldReflect;
    Field longFieldReflect;
    try {
        longFieldReflect = getClass().getDeclaredField("longField");
        long longFieldOffset = unsafe.objectFieldOffset(longFieldReflect);
        AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, 2), 0);
        AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, 3), 2);
        AssertJUnit.assertEquals(unsafe.getAndSetLong(this, longFieldOffset, 7), 5);
        AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, -3), 7);
        AssertJUnit.assertEquals(unsafe.getAndSetLong(this, longFieldOffset, -9), 4);
        AssertJUnit.assertEquals(longField, -9);
        unsafe.loadFence();
        AssertJUnit.assertEquals(longField, -9);
        unsafe.storeFence();
        AssertJUnit.assertEquals(longField, -9);
        unsafe.fullFence();
        AssertJUnit.assertEquals(longField, -9);
    } catch (NoSuchFieldException e) {
        Assert.fail("Missing field");
    } catch (SecurityException e) {
        Assert.fail("SecurityException: " + e.getMessage());
    }
}
Also used : Unsafe(sun.misc.Unsafe) Test(org.testng.annotations.Test)

Example 87 with Unsafe

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

the class FrameIteratorTestGenerator method testFrameIteratorSkipAnnotationForNonBootstrapClass.

@Test
public void testFrameIteratorSkipAnnotationForNonBootstrapClass() 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 88 with Unsafe

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

the class main method main.

public static void main(String[] args) throws Throwable {
    Unsafe u = Unsafe.getUnsafe();
    long offset;
    Class cl = Class.forName(args[0]);
    int alignment = com.ibm.oti.vm.VM.FJ9OBJECT_SIZE;
    try {
        offset = u.objectFieldOffset(cl.getDeclaredField("i"));
        System.out.println(offset);
        if (offset % 4 != 0) {
            System.out.println("error: int is not aligned");
        }
    } catch (Exception e) {
    }
    try {
        offset = u.objectFieldOffset(cl.getDeclaredField("l"));
        System.out.println(offset);
        if (offset % 8 != 0) {
            System.out.println("error: long is not aligned");
        }
    } catch (Exception e) {
    }
    try {
        offset = u.objectFieldOffset(cl.getDeclaredField("o"));
        System.out.println(offset);
        if (offset % alignment != 0) {
            System.out.println("error: object is not aligned");
        }
    } catch (Exception e) {
    }
    try {
        offset = u.objectFieldOffset(cl.getDeclaredField("d"));
        System.out.println(offset);
        if (offset % 8 != 0) {
            System.out.println("error: double is not aligned");
        }
    } catch (Exception e) {
    }
}
Also used : Unsafe(sun.misc.Unsafe)

Example 89 with Unsafe

use of sun.misc.Unsafe in project lombok by rzwitserloot.

the class LombokProcessor method addOpensForLombok.

/**
 * Useful from jdk9 and up; required from jdk16 and up. This code is supposed to gracefully do nothing on jdk8 and below, as this operation isn't needed there.
 */
public static void addOpensForLombok() {
    Class<?> cModule;
    try {
        cModule = Class.forName("java.lang.Module");
    } catch (ClassNotFoundException e) {
        // jdk8-; this is not needed.
        return;
    }
    Unsafe unsafe = getUnsafe();
    Object jdkCompilerModule = getJdkCompilerModule();
    Object ownModule = getOwnModule();
    String[] allPkgs = { "com.sun.tools.javac.code", "com.sun.tools.javac.comp", "com.sun.tools.javac.file", "com.sun.tools.javac.main", "com.sun.tools.javac.model", "com.sun.tools.javac.parser", "com.sun.tools.javac.processing", "com.sun.tools.javac.tree", "com.sun.tools.javac.util", "com.sun.tools.javac.jvm" };
    try {
        Method m = cModule.getDeclaredMethod("implAddOpens", String.class, cModule);
        long firstFieldOffset = getFirstFieldOffset(unsafe);
        unsafe.putBooleanVolatile(m, firstFieldOffset, true);
        for (String p : allPkgs) m.invoke(jdkCompilerModule, p, ownModule);
    } catch (Exception ignore) {
    }
}
Also used : Unsafe(sun.misc.Unsafe) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 90 with Unsafe

use of sun.misc.Unsafe in project carbondata by apache.

the class CarbondataConnectorFactory method addHiveStorageFormatsForCarbondata.

private static void addHiveStorageFormatsForCarbondata(String storedAs) throws Exception {
    Constructor<?> constructor = Unsafe.class.getDeclaredConstructors()[0];
    constructor.setAccessible(true);
    Unsafe unsafe = (Unsafe) constructor.newInstance();
    HiveStorageFormat enumValue = (HiveStorageFormat) unsafe.allocateInstance(HiveStorageFormat.class);
    Field nameField = Enum.class.getDeclaredField("name");
    makeAccessible(nameField);
    nameField.set(enumValue, storedAs);
    Field ordinalField = Enum.class.getDeclaredField("ordinal");
    makeAccessible(ordinalField);
    ordinalField.setInt(enumValue, HiveStorageFormat.values().length);
    Field serdeField = HiveStorageFormat.class.getDeclaredField("serde");
    makeAccessible(serdeField);
    serdeField.set(enumValue, CarbonHiveSerDe.class.getName());
    Field inputFormatField = HiveStorageFormat.class.getDeclaredField("inputFormat");
    makeAccessible(inputFormatField);
    inputFormatField.set(enumValue, MapredCarbonInputFormat.class.getName());
    Field outputFormatField = HiveStorageFormat.class.getDeclaredField("outputFormat");
    makeAccessible(outputFormatField);
    outputFormatField.set(enumValue, MapredCarbonOutputFormat.class.getName());
    Field estimatedWriterSystemMemoryUsageField = HiveStorageFormat.class.getDeclaredField("estimatedWriterSystemMemoryUsage");
    makeAccessible(estimatedWriterSystemMemoryUsageField);
    estimatedWriterSystemMemoryUsageField.set(enumValue, new DataSize((long) 256, MEGABYTE));
    Field values = HiveStorageFormat.class.getDeclaredField("$VALUES");
    makeAccessible(values);
    HiveStorageFormat[] hiveStorageFormats = new HiveStorageFormat[HiveStorageFormat.values().length + 1];
    HiveStorageFormat[] src = (HiveStorageFormat[]) values.get(null);
    System.arraycopy(src, 0, hiveStorageFormats, 0, src.length);
    hiveStorageFormats[src.length] = enumValue;
    values.set(null, hiveStorageFormats);
}
Also used : MapredCarbonInputFormat(org.apache.carbondata.hive.MapredCarbonInputFormat) HiveStorageFormat(io.prestosql.plugin.hive.HiveStorageFormat) DataSize(io.airlift.units.DataSize) Unsafe(sun.misc.Unsafe) CarbonHiveSerDe(org.apache.carbondata.hive.CarbonHiveSerDe) MapredCarbonOutputFormat(org.apache.carbondata.hive.MapredCarbonOutputFormat)

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