1 /* 2 * Copyright (c) 2023, 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 #include "precompiled.hpp" 25 #include "classfile/classLoaderData.hpp" 26 #include "classfile/placeholders.hpp" 27 #include "classfile/symbolTable.hpp" 28 #include "oops/symbol.hpp" 29 #include "runtime/interfaceSupport.inline.hpp" 30 #include "runtime/mutexLocker.hpp" 31 #include "threadHelper.inline.hpp" 32 #include "unittest.hpp" 33 34 // Test that multiple threads calling handle_parallel_super_load don't underflow supername refcount. 35 TEST_VM(PlaceholderTable, supername) { 36 JavaThread* THREAD = JavaThread::current(); 37 JavaThread* T2 = THREAD; 38 // the thread should be in vm to use locks 39 ThreadInVMfromNative tivfn(THREAD); 40 41 // Assert messages assume these symbols are unique, and the refcounts start at one. 42 Symbol* A = SymbolTable::new_symbol("abc2_8_2023_class"); 43 Symbol* D = SymbolTable::new_symbol("def2_8_2023_class"); 44 Symbol* super = SymbolTable::new_symbol("super2_8_2023_supername"); 45 Symbol* interf = SymbolTable::new_symbol("interface2_8_2023_supername"); 46 47 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 48 49 { 50 MutexLocker ml(THREAD, SystemDictionary_lock); 51 52 PlaceholderTable::classloadAction super_action = PlaceholderTable::LOAD_SUPER; 53 PlaceholderTable::classloadAction define_action = PlaceholderTable::DEFINE_CLASS; 54 55 // DefineClass A and D 56 PlaceholderTable::find_and_add(A, loader_data, define_action, nullptr, THREAD); 57 PlaceholderTable::find_and_add(D, loader_data, define_action, nullptr, T2); 58 59 // Load interfaces first to get supername replaced 60 PlaceholderTable::find_and_add(A, loader_data, super_action, interf, THREAD); 61 PlaceholderTable::find_and_remove(A, loader_data, super_action, THREAD); 62 63 PlaceholderTable::find_and_add(D, loader_data, super_action, interf, T2); 64 PlaceholderTable::find_and_remove(D, loader_data, super_action, T2); 65 66 ASSERT_EQ(interf->refcount(), 1) << "supername is replaced with null"; 67 68 // Add placeholder to the table for loading A and super, and D also loading super 69 PlaceholderTable::find_and_add(A, loader_data, super_action, super, THREAD); 70 PlaceholderTable::find_and_add(D, loader_data, super_action, super, T2); 71 72 // Another thread comes in and finds A loading Super 73 PlaceholderEntry* placeholder = PlaceholderTable::get_entry(A, loader_data); 74 SymbolHandle supername = placeholder->supername(); 75 76 // Other thread is done before handle_parallel_super_load 77 PlaceholderTable::find_and_remove(A, loader_data, super_action, THREAD); 78 79 // if THREAD drops reference to supername (loading failed or class unloaded), we're left with 80 // a supername without refcount 81 super->decrement_refcount(); 82 83 // handle_parallel_super_load (same thread doesn't assert) 84 PlaceholderTable::find_and_add(A, loader_data, super_action, supername, T2); 85 86 // Refcount should be 3: one in table for class A, one in table for class D 87 // and one locally with SymbolHandle keeping it alive 88 placeholder = PlaceholderTable::get_entry(A, loader_data); 89 supername = placeholder->supername(); 90 EXPECT_EQ(super->refcount(), 3) << "super class name refcount should be 3"; 91 92 // Second thread's done too 93 PlaceholderTable::find_and_remove(D, loader_data, super_action, T2); 94 95 // Other threads are done. 96 PlaceholderTable::find_and_remove(A, loader_data, super_action, THREAD); 97 98 // Remove A and D define_class placeholder 99 PlaceholderTable::find_and_remove(A, loader_data, define_action, THREAD); 100 PlaceholderTable::find_and_remove(D, loader_data, define_action, T2); 101 102 placeholder = PlaceholderTable::get_entry(A, loader_data); 103 ASSERT_TRUE(placeholder == nullptr) << "placeholder should be removed"; 104 placeholder = PlaceholderTable::get_entry(D, loader_data); 105 ASSERT_TRUE(placeholder == nullptr) << "placeholder should be removed"; 106 107 EXPECT_EQ(super->refcount(), 1) << "super class name refcount should be 1 - kept alive in this scope"; 108 } 109 110 EXPECT_EQ(A->refcount(), 1) << "first lass name refcount should be 1"; 111 EXPECT_EQ(D->refcount(), 1) << "second class name refcount should be 1"; 112 EXPECT_EQ(super->refcount(), 0) << "super class name refcount should be 0 - was unloaded."; 113 114 // clean up temporary symbols 115 A->decrement_refcount(); 116 D->decrement_refcount(); 117 interf->decrement_refcount(); 118 }