runtime.cc revision 2f8da3e9ff60e5cb2a3fdf57dbcb67f513b9c2c2
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "runtime.h"
18
19// sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc
20#include <sys/mount.h>
21#include <linux/fs.h>
22
23#include <signal.h>
24#include <sys/syscall.h>
25#include <valgrind.h>
26
27#include <cstdio>
28#include <cstdlib>
29#include <limits>
30#include <vector>
31#include <fcntl.h>
32
33#include "arch/arm/registers_arm.h"
34#include "arch/arm64/registers_arm64.h"
35#include "arch/mips/registers_mips.h"
36#include "arch/x86/registers_x86.h"
37#include "arch/x86_64/registers_x86_64.h"
38#include "atomic.h"
39#include "class_linker.h"
40#include "debugger.h"
41#include "gc/accounting/card_table-inl.h"
42#include "gc/heap.h"
43#include "gc/space/space.h"
44#include "image.h"
45#include "instrumentation.h"
46#include "intern_table.h"
47#include "jni_internal.h"
48#include "mirror/art_field-inl.h"
49#include "mirror/art_method-inl.h"
50#include "mirror/array.h"
51#include "mirror/class-inl.h"
52#include "mirror/class_loader.h"
53#include "mirror/stack_trace_element.h"
54#include "mirror/throwable.h"
55#include "monitor.h"
56#include "parsed_options.h"
57#include "oat_file.h"
58#include "reflection.h"
59#include "ScopedLocalRef.h"
60#include "scoped_thread_state_change.h"
61#include "signal_catcher.h"
62#include "signal_set.h"
63#include "sirt_ref.h"
64#include "thread.h"
65#include "thread_list.h"
66#include "trace.h"
67#include "transaction.h"
68#include "profiler.h"
69#include "UniquePtr.h"
70#include "verifier/method_verifier.h"
71#include "well_known_classes.h"
72
73#include "JniConstants.h"  // Last to avoid LOG redefinition in ics-mr1-plus-art.
74
75#ifdef HAVE_ANDROID_OS
76#include "cutils/properties.h"
77#endif
78
79namespace art {
80
81static constexpr bool kEnableJavaStackTraceHandler = true;
82const char* Runtime::kDefaultInstructionSetFeatures =
83    STRINGIFY(ART_DEFAULT_INSTRUCTION_SET_FEATURES);
84Runtime* Runtime::instance_ = NULL;
85
86Runtime::Runtime()
87    : pre_allocated_OutOfMemoryError_(nullptr),
88      resolution_method_(nullptr),
89      imt_conflict_method_(nullptr),
90      default_imt_(nullptr),
91      compiler_callbacks_(nullptr),
92      is_zygote_(false),
93      is_concurrent_gc_enabled_(true),
94      is_explicit_gc_disabled_(false),
95      default_stack_size_(0),
96      heap_(nullptr),
97      max_spins_before_thin_lock_inflation_(Monitor::kDefaultMaxSpinsBeforeThinLockInflation),
98      monitor_list_(nullptr),
99      monitor_pool_(nullptr),
100      thread_list_(nullptr),
101      intern_table_(nullptr),
102      class_linker_(nullptr),
103      signal_catcher_(nullptr),
104      java_vm_(nullptr),
105      fault_message_lock_("Fault message lock"),
106      fault_message_(""),
107      method_verifier_lock_("Method verifiers lock"),
108      threads_being_born_(0),
109      shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)),
110      shutting_down_(false),
111      shutting_down_started_(false),
112      started_(false),
113      finished_starting_(false),
114      vfprintf_(nullptr),
115      exit_(nullptr),
116      abort_(nullptr),
117      stats_enabled_(false),
118      running_on_valgrind_(RUNNING_ON_VALGRIND > 0),
119      profile_(false),
120      profile_period_s_(0),
121      profile_duration_s_(0),
122      profile_interval_us_(0),
123      profile_backoff_coefficient_(0),
124      profile_start_immediately_(true),
125      method_trace_(false),
126      method_trace_file_size_(0),
127      instrumentation_(),
128      use_compile_time_class_path_(false),
129      main_thread_group_(nullptr),
130      system_thread_group_(nullptr),
131      system_class_loader_(nullptr),
132      dump_gc_performance_on_shutdown_(false),
133      preinitialization_transaction_(nullptr),
134      null_pointer_handler_(nullptr),
135      suspend_handler_(nullptr),
136      stack_overflow_handler_(nullptr),
137      verify_(false) {
138  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
139    callee_save_methods_[i] = nullptr;
140  }
141}
142
143Runtime::~Runtime() {
144  if (dump_gc_performance_on_shutdown_) {
145    // This can't be called from the Heap destructor below because it
146    // could call RosAlloc::InspectAll() which needs the thread_list
147    // to be still alive.
148    heap_->DumpGcPerformanceInfo(LOG(INFO));
149  }
150
151  Thread* self = Thread::Current();
152  {
153    MutexLock mu(self, *Locks::runtime_shutdown_lock_);
154    shutting_down_started_ = true;
155    while (threads_being_born_ > 0) {
156      shutdown_cond_->Wait(self);
157    }
158    shutting_down_ = true;
159  }
160  Trace::Shutdown();
161
162  // Make sure to let the GC complete if it is running.
163  heap_->WaitForGcToComplete(self);
164  heap_->DeleteThreadPool();
165
166  // Make sure our internal threads are dead before we start tearing down things they're using.
167  Dbg::StopJdwp();
168  delete signal_catcher_;
169
170  // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
171  delete thread_list_;
172  delete monitor_list_;
173  delete monitor_pool_;
174  delete class_linker_;
175  delete heap_;
176  delete intern_table_;
177  delete java_vm_;
178  Thread::Shutdown();
179  QuasiAtomic::Shutdown();
180  verifier::MethodVerifier::Shutdown();
181  // TODO: acquire a static mutex on Runtime to avoid racing.
182  CHECK(instance_ == nullptr || instance_ == this);
183  instance_ = nullptr;
184
185  delete null_pointer_handler_;
186  delete suspend_handler_;
187  delete stack_overflow_handler_;
188}
189
190struct AbortState {
191  void Dump(std::ostream& os) NO_THREAD_SAFETY_ANALYSIS {
192    if (gAborting > 1) {
193      os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
194      return;
195    }
196    gAborting++;
197    os << "Runtime aborting...\n";
198    if (Runtime::Current() == NULL) {
199      os << "(Runtime does not yet exist!)\n";
200      return;
201    }
202    Thread* self = Thread::Current();
203    if (self == nullptr) {
204      os << "(Aborting thread was not attached to runtime!)\n";
205    } else {
206      os << "Aborting thread:\n";
207      if (Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self)) {
208        DumpThread(os, self);
209      } else {
210        if (Locks::mutator_lock_->SharedTryLock(self)) {
211          DumpThread(os, self);
212          Locks::mutator_lock_->SharedUnlock(self);
213        }
214      }
215    }
216    DumpAllThreads(os, self);
217  }
218
219  void DumpThread(std::ostream& os, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
220    self->Dump(os);
221    if (self->IsExceptionPending()) {
222      ThrowLocation throw_location;
223      mirror::Throwable* exception = self->GetException(&throw_location);
224      os << "Pending exception " << PrettyTypeOf(exception)
225          << " thrown by '" << throw_location.Dump() << "'\n"
226          << exception->Dump();
227    }
228  }
229
230  void DumpAllThreads(std::ostream& os, Thread* self) NO_THREAD_SAFETY_ANALYSIS {
231    bool tll_already_held = Locks::thread_list_lock_->IsExclusiveHeld(self);
232    bool ml_already_held = Locks::mutator_lock_->IsSharedHeld(self);
233    if (!tll_already_held || !ml_already_held) {
234      os << "Dumping all threads without appropriate locks held:"
235          << (!tll_already_held ? " thread list lock" : "")
236          << (!ml_already_held ? " mutator lock" : "")
237          << "\n";
238    }
239    os << "All threads:\n";
240    Runtime::Current()->GetThreadList()->DumpLocked(os);
241  }
242};
243
244void Runtime::Abort() {
245  gAborting++;  // set before taking any locks
246
247  // Ensure that we don't have multiple threads trying to abort at once,
248  // which would result in significantly worse diagnostics.
249  MutexLock mu(Thread::Current(), *Locks::abort_lock_);
250
251  // Get any pending output out of the way.
252  fflush(NULL);
253
254  // Many people have difficulty distinguish aborts from crashes,
255  // so be explicit.
256  AbortState state;
257  LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
258
259  // Call the abort hook if we have one.
260  if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
261    LOG(INTERNAL_FATAL) << "Calling abort hook...";
262    Runtime::Current()->abort_();
263    // notreached
264    LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
265  }
266
267#if defined(__GLIBC__)
268  // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
269  // which POSIX defines in terms of raise(3), which POSIX defines in terms
270  // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
271  // libpthread, which means the stacks we dump would be useless. Calling
272  // tgkill(2) directly avoids that.
273  syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
274  // TODO: LLVM installs it's own SIGABRT handler so exit to be safe... Can we disable that in LLVM?
275  // If not, we could use sigaction(3) before calling tgkill(2) and lose this call to exit(3).
276  exit(1);
277#else
278  abort();
279#endif
280  // notreached
281}
282
283void Runtime::PreZygoteFork() {
284  heap_->PreZygoteFork();
285}
286
287void Runtime::CallExitHook(jint status) {
288  if (exit_ != NULL) {
289    ScopedThreadStateChange tsc(Thread::Current(), kNative);
290    exit_(status);
291    LOG(WARNING) << "Exit hook returned instead of exiting!";
292  }
293}
294
295void Runtime::SweepSystemWeaks(IsMarkedCallback* visitor, void* arg) {
296  GetInternTable()->SweepInternTableWeaks(visitor, arg);
297  GetMonitorList()->SweepMonitorList(visitor, arg);
298  GetJavaVM()->SweepJniWeakGlobals(visitor, arg);
299  Dbg::UpdateObjectPointers(visitor, arg);
300}
301
302bool Runtime::Create(const Options& options, bool ignore_unrecognized) {
303  // TODO: acquire a static mutex on Runtime to avoid racing.
304  if (Runtime::instance_ != NULL) {
305    return false;
306  }
307  InitLogging(NULL);  // Calls Locks::Init() as a side effect.
308  instance_ = new Runtime;
309  if (!instance_->Init(options, ignore_unrecognized)) {
310    delete instance_;
311    instance_ = NULL;
312    return false;
313  }
314  return true;
315}
316
317jobject CreateSystemClassLoader() {
318  if (Runtime::Current()->UseCompileTimeClassPath()) {
319    return NULL;
320  }
321
322  ScopedObjectAccess soa(Thread::Current());
323  ClassLinker* cl = Runtime::Current()->GetClassLinker();
324
325  SirtRef<mirror::Class> class_loader_class(
326      soa.Self(), soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader));
327  CHECK(cl->EnsureInitialized(class_loader_class, true, true));
328
329  mirror::ArtMethod* getSystemClassLoader =
330      class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
331  CHECK(getSystemClassLoader != NULL);
332
333  JValue result = InvokeWithJValues(soa, nullptr, soa.EncodeMethod(getSystemClassLoader), nullptr);
334  SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
335                                            down_cast<mirror::ClassLoader*>(result.GetL()));
336  CHECK(class_loader.get() != nullptr);
337  JNIEnv* env = soa.Self()->GetJniEnv();
338  ScopedLocalRef<jobject> system_class_loader(env,
339                                              soa.AddLocalReference<jobject>(class_loader.get()));
340  CHECK(system_class_loader.get() != nullptr);
341
342  soa.Self()->SetClassLoaderOverride(class_loader.get());
343
344  SirtRef<mirror::Class> thread_class(
345      soa.Self(),
346      soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread));
347  CHECK(cl->EnsureInitialized(thread_class, true, true));
348
349  mirror::ArtField* contextClassLoader =
350      thread_class->FindDeclaredInstanceField("contextClassLoader", "Ljava/lang/ClassLoader;");
351  CHECK(contextClassLoader != NULL);
352
353  // We can't run in a transaction yet.
354  contextClassLoader->SetObject<false>(soa.Self()->GetPeer(), class_loader.get());
355
356  return env->NewGlobalRef(system_class_loader.get());
357}
358
359bool Runtime::Start() {
360  VLOG(startup) << "Runtime::Start entering";
361
362  // Restore main thread state to kNative as expected by native code.
363  Thread* self = Thread::Current();
364  self->TransitionFromRunnableToSuspended(kNative);
365
366  started_ = true;
367
368  // InitNativeMethods needs to be after started_ so that the classes
369  // it touches will have methods linked to the oat file if necessary.
370  InitNativeMethods();
371
372  // Initialize well known thread group values that may be accessed threads while attaching.
373  InitThreadGroups(self);
374
375  Thread::FinishStartup();
376
377  if (is_zygote_) {
378    if (!InitZygote()) {
379      return false;
380    }
381  } else {
382    DidForkFromZygote();
383  }
384
385  StartDaemonThreads();
386
387  system_class_loader_ = CreateSystemClassLoader();
388
389  self->GetJniEnv()->locals.AssertEmpty();
390
391  VLOG(startup) << "Runtime::Start exiting";
392
393  finished_starting_ = true;
394
395  if (profile_) {
396    // User has asked for a profile using -Xprofile
397    // Create the profile file if it doesn't exist.
398    int fd = open(profile_output_filename_.c_str(), O_RDWR|O_CREAT|O_EXCL, 0660);
399    if (fd >= 0) {
400      close(fd);
401    }
402    StartProfiler(profile_output_filename_.c_str(), "");
403  }
404
405  return true;
406}
407
408void Runtime::EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
409  DCHECK_GT(threads_being_born_, 0U);
410  threads_being_born_--;
411  if (shutting_down_started_ && threads_being_born_ == 0) {
412    shutdown_cond_->Broadcast(Thread::Current());
413  }
414}
415
416// Do zygote-mode-only initialization.
417bool Runtime::InitZygote() {
418  // zygote goes into its own process group
419  setpgid(0, 0);
420
421  // See storage config details at http://source.android.com/tech/storage/
422  // Create private mount namespace shared by all children
423  if (unshare(CLONE_NEWNS) == -1) {
424    PLOG(WARNING) << "Failed to unshare()";
425    return false;
426  }
427
428  // Mark rootfs as being a slave so that changes from default
429  // namespace only flow into our children.
430  if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) {
431    PLOG(WARNING) << "Failed to mount() rootfs as MS_SLAVE";
432    return false;
433  }
434
435  // Create a staging tmpfs that is shared by our children; they will
436  // bind mount storage into their respective private namespaces, which
437  // are isolated from each other.
438  const char* target_base = getenv("EMULATED_STORAGE_TARGET");
439  if (target_base != NULL) {
440    if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
441              "uid=0,gid=1028,mode=0751") == -1) {
442      LOG(WARNING) << "Failed to mount tmpfs to " << target_base;
443      return false;
444    }
445  }
446
447  return true;
448}
449
450void Runtime::DidForkFromZygote() {
451  is_zygote_ = false;
452
453  // Create the thread pool.
454  heap_->CreateThreadPool();
455
456  StartSignalCatcher();
457
458  // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
459  // this will pause the runtime, so we probably want this to come last.
460  Dbg::StartJdwp();
461}
462
463void Runtime::StartSignalCatcher() {
464  if (!is_zygote_) {
465    signal_catcher_ = new SignalCatcher(stack_trace_file_);
466  }
467}
468
469bool Runtime::IsShuttingDown(Thread* self) {
470  MutexLock mu(self, *Locks::runtime_shutdown_lock_);
471  return IsShuttingDownLocked();
472}
473
474void Runtime::StartDaemonThreads() {
475  VLOG(startup) << "Runtime::StartDaemonThreads entering";
476
477  Thread* self = Thread::Current();
478
479  // Must be in the kNative state for calling native methods.
480  CHECK_EQ(self->GetState(), kNative);
481
482  JNIEnv* env = self->GetJniEnv();
483  env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons,
484                            WellKnownClasses::java_lang_Daemons_start);
485  if (env->ExceptionCheck()) {
486    env->ExceptionDescribe();
487    LOG(FATAL) << "Error starting java.lang.Daemons";
488  }
489
490  VLOG(startup) << "Runtime::StartDaemonThreads exiting";
491}
492
493bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
494  CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
495
496  UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
497  if (options.get() == NULL) {
498    LOG(ERROR) << "Failed to parse options";
499    return false;
500  }
501  VLOG(startup) << "Runtime::Init -verbose:startup enabled";
502
503  QuasiAtomic::Startup();
504
505  Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
506
507  boot_class_path_string_ = options->boot_class_path_string_;
508  class_path_string_ = options->class_path_string_;
509  properties_ = options->properties_;
510
511  compiler_callbacks_ = options->compiler_callbacks_;
512  is_zygote_ = options->is_zygote_;
513  is_explicit_gc_disabled_ = options->is_explicit_gc_disabled_;
514
515  vfprintf_ = options->hook_vfprintf_;
516  exit_ = options->hook_exit_;
517  abort_ = options->hook_abort_;
518
519  default_stack_size_ = options->stack_size_;
520  stack_trace_file_ = options->stack_trace_file_;
521
522  compiler_options_ = options->compiler_options_;
523  image_compiler_options_ = options->image_compiler_options_;
524
525  max_spins_before_thin_lock_inflation_ = options->max_spins_before_thin_lock_inflation_;
526
527  monitor_list_ = new MonitorList;
528  monitor_pool_ = MonitorPool::Create();
529  thread_list_ = new ThreadList;
530  intern_table_ = new InternTable;
531
532  verify_ = options->verify_;
533
534  if (options->interpreter_only_) {
535    GetInstrumentation()->ForceInterpretOnly();
536  }
537
538  if (options->explicit_checks_ != (ParsedOptions::kExplicitSuspendCheck |
539        ParsedOptions::kExplicitNullCheck |
540        ParsedOptions::kExplicitStackOverflowCheck) || kEnableJavaStackTraceHandler) {
541    fault_manager.Init();
542
543    // These need to be in a specific order.  The null point check handler must be
544    // after the suspend check and stack overflow check handlers.
545    if ((options->explicit_checks_ & ParsedOptions::kExplicitSuspendCheck) == 0) {
546      suspend_handler_ = new SuspensionHandler(&fault_manager);
547    }
548
549    if ((options->explicit_checks_ & ParsedOptions::kExplicitStackOverflowCheck) == 0) {
550      stack_overflow_handler_ = new StackOverflowHandler(&fault_manager);
551    }
552
553    if ((options->explicit_checks_ & ParsedOptions::kExplicitNullCheck) == 0) {
554      null_pointer_handler_ = new NullPointerHandler(&fault_manager);
555    }
556
557    if (kEnableJavaStackTraceHandler) {
558      new JavaStackTraceHandler(&fault_manager);
559    }
560  }
561
562  heap_ = new gc::Heap(options->heap_initial_size_,
563                       options->heap_growth_limit_,
564                       options->heap_min_free_,
565                       options->heap_max_free_,
566                       options->heap_target_utilization_,
567                       options->foreground_heap_growth_multiplier_,
568                       options->heap_maximum_size_,
569                       options->image_,
570                       options->collector_type_,
571                       options->background_collector_type_,
572                       options->parallel_gc_threads_,
573                       options->conc_gc_threads_,
574                       options->low_memory_mode_,
575                       options->long_pause_log_threshold_,
576                       options->long_gc_log_threshold_,
577                       options->ignore_max_footprint_,
578                       options->use_tlab_,
579                       options->verify_pre_gc_heap_,
580                       options->verify_post_gc_heap_,
581                       options->verify_pre_gc_rosalloc_,
582                       options->verify_post_gc_rosalloc_);
583
584  dump_gc_performance_on_shutdown_ = options->dump_gc_performance_on_shutdown_;
585
586  BlockSignals();
587  InitPlatformSignalHandlers();
588
589  java_vm_ = new JavaVMExt(this, options.get());
590
591  Thread::Startup();
592
593  // ClassLinker needs an attached thread, but we can't fully attach a thread without creating
594  // objects. We can't supply a thread group yet; it will be fixed later. Since we are the main
595  // thread, we do not get a java peer.
596  Thread* self = Thread::Attach("main", false, NULL, false);
597  CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId);
598  CHECK(self != NULL);
599
600  // Set us to runnable so tools using a runtime can allocate and GC by default
601  self->TransitionFromSuspendedToRunnable();
602
603  // Now we're attached, we can take the heap locks and validate the heap.
604  GetHeap()->EnableObjectValidation();
605
606  CHECK_GE(GetHeap()->GetContinuousSpaces().size(), 1U);
607  class_linker_ = new ClassLinker(intern_table_);
608  if (GetHeap()->HasImageSpace()) {
609    class_linker_->InitFromImage();
610  } else {
611    CHECK(options->boot_class_path_ != NULL);
612    CHECK_NE(options->boot_class_path_->size(), 0U);
613    class_linker_->InitFromCompiler(*options->boot_class_path_);
614  }
615  CHECK(class_linker_ != NULL);
616  verifier::MethodVerifier::Init();
617
618  method_trace_ = options->method_trace_;
619  method_trace_file_ = options->method_trace_file_;
620  method_trace_file_size_ = options->method_trace_file_size_;
621
622  // Extract the profile options.
623  // TODO: move into a Trace options struct?
624  profile_period_s_ = options->profile_period_s_;
625  profile_duration_s_ = options->profile_duration_s_;
626  profile_interval_us_ = options->profile_interval_us_;
627  profile_backoff_coefficient_ = options->profile_backoff_coefficient_;
628  profile_start_immediately_ = options->profile_start_immediately_;
629  profile_ = options->profile_;
630  profile_output_filename_ = options->profile_output_filename_;
631  // TODO: move this to just be an Trace::Start argument
632  Trace::SetDefaultClockSource(options->profile_clock_source_);
633
634  if (options->method_trace_) {
635    Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0,
636                 false, false, 0);
637  }
638
639  // Pre-allocate an OutOfMemoryError for the double-OOME case.
640  self->ThrowNewException(ThrowLocation(), "Ljava/lang/OutOfMemoryError;",
641                          "OutOfMemoryError thrown while trying to throw OutOfMemoryError; "
642                          "no stack available");
643  pre_allocated_OutOfMemoryError_ = self->GetException(NULL);
644  self->ClearException();
645
646  VLOG(startup) << "Runtime::Init exiting";
647  return true;
648}
649
650void Runtime::InitNativeMethods() {
651  VLOG(startup) << "Runtime::InitNativeMethods entering";
652  Thread* self = Thread::Current();
653  JNIEnv* env = self->GetJniEnv();
654
655  // Must be in the kNative state for calling native methods (JNI_OnLoad code).
656  CHECK_EQ(self->GetState(), kNative);
657
658  // First set up JniConstants, which is used by both the runtime's built-in native
659  // methods and libcore.
660  JniConstants::init(env);
661  WellKnownClasses::Init(env);
662
663  // Then set up the native methods provided by the runtime itself.
664  RegisterRuntimeNativeMethods(env);
665
666  // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
667  // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
668  // the library that implements System.loadLibrary!
669  {
670    std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
671    std::string reason;
672    self->TransitionFromSuspendedToRunnable();
673    SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
674    if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, class_loader, &reason)) {
675      LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
676    }
677    self->TransitionFromRunnableToSuspended(kNative);
678  }
679
680  // Initialize well known classes that may invoke runtime native methods.
681  WellKnownClasses::LateInit(env);
682
683  VLOG(startup) << "Runtime::InitNativeMethods exiting";
684}
685
686void Runtime::InitThreadGroups(Thread* self) {
687  JNIEnvExt* env = self->GetJniEnv();
688  ScopedJniEnvLocalRefState env_state(env);
689  main_thread_group_ =
690      env->NewGlobalRef(env->GetStaticObjectField(
691          WellKnownClasses::java_lang_ThreadGroup,
692          WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
693  CHECK(main_thread_group_ != NULL || IsCompiler());
694  system_thread_group_ =
695      env->NewGlobalRef(env->GetStaticObjectField(
696          WellKnownClasses::java_lang_ThreadGroup,
697          WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
698  CHECK(system_thread_group_ != NULL || IsCompiler());
699}
700
701jobject Runtime::GetMainThreadGroup() const {
702  CHECK(main_thread_group_ != NULL || IsCompiler());
703  return main_thread_group_;
704}
705
706jobject Runtime::GetSystemThreadGroup() const {
707  CHECK(system_thread_group_ != NULL || IsCompiler());
708  return system_thread_group_;
709}
710
711jobject Runtime::GetSystemClassLoader() const {
712  CHECK(system_class_loader_ != NULL || IsCompiler());
713  return system_class_loader_;
714}
715
716void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
717#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
718  // Register Throwable first so that registration of other native methods can throw exceptions
719  REGISTER(register_java_lang_Throwable);
720  REGISTER(register_dalvik_system_DexFile);
721  REGISTER(register_dalvik_system_VMDebug);
722  REGISTER(register_dalvik_system_VMRuntime);
723  REGISTER(register_dalvik_system_VMStack);
724  REGISTER(register_dalvik_system_ZygoteHooks);
725  REGISTER(register_java_lang_Class);
726  REGISTER(register_java_lang_DexCache);
727  REGISTER(register_java_lang_Object);
728  REGISTER(register_java_lang_Runtime);
729  REGISTER(register_java_lang_String);
730  REGISTER(register_java_lang_System);
731  REGISTER(register_java_lang_Thread);
732  REGISTER(register_java_lang_VMClassLoader);
733  REGISTER(register_java_lang_reflect_Array);
734  REGISTER(register_java_lang_reflect_Constructor);
735  REGISTER(register_java_lang_reflect_Field);
736  REGISTER(register_java_lang_reflect_Method);
737  REGISTER(register_java_lang_reflect_Proxy);
738  REGISTER(register_java_util_concurrent_atomic_AtomicLong);
739  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
740  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
741  REGISTER(register_sun_misc_Unsafe);
742#undef REGISTER
743}
744
745void Runtime::DumpForSigQuit(std::ostream& os) {
746  GetClassLinker()->DumpForSigQuit(os);
747  GetInternTable()->DumpForSigQuit(os);
748  GetJavaVM()->DumpForSigQuit(os);
749  GetHeap()->DumpForSigQuit(os);
750  os << "\n";
751
752  thread_list_->DumpForSigQuit(os);
753  BaseMutex::DumpAll(os);
754}
755
756void Runtime::DumpLockHolders(std::ostream& os) {
757  uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
758  pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
759  pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
760  pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
761  if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
762    os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
763       << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
764       << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
765       << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
766  }
767}
768
769void Runtime::SetStatsEnabled(bool new_state) {
770  if (new_state == true) {
771    GetStats()->Clear(~0);
772    // TODO: wouldn't it make more sense to clear _all_ threads' stats?
773    Thread::Current()->GetStats()->Clear(~0);
774    GetInstrumentation()->InstrumentQuickAllocEntryPoints();
775  } else {
776    GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
777  }
778  stats_enabled_ = new_state;
779}
780
781void Runtime::ResetStats(int kinds) {
782  GetStats()->Clear(kinds & 0xffff);
783  // TODO: wouldn't it make more sense to clear _all_ threads' stats?
784  Thread::Current()->GetStats()->Clear(kinds >> 16);
785}
786
787int32_t Runtime::GetStat(int kind) {
788  RuntimeStats* stats;
789  if (kind < (1<<16)) {
790    stats = GetStats();
791  } else {
792    stats = Thread::Current()->GetStats();
793    kind >>= 16;
794  }
795  switch (kind) {
796  case KIND_ALLOCATED_OBJECTS:
797    return stats->allocated_objects;
798  case KIND_ALLOCATED_BYTES:
799    return stats->allocated_bytes;
800  case KIND_FREED_OBJECTS:
801    return stats->freed_objects;
802  case KIND_FREED_BYTES:
803    return stats->freed_bytes;
804  case KIND_GC_INVOCATIONS:
805    return stats->gc_for_alloc_count;
806  case KIND_CLASS_INIT_COUNT:
807    return stats->class_init_count;
808  case KIND_CLASS_INIT_TIME:
809    // Convert ns to us, reduce to 32 bits.
810    return static_cast<int>(stats->class_init_time_ns / 1000);
811  case KIND_EXT_ALLOCATED_OBJECTS:
812  case KIND_EXT_ALLOCATED_BYTES:
813  case KIND_EXT_FREED_OBJECTS:
814  case KIND_EXT_FREED_BYTES:
815    return 0;  // backward compatibility
816  default:
817    LOG(FATAL) << "Unknown statistic " << kind;
818    return -1;  // unreachable
819  }
820}
821
822void Runtime::BlockSignals() {
823  SignalSet signals;
824  signals.Add(SIGPIPE);
825  // SIGQUIT is used to dump the runtime's state (including stack traces).
826  signals.Add(SIGQUIT);
827  // SIGUSR1 is used to initiate a GC.
828  signals.Add(SIGUSR1);
829  signals.Block();
830}
831
832bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
833                                  bool create_peer) {
834  bool success = Thread::Attach(thread_name, as_daemon, thread_group, create_peer) != NULL;
835  if (thread_name == NULL) {
836    LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
837  }
838  return success;
839}
840
841void Runtime::DetachCurrentThread() {
842  Thread* self = Thread::Current();
843  if (self == NULL) {
844    LOG(FATAL) << "attempting to detach thread that is not attached";
845  }
846  if (self->HasManagedStack()) {
847    LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
848  }
849  thread_list_->Unregister(self);
850}
851
852  mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() const {
853  if (pre_allocated_OutOfMemoryError_ == NULL) {
854    LOG(ERROR) << "Failed to return pre-allocated OOME";
855  }
856  return pre_allocated_OutOfMemoryError_;
857}
858
859void Runtime::VisitConstantRoots(RootCallback* callback, void* arg) {
860  // Visit the classes held as static in mirror classes, these can be visited concurrently and only
861  // need to be visited once per GC since they never change.
862  mirror::ArtField::VisitRoots(callback, arg);
863  mirror::ArtMethod::VisitRoots(callback, arg);
864  mirror::Class::VisitRoots(callback, arg);
865  mirror::StackTraceElement::VisitRoots(callback, arg);
866  mirror::String::VisitRoots(callback, arg);
867  mirror::Throwable::VisitRoots(callback, arg);
868  // Visit all the primitive array types classes.
869  mirror::PrimitiveArray<uint8_t>::VisitRoots(callback, arg);   // BooleanArray
870  mirror::PrimitiveArray<int8_t>::VisitRoots(callback, arg);    // ByteArray
871  mirror::PrimitiveArray<uint16_t>::VisitRoots(callback, arg);  // CharArray
872  mirror::PrimitiveArray<double>::VisitRoots(callback, arg);    // DoubleArray
873  mirror::PrimitiveArray<float>::VisitRoots(callback, arg);     // FloatArray
874  mirror::PrimitiveArray<int32_t>::VisitRoots(callback, arg);   // IntArray
875  mirror::PrimitiveArray<int64_t>::VisitRoots(callback, arg);   // LongArray
876  mirror::PrimitiveArray<int16_t>::VisitRoots(callback, arg);   // ShortArray
877}
878
879void Runtime::VisitConcurrentRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
880  intern_table_->VisitRoots(callback, arg, flags);
881  class_linker_->VisitRoots(callback, arg, flags);
882  Dbg::VisitRoots(callback, arg);
883  if ((flags & kVisitRootFlagNewRoots) == 0) {
884    // Guaranteed to have no new roots in the constant roots.
885    VisitConstantRoots(callback, arg);
886  }
887}
888
889void Runtime::VisitNonThreadRoots(RootCallback* callback, void* arg) {
890  java_vm_->VisitRoots(callback, arg);
891  if (pre_allocated_OutOfMemoryError_ != nullptr) {
892    callback(reinterpret_cast<mirror::Object**>(&pre_allocated_OutOfMemoryError_), arg, 0,
893             kRootVMInternal);
894    DCHECK(pre_allocated_OutOfMemoryError_ != nullptr);
895  }
896  callback(reinterpret_cast<mirror::Object**>(&resolution_method_), arg, 0, kRootVMInternal);
897  DCHECK(resolution_method_ != nullptr);
898  if (HasImtConflictMethod()) {
899    callback(reinterpret_cast<mirror::Object**>(&imt_conflict_method_), arg, 0, kRootVMInternal);
900  }
901  if (HasDefaultImt()) {
902    callback(reinterpret_cast<mirror::Object**>(&default_imt_), arg, 0, kRootVMInternal);
903  }
904  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
905    if (callee_save_methods_[i] != nullptr) {
906      callback(reinterpret_cast<mirror::Object**>(&callee_save_methods_[i]), arg, 0,
907               kRootVMInternal);
908    }
909  }
910  {
911    MutexLock mu(Thread::Current(), method_verifier_lock_);
912    for (verifier::MethodVerifier* verifier : method_verifiers_) {
913      verifier->VisitRoots(callback, arg);
914    }
915  }
916  if (preinitialization_transaction_ != nullptr) {
917    preinitialization_transaction_->VisitRoots(callback, arg);
918  }
919  instrumentation_.VisitRoots(callback, arg);
920}
921
922void Runtime::VisitNonConcurrentRoots(RootCallback* callback, void* arg) {
923  thread_list_->VisitRoots(callback, arg);
924  VisitNonThreadRoots(callback, arg);
925}
926
927void Runtime::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
928  VisitConcurrentRoots(callback, arg, flags);
929  VisitNonConcurrentRoots(callback, arg);
930}
931
932mirror::ObjectArray<mirror::ArtMethod>* Runtime::CreateDefaultImt(ClassLinker* cl) {
933  Thread* self = Thread::Current();
934  SirtRef<mirror::ObjectArray<mirror::ArtMethod> > imtable(self, cl->AllocArtMethodArray(self, 64));
935  mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod();
936  for (size_t i = 0; i < static_cast<size_t>(imtable->GetLength()); i++) {
937    imtable->Set<false>(i, imt_conflict_method);
938  }
939  return imtable.get();
940}
941
942mirror::ArtMethod* Runtime::CreateImtConflictMethod() {
943  Thread* self = Thread::Current();
944  Runtime* runtime = Runtime::Current();
945  ClassLinker* class_linker = runtime->GetClassLinker();
946  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
947  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
948  // TODO: use a special method for imt conflict method saves.
949  method->SetDexMethodIndex(DexFile::kDexNoIndex);
950  // When compiling, the code pointer will get set later when the image is loaded.
951  if (runtime->IsCompiler()) {
952    method->SetEntryPointFromPortableCompiledCode(nullptr);
953    method->SetEntryPointFromQuickCompiledCode(nullptr);
954  } else {
955    method->SetEntryPointFromPortableCompiledCode(GetPortableImtConflictTrampoline(class_linker));
956    method->SetEntryPointFromQuickCompiledCode(GetQuickImtConflictTrampoline(class_linker));
957  }
958  return method.get();
959}
960
961mirror::ArtMethod* Runtime::CreateResolutionMethod() {
962  Thread* self = Thread::Current();
963  Runtime* runtime = Runtime::Current();
964  ClassLinker* class_linker = runtime->GetClassLinker();
965  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
966  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
967  // TODO: use a special method for resolution method saves
968  method->SetDexMethodIndex(DexFile::kDexNoIndex);
969  // When compiling, the code pointer will get set later when the image is loaded.
970  if (runtime->IsCompiler()) {
971    method->SetEntryPointFromPortableCompiledCode(nullptr);
972    method->SetEntryPointFromQuickCompiledCode(nullptr);
973  } else {
974    method->SetEntryPointFromPortableCompiledCode(GetPortableResolutionTrampoline(class_linker));
975    method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionTrampoline(class_linker));
976  }
977  return method.get();
978}
979
980mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set,
981                                                   CalleeSaveType type) {
982  Thread* self = Thread::Current();
983  Runtime* runtime = Runtime::Current();
984  ClassLinker* class_linker = runtime->GetClassLinker();
985  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
986  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
987  // TODO: use a special method for callee saves
988  method->SetDexMethodIndex(DexFile::kDexNoIndex);
989  method->SetEntryPointFromPortableCompiledCode(nullptr);
990  method->SetEntryPointFromQuickCompiledCode(nullptr);
991  if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
992    uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6)  | (1 << art::arm::R7) |
993                          (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
994    uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
995    uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
996    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
997                           (type == kSaveAll ? all_spills : 0) | (1 << art::arm::LR);
998    uint32_t fp_all_spills = (1 << art::arm::S0)  | (1 << art::arm::S1)  | (1 << art::arm::S2) |
999                             (1 << art::arm::S3)  | (1 << art::arm::S4)  | (1 << art::arm::S5) |
1000                             (1 << art::arm::S6)  | (1 << art::arm::S7)  | (1 << art::arm::S8) |
1001                             (1 << art::arm::S9)  | (1 << art::arm::S10) | (1 << art::arm::S11) |
1002                             (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
1003                             (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
1004                             (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
1005                             (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
1006                             (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
1007                             (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
1008                             (1 << art::arm::S30) | (1 << art::arm::S31);
1009    uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1010    size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1011                                 __builtin_popcount(fp_spills) /* fprs */ +
1012                                 1 /* Method* */) * kArmPointerSize, kStackAlignment);
1013    method->SetFrameSizeInBytes(frame_size);
1014    method->SetCoreSpillMask(core_spills);
1015    method->SetFpSpillMask(fp_spills);
1016  } else if (instruction_set == kMips) {
1017    uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1018                          (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1019                          (1 << art::mips::GP) | (1 << art::mips::FP);
1020    uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
1021    uint32_t all_spills = (1 << art::mips::S0) | (1 << art::mips::S1);
1022    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1023                           (type == kSaveAll ? all_spills : 0) | (1 << art::mips::RA);
1024    size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1025                                (type == kRefsAndArgs ? 0 : 3) + 1 /* Method* */) *
1026                                kMipsPointerSize, kStackAlignment);
1027    method->SetFrameSizeInBytes(frame_size);
1028    method->SetCoreSpillMask(core_spills);
1029    method->SetFpSpillMask(0);
1030  } else if (instruction_set == kX86) {
1031    uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1032    uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1033    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1034                         (1 << art::x86::kNumberOfCpuRegisters);  // fake return address callee save
1035    size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1036                                 1 /* Method* */) * kX86PointerSize, kStackAlignment);
1037    method->SetFrameSizeInBytes(frame_size);
1038    method->SetCoreSpillMask(core_spills);
1039    method->SetFpSpillMask(0);
1040  } else if (instruction_set == kX86_64) {
1041    uint32_t ref_spills =
1042        (1 << art::x86_64::RBX) | (1 << art::x86_64::RBP) | (1 << art::x86_64::R12) |
1043        (1 << art::x86_64::R13) | (1 << art::x86_64::R14) | (1 << art::x86_64::R15);
1044    uint32_t arg_spills =
1045        (1 << art::x86_64::RSI) | (1 << art::x86_64::RDX) | (1 << art::x86_64::RCX) |
1046        (1 << art::x86_64::R8) | (1 << art::x86_64::R9);
1047    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1048        (1 << art::x86_64::kNumberOfCpuRegisters);  // fake return address callee save
1049    uint32_t fp_arg_spills =
1050        (1 << art::x86_64::XMM0) | (1 << art::x86_64::XMM1) | (1 << art::x86_64::XMM2) |
1051        (1 << art::x86_64::XMM3) | (1 << art::x86_64::XMM4) | (1 << art::x86_64::XMM5) |
1052        (1 << art::x86_64::XMM6) | (1 << art::x86_64::XMM7);
1053    uint32_t fp_spills = (type == kRefsAndArgs ? fp_arg_spills : 0);
1054    size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1055                                 __builtin_popcount(fp_spills) /* fprs */ +
1056                                 1 /* Method* */) * kX86_64PointerSize, kStackAlignment);
1057    method->SetFrameSizeInBytes(frame_size);
1058    method->SetCoreSpillMask(core_spills);
1059    method->SetFpSpillMask(fp_spills);
1060  } else if (instruction_set == kArm64) {
1061      // Callee saved registers
1062      uint32_t ref_spills = (1 << art::arm64::X19) | (1 << art::arm64::X20) | (1 << art::arm64::X21) |
1063                            (1 << art::arm64::X22) | (1 << art::arm64::X23) | (1 << art::arm64::X24) |
1064                            (1 << art::arm64::X25) | (1 << art::arm64::X26) | (1 << art::arm64::X27) |
1065                            (1 << art::arm64::X28);
1066      // X0 is the method pointer. Not saved.
1067      uint32_t arg_spills = (1 << art::arm64::X1) | (1 << art::arm64::X2) | (1 << art::arm64::X3) |
1068                            (1 << art::arm64::X4) | (1 << art::arm64::X5) | (1 << art::arm64::X6) |
1069                            (1 << art::arm64::X7);
1070      // TODO  This is conservative. Only ALL should include the thread register.
1071      // The thread register is not preserved by the aapcs64.
1072      // LR is always saved.
1073      uint32_t all_spills =  0;  // (1 << art::arm64::LR);
1074      uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1075                             (type == kSaveAll ? all_spills : 0) | (1 << art::arm64::FP)
1076                             | (1 << art::arm64::X18) | (1 << art::arm64::LR);
1077
1078      // Save callee-saved floating point registers. Rest are scratch/parameters.
1079      uint32_t fp_arg_spills = (1 << art::arm64::D0) | (1 << art::arm64::D1) | (1 << art::arm64::D2) |
1080                            (1 << art::arm64::D3) | (1 << art::arm64::D4) | (1 << art::arm64::D5) |
1081                            (1 << art::arm64::D6) | (1 << art::arm64::D7);
1082      uint32_t fp_ref_spills = (1 << art::arm64::D8)  | (1 << art::arm64::D9)  | (1 << art::arm64::D10) |
1083                               (1 << art::arm64::D11)  | (1 << art::arm64::D12)  | (1 << art::arm64::D13) |
1084                               (1 << art::arm64::D14)  | (1 << art::arm64::D15);
1085      uint32_t fp_all_spills = fp_arg_spills |
1086                          (1 << art::arm64::D16)  | (1 << art::arm64::D17) | (1 << art::arm64::D18) |
1087                          (1 << art::arm64::D19)  | (1 << art::arm64::D20) | (1 << art::arm64::D21) |
1088                          (1 << art::arm64::D22)  | (1 << art::arm64::D23) | (1 << art::arm64::D24) |
1089                          (1 << art::arm64::D25)  | (1 << art::arm64::D26) | (1 << art::arm64::D27) |
1090                          (1 << art::arm64::D28)  | (1 << art::arm64::D29) | (1 << art::arm64::D30) |
1091                          (1 << art::arm64::D31);
1092      uint32_t fp_spills = fp_ref_spills | (type == kRefsAndArgs ? fp_arg_spills: 0)
1093                          | (type == kSaveAll ? fp_all_spills : 0);
1094      size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1095                                   __builtin_popcount(fp_spills) /* fprs */ +
1096                                   1 /* Method* */) * kArm64PointerSize, kStackAlignment);
1097      method->SetFrameSizeInBytes(frame_size);
1098      method->SetCoreSpillMask(core_spills);
1099      method->SetFpSpillMask(fp_spills);
1100  } else {
1101    UNIMPLEMENTED(FATAL) << instruction_set;
1102  }
1103  return method.get();
1104}
1105
1106void Runtime::DisallowNewSystemWeaks() {
1107  monitor_list_->DisallowNewMonitors();
1108  intern_table_->DisallowNewInterns();
1109  java_vm_->DisallowNewWeakGlobals();
1110  Dbg::DisallowNewObjectRegistryObjects();
1111}
1112
1113void Runtime::AllowNewSystemWeaks() {
1114  monitor_list_->AllowNewMonitors();
1115  intern_table_->AllowNewInterns();
1116  java_vm_->AllowNewWeakGlobals();
1117  Dbg::AllowNewObjectRegistryObjects();
1118}
1119
1120void Runtime::SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type) {
1121  DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1122  callee_save_methods_[type] = method;
1123}
1124
1125const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
1126  if (class_loader == NULL) {
1127    return GetClassLinker()->GetBootClassPath();
1128  }
1129  CHECK(UseCompileTimeClassPath());
1130  CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1131  CHECK(it != compile_time_class_paths_.end());
1132  return it->second;
1133}
1134
1135void Runtime::SetCompileTimeClassPath(jobject class_loader,
1136                                      std::vector<const DexFile*>& class_path) {
1137  CHECK(!IsStarted());
1138  use_compile_time_class_path_ = true;
1139  compile_time_class_paths_.Put(class_loader, class_path);
1140}
1141
1142void Runtime::AddMethodVerifier(verifier::MethodVerifier* verifier) {
1143  DCHECK(verifier != nullptr);
1144  MutexLock mu(Thread::Current(), method_verifier_lock_);
1145  method_verifiers_.insert(verifier);
1146}
1147
1148void Runtime::RemoveMethodVerifier(verifier::MethodVerifier* verifier) {
1149  DCHECK(verifier != nullptr);
1150  MutexLock mu(Thread::Current(), method_verifier_lock_);
1151  auto it = method_verifiers_.find(verifier);
1152  CHECK(it != method_verifiers_.end());
1153  method_verifiers_.erase(it);
1154}
1155
1156void Runtime::StartProfiler(const char* appDir, const char* procName) {
1157  BackgroundMethodSamplingProfiler::Start(profile_period_s_, profile_duration_s_, appDir,
1158      procName, profile_interval_us_, profile_backoff_coefficient_, profile_start_immediately_);
1159}
1160
1161// Transaction support.
1162void Runtime::EnterTransactionMode(Transaction* transaction) {
1163  DCHECK(IsCompiler());
1164  DCHECK(transaction != nullptr);
1165  DCHECK(!IsActiveTransaction());
1166  preinitialization_transaction_ = transaction;
1167}
1168
1169void Runtime::ExitTransactionMode() {
1170  DCHECK(IsCompiler());
1171  DCHECK(IsActiveTransaction());
1172  preinitialization_transaction_ = nullptr;
1173}
1174
1175void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset,
1176                                 uint32_t value, bool is_volatile) const {
1177  DCHECK(IsCompiler());
1178  DCHECK(IsActiveTransaction());
1179  preinitialization_transaction_->RecordWriteField32(obj, field_offset, value, is_volatile);
1180}
1181
1182void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset,
1183                                 uint64_t value, bool is_volatile) const {
1184  DCHECK(IsCompiler());
1185  DCHECK(IsActiveTransaction());
1186  preinitialization_transaction_->RecordWriteField64(obj, field_offset, value, is_volatile);
1187}
1188
1189void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset,
1190                                        mirror::Object* value, bool is_volatile) const {
1191  DCHECK(IsCompiler());
1192  DCHECK(IsActiveTransaction());
1193  preinitialization_transaction_->RecordWriteFieldReference(obj, field_offset, value, is_volatile);
1194}
1195
1196void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const {
1197  DCHECK(IsCompiler());
1198  DCHECK(IsActiveTransaction());
1199  preinitialization_transaction_->RecordWriteArray(array, index, value);
1200}
1201
1202void Runtime::RecordStrongStringInsertion(mirror::String* s, uint32_t hash_code) const {
1203  DCHECK(IsCompiler());
1204  DCHECK(IsActiveTransaction());
1205  preinitialization_transaction_->RecordStrongStringInsertion(s, hash_code);
1206}
1207
1208void Runtime::RecordWeakStringInsertion(mirror::String* s, uint32_t hash_code) const {
1209  DCHECK(IsCompiler());
1210  DCHECK(IsActiveTransaction());
1211  preinitialization_transaction_->RecordWeakStringInsertion(s, hash_code);
1212}
1213
1214void Runtime::RecordStrongStringRemoval(mirror::String* s, uint32_t hash_code) const {
1215  DCHECK(IsCompiler());
1216  DCHECK(IsActiveTransaction());
1217  preinitialization_transaction_->RecordStrongStringRemoval(s, hash_code);
1218}
1219
1220void Runtime::RecordWeakStringRemoval(mirror::String* s, uint32_t hash_code) const {
1221  DCHECK(IsCompiler());
1222  DCHECK(IsActiveTransaction());
1223  preinitialization_transaction_->RecordWeakStringRemoval(s, hash_code);
1224}
1225
1226void Runtime::SetFaultMessage(const std::string& message) {
1227  MutexLock mu(Thread::Current(), fault_message_lock_);
1228  fault_message_ = message;
1229}
1230
1231void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv)
1232    const {
1233  argv->push_back("--runtime-arg");
1234  std::string checkstr = "-implicit-checks";
1235
1236  int nchecks = 0;
1237  char checksep = ':';
1238
1239  if (!ExplicitNullChecks()) {
1240    checkstr += checksep;
1241    checksep = ',';
1242    checkstr += "null";
1243    ++nchecks;
1244  }
1245  if (!ExplicitSuspendChecks()) {
1246    checkstr += checksep;
1247    checksep = ',';
1248    checkstr += "suspend";
1249    ++nchecks;
1250  }
1251
1252  if (!ExplicitStackOverflowChecks()) {
1253    checkstr += checksep;
1254    checksep = ',';
1255    checkstr += "stack";
1256    ++nchecks;
1257  }
1258
1259  if (nchecks == 0) {
1260    checkstr += ":none";
1261  }
1262  argv->push_back(checkstr);
1263
1264  // Make the dex2oat instruction set match that of the launching runtime. If we have multiple
1265  // architecture support, dex2oat may be compiled as a different instruction-set than that
1266  // currently being executed.
1267#if defined(__arm__)
1268  argv->push_back("--instruction-set=arm");
1269#elif defined(__aarch64__)
1270  argv->push_back("--instruction-set=arm64");
1271#elif defined(__i386__)
1272  argv->push_back("--instruction-set=x86");
1273#elif defined(__x86_64__)
1274  argv->push_back("--instruction-set=x86_64");
1275#elif defined(__mips__)
1276  argv->push_back("--instruction-set=mips");
1277#endif
1278
1279  std::string features("--instruction-set-features=");
1280  features += GetDefaultInstructionSetFeatures();
1281  argv->push_back(features);
1282}
1283
1284void Runtime::UpdateProfilerState(int state) {
1285  LOG(DEBUG) << "Profiler state updated to " << state;
1286}
1287}  // namespace art
1288