Esempio n. 1
0
  public static void main(String args[]) throws Exception {
    Unsafe unsafe = Utils.getUnsafe();

    // Allocate a byte, write to the location and read back the value
    long address = unsafe.allocateMemory(1);
    assertNotEquals(address, 0L);

    unsafe.putByte(address, Byte.MAX_VALUE);
    assertEquals(Byte.MAX_VALUE, unsafe.getByte(address));
    unsafe.freeMemory(address);

    // Call to allocateMemory() with a negative value should result in an IllegalArgumentException
    try {
      address = unsafe.allocateMemory(-1);
      throw new RuntimeException("Did not get expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
      // Expected
      assertNotEquals(address, 0L);
    }

    // allocateMemory() should throw an OutOfMemoryError when the underlying malloc fails,
    // we test this by limiting the malloc using -XX:MallocMaxTestWords
    try {
      address = unsafe.allocateMemory(100 * 1024 * 1024 * 8);
    } catch (OutOfMemoryError e) {
      // Expected
      return;
    }
    throw new RuntimeException("Did not get expected OutOfMemoryError");
  }
Esempio n. 2
0
/** Tests for jdk.vm.ci.hotspot.CompilerToVM::getConstantPool method */
public class GetConstantPoolTest {
  private static enum TestCase {
    NULL_BASE {
      @Override
      ConstantPool getConstantPool() {
        return CompilerToVMHelper.getConstantPool(null, getPtrToCpAddress());
      }
    },
    JAVA_METHOD_BASE {
      @Override
      ConstantPool getConstantPool() {
        HotSpotResolvedJavaMethod methodInstance =
            CompilerToVMHelper.getResolvedJavaMethodAtSlot(TEST_CLASS, 0);
        Field field;
        try {
          // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod
          field = methodInstance.getClass().getDeclaredField("metaspaceMethod");
          field.setAccessible(true);
          field.set(methodInstance, getPtrToCpAddress());
        } catch (ReflectiveOperationException e) {
          throw new Error("TESTBUG : " + e, e);
        }

        return CompilerToVMHelper.getConstantPool(methodInstance, 0L);
      }
    },
    CONSTANT_POOL_BASE {
      @Override
      ConstantPool getConstantPool() {
        ConstantPool cpInst;
        try {
          cpInst = CompilerToVMHelper.getConstantPool(null, getPtrToCpAddress());
          Field field =
              CompilerToVMHelper.HotSpotConstantPoolClass()
                  .getDeclaredField("metaspaceConstantPool");
          field.setAccessible(true);
          field.set(cpInst, getPtrToCpAddress());
        } catch (ReflectiveOperationException e) {
          throw new Error("TESTBUG : " + e.getMessage(), e);
        }
        return CompilerToVMHelper.getConstantPool(cpInst, 0L);
      }
    },
    CONSTANT_POOL_BASE_IN_TWO {
      @Override
      ConstantPool getConstantPool() {
        long ptr = getPtrToCpAddress();
        ConstantPool cpInst;
        try {
          cpInst = CompilerToVMHelper.getConstantPool(null, ptr);
          Field field =
              CompilerToVMHelper.HotSpotConstantPoolClass()
                  .getDeclaredField("metaspaceConstantPool");
          field.setAccessible(true);
          field.set(cpInst, ptr / 2L);
        } catch (ReflectiveOperationException e) {
          throw new Error("TESTBUG : " + e.getMessage(), e);
        }
        return CompilerToVMHelper.getConstantPool(cpInst, ptr - ptr / 2L);
      }
    },
    CONSTANT_POOL_BASE_ZERO {
      @Override
      ConstantPool getConstantPool() {
        long ptr = getPtrToCpAddress();
        ConstantPool cpInst;
        try {
          cpInst = CompilerToVMHelper.getConstantPool(null, ptr);
          Field field =
              CompilerToVMHelper.HotSpotConstantPoolClass()
                  .getDeclaredField("metaspaceConstantPool");
          field.setAccessible(true);
          field.set(cpInst, 0L);
        } catch (ReflectiveOperationException e) {
          throw new Error("TESTBUG : " + e.getMessage(), e);
        }
        return CompilerToVMHelper.getConstantPool(cpInst, ptr);
      }
    },
    OBJECT_TYPE_BASE {
      @Override
      ConstantPool getConstantPool() {
        HotSpotResolvedObjectType type =
            HotSpotResolvedObjectType.fromObjectClass(OBJECT_TYPE_BASE.getClass());
        long ptrToClass = UNSAFE.getKlassPointer(OBJECT_TYPE_BASE);
        return CompilerToVMHelper.getConstantPool(type, getPtrToCpAddress() - ptrToClass);
      }
    },
    ;

    abstract ConstantPool getConstantPool();
  }

  private static final WhiteBox WB = WhiteBox.getWhiteBox();
  private static final Unsafe UNSAFE = Utils.getUnsafe();

  private static final Class TEST_CLASS = GetConstantPoolTest.class;
  private static final long CP_ADDRESS = WB.getConstantPool(GetConstantPoolTest.class);

  public void test(TestCase testCase) {
    System.out.println(testCase.name());
    ConstantPool cp = testCase.getConstantPool();
    String cpStringRep = cp.toString();
    String cpClassSimpleName = CompilerToVMHelper.HotSpotConstantPoolClass().getSimpleName();
    if (!cpStringRep.contains(cpClassSimpleName) || !cpStringRep.contains(TEST_CLASS.getName())) {
      String msg =
          String.format(
              "%s : "
                  + " Constant pool is not valid."
                  + " String representation should contain \"%s\" and \"%s\"",
              testCase.name(), cpClassSimpleName, TEST_CLASS.getName());
      throw new AssertionError(msg);
    }
  }

  public static void main(String[] args) {
    GetConstantPoolTest test = new GetConstantPoolTest();
    for (TestCase testCase : TestCase.values()) {
      test.test(testCase);
    }
    testObjectBase();
    testMetaspaceWrapperBase();
  }

  private static void testObjectBase() {
    try {
      Object cp = CompilerToVMHelper.getConstantPool(new Object(), 0L);
      throw new AssertionError(
          "Test OBJECT_BASE." + " Expected IllegalArgumentException has not been caught");
    } catch (IllegalArgumentException iae) {
      // expected
    }
  }

  private static void testMetaspaceWrapperBase() {
    try {
      Object cp =
          CompilerToVMHelper.getConstantPool(
              new PublicMetaspaceWrapperObject() {
                @Override
                public long getMetaspacePointer() {
                  return getPtrToCpAddress();
                }
              },
              0L);
      throw new AssertionError(
          "Test METASPACE_WRAPPER_BASE."
              + " Expected IllegalArgumentException has not been caught");
    } catch (IllegalArgumentException iae) {
      // expected
    }
  }

  private static long getPtrToCpAddress() {
    Field field;
    try {
      field = TEST_CLASS.getDeclaredField("CP_ADDRESS");
    } catch (NoSuchFieldException nsfe) {
      throw new Error("TESTBUG : cannot find field \"CP_ADDRESS\" : " + nsfe.getMessage(), nsfe);
    }
    Object base = UNSAFE.staticFieldBase(field);
    return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);
  }
}