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 "classfile/javaClasses.hpp" |
27 | #include "classfile/symbolTable.hpp" |
28 | #include "classfile/systemDictionary.hpp" |
29 | #include "jfr/jni/jfrJavaSupport.hpp" |
30 | #include "jfr/jni/jfrUpcalls.hpp" |
31 | #include "jfr/support/jfrEventClass.hpp" |
32 | #include "logging/log.hpp" |
33 | #include "memory/oopFactory.hpp" |
34 | #include "oops/oop.inline.hpp" |
35 | #include "oops/typeArrayKlass.hpp" |
36 | #include "oops/typeArrayOop.inline.hpp" |
37 | #include "runtime/handles.inline.hpp" |
38 | #include "runtime/os.hpp" |
39 | #include "runtime/thread.inline.hpp" |
40 | #include "utilities/exceptions.hpp" |
41 | |
42 | static Symbol* jvm_upcalls_class_sym = NULL; |
43 | static Symbol* on_retransform_method_sym = NULL; |
44 | static Symbol* on_retransform_signature_sym = NULL; |
45 | static Symbol* bytes_for_eager_instrumentation_sym = NULL; |
46 | static Symbol* bytes_for_eager_instrumentation_sig_sym = NULL; |
47 | |
48 | static bool initialize(TRAPS) { |
49 | static bool initialized = false; |
50 | if (!initialized) { |
51 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
52 | jvm_upcalls_class_sym = SymbolTable::new_permanent_symbol("jdk/jfr/internal/JVMUpcalls" ); |
53 | on_retransform_method_sym = SymbolTable::new_permanent_symbol("onRetransform" ); |
54 | on_retransform_signature_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B" ); |
55 | bytes_for_eager_instrumentation_sym = SymbolTable::new_permanent_symbol("bytesForEagerInstrumentation" ); |
56 | bytes_for_eager_instrumentation_sig_sym = SymbolTable::new_permanent_symbol("(JZLjava/lang/Class;[B)[B" ); |
57 | initialized = bytes_for_eager_instrumentation_sig_sym != NULL; |
58 | } |
59 | return initialized; |
60 | } |
61 | |
62 | static const typeArrayOop invoke(jlong trace_id, |
63 | jboolean force_instrumentation, |
64 | jclass class_being_redefined, |
65 | jint class_data_len, |
66 | const unsigned char* class_data, |
67 | Symbol* method_sym, |
68 | Symbol* signature_sym, |
69 | jint& new_bytes_length, |
70 | TRAPS) { |
71 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
72 | const Klass* klass = SystemDictionary::resolve_or_fail(jvm_upcalls_class_sym, true, CHECK_NULL); |
73 | assert(klass != NULL, "invariant" ); |
74 | typeArrayOop old_byte_array = oopFactory::new_byteArray(class_data_len, CHECK_NULL); |
75 | memcpy(old_byte_array->byte_at_addr(0), class_data, class_data_len); |
76 | JavaValue result(T_OBJECT); |
77 | JfrJavaArguments args(&result, klass, method_sym, signature_sym); |
78 | args.push_long(trace_id); |
79 | args.push_int(force_instrumentation); |
80 | args.push_jobject(class_being_redefined); |
81 | args.push_oop(old_byte_array); |
82 | JfrJavaSupport::call_static(&args, THREAD); |
83 | if (HAS_PENDING_EXCEPTION) { |
84 | log_error(jfr, system)("JfrUpcall failed" ); |
85 | return NULL; |
86 | } |
87 | // The result should be a [B |
88 | const oop res = (oop)result.get_jobject(); |
89 | assert(res != NULL, "invariant" ); |
90 | assert(res->is_typeArray(), "invariant" ); |
91 | assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "invariant" ); |
92 | const typeArrayOop new_byte_array = typeArrayOop(res); |
93 | new_bytes_length = (jint)new_byte_array->length(); |
94 | return new_byte_array; |
95 | } |
96 | |
97 | static const size_t ERROR_MSG_BUFFER_SIZE = 256; |
98 | static void log_error_and_throw_oom(jint new_bytes_length, TRAPS) { |
99 | char error_buffer[ERROR_MSG_BUFFER_SIZE]; |
100 | jio_snprintf(error_buffer, ERROR_MSG_BUFFER_SIZE, |
101 | "Thread local allocation (native) for " SIZE_FORMAT " bytes failed in JfrUpcalls" , (size_t)new_bytes_length); |
102 | log_error(jfr, system)("%s" , error_buffer); |
103 | JfrJavaSupport::throw_out_of_memory_error(error_buffer, CHECK); |
104 | } |
105 | |
106 | void JfrUpcalls::on_retransform(jlong trace_id, |
107 | jclass class_being_redefined, |
108 | jint class_data_len, |
109 | const unsigned char* class_data, |
110 | jint* new_class_data_len, |
111 | unsigned char** new_class_data, |
112 | TRAPS) { |
113 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
114 | assert(class_being_redefined != NULL, "invariant" ); |
115 | assert(class_data != NULL, "invariant" ); |
116 | assert(new_class_data_len != NULL, "invariant" ); |
117 | assert(new_class_data != NULL, "invariant" ); |
118 | if (!JdkJfrEvent::is_visible(class_being_redefined)) { |
119 | return; |
120 | } |
121 | jint new_bytes_length = 0; |
122 | initialize(THREAD); |
123 | const typeArrayOop new_byte_array = invoke(trace_id, |
124 | false, |
125 | class_being_redefined, |
126 | class_data_len, |
127 | class_data, |
128 | on_retransform_method_sym, |
129 | on_retransform_signature_sym, |
130 | new_bytes_length, |
131 | CHECK); |
132 | assert(new_byte_array != NULL, "invariant" ); |
133 | assert(new_bytes_length > 0, "invariant" ); |
134 | // memory space must be malloced as mtInternal |
135 | // as it will be deallocated by JVMTI routines |
136 | unsigned char* const new_bytes = (unsigned char* const)os::malloc(new_bytes_length, mtInternal); |
137 | if (new_bytes == NULL) { |
138 | log_error_and_throw_oom(new_bytes_length, THREAD); // unwinds |
139 | } |
140 | assert(new_bytes != NULL, "invariant" ); |
141 | memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length); |
142 | *new_class_data_len = new_bytes_length; |
143 | *new_class_data = new_bytes; |
144 | } |
145 | |
146 | void JfrUpcalls::new_bytes_eager_instrumentation(jlong trace_id, |
147 | jboolean force_instrumentation, |
148 | jclass super, |
149 | jint class_data_len, |
150 | const unsigned char* class_data, |
151 | jint* new_class_data_len, |
152 | unsigned char** new_class_data, |
153 | TRAPS) { |
154 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
155 | assert(super != NULL, "invariant" ); |
156 | assert(class_data != NULL, "invariant" ); |
157 | assert(new_class_data_len != NULL, "invariant" ); |
158 | assert(new_class_data != NULL, "invariant" ); |
159 | jint new_bytes_length = 0; |
160 | initialize(THREAD); |
161 | const typeArrayOop new_byte_array = invoke(trace_id, |
162 | force_instrumentation, |
163 | super, |
164 | class_data_len, |
165 | class_data, |
166 | bytes_for_eager_instrumentation_sym, |
167 | bytes_for_eager_instrumentation_sig_sym, |
168 | new_bytes_length, |
169 | CHECK); |
170 | assert(new_byte_array != NULL, "invariant" ); |
171 | assert(new_bytes_length > 0, "invariant" ); |
172 | unsigned char* const new_bytes = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, unsigned char, new_bytes_length); |
173 | if (new_bytes == NULL) { |
174 | log_error_and_throw_oom(new_bytes_length, THREAD); // this unwinds |
175 | } |
176 | assert(new_bytes != NULL, "invariant" ); |
177 | memcpy(new_bytes, new_byte_array->byte_at_addr(0), (size_t)new_bytes_length); |
178 | *new_class_data_len = new_bytes_length; |
179 | *new_class_data = new_bytes; |
180 | } |
181 | |