Path: blob/master/src/hotspot/share/cds/archiveUtils.hpp
41144 views
/*1* Copyright (c) 2019, 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_ARCHIVEUTILS_HPP25#define SHARE_CDS_ARCHIVEUTILS_HPP2627#include "logging/log.hpp"28#include "memory/iterator.hpp"29#include "memory/virtualspace.hpp"30#include "utilities/bitMap.hpp"31#include "utilities/exceptions.hpp"3233class BootstrapInfo;34class ReservedSpace;35class VirtualSpace;3637// ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an38// InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling39// mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is40// fixed, but _ptr_end can be expanded as more objects are dumped.41class ArchivePtrMarker : AllStatic {42static CHeapBitMap* _ptrmap;43static VirtualSpace* _vs;4445// Once _ptrmap is compacted, we don't allow bit marking anymore. This is to46// avoid unintentional copy operations after the bitmap has been finalized and written.47static bool _compacted;4849static address* ptr_base() { return (address*)_vs->low(); } // committed lower bound (inclusive)50static address* ptr_end() { return (address*)_vs->high(); } // committed upper bound (exclusive)5152public:53static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);54static void mark_pointer(address* ptr_loc);55static void clear_pointer(address* ptr_loc);56static void compact(address relocatable_base, address relocatable_end);57static void compact(size_t max_non_null_offset);5859template <typename T>60static void mark_pointer(T* ptr_loc) {61mark_pointer((address*)ptr_loc);62}6364template <typename T>65static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {66*ptr_loc = ptr_value;67mark_pointer(ptr_loc);68}6970static CHeapBitMap* ptrmap() {71return _ptrmap;72}73};7475// SharedDataRelocator is used to shift pointers in the CDS archive.76//77// The CDS archive is basically a contiguous block of memory (divided into several regions)78// that contains multiple objects. The objects may contain direct pointers that point to other objects79// within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we80// built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).81//82// The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000).83// If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator84// is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to85// the actually mapped location of the target object.86class SharedDataRelocator: public BitMapClosure {87// for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }8889// Patch all pointers within this region that are marked.90address* _patch_base;91address* _patch_end;9293// Before patching, all pointers must point to this region.94address _valid_old_base;95address _valid_old_end;9697// After patching, all pointers must point to this region.98address _valid_new_base;99address _valid_new_end;100101// How much to relocate for each pointer.102intx _delta;103104public:105SharedDataRelocator(address* patch_base, address* patch_end,106address valid_old_base, address valid_old_end,107address valid_new_base, address valid_new_end, intx delta) :108_patch_base(patch_base), _patch_end(patch_end),109_valid_old_base(valid_old_base), _valid_old_end(valid_old_end),110_valid_new_base(valid_new_base), _valid_new_end(valid_new_end),111_delta(delta) {112log_debug(cds, reloc)("SharedDataRelocator::_patch_base = " PTR_FORMAT, p2i(_patch_base));113log_debug(cds, reloc)("SharedDataRelocator::_patch_end = " PTR_FORMAT, p2i(_patch_end));114log_debug(cds, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base));115log_debug(cds, reloc)("SharedDataRelocator::_valid_old_end = " PTR_FORMAT, p2i(_valid_old_end));116log_debug(cds, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base));117log_debug(cds, reloc)("SharedDataRelocator::_valid_new_end = " PTR_FORMAT, p2i(_valid_new_end));118}119120bool do_bit(size_t offset);121};122123class DumpRegion {124private:125const char* _name;126char* _base;127char* _top;128char* _end;129uintx _max_delta;130bool _is_packed;131ReservedSpace* _rs;132VirtualSpace* _vs;133134void commit_to(char* newtop);135136public:137DumpRegion(const char* name, uintx max_delta = 0)138: _name(name), _base(NULL), _top(NULL), _end(NULL),139_max_delta(max_delta), _is_packed(false) {}140141char* expand_top_to(char* newtop);142char* allocate(size_t num_bytes);143144void append_intptr_t(intptr_t n, bool need_to_mark = false);145146char* base() const { return _base; }147char* top() const { return _top; }148char* end() const { return _end; }149size_t reserved() const { return _end - _base; }150size_t used() const { return _top - _base; }151bool is_packed() const { return _is_packed; }152bool is_allocatable() const {153return !is_packed() && _base != NULL;154}155156void print(size_t total_bytes) const;157void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);158159void init(ReservedSpace* rs, VirtualSpace* vs);160161void pack(DumpRegion* next = NULL);162163bool contains(char* p) {164return base() <= p && p < top();165}166};167168// Closure for serializing initialization data out to a data area to be169// written to the shared file.170171class WriteClosure : public SerializeClosure {172private:173DumpRegion* _dump_region;174175public:176WriteClosure(DumpRegion* r) {177_dump_region = r;178}179180void do_ptr(void** p) {181_dump_region->append_intptr_t((intptr_t)*p, true);182}183184void do_u4(u4* p) {185_dump_region->append_intptr_t((intptr_t)(*p));186}187188void do_bool(bool *p) {189_dump_region->append_intptr_t((intptr_t)(*p));190}191192void do_tag(int tag) {193_dump_region->append_intptr_t((intptr_t)tag);194}195196void do_oop(oop* o);197void do_region(u_char* start, size_t size);198bool reading() const { return false; }199};200201// Closure for serializing initialization data in from a data area202// (ptr_array) read from the shared file.203204class ReadClosure : public SerializeClosure {205private:206intptr_t** _ptr_array;207208inline intptr_t nextPtr() {209return *(*_ptr_array)++;210}211212public:213ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }214215void do_ptr(void** p);216void do_u4(u4* p);217void do_bool(bool *p);218void do_tag(int tag);219void do_oop(oop *p);220void do_region(u_char* start, size_t size);221bool reading() const { return true; }222};223224class ArchiveUtils {225public:226static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;227};228229#endif // SHARE_CDS_ARCHIVEUTILS_HPP230231232