Changeset 551

Show
Ignore:
Timestamp:
09/01/08 21:07:14 (4 months ago)
Author:
chris
Message:

Attach a w_package pointer to every w_Clazz, and use this to detect
same-runtime-packageness when doing access checks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Mika/trunk/core-vm/include/clazz.h

    r538 r551  
    3434#define _CLAZZ_H 
    3535 
     36#include "package.h" 
    3637#include "wonka.h" 
    3738#include "hashtable.h" 
     
    7273  w_thread   resolution_thread;  /* thread which is busy with this class    */ 
    7374                                 /* (unstable class states only)            */ 
     75  w_package  package;    /* runtime package of which this class is a member */ 
    7476 
    7577/* Information available in states CLAZZ_STATE_SUPERS_LOADED and higher */ 
     
    338340extern w_hashtable system_unloaded_class_hashtable; 
    339341 
     342extern w_hashtable system_package_hashtable; 
     343 
    340344/* 
    341345** Get a copy of a reference field of a class 
  • Mika/trunk/core-vm/include/loading.h

    r372 r551  
    124124** Given an instance 'l' of java.lang.ClassLoader, return a pointer to the  
    125125** w_Hashtable which holds a pointer to a w_UnloadedClazz structure for each 
    126 ** class which has been marked as loadable by theis loader but has not yet 
     126** class which has been marked as loadable by this loader but has not yet 
    127127** been loaded. 
    128128** If 'l' is null, system_unloaded_class_hashtable is returned. 
    129129*/ 
    130130#define loader2unloaded_classes(l) ((l) ? getWotsitField((l), F_ClassLoader_unloaded_classes) :system_unloaded_class_hashtable) 
     131 
     132/* 
     133** Given an instance 'l' of java.lang.ClassLoader, return a pointer to the  
     134** w_Hashtable which holds a pointer to a w_Package structure for each 
     135** package for which at least one class which has been loaded by this loader. 
     136** If 'l' is null, system_package_hashtable is returned. 
     137*/ 
     138#define loader2packages(l) ((l) ? getWotsitField((l), F_ClassLoader_packages) :system_package_hashtable) 
    131139 
    132140/* 
  • Mika/trunk/core-vm/include/package.h

    r543 r551  
    3838  w_string name;         /* Package name, e.g. java.lang                */ 
    3939  char   *label;         /* null-terminated C string beginning "pack"   */ 
    40   w_flags flags;         /* reserved                                    */ 
     40  w_flags flags;         /* not yet used                                */ 
    4141  w_instance loader;     /* the ClassLoader that defined this package   */ 
    4242} w_Package; 
     
    5454void destroyPackage(w_package); 
    5555 
     56/** 
     57 ** Get the runtime package corresponding to the named class, creating it 
     58 ** if it does not already exist. 
     59 */ 
     60w_package getPackageForClazz(w_clazz clazz, w_instance loader); 
     61 
     62/* 
     63** Function called as soon as the system class loader has been created, 
     64** to back-patch all existing packages to have loader = systemClassLoader. 
     65*/ 
     66void patchPackages(); 
     67 
    5668#endif /* _PACKAGE_H */ 
    5769 
  • Mika/trunk/core-vm/src/classfile/clazz.c

    r538 r551  
    7474/* 
    7575** Each classloader has associated with it a hashtable of loaded classes 
    76 ** and one of unloaded classes. For the "primordial" class loader these are 
    77 ** static items. 
     76** and one of unloaded classes, plus a hashtable of packages it has loaded. 
     77** For the "primordial" class loader these are static items. 
    7878*/ 
    7979 
    8080w_hashtable system_loaded_class_hashtable; 
    8181w_hashtable system_unloaded_class_hashtable; 
     82w_hashtable system_package_hashtable; 
    8283 
    8384/* 
     
    17781779 
    17791780  set_classname(clazz); 
     1781  getPackageForClazz(clazz, loader); 
    17801782 
    17811783  clazz->resolution_thread = NULL; 
     
    19831985      woempa(7, "Releasing temp.inner_class_info\n"); 
    19841986      releaseMem(clazz->temp.inner_class_info); 
     1987    } 
     1988 
     1989    if (clazz->resolution_monitor) { 
     1990      woempa(7, "Releasing resolution_monitor\n"); 
     1991      x_monitor_delete(clazz->resolution_monitor); 
     1992      releaseMem(clazz->resolution_monitor); 
    19851993    } 
    19861994  } 
  • Mika/trunk/core-vm/src/heap/collector.c

    r495 r551  
    22* Parts copyright (c) 2001, 2002, 2003 by Punch Telematix. All rights     * 
    33* reserved.                                                               * 
    4 * Parts copyright (c) 2004, 2005, 2006, 2007 by Chris Gray,               * 
     4* Parts copyright (c) 2004, 2005, 2006, 2007, 2008 by Chris Gray,         * 
    55* /k/ Embedded Java Solutions.  All rights reserved.                      * 
    66*                                                                         * 
     
    337337 
    338338/* 
    339 ** Release the memory held by a w_UnloadedClazz in a class loader's unload 
    340 ** class hashtable. We shouldn't really need to do this - by the time the 
    341 ** class loader is collected the table should be empty. Strange ... 
     339** Release the memory held by a w_UnloadedClazz in a class loader's unloaded 
     340** class hashtable. 
    342341*/ 
    343342static void trashUnloadedClasses(w_word key, w_word value, void *dummy1, void*dummy2) { 
     
    346345  woempa(7, "trashing %K (%d references)\n", clazz, value); 
    347346  releaseMem(clazz); 
     347} 
     348 
     349/* 
     350** Release the memory held by a w_Package structs in a class loader's package 
     351** hashtable. 
     352*/ 
     353static void trashPackages(w_word key, w_word value, void *dummy1, void*dummy2) { 
     354  w_package package = (w_package)value; 
     355 
     356  woempa(7, "trashing %w in %j\n", package->name, package->loader); 
     357  wprintf("trashing %w in %j\n", package->name, package->loader); 
     358  destroyPackage(package); 
    348359} 
    349360 
     
    368379    if (class_hashtable->occupancy) { 
    369380      ht_iterate(class_hashtable, trashUnloadedClasses, NULL, NULL); 
     381    } 
     382    releaseMem(class_hashtable->label); 
     383    ht_destroy(class_hashtable); 
     384  } 
     385 
     386  class_hashtable = getWotsitField(theClassLoader, F_ClassLoader_packages); 
     387  if (class_hashtable) { 
     388    clearWotsitField(theClassLoader, F_ClassLoader_packages); 
     389    woempa(7, "Releasing hashtable %s for %j\n", class_hashtable->label, theClassLoader); 
     390    if (class_hashtable->occupancy) { 
     391      ht_iterate(class_hashtable, trashPackages, NULL, NULL); 
    370392    } 
    371393    releaseMem(class_hashtable->label); 
     
    15911613 *  - system thread group (and hence all threads) 
    15921614 *  - system class hashtable (static fields of system classes) 
    1593  *  - global references hashtable (includes current local references) 
     1615 *  - global references hashtable 
    15941616 * For every item we mark, we append all references it contains to 
    15951617 * the appropriate fifo (strong, weak, or phantom).  Therefore 
     
    16121634  w_int      marked = 0; 
    16131635 
    1614   //printf("Entering mark phase, ticks = %d\n", x_time_get() - gc_start_ticks); 
    1615   woempa(7, "(GC) Marking globals/locals hashtable.\n"); 
     1636  woempa(7, "(GC) Marking globals hashtable.\n"); 
    16161637  temp_fifo = ht_list_keys(globals_hashtable); 
    16171638  if (!temp_fifo) { 
  • Mika/trunk/core-vm/src/native/java/lang/ClassLoader.c

    r495 r551  
    5656w_instance applicationClassLoader; 
    5757 
     58#define PACKAGE_HASHTABLE_SIZE           19 
    5859#define CLASSLOADER_HASHTABLE_SIZE       89 
    5960 
     
    9697    setWotsitField(ClassLoader, F_ClassLoader_loaded_classes, system_loaded_class_hashtable); 
    9798    setWotsitField(ClassLoader, F_ClassLoader_unloaded_classes, system_unloaded_class_hashtable); 
    98     woempa(7, "Created %j by recycling %s and %s)\n", ClassLoader, system_loaded_class_hashtable->label, system_unloaded_class_hashtable->label); 
     99    setWotsitField(ClassLoader, F_ClassLoader_packages, system_package_hashtable); 
     100    woempa(7, "Created %j by recycling %s, %s and %s)\n", ClassLoader, system_loaded_class_hashtable->label, system_unloaded_class_hashtable->label, system_package_hashtable->label); 
    99101  } 
    100102  else { 
     
    122124    setWotsitField(ClassLoader, F_ClassLoader_unloaded_classes, class_hashtable); 
    123125    woempa(7, "%j: unloaded_classes in %s\n", ClassLoader, class_hashtable->label); 
     126    label_buf = allocMem((w_size)(string_length(clazz->dotified) + 38)); 
     127    if (!label_buf) { 
     128      wabort(ABORT_WONKA, "Unable to allocate label_buf\n"); 
     129    } 
     130    x_snprintf(label_buf, string_length(clazz->dotified) + 37, "hashtable:%j-packages", ClassLoader); 
     131    class_hashtable = ht_create(label_buf, PACKAGE_HASHTABLE_SIZE, NULL, NULL, 0, 0); 
     132    if (!class_hashtable) { 
     133      wabort(ABORT_WONKA, "Unable to allocate package hashtable\n"); 
     134    } 
     135    setWotsitField(ClassLoader, F_ClassLoader_packages, class_hashtable); 
     136    woempa(7, "%j: packages in %s\n", ClassLoader, class_hashtable->label); 
    124137  } 
    125138} 
  • Mika/trunk/core-vm/src/vm/checks.c

    r361 r551  
    11/************************************************************************** 
    2 * Copyright (c) 2001, 2002 by Punch Telematix. All rights reserved.       * 
     2* Parts copyright (c) 2001, 2002 by Punch Telematix. All rights reserved. * 
     3* Parts copyright (c) 2004, 2005, 2006, 2007, 2008 by Chris Gray, /k/     * 
     4* Embedded Java Solutions. All rights reserved.                           * 
    35*                                                                         * 
    46* Redistribution and use in source and binary forms, with or without      * 
     
    159161 
    160162w_boolean isAllowedToCall(w_clazz caller, w_method method, w_clazz objClazz) { 
    161  
    162163  if (method->spec.declaring_clazz == caller) { 
    163164    woempa(1,"This Class (%K) is same as calling Class, so any method can be called.\n",method->spec.declaring_clazz); 
     
    226227 */ 
    227228w_boolean sameRuntimePackage(w_clazz clazz1, w_clazz clazz2) { 
     229  return clazz1->package == clazz2->package; 
     230/* 
    228231  w_string name1; 
    229232  w_string name2; 
     
    284287 
    285288  return WONKA_TRUE; 
    286 
    287    
     289*/ 
     290
     291   
  • Mika/trunk/core-vm/src/vm/core-classes.in

    r540 r551  
    321321        loaded_classes          + 
    322322        unloaded_classes        + 
     323        packages                + 
    323324        create                  ()V                                             ClassLoader_create 
    324325        _defineClass            (Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;       ClassLoader_defineClass 
  • Mika/trunk/core-vm/src/vm/loading.c

    r538 r551  
    311311  w_clazz result = NULL; 
    312312 
    313   result = (w_clazz)ht_read_no_lock(loader2loaded_classes(initiating_loader), (w_word)name); 
     313  result = (w_clazz)ht_read(loader2loaded_classes(initiating_loader), (w_word)name); 
     314  woempa(7, "searched loaded classes of %j for %w, found %p\n", initiating_loader, name, result); 
    314315 
    315316  if (!result) { 
     
    325326 
    326327    result = registerUnloadedClazz(result); 
     328    woempa(1, "returning unloaded class %p\n", result); 
    327329  } 
    328330 
     
    347349  x_status  status; 
    348350 
     351  threadMustBeSafe(thread); 
     352 
    349353  if (state == CLAZZ_STATE_UNLOADED) { 
    350354    w_clazz loaded; 
     
    377381  } 
    378382  else if (state < CLAZZ_STATE_LOADED) { 
    379     threadMustBeSafe(thread); 
    380  
    381383    monitor = current->resolution_monitor; 
    382384 
     
    401403} 
    402404 
    403 /* 
    404 static void printBootstrapClass(w_word key, w_word value) { 
     405static void loaded_class_iterator(w_word key, w_word value) { 
    405406  w_string name = (w_string) key; 
    406   wprintf("  %w\n", name); 
    407 
    408 */ 
    409  
     407  w_clazz clazz = (w_clazz)value; 
     408  if (!clazz->loader) { 
     409    clazz->loader = systemClassLoader; 
     410  } 
     411
     412 
     413/* 
     414** Fix system_loaded_class_hashtable so that every class which currently 
     415** has its loader set to null points to systemClassLoader instead. 
     416*/ 
     417static void patchLoadedClasses() { 
     418  ht_every(system_loaded_class_hashtable, loaded_class_iterator); 
     419
     420   
    410421/* 
    411422** Set the systemClassLoader global variable. From now on the System 
     
    413424*/ 
    414425void setSystemClassLoader(w_instance scl) { 
    415 //  w_int n; 
    416426  x_monitor_eternal(&system_loaded_class_hashtable->monitor); 
    417 /* 
    418   wprintf("Bootstrap classes:\n"); 
    419   n = ht_every(system_loaded_class_hashtable, printBootstrapClass); 
    420   wprintf("Total of %d bootstrap classes\n\n", n); 
    421 */ 
    422427  if (systemClassLoader) { 
    423428    woempa(9, "Ahoy there! Someone tried to install SystemClassLoader twice ...\n"); 
     
    427432    systemClassLoader = scl; 
    428433    newGlobalReference(scl); 
    429     //releaseZipFile(bootzipfile); 
     434    patchLoadedClasses(); 
     435    patchPackages(); 
    430436  } 
    431437  x_monitor_exit(&system_loaded_class_hashtable->monitor); 
     
    721727  system_unloaded_class_hashtable = ht_create((char*)"hashtable:system-unloaded-classes", 97, clazz_hashcode, clazz_comparator, 0, 0); 
    722728  woempa(7,"created system_unloaded_class_hashtable at %p\n",system_unloaded_class_hashtable); 
     729 
     730  system_package_hashtable = ht_create((char*)"hashtable:system-packages", 17, NULL, NULL, 0, 0); 
     731  woempa(7,"created system_package_hashtable at %p\n",system_package_hashtable); 
    723732 
    724733  collectCoreFixups(); 
     
    10831092  array_clazz->loader = base_clazz->loader; 
    10841093  array_clazz->bits = 32; 
     1094  array_clazz->package = base_clazz->package; 
    10851095 
    10861096  name_buffer[0] = '['; 
  • Mika/trunk/core-vm/src/vm/package.c

    r543 r551  
    2929**************************************************************************/ 
    3030 
     31#include "clazz.h" 
     32#include "loading.h" 
    3133#include "package.h" 
     34#include "wstrings.h" 
    3235 
    3336w_package createPackage(w_string name, w_instance loader) { 
     
    4245void destroyPackage(w_package p) { 
    4346  deregisterString(p->name); 
    44   deallocMem(p); 
     47  releaseMem(p); 
    4548} 
    4649 
     50/** 
     51 ** Get the w_Package for a class, creating it if it does not already exist. 
     52 */ 
     53w_package getPackageForClazz(w_clazz clazz, w_instance loader) { 
     54  w_string class_name = clazz->dotified; 
     55  w_instance effective_loader = loader ? loader : systemClassLoader; 
     56  w_hashtable ht = loader2packages(effective_loader); 
     57  w_string package_name; 
     58  w_int i; 
     59  w_int j; 
     60  w_package p; 
     61 
     62  for (i = 0; string_char(class_name, i) == '['; ++i);  
     63  for (j = string_length(class_name) - 1; string_char(class_name, j) != '.'; --j);  
     64  package_name = j > i ? w_substring(class_name, i, j - i) : registerString(string_empty); 
     65  ht_lock(ht); 
     66  p = (w_package)ht_read_no_lock(ht, (w_word)package_name); 
     67  if (!p) { 
     68    p = createPackage(package_name, effective_loader); 
     69    ht_write_no_lock(ht, (w_word)package_name, (w_word)p); 
     70  } 
     71  clazz->package = p; 
     72  ht_unlock(ht); 
     73  deregisterString(package_name); 
     74 
     75  return p; 
     76} 
     77 
     78static void package_iterator(w_word key, w_word value) { 
     79  w_string name = (w_string) key; 
     80  w_package package = (w_package)value; 
     81  if (!package->loader) { 
     82    package->loader = systemClassLoader; 
     83  } 
     84} 
     85 
     86/* 
     87** Fix system_package_hashtable so that every package which currently 
     88** has its loader set to null points to systemClassLoader instead. 
     89*/ 
     90void patchPackages() { 
     91  ht_every(system_package_hashtable, package_iterator); 
     92} 
     93