8194490: [JVMCI] Move `iterateFrames` to C++
authorgdub
Wed, 07 Mar 2018 19:32:54 -0800
changeset 49358 0dc249f5c260
parent 49357 aaedb8343784
child 49359 59f6547e151f
8194490: [JVMCI] Move `iterateFrames` to C++ Reviewed-by: kvn, never, dnsimon
src/hotspot/share/jvmci/jvmciCompilerToVM.cpp
src/hotspot/share/jvmci/jvmciJavaClasses.hpp
src/hotspot/share/jvmci/systemDictionary_jvmci.hpp
src/hotspot/share/jvmci/vmSymbols_jvmci.hpp
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.code/src/jdk/vm/ci/code/stack/StackIntrospection.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/CompilerToVM.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackFrameReference.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackIntrospection.java
test/hotspot/jtreg/compiler/jvmci/common/patches/jdk.internal.vm.ci/jdk/vm/ci/hotspot/CompilerToVMHelper.java
test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetNextStackFrameTest.java
test/hotspot/jtreg/compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest.java
--- a/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp	Wed Mar 07 19:32:54 2018 -0800
@@ -1376,53 +1376,40 @@
   return false;
 }
 
-C2V_VMENTRY(jobject, getNextStackFrame, (JNIEnv*, jobject compilerToVM, jobject hs_frame, jobjectArray methods, jint initialSkip))
+void call_interface(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
+  CallInfo callinfo;
+  Handle receiver = args->receiver();
+  Klass* recvrKlass = receiver.is_null() ? (Klass*)NULL : receiver->klass();
+  LinkInfo link_info(spec_klass, name, signature);
+  LinkResolver::resolve_interface_call(
+          callinfo, receiver, recvrKlass, link_info, true, CHECK);
+  methodHandle method = callinfo.selected_method();
+  assert(method.not_null(), "should have thrown exception");
+
+  // Invoke the method
+  JavaCalls::call(result, method, args, CHECK);
+}
+
+C2V_VMENTRY(jobject, iterateFrames, (JNIEnv*, jobject compilerToVM, jobjectArray initial_methods, jobjectArray match_methods, jint initialSkip, jobject visitor_handle))
   ResourceMark rm;
 
-  if (!thread->has_last_Java_frame()) return NULL;
-  Handle result = HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);
+  if (!thread->has_last_Java_frame()) {
+    return NULL;
+  }
+  Handle visitor(THREAD, JNIHandles::resolve_non_null(visitor_handle));
+  Handle frame_reference = HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);
   HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);
 
   StackFrameStream fst(thread);
-  if (hs_frame != NULL) {
-    // look for the correct stack frame if one is given
-    intptr_t* stack_pointer = (intptr_t*) HotSpotStackFrameReference::stackPointer(hs_frame);
-    while (fst.current()->sp() != stack_pointer && !fst.is_done()) {
-      fst.next();
-    }
-    if (fst.current()->sp() != stack_pointer) {
-      THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "stack frame not found")
-    }
-  }
+
+  jobjectArray methods = initial_methods;
 
   int frame_number = 0;
   vframe* vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
