1 /*
  2  * Copyright (c) 2021, 2023, 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_CDS_CDSPROTECTIONDOMAIN_HPP
 26 #define SHARE_CDS_CDSPROTECTIONDOMAIN_HPP
 27 
 28 #include "classfile/moduleEntry.hpp"
 29 #include "oops/oopHandle.inline.hpp"
 30 #include "runtime/handles.inline.hpp"
 31 #include "runtime/javaThread.hpp"
 32 
 33 class InstanceKlass;
 34 class Symbol;
 35 class PackageEntry;
 36 class ModuleEntry;
 37 
 38 // CDS security
 39 class CDSProtectionDomain : AllStatic {
 40   // See init_security_info for more info.
 41   static OopHandle _shared_protection_domains;
 42   static OopHandle _shared_jar_urls;
 43   static OopHandle _shared_jar_manifests;
 44 
 45   static Handle get_archived_protection_domain(JavaThread* current, InstanceKlass* klass);
 46 public:
 47   // Package handling:
 48   //
 49   // 1. For named modules in the runtime image
 50   //    BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces
 51   //                  to get packages in named modules for shared classes.
 52   //                  Package for non-shared classes in named module is also
 53   //                  handled using JVM_GetSystemPackage(s).
 54   //
 55   //    APP  classes: VM calls ClassLoaders.AppClassLoader::definePackage(String, Module)
 56   //                  to define package for shared app classes from named
 57   //                  modules.
 58   //
 59   //    PLATFORM  classes: VM calls ClassLoaders.PlatformClassLoader::definePackage(String, Module)
 60   //                  to define package for shared platform classes from named
 61   //                  modules.
 62   //
 63   // 2. For unnamed modules
 64   //    BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces to
 65   //                  get packages for shared boot classes in unnamed modules.
 66   //
 67   //    APP  classes: VM calls ClassLoaders.AppClassLoader::defineOrCheckPackage()
 68   //                  with with the manifest and url from archived data.
 69   //
 70   //    PLATFORM  classes: No package is defined.
 71   //
 72   // The following two define_shared_package() functions are used to define
 73   // package for shared APP and PLATFORM classes.
 74   static Handle        get_package_name(Symbol*  class_name, TRAPS);
 75   static PackageEntry* get_package_entry_from_class(InstanceKlass* ik, Handle class_loader);
 76   static void define_shared_package(Symbol*  class_name,
 77                                     Handle class_loader,
 78                                     Handle manifest,
 79                                     Handle url,
 80                                     TRAPS);
 81   static Handle create_jar_manifest(const char* man, size_t size, TRAPS);
 82   static Handle get_shared_jar_manifest(int shared_path_index, TRAPS);
 83   static Handle get_shared_jar_url(int shared_path_index, TRAPS);
 84   static oop to_file_URL(const char* path, Handle url_h, TRAPS);
 85   static Handle get_protection_domain_from_classloader(Handle class_loader,
 86                                                        Handle url, TRAPS);
 87   static Handle get_shared_protection_domain(Handle class_loader,
 88                                              int shared_path_index,
 89                                              Handle url,
 90                                              TRAPS);
 91   static Handle get_shared_protection_domain(Handle class_loader,
 92                                              ModuleEntry* mod, TRAPS);
 93   static void atomic_set_array_index(OopHandle array, int index, oop o);
 94   static oop shared_protection_domain(int index);
 95   static void allocate_shared_protection_domain_array(int size, TRAPS);
 96   static oop shared_jar_url(int index);
 97   static void allocate_shared_jar_url_array(int size, TRAPS);
 98   static oop shared_jar_manifest(int index);
 99   static void allocate_shared_jar_manifest_array(int size, TRAPS);
100   static Handle init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS);
101 
102   static void allocate_shared_data_arrays(int size, TRAPS) {
103     allocate_shared_protection_domain_array(size, CHECK);
104     allocate_shared_jar_url_array(size, CHECK);
105     allocate_shared_jar_manifest_array(size, CHECK);
106   }
107   static void atomic_set_shared_protection_domain(int index, oop pd) {
108     atomic_set_array_index(_shared_protection_domains, index, pd);
109   }
110   static void atomic_set_shared_jar_url(int index, oop url) {
111     atomic_set_array_index(_shared_jar_urls, index, url);
112   }
113   static void atomic_set_shared_jar_manifest(int index, oop man) {
114     atomic_set_array_index(_shared_jar_manifests, index, man);
115   }
116 };
117 
118 #endif // SHARE_CDS_CDSPROTECTIONDOMAIN_HPP