1 /*
  2  * Copyright (c) 2019, 2021, 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_ARCHIVEUTILS_HPP
 26 #define SHARE_CDS_ARCHIVEUTILS_HPP
 27 
 28 #include "logging/log.hpp"
 29 #include "memory/iterator.hpp"
 30 #include "memory/virtualspace.hpp"
 31 #include "utilities/bitMap.hpp"
 32 #include "utilities/exceptions.hpp"
 33 
 34 class BootstrapInfo;
 35 class ReservedSpace;
 36 class VirtualSpace;
 37 
 38 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
 39 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
 40 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
 41 // fixed, but _ptr_end can be expanded as more objects are dumped.
 42 class ArchivePtrMarker : AllStatic {
 43   static CHeapBitMap*  _ptrmap;
 44   static VirtualSpace* _vs;
 45 
 46   // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
 47   // avoid unintentional copy operations after the bitmap has been finalized and written.
 48   static bool         _compacted;
 49 
 50   static address* ptr_base() { return (address*)_vs->low();  } // committed lower bound (inclusive)
 51   static address* ptr_end()  { return (address*)_vs->high(); } // committed upper bound (exclusive)
 52 
 53 public:
 54   static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
 55   static void mark_pointer(address* ptr_loc);
 56   static void clear_pointer(address* ptr_loc);
 57   static void compact(address relocatable_base, address relocatable_end);
 58   static void compact(size_t max_non_null_offset);
 59 
 60   template <typename T>
 61   static void mark_pointer(T* ptr_loc) {
 62     mark_pointer((address*)ptr_loc);
 63   }
 64 
 65   template <typename T>
 66   static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
 67     *ptr_loc = ptr_value;
 68     mark_pointer(ptr_loc);
 69   }
 70 
 71   static CHeapBitMap* ptrmap() {
 72     return _ptrmap;
 73   }
 74 
 75   static void reset_map_and_vs() {
 76     _ptrmap = nullptr;
 77     _vs = nullptr;
 78   }
 79 };
 80 
 81 // SharedDataRelocator is used to shift pointers in the CDS archive.
 82 //
 83 // The CDS archive is basically a contiguous block of memory (divided into several regions)
 84 // that contains multiple objects. The objects may contain direct pointers that point to other objects
 85 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
 86 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
 87 //
 88 // The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000).
 89 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
 90 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
 91 // the actually mapped location of the target object.
 92 class SharedDataRelocator: public BitMapClosure {
 93   // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
 94 
 95   // Patch all pointers within this region that are marked.
 96   address* _patch_base;
 97   address* _patch_end;
 98 
 99   // Before patching, all pointers must point to this region.
100   address _valid_old_base;
101   address _valid_old_end;
102 
103   // After patching, all pointers must point to this region.
104   address _valid_new_base;
105   address _valid_new_end;
106 
107   // How much to relocate for each pointer.
108   intx _delta;
109 
110  public:
111   SharedDataRelocator(address* patch_base, address* patch_end,
112                       address valid_old_base, address valid_old_end,
113                       address valid_new_base, address valid_new_end, intx delta) :
114     _patch_base(patch_base), _patch_end(patch_end),
115     _valid_old_base(valid_old_base), _valid_old_end(valid_old_end),
116     _valid_new_base(valid_new_base), _valid_new_end(valid_new_end),
117     _delta(delta) {
118     log_debug(cds, reloc)("SharedDataRelocator::_patch_base     = " PTR_FORMAT, p2i(_patch_base));
119     log_debug(cds, reloc)("SharedDataRelocator::_patch_end      = " PTR_FORMAT, p2i(_patch_end));
120     log_debug(cds, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base));
121     log_debug(cds, reloc)("SharedDataRelocator::_valid_old_end  = " PTR_FORMAT, p2i(_valid_old_end));
122     log_debug(cds, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base));
123     log_debug(cds, reloc)("SharedDataRelocator::_valid_new_end  = " PTR_FORMAT, p2i(_valid_new_end));
124   }
125 
126   bool do_bit(size_t offset);
127 };
128 
129 class DumpRegion {
130 private:
131   const char* _name;
132   char* _base;
133   char* _top;
134   char* _end;
135   uintx _max_delta;
136   bool _is_packed;
137   ReservedSpace* _rs;
138   VirtualSpace* _vs;
139 
140   void commit_to(char* newtop);
141 
142 public:
143   DumpRegion(const char* name, uintx max_delta = 0)
144     : _name(name), _base(NULL), _top(NULL), _end(NULL),
145       _max_delta(max_delta), _is_packed(false) {}
146 
147   char* expand_top_to(char* newtop);
148   char* allocate(size_t num_bytes);
149 
150   void append_intptr_t(intptr_t n, bool need_to_mark = false);
151 
152   char* base()      const { return _base;        }
153   char* top()       const { return _top;         }
154   char* end()       const { return _end;         }
155   size_t reserved() const { return _end - _base; }
156   size_t used()     const { return _top - _base; }
157   bool is_packed()  const { return _is_packed;   }
158   bool is_allocatable() const {
159     return !is_packed() && _base != NULL;
160   }
161 
162   void print(size_t total_bytes) const;
163   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
164 
165   void init(ReservedSpace* rs, VirtualSpace* vs);
166 
167   void pack(DumpRegion* next = NULL);
168 
169   bool contains(char* p) {
170     return base() <= p && p < top();
171   }
172 };
173 
174 // Closure for serializing initialization data out to a data area to be
175 // written to the shared file.
176 
177 class WriteClosure : public SerializeClosure {
178 private:
179   DumpRegion* _dump_region;
180 
181 public:
182   WriteClosure(DumpRegion* r) {
183     _dump_region = r;
184   }
185 
186   void do_ptr(void** p) {
187     _dump_region->append_intptr_t((intptr_t)*p, true);
188   }
189 
190   void do_u4(u4* p) {
191     _dump_region->append_intptr_t((intptr_t)(*p));
192   }
193 
194   void do_bool(bool *p) {
195     _dump_region->append_intptr_t((intptr_t)(*p));
196   }
197 
198   void do_tag(int tag) {
199     _dump_region->append_intptr_t((intptr_t)tag);
200   }
201 
202   void do_oop(oop* o);
203   void do_region(u_char* start, size_t size);
204   bool reading() const { return false; }
205 };
206 
207 // Closure for serializing initialization data in from a data area
208 // (ptr_array) read from the shared file.
209 
210 class ReadClosure : public SerializeClosure {
211 private:
212   intptr_t** _ptr_array;
213 
214   inline intptr_t nextPtr() {
215     return *(*_ptr_array)++;
216   }
217 
218 public:
219   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
220 
221   void do_ptr(void** p);
222   void do_u4(u4* p);
223   void do_bool(bool *p);
224   void do_tag(int tag);
225   void do_oop(oop *p);
226   void do_region(u_char* start, size_t size);
227   bool reading() const { return true; }
228 };
229 
230 class ArchiveUtils {
231 public:
232   static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
233 };
234 
235 #endif // SHARE_CDS_ARCHIVEUTILS_HPP