runtime.cc revision 052664322767de1799f060c306e729b0e73ce893
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(gc::kGcCauseBackground, 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  bool implicit_checks_supported = false;
539  switch (kRuntimeISA) {
540  case kArm:
541  case kThumb2:
542    implicit_checks_supported = true;
543    break;
544  default:
545    break;
546  }
547
548  if (implicit_checks_supported &&
549    (options->explicit_checks_ != (ParsedOptions::kExplicitSuspendCheck |
550        ParsedOptions::kExplicitNullCheck |
551        ParsedOptions::kExplicitStackOverflowCheck) || kEnableJavaStackTraceHandler)) {
552    fault_manager.Init();
553
554    // These need to be in a specific order.  The null point check handler must be
555    // after the suspend check and stack overflow check handlers.
556    if ((options->explicit_checks_ & ParsedOptions::kExplicitSuspendCheck) == 0) {
557      suspend_handler_ = new SuspensionHandler(&fault_manager);
558    }
559
560    if ((options->explicit_checks_ & ParsedOptions::kExplicitStackOverflowCheck) == 0) {
561      stack_overflow_handler_ = new StackOverflowHandler(&fault_manager);
562    }
563
564    if ((options->explicit_checks_ & ParsedOptions::kExplicitNullCheck) == 0) {
565      null_pointer_handler_ = new NullPointerHandler(&fault_manager);
566    }
567
568    if (kEnableJavaStackTraceHandler) {
569      new JavaStackTraceHandler(&fault_manager);
570    }
571  }
572
573  heap_ = new gc::Heap(options->heap_initial_size_,
574                       options->heap_growth_limit_,
575                       options->heap_min_free_,
576                       options->heap_max_free_,
577                       options->heap_target_utilization_,
578                       options->foreground_heap_growth_multiplier_,
579                       options->heap_maximum_size_,
580                       options->image_,
581                       options->image_isa_,
582                       options->collector_type_,
583                       options->background_collector_type_,
584                       options->parallel_gc_threads_,
585                       options->conc_gc_threads_,
586                       options->low_memory_mode_,
587                       options->long_pause_log_threshold_,
588                       options->long_gc_log_threshold_,
589                       options->ignore_max_footprint_,
590                       options->use_tlab_,
591                       options->verify_pre_gc_heap_,
592                       options->verify_pre_sweeping_heap_,
593                       options->verify_post_gc_heap_,
594                       options->verify_pre_gc_rosalloc_,
595                       options->verify_pre_sweeping_rosalloc_,
596                       options->verify_post_gc_rosalloc_);
597
598  dump_gc_performance_on_shutdown_ = options->dump_gc_performance_on_shutdown_;
599
600  BlockSignals();
601  InitPlatformSignalHandlers();
602
603  java_vm_ = new JavaVMExt(this, options.get());
604
605  Thread::Startup();
606
607  // ClassLinker needs an attached thread, but we can't fully attach a thread without creating
608  // objects. We can't supply a thread group yet; it will be fixed later. Since we are the main
609  // thread, we do not get a java peer.
610  Thread* self = Thread::Attach("main", false, NULL, false);
611  CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId);
612  CHECK(self != NULL);
613
614  // Set us to runnable so tools using a runtime can allocate and GC by default
615  self->TransitionFromSuspendedToRunnable();
616
617  // Now we're attached, we can take the heap locks and validate the heap.
618  GetHeap()->EnableObjectValidation();
619
620  CHECK_GE(GetHeap()->GetContinuousSpaces().size(), 1U);
621  class_linker_ = new ClassLinker(intern_table_);
622  if (GetHeap()->HasImageSpace()) {
623    class_linker_->InitFromImage();
624  } else {
625    CHECK(options->boot_class_path_ != NULL);
626    CHECK_NE(options->boot_class_path_->size(), 0U);
627    class_linker_->InitFromCompiler(*options->boot_class_path_);
628  }
629  CHECK(class_linker_ != NULL);
630  verifier::MethodVerifier::Init();
631
632  method_trace_ = options->method_trace_;
633  method_trace_file_ = options->method_trace_file_;
634  method_trace_file_size_ = options->method_trace_file_size_;
635
636  // Extract the profile options.
637  // TODO: move into a Trace options struct?
638  profile_period_s_ = options->profile_period_s_;
639  profile_duration_s_ = options->profile_duration_s_;
640  profile_interval_us_ = options->profile_interval_us_;
641  profile_backoff_coefficient_ = options->profile_backoff_coefficient_;
642  profile_start_immediately_ = options->profile_start_immediately_;
643  profile_ = options->profile_;
644  profile_output_filename_ = options->profile_output_filename_;
645  // TODO: move this to just be an Trace::Start argument
646  Trace::SetDefaultClockSource(options->profile_clock_source_);
647
648  if (options->method_trace_) {
649    Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0,
650                 false, false, 0);
651  }
652
653  // Pre-allocate an OutOfMemoryError for the double-OOME case.
654  self->ThrowNewException(ThrowLocation(), "Ljava/lang/OutOfMemoryError;",
655                          "OutOfMemoryError thrown while trying to throw OutOfMemoryError; "
656                          "no stack available");
657  pre_allocated_OutOfMemoryError_ = self->GetException(NULL);
658  self->ClearException();
659
660  VLOG(startup) << "Runtime::Init exiting";
661  return true;
662}
663
664void Runtime::InitNativeMethods() {
665  VLOG(startup) << "Runtime::InitNativeMethods entering";
666  Thread* self = Thread::Current();
667  JNIEnv* env = self->GetJniEnv();
668
669  // Must be in the kNative state for calling native methods (JNI_OnLoad code).
670  CHECK_EQ(self->GetState(), kNative);
671
672  // First set up JniConstants, which is used by both the runtime's built-in native
673  // methods and libcore.
674  JniConstants::init(env);
675  WellKnownClasses::Init(env);
676
677  // Then set up the native methods provided by the runtime itself.
678  RegisterRuntimeNativeMethods(env);
679
680  // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
681  // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
682  // the library that implements System.loadLibrary!
683  {
684    std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
685    std::string reason;
686    self->TransitionFromSuspendedToRunnable();
687    SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
688    if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, class_loader, &reason)) {
689      LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
690    }
691    self->TransitionFromRunnableToSuspended(kNative);
692  }
693
694  // Initialize well known classes that may invoke runtime native methods.
695  WellKnownClasses::LateInit(env);
696
697  VLOG(startup) << "Runtime::InitNativeMethods exiting";
698}
699
700void Runtime::InitThreadGroups(Thread* self) {
701  JNIEnvExt* env = self->GetJniEnv();
702  ScopedJniEnvLocalRefState env_state(env);
703  main_thread_group_ =
704      env->NewGlobalRef(env->GetStaticObjectField(
705          WellKnownClasses::java_lang_ThreadGroup,
706          WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
707  CHECK(main_thread_group_ != NULL || IsCompiler());
708  system_thread_group_ =
709      env->NewGlobalRef(env->GetStaticObjectField(
710          WellKnownClasses::java_lang_ThreadGroup,
711          WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
712  CHECK(system_thread_group_ != NULL || IsCompiler());
713}
714
715jobject Runtime::GetMainThreadGroup() const {
716  CHECK(main_thread_group_ != NULL || IsCompiler());
717  return main_thread_group_;
718}
719
720jobject Runtime::GetSystemThreadGroup() const {
721  CHECK(system_thread_group_ != NULL || IsCompiler());
722  return system_thread_group_;
723}
724
725jobject Runtime::GetSystemClassLoader() const {
726  CHECK(system_class_loader_ != NULL || IsCompiler());
727  return system_class_loader_;
728}
729
730void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
731#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
732  // Register Throwable first so that registration of other native methods can throw exceptions
733  REGISTER(register_java_lang_Throwable);
734  REGISTER(register_dalvik_system_DexFile);
735  REGISTER(register_dalvik_system_VMDebug);
736  REGISTER(register_dalvik_system_VMRuntime);
737  REGISTER(register_dalvik_system_VMStack);
738  REGISTER(register_dalvik_system_ZygoteHooks);
739  REGISTER(register_java_lang_Class);
740  REGISTER(register_java_lang_DexCache);
741  REGISTER(register_java_lang_Object);
742  REGISTER(register_java_lang_Runtime);
743  REGISTER(register_java_lang_String);
744  REGISTER(register_java_lang_System);
745  REGISTER(register_java_lang_Thread);
746  REGISTER(register_java_lang_VMClassLoader);
747  REGISTER(register_java_lang_reflect_Array);
748  REGISTER(register_java_lang_reflect_Constructor);
749  REGISTER(register_java_lang_reflect_Field);
750  REGISTER(register_java_lang_reflect_Method);
751  REGISTER(register_java_lang_reflect_Proxy);
752  REGISTER(register_java_util_concurrent_atomic_AtomicLong);
753  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
754  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
755  REGISTER(register_sun_misc_Unsafe);
756#undef REGISTER
757}
758
759void Runtime::DumpForSigQuit(std::ostream& os) {
760  GetClassLinker()->DumpForSigQuit(os);
761  GetInternTable()->DumpForSigQuit(os);
762  GetJavaVM()->DumpForSigQuit(os);
763  GetHeap()->DumpForSigQuit(os);
764  os << "\n";
765
766  thread_list_->DumpForSigQuit(os);
767  BaseMutex::DumpAll(os);
768}
769
770void Runtime::DumpLockHolders(std::ostream& os) {
771  uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
772  pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
773  pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
774  pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
775  if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
776    os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
777       << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
778       << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
779       << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
780  }
781}
782
783void Runtime::SetStatsEnabled(bool new_state) {
784  if (new_state == true) {
785    GetStats()->Clear(~0);
786    // TODO: wouldn't it make more sense to clear _all_ threads' stats?
787    Thread::Current()->GetStats()->Clear(~0);
788    GetInstrumentation()->InstrumentQuickAllocEntryPoints();
789  } else {
790    GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
791  }
792  stats_enabled_ = new_state;
793}
794
795void Runtime::ResetStats(int kinds) {
796  GetStats()->Clear(kinds & 0xffff);
797  // TODO: wouldn't it make more sense to clear _all_ threads' stats?
798  Thread::Current()->GetStats()->Clear(kinds >> 16);
799}
800
801int32_t Runtime::GetStat(int kind) {
802  RuntimeStats* stats;
803  if (kind < (1<<16)) {
804    stats = GetStats();
805  } else {
806    stats = Thread::Current()->GetStats();
807    kind >>= 16;
808  }
809  switch (kind) {
810  case KIND_ALLOCATED_OBJECTS:
811    return stats->allocated_objects;
812  case KIND_ALLOCATED_BYTES:
813    return stats->allocated_bytes;
814  case KIND_FREED_OBJECTS:
815    return stats->freed_objects;
816  case KIND_FREED_BYTES:
817    return stats->freed_bytes;
818  case KIND_GC_INVOCATIONS:
819    return stats->gc_for_alloc_count;
820  case KIND_CLASS_INIT_COUNT:
821    return stats->class_init_count;
822  case KIND_CLASS_INIT_TIME:
823    // Convert ns to us, reduce to 32 bits.
824    return static_cast<int>(stats->class_init_time_ns / 1000);
825  case KIND_EXT_ALLOCATED_OBJECTS:
826  case KIND_EXT_ALLOCATED_BYTES:
827  case KIND_EXT_FREED_OBJECTS:
828  case KIND_EXT_FREED_BYTES:
829    return 0;  // backward compatibility
830  default:
831    LOG(FATAL) << "Unknown statistic " << kind;
832    return -1;  // unreachable
833  }
834}
835
836void Runtime::BlockSignals() {
837  SignalSet signals;
838  signals.Add(SIGPIPE);
839  // SIGQUIT is used to dump the runtime's state (including stack traces).
840  signals.Add(SIGQUIT);
841  // SIGUSR1 is used to initiate a GC.
842  signals.Add(SIGUSR1);
843  signals.Block();
844}
845
846bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
847                                  bool create_peer) {
848  bool success = Thread::Attach(thread_name, as_daemon, thread_group, create_peer) != NULL;
849  if (thread_name == NULL) {
850    LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
851  }
852  return success;
853}
854
855void Runtime::DetachCurrentThread() {
856  Thread* self = Thread::Current();
857  if (self == NULL) {
858    LOG(FATAL) << "attempting to detach thread that is not attached";
859  }
860  if (self->HasManagedStack()) {
861    LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
862  }
863  thread_list_->Unregister(self);
864}
865
866  mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() const {
867  if (pre_allocated_OutOfMemoryError_ == NULL) {
868    LOG(ERROR) << "Failed to return pre-allocated OOME";
869  }
870  return pre_allocated_OutOfMemoryError_;
871}
872
873void Runtime::VisitConstantRoots(RootCallback* callback, void* arg) {
874  // Visit the classes held as static in mirror classes, these can be visited concurrently and only
875  // need to be visited once per GC since they never change.
876  mirror::ArtField::VisitRoots(callback, arg);
877  mirror::ArtMethod::VisitRoots(callback, arg);
878  mirror::Class::VisitRoots(callback, arg);
879  mirror::StackTraceElement::VisitRoots(callback, arg);
880  mirror::String::VisitRoots(callback, arg);
881  mirror::Throwable::VisitRoots(callback, arg);
882  // Visit all the primitive array types classes.
883  mirror::PrimitiveArray<uint8_t>::VisitRoots(callback, arg);   // BooleanArray
884  mirror::PrimitiveArray<int8_t>::VisitRoots(callback, arg);    // ByteArray
885  mirror::PrimitiveArray<uint16_t>::VisitRoots(callback, arg);  // CharArray
886  mirror::PrimitiveArray<double>::VisitRoots(callback, arg);    // DoubleArray
887  mirror::PrimitiveArray<float>::VisitRoots(callback, arg);     // FloatArray
888  mirror::PrimitiveArray<int32_t>::VisitRoots(callback, arg);   // IntArray
889  mirror::PrimitiveArray<int64_t>::VisitRoots(callback, arg);   // LongArray
890  mirror::PrimitiveArray<int16_t>::VisitRoots(callback, arg);   // ShortArray
891}
892
893void Runtime::VisitConcurrentRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
894  intern_table_->VisitRoots(callback, arg, flags);
895  class_linker_->VisitRoots(callback, arg, flags);
896  Dbg::VisitRoots(callback, arg);
897  if ((flags & kVisitRootFlagNewRoots) == 0) {
898    // Guaranteed to have no new roots in the constant roots.
899    VisitConstantRoots(callback, arg);
900  }
901}
902
903void Runtime::VisitNonThreadRoots(RootCallback* callback, void* arg) {
904  java_vm_->VisitRoots(callback, arg);
905  if (pre_allocated_OutOfMemoryError_ != nullptr) {
906    callback(reinterpret_cast<mirror::Object**>(&pre_allocated_OutOfMemoryError_), arg, 0,
907             kRootVMInternal);
908    DCHECK(pre_allocated_OutOfMemoryError_ != nullptr);
909  }
910  callback(reinterpret_cast<mirror::Object**>(&resolution_method_), arg, 0, kRootVMInternal);
911  DCHECK(resolution_method_ != nullptr);
912  if (HasImtConflictMethod()) {
913    callback(reinterpret_cast<mirror::Object**>(&imt_conflict_method_), arg, 0, kRootVMInternal);
914  }
915  if (HasDefaultImt()) {
916    callback(reinterpret_cast<mirror::Object**>(&default_imt_), arg, 0, kRootVMInternal);
917  }
918  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
919    if (callee_save_methods_[i] != nullptr) {
920      callback(reinterpret_cast<mirror::Object**>(&callee_save_methods_[i]), arg, 0,
921               kRootVMInternal);
922    }
923  }
924  {
925    MutexLock mu(Thread::Current(), method_verifier_lock_);
926    for (verifier::MethodVerifier* verifier : method_verifiers_) {
927      verifier->VisitRoots(callback, arg);
928    }
929  }
930  if (preinitialization_transaction_ != nullptr) {
931    preinitialization_transaction_->VisitRoots(callback, arg);
932  }
933  instrumentation_.VisitRoots(callback, arg);
934}
935
936void Runtime::VisitNonConcurrentRoots(RootCallback* callback, void* arg) {
937  thread_list_->VisitRoots(callback, arg);
938  VisitNonThreadRoots(callback, arg);
939}
940
941void Runtime::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
942  VisitConcurrentRoots(callback, arg, flags);
943  VisitNonConcurrentRoots(callback, arg);
944}
945
946mirror::ObjectArray<mirror::ArtMethod>* Runtime::CreateDefaultImt(ClassLinker* cl) {
947  Thread* self = Thread::Current();
948  SirtRef<mirror::ObjectArray<mirror::ArtMethod> > imtable(self, cl->AllocArtMethodArray(self, 64));
949  mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod();
950  for (size_t i = 0; i < static_cast<size_t>(imtable->GetLength()); i++) {
951    imtable->Set<false>(i, imt_conflict_method);
952  }
953  return imtable.get();
954}
955
956mirror::ArtMethod* Runtime::CreateImtConflictMethod() {
957  Thread* self = Thread::Current();
958  Runtime* runtime = Runtime::Current();
959  ClassLinker* class_linker = runtime->GetClassLinker();
960  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
961  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
962  // TODO: use a special method for imt conflict method saves.
963  method->SetDexMethodIndex(DexFile::kDexNoIndex);
964  // When compiling, the code pointer will get set later when the image is loaded.
965  if (runtime->IsCompiler()) {
966    method->SetEntryPointFromPortableCompiledCode(nullptr);
967    method->SetEntryPointFromQuickCompiledCode(nullptr);
968  } else {
969    method->SetEntryPointFromPortableCompiledCode(GetPortableImtConflictTrampoline(class_linker));
970    method->SetEntryPointFromQuickCompiledCode(GetQuickImtConflictTrampoline(class_linker));
971  }
972  return method.get();
973}
974
975mirror::ArtMethod* Runtime::CreateResolutionMethod() {
976  Thread* self = Thread::Current();
977  Runtime* runtime = Runtime::Current();
978  ClassLinker* class_linker = runtime->GetClassLinker();
979  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
980  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
981  // TODO: use a special method for resolution method saves
982  method->SetDexMethodIndex(DexFile::kDexNoIndex);
983  // When compiling, the code pointer will get set later when the image is loaded.
984  if (runtime->IsCompiler()) {
985    method->SetEntryPointFromPortableCompiledCode(nullptr);
986    method->SetEntryPointFromQuickCompiledCode(nullptr);
987  } else {
988    method->SetEntryPointFromPortableCompiledCode(GetPortableResolutionTrampoline(class_linker));
989    method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionTrampoline(class_linker));
990  }
991  return method.get();
992}
993
994mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set,
995                                                   CalleeSaveType type) {
996  Thread* self = Thread::Current();
997  Runtime* runtime = Runtime::Current();
998  ClassLinker* class_linker = runtime->GetClassLinker();
999  SirtRef<mirror::ArtMethod> method(self, class_linker->AllocArtMethod(self));
1000  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
1001  // TODO: use a special method for callee saves
1002  method->SetDexMethodIndex(DexFile::kDexNoIndex);
1003  method->SetEntryPointFromPortableCompiledCode(nullptr);
1004  method->SetEntryPointFromQuickCompiledCode(nullptr);
1005  if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
1006    uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6)  | (1 << art::arm::R7) |
1007                          (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
1008    uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
1009    uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
1010    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1011                           (type == kSaveAll ? all_spills : 0) | (1 << art::arm::LR);
1012    uint32_t fp_all_spills = (1 << art::arm::S0)  | (1 << art::arm::S1)  | (1 << art::arm::S2) |
1013                             (1 << art::arm::S3)  | (1 << art::arm::S4)  | (1 << art::arm::S5) |
1014                             (1 << art::arm::S6)  | (1 << art::arm::S7)  | (1 << art::arm::S8) |
1015                             (1 << art::arm::S9)  | (1 << art::arm::S10) | (1 << art::arm::S11) |
1016                             (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
1017                             (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
1018                             (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
1019                             (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
1020                             (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
1021                             (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
1022                             (1 << art::arm::S30) | (1 << art::arm::S31);
1023    uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1024    size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1025                                 POPCOUNT(fp_spills) /* fprs */ +
1026                                 1 /* Method* */) * kArmPointerSize, kStackAlignment);
1027    method->SetFrameSizeInBytes(frame_size);
1028    method->SetCoreSpillMask(core_spills);
1029    method->SetFpSpillMask(fp_spills);
1030  } else if (instruction_set == kMips) {
1031    uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1032                          (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1033                          (1 << art::mips::GP) | (1 << art::mips::FP);
1034    uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
1035    uint32_t all_spills = (1 << art::mips::S0) | (1 << art::mips::S1);
1036    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1037                           (type == kSaveAll ? all_spills : 0) | (1 << art::mips::RA);
1038    size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1039                                (type == kRefsAndArgs ? 0 : 3) + 1 /* Method* */) *
1040                                kMipsPointerSize, kStackAlignment);
1041    method->SetFrameSizeInBytes(frame_size);
1042    method->SetCoreSpillMask(core_spills);
1043    method->SetFpSpillMask(0);
1044  } else if (instruction_set == kX86) {
1045    uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1046    uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1047    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1048                         (1 << art::x86::kNumberOfCpuRegisters);  // fake return address callee save
1049    size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1050                                 1 /* Method* */) * kX86PointerSize, kStackAlignment);
1051    method->SetFrameSizeInBytes(frame_size);
1052    method->SetCoreSpillMask(core_spills);
1053    method->SetFpSpillMask(0);
1054  } else if (instruction_set == kX86_64) {
1055    uint32_t ref_spills =
1056        (1 << art::x86_64::RBX) | (1 << art::x86_64::RBP) | (1 << art::x86_64::R12) |
1057        (1 << art::x86_64::R13) | (1 << art::x86_64::R14) | (1 << art::x86_64::R15);
1058    uint32_t arg_spills =
1059        (1 << art::x86_64::RSI) | (1 << art::x86_64::RDX) | (1 << art::x86_64::RCX) |
1060        (1 << art::x86_64::R8) | (1 << art::x86_64::R9);
1061    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1062        (1 << art::x86_64::kNumberOfCpuRegisters);  // fake return address callee save
1063    uint32_t fp_arg_spills =
1064        (1 << art::x86_64::XMM0) | (1 << art::x86_64::XMM1) | (1 << art::x86_64::XMM2) |
1065        (1 << art::x86_64::XMM3) | (1 << art::x86_64::XMM4) | (1 << art::x86_64::XMM5) |
1066        (1 << art::x86_64::XMM6) | (1 << art::x86_64::XMM7);
1067    uint32_t fp_spills = (type == kRefsAndArgs ? fp_arg_spills : 0);
1068    size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1069                                 POPCOUNT(fp_spills) /* fprs */ +
1070                                 1 /* Method* */) * kX86_64PointerSize, kStackAlignment);
1071    method->SetFrameSizeInBytes(frame_size);
1072    method->SetCoreSpillMask(core_spills);
1073    method->SetFpSpillMask(fp_spills);
1074  } else if (instruction_set == kArm64) {
1075      // Callee saved registers
1076      uint32_t ref_spills = (1 << art::arm64::X19) | (1 << art::arm64::X20) | (1 << art::arm64::X21) |
1077                            (1 << art::arm64::X22) | (1 << art::arm64::X23) | (1 << art::arm64::X24) |
1078                            (1 << art::arm64::X25) | (1 << art::arm64::X26) | (1 << art::arm64::X27) |
1079                            (1 << art::arm64::X28);
1080      // X0 is the method pointer. Not saved.
1081      uint32_t arg_spills = (1 << art::arm64::X1) | (1 << art::arm64::X2) | (1 << art::arm64::X3) |
1082                            (1 << art::arm64::X4) | (1 << art::arm64::X5) | (1 << art::arm64::X6) |
1083                            (1 << art::arm64::X7);
1084      // TODO  This is conservative. Only ALL should include the thread register.
1085      // The thread register is not preserved by the aapcs64.
1086      // LR is always saved.
1087      uint32_t all_spills =  0;  // (1 << art::arm64::LR);
1088      uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1089                             (type == kSaveAll ? all_spills : 0) | (1 << art::arm64::FP)
1090                             | (1 << art::arm64::X18) | (1 << art::arm64::LR);
1091
1092      // Save callee-saved floating point registers. Rest are scratch/parameters.
1093      uint32_t fp_arg_spills = (1 << art::arm64::D0) | (1 << art::arm64::D1) | (1 << art::arm64::D2) |
1094                            (1 << art::arm64::D3) | (1 << art::arm64::D4) | (1 << art::arm64::D5) |
1095                            (1 << art::arm64::D6) | (1 << art::arm64::D7);
1096      uint32_t fp_ref_spills = (1 << art::arm64::D8)  | (1 << art::arm64::D9)  | (1 << art::arm64::D10) |
1097                               (1 << art::arm64::D11)  | (1 << art::arm64::D12)  | (1 << art::arm64::D13) |
1098                               (1 << art::arm64::D14)  | (1 << art::arm64::D15);
1099      uint32_t fp_all_spills = fp_arg_spills |
1100                          (1 << art::arm64::D16)  | (1 << art::arm64::D17) | (1 << art::arm64::D18) |
1101                          (1 << art::arm64::D19)  | (1 << art::arm64::D20) | (1 << art::arm64::D21) |
1102                          (1 << art::arm64::D22)  | (1 << art::arm64::D23) | (1 << art::arm64::D24) |
1103                          (1 << art::arm64::D25)  | (1 << art::arm64::D26) | (1 << art::arm64::D27) |
1104                          (1 << art::arm64::D28)  | (1 << art::arm64::D29) | (1 << art::arm64::D30) |
1105                          (1 << art::arm64::D31);
1106      uint32_t fp_spills = fp_ref_spills | (type == kRefsAndArgs ? fp_arg_spills: 0)
1107                          | (type == kSaveAll ? fp_all_spills : 0);
1108      size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1109                                   POPCOUNT(fp_spills) /* fprs */ +
1110                                   1 /* Method* */) * kArm64PointerSize, kStackAlignment);
1111      method->SetFrameSizeInBytes(frame_size);
1112      method->SetCoreSpillMask(core_spills);
1113      method->SetFpSpillMask(fp_spills);
1114  } else {
1115    UNIMPLEMENTED(FATAL) << instruction_set;
1116  }
1117  return method.get();
1118}
1119
1120void Runtime::DisallowNewSystemWeaks() {
1121  monitor_list_->DisallowNewMonitors();
1122  intern_table_->DisallowNewInterns();
1123  java_vm_->DisallowNewWeakGlobals();
1124  Dbg::DisallowNewObjectRegistryObjects();
1125}
1126
1127void Runtime::AllowNewSystemWeaks() {
1128  monitor_list_->AllowNewMonitors();
1129  intern_table_->AllowNewInterns();
1130  java_vm_->AllowNewWeakGlobals();
1131  Dbg::AllowNewObjectRegistryObjects();
1132}
1133
1134void Runtime::SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type) {
1135  DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1136  callee_save_methods_[type] = method;
1137}
1138
1139const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
1140  if (class_loader == NULL) {
1141    return GetClassLinker()->GetBootClassPath();
1142  }
1143  CHECK(UseCompileTimeClassPath());
1144  CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1145  CHECK(it != compile_time_class_paths_.end());
1146  return it->second;
1147}
1148
1149void Runtime::SetCompileTimeClassPath(jobject class_loader,
1150                                      std::vector<const DexFile*>& class_path) {
1151  CHECK(!IsStarted());
1152  use_compile_time_class_path_ = true;
1153  compile_time_class_paths_.Put(class_loader, class_path);
1154}
1155
1156void Runtime::AddMethodVerifier(verifier::MethodVerifier* verifier) {
1157  DCHECK(verifier != nullptr);
1158  MutexLock mu(Thread::Current(), method_verifier_lock_);
1159  method_verifiers_.insert(verifier);
1160}
1161
1162void Runtime::RemoveMethodVerifier(verifier::MethodVerifier* verifier) {
1163  DCHECK(verifier != nullptr);
1164  MutexLock mu(Thread::Current(), method_verifier_lock_);
1165  auto it = method_verifiers_.find(verifier);
1166  CHECK(it != method_verifiers_.end());
1167  method_verifiers_.erase(it);
1168}
1169
1170void Runtime::StartProfiler(const char* appDir, const char* procName) {
1171  BackgroundMethodSamplingProfiler::Start(profile_period_s_, profile_duration_s_, appDir,
1172      procName, profile_interval_us_, profile_backoff_coefficient_, profile_start_immediately_);
1173}
1174
1175// Transaction support.
1176void Runtime::EnterTransactionMode(Transaction* transaction) {
1177  DCHECK(IsCompiler());
1178  DCHECK(transaction != nullptr);
1179  DCHECK(!IsActiveTransaction());
1180  preinitialization_transaction_ = transaction;
1181}
1182
1183void Runtime::ExitTransactionMode() {
1184  DCHECK(IsCompiler());
1185  DCHECK(IsActiveTransaction());
1186  preinitialization_transaction_ = nullptr;
1187}
1188
1189void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset,
1190                                 uint32_t value, bool is_volatile) const {
1191  DCHECK(IsCompiler());
1192  DCHECK(IsActiveTransaction());
1193  preinitialization_transaction_->RecordWriteField32(obj, field_offset, value, is_volatile);
1194}
1195
1196void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset,
1197                                 uint64_t value, bool is_volatile) const {
1198  DCHECK(IsCompiler());
1199  DCHECK(IsActiveTransaction());
1200  preinitialization_transaction_->RecordWriteField64(obj, field_offset, value, is_volatile);
1201}
1202
1203void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset,
1204                                        mirror::Object* value, bool is_volatile) const {
1205  DCHECK(IsCompiler());
1206  DCHECK(IsActiveTransaction());
1207  preinitialization_transaction_->RecordWriteFieldReference(obj, field_offset, value, is_volatile);
1208}
1209
1210void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const {
1211  DCHECK(IsCompiler());
1212  DCHECK(IsActiveTransaction());
1213  preinitialization_transaction_->RecordWriteArray(array, index, value);
1214}
1215
1216void Runtime::RecordStrongStringInsertion(mirror::String* s, uint32_t hash_code) const {
1217  DCHECK(IsCompiler());
1218  DCHECK(IsActiveTransaction());
1219  preinitialization_transaction_->RecordStrongStringInsertion(s, hash_code);
1220}
1221
1222void Runtime::RecordWeakStringInsertion(mirror::String* s, uint32_t hash_code) const {
1223  DCHECK(IsCompiler());
1224  DCHECK(IsActiveTransaction());
1225  preinitialization_transaction_->RecordWeakStringInsertion(s, hash_code);
1226}
1227
1228void Runtime::RecordStrongStringRemoval(mirror::String* s, uint32_t hash_code) const {
1229  DCHECK(IsCompiler());
1230  DCHECK(IsActiveTransaction());
1231  preinitialization_transaction_->RecordStrongStringRemoval(s, hash_code);
1232}
1233
1234void Runtime::RecordWeakStringRemoval(mirror::String* s, uint32_t hash_code) const {
1235  DCHECK(IsCompiler());
1236  DCHECK(IsActiveTransaction());
1237  preinitialization_transaction_->RecordWeakStringRemoval(s, hash_code);
1238}
1239
1240void Runtime::SetFaultMessage(const std::string& message) {
1241  MutexLock mu(Thread::Current(), fault_message_lock_);
1242  fault_message_ = message;
1243}
1244
1245void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv)
1246    const {
1247  if (GetInstrumentation()->InterpretOnly()) {
1248    argv->push_back("--compiler-filter=interpret-only");
1249  }
1250
1251  argv->push_back("--runtime-arg");
1252  std::string checkstr = "-implicit-checks";
1253
1254  int nchecks = 0;
1255  char checksep = ':';
1256
1257  if (!ExplicitNullChecks()) {
1258    checkstr += checksep;
1259    checksep = ',';
1260    checkstr += "null";
1261    ++nchecks;
1262  }
1263  if (!ExplicitSuspendChecks()) {
1264    checkstr += checksep;
1265    checksep = ',';
1266    checkstr += "suspend";
1267    ++nchecks;
1268  }
1269
1270  if (!ExplicitStackOverflowChecks()) {
1271    checkstr += checksep;
1272    checksep = ',';
1273    checkstr += "stack";
1274    ++nchecks;
1275  }
1276
1277  if (nchecks == 0) {
1278    checkstr += ":none";
1279  }
1280  argv->push_back(checkstr);
1281
1282  // Make the dex2oat instruction set match that of the launching runtime. If we have multiple
1283  // architecture support, dex2oat may be compiled as a different instruction-set than that
1284  // currently being executed.
1285#if defined(__arm__)
1286  argv->push_back("--instruction-set=arm");
1287#elif defined(__aarch64__)
1288  argv->push_back("--instruction-set=arm64");
1289#elif defined(__i386__)
1290  argv->push_back("--instruction-set=x86");
1291#elif defined(__x86_64__)
1292  argv->push_back("--instruction-set=x86_64");
1293#elif defined(__mips__)
1294  argv->push_back("--instruction-set=mips");
1295#endif
1296
1297  std::string features("--instruction-set-features=");
1298  features += GetDefaultInstructionSetFeatures();
1299  argv->push_back(features);
1300}
1301
1302void Runtime::UpdateProfilerState(int state) {
1303  LOG(DEBUG) << "Profiler state updated to " << state;
1304}
1305}  // namespace art
1306