1 /*
  2  * Copyright (c) 1997, 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 #ifndef SHARE_CODE_COMPILEDIC_HPP
 26 #define SHARE_CODE_COMPILEDIC_HPP
 27 
 28 #include "code/nativeInst.hpp"
 29 #include "interpreter/linkResolver.hpp"
 30 #include "oops/compiledICHolder.hpp"
 31 #include "runtime/safepointVerifiers.hpp"
 32 
 33 //-----------------------------------------------------------------------------
 34 // The CompiledIC represents a compiled inline cache.
 35 //
 36 // In order to make patching of the inline cache MT-safe, we only allow the following
 37 // transitions (when not at a safepoint):
 38 //
 39 //
 40 //         [1] --<--  Clean -->---  [1]
 41 //            /       (null)      \
 42 //           /                     \      /-<-\
 43 //          /          [2]          \    /     \
 44 //      Interpreted  ---------> Monomorphic     | [3]
 45 //  (CompiledICHolder*)            (Klass*)     |
 46 //          \                        /   \     /
 47 //       [4] \                      / [4] \->-/
 48 //            \->-  Megamorphic -<-/
 49 //              (CompiledICHolder*)
 50 //
 51 // The text in parentheses () refers to the value of the inline cache receiver (mov instruction)
 52 //
 53 // The numbers in square brackets refer to the kind of transition:
 54 // [1]: Initial fixup. Receiver it found from debug information
 55 // [2]: Compilation of a method
 56 // [3]: Recompilation of a method (note: only entry is changed. The Klass* must stay the same)
 57 // [4]: Inline cache miss. We go directly to megamorphic call.
 58 //
 59 // The class automatically inserts transition stubs (using the InlineCacheBuffer) when an MT-unsafe
 60 // transition is made to a stub.
 61 //
 62 class CompiledIC;
 63 class CompiledICProtectionBehaviour;
 64 class CompiledMethod;
 65 class ICStub;
 66 
 67 class CompiledICLocker: public StackObj {
 68   CompiledMethod* _method;
 69   CompiledICProtectionBehaviour* _behaviour;
 70   bool _locked;
 71   NoSafepointVerifier _nsv;
 72 
 73 public:
 74   CompiledICLocker(CompiledMethod* method);
 75   ~CompiledICLocker();
 76   static bool is_safe(CompiledMethod* method);
 77   static bool is_safe(address code);
 78 };
 79 
 80 class CompiledICInfo : public StackObj {
 81  private:
 82   address _entry;              // entry point for call
 83   void*   _cached_value;         // Value of cached_value (either in stub or inline cache)
 84   bool    _is_icholder;          // Is the cached value a CompiledICHolder*
 85   bool    _is_optimized;       // it is an optimized virtual call (i.e., can be statically bound)
 86   bool    _to_interpreter;     // Call it to interpreter
 87   bool    _release_icholder;
 88  public:
 89   address entry() const        { return _entry; }
 90   Metadata*    cached_metadata() const         { assert(!_is_icholder, ""); return (Metadata*)_cached_value; }
 91   CompiledICHolder*    claim_cached_icholder() {
 92     assert(_is_icholder, "");
 93     assert(_cached_value != NULL, "must be non-NULL");
 94     _release_icholder = false;
 95     CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
 96     icholder->claim();
 97     return icholder;
 98   }
 99   bool    is_optimized() const { return _is_optimized; }
100   bool  to_interpreter() const { return _to_interpreter; }
101 
102   void set_compiled_entry(address entry, Klass* klass, bool is_optimized) {
103     _entry      = entry;
104     _cached_value = (void*)klass;
105     _to_interpreter = false;
106     _is_icholder = false;
107     _is_optimized = is_optimized;
108     _release_icholder = false;
109   }
110 
111   void set_interpreter_entry(address entry, Method* method) {
112     _entry      = entry;
113     _cached_value = (void*)method;
114     _to_interpreter = true;
115     _is_icholder = false;
116     _is_optimized = true;
117     _release_icholder = false;
118   }
119 
120   void set_icholder_entry(address entry, CompiledICHolder* icholder) {
121     _entry      = entry;
122     _cached_value = (void*)icholder;
123     _to_interpreter = true;
124     _is_icholder = true;
125     _is_optimized = false;
126     _release_icholder = true;
127   }
128 
129   CompiledICInfo(): _entry(NULL), _cached_value(NULL), _is_icholder(false),
130                     _is_optimized(false), _to_interpreter(false), _release_icholder(false) {
131   }
132   ~CompiledICInfo() {
133     // In rare cases the info is computed but not used, so release any
134     // CompiledICHolder* that was created
135     if (_release_icholder) {
136       assert(_is_icholder, "must be");
137       CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
138       icholder->claim();
139       delete icholder;
140     }
141   }
142 };
143 
144 class NativeCallWrapper: public ResourceObj {
145 public:
146   virtual address destination() const = 0;
147   virtual address instruction_address() const = 0;
148   virtual address next_instruction_address() const = 0;
149   virtual address return_address() const = 0;
150   virtual address get_resolve_call_stub(bool is_optimized) const = 0;
151   virtual void set_destination_mt_safe(address dest) = 0;
152   virtual void set_to_interpreted(const methodHandle& method, CompiledICInfo& info) = 0;
153   virtual void verify() const = 0;
154   virtual void verify_resolve_call(address dest) const = 0;
155 
156   virtual bool is_call_to_interpreted(address dest) const = 0;
157   virtual bool is_safe_for_patching() const = 0;
158 
159   virtual NativeInstruction* get_load_instruction(virtual_call_Relocation* r) const = 0;
160 
161   virtual void *get_data(NativeInstruction* instruction) const = 0;
162   virtual void set_data(NativeInstruction* instruction, intptr_t data) = 0;
163 };
164 
165 class CompiledIC: public ResourceObj {
166   friend class InlineCacheBuffer;
167   friend class ICStub;
168 
169  private:
170   NativeCallWrapper* _call;
171   NativeInstruction* _value;    // patchable value cell for this IC
172   bool          _is_optimized;  // an optimized virtual call (i.e., no compiled IC)
173   CompiledMethod* _method;
174 
175   CompiledIC(CompiledMethod* cm, NativeCall* ic_call);
176   CompiledIC(RelocIterator* iter);
177 
178   void initialize_from_iter(RelocIterator* iter);
179 
180   static bool is_icholder_entry(address entry);
181 
182   // low-level inline-cache manipulation. Cannot be accessed directly, since it might not be MT-safe
183   // to change an inline-cache. These changes the underlying inline-cache directly. They *newer* make
184   // changes to a transition stub.
185   void internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder);
186   void set_ic_destination(ICStub* stub);
187   void set_ic_destination(address entry_point) {
188     assert(_is_optimized, "use set_ic_destination_and_value instead");
189     internal_set_ic_destination(entry_point, false, NULL, false);
190   }
191   // This only for use by ICStubs where the type of the value isn't known
192   void set_ic_destination_and_value(address entry_point, void* value) {
193     internal_set_ic_destination(entry_point, false, value, is_icholder_entry(entry_point));
194   }
195   void set_ic_destination_and_value(address entry_point, Metadata* value) {
196     internal_set_ic_destination(entry_point, false, value, false);
197   }
198   void set_ic_destination_and_value(address entry_point, CompiledICHolder* value) {
199     internal_set_ic_destination(entry_point, false, value, true);
200   }
201 
202   // Reads the location of the transition stub. This will fail with an assertion, if no transition stub is
203   // associated with the inline cache.
204   address stub_address() const;
205   bool is_in_transition_state() const;  // Use InlineCacheBuffer
206 
207  public:
208   // conversion (machine PC to CompiledIC*)
209   friend CompiledIC* CompiledIC_before(CompiledMethod* nm, address return_addr);
210   friend CompiledIC* CompiledIC_at(CompiledMethod* nm, address call_site);
211   friend CompiledIC* CompiledIC_at(Relocation* call_site);
212   friend CompiledIC* CompiledIC_at(RelocIterator* reloc_iter);
213 
214   static bool is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm);
215 
216   // Return the cached_metadata/destination associated with this inline cache. If the cache currently points
217   // to a transition stub, it will read the values from the transition stub.
218   void* cached_value() const;
219   CompiledICHolder* cached_icholder() const {
220     assert(is_icholder_call(), "must be");
221     return (CompiledICHolder*) cached_value();
222   }
223   Metadata* cached_metadata() const {
224     assert(!is_icholder_call(), "must be");
225     return (Metadata*) cached_value();
226   }
227 
228   void* get_data() const {
229     return _call->get_data(_value);
230   }
231 
232   void set_data(intptr_t data) {
233     _call->set_data(_value, data);
234   }
235 
236   address ic_destination() const;
237 
238   bool is_optimized() const   { return _is_optimized; }
239 
240   // State
241   bool is_clean() const;
242   bool is_megamorphic() const;
243   bool is_call_to_compiled() const;
244   bool is_call_to_interpreted() const;
245 
246   bool is_icholder_call() const;
247 
248   address end_of_call() const { return  _call->return_address(); }
249 
250   // MT-safe patching of inline caches. Note: Only safe to call is_xxx when holding the CompiledIC_ock
251   // so you are guaranteed that no patching takes place. The same goes for verify.
252   //
253   // Note: We do not provide any direct access to the stub code, to prevent parts of the code
254   // to manipulate the inline cache in MT-unsafe ways.
255   //
256   // They all takes a TRAP argument, since they can cause a GC if the inline-cache buffer is full.
257   //
258   bool set_to_clean(bool in_use = true);
259   bool set_to_monomorphic(CompiledICInfo& info);
260   void clear_ic_stub();
261 
262   // Returns true if successful and false otherwise. The call can fail if memory
263   // allocation in the code cache fails, or ic stub refill is required.
264   bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, bool& needs_ic_stub_refill, TRAPS);
265 
266   static void compute_monomorphic_entry(const methodHandle& method, Klass* receiver_klass,
267                                         bool is_optimized, bool static_bound, bool caller_is_nmethod,
268                                         CompiledICInfo& info, TRAPS);
269 
270   // Location
271   address instruction_address() const { return _call->instruction_address(); }
272 
273   // Misc
274   void print()             PRODUCT_RETURN;
275   void print_compiled_ic() PRODUCT_RETURN;
276   void verify()            PRODUCT_RETURN;
277 };
278 
279 inline CompiledIC* CompiledIC_before(CompiledMethod* nm, address return_addr) {
280   CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr));
281   c_ic->verify();
282   return c_ic;
283 }
284 
285 inline CompiledIC* CompiledIC_at(CompiledMethod* nm, address call_site) {
286   CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site));
287   c_ic->verify();
288   return c_ic;
289 }
290 
291 inline CompiledIC* CompiledIC_at(Relocation* call_site) {
292   assert(call_site->type() == relocInfo::virtual_call_type ||
293          call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
294   CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr()));
295   c_ic->verify();
296   return c_ic;
297 }
298 
299 inline CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
300   assert(reloc_iter->type() == relocInfo::virtual_call_type ||
301       reloc_iter->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
302   CompiledIC* c_ic = new CompiledIC(reloc_iter);
303   c_ic->verify();
304   return c_ic;
305 }
306 
307 //-----------------------------------------------------------------------------
308 // The CompiledStaticCall represents a call to a static method in the compiled
309 //
310 // Transition diagram of a static call site is somewhat simpler than for an inlined cache:
311 //
312 //
313 //           -----<----- Clean ----->-----
314 //          /                             \
315 //         /                               \
316 //    compilled code <------------> interpreted code
317 //
318 //  Clean:            Calls directly to runtime method for fixup
319 //  Compiled code:    Calls directly to compiled code
320 //  Interpreted code: Calls to stub that set Method* reference
321 //
322 //
323 
324 class StaticCallInfo {
325  private:
326   address      _entry;          // Entrypoint
327   methodHandle _callee;         // Callee (used when calling interpreter)
328   bool         _to_interpreter; // call to interpreted method (otherwise compiled)
329 
330   friend class CompiledStaticCall;
331   friend class CompiledDirectStaticCall;
332   friend class CompiledPltStaticCall;
333  public:
334   address      entry() const    { return _entry;  }
335   methodHandle callee() const   { return _callee; }
336 };
337 
338 class CompiledStaticCall : public ResourceObj {
339  public:
340   // Code
341   static address emit_to_interp_stub(CodeBuffer &cbuf, address mark = NULL);
342   static int to_interp_stub_size();
343   static int to_trampoline_stub_size();
344   static int reloc_to_interp_stub();
345 
346   // Compute entry point given a method
347   static void compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info);
348 
349 public:
350   // Clean static call (will force resolving on next use)
351   virtual address destination() const = 0;
352 
353   // Clean static call (will force resolving on next use)
354   bool set_to_clean(bool in_use = true);
355 
356   // Set state. The entry must be the same, as computed by compute_entry.
357   // Computation and setting is split up, since the actions are separate during
358   // a OptoRuntime::resolve_xxx.
359   void set(const StaticCallInfo& info);
360 
361   // State
362   bool is_clean() const;
363   bool is_call_to_compiled() const;
364   virtual bool is_call_to_interpreted() const = 0;
365 
366   virtual address instruction_address() const = 0;
367   virtual address end_of_call() const = 0;
368 protected:
369   virtual address resolve_call_stub() const = 0;
370   virtual void set_destination_mt_safe(address dest) = 0;
371   virtual void set_to_interpreted(const methodHandle& callee, address entry) = 0;
372   virtual const char* name() const = 0;
373 
374   void set_to_compiled(address entry);
375 };
376 
377 class CompiledDirectStaticCall : public CompiledStaticCall {
378 private:
379   friend class CompiledIC;
380   friend class DirectNativeCallWrapper;
381 
382   // Also used by CompiledIC
383   void set_to_interpreted(const methodHandle& callee, address entry);
384   void verify_mt_safe(const methodHandle& callee, address entry,
385                       NativeMovConstReg* method_holder,
386                       NativeJump*        jump) PRODUCT_RETURN;
387   address instruction_address() const { return _call->instruction_address(); }
388   void set_destination_mt_safe(address dest) { _call->set_destination_mt_safe(dest); }
389 
390   NativeCall* _call;
391 
392   CompiledDirectStaticCall(NativeCall* call) : _call(call) {}
393 
394  public:
395   static inline CompiledDirectStaticCall* before(address return_addr) {
396     CompiledDirectStaticCall* st = new CompiledDirectStaticCall(nativeCall_before(return_addr));
397     st->verify();
398     return st;
399   }
400 
401   static inline CompiledDirectStaticCall* at(address native_call) {
402     CompiledDirectStaticCall* st = new CompiledDirectStaticCall(nativeCall_at(native_call));
403     st->verify();
404     return st;
405   }
406 
407   static inline CompiledDirectStaticCall* at(Relocation* call_site) {
408     return at(call_site->addr());
409   }
410 
411   // Delegation
412   address destination() const { return _call->destination(); }
413   address end_of_call() const { return _call->return_address(); }
414 
415   // State
416   virtual bool is_call_to_interpreted() const;
417 
418   // Stub support
419   static address find_stub_for(address instruction);
420   address find_stub();
421   static void set_stub_to_clean(static_stub_Relocation* static_stub);
422 
423   // Misc.
424   void print()  PRODUCT_RETURN;
425   void verify() PRODUCT_RETURN;
426 
427  protected:
428   virtual address resolve_call_stub() const;
429   virtual const char* name() const { return "CompiledDirectStaticCall"; }
430 };
431 
432 #endif // SHARE_CODE_COMPILEDIC_HPP