1 /*
2 * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include "classfile/javaClasses.inline.hpp"
25 #include "code/compiledIC.hpp"
26 #include "compiler/compileBroker.hpp"
27 #include "compiler/compilerThread.hpp"
28 #include "compiler/oopMap.hpp"
29 #include "gc/shared/barrierSetNMethod.hpp"
30 #include "jvmci/jvmciCodeInstaller.hpp"
31 #include "jvmci/jvmciCompilerToVM.hpp"
32 #include "jvmci/jvmciRuntime.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/compressedKlass.inline.hpp"
35 #include "oops/klass.inline.hpp"
36 #include "prims/jvmtiExport.hpp"
37 #include "prims/methodHandles.hpp"
38 #include "runtime/arguments.hpp"
39 #include "runtime/interfaceSupport.inline.hpp"
40 #include "runtime/javaThread.hpp"
41 #include "runtime/jniHandles.inline.hpp"
42 #include "runtime/os.hpp"
43 #include "runtime/sharedRuntime.hpp"
44 #include "utilities/align.hpp"
45 #include "utilities/exceptions.hpp"
46
47 // frequently used constants
48 // Allocate them with new so they are never destroyed (otherwise, a
49 // forced exit could destroy these objects while they are still in
50 // use).
51 ConstantOopWriteValue* CodeInstaller::_oop_null_scope_value = new (mtJVMCI) ConstantOopWriteValue(nullptr);
52 ConstantIntValue* CodeInstaller::_int_m1_scope_value = new (mtJVMCI) ConstantIntValue(-1);
53 ConstantIntValue* CodeInstaller::_int_0_scope_value = new (mtJVMCI) ConstantIntValue((jint)0);
54 ConstantIntValue* CodeInstaller::_int_1_scope_value = new (mtJVMCI) ConstantIntValue(1);
55 ConstantIntValue* CodeInstaller::_int_2_scope_value = new (mtJVMCI) ConstantIntValue(2);
56 LocationValue* CodeInstaller::_illegal_value = new (mtJVMCI) LocationValue(Location());
57 MarkerValue* CodeInstaller::_virtual_byte_array_marker = new (mtJVMCI) MarkerValue();
58
59 static bool is_set(u1 flags, u1 bit) {
60 return flags & bit;
61 }
62
63 oop HotSpotCompiledCodeStream::get_oop(int id, JVMCI_TRAPS) const {
64 if (_object_pool.is_null()) {
65 JVMCI_ERROR_NULL("object pool is null%s", context());
66 }
67 if (!_object_pool.is_null() && 0 <= id && id < _object_pool->length()) {
68 JavaThread* THREAD = JavaThread::current(); // For exception macros.
69 return _object_pool->obj_at(id, CHECK_NULL);
70 }
71 JVMCI_ERROR_NULL("unknown direct object id %d%s", id, context());
72 }
73
74 u4 HotSpotCompiledCodeStream::offset() const {
75 u4 res = 0;
76 for (Chunk* c = _head; c != nullptr; c = c->next()) {
77 if (c == _chunk) {
78 res += _pos - c->data();
79 break;
80 } else {
81 res += c->size();
82 }
83 }
84 return res;
85 }
86
87 bool HotSpotCompiledCodeStream::available() const {
88 u4 rem = _chunk->data_end() - _pos;
89 for (Chunk* c = _chunk->next(); c != nullptr; c = c->next()) {
90 rem += c->size();
91 }
92 return rem;
93 }
94
95 void HotSpotCompiledCodeStream::dump_buffer(outputStream* st) const {
96 st->print_cr("HotSpotCompiledCode stream for %s:", code_desc());
97 int chunk_index = 0;
98 for (Chunk* c = _head; c != nullptr; c = c->next()) {
99 const u1* data = c->data();
100 const u1* data_end = c->data_end();
101
102 int to_dump = data_end - data;
103 st->print_cr("# chunk %d, %d bytes", chunk_index, to_dump);
104 st->print_data((void*) data, to_dump, true, false);
105 chunk_index++;
106 }
107 }
108
109 void HotSpotCompiledCodeStream::dump_buffer_tail(int len, outputStream* st) const {
110 const u1* start;
111 int avail = _pos - _chunk->data();
112 if (len >= avail) {
113 len = avail;
114 start = _chunk->data();
115 } else {
116 start = _pos - len;
117
118 // Ensure start is 16-byte aligned wrt chunk start
119 int start_offset = start - _chunk->data();
120 start -= (start_offset % 16);
121 len = _pos - start;
122 }
123
124 st->print_cr("Last %d bytes up to current read position " INTPTR_FORMAT " in HotSpotCompiledCode stream for %s:", len, p2i(_pos), code_desc());
125 st->print_data((void*) start, len, true, false);
126 }
127
128 const char* HotSpotCompiledCodeStream::context() const {
129 stringStream st;
130 st.cr();
131 st.print_cr("at " INTPTR_FORMAT " in HotSpotCompiledCode stream", p2i(_pos));
132 dump_buffer_tail(100, &st);
133 return st.as_string();
134 }
135
136 void HotSpotCompiledCodeStream::before_read(u1 size) {
137 if (_pos + size > _chunk->data_end()) {
138 Chunk* next = _chunk->next();
139 if (next == nullptr || size > next->size()) {
140 dump_buffer();
141 fatal("%s: reading %d bytes overflows buffer at " INTPTR_FORMAT, code_desc(), size, p2i(_pos));
142 }
143 _chunk = next;
144 _pos = _chunk->data();
145 }
146 }
147
148 // Reads a size followed by an ascii string from the stream and
149 // checks that they match `expect_size` and `expect_name` respectively. This
150 // implements a rudimentary type checking of the stream between the stream producer
151 // (Java) and the consumer (C++).
152 void HotSpotCompiledCodeStream::check_data(u2 expect_size, const char* expect_name) {
153 u2 actual_size = get_u1();
154 u2 ascii_len = get_u1();
155 const char* actual_name = (const char*) _pos;
156 char* end = (char*) _pos + ascii_len;
157 _pos = (const u1*) end;
158 if (strlen(expect_name) != ascii_len || strncmp(expect_name, actual_name, ascii_len) != 0) {
159 dump_buffer();
160 fatal("%s: expected \"%s\" at " INTPTR_FORMAT ", got \"%.*s\" (len: %d)",
161 code_desc(), expect_name, p2i(actual_name), ascii_len, actual_name, ascii_len);
162 }
163 if (actual_size != expect_size) {
164 dump_buffer();
165 fatal("%s: expected \"%s\" at " INTPTR_FORMAT " to have size %u, got %u",
166 code_desc(), expect_name, p2i(actual_name), expect_size, actual_size);
167 }
168 }
169
170 const char* HotSpotCompiledCodeStream::read_utf8(const char* name, JVMCI_TRAPS) {
171 jint utf_len = read_s4(name);
172 if (utf_len == -1) {
173 return nullptr;
174 }
175 guarantee(utf_len >= 0, "bad utf_len: %d", utf_len);
176
177 const char* utf = (const char*) _pos;
178 char* end = (char*) _pos + utf_len;
179 _pos = (const u1*) (end + 1);
180 if (*end != '\0') {
181 JVMCI_ERROR_NULL("UTF8 string at " INTPTR_FORMAT " of length %d missing 0 terminator: \"%.*s\"%s",
182 p2i(utf), utf_len, utf_len, utf, context());
183 }
184 return utf;
185 }
186
187 Method* HotSpotCompiledCodeStream::read_method(const char* name) {
188 return (Method*) read_u8(name);
189 }
190
191 Klass* HotSpotCompiledCodeStream::read_klass(const char* name) {
192 return (Klass*) read_u8(name);
193 }
194
195 ScopeValue* HotSpotCompiledCodeStream::virtual_object_at(int id, JVMCI_TRAPS) const {
196 if (_virtual_objects == nullptr) {
197 JVMCI_ERROR_NULL("virtual object id %d read outside scope of decoding DebugInfo%s", id, context());
198 }
199 if (id < 0 || id >= _virtual_objects->length()) {
200 JVMCI_ERROR_NULL("invalid virtual object id %d%s", id, context());
201 }
202 return _virtual_objects->at(id);
203 }
204
205 #ifndef PRODUCT
206 void CodeInstaller::verify_bci_constants(JVMCIEnv* env) {
207 #define CHECK_IN_SYNC(name) do { \
208 int expect = env->get_BytecodeFrame_ ## name ##_BCI(); \
209 int actual = name##_BCI; \
210 if (expect != actual) fatal("CodeInstaller::" #name "_BCI(%d) != BytecodeFrame." #name "_BCI(%d)", expect, actual); \
211 } while(0)
212
213 CHECK_IN_SYNC(UNWIND);
214 CHECK_IN_SYNC(BEFORE);
215 CHECK_IN_SYNC(AFTER);
216 CHECK_IN_SYNC(AFTER_EXCEPTION);
217 CHECK_IN_SYNC(UNKNOWN);
218 CHECK_IN_SYNC(INVALID_FRAMESTATE);
219 #undef CHECK_IN_SYNC
220 }
221 #endif
222
223 VMReg CodeInstaller::getVMRegFromLocation(HotSpotCompiledCodeStream* stream, int total_frame_size, JVMCI_TRAPS) {
224 u2 reg = stream->read_u2("register");
225 u2 offset = stream->read_u2("offset");
226
227 if (reg != NO_REGISTER) {
228 VMReg vmReg = CodeInstaller::get_hotspot_reg(reg, JVMCI_CHECK_NULL);
229 if (offset % 4 == 0) {
230 return vmReg->next(offset / 4);
231 } else {
232 JVMCI_ERROR_NULL("unaligned subregister offset %d in oop map%s", offset, stream->context());
233 }
234 } else {
235 if (offset % 4 == 0) {
236 VMReg vmReg = VMRegImpl::stack2reg(offset / 4);
237 if (!OopMapValue::legal_vm_reg_name(vmReg)) {
238 // This restriction only applies to VMRegs that are used in OopMap but
239 // since that's the only use of VMRegs it's simplest to put this test
240 // here. This test should also be equivalent legal_vm_reg_name but JVMCI
241 // clients can use max_oop_map_stack_stack_offset to detect this problem
242 // directly. The asserts just ensure that the tests are in agreement.
243 assert(offset > CompilerToVM::Data::max_oop_map_stack_offset(), "illegal VMReg");
244 JVMCI_ERROR_NULL("stack offset %d is too large to be encoded in OopMap (max %d)%s",
245 offset, CompilerToVM::Data::max_oop_map_stack_offset(), stream->context());
246 }
247 assert(OopMapValue::legal_vm_reg_name(vmReg), "illegal VMReg");
248 return vmReg;
249 } else {
250 JVMCI_ERROR_NULL("unaligned stack offset %d in oop map%s", offset, stream->context());
251 }
252 }
253 }
254
255 OopMap* CodeInstaller::create_oop_map(HotSpotCompiledCodeStream* stream, u1 debug_info_flags, JVMCI_TRAPS) {
256 assert(is_set(debug_info_flags, DI_HAS_REFERENCE_MAP), "must be");
257 u2 max_register_size = stream->read_u2("maxRegisterSize");
258 if (!_has_wide_vector && SharedRuntime::is_wide_vector(max_register_size)) {
259 if (SharedRuntime::polling_page_vectors_safepoint_handler_blob() == nullptr) {
260 JVMCI_ERROR_NULL("JVMCI is producing code using vectors larger than the runtime supports%s", stream->context());
261 }
262 _has_wide_vector = true;
263 }
264 u2 length = stream->read_u2("referenceMap:length");
265
266 OopMap* map = new OopMap(_total_frame_size, _parameter_count);
267 for (int i = 0; i < length; i++) {
268 bool has_derived = stream->read_bool("hasDerived");
269 u2 bytes = stream->read_u2("sizeInBytes");
270 VMReg vmReg = getVMRegFromLocation(stream, _total_frame_size, JVMCI_CHECK_NULL);
271 if (has_derived) {
272 // derived oop
273 if (bytes == LP64_ONLY(8) NOT_LP64(4)) {
274 VMReg baseReg = getVMRegFromLocation(stream, _total_frame_size, JVMCI_CHECK_NULL);
275 map->set_derived_oop(vmReg, baseReg);
276 } else {
277 JVMCI_ERROR_NULL("invalid derived oop size in ReferenceMap: %d%s", bytes, stream->context());
278 }
279 #ifdef _LP64
280 } else if (bytes == 8) {
281 // wide oop
282 map->set_oop(vmReg);
283 } else if (bytes == 4) {
284 // narrow oop
285 map->set_narrowoop(vmReg);
286 #else
287 } else if (bytes == 4) {
288 map->set_oop(vmReg);
289 #endif
290 } else {
291 JVMCI_ERROR_NULL("invalid oop size in ReferenceMap: %d%s", bytes, stream->context());
292 }
293 }
294
295 if (is_set(debug_info_flags, DI_HAS_CALLEE_SAVE_INFO)) {
296 length = stream->read_u2("calleeSaveInfo:length");
297 for (jint i = 0; i < length; i++) {
298 u2 jvmci_reg_number = stream->read_u2("register");
299 VMReg hotspot_reg = CodeInstaller::get_hotspot_reg(jvmci_reg_number, JVMCI_CHECK_NULL);
300 // HotSpot stack slots are 4 bytes
301 u2 jvmci_slot = stream->read_u2("slot");
302 jint hotspot_slot = jvmci_slot * VMRegImpl::slots_per_word;
303 VMReg hotspot_slot_as_reg = VMRegImpl::stack2reg(hotspot_slot);
304 map->set_callee_saved(hotspot_slot_as_reg, hotspot_reg);
305 #ifdef _LP64
306 // (copied from generate_oop_map() in c1_Runtime1_x86.cpp)
307 VMReg hotspot_slot_hi_as_reg = VMRegImpl::stack2reg(hotspot_slot + 1);
308 map->set_callee_saved(hotspot_slot_hi_as_reg, hotspot_reg->next());
309 #endif
310 }
311 }
312 return map;
313 }
314
315 void* CodeInstaller::record_metadata_reference(CodeSection* section, address dest, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
316 /*
317 * This method needs to return a raw (untyped) pointer, since the value of a pointer to the base
318 * class is in general not equal to the pointer of the subclass. When patching metaspace pointers,
319 * the compiler expects a direct pointer to the subclass (Klass* or Method*), not a pointer to the
320 * base class (Metadata* or MetaspaceObj*).
321 */
322 if (tag == PATCH_KLASS) {
323 Klass* klass = stream->read_klass("patch:klass");
324 int index = _oop_recorder->find_index(klass);
325 section->relocate(dest, metadata_Relocation::spec(index));
326 JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
327 return klass;
328 } else if (tag == PATCH_METHOD) {
329 Method* method = stream->read_method("patch:method");
330 int index = _oop_recorder->find_index(method);
331 section->relocate(dest, metadata_Relocation::spec(index));
332 JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), method->name()->as_C_string());
333 return method;
334 } else {
335 JVMCI_ERROR_NULL("unexpected metadata reference tag: %d%s", tag, stream->context());
336 }
337 }
338
339 #ifdef _LP64
340 narrowKlass CodeInstaller::record_narrow_metadata_reference(CodeSection* section, address dest, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
341 if (tag != PATCH_NARROW_KLASS) {
342 JVMCI_ERROR_0("unexpected compressed pointer tag %d%s", tag, stream->context());
343 }
344 Klass* klass = stream->read_klass("patch:klass");
345 int index = _oop_recorder->find_index(klass);
346 section->relocate(dest, metadata_Relocation::spec(index));
347 JVMCI_event_3("narrowKlass[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
348 guarantee(CompressedKlassPointers::is_encodable(klass), "klass cannot be compressed: %s", klass->external_name());
349 return CompressedKlassPointers::encode(klass);
350 }
351 #endif
352
353 ScopeValue* CodeInstaller::to_primitive_value(HotSpotCompiledCodeStream* stream, jlong raw, BasicType type, ScopeValue* &second, JVMCI_TRAPS) {
354 if (type == T_INT || type == T_FLOAT) {
355 jint prim = (jint) raw;
356 switch (prim) {
357 case -1: return _int_m1_scope_value;
358 case 0: return _int_0_scope_value;
359 case 1: return _int_1_scope_value;
360 case 2: return _int_2_scope_value;
361 default: return new ConstantIntValue(prim);
362 }
363 } else if (type == T_LONG || type == T_DOUBLE) {
364 jlong prim = raw;
365 second = _int_1_scope_value;
366 return new ConstantLongValue(prim);
367 } else {
368 JVMCI_ERROR_NULL("unexpected primitive constant type %s%s", basictype_to_str(type), stream->context());
369 }
370 }
371
372 Handle CodeInstaller::read_oop(HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
373 oop obj;
374 if (tag == OBJECT_ID) {
375 obj = stream->get_oop(stream->read_u1("id"), JVMCI_CHECK_(Handle()));
376 } else if (tag == OBJECT_ID2) {
377 obj = stream->get_oop(stream->read_u2("id:2"), JVMCI_CHECK_(Handle()));
378 } else if (tag == JOBJECT) {
379 jlong object_handle = stream->read_u8("jobject");
380 obj = jvmci_env()->resolve_oop_handle(object_handle);
381 } else {
382 JVMCI_ERROR_(Handle(), "unexpected oop tag: %d", tag)
383 }
384 if (obj == nullptr) {
385 JVMCI_THROW_MSG_(InternalError, "Constant was unexpectedly null", Handle());
386 } else {
387 guarantee(oopDesc::is_oop_or_null(obj), "invalid oop: " INTPTR_FORMAT, p2i((oopDesc*) obj));
388 }
389 return Handle(stream->thread(), obj);
390 }
391
392 ScopeValue* CodeInstaller::get_scope_value(HotSpotCompiledCodeStream* stream, u1 tag, BasicType type, ScopeValue* &second, JVMCI_TRAPS) {
393 second = nullptr;
394 bool stack_slot_is_s2 = true;
395 switch (tag) {
396 case ILLEGAL: {
397 if (type != T_ILLEGAL) {
398 JVMCI_ERROR_NULL("unexpected illegal value, expected %s%s", basictype_to_str(type), stream->context());
399 }
400 return _illegal_value;
401 }
402 case REGISTER_PRIMITIVE:
403 case REGISTER_NARROW_OOP:
404 case REGISTER_OOP:
405 case REGISTER_VECTOR: {
406 u2 number = stream->read_u2("register");
407 VMReg hotspotRegister = get_hotspot_reg(number, JVMCI_CHECK_NULL);
408 if (is_general_purpose_reg(hotspotRegister)) {
409 Location::Type locationType;
410 if (type == T_OBJECT) {
411 locationType = tag == REGISTER_NARROW_OOP ? Location::narrowoop : Location::oop;
412 } else if (type == T_LONG) {
413 locationType = Location::lng;
414 } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
415 locationType = Location::int_in_long;
416 } else {
417 JVMCI_ERROR_NULL("unexpected type %s in CPU register%s", basictype_to_str(type), stream->context());
418 }
419 ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
420 if (type == T_LONG) {
421 second = value;
422 }
423 return value;
424 } else {
425 Location::Type locationType;
426 if (type == T_FLOAT) {
427 // this seems weird, but the same value is used in c1_LinearScan
428 locationType = Location::normal;
429 } else if (type == T_DOUBLE) {
430 locationType = Location::dbl;
431 } else if (type == T_OBJECT && tag == REGISTER_VECTOR) {
432 locationType = Location::vector;
433 } else {
434 JVMCI_ERROR_NULL("unexpected type %s in floating point register%s", basictype_to_str(type), stream->context());
435 }
436 ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
437 if (type == T_DOUBLE) {
438 second = value;
439 }
440 return value;
441 }
442 }
443 case STACK_SLOT4_PRIMITIVE:
444 case STACK_SLOT4_NARROW_OOP:
445 case STACK_SLOT4_OOP:
446 case STACK_SLOT4_VECTOR:
447 stack_slot_is_s2 = false;
448 // fall through
449 case STACK_SLOT_PRIMITIVE:
450 case STACK_SLOT_NARROW_OOP:
451 case STACK_SLOT_OOP:
452 case STACK_SLOT_VECTOR: {
453 jint offset = stack_slot_is_s2 ? (jshort) stream->read_s2("offset") : stream->read_s4("offset4");
454 if (stream->read_bool("addRawFrameSize")) {
455 offset += _total_frame_size;
456 }
457 Location::Type locationType;
458 if (type == T_OBJECT) {
459 locationType = tag == STACK_SLOT_VECTOR ? Location::vector : tag == STACK_SLOT_NARROW_OOP ? Location::narrowoop : Location::oop;
460 } else if (type == T_LONG) {
461 locationType = Location::lng;
462 } else if (type == T_DOUBLE) {
463 locationType = Location::dbl;
464 } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
465 locationType = Location::normal;
466 } else {
467 JVMCI_ERROR_NULL("unexpected type %s in stack slot%s", basictype_to_str(type), stream->context());
468 }
469 ScopeValue* value = new LocationValue(Location::new_stk_loc(locationType, offset));
470 if (type == T_DOUBLE || type == T_LONG) {
471 second = value;
472 }
473 return value;
474 }
475 case NULL_CONSTANT: { return _oop_null_scope_value; }
476 case RAW_CONSTANT: { return new ConstantLongValue(stream->read_u8("primitive")); }
477 case PRIMITIVE_0: { ScopeValue* v = to_primitive_value(stream, 0, type, second, JVMCI_CHECK_NULL); return v; }
478 case PRIMITIVE4: { ScopeValue* v = to_primitive_value(stream, stream->read_s4("primitive4"), type, second, JVMCI_CHECK_NULL); return v; }
479 case PRIMITIVE8: { ScopeValue* v = to_primitive_value(stream, stream->read_s8("primitive8"), type, second, JVMCI_CHECK_NULL); return v; }
480 case VIRTUAL_OBJECT_ID: { ScopeValue* v = stream->virtual_object_at(stream->read_u1("id"), JVMCI_CHECK_NULL); return v; }
481 case VIRTUAL_OBJECT_ID2: { ScopeValue* v = stream->virtual_object_at(stream->read_u2("id:2"), JVMCI_CHECK_NULL); return v; }
482
483 case OBJECT_ID:
484 case OBJECT_ID2:
485 case JOBJECT: {
486 Handle obj = read_oop(stream, tag, JVMCI_CHECK_NULL);
487 return new ConstantOopWriteValue(JNIHandles::make_local(obj()));
488 }
489 default: {
490 JVMCI_ERROR_NULL("unexpected tag in scope: %d%s", tag, stream->context())
491 }
492 }
493 }
494
495 void CodeInstaller::record_object_value(ObjectValue* sv, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
496 oop javaMirror = JNIHandles::resolve(sv->klass()->as_ConstantOopWriteValue()->value());
497 Klass* klass = java_lang_Class::as_Klass(javaMirror);
498 bool isLongArray = klass == Universe::longArrayKlass();
499 bool isByteArray = klass == Universe::byteArrayKlass();
500
501 u2 length = stream->read_u2("values:length");
502 for (jint i = 0; i < length; i++) {
503 ScopeValue* cur_second = nullptr;
504 BasicType type = (BasicType) stream->read_u1("basicType");
505 ScopeValue* value;
506 u1 tag = stream->read_u1("tag");
507 if (tag == ILLEGAL) {
508 if (isByteArray && type == T_ILLEGAL) {
509 /*
510 * The difference between a virtualized large access and a deferred write is the kind stored in the slotKinds
511 * of the virtual object: in the virtualization case, the kind is illegal, in the deferred write case, the kind
512 * is access stack kind (an int).
513 */
514 value = _virtual_byte_array_marker;
515 } else {
516 value = _illegal_value;
517 if (type == T_DOUBLE || type == T_LONG) {
518 cur_second = _illegal_value;
519 }
520 }
521 } else {
522 value = get_scope_value(stream, tag, type, cur_second, JVMCI_CHECK);
523 }
524
525 if (isLongArray && cur_second == nullptr) {
526 // we're trying to put ints into a long array... this isn't really valid, but it's used for some optimizations.
527 // add an int 0 constant
528 cur_second = _int_0_scope_value;
529 }
530
531 if (isByteArray && cur_second != nullptr && (type == T_DOUBLE || type == T_LONG)) {
532 // we are trying to write a long in a byte Array. We will need to count the illegals to restore the type of
533 // the thing we put inside.
534 cur_second = nullptr;
535 }
536
537 if (cur_second != nullptr) {
538 sv->field_values()->append(cur_second);
539 }
540 assert(value != nullptr, "missing value");
541 sv->field_values()->append(value);
542 }
543 }
544
545 GrowableArray<ScopeValue*>* CodeInstaller::read_local_or_stack_values(HotSpotCompiledCodeStream* stream, u1 frame_flags, bool is_locals, JVMCI_TRAPS) {
546 u2 length;
547 if (is_locals) {
548 if (!is_set(frame_flags, DIF_HAS_LOCALS)) {
549 return nullptr;
550 }
551 length = stream->read_u2("numLocals");
552 } else {
553 if (!is_set(frame_flags, DIF_HAS_STACK)) {
554 return nullptr;
555 }
556 length = stream->read_u2("numStack");
557 }
558 GrowableArray<ScopeValue*>* values = new GrowableArray<ScopeValue*> (length);
559 for (int i = 0; i < length; i++) {
560 ScopeValue* second = nullptr;
561 BasicType type = (BasicType) stream->read_u1("basicType");
562 u1 tag = stream->read_u1("tag");
563 ScopeValue* first = get_scope_value(stream, tag, type, second, JVMCI_CHECK_NULL);
564 if (second != nullptr) {
565 if (i == length) {
566 JVMCI_ERROR_NULL("double-slot value not followed by Value.ILLEGAL%s", stream->context());
567 }
568 i++;
569 stream->read_u1("basicType");
570 tag = stream->read_u1("tag");
571 if (tag != ILLEGAL) {
572 JVMCI_ERROR_NULL("double-slot value not followed by Value.ILLEGAL%s", stream->context());
573 }
574 values->append(second);
575 }
576 values->append(first);
577 }
578 return values;
579 }
580
581 GrowableArray<MonitorValue*>* CodeInstaller::read_monitor_values(HotSpotCompiledCodeStream* stream, u1 frame_flags, JVMCI_TRAPS) {
582 if (!is_set(frame_flags, DIF_HAS_LOCKS)) {
583 return nullptr;
584 }
585 if (!_has_monitors) {
586 _has_monitors = true;
587 }
588 u2 length = stream->read_u2("numLocks");
589 GrowableArray<MonitorValue*>* monitors = new GrowableArray<MonitorValue*>(length);
590 for (int i = 0; i < length; i++) {
591 bool eliminated = stream->read_bool("isEliminated");
592 ScopeValue* second = nullptr;
593 ScopeValue* owner_value = get_scope_value(stream, stream->read_u1("tag"), T_OBJECT, second, JVMCI_CHECK_NULL);
594 assert(second == nullptr, "monitor cannot occupy two stack slots");
595
596 ScopeValue* lock_data_value = get_scope_value(stream, stream->read_u1("tag"), T_LONG, second, JVMCI_CHECK_NULL);
597 assert(second == lock_data_value, "monitor is LONG value that occupies two stack slots");
598 assert(lock_data_value->is_location(), "invalid monitor location");
599 Location lock_data_loc = ((LocationValue*) lock_data_value)->location();
600
601 monitors->append(new MonitorValue(owner_value, lock_data_loc, eliminated));
602 }
603 return monitors;
604 }
605
606 void CodeInstaller::initialize_dependencies(HotSpotCompiledCodeStream* stream, u1 code_flags, OopRecorder* oop_recorder, JVMCI_TRAPS) {
607 JavaThread* thread = stream->thread();
608 CompilerThread* compilerThread = thread->is_Compiler_thread() ? CompilerThread::cast(thread) : nullptr;
609 _oop_recorder = oop_recorder;
610 _dependencies = new Dependencies(&_arena, _oop_recorder, compilerThread != nullptr ? compilerThread->log() : nullptr);
611 if (is_set(code_flags, HCC_HAS_ASSUMPTIONS)) {
612 u2 length = stream->read_u2("assumptions:length");
613 for (int i = 0; i < length; ++i) {
614 u1 tag = stream->read_u1("tag");
615 switch (tag) {
616 case NO_FINALIZABLE_SUBCLASS: {
617 Klass* receiver_type = stream->read_klass("receiverType");
618 _dependencies->assert_has_no_finalizable_subclasses(receiver_type);
619 break;
620 }
621 case CONCRETE_SUBTYPE: {
622 Klass* context = stream->read_klass("context");
623 Klass* subtype = stream->read_klass("subtype");
624 assert(context->is_abstract(), "must be");
625 _dependencies->assert_abstract_with_unique_concrete_subtype(context, subtype);
626 break;
627 }
628 case LEAF_TYPE: {
629 Klass* context = stream->read_klass("context");
630 _dependencies->assert_leaf_type(context);
631 break;
632 }
633 case CONCRETE_METHOD: {
634 Klass* context = stream->read_klass("context");
635 Method* impl = stream->read_method("impl");
636 _dependencies->assert_unique_concrete_method(context, impl);
637 break;
638 }
639 case CALLSITE_TARGET_VALUE: {
640 u1 obj_tag = stream->read_u1("tag");
641 Handle callSite = read_oop(stream, obj_tag, JVMCI_CHECK);
642 obj_tag = stream->read_u1("tag");
643 Handle methodHandle = read_oop(stream, obj_tag, JVMCI_CHECK);
644 _dependencies->assert_call_site_target_value(callSite(), methodHandle());
645 break;
646 }
647 default: {
648 JVMCI_ERROR("unexpected assumption tag %d%s", tag, stream->context());
649 }
650 }
651 }
652 }
653 if (is_set(code_flags, HCC_HAS_METHODS)) {
654 u2 length = stream->read_u2("methods:length");
655 for (int i = 0; i < length; ++i) {
656 Method* method = stream->read_method("method");
657 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
658 _dependencies->assert_evol_method(method);
659 }
660 }
661 }
662 }
663
664 JVMCI::CodeInstallResult CodeInstaller::install_runtime_stub(CodeBlob*& cb,
665 const char* name,
666 CodeBuffer* buffer,
667 int stack_slots,
668 JVMCI_TRAPS) {
669 if (name == nullptr) {
670 JVMCI_ERROR_OK("stub should have a name");
671 }
672
673 name = os::strdup(name);
674 GrowableArray<RuntimeStub*> *stubs_to_free = nullptr;
675 #ifdef ASSERT
676 const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceRuntimeStubAllocFail");
677 if (val != nullptr && strstr(name , val) != nullptr) {
678 stubs_to_free = new GrowableArray<RuntimeStub*>();
679 JVMCI_event_1("forcing allocation of %s in code cache to fail", name);
680 }
681 #endif
682
683 do {
684 RuntimeStub* stub = RuntimeStub::new_runtime_stub(name,
685 buffer,
686 _offsets.value(CodeOffsets::Frame_Complete),
687 stack_slots,
688 _debug_recorder->_oopmaps,
689 /* caller_must_gc_arguments */ false,
690 /* alloc_fail_is_fatal */ false);
691 cb = stub;
692 if (stub == nullptr) {
693 // Allocation failed
694 #ifdef ASSERT
695 if (stubs_to_free != nullptr) {
696 JVMCI_event_1("allocation of %s in code cache failed, freeing %d stubs", name, stubs_to_free->length());
697 for (GrowableArrayIterator<RuntimeStub*> iter = stubs_to_free->begin(); iter != stubs_to_free->end(); ++iter) {
698 RuntimeStub::free(*iter);
699 }
700 }
701 #endif
702 return JVMCI::cache_full;
703 }
704 if (stubs_to_free == nullptr) {
705 return JVMCI::ok;
706 }
707 stubs_to_free->append(stub);
708 } while (true);
709 }
710
711 JVMCI::CodeInstallResult CodeInstaller::install(JVMCICompiler* compiler,
712 jlong compiled_code_buffer,
713 bool with_type_info,
714 JVMCIObject compiled_code,
715 objArrayHandle object_pool,
716 CodeBlob*& cb,
717 JVMCINMethodHandle& nmethod_handle,
718 JVMCIObject installed_code,
719 FailedSpeculation** failed_speculations,
720 char* speculations,
721 int speculations_len,
722 JVMCI_TRAPS) {
723
724 JavaThread* thread = JavaThread::current();
725 HotSpotCompiledCodeStream* stream = new HotSpotCompiledCodeStream(thread, (const u1*) compiled_code_buffer, with_type_info, object_pool);
726
727 u1 code_flags = stream->read_u1("code:flags");
728 bool is_nmethod = is_set(code_flags, HCC_IS_NMETHOD);
729 const char* name = stream->read_utf8("name", JVMCI_CHECK_OK);
730
731 methodHandle method;
732 jint entry_bci = -1;
733 JVMCICompileState* compile_state = nullptr;
734 bool has_unsafe_access = false;
735 bool has_scoped_access = false;
736 jint id = -1;
737
738 if (is_nmethod) {
739 method = methodHandle(thread, stream->read_method("method"));
740 entry_bci = is_nmethod ? stream->read_s4("entryBCI") : -1;
741 compile_state = (JVMCICompileState*) stream->read_u8("compileState");
742 has_unsafe_access = stream->read_bool("hasUnsafeAccess");
743 has_scoped_access = stream->read_bool("hasScopedAccess");
744 id = stream->read_s4("id");
745 }
746 stream->set_code_desc(name, method);
747
748 CodeBuffer buffer("JVMCI Compiler CodeBuffer");
749 OopRecorder* recorder = new OopRecorder(&_arena, true);
750 initialize_dependencies(stream, code_flags, recorder, JVMCI_CHECK_OK);
751
752 // Get instructions and constants CodeSections early because we need it.
753 _instructions = buffer.insts();
754 _constants = buffer.consts();
755
756 initialize_fields(stream, code_flags, method, buffer, JVMCI_CHECK_OK);
757 JVMCI::CodeInstallResult result = initialize_buffer(compiled_code, buffer, stream, code_flags, JVMCI_CHECK_OK);
758
759 u4 available = stream->available();
760 if (result == JVMCI::ok && available != 0) {
761 JVMCI_ERROR_OK("%d bytes remaining in stream%s", available, stream->context());
762 }
763
764 if (result != JVMCI::ok) {
765 return result;
766 }
767
768 int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
769
770 if (!is_nmethod) {
771 return install_runtime_stub(cb, name, &buffer, stack_slots, JVMCI_CHECK_OK);
772 } else {
773 if (compile_state != nullptr) {
774 jvmci_env()->set_compile_state(compile_state);
775 }
776
777 if (id == -1) {
778 // Make sure a valid compile_id is associated with every compile
779 id = CompileBroker::assign_compile_id_unlocked(thread, method, entry_bci);
780 jvmci_env()->set_HotSpotCompiledNmethod_id(compiled_code, id);
781 }
782 if (!jvmci_env()->isa_HotSpotNmethod(installed_code)) {
783 JVMCI_THROW_MSG_(IllegalArgumentException, "InstalledCode object must be a HotSpotNmethod when installing a HotSpotCompiledNmethod", JVMCI::ok);
784 }
785
786 // Enforce that compiled methods have an nmethod barrier.
787 if (_nmethod_entry_patch_offset == -1) {
788 JVMCI_THROW_MSG_(IllegalArgumentException, "nmethod entry barrier is missing", JVMCI::ok);
789 }
790
791 JVMCIObject mirror = installed_code;
792 nmethod* nm = nullptr; // nm is an out parameter of register_method
793 result = runtime()->register_method(jvmci_env(),
794 method,
795 nm,
796 entry_bci,
797 &_offsets,
798 _orig_pc_offset,
799 &buffer,
800 stack_slots,
801 _debug_recorder->_oopmaps,
802 &_exception_handler_table,
803 &_implicit_exception_table,
804 compiler,
805 _debug_recorder,
806 _dependencies,
807 id,
808 _has_monitors,
809 has_unsafe_access,
810 has_scoped_access,
811 _has_wide_vector,
812 compiled_code,
813 mirror,
814 failed_speculations,
815 speculations,
816 speculations_len,
817 _nmethod_entry_patch_offset);
818 if (result == JVMCI::ok) {
819 guarantee(nm != nullptr, "successful compile must produce an nmethod");
820 nmethod_handle.set_nmethod(nm);
821 cb = nm;
822 if (compile_state == nullptr) {
823 // This compile didn't come through the CompileBroker so perform the printing here
824 DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler);
825 nm->maybe_print_nmethod(directive);
826 DirectivesStack::release(directive);
827
828 // Since this compilation didn't pass through the broker it wasn't logged yet.
829 if (PrintCompilation) {
830 ttyLocker ttyl;
831 if (name != nullptr) {
832 stringStream st;
833 st.print_cr("(hosted JVMCI compilation: %s)", name);
834 CompileTask::print(tty, nm, st.as_string());
835 } else {
836 CompileTask::print(tty, nm, "(hosted JVMCI compilation)");
837 }
838 }
839 }
840
841 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
842
843 // an empty error buffer for use by the verify_barrier code
844 err_msg msg("");
845 if (!bs_nm->verify_barrier(nm, msg)) {
846 JVMCI_THROW_MSG_(IllegalArgumentException, err_msg("nmethod entry barrier is malformed: %s", msg.buffer()), JVMCI::ok);
847 }
848 }
849 }
850
851 if (cb != nullptr) {
852 // Make sure the pre-calculated constants section size was correct.
853 guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);
854 }
855 return result;
856 }
857
858 void CodeInstaller::initialize_fields(HotSpotCompiledCodeStream* stream, u1 code_flags, methodHandle& method, CodeBuffer& buffer, JVMCI_TRAPS) {
859 if (!method.is_null()) {
860 _parameter_count = method->size_of_parameters();
861 JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());
862 } else {
863 // Must be a HotSpotCompiledCode for a stub.
864 // Only used in OopMap constructor for non-product builds
865 _parameter_count = 0;
866 }
867 _sites_count = stream->read_s4("sites:length");
868 _code_size = stream->read_s4("targetCodeSize");
869 _total_frame_size = stream->read_s4("totalFrameSize");
870 if (!is_set(code_flags, HCC_HAS_DEOPT_RESCUE_SLOT)) {
871 _orig_pc_offset = -1;
872 } else {
873 _orig_pc_offset = stream->read_s4("offset");
874 if (stream->read_bool("addRawFrameSize")) {
875 _orig_pc_offset += _total_frame_size;
876 }
877 if (_orig_pc_offset < 0) {
878 JVMCI_ERROR("invalid deopt rescue slot: %d%s", _orig_pc_offset, stream->context());
879 }
880 }
881
882 // Pre-calculate the constants section size. This is required for PC-relative addressing.
883 u4 data_section_size = stream->read_u4("dataSectionSize");
884 u1 data_section_alignment = stream->read_u1("dataSectionAlignment");
885 buffer.set_const_section_alignment(data_section_alignment);
886 if ((_constants->alignment() % data_section_alignment) != 0) {
887 JVMCI_ERROR("invalid data section alignment: %d [constants alignment: %d]%s",
888 data_section_alignment, _constants->alignment(), stream->context());
889 }
890 _constants_size = data_section_size;
891 _next_call_type = INVOKE_INVALID;
892 _has_monitors = false;
893 _has_wide_vector = false;
894 _nmethod_entry_patch_offset = -1;
895 }
896
897 u1 CodeInstaller::as_read_oop_tag(HotSpotCompiledCodeStream* stream, u1 patch_object_tag, JVMCI_TRAPS) {
898 switch (patch_object_tag) {
899 case PATCH_OBJECT_ID:
900 case PATCH_NARROW_OBJECT_ID: {
901 return OBJECT_ID;
902 }
903 case PATCH_OBJECT_ID2:
904 case PATCH_NARROW_OBJECT_ID2: {
905 return OBJECT_ID2;
906 }
907 case PATCH_NARROW_JOBJECT:
908 case PATCH_JOBJECT: {
909 return JOBJECT;
910 }
911 default: {
912 JVMCI_ERROR_0("unknown object patch tag: %d%s", patch_object_tag, stream->context());
913 }
914 }
915 }
916
917 int CodeInstaller::estimate_stubs_size(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
918 // Estimate the number of static call stubs that might be emitted.
919 u2 static_call_stubs = stream->read_u2("numStaticCallStubs");
920 u2 trampoline_stubs = stream->read_u2("numTrampolineStubs");
921 int size = static_call_stubs * CompiledDirectCall::to_interp_stub_size();
922 size += trampoline_stubs * CompiledDirectCall::to_trampoline_stub_size();
923 return size;
924 }
925
926 // perform data and call relocation on the CodeBuffer
927 JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(JVMCIObject compiled_code, CodeBuffer& buffer, HotSpotCompiledCodeStream* stream, u1 code_flags, JVMCI_TRAPS) {
928 JavaThread* thread = stream->thread();
929 HandleMark hm(thread);
930 int locs_buffer_size = _sites_count * (relocInfo::length_limit + sizeof(relocInfo));
931
932
933 // Allocate enough space in the stub section for the static call
934 // stubs. Stubs have extra relocs but they are managed by the stub
935 // section itself so they don't need to be accounted for in the
936 // locs_buffer above.
937 int stubs_size = estimate_stubs_size(stream, JVMCI_CHECK_OK);
938
939 assert((CodeBuffer::SECT_INSTS == CodeBuffer::SECT_STUBS - 1) &&
940 (CodeBuffer::SECT_CONSTS == CodeBuffer::SECT_INSTS - 1), "sections order: consts, insts, stubs");
941 // buffer content: [constants + code_align] + [code + stubs_align] + [stubs]
942 int total_size = align_up(_constants_size, buffer.insts()->alignment()) +
943 align_up(_code_size, buffer.stubs()->alignment()) +
944 stubs_size;
945
946 if (total_size > JVMCINMethodSizeLimit) {
947 return JVMCI::code_too_large;
948 }
949
950 buffer.initialize(total_size, locs_buffer_size);
951 if (buffer.blob() == nullptr) {
952 return JVMCI::cache_full;
953 }
954 buffer.initialize_stubs_size(stubs_size);
955 buffer.initialize_consts_size(_constants_size);
956
957 _debug_recorder = new DebugInformationRecorder(_oop_recorder);
958 _debug_recorder->set_oopmaps(new OopMapSet());
959
960 buffer.initialize_oop_recorder(_oop_recorder);
961
962 // copy the constant data into the newly created CodeBuffer
963 address end_data = _constants->start() + _constants_size;
964 JVMCIObject data_section = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);
965 JVMCIENV->copy_bytes_to(data_section, (jbyte*) _constants->start(), 0, _constants_size);
966 _constants->set_end(end_data);
967
968 // copy the code into the newly created CodeBuffer
969 address end_pc = _instructions->start() + _code_size;
970 guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");
971
972 JVMCIPrimitiveArray code = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);
973 JVMCIENV->copy_bytes_to(code, (jbyte*) _instructions->start(), 0, _code_size);
974 _instructions->set_end(end_pc);
975
976
977 u2 length = stream->read_u2("dataSectionPatches:length");
978 for (int i = 0; i < length; i++) {
979 address dest = _constants->start() + stream->read_u4("patch:pcOffset");
980 u1 tag = stream->read_u1("tag");
981
982 switch (tag) {
983 case PATCH_METHOD:
984 case PATCH_KLASS: {
985 *((void**) dest) = record_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
986 break;
987 }
988 case PATCH_NARROW_KLASS: {
989 #ifdef _LP64
990 *((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
991 #else
992 JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");
993 #endif
994 break;
995 }
996 case PATCH_OBJECT_ID:
997 case PATCH_OBJECT_ID2:
998 case PATCH_NARROW_OBJECT_ID:
999 case PATCH_NARROW_OBJECT_ID2:
1000 case PATCH_JOBJECT:
1001 case PATCH_NARROW_JOBJECT: {
1002 bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2 || tag == PATCH_NARROW_JOBJECT;
1003 u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK_OK);
1004 record_oop_patch(stream, dest, read_tag, narrow, JVMCI_CHECK_OK);
1005 break;
1006 }
1007 default: {
1008 JVMCI_ERROR_OK("invalid constant tag: %d%s", tag, stream->context());
1009 break;
1010 }
1011 }
1012 }
1013
1014 jint last_pc_offset = -1;
1015 for (int i = 0; i < _sites_count; i++) {
1016 u4 pc_offset = stream->read_s4("site:pcOffset");
1017 u1 tag = stream->read_u1("tag");
1018 switch (tag) {
1019 case SITE_FOREIGN_CALL:
1020 case SITE_FOREIGN_CALL_NO_DEBUG_INFO:
1021 case SITE_CALL: {
1022 site_Call(buffer, tag, pc_offset, stream, JVMCI_CHECK_OK);
1023 break;
1024 }
1025 case SITE_SAFEPOINT:
1026 case SITE_IMPLICIT_EXCEPTION:
1027 case SITE_IMPLICIT_EXCEPTION_DISPATCH: {
1028 site_Safepoint(buffer, pc_offset, stream, tag, JVMCI_CHECK_OK);
1029 break;
1030 }
1031 case SITE_INFOPOINT: {
1032 site_Infopoint(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1033 break;
1034 }
1035 case SITE_MARK: {
1036 site_Mark(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1037 break;
1038 }
1039 case SITE_DATA_PATCH: {
1040 site_DataPatch(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1041 break;
1042 }
1043 case SITE_EXCEPTION_HANDLER: {
1044 site_ExceptionHandler(pc_offset, stream);
1045 break;
1046 }
1047 default: {
1048 JVMCI_ERROR_OK("unexpected site tag at " INTPTR_FORMAT ": %d", p2i(stream->pos() - 1), tag);
1049 }
1050 }
1051
1052 last_pc_offset = pc_offset;
1053
1054 if ((i % 32 == 0) && SafepointMechanism::should_process(thread)) {
1055 // Force a safepoint to mitigate pause time installing large code
1056 ThreadToNativeFromVM ttnfv(thread);
1057 }
1058 }
1059
1060 if (is_set(code_flags, HCC_HAS_COMMENTS)) {
1061 u2 length = stream->read_u2("comments:length");
1062 for (int i = 0; i < length; i++) {
1063 u4 pc_offset = stream->read_u4("comment:pcOffset");
1064 const char* text = stream->read_utf8("comment:text", JVMCI_CHECK_OK);
1065 #ifndef PRODUCT
1066 buffer.block_comment(pc_offset, text);
1067 #endif
1068 }
1069 }
1070 if (_has_auto_box) {
1071 JavaThread* THREAD = thread; // For exception macros.
1072 JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));
1073 }
1074 return JVMCI::ok;
1075 }
1076
1077 void CodeInstaller::record_oop_patch(HotSpotCompiledCodeStream* stream, address dest, u1 read_tag, bool narrow, JVMCI_TRAPS) {
1078 Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1079 jobject value = JNIHandles::make_local(obj());
1080 int oop_index = _oop_recorder->find_index(value);
1081 if (narrow) {
1082 #ifdef _LP64
1083 _constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);
1084 #else
1085 JVMCI_ERROR("unexpected compressed oop in 32-bit mode");
1086 #endif
1087 } else {
1088 _constants->relocate(dest, oop_Relocation::spec(oop_index));
1089 }
1090 }
1091
1092 void CodeInstaller::site_ExceptionHandler(jint pc_offset, HotSpotCompiledCodeStream* stream) {
1093 u4 handler_offset = stream->read_u4("site:handlerPos");
1094
1095 // Subtable header
1096 _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
1097
1098 // Subtable entry
1099 _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
1100 }
1101
1102 void CodeInstaller::read_virtual_objects(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1103 u2 length = stream->read_u2("virtualObjects:length");
1104 if (length == 0) {
1105 return;
1106 }
1107 GrowableArray<ScopeValue*> *objects = new GrowableArray<ScopeValue*>(length, length, nullptr);
1108 stream->set_virtual_objects(objects);
1109 // Create the unique ObjectValues
1110 JavaThread* thread = stream->thread();
1111 for (int id = 0; id < length; id++) {
1112 Klass* klass = stream->read_klass("type");
1113 bool is_auto_box = stream->read_bool("isAutoBox");
1114 if (is_auto_box) {
1115 _has_auto_box = true;
1116 }
1117 oop javaMirror = klass->java_mirror();
1118 ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(javaMirror));
1119 ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
1120 objects->at_put(id, sv);
1121 }
1122 // All the values which could be referenced by the VirtualObjects
1123 // exist, so now describe all the VirtualObjects themselves.
1124 for (int id = 0; id < length; id++) {
1125 record_object_value(objects->at(id)->as_ObjectValue(), stream, JVMCI_CHECK);
1126 }
1127 _debug_recorder->dump_object_pool(objects);
1128
1129 stream->set_virtual_objects(objects);
1130 }
1131
1132 int CodeInstaller::map_jvmci_bci(int bci) {
1133 if (bci < 0) {
1134 switch (bci) {
1135 case BEFORE_BCI: return BeforeBci;
1136 case AFTER_BCI: return AfterBci;
1137 case UNWIND_BCI: return UnwindBci;
1138 case AFTER_EXCEPTION_BCI: return AfterExceptionBci;
1139 case UNKNOWN_BCI: return UnknownBci;
1140 case INVALID_FRAMESTATE_BCI: return InvalidFrameStateBci;
1141 }
1142 ShouldNotReachHere();
1143 }
1144 return bci;
1145 }
1146
1147 void CodeInstaller::record_scope(jint pc_offset, HotSpotCompiledCodeStream* stream, u1 debug_info_flags, bool full_info, bool return_oop, JVMCI_TRAPS) {
1148 if (full_info) {
1149 read_virtual_objects(stream, JVMCI_CHECK);
1150 }
1151 if (is_set(debug_info_flags, DI_HAS_FRAMES)) {
1152 u2 depth = stream->read_u2("depth");
1153 for (int i = 0; i < depth; i++) {
1154 Thread* thread = Thread::current();
1155 methodHandle method(thread, stream->read_method("method"));
1156 jint bci = map_jvmci_bci(stream->read_s4("bci"));
1157 if (bci == BEFORE_BCI) {
1158 bci = SynchronizationEntryBCI;
1159 }
1160
1161 JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());
1162
1163 bool reexecute = false;
1164 bool rethrow_exception = false;
1165
1166 DebugToken* locals_token = nullptr;
1167 DebugToken* stack_token = nullptr;
1168 DebugToken* monitors_token = nullptr;
1169
1170 if (full_info) {
1171 u1 frame_flags = stream->read_u1("flags");
1172 rethrow_exception = is_set(frame_flags, DIF_RETHROW_EXCEPTION);
1173
1174 if (bci >= 0) {
1175 reexecute = !is_set(frame_flags, DIF_DURING_CALL);
1176 }
1177
1178 GrowableArray<ScopeValue*>* locals = read_local_or_stack_values(stream, frame_flags, true, JVMCI_CHECK);
1179 GrowableArray<ScopeValue*>* stack = read_local_or_stack_values(stream, frame_flags, false, JVMCI_CHECK);
1180 GrowableArray<MonitorValue*>* monitors = read_monitor_values(stream, frame_flags, JVMCI_CHECK);
1181
1182 locals_token = _debug_recorder->create_scope_values(locals);
1183 stack_token = _debug_recorder->create_scope_values(stack);
1184 monitors_token = _debug_recorder->create_monitor_values(monitors);
1185 }
1186
1187 // has_ea_local_in_scope and arg_escape should be added to JVMCI
1188 const bool return_scalarized = false;
1189 const bool has_ea_local_in_scope = false;
1190 const bool arg_escape = false;
1191 _debug_recorder->describe_scope(pc_offset, method, nullptr, bci, reexecute, rethrow_exception, return_oop,
1192 return_scalarized, has_ea_local_in_scope, arg_escape,
1193 locals_token, stack_token, monitors_token);
1194 }
1195 }
1196 if (full_info) {
1197 // Clear the virtual objects as they are specific to one DebugInfo
1198 stream->set_virtual_objects(nullptr);
1199 }
1200 }
1201
1202 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
1203 u1 flags = stream->read_u1("debugInfo:flags");
1204 OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1205 _debug_recorder->add_safepoint(pc_offset, map);
1206 record_scope(pc_offset, stream, flags, true, JVMCI_CHECK);
1207 _debug_recorder->end_safepoint(pc_offset);
1208 if (_orig_pc_offset < 0) {
1209 JVMCI_ERROR("method contains safepoint, but has no deopt rescue slot");
1210 }
1211 if (tag == SITE_IMPLICIT_EXCEPTION_DISPATCH) {
1212 jint dispatch_offset = stream->read_s4("dispatchOffset");
1213 _implicit_exception_table.append(pc_offset, dispatch_offset);
1214 } else if (tag == SITE_IMPLICIT_EXCEPTION) {
1215 _implicit_exception_table.add_deoptimize(pc_offset);
1216 }
1217 }
1218
1219 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1220 u1 flags = stream->read_u1("debugInfo:flags");
1221 _debug_recorder->add_non_safepoint(pc_offset);
1222 record_scope(pc_offset, stream, flags, false, JVMCI_CHECK);
1223 _debug_recorder->end_non_safepoint(pc_offset);
1224 }
1225
1226 void CodeInstaller::site_Call(CodeBuffer& buffer, u1 tag, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1227 JavaThread* thread = stream->thread();
1228 jlong target = stream->read_u8("target");
1229 methodHandle method;
1230 bool direct_call = false;
1231 if (tag == SITE_CALL) {
1232 method = methodHandle(thread, (Method*) target);
1233 assert(Method::is_valid_method(method()), "invalid method");
1234 direct_call = stream->read_bool("direct");
1235 if (method.is_null()) {
1236 JVMCI_THROW(NullPointerException);
1237 }
1238 }
1239
1240 NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
1241 jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, JVMCI_CHECK);
1242
1243 if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1244 u1 flags = stream->read_u1("debugInfo:flags");
1245 OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1246 _debug_recorder->add_safepoint(next_pc_offset, map);
1247
1248 if (!method.is_null()) {
1249 bool return_oop = method->is_returning_oop();
1250 record_scope(next_pc_offset, stream, flags, true, return_oop, JVMCI_CHECK);
1251 } else {
1252 record_scope(next_pc_offset, stream, flags, true, JVMCI_CHECK);
1253 }
1254 }
1255
1256 if (tag != SITE_CALL) {
1257 jlong foreign_call_destination = target;
1258 CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);
1259 } else {
1260 CodeInstaller::pd_relocate_JavaMethod(buffer, method, pc_offset, JVMCI_CHECK);
1261 if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {
1262 // Need a static call stub for transitions from compiled to interpreted.
1263 MacroAssembler masm(&buffer);
1264 if (CompiledDirectCall::emit_to_interp_stub(&masm, _instructions->start() + pc_offset) == nullptr) {
1265 JVMCI_ERROR("could not emit to_interp stub - code cache is full");
1266 }
1267 }
1268 }
1269
1270 _next_call_type = INVOKE_INVALID;
1271
1272 if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1273 _debug_recorder->end_safepoint(next_pc_offset);
1274 }
1275 }
1276
1277 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1278 u1 tag = stream->read_u1("tag");
1279 switch (tag) {
1280 case PATCH_OBJECT_ID:
1281 case PATCH_OBJECT_ID2:
1282 case PATCH_NARROW_OBJECT_ID:
1283 case PATCH_NARROW_OBJECT_ID2:
1284 case PATCH_JOBJECT:
1285 case PATCH_NARROW_JOBJECT: {
1286 bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2 || tag == PATCH_NARROW_JOBJECT;
1287 u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK);
1288 Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1289 pd_patch_OopConstant(pc_offset, obj, narrow, JVMCI_CHECK);
1290 break;
1291 }
1292 case PATCH_METHOD:
1293 case PATCH_KLASS:
1294 case PATCH_NARROW_KLASS: {
1295 pd_patch_MetaspaceConstant(pc_offset, stream, tag, JVMCI_CHECK);
1296 break;
1297 }
1298 case PATCH_DATA_SECTION_REFERENCE: {
1299 int data_offset = stream->read_u4("data:offset");
1300 if (0 <= data_offset && data_offset < _constants_size) {
1301 if (!is_aligned(data_offset, CompilerToVM::Data::get_data_section_item_alignment())) {
1302 JVMCI_ERROR("data offset 0x%x is not %d-byte aligned%s", data_offset, relocInfo::addr_unit(), stream->context());
1303 }
1304 pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);
1305 } else {
1306 JVMCI_ERROR("data offset 0x%x points outside data section (size 0x%x)%s", data_offset, _constants_size, stream->context());
1307 }
1308 break;
1309 }
1310 default: {
1311 JVMCI_ERROR("unknown data patch tag: %d%s", tag, stream->context());
1312 }
1313 }
1314 }
1315
1316 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1317 u1 id = stream->read_u1("mark:id");
1318 address pc = _instructions->start() + pc_offset;
1319
1320 if (pd_relocate(pc, id)) {
1321 return;
1322 }
1323
1324 switch (id) {
1325 case UNVERIFIED_ENTRY:
1326 _offsets.set_value(CodeOffsets::Entry, pc_offset);
1327 break;
1328 case VERIFIED_ENTRY:
1329 _offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);
1330 _offsets.set_value(CodeOffsets::Verified_Inline_Entry, pc_offset);
1331 _offsets.set_value(CodeOffsets::Verified_Inline_Entry_RO, pc_offset);
1332 break;
1333 case OSR_ENTRY:
1334 _offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);
1335 break;
1336 case EXCEPTION_HANDLER_ENTRY:
1337 _offsets.set_value(CodeOffsets::Exceptions, pc_offset);
1338 break;
1339 case DEOPT_HANDLER_ENTRY:
1340 _offsets.set_value(CodeOffsets::Deopt, pc_offset);
1341 break;
1342 case FRAME_COMPLETE:
1343 _offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
1344 break;
1345 case ENTRY_BARRIER_PATCH:
1346 _nmethod_entry_patch_offset = pc_offset;
1347 break;
1348 case INVOKEVIRTUAL:
1349 case INVOKEINTERFACE:
1350 case INLINE_INVOKE:
1351 case INVOKESTATIC:
1352 case INVOKESPECIAL:
1353 _next_call_type = (MarkId) id;
1354 _invoke_mark_pc = pc;
1355 break;
1356 case CARD_TABLE_SHIFT:
1357 case CARD_TABLE_ADDRESS:
1358 case HEAP_TOP_ADDRESS:
1359 case HEAP_END_ADDRESS:
1360 case NARROW_KLASS_BASE_ADDRESS:
1361 case NARROW_OOP_BASE_ADDRESS:
1362 case CRC_TABLE_ADDRESS:
1363 case LOG_OF_HEAP_REGION_GRAIN_BYTES:
1364 case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:
1365 case VERIFY_OOPS:
1366 case VERIFY_OOP_BITS:
1367 case VERIFY_OOP_MASK:
1368 case VERIFY_OOP_COUNT_ADDRESS:
1369 case DEOPT_MH_HANDLER_ENTRY:
1370 break;
1371
1372 default:
1373 JVMCI_ERROR("invalid mark id: %d%s", id, stream->context());
1374 break;
1375 }
1376 }