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