1 /*
 2  * Copyright (c) 2023, 2025, 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 #include "cds/archiveBuilder.hpp"
26 #include "cds/regeneratedClasses.hpp"
27 #include "memory/universe.hpp"
28 #include "oops/instanceKlass.hpp"
29 #include "oops/method.hpp"
30 #include "oops/oopHandle.inline.hpp"
31 #include "runtime/mutexLocker.hpp"
32 #include "runtime/thread.hpp"
33 #include "utilities/growableArray.hpp"
34 #include "utilities/resourceHash.hpp"
35 
36 using RegeneratedObjTable = ResourceHashtable<address, address, 15889, AnyObj::C_HEAP, mtClassShared>;
37 static RegeneratedObjTable* _renegerated_objs = nullptr; // InstanceKlass* and Method*
38 static GrowableArrayCHeap<OopHandle, mtClassShared>* _regenerated_mirrors = nullptr;
39 
40 // The regenerated Klass is not added to any class loader, so we need
41 // to keep its java_mirror alive to avoid class unloading.
42 void RegeneratedClasses::add_class(InstanceKlass* orig_klass, InstanceKlass* regen_klass) {
43   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
44   if (_regenerated_mirrors == nullptr) {
45     _regenerated_mirrors = new GrowableArrayCHeap<OopHandle, mtClassShared>(150);
46   }
47   _regenerated_mirrors->append(OopHandle(Universe::vm_global(), regen_klass->java_mirror()));
48 
49   if (_renegerated_objs == nullptr) {
50     _renegerated_objs = new (mtClass)RegeneratedObjTable();
51   }
52 
53   _renegerated_objs->put((address)orig_klass, (address)regen_klass);
54   Array<Method*>* methods = orig_klass->methods();
55   for (int i = 0; i < methods->length(); i++) {
56     Method* orig_m = methods->at(i);
57     Method* regen_m = regen_klass->find_method(orig_m->name(), orig_m->signature());
58     if (regen_m == nullptr) {
59       ResourceMark rm;
60       log_warning(cds)("Method in original class is missing from regenerated class: " INTPTR_FORMAT " %s",
61                        p2i(orig_m), orig_m->external_name());
62     } else {
63       _renegerated_objs->put((address)orig_m, (address)regen_m);
64     }
65   }
66 }
67 
68 bool RegeneratedClasses::has_been_regenerated(address orig_obj) {
69   if (_renegerated_objs == nullptr) {
70     return false;
71   } else {
72     return _renegerated_objs->get(orig_obj) != nullptr;
73   }
74 }
75 
76 void RegeneratedClasses::record_regenerated_objects() {
77   assert_locked_or_safepoint(DumpTimeTable_lock);
78   if (_renegerated_objs != nullptr) {
79     auto doit = [&] (address orig_obj, address regen_obj) {
80       ArchiveBuilder::current()->record_regenerated_object(orig_obj, regen_obj);
81     };
82     _renegerated_objs->iterate_all(doit);
83   }
84 }
85 
86 void RegeneratedClasses::cleanup() {
87   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
88   if (_regenerated_mirrors != nullptr) {
89     for (int i = 0; i < _regenerated_mirrors->length(); i++) {
90       _regenerated_mirrors->at(i).release(Universe::vm_global());
91     }
92     delete _regenerated_mirrors;
93     _regenerated_mirrors = nullptr;
94   }
95   if (_renegerated_objs != nullptr) {
96     delete _renegerated_objs;
97   }
98 }