add ProtectionDomain/Codesource support
authormotse <none@none>
Thu, 27 Jan 2005 21:04:09 +0000 (21:04 +0000)
committermotse <none@none>
Thu, 27 Jan 2005 21:04:09 +0000 (21:04 +0000)
.project [new file with mode: 0644]
src/native/native.c
src/vm/loader.c
src/vm/loader.h

diff --git a/.project b/.project
new file mode 100644 (file)
index 0000000..1130b50
--- /dev/null
+++ b/.project
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>cacaodev</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+       </buildSpec>
+       <natures>
+       </natures>
+</projectDescription>
index 84d967d76d2093f847840779b80dfb5d9d306fc8..620245135852cad2480b96cf8ef5f528122be0be 100644 (file)
@@ -31,7 +31,7 @@
    The .hh files created with the header file generator are all
    included here as are the C functions implementing these methods.
 
-   $Id: native.c 1875 2005-01-21 09:37:33Z twisti $
+   $Id: native.c 1888 2005-01-27 21:04:09Z motse $
 
 */
 
@@ -56,6 +56,7 @@
 #include <sys/stat.h>
 
 #include "config.h"
+#include "cacao/cacao.h"
 #include "mm/memory.h"
 #include "native/jni.h"
 #include "native/native.h"
@@ -116,6 +117,196 @@ struct java_lang_ClassLoader *SystemClassLoader = NULL;
 java_objectheader* _exceptionptr = NULL;
 #endif
 
