1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADER_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSLOADER_HPP
  27 
  28 #include "classfile/classFileParser.hpp"
  29 #include "runtime/perfData.hpp"
  30 #include "utilities/macros.hpp"
  31 
  32 // The VM class loader.
  33 #include <sys/stat.h>
  34 
  35 
  36 // Class path entry (directory or zip file)
  37 
  38 class ClassPathEntry: public CHeapObj<mtClass> {
  39  private:
  40   ClassPathEntry* _next;
  41  public:
  42   // Next entry in class path
  43   ClassPathEntry* next()              { return _next; }
  44   void set_next(ClassPathEntry* next) {
  45     // may have unlocked readers, so write atomically.
  46     OrderAccess::release_store_ptr(&_next, next);
  47   }
  48   virtual bool is_jar_file() = 0;
  49   virtual const char* name() = 0;
  50   virtual bool is_lazy();
  51   // Constructor
  52   ClassPathEntry();
  53   // Attempt to locate file_name through this class path entry.
  54   // Returns a class file parsing stream if successfull.
  55   virtual ClassFileStream* open_stream(const char* name, TRAPS) = 0;
  56   // Debugging
  57   NOT_PRODUCT(virtual void compile_the_world(Handle loader, TRAPS) = 0;)
  58   NOT_PRODUCT(virtual bool is_jrt() = 0;)
  59 };
  60 
  61 
  62 class ClassPathDirEntry: public ClassPathEntry {
  63  private:
  64   const char* _dir;           // Name of directory
  65  public:
  66   bool is_jar_file()  { return false;  }
  67   const char* name()  { return _dir; }
  68   ClassPathDirEntry(const char* dir);
  69   ClassFileStream* open_stream(const char* name, TRAPS);
  70   // Debugging
  71   NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
  72   NOT_PRODUCT(bool is_jrt();)
  73 };
  74 
  75 
  76 // Type definitions for zip file and zip file entry
  77 typedef void* jzfile;
  78 typedef struct {
  79   char *name;                   /* entry name */
  80   jlong time;                   /* modification time */
  81   jlong size;                   /* size of uncompressed data */
  82   jlong csize;                  /* size of compressed data (zero if uncompressed) */
  83   jint crc;                     /* crc of uncompressed data */
  84   char *comment;                /* optional zip file comment */
  85   jbyte *extra;                 /* optional extra data */
  86   jlong pos;                    /* position of LOC header (if negative) or data */
  87 } jzentry;
  88 
  89 
  90 class ClassPathZipEntry: public ClassPathEntry {
  91  private:
  92   jzfile* _zip;              // The zip archive
  93   const char*   _zip_name;   // Name of zip archive
  94  public:
  95   bool is_jar_file()  { return true;  }
  96   const char* name()  { return _zip_name; }
  97   ClassPathZipEntry(jzfile* zip, const char* zip_name);
  98   ~ClassPathZipEntry();
  99   u1* open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS);
 100   ClassFileStream* open_stream(const char* name, TRAPS);
 101   void contents_do(void f(const char* name, void* context), void* context);
 102   // Debugging
 103   NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
 104   NOT_PRODUCT(bool is_jrt();)
 105 };
 106 
 107 
 108 // For lazier loading of boot class path entries
 109 class LazyClassPathEntry: public ClassPathEntry {
 110  private:
 111   const char* _path; // dir or file
 112   struct stat _st;
 113   bool _has_error;
 114   bool _throw_exception;
 115   volatile ClassPathEntry* _resolved_entry;
 116   ClassPathEntry* resolve_entry(TRAPS);
 117  public:
 118   bool is_jar_file();
 119   const char* name()  { return _path; }
 120   LazyClassPathEntry(const char* path, const struct stat* st, bool throw_exception);
 121   virtual ~LazyClassPathEntry();
 122   u1* open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS);
 123 
 124   ClassFileStream* open_stream(const char* name, TRAPS);
 125   virtual bool is_lazy();
 126   // Debugging
 127   NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
 128   NOT_PRODUCT(bool is_jrt();)
 129 };
 130 
 131 // For java image files
 132 class ImageFile;
 133 class ClassPathImageEntry: public ClassPathEntry {
 134 private:
 135   ImageFile *_image;
 136 public:
 137   bool is_jar_file()  { return false;  }
 138   bool is_open()  { return _image != NULL; }
 139   const char* name();
 140   ClassPathImageEntry(char* name);
 141   ~ClassPathImageEntry();
 142   ClassFileStream* open_stream(const char* name, TRAPS);
 143 
 144   // Debugging
 145   NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
 146   NOT_PRODUCT(bool is_jrt();)
 147 };
 148 
 149 class PackageHashtable;
 150 class PackageInfo;
 151 class SharedPathsMiscInfo;
 152 template <MEMFLAGS F> class HashtableBucket;
 153 
 154 class ClassLoader: AllStatic {
 155  public:
 156   enum SomeConstants {
 157     package_hash_table_size = 31  // Number of buckets
 158   };
 159  protected:
 160   friend class LazyClassPathEntry;
 161 
 162   // Performance counters
 163   static PerfCounter* _perf_accumulated_time;
 164   static PerfCounter* _perf_classes_inited;
 165   static PerfCounter* _perf_class_init_time;
 166   static PerfCounter* _perf_class_init_selftime;
 167   static PerfCounter* _perf_classes_verified;
 168   static PerfCounter* _perf_class_verify_time;
 169   static PerfCounter* _perf_class_verify_selftime;
 170   static PerfCounter* _perf_classes_linked;
 171   static PerfCounter* _perf_class_link_time;
 172   static PerfCounter* _perf_class_link_selftime;
 173   static PerfCounter* _perf_class_parse_time;
 174   static PerfCounter* _perf_class_parse_selftime;
 175   static PerfCounter* _perf_sys_class_lookup_time;
 176   static PerfCounter* _perf_shared_classload_time;
 177   static PerfCounter* _perf_sys_classload_time;
 178   static PerfCounter* _perf_app_classload_time;
 179   static PerfCounter* _perf_app_classload_selftime;
 180   static PerfCounter* _perf_app_classload_count;
 181   static PerfCounter* _perf_define_appclasses;
 182   static PerfCounter* _perf_define_appclass_time;
 183   static PerfCounter* _perf_define_appclass_selftime;
 184   static PerfCounter* _perf_app_classfile_bytes_read;
 185   static PerfCounter* _perf_sys_classfile_bytes_read;
 186 
 187   static PerfCounter* _sync_systemLoaderLockContentionRate;
 188   static PerfCounter* _sync_nonSystemLoaderLockContentionRate;
 189   static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter;
 190   static PerfCounter* _sync_JVMDefineClassLockFreeCounter;
 191   static PerfCounter* _sync_JNIDefineClassLockFreeCounter;
 192 
 193   static PerfCounter* _unsafe_defineClassCallCounter;
 194   static PerfCounter* _isUnsyncloadClass;
 195   static PerfCounter* _load_instance_class_failCounter;
 196 
 197   // First entry in linked list of ClassPathEntry instances
 198   static ClassPathEntry* _first_entry;
 199   // Last entry in linked list of ClassPathEntry instances
 200   static ClassPathEntry* _last_entry;
 201   static int _num_entries;
 202 
 203   // Hash table used to keep track of loaded packages
 204   static PackageHashtable* _package_hash_table;
 205   static const char* _shared_archive;
 206 
 207   // Info used by CDS
 208   CDS_ONLY(static SharedPathsMiscInfo * _shared_paths_misc_info;)
 209 
 210   // Hash function
 211   static unsigned int hash(const char *s, int n);
 212   // Returns the package file name corresponding to the specified package
 213   // or class name, or null if not found.
 214   static PackageInfo* lookup_package(const char *pkgname);
 215   // Adds a new package entry for the specified class or package name and
 216   // corresponding directory or jar file name.
 217   static bool add_package(const char *pkgname, int classpath_index, TRAPS);
 218 
 219   // Initialization
 220   static void setup_bootstrap_search_path();
 221   static void setup_search_path(const char *class_path);
 222 
 223   static void load_zip_library();
 224   static ClassPathEntry* create_class_path_entry(const char *path, const struct stat* st,
 225                                                  bool lazy, bool throw_exception, TRAPS);
 226 
 227   // Canonicalizes path names, so strcmp will work properly. This is mainly
 228   // to avoid confusing the zip library
 229   static bool get_canonical_path(const char* orig, char* out, int len);
 230  public:
 231   static jboolean decompress(void *in, u8 inSize, void *out, u8 outSize, char **pmsg);
 232   static int crc32(int crc, const char* buf, int len);
 233   static bool update_class_path_entry_list(const char *path,
 234                                            bool check_for_duplicates,
 235                                            bool throw_exception=true);
 236   static void print_bootclasspath();
 237 
 238   // Timing
 239   static PerfCounter* perf_accumulated_time()         { return _perf_accumulated_time; }
 240   static PerfCounter* perf_classes_inited()           { return _perf_classes_inited; }
 241   static PerfCounter* perf_class_init_time()          { return _perf_class_init_time; }
 242   static PerfCounter* perf_class_init_selftime()      { return _perf_class_init_selftime; }
 243   static PerfCounter* perf_classes_verified()         { return _perf_classes_verified; }
 244   static PerfCounter* perf_class_verify_time()        { return _perf_class_verify_time; }
 245   static PerfCounter* perf_class_verify_selftime()    { return _perf_class_verify_selftime; }
 246   static PerfCounter* perf_classes_linked()           { return _perf_classes_linked; }
 247   static PerfCounter* perf_class_link_time()          { return _perf_class_link_time; }
 248   static PerfCounter* perf_class_link_selftime()      { return _perf_class_link_selftime; }
 249   static PerfCounter* perf_class_parse_time()         { return _perf_class_parse_time; }
 250   static PerfCounter* perf_class_parse_selftime()     { return _perf_class_parse_selftime; }
 251   static PerfCounter* perf_sys_class_lookup_time()    { return _perf_sys_class_lookup_time; }
 252   static PerfCounter* perf_shared_classload_time()    { return _perf_shared_classload_time; }
 253   static PerfCounter* perf_sys_classload_time()       { return _perf_sys_classload_time; }
 254   static PerfCounter* perf_app_classload_time()       { return _perf_app_classload_time; }
 255   static PerfCounter* perf_app_classload_selftime()   { return _perf_app_classload_selftime; }
 256   static PerfCounter* perf_app_classload_count()      { return _perf_app_classload_count; }
 257   static PerfCounter* perf_define_appclasses()        { return _perf_define_appclasses; }
 258   static PerfCounter* perf_define_appclass_time()     { return _perf_define_appclass_time; }
 259   static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }
 260   static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }
 261   static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }
 262 
 263   // Record how often system loader lock object is contended
 264   static PerfCounter* sync_systemLoaderLockContentionRate() {
 265     return _sync_systemLoaderLockContentionRate;
 266   }
 267 
 268   // Record how often non system loader lock object is contended
 269   static PerfCounter* sync_nonSystemLoaderLockContentionRate() {
 270     return _sync_nonSystemLoaderLockContentionRate;
 271   }
 272 
 273   // Record how many calls to JVM_FindLoadedClass w/o holding a lock
 274   static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() {
 275     return _sync_JVMFindLoadedClassLockFreeCounter;
 276   }
 277 
 278   // Record how many calls to JVM_DefineClass w/o holding a lock
 279   static PerfCounter* sync_JVMDefineClassLockFreeCounter() {
 280     return _sync_JVMDefineClassLockFreeCounter;
 281   }
 282 
 283   // Record how many calls to jni_DefineClass w/o holding a lock
 284   static PerfCounter* sync_JNIDefineClassLockFreeCounter() {
 285     return _sync_JNIDefineClassLockFreeCounter;
 286   }
 287 
 288   // Record how many calls to Unsafe_DefineClass
 289   static PerfCounter* unsafe_defineClassCallCounter() {
 290     return _unsafe_defineClassCallCounter;
 291   }
 292 
 293   // Record how many times SystemDictionary::load_instance_class call
 294   // fails with linkageError when Unsyncloadclass flag is set.
 295   static PerfCounter* load_instance_class_failCounter() {
 296     return _load_instance_class_failCounter;
 297   }
 298 
 299   // Load individual .class file
 300   static instanceKlassHandle load_classfile(Symbol* h_name, TRAPS);
 301 
 302   // If the specified package has been loaded by the system, then returns
 303   // the name of the directory or ZIP file that the package was loaded from.
 304   // Returns null if the package was not loaded.
 305   // Note: The specified name can either be the name of a class or package.
 306   // If a package name is specified, then it must be "/"-separator and also
 307   // end with a trailing "/".
 308   static oop get_system_package(const char* name, TRAPS);
 309 
 310   // Returns an array of Java strings representing all of the currently
 311   // loaded system packages.
 312   // Note: The package names returned are "/"-separated and end with a
 313   // trailing "/".
 314   static objArrayOop get_system_packages(TRAPS);
 315 
 316   // Initialization
 317   static void initialize();
 318   CDS_ONLY(static void initialize_shared_path();)
 319   static void create_package_info_table();
 320   static void create_package_info_table(HashtableBucket<mtClass> *t, int length,
 321                                         int number_of_entries);
 322   static int compute_Object_vtable();
 323 
 324   static ClassPathEntry* classpath_entry(int n) {
 325     ClassPathEntry* e = ClassLoader::_first_entry;
 326     while (--n >= 0) {
 327       assert(e != NULL, "Not that many classpath entries.");
 328       e = e->next();
 329     }
 330     return e;
 331   }
 332 
 333 #if INCLUDE_CDS
 334   // Sharing dump and restore
 335   static void copy_package_info_buckets(char** top, char* end);
 336   static void copy_package_info_table(char** top, char* end);
 337 
 338   static void  check_shared_classpath(const char *path);
 339   static void  finalize_shared_paths_misc_info();
 340   static int   get_shared_paths_misc_info_size();
 341   static void* get_shared_paths_misc_info();
 342   static bool  check_shared_paths_misc_info(void* info, int size);
 343   static void  exit_with_path_failure(const char* error, const char* message);
 344 #endif
 345 
 346   static void  trace_class_path(const char* msg, const char* name = NULL);
 347 
 348   // VM monitoring and management support
 349   static jlong classloader_time_ms();
 350   static jlong class_method_total_size();
 351   static jlong class_init_count();
 352   static jlong class_init_time_ms();
 353   static jlong class_verify_time_ms();
 354   static jlong class_link_count();
 355   static jlong class_link_time_ms();
 356 
 357   // indicates if class path already contains a entry (exact match by name)
 358   static bool contains_entry(ClassPathEntry* entry);
 359 
 360   // adds a class path list
 361   static void add_to_list(ClassPathEntry* new_entry);
 362 
 363   // creates a class path zip entry (returns NULL if JAR file cannot be opened)
 364   static ClassPathZipEntry* create_class_path_zip_entry(const char *apath);
 365 
 366   // Debugging
 367   static void verify()              PRODUCT_RETURN;
 368 
 369   // Force compilation of all methods in all classes in bootstrap class path (stress test)
 370 #ifndef PRODUCT
 371  protected:
 372   static int _compile_the_world_class_counter;
 373   static int _compile_the_world_method_counter;
 374  public:
 375   static void compile_the_world();
 376   static void compile_the_world_in(char* name, Handle loader, TRAPS);
 377   static int  compile_the_world_counter() { return _compile_the_world_class_counter; }
 378 #endif //PRODUCT
 379 };
 380 
 381 // PerfClassTraceTime is used to measure time for class loading related events.
 382 // This class tracks cumulative time and exclusive time for specific event types.
 383 // During the execution of one event, other event types (e.g. class loading and
 384 // resolution) as well as recursive calls of the same event type could happen.
 385 // Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)
 386 // (i.e. only one event type) are active at a time even multiple PerfClassTraceTime
 387 // instances have been created as multiple events are happening.
 388 class PerfClassTraceTime {
 389  public:
 390   enum {
 391     CLASS_LOAD   = 0,
 392     PARSE_CLASS  = 1,
 393     CLASS_LINK   = 2,
 394     CLASS_VERIFY = 3,
 395     CLASS_CLINIT = 4,
 396     DEFINE_CLASS = 5,
 397     EVENT_TYPE_COUNT = 6
 398   };
 399  protected:
 400   // _t tracks time from initialization to destruction of this timer instance
 401   // including time for all other event types, and recursive calls of this type.
 402   // When a timer is called recursively, the elapsedTimer _t would not be used.
 403   elapsedTimer     _t;
 404   PerfLongCounter* _timep;
 405   PerfLongCounter* _selftimep;
 406   PerfLongCounter* _eventp;
 407   // pointer to thread-local recursion counter and timer array
 408   // The thread_local timers track cumulative time for specific event types
 409   // exclusive of time for other event types, but including recursive calls
 410   // of the same type.
 411   int*             _recursion_counters;
 412   elapsedTimer*    _timers;
 413   int              _event_type;
 414   int              _prev_active_event;
 415 
 416  public:
 417 
 418   inline PerfClassTraceTime(PerfLongCounter* timep,     /* counter incremented with inclusive time */
 419                             PerfLongCounter* selftimep, /* counter incremented with exclusive time */
 420                             PerfLongCounter* eventp,    /* event counter */
 421                             int* recursion_counters,    /* thread-local recursion counter array */
 422                             elapsedTimer* timers,       /* thread-local timer array */
 423                             int type                    /* event type */ ) :
 424       _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {
 425     initialize();
 426   }
 427 
 428   inline PerfClassTraceTime(PerfLongCounter* timep,     /* counter incremented with inclusive time */
 429                             elapsedTimer* timers,       /* thread-local timer array */
 430                             int type                    /* event type */ ) :
 431       _timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {
 432     initialize();
 433   }
 434 
 435   inline void suspend() { _t.stop(); _timers[_event_type].stop(); }
 436   inline void resume()  { _t.start(); _timers[_event_type].start(); }
 437 
 438   ~PerfClassTraceTime();
 439   void initialize();
 440 };
 441 
 442 #endif // SHARE_VM_CLASSFILE_CLASSLOADER_HPP