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 Handle get_protection_domain_from_classloader(Handle class_loader,
 85                                                        Handle url, TRAPS);
 86   static Handle get_shared_protection_domain(Handle class_loader,
 87                                              int shared_path_index,
 88                                              Handle url,
 89                                              TRAPS);
 90   static Handle get_shared_protection_domain(Handle class_loader,
 91                                              ModuleEntry* mod, TRAPS);
 92   static void atomic_set_array_index(OopHandle array, int index, oop o);
 93   static oop shared_protection_domain(int index);
 94   static void allocate_shared_protection_domain_array(int size, TRAPS);
 95   static oop shared_jar_url(int index);
 96   static void allocate_shared_jar_url_array(int size, TRAPS);
 97   static oop shared_jar_manifest(int index);
 98   static void allocate_shared_jar_manifest_array(int size, TRAPS);
 99   static Handle init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS);
100 
101   static void allocate_shared_data_arrays(int size, TRAPS) {
102     allocate_shared_protection_domain_array(size, CHECK);
103     allocate_shared_jar_url_array(size, CHECK);
104     allocate_shared_jar_manifest_array(size, CHECK);
105   }
106   static void atomic_set_shared_protection_domain(int index, oop pd) {
107     atomic_set_array_index(_shared_protection_domains, index, pd);
108   }
109   static void atomic_set_shared_jar_url(int index, oop url) {
110     atomic_set_array_index(_shared_jar_urls, index, url);
111   }
112   static void atomic_set_shared_jar_manifest(int index, oop man) {
113     atomic_set_array_index(_shared_jar_manifests, index, man);
114   }
115 };
116 
117 #endif // SHARE_CDS_CDSPROTECTIONDOMAIN_HPP