Re: VMAccessController suggestion

Marcel Wiesweg <[email protected]> Wed, 1 Jun 2005 16:51:43 +0200
Newsgroups gmane.comp.java.vm.sablevm.devel
Message-ID <[email protected]>
> Am I missing something when I say that the VMStackWalker implementation can
> be used to obtain the Classes array for VMAccessController.getStack()? If
> that is possible, I would suggest to add a native function like
> fillMethodNames to VMStackWalker (or move both to some other class? I would
> not, depends on your taste), which is as lightweight as fillContext, to
> obtain the method names, and use both in VMAccessController.getStack().

Attached are two short patches which implement what I suggested. (one against 
svm-cp-merge, the other against svm-merge)

Marcel

_______________________________________________
SableVM-devel mailing list
[email protected]
http://sablevm.org/lists/control/listinfo/sablevm-devel
vmstackwalker-svm.diff (text/x-diff, 2.3 KB)
Index: src/libsablevm/gnu_classpath_VMStackWalker.c
===================================================================
--- src/libsablevm/gnu_classpath_VMStackWalker.c	(revision 4038)
+++ src/libsablevm/gnu_classpath_VMStackWalker.c	(working copy)
@@ -118,6 +118,77 @@
 
 /*
 ----------------------------------------------------------------------
+Java_gnu_classpath_VMStackWalker_fillMethods
+----------------------------------------------------------------------
+*/
+
+/*
+ * Class:     gnu_classpath_VMStackWalker
+ * Method:    fillMethods
+ * Signature: ([Ljava/lang/String;)V
+ */
+
+JNIEXPORT static void JNICALL
+Java_gnu_classpath_VMStackWalker_fillMethods (JNIEnv *_env,
+					      jclass _class SVM_UNUSED,
+					      jobjectArray context)
+{
+  _svmt_JNIEnv *env = _svmf_cast_svmt_JNIEnv (_env);
+
+  _svmm_resuming_java (env);
+
+  jstring methodName = NULL;
+
+  {
+    jint size = -2;		/* we must ignore the top 2 frames */
+    _svmt_JavaVM *vm = env->vm;
+    _svmt_stack_frame *frame = env->stack.current_frame;
+    _svmt_method_info *method = frame->method;
+
+    while (method != &vm->stack_bottom_method)
+      {
+	/* skip internal frames */
+	if (_svmf_is_set_flag (method->access_flags, SVM_ACC_INTERNAL))
+	  {
+	    frame = (_svmt_stack_frame *) (void *)
+	      (((char *) frame) - frame->previous_offset);
+	    method = frame->method;
+	    continue;
+	  }
+
+	if (size >= 0)
+	  {
+            methodName = _svmf_get_jni_frame_native_local (env);
+            if (_svmf_get_string (env, DREF (method->name, value), methodName) !=
+                  JNI_OK)
+               {
+                  goto end;
+               }
+#ifndef NDEBUG
+	    int result =
+#endif
+	      _svmf_set_reference_array_element_no_exception
+	      (env, *context, size, *methodName);
+
+	    assert (result == JNI_OK);	/* calling java library code *must* be flawless! */
+	    _svmm_release_jni_frame_native_local (methodName);
+	  }
+
+	size++;
+
+	frame = (_svmt_stack_frame *) (void *)
+	  (((char *) frame) - frame->previous_offset);
+	method = frame->method;
+      }
+  }
+
+end:
+
+  _svmm_stopping_java (env);
+}
+
+/*
+----------------------------------------------------------------------
 Java_gnu_classpath_VMStackWalker_getCallingClass
 ----------------------------------------------------------------------
 */
vmaccesscontroller-cp.diff (text/x-diff, 1.5 KB)
Index: vm/reference/java/security/VMAccessController.java
===================================================================
--- vm/reference/java/security/VMAccessController.java	(revision 4067)
+++ vm/reference/java/security/VMAccessController.java	(working copy)
@@ -39,6 +39,8 @@
 import java.util.HashSet;
 import java.util.LinkedList;
 
+import gnu.classpath.VMStackWalker;
+
 final class VMAccessController
 {
 
@@ -255,6 +257,7 @@
    */
   private static Object[][] getStack()
   {
-    return new Object[][] { new Class[0], new String[0] };
+    return VMStackWalker.getStack();
+    //return new Object[][] { new Class[0], new String[0] };
   }
 }
Index: vm/reference/gnu/classpath/VMStackWalker.java
===================================================================
--- vm/reference/gnu/classpath/VMStackWalker.java	(revision 4067)
+++ vm/reference/gnu/classpath/VMStackWalker.java	(working copy)
@@ -70,9 +70,19 @@
       fillContext(context);
       return context;
   }
+  
+  public static Object[][] getStack() {
+      int contextSize = getContextSize();
+      Class[] context = new Class[contextSize];
+      String[] methods = new String[contextSize];
+      fillContext(context);
+      fillMethods(methods);
+      return new Object[][] { context, methods };
+  }
 
   private static native int getContextSize();
   private static native void fillContext(Class[] context);
+  private static native void fillMethods(String[] methods);
 
   /**
    * Equivalent to: