1 /*
   2  * Copyright (c) 2014, 2018, 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_SYSTEMDICTIONARYSHARED_HPP
  26 #define SHARE_VM_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP
  27 
  28 #include "oops/klass.hpp"
  29 #include "classfile/dictionary.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "memory/filemap.hpp"
  32 
  33 
  34 /*===============================================================================
  35 
  36     Handling of the classes in the AppCDS archive
  37 
  38     To ensure safety and to simplify the implementation, archived classes are
  39     "segregated" into several types. The following rules describe how they
  40     are stored and looked up.
  41 
  42 [1] Category of archived classes
  43 
  44     There are 3 disjoint groups of classes stored in the AppCDS archive. They are
  45     categorized as by their SharedDictionaryEntry::loader_type()
  46 
  47     BUILTIN:              These classes may be defined ONLY by the BOOT/PLATFORM/APP
  48                           loaders.
  49 
  50     UNREGISTERED:         These classes may be defined ONLY by a ClassLoader
  51                           instance that's not listed above (using fingerprint matching)
  52 
  53 [2] How classes from different categories are specified in the classlist:
  54 
  55     Starting from JDK9, each class in the classlist may be specified with
  56     these keywords: "id", "super", "interfaces", "loader" and "source".
  57 
  58 
  59     BUILTIN               Only the "id" keyword may be (optionally) specified. All other
  60                           keywords are forbidden.
  61 
  62                           The named class is looked up from the jimage and from
  63                           Xbootclasspath/a and CLASSPATH.
  64 
  65     UNREGISTERED:         The "id", "super", and "source" keywords must all be
  66                           specified.
  67 
  68                           The "interfaces" keyword must be specified if the class implements
  69                           one or more local interfaces. The "interfaces" keyword must not be
  70                           specified if the class does not implement local interfaces.
  71 
  72                           The named class is looked up from the location specified in the
  73                           "source" keyword.
  74 
  75     Example classlist:
  76 
  77     # BUILTIN
  78     java/lang/Object id: 0
  79     java/lang/Cloneable id: 1
  80     java/lang/String
  81 
  82     # UNREGISTERED
  83     Bar id: 3 super: 0 interfaces: 1 source: /foo.jar
  84 
  85 
  86 [3] Identifying the loader_type of archived classes in the shared dictionary
  87 
  88     Each archived Klass* C is associated with a SharedDictionaryEntry* E
  89 
  90     BUILTIN:              (C->shared_classpath_index() >= 0)
  91     UNREGISTERED:         (C->shared_classpath_index() <  0)
  92 
  93 [4] Lookup of archived classes at run time:
  94 
  95     (a) BUILTIN loaders:
  96 
  97         Search the shared directory for a BUILTIN class with a matching name.
  98 
  99     (b) UNREGISTERED loaders:
 100 
 101         The search originates with SystemDictionaryShared::lookup_from_stream().
 102 
 103         Search the shared directory for a UNREGISTERED class with a matching
 104         (name, clsfile_len, clsfile_crc32) tuple.
 105 
 106 ===============================================================================*/
 107 #define UNREGISTERED_INDEX -9999
 108 
 109 class ClassFileStream;
 110 
 111 // Archived classes need extra information not needed by traditionally loaded classes.
 112 // To keep footprint small, we add these in the dictionary entry instead of the InstanceKlass.
 113 class SharedDictionaryEntry : public DictionaryEntry {
 114 
 115 public:
 116   enum LoaderType {
 117     LT_BUILTIN,
 118     LT_UNREGISTERED
 119   };
 120 
 121   enum {
 122     FROM_FIELD_IS_PROTECTED = 1 << 0,
 123     FROM_IS_ARRAY           = 1 << 1,
 124     FROM_IS_OBJECT          = 1 << 2
 125   };
 126 
 127   int             _id;
 128   int             _clsfile_size;
 129   int             _clsfile_crc32;
 130   void*           _verifier_constraints; // FIXME - use a union here to avoid type casting??
 131   void*           _verifier_constraint_flags;
 132 
 133   // See "Identifying the loader_type of archived classes" comments above.
 134   LoaderType loader_type() const {
 135     Klass* k = (Klass*)literal();
 136 
 137     if ((k->shared_classpath_index() != UNREGISTERED_INDEX)) {
 138       return LT_BUILTIN;
 139     } else {
 140       return LT_UNREGISTERED;
 141     }
 142   }
 143 
 144   SharedDictionaryEntry* next() {
 145     return (SharedDictionaryEntry*)(DictionaryEntry::next());
 146   }
 147 
 148   bool is_builtin() const {
 149     return loader_type() == LT_BUILTIN;
 150   }
 151   bool is_unregistered() const {
 152     return loader_type() == LT_UNREGISTERED;
 153   }
 154 
 155   void add_verification_constraint(Symbol* name,
 156          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object);
 157   int finalize_verification_constraints();
 158   void check_verification_constraints(InstanceKlass* klass, TRAPS);
 159   void metaspace_pointers_do(MetaspaceClosure* it) NOT_CDS_RETURN;
 160 };
 161 
 162 class SharedDictionary : public Dictionary {
 163   SharedDictionaryEntry* get_entry_for_builtin_loader(const Symbol* name) const;
 164   SharedDictionaryEntry* get_entry_for_unregistered_loader(const Symbol* name,
 165                                                            int clsfile_size,
 166                                                            int clsfile_crc32) const;
 167 
 168   // Convenience functions
 169   SharedDictionaryEntry* bucket(int index) const {
 170     return (SharedDictionaryEntry*)(Dictionary::bucket(index));
 171   }
 172 
 173 public:
 174   SharedDictionaryEntry* find_entry_for(Klass* klass);
 175   void finalize_verification_constraints();
 176 
 177   bool add_non_builtin_klass(const Symbol* class_name,
 178                              ClassLoaderData* loader_data,
 179                              InstanceKlass* obj);
 180 
 181   void update_entry(Klass* klass, int id);
 182 
 183   Klass* find_class_for_builtin_loader(const Symbol* name) const;
 184   Klass* find_class_for_unregistered_loader(const Symbol* name,
 185                                             int clsfile_size,
 186                                             int clsfile_crc32) const;
 187   bool class_exists_for_unregistered_loader(const Symbol* name) {
 188     return (get_entry_for_unregistered_loader(name, -1, -1) != NULL);
 189   }
 190 };
 191 
 192 class SystemDictionaryShared: public SystemDictionary {
 193 private:
 194   // These _shared_xxxs arrays are used to initialize the java.lang.Package and
 195   // java.security.ProtectionDomain objects associated with each shared class.
 196   //
 197   // See SystemDictionaryShared::init_security_info for more info.
 198   static objArrayOop _shared_protection_domains;
 199   static objArrayOop _shared_jar_urls;
 200   static objArrayOop _shared_jar_manifests;
 201 
 202   static InstanceKlass* load_shared_class_for_builtin_loader(
 203                                                Symbol* class_name,
 204                                                Handle class_loader,
 205                                                TRAPS);
 206   static Handle get_package_name(Symbol*  class_name, TRAPS);
 207 
 208 
 209   // Package handling:
 210   //
 211   // 1. For named modules in the runtime image
 212   //    BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces
 213   //                  to get packages in named modules for shared classes.
 214   //                  Package for non-shared classes in named module is also
 215   //                  handled using JVM_GetSystemPackage(s).
 216   //
 217   //    APP  classes: VM calls ClassLoaders.AppClassLoader::definePackage(String, Module)
 218   //                  to define package for shared app classes from named
 219   //                  modules.
 220   //
 221   //    PLATFORM  classes: VM calls ClassLoaders.PlatformClassLoader::definePackage(String, Module)
 222   //                  to define package for shared platform classes from named
 223   //                  modules.
 224   //
 225   // 2. For unnamed modules
 226   //    BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces to
 227   //                  get packages for shared boot classes in unnamed modules.
 228   //
 229   //    APP  classes: VM calls ClassLoaders.AppClassLoader::defineOrCheckPackage()
 230   //                  with with the manifest and url from archived data.
 231   //
 232   //    PLATFORM  classes: No package is defined.
 233   //
 234   // The following two define_shared_package() functions are used to define
 235   // package for shared APP and PLATFORM classes.
 236   static void define_shared_package(Symbol*  class_name,
 237                                     Handle class_loader,
 238                                     Handle manifest,
 239                                     Handle url,
 240                                     TRAPS);
 241   static void define_shared_package(Symbol* class_name,
 242                                     Handle class_loader,
 243                                     ModuleEntry* mod_entry,
 244                                     TRAPS);
 245 
 246   static Handle get_shared_jar_manifest(int shared_path_index, TRAPS);
 247   static Handle get_shared_jar_url(int shared_path_index, TRAPS);
 248   static Handle get_protection_domain_from_classloader(Handle class_loader,
 249                                                        Handle url, TRAPS);
 250   static Handle get_shared_protection_domain(Handle class_loader,
 251                                              int shared_path_index,
 252                                              Handle url,
 253                                              TRAPS);
 254   static Handle get_shared_protection_domain(Handle class_loader,
 255                                              ModuleEntry* mod, TRAPS);
 256   static Handle init_security_info(Handle class_loader, InstanceKlass* ik, TRAPS);
 257 
 258   static void atomic_set_array_index(objArrayOop array, int index, oop o) {
 259     // Benign race condition:  array.obj_at(index) may already be filled in.
 260     // The important thing here is that all threads pick up the same result.
 261     // It doesn't matter which racing thread wins, as long as only one
 262     // result is used by all threads, and all future queries.
 263     array->atomic_compare_exchange_oop(index, o, NULL);
 264   }
 265 
 266   static oop shared_protection_domain(int index);
 267   static void atomic_set_shared_protection_domain(int index, oop pd) {
 268     atomic_set_array_index(_shared_protection_domains, index, pd);
 269   }
 270   static void allocate_shared_protection_domain_array(int size, TRAPS);
 271   static oop shared_jar_url(int index);
 272   static void atomic_set_shared_jar_url(int index, oop url) {
 273     atomic_set_array_index(_shared_jar_urls, index, url);
 274   }
 275   static void allocate_shared_jar_url_array(int size, TRAPS);
 276   static oop shared_jar_manifest(int index);
 277   static void atomic_set_shared_jar_manifest(int index, oop man) {
 278     atomic_set_array_index(_shared_jar_manifests, index, man);
 279   }
 280   static void allocate_shared_jar_manifest_array(int size, TRAPS);
 281   static InstanceKlass* acquire_class_for_current_thread(
 282                                  InstanceKlass *ik,
 283                                  Handle class_loader,
 284                                  Handle protection_domain,
 285                                  TRAPS);
 286 
 287 public:
 288   static void initialize(TRAPS);
 289 
 290   // Called by PLATFORM/APP loader only
 291   static InstanceKlass* find_or_load_shared_class(Symbol* class_name,
 292                                                Handle class_loader,
 293                                                TRAPS);
 294 
 295 
 296   static void allocate_shared_data_arrays(int size, TRAPS);
 297   static void oops_do(OopClosure* f);
 298   static void roots_oops_do(OopClosure* f) {
 299     oops_do(f);
 300   }
 301 
 302   // Check if sharing is supported for the class loader.
 303   static bool is_sharing_possible(ClassLoaderData* loader_data);
 304   static bool is_shared_class_visible_for_classloader(InstanceKlass* ik,
 305                                                       Handle class_loader,
 306                                                       const char* pkg_string,
 307                                                       Symbol* pkg_name,
 308                                                       PackageEntry* pkg_entry,
 309                                                       ModuleEntry* mod_entry,
 310                                                       TRAPS);
 311   static PackageEntry* get_package_entry(Symbol* pkg,
 312                                          ClassLoaderData *loader_data) {
 313     if (loader_data != NULL) {
 314       PackageEntryTable* pkgEntryTable = loader_data->packages();
 315       return pkgEntryTable->lookup_only(pkg);
 316     }
 317     return NULL;
 318   }
 319 
 320   static bool add_non_builtin_klass(Symbol* class_name, ClassLoaderData* loader_data,
 321                                     InstanceKlass* k, TRAPS);
 322   static Klass* dump_time_resolve_super_or_fail(Symbol* child_name,
 323                                                 Symbol* class_name,
 324                                                 Handle class_loader,
 325                                                 Handle protection_domain,
 326                                                 bool is_superclass,
 327                                                 TRAPS);
 328 
 329   static size_t dictionary_entry_size() {
 330     return (DumpSharedSpaces) ? sizeof(SharedDictionaryEntry) : sizeof(DictionaryEntry);
 331   }
 332   static void init_shared_dictionary_entry(Klass* k, DictionaryEntry* entry) NOT_CDS_RETURN;
 333   static bool is_builtin(DictionaryEntry* ent) {
 334     // Can't use virtual function is_builtin because DictionaryEntry doesn't initialize
 335     // vtable because it's not constructed properly.
 336     SharedDictionaryEntry* entry = (SharedDictionaryEntry*)ent;
 337     return entry->is_builtin();
 338   }
 339 
 340   // For convenient access to the SharedDictionaryEntry's of the archived classes.
 341   static SharedDictionary* shared_dictionary() {
 342     assert(!DumpSharedSpaces, "not for dumping");
 343     return (SharedDictionary*)SystemDictionary::shared_dictionary();
 344   }
 345 
 346   static SharedDictionary* boot_loader_dictionary() {
 347     return (SharedDictionary*)ClassLoaderData::the_null_class_loader_data()->dictionary();
 348   }
 349 
 350   static void update_shared_entry(Klass* klass, int id) {
 351     assert(DumpSharedSpaces, "sanity");
 352     assert((SharedDictionary*)(klass->class_loader_data()->dictionary()) != NULL, "sanity");
 353     ((SharedDictionary*)(klass->class_loader_data()->dictionary()))->update_entry(klass, id);
 354   }
 355 
 356   static void set_shared_class_misc_info(Klass* k, ClassFileStream* cfs);
 357 
 358   static InstanceKlass* lookup_from_stream(const Symbol* class_name,
 359                                            Handle class_loader,
 360                                            Handle protection_domain,
 361                                            const ClassFileStream* st,
 362                                            TRAPS);
 363   // "verification_constraints" are a set of checks performed by
 364   // VerificationType::is_reference_assignable_from when verifying a shared class during
 365   // dump time.
 366   //
 367   // With AppCDS, it is possible to override archived classes by calling
 368   // ClassLoader.defineClass() directly. SystemDictionary::load_shared_class() already
 369   // ensures that you cannot load a shared class if its super type(s) are changed. However,
 370   // we need an additional check to ensure that the verification_constraints did not change
 371   // between dump time and runtime.
 372   static bool add_verification_constraint(Klass* k, Symbol* name,
 373                   Symbol* from_name, bool from_field_is_protected,
 374                   bool from_is_array, bool from_is_object) NOT_CDS_RETURN_(false);
 375   static void finalize_verification_constraints() NOT_CDS_RETURN;
 376   static void check_verification_constraints(InstanceKlass* klass,
 377                                               TRAPS) NOT_CDS_RETURN;
 378 };
 379 
 380 #endif // SHARE_VM_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP