0 /*                                                                                                                                   
1  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.                                                      
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.                                                                     
3  *                                                                                                                                   
4  * This code is free software; you can redistribute it and/or modify it                                                              
5  * under the terms of the GNU General Public License version 2 only, as                                                              
6  * published by the Free Software Foundation.                                                                                        
7  *                                                                                                                                   
8  * This code is distributed in the hope that it will be useful, but WITHOUT                                                          
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                             
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                             
11  * version 2 for more details (a copy is included in the LICENSE file that                                                           
12  * accompanied this code).                                                                                                           
13  *                                                                                                                                   
14  * You should have received a copy of the GNU General Public License version                                                         
15  * 2 along with this work; if not, write to the Free Software Foundation,                                                            
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                                     
17  *                                                                                                                                   
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA                                                           
19  * or visit www.oracle.com if you need additional information or have any                                                            
20  * questions.                                                                                                                        
21  *                                                                                                                                   
22  */                                                                                                                                  
23 
24 #include "precompiled.hpp"                                                                                                           
25 #include "code/compiledIC.hpp"                                                                                                       
26 #include "code/compiledMethod.inline.hpp"                                                                                            
27 #include "code/scopeDesc.hpp"                                                                                                        
28 #include "code/codeCache.hpp"                                                                                                        
29 #include "interpreter/bytecode.inline.hpp"                                                                                           
30 #include "logging/log.hpp"                                                                                                           
31 #include "logging/logTag.hpp"                                                                                                        
32 #include "memory/resourceArea.hpp"                                                                                                   
33 #include "oops/methodData.hpp"                                                                                                       
34 #include "oops/method.inline.hpp"                                                                                                    
35 #include "prims/methodHandles.hpp"                                                                                                   
36 #include "runtime/handles.inline.hpp"                                                                                                
37 #include "runtime/mutexLocker.hpp"                                                                                                   
38 
39 CompiledMethod::CompiledMethod(Method* method, const char* name, CompilerType type, const CodeBlobLayout& layout, int frame_complete_
40   : CodeBlob(name, type, layout, frame_complete_offset, frame_size, oop_maps, caller_must_gc_arguments),                             
41   _mark_for_deoptimization_status(not_marked), _method(method) {                                                                     
42   init_defaults();                                                                                                                   
43 }                                                                                                                                    
44 
45 CompiledMethod::CompiledMethod(Method* method, const char* name, CompilerType type, int size, int header_size, CodeBuffer* cb, int fr
46   : CodeBlob(name, type, CodeBlobLayout((address) this, size, header_size, cb), cb, frame_complete_offset, frame_size, oop_maps, call
47   _mark_for_deoptimization_status(not_marked), _method(method) {                                                                     
48   init_defaults();                                                                                                                   
49 }                                                                                                                                    
50 
51 void CompiledMethod::init_defaults() {                                                                                               
52   _has_unsafe_access          = 0;                                                                                                   
53   _has_method_handle_invokes  = 0;                                                                                                   
54   _lazy_critical_native       = 0;                                                                                                   
55   _has_wide_vectors           = 0;                                                                                                   
56   _unloading_clock            = 0;                                                                                                   
57 }                                                                                                                                    
58 
59 bool CompiledMethod::is_method_handle_return(address return_pc) {                                                                    
60   if (!has_method_handle_invokes())  return false;                                                                                   
61   PcDesc* pd = pc_desc_at(return_pc);                                                                                                
62   if (pd == NULL)                                                                                                                    
63     return false;                                                                                                                    
64   return pd->is_method_handle_invoke();                                                                                              
65 }                                                                                                                                    
66 
67 // Returns a string version of the method state.                                                                                     
68 const char* CompiledMethod::state() const {                                                                                          
69   int state = get_state();                                                                                                           
70   switch (state) {                                                                                                                   
71   case not_installed:                                                                                                                
72     return "not installed";                                                                                                          
73   case in_use:                                                                                                                       
74     return "in use";                                                                                                                 
75   case not_used:                                                                                                                     
76     return "not_used";                                                                                                               
77   case not_entrant:                                                                                                                  
78     return "not_entrant";                                                                                                            
79   case zombie:                                                                                                                       
80     return "zombie";                                                                                                                 
81   case unloaded:                                                                                                                     
82     return "unloaded";                                                                                                               
83   default:                                                                                                                           
84     fatal("unexpected method state: %d", state);                                                                                     
85     return NULL;                                                                                                                     
86   }                                                                                                                                  
87 }                                                                                                                                    
88 
89 //-----------------------------------------------------------------------------                                                      
90 
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
91 void CompiledMethod::add_exception_cache_entry(ExceptionCache* new_entry) {                                                          
92   assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock");                                                  
93   assert(new_entry != NULL,"Must be non null");                                                                                      
94   assert(new_entry->next() == NULL, "Must be null");                                                                                 
95 
96   ExceptionCache *ec = exception_cache();                                                                                            
97   if (ec != NULL) {                                                                                                                  
98     new_entry->set_next(ec);                                                                                                         
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
99   }                                                                                                                                  
100   release_set_exception_cache(new_entry);                                                                                            
101 }                                                                                                                                    
102 
103 void CompiledMethod::clean_exception_cache() {                                                                                       
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
104   ExceptionCache* prev = NULL;                                                                                                       
105   ExceptionCache* curr = exception_cache();                                                                                          
106 
107   while (curr != NULL) {                                                                                                             
108     ExceptionCache* next = curr->next();                                                                                             
109 
110     Klass* ex_klass = curr->exception_type();                                                                                        
111     if (ex_klass != NULL && !ex_klass->is_loader_alive()) {                                                                          
112       if (prev == NULL) {                                                                                                            
113         set_exception_cache(next);                                                                                                   
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
114       } else {                                                                                                                       
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
115         prev->set_next(next);                                                                                                        
116       }                                                                                                                              
117       delete curr;                                                                                                                   
118       // prev stays the same.                                                                                                        
                                                                                                                                     
                                                                                                                                     
119     } else {                                                                                                                         
120       prev = curr;                                                                                                                   
121     }                                                                                                                                
122 
123     curr = next;                                                                                                                     
124   }                                                                                                                                  
125 }                                                                                                                                    
126 
127 // public method for accessing the exception cache                                                                                   
128 // These are the public access methods.                                                                                              
129 address CompiledMethod::handler_for_exception_and_pc(Handle exception, address pc) {                                                 
130   // We never grab a lock to read the exception cache, so we may                                                                     
131   // have false negatives. This is okay, as it can only happen during                                                                
132   // the first few exception lookups for a given nmethod.                                                                            
133   ExceptionCache* ec = exception_cache();                                                                                            
134   while (ec != NULL) {                                                                                                               
135     address ret_val;                                                                                                                 
136     if ((ret_val = ec->match(exception,pc)) != NULL) {                                                                               
137       return ret_val;                                                                                                                
138     }                                                                                                                                
139     ec = ec->next();                                                                                                                 
140   }                                                                                                                                  
141   return NULL;                                                                                                                       
142 }                                                                                                                                    
143 
144 void CompiledMethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) {                               
145   // There are potential race conditions during exception cache updates, so we                                                       
146   // must own the ExceptionCache_lock before doing ANY modifications. Because                                                        
147   // we don't lock during reads, it is possible to have several threads attempt                                                      
148   // to update the cache with the same data. We need to check for already inserted                                                   
149   // copies of the current data before adding it.                                                                                    
150 
151   MutexLocker ml(ExceptionCache_lock);                                                                                               
152   ExceptionCache* target_entry = exception_cache_entry_for_exception(exception);                                                     
153 
154   if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) {                                                  
155     target_entry = new ExceptionCache(exception,pc,handler);                                                                         
156     add_exception_cache_entry(target_entry);                                                                                         
157   }                                                                                                                                  
158 }                                                                                                                                    
159 
160 //-------------end of code for ExceptionCache--------------                                                                          
161                                                                                                                                      
162 // private method for handling exception cache                                                                                       
163 // These methods are private, and used to manipulate the exception cache                                                             
164 // directly.                                                                                                                         
165 ExceptionCache* CompiledMethod::exception_cache_entry_for_exception(Handle exception) {                                              
166   ExceptionCache* ec = exception_cache();                                                                                            
167   while (ec != NULL) {                                                                                                               
168     if (ec->match_exception_with_space(exception)) {                                                                                 
169       return ec;                                                                                                                     
170     }                                                                                                                                
171     ec = ec->next();                                                                                                                 
172   }                                                                                                                                  
173   return NULL;                                                                                                                       
174 }                                                                                                                                    
                                                                                                                                     
                                                                                                                                     
175 
176 bool CompiledMethod::is_at_poll_return(address pc) {                                                                                 
177   RelocIterator iter(this, pc, pc+1);                                                                                                
178   while (iter.next()) {                                                                                                              
179     if (iter.type() == relocInfo::poll_return_type)                                                                                  
180       return true;                                                                                                                   
181   }                                                                                                                                  
182   return false;                                                                                                                      
183 }                                                                                                                                    
184 
185 
186 bool CompiledMethod::is_at_poll_or_poll_return(address pc) {                                                                         
187   RelocIterator iter(this, pc, pc+1);                                                                                                
188   while (iter.next()) {                                                                                                              
189     relocInfo::relocType t = iter.type();                                                                                            
190     if (t == relocInfo::poll_return_type || t == relocInfo::poll_type)                                                               
191       return true;                                                                                                                   
192   }                                                                                                                                  
193   return false;                                                                                                                      
194 }                                                                                                                                    
195 
196 void CompiledMethod::verify_oop_relocations() {                                                                                      
197   // Ensure sure that the code matches the current oop values                                                                        
198   RelocIterator iter(this, NULL, NULL);                                                                                              
199   while (iter.next()) {                                                                                                              
200     if (iter.type() == relocInfo::oop_type) {                                                                                        
201       oop_Relocation* reloc = iter.oop_reloc();                                                                                      
202       if (!reloc->oop_is_immediate()) {                                                                                              
203         reloc->verify_oop_relocation();                                                                                              
204       }                                                                                                                              
205     }                                                                                                                                
206   }                                                                                                                                  
207 }                                                                                                                                    
208 
209 
210 ScopeDesc* CompiledMethod::scope_desc_at(address pc) {                                                                               
211   PcDesc* pd = pc_desc_at(pc);                                                                                                       
212   guarantee(pd != NULL, "scope must be present");                                                                                    
213   return new ScopeDesc(this, pd->scope_decode_offset(),                                                                              
214                        pd->obj_decode_offset(), pd->should_reexecute(), pd->rethrow_exception(),                                     
215                        pd->return_oop());                                                                                            
216 }                                                                                                                                    
217 
218 ScopeDesc* CompiledMethod::scope_desc_near(address pc) {                                                                             
219   PcDesc* pd = pc_desc_near(pc);                                                                                                     
220   guarantee(pd != NULL, "scope must be present");                                                                                    
221   return new ScopeDesc(this, pd->scope_decode_offset(),                                                                              
222                        pd->obj_decode_offset(), pd->should_reexecute(), pd->rethrow_exception(),                                     
223                        pd->return_oop());                                                                                            
224 }                                                                                                                                    
225 
226 address CompiledMethod::oops_reloc_begin() const {                                                                                   
227   // If the method is not entrant or zombie then a JMP is plastered over the                                                         
228   // first few bytes.  If an oop in the old code was there, that oop                                                                 
229   // should not get GC'd.  Skip the first few bytes of oops on                                                                       
230   // not-entrant methods.                                                                                                            
231   address low_boundary = verified_entry_point();                                                                                     
232   if (!is_in_use() && is_nmethod()) {                                                                                                
233     low_boundary += NativeJump::instruction_size;                                                                                    
234     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.                                                       
235     // This means that the low_boundary is going to be a little too high.                                                            
236     // This shouldn't matter, since oops of non-entrant methods are never used.                                                      
237     // In fact, why are we bothering to look at oops in a non-entrant method??                                                       
238   }                                                                                                                                  
239   return low_boundary;                                                                                                               
240 }                                                                                                                                    
241 
242 int CompiledMethod::verify_icholder_relocations() {                                                                                  
243   ResourceMark rm;                                                                                                                   
244   int count = 0;                                                                                                                     
245 
246   RelocIterator iter(this);                                                                                                          
247   while(iter.next()) {                                                                                                               
248     if (iter.type() == relocInfo::virtual_call_type) {                                                                               
249       if (CompiledIC::is_icholder_call_site(iter.virtual_call_reloc(), this)) {                                                      
250         CompiledIC *ic = CompiledIC_at(&iter);                                                                                       
251         if (TraceCompiledIC) {                                                                                                       
252           tty->print("noticed icholder " INTPTR_FORMAT " ", p2i(ic->cached_icholder()));                                             
253           ic->print();                                                                                                               
254         }                                                                                                                            
255         assert(ic->cached_icholder() != NULL, "must be non-NULL");                                                                   
256         count++;                                                                                                                     
257       }                                                                                                                              
258     }                                                                                                                                
259   }                                                                                                                                  
260 
261   return count;                                                                                                                      
262 }                                                                                                                                    
263 
264 // Method that knows how to preserve outgoing arguments at call. This method must be                                                 
265 // called with a frame corresponding to a Java invoke                                                                                
266 void CompiledMethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) {                            
267   if (method() != NULL && !method()->is_native()) {                                                                                  
268     address pc = fr.pc();                                                                                                            
269     SimpleScopeDesc ssd(this, pc);                                                                                                   
270     Bytecode_invoke call(ssd.method(), ssd.bci());                                                                                   
271     bool has_receiver = call.has_receiver();                                                                                         
272     bool has_appendix = call.has_appendix();                                                                                         
273     Symbol* signature = call.signature();                                                                                            
274 
275     // The method attached by JIT-compilers should be used, if present.                                                              
276     // Bytecode can be inaccurate in such case.                                                                                      
277     Method* callee = attached_method_before_pc(pc);                                                                                  
278     if (callee != NULL) {                                                                                                            
279       has_receiver = !(callee->access_flags().is_static());                                                                          
280       has_appendix = false;                                                                                                          
281       signature = callee->signature();                                                                                               
282     }                                                                                                                                
283 
284     fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);                                                
285   }                                                                                                                                  
286 }                                                                                                                                    
287 
288 Method* CompiledMethod::attached_method(address call_instr) {                                                                        
289   assert(code_contains(call_instr), "not part of the nmethod");                                                                      
290   RelocIterator iter(this, call_instr, call_instr + 1);                                                                              
291   while (iter.next()) {                                                                                                              
292     if (iter.addr() == call_instr) {                                                                                                 
293       switch(iter.type()) {                                                                                                          
294         case relocInfo::static_call_type:      return iter.static_call_reloc()->method_value();                                      
295         case relocInfo::opt_virtual_call_type: return iter.opt_virtual_call_reloc()->method_value();                                 
296         case relocInfo::virtual_call_type:     return iter.virtual_call_reloc()->method_value();                                     
297         default:                               break;                                                                                
298       }                                                                                                                              
299     }                                                                                                                                
300   }                                                                                                                                  
301   return NULL; // not found                                                                                                          
302 }                                                                                                                                    
303 
304 Method* CompiledMethod::attached_method_before_pc(address pc) {                                                                      
305   if (NativeCall::is_call_before(pc)) {                                                                                              
306     NativeCall* ncall = nativeCall_before(pc);                                                                                       
307     return attached_method(ncall->instruction_address());                                                                            
308   }                                                                                                                                  
309   return NULL; // not a call                                                                                                         
310 }                                                                                                                                    
311 
312 void CompiledMethod::clear_inline_caches() {                                                                                         
313   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");                                     
314   if (is_zombie()) {                                                                                                                 
315     return;                                                                                                                          
316   }                                                                                                                                  
317 
318   RelocIterator iter(this);                                                                                                          
319   while (iter.next()) {                                                                                                              
320     iter.reloc()->clear_inline_cache();                                                                                              
321   }                                                                                                                                  
322 }                                                                                                                                    
323 
324 // Clear ICStubs of all compiled ICs                                                                                                 
325 void CompiledMethod::clear_ic_stubs() {                                                                                              
326   assert_locked_or_safepoint(CompiledIC_lock);                                                                                       
327   ResourceMark rm;                                                                                                                   
328   RelocIterator iter(this);                                                                                                          
329   while(iter.next()) {                                                                                                               
330     if (iter.type() == relocInfo::virtual_call_type) {                                                                               
331       CompiledIC* ic = CompiledIC_at(&iter);                                                                                         
332       ic->clear_ic_stub();                                                                                                           
333     }                                                                                                                                
334   }                                                                                                                                  
335 }                                                                                                                                    
336 
337 #ifdef ASSERT                                                                                                                        
338 // Check class_loader is alive for this bit of metadata.                                                                             
339 static void check_class(Metadata* md) {                                                                                              
340    Klass* klass = NULL;                                                                                                              
341    if (md->is_klass()) {                                                                                                             
342      klass = ((Klass*)md);                                                                                                           
343    } else if (md->is_method()) {                                                                                                     
344      klass = ((Method*)md)->method_holder();                                                                                         
345    } else if (md->is_methodData()) {                                                                                                 
346      klass = ((MethodData*)md)->method()->method_holder();                                                                           
347    } else {                                                                                                                          
348      md->print();                                                                                                                    
349      ShouldNotReachHere();                                                                                                           
350    }                                                                                                                                 
351    assert(klass->is_loader_alive(), "must be alive");                                                                                
352 }                                                                                                                                    
353 #endif // ASSERT                                                                                                                     
354 
355 
356 void CompiledMethod::clean_ic_if_metadata_is_dead(CompiledIC *ic) {                                                                  
357   if (ic->is_icholder_call()) {                                                                                                      
358     // The only exception is compiledICHolder metdata which may                                                                      
359     // yet be marked below. (We check this further below).                                                                           
360     CompiledICHolder* cichk_metdata = ic->cached_icholder();                                                                         
361 
362     if (cichk_metdata->is_loader_alive()) {                                                                                          
363       return;                                                                                                                        
364     }                                                                                                                                
365   } else {                                                                                                                           
366     Metadata* ic_metdata = ic->cached_metadata();                                                                                    
367     if (ic_metdata != NULL) {                                                                                                        
368       if (ic_metdata->is_klass()) {                                                                                                  
369         if (((Klass*)ic_metdata)->is_loader_alive()) {                                                                               
370           return;                                                                                                                    
371         }                                                                                                                            
372       } else if (ic_metdata->is_method()) {                                                                                          
373         Method* method = (Method*)ic_metdata;                                                                                        
374         assert(!method->is_old(), "old method should have been cleaned");                                                            
375         if (method->method_holder()->is_loader_alive()) {                                                                            
376           return;                                                                                                                    
377         }                                                                                                                            
378       } else {                                                                                                                       
379         ShouldNotReachHere();                                                                                                        
380       }                                                                                                                              
381     }                                                                                                                                
382   }                                                                                                                                  
383 
384   ic->set_to_clean();                                                                                                                
385 }                                                                                                                                    
386 
387 unsigned char CompiledMethod::_global_unloading_clock = 0;                                                                           
388 
389 void CompiledMethod::increase_unloading_clock() {                                                                                    
390   _global_unloading_clock++;                                                                                                         
391   if (_global_unloading_clock == 0) {                                                                                                
392     // _nmethods are allocated with _unloading_clock == 0,                                                                           
393     // so 0 is never used as a clock value.                                                                                          
394     _global_unloading_clock = 1;                                                                                                     
395   }                                                                                                                                  
396 }                                                                                                                                    
397 
398 void CompiledMethod::set_unloading_clock(unsigned char unloading_clock) {                                                            
399   OrderAccess::release_store(&_unloading_clock, unloading_clock);                                                                    
400 }                                                                                                                                    
401 
402 unsigned char CompiledMethod::unloading_clock() {                                                                                    
403   return OrderAccess::load_acquire(&_unloading_clock);                                                                               
404 }                                                                                                                                    
405 
406 
407 // static_stub_Relocations may have dangling references to                                                                           
408 // nmethods so trim them out here.  Otherwise it looks like                                                                          
409 // compiled code is maintaining a link to dead metadata.                                                                             
410 void CompiledMethod::clean_ic_stubs() {                                                                                              
411 #ifdef ASSERT                                                                                                                        
412   address low_boundary = oops_reloc_begin();                                                                                         
413   RelocIterator iter(this, low_boundary);                                                                                            
414   while (iter.next()) {                                                                                                              
415     address static_call_addr = NULL;                                                                                                 
416     if (iter.type() == relocInfo::opt_virtual_call_type) {                                                                           
417       CompiledIC* cic = CompiledIC_at(&iter);                                                                                        
418       if (!cic->is_call_to_interpreted()) {                                                                                          
419         static_call_addr = iter.addr();                                                                                              
420       }                                                                                                                              
421     } else if (iter.type() == relocInfo::static_call_type) {                                                                         
422       CompiledStaticCall* csc = compiledStaticCall_at(iter.reloc());                                                                 
423       if (!csc->is_call_to_interpreted()) {                                                                                          
424         static_call_addr = iter.addr();                                                                                              
425       }                                                                                                                              
426     }                                                                                                                                
427     if (static_call_addr != NULL) {                                                                                                  
428       RelocIterator sciter(this, low_boundary);                                                                                      
429       while (sciter.next()) {                                                                                                        
430         if (sciter.type() == relocInfo::static_stub_type &&                                                                          
431             sciter.static_stub_reloc()->static_call() == static_call_addr) {                                                         
432           sciter.static_stub_reloc()->clear_inline_cache();                                                                          
433         }                                                                                                                            
434       }                                                                                                                              
435     }                                                                                                                                
436   }                                                                                                                                  
437 #endif                                                                                                                               
438 }                                                                                                                                    
439 
440 // This is called at the end of the strong tracing/marking phase of a                                                                
441 // GC to unload an nmethod if it contains otherwise unreachable                                                                      
442 // oops.                                                                                                                             
443 
444 void CompiledMethod::do_unloading(BoolObjectClosure* is_alive) {                                                                     
445   // Make sure the oop's ready to receive visitors                                                                                   
446   assert(!is_zombie() && !is_unloaded(),                                                                                             
447          "should not call follow on zombie or unloaded nmethod");                                                                    
448 
449   address low_boundary = oops_reloc_begin();                                                                                         
450 
451   if (do_unloading_oops(low_boundary, is_alive)) {                                                                                   
452     return;                                                                                                                          
453   }                                                                                                                                  
454 
455 #if INCLUDE_JVMCI                                                                                                                    
456   if (do_unloading_jvmci()) {                                                                                                        
457     return;                                                                                                                          
458   }                                                                                                                                  
459 #endif                                                                                                                               
460 
461   // Cleanup exception cache and inline caches happens                                                                               
462   // after all the unloaded methods are found.                                                                                       
463 }                                                                                                                                    
464 
465 // Clean references to unloaded nmethods at addr from this one, which is not unloaded.                                               
466 template <class CompiledICorStaticCall>                                                                                              
467 static bool clean_if_nmethod_is_unloaded(CompiledICorStaticCall *ic, address addr, CompiledMethod* from,                             
468                                          bool parallel, bool clean_all) {                                                            
469   // Ok, to lookup references to zombies here                                                                                        
470   CodeBlob *cb = CodeCache::find_blob_unsafe(addr);                                                                                  
471   CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;                                                       
472   if (nm != NULL) {                                                                                                                  
473     if (parallel && nm->unloading_clock() != CompiledMethod::global_unloading_clock()) {                                             
474       // The nmethod has not been processed yet.                                                                                     
475       return true;                                                                                                                   
476     }                                                                                                                                
477 
478     // Clean inline caches pointing to both zombie and not_entrant methods                                                           
479     if (clean_all || !nm->is_in_use() || (nm->method()->code() != nm)) {                                                             
480       ic->set_to_clean(from->is_alive());                                                                                            
481       assert(ic->is_clean(), "nmethod " PTR_FORMAT "not clean %s", p2i(from), from->method()->name_and_sig_as_C_string());           
482     }                                                                                                                                
483   }                                                                                                                                  
484 
485   return false;                                                                                                                      
486 }                                                                                                                                    
487 
488 static bool clean_if_nmethod_is_unloaded(CompiledIC *ic, CompiledMethod* from,                                                       
489                                          bool parallel, bool clean_all = false) {                                                    
490   return clean_if_nmethod_is_unloaded(ic, ic->ic_destination(), from, parallel, clean_all);                                          
491 }                                                                                                                                    
492 
493 static bool clean_if_nmethod_is_unloaded(CompiledStaticCall *csc, CompiledMethod* from,                                              
494                                          bool parallel, bool clean_all = false) {                                                    
495   return clean_if_nmethod_is_unloaded(csc, csc->destination(), from, parallel, clean_all);                                           
496 }                                                                                                                                    
497 
498 bool CompiledMethod::do_unloading_parallel(BoolObjectClosure* is_alive, bool unloading_occurred) {                                   
499   ResourceMark rm;                                                                                                                   
500 
501   // Make sure the oop's ready to receive visitors                                                                                   
502   assert(!is_zombie() && !is_unloaded(),                                                                                             
503          "should not call follow on zombie or unloaded nmethod");                                                                    
504 
505   address low_boundary = oops_reloc_begin();                                                                                         
506 
507   if (do_unloading_oops(low_boundary, is_alive)) {                                                                                   
508     return false;                                                                                                                    
509   }                                                                                                                                  
510 
511 #if INCLUDE_JVMCI                                                                                                                    
512   if (do_unloading_jvmci()) {                                                                                                        
513     return false;                                                                                                                    
514   }                                                                                                                                  
515 #endif                                                                                                                               
516 
517   return unload_nmethod_caches(/*parallel*/true, unloading_occurred);                                                                
518 }                                                                                                                                    
519 
520 // Cleans caches in nmethods that point to either classes that are unloaded                                                          
521 // or nmethods that are unloaded.                                                                                                    
522 //                                                                                                                                   
523 // Can be called either in parallel by G1 currently or after all                                                                     
524 // nmethods are unloaded.  Return postponed=true in the parallel case for                                                            
525 // inline caches found that point to nmethods that are not yet visited during                                                        
526 // the do_unloading walk.                                                                                                            
527 bool CompiledMethod::unload_nmethod_caches(bool parallel, bool unloading_occurred) {                                                 
528 
529   // Exception cache only needs to be called if unloading occurred                                                                   
530   if (unloading_occurred) {                                                                                                          
531     clean_exception_cache();                                                                                                         
532   }                                                                                                                                  
533 
534   bool postponed = cleanup_inline_caches_impl(parallel, unloading_occurred, /*clean_all*/false);                                     
535 
536   // All static stubs need to be cleaned.                                                                                            
537   clean_ic_stubs();                                                                                                                  
538 
539   // Check that the metadata embedded in the nmethod is alive                                                                        
540   DEBUG_ONLY(metadata_do(check_class));                                                                                              
541 
542   return postponed;                                                                                                                  
543 }                                                                                                                                    
544 
545 // Called to clean up after class unloading for live nmethods and from the sweeper                                                   
546 // for all methods.                                                                                                                  
547 bool CompiledMethod::cleanup_inline_caches_impl(bool parallel, bool unloading_occurred, bool clean_all) {                            
548   assert_locked_or_safepoint(CompiledIC_lock);                                                                                       
549   bool postponed = false;                                                                                                            
550   ResourceMark rm;                                                                                                                   
551 
552   // Find all calls in an nmethod and clear the ones that point to non-entrant,                                                      
553   // zombie and unloaded nmethods.                                                                                                   
554   RelocIterator iter(this, oops_reloc_begin());                                                                                      
555   while(iter.next()) {                                                                                                               
556 
557     switch (iter.type()) {                                                                                                           
558 
559     case relocInfo::virtual_call_type:                                                                                               
560       if (unloading_occurred) {                                                                                                      
561         // If class unloading occurred we first clear ICs where the cached metadata                                                  
562         // is referring to an unloaded klass or method.                                                                              
563         clean_ic_if_metadata_is_dead(CompiledIC_at(&iter));                                                                          
564       }                                                                                                                              
565 
566       postponed |= clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), this, parallel, clean_all);                                    
567       break;                                                                                                                         
568 
569     case relocInfo::opt_virtual_call_type:                                                                                           
570       postponed |= clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), this, parallel, clean_all);                                    
571       break;                                                                                                                         
572 
573     case relocInfo::static_call_type:                                                                                                
574       postponed |= clean_if_nmethod_is_unloaded(compiledStaticCall_at(iter.reloc()), this, parallel, clean_all);                     
575       break;                                                                                                                         
576 
577     case relocInfo::oop_type:                                                                                                        
578       // handled by do_unloading_oops already                                                                                        
579       break;                                                                                                                         
580 
581     case relocInfo::metadata_type:                                                                                                   
582       break; // nothing to do.                                                                                                       
583 
584     default:                                                                                                                         
585       break;                                                                                                                         
586     }                                                                                                                                
587   }                                                                                                                                  
588 
589   return postponed;                                                                                                                  
590 }                                                                                                                                    
591 
592 void CompiledMethod::do_unloading_parallel_postponed() {                                                                             
593   ResourceMark rm;                                                                                                                   
594 
595   // Make sure the oop's ready to receive visitors                                                                                   
596   assert(!is_zombie(),                                                                                                               
597          "should not call follow on zombie nmethod");                                                                                
598 
599   RelocIterator iter(this, oops_reloc_begin());                                                                                      
600   while(iter.next()) {                                                                                                               
601 
602     switch (iter.type()) {                                                                                                           
603 
604     case relocInfo::virtual_call_type:                                                                                               
605       clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), this, true);                                                                
606       break;                                                                                                                         
607 
608     case relocInfo::opt_virtual_call_type:                                                                                           
609       clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), this, true);                                                                
610       break;                                                                                                                         
611 
612     case relocInfo::static_call_type:                                                                                                
613       clean_if_nmethod_is_unloaded(compiledStaticCall_at(iter.reloc()), this, true);                                                 
614       break;                                                                                                                         
615 
616     default:                                                                                                                         
617       break;                                                                                                                         
618     }                                                                                                                                
619   }                                                                                                                                  
620 }                                                                                                                                    
621 
622 // Iterating over all nmethods, e.g. with the help of CodeCache::nmethods_do(fun) was found                                          
623 // to not be inherently safe. There is a chance that fields are seen which are not properly                                          
624 // initialized. This happens despite the fact that nmethods_do() asserts the CodeCache_lock                                          
625 // to be held.                                                                                                                       
626 // To bundle knowledge about necessary checks in one place, this function was introduced.                                            
627 // It is not claimed that these checks are sufficient, but they were found to be necessary.                                          
628 bool CompiledMethod::nmethod_access_is_safe(nmethod* nm) {                                                                           
629   Method* method = (nm == NULL) ? NULL : nm->method();  // nm->method() may be uninitialized, i.e. != NULL, but invalid              
630   return (nm != NULL) && (method != NULL) && (method->signature() != NULL) &&                                                        
631          !nm->is_zombie() && !nm->is_not_installed() &&                                                                              
632          os::is_readable_pointer(method) &&                                                                                          
633          os::is_readable_pointer(method->constants()) &&                                                                             
634          os::is_readable_pointer(method->signature());                                                                               
635 }                                                                                                                                    
--- EOF ---