runtime.h revision 90f8addf6c68a9be6d813e5da7f6bea370a048f9
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#ifndef ART_RUNTIME_RUNTIME_H_
18#define ART_RUNTIME_RUNTIME_H_
19
20#include <jni.h>
21#include <stdio.h>
22
23#include <iosfwd>
24#include <set>
25#include <string>
26#include <utility>
27#include <memory>
28#include <vector>
29
30#include "arch/instruction_set.h"
31#include "base/macros.h"
32#include "base/mutex.h"
33#include "deoptimization_kind.h"
34#include "dex/dex_file_types.h"
35#include "experimental_flags.h"
36#include "gc_root.h"
37#include "instrumentation.h"
38#include "jdwp_provider.h"
39#include "obj_ptr.h"
40#include "offsets.h"
41#include "process_state.h"
42#include "quick/quick_method_frame_info.h"
43#include "runtime_stats.h"
44
45namespace art {
46
47namespace gc {
48class AbstractSystemWeakHolder;
49class Heap;
50}  // namespace gc
51
52namespace hiddenapi {
53enum class EnforcementPolicy;
54}  // namespace hiddenapi
55
56namespace jit {
57class Jit;
58class JitOptions;
59}  // namespace jit
60
61namespace mirror {
62class Array;
63class ClassLoader;
64class DexCache;
65template<class T> class ObjectArray;
66template<class T> class PrimitiveArray;
67typedef PrimitiveArray<int8_t> ByteArray;
68class String;
69class Throwable;
70}  // namespace mirror
71namespace ti {
72class Agent;
73class AgentSpec;
74}  // namespace ti
75namespace verifier {
76class MethodVerifier;
77enum class VerifyMode : int8_t;
78}  // namespace verifier
79class ArenaPool;
80class ArtMethod;
81enum class CalleeSaveType: uint32_t;
82class ClassLinker;
83class CompilerCallbacks;
84class DexFile;
85class InternTable;
86class IsMarkedVisitor;
87class JavaVMExt;
88class LinearAlloc;
89class MemMap;
90class MonitorList;
91class MonitorPool;
92class NullPointerHandler;
93class OatFileManager;
94class Plugin;
95struct RuntimeArgumentMap;
96class RuntimeCallbacks;
97class SignalCatcher;
98class StackOverflowHandler;
99class SuspensionHandler;
100class ThreadList;
101class Trace;
102struct TraceConfig;
103class Transaction;
104
105typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
106
107class Runtime {
108 public:
109  // Parse raw runtime options.
110  static bool ParseOptions(const RuntimeOptions& raw_options,
111                           bool ignore_unrecognized,
112                           RuntimeArgumentMap* runtime_options);
113
114  // Creates and initializes a new runtime.
115  static bool Create(RuntimeArgumentMap&& runtime_options)
116      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
117
118  // Creates and initializes a new runtime.
119  static bool Create(const RuntimeOptions& raw_options, bool ignore_unrecognized)
120      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
121
122  // IsAotCompiler for compilers that don't have a running runtime. Only dex2oat currently.
123  bool IsAotCompiler() const {
124    return !UseJitCompilation() && IsCompiler();
125  }
126
127  // IsCompiler is any runtime which has a running compiler, either dex2oat or JIT.
128  bool IsCompiler() const {
129    return compiler_callbacks_ != nullptr;
130  }
131
132  // If a compiler, are we compiling a boot image?
133  bool IsCompilingBootImage() const;
134
135  bool CanRelocate() const;
136
137  bool ShouldRelocate() const {
138    return must_relocate_ && CanRelocate();
139  }
140
141  bool MustRelocateIfPossible() const {
142    return must_relocate_;
143  }
144
145  bool IsDex2OatEnabled() const {
146    return dex2oat_enabled_ && IsImageDex2OatEnabled();
147  }
148
149  bool IsImageDex2OatEnabled() const {
150    return image_dex2oat_enabled_;
151  }
152
153  CompilerCallbacks* GetCompilerCallbacks() {
154    return compiler_callbacks_;
155  }
156
157  void SetCompilerCallbacks(CompilerCallbacks* callbacks) {
158    CHECK(callbacks != nullptr);
159    compiler_callbacks_ = callbacks;
160  }
161
162  bool IsZygote() const {
163    return is_zygote_;
164  }
165
166  bool IsExplicitGcDisabled() const {
167    return is_explicit_gc_disabled_;
168  }
169
170  std::string GetCompilerExecutable() const;
171  std::string GetPatchoatExecutable() const;
172
173  const std::vector<std::string>& GetCompilerOptions() const {
174    return compiler_options_;
175  }
176
177  void AddCompilerOption(const std::string& option) {
178    compiler_options_.push_back(option);
179  }
180
181  const std::vector<std::string>& GetImageCompilerOptions() const {
182    return image_compiler_options_;
183  }
184
185  const std::string& GetImageLocation() const {
186    return image_location_;
187  }
188
189  // Starts a runtime, which may cause threads to be started and code to run.
190  bool Start() UNLOCK_FUNCTION(Locks::mutator_lock_);
191
192  bool IsShuttingDown(Thread* self);
193  bool IsShuttingDownLocked() const REQUIRES(Locks::runtime_shutdown_lock_) {
194    return shutting_down_;
195  }
196
197  size_t NumberOfThreadsBeingBorn() const REQUIRES(Locks::runtime_shutdown_lock_) {
198    return threads_being_born_;
199  }
200
201  void StartThreadBirth() REQUIRES(Locks::runtime_shutdown_lock_) {
202    threads_being_born_++;
203  }
204
205  void EndThreadBirth() REQUIRES(Locks::runtime_shutdown_lock_);
206
207  bool IsStarted() const {
208    return started_;
209  }
210
211  bool IsFinishedStarting() const {
212    return finished_starting_;
213  }
214
215  static Runtime* Current() {
216    return instance_;
217  }
218
219  // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
220  // callers should prefer.
221  NO_RETURN static void Abort(const char* msg) REQUIRES(!Locks::abort_lock_);
222
223  // Returns the "main" ThreadGroup, used when attaching user threads.
224  jobject GetMainThreadGroup() const;
225
226  // Returns the "system" ThreadGroup, used when attaching our internal threads.
227  jobject GetSystemThreadGroup() const;
228
229  // Returns the system ClassLoader which represents the CLASSPATH.
230  jobject GetSystemClassLoader() const;
231
232  // Attaches the calling native thread to the runtime.
233  bool AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
234                           bool create_peer);
235
236  void CallExitHook(jint status);
237
238  // Detaches the current native thread from the runtime.
239  void DetachCurrentThread() REQUIRES(!Locks::mutator_lock_);
240
241  void DumpDeoptimizations(std::ostream& os);
242  void DumpForSigQuit(std::ostream& os);
243  void DumpLockHolders(std::ostream& os);
244
245  ~Runtime();
246
247  const std::string& GetBootClassPathString() const {
248    return boot_class_path_string_;
249  }
250
251  const std::string& GetClassPathString() const {
252    return class_path_string_;
253  }
254
255  ClassLinker* GetClassLinker() const {
256    return class_linker_;
257  }
258
259  size_t GetDefaultStackSize() const {
260    return default_stack_size_;
261  }
262
263  gc::Heap* GetHeap() const {
264    return heap_;
265  }
266
267  InternTable* GetInternTable() const {
268    DCHECK(intern_table_ != nullptr);
269    return intern_table_;
270  }
271
272  JavaVMExt* GetJavaVM() const {
273    return java_vm_.get();
274  }
275
276  size_t GetMaxSpinsBeforeThinLockInflation() const {
277    return max_spins_before_thin_lock_inflation_;
278  }
279
280  MonitorList* GetMonitorList() const {
281    return monitor_list_;
282  }
283
284  MonitorPool* GetMonitorPool() const {
285    return monitor_pool_;
286  }
287
288  // Is the given object the special object used to mark a cleared JNI weak global?
289  bool IsClearedJniWeakGlobal(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
290
291  // Get the special object used to mark a cleared JNI weak global.
292  mirror::Object* GetClearedJniWeakGlobal() REQUIRES_SHARED(Locks::mutator_lock_);
293
294  mirror::Throwable* GetPreAllocatedOutOfMemoryError() REQUIRES_SHARED(Locks::mutator_lock_);
295
296  mirror::Throwable* GetPreAllocatedNoClassDefFoundError()
297      REQUIRES_SHARED(Locks::mutator_lock_);
298
299  const std::vector<std::string>& GetProperties() const {
300    return properties_;
301  }
302
303  ThreadList* GetThreadList() const {
304    return thread_list_;
305  }
306
307  static const char* GetVersion() {
308    return "2.1.0";
309  }
310
311  bool IsMethodHandlesEnabled() const {
312    return true;
313  }
314
315  void DisallowNewSystemWeaks() REQUIRES_SHARED(Locks::mutator_lock_);
316  void AllowNewSystemWeaks() REQUIRES_SHARED(Locks::mutator_lock_);
317  // broadcast_for_checkpoint is true when we broadcast for making blocking threads to respond to
318  // checkpoint requests. It's false when we broadcast to unblock blocking threads after system weak
319  // access is reenabled.
320  void BroadcastForNewSystemWeaks(bool broadcast_for_checkpoint = false);
321
322  // Visit all the roots. If only_dirty is true then non-dirty roots won't be visited. If
323  // clean_dirty is true then dirty roots will be marked as non-dirty after visiting.
324  void VisitRoots(RootVisitor* visitor, VisitRootFlags flags = kVisitRootFlagAllRoots)
325      REQUIRES(!Locks::classlinker_classes_lock_, !Locks::trace_lock_)
326      REQUIRES_SHARED(Locks::mutator_lock_);
327
328  // Visit image roots, only used for hprof since the GC uses the image space mod union table
329  // instead.
330  void VisitImageRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
331
332  // Visit all of the roots we can safely visit concurrently.
333  void VisitConcurrentRoots(RootVisitor* visitor,
334                            VisitRootFlags flags = kVisitRootFlagAllRoots)
335      REQUIRES(!Locks::classlinker_classes_lock_, !Locks::trace_lock_)
336      REQUIRES_SHARED(Locks::mutator_lock_);
337
338  // Visit all of the non thread roots, we can do this with mutators unpaused.
339  void VisitNonThreadRoots(RootVisitor* visitor)
340      REQUIRES_SHARED(Locks::mutator_lock_);
341
342  void VisitTransactionRoots(RootVisitor* visitor)
343      REQUIRES_SHARED(Locks::mutator_lock_);
344
345  // Sweep system weaks, the system weak is deleted if the visitor return null. Otherwise, the
346  // system weak is updated to be the visitor's returned value.
347  void SweepSystemWeaks(IsMarkedVisitor* visitor)
348      REQUIRES_SHARED(Locks::mutator_lock_);
349
350  // Returns a special method that calls into a trampoline for runtime method resolution
351  ArtMethod* GetResolutionMethod();
352
353  bool HasResolutionMethod() const {
354    return resolution_method_ != nullptr;
355  }
356
357  void SetResolutionMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
358  void ClearResolutionMethod() {
359    resolution_method_ = nullptr;
360  }
361
362  ArtMethod* CreateResolutionMethod() REQUIRES_SHARED(Locks::mutator_lock_);
363
364  // Returns a special method that calls into a trampoline for runtime imt conflicts.
365  ArtMethod* GetImtConflictMethod();
366  ArtMethod* GetImtUnimplementedMethod();
367
368  bool HasImtConflictMethod() const {
369    return imt_conflict_method_ != nullptr;
370  }
371
372  void ClearImtConflictMethod() {
373    imt_conflict_method_ = nullptr;
374  }
375
376  void FixupConflictTables();
377  void SetImtConflictMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
378  void SetImtUnimplementedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
379
380  ArtMethod* CreateImtConflictMethod(LinearAlloc* linear_alloc)
381      REQUIRES_SHARED(Locks::mutator_lock_);
382
383  void ClearImtUnimplementedMethod() {
384    imt_unimplemented_method_ = nullptr;
385  }
386
387  bool HasCalleeSaveMethod(CalleeSaveType type) const {
388    return callee_save_methods_[static_cast<size_t>(type)] != 0u;
389  }
390
391  ArtMethod* GetCalleeSaveMethod(CalleeSaveType type)
392      REQUIRES_SHARED(Locks::mutator_lock_);
393
394  ArtMethod* GetCalleeSaveMethodUnchecked(CalleeSaveType type)
395      REQUIRES_SHARED(Locks::mutator_lock_);
396
397  QuickMethodFrameInfo GetCalleeSaveMethodFrameInfo(CalleeSaveType type) const {
398    return callee_save_method_frame_infos_[static_cast<size_t>(type)];
399  }
400
401  QuickMethodFrameInfo GetRuntimeMethodFrameInfo(ArtMethod* method)
402      REQUIRES_SHARED(Locks::mutator_lock_);
403
404  static size_t GetCalleeSaveMethodOffset(CalleeSaveType type) {
405    return OFFSETOF_MEMBER(Runtime, callee_save_methods_[static_cast<size_t>(type)]);
406  }
407
408  InstructionSet GetInstructionSet() const {
409    return instruction_set_;
410  }
411
412  void SetInstructionSet(InstructionSet instruction_set);
413  void ClearInstructionSet();
414
415  void SetCalleeSaveMethod(ArtMethod* method, CalleeSaveType type);
416  void ClearCalleeSaveMethods();
417
418  ArtMethod* CreateCalleeSaveMethod() REQUIRES_SHARED(Locks::mutator_lock_);
419
420  int32_t GetStat(int kind);
421
422  RuntimeStats* GetStats() {
423    return &stats_;
424  }
425
426  bool HasStatsEnabled() const {
427    return stats_enabled_;
428  }
429
430  void ResetStats(int kinds);
431
432  void SetStatsEnabled(bool new_state)
433      REQUIRES(!Locks::instrument_entrypoints_lock_, !Locks::mutator_lock_);
434
435  enum class NativeBridgeAction {  // private
436    kUnload,
437    kInitialize
438  };
439
440  jit::Jit* GetJit() const {
441    return jit_.get();
442  }
443
444  // Returns true if JIT compilations are enabled. GetJit() will be not null in this case.
445  bool UseJitCompilation() const;
446
447  void PreZygoteFork();
448  void InitNonZygoteOrPostFork(
449      JNIEnv* env, bool is_system_server, NativeBridgeAction action, const char* isa);
450
451  const instrumentation::Instrumentation* GetInstrumentation() const {
452    return &instrumentation_;
453  }
454
455  instrumentation::Instrumentation* GetInstrumentation() {
456    return &instrumentation_;
457  }
458
459  void RegisterAppInfo(const std::vector<std::string>& code_paths,
460                       const std::string& profile_output_filename);
461
462  // Transaction support.
463  bool IsActiveTransaction() const;
464  void EnterTransactionMode();
465  void EnterTransactionMode(bool strict, mirror::Class* root);
466  void ExitTransactionMode();
467  void RollbackAllTransactions() REQUIRES_SHARED(Locks::mutator_lock_);
468  // Transaction rollback and exit transaction are always done together, it's convenience to
469  // do them in one function.
470  void RollbackAndExitTransactionMode() REQUIRES_SHARED(Locks::mutator_lock_);
471  bool IsTransactionAborted() const;
472  const std::unique_ptr<Transaction>& GetTransaction() const;
473  bool IsActiveStrictTransactionMode() const;
474
475  void AbortTransactionAndThrowAbortError(Thread* self, const std::string& abort_message)
476      REQUIRES_SHARED(Locks::mutator_lock_);
477  void ThrowTransactionAbortError(Thread* self)
478      REQUIRES_SHARED(Locks::mutator_lock_);
479
480  void RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset, uint8_t value,
481                               bool is_volatile) const;
482  void RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset, int8_t value,
483                            bool is_volatile) const;
484  void RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset, uint16_t value,
485                            bool is_volatile) const;
486  void RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset, int16_t value,
487                          bool is_volatile) const;
488  void RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, uint32_t value,
489                          bool is_volatile) const;
490  void RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, uint64_t value,
491                          bool is_volatile) const;
492  void RecordWriteFieldReference(mirror::Object* obj,
493                                 MemberOffset field_offset,
494                                 ObjPtr<mirror::Object> value,
495                                 bool is_volatile) const
496      REQUIRES_SHARED(Locks::mutator_lock_);
497  void RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const
498      REQUIRES_SHARED(Locks::mutator_lock_);
499  void RecordStrongStringInsertion(ObjPtr<mirror::String> s) const
500      REQUIRES(Locks::intern_table_lock_);
501  void RecordWeakStringInsertion(ObjPtr<mirror::String> s) const
502      REQUIRES(Locks::intern_table_lock_);
503  void RecordStrongStringRemoval(ObjPtr<mirror::String> s) const
504      REQUIRES(Locks::intern_table_lock_);
505  void RecordWeakStringRemoval(ObjPtr<mirror::String> s) const
506      REQUIRES(Locks::intern_table_lock_);
507  void RecordResolveString(ObjPtr<mirror::DexCache> dex_cache, dex::StringIndex string_idx) const
508      REQUIRES_SHARED(Locks::mutator_lock_);
509
510  void SetFaultMessage(const std::string& message) REQUIRES(!fault_message_lock_);
511  // Only read by the signal handler, NO_THREAD_SAFETY_ANALYSIS to prevent lock order violations
512  // with the unexpected_signal_lock_.
513  const std::string& GetFaultMessage() NO_THREAD_SAFETY_ANALYSIS {
514    return fault_message_;
515  }
516
517  void AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* arg_vector) const;
518
519  bool ExplicitStackOverflowChecks() const {
520    return !implicit_so_checks_;
521  }
522
523  void DisableVerifier();
524  bool IsVerificationEnabled() const;
525  bool IsVerificationSoftFail() const;
526
527  void SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy policy) {
528    hidden_api_policy_ = policy;
529  }
530
531  hiddenapi::EnforcementPolicy GetHiddenApiEnforcementPolicy() const {
532    return hidden_api_policy_;
533  }
534
535  void SetPendingHiddenApiWarning(bool value) {
536    pending_hidden_api_warning_ = value;
537  }
538
539  void SetHiddenApiExemptions(const std::vector<std::string>& exemptions) {
540    hidden_api_exemptions_ = exemptions;
541  }
542
543  const std::vector<std::string>& GetHiddenApiExemptions() {
544    return hidden_api_exemptions_;
545  }
546
547  bool HasPendingHiddenApiWarning() const {
548    return pending_hidden_api_warning_;
549  }
550
551  void SetDedupeHiddenApiWarnings(bool value) {
552    dedupe_hidden_api_warnings_ = value;
553  }
554
555  bool ShouldDedupeHiddenApiWarnings() {
556    return dedupe_hidden_api_warnings_;
557  }
558
559  void AlwaysSetHiddenApiWarningFlag() {
560    always_set_hidden_api_warning_flag_ = true;
561  }
562
563  bool ShouldAlwaysSetHiddenApiWarningFlag() const {
564    return always_set_hidden_api_warning_flag_;
565  }
566
567  void SetHiddenApiEventLogSampleRate(uint32_t rate) {
568    hidden_api_access_event_log_rate_ = rate;
569  }
570
571  uint32_t GetHiddenApiEventLogSampleRate() const {
572    return hidden_api_access_event_log_rate_;
573  }
574
575  bool IsDexFileFallbackEnabled() const {
576    return allow_dex_file_fallback_;
577  }
578
579  const std::vector<std::string>& GetCpuAbilist() const {
580    return cpu_abilist_;
581  }
582
583  bool IsRunningOnMemoryTool() const {
584    return is_running_on_memory_tool_;
585  }
586
587  void SetTargetSdkVersion(int32_t version) {
588    target_sdk_version_ = version;
589  }
590
591  int32_t GetTargetSdkVersion() const {
592    return target_sdk_version_;
593  }
594
595  uint32_t GetZygoteMaxFailedBoots() const {
596    return zygote_max_failed_boots_;
597  }
598
599  bool AreExperimentalFlagsEnabled(ExperimentalFlags flags) {
600    return (experimental_flags_ & flags) != ExperimentalFlags::kNone;
601  }
602
603  // Create the JIT and instrumentation and code cache.
604  void CreateJit();
605
606  ArenaPool* GetArenaPool() {
607    return arena_pool_.get();
608  }
609  ArenaPool* GetJitArenaPool() {
610    return jit_arena_pool_.get();
611  }
612  const ArenaPool* GetArenaPool() const {
613    return arena_pool_.get();
614  }
615
616  void ReclaimArenaPoolMemory();
617
618  LinearAlloc* GetLinearAlloc() {
619    return linear_alloc_.get();
620  }
621
622  jit::JitOptions* GetJITOptions() {
623    return jit_options_.get();
624  }
625
626  bool IsJavaDebuggable() const {
627    return is_java_debuggable_;
628  }
629
630  void SetJavaDebuggable(bool value);
631
632  // Deoptimize the boot image, called for Java debuggable apps.
633  void DeoptimizeBootImage();
634
635  bool IsNativeDebuggable() const {
636    return is_native_debuggable_;
637  }
638
639  void SetNativeDebuggable(bool value) {
640    is_native_debuggable_ = value;
641  }
642
643  bool AreAsyncExceptionsThrown() const {
644    return async_exceptions_thrown_;
645  }
646
647  void SetAsyncExceptionsThrown() {
648    async_exceptions_thrown_ = true;
649  }
650
651  // Returns the build fingerprint, if set. Otherwise an empty string is returned.
652  std::string GetFingerprint() {
653    return fingerprint_;
654  }
655
656  // Called from class linker.
657  void SetSentinel(mirror::Object* sentinel) REQUIRES_SHARED(Locks::mutator_lock_);
658
659  // Create a normal LinearAlloc or low 4gb version if we are 64 bit AOT compiler.
660  LinearAlloc* CreateLinearAlloc();
661
662  OatFileManager& GetOatFileManager() const {
663    DCHECK(oat_file_manager_ != nullptr);
664    return *oat_file_manager_;
665  }
666
667  double GetHashTableMinLoadFactor() const;
668  double GetHashTableMaxLoadFactor() const;
669
670  void SetSafeMode(bool mode) {
671    safe_mode_ = mode;
672  }
673
674  bool GetDumpNativeStackOnSigQuit() const {
675    return dump_native_stack_on_sig_quit_;
676  }
677
678  bool GetPrunedDalvikCache() const {
679    return pruned_dalvik_cache_;
680  }
681
682  void SetPrunedDalvikCache(bool pruned) {
683    pruned_dalvik_cache_ = pruned;
684  }
685
686  void UpdateProcessState(ProcessState process_state);
687
688  // Returns true if we currently care about long mutator pause.
689  bool InJankPerceptibleProcessState() const {
690    return process_state_ == kProcessStateJankPerceptible;
691  }
692
693  void RegisterSensitiveThread() const;
694
695  void SetZygoteNoThreadSection(bool val) {
696    zygote_no_threads_ = val;
697  }
698
699  bool IsZygoteNoThreadSection() const {
700    return zygote_no_threads_;
701  }
702
703  // Returns if the code can be deoptimized asynchronously. Code may be compiled with some
704  // optimization that makes it impossible to deoptimize.
705  bool IsAsyncDeoptimizeable(uintptr_t code) const REQUIRES_SHARED(Locks::mutator_lock_);
706
707  // Returns a saved copy of the environment (getenv/setenv values).
708  // Used by Fork to protect against overwriting LD_LIBRARY_PATH, etc.
709  char** GetEnvSnapshot() const {
710    return env_snapshot_.GetSnapshot();
711  }
712
713  void AddSystemWeakHolder(gc::AbstractSystemWeakHolder* holder);
714  void RemoveSystemWeakHolder(gc::AbstractSystemWeakHolder* holder);
715
716  void AttachAgent(JNIEnv* env, const std::string& agent_arg, jobject class_loader);
717
718  const std::list<std::unique_ptr<ti::Agent>>& GetAgents() const {
719    return agents_;
720  }
721
722  RuntimeCallbacks* GetRuntimeCallbacks();
723
724  bool HasLoadedPlugins() const {
725    return !plugins_.empty();
726  }
727
728  void InitThreadGroups(Thread* self);
729
730  void SetDumpGCPerformanceOnShutdown(bool value) {
731    dump_gc_performance_on_shutdown_ = value;
732  }
733
734  void IncrementDeoptimizationCount(DeoptimizationKind kind) {
735    DCHECK_LE(kind, DeoptimizationKind::kLast);
736    deoptimization_counts_[static_cast<size_t>(kind)]++;
737  }
738
739  uint32_t GetNumberOfDeoptimizations() const {
740    uint32_t result = 0;
741    for (size_t i = 0; i <= static_cast<size_t>(DeoptimizationKind::kLast); ++i) {
742      result += deoptimization_counts_[i];
743    }
744    return result;
745  }
746
747  // Whether or not we use MADV_RANDOM on files that are thought to have random access patterns.
748  // This is beneficial for low RAM devices since it reduces page cache thrashing.
749  bool MAdviseRandomAccess() const {
750    return madvise_random_access_;
751  }
752
753  const std::string& GetJdwpOptions() {
754    return jdwp_options_;
755  }
756
757  JdwpProvider GetJdwpProvider() const {
758    return jdwp_provider_;
759  }
760
761  static constexpr int32_t kUnsetSdkVersion = 0u;
762
763 private:
764  static void InitPlatformSignalHandlers();
765
766  Runtime();
767
768  void BlockSignals();
769
770  bool Init(RuntimeArgumentMap&& runtime_options)
771      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
772  void InitNativeMethods() REQUIRES(!Locks::mutator_lock_);
773  void RegisterRuntimeNativeMethods(JNIEnv* env);
774
775  void StartDaemonThreads();
776  void StartSignalCatcher();
777
778  void MaybeSaveJitProfilingInfo();
779
780  // Visit all of the thread roots.
781  void VisitThreadRoots(RootVisitor* visitor, VisitRootFlags flags)
782      REQUIRES_SHARED(Locks::mutator_lock_);
783
784  // Visit all other roots which must be done with mutators suspended.
785  void VisitNonConcurrentRoots(RootVisitor* visitor, VisitRootFlags flags)
786      REQUIRES_SHARED(Locks::mutator_lock_);
787
788  // Constant roots are the roots which never change after the runtime is initialized, they only
789  // need to be visited once per GC cycle.
790  void VisitConstantRoots(RootVisitor* visitor)
791      REQUIRES_SHARED(Locks::mutator_lock_);
792
793  // A pointer to the active runtime or null.
794  static Runtime* instance_;
795
796  // NOTE: these must match the gc::ProcessState values as they come directly from the framework.
797  static constexpr int kProfileForground = 0;
798  static constexpr int kProfileBackground = 1;
799
800  static constexpr uint32_t kCalleeSaveSize = 6u;
801
802  // 64 bit so that we can share the same asm offsets for both 32 and 64 bits.
803  uint64_t callee_save_methods_[kCalleeSaveSize];
804  GcRoot<mirror::Throwable> pre_allocated_OutOfMemoryError_;
805  GcRoot<mirror::Throwable> pre_allocated_NoClassDefFoundError_;
806  ArtMethod* resolution_method_;
807  ArtMethod* imt_conflict_method_;
808  // Unresolved method has the same behavior as the conflict method, it is used by the class linker
809  // for differentiating between unfilled imt slots vs conflict slots in superclasses.
810  ArtMethod* imt_unimplemented_method_;
811
812  // Special sentinel object used to invalid conditions in JNI (cleared weak references) and
813  // JDWP (invalid references).
814  GcRoot<mirror::Object> sentinel_;
815
816  InstructionSet instruction_set_;
817  QuickMethodFrameInfo callee_save_method_frame_infos_[kCalleeSaveSize];
818
819  CompilerCallbacks* compiler_callbacks_;
820  bool is_zygote_;
821  bool must_relocate_;
822  bool is_concurrent_gc_enabled_;
823  bool is_explicit_gc_disabled_;
824  bool dex2oat_enabled_;
825  bool image_dex2oat_enabled_;
826
827  std::string compiler_executable_;
828  std::string patchoat_executable_;
829  std::vector<std::string> compiler_options_;
830  std::vector<std::string> image_compiler_options_;
831  std::string image_location_;
832
833  std::string boot_class_path_string_;
834  std::string class_path_string_;
835  std::vector<std::string> properties_;
836
837  std::list<ti::AgentSpec> agent_specs_;
838  std::list<std::unique_ptr<ti::Agent>> agents_;
839  std::vector<Plugin> plugins_;
840
841  // The default stack size for managed threads created by the runtime.
842  size_t default_stack_size_;
843
844  gc::Heap* heap_;
845
846  std::unique_ptr<ArenaPool> jit_arena_pool_;
847  std::unique_ptr<ArenaPool> arena_pool_;
848  // Special low 4gb pool for compiler linear alloc. We need ArtFields to be in low 4gb if we are
849  // compiling using a 32 bit image on a 64 bit compiler in case we resolve things in the image
850  // since the field arrays are int arrays in this case.
851  std::unique_ptr<ArenaPool> low_4gb_arena_pool_;
852
853  // Shared linear alloc for now.
854  std::unique_ptr<LinearAlloc> linear_alloc_;
855
856  // The number of spins that are done before thread suspension is used to forcibly inflate.
857  size_t max_spins_before_thin_lock_inflation_;
858  MonitorList* monitor_list_;
859  MonitorPool* monitor_pool_;
860
861  ThreadList* thread_list_;
862
863  InternTable* intern_table_;
864
865  ClassLinker* class_linker_;
866
867  SignalCatcher* signal_catcher_;
868
869  // If true, the runtime will connect to tombstoned via a socket to
870  // request an open file descriptor to write its traces to.
871  bool use_tombstoned_traces_;
872
873  // Location to which traces must be written on SIGQUIT. Only used if
874  // tombstoned_traces_ == false.
875  std::string stack_trace_file_;
876
877  std::unique_ptr<JavaVMExt> java_vm_;
878
879  std::unique_ptr<jit::Jit> jit_;
880  std::unique_ptr<jit::JitOptions> jit_options_;
881
882  // Fault message, printed when we get a SIGSEGV.
883  Mutex fault_message_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
884  std::string fault_message_ GUARDED_BY(fault_message_lock_);
885
886  // A non-zero value indicates that a thread has been created but not yet initialized. Guarded by
887  // the shutdown lock so that threads aren't born while we're shutting down.
888  size_t threads_being_born_ GUARDED_BY(Locks::runtime_shutdown_lock_);
889
890  // Waited upon until no threads are being born.
891  std::unique_ptr<ConditionVariable> shutdown_cond_ GUARDED_BY(Locks::runtime_shutdown_lock_);
892
893  // Set when runtime shutdown is past the point that new threads may attach.
894  bool shutting_down_ GUARDED_BY(Locks::runtime_shutdown_lock_);
895
896  // The runtime is starting to shutdown but is blocked waiting on shutdown_cond_.
897  bool shutting_down_started_ GUARDED_BY(Locks::runtime_shutdown_lock_);
898
899  bool started_;
900
901  // New flag added which tells us if the runtime has finished starting. If
902  // this flag is set then the Daemon threads are created and the class loader
903  // is created. This flag is needed for knowing if its safe to request CMS.
904  bool finished_starting_;
905
906  // Hooks supported by JNI_CreateJavaVM
907  jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
908  void (*exit_)(jint status);
909  void (*abort_)();
910
911  bool stats_enabled_;
912  RuntimeStats stats_;
913
914  const bool is_running_on_memory_tool_;
915
916  std::unique_ptr<TraceConfig> trace_config_;
917
918  instrumentation::Instrumentation instrumentation_;
919
920  jobject main_thread_group_;
921  jobject system_thread_group_;
922
923  // As returned by ClassLoader.getSystemClassLoader().
924  jobject system_class_loader_;
925
926  // If true, then we dump the GC cumulative timings on shutdown.
927  bool dump_gc_performance_on_shutdown_;
928
929  // Transactions used for pre-initializing classes at compilation time.
930  // Support nested transactions, maintain a list containing all transactions. Transactions are
931  // handled under a stack discipline. Because GC needs to go over all transactions, we choose list
932  // as substantial data structure instead of stack.
933  std::list<std::unique_ptr<Transaction>> preinitialization_transactions_;
934
935  // If kNone, verification is disabled. kEnable by default.
936  verifier::VerifyMode verify_;
937
938  // If true, the runtime may use dex files directly with the interpreter if an oat file is not
939  // available/usable.
940  bool allow_dex_file_fallback_;
941
942  // List of supported cpu abis.
943  std::vector<std::string> cpu_abilist_;
944
945  // Specifies target SDK version to allow workarounds for certain API levels.
946  int32_t target_sdk_version_;
947
948  // Implicit checks flags.
949  bool implicit_null_checks_;       // NullPointer checks are implicit.
950  bool implicit_so_checks_;         // StackOverflow checks are implicit.
951  bool implicit_suspend_checks_;    // Thread suspension checks are implicit.
952
953  // Whether or not the sig chain (and implicitly the fault handler) should be
954  // disabled. Tools like dex2oat or patchoat don't need them. This enables
955  // building a statically link version of dex2oat.
956  bool no_sig_chain_;
957
958  // Force the use of native bridge even if the app ISA matches the runtime ISA.
959  bool force_native_bridge_;
960
961  // Whether or not a native bridge has been loaded.
962  //
963  // The native bridge allows running native code compiled for a foreign ISA. The way it works is,
964  // if standard dlopen fails to load native library associated with native activity, it calls to
965  // the native bridge to load it and then gets the trampoline for the entry to native activity.
966  //
967  // The option 'native_bridge_library_filename' specifies the name of the native bridge.
968  // When non-empty the native bridge will be loaded from the given file. An empty value means
969  // that there's no native bridge.
970  bool is_native_bridge_loaded_;
971
972  // Whether we are running under native debugger.
973  bool is_native_debuggable_;
974
975  // whether or not any async exceptions have ever been thrown. This is used to speed up the
976  // MterpShouldSwitchInterpreters function.
977  bool async_exceptions_thrown_;
978
979  // Whether Java code needs to be debuggable.
980  bool is_java_debuggable_;
981
982  // The maximum number of failed boots we allow before pruning the dalvik cache
983  // and trying again. This option is only inspected when we're running as a
984  // zygote.
985  uint32_t zygote_max_failed_boots_;
986
987  // Enable experimental opcodes that aren't fully specified yet. The intent is to
988  // eventually publish them as public-usable opcodes, but they aren't ready yet.
989  //
990  // Experimental opcodes should not be used by other production code.
991  ExperimentalFlags experimental_flags_;
992
993  // Contains the build fingerprint, if given as a parameter.
994  std::string fingerprint_;
995
996  // Oat file manager, keeps track of what oat files are open.
997  OatFileManager* oat_file_manager_;
998
999  // Whether or not we are on a low RAM device.
1000  bool is_low_memory_mode_;
1001
1002  // Whether or not we use MADV_RANDOM on files that are thought to have random access patterns.
1003  // This is beneficial for low RAM devices since it reduces page cache thrashing.
1004  bool madvise_random_access_;
1005
1006  // Whether the application should run in safe mode, that is, interpreter only.
1007  bool safe_mode_;
1008
1009  // Whether access checks on hidden API should be performed.
1010  hiddenapi::EnforcementPolicy hidden_api_policy_;
1011
1012  // List of signature prefixes of methods that have been removed from the blacklist, and treated
1013  // as if whitelisted.
1014  std::vector<std::string> hidden_api_exemptions_;
1015
1016  // Whether the application has used an API which is not restricted but we
1017  // should issue a warning about it.
1018  bool pending_hidden_api_warning_;
1019
1020  // Do not warn about the same hidden API access violation twice.
1021  // This is only used for testing.
1022  bool dedupe_hidden_api_warnings_;
1023
1024  // Hidden API can print warnings into the log and/or set a flag read by the
1025  // framework to show a UI warning. If this flag is set, always set the flag
1026  // when there is a warning. This is only used for testing.
1027  bool always_set_hidden_api_warning_flag_;
1028
1029  // How often to log hidden API access to the event log. An integer between 0 (never)
1030  // and 0x10000 (always).
1031  uint32_t hidden_api_access_event_log_rate_;
1032
1033  // Whether threads should dump their native stack on SIGQUIT.
1034  bool dump_native_stack_on_sig_quit_;
1035
1036  // Whether the dalvik cache was pruned when initializing the runtime.
1037  bool pruned_dalvik_cache_;
1038
1039  // Whether or not we currently care about pause times.
1040  ProcessState process_state_;
1041
1042  // Whether zygote code is in a section that should not start threads.
1043  bool zygote_no_threads_;
1044
1045  // The string containing requested jdwp options
1046  std::string jdwp_options_;
1047
1048  // The jdwp provider we were configured with.
1049  JdwpProvider jdwp_provider_;
1050
1051  // Saved environment.
1052  class EnvSnapshot {
1053   public:
1054    EnvSnapshot() = default;
1055    void TakeSnapshot();
1056    char** GetSnapshot() const;
1057
1058   private:
1059    std::unique_ptr<char*[]> c_env_vector_;
1060    std::vector<std::unique_ptr<std::string>> name_value_pairs_;
1061
1062    DISALLOW_COPY_AND_ASSIGN(EnvSnapshot);
1063  } env_snapshot_;
1064
1065  // Generic system-weak holders.
1066  std::vector<gc::AbstractSystemWeakHolder*> system_weak_holders_;
1067
1068  std::unique_ptr<RuntimeCallbacks> callbacks_;
1069
1070  std::atomic<uint32_t> deoptimization_counts_[
1071      static_cast<uint32_t>(DeoptimizationKind::kLast) + 1];
1072
1073  std::unique_ptr<MemMap> protected_fault_page_;
1074
1075  DISALLOW_COPY_AND_ASSIGN(Runtime);
1076};
1077
1078}  // namespace art
1079
1080#endif  // ART_RUNTIME_RUNTIME_H_
1081