1 /*
   2  * Copyright (c) 2015, 2016, 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_VM_CODE_COMPILEDMETHOD_HPP
  26 #define SHARE_VM_CODE_COMPILEDMETHOD_HPP
  27 
  28 #include "code/codeBlob.hpp"
  29 #include "code/pcDesc.hpp"
  30 #include "oops/metadata.hpp"
  31 
  32 class Dependencies;
  33 class ExceptionHandlerTable;
  34 class ImplicitExceptionTable;
  35 class AbstractCompiler;
  36 class xmlStream;
  37 class CompiledStaticCall;
  38 
  39 // This class is used internally by nmethods, to cache
  40 // exception/pc/handler information.
  41 
  42 class ExceptionCache : public CHeapObj<mtCode> {
  43   friend class VMStructs;
  44  private:
  45   enum { cache_size = 16 };
  46   Klass*   _exception_type;
  47   address  _pc[cache_size];
  48   address  _handler[cache_size];
  49   volatile int _count;
  50   ExceptionCache* _next;
  51 
  52   address pc_at(int index)                     { assert(index >= 0 && index < count(),""); return _pc[index]; }
  53   void    set_pc_at(int index, address a)      { assert(index >= 0 && index < cache_size,""); _pc[index] = a; }
  54   address handler_at(int index)                { assert(index >= 0 && index < count(),""); return _handler[index]; }
  55   void    set_handler_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _handler[index] = a; }
  56   int     count()                              { return OrderAccess::load_acquire(&_count); }
  57   // increment_count is only called under lock, but there may be concurrent readers.
  58   void    increment_count()                    { OrderAccess::release_store(&_count, _count + 1); }
  59 
  60  public:
  61 
  62   ExceptionCache(Handle exception, address pc, address handler);
  63 
  64   Klass*    exception_type()                { return _exception_type; }
  65   ExceptionCache* next()                    { return _next; }
  66   void      set_next(ExceptionCache *ec)    { _next = ec; }
  67 
  68   address match(Handle exception, address pc);
  69   bool    match_exception_with_space(Handle exception) ;
  70   address test_address(address addr);
  71   bool    add_address_and_handler(address addr, address handler) ;
  72 };
  73 
  74 class nmethod;
  75 
  76 // cache pc descs found in earlier inquiries
  77 class PcDescCache VALUE_OBJ_CLASS_SPEC {
  78   friend class VMStructs;
  79  private:
  80   enum { cache_size = 4 };
  81   // The array elements MUST be volatile! Several threads may modify
  82   // and read from the cache concurrently. find_pc_desc_internal has
  83   // returned wrong results. C++ compiler (namely xlC12) may duplicate
  84   // C++ field accesses if the elements are not volatile.
  85   typedef PcDesc* PcDescPtr;
  86   volatile PcDescPtr _pc_descs[cache_size]; // last cache_size pc_descs found
  87  public:
  88   PcDescCache() { debug_only(_pc_descs[0] = NULL); }
  89   void    reset_to(PcDesc* initial_pc_desc);
  90   PcDesc* find_pc_desc(int pc_offset, bool approximate);
  91   void    add_pc_desc(PcDesc* pc_desc);
  92   PcDesc* last_pc_desc() { return _pc_descs[0]; }
  93 };
  94 
  95 class PcDescSearch {
  96 private:
  97   address _code_begin;
  98   PcDesc* _lower;
  99   PcDesc* _upper;
 100 public:
 101   PcDescSearch(address code, PcDesc* lower, PcDesc* upper) :
 102     _code_begin(code), _lower(lower), _upper(upper)
 103   {
 104   }
 105 
 106   address code_begin() const { return _code_begin; }
 107   PcDesc* scopes_pcs_begin() const { return _lower; }
 108   PcDesc* scopes_pcs_end() const { return _upper; }
 109 };
 110 
 111 class PcDescContainer VALUE_OBJ_CLASS_SPEC {
 112 private:
 113   PcDescCache _pc_desc_cache;
 114 public:
 115   PcDescContainer() {}
 116 
 117   PcDesc* find_pc_desc_internal(address pc, bool approximate, const PcDescSearch& search);
 118   void    reset_to(PcDesc* initial_pc_desc) { _pc_desc_cache.reset_to(initial_pc_desc); }
 119 
 120   PcDesc* find_pc_desc(address pc, bool approximate, const PcDescSearch& search) {
 121     address base_address = search.code_begin();
 122     PcDesc* desc = _pc_desc_cache.last_pc_desc();
 123     if (desc != NULL && desc->pc_offset() == pc - base_address) {
 124       return desc;
 125     }
 126     return find_pc_desc_internal(pc, approximate, search);
 127   }
 128 };
 129 
 130 
 131 class CompiledMethod : public CodeBlob {
 132   friend class VMStructs;
 133   friend class NMethodSweeper;
 134 
 135   void init_defaults();
 136 protected:
 137   enum MarkForDeoptimizationStatus {
 138     not_marked,
 139     deoptimize,
 140     deoptimize_noupdate
 141   };
 142 
 143   MarkForDeoptimizationStatus _mark_for_deoptimization_status; // Used for stack deoptimization
 144 
 145   bool _is_far_code; // Code is far from CodeCache.
 146                      // Have to use far call instructions to call it from code in CodeCache.
 147   // set during construction
 148   unsigned int _has_unsafe_access:1;         // May fault due to unsafe access.
 149   unsigned int _has_method_handle_invokes:1; // Has this method MethodHandle invokes?
 150   unsigned int _lazy_critical_native:1;      // Lazy JNI critical native
 151   unsigned int _has_wide_vectors:1;          // Preserve wide vectors at safepoints
 152 
 153   Method*   _method;
 154   address _scopes_data_begin;
 155   // All deoptee's will resume execution at this location described by
 156   // this address.
 157   address _deopt_handler_begin;
 158   // All deoptee's at a MethodHandle call site will resume execution
 159   // at this location described by this offset.
 160   address _deopt_mh_handler_begin;
 161 
 162   PcDescContainer _pc_desc_container;
 163   ExceptionCache * volatile _exception_cache;
 164 
 165   virtual void flush() = 0;
 166 protected:
 167   CompiledMethod(Method* method, const char* name, CompilerType type, const CodeBlobLayout& layout, int frame_complete_offset, int frame_size, ImmutableOopMapSet* oop_maps, bool caller_must_gc_arguments);
 168   CompiledMethod(Method* method, const char* name, CompilerType type, int size, int header_size, CodeBuffer* cb, int frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments);
 169 
 170 public:
 171   virtual bool is_compiled() const                { return true; }
 172 
 173   bool  has_unsafe_access() const                 { return _has_unsafe_access; }
 174   void  set_has_unsafe_access(bool z)             { _has_unsafe_access = z; }
 175 
 176   bool  has_method_handle_invokes() const         { return _has_method_handle_invokes; }
 177   void  set_has_method_handle_invokes(bool z)     { _has_method_handle_invokes = z; }
 178 
 179   bool  is_lazy_critical_native() const           { return _lazy_critical_native; }
 180   void  set_lazy_critical_native(bool z)          { _lazy_critical_native = z; }
 181 
 182   bool  has_wide_vectors() const                  { return _has_wide_vectors; }
 183   void  set_has_wide_vectors(bool z)              { _has_wide_vectors = z; }
 184 
 185   enum { in_use       = 0,   // executable nmethod
 186          not_used     = 1,   // not entrant, but revivable
 187          not_entrant  = 2,   // marked for deoptimization but activations may still exist,
 188                              // will be transformed to zombie when all activations are gone
 189          zombie       = 3,   // no activations exist, nmethod is ready for purge
 190          unloaded     = 4    // there should be no activations, should not be called,
 191                              // will be transformed to zombie immediately
 192   };
 193 
 194   virtual bool  is_in_use() const = 0;
 195   virtual int   comp_level() const = 0;
 196   virtual int   compile_id() const = 0;
 197 
 198   virtual address verified_entry_point() const = 0;
 199   virtual void log_identity(xmlStream* log) const = 0;
 200   virtual void log_state_change() const = 0;
 201   virtual bool make_not_used() = 0;
 202   virtual bool make_not_entrant() = 0;
 203   virtual bool make_entrant() = 0;
 204   virtual address entry_point() const = 0;
 205   virtual bool make_zombie() = 0;
 206   virtual bool is_osr_method() const = 0;
 207   virtual int osr_entry_bci() const = 0;
 208   Method* method() const                          { return _method; }
 209   virtual void print_pcs() = 0;
 210   bool is_native_method() const { return _method != NULL && _method->is_native(); }
 211   bool is_java_method() const { return _method != NULL && !_method->is_native(); }
 212 
 213   // ScopeDesc retrieval operation
 214   PcDesc* pc_desc_at(address pc)   { return find_pc_desc(pc, false); }
 215   // pc_desc_near returns the first PcDesc at or after the givne pc.
 216   PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); }
 217 
 218   // ScopeDesc for an instruction
 219   ScopeDesc* scope_desc_at(address pc);
 220 
 221   bool is_at_poll_return(address pc);
 222   bool is_at_poll_or_poll_return(address pc);
 223 
 224   bool  is_marked_for_deoptimization() const      { return _mark_for_deoptimization_status != not_marked; }
 225   void  mark_for_deoptimization(bool inc_recompile_counts = true) {
 226     _mark_for_deoptimization_status = (inc_recompile_counts ? deoptimize : deoptimize_noupdate);
 227   }
 228   bool update_recompile_counts() const {
 229     // Update recompile counts when either the update is explicitly requested (deoptimize)
 230     // or the nmethod is not marked for deoptimization at all (not_marked).
 231     // The latter happens during uncommon traps when deoptimized nmethod is made not entrant.
 232     return _mark_for_deoptimization_status != deoptimize_noupdate;
 233   }
 234 
 235   // tells whether frames described by this nmethod can be deoptimized
 236   // note: native wrappers cannot be deoptimized.
 237   bool can_be_deoptimized() const { return is_java_method(); }
 238 
 239   virtual oop oop_at(int index) const = 0;
 240   virtual Metadata* metadata_at(int index) const = 0;
 241 
 242   address scopes_data_begin() const { return _scopes_data_begin; }
 243   virtual address scopes_data_end() const = 0;
 244   int scopes_data_size() const { return scopes_data_end() - scopes_data_begin(); }
 245 
 246   virtual PcDesc* scopes_pcs_begin() const = 0;
 247   virtual PcDesc* scopes_pcs_end() const = 0;
 248   int scopes_pcs_size() const { return (intptr_t) scopes_pcs_end() - (intptr_t) scopes_pcs_begin(); }
 249 
 250   address insts_begin() const { return code_begin(); }
 251   address insts_end() const { return stub_begin(); }
 252   bool insts_contains(address addr) const { return insts_begin() <= addr && addr < insts_end(); }
 253   int insts_size() const { return insts_end() - insts_begin(); }
 254 
 255   virtual address consts_begin() const = 0;
 256   virtual address consts_end() const = 0;
 257   bool consts_contains(address addr) const { return consts_begin() <= addr && addr < consts_end(); }
 258   int consts_size() const { return consts_end() - consts_begin(); }
 259 
 260   virtual address stub_begin() const = 0;
 261   virtual address stub_end() const = 0;
 262   bool stub_contains(address addr) const { return stub_begin() <= addr && addr < stub_end(); }
 263   int stub_size() const { return stub_end() - stub_begin(); }
 264 
 265   virtual address handler_table_begin() const = 0;
 266   virtual address handler_table_end() const = 0;
 267   bool handler_table_contains(address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); }
 268   int handler_table_size() const { return handler_table_end() - handler_table_begin(); }
 269 
 270   virtual address nul_chk_table_begin() const = 0;
 271   virtual address nul_chk_table_end() const = 0;
 272   bool nul_chk_table_contains(address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); }
 273   int nul_chk_table_size() const { return nul_chk_table_end() - nul_chk_table_begin(); }
 274 
 275   virtual oop* oop_addr_at(int index) const = 0;
 276   virtual Metadata** metadata_addr_at(int index) const = 0;
 277   virtual void    set_original_pc(const frame* fr, address pc) = 0;
 278 
 279   // Exception cache support
 280   // Note: _exception_cache may be read concurrently. We rely on memory_order_consume here.
 281   ExceptionCache* exception_cache() const         { return _exception_cache; }
 282   void set_exception_cache(ExceptionCache *ec)    { _exception_cache = ec; }
 283   void release_set_exception_cache(ExceptionCache *ec) { OrderAccess::release_store_ptr(&_exception_cache, ec); }
 284   address handler_for_exception_and_pc(Handle exception, address pc);
 285   void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
 286   void clean_exception_cache(BoolObjectClosure* is_alive);
 287 
 288   void add_exception_cache_entry(ExceptionCache* new_entry);
 289   ExceptionCache* exception_cache_entry_for_exception(Handle exception);
 290 
 291   // MethodHandle
 292   bool is_method_handle_return(address return_pc);
 293   address deopt_mh_handler_begin() const  { return _deopt_mh_handler_begin; }
 294 
 295   address deopt_handler_begin() const { return _deopt_handler_begin; }
 296   virtual address get_original_pc(const frame* fr) = 0;
 297   // Deopt
 298   // Return true is the PC is one would expect if the frame is being deopted.
 299   bool is_deopt_pc      (address pc) { return is_deopt_entry(pc) || is_deopt_mh_entry(pc); }
 300   bool is_deopt_mh_entry(address pc) { return pc == deopt_mh_handler_begin(); }
 301   bool is_deopt_entry(address pc);
 302 
 303   virtual bool can_convert_to_zombie() = 0;
 304   virtual const char* compile_kind() const = 0;
 305   virtual int get_state() const = 0;
 306 
 307   const char* state() const;
 308 
 309   bool is_far_code() const { return _is_far_code; }
 310 
 311   bool inlinecache_check_contains(address addr) const {
 312     return (addr >= code_begin() && addr < verified_entry_point());
 313   }
 314 
 315   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f);
 316 
 317   // implicit exceptions support
 318   virtual address continuation_for_implicit_exception(address pc) { return NULL; }
 319 
 320   static address get_deopt_original_pc(const frame* fr);
 321 
 322   // Inline cache support
 323   void cleanup_inline_caches(bool clean_all = false);
 324   virtual void clear_inline_caches();
 325   void clear_ic_stubs();
 326 
 327   // Verify and count cached icholder relocations.
 328   int  verify_icholder_relocations();
 329   void verify_oop_relocations();
 330 
 331   virtual bool is_evol_dependent_on(Klass* dependee) = 0;
 332   // Fast breakpoint support. Tells if this compiled method is
 333   // dependent on the given method. Returns true if this nmethod
 334   // corresponds to the given method as well.
 335   virtual bool is_dependent_on_method(Method* dependee) = 0;
 336 
 337   Method* attached_method(address call_pc);
 338   Method* attached_method_before_pc(address pc);
 339 
 340   virtual void metadata_do(void f(Metadata*)) = 0;
 341 
 342   // GC support
 343 
 344   void set_unloading_next(CompiledMethod* next) { _unloading_next = next; }
 345   CompiledMethod* unloading_next()              { return _unloading_next; }
 346 
 347   void static clean_ic_if_metadata_is_dead(CompiledIC *ic, BoolObjectClosure *is_alive);
 348 
 349   // Check that all metadata is still alive
 350   void verify_metadata_loaders(address low_boundary, BoolObjectClosure* is_alive);
 351 
 352   virtual void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred);
 353   //  The parallel versions are used by G1.
 354   virtual bool do_unloading_parallel(BoolObjectClosure* is_alive, bool unloading_occurred);
 355   virtual void do_unloading_parallel_postponed(BoolObjectClosure* is_alive, bool unloading_occurred);
 356 
 357   static unsigned char global_unloading_clock()   { return _global_unloading_clock; }
 358   static void increase_unloading_clock();
 359 
 360   void set_unloading_clock(unsigned char unloading_clock);
 361   unsigned char unloading_clock();
 362 
 363 protected:
 364   virtual bool do_unloading_oops(address low_boundary, BoolObjectClosure* is_alive, bool unloading_occurred) = 0;
 365 #if INCLUDE_JVMCI
 366   virtual bool do_unloading_jvmci(BoolObjectClosure* is_alive, bool unloading_occurred) = 0;
 367 #endif
 368 
 369 private:
 370   // GC support to help figure out if an nmethod has been
 371   // cleaned/unloaded by the current GC.
 372   static unsigned char _global_unloading_clock;
 373 
 374   volatile unsigned char _unloading_clock;   // Incremented after GC unloaded/cleaned the nmethod
 375 
 376   PcDesc* find_pc_desc(address pc, bool approximate) {
 377     return _pc_desc_container.find_pc_desc(pc, approximate, PcDescSearch(code_begin(), scopes_pcs_begin(), scopes_pcs_end()));
 378   }
 379 
 380 protected:
 381   union {
 382     // Used by G1 to chain nmethods.
 383     CompiledMethod* _unloading_next;
 384     // Used by non-G1 GCs to chain nmethods.
 385     nmethod* _scavenge_root_link; // from CodeCache::scavenge_root_nmethods
 386   };
 387 };
 388 
 389 #endif //SHARE_VM_CODE_COMPILEDMETHOD_HPP