1 /*
2 * Copyright (c) 2011, 2026, 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 CompilerDirectiveMatcher matcher(method, CompLevel_full_optimization);
825 nm->maybe_print_nmethod(matcher.directive_set());
826
827 // Since this compilation didn't pass through the broker it wasn't logged yet.
828 if (PrintCompilation) {
829 ttyLocker ttyl;
830 if (name != nullptr) {
831 stringStream st;
832 st.print_cr("(hosted JVMCI compilation: %s)", name);
833 CompileTask::print(tty, nm, st.as_string());
834 } else {
835 CompileTask::print(tty, nm, "(hosted JVMCI compilation)");
836 }
837 }
838 }
839
840 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
841
842 // an empty error buffer for use by the verify_barrier code
843 err_msg msg("");
844 if (!bs_nm->verify_barrier(nm, msg)) {
845 JVMCI_THROW_MSG_(IllegalArgumentException, err_msg("nmethod entry barrier is malformed: %s", msg.buffer()), JVMCI::ok);
846 }
847 }
848 }
849
850 if (cb != nullptr) {
851 // Make sure the pre-calculated constants section size was correct.
852 guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);
853 }
854 return result;
855 }
856
857 void CodeInstaller::initialize_fields(HotSpotCompiledCodeStream* stream, u1 code_flags, methodHandle& method, CodeBuffer& buffer, JVMCI_TRAPS) {
858 if (!method.is_null()) {
859 _parameter_count = method->size_of_parameters();
860 JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());
861 } else {
862 // Must be a HotSpotCompiledCode for a stub.
863 // Only used in OopMap constructor for non-product builds
864 _parameter_count = 0;
865 }
866 _sites_count = stream->read_s4("sites:length");
867 _code_size = stream->read_s4("targetCodeSize");
868 _total_frame_size = stream->read_s4("totalFrameSize");
869 if (!is_set(code_flags, HCC_HAS_DEOPT_RESCUE_SLOT)) {
870 _orig_pc_offset = -1;
871 } else {
872 _orig_pc_offset = stream->read_s4("offset");
873 if (stream->read_bool("addRawFrameSize")) {
874 _orig_pc_offset += _total_frame_size;
875 }
876 if (_orig_pc_offset < 0) {
877 JVMCI_ERROR("invalid deopt rescue slot: %d%s", _orig_pc_offset, stream->context());
878 }
879 }
880
881 // Pre-calculate the constants section size. This is required for PC-relative addressing.
882 u4 data_section_size = stream->read_u4("dataSectionSize");
883 u1 data_section_alignment = stream->read_u1("dataSectionAlignment");
884 buffer.set_const_section_alignment(data_section_alignment);
885 if ((_constants->alignment() % data_section_alignment) != 0) {
886 JVMCI_ERROR("invalid data section alignment: %d [constants alignment: %d]%s",
887 data_section_alignment, _constants->alignment(), stream->context());
888 }
889 _constants_size = data_section_size;
890 _next_call_type = INVOKE_INVALID;
891 _has_monitors = false;
892 _has_wide_vector = false;
893 _nmethod_entry_patch_offset = -1;
894 }
895
896 u1 CodeInstaller::as_read_oop_tag(HotSpotCompiledCodeStream* stream, u1 patch_object_tag, JVMCI_TRAPS) {
897 switch (patch_object_tag) {
898 case PATCH_OBJECT_ID:
899 case PATCH_NARROW_OBJECT_ID: {
900 return OBJECT_ID;
901 }
902 case PATCH_OBJECT_ID2:
903 case PATCH_NARROW_OBJECT_ID2: {
904 return OBJECT_ID2;
905 }
906 case PATCH_NARROW_JOBJECT:
907 case PATCH_JOBJECT: {
908 return JOBJECT;
909 }
910 default: {
911 JVMCI_ERROR_0("unknown object patch tag: %d%s", patch_object_tag, stream->context());
912 }
913 }
914 }
915
916 int CodeInstaller::estimate_stubs_size(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
917 // Estimate the number of static call stubs that might be emitted.
918 u2 static_call_stubs = stream->read_u2("numStaticCallStubs");
919 u2 trampoline_stubs = stream->read_u2("numTrampolineStubs");
920 int size = static_call_stubs * CompiledDirectCall::to_interp_stub_size();
921 size += trampoline_stubs * CompiledDirectCall::to_trampoline_stub_size();
922 return size;
923 }
924
925 // perform data and call relocation on the CodeBuffer
926 JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(JVMCIObject compiled_code, CodeBuffer& buffer, HotSpotCompiledCodeStream* stream, u1 code_flags, JVMCI_TRAPS) {
927 JavaThread* thread = stream->thread();
928 HandleMark hm(thread);
929 int locs_buffer_size = _sites_count * (relocInfo::length_limit + sizeof(relocInfo));
930
931
932 // Allocate enough space in the stub section for the static call
933 // stubs. Stubs have extra relocs but they are managed by the stub
934 // section itself so they don't need to be accounted for in the
935 // locs_buffer above.
936 int stubs_size = estimate_stubs_size(stream, JVMCI_CHECK_OK);
937
938 assert((CodeBuffer::SECT_INSTS == CodeBuffer::SECT_STUBS - 1) &&
939 (CodeBuffer::SECT_CONSTS == CodeBuffer::SECT_INSTS - 1), "sections order: consts, insts, stubs");
940 // buffer content: [constants + code_align] + [code + stubs_align] + [stubs]
941 int total_size = align_up(_constants_size, buffer.insts()->alignment()) +
942 align_up(_code_size, buffer.stubs()->alignment()) +
943 stubs_size;
944
945 if (total_size > JVMCINMethodSizeLimit) {
946 return JVMCI::code_too_large;
947 }
948
949 buffer.initialize(total_size, locs_buffer_size);
950 if (buffer.blob() == nullptr) {
951 return JVMCI::cache_full;
952 }
953 buffer.initialize_stubs_size(stubs_size);
954 buffer.initialize_consts_size(_constants_size);
955
956 _debug_recorder = new DebugInformationRecorder(_oop_recorder);
957 _debug_recorder->set_oopmaps(new OopMapSet());
958
959 buffer.initialize_oop_recorder(_oop_recorder);
960
961 // copy the constant data into the newly created CodeBuffer
962 address end_data = _constants->start() + _constants_size;
963 JVMCIObject data_section = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);
964 JVMCIENV->copy_bytes_to(data_section, (jbyte*) _constants->start(), 0, _constants_size);
965 _constants->set_end(end_data);
966
967 // copy the code into the newly created CodeBuffer
968 address end_pc = _instructions->start() + _code_size;
969 guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");
970
971 JVMCIPrimitiveArray code = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);
972 JVMCIENV->copy_bytes_to(code, (jbyte*) _instructions->start(), 0, _code_size);
973 _instructions->set_end(end_pc);
974
975
976 u2 length = stream->read_u2("dataSectionPatches:length");
977 for (int i = 0; i < length; i++) {
978 address dest = _constants->start() + stream->read_u4("patch:pcOffset");
979 u1 tag = stream->read_u1("tag");
980
981 switch (tag) {
982 case PATCH_METHOD:
983 case PATCH_KLASS: {
984 *((void**) dest) = record_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
985 break;
986 }
987 case PATCH_NARROW_KLASS: {
988 #ifdef _LP64
989 *((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
990 #else
991 JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");
992 #endif
993 break;
994 }
995 case PATCH_OBJECT_ID:
996 case PATCH_OBJECT_ID2:
997 case PATCH_NARROW_OBJECT_ID:
998 case PATCH_NARROW_OBJECT_ID2:
999 case PATCH_JOBJECT:
1000 case PATCH_NARROW_JOBJECT: {
1001 bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2 || tag == PATCH_NARROW_JOBJECT;
1002 u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK_OK);
1003 record_oop_patch(stream, dest, read_tag, narrow, JVMCI_CHECK_OK);
1004 break;
1005 }
1006 default: {
1007 JVMCI_ERROR_OK("invalid constant tag: %d%s", tag, stream->context());
1008 break;
1009 }
1010 }
1011 }
1012
1013 jint last_pc_offset = -1;
1014 for (int i = 0; i < _sites_count; i++) {
1015 u4 pc_offset = stream->read_s4("site:pcOffset");
1016 u1 tag = stream->read_u1("tag");
1017 switch (tag) {
1018 case SITE_FOREIGN_CALL:
1019 case SITE_FOREIGN_CALL_NO_DEBUG_INFO:
1020 case SITE_CALL: {
1021 site_Call(buffer, tag, pc_offset, stream, JVMCI_CHECK_OK);
1022 break;
1023 }
1024 case SITE_SAFEPOINT:
1025 case SITE_IMPLICIT_EXCEPTION:
1026 case SITE_IMPLICIT_EXCEPTION_DISPATCH: {
1027 site_Safepoint(buffer, pc_offset, stream, tag, JVMCI_CHECK_OK);
1028 break;
1029 }
1030 case SITE_INFOPOINT: {
1031 site_Infopoint(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1032 break;
1033 }
1034 case SITE_MARK: {
1035 site_Mark(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1036 break;
1037 }
1038 case SITE_DATA_PATCH: {
1039 site_DataPatch(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1040 break;
1041 }
1042 case SITE_EXCEPTION_HANDLER: {
1043 site_ExceptionHandler(pc_offset, stream);
1044 break;
1045 }
1046 default: {
1047 JVMCI_ERROR_OK("unexpected site tag at " INTPTR_FORMAT ": %d", p2i(stream->pos() - 1), tag);
1048 }
1049 }
1050
1051 last_pc_offset = pc_offset;
1052
1053 if ((i % 32 == 0) && SafepointMechanism::should_process(thread)) {
1054 // Force a safepoint to mitigate pause time installing large code
1055 ThreadToNativeFromVM ttnfv(thread);
1056 }
1057 }
1058
1059 if (is_set(code_flags, HCC_HAS_COMMENTS)) {
1060 u2 length = stream->read_u2("comments:length");
1061 for (int i = 0; i < length; i++) {
1062 u4 pc_offset = stream->read_u4("comment:pcOffset");
1063 const char* text = stream->read_utf8("comment:text", JVMCI_CHECK_OK);
1064 #ifndef PRODUCT
1065 buffer.block_comment(pc_offset, text);
1066 #endif
1067 }
1068 }
1069 if (_has_auto_box) {
1070 JavaThread* THREAD = thread; // For exception macros.
1071 JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));
1072 }
1073 return JVMCI::ok;
1074 }
1075
1076 void CodeInstaller::record_oop_patch(HotSpotCompiledCodeStream* stream, address dest, u1 read_tag, bool narrow, JVMCI_TRAPS) {
1077 Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1078 jobject value = JNIHandles::make_local(obj());
1079 int oop_index = _oop_recorder->find_index(value);
1080 if (narrow) {
1081 #ifdef _LP64
1082 _constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);
1083 #else
1084 JVMCI_ERROR("unexpected compressed oop in 32-bit mode");
1085 #endif
1086 } else {
1087 _constants->relocate(dest, oop_Relocation::spec(oop_index));
1088 }
1089 }
1090
1091 void CodeInstaller::site_ExceptionHandler(jint pc_offset, HotSpotCompiledCodeStream* stream) {
1092 u4 handler_offset = stream->read_u4("site:handlerPos");
1093
1094 // Subtable header
1095 _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
1096
1097 // Subtable entry
1098 _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
1099 }
1100
1101 void CodeInstaller::read_virtual_objects(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1102 u2 length = stream->read_u2("virtualObjects:length");
1103 if (length == 0) {
1104 return;
1105 }
1106 GrowableArray<ScopeValue*> *objects = new GrowableArray<ScopeValue*>(length, length, nullptr);
1107 stream->set_virtual_objects(objects);
1108 // Create the unique ObjectValues
1109 JavaThread* thread = stream->thread();
1110 for (int id = 0; id < length; id++) {
1111 Klass* klass = stream->read_klass("type");
1112 bool is_auto_box = stream->read_bool("isAutoBox");
1113 if (is_auto_box) {
1114 _has_auto_box = true;
1115 }
1116 oop javaMirror = klass->java_mirror();
1117 ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(javaMirror));
1118 ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
1119 objects->at_put(id, sv);
1120 }
1121 // All the values which could be referenced by the VirtualObjects
1122 // exist, so now describe all the VirtualObjects themselves.
1123 for (int id = 0; id < length; id++) {
1124 record_object_value(objects->at(id)->as_ObjectValue(), stream, JVMCI_CHECK);
1125 }
1126 _debug_recorder->dump_object_pool(objects);
1127
1128 stream->set_virtual_objects(objects);
1129 }
1130
1131 int CodeInstaller::map_jvmci_bci(int bci) {
1132 if (bci < 0) {
1133 switch (bci) {
1134 case BEFORE_BCI: return BeforeBci;
1135 case AFTER_BCI: return AfterBci;
1136 case UNWIND_BCI: return UnwindBci;
1137 case AFTER_EXCEPTION_BCI: return AfterExceptionBci;
1138 case UNKNOWN_BCI: return UnknownBci;
1139 case INVALID_FRAMESTATE_BCI: return InvalidFrameStateBci;
1140 }
1141 ShouldNotReachHere();
1142 }
1143 return bci;
1144 }
1145
1146 void CodeInstaller::record_scope(jint pc_offset, HotSpotCompiledCodeStream* stream, u1 debug_info_flags, bool full_info, bool return_oop, JVMCI_TRAPS) {
1147 if (full_info) {
1148 read_virtual_objects(stream, JVMCI_CHECK);
1149 }
1150 if (is_set(debug_info_flags, DI_HAS_FRAMES)) {
1151 u2 depth = stream->read_u2("depth");
1152 for (int i = 0; i < depth; i++) {
1153 Thread* thread = Thread::current();
1154 methodHandle method(thread, stream->read_method("method"));
1155 jint bci = map_jvmci_bci(stream->read_s4("bci"));
1156 if (bci == BEFORE_BCI) {
1157 bci = SynchronizationEntryBCI;
1158 }
1159
1160 JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());
1161
1162 bool reexecute = false;
1163 bool rethrow_exception = false;
1164
1165 DebugToken* locals_token = nullptr;
1166 DebugToken* stack_token = nullptr;
1167 DebugToken* monitors_token = nullptr;
1168
1169 if (full_info) {
1170 u1 frame_flags = stream->read_u1("flags");
1171 rethrow_exception = is_set(frame_flags, DIF_RETHROW_EXCEPTION);
1172
1173 if (bci >= 0) {
1174 reexecute = !is_set(frame_flags, DIF_DURING_CALL);
1175 }
1176
1177 GrowableArray<ScopeValue*>* locals = read_local_or_stack_values(stream, frame_flags, true, JVMCI_CHECK);
1178 GrowableArray<ScopeValue*>* stack = read_local_or_stack_values(stream, frame_flags, false, JVMCI_CHECK);
1179 GrowableArray<MonitorValue*>* monitors = read_monitor_values(stream, frame_flags, JVMCI_CHECK);
1180
1181 locals_token = _debug_recorder->create_scope_values(locals);
1182 stack_token = _debug_recorder->create_scope_values(stack);
1183 monitors_token = _debug_recorder->create_monitor_values(monitors);
1184 }
1185
1186 // has_ea_local_in_scope and arg_escape should be added to JVMCI
1187 const bool return_scalarized = false;
1188 const bool has_ea_local_in_scope = false;
1189 const bool arg_escape = false;
1190 _debug_recorder->describe_scope(pc_offset, method, nullptr, bci, reexecute, rethrow_exception, return_oop,
1191 return_scalarized, has_ea_local_in_scope, arg_escape,
1192 locals_token, stack_token, monitors_token);
1193 }
1194 }
1195 if (full_info) {
1196 // Clear the virtual objects as they are specific to one DebugInfo
1197 stream->set_virtual_objects(nullptr);
1198 }
1199 }
1200
1201 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
1202 u1 flags = stream->read_u1("debugInfo:flags");
1203 OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1204 _debug_recorder->add_safepoint(pc_offset, map);
1205 record_scope(pc_offset, stream, flags, true, JVMCI_CHECK);
1206 _debug_recorder->end_safepoint(pc_offset);
1207 if (_orig_pc_offset < 0) {
1208 JVMCI_ERROR("method contains safepoint, but has no deopt rescue slot");
1209 }
1210 if (tag == SITE_IMPLICIT_EXCEPTION_DISPATCH) {
1211 jint dispatch_offset = stream->read_s4("dispatchOffset");
1212 _implicit_exception_table.append(pc_offset, dispatch_offset);
1213 } else if (tag == SITE_IMPLICIT_EXCEPTION) {
1214 _implicit_exception_table.add_deoptimize(pc_offset);
1215 }
1216 }
1217
1218 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1219 u1 flags = stream->read_u1("debugInfo:flags");
1220 _debug_recorder->add_non_safepoint(pc_offset);
1221 record_scope(pc_offset, stream, flags, false, JVMCI_CHECK);
1222 _debug_recorder->end_non_safepoint(pc_offset);
1223 }
1224
1225 void CodeInstaller::site_Call(CodeBuffer& buffer, u1 tag, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1226 JavaThread* thread = stream->thread();
1227 jlong target = stream->read_u8("target");
1228 methodHandle method;
1229 bool direct_call = false;
1230 if (tag == SITE_CALL) {
1231 method = methodHandle(thread, (Method*) target);
1232 assert(Method::is_valid_method(method()), "invalid method");
1233 direct_call = stream->read_bool("direct");
1234 if (method.is_null()) {
1235 JVMCI_THROW(NullPointerException);
1236 }
1237 }
1238
1239 NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
1240 jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, JVMCI_CHECK);
1241
1242 if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1243 u1 flags = stream->read_u1("debugInfo:flags");
1244 OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1245 _debug_recorder->add_safepoint(next_pc_offset, map);
1246
1247 if (!method.is_null()) {
1248 bool return_oop = method->is_returning_oop();
1249 record_scope(next_pc_offset, stream, flags, true, return_oop, JVMCI_CHECK);
1250 } else {
1251 record_scope(next_pc_offset, stream, flags, true, JVMCI_CHECK);
1252 }
1253 }
1254
1255 if (tag != SITE_CALL) {
1256 jlong foreign_call_destination = target;
1257 CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);
1258 } else {
1259 CodeInstaller::pd_relocate_JavaMethod(buffer, method, pc_offset, JVMCI_CHECK);
1260 if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {
1261 // Need a static call stub for transitions from compiled to interpreted.
1262 MacroAssembler masm(&buffer);
1263 if (CompiledDirectCall::emit_to_interp_stub(&masm, _instructions->start() + pc_offset) == nullptr) {
1264 JVMCI_ERROR("could not emit to_interp stub - code cache is full");
1265 }
1266 }
1267 }
1268
1269 _next_call_type = INVOKE_INVALID;
1270
1271 if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1272 _debug_recorder->end_safepoint(next_pc_offset);
1273 }
1274 }
1275
1276 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1277 u1 tag = stream->read_u1("tag");
1278 switch (tag) {
1279 case PATCH_OBJECT_ID:
1280 case PATCH_OBJECT_ID2:
1281 case PATCH_NARROW_OBJECT_ID:
1282 case PATCH_NARROW_OBJECT_ID2:
1283 case PATCH_JOBJECT:
1284 case PATCH_NARROW_JOBJECT: {
1285 bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2 || tag == PATCH_NARROW_JOBJECT;
1286 u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK);
1287 Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1288 pd_patch_OopConstant(pc_offset, obj, narrow, JVMCI_CHECK);
1289 break;
1290 }
1291 case PATCH_METHOD:
1292 case PATCH_KLASS:
1293 case PATCH_NARROW_KLASS: {
1294 pd_patch_MetaspaceConstant(pc_offset, stream, tag, JVMCI_CHECK);
1295 break;
1296 }
1297 case PATCH_DATA_SECTION_REFERENCE: {
1298 int data_offset = stream->read_u4("data:offset");
1299 if (0 <= data_offset && data_offset < _constants_size) {
1300 if (!is_aligned(data_offset, CompilerToVM::Data::get_data_section_item_alignment())) {
1301 JVMCI_ERROR("data offset 0x%x is not %d-byte aligned%s", data_offset, relocInfo::addr_unit(), stream->context());
1302 }
1303 pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);
1304 } else {
1305 JVMCI_ERROR("data offset 0x%x points outside data section (size 0x%x)%s", data_offset, _constants_size, stream->context());
1306 }
1307 break;
1308 }
1309 default: {
1310 JVMCI_ERROR("unknown data patch tag: %d%s", tag, stream->context());
1311 }
1312 }
1313 }
1314
1315 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1316 u1 id = stream->read_u1("mark:id");
1317 address pc = _instructions->start() + pc_offset;
1318
1319 if (pd_relocate(pc, id)) {
1320 return;
1321 }
1322
1323 switch (id) {
1324 case UNVERIFIED_ENTRY:
1325 _offsets.set_value(CodeOffsets::Entry, pc_offset);
1326 break;
1327 case VERIFIED_ENTRY:
1328 _offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);
1329 _offsets.set_value(CodeOffsets::Verified_Inline_Entry, pc_offset);
1330 _offsets.set_value(CodeOffsets::Verified_Inline_Entry_RO, pc_offset);
1331 break;
1332 case OSR_ENTRY:
1333 _offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);
1334 break;
1335 case EXCEPTION_HANDLER_ENTRY:
1336 _offsets.set_value(CodeOffsets::Exceptions, pc_offset);
1337 break;
1338 case DEOPT_HANDLER_ENTRY:
1339 _offsets.set_value(CodeOffsets::Deopt, pc_offset);
1340 break;
1341 case FRAME_COMPLETE:
1342 _offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
1343 break;
1344 case ENTRY_BARRIER_PATCH:
1345 _nmethod_entry_patch_offset = pc_offset;
1346 break;
1347 case INVOKEVIRTUAL:
1348 case INVOKEINTERFACE:
1349 case INLINE_INVOKE:
1350 case INVOKESTATIC:
1351 case INVOKESPECIAL:
1352 _next_call_type = (MarkId) id;
1353 _invoke_mark_pc = pc;
1354 break;
1355 case CARD_TABLE_SHIFT:
1356 case CARD_TABLE_ADDRESS:
1357 case HEAP_TOP_ADDRESS:
1358 case HEAP_END_ADDRESS:
1359 case NARROW_KLASS_BASE_ADDRESS:
1360 case NARROW_OOP_BASE_ADDRESS:
1361 case CRC_TABLE_ADDRESS:
1362 case LOG_OF_HEAP_REGION_GRAIN_BYTES:
1363 case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:
1364 case VERIFY_OOPS:
1365 case VERIFY_OOP_BITS:
1366 case VERIFY_OOP_MASK:
1367 case VERIFY_OOP_COUNT_ADDRESS:
1368 case DEOPT_MH_HANDLER_ENTRY:
1369 break;
1370
1371 default:
1372 JVMCI_ERROR("invalid mark id: %d%s", id, stream->context());
1373 break;
1374 }
1375 }