+
+/** Create a new ProtectionDomain object for a classpath_info structure. 
+       This object will be used for every java.lang.class object who represents a 
+       class which has been loaded from the location this classpath_info structure 
+       points to. **/
+static inline void create_ProtectionDomain(struct classpath_info *cpi) {
+       jmethodID mid;
+       jobject obj;
+
+       jobject cs;
+       jobject pc;
+       jobject url;
+
+       classinfo *c;  
+       classinfo *classpc;
+
+       /* create a new java.io.File object */ 
+       c = class_new(utf_new_char_classname("java/io/File"));
+       
+       if (!class_load(c) || !class_link(c)) {
+               log_text("unable to find java.io.File");
+               throw_main_exception_exit();
+       }
+
+       mid = class_resolvemethod(c, utf_new_char("<init>"), utf_new_char("(Ljava/lang/String;)V"));
+
+       if (mid == NULL) {
+               log_text("unable to find constructor in java.io.File");
+               cacao_exit(1);
+       }
+
+       obj = builtin_new (c);
+       asm_calljavafunction(mid,obj,javastring_new(utf_new_char(cpi->path)),NULL,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+       /* get URL of CodeSource from java.io.File object */ 
+       mid = class_resolvemethod(c, utf_new_char("toURL"), utf_new_char("()Ljava/net/URL;"));
+
+       if (mid == NULL) {
+               log_text("unable to find toURL in java.io.File");
+               cacao_exit(1);
+       }
+
+       url = asm_calljavafunction(mid,obj,url,NULL,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+       /* create a new java.security.CodeSource object */ 
+       c = class_new(utf_new_char_classname("java/security/CodeSource"));
+       
+       if (!class_load(c) || !class_link(c)) {
+               log_text("unable to find java.security.CodeSource");
+               throw_main_exception_exit();
+       }
+       
+       mid = class_resolvemethod(c, 
+                                                         utf_new_char("<init>"), 
+                                                         utf_new_char("(Ljava/net/URL;[Ljava/security/cert/Certificate;)V"));
+
+       if (mid == NULL) {
+               log_text("unable to find constructor in java.security.CodeSource");
+               cacao_exit(1);
+       }
+
+       cs = builtin_new (c);
+       asm_calljavafunction(mid,cs,url,NULL,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+       /* create a new java.security.PermissionCollection object */ 
+
+
+       classpc = class_new(utf_new_char_classname("java/security/PermissionCollection"));
+       
+       if (!class_load(c) || !class_link(c)) {
+               log_text("unable to find java.security.PermissionCollection");
+               throw_main_exception_exit();
+       }
+
+       mid = class_resolvemethod(classpc, utf_new_char("<init>"), 
+                                                         utf_new_char("()V"));
+
+       if (mid == NULL) {
+               log_text("unable to find constructor in java.security.PermissionCollection");
+               cacao_exit(1);
+       }
+
+       pc = builtin_new (classpc);
+       asm_calljavafunction(mid,pc,NULL,NULL,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+    /* add AllPermission - todo: this should not be the default behaviour */
+       c = class_new(utf_new_char_classname("java/security/AllPermission"));
+       
+       if (!class_load(c) || !class_link(c)) {
+               log_text("unable to find java.security.AllPermission");
+               throw_main_exception_exit();
+       }
+
+       mid = class_resolvemethod(c, 
+                                                         utf_new_char("<init>"), 
+                                                         utf_new_char("()V"));
+
+       if (mid == NULL) {
+               log_text("unable to find constructor in java.security.AllPermission");
+               cacao_exit(1);
+       }
+
+
+       obj = builtin_new (c);
+       asm_calljavafunction(mid,obj,NULL,NULL,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+
+       mid = class_resolvemethod(classpc, 
+                                                         utf_new_char("add"), 
+                                                         utf_new_char("(Ljava/security/Permission;)V"));
+
+       if (mid == NULL) {
+               log_text("unable to find add in java.security.PermissionCollection");
+               cacao_exit(1);
+       }
+
+       asm_calljavafunction(mid,pc,obj,NULL,NULL);
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+       /* create a new java.security.ProtectionDomain object */ 
+       c = class_new(utf_new_char_classname("java/security/ProtectionDomain"));
+       
+       if (!class_load(c) || !class_link(c)) {
+               log_text("unable to find java.security.ProtectionDomain");
+               throw_main_exception_exit();
+       }
+
+       mid = class_resolvemethod(c, 
+                                                         utf_new_char("<init>"), 
+                                                         utf_new_char("(Ljava/security/CodeSource;Ljava/security/PermissionCollection;)V"));
+
+       if (mid == NULL) {
+               log_text("unable to find constructor in java.security.ProtectionDomain");
+               cacao_exit(1);
+       }
+       
+       obj = builtin_new (c);
+       asm_calljavafunction(mid,obj,cs,pc,NULL);
+
+       if (*exceptionptr != NULL) {
+               utf_display((*exceptionptr)->vftbl->class->name);
+               printf ("\n");
+               fflush (stdout);        
+               cacao_exit(1);
+       }
+
+       cpi->pd = (struct java_security_ProtectionDomain*) obj;
+}
+
+
 /************* use classinfo structure as java.lang.Class object **************/
 
 void use_class_as_object(classinfo *c) 
@@ -134,7 +325,17 @@ void use_class_as_object(classinfo *c)
                if (class_java_lang_Class->vftbl ==0) panic ("vftbl == 0 in use_class_as_object");*/
                c->header.vftbl = class_java_lang_Class->vftbl;
                c->classvftbl = true;
-               /*printf("use_class_as_object: %s\n",c->name->text);*/
+
+
+               /* set a real, java object ProtectionDomain and CodeSource.
+                  Only one ProtectionDomain-object per classpath_info is needed.*/
+               if (c->pd != NULL) { 
+            /* only set ProtectionDomain for non primitive type classes*/
+                       if (((struct classpath_info*)c->pd)->pd == NULL) 
+                               create_ProtectionDomain((struct classpath_info *)c->pd);
+               
+                       c->pd = ((struct classpath_info*)c->pd)->pd;
+               }
        }
             
 }
index 8464c58505ecae9646280ac61d551a7efab5c593..525f94eab10882171b9494107b6d624d2ded2c24 100644 (file)
@@ -32,7 +32,7 @@
             Edwin Steiner
             Christian Thalinger
 
-   $Id: loader.c 1826 2004-12-29 12:47:18Z twisti $
+   $Id: loader.c 1888 2005-01-27 21:04:09Z motse $
 
 */
 
@@ -373,6 +373,8 @@ void suck_init(char *classpath)
                                        cpi->type = CLASSPATH_ARCHIVE;
                                        cpi->uf = uf;
                                        cpi->next = NULL;
+                                       cpi->pd = NULL; /* ProtectionDomain not set yet */
+                                       cpi->path = filename;
                                }
 #else
                                throw_cacao_exception_exit(string_java_lang_InternalError,
@@ -383,6 +385,7 @@ void suck_init(char *classpath)
                                cpi = NEW(classpath_info);
                                cpi->type = CLASSPATH_PATH;
                                cpi->next = NULL;
+                               cpi->pd = NULL; /* ProtectionDomain not set yet */
 
                                if (filename[filenamelen - 1] != '/') {/*PERHAPS THIS SHOULD BE READ FROM A GLOBAL CONFIGURATION */
                                        filename[filenamelen] = '/';
@@ -497,7 +500,10 @@ classbuffer *suck_start(classinfo *c)
                                                cb->size = file_info.uncompressed_size;
                                                cb->data = MNEW(u1, cb->size);
                                                cb->pos = cb->data - 1;
-
+                                               /* we need this later in use_class_as_object to set a correct 
+                            ProtectionDomain and CodeSource */
+                                               c->pd = cpi; 
+                                               
                                                len = unzReadCurrentFile(cpi->uf, cb->data, cb->size);
 
                                                if (len != cb->size) {
@@ -534,6 +540,9 @@ classbuffer *suck_start(classinfo *c)
                                        cb->size = buffer.st_size;
                                        cb->data = MNEW(u1, cb->size);
                                        cb->pos = cb->data - 1;
+                                       /* we need this later in use_class_as_object to set a correct 
+                       ProtectionDomain and CodeSource */
+                                       c->pd = cpi; 
 
                                        /* read class data */
                                        len = fread(cb->data, 1, cb->size, classfile);
index 73905af82d189466e9e67332c163b9c243468fa5..35052f227165156f3c4baccceb41e832e7b22be6 100644 (file)
@@ -26,7 +26,7 @@
 
    Authors: Reinhard Grafl
 
-   $Id: loader.h 1826 2004-12-29 12:47:18Z twisti $
+   $Id: loader.h 1888 2005-01-27 21:04:09Z motse $
 */
 
 
@@ -62,6 +62,7 @@ struct classpath_info {
        s4              type;
        char           *path;
        s4              pathlen;
+       struct java_security_ProtectionDomain* pd;
 #if defined(USE_ZLIB)
        unzFile         uf;
 #endif