java.lang.SecurityManager

Peter Nagy tegla at katalin.csoma.elte.hu
Fri Apr 4 04:24:37 PST 1997


Szia mindenkinek!

Last day I wanted use the sun.applet.AppletViewer, but I got some serious
trouble: there is just a skeleton of SecurityManager in 0.8.3 So I drunk
some coffe and created a patch. With this some simple applets can run, and
it seems like the security checks are right (no applet can use
System.exit() for example) I've never seen the kaffe source before, so
maybe there are some design mistakes in it, if so, pleez tell me. Tested
it on i586-unknown-linux, with interpreter and jit. 

tegla
PS: hope this is the right place to send patches....
-------------- next part --------------
diff -urP kaffe-0.8.3/kaffe/kaffevm/Makefile.in kaffe-0.8.3.tegla/kaffe/kaffevm/Makefile.in
--- kaffe-0.8.3/kaffe/kaffevm/Makefile.in	Thu Mar  6 23:20:28 1997
+++ kaffe-0.8.3.tegla/kaffe/kaffevm/Makefile.in	Fri Apr  4 11:30:53 1997
@@ -59,7 +59,8 @@
 		util$(OBJEXT) \
 		verify$(OBJEXT) \
 		exception$(OBJEXT) \
-		md$(OBJEXT)
+		md$(OBJEXT) \
+		stackTrace$(OBJEXT)
 
 all:		mkkaffevm
 
diff -urP kaffe-0.8.3/kaffe/kaffevm/classMethod.c kaffe-0.8.3.tegla/kaffe/kaffevm/classMethod.c
--- kaffe-0.8.3/kaffe/kaffevm/classMethod.c	Thu Mar 20 11:09:14 1997
+++ kaffe-0.8.3.tegla/kaffe/kaffevm/classMethod.c	Fri Apr  4 11:30:53 1997
@@ -35,6 +35,7 @@
 #include "lookup.h"
 #include "support.h"
 #include "gc.h"
+#include "thread.h"
 
 #define	CLASSHASHSZ		256
 #if CLASSHASHSZ & (CLASSHASHSZ - 1)
@@ -481,6 +482,10 @@
 	int i;
 	int idx;
 	constants* pool;
+
+	if (class->state == CSTATE_LOADED) {
+		prepareClass(class);
+	}
 
 	if (class->state >= CSTATE_LINKED) {
 		return;
diff -urP kaffe-0.8.3/kaffe/kaffevm/exception.c kaffe-0.8.3.tegla/kaffe/kaffevm/exception.c
--- kaffe-0.8.3/kaffe/kaffevm/exception.c	Mon Mar 17 11:41:32 1997
+++ kaffe-0.8.3.tegla/kaffe/kaffevm/exception.c	Fri Apr  4 11:41:42 1997
@@ -32,6 +32,7 @@
 #include "external.h"
 #include "soft.h"
 #include "md.h"
+#include "stackTrace.h"
 
 #if defined(INTERPRETER)
 extern vmException* cjbuf;
@@ -182,96 +183,6 @@
 	exit(1);
 }
 
