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 if (log_is_enabled(Info, cds)) { 68 ResourceMark rm; 69 log_info(cds)("Regenerated class %s: methods %d -> %d)", orig_klass->external_name(), 70 orig_klass->methods()->length(), regen_klass->methods()->length()); 71 } 72 } 73 74 bool RegeneratedClasses::has_been_regenerated(address orig_obj) { 75 if (_renegerated_objs == nullptr) { 76 return false; 77 } else { 78 return _renegerated_objs->get(orig_obj) != nullptr; 79 } 80 } 81 82 address RegeneratedClasses::get_regenerated_object(address orig_obj) { 83 assert(_renegerated_objs != nullptr, "must be"); 84 address* p =_renegerated_objs->get(orig_obj); 85 assert(p != nullptr, "must be"); 86 return *p; 87 } 88 89 void RegeneratedClasses::record_regenerated_objects() { 90 assert_locked_or_safepoint(DumpTimeTable_lock); 91 if (_renegerated_objs != nullptr) { 92 auto doit = [&] (address orig_obj, address regen_obj) { 93 ArchiveBuilder::current()->record_regenerated_object(orig_obj, regen_obj); 94 }; 95 _renegerated_objs->iterate_all(doit); 96 } 97 } 98 99 void RegeneratedClasses::cleanup() { 100 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 101 if (_regenerated_mirrors != nullptr) { 102 for (int i = 0; i < _regenerated_mirrors->length(); i++) { 103 _regenerated_mirrors->at(i).release(Universe::vm_global()); 104 } 105 delete _regenerated_mirrors; 106 _regenerated_mirrors = nullptr; 107 } 108 if (_renegerated_objs != nullptr) { 109 delete _renegerated_objs; 110 } 111 }