1 /*
  2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "asm/macroAssembler.hpp"
 27 #include "code/vtableStubs.hpp"
 28 #include "interp_masm_x86.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "oops/compiledICHolder.hpp"
 31 #include "oops/instanceKlass.hpp"
 32 #include "oops/klassVtable.hpp"
 33 #include "runtime/sharedRuntime.hpp"
 34 #include "vmreg_x86.inline.hpp"
 35 #ifdef COMPILER2
 36 #include "opto/runtime.hpp"
 37 #endif
 38 
 39 // machine-dependent part of VtableStubs: create VtableStub of correct size and
 40 // initialize its code
 41 
 42 #define __ masm->
 43 
 44 #ifndef PRODUCT
 45 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
 46 #endif
 47 
 48 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
 49   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
 50   const int stub_code_length = code_size_limit(true);
 51   Register tmp_load_klass = rscratch1;
 52   VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index);
 53   // Can be NULL if there is no free space in the code cache.
 54   if (s == NULL) {
 55     return NULL;
 56   }
 57 
 58   // Count unused bytes in instruction sequences of variable size.
 59   // We add them to the computed buffer size in order to avoid
 60   // overflow in subsequently generated stubs.
 61   address   start_pc;
 62   int       slop_bytes = 0;
 63   int       slop_delta = 0;
 64   // No variance was detected in vtable stub sizes. Setting index_dependent_slop == 0 will unveil any deviation from this observation.
 65   const int index_dependent_slop     = 0;
 66 
 67   ResourceMark    rm;
 68   CodeBuffer      cb(s->entry_point(), stub_code_length);
 69   MacroAssembler* masm = new MacroAssembler(&cb);
 70 
 71 #if (!defined(PRODUCT) && defined(COMPILER2))
 72   if (CountCompiledCalls) {
 73     __ incrementq(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
 74   }
 75 #endif
 76 
 77   // get receiver (need to skip return address on top of stack)
 78   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
 79 
 80   // Free registers (non-args) are rax, rbx
 81 
 82   // get receiver klass
 83   address npe_addr = __ pc();
 84   __ load_klass(rax, j_rarg0, tmp_load_klass);
 85 
 86 #ifndef PRODUCT
 87   if (DebugVtables) {
 88     Label L;
 89     start_pc = __ pc();
 90     // check offset vs vtable length
 91     __ cmpl(Address(rax, Klass::vtable_length_offset()), vtable_index*vtableEntry::size());
 92     slop_delta  = 12 - (__ pc() - start_pc);  // cmpl varies in length, depending on data
 93     slop_bytes += slop_delta;
 94     assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
 95 
 96     __ jcc(Assembler::greater, L);
 97     __ movl(rbx, vtable_index);
 98     // VTABLE TODO: find upper bound for call_VM length.
 99     start_pc = __ pc();
100     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, rbx);
101     slop_delta  = 550 - (__ pc() - start_pc);
102     slop_bytes += slop_delta;
103     assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
104     __ bind(L);
105   }
106 #endif // PRODUCT
107 
108   const Register method = rbx;
109 
110   // load Method* and target address
111   start_pc = __ pc();
112   __ lookup_virtual_method(rax, vtable_index, method);
113   slop_delta  = 8 - (int)(__ pc() - start_pc);
114   slop_bytes += slop_delta;
115   assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
116 
117 #ifndef PRODUCT
118   if (DebugVtables) {
119     Label L;
120     __ cmpptr(method, (int32_t)NULL_WORD);
121     __ jcc(Assembler::equal, L);
122     __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD);
123     __ jcc(Assembler::notZero, L);
124     __ stop("Vtable entry is NULL");
125     __ bind(L);
126   }
127 #endif // PRODUCT
128 
129   // rax: receiver klass
130   // method (rbx): Method*
131   // rcx: receiver
132   address ame_addr = __ pc();
133   __ jmp( Address(rbx, Method::from_compiled_offset()));
134 
135   masm->flush();
136   slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
137   bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, index_dependent_slop);
138 
139   return s;
140 }
141 
142 
143 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
144   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
145   const int stub_code_length = code_size_limit(false);
146   VtableStub* s = new(stub_code_length) VtableStub(false, itable_index);
147   // Can be NULL if there is no free space in the code cache.
148   if (s == NULL) {
149     return NULL;
150   }
151 
152   // Count unused bytes in instruction sequences of variable size.
153   // We add them to the computed buffer size in order to avoid
154   // overflow in subsequently generated stubs.
155   address   start_pc;
156   int       slop_bytes = 0;
157   int       slop_delta = 0;
158   const int index_dependent_slop = (itable_index == 0) ? 4 :     // code size change with transition from 8-bit to 32-bit constant (@index == 16).
159                                    (itable_index < 16) ? 3 : 0;  // index == 0 generates even shorter code.
160 
161   ResourceMark    rm;
162   CodeBuffer      cb(s->entry_point(), stub_code_length);
163   MacroAssembler *masm = new MacroAssembler(&cb);
164 
165 #if (!defined(PRODUCT) && defined(COMPILER2))
166   if (CountCompiledCalls) {
167     __ incrementq(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
168   }
169 #endif // PRODUCT
170 
171   // Entry arguments:
172   //  rax: CompiledICHolder
173   //  j_rarg0: Receiver
174 
175   // Most registers are in use; we'll use rax, rbx, r10, r11
176   // (various calling sequences use r[cd]x, r[sd]i, r[89]; stay away from them)
177   const Register recv_klass_reg     = r10;
178   const Register holder_klass_reg   = rax; // declaring interface klass (DECC)
179   const Register resolved_klass_reg = rbx; // resolved interface klass (REFC)
180   const Register temp_reg           = r11;
181 
182   const Register icholder_reg = rax;
183   __ movptr(resolved_klass_reg, Address(icholder_reg, CompiledICHolder::holder_klass_offset()));
184   __ movptr(holder_klass_reg,   Address(icholder_reg, CompiledICHolder::holder_metadata_offset()));
185 
186   Label L_no_such_interface;
187 
188   // get receiver klass (also an implicit null-check)
189   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
190   address npe_addr = __ pc();
191   __ load_klass(recv_klass_reg, j_rarg0, temp_reg);
192 
193   start_pc = __ pc();
194 
195   // Receiver subtype check against REFC.
196   // Destroys recv_klass_reg value.
197   __ lookup_interface_method(// inputs: rec. class, interface
198                              recv_klass_reg, resolved_klass_reg, noreg,
199                              // outputs:  scan temp. reg1, scan temp. reg2
200                              recv_klass_reg, temp_reg,
201                              L_no_such_interface,
202                              /*return_method=*/false);
203 
204   const ptrdiff_t  typecheckSize = __ pc() - start_pc;
205   start_pc = __ pc();
206 
207   // Get selected method from declaring class and itable index
208   const Register method = rbx;
209   __ load_klass(recv_klass_reg, j_rarg0, temp_reg);   // restore recv_klass_reg
210   __ lookup_interface_method(// inputs: rec. class, interface, itable index
211                              recv_klass_reg, holder_klass_reg, itable_index,
212                              // outputs: method, scan temp. reg
213                              method, temp_reg,
214                              L_no_such_interface);
215 
216   const ptrdiff_t  lookupSize = __ pc() - start_pc;
217 
218   // We expect we need index_dependent_slop extra bytes. Reason:
219   // The emitted code in lookup_interface_method changes when itable_index exceeds 15.
220   // For linux, a very narrow estimate would be 112, but Solaris requires some more space (130).
221   const ptrdiff_t estimate = 136;
222   const ptrdiff_t codesize = typecheckSize + lookupSize + index_dependent_slop;
223   slop_delta  = (int)(estimate - codesize);
224   slop_bytes += slop_delta;
225   assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
226 
227   // If we take a trap while this arg is on the stack we will not
228   // be able to walk the stack properly. This is not an issue except
229   // when there are mistakes in this assembly code that could generate
230   // a spurious fault. Ask me how I know...
231 
232   // method (rbx): Method*
233   // j_rarg0: receiver
234 
235 #ifdef ASSERT
236   if (DebugVtables) {
237     Label L2;
238     __ cmpptr(method, (int32_t)NULL_WORD);
239     __ jcc(Assembler::equal, L2);
240     __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD);
241     __ jcc(Assembler::notZero, L2);
242     __ stop("compiler entrypoint is null");
243     __ bind(L2);
244   }
245 #endif // ASSERT
246 
247   address ame_addr = __ pc();
248   __ jmp(Address(method, Method::from_compiled_offset()));
249 
250   __ bind(L_no_such_interface);
251   // Handle IncompatibleClassChangeError in itable stubs.
252   // More detailed error message.
253   // We force resolving of the call site by jumping to the "handle
254   // wrong method" stub, and so let the interpreter runtime do all the
255   // dirty work.
256   __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
257 
258   masm->flush();
259   slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
260   bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, index_dependent_slop);
261 
262   return s;
263 }
264 
265 int VtableStub::pd_code_alignment() {
266   // x86 cache line size is 64 bytes, but we want to limit alignment loss.
267   const unsigned int icache_line_size = wordSize;
268   return icache_line_size;
269 }