Path: blob/master/src/hotspot/share/cds/filemap.hpp
41144 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_CDS_FILEMAP_HPP25#define SHARE_CDS_FILEMAP_HPP2627#include "cds/metaspaceShared.hpp"28#include "include/cds.h"29#include "oops/array.hpp"30#include "oops/compressedOops.hpp"31#include "utilities/align.hpp"3233// To understand the layout of the CDS archive file:34//35// java -Xlog:cds+map=info:file=cds.map:none:filesize=036// java -Xlog:cds+map=debug:file=cds.map:none:filesize=037// java -Xlog:cds+map=trace:file=cds.map:none:filesize=03839static const int JVM_IDENT_MAX = 256;4041class CHeapBitMap;42class ClassFileStream;43class ClassLoaderData;44class ClassPathEntry;45class outputStream;4647class SharedClassPathEntry {48enum {49modules_image_entry,50jar_entry,51signed_jar_entry,52dir_entry,53non_existent_entry,54unknown_entry55};5657void set_name(const char* name, TRAPS);5859u1 _type;60bool _is_module_path;61bool _from_class_path_attr;62time_t _timestamp; // jar timestamp, 0 if is directory, modules image or other63int64_t _filesize; // jar/jimage file size, -1 if is directory, -2 if other64Array<char>* _name;65Array<u1>* _manifest;6667public:68void init(bool is_modules_image, bool is_module_path, ClassPathEntry* cpe, TRAPS);69void init_as_non_existent(const char* path, TRAPS);70void metaspace_pointers_do(MetaspaceClosure* it);71bool validate(bool is_class_path = true) const;7273// The _timestamp only gets set for jar files.74bool has_timestamp() const {75return _timestamp != 0;76}77bool is_dir() const { return _type == dir_entry; }78bool is_modules_image() const { return _type == modules_image_entry; }79bool is_jar() const { return _type == jar_entry; }80bool is_signed() const { return _type == signed_jar_entry; }81void set_is_signed() {82_type = signed_jar_entry;83}84bool from_class_path_attr() { return _from_class_path_attr; }85time_t timestamp() const { return _timestamp; }86const char* name() const;87const char* manifest() const {88return (_manifest == NULL) ? NULL : (const char*)_manifest->data();89}90int manifest_size() const {91return (_manifest == NULL) ? 0 : _manifest->length();92}93void set_manifest(Array<u1>* manifest) {94_manifest = manifest;95}96bool check_non_existent() const;97void copy_from(SharedClassPathEntry* ent, ClassLoaderData* loader_data, TRAPS);98bool in_named_module() {99return is_modules_image() || // modules image doesn't contain unnamed modules100_is_module_path; // module path doesn't contain unnamed modules101}102};103104struct ArchiveHeapOopmapInfo {105address _oopmap; // bitmap for relocating embedded oops106size_t _offset; // this oopmap is stored at this offset from the bottom of the BM region107size_t _oopmap_size_in_bits;108size_t _oopmap_size_in_bytes;109};110111class SharedPathTable {112Array<u8>* _table;113int _size;114public:115SharedPathTable() : _table(NULL), _size(0) {}116SharedPathTable(Array<u8>* table, int size) : _table(table), _size(size) {}117118void dumptime_init(ClassLoaderData* loader_data, TRAPS);119void metaspace_pointers_do(MetaspaceClosure* it);120121int size() {122return _size;123}124SharedClassPathEntry* path_at(int index) {125if (index < 0) {126return NULL;127}128assert(index < _size, "sanity");129char* p = (char*)_table->data();130p += sizeof(SharedClassPathEntry) * index;131return (SharedClassPathEntry*)p;132}133Array<u8>* table() {return _table;}134void set_table(Array<u8>* table) {_table = table;}135};136137138class FileMapRegion: private CDSFileMapRegion {139void assert_is_heap_region() const {140assert(_is_heap_region, "must be heap region");141}142void assert_is_not_heap_region() const {143assert(!_is_heap_region, "must not be heap region");144}145146public:147static FileMapRegion* cast(CDSFileMapRegion* p) {148return (FileMapRegion*)p;149}150151// Accessors152int crc() const { return _crc; }153size_t file_offset() const { return _file_offset; }154size_t mapping_offset() const { return _mapping_offset; }155size_t mapping_end_offset() const { return _mapping_offset + used_aligned(); }156size_t used() const { return _used; }157size_t used_aligned() const; // aligned up to MetaspaceShared::core_region_alignment()158char* mapped_base() const { assert_is_not_heap_region(); return _mapped_base; }159char* mapped_end() const { return mapped_base() + used_aligned(); }160bool read_only() const { return _read_only != 0; }161bool allow_exec() const { return _allow_exec != 0; }162bool mapped_from_file() const { return _mapped_from_file != 0; }163size_t oopmap_offset() const { assert_is_heap_region(); return _oopmap_offset; }164size_t oopmap_size_in_bits() const { assert_is_heap_region(); return _oopmap_size_in_bits; }165166void set_file_offset(size_t s) { _file_offset = s; }167void set_read_only(bool v) { _read_only = v; }168void set_mapped_base(char* p) { _mapped_base = p; }169void set_mapped_from_file(bool v) { _mapped_from_file = v; }170void init(int region_index, size_t mapping_offset, size_t size, bool read_only,171bool allow_exec, int crc);172173void init_oopmap(size_t oopmap_offset, size_t size_in_bits) {174_oopmap_offset = oopmap_offset;175_oopmap_size_in_bits = size_in_bits;176}177178void print(outputStream* st, int region_index);179};180181class FileMapHeader: private CDSFileMapHeaderBase {182friend class CDSOffsets;183friend class VMStructs;184185size_t _header_size;186187// The following fields record the states of the VM during dump time.188// They are compared with the runtime states to see if the archive189// can be used.190size_t _core_region_alignment; // how shared archive should be aligned191int _obj_alignment; // value of ObjectAlignmentInBytes192address _narrow_oop_base; // compressed oop encoding base193int _narrow_oop_shift; // compressed oop encoding shift194bool _compact_strings; // value of CompactStrings195uintx _max_heap_size; // java max heap size during dumping196CompressedOops::Mode _narrow_oop_mode; // compressed oop encoding mode197int _narrow_klass_shift; // save narrow klass base and shift198bool _compressed_oops; // save the flag UseCompressedOops199bool _compressed_class_ptrs; // save the flag UseCompressedClassPointers200size_t _cloned_vtables_offset; // The address of the first cloned vtable201size_t _serialized_data_offset; // Data accessed using {ReadClosure,WriteClosure}::serialize()202address _heap_begin; // heap begin at dump time.203address _heap_end; // heap end at dump time.204bool _base_archive_is_default; // indicates if the base archive is the system default one205206// The following fields are all sanity checks for whether this archive207// will function correctly with this JVM and the bootclasspath it's208// invoked with.209char _jvm_ident[JVM_IDENT_MAX]; // identifier string of the jvm that created this dump210211// size of the base archive name including NULL terminator212size_t _base_archive_name_size;213214// The following is a table of all the boot/app/module path entries that were used215// during dumping. At run time, we validate these entries according to their216// SharedClassPathEntry::_type. See:217// check_nonempty_dir_in_shared_path_table()218// validate_shared_path_table()219// validate_non_existent_class_paths()220size_t _shared_path_table_offset;221int _shared_path_table_size;222223jshort _app_class_paths_start_index; // Index of first app classpath entry224jshort _app_module_paths_start_index; // Index of first module path entry225jshort _num_module_paths; // number of module path entries226jshort _max_used_path_index; // max path index referenced during CDS dump227bool _verify_local; // BytecodeVerificationLocal setting228bool _verify_remote; // BytecodeVerificationRemote setting229bool _has_platform_or_app_classes; // Archive contains app classes230char* _requested_base_address; // Archive relocation is not necessary if we map with this base address.231char* _mapped_base_address; // Actual base address where archive is mapped.232233bool _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option234bool _use_optimized_module_handling;// No module-relation VM options were specified, so we can skip235// some expensive operations.236bool _use_full_module_graph; // Can we use the full archived module graph?237size_t _ptrmap_size_in_bits; // Size of pointer relocation bitmap238narrowOop _heap_obj_roots; // An objArray that stores all the roots of archived heap objects239char* from_mapped_offset(size_t offset) const {240return mapped_base_address() + offset;241}242void set_as_offset(char* p, size_t *offset);243public:244// Accessors -- fields declared in CDSFileMapHeaderBase245unsigned int magic() const { return _magic; }246int crc() const { return _crc; }247int version() const { return _version; }248249void set_crc(int crc_value) { _crc = crc_value; }250void set_version(int v) { _version = v; }251252// Accessors -- fields declared in FileMapHeader253254size_t header_size() const { return _header_size; }255size_t core_region_alignment() const { return _core_region_alignment; }256int obj_alignment() const { return _obj_alignment; }257address narrow_oop_base() const { return _narrow_oop_base; }258int narrow_oop_shift() const { return _narrow_oop_shift; }259bool compact_strings() const { return _compact_strings; }260uintx max_heap_size() const { return _max_heap_size; }261CompressedOops::Mode narrow_oop_mode() const { return _narrow_oop_mode; }262int narrow_klass_shift() const { return _narrow_klass_shift; }263address narrow_klass_base() const { return (address)mapped_base_address(); }264char* cloned_vtables() const { return from_mapped_offset(_cloned_vtables_offset); }265char* serialized_data() const { return from_mapped_offset(_serialized_data_offset); }266address heap_begin() const { return _heap_begin; }267address heap_end() const { return _heap_end; }268bool base_archive_is_default() const { return _base_archive_is_default; }269const char* jvm_ident() const { return _jvm_ident; }270size_t base_archive_name_size() const { return _base_archive_name_size; }271char* requested_base_address() const { return _requested_base_address; }272char* mapped_base_address() const { return _mapped_base_address; }273bool has_platform_or_app_classes() const { return _has_platform_or_app_classes; }274size_t ptrmap_size_in_bits() const { return _ptrmap_size_in_bits; }275bool compressed_oops() const { return _compressed_oops; }276bool compressed_class_pointers() const { return _compressed_class_ptrs; }277// FIXME: These should really return int278jshort max_used_path_index() const { return _max_used_path_index; }279jshort app_module_paths_start_index() const { return _app_module_paths_start_index; }280jshort app_class_paths_start_index() const { return _app_class_paths_start_index; }281jshort num_module_paths() const { return _num_module_paths; }282narrowOop heap_obj_roots() const { return _heap_obj_roots; }283284void set_has_platform_or_app_classes(bool v) { _has_platform_or_app_classes = v; }285void set_cloned_vtables(char* p) { set_as_offset(p, &_cloned_vtables_offset); }286void set_serialized_data(char* p) { set_as_offset(p, &_serialized_data_offset); }287void set_base_archive_name_size(size_t s) { _base_archive_name_size = s; }288void set_base_archive_is_default(bool b) { _base_archive_is_default = b; }289void set_header_size(size_t s) { _header_size = s; }290void set_ptrmap_size_in_bits(size_t s) { _ptrmap_size_in_bits = s; }291void set_mapped_base_address(char* p) { _mapped_base_address = p; }292void set_heap_obj_roots(narrowOop r) { _heap_obj_roots = r; }293294void set_shared_path_table(SharedPathTable table) {295set_as_offset((char*)table.table(), &_shared_path_table_offset);296_shared_path_table_size = table.size();297}298299void set_requested_base(char* b) {300_requested_base_address = b;301_mapped_base_address = 0;302}303304SharedPathTable shared_path_table() const {305return SharedPathTable((Array<u8>*)from_mapped_offset(_shared_path_table_offset),306_shared_path_table_size);307}308309bool validate();310int compute_crc();311312FileMapRegion* space_at(int i) {313assert(is_valid_region(i), "invalid region");314return FileMapRegion::cast(&_space[i]);315}316317void populate(FileMapInfo* info, size_t core_region_alignment);318319static bool is_valid_region(int region) {320return (0 <= region && region < NUM_CDS_REGIONS);321}322323void print(outputStream* st);324};325326class FileMapInfo : public CHeapObj<mtInternal> {327private:328friend class ManifestStream;329friend class VMStructs;330friend class ArchiveBuilder;331friend class CDSOffsets;332friend class FileMapHeader;333334bool _is_static;335bool _file_open;336bool _is_mapped;337int _fd;338size_t _file_offset;339const char* _full_path;340const char* _base_archive_name;341FileMapHeader* _header;342343// TODO: Probably change the following to be non-static344static SharedPathTable _shared_path_table;345static SharedPathTable _saved_shared_path_table;346static bool _validating_shared_path_table;347348// FileMapHeader describes the shared space data in the file to be349// mapped. This structure gets written to a file. It is not a class, so350// that the compilers don't add any compiler-private data to it.351352static FileMapInfo* _current_info;353static FileMapInfo* _dynamic_archive_info;354static bool _heap_pointers_need_patching;355static bool _memory_mapping_failed;356static GrowableArray<const char*>* _non_existent_class_paths;357358FileMapHeader *header() const { return _header; }359360public:361static bool get_base_archive_name_from_header(const char* archive_name,362int* size, char** base_archive_name);363static bool check_archive(const char* archive_name, bool is_static);364static SharedPathTable shared_path_table() {365return _shared_path_table;366}367static SharedPathTable saved_shared_path_table() {368return _saved_shared_path_table;369}370371bool init_from_file(int fd);372static void metaspace_pointers_do(MetaspaceClosure* it, bool use_copy = true);373374void log_paths(const char* msg, int start_idx, int end_idx);375376FileMapInfo(bool is_static);377~FileMapInfo();378379// Accessors380int compute_header_crc() const { return header()->compute_crc(); }381void set_header_crc(int crc) { header()->set_crc(crc); }382int space_crc(int i) const { return space_at(i)->crc(); }383void populate_header(size_t core_region_alignment);384bool validate_header();385void invalidate();386int crc() const { return header()->crc(); }387int version() const { return header()->version(); }388unsigned int magic() const { return header()->magic(); }389address narrow_oop_base() const { return header()->narrow_oop_base(); }390int narrow_oop_shift() const { return header()->narrow_oop_shift(); }391uintx max_heap_size() const { return header()->max_heap_size(); }392address narrow_klass_base() const { return header()->narrow_klass_base(); }393int narrow_klass_shift() const { return header()->narrow_klass_shift(); }394size_t core_region_alignment() const { return header()->core_region_alignment(); }395396void set_header_base_archive_name_size(size_t size) { header()->set_base_archive_name_size(size); }397void set_header_base_archive_is_default(bool is_default) { header()->set_base_archive_is_default(is_default); }398399CompressedOops::Mode narrow_oop_mode() const { return header()->narrow_oop_mode(); }400jshort app_module_paths_start_index() const { return header()->app_module_paths_start_index(); }401jshort app_class_paths_start_index() const { return header()->app_class_paths_start_index(); }402403char* cloned_vtables() const { return header()->cloned_vtables(); }404void set_cloned_vtables(char* p) const { header()->set_cloned_vtables(p); }405char* serialized_data() const { return header()->serialized_data(); }406void set_serialized_data(char* p) const { header()->set_serialized_data(p); }407408bool is_file_position_aligned() const;409void align_file_position();410411bool is_static() const { return _is_static; }412bool is_mapped() const { return _is_mapped; }413void set_is_mapped(bool v) { _is_mapped = v; }414const char* full_path() const { return _full_path; }415416void set_requested_base(char* b) { header()->set_requested_base(b); }417char* requested_base_address() const { return header()->requested_base_address(); }418419class DynamicArchiveHeader* dynamic_header() const {420assert(!is_static(), "must be");421return (DynamicArchiveHeader*)header();422}423424void set_has_platform_or_app_classes(bool v) {425header()->set_has_platform_or_app_classes(v);426}427bool has_platform_or_app_classes() const {428return header()->has_platform_or_app_classes();429}430431static FileMapInfo* current_info() {432CDS_ONLY(return _current_info;)433NOT_CDS(return NULL;)434}435436static void set_current_info(FileMapInfo* info) {437CDS_ONLY(_current_info = info;)438}439440static FileMapInfo* dynamic_info() {441CDS_ONLY(return _dynamic_archive_info;)442NOT_CDS(return NULL;)443}444445static void assert_mark(bool check);446447// File manipulation.448bool initialize() NOT_CDS_RETURN_(false);449bool open_for_read();450void open_for_write(const char* path = NULL);451void write_header();452void write_region(int region, char* base, size_t size,453bool read_only, bool allow_exec);454char* write_bitmap_region(const CHeapBitMap* ptrmap,455GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps,456GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps,457size_t &size_in_bytes);458size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,459GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,460int first_region_id, int max_num_regions);461void write_bytes(const void* buffer, size_t count);462void write_bytes_aligned(const void* buffer, size_t count);463size_t read_bytes(void* buffer, size_t count);464MapArchiveResult map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs);465void unmap_regions(int regions[], int num_regions);466void map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;467void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;468void patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN;469void patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,470int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN;471bool has_heap_regions() NOT_CDS_JAVA_HEAP_RETURN_(false);472MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion());473void unmap_region(int i);474bool verify_region_checksum(int i);475void close();476bool is_open() { return _file_open; }477ReservedSpace reserve_shared_memory();478479// JVM/TI RedefineClasses() support:480// Remap the shared readonly space to shared readwrite, private.481bool remap_shared_readonly_as_readwrite();482483// Errors.484static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);485static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);486static bool memory_mapping_failed() {487CDS_ONLY(return _memory_mapping_failed;)488NOT_CDS(return false;)489}490bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);491492// Stop CDS sharing and unmap CDS regions.493static void stop_sharing_and_unmap(const char* msg);494495static void allocate_shared_path_table(TRAPS);496static void copy_shared_path_table(ClassLoaderData* loader_data, TRAPS);497static int add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS);498static void check_nonempty_dir_in_shared_path_table();499bool validate_shared_path_table();500void validate_non_existent_class_paths();501static void set_shared_path_table(FileMapInfo* info) {502_shared_path_table = info->header()->shared_path_table();503}504static void update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS);505static int num_non_existent_class_paths();506static void record_non_existent_class_path_entry(const char* path);507508#if INCLUDE_JVMTI509static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS);510#endif511512static SharedClassPathEntry* shared_path(int index) {513return _shared_path_table.path_at(index);514}515516static const char* shared_path_name(int index) {517assert(index >= 0, "Sanity");518return shared_path(index)->name();519}520521static int get_number_of_shared_paths() {522return _shared_path_table.size();523}524525static int get_module_shared_path_index(Symbol* location) NOT_CDS_RETURN_(-1);526527char* region_addr(int idx);528529// The offset of the first core region in the archive, relative to SharedBaseAddress530size_t mapping_base_offset() const { return first_core_space()->mapping_offset(); }531// The offset of the (exclusive) end of the last core region in this archive, relative to SharedBaseAddress532size_t mapping_end_offset() const { return last_core_space()->mapping_end_offset(); }533534char* mapped_base() const { return first_core_space()->mapped_base(); }535char* mapped_end() const { return last_core_space()->mapped_end(); }536537// Non-zero if the archive needs to be mapped a non-default location due to ASLR.538intx relocation_delta() const {539return header()->mapped_base_address() - header()->requested_base_address();540}541542FileMapRegion* first_core_space() const;543FileMapRegion* last_core_space() const;544545FileMapRegion* space_at(int i) const {546return header()->space_at(i);547}548549void print(outputStream* st) {550header()->print(st);551}552553const char* vm_version() {554return header()->jvm_ident();555}556557private:558void seek_to_position(size_t pos);559char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL);560int num_paths(const char* path) NOT_CDS_RETURN_(0);561GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);562bool classpath_failure(const char* msg, const char* name) NOT_CDS_RETURN_(false);563bool check_paths(int shared_path_start_idx, int num_paths,564GrowableArray<const char*>* rp_array) NOT_CDS_RETURN_(false);565bool validate_boot_class_paths() NOT_CDS_RETURN_(false);566bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);567bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num,568bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false);569bool region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false);570void dealloc_archive_heap_regions(MemRegion* regions, int num) NOT_CDS_JAVA_HEAP_RETURN;571void map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN;572char* map_bitmap_region();573MapArchiveResult map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs);574bool read_region(int i, char* base, size_t size);575bool relocate_pointers_in_core_regions(intx addr_delta);576static size_t set_oopmaps_offset(GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, size_t curr_size);577static size_t write_oopmaps(GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, size_t curr_offset, char* buffer);578579// The starting address of spc, as calculated with CompressedOop::decode_non_null()580address start_address_as_decoded_with_current_oop_encoding_mode(FileMapRegion* spc) {581return decode_start_address(spc, true);582}583584// The starting address of spc, as calculated with HeapShared::decode_from_archive()585address start_address_as_decoded_from_archive(FileMapRegion* spc) {586return decode_start_address(spc, false);587}588589address decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode);590591#if INCLUDE_JVMTI592static ClassPathEntry** _classpath_entries_for_jvmti;593static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS);594#endif595};596597#endif // SHARE_CDS_FILEMAP_HPP598599600