-  if (hs_frame != NULL) {
-    // look for the correct vframe within the stack frame if one is given
-    int last_frame_number = HotSpotStackFrameReference::frameNumber(hs_frame);
-    while (frame_number < last_frame_number) {
-      if (vf->is_top()) {
-        THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "invalid frame number")
-      }
-      vf = vf->sender();
-      frame_number ++;
-    }
-    // move one frame forward
-    if (vf->is_top()) {
-      if (fst.is_done()) {
-        return NULL;
-      }
-      fst.next();
-      vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
-      frame_number = 0;
-    } else {
-      vf = vf->sender();
-      frame_number++;
-    }
-  }
 
   while (true) {
     // look for the given method
+    bool realloc_called = false;
     while (true) {
       StackValueCollection* locals = NULL;
       if (vf->is_compiled_frame()) {
@@ -1430,13 +1417,28 @@
         compiledVFrame* cvf = compiledVFrame::cast(vf);
         if (methods == NULL || matches(methods, cvf->method())) {
           if (initialSkip > 0) {
-            initialSkip --;
+            initialSkip--;
           } else {
             ScopeDesc* scope = cvf->scope();
             // native wrappers do not have a scope
             if (scope != NULL && scope->objects() != NULL) {
-              bool realloc_failures = Deoptimization::realloc_objects(thread, fst.current(), scope->objects(), CHECK_NULL);
-              Deoptimization::reassign_fields(fst.current(), fst.register_map(), scope->objects(), realloc_failures, false);
+              GrowableArray<ScopeValue*>* objects;
+              if (!realloc_called) {
+                objects = scope->objects();
+              } else {
+                // some object might already have been re-allocated, only reallocate the non-allocated ones
+                objects = new GrowableArray<ScopeValue*>(scope->objects()->length());
+                int ii = 0;
+                for (int i = 0; i < scope->objects()->length(); i++) {
+                  ObjectValue* sv = (ObjectValue*) scope->objects()->at(i);
+                  if (sv->value().is_null()) {
+                    objects->at_put(ii++, sv);
+                  }
+                }
+              }
+              bool realloc_failures = Deoptimization::realloc_objects(thread, fst.current(), objects, CHECK_NULL);
+              Deoptimization::reassign_fields(fst.current(), fst.register_map(), objects, realloc_failures, false);
+              realloc_called = true;
 
               GrowableArray<ScopeValue*>* local_values = scope->locals();
               assert(local_values != NULL, "NULL locals");
@@ -1448,15 +1450,15 @@
                   array->bool_at_put(i, true);
                 }
               }
-              HotSpotStackFrameReference::set_localIsVirtual(result, array());
+              HotSpotStackFrameReference::set_localIsVirtual(frame_reference, array());
             } else {
-              HotSpotStackFrameReference::set_localIsVirtual(result, NULL);
+              HotSpotStackFrameReference::set_localIsVirtual(frame_reference, NULL);
             }
 
             locals = cvf->locals();
-            HotSpotStackFrameReference::set_bci(result, cvf->bci());
+            HotSpotStackFrameReference::set_bci(frame_reference, cvf->bci());
             oop method = CompilerToVM::get_jvmci_method(cvf->method(), CHECK_NULL);
-            HotSpotStackFrameReference::set_method(result, method);
+            HotSpotStackFrameReference::set_method(frame_reference, method);
           }
         }
       } else if (vf->is_interpreted_frame()) {
@@ -1464,22 +1466,23 @@
         interpretedVFrame* ivf = interpretedVFrame::cast(vf);
         if (methods == NULL || matches(methods, ivf->method())) {
           if (initialSkip > 0) {
-            initialSkip --;
+            initialSkip--;
           } else {
             locals = ivf->locals();
-            HotSpotStackFrameReference::set_bci(result, ivf->bci());
+            HotSpotStackFrameReference::set_bci(frame_reference, ivf->bci());
             oop method = CompilerToVM::get_jvmci_method(ivf->method(), CHECK_NULL);
-            HotSpotStackFrameReference::set_method(result, method);
-            HotSpotStackFrameReference::set_localIsVirtual(result, NULL);
+            HotSpotStackFrameReference::set_method(frame_reference, method);
+            HotSpotStackFrameReference::set_localIsVirtual(frame_reference, NULL);
           }
         }
       }
 
       // locals != NULL means that we found a matching frame and result is already partially initialized
       if (locals != NULL) {
-        HotSpotStackFrameReference::set_compilerToVM(result, JNIHandles::resolve(compilerToVM));
-        HotSpotStackFrameReference::set_stackPointer(result, (jlong) fst.current()->sp());
-        HotSpotStackFrameReference::set_frameNumber(result, frame_number);
+        methods = match_methods;
+        HotSpotStackFrameReference::set_compilerToVM(frame_reference, JNIHandles::resolve(compilerToVM));
+        HotSpotStackFrameReference::set_stackPointer(frame_reference, (jlong) fst.current()->sp());
+        HotSpotStackFrameReference::set_frameNumber(frame_reference, frame_number);
 
         // initialize the locals array
         objArrayOop array_oop = oopFactory::new_objectArray(locals->size(), CHECK_NULL);
@@ -1490,9 +1493,41 @@
             array->obj_at_put(i, locals->at(i)->get_obj()());
           }
         }
-        HotSpotStackFrameReference::set_locals(result, array());
+        HotSpotStackFrameReference::set_locals(frame_reference, array());
+        HotSpotStackFrameReference::set_objectsMaterialized(frame_reference, JNI_FALSE);
 
