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/lambdaFormInvokers.hpp"
27 #include "cds/metaspaceShared.hpp"
28 #include "cds/regeneratedClasses.hpp"
29 #include "classfile/classFileStream.hpp"
30 #include "classfile/classLoadInfo.hpp"
31 #include "classfile/javaClasses.inline.hpp"
32 #include "classfile/klassFactory.hpp"
33 #include "classfile/symbolTable.hpp"
34 #include "classfile/systemDictionary.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "classfile/vmClasses.hpp"
37 #include "classfile/vmSymbols.hpp"
38 #include "logging/log.hpp"
39 #include "memory/oopFactory.hpp"
40 #include "memory/resourceArea.hpp"
41 #include "oops/instanceKlass.hpp"
42 #include "oops/klass.inline.hpp"
43 #include "oops/objArrayKlass.hpp"
44 #include "oops/objArrayOop.hpp"
45 #include "oops/oop.inline.hpp"
72 MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
73 if (_lambdaform_lines == nullptr) {
74 _lambdaform_lines = new GrowableArrayCHeap<char*, mtClassShared>(150);
75 }
76 _lambdaform_lines->append(line);
77 }
78
79
80 // convenient output
81 class PrintLambdaFormMessage {
82 public:
83 PrintLambdaFormMessage() {
84 log_info(cds)("Regenerate MethodHandle Holder classes...");
85 }
86 ~PrintLambdaFormMessage() {
87 log_info(cds)("Regenerate MethodHandle Holder classes...done");
88 }
89 };
90
91 void LambdaFormInvokers::regenerate_holder_classes(TRAPS) {
92 PrintLambdaFormMessage plm;
93 if (_lambdaform_lines == nullptr || _lambdaform_lines->length() == 0) {
94 log_info(cds)("Nothing to regenerate for holder classes");
95 return;
96 }
97
98 if (CDSConfig::is_dumping_static_archive() && CDSConfig::is_dumping_invokedynamic()) {
99 // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
100 log_info(cds)("Archived MethodHandles may refer to lambda form holder classes. Cannot regenerate.");
101 return;
102 }
103
104 if (CDSConfig::is_dumping_dynamic_archive() && CDSConfig::is_dumping_aot_linked_classes() &&
105 CDSConfig::is_using_aot_linked_classes()) {
106 // The base archive may have some pre-resolved CP entries that point to the lambda form holder
107 // classes in the base archive. If we generate new versions of these classes, those CP entries
108 // will be pointing to invalid classes.
109 log_info(cds)("Base archive already has aot-linked lambda form holder classes. Cannot regenerate.");
110 return;
111 }
112
113 ResourceMark rm(THREAD);
114
115 Symbol* cds_name = vmSymbols::jdk_internal_misc_CDS();
116 Klass* cds_klass = SystemDictionary::resolve_or_null(cds_name, THREAD);
117 guarantee(cds_klass != nullptr, "jdk/internal/misc/CDS must exist!");
118
119 HandleMark hm(THREAD);
120 int len = _lambdaform_lines->length();
121 objArrayHandle list_lines;
122 {
123 MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
124 list_lines = oopFactory::new_objArray_handle(vmClasses::String_klass(), len, CHECK);
125 for (int i = 0; i < len; i++) {
126 Handle h_line = java_lang_String::create_from_str(_lambdaform_lines->at(i), CHECK);
127 list_lines->obj_at_put(i, h_line());
128 }
129 } // Before calling into java, release vm lock.
130 //
131 // Object[] CDS.generateLambdaFormHolderClasses(String[] lines)
132 // the returned Object[] layout:
164 if (strstr(class_name, "java/lang/invoke/BoundMethodHandle$Species_") != nullptr) {
165 // The species classes are already loaded into the system dictionary
166 // during the execution of CDS.generateLambdaFormHolderClasses(). No
167 // need to regenerate.
168 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
169 Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
170 assert(klass != nullptr, "must already be loaded");
171 if (!klass->is_shared() && klass->shared_classpath_index() < 0) {
172 // Fake it, so that it will be included into the archive.
173 klass->set_shared_classpath_index(0);
174 // Set the "generated" bit, so it won't interfere with JVMTI.
175 // See SystemDictionaryShared::find_builtin_class().
176 klass->set_is_generated_shared_class();
177 }
178 } else {
179 int len = h_bytes->length();
180 // make a copy of class bytes so GC will not affect us.
181 char *buf = NEW_RESOURCE_ARRAY(char, len);
182 memcpy(buf, (char*)h_bytes->byte_at_addr(0), len);
183 ClassFileStream st((u1*)buf, len, nullptr);
184 regenerate_class(class_name, st, CHECK);
185 }
186 }
187 }
188
189 void LambdaFormInvokers::regenerate_class(char* class_name, ClassFileStream& st, TRAPS) {
190 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
191 Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
192 assert(klass != nullptr, "must exist");
193 assert(klass->is_instance_klass(), "Should be");
194
195 ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
196 Handle protection_domain;
197 ClassLoadInfo cl_info(protection_domain);
198
199 InstanceKlass* result = KlassFactory::create_from_stream(&st,
200 class_name_sym,
201 cld,
202 cl_info,
203 CHECK);
204
228 if (should_be_archived(str)) {
229 count++;
230 }
231 }
232 if (count > 0) {
233 _static_archive_invokers = ArchiveBuilder::new_ro_array<u4>(count);
234 int index = 0;
235 for (int i = 0; i < len; i++) {
236 char* str = _lambdaform_lines->at(i);
237 if (should_be_archived(str)) {
238 size_t str_len = strlen(str) + 1; // including terminating zero
239 Array<char>* line = ArchiveBuilder::new_ro_array<char>((int)str_len);
240 strncpy(line->adr_at(0), str, str_len);
241
242 _static_archive_invokers->at_put(index, ArchiveBuilder::current()->any_to_offset_u4(line));
243 index++;
244 }
245 }
246 assert(index == count, "Should match");
247 }
248 log_debug(cds)("Total LF lines stored into static archive: %d", count);
249 }
250 }
251
252 void LambdaFormInvokers::read_static_archive_invokers() {
253 if (_static_archive_invokers != nullptr) {
254 for (int i = 0; i < _static_archive_invokers->length(); i++) {
255 u4 offset = _static_archive_invokers->at(i);
256 Array<char>* line = ArchiveUtils::offset_to_archived_address<Array<char>*>(offset);
257 char* str = line->adr_at(0);
258 append(str);
259 }
260 log_debug(cds)("Total LF lines read from static archive: %d", _static_archive_invokers->length());
261 }
262 }
263
264 void LambdaFormInvokers::serialize(SerializeClosure* soc) {
265 soc->do_ptr(&_static_archive_invokers);
266 if (soc->reading() && CDSConfig::is_dumping_final_static_archive()) {
267 LambdaFormInvokers::read_static_archive_invokers();
268 _static_archive_invokers = nullptr;
269 }
270 }
|
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/cdsConfig.hpp"
27 #include "cds/lambdaFormInvokers.hpp"
28 #include "cds/metaspaceShared.hpp"
29 #include "cds/regeneratedClasses.hpp"
30 #include "classfile/classFileStream.hpp"
31 #include "classfile/classLoadInfo.hpp"
32 #include "classfile/javaClasses.inline.hpp"
33 #include "classfile/klassFactory.hpp"
34 #include "classfile/symbolTable.hpp"
35 #include "classfile/systemDictionary.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "classfile/vmClasses.hpp"
38 #include "classfile/vmSymbols.hpp"
39 #include "logging/log.hpp"
40 #include "memory/oopFactory.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/instanceKlass.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/objArrayKlass.hpp"
45 #include "oops/objArrayOop.hpp"
46 #include "oops/oop.inline.hpp"
73 MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
74 if (_lambdaform_lines == nullptr) {
75 _lambdaform_lines = new GrowableArrayCHeap<char*, mtClassShared>(150);
76 }
77 _lambdaform_lines->append(line);
78 }
79
80
81 // convenient output
82 class PrintLambdaFormMessage {
83 public:
84 PrintLambdaFormMessage() {
85 log_info(cds)("Regenerate MethodHandle Holder classes...");
86 }
87 ~PrintLambdaFormMessage() {
88 log_info(cds)("Regenerate MethodHandle Holder classes...done");
89 }
90 };
91
92 void LambdaFormInvokers::regenerate_holder_classes(TRAPS) {
93 if (!CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
94 return;
95 }
96 PrintLambdaFormMessage plm;
97 if (_lambdaform_lines == nullptr || _lambdaform_lines->length() == 0) {
98 log_info(cds)("Nothing to regenerate for holder classes");
99 return;
100 }
101
102 ResourceMark rm(THREAD);
103
104 Symbol* cds_name = vmSymbols::jdk_internal_misc_CDS();
105 Klass* cds_klass = SystemDictionary::resolve_or_null(cds_name, THREAD);
106 guarantee(cds_klass != nullptr, "jdk/internal/misc/CDS must exist!");
107
108 HandleMark hm(THREAD);
109 int len = _lambdaform_lines->length();
110 objArrayHandle list_lines;
111 {
112 MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
113 list_lines = oopFactory::new_objArray_handle(vmClasses::String_klass(), len, CHECK);
114 for (int i = 0; i < len; i++) {
115 Handle h_line = java_lang_String::create_from_str(_lambdaform_lines->at(i), CHECK);
116 list_lines->obj_at_put(i, h_line());
117 }
118 } // Before calling into java, release vm lock.
119 //
120 // Object[] CDS.generateLambdaFormHolderClasses(String[] lines)
121 // the returned Object[] layout:
153 if (strstr(class_name, "java/lang/invoke/BoundMethodHandle$Species_") != nullptr) {
154 // The species classes are already loaded into the system dictionary
155 // during the execution of CDS.generateLambdaFormHolderClasses(). No
156 // need to regenerate.
157 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
158 Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
159 assert(klass != nullptr, "must already be loaded");
160 if (!klass->is_shared() && klass->shared_classpath_index() < 0) {
161 // Fake it, so that it will be included into the archive.
162 klass->set_shared_classpath_index(0);
163 // Set the "generated" bit, so it won't interfere with JVMTI.
164 // See SystemDictionaryShared::find_builtin_class().
165 klass->set_is_generated_shared_class();
166 }
167 } else {
168 int len = h_bytes->length();
169 // make a copy of class bytes so GC will not affect us.
170 char *buf = NEW_RESOURCE_ARRAY(char, len);
171 memcpy(buf, (char*)h_bytes->byte_at_addr(0), len);
172 ClassFileStream st((u1*)buf, len, nullptr);
173 if (!CDSConfig::is_dumping_invokedynamic() /* work around JDK-8310831 */) {
174 regenerate_class(class_name, st, CHECK);
175 }
176 }
177 }
178 }
179
180 void LambdaFormInvokers::regenerate_class(char* class_name, ClassFileStream& st, TRAPS) {
181 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
182 Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
183 assert(klass != nullptr, "must exist");
184 assert(klass->is_instance_klass(), "Should be");
185
186 ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
187 Handle protection_domain;
188 ClassLoadInfo cl_info(protection_domain);
189
190 InstanceKlass* result = KlassFactory::create_from_stream(&st,
191 class_name_sym,
192 cld,
193 cl_info,
194 CHECK);
195
219 if (should_be_archived(str)) {
220 count++;
221 }
222 }
223 if (count > 0) {
224 _static_archive_invokers = ArchiveBuilder::new_ro_array<u4>(count);
225 int index = 0;
226 for (int i = 0; i < len; i++) {
227 char* str = _lambdaform_lines->at(i);
228 if (should_be_archived(str)) {
229 size_t str_len = strlen(str) + 1; // including terminating zero
230 Array<char>* line = ArchiveBuilder::new_ro_array<char>((int)str_len);
231 strncpy(line->adr_at(0), str, str_len);
232
233 _static_archive_invokers->at_put(index, ArchiveBuilder::current()->any_to_offset_u4(line));
234 index++;
235 }
236 }
237 assert(index == count, "Should match");
238 }
239 log_debug(cds)("Total LF lines stored into %s: %d", CDSConfig::type_of_archive_being_written(), count);
240 }
241 }
242
243 void LambdaFormInvokers::read_static_archive_invokers() {
244 if (_static_archive_invokers != nullptr) {
245 for (int i = 0; i < _static_archive_invokers->length(); i++) {
246 u4 offset = _static_archive_invokers->at(i);
247 Array<char>* line = ArchiveUtils::offset_to_archived_address<Array<char>*>(offset);
248 char* str = line->adr_at(0);
249 append(str);
250 }
251 log_debug(cds)("Total LF lines read from %s: %d", CDSConfig::type_of_archive_being_loaded(), _static_archive_invokers->length());
252 }
253 }
254
255 void LambdaFormInvokers::serialize(SerializeClosure* soc) {
256 soc->do_ptr(&_static_archive_invokers);
257 if (soc->reading() && CDSConfig::is_dumping_final_static_archive()) {
258 if (!CDSConfig::is_dumping_aot_linked_classes()) {
259 // See CDSConfig::is_dumping_regenerated_lambdaform_invokers() -- a dynamic archive can
260 // regenerate lambda form invokers only if the base archive does not contain aot-linked classes.
261 // If so, we copy the contents of _static_archive_invokers (from the preimage) into
262 //_lambdaform_lines, which will be written as _static_archive_invokers into final static archive.
263 LambdaFormInvokers::read_static_archive_invokers();
264 }
265 _static_archive_invokers = nullptr;
266 }
267 }
|