1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "precompiled.hpp"
25
26#include "aot/aotCodeHeap.hpp"
27#include "aot/aotLoader.hpp"
28#include "aot/compiledIC_aot.hpp"
29#include "code/codeCache.hpp"
30#include "code/compiledIC.hpp"
31#include "code/nativeInst.hpp"
32#include "compiler/compilerOracle.hpp"
33#include "gc/shared/cardTableModRefBS.hpp"
34#include "gc/shared/collectedHeap.hpp"
35#include "gc/shared/gcLocker.hpp"
36#include "jvmci/compilerRuntime.hpp"
37#include "jvmci/jvmciRuntime.hpp"
38#include "oops/method.hpp"
39#include "runtime/java.hpp"
40#include "runtime/os.hpp"
41#include "runtime/sharedRuntime.hpp"
42#include "utilities/array.hpp"
43#include "utilities/xmlstream.hpp"
44
45#include <dlfcn.h>
46#include <stdio.h>
47
48#if 0
49static void metadata_oops_do(Metadata** metadata_begin, Metadata **metadata_end, OopClosure* f) {
50  // Visit the metadata/oops section
51  for (Metadata** p = metadata_begin; p < metadata_end; p++) {
52    Metadata* m = *p;
53
54    intptr_t meta = (intptr_t)m;
55    if ((meta & 1) == 1) {
56      // already resolved
57      m = (Metadata*)(meta & ~1);
58    } else {
59      continue;
60    }
61    assert(Metaspace::contains(m), "");
62    if (m->is_method()) {
63      m = ((Method*)m)->method_holder();
64    }
65    assert(m->is_klass(), "must be");
66    oop o = ((Klass*)m)->klass_holder();
67    if (o != NULL) {
68      f->do_oop(&o);
69    }
70  }
71}
72#endif
73
74bool AOTCompiledMethod::do_unloading_oops(address low_boundary, BoolObjectClosure* is_alive, bool unloading_occurred) {
75  return false;
76}
77
78oop AOTCompiledMethod::oop_at(int index) const {
79  if (index == 0) { // 0 is reserved
80    return NULL;
81  }
82  Metadata** entry = _metadata_got + (index - 1);
83  intptr_t meta = (intptr_t)*entry;
84  if ((meta & 1) == 1) {
85    // already resolved
86    Klass* k = (Klass*)(meta & ~1);
87    return k->java_mirror();
88  }
89  // The entry is string which we need to resolve.
90  const char* meta_name = _heap->get_name_at((int)meta);
91  int klass_len = build_u2_from((address)meta_name);
92  const char* klass_name = meta_name + 2;
93  // Quick check the current method's holder.
94  Klass* k = _method->method_holder();
95
96  ResourceMark rm; // for signature_name()
97  if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
98    // Search klass in got cells in DSO which have this compiled method.
99    k = _heap->get_klass_from_got(klass_name, klass_len, _method);
100  }
101  int method_name_len = build_u2_from((address)klass_name + klass_len);
102  guarantee(method_name_len == 0, "only klass is expected here");
103  meta = ((intptr_t)k) | 1;
104  *entry = (Metadata*)meta; // Should be atomic on x64
105  return k->java_mirror();
106}
107
108Metadata* AOTCompiledMethod::metadata_at(int index) const {
109  if (index == 0) { // 0 is reserved
110    return NULL;
111  }
112  assert(index - 1 < _metadata_size, "");
113  {
114    Metadata** entry = _metadata_got + (index - 1);
115    intptr_t meta = (intptr_t)*entry;
116    if ((meta & 1) == 1) {
117      // already resolved
118      Metadata *m = (Metadata*)(meta & ~1);
119      return m;
120    }
121    // The entry is string which we need to resolve.
122    const char* meta_name = _heap->get_name_at((int)meta);
123    int klass_len = build_u2_from((address)meta_name);
124    const char* klass_name = meta_name + 2;
125    // Quick check the current method's holder.
126    Klass* k = _method->method_holder();
127    bool klass_matched = true;
128
129    ResourceMark rm; // for signature_name() and find_method()
130    if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
131      // Search klass in got cells in DSO which have this compiled method.
132      k = _heap->get_klass_from_got(klass_name, klass_len, _method);
133      klass_matched = false;
134    }
135    int method_name_len = build_u2_from((address)klass_name + klass_len);
136    if (method_name_len == 0) { // Array or Klass name only?
137      meta = ((intptr_t)k) | 1;
138      *entry = (Metadata*)meta; // Should be atomic on x64
139      return (Metadata*)k;
140    } else { // Method
141      // Quick check the current method's name.
142      Method* m = _method;
143      int signature_len = build_u2_from((address)klass_name + klass_len + 2 + method_name_len);
144      int full_len = 2 + klass_len + 2 + method_name_len + 2 + signature_len;
145      if (!klass_matched || memcmp(_name, meta_name, full_len) != 0) { // Does not match?
146        Thread* thread = Thread::current();
147        KlassHandle klass = KlassHandle(thread, k);
148        const char* method_name = klass_name + klass_len;
149        m = AOTCodeHeap::find_method(klass, thread, method_name);
150      }
151      meta = ((intptr_t)m) | 1;
152      *entry = (Metadata*)meta; // Should be atomic on x64
153      return (Metadata*)m;
154    }
155  }
156  ShouldNotReachHere(); return NULL;
157}
158
159bool AOTCompiledMethod::make_not_entrant_helper(int new_state) {
160  // Make sure the method is not flushed in case of a safepoint in code below.
161  methodHandle the_method(method());
162  NoSafepointVerifier nsv;
163
164  {
165    // Enter critical section.  Does not block for safepoint.
166    MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
167
168    if (*_state_adr == new_state) {
169      // another thread already performed this transition so nothing
170      // to do, but return false to indicate this.
171      return false;
172    }
173
174    // Change state
175    OrderAccess::storestore();
176    *_state_adr = new_state;
177
178    // Log the transition once
179    log_state_change();
180
181#ifdef TIERED
182    // Remain non-entrant forever
183    if (new_state == not_entrant && method() != NULL) {
184        method()->set_aot_code(NULL);
185    }
186#endif
187
188    // Remove AOTCompiledMethod from method.
189    if (method() != NULL && (method()->code() == this ||
190                             method()->from_compiled_entry() == verified_entry_point())) {
191      HandleMark hm;
192      method()->clear_code(false /* already owns Patching_lock */);
193    }
194  } // leave critical region under Patching_lock
195
196
197  if (TraceCreateZombies) {
198    ResourceMark m;
199    const char *new_state_str = (new_state == not_entrant) ? "not entrant" : "not used";
200    tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null", new_state_str);
201  }
202
203  return true;
204}
205
206bool AOTCompiledMethod::make_entrant() {
207  assert(!method()->is_old(), "reviving evolved method!");
208  assert(*_state_adr != not_entrant, "%s", method()->has_aot_code() ? "has_aot_code() not cleared" : "caller didn't check has_aot_code()");
209
210  // Make sure the method is not flushed in case of a safepoint in code below.
211  methodHandle the_method(method());
212  NoSafepointVerifier nsv;
213
214  {
215    // Enter critical section.  Does not block for safepoint.
216    MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
217
218    if (*_state_adr == in_use) {
219      // another thread already performed this transition so nothing
220      // to do, but return false to indicate this.
221      return false;
222    }
223
224    // Change state
225    OrderAccess::storestore();
226    *_state_adr = in_use;
227
228    // Log the transition once
229    log_state_change();
230  } // leave critical region under Patching_lock
231
232
233  if (TraceCreateZombies) {
234    ResourceMark m;
235    tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made entrant", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null");
236  }
237
238  return true;
239}
240
241// We don't have full dependencies for AOT methods, so flushing is
242// more conservative than for nmethods.
243void AOTCompiledMethod::flush_evol_dependents_on(instanceKlassHandle dependee) {
244  if (is_java_method()) {
245    cleanup_inline_caches();
246    mark_for_deoptimization();
247    make_not_entrant();
248  }
249}
250
251// Iterate over metadata calling this function.   Used by RedefineClasses
252// Copied from nmethod::metadata_do
253void AOTCompiledMethod::metadata_do(void f(Metadata*)) {
254  address low_boundary = verified_entry_point();
255  {
256    // Visit all immediate references that are embedded in the instruction stream.
257    RelocIterator iter(this, low_boundary);
258    while (iter.next()) {
259      if (iter.type() == relocInfo::metadata_type ) {
260        metadata_Relocation* r = iter.metadata_reloc();
261        // In this metadata, we must only follow those metadatas directly embedded in
262        // the code.  Other metadatas (oop_index>0) are seen as part of
263        // the metadata section below.
264        assert(1 == (r->metadata_is_immediate()) +
265               (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
266               "metadata must be found in exactly one place");
267        if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
268          Metadata* md = r->metadata_value();
269          if (md != _method) f(md);
270        }
271      } else if (iter.type() == relocInfo::virtual_call_type) {
272        // Check compiledIC holders associated with this nmethod
273        CompiledIC *ic = CompiledIC_at(&iter);
274        if (ic->is_icholder_call()) {
275          CompiledICHolder* cichk = ic->cached_icholder();
276          f(cichk->holder_method());
277          f(cichk->holder_klass());
278        } else {
279          // Get Klass* or NULL (if value is -1) from GOT cell of virtual call PLT stub.
280          Metadata* ic_oop = ic->cached_metadata();
281          if (ic_oop != NULL) {
282            f(ic_oop);
283          }
284        }
285      } else if (iter.type() == relocInfo::static_call_type ||
286                 iter.type() == relocInfo::opt_virtual_call_type){
287        // Check Method* in AOT c2i stub for other calls.
288        Metadata* meta = (Metadata*)nativeLoadGot_at(nativePltCall_at(iter.addr())->plt_c2i_stub())->data();
289        if (meta != NULL) {
290          f(meta);
291        }
292      }
293    }
294  }
295
296  // Visit the metadata section
297  for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
298    Metadata* m = *p;
299
300    intptr_t meta = (intptr_t)m;
301    if ((meta & 1) == 1) {
302      // already resolved
303      m = (Metadata*)(meta & ~1);
304    } else {
305      continue;
306    }
307    assert(Metaspace::contains(m), "");
308    f(m);
309  }
310
311  // Visit metadata not embedded in the other places.
312  if (_method != NULL) f(_method);
313}
314
315void AOTCompiledMethod::print() const {
316  print_on(tty, "AOTCompiledMethod");
317}
318
319void AOTCompiledMethod::print_on(outputStream* st) const {
320  print_on(st, "AOTCompiledMethod");
321}
322
323// Print out more verbose output usually for a newly created aot method.
324void AOTCompiledMethod::print_on(outputStream* st, const char* msg) const {
325  if (st != NULL) {
326    ttyLocker ttyl;
327    st->print("%7d ", (int) st->time_stamp().milliseconds());
328    st->print("%4d ", _aot_id);    // print compilation number
329    st->print("    aot[%2d]", _heap->dso_id());
330    // Stubs have _method == NULL
331    if (_method == NULL) {
332      st->print("   %s", _name);
333    } else {
334      ResourceMark m;
335      st->print("   %s", _method->name_and_sig_as_C_string());
336    }
337    if (Verbose) {
338      st->print(" entry at " INTPTR_FORMAT, p2i(_code));
339    }
340    if (msg != NULL) {
341      st->print("   %s", msg);
342    }
343    st->cr();
344  }
345}
346
347void AOTCompiledMethod::print_value_on(outputStream* st) const {
348  st->print("AOTCompiledMethod ");
349  print_on(st, NULL);
350}
351
352// Print a short set of xml attributes to identify this aot method.  The
353// output should be embedded in some other element.
354void AOTCompiledMethod::log_identity(xmlStream* log) const {
355  log->print(" aot_id='%d'", _aot_id);
356  log->print(" aot='%2d'", _heap->dso_id());
357}
358
359void AOTCompiledMethod::log_state_change() const {
360  if (LogCompilation) {
361    ResourceMark m;
362    if (xtty != NULL) {
363      ttyLocker ttyl;  // keep the following output all in one block
364      if (*_state_adr == not_entrant) {
365        xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'",
366                         os::current_thread_id());
367      } else if (*_state_adr == not_used) {
368        xtty->begin_elem("make_not_used thread='" UINTX_FORMAT "'",
369                         os::current_thread_id());
370      } else if (*_state_adr == in_use) {
371        xtty->begin_elem("make_entrant thread='" UINTX_FORMAT "'",
372                         os::current_thread_id());
373      }
374      log_identity(xtty);
375      xtty->stamp();
376      xtty->end_elem();
377    }
378  }
379  if (PrintCompilation) {
380    ResourceMark m;
381    if (*_state_adr == not_entrant) {
382      print_on(tty, "made not entrant");
383    } else if (*_state_adr == not_used) {
384      print_on(tty, "made not used");
385    } else if (*_state_adr == in_use) {
386      print_on(tty, "made entrant");
387    }
388  }
389}
390
391
392NativeInstruction* PltNativeCallWrapper::get_load_instruction(virtual_call_Relocation* r) const {
393  return nativeLoadGot_at(_call->plt_load_got());
394}
395
396void PltNativeCallWrapper::verify_resolve_call(address dest) const {
397  CodeBlob* db = CodeCache::find_blob_unsafe(dest);
398  if (db == NULL) {
399    assert(dest == _call->plt_resolve_call(), "sanity");
400  }
401}
402
403void PltNativeCallWrapper::set_to_interpreted(const methodHandle& method, CompiledICInfo& info) {
404  assert(!info.to_aot(), "only for nmethod");
405  CompiledPltStaticCall* csc = CompiledPltStaticCall::at(instruction_address());
406  csc->set_to_interpreted(method, info.entry());
407}
408
409NativeCallWrapper* AOTCompiledMethod::call_wrapper_at(address call) const {
410  return new PltNativeCallWrapper((NativePltCall*) call);
411}
412
413NativeCallWrapper* AOTCompiledMethod::call_wrapper_before(address return_pc) const {
414  return new PltNativeCallWrapper(nativePltCall_before(return_pc));
415}
416
417CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(Relocation* call_site) const {
418  return CompiledPltStaticCall::at(call_site);
419}
420
421CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(address call_site) const {
422  return CompiledPltStaticCall::at(call_site);
423}
424
425CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_before(address return_addr) const {
426  return CompiledPltStaticCall::before(return_addr);
427}
428
429address AOTCompiledMethod::call_instruction_address(address pc) const {
430  NativePltCall* pltcall = nativePltCall_before(pc);
431  return pltcall->instruction_address();
432}
433
434bool AOTCompiledMethod::is_evol_dependent_on(Klass* dependee) {
435  return !is_aot_runtime_stub() && _heap->is_dependent_method(dependee, this);
436}
437
438void AOTCompiledMethod::clear_inline_caches() {
439  assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
440  if (is_zombie()) {
441    return;
442  }
443
444  RelocIterator iter(this);
445  while (iter.next()) {
446    iter.reloc()->clear_inline_cache();
447    if (iter.type() == relocInfo::opt_virtual_call_type) {
448      CompiledIC* cic = CompiledIC_at(&iter);
449      assert(cic->is_clean(), "!");
450      nativePltCall_at(iter.addr())->set_stub_to_clean();
451    }
452  }
453}
454
455