src/share/vm/memory/metaspaceShared.cpp

changeset 7089
6e0cb14ce59b
parent 6680
78bbf4d43a14
child 7103
622c6e0ad4d6
     1.1 --- a/src/share/vm/memory/metaspaceShared.cpp	Fri Aug 22 12:03:49 2014 -0700
     1.2 +++ b/src/share/vm/memory/metaspaceShared.cpp	Thu Aug 21 13:57:51 2014 -0700
     1.3 @@ -26,6 +26,7 @@
     1.4  #include "classfile/dictionary.hpp"
     1.5  #include "classfile/loaderConstraints.hpp"
     1.6  #include "classfile/placeholders.hpp"
     1.7 +#include "classfile/sharedClassUtil.hpp"
     1.8  #include "classfile/symbolTable.hpp"
     1.9  #include "classfile/systemDictionary.hpp"
    1.10  #include "code/codeCache.hpp"
    1.11 @@ -46,6 +47,10 @@
    1.12  
    1.13  ReservedSpace* MetaspaceShared::_shared_rs = NULL;
    1.14  
    1.15 +bool MetaspaceShared::_link_classes_made_progress;
    1.16 +bool MetaspaceShared::_check_classes_made_progress;
    1.17 +bool MetaspaceShared::_has_error_classes;
    1.18 +bool MetaspaceShared::_archive_loading_failed = false;
    1.19  // Read/write a data stream for restoring/preserving metadata pointers and
    1.20  // miscellaneous data from/to the shared archive file.
    1.21  
    1.22 @@ -445,6 +450,23 @@
    1.23    SystemDictionary::classes_do(collect_classes);
    1.24  
    1.25    tty->print_cr("Number of classes %d", _global_klass_objects->length());
    1.26 +  {
    1.27 +    int num_type_array = 0, num_obj_array = 0, num_inst = 0;
    1.28 +    for (int i = 0; i < _global_klass_objects->length(); i++) {
    1.29 +      Klass* k = _global_klass_objects->at(i);
    1.30 +      if (k->oop_is_instance()) {
    1.31 +        num_inst ++;
    1.32 +      } else if (k->oop_is_objArray()) {
    1.33 +        num_obj_array ++;
    1.34 +      } else {
    1.35 +        assert(k->oop_is_typeArray(), "sanity");
    1.36 +        num_type_array ++;
    1.37 +      }
    1.38 +    }
    1.39 +    tty->print_cr("    instance classes   = %5d", num_inst);
    1.40 +    tty->print_cr("    obj array classes  = %5d", num_obj_array);
    1.41 +    tty->print_cr("    type array classes = %5d", num_type_array);
    1.42 +  }
    1.43  
    1.44    // Update all the fingerprints in the shared methods.
    1.45    tty->print("Calculating fingerprints ... ");
    1.46 @@ -610,38 +632,58 @@
    1.47  #undef fmt_space
    1.48  }
    1.49  
    1.50 -static void link_shared_classes(Klass* obj, TRAPS) {
    1.51 +
    1.52 +void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) {
    1.53    Klass* k = obj;
    1.54    if (k->oop_is_instance()) {
    1.55      InstanceKlass* ik = (InstanceKlass*) k;
    1.56      // Link the class to cause the bytecodes to be rewritten and the
    1.57 -    // cpcache to be created.
    1.58 -    if (ik->init_state() < InstanceKlass::linked) {
    1.59 -      ik->link_class(THREAD);
    1.60 -      guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting");
    1.61 +    // cpcache to be created. Class verification is done according
    1.62 +    // to -Xverify setting.
    1.63 +    _link_classes_made_progress |= try_link_class(ik, THREAD);
    1.64 +    guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
    1.65 +  }
    1.66 +}
    1.67 +
    1.68 +void MetaspaceShared::check_one_shared_class(Klass* k) {
    1.69 +  if (k->oop_is_instance() && InstanceKlass::cast(k)->check_sharing_error_state()) {
    1.70 +    _check_classes_made_progress = true;
    1.71 +  }
    1.72 +}
    1.73 +
    1.74 +void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
    1.75 +  // We need to iterate because verification may cause additional classes
    1.76 +  // to be loaded.
    1.77 +  do {
    1.78 +    _link_classes_made_progress = false;
    1.79 +    SystemDictionary::classes_do(link_one_shared_class, THREAD);
    1.80 +    guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
    1.81 +  } while (_link_classes_made_progress);
    1.82 +
    1.83 +  if (_has_error_classes) {
    1.84 +    // Mark all classes whose super class or interfaces failed verification.
    1.85 +    do {
    1.86 +      // Not completely sure if we need to do this iteratively. Anyway,
    1.87 +      // we should come here only if there are unverifiable classes, which
    1.88 +      // shouldn't happen in normal cases. So better safe than sorry.
    1.89 +      _check_classes_made_progress = false;
    1.90 +      SystemDictionary::classes_do(check_one_shared_class);
    1.91 +    } while (_check_classes_made_progress);
    1.92 +
    1.93 +    if (IgnoreUnverifiableClassesDuringDump) {
    1.94 +      // This is useful when running JCK or SQE tests. You should not
    1.95 +      // enable this when running real apps.
    1.96 +      SystemDictionary::remove_classes_in_error_state();
    1.97 +    } else {
    1.98 +      tty->print_cr("Please remove the unverifiable classes from your class list and try again");
    1.99 +      exit(1);
   1.100      }
   1.101    }
   1.102  }
   1.103  
   1.104 -
   1.105 -// Support for a simple checksum of the contents of the class list
   1.106 -// file to prevent trivial tampering. The algorithm matches that in
   1.107 -// the MakeClassList program used by the J2SE build process.
   1.108 -#define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe))
   1.109 -static jlong
   1.110 -jsum(jlong start, const char *buf, const int len)
   1.111 -{
   1.112 -    jlong h = start;
   1.113 -    char *p = (char *)buf, *e = p + len;
   1.114 -    while (p < e) {
   1.115 -        char c = *p++;
   1.116 -        if (c <= ' ') {
   1.117 -            /* Skip spaces and control characters */
   1.118 -            continue;
   1.119 -        }
   1.120 -        h = 31 * h + c;
   1.121 -    }
   1.122 -    return h;
   1.123 +void MetaspaceShared::prepare_for_dumping() {
   1.124 +  ClassLoader::initialize_shared_path();
   1.125 +  FileMapInfo::allocate_classpath_entry_table();
   1.126  }
   1.127  
   1.128  // Preload classes from a list, populate the shared spaces and dump to a
   1.129 @@ -650,72 +692,112 @@
   1.130    TraceTime timer("Dump Shared Spaces", TraceStartupTime);
   1.131    ResourceMark rm;
   1.132  
   1.133 +  tty->print_cr("Allocated shared space: %d bytes at " PTR_FORMAT,
   1.134 +                MetaspaceShared::shared_rs()->size(),
   1.135 +                MetaspaceShared::shared_rs()->base());
   1.136 +
   1.137    // Preload classes to be shared.
   1.138    // Should use some os:: method rather than fopen() here. aB.
   1.139 -  // Construct the path to the class list (in jre/lib)
   1.140 -  // Walk up two directories from the location of the VM and
   1.141 -  // optionally tack on "lib" (depending on platform)
   1.142 -  char class_list_path[JVM_MAXPATHLEN];
   1.143 -  os::jvm_path(class_list_path, sizeof(class_list_path));
   1.144 -  for (int i = 0; i < 3; i++) {
   1.145 -    char *end = strrchr(class_list_path, *os::file_separator());
   1.146 -    if (end != NULL) *end = '\0';
   1.147 +  const char* class_list_path;
   1.148 +  if (SharedClassListFile == NULL) {
   1.149 +    // Construct the path to the class list (in jre/lib)
   1.150 +    // Walk up two directories from the location of the VM and
   1.151 +    // optionally tack on "lib" (depending on platform)
   1.152 +    char class_list_path_str[JVM_MAXPATHLEN];
   1.153 +    os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
   1.154 +    for (int i = 0; i < 3; i++) {
   1.155 +      char *end = strrchr(class_list_path_str, *os::file_separator());
   1.156 +      if (end != NULL) *end = '\0';
   1.157 +    }
   1.158 +    int class_list_path_len = (int)strlen(class_list_path_str);
   1.159 +    if (class_list_path_len >= 3) {
   1.160 +      if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
   1.161 +        strcat(class_list_path_str, os::file_separator());
   1.162 +        strcat(class_list_path_str, "lib");
   1.163 +      }
   1.164 +    }
   1.165 +    strcat(class_list_path_str, os::file_separator());
   1.166 +    strcat(class_list_path_str, "classlist");
   1.167 +    class_list_path = class_list_path_str;
   1.168 +  } else {
   1.169 +    class_list_path = SharedClassListFile;
   1.170    }
   1.171 -  int class_list_path_len = (int)strlen(class_list_path);
   1.172 -  if (class_list_path_len >= 3) {
   1.173 -    if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) {
   1.174 -      strcat(class_list_path, os::file_separator());
   1.175 -      strcat(class_list_path, "lib");
   1.176 -    }
   1.177 +
   1.178 +  int class_count = 0;
   1.179 +  GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
   1.180 +
   1.181 +  // sun.io.Converters
   1.182 +  static const char obj_array_sig[] = "[[Ljava/lang/Object;";
   1.183 +  SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
   1.184 +
   1.185 +  // java.util.HashMap
   1.186 +  static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
   1.187 +  SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
   1.188 +
   1.189 +  tty->print_cr("Loading classes to share ...");
   1.190 +  _has_error_classes = false;
   1.191 +  class_count += preload_and_dump(class_list_path, class_promote_order,
   1.192 +                                  THREAD);
   1.193 +  if (ExtraSharedClassListFile) {
   1.194 +    class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order,
   1.195 +                                    THREAD);
   1.196    }
   1.197 -  strcat(class_list_path, os::file_separator());
   1.198 -  strcat(class_list_path, "classlist");
   1.199 +  tty->print_cr("Loading classes to share: done.");
   1.200  
   1.201 +  if (PrintSharedSpaces) {
   1.202 +    tty->print_cr("Shared spaces: preloaded %d classes", class_count);
   1.203 +  }
   1.204 +
   1.205 +  // Rewrite and link classes
   1.206 +  tty->print_cr("Rewriting and linking classes ...");
   1.207 +
   1.208 +  // Link any classes which got missed. This would happen if we have loaded classes that
   1.209 +  // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
   1.210 +  // fails verification, all other interfaces that were not specified in the classlist but
   1.211 +  // are implemented by K are not verified.
   1.212 +  link_and_cleanup_shared_classes(CATCH);
   1.213 +  tty->print_cr("Rewriting and linking classes: done");
   1.214 +
   1.215 +  // Create and dump the shared spaces.   Everything so far is loaded
   1.216 +  // with the null class loader.
   1.217 +  ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   1.218 +  VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
   1.219 +  VMThread::execute(&op);
   1.220 +
   1.221 +  // Since various initialization steps have been undone by this process,
   1.222 +  // it is not reasonable to continue running a java process.
   1.223 +  exit(0);
   1.224 +}
   1.225 +
   1.226 +int MetaspaceShared::preload_and_dump(const char * class_list_path,
   1.227 +                                      GrowableArray<Klass*>* class_promote_order,
   1.228 +                                      TRAPS) {
   1.229    FILE* file = fopen(class_list_path, "r");
   1.230 +  char class_name[256];
   1.231 +  int class_count = 0;
   1.232 +
   1.233    if (file != NULL) {
   1.234 -    jlong computed_jsum  = JSUM_SEED;
   1.235 -    jlong file_jsum      = 0;
   1.236 -
   1.237 -    char class_name[256];
   1.238 -    int class_count = 0;
   1.239 -    GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
   1.240 -
   1.241 -    // sun.io.Converters
   1.242 -    static const char obj_array_sig[] = "[[Ljava/lang/Object;";
   1.243 -    SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
   1.244 -
   1.245 -    // java.util.HashMap
   1.246 -    static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
   1.247 -    SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
   1.248 -
   1.249 -    tty->print("Loading classes to share ... ");
   1.250      while ((fgets(class_name, sizeof class_name, file)) != NULL) {
   1.251 -      if (*class_name == '#') {
   1.252 -        jint fsh, fsl;
   1.253 -        if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
   1.254 -          file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
   1.255 -        }
   1.256 -
   1.257 +      if (*class_name == '#') { // comment
   1.258          continue;
   1.259        }
   1.260        // Remove trailing newline
   1.261        size_t name_len = strlen(class_name);
   1.262 -      class_name[name_len-1] = '\0';
   1.263 -
   1.264 -      computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1);
   1.265 +      if (class_name[name_len-1] == '\n') {
   1.266 +        class_name[name_len-1] = '\0';
   1.267 +      }
   1.268  
   1.269        // Got a class name - load it.
   1.270        TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
   1.271        guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
   1.272        Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
   1.273                                                           THREAD);
   1.274 -      guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class.");
   1.275 +      CLEAR_PENDING_EXCEPTION;
   1.276        if (klass != NULL) {
   1.277          if (PrintSharedSpaces && Verbose && WizardMode) {
   1.278            tty->print_cr("Shared spaces preloaded: %s", class_name);
   1.279          }
   1.280  
   1.281 -
   1.282          InstanceKlass* ik = InstanceKlass::cast(klass);
   1.283  
   1.284          // Should be class load order as per -XX:+TraceClassLoadingPreorder
   1.285 @@ -725,52 +807,14 @@
   1.286          // cpcache to be created. The linking is done as soon as classes
   1.287          // are loaded in order that the related data structures (klass and
   1.288          // cpCache) are located together.
   1.289 -
   1.290 -        if (ik->init_state() < InstanceKlass::linked) {
   1.291 -          ik->link_class(THREAD);
   1.292 -          guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
   1.293 -        }
   1.294 -
   1.295 -        // TODO: Resolve klasses in constant pool
   1.296 -        ik->constants()->resolve_class_constants(THREAD);
   1.297 +        try_link_class(ik, THREAD);
   1.298 +        guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
   1.299  
   1.300          class_count++;
   1.301        } else {
   1.302 -        if (PrintSharedSpaces && Verbose && WizardMode) {
   1.303 -          tty->cr();
   1.304 -          tty->print_cr(" Preload failed: %s", class_name);
   1.305 -        }
   1.306 +        //tty->print_cr("Preload failed: %s", class_name);
   1.307        }
   1.308 -      file_jsum = 0; // Checksum must be on last line of file
   1.309      }
   1.310 -    if (computed_jsum != file_jsum) {
   1.311 -      tty->cr();
   1.312 -      tty->print_cr("Preload failed: checksum of class list was incorrect.");
   1.313 -      exit(1);
   1.314 -    }
   1.315 -
   1.316 -    tty->print_cr("done. ");
   1.317 -
   1.318 -    if (PrintSharedSpaces) {
   1.319 -      tty->print_cr("Shared spaces: preloaded %d classes", class_count);
   1.320 -    }
   1.321 -
   1.322 -    // Rewrite and unlink classes.
   1.323 -    tty->print("Rewriting and linking classes ... ");
   1.324 -
   1.325 -    // Link any classes which got missed.  (It's not quite clear why
   1.326 -    // they got missed.)  This iteration would be unsafe if we weren't
   1.327 -    // single-threaded at this point; however we can't do it on the VM
   1.328 -    // thread because it requires object allocation.
   1.329 -    SystemDictionary::classes_do(link_shared_classes, CATCH);
   1.330 -    tty->print_cr("done. ");
   1.331 -
   1.332 -    // Create and dump the shared spaces.   Everything so far is loaded
   1.333 -    // with the null class loader.
   1.334 -    ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   1.335 -    VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
   1.336 -    VMThread::execute(&op);
   1.337 -
   1.338    } else {
   1.339      char errmsg[JVM_MAXPATHLEN];
   1.340      os::lasterror(errmsg, JVM_MAXPATHLEN);
   1.341 @@ -778,11 +822,39 @@
   1.342      exit(1);
   1.343    }
   1.344  
   1.345 -  // Since various initialization steps have been undone by this process,
   1.346 -  // it is not reasonable to continue running a java process.
   1.347 -  exit(0);
   1.348 +  return class_count;
   1.349  }
   1.350  
   1.351 +// Returns true if the class's status has changed
   1.352 +bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
   1.353 +  assert(DumpSharedSpaces, "should only be called during dumping");
   1.354 +  if (ik->init_state() < InstanceKlass::linked) {
   1.355 +    bool saved = BytecodeVerificationLocal;
   1.356 +    if (!SharedClassUtil::is_shared_boot_class(ik)) {
   1.357 +      // The verification decision is based on BytecodeVerificationRemote
   1.358 +      // for non-system classes. Since we are using the NULL classloader
   1.359 +      // to load non-system classes during dumping, we need to temporarily
   1.360 +      // change BytecodeVerificationLocal to be the same as
   1.361 +      // BytecodeVerificationRemote. Note this can cause the parent system
   1.362 +      // classes also being verified. The extra overhead is acceptable during
   1.363 +      // dumping.
   1.364 +      BytecodeVerificationLocal = BytecodeVerificationRemote;
   1.365 +    }
   1.366 +    ik->link_class(THREAD);
   1.367 +    if (HAS_PENDING_EXCEPTION) {
   1.368 +      ResourceMark rm;
   1.369 +      tty->print_cr("Preload Error: Verification failed for %s",
   1.370 +                    ik->external_name());
   1.371 +      CLEAR_PENDING_EXCEPTION;
   1.372 +      ik->set_in_error_state();
   1.373 +      _has_error_classes = true;
   1.374 +    }
   1.375 +    BytecodeVerificationLocal = saved;
   1.376 +    return true;
   1.377 +  } else {
   1.378 +    return false;
   1.379 +  }
   1.380 +}
   1.381  
   1.382  // Closure for serializing initialization data in from a data area
   1.383  // (ptr_array) read from the shared file.
   1.384 @@ -866,7 +938,8 @@
   1.385        (_rw_base = mapinfo->map_region(rw)) != NULL &&
   1.386        (_md_base = mapinfo->map_region(md)) != NULL &&
   1.387        (_mc_base = mapinfo->map_region(mc)) != NULL &&
   1.388 -      (image_alignment == (size_t)max_alignment())) {
   1.389 +      (image_alignment == (size_t)max_alignment()) &&
   1.390 +      mapinfo->validate_classpath_entry_table()) {
   1.391      // Success (no need to do anything)
   1.392      return true;
   1.393    } else {
   1.394 @@ -883,7 +956,7 @@
   1.395      // If -Xshare:on is specified, print out the error message and exit VM,
   1.396      // otherwise, set UseSharedSpaces to false and continue.
   1.397      if (RequireSharedSpaces) {
   1.398 -      vm_exit_during_initialization("Unable to use shared archive.", NULL);
   1.399 +      vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
   1.400      } else {
   1.401        FLAG_SET_DEFAULT(UseSharedSpaces, false);
   1.402      }
   1.403 @@ -983,6 +1056,20 @@
   1.404  
   1.405    // Close the mapinfo file
   1.406    mapinfo->close();
   1.407 +
   1.408 +  if (PrintSharedArchiveAndExit) {
   1.409 +    if (PrintSharedDictionary) {
   1.410 +      tty->print_cr("\nShared classes:\n");
   1.411 +      SystemDictionary::print_shared(false);
   1.412 +    }
   1.413 +    if (_archive_loading_failed) {
   1.414 +      tty->print_cr("archive is invalid");
   1.415 +      vm_exit(1);
   1.416 +    } else {
   1.417 +      tty->print_cr("archive is valid");
   1.418 +      vm_exit(0);
   1.419 +    }
   1.420 +  }
   1.421  }
   1.422  
   1.423  // JVM/TI RedefineClasses() support:

mercurial