blob: caed6643fa2d0333d8f1aee2746d7872cbbe1e0a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_RUNTIME_H_
18#define ART_RUNTIME_RUNTIME_H_
Carl Shapiro1fb86202011-06-27 17:43:13 -070019
Elliott Hughesa0e18062012-04-13 15:59:59 -070020#include <jni.h>
Elliott Hughesc5f7c912011-08-18 14:00:42 -070021#include <stdio.h>
22
Elliott Hughese27955c2011-08-26 15:21:24 -070023#include <iosfwd>
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -070024#include <set>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070025#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -070026#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070027#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028
Mathieu Chartierbad02672014-08-25 13:08:22 -070029#include "base/allocator.h"
Alex Lighta59dd802014-07-02 16:28:08 -070030#include "compiler_callbacks.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070031#include "gc_root.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080032#include "instrumentation.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070033#include "instruction_set.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "jobject_comparator.h"
Calin Juravlec8423522014-08-12 20:55:20 +010035#include "nativebridge/native_bridge.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080036#include "object_callbacks.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070037#include "offsets.h"
Calin Juravlec1b643c2014-05-30 23:44:11 +010038#include "profiler_options.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010039#include "quick/quick_method_frame_info.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070040#include "runtime_stats.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070041#include "safe_map.h"
Carl Shapirob5573532011-07-12 18:22:59 -070042
Carl Shapiro1fb86202011-06-27 17:43:13 -070043namespace art {
44
Ian Rogers1d54e732013-05-02 21:10:01 -070045namespace gc {
46 class Heap;
Ian Rogers576ca0c2014-06-06 15:58:22 -070047} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070049 class ArtMethod;
Ian Rogers1d54e732013-05-02 21:10:01 -070050 class ClassLoader;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010051 class Array;
Jeff Hao88474b42013-10-23 16:24:40 -070052 template<class T> class ObjectArray;
Ian Rogers1d54e732013-05-02 21:10:01 -070053 template<class T> class PrimitiveArray;
54 typedef PrimitiveArray<int8_t> ByteArray;
55 class String;
56 class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057} // namespace mirror
Mathieu Chartierc528dba2013-11-26 12:00:11 -080058namespace verifier {
59class MethodVerifier;
60}
Carl Shapiro61e019d2011-07-14 16:53:09 -070061class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070062class DexFile;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063class InternTable;
Ian Rogersb48b9eb2014-02-28 16:20:21 -080064class JavaVMExt;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070065class MonitorList;
Ian Rogersef7d42f2014-01-06 12:55:46 -080066class MonitorPool;
Ian Rogers576ca0c2014-06-06 15:58:22 -070067class NullPointerHandler;
Elliott Hughese27955c2011-08-26 15:21:24 -070068class SignalCatcher;
Ian Rogers576ca0c2014-06-06 15:58:22 -070069class StackOverflowHandler;
70class SuspensionHandler;
Carl Shapiro61e019d2011-07-14 16:53:09 -070071class ThreadList;
jeffhao2692b572011-12-16 15:42:28 -080072class Trace;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010073class Transaction;
Carl Shapiro61e019d2011-07-14 16:53:09 -070074
Ian Rogerse63db272014-07-15 15:36:11 -070075typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
76
Mathieu Chartier661974a2014-01-09 11:23:53 -080077// Not all combinations of flags are valid. You may not visit all roots as well as the new roots
78// (no logical reason to do this). You also may not start logging new roots and stop logging new
79// roots (also no logical reason to do this).
Mathieu Chartier893263b2014-03-04 11:07:42 -080080enum VisitRootFlags : uint8_t {
81 kVisitRootFlagAllRoots = 0x1,
82 kVisitRootFlagNewRoots = 0x2,
83 kVisitRootFlagStartLoggingNewRoots = 0x4,
84 kVisitRootFlagStopLoggingNewRoots = 0x8,
85 kVisitRootFlagClearRootLog = 0x10,
86};
87
Carl Shapiro1fb86202011-06-27 17:43:13 -070088class Runtime {
89 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -070090 // Creates and initializes a new runtime.
Ian Rogerse63db272014-07-15 15:36:11 -070091 static bool Create(const RuntimeOptions& options, bool ignore_unrecognized)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070093
Elliott Hughesd9c67be2012-02-02 19:54:06 -080094 bool IsCompiler() const {
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000095 return compiler_callbacks_ != nullptr;
96 }
97
Alex Lighta59dd802014-07-02 16:28:08 -070098 bool CanRelocate() const {
99 return !IsCompiler() || compiler_callbacks_->IsRelocationPossible();
100 }
101
102 bool ShouldRelocate() const {
103 return must_relocate_ && CanRelocate();
104 }
105
106 bool MustRelocateIfPossible() const {
107 return must_relocate_;
108 }
109
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100110 bool IsDex2OatEnabled() const {
Alex Light64ad14d2014-08-19 14:23:13 -0700111 return dex2oat_enabled_ && IsImageDex2OatEnabled();
112 }
113
114 bool IsImageDex2OatEnabled() const {
115 return image_dex2oat_enabled_;
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100116 }
117
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000118 CompilerCallbacks* GetCompilerCallbacks() {
119 return compiler_callbacks_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800120 }
121
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700122 bool IsZygote() const {
123 return is_zygote_;
124 }
125
Anwar Ghuloum87183592013-08-14 12:12:19 -0700126 bool IsExplicitGcDisabled() const {
127 return is_explicit_gc_disabled_;
128 }
129
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700130 std::string GetCompilerExecutable() const;
Alex Lighta59dd802014-07-02 16:28:08 -0700131 std::string GetPatchoatExecutable() const;
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700132
Brian Carlstrom6449c622014-02-10 23:48:36 -0800133 const std::vector<std::string>& GetCompilerOptions() const {
134 return compiler_options_;
Dragos Sbirlea7467ee02013-06-21 09:20:34 -0700135 }
136
Brian Carlstrom6449c622014-02-10 23:48:36 -0800137 const std::vector<std::string>& GetImageCompilerOptions() const {
138 return image_compiler_options_;
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700139 }
140
Calin Juravlec1b643c2014-05-30 23:44:11 +0100141 const ProfilerOptions& GetProfilerOptions() const {
142 return profiler_options_;
143 }
144
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700145 // Starts a runtime, which may cause threads to be started and code to run.
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700146 bool Start() UNLOCK_FUNCTION(Locks::mutator_lock_);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700147
Mathieu Chartier590fee92013-09-13 13:46:47 -0700148 bool IsShuttingDown(Thread* self);
149 bool IsShuttingDownLocked() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
Ian Rogers9af209c2012-06-03 20:50:30 -0700150 return shutting_down_;
151 }
152
Ian Rogers120f1c72012-09-28 17:17:10 -0700153 size_t NumberOfThreadsBeingBorn() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
154 return threads_being_born_;
155 }
156
157 void StartThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
158 threads_being_born_++;
159 }
160
161 void EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
162
Ian Rogers9af209c2012-06-03 20:50:30 -0700163 bool IsStarted() const {
164 return started_;
165 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700166
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700167 bool IsFinishedStarting() const {
168 return finished_starting_;
169 }
170
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700171 static Runtime* Current() {
172 return instance_;
173 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700174
Elliott Hughesffe67362011-07-17 12:09:27 -0700175 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
176 // callers should prefer.
177 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
178 // in a single function together. This reduces code size slightly, but means
179 // that the native stack trace we get may point at the wrong call site.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 static void Abort() LOCKS_EXCLUDED(Locks::abort_lock_);
Elliott Hughesffe67362011-07-17 12:09:27 -0700181
Ian Rogers365c1022012-06-22 15:05:28 -0700182 // Returns the "main" ThreadGroup, used when attaching user threads.
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700183 jobject GetMainThreadGroup() const;
Ian Rogers365c1022012-06-22 15:05:28 -0700184
185 // Returns the "system" ThreadGroup, used when attaching our internal threads.
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700186 jobject GetSystemThreadGroup() const;
Ian Rogers365c1022012-06-22 15:05:28 -0700187
Brian Carlstromce888532013-10-10 00:32:58 -0700188 // Returns the system ClassLoader which represents the CLASSPATH.
189 jobject GetSystemClassLoader() const;
190
Elliott Hughes462c9442012-03-23 18:47:50 -0700191 // Attaches the calling native thread to the runtime.
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800192 bool AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
193 bool create_peer);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700194
Elliott Hughesbf86d042011-08-31 17:53:14 -0700195 void CallExitHook(jint status);
196
Carl Shapiro61e019d2011-07-14 16:53:09 -0700197 // Detaches the current native thread from the runtime.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 void DetachCurrentThread() LOCKS_EXCLUDED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700199
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 void DumpForSigQuit(std::ostream& os)
Mathieu Chartierb363f662014-07-16 13:28:58 -0700201 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800202 void DumpLockHolders(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700203
Carl Shapiro61e019d2011-07-14 16:53:09 -0700204 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700205
Brian Carlstroma004aa92012-02-08 18:05:09 -0800206 const std::string& GetBootClassPathString() const {
207 return boot_class_path_string_;
208 }
209
210 const std::string& GetClassPathString() const {
211 return class_path_string_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700212 }
213
214 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700215 return class_linker_;
216 }
217
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700218 size_t GetDefaultStackSize() const {
219 return default_stack_size_;
220 }
221
Ian Rogers1d54e732013-05-02 21:10:01 -0700222 gc::Heap* GetHeap() const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800223 return heap_;
224 }
225
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700226 InternTable* GetInternTable() const {
Brian Carlstrome8104522013-10-15 21:56:36 -0700227 DCHECK(intern_table_ != NULL);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700228 return intern_table_;
229 }
230
Elliott Hughes0af55432011-08-17 18:37:28 -0700231 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700232 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700233 }
234
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700235 size_t GetMaxSpinsBeforeThinkLockInflation() const {
236 return max_spins_before_thin_lock_inflation_;
237 }
238
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700239 MonitorList* GetMonitorList() const {
240 return monitor_list_;
241 }
242
Ian Rogersef7d42f2014-01-06 12:55:46 -0800243 MonitorPool* GetMonitorPool() const {
244 return monitor_pool_;
245 }
246
Ian Rogersc0542af2014-09-03 16:16:56 -0700247 // Is the given object the special object used to mark a cleared JNI weak global?
248 bool IsClearedJniWeakGlobal(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249
250 // Get the special object used to mark a cleared JNI weak global.
251 mirror::Object* GetClearedJniWeakGlobal() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
252
Hiroshi Yamauchiab088112014-07-14 13:00:14 -0700253 mirror::Throwable* GetPreAllocatedOutOfMemoryError() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700254
Ian Rogers63557452014-06-04 16:57:15 -0700255 mirror::Throwable* GetPreAllocatedNoClassDefFoundError()
256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
257
Elliott Hughes225f5a12012-06-11 11:23:48 -0700258 const std::vector<std::string>& GetProperties() const {
259 return properties_;
260 }
261
Elliott Hughesd92bec42011-09-02 17:04:36 -0700262 ThreadList* GetThreadList() const {
263 return thread_list_;
264 }
265
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800266 static const char* GetVersion() {
Andreas Gampe2153f932014-06-26 08:09:17 -0700267 return "2.1.0";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700268 }
269
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700270 void DisallowNewSystemWeaks() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
271 void AllowNewSystemWeaks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
272
Ian Rogers1d54e732013-05-02 21:10:01 -0700273 // Visit all the roots. If only_dirty is true then non-dirty roots won't be visited. If
274 // clean_dirty is true then dirty roots will be marked as non-dirty after visiting.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800275 void VisitRoots(RootCallback* visitor, void* arg, VisitRootFlags flags = kVisitRootFlagAllRoots)
Ian Rogers1d54e732013-05-02 21:10:01 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700277
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700278 // Visit all of the roots we can do safely do concurrently.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800279 void VisitConcurrentRoots(RootCallback* visitor, void* arg,
280 VisitRootFlags flags = kVisitRootFlagAllRoots)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700282
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700283 // Visit all of the non thread roots, we can do this with mutators unpaused.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800284 void VisitNonThreadRoots(RootCallback* visitor, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700286
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700287 // Visit all other roots which must be done with mutators suspended.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800288 void VisitNonConcurrentRoots(RootCallback* visitor, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700290
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700291 // Sweep system weaks, the system weak is deleted if the visitor return nullptr. Otherwise, the
292 // system weak is updated to be the visitor's returned value.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800293 void SweepSystemWeaks(IsMarkedCallback* visitor, void* arg)
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700295
Mathieu Chartier893263b2014-03-04 11:07:42 -0800296 // Constant roots are the roots which never change after the runtime is initialized, they only
297 // need to be visited once per GC cycle.
298 void VisitConstantRoots(RootCallback* callback, void* arg)
299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
300
Ian Rogers9af209c2012-06-03 20:50:30 -0700301 // Returns a special method that calls into a trampoline for runtime method resolution
Ian Rogerse63db272014-07-15 15:36:11 -0700302 mirror::ArtMethod* GetResolutionMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers9af209c2012-06-03 20:50:30 -0700303
304 bool HasResolutionMethod() const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700305 return !resolution_method_.IsNull();
Ian Rogers9af209c2012-06-03 20:50:30 -0700306 }
307
Brian Carlstromea46f952013-07-30 01:26:50 -0700308 void SetResolutionMethod(mirror::ArtMethod* method) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700309 resolution_method_ = GcRoot<mirror::ArtMethod>(method);
Ian Rogers9af209c2012-06-03 20:50:30 -0700310 }
311
Brian Carlstromea46f952013-07-30 01:26:50 -0700312 mirror::ArtMethod* CreateResolutionMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800313
Ian Rogerse63db272014-07-15 15:36:11 -0700314 // Returns a special method that calls into a trampoline for runtime imt conflicts.
315 mirror::ArtMethod* GetImtConflictMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jeff Hao88474b42013-10-23 16:24:40 -0700316
317 bool HasImtConflictMethod() const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700318 return !imt_conflict_method_.IsNull();
Jeff Hao88474b42013-10-23 16:24:40 -0700319 }
320
321 void SetImtConflictMethod(mirror::ArtMethod* method) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700322 imt_conflict_method_ = GcRoot<mirror::ArtMethod>(method);
Jeff Hao88474b42013-10-23 16:24:40 -0700323 }
324
325 mirror::ArtMethod* CreateImtConflictMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
326
327 // Returns an imt with every entry set to conflict, used as default imt for all classes.
Hiroshi Yamauchiab088112014-07-14 13:00:14 -0700328 mirror::ObjectArray<mirror::ArtMethod>* GetDefaultImt()
Ian Rogerse63db272014-07-15 15:36:11 -0700329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jeff Hao88474b42013-10-23 16:24:40 -0700330
331 bool HasDefaultImt() const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700332 return !default_imt_.IsNull();
Jeff Hao88474b42013-10-23 16:24:40 -0700333 }
334
335 void SetDefaultImt(mirror::ObjectArray<mirror::ArtMethod>* imt) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700336 default_imt_ = GcRoot<mirror::ObjectArray<mirror::ArtMethod>>(imt);
Jeff Hao88474b42013-10-23 16:24:40 -0700337 }
338
339 mirror::ObjectArray<mirror::ArtMethod>* CreateDefaultImt(ClassLinker* cl)
340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
341
Ian Rogersff1ed472011-09-20 13:46:24 -0700342 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700343 enum CalleeSaveType {
344 kSaveAll,
345 kRefsOnly,
346 kRefsAndArgs,
347 kLastCalleeSaveType // Value used for iteration
348 };
Ian Rogers9af209c2012-06-03 20:50:30 -0700349
350 bool HasCalleeSaveMethod(CalleeSaveType type) const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700351 return !callee_save_methods_[type].IsNull();
Ian Rogers9af209c2012-06-03 20:50:30 -0700352 }
353
Hiroshi Yamauchiab088112014-07-14 13:00:14 -0700354 mirror::ArtMethod* GetCalleeSaveMethod(CalleeSaveType type)
Ian Rogerse63db272014-07-15 15:36:11 -0700355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchiab088112014-07-14 13:00:14 -0700356
357 mirror::ArtMethod* GetCalleeSaveMethodUnchecked(CalleeSaveType type)
Ian Rogerse63db272014-07-15 15:36:11 -0700358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers9af209c2012-06-03 20:50:30 -0700359
Vladimir Marko7624d252014-05-02 14:40:15 +0100360 QuickMethodFrameInfo GetCalleeSaveMethodFrameInfo(CalleeSaveType type) const {
361 return callee_save_method_frame_infos_[type];
362 }
363
Hiroshi Yamauchiab088112014-07-14 13:00:14 -0700364 QuickMethodFrameInfo GetRuntimeMethodFrameInfo(mirror::ArtMethod* method)
365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100366
Ian Rogers936b37f2014-02-14 00:52:24 -0800367 static size_t GetCalleeSaveMethodOffset(CalleeSaveType type) {
368 return OFFSETOF_MEMBER(Runtime, callee_save_methods_[type]);
369 }
370
Vladimir Marko7624d252014-05-02 14:40:15 +0100371 InstructionSet GetInstructionSet() const {
372 return instruction_set_;
373 }
374
375 void SetInstructionSet(InstructionSet instruction_set);
376
Brian Carlstromea46f952013-07-30 01:26:50 -0700377 void SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700378
Vladimir Marko7624d252014-05-02 14:40:15 +0100379 mirror::ArtMethod* CreateCalleeSaveMethod(CalleeSaveType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersff1ed472011-09-20 13:46:24 -0700381
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700382 int32_t GetStat(int kind);
383
Ian Rogers9af209c2012-06-03 20:50:30 -0700384 RuntimeStats* GetStats() {
385 return &stats_;
386 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700387
388 bool HasStatsEnabled() const {
389 return stats_enabled_;
390 }
391
392 void ResetStats(int kinds);
393
394 void SetStatsEnabled(bool new_state);
395
Narayan Kamath3de95a72014-04-02 12:54:23 +0100396 void PreZygoteFork();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700397 bool InitZygote();
398 void DidForkFromZygote();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700399
Ian Rogers9bc54402014-04-17 16:40:01 -0700400 const instrumentation::Instrumentation* GetInstrumentation() const {
401 return &instrumentation_;
402 }
403
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 instrumentation::Instrumentation* GetInstrumentation() {
405 return &instrumentation_;
406 }
jeffhao2692b572011-12-16 15:42:28 -0800407
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800408 bool UseCompileTimeClassPath() const {
409 return use_compile_time_class_path_;
410 }
Ian Rogers9af209c2012-06-03 20:50:30 -0700411
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800412 void AddMethodVerifier(verifier::MethodVerifier* verifier) LOCKS_EXCLUDED(method_verifier_lock_);
413 void RemoveMethodVerifier(verifier::MethodVerifier* verifier)
414 LOCKS_EXCLUDED(method_verifier_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800415
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 const std::vector<const DexFile*>& GetCompileTimeClassPath(jobject class_loader);
417 void SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800418
Calin Juravlec1b643c2014-05-30 23:44:11 +0100419 void StartProfiler(const char* profile_output_filename);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800420 void UpdateProfilerState(int state);
Dave Allison0aded082013-11-07 13:15:11 -0800421
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100422 // Transaction support.
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100423 bool IsActiveTransaction() const {
424 return preinitialization_transaction_ != nullptr;
425 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100426 void EnterTransactionMode(Transaction* transaction);
427 void ExitTransactionMode();
Fred Shih37f05ef2014-07-16 18:38:08 -0700428 void RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset, uint8_t value,
429 bool is_volatile) const;
430 void RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset, int8_t value,
431 bool is_volatile) const;
432 void RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset, uint16_t value,
433 bool is_volatile) const;
434 void RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset, int16_t value,
435 bool is_volatile) const;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100436 void RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, uint32_t value,
437 bool is_volatile) const;
438 void RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, uint64_t value,
439 bool is_volatile) const;
440 void RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset,
441 mirror::Object* value, bool is_volatile) const;
442 void RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const
443 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
444 void RecordStrongStringInsertion(mirror::String* s, uint32_t hash_code) const
445 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
446 void RecordWeakStringInsertion(mirror::String* s, uint32_t hash_code) const
447 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
448 void RecordStrongStringRemoval(mirror::String* s, uint32_t hash_code) const
449 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
450 void RecordWeakStringRemoval(mirror::String* s, uint32_t hash_code) const
451 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
452
Mathieu Chartier15d34022014-02-26 17:16:38 -0800453 void SetFaultMessage(const std::string& message);
454 // Only read by the signal handler, NO_THREAD_SAFETY_ANALYSIS to prevent lock order violations
455 // with the unexpected_signal_lock_.
456 const std::string& GetFaultMessage() NO_THREAD_SAFETY_ANALYSIS {
457 return fault_message_;
458 }
459
Ian Rogers8afeb852014-04-02 14:55:49 -0700460 void AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* arg_vector) const;
461
Dave Allisonb373e092014-02-20 16:06:36 -0800462 bool ExplicitNullChecks() const {
463 return null_pointer_handler_ == nullptr;
464 }
465
466 bool ExplicitSuspendChecks() const {
467 return suspend_handler_ == nullptr;
468 }
469
470 bool ExplicitStackOverflowChecks() const {
471 return stack_overflow_handler_ == nullptr;
472 }
473
Jeff Hao4a200f52014-04-01 14:58:49 -0700474 bool IsVerificationEnabled() const {
475 return verify_;
476 }
477
Mathieu Chartierda44d772014-04-01 15:01:46 -0700478 bool RunningOnValgrind() const {
479 return running_on_valgrind_;
480 }
481
Jeff Haof00571c2014-05-29 17:29:47 -0700482 void SetTargetSdkVersion(int32_t version) {
483 target_sdk_version_ = version;
484 }
485
486 int32_t GetTargetSdkVersion() const {
487 return target_sdk_version_;
488 }
489
Ian Rogers8afeb852014-04-02 14:55:49 -0700490 static const char* GetDefaultInstructionSetFeatures() {
491 return kDefaultInstructionSetFeatures;
492 }
493
Carl Shapirob5573532011-07-12 18:22:59 -0700494 private:
Elliott Hughes457005c2012-04-16 13:54:25 -0700495 static void InitPlatformSignalHandlers();
Elliott Hughesffe67362011-07-17 12:09:27 -0700496
Elliott Hughesdcc24742011-09-07 14:02:44 -0700497 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700498
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700499 void BlockSignals();
500
Ian Rogerse63db272014-07-15 15:36:11 -0700501 bool Init(const RuntimeOptions& options, bool ignore_unrecognized)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
503 void InitNativeMethods() LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers365c1022012-06-22 15:05:28 -0700504 void InitThreadGroups(Thread* self);
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800505 void RegisterRuntimeNativeMethods(JNIEnv* env);
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700506
Elliott Hughes85d15452011-09-16 17:33:01 -0700507 void StartDaemonThreads();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700508 void StartSignalCatcher();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700509
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800510 // A pointer to the active runtime or NULL.
511 static Runtime* instance_;
512
Ian Rogers8afeb852014-04-02 14:55:49 -0700513 static const char* kDefaultInstructionSetFeatures;
514
515 // NOTE: these must match the gc::ProcessState values as they come directly from the framework.
516 static constexpr int kProfileForground = 0;
517 static constexpr int kProfileBackgrouud = 1;
518
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700519 GcRoot<mirror::ArtMethod> callee_save_methods_[kLastCalleeSaveType];
520 GcRoot<mirror::Throwable> pre_allocated_OutOfMemoryError_;
Ian Rogers63557452014-06-04 16:57:15 -0700521 GcRoot<mirror::Throwable> pre_allocated_NoClassDefFoundError_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700522 GcRoot<mirror::ArtMethod> resolution_method_;
523 GcRoot<mirror::ArtMethod> imt_conflict_method_;
524 GcRoot<mirror::ObjectArray<mirror::ArtMethod>> default_imt_;
Ian Rogers6ed19fd2014-03-20 08:10:17 -0700525
Ian Rogersc0542af2014-09-03 16:16:56 -0700526 // Special sentinel object used to invalid conditions in JNI (cleared weak references) and
527 // JDWP (invalid references).
528 GcRoot<mirror::Object> sentinel_;
529
Vladimir Marko7624d252014-05-02 14:40:15 +0100530 InstructionSet instruction_set_;
531 QuickMethodFrameInfo callee_save_method_frame_infos_[kLastCalleeSaveType];
532
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000533 CompilerCallbacks* compiler_callbacks_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700534 bool is_zygote_;
Alex Lighta59dd802014-07-02 16:28:08 -0700535 bool must_relocate_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700536 bool is_concurrent_gc_enabled_;
Anwar Ghuloum87183592013-08-14 12:12:19 -0700537 bool is_explicit_gc_disabled_;
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100538 bool dex2oat_enabled_;
Alex Light64ad14d2014-08-19 14:23:13 -0700539 bool image_dex2oat_enabled_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700540
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700541 std::string compiler_executable_;
Alex Lighta59dd802014-07-02 16:28:08 -0700542 std::string patchoat_executable_;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800543 std::vector<std::string> compiler_options_;
544 std::vector<std::string> image_compiler_options_;
Dragos Sbirlea7467ee02013-06-21 09:20:34 -0700545
Brian Carlstroma004aa92012-02-08 18:05:09 -0800546 std::string boot_class_path_string_;
547 std::string class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700548 std::vector<std::string> properties_;
549
Brian Carlstromb765be02011-08-17 23:54:10 -0700550 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700551 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700552
Ian Rogers1d54e732013-05-02 21:10:01 -0700553 gc::Heap* heap_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800554
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700555 // The number of spins that are done before thread suspension is used to forcibly inflate.
556 size_t max_spins_before_thin_lock_inflation_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700557 MonitorList* monitor_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800558 MonitorPool* monitor_pool_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700559
Carl Shapirob5573532011-07-12 18:22:59 -0700560 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700561
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700562 InternTable* intern_table_;
563
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700564 ClassLinker* class_linker_;
565
Elliott Hughese27955c2011-08-26 15:21:24 -0700566 SignalCatcher* signal_catcher_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700567 std::string stack_trace_file_;
Elliott Hughese27955c2011-08-26 15:21:24 -0700568
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700569 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700570
Mathieu Chartier15d34022014-02-26 17:16:38 -0800571 // Fault message, printed when we get a SIGSEGV.
572 Mutex fault_message_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
573 std::string fault_message_ GUARDED_BY(fault_message_lock_);
574
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800575 // Method verifier set, used so that we can update their GC roots.
Ian Rogers719d1a32014-03-06 12:13:39 -0800576 Mutex method_verifier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800577 std::set<verifier::MethodVerifier*> method_verifiers_;
578
Ian Rogers120f1c72012-09-28 17:17:10 -0700579 // A non-zero value indicates that a thread has been created but not yet initialized. Guarded by
580 // the shutdown lock so that threads aren't born while we're shutting down.
581 size_t threads_being_born_ GUARDED_BY(Locks::runtime_shutdown_lock_);
582
583 // Waited upon until no threads are being born.
Ian Rogers700a4022014-05-19 16:49:03 -0700584 std::unique_ptr<ConditionVariable> shutdown_cond_ GUARDED_BY(Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700585
586 // Set when runtime shutdown is past the point that new threads may attach.
587 bool shutting_down_ GUARDED_BY(Locks::runtime_shutdown_lock_);
588
589 // The runtime is starting to shutdown but is blocked waiting on shutdown_cond_.
590 bool shutting_down_started_ GUARDED_BY(Locks::runtime_shutdown_lock_);
591
Elliott Hughesdcc24742011-09-07 14:02:44 -0700592 bool started_;
593
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700594 // New flag added which tells us if the runtime has finished starting. If
595 // this flag is set then the Daemon threads are created and the class loader
596 // is created. This flag is needed for knowing if its safe to request CMS.
597 bool finished_starting_;
598
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700599 // Hooks supported by JNI_CreateJavaVM
600 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
601 void (*exit_)(jint status);
602 void (*abort_)();
603
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700604 bool stats_enabled_;
605 RuntimeStats stats_;
606
Mathieu Chartierda44d772014-04-01 15:01:46 -0700607 const bool running_on_valgrind_;
608
Dave Allison0aded082013-11-07 13:15:11 -0800609 std::string profile_output_filename_;
Calin Juravlec1b643c2014-05-30 23:44:11 +0100610 ProfilerOptions profiler_options_;
611 bool profiler_started_;
Dave Allison0aded082013-11-07 13:15:11 -0800612
jeffhaob5e81852012-03-12 11:15:45 -0700613 bool method_trace_;
614 std::string method_trace_file_;
615 size_t method_trace_file_size_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800616 instrumentation::Instrumentation instrumentation_;
jeffhao2692b572011-12-16 15:42:28 -0800617
Mathieu Chartierbad02672014-08-25 13:08:22 -0700618 typedef AllocationTrackingSafeMap<jobject, std::vector<const DexFile*>,
619 kAllocatorTagCompileTimeClassPath, JobjectComparator>
620 CompileTimeClassPaths;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800621 CompileTimeClassPaths compile_time_class_paths_;
622 bool use_compile_time_class_path_;
Elliott Hughes131aef82012-01-27 11:53:35 -0800623
Ian Rogers365c1022012-06-22 15:05:28 -0700624 jobject main_thread_group_;
625 jobject system_thread_group_;
626
Brian Carlstromce888532013-10-10 00:32:58 -0700627 // As returned by ClassLoader.getSystemClassLoader().
628 jobject system_class_loader_;
629
Hiroshi Yamauchi2e899a92013-11-22 16:50:12 -0800630 // If true, then we dump the GC cumulative timings on shutdown.
631 bool dump_gc_performance_on_shutdown_;
632
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100633 // Transaction used for pre-initializing classes at compilation time.
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100634 Transaction* preinitialization_transaction_;
Dave Allisonb373e092014-02-20 16:06:36 -0800635 NullPointerHandler* null_pointer_handler_;
636 SuspensionHandler* suspend_handler_;
637 StackOverflowHandler* stack_overflow_handler_;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100638
Jeff Hao4a200f52014-04-01 14:58:49 -0700639 // If false, verification is disabled. True by default.
640 bool verify_;
641
Jeff Haof00571c2014-05-29 17:29:47 -0700642 // Specifies target SDK version to allow workarounds for certain API levels.
643 int32_t target_sdk_version_;
644
Dave Allison69dfe512014-07-11 17:11:58 +0000645 // Implicit checks flags.
646 bool implicit_null_checks_; // NullPointer checks are implicit.
647 bool implicit_so_checks_; // StackOverflow checks are implicit.
648 bool implicit_suspend_checks_; // Thread suspension checks are implicit.
649
Calin Juravlea68629e2014-08-22 12:53:59 +0100650 // The filename to the native bridge library. If this is not empty the native bridge will be
651 // initialized and loaded from the given file (initialized and available). An empty value means
652 // that there's no native bridge (initialized but not available).
Calin Juravlec8423522014-08-12 20:55:20 +0100653 //
654 // The native bridge allows running native code compiled for a foreign ISA. The way it works is,
655 // if standard dlopen fails to load native library associated with native activity, it calls to
656 // the native bridge to load it and then gets the trampoline for the entry to native activity.
Calin Juravlea68629e2014-08-22 12:53:59 +0100657 std::string native_bridge_library_filename_;
Calin Juravlec8423522014-08-12 20:55:20 +0100658
659 // Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
660 //
661 // The interface is expected to expose the following methods:
662 // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
663 // native bridge calls back to VM for the shorty of the method so that it can prepare based on
664 // host calling convention.
665 // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
666 // native bridge can call back to get all native methods of specified class so that all
667 // corresponding trampolines can be destroyed.
668 android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_;
669
Carl Shapiro61e019d2011-07-14 16:53:09 -0700670 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700671};
672
673} // namespace art
674
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700675#endif // ART_RUNTIME_RUNTIME_H_