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