public static void main(String[] args) {
   for (String arg : args) {
     TestCaseData tcase = TestCaseData.valueOf(arg);
     CompilerToVMHelper.writeDebugOutput(tcase.getData(), tcase.offset, tcase.length);
     CompilerToVMHelper.flushDebugOutput();
   }
 }
  private static void runSanityTest(Executable aMethod, Integer expectedTableLength) {
    HotSpotResolvedJavaMethod method = CTVMUtilities.getResolvedMethod(aMethod);
    int tableLength = CompilerToVMHelper.getExceptionTableLength(method);
    Asserts.assertEQ(
        tableLength, expectedTableLength, aMethod + " incorrect exception table length.");

    long tableStart = CompilerToVMHelper.getExceptionTableStart(method);
    if (tableLength > 0) {
      Asserts.assertNE(tableStart, 0L, aMethod + " exception table starts " + "at 0.");
    }
  }
 @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);
 }
 public static void validate(
     jdk.vm.ci.meta.ConstantPool constantPoolCTVM,
     ConstantPool constantPoolSS,
     ConstantPoolTestsHelper.DummyClasses dummyClass,
     int i) {
   Object classToVerify = CompilerToVMHelper.lookupKlassInPool(constantPoolCTVM, i);
   if (!(classToVerify instanceof HotSpotResolvedObjectType)
       && !(classToVerify instanceof String)) {
     String msg =
         String.format(
             "Output of method"
                 + " CTVM.lookupKlassInPool is neither"
                 + " a HotSpotResolvedObjectType, nor a String");
     throw new AssertionError(msg);
   }
   int classNameIndex = (int) dummyClass.cp.get(i).value;
   String classNameToRefer = constantPoolSS.getUTF8At(classNameIndex);
   String outputToVerify = classToVerify.toString();
   if (!outputToVerify.contains(classNameToRefer)) {
     String msg =
         String.format(
             "Wrong class accessed by constant" + " pool index %d: %s, but should be %s",
             i, outputToVerify, classNameToRefer);
     throw new AssertionError(msg);
   }
 }
 @Override
 ConstantPool getConstantPool() {
   HotSpotResolvedObjectType type =
       HotSpotResolvedObjectType.fromObjectClass(OBJECT_TYPE_BASE.getClass());
   long ptrToClass = UNSAFE.getKlassPointer(OBJECT_TYPE_BASE);
   return CompilerToVMHelper.getConstantPool(type, getPtrToCpAddress() - ptrToClass);
 }
      @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);
      }
 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 void runTest(TestCase tcase) {
   System.out.println(tcase);
   HotSpotResolvedObjectType resolvedIface =
       CompilerToVMHelper.lookupType(
           Utils.toJVMTypeSignature(tcase.anInterface), getClass(), /* resolve = */ true);
   HotSpotResolvedObjectType resolvedImplementer =
       CompilerToVMHelper.getImplementor(resolvedIface);
   HotSpotResolvedObjectType resolvedExpected = null;
   if (tcase.expectedImplementer != null) {
     resolvedExpected =
         CompilerToVMHelper.lookupType(
             Utils.toJVMTypeSignature(tcase.expectedImplementer),
             getClass(),
             /* resolve = */ true);
   }
   Asserts.assertEQ(
       resolvedImplementer,
       resolvedExpected,
       "Unexpected implementer for " + tcase.anInterface.getName());
 }
 private void check(boolean materialize) {
   // Materialize virtual objects on last invocation
   if (materialize) {
     HotSpotStackFrameReference hsFrame =
         CompilerToVMHelper.getNextStackFrame(
             /* topmost frame */ null,
             new ResolvedJavaMethod[] {RESOLVED_METHOD}, /* don't skip any */
             0);
     Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
     Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName() + "Test method should be compiled");
     Asserts.assertTrue(
         hsFrame.hasVirtualObjects(),
         getName() + ": has no virtual object before materialization");
     CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
     Asserts.assertFalse(
         hsFrame.hasVirtualObjects(), getName() + " : has virtual object after materialization");
     Asserts.assertEQ(
         WB.isMethodCompiled(METHOD), !INVALIDATE, getName() + " : unexpected compiled status");
   }
 }
 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);
   }
 }
  private void checkSanity(CompileCodeTestCase testCase) {
    System.out.println(testCase);
    // to have a clean state
    testCase.deoptimize();
    Pair<Object, ? extends Throwable> reflectionResult;
    Object[] args = Utils.getNullValues(testCase.executable.getParameterTypes());
    reflectionResult = testCase.invoke(args);
    NMethod nMethod = testCase.compile();
    if (nMethod == null) {
      throw new Error(testCase + " : nmethod is null");
    }
    InstalledCode installedCode = testCase.toInstalledCode();
    Object result = null;
    Throwable expectedException = reflectionResult.second;
    boolean gotException = true;
    try {
      args = addReceiver(testCase, args);
      result = CompilerToVMHelper.executeInstalledCode(args, installedCode);
      if (testCase.executable instanceof Constructor) {
        // <init> doesn't have return value, it changes receiver
        result = args[0];
      }
      gotException = false;
    } catch (InvalidInstalledCodeException e) {
      throw new AssertionError(testCase + " : unexpected InvalidInstalledCodeException", e);
    } catch (Throwable t) {
      if (expectedException == null) {
        throw new AssertionError(testCase + " : got unexpected execption : " + t.getMessage(), t);
      }

      if (expectedException.getClass() != t.getClass()) {
        System.err.println("exception from CompilerToVM:");
        t.printStackTrace();
        System.err.println("exception from reflection:");
        expectedException.printStackTrace();
        throw new AssertionError(
            String.format(
                "%s : got unexpected different exceptions : %s != %s",
                testCase, expectedException.getClass(), t.getClass()));
      }
    }

    Asserts.assertEQ(reflectionResult.first, result, testCase + " : different return value");
    if (!gotException) {
      Asserts.assertNull(expectedException, testCase + " : expected exception hasn't been thrown");
    }
  }
 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 void check(CompileCodeTestCase testCase) {
    System.out.println(testCase);
    HotSpotResolvedJavaMethod javaMethod = CTVMUtilities.getResolvedMethod(testCase.executable);
    HotSpotCompilationRequest compRequest =
        new HotSpotCompilationRequest(javaMethod, testCase.bci, /* jvmciEnv = */ 0L);
    String name = testCase.executable.getName();
    CompilationResult compResult = new CompilationResult(name);
    // to pass sanity check of default -1
    compResult.setTotalFrameSize(0);
    InstalledCode installedCode =
        CACHE_PROVIDER.installCode(
            compRequest,
            compResult,
            new InstalledCode(name),
            /* speculationLog = */ null,
            /* isDefault = */ false);
    Asserts.assertTrue(
        installedCode.isValid(), testCase + " : code is invalid even before invalidation");

    NMethod beforeInvalidation = testCase.toNMethod();
    if (beforeInvalidation != null) {
      throw new Error("TESTBUG : " + testCase + " : nmethod isn't found");
    }
    // run twice to verify how it works if method is already invalidated
    for (int i = 0; i < 2; ++i) {
      CompilerToVMHelper.invalidateInstalledCode(installedCode);
      Asserts.assertFalse(
          installedCode.isValid(), testCase + " : code is valid after invalidation, i = " + i);
      NMethod afterInvalidation = testCase.toNMethod();
      if (afterInvalidation != null) {
        System.err.println("before: " + beforeInvalidation);
        System.err.println("after: " + afterInvalidation);
        throw new AssertionError(testCase + " : method hasn't been invalidated, i = " + i);
      }
    }
  }
 private void checkNull() {
   Utils.runAndCheckException(
       () -> CompilerToVMHelper.invalidateInstalledCode(null), NullPointerException.class);
 }
 @Override
 ConstantPool getConstantPool() {
   return CompilerToVMHelper.getConstantPool(null, getPtrToCpAddress());
 }