-        return JNIHandles::make_local(thread, result());
+        JavaValue result(T_OBJECT);
+        JavaCallArguments args(visitor);
+        args.push_oop(frame_reference);
+        call_interface(&result, SystemDictionary::InspectedFrameVisitor_klass(), vmSymbols::visitFrame_name(), vmSymbols::visitFrame_signature(), &args, CHECK_NULL);
+        if (result.get_jobject() != NULL) {
+          return JNIHandles::make_local(thread, (oop) result.get_jobject());
+        }
+        assert(initialSkip == 0, "There should be no match before initialSkip == 0");
+        if (HotSpotStackFrameReference::objectsMaterialized(frame_reference) == JNI_TRUE) {
+          // the frame has been deoptimized, we need to re-synchronize the frame and vframe
+          intptr_t* stack_pointer = (intptr_t*) HotSpotStackFrameReference::stackPointer(frame_reference);
+          fst = StackFrameStream(thread);
+          while (fst.current()->sp() != stack_pointer && !fst.is_done()) {
+            fst.next();
+          }
+          if (fst.current()->sp() != stack_pointer) {
+            THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "stack frame not found after deopt")
+          }
+          vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
+          if (!vf->is_compiled_frame()) {
+            THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected")
+          }
+          for (int i = 0; i < frame_number; i++) {
+            if (vf->is_top()) {
+              THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "vframe not found after deopt")
+            }
+            vf = vf->sender();
+            assert(vf->is_compiled_frame(), "Wrong frame type");
+          }
+        }
+        frame_reference = HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);
+        HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);
       }
 
       if (vf->is_top()) {
@@ -1712,6 +1747,7 @@
       array->obj_at_put(i, locals->at(i)->get_obj()());
     }
   }
+  HotSpotStackFrameReference::set_objectsMaterialized(hs_frame, JNI_TRUE);
 C2V_END
 
 C2V_VMENTRY(void, writeDebugOutput, (JNIEnv*, jobject, jbyteArray bytes, jint offset, jint length))
@@ -1826,24 +1862,25 @@
 #define CC (char*)  /*cast a literal from (const char*)*/
 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f))
 
-#define STRING                "Ljava/lang/String;"
-#define OBJECT                "Ljava/lang/Object;"
-#define CLASS                 "Ljava/lang/Class;"
-#define EXECUTABLE            "Ljava/lang/reflect/Executable;"
-#define STACK_TRACE_ELEMENT   "Ljava/lang/StackTraceElement;"
-#define INSTALLED_CODE        "Ljdk/vm/ci/code/InstalledCode;"
-#define TARGET_DESCRIPTION    "Ljdk/vm/ci/code/TargetDescription;"
-#define BYTECODE_FRAME        "Ljdk/vm/ci/code/BytecodeFrame;"
-#define RESOLVED_METHOD       "Ljdk/vm/ci/meta/ResolvedJavaMethod;"
-#define HS_RESOLVED_METHOD    "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;"
-#define HS_RESOLVED_KLASS     "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;"
-#define HS_CONSTANT_POOL      "Ljdk/vm/ci/hotspot/HotSpotConstantPool;"
-#define HS_COMPILED_CODE      "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;"
-#define HS_CONFIG             "Ljdk/vm/ci/hotspot/HotSpotVMConfig;"
-#define HS_METADATA           "Ljdk/vm/ci/hotspot/HotSpotMetaData;"
-#define HS_STACK_FRAME_REF    "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;"
-#define HS_SPECULATION_LOG    "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;"
-#define METASPACE_METHOD_DATA "J"
+#define STRING                  "Ljava/lang/String;"
+#define OBJECT                  "Ljava/lang/Object;"
+#define CLASS                   "Ljava/lang/Class;"
+#define EXECUTABLE              "Ljava/lang/reflect/Executable;"
+#define STACK_TRACE_ELEMENT     "Ljava/lang/StackTraceElement;"
+#define INSTALLED_CODE          "Ljdk/vm/ci/code/InstalledCode;"
+#define TARGET_DESCRIPTION      "Ljdk/vm/ci/code/TargetDescription;"
+#define BYTECODE_FRAME          "Ljdk/vm/ci/code/BytecodeFrame;"
+#define INSPECTED_FRAME_VISITOR "Ljdk/vm/ci/code/stack/InspectedFrameVisitor;"
+#define RESOLVED_METHOD         "Ljdk/vm/ci/meta/ResolvedJavaMethod;"
+#define HS_RESOLVED_METHOD      "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;"
+#define HS_RESOLVED_KLASS       "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;"
+#define HS_CONSTANT_POOL        "Ljdk/vm/ci/hotspot/HotSpotConstantPool;"
+#define HS_COMPILED_CODE        "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;"
+#define HS_CONFIG               "Ljdk/vm/ci/hotspot/HotSpotVMConfig;"
+#define HS_METADATA             "Ljdk/vm/ci/hotspot/HotSpotMetaData;"
+#define HS_STACK_FRAME_REF      "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;"
+#define HS_SPECULATION_LOG      "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;"
+#define METASPACE_METHOD_DATA   "J"
 
 JNINativeMethod CompilerToVM::methods[] = {
   {CC "getBytecode",                                  CC "(" HS_RESOLVED_METHOD ")[B",                                                      FN_PTR(getBytecode)},
@@ -1899,7 +1936,7 @@
   {CC "isMature",                                     CC "(" METASPACE_METHOD_DATA ")Z",                                                    FN_PTR(isMature)},
   {CC "hasCompiledCodeForOSR",                        CC "(" HS_RESOLVED_METHOD "II)Z",                                                     FN_PTR(hasCompiledCodeForOSR)},
   {CC "getSymbol",                                    CC "(J)" STRING,                                                                      FN_PTR(getSymbol)},
-  {CC "getNextStackFrame",                            CC "(" HS_STACK_FRAME_REF "[" RESOLVED_METHOD "I)" HS_STACK_FRAME_REF,                FN_PTR(getNextStackFrame)},
+  {CC "iterateFrames",                                CC "([" RESOLVED_METHOD "[" RESOLVED_METHOD "I" INSPECTED_FRAME_VISITOR ")" OBJECT,   FN_PTR(iterateFrames)},
   {CC "materializeVirtualObjects",                    CC "(" HS_STACK_FRAME_REF "Z)V",                                                      FN_PTR(materializeVirtualObjects)},
   {CC "shouldDebugNonSafepoints",                     CC "()Z",                                                                             FN_PTR(shouldDebugNonSafepoints)},
   {CC "writeDebugOutput",                             CC "([BII)V",                                                                         FN_PTR(writeDebugOutput)},
--- a/src/hotspot/share/jvmci/jvmciJavaClasses.hpp	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/hotspot/share/jvmci/jvmciJavaClasses.hpp	Wed Mar 07 19:32:54 2018 -0800
@@ -288,6 +288,7 @@
   end_class                                                                                                                                                    \
   start_class(HotSpotStackFrameReference)                                                                                                                      \
     oop_field(HotSpotStackFrameReference, compilerToVM, "Ljdk/vm/ci/hotspot/CompilerToVM;")                                                                    \
+    boolean_field(HotSpotStackFrameReference, objectsMaterialized)                                                                                             \
     long_field(HotSpotStackFrameReference, stackPointer)                                                                                                       \
     int_field(HotSpotStackFrameReference, frameNumber)                                                                                                         \
     int_field(HotSpotStackFrameReference, bci)                                                                                                                 \
--- a/src/hotspot/share/jvmci/systemDictionary_jvmci.hpp	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/hotspot/share/jvmci/systemDictionary_jvmci.hpp	Wed Mar 07 19:32:54 2018 -0800
@@ -80,6 +80,7 @@
   do_klass(site_Infopoint_klass,                         jdk_vm_ci_code_site_Infopoint,                         Jvmci) \
   do_klass(site_Site_klass,                              jdk_vm_ci_code_site_Site,                              Jvmci) \
   do_klass(site_InfopointReason_klass,                   jdk_vm_ci_code_site_InfopointReason,                   Jvmci) \
+  do_klass(InspectedFrameVisitor_klass,                  jdk_vm_ci_code_stack_InspectedFrameVisitor,            Jvmci) \
   do_klass(JavaConstant_klass,                           jdk_vm_ci_meta_JavaConstant,                           Jvmci) \
   do_klass(PrimitiveConstant_klass,                      jdk_vm_ci_meta_PrimitiveConstant,                      Jvmci) \
   do_klass(RawConstant_klass,                            jdk_vm_ci_meta_RawConstant,                            Jvmci) \
--- a/src/hotspot/share/jvmci/vmSymbols_jvmci.hpp	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/hotspot/share/jvmci/vmSymbols_jvmci.hpp	Wed Mar 07 19:32:54 2018 -0800
@@ -88,9 +88,12 @@
   template(jdk_vm_ci_code_site_ExceptionHandler,                  "jdk/vm/ci/code/site/ExceptionHandler")                  \
   template(jdk_vm_ci_code_site_Mark,                              "jdk/vm/ci/code/site/Mark")                              \
   template(jdk_vm_ci_code_site_Infopoint,                         "jdk/vm/ci/code/site/Infopoint")                         \
+  template(jdk_vm_ci_code_stack_InspectedFrameVisitor,            "jdk/vm/ci/code/stack/InspectedFrameVisitor")            \
   template(jdk_vm_ci_code_site_Site,                              "jdk/vm/ci/code/site/Site")                              \
   template(jdk_vm_ci_code_site_InfopointReason,                   "jdk/vm/ci/code/site/InfopointReason")                   \
   template(jdk_vm_ci_common_JVMCIError,                           "jdk/vm/ci/common/JVMCIError")                           \
+  template(visitFrame_name,                                       "visitFrame")                                            \
+  template(visitFrame_signature,                                  "(Ljdk/vm/ci/code/stack/InspectedFrame;)Ljava/lang/Object;") \
   template(adjustCompilationLevel_name,                           "adjustCompilationLevel")                                \
   template(adjustCompilationLevel_signature,                      "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;ZI)I") \
   template(compileMethod_name,                                    "compileMethod")                                         \
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.code/src/jdk/vm/ci/code/stack/StackIntrospection.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.code/src/jdk/vm/ci/code/stack/StackIntrospection.java	Wed Mar 07 19:32:54 2018 -0800
@@ -27,15 +27,16 @@
 public interface StackIntrospection {
 
     /**
-     * Accesses the current stack, providing {@link InspectedFrame}s to the visitor that can be used
-     * to inspect the stack frames' contents. Iteration continues as long as
+     * Walks the current stack, providing {@link InspectedFrame}s to the visitor that can be used to
+     * inspect the stack frame's contents. Iteration continues as long as
      * {@link InspectedFrameVisitor#visitFrame}, which is invoked for every {@link InspectedFrame},
-     * returns null. Any non-null result of the visitor indicates that frame iteration should stop.
+     * returns {@code null}. A non-null return value from {@link InspectedFrameVisitor#visitFrame}
+     * indicates that frame iteration should stop.
      *
-     * @param initialMethods if this is non-{@code null}, then the stack trace will start at these
-     *            methods
-     * @param matchingMethods if this is non-{@code null}, then only matching stack frames are
-     *            returned
+     * @param initialMethods if this is non-{@code null}, then the stack walk will start at the
+     *            first frame whose method is one of these methods.
+     * @param matchingMethods if this is non-{@code null}, then only frames whose methods are in
+     *            this array are visited
      * @param initialSkip the number of matching methods to skip (including the initial method)
      * @param visitor the visitor that is called for every matching method
      * @return the last result returned by the visitor (which is non-null to indicate that iteration
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/CompilerToVM.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/CompilerToVM.java	Wed Mar 07 19:32:54 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,7 @@
 import jdk.vm.ci.code.InstalledCode;
 import jdk.vm.ci.code.InvalidInstalledCodeException;
 import jdk.vm.ci.code.TargetDescription;
+import jdk.vm.ci.code.stack.InspectedFrameVisitor;
 import jdk.vm.ci.common.InitTimer;
 import jdk.vm.ci.common.JVMCIError;
 import jdk.vm.ci.meta.JavaType;
@@ -514,13 +515,9 @@
     native String getSymbol(long metaspaceSymbol);
 
     /**
-     * Looks for the next Java stack frame matching an entry in {@code methods}.
-     *
-     * @param frame the starting point of the search, where {@code null} refers to the topmost frame
-     * @param methods the methods to look for, where {@code null} means that any frame is returned
-     * @return the frame, or {@code null} if the end of the stack was reached during the search
+     * @see jdk.vm.ci.code.stack.StackIntrospection#iterateFrames
      */
-    native HotSpotStackFrameReference getNextStackFrame(HotSpotStackFrameReference frame, ResolvedJavaMethod[] methods, int initialSkip);
+    native <T> T iterateFrames(ResolvedJavaMethod[] initialMethods, ResolvedJavaMethod[] matchingMethods, int initialSkip, InspectedFrameVisitor<T> visitor);
 
     /**
      * Materializes all virtual objects within {@code stackFrame} and updates its locals.
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackFrameReference.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackFrameReference.java	Wed Mar 07 19:32:54 2018 -0800
@@ -30,6 +30,8 @@
 public class HotSpotStackFrameReference implements InspectedFrame {
 
     private CompilerToVM compilerToVM;
+    // set in the VM when materializeVirtualObjects is called
+    @SuppressWarnings("unused") private boolean objectsMaterialized;
 
     // information used to find the stack frame
     private long stackPointer;
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackIntrospection.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotStackIntrospection.java	Wed Mar 07 19:32:54 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,14 +37,6 @@
     @Override
     public <T> T iterateFrames(ResolvedJavaMethod[] initialMethods, ResolvedJavaMethod[] matchingMethods, int initialSkip, InspectedFrameVisitor<T> visitor) {
         CompilerToVM compilerToVM = runtime.getCompilerToVM();
-        HotSpotStackFrameReference current = compilerToVM.getNextStackFrame(null, initialMethods, initialSkip);
-        while (current != null) {
-            T result = visitor.visitFrame(current);
-            if (result != null) {
-                return result;
-            }
-            current = compilerToVM.getNextStackFrame(current, matchingMethods, 0);
-        }
-        return null;
+        return compilerToVM.iterateFrames(initialMethods, matchingMethods, initialSkip, visitor);
     }
 }
--- a/test/hotspot/jtreg/compiler/jvmci/common/patches/jdk.internal.vm.ci/jdk/vm/ci/hotspot/CompilerToVMHelper.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/test/hotspot/jtreg/compiler/jvmci/common/patches/jdk.internal.vm.ci/jdk/vm/ci/hotspot/CompilerToVMHelper.java	Wed Mar 07 19:32:54 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
 import jdk.vm.ci.code.InstalledCode;
 import jdk.vm.ci.code.InvalidInstalledCodeException;
 import jdk.vm.ci.code.TargetDescription;
+import jdk.vm.ci.code.stack.InspectedFrameVisitor;
 import jdk.vm.ci.meta.ConstantPool;
 import jdk.vm.ci.meta.ResolvedJavaMethod;
 import java.lang.reflect.Executable;
@@ -258,10 +259,12 @@
         return CTVM.getSymbol(metaspaceSymbol);
     }
 
-    public static HotSpotStackFrameReference getNextStackFrame(
-            HotSpotStackFrameReference frame,
-            ResolvedJavaMethod[] methods, int initialSkip) {
-        return CTVM.getNextStackFrame(frame, methods, initialSkip);
+    public static <T> T iterateFrames(
+            ResolvedJavaMethod[] initialMethods,
+            ResolvedJavaMethod[] matchingMethods,
+            int initialSkip,
+            InspectedFrameVisitor<T> visitor) {
+        return CTVM.iterateFrames(initialMethods, matchingMethods, initialSkip, visitor);
     }
 
     public static void materializeVirtualObjects(
--- a/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetNextStackFrameTest.java	Wed Mar 07 13:26:15 2018 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,244 +0,0 @@
-/*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * @test
- * @bug 8136421
- * @requires vm.jvmci
- * @library / /test/lib
- * @library ../common/patches
- * @modules java.base/jdk.internal.misc
- * @modules java.base/jdk.internal.org.objectweb.asm
- *          java.base/jdk.internal.org.objectweb.asm.tree
- *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
- *          jdk.internal.vm.ci/jdk.vm.ci.code
- *          jdk.internal.vm.ci/jdk.vm.ci.meta
- * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
- * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
- *                   -Djvmci.Compiler=null
- *                   compiler.jvmci.compilerToVM.GetNextStackFrameTest
- */
-
-package compiler.jvmci.compilerToVM;
-
-import compiler.jvmci.common.CTVMUtilities;
-import jdk.test.lib.Asserts;
-import jdk.vm.ci.hotspot.CompilerToVMHelper;
-import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
-import jdk.vm.ci.meta.ResolvedJavaMethod;
-
-import java.lang.reflect.Method;
-
-public class GetNextStackFrameTest {
-    private static final int RECURSION_AMOUNT = 3;
-    private static final ResolvedJavaMethod REC_FRAME_METHOD;
-    private static final ResolvedJavaMethod FRAME1_METHOD;
-    private static final ResolvedJavaMethod FRAME2_METHOD;
-    private static final ResolvedJavaMethod FRAME3_METHOD;
-    private static final ResolvedJavaMethod FRAME4_METHOD;
-    private static final ResolvedJavaMethod RUN_METHOD;
-
-    static {
-        Method method;
-        try {
-            Class<?> aClass = GetNextStackFrameTest.class;
-            method = aClass.getDeclaredMethod("recursiveFrame", int.class);
-            REC_FRAME_METHOD = CTVMUtilities.getResolvedMethod(method);
-            method = aClass.getDeclaredMethod("frame1");
-            FRAME1_METHOD = CTVMUtilities.getResolvedMethod(method);
-            method = aClass.getDeclaredMethod("frame2");
-            FRAME2_METHOD = CTVMUtilities.getResolvedMethod(method);
-            method = aClass.getDeclaredMethod("frame3");
-            FRAME3_METHOD = CTVMUtilities.getResolvedMethod(method);
-            method = aClass.getDeclaredMethod("frame4");
-            FRAME4_METHOD = CTVMUtilities.getResolvedMethod(method);
-            method = Thread.class.getDeclaredMethod("run");
-            RUN_METHOD = CTVMUtilities.getResolvedMethod(Thread.class, method);
-        } catch (NoSuchMethodException e) {
-            throw new Error("TEST BUG: can't find a test method : " + e, e);
-        }
-    }
-
-    public static void main(String[] args) {
-        new GetNextStackFrameTest().test();
-    }
-
-    private void test() {
-        // Create new thread to get new clean stack
-        Thread thread = new Thread(() -> recursiveFrame(RECURSION_AMOUNT));
-        thread.start();
-        try {
-            thread.join();
-        } catch (InterruptedException e) {
-            throw new Error("Interrupted while waiting to join", e);
-        }
-    }
-
-    // Helper methods for a longer stack
-    private void recursiveFrame(int recursionAmount) {
-        if (--recursionAmount != 0) {
-            recursiveFrame(recursionAmount);
-        } else {
-            frame1();
-        }
-    }
-
-    private void frame1() {
-        frame2();
-    }
-
-    private void frame2() {
-        frame3();
-    }
-
-    private void frame3() {
-        frame4();
-    }
-
-    private void frame4() {
-        check();
-    }
-
-    private void check() {
-        findFirst();
-        walkThrough();
-        skipAll();
-        findNextSkipped();
-        findYourself();
-    }
-
-    /**
-     * Finds the first topmost frame from the list of methods to search
-     */
-    private void findFirst() {
-        checkNextFrameFor(null /* topmost frame */,
-                new ResolvedJavaMethod[]
-                        {FRAME2_METHOD, FRAME3_METHOD, FRAME4_METHOD},
-                FRAME4_METHOD, 0);
-    }
-
-    /**
-     * Walks through whole stack and checks that every frame could be found
-     * while going down the stack till the end
-     */
-    private void walkThrough() {
-        // Check that we would get a frame 4 starting from the topmost frame
-        HotSpotStackFrameReference nextStackFrame = checkNextFrameFor(
-                null /* topmost frame */,
-                new ResolvedJavaMethod[] {FRAME4_METHOD},
-                FRAME4_METHOD, 0);
-        // Check that we would get a frame 3 starting from frame 4 when we try
-        // to search one of the next two frames
-        nextStackFrame = checkNextFrameFor(nextStackFrame,
-                new ResolvedJavaMethod[] {FRAME3_METHOD,
-                        FRAME2_METHOD},
-                FRAME3_METHOD, 0);
-        // Check that we would get a frame 1
-        nextStackFrame = checkNextFrameFor(nextStackFrame,
-                new ResolvedJavaMethod[] {FRAME1_METHOD},
-                FRAME1_METHOD, 0);
-        // Check that we would skip (RECURSION_AMOUNT - 1) methods and find a
-        // recursionFrame starting from frame 1
-        nextStackFrame = checkNextFrameFor(nextStackFrame,
-                new ResolvedJavaMethod[] {REC_FRAME_METHOD},
-                REC_FRAME_METHOD, RECURSION_AMOUNT - 1);
-        // Check that we would get a Thread::run method frame;
-        nextStackFrame = checkNextFrameFor(nextStackFrame,
-                new ResolvedJavaMethod[] {RUN_METHOD},
-                RUN_METHOD, 0);
-        // Check that there are no more frames after thread's run method
-        nextStackFrame = CompilerToVMHelper.getNextStackFrame(nextStackFrame,
-                null /* any */, 0);
-        Asserts.assertNull(nextStackFrame,
-                "Found stack frame after Thread::run");
-    }
-
-    /**
-     * Skips all frames to get null at the end of the stack
-     */
-    private void skipAll() {
-        // Skip all frames (stack size) + 2 (getNextStackFrame() itself
-        // and from CompilerToVMHelper)
-        int initialSkip = Thread.currentThread().getStackTrace().length + 2;
-        HotSpotStackFrameReference nextStackFrame = CompilerToVMHelper
-                .getNextStackFrame(null /* topmost frame */, null /* any */,
-                        initialSkip);
-        Asserts.assertNull(nextStackFrame, "Unexpected frame");
-    }
-
-    /**
-     * Search for any frame skipping one frame
-     */
-    private void findNextSkipped() {
-        // Get frame 4
-        HotSpotStackFrameReference nextStackFrame = CompilerToVMHelper
-                .getNextStackFrame(null /* topmost frame */,
-                        new ResolvedJavaMethod[] {FRAME4_METHOD}, 0);
-        // Get frame 2 by skipping one method starting from frame 4
-        checkNextFrameFor(nextStackFrame, null /* any */,
-                FRAME2_METHOD , 1 /* skip one */);
-    }
-
-    /**
-     * Finds test method in the stack
-     */
-    private void findYourself() {
-        Method method;
-        Class<?> aClass = CompilerToVMHelper.CompilerToVMClass();
-        try {
-            method = aClass.getDeclaredMethod(
-                    "getNextStackFrame",
-                    HotSpotStackFrameReference.class,
-                    ResolvedJavaMethod[].class,
-                    int.class);
-        } catch (NoSuchMethodException e) {
-            throw new Error("TEST BUG: can't find getNextStackFrame : " + e, e);
-        }
-        ResolvedJavaMethod self
-                = CTVMUtilities.getResolvedMethod(aClass, method);
-        checkNextFrameFor(null /* topmost frame */, null /* any */, self, 0);
-    }
-
-    /**
-     * Searches next frame and checks that it equals to expected
-     *
-     * @param currentFrame  start frame to search from
-     * @param searchMethods a list of methods to search
-     * @param expected      expected frame
-     * @param skip          amount of frames to be skipped
-     * @return frame reference
-     */
-    private HotSpotStackFrameReference checkNextFrameFor(
-            HotSpotStackFrameReference currentFrame,
-            ResolvedJavaMethod[] searchMethods,
-            ResolvedJavaMethod expected,
-            int skip) {
-        HotSpotStackFrameReference nextStackFrame = CompilerToVMHelper
-                .getNextStackFrame(currentFrame, searchMethods, skip);
-        Asserts.assertNotNull(nextStackFrame);
-        Asserts.assertTrue(nextStackFrame.isMethod(expected),
-                "Unexpected next frame: " + nextStackFrame
-                        + " from current frame: " + currentFrame);
-        return nextStackFrame;
-    }
-}
--- a/test/hotspot/jtreg/compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest.java	Wed Mar 07 13:26:15 2018 -0800
+++ b/test/hotspot/jtreg/compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest.java	Wed Mar 07 19:32:54 2018 -0800
@@ -34,6 +34,7 @@
  *          java.base/jdk.internal.org.objectweb.asm.tree
  *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
  *          jdk.internal.vm.ci/jdk.vm.ci.code
+ *          jdk.internal.vm.ci/jdk.vm.ci.code.stack
  *          jdk.internal.vm.ci/jdk.vm.ci.meta
  *
  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
@@ -91,6 +92,7 @@
 import compiler.testlibrary.CompilerUtils;
 import compiler.whitebox.CompilerWhiteBoxTest;
 import jdk.test.lib.Asserts;
+import jdk.vm.ci.code.stack.InspectedFrame;
 import jdk.vm.ci.hotspot.CompilerToVMHelper;
 import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
 import jdk.vm.ci.meta.ResolvedJavaMethod;
@@ -200,18 +202,30 @@
         // Materialize virtual objects on last invocation
         if (iteration == COMPILE_THRESHOLD) {
             // get frames and check not-null
-            HotSpotStackFrameReference materialized = CompilerToVMHelper.getNextStackFrame(
-                    /* topmost frame */ null, new ResolvedJavaMethod[]{MATERIALIZED_RESOLVED},
-                    /* don't skip any */ 0);
+            HotSpotStackFrameReference materialized = CompilerToVMHelper.iterateFrames(
+                new ResolvedJavaMethod[] {MATERIALIZED_RESOLVED},
+                null /* any */,
+                0,
+                f -> (HotSpotStackFrameReference) f);
             Asserts.assertNotNull(materialized, getName()
                     + " : got null frame for materialized method");
-            HotSpotStackFrameReference notMaterialized = CompilerToVMHelper.getNextStackFrame(
-                    /* topmost frame */ null, new ResolvedJavaMethod[]{NOT_MATERIALIZED_RESOLVED},
-                    /* don't skip any */ 0);
+            Asserts.assertTrue(materialized.isMethod(MATERIALIZED_RESOLVED),
+                "Expected materialized method but got " + materialized);
+            InspectedFrame notMaterialized = CompilerToVMHelper.iterateFrames(
+                new ResolvedJavaMethod[] {NOT_MATERIALIZED_RESOLVED},
+                null /* any */,
+                0,
+                f -> f);
             Asserts.assertNE(materialized, notMaterialized,
                     "Got same frame pointer for both tested frames");
+            Asserts.assertTrue(notMaterialized.isMethod(NOT_MATERIALIZED_RESOLVED),
+                "Expected notMaterialized method but got " + notMaterialized);
             Asserts.assertNotNull(notMaterialized, getName()
                     + " : got null frame for not materialized method");
+            Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
+                + " : materialized method not compiled");
+            Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
+                getName() + " : not materialized method not compiled");
             // check that frames has virtual objects before materialization stage
             Asserts.assertTrue(materialized.hasVirtualObjects(), getName()
                     + ": materialized frame has no virtual object before materialization");