-/*
- * Build an array of char[] for the current stack backtrace.
- */
-Hjava_lang_Object*
-buildStackTrace(struct _exceptionFrame* base)
-{
-	char buf[100];
-	Hjava_lang_Object* str;
-	Hjava_lang_Object* strarray;
-	int cnt;
-	int len;
-	int i;
-	methods* meth;
-	uintp pc;
-	int32 linenr;
-	uintp linepc;
-
-#if defined(INTERPRETER)
-	vmException* frame;
-
-	cnt = 0;
-	/* First count the stack frames */
-	for (frame = cjbuf; frame != 0; frame = frame->prev) {
-		cnt++;
-	}
-#elif defined(TRANSLATOR)
-	exceptionFrame nframe;
-	exceptionFrame* frame;
-
-	if (base == 0) {
-		FIRSTFRAME(nframe, base);
-		base = &nframe;
-	}
-	/* First count the stack frames */
-	cnt = 0;
-	for (frame = base; FRAMEOKAY(frame); NEXTFRAME(frame)) {
-		cnt++;
-	}
-#endif
-
-	/* Build an array of strings */
-	strarray = alloc_objectarray(cnt, "[[B");
-	assert(strarray != 0);
-
-	cnt = 0;
-#if defined(INTERPRETER)
-	for (frame = cjbuf; frame; frame = frame->prev) {
-		meth = frame->meth;
-		pc = frame->pc;
-#elif defined(TRANSLATOR)
-	for (frame = base; FRAMEOKAY(frame); NEXTFRAME(frame)) {
-		meth = findMethodFromPC(PCFRAME(frame));
-		pc = PCFRAME(frame);
-#endif
-		if (meth != 0) {
-			linepc = 0;
-			linenr = -1;
-			if (meth->lines != 0) {
-				for (i = 0; i < meth->lines->length; i++) {
-					if (pc >= meth->lines->entry[i].start_pc && linepc < meth->lines->entry[i].start_pc) {
-						linenr = meth->lines->entry[i].line_nr;
-						linepc = meth->lines->entry[i].start_pc;
-					}
-				}
-			}
-			if (linenr == -1) {
-				sprintf(buf, "\tat %s.%s(line unknown, pc %p)",
-					CLASS_CNAME(meth->class),
-					meth->name->data, (void*)pc);
-			}
-			else {
-				sprintf(buf, "\tat %s.%s(%d)",
-					CLASS_CNAME(meth->class),
-					meth->name->data,
-					linenr);
-			}
-			len = strlen(buf);
-			str = alloc_array(len, TYPE_Byte);
-			assert(str != 0);
-			memcpy ((char*)OBJARRAY_DATA(str), buf, len);
-		}
-		else {
-			str = 0;
-		}
-		OBJARRAY_DATA(strarray)[cnt] = str;
-
-		cnt++;
-	}
-	return (strarray);
-}
 
 /*
  * Null exception - catches bad memory accesses.
diff -urP kaffe-0.8.3/kaffe/kaffevm/stackTrace.c kaffe-0.8.3.tegla/kaffe/kaffevm/stackTrace.c
--- kaffe-0.8.3/kaffe/kaffevm/stackTrace.c	Thu Jan  1 01:00:00 1970
+++ kaffe-0.8.3.tegla/kaffe/kaffevm/stackTrace.c	Fri Apr  4 11:46:35 1997
@@ -0,0 +1,184 @@
+/*
+ * stackTrace.c
+ * Handle stack trace for the interpreter or translator.
+ *
+ * Written by Peter Nagy <tegla at katalin.csoma.elte.hu>
+ */
+
+#define	DBG(s)
+
+#include "config.h"
+#include "config-std.h"
+#include "config-signal.h"
+#include "config-mem.h"
+#include <setjmp.h>
+#include "jtypes.h"
+#include "access.h"
+#include "object.h"
+#include "constants.h"
+#include "classMethod.h"
+#include "code.h"
+#include "exception.h"
+#include "baseClasses.h"
+#include "lookup.h"
+#include "thread.h"
+#include "errors.h"
+#include "itypes.h"
+#include "external.h"
+#include "soft.h"
+#include "md.h"
+#include "stackTrace.h"
+
+Hjava_lang_Object* getClassContext(void* bulk)
+{
+	int cnt=0;
+	int i;
+	stackTrace trace;
+	Hjava_lang_Object* clazz;
+	methods* meth;
+	
+	STACKTRACEINIT(trace,NULL,bulk);
+	for( ; !STACKTRACEEND(trace) ; STACKTRACESTEP(trace) ) {
+		if ( STACKTRACEMETH(trace) != 0 ) {
+			cnt++;
+		}
+	}
+
+	assert( cnt > 0 );
+	
+	clazz=alloc_objectarray(cnt,"[Ljava/lang/Class");
+	
+	STACKTRACEINIT(trace,NULL,bulk);
+	for(i=0;!STACKTRACEEND(trace);STACKTRACESTEP(trace)){
+		meth=STACKTRACEMETH(trace);
+		if ( meth !=0 ) {
+			OBJARRAY_DATA(clazz)[i] = 
+				(Hjava_lang_Object*)meth->class;
+			fprintf(stderr,"class %s, loader %d\n",
+				CLASS_CNAME(meth->class),meth->class->loader);
+			i++;
+		}
+	}
+
+	assert( cnt == i );
+
+	return clazz;
+}
+
+Hjava_lang_Class* getClassWithLoader(int* depth) {
+	int cnt=0;
+	stackTrace trace;
+	methods* meth;
+	
+	STACKTRACEINIT(trace,NULL,depth);
+	for( ; !STACKTRACEEND(trace) ; STACKTRACESTEP(trace) ) {
+		meth=STACKTRACEMETH(trace);
+		if ( meth != 0 ) {
+			if (meth->class->loader!=NULL) {
+				*depth=cnt;
+				return(meth->class);
+			}
+
+			cnt++;
+		}
+	}
+	
+	*depth=-1;
+	return(NULL);
+	
+}
+
+jint classDepth(char* name) {
+	int cnt=0;
+	stackTrace trace;
+	methods* meth;
+	
+	STACKTRACEINIT(trace,NULL,name);
+	for( ; !STACKTRACEEND(trace) ; STACKTRACESTEP(trace) ) {
+		meth=STACKTRACEMETH(trace);
+		if ( meth != 0 ) {
+			if (!strcmp(CLASS_CNAME(meth->class),name)) {
+				return cnt;
+			}
+			cnt++;
+		}
+	}
+	
+	return(-1);
+	
+}
+
+
+/*
+ * Build an array of char[] for the current stack backtrace.
+ */
+Hjava_lang_Object*
+buildStackTrace(struct _exceptionFrame* base)
+{
+	char buf[100];
+	Hjava_lang_Object* str;
+	Hjava_lang_Object* strarray;
+	int cnt;
+	int len;
+	int i;
+	methods* meth;
+	uintp pc;
+	int32 linenr;
+	uintp linepc;
+	struct _stackTrace trace;
+
+	STACKTRACEINIT(trace,base,base);
+	cnt=0;
+	while(!STACKTRACEEND(trace)) {
+		STACKTRACESTEP(trace);
+		cnt++;
+	}
+
+
+	/* Build an array of strings */
+	strarray = alloc_objectarray(cnt, "[[B");
+	assert(strarray != 0);
+
+	cnt = 0;
+
+	STACKTRACEINIT(trace,base,base);
+	for(;!STACKTRACEEND(trace);STACKTRACESTEP(trace)) {
+		meth=STACKTRACEMETH(trace);
+		pc=STACKTRACEPC(trace);
+
+		if (meth != 0) {
+			linepc = 0;
+			linenr = -1;
+			if (meth->lines != 0) {
+				for (i = 0; i < meth->lines->length; i++) {
+					if (pc >= meth->lines->entry[i].start_pc && linepc < meth->lines->entry[i].start_pc) {
+						linenr = meth->lines->entry[i].line_nr;
+						linepc = meth->lines->entry[i].start_pc;
+					}
+				}
+			}
+			if (linenr == -1) {
+				sprintf(buf, "\tat %s.%s(line unknown, pc %p)",
+					CLASS_CNAME(meth->class),
+					meth->name->data, (void*)pc);
+			}
+			else {
+				sprintf(buf, "\tat %s.%s(%d)",
+					CLASS_CNAME(meth->class),
+					meth->name->data,
+					linenr);
+			}
+			len = strlen(buf);
+			str = alloc_array(len, TYPE_Byte);
+			assert(str != 0);
+			memcpy ((char*)OBJARRAY_DATA(str), buf, len);
+		}
+		else {
+			str = 0;
+		}
+		OBJARRAY_DATA(strarray)[cnt] = str;
+
+		cnt++;
+	}
+	return (strarray);
+}
diff -urP kaffe-0.8.3/kaffe/kaffevm/stackTrace.h kaffe-0.8.3.tegla/kaffe/kaffevm/stackTrace.h
--- kaffe-0.8.3/kaffe/kaffevm/stackTrace.h	Thu Jan  1 01:00:00 1970
+++ kaffe-0.8.3.tegla/kaffe/kaffevm/stackTrace.h	Fri Apr  4 11:46:31 1997
@@ -0,0 +1,55 @@
+/*
+ * stackTrace.h
+ *
+ */
+
+#ifndef __stacktrace_h
+#define __stacktrace_h
+
+#include "exception.h"
+
+#if defined(INTERPRETER)
+extern vmException* cjbuf;
+typedef struct _stackTrace {
+	vmException* frame;
+} stackTrace;
+
+#define STACKTRACEINIT(stacktrace,init,o) ((stacktrace).frame=(cjbuf))
+
+#define STACKTRACESTEP(stacktrace)	((stacktrace).frame=(stacktrace).frame->prev)
+#define STACKTRACEPC(stacktrace)	((stacktrace).frame->pc)
+#define STACKTRACEMETH(stacktrace)	((stacktrace).frame->meth)
+#define STACKTRACEEND(stacktrace)	((stacktrace).frame == 0)
+
+
+
+
+#elif defined(TRANSLATOR)
+typedef struct _stackTrace {
+	struct _exceptionFrame	nframe;
+	struct _exceptionFrame* frame;
+} stackTrace;
+
+#define STACKTRACEINIT(stacktrace,init,o)	\
+	{\
+		if ((init)==NULL) { \
+			FIRSTFRAME((stacktrace).nframe,o); \
+			(stacktrace).frame=&((stacktrace).nframe);\
+		} else {\
+			(stacktrace).frame=init;\
+		}\
+	}
+
+#define	STACKTRACESTEP(stacktrace)	(NEXTFRAME((stacktrace).frame))
+#define STACKTRACEPC(stacktrace)	(PCFRAME((stacktrace).frame))
+#define	STACKTRACEMETH(stacktrace)	(findMethodFromPC(PCFRAME((stacktrace).frame)))
+#define	STACKTRACEEND(stacktrace)	(!FRAMEOKAY((stacktrace).frame))
+
+#endif /* defined(TRANSLATOR) */
+
+Hjava_lang_Object* buildStackTrace(struct _exceptionFrame* base);
+Hjava_lang_Object* getClassContext(void* bulk);
+Hjava_lang_Class* getClassWithLoader(int* depth);
+jint classDepth(char* name);
+
+#endif /* __stacktrace_h */
Only in kaffe-0.8.3/kaffe/kaffevm: support.c.orig
diff -urP kaffe-0.8.3/packages/tjwassoc.co.uk/APIcore/lib/java.lang/SecurityManager.c kaffe-0.8.3.tegla/packages/tjwassoc.co.uk/APIcore/lib/java.lang/SecurityManager.c
--- kaffe-0.8.3/packages/tjwassoc.co.uk/APIcore/lib/java.lang/SecurityManager.c	Mon Mar  3 23:18:21 1997
+++ kaffe-0.8.3.tegla/packages/tjwassoc.co.uk/APIcore/lib/java.lang/SecurityManager.c	Fri Apr  4 11:30:53 1997
@@ -9,34 +9,54 @@
  * Written by Tim Wilkinson <tim at tjwassoc.demon.co.uk>, 1996.
  */
 
-#include <stdlib.h>
+#include "config.h"
+#include "config-std.h"
+#include "config-mem.h"
+#include "../../../../kaffe/kaffevm/gtypes.h"
+#include "../../../../kaffe/kaffevm/access.h"
+#include "../../../../kaffe/kaffevm/constants.h"
+#include "../../../../kaffe/kaffevm/object.h"
+#include "../../../../kaffe/kaffevm/classMethod.h"
+#include "../../../../kaffe/kaffevm/itypes.h"
 #include <native.h>
+#include "defs.h"
+#include <stdlib.h>
 #include "java.lang.stubs/SecurityManager.h"
 
 HArrayOfObject* /* HArrayOfClass */
 java_lang_SecurityManager_getClassContext(struct Hjava_lang_SecurityManager* this)
 {
-	abort();
-	return (0);
+	return ((HArrayOfObject*)getClassContext());
 }
 
 struct Hjava_lang_ClassLoader*
 java_lang_SecurityManager_currentClassLoader(struct Hjava_lang_SecurityManager* this)
 {
-	/* abort(); */
-	return (0);
+	int	depth;
+	struct Hjava_lang_Class* class=getClassWithLoader(&depth);
+
+	if (class!=NULL) {
+		return ((struct Hjava_lang_ClassLoader*)(class->loader));
+	} else {
+		return NULL;
+	}
 }
 
 jint
 java_lang_SecurityManager_classDepth(struct Hjava_lang_SecurityManager* this, struct Hjava_lang_String* str)
 {
-	abort();
-	return (0);
+	char buf[MAXNAMELEN];
+	
+	javaString2CString(str, buf, sizeof(buf));
+	classname2pathname(buf, buf);
+
+	return (classDepth(buf));
 }
 
 jint
 java_lang_SecurityManager_classLoaderDepth(struct Hjava_lang_SecurityManager* this)
 {
-	/* abort(); */
-	return (0);
+	int	depth;
+	struct Hjava_lang_Class* class=getClassWithLoader(&depth);
+	return	depth;
 }


More information about the kaffe mailing list