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