1 | /* |
2 | * Copyright (c) 2016, 2019, 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 "precompiled.hpp" |
26 | #include "jvm.h" |
27 | #include "classfile/classFileParser.hpp" |
28 | #include "classfile/classFileStream.hpp" |
29 | #include "classfile/javaClasses.inline.hpp" |
30 | #include "classfile/moduleEntry.hpp" |
31 | #include "classfile/modules.hpp" |
32 | #include "classfile/stackMapTable.hpp" |
33 | #include "classfile/symbolTable.hpp" |
34 | #include "classfile/verificationType.hpp" |
35 | #include "interpreter/bytecodes.hpp" |
36 | #include "jfr/instrumentation/jfrEventClassTransformer.hpp" |
37 | #include "jfr/jfr.hpp" |
38 | #include "jfr/jni/jfrJavaSupport.hpp" |
39 | #include "jfr/jni/jfrUpcalls.hpp" |
40 | #include "jfr/support/jfrEventClass.hpp" |
41 | #include "jfr/utilities/jfrBigEndian.hpp" |
42 | #include "jfr/writers/jfrBigEndianWriter.hpp" |
43 | #include "logging/log.hpp" |
44 | #include "memory/allocation.inline.hpp" |
45 | #include "memory/resourceArea.hpp" |
46 | #include "oops/array.hpp" |
47 | #include "oops/constantPool.hpp" |
48 | #include "oops/instanceKlass.hpp" |
49 | #include "oops/method.hpp" |
50 | #include "prims/jvmtiRedefineClasses.hpp" |
51 | #include "runtime/handles.inline.hpp" |
52 | #include "runtime/os.hpp" |
53 | #include "runtime/thread.inline.hpp" |
54 | #include "utilities/exceptions.hpp" |
55 | #include "utilities/globalDefinitions.hpp" |
56 | #include "utilities/macros.hpp" |
57 | |
58 | static const u2 number_of_new_methods = 5; |
59 | static const u2 number_of_new_fields = 3; |
60 | static const int = 0x280; |
61 | static const u2 invalid_cp_index = 0; |
62 | |
63 | static const char* utf8_constants[] = { |
64 | "Code" , // 0 |
65 | "J" , // 1 |
66 | "commit" , // 2 |
67 | "eventHandler" , // 3 |
68 | "duration" , // 4 |
69 | "begin" , // 5 |
70 | "()V" , // 6 |
71 | "isEnabled" , // 7 |
72 | "()Z" , // 8 |
73 | "end" , // 9 |
74 | "shouldCommit" , // 10 |
75 | "startTime" , // 11 // LAST_REQUIRED_UTF8 |
76 | "Ljdk/jfr/internal/handlers/EventHandler;" , // 12 |
77 | "Ljava/lang/Object;" , // 13 |
78 | "<clinit>" , // 14 |
79 | "jdk/jfr/FlightRecorder" , // 15 |
80 | "register" , // 16 |
81 | "(Ljava/lang/Class;)V" , // 17 |
82 | "StackMapTable" , // 18 |
83 | "Exceptions" , // 19 |
84 | "LineNumberTable" , // 20 |
85 | "LocalVariableTable" , // 21 |
86 | "LocalVariableTypeTable" , // 22 |
87 | "RuntimeVisibleAnnotation" , // 23 |
88 | }; |
89 | |
90 | enum utf8_req_symbols { |
91 | UTF8_REQ_Code, |
92 | UTF8_REQ_J_FIELD_DESC, |
93 | UTF8_REQ_commit, |
94 | UTF8_REQ_eventHandler, |
95 | UTF8_REQ_duration, |
96 | UTF8_REQ_begin, |
97 | UTF8_REQ_EMPTY_VOID_METHOD_DESC, |
98 | UTF8_REQ_isEnabled, |
99 | UTF8_REQ_EMPTY_BOOLEAN_METHOD_DESC, |
100 | UTF8_REQ_end, |
101 | UTF8_REQ_shouldCommit, |
102 | UTF8_REQ_startTime, |
103 | NOF_UTF8_REQ_SYMBOLS |
104 | }; |
105 | |
106 | enum utf8_opt_symbols { |
107 | UTF8_OPT_eventHandler_FIELD_DESC = NOF_UTF8_REQ_SYMBOLS, |
108 | UTF8_OPT_LjavaLangObject, |
109 | UTF8_OPT_clinit, |
110 | UTF8_OPT_FlightRecorder, |
111 | UTF8_OPT_register, |
112 | UTF8_OPT_CLASS_VOID_METHOD_DESC, |
113 | UTF8_OPT_StackMapTable, |
114 | UTF8_OPT_Exceptions, |
115 | UTF8_OPT_LineNumberTable, |
116 | UTF8_OPT_LocalVariableTable, |
117 | UTF8_OPT_LocalVariableTypeTable, |
118 | UTF8_OPT_RuntimeVisibleAnnotation, |
119 | NOF_UTF8_SYMBOLS |
120 | }; |
121 | |
122 | static u1 empty_void_method_code_attribute[] = { |
123 | 0x0, |
124 | 0x0, |
125 | 0x0, |
126 | 0xd, // attribute len |
127 | 0x0, |
128 | 0x0, // max stack |
129 | 0x0, |
130 | 0x1, // max locals |
131 | 0x0, |
132 | 0x0, |
133 | 0x0, |
134 | 0x1, // code length |
135 | Bytecodes::_return, |
136 | 0x0, |
137 | 0x0, // ex table len |
138 | 0x0, |
139 | 0x0 // attributes_count |
140 | }; |
141 | |
142 | static u1 boolean_method_code_attribute[] = { |
143 | 0x0, |
144 | 0x0, |
145 | 0x0, |
146 | 0xe, |
147 | 0x0, |
148 | 0x1, // max stack |
149 | 0x0, |
150 | 0x1, // max locals |
151 | 0x0, |
152 | 0x0, |
153 | 0x0, |
154 | 0x2, |
155 | Bytecodes::_iconst_0, |
156 | Bytecodes::_ireturn, |
157 | 0x0, |
158 | 0x0, // ex table len |
159 | 0x0, |
160 | 0x0, // attributes_count |
161 | }; |
162 | |
163 | // annotation processing support |
164 | |
165 | enum { // initial annotation layout |
166 | atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' |
167 | count_off = 2, // u2 such as 1 (one value) |
168 | member_off = 4, // utf8 such as 'value' |
169 | tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) |
170 | e_tag_val = 'e', |
171 | e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' |
172 | e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' |
173 | e_size = 11, // end of 'e' annotation |
174 | c_tag_val = 'c', // payload is type |
175 | c_con_off = 7, // utf8 payload, such as 'I' |
176 | c_size = 9, // end of 'c' annotation |
177 | s_tag_val = 's', // payload is String |
178 | s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' |
179 | s_size = 9, |
180 | min_size = 6 // smallest possible size (zero members) |
181 | }; |
182 | |
183 | static int skip_annotation_value(const address, int, int); // fwd decl |
184 | |
185 | // Skip an annotation. Return >=limit if there is any problem. |
186 | static int next_annotation_index(const address buffer, int limit, int index) { |
187 | assert(buffer != NULL, "invariant" ); |
188 | index += 2; // skip atype |
189 | if ((index += 2) >= limit) { |
190 | return limit; |
191 | } |
192 | int nof_members = JfrBigEndian::read<u2>(buffer + index - 2); |
193 | while (--nof_members >= 0 && index < limit) { |
194 | index += 2; // skip member |
195 | index = skip_annotation_value(buffer, limit, index); |
196 | } |
197 | return index; |
198 | } |
199 | |
200 | // Skip an annotation value. Return >=limit if there is any problem. |
201 | static int skip_annotation_value(const address buffer, int limit, int index) { |
202 | assert(buffer != NULL, "invariant" ); |
203 | // value := switch (tag:u1) { |
204 | // case B, C, I, S, Z, D, F, J, c: con:u2; |
205 | // case e: e_class:u2 e_name:u2; |
206 | // case s: s_con:u2; |
207 | // case [: do(nval:u2) {value}; |
208 | // case @: annotation; |
209 | // case s: s_con:u2; |
210 | // } |
211 | if ((index += 1) >= limit) { |
212 | return limit; |
213 | } |
214 | const u1 tag = buffer[index - 1]; |
215 | switch (tag) { |
216 | case 'B': |
217 | case 'C': |
218 | case 'I': |
219 | case 'S': |
220 | case 'Z': |
221 | case 'D': |
222 | case 'F': |
223 | case 'J': |
224 | case 'c': |
225 | case 's': |
226 | index += 2; // skip con or s_con |
227 | break; |
228 | case 'e': |
229 | index += 4; // skip e_class, e_name |
230 | break; |
231 | case '[': |
232 | { |
233 | if ((index += 2) >= limit) { |
234 | return limit; |
235 | } |
236 | int nof_values = JfrBigEndian::read<u2>(buffer + index - 2); |
237 | while (--nof_values >= 0 && index < limit) { |
238 | index = skip_annotation_value(buffer, limit, index); |
239 | } |
240 | } |
241 | break; |
242 | case '@': |
243 | index = next_annotation_index(buffer, limit, index); |
244 | break; |
245 | default: |
246 | return limit; // bad tag byte |
247 | } |
248 | return index; |
249 | } |
250 | |
251 | static const u2 number_of_elements_offset = (u2)2; |
252 | static const u2 element_name_offset = (u2)(number_of_elements_offset + 2); |
253 | static const u2 element_name_size = (u2)2; |
254 | static const u2 value_type_relative_offset = (u2)2; |
255 | static const u2 value_relative_offset = (u2)(value_type_relative_offset + 1); |
256 | |
257 | // see JVMS - 4.7.16. The RuntimeVisibleAnnotations Attribute |
258 | |
259 | class AnnotationElementIterator : public StackObj { |
260 | private: |
261 | const InstanceKlass* _ik; |
262 | const address _buffer; |
263 | const u2 _limit; // length of annotation |
264 | mutable u2 _current; // element |
265 | mutable u2 _next; // element |
266 | u2 value_index() const { |
267 | return JfrBigEndian::read<u2>(_buffer + _current + value_relative_offset); |
268 | } |
269 | |
270 | public: |
271 | AnnotationElementIterator(const InstanceKlass* ik, address buffer, u2 limit) : _ik(ik), |
272 | _buffer(buffer), |
273 | _limit(limit), |
274 | _current(element_name_offset), |
275 | _next(element_name_offset) { |
276 | assert(_buffer != NULL, "invariant" ); |
277 | assert(_next == element_name_offset, "invariant" ); |
278 | assert(_current == element_name_offset, "invariant" ); |
279 | } |
280 | |
281 | bool has_next() const { |
282 | return _next < _limit; |
283 | } |
284 | |
285 | void move_to_next() const { |
286 | assert(has_next(), "invariant" ); |
287 | _current = _next; |
288 | if (_next < _limit) { |
289 | _next = skip_annotation_value(_buffer, _limit, _next + element_name_size); |
290 | } |
291 | assert(_next <= _limit, "invariant" ); |
292 | assert(_current <= _limit, "invariant" ); |
293 | } |
294 | |
295 | u2 number_of_elements() const { |
296 | return JfrBigEndian::read<u2>(_buffer + number_of_elements_offset); |
297 | } |
298 | |
299 | const Symbol* name() const { |
300 | assert(_current < _next, "invariant" ); |
301 | return _ik->constants()->symbol_at(JfrBigEndian::read<u2>(_buffer + _current)); |
302 | } |
303 | |
304 | char value_type() const { |
305 | return JfrBigEndian::read<u1>(_buffer + _current + value_type_relative_offset); |
306 | } |
307 | |
308 | jint read_int() const { |
309 | return _ik->constants()->int_at(value_index()); |
310 | } |
311 | |
312 | bool read_bool() const { |
313 | return read_int() != 0; |
314 | } |
315 | }; |
316 | |
317 | class AnnotationIterator : public StackObj { |
318 | private: |
319 | const InstanceKlass* _ik; |
320 | // ensure _limit field is declared before _buffer |
321 | u2 _limit; // length of annotations array |
322 | const address _buffer; |
323 | mutable u2 _current; // annotation |
324 | mutable u2 _next; // annotation |
325 | |
326 | public: |
327 | AnnotationIterator(const InstanceKlass* ik, AnnotationArray* ar) : _ik(ik), |
328 | _limit(ar != NULL ? ar->length() : 0), |
329 | _buffer(_limit > 2 ? ar->adr_at(2) : NULL), |
330 | _current(0), |
331 | _next(0) { |
332 | if (_buffer != NULL) { |
333 | _limit -= 2; // subtract sizeof(u2) number of annotations field |
334 | } |
335 | } |
336 | bool has_next() const { |
337 | return _next < _limit; |
338 | } |
339 | |
340 | void move_to_next() const { |
341 | assert(has_next(), "invariant" ); |
342 | _current = _next; |
343 | if (_next < _limit) { |
344 | _next = next_annotation_index(_buffer, _limit, _next); |
345 | } |
346 | assert(_next <= _limit, "invariant" ); |
347 | assert(_current <= _limit, "invariant" ); |
348 | } |
349 | const AnnotationElementIterator elements() const { |
350 | assert(_current < _next, "invariant" ); |
351 | return AnnotationElementIterator(_ik, _buffer + _current, _next - _current); |
352 | } |
353 | const Symbol* type() const { |
354 | assert(_buffer != NULL, "invariant" ); |
355 | assert(_current < _limit, "invariant" ); |
356 | return _ik->constants()->symbol_at(JfrBigEndian::read<u2>(_buffer + _current)); |
357 | } |
358 | }; |
359 | |
360 | static const char value_name[] = "value" ; |
361 | static bool has_annotation(const InstanceKlass* ik, const Symbol* annotation_type, bool& value) { |
362 | assert(annotation_type != NULL, "invariant" ); |
363 | AnnotationArray* class_annotations = ik->class_annotations(); |
364 | if (class_annotations == NULL) { |
365 | return false; |
366 | } |
367 | |
368 | const AnnotationIterator annotation_iterator(ik, class_annotations); |
369 | while (annotation_iterator.has_next()) { |
370 | annotation_iterator.move_to_next(); |
371 | if (annotation_iterator.type() == annotation_type) { |
372 | // target annotation found |
373 | static const Symbol* value_symbol = |
374 | SymbolTable::probe(value_name, sizeof value_name - 1); |
375 | assert(value_symbol != NULL, "invariant" ); |
376 | const AnnotationElementIterator element_iterator = annotation_iterator.elements(); |
377 | while (element_iterator.has_next()) { |
378 | element_iterator.move_to_next(); |
379 | if (value_symbol == element_iterator.name()) { |
380 | // "value" element |
381 | assert('Z' == element_iterator.value_type(), "invariant" ); |
382 | value = element_iterator.read_bool(); |
383 | return true; |
384 | } |
385 | } |
386 | } |
387 | } |
388 | return false; |
389 | } |
390 | |
391 | // Evaluate to the value of the first found Symbol* annotation type. |
392 | // Searching moves upwards in the klass hierarchy in order to support |
393 | // inherited annotations in addition to the ability to override. |
394 | static bool annotation_value(const InstanceKlass* ik, const Symbol* annotation_type, bool& value) { |
395 | assert(ik != NULL, "invariant" ); |
396 | assert(annotation_type != NULL, "invariant" ); |
397 | assert(JdkJfrEvent::is_a(ik), "invariant" ); |
398 | if (has_annotation(ik, annotation_type, value)) { |
399 | return true; |
400 | } |
401 | InstanceKlass* const super = InstanceKlass::cast(ik->super()); |
402 | return super != NULL && JdkJfrEvent::is_a(super) ? annotation_value(super, annotation_type, value) : false; |
403 | } |
404 | |
405 | static const char jdk_jfr_module_name[] = "jdk.jfr" ; |
406 | |
407 | static bool java_base_can_read_jdk_jfr() { |
408 | static bool can_read = false; |
409 | if (can_read) { |
410 | return true; |
411 | } |
412 | static Symbol* jdk_jfr_module_symbol = NULL; |
413 | if (jdk_jfr_module_symbol == NULL) { |
414 | jdk_jfr_module_symbol = SymbolTable::probe(jdk_jfr_module_name, sizeof jdk_jfr_module_name - 1); |
415 | if (jdk_jfr_module_symbol == NULL) { |
416 | return false; |
417 | } |
418 | } |
419 | assert(jdk_jfr_module_symbol != NULL, "invariant" ); |
420 | ModuleEntryTable* const table = Modules::get_module_entry_table(Handle()); |
421 | assert(table != NULL, "invariant" ); |
422 | const ModuleEntry* const java_base_module = table->javabase_moduleEntry(); |
423 | if (java_base_module == NULL) { |
424 | return false; |
425 | } |
426 | assert(java_base_module != NULL, "invariant" ); |
427 | ModuleEntry* const jdk_jfr_module = table->lookup_only(jdk_jfr_module_symbol); |
428 | if (jdk_jfr_module == NULL) { |
429 | return false; |
430 | } |
431 | assert(jdk_jfr_module != NULL, "invariant" ); |
432 | if (java_base_module->can_read(jdk_jfr_module)) { |
433 | can_read = true; |
434 | } |
435 | return can_read; |
436 | } |
437 | |
438 | static const char registered_constant[] = "Ljdk/jfr/Registered;" ; |
439 | |
440 | // Evaluate to the value of the first found "Ljdk/jfr/Registered;" annotation. |
441 | // Searching moves upwards in the klass hierarchy in order to support |
442 | // inherited annotations in addition to the ability to override. |
443 | static bool should_register_klass(const InstanceKlass* ik, bool& untypedEventHandler) { |
444 | assert(ik != NULL, "invariant" ); |
445 | assert(JdkJfrEvent::is_a(ik), "invariant" ); |
446 | assert(!untypedEventHandler, "invariant" ); |
447 | static const Symbol* registered_symbol = NULL; |
448 | if (registered_symbol == NULL) { |
449 | registered_symbol = SymbolTable::probe(registered_constant, sizeof registered_constant - 1); |
450 | if (registered_symbol == NULL) { |
451 | untypedEventHandler = true; |
452 | return false; |
453 | } |
454 | } |
455 | assert(registered_symbol != NULL, "invariant" ); |
456 | bool value = false; // to be set by annotation_value |
457 | untypedEventHandler = !(annotation_value(ik, registered_symbol, value) || java_base_can_read_jdk_jfr()); |
458 | return value; |
459 | } |
460 | |
461 | /* |
462 | * Map an utf8 constant back to its CONSTANT_UTF8_INFO |
463 | */ |
464 | static u2 utf8_info_index(const InstanceKlass* ik, const Symbol* const target, TRAPS) { |
465 | assert(target != NULL, "invariant" ); |
466 | const ConstantPool* cp = ik->constants(); |
467 | const int cp_len = cp->length(); |
468 | for (u2 index = 1; index < cp_len; ++index) { |
469 | const constantTag tag = cp->tag_at(index); |
470 | if (tag.is_utf8()) { |
471 | const Symbol* const utf8_sym = cp->symbol_at(index); |
472 | assert(utf8_sym != NULL, "invariant" ); |
473 | if (utf8_sym == target) { |
474 | return index; |
475 | } |
476 | } |
477 | } |
478 | // not in constant pool |
479 | return invalid_cp_index; |
480 | } |
481 | |
482 | #ifdef ASSERT |
483 | static bool is_index_within_range(u2 index, u2 orig_cp_len, u2 new_cp_entries_len) { |
484 | return index > 0 && index < orig_cp_len + new_cp_entries_len; |
485 | } |
486 | #endif |
487 | |
488 | static u2 add_utf8_info(JfrBigEndianWriter& writer, const char* utf8_constant, u2 orig_cp_len, u2& new_cp_entries_len) { |
489 | assert(utf8_constant != NULL, "invariant" ); |
490 | writer.write<u1>(JVM_CONSTANT_Utf8); |
491 | writer.write_utf8_u2_len(utf8_constant); |
492 | assert(writer.is_valid(), "invariant" ); |
493 | // return index for the added utf8 info |
494 | return orig_cp_len + new_cp_entries_len++; |
495 | } |
496 | |
497 | static u2 add_method_ref_info(JfrBigEndianWriter& writer, |
498 | u2 cls_name_index, |
499 | u2 method_index, |
500 | u2 desc_index, |
501 | u2 orig_cp_len, |
502 | u2& number_of_new_constants, |
503 | TRAPS) { |
504 | assert(cls_name_index != invalid_cp_index, "invariant" ); |
505 | assert(method_index != invalid_cp_index, "invariant" ); |
506 | assert(desc_index != invalid_cp_index, "invariant" ); |
507 | assert(is_index_within_range(cls_name_index, orig_cp_len, number_of_new_constants), "invariant" ); |
508 | assert(is_index_within_range(method_index, orig_cp_len, number_of_new_constants), "invariant" ); |
509 | assert(is_index_within_range(desc_index, orig_cp_len, number_of_new_constants), "invariant" ); |
510 | writer.write<u1>(JVM_CONSTANT_Class); |
511 | writer.write<u2>(cls_name_index); |
512 | const u2 cls_entry_index = orig_cp_len + number_of_new_constants; |
513 | ++number_of_new_constants; |
514 | writer.write<u1>(JVM_CONSTANT_NameAndType); |
515 | writer.write<u2>(method_index); |
516 | writer.write<u2>(desc_index); |
517 | const u2 nat_entry_index = orig_cp_len + number_of_new_constants; |
518 | ++number_of_new_constants; |
519 | writer.write<u1>(JVM_CONSTANT_Methodref); |
520 | writer.write<u2>(cls_entry_index); |
521 | writer.write<u2>(nat_entry_index); |
522 | // post-increment number_of_new_constants |
523 | // value returned is the index to the added method_ref |
524 | return orig_cp_len + number_of_new_constants++; |
525 | } |
526 | |
527 | static u2 add_flr_register_method_constants(JfrBigEndianWriter& writer, |
528 | const u2* utf8_indexes, |
529 | u2 orig_cp_len, |
530 | u2& number_of_new_constants, |
531 | TRAPS) { |
532 | assert(utf8_indexes != NULL, "invariant" ); |
533 | return add_method_ref_info(writer, |
534 | utf8_indexes[UTF8_OPT_FlightRecorder], |
535 | utf8_indexes[UTF8_OPT_register], |
536 | utf8_indexes[UTF8_OPT_CLASS_VOID_METHOD_DESC], |
537 | orig_cp_len, |
538 | number_of_new_constants, |
539 | THREAD); |
540 | } |
541 | |
542 | /* |
543 | * field_info { |
544 | * u2 access_flags; |
545 | * u2 name_index; |
546 | * u2 descriptor_index; |
547 | * u2 attributes_count; |
548 | * attribute_info attributes[attributes_count]; |
549 | * } |
550 | */ |
551 | static jlong add_field_info(JfrBigEndianWriter& writer, u2 name_index, u2 desc_index, bool is_static = false) { |
552 | assert(name_index != invalid_cp_index, "invariant" ); |
553 | assert(desc_index != invalid_cp_index, "invariant" ); |
554 | DEBUG_ONLY(const jlong start_offset = writer.current_offset();) |
555 | writer.write<u2>(JVM_ACC_SYNTHETIC | JVM_ACC_PRIVATE | (is_static ? JVM_ACC_STATIC : JVM_ACC_TRANSIENT)); // flags |
556 | writer.write(name_index); |
557 | writer.write(desc_index); |
558 | writer.write((u2)0x0); // attributes_count |
559 | assert(writer.is_valid(), "invariant" ); |
560 | DEBUG_ONLY(assert(start_offset + 8 == writer.current_offset(), "invariant" );) |
561 | return writer.current_offset(); |
562 | } |
563 | |
564 | static u2 add_field_infos(JfrBigEndianWriter& writer, const u2* utf8_indexes, bool untypedEventHandler) { |
565 | assert(utf8_indexes != NULL, "invariant" ); |
566 | add_field_info(writer, |
567 | utf8_indexes[UTF8_REQ_eventHandler], |
568 | untypedEventHandler ? utf8_indexes[UTF8_OPT_LjavaLangObject] : utf8_indexes[UTF8_OPT_eventHandler_FIELD_DESC], |
569 | true); // static |
570 | |
571 | add_field_info(writer, |
572 | utf8_indexes[UTF8_REQ_startTime], |
573 | utf8_indexes[UTF8_REQ_J_FIELD_DESC]); |
574 | |
575 | add_field_info(writer, |
576 | utf8_indexes[UTF8_REQ_duration], |
577 | utf8_indexes[UTF8_REQ_J_FIELD_DESC]); |
578 | |
579 | return number_of_new_fields; |
580 | } |
581 | |
582 | /* |
583 | * method_info { |
584 | * u2 access_flags; |
585 | * u2 name_index; |
586 | * u2 descriptor_index; |
587 | * u2 attributes_count; |
588 | * attribute_info attributes[attributes_count]; |
589 | * } |
590 | * |
591 | * Code_attribute { |
592 | * u2 attribute_name_index; |
593 | * u4 attribute_length; |
594 | * u2 max_stack; |
595 | * u2 max_locals; |
596 | * u4 code_length; |
597 | * u1 code[code_length]; |
598 | * u2 exception_table_length; |
599 | * { u2 start_pc; |
600 | * u2 end_pc; |
601 | * u2 handler_pc; |
602 | * u2 catch_type; |
603 | * } exception_table[exception_table_length]; |
604 | * u2 attributes_count; |
605 | * attribute_info attributes[attributes_count]; |
606 | * } |
607 | */ |
608 | |
609 | static jlong add_method_info(JfrBigEndianWriter& writer, |
610 | u2 name_index, |
611 | u2 desc_index, |
612 | u2 code_index, |
613 | const u1* const code, |
614 | const size_t code_len) { |
615 | assert(name_index > 0, "invariant" ); |
616 | assert(desc_index > 0, "invariant" ); |
617 | assert(code_index > 0, "invariant" ); |
618 | DEBUG_ONLY(const jlong start_offset = writer.current_offset();) |
619 | writer.write<u2>(JVM_ACC_SYNTHETIC | JVM_ACC_PUBLIC); // flags |
620 | writer.write(name_index); |
621 | writer.write(desc_index); |
622 | writer.write<u2>(0x1); // attributes_count ; 1 for "Code" attribute |
623 | assert(writer.is_valid(), "invariant" ); |
624 | DEBUG_ONLY(assert(start_offset + 8 == writer.current_offset(), "invariant" );) |
625 | // Code attribute |
626 | writer.write(code_index); // "Code" |
627 | writer.bytes(code, code_len); |
628 | DEBUG_ONLY(assert((start_offset + 8 + 2 + (jlong)code_len) == writer.current_offset(), "invariant" );) |
629 | return writer.current_offset(); |
630 | } |
631 | |
632 | /* |
633 | * On return, the passed stream will be positioned |
634 | * just after the constant pool section in the classfile |
635 | * and the cp length is returned. |
636 | * |
637 | * Stream should come in at the start position. |
638 | */ |
639 | static u2 position_stream_after_cp(const ClassFileStream* stream) { |
640 | assert(stream != NULL, "invariant" ); |
641 | assert(stream->current_offset() == 0, "invariant" ); |
642 | stream->skip_u4_fast(2); // 8 bytes skipped |
643 | const u2 cp_len = stream->get_u2_fast(); |
644 | assert(cp_len > 0, "invariant" ); |
645 | // now spin the stream position to just after the constant pool |
646 | for (u2 index = 1; index < cp_len; ++index) { |
647 | const u1 tag = stream->get_u1_fast(); // cp tag |
648 | switch (tag) { |
649 | case JVM_CONSTANT_Class: |
650 | case JVM_CONSTANT_String: { |
651 | stream->skip_u2_fast(1); // skip 2 bytes |
652 | continue; |
653 | } |
654 | case JVM_CONSTANT_Fieldref: |
655 | case JVM_CONSTANT_Methodref: |
656 | case JVM_CONSTANT_InterfaceMethodref: |
657 | case JVM_CONSTANT_Integer: |
658 | case JVM_CONSTANT_Float: |
659 | case JVM_CONSTANT_NameAndType: |
660 | case JVM_CONSTANT_InvokeDynamic: { |
661 | stream->skip_u4_fast(1); // skip 4 bytes |
662 | continue; |
663 | } |
664 | case JVM_CONSTANT_Long: |
665 | case JVM_CONSTANT_Double: { |
666 | stream->skip_u4_fast(2); // skip 8 bytes |
667 | // Skip entry following eigth-byte constant, see JVM book p. 98 |
668 | ++index; |
669 | continue; |
670 | } |
671 | case JVM_CONSTANT_Utf8: { |
672 | u2 utf8_length = stream->get_u2_fast(); |
673 | stream->skip_u1_fast(utf8_length); // skip 2 + len bytes |
674 | continue; |
675 | } |
676 | case JVM_CONSTANT_MethodHandle: |
677 | case JVM_CONSTANT_MethodType: { |
678 | if (tag == JVM_CONSTANT_MethodHandle) { |
679 | stream->skip_u1_fast(1); |
680 | stream->skip_u2_fast(1); // skip 3 bytes |
681 | } |
682 | else if (tag == JVM_CONSTANT_MethodType) { |
683 | stream->skip_u2_fast(1); // skip 3 bytes |
684 | } |
685 | } |
686 | continue; |
687 | default: |
688 | assert(false, "error in skip logic!" ); |
689 | break; |
690 | } // end switch(tag) |
691 | } |
692 | return cp_len; |
693 | } |
694 | |
695 | /* |
696 | * On return, the passed stream will be positioned |
697 | * just after the fields section in the classfile |
698 | * and the number of fields will be returned. |
699 | * |
700 | * Stream should come in positioned just before fields_count |
701 | */ |
702 | static u2 position_stream_after_fields(const ClassFileStream* stream) { |
703 | assert(stream != NULL, "invariant" ); |
704 | assert(stream->current_offset() > 0, "invariant" ); |
705 | // fields len |
706 | const u2 orig_fields_len = stream->get_u2_fast(); |
707 | // fields |
708 | for (u2 i = 0; i < orig_fields_len; ++i) { |
709 | stream->skip_u2_fast(3); |
710 | const u2 attrib_info_len = stream->get_u2_fast(); |
711 | for (u2 j = 0; j < attrib_info_len; ++j) { |
712 | stream->skip_u2_fast(1); |
713 | const u4 attrib_len = stream->get_u4_fast(); |
714 | stream->skip_u1_fast(attrib_len); |
715 | } |
716 | } |
717 | return orig_fields_len; |
718 | } |
719 | |
720 | /* |
721 | * On return, the passed stream will be positioned |
722 | * just after the methods section in the classfile |
723 | * and the number of methods will be returned. |
724 | * |
725 | * Stream should come in positioned just before methods_count |
726 | */ |
727 | static u2 position_stream_after_methods(JfrBigEndianWriter& writer, |
728 | const ClassFileStream* stream, |
729 | const u2* utf8_indexes, |
730 | bool register_klass, |
731 | const Method* clinit_method, |
732 | u4& orig_method_len_offset) { |
733 | assert(stream != NULL, "invariant" ); |
734 | assert(stream->current_offset() > 0, "invariant" ); |
735 | assert(utf8_indexes != NULL, "invariant" ); |
736 | // We will come back to this location when we |
737 | // know how many methods there will be. |
738 | writer.reserve(sizeof(u2)); |
739 | const u2 orig_methods_len = stream->get_u2_fast(); |
740 | // Move copy position past original method_count |
741 | // in order to not copy the original count |
742 | orig_method_len_offset += sizeof(u2); |
743 | for (u2 i = 0; i < orig_methods_len; ++i) { |
744 | const u4 method_offset = stream->current_offset(); |
745 | stream->skip_u2_fast(1); // Access Flags |
746 | const u2 name_index = stream->get_u2_fast(); // Name index |
747 | stream->skip_u2_fast(1); // Descriptor index |
748 | const u2 attributes_count = stream->get_u2_fast(); |
749 | for (u2 j = 0; j < attributes_count; ++j) { |
750 | stream->skip_u2_fast(1); |
751 | const u4 attrib_len = stream->get_u4_fast(); |
752 | stream->skip_u1_fast(attrib_len); |
753 | } |
754 | if (clinit_method != NULL && name_index == clinit_method->name_index()) { |
755 | // The method just parsed is an existing <clinit> method. |
756 | // If the class has the @Registered(false) annotation, i.e. marking a class |
757 | // for opting out from automatic registration, then we do not need to do anything. |
758 | if (!register_klass) { |
759 | continue; |
760 | } |
761 | // Automatic registration with the jfr system is acccomplished |
762 | // by pre-pending code to the <clinit> method of the class. |
763 | // We will need to re-create a new <clinit> in a later step. |
764 | // For now, ensure that this method is excluded from the methods |
765 | // being copied. |
766 | writer.bytes(stream->buffer() + orig_method_len_offset, |
767 | method_offset - orig_method_len_offset); |
768 | assert(writer.is_valid(), "invariant" ); |
769 | |
770 | // Update copy position to skip copy of <clinit> method |
771 | orig_method_len_offset = stream->current_offset(); |
772 | } |
773 | } |
774 | return orig_methods_len; |
775 | } |
776 | |
777 | static u2 add_method_infos(JfrBigEndianWriter& writer, const u2* utf8_indexes) { |
778 | assert(utf8_indexes != NULL, "invariant" ); |
779 | add_method_info(writer, |
780 | utf8_indexes[UTF8_REQ_begin], |
781 | utf8_indexes[UTF8_REQ_EMPTY_VOID_METHOD_DESC], |
782 | utf8_indexes[UTF8_REQ_Code], |
783 | empty_void_method_code_attribute, |
784 | sizeof(empty_void_method_code_attribute)); |
785 | |
786 | assert(writer.is_valid(), "invariant" ); |
787 | |
788 | add_method_info(writer, |
789 | utf8_indexes[UTF8_REQ_end], |
790 | utf8_indexes[UTF8_REQ_EMPTY_VOID_METHOD_DESC], |
791 | utf8_indexes[UTF8_REQ_Code], |
792 | empty_void_method_code_attribute, |
793 | sizeof(empty_void_method_code_attribute)); |
794 | |
795 | assert(writer.is_valid(), "invariant" ); |
796 | |
797 | add_method_info(writer, |
798 | utf8_indexes[UTF8_REQ_commit], |
799 | utf8_indexes[UTF8_REQ_EMPTY_VOID_METHOD_DESC], |
800 | utf8_indexes[UTF8_REQ_Code], |
801 | empty_void_method_code_attribute, |
802 | sizeof(empty_void_method_code_attribute)); |
803 | |
804 | assert(writer.is_valid(), "invariant" ); |
805 | |
806 | add_method_info(writer, |
807 | utf8_indexes[UTF8_REQ_isEnabled], |
808 | utf8_indexes[UTF8_REQ_EMPTY_BOOLEAN_METHOD_DESC], |
809 | utf8_indexes[UTF8_REQ_Code], |
810 | boolean_method_code_attribute, |
811 | sizeof(boolean_method_code_attribute)); |
812 | |
813 | assert(writer.is_valid(), "invariant" ); |
814 | |
815 | add_method_info(writer, |
816 | utf8_indexes[UTF8_REQ_shouldCommit], |
817 | utf8_indexes[UTF8_REQ_EMPTY_BOOLEAN_METHOD_DESC], |
818 | utf8_indexes[UTF8_REQ_Code], |
819 | boolean_method_code_attribute, |
820 | sizeof(boolean_method_code_attribute)); |
821 | assert(writer.is_valid(), "invariant" ); |
822 | return number_of_new_methods; |
823 | } |
824 | |
825 | static void adjust_exception_table(JfrBigEndianWriter& writer, u2 bci_adjustment_offset, const Method* method, TRAPS) { |
826 | const u2 ex_table_length = method != NULL ? (u2)method->exception_table_length() : 0; |
827 | writer.write<u2>(ex_table_length); // Exception table length |
828 | if (ex_table_length > 0) { |
829 | assert(method != NULL, "invariant" ); |
830 | const ExceptionTableElement* const ex_elements = method->exception_table_start(); |
831 | for (int i = 0; i < ex_table_length; ++i) { |
832 | assert(ex_elements != NULL, "invariant" ); |
833 | writer.write<u2>(ex_elements[i].start_pc + bci_adjustment_offset); |
834 | writer.write<u2>(ex_elements[i].end_pc + bci_adjustment_offset); |
835 | writer.write<u2>(ex_elements[i].handler_pc + bci_adjustment_offset); |
836 | writer.write<u2>(ex_elements[i].catch_type_index); // no adjustment |
837 | } |
838 | } |
839 | } |
840 | |
841 | enum StackMapFrameTypes { |
842 | SAME_FRAME_BEGIN = 0, |
843 | SAME_FRAME_END = 63, |
844 | SAME_LOCALS_1_STACK_ITEM_FRAME_BEGIN = 64, |
845 | SAME_LOCALS_1_STACK_ITEM_FRAME_END = 127, |
846 | SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED = 247, |
847 | CHOP_FRAME_BEGIN = 248, |
848 | CHOP_FRAME_END = 250, |
849 | SAME_FRAME_EXTENDED = 251, |
850 | APPEND_FRAME_BEGIN = 252, |
851 | APPEND_FRAME_END = 254, |
852 | FULL_FRAME = 255 |
853 | }; |
854 | |
855 | static void adjust_stack_map(JfrBigEndianWriter& writer, |
856 | Array<u1>* stack_map, |
857 | const u2* utf8_indexes, |
858 | u2 bci_adjustment_offset, |
859 | TRAPS) { |
860 | assert(stack_map != NULL, "invariant" ); |
861 | assert(utf8_indexes != NULL, "invariant" ); |
862 | writer.write<u2>(utf8_indexes[UTF8_OPT_StackMapTable]); |
863 | const jlong stack_map_attrib_len_offset = writer.current_offset(); |
864 | writer.reserve(sizeof(u4)); |
865 | StackMapStream stream(stack_map); |
866 | const u2 stack_map_entries = stream.get_u2(THREAD); |
867 | // number of entries |
868 | writer.write<u2>(stack_map_entries); // new stack map entry added |
869 | const u1 frame_type = stream.get_u1(THREAD); |
870 | // SAME_FRAME and SAME_LOCALS_1_STACK_ITEM_FRAME encode |
871 | // their offset_delta into the actual frame type itself. |
872 | // If such a frame type is the first frame, then we transform |
873 | // it to a SAME_FRAME_EXTENDED or a SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED frame. |
874 | // This is done in order to not overflow frame types accidentally |
875 | // when adjusting the offset_delta. In changing the frame types, |
876 | // we can work with an explicit u2 offset_delta field (like the other frame types) |
877 | if (frame_type <= SAME_FRAME_END) { |
878 | writer.write<u1>(SAME_FRAME_EXTENDED); |
879 | writer.write<u2>(frame_type + bci_adjustment_offset); |
880 | } else if (frame_type >= SAME_LOCALS_1_STACK_ITEM_FRAME_BEGIN && |
881 | frame_type <= SAME_LOCALS_1_STACK_ITEM_FRAME_END) { |
882 | writer.write<u1>(SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED); |
883 | writer.write<u2>((frame_type - SAME_LOCALS_1_STACK_ITEM_FRAME_BEGIN) + bci_adjustment_offset); |
884 | } else if (frame_type >= SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { |
885 | // SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED to FULL_FRAME |
886 | // has a u2 offset_delta field |
887 | writer.write<u1>(frame_type); |
888 | writer.write<u2>(stream.get_u2(THREAD) + bci_adjustment_offset); |
889 | } else { |
890 | assert(false, "stackMapFrame type is invalid" ); |
891 | } |
892 | |
893 | while (!stream.at_end()) { |
894 | writer.write<u1>(stream.get_u1(THREAD)); |
895 | } |
896 | |
897 | u4 stack_map_attrib_len = writer.current_offset() - stack_map_attrib_len_offset; |
898 | // the stack_map_table_attributes_length value is exclusive |
899 | stack_map_attrib_len -= sizeof(u4); |
900 | writer.write_at_offset(stack_map_attrib_len, stack_map_attrib_len_offset); |
901 | } |
902 | |
903 | static void adjust_line_number_table(JfrBigEndianWriter& writer, |
904 | const u2* utf8_indexes, |
905 | u4 bci_adjustement_offset, |
906 | const Method* method, |
907 | TRAPS) { |
908 | assert(utf8_indexes != NULL, "invariant" ); |
909 | assert(method != NULL, "invariant" ); |
910 | assert(method->has_linenumber_table(), "invariant" ); |
911 | writer.write(utf8_indexes[UTF8_OPT_LineNumberTable]); |
912 | const jlong lnt_attributes_length_offset = writer.current_offset(); |
913 | writer.reserve(sizeof(u4)); |
914 | const jlong lnt_attributes_entries_offset = writer.current_offset(); |
915 | writer.reserve(sizeof(u2)); |
916 | u1* lnt = method->compressed_linenumber_table(); |
917 | CompressedLineNumberReadStream lnt_stream(lnt); |
918 | u2 line_number_table_entries = 0; |
919 | while (lnt_stream.read_pair()) { |
920 | ++line_number_table_entries; |
921 | const u2 bci = (u2)lnt_stream.bci(); |
922 | writer.write<u2>(bci + (u2)bci_adjustement_offset); |
923 | writer.write<u2>((u2)lnt_stream.line()); |
924 | } |
925 | writer.write_at_offset(line_number_table_entries, lnt_attributes_entries_offset); |
926 | u4 lnt_table_attributes_len = writer.current_offset() - lnt_attributes_length_offset; |
927 | // the line_number_table_attributes_length value is exclusive |
928 | lnt_table_attributes_len -= sizeof(u4); |
929 | writer.write_at_offset(lnt_table_attributes_len, lnt_attributes_length_offset); |
930 | } |
931 | |
932 | // returns the number of lvtt entries |
933 | static u2 adjust_local_variable_table(JfrBigEndianWriter& writer, |
934 | const u2* utf8_indexes, |
935 | u2 bci_adjustment_offset, |
936 | const Method* method, |
937 | TRAPS) { |
938 | assert(utf8_indexes != NULL, "invariant" ); |
939 | assert(method != NULL, "invariant" ); |
940 | assert(method->has_localvariable_table(), "invariant" ); |
941 | writer.write<u2>(utf8_indexes[UTF8_OPT_LocalVariableTable]); |
942 | const jlong lvt_attributes_length_offset = writer.current_offset(); |
943 | writer.reserve(sizeof(u4)); |
944 | const int lvt_len = method->localvariable_table_length(); |
945 | writer.write<u2>((u2)lvt_len); |
946 | const LocalVariableTableElement* table = method->localvariable_table_start(); |
947 | assert(table != NULL, "invariant" ); |
948 | u2 num_lvtt_entries = 0; |
949 | for (int i = 0; i < lvt_len; ++i) { |
950 | writer.write<u2>(table[i].start_bci + bci_adjustment_offset); |
951 | writer.write<u2>(table[i].length); |
952 | writer.write<u2>(table[i].name_cp_index); |
953 | writer.write<u2>(table[i].descriptor_cp_index); |
954 | writer.write<u2>(table[i].slot); |
955 | if (table[i].signature_cp_index > 0) { |
956 | ++num_lvtt_entries; |
957 | } |
958 | } |
959 | u4 lvt_table_attributes_len = writer.current_offset() - lvt_attributes_length_offset; |
960 | // the lvt_table_attributes_length value is exclusive |
961 | lvt_table_attributes_len -= sizeof(u4); |
962 | writer.write_at_offset(lvt_table_attributes_len, lvt_attributes_length_offset); |
963 | return num_lvtt_entries; |
964 | } |
965 | |
966 | static void adjust_local_variable_type_table(JfrBigEndianWriter& writer, |
967 | const u2* utf8_indexes, |
968 | u2 bci_adjustment_offset, |
969 | u2 num_lvtt_entries, |
970 | const Method* method, |
971 | TRAPS) { |
972 | assert(num_lvtt_entries > 0, "invariant" ); |
973 | writer.write<u2>(utf8_indexes[UTF8_OPT_LocalVariableTypeTable]); |
974 | const jlong lvtt_attributes_length_offset = writer.current_offset(); |
975 | writer.reserve(sizeof(u4)); |
976 | writer.write<u2>(num_lvtt_entries); |
977 | const LocalVariableTableElement* table = method->localvariable_table_start(); |
978 | assert(table != NULL, "invariant" ); |
979 | const int lvt_len = method->localvariable_table_length(); |
980 | for (int i = 0; i < lvt_len; ++i) { |
981 | if (table[i].signature_cp_index > 0) { |
982 | writer.write<u2>(table[i].start_bci + bci_adjustment_offset); |
983 | writer.write<u2>(table[i].length); |
984 | writer.write<u2>(table[i].name_cp_index); |
985 | writer.write<u2>(table[i].signature_cp_index); |
986 | writer.write<u2>(table[i].slot); |
987 | } |
988 | } |
989 | u4 lvtt_table_attributes_len = writer.current_offset() - lvtt_attributes_length_offset; |
990 | // the lvtt_table_attributes_length value is exclusive |
991 | lvtt_table_attributes_len -= sizeof(u4); |
992 | writer.write_at_offset(lvtt_table_attributes_len, lvtt_attributes_length_offset); |
993 | } |
994 | |
995 | static void adjust_code_attributes(JfrBigEndianWriter& writer, |
996 | const u2* utf8_indexes, |
997 | u2 bci_adjustment_offset, |
998 | const Method* clinit_method, |
999 | TRAPS) { |
1000 | // "Code" attributes |
1001 | assert(utf8_indexes != NULL, "invariant" ); |
1002 | const jlong code_attributes_offset = writer.current_offset(); |
1003 | writer.reserve(sizeof(u2)); |
1004 | u2 number_of_code_attributes = 0; |
1005 | if (clinit_method != NULL) { |
1006 | Array<u1>* stack_map = clinit_method->stackmap_data(); |
1007 | if (stack_map != NULL) { |
1008 | ++number_of_code_attributes; |
1009 | adjust_stack_map(writer, stack_map, utf8_indexes, bci_adjustment_offset, THREAD); |
1010 | assert(writer.is_valid(), "invariant" ); |
1011 | } |
1012 | if (clinit_method != NULL && clinit_method->has_linenumber_table()) { |
1013 | ++number_of_code_attributes; |
1014 | adjust_line_number_table(writer, utf8_indexes, bci_adjustment_offset, clinit_method, THREAD); |
1015 | assert(writer.is_valid(), "invariant" ); |
1016 | } |
1017 | if (clinit_method != NULL && clinit_method->has_localvariable_table()) { |
1018 | ++number_of_code_attributes; |
1019 | const u2 num_of_lvtt_entries = adjust_local_variable_table(writer, utf8_indexes, bci_adjustment_offset, clinit_method, THREAD); |
1020 | assert(writer.is_valid(), "invariant" ); |
1021 | if (num_of_lvtt_entries > 0) { |
1022 | ++number_of_code_attributes; |
1023 | adjust_local_variable_type_table(writer, utf8_indexes, bci_adjustment_offset, num_of_lvtt_entries, clinit_method, THREAD); |
1024 | assert(writer.is_valid(), "invariant" ); |
1025 | } |
1026 | } |
1027 | } |
1028 | |
1029 | // Store the number of code_attributes |
1030 | writer.write_at_offset(number_of_code_attributes, code_attributes_offset); |
1031 | } |
1032 | |
1033 | static jlong insert_clinit_method(const InstanceKlass* ik, |
1034 | const ClassFileParser& parser, |
1035 | JfrBigEndianWriter& writer, |
1036 | u2 orig_constant_pool_len, |
1037 | const u2* utf8_indexes, |
1038 | const u2 register_method_ref_index, |
1039 | const Method* clinit_method, |
1040 | TRAPS) { |
1041 | assert(utf8_indexes != NULL, "invariant" ); |
1042 | // The injected code length is always this value. |
1043 | // This is to ensure that padding can be done |
1044 | // where needed and to simplify size calculations. |
1045 | static const u2 injected_code_length = 8; |
1046 | const u2 name_index = utf8_indexes[UTF8_OPT_clinit]; |
1047 | assert(name_index != invalid_cp_index, "invariant" ); |
1048 | const u2 desc_index = utf8_indexes[UTF8_REQ_EMPTY_VOID_METHOD_DESC]; |
1049 | const u2 max_stack = MAX2(clinit_method != NULL ? clinit_method->verifier_max_stack() : 1, 1); |
1050 | const u2 max_locals = MAX2(clinit_method != NULL ? clinit_method->max_locals() : 0, 0); |
1051 | const u2 orig_bytecodes_length = clinit_method != NULL ? (u2)clinit_method->code_size() : 0; |
1052 | const address orig_bytecodes = clinit_method != NULL ? clinit_method->code_base() : NULL; |
1053 | const u2 new_code_length = injected_code_length + orig_bytecodes_length; |
1054 | DEBUG_ONLY(const jlong start_offset = writer.current_offset();) |
1055 | writer.write<u2>(JVM_ACC_STATIC); // flags |
1056 | writer.write<u2>(name_index); |
1057 | writer.write<u2>(desc_index); |
1058 | writer.write<u2>((u2)0x1); // attributes_count // "Code" |
1059 | assert(writer.is_valid(), "invariant" ); |
1060 | DEBUG_ONLY(assert(start_offset + 8 == writer.current_offset(), "invariant" );) |
1061 | // "Code" attribute |
1062 | writer.write<u2>(utf8_indexes[UTF8_REQ_Code]); // "Code" |
1063 | const jlong code_attribute_length_offset = writer.current_offset(); |
1064 | writer.reserve(sizeof(u4)); |
1065 | writer.write<u2>(max_stack); // max stack |
1066 | writer.write<u2>(max_locals); // max locals |
1067 | writer.write<u4>((u4)new_code_length); // code length |
1068 | |
1069 | /* BEGIN CLINIT CODE */ |
1070 | |
1071 | // Note the use of ldc_w here instead of ldc. |
1072 | // This is to handle all values of "this_class_index" |
1073 | writer.write<u1>((u1)Bytecodes::_ldc_w); |
1074 | writer.write<u2>((u2)parser.this_class_index()); // load constant "this class" |
1075 | writer.write<u1>((u1)Bytecodes::_invokestatic); |
1076 | // invoke "FlightRecorder.register(Ljava/lang/Class;") |
1077 | writer.write<u2>(register_method_ref_index); |
1078 | if (clinit_method == NULL) { |
1079 | writer.write<u1>((u1)Bytecodes::_nop); |
1080 | writer.write<u1>((u1)Bytecodes::_return); |
1081 | } else { |
1082 | // If we are pre-pending to original code, |
1083 | // do padding to minimize disruption to the original. |
1084 | // It might have dependencies on 4-byte boundaries |
1085 | // i.e. lookupswitch and tableswitch instructions |
1086 | writer.write<u1>((u1)Bytecodes::_nop); |
1087 | writer.write<u1>((u1)Bytecodes::_nop); |
1088 | // insert original clinit code |
1089 | writer.bytes(orig_bytecodes, orig_bytecodes_length); |
1090 | } |
1091 | |
1092 | /* END CLINIT CODE */ |
1093 | |
1094 | assert(writer.is_valid(), "invariant" ); |
1095 | adjust_exception_table(writer, injected_code_length, clinit_method, THREAD); |
1096 | assert(writer.is_valid(), "invariant" ); |
1097 | adjust_code_attributes(writer, utf8_indexes, injected_code_length, clinit_method, THREAD); |
1098 | assert(writer.is_valid(), "invariant" ); |
1099 | u4 code_attribute_len = writer.current_offset() - code_attribute_length_offset; |
1100 | // the code_attribute_length value is exclusive |
1101 | code_attribute_len -= sizeof(u4); |
1102 | writer.write_at_offset(code_attribute_len, code_attribute_length_offset); |
1103 | return writer.current_offset(); |
1104 | } |
1105 | |
1106 | // Caller needs ResourceMark |
1107 | static ClassFileStream* create_new_bytes_for_event_klass(const InstanceKlass* ik, const ClassFileParser& parser, TRAPS) { |
1108 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
1109 | static const u2 public_final_flag_mask = JVM_ACC_PUBLIC | JVM_ACC_FINAL; |
1110 | const ClassFileStream* const orig_stream = parser.clone_stream(); |
1111 | const int orig_stream_length = orig_stream->length(); |
1112 | // allocate an identically sized buffer |
1113 | u1* const new_buffer = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, u1, orig_stream_length); |
1114 | if (new_buffer == NULL) { |
1115 | return NULL; |
1116 | } |
1117 | assert(new_buffer != NULL, "invariant" ); |
1118 | // memcpy the entire [B |
1119 | memcpy(new_buffer, orig_stream->buffer(), orig_stream_length); |
1120 | const u2 orig_cp_len = position_stream_after_cp(orig_stream); |
1121 | assert(orig_cp_len > 0, "invariant" ); |
1122 | assert(orig_stream->current_offset() > 0, "invariant" ); |
1123 | orig_stream->skip_u2_fast(3); // access_flags, this_class_index, super_class_index |
1124 | const u2 iface_len = orig_stream->get_u2_fast(); |
1125 | orig_stream->skip_u2_fast(iface_len); |
1126 | // fields len |
1127 | const u2 orig_fields_len = orig_stream->get_u2_fast(); |
1128 | // fields |
1129 | for (u2 i = 0; i < orig_fields_len; ++i) { |
1130 | orig_stream->skip_u2_fast(3); |
1131 | const u2 attrib_info_len = orig_stream->get_u2_fast(); |
1132 | for (u2 j = 0; j < attrib_info_len; ++j) { |
1133 | orig_stream->skip_u2_fast(1); |
1134 | const u4 attrib_len = orig_stream->get_u4_fast(); |
1135 | orig_stream->skip_u1_fast(attrib_len); |
1136 | } |
1137 | } |
1138 | // methods |
1139 | const u2 orig_methods_len = orig_stream->get_u2_fast(); |
1140 | for (u2 i = 0; i < orig_methods_len; ++i) { |
1141 | const u4 access_flag_offset = orig_stream->current_offset(); |
1142 | const u2 flags = orig_stream->get_u2_fast(); |
1143 | // Rewrite JVM_ACC_FINAL -> JVM_ACC_PUBLIC |
1144 | if (public_final_flag_mask == flags) { |
1145 | JfrBigEndianWriter accessflagsrewriter(new_buffer + access_flag_offset, sizeof(u2)); |
1146 | accessflagsrewriter.write<u2>(JVM_ACC_PUBLIC); |
1147 | assert(accessflagsrewriter.is_valid(), "invariant" ); |
1148 | } |
1149 | orig_stream->skip_u2_fast(2); |
1150 | const u2 attributes_count = orig_stream->get_u2_fast(); |
1151 | for (u2 j = 0; j < attributes_count; ++j) { |
1152 | orig_stream->skip_u2_fast(1); |
1153 | const u4 attrib_len = orig_stream->get_u4_fast(); |
1154 | orig_stream->skip_u1_fast(attrib_len); |
1155 | } |
1156 | } |
1157 | return new ClassFileStream(new_buffer, orig_stream_length, NULL, ClassFileStream::verify); |
1158 | } |
1159 | |
1160 | // Attempt to locate an existing UTF8_INFO mapping the utf8_constant. |
1161 | // If no UTF8_INFO exists, add (append) a new one to the constant pool. |
1162 | static u2 find_or_add_utf8_info(JfrBigEndianWriter& writer, |
1163 | const InstanceKlass* ik, |
1164 | const char* const utf8_constant, |
1165 | u2 orig_cp_len, |
1166 | u2& added_cp_entries, |
1167 | TRAPS) { |
1168 | assert(utf8_constant != NULL, "invariant" ); |
1169 | TempNewSymbol utf8_sym = SymbolTable::new_symbol(utf8_constant); |
1170 | // lookup existing |
1171 | const int utf8_orig_idx = utf8_info_index(ik, utf8_sym, THREAD); |
1172 | if (utf8_orig_idx != invalid_cp_index) { |
1173 | // existing constant pool entry found |
1174 | return utf8_orig_idx; |
1175 | } |
1176 | // no existing match, need to add a new utf8 cp entry |
1177 | assert(invalid_cp_index == utf8_orig_idx, "invariant" ); |
1178 | // add / append new |
1179 | return add_utf8_info(writer, utf8_constant, orig_cp_len, added_cp_entries); |
1180 | } |
1181 | |
1182 | /* |
1183 | * This routine will resolve the required utf8_constants array |
1184 | * to their constant pool indexes (mapping to their UTF8_INFO's) |
1185 | * Only if a constant is actually needed and does not already exist |
1186 | * will it be added. |
1187 | * |
1188 | * The passed in indexes array will be populated with the resolved indexes. |
1189 | * The number of newly added constant pool entries is returned. |
1190 | */ |
1191 | static u2 resolve_utf8_indexes(JfrBigEndianWriter& writer, |
1192 | const InstanceKlass* ik, |
1193 | u2* const utf8_indexes, |
1194 | u2 orig_cp_len, |
1195 | const Method* clinit_method, |
1196 | bool register_klass, |
1197 | bool untypedEventHandler, |
1198 | TRAPS) { |
1199 | assert(utf8_indexes != NULL, "invariant" ); |
1200 | u2 added_cp_entries = 0; |
1201 | // resolve all required symbols |
1202 | for (u2 index = 0; index < NOF_UTF8_REQ_SYMBOLS; ++index) { |
1203 | utf8_indexes[index] = find_or_add_utf8_info(writer, ik, utf8_constants[index], orig_cp_len, added_cp_entries, THREAD); |
1204 | } |
1205 | |
1206 | // resolve optional constants |
1207 | utf8_indexes[UTF8_OPT_eventHandler_FIELD_DESC] = untypedEventHandler ? invalid_cp_index : |
1208 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_eventHandler_FIELD_DESC], orig_cp_len, added_cp_entries, THREAD); |
1209 | |
1210 | utf8_indexes[UTF8_OPT_LjavaLangObject] = untypedEventHandler ? |
1211 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_LjavaLangObject], orig_cp_len, added_cp_entries, THREAD) : invalid_cp_index; |
1212 | |
1213 | if (register_klass) { |
1214 | utf8_indexes[UTF8_OPT_clinit] = |
1215 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_clinit], orig_cp_len, added_cp_entries, THREAD); |
1216 | utf8_indexes[UTF8_OPT_FlightRecorder] = |
1217 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_FlightRecorder], orig_cp_len, added_cp_entries, THREAD); |
1218 | utf8_indexes[UTF8_OPT_register] = |
1219 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_register], orig_cp_len, added_cp_entries, THREAD); |
1220 | utf8_indexes[UTF8_OPT_CLASS_VOID_METHOD_DESC] = |
1221 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_CLASS_VOID_METHOD_DESC], orig_cp_len, added_cp_entries, THREAD); |
1222 | } else { |
1223 | utf8_indexes[UTF8_OPT_clinit] = invalid_cp_index; |
1224 | utf8_indexes[UTF8_OPT_FlightRecorder] = invalid_cp_index; |
1225 | utf8_indexes[UTF8_OPT_register] = invalid_cp_index; |
1226 | utf8_indexes[UTF8_OPT_CLASS_VOID_METHOD_DESC] = invalid_cp_index; |
1227 | } |
1228 | |
1229 | if (clinit_method != NULL && clinit_method->has_stackmap_table()) { |
1230 | utf8_indexes[UTF8_OPT_StackMapTable] = |
1231 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_StackMapTable], orig_cp_len, added_cp_entries, THREAD); |
1232 | } else { |
1233 | utf8_indexes[UTF8_OPT_StackMapTable] = invalid_cp_index; |
1234 | } |
1235 | |
1236 | if (clinit_method != NULL && clinit_method->has_linenumber_table()) { |
1237 | utf8_indexes[UTF8_OPT_LineNumberTable] = |
1238 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_LineNumberTable], orig_cp_len, added_cp_entries, THREAD); |
1239 | } else { |
1240 | utf8_indexes[UTF8_OPT_LineNumberTable] = invalid_cp_index; |
1241 | } |
1242 | |
1243 | if (clinit_method != NULL && clinit_method->has_localvariable_table()) { |
1244 | utf8_indexes[UTF8_OPT_LocalVariableTable] = |
1245 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_LocalVariableTable], orig_cp_len, added_cp_entries, THREAD); |
1246 | utf8_indexes[UTF8_OPT_LocalVariableTypeTable] = |
1247 | find_or_add_utf8_info(writer, ik, utf8_constants[UTF8_OPT_LocalVariableTypeTable], orig_cp_len, added_cp_entries, THREAD); |
1248 | } else { |
1249 | utf8_indexes[UTF8_OPT_LocalVariableTable] = invalid_cp_index; |
1250 | utf8_indexes[UTF8_OPT_LocalVariableTypeTable] = invalid_cp_index; |
1251 | } |
1252 | |
1253 | return added_cp_entries; |
1254 | } |
1255 | |
1256 | static u1* new_bytes_for_lazy_instrumentation(const InstanceKlass* ik, |
1257 | const ClassFileParser& parser, |
1258 | jint& size_of_new_bytes, |
1259 | TRAPS) { |
1260 | assert(ik != NULL, "invariant" ); |
1261 | // If the class already has a clinit method |
1262 | // we need to take that into account |
1263 | const Method* clinit_method = ik->class_initializer(); |
1264 | bool untypedEventHandler = false; |
1265 | const bool register_klass = should_register_klass(ik, untypedEventHandler); |
1266 | const ClassFileStream* const orig_stream = parser.clone_stream(); |
1267 | const int orig_stream_size = orig_stream->length(); |
1268 | assert(orig_stream->current_offset() == 0, "invariant" ); |
1269 | const u2 orig_cp_len = position_stream_after_cp(orig_stream); |
1270 | assert(orig_cp_len > 0, "invariant" ); |
1271 | assert(orig_stream->current_offset() > 0, "invariant" ); |
1272 | // Dimension and allocate a working byte buffer |
1273 | // to be used in building up a modified class [B. |
1274 | const jint new_buffer_size = extra_stream_bytes + orig_stream_size; |
1275 | u1* const new_buffer = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, u1, new_buffer_size); |
1276 | if (new_buffer == NULL) { |
1277 | log_error(jfr, system) ("Thread local allocation (native) for " SIZE_FORMAT |
1278 | " bytes failed in JfrClassAdapter::on_klass_creation" , (size_t)new_buffer_size); |
1279 | return NULL; |
1280 | } |
1281 | assert(new_buffer != NULL, "invariant" ); |
1282 | // [B wrapped in a big endian writer |
1283 | JfrBigEndianWriter writer(new_buffer, new_buffer_size); |
1284 | assert(writer.current_offset() == 0, "invariant" ); |
1285 | const u4 orig_access_flag_offset = orig_stream->current_offset(); |
1286 | // Copy original stream from the beginning up to AccessFlags |
1287 | // This means the original constant pool contents are copied unmodified |
1288 | writer.bytes(orig_stream->buffer(), orig_access_flag_offset); |
1289 | assert(writer.is_valid(), "invariant" ); |
1290 | assert(writer.current_offset() == (intptr_t)orig_access_flag_offset, "invariant" ); // same positions |
1291 | // Our writer now sits just after the last original constant pool entry. |
1292 | // I.e. we are in a good position to append new constant pool entries |
1293 | // This array will contain the resolved indexes |
1294 | // in order to reference UTF8_INFO's needed |
1295 | u2 utf8_indexes[NOF_UTF8_SYMBOLS]; |
1296 | // Resolve_utf8_indexes will be conservative in attempting to |
1297 | // locate an existing UTF8_INFO; it will only append constants |
1298 | // that is absolutely required |
1299 | u2 number_of_new_constants = |
1300 | resolve_utf8_indexes(writer, ik, utf8_indexes, orig_cp_len, clinit_method, register_klass, untypedEventHandler, THREAD); |
1301 | // UTF8_INFO entries now added to the constant pool |
1302 | // In order to invoke a method we would need additional |
1303 | // constants, JVM_CONSTANT_Class, JVM_CONSTANT_NameAndType |
1304 | // and JVM_CONSTANT_Methodref. |
1305 | const u2 flr_register_method_ref_index = |
1306 | register_klass ? |
1307 | add_flr_register_method_constants(writer, |
1308 | utf8_indexes, |
1309 | orig_cp_len, |
1310 | number_of_new_constants, |
1311 | THREAD) : invalid_cp_index; |
1312 | |
1313 | // New constant pool entries added and all UTF8_INFO indexes resolved |
1314 | // Now update the class file constant_pool_count with an updated count |
1315 | writer.write_at_offset<u2>(orig_cp_len + number_of_new_constants, 8); |
1316 | assert(writer.is_valid(), "invariant" ); |
1317 | orig_stream->skip_u2_fast(3); // access_flags, this_class_index, super_class_index |
1318 | const u2 iface_len = orig_stream->get_u2_fast(); // interfaces |
1319 | orig_stream->skip_u2_fast(iface_len); |
1320 | const u4 orig_fields_len_offset = orig_stream->current_offset(); |
1321 | // Copy from AccessFlags up to and including interfaces |
1322 | writer.bytes(orig_stream->buffer() + orig_access_flag_offset, |
1323 | orig_fields_len_offset - orig_access_flag_offset); |
1324 | assert(writer.is_valid(), "invariant" ); |
1325 | const jlong new_fields_len_offset = writer.current_offset(); |
1326 | const u2 orig_fields_len = position_stream_after_fields(orig_stream); |
1327 | u4 orig_method_len_offset = orig_stream->current_offset(); |
1328 | // Copy up to and including fields |
1329 | writer.bytes(orig_stream->buffer() + orig_fields_len_offset, orig_method_len_offset - orig_fields_len_offset); |
1330 | assert(writer.is_valid(), "invariant" ); |
1331 | // We are sitting just after the original number of field_infos |
1332 | // so this is a position where we can add (append) new field_infos |
1333 | const u2 number_of_new_fields = add_field_infos(writer, utf8_indexes, untypedEventHandler); |
1334 | assert(writer.is_valid(), "invariant" ); |
1335 | const jlong new_method_len_offset = writer.current_offset(); |
1336 | // Additional field_infos added, update classfile fields_count |
1337 | writer.write_at_offset<u2>(orig_fields_len + number_of_new_fields, new_fields_len_offset); |
1338 | assert(writer.is_valid(), "invariant" ); |
1339 | // Our current location is now at classfile methods_count |
1340 | const u2 orig_methods_len = position_stream_after_methods(writer, |
1341 | orig_stream, |
1342 | utf8_indexes, |
1343 | register_klass, |
1344 | clinit_method, |
1345 | orig_method_len_offset); |
1346 | const u4 orig_attributes_count_offset = orig_stream->current_offset(); |
1347 | // Copy existing methods |
1348 | writer.bytes(orig_stream->buffer() + orig_method_len_offset, orig_attributes_count_offset - orig_method_len_offset); |
1349 | assert(writer.is_valid(), "invariant" ); |
1350 | // We are sitting just after the original number of method_infos |
1351 | // so this is a position where we can add (append) new method_infos |
1352 | u2 number_of_new_methods = add_method_infos(writer, utf8_indexes); |
1353 | |
1354 | // We have just added the new methods. |
1355 | // |
1356 | // What about the state of <clinit>? |
1357 | // We would need to do: |
1358 | // 1. Nothing (@Registered(false) annotation) |
1359 | // 2. Build up a new <clinit> - and if the original class already contains a <clinit>, |
1360 | // merging will be neccessary. |
1361 | // |
1362 | if (register_klass) { |
1363 | insert_clinit_method(ik, parser, writer, orig_cp_len, utf8_indexes, flr_register_method_ref_index, clinit_method, THREAD); |
1364 | } |
1365 | number_of_new_methods += clinit_method != NULL ? 0 : register_klass ? 1 : 0; |
1366 | // Update classfile methods_count |
1367 | writer.write_at_offset<u2>(orig_methods_len + number_of_new_methods, new_method_len_offset); |
1368 | assert(writer.is_valid(), "invariant" ); |
1369 | // Copy last remaining bytes |
1370 | writer.bytes(orig_stream->buffer() + orig_attributes_count_offset, orig_stream_size - orig_attributes_count_offset); |
1371 | assert(writer.is_valid(), "invariant" ); |
1372 | assert(writer.current_offset() > orig_stream->length(), "invariant" ); |
1373 | size_of_new_bytes = (jint)writer.current_offset(); |
1374 | return new_buffer; |
1375 | } |
1376 | |
1377 | static void log_pending_exception(oop throwable) { |
1378 | assert(throwable != NULL, "invariant" ); |
1379 | oop msg = java_lang_Throwable::message(throwable); |
1380 | if (msg != NULL) { |
1381 | char* text = java_lang_String::as_utf8_string(msg); |
1382 | if (text != NULL) { |
1383 | log_error(jfr, system) ("%s" , text); |
1384 | } |
1385 | } |
1386 | } |
1387 | |
1388 | static bool should_force_instrumentation() { |
1389 | return !JfrOptionSet::allow_event_retransforms() || JfrEventClassTransformer::is_force_instrumentation(); |
1390 | } |
1391 | |
1392 | static ClassFileStream* create_new_bytes_for_subklass(const InstanceKlass* ik, const ClassFileParser& parser, Thread* t) { |
1393 | assert(JdkJfrEvent::is_a(ik), "invariant" ); |
1394 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t)); |
1395 | jint size_of_new_bytes = 0; |
1396 | const u1* new_bytes = new_bytes_for_lazy_instrumentation(ik, parser, size_of_new_bytes, t); |
1397 | if (new_bytes == NULL) { |
1398 | return NULL; |
1399 | } |
1400 | assert(new_bytes != NULL, "invariant" ); |
1401 | assert(size_of_new_bytes > 0, "invariant" ); |
1402 | |
1403 | bool force_instrumentation = should_force_instrumentation(); |
1404 | if (Jfr::is_recording() || force_instrumentation) { |
1405 | jint size_instrumented_data = 0; |
1406 | unsigned char* instrumented_data = NULL; |
1407 | const jclass super = (jclass)JNIHandles::make_local(ik->super()->java_mirror()); |
1408 | JfrUpcalls::new_bytes_eager_instrumentation(TRACE_ID(ik), |
1409 | force_instrumentation, |
1410 | super, |
1411 | size_of_new_bytes, |
1412 | new_bytes, |
1413 | &size_instrumented_data, |
1414 | &instrumented_data, |
1415 | t); |
1416 | if (t->has_pending_exception()) { |
1417 | log_pending_exception(t->pending_exception()); |
1418 | t->clear_pending_exception(); |
1419 | return NULL; |
1420 | } |
1421 | assert(instrumented_data != NULL, "invariant" ); |
1422 | assert(size_instrumented_data > 0, "invariant" ); |
1423 | return new ClassFileStream(instrumented_data, size_instrumented_data, NULL, ClassFileStream::verify); |
1424 | } |
1425 | return new ClassFileStream(new_bytes, size_of_new_bytes, NULL, ClassFileStream::verify); |
1426 | } |
1427 | |
1428 | static bool cache_bytes(InstanceKlass* ik, ClassFileStream* new_stream, InstanceKlass* new_ik, TRAPS) { |
1429 | assert(ik != NULL, "invariant" ); |
1430 | assert(new_ik != NULL, "invariant" ); |
1431 | assert(new_ik->name() != NULL, "invariant" ); |
1432 | assert(new_stream != NULL, "invariant" ); |
1433 | assert(!HAS_PENDING_EXCEPTION, "invariant" ); |
1434 | static const bool can_retransform = JfrOptionSet::allow_retransforms(); |
1435 | if (!can_retransform) { |
1436 | return true; |
1437 | } |
1438 | const jint stream_len = new_stream->length(); |
1439 | JvmtiCachedClassFileData* p = |
1440 | (JvmtiCachedClassFileData*)NEW_C_HEAP_ARRAY_RETURN_NULL(u1, offset_of(JvmtiCachedClassFileData, data) + stream_len, mtInternal); |
1441 | if (p == NULL) { |
1442 | log_error(jfr, system)("Allocation using C_HEAP_ARRAY for " SIZE_FORMAT |
1443 | " bytes failed in JfrClassAdapter::on_klass_creation" , (size_t)offset_of(JvmtiCachedClassFileData, data) + stream_len); |
1444 | return false; |
1445 | } |
1446 | p->length = stream_len; |
1447 | memcpy(p->data, new_stream->buffer(), stream_len); |
1448 | new_ik->set_cached_class_file(p); |
1449 | JvmtiCachedClassFileData* const cached_class_data = ik->get_cached_class_file(); |
1450 | if (cached_class_data != NULL) { |
1451 | os::free(cached_class_data); |
1452 | ik->set_cached_class_file(NULL); |
1453 | } |
1454 | return true; |
1455 | } |
1456 | |
1457 | static InstanceKlass* create_new_instance_klass(InstanceKlass* ik, ClassFileStream* stream, TRAPS) { |
1458 | assert(stream != NULL, "invariant" ); |
1459 | ResourceMark rm(THREAD); |
1460 | ClassLoaderData* const cld = ik->class_loader_data(); |
1461 | Handle pd(THREAD, ik->protection_domain()); |
1462 | Symbol* const class_name = ik->name(); |
1463 | const char* const klass_name = class_name != NULL ? class_name->as_C_string() : "" ; |
1464 | ClassFileParser new_parser(stream, |
1465 | class_name, |
1466 | cld, |
1467 | pd, |
1468 | NULL, // host klass |
1469 | NULL, // cp_patches |
1470 | ClassFileParser::INTERNAL, // internal visibility |
1471 | THREAD); |
1472 | if (HAS_PENDING_EXCEPTION) { |
1473 | log_pending_exception(PENDING_EXCEPTION); |
1474 | CLEAR_PENDING_EXCEPTION; |
1475 | return NULL; |
1476 | } |
1477 | InstanceKlass* const new_ik = new_parser.create_instance_klass(false, THREAD); |
1478 | if (HAS_PENDING_EXCEPTION) { |
1479 | log_pending_exception(PENDING_EXCEPTION); |
1480 | CLEAR_PENDING_EXCEPTION; |
1481 | return NULL; |
1482 | } |
1483 | assert(new_ik != NULL, "invariant" ); |
1484 | assert(new_ik->name() != NULL, "invariant" ); |
1485 | assert(strncmp(ik->name()->as_C_string(), new_ik->name()->as_C_string(), strlen(ik->name()->as_C_string())) == 0, "invariant" ); |
1486 | return cache_bytes(ik, stream, new_ik, THREAD) ? new_ik : NULL; |
1487 | } |
1488 | |
1489 | static void rewrite_klass_pointer(InstanceKlass*& ik, InstanceKlass* new_ik, ClassFileParser& parser, TRAPS) { |
1490 | assert(ik != NULL, "invariant" ); |
1491 | assert(new_ik != NULL, "invariant" ); |
1492 | assert(new_ik->name() != NULL, "invariant" ); |
1493 | assert(JdkJfrEvent::is(new_ik) || JdkJfrEvent::is_subklass(new_ik), "invariant" ); |
1494 | assert(!HAS_PENDING_EXCEPTION, "invariant" ); |
1495 | // assign original InstanceKlass* back onto "its" parser object for proper destruction |
1496 | parser.set_klass_to_deallocate(ik); |
1497 | // now rewrite original pointer to newly created InstanceKlass |
1498 | ik = new_ik; |
1499 | } |
1500 | |
1501 | // During retransform/redefine, copy the Method specific trace flags |
1502 | // from the previous ik ("the original klass") to the new ik ("the scratch_klass"). |
1503 | // The open code for retransform/redefine does not know about these. |
1504 | // In doing this migration here, we ensure the new Methods (defined in scratch klass) |
1505 | // will carry over trace tags from the old Methods being replaced, |
1506 | // ensuring flag/tag continuity while being transparent to open code. |
1507 | static void copy_method_trace_flags(const InstanceKlass* the_original_klass, const InstanceKlass* the_scratch_klass) { |
1508 | assert(the_original_klass != NULL, "invariant" ); |
1509 | assert(the_scratch_klass != NULL, "invariant" ); |
1510 | assert(the_original_klass->name() == the_scratch_klass->name(), "invariant" ); |
1511 | const Array<Method*>* old_methods = the_original_klass->methods(); |
1512 | const Array<Method*>* new_methods = the_scratch_klass->methods(); |
1513 | const bool equal_array_length = old_methods->length() == new_methods->length(); |
1514 | // The Method array has the property of being sorted. |
1515 | // If they are the same length, there is a one-to-one mapping. |
1516 | // If they are unequal, there was a method added (currently only |
1517 | // private static methods allowed to be added), use lookup. |
1518 | for (int i = 0; i < old_methods->length(); ++i) { |
1519 | const Method* const old_method = old_methods->at(i); |
1520 | Method* const new_method = equal_array_length ? new_methods->at(i) : |
1521 | the_scratch_klass->find_method(old_method->name(), old_method->signature()); |
1522 | assert(new_method != NULL, "invariant" ); |
1523 | assert(new_method->name() == old_method->name(), "invariant" ); |
1524 | assert(new_method->signature() == old_method->signature(), "invariant" ); |
1525 | *new_method->trace_flags_addr() = old_method->trace_flags(); |
1526 | assert(new_method->trace_flags() == old_method->trace_flags(), "invariant" ); |
1527 | } |
1528 | } |
1529 | |
1530 | static bool is_retransforming(const InstanceKlass* ik, TRAPS) { |
1531 | assert(ik != NULL, "invariant" ); |
1532 | assert(JdkJfrEvent::is_a(ik), "invariant" ); |
1533 | Symbol* const name = ik->name(); |
1534 | assert(name != NULL, "invariant" ); |
1535 | Handle class_loader(THREAD, ik->class_loader()); |
1536 | Handle protection_domain(THREAD, ik->protection_domain()); |
1537 | // nota bene: use lock-free dictionary lookup |
1538 | const InstanceKlass* prev_ik = (const InstanceKlass*)SystemDictionary::find(name, class_loader, protection_domain, THREAD); |
1539 | if (prev_ik == NULL) { |
1540 | return false; |
1541 | } |
1542 | // an existing ik implies a retransform/redefine |
1543 | assert(prev_ik != NULL, "invariant" ); |
1544 | assert(JdkJfrEvent::is_a(prev_ik), "invariant" ); |
1545 | copy_method_trace_flags(prev_ik, ik); |
1546 | return true; |
1547 | } |
1548 | |
1549 | // target for JFR_ON_KLASS_CREATION hook |
1550 | void JfrEventClassTransformer::on_klass_creation(InstanceKlass*& ik, ClassFileParser& parser, TRAPS) { |
1551 | assert(ik != NULL, "invariant" ); |
1552 | if (JdkJfrEvent::is(ik)) { |
1553 | ResourceMark rm(THREAD); |
1554 | HandleMark hm(THREAD); |
1555 | ClassFileStream* new_stream = create_new_bytes_for_event_klass(ik, parser, THREAD); |
1556 | if (new_stream == NULL) { |
1557 | log_error(jfr, system)("JfrClassAdapter: unable to create ClassFileStream" ); |
1558 | return; |
1559 | } |
1560 | assert(new_stream != NULL, "invariant" ); |
1561 | InstanceKlass* new_ik = create_new_instance_klass(ik, new_stream, THREAD); |
1562 | if (new_ik == NULL) { |
1563 | log_error(jfr, system)("JfrClassAdapter: unable to create InstanceKlass" ); |
1564 | return; |
1565 | } |
1566 | assert(new_ik != NULL, "invariant" ); |
1567 | // We now need to explicitly tag the replaced klass as the jdk.jfr.Event klass |
1568 | assert(!JdkJfrEvent::is(new_ik), "invariant" ); |
1569 | JdkJfrEvent::tag_as(new_ik); |
1570 | assert(JdkJfrEvent::is(new_ik), "invariant" ); |
1571 | rewrite_klass_pointer(ik, new_ik, parser, THREAD); |
1572 | return; |
1573 | } |
1574 | assert(JdkJfrEvent::is_subklass(ik), "invariant" ); |
1575 | if (is_retransforming(ik, THREAD)) { |
1576 | // not the initial klass load |
1577 | return; |
1578 | } |
1579 | if (ik->is_abstract()) { |
1580 | // abstract classes are not instrumented |
1581 | return; |
1582 | } |
1583 | ResourceMark rm(THREAD); |
1584 | HandleMark hm(THREAD); |
1585 | ClassFileStream* const new_stream = create_new_bytes_for_subklass(ik, parser, THREAD); |
1586 | if (NULL == new_stream) { |
1587 | log_error(jfr, system)("JfrClassAdapter: unable to create ClassFileStream" ); |
1588 | return; |
1589 | } |
1590 | assert(new_stream != NULL, "invariant" ); |
1591 | InstanceKlass* new_ik = create_new_instance_klass(ik, new_stream, THREAD); |
1592 | if (new_ik == NULL) { |
1593 | log_error(jfr, system)("JfrClassAdapter: unable to create InstanceKlass" ); |
1594 | return; |
1595 | } |
1596 | assert(new_ik != NULL, "invariant" ); |
1597 | // would have been tagged already as a subklass during the normal process of traceid assignment |
1598 | assert(JdkJfrEvent::is_subklass(new_ik), "invariant" ); |
1599 | traceid id = ik->trace_id(); |
1600 | ik->set_trace_id(0); |
1601 | new_ik->set_trace_id(id); |
1602 | rewrite_klass_pointer(ik, new_ik, parser, THREAD); |
1603 | } |
1604 | |
1605 | static bool _force_instrumentation = false; |
1606 | void JfrEventClassTransformer::set_force_instrumentation(bool force_instrumentation) { |
1607 | _force_instrumentation = force_instrumentation; |
1608 | } |
1609 | |
1610 | bool JfrEventClassTransformer::is_force_instrumentation() { |
1611 | return _force_instrumentation; |
1612 | } |
1613 | |