thread.cc revision a1b94c6d06e3cf2aa4f3aa42a1133995a4d8a1fb
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#define ATRACE_TAG ATRACE_TAG_DALVIK
18
19#include "thread.h"
20
21#include <cutils/trace.h>
22#include <pthread.h>
23#include <signal.h>
24#include <sys/resource.h>
25#include <sys/time.h>
26
27#include <algorithm>
28#include <bitset>
29#include <cerrno>
30#include <iostream>
31#include <list>
32#include <sstream>
33
34#include "arch/context.h"
35#include "base/mutex.h"
36#include "base/to_str.h"
37#include "class_linker-inl.h"
38#include "class_linker.h"
39#include "debugger.h"
40#include "dex_file-inl.h"
41#include "entrypoints/entrypoint_utils.h"
42#include "entrypoints/quick/quick_alloc_entrypoints.h"
43#include "gc_map.h"
44#include "gc/accounting/card_table-inl.h"
45#include "gc/allocator/rosalloc.h"
46#include "gc/heap.h"
47#include "gc/space/space.h"
48#include "handle_scope-inl.h"
49#include "handle_scope.h"
50#include "indirect_reference_table-inl.h"
51#include "jni_internal.h"
52#include "mirror/art_field-inl.h"
53#include "mirror/art_method-inl.h"
54#include "mirror/class_loader.h"
55#include "mirror/class-inl.h"
56#include "mirror/object_array-inl.h"
57#include "mirror/stack_trace_element.h"
58#include "monitor.h"
59#include "object_lock.h"
60#include "quick_exception_handler.h"
61#include "quick/quick_method_frame_info.h"
62#include "reflection.h"
63#include "runtime.h"
64#include "scoped_thread_state_change.h"
65#include "ScopedLocalRef.h"
66#include "ScopedUtfChars.h"
67#include "stack.h"
68#include "thread_list.h"
69#include "thread-inl.h"
70#include "utils.h"
71#include "verifier/dex_gc_map.h"
72#include "verify_object-inl.h"
73#include "vmap_table.h"
74#include "well_known_classes.h"
75
76namespace art {
77
78bool Thread::is_started_ = false;
79pthread_key_t Thread::pthread_key_self_;
80ConditionVariable* Thread::resume_cond_ = nullptr;
81const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
82
83static const char* kThreadNameDuringStartup = "<native thread without managed peer>";
84
85void Thread::InitCardTable() {
86  tlsPtr_.card_table = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin();
87}
88
89static void UnimplementedEntryPoint() {
90  UNIMPLEMENTED(FATAL);
91}
92
93void InitEntryPoints(InterpreterEntryPoints* ipoints, JniEntryPoints* jpoints,
94                     QuickEntryPoints* qpoints);
95
96void Thread::InitTlsEntryPoints() {
97  // Insert a placeholder so we can easily tell if we call an unimplemented entry point.
98  uintptr_t* begin = reinterpret_cast<uintptr_t*>(&tlsPtr_.interpreter_entrypoints);
99  uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(&tlsPtr_.quick_entrypoints) +
100      sizeof(tlsPtr_.quick_entrypoints));
101  for (uintptr_t* it = begin; it != end; ++it) {
102    *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint);
103  }
104  InitEntryPoints(&tlsPtr_.interpreter_entrypoints, &tlsPtr_.jni_entrypoints,
105                  &tlsPtr_.quick_entrypoints);
106}
107
108void Thread::ResetQuickAllocEntryPointsForThread() {
109  ResetQuickAllocEntryPoints(&tlsPtr_.quick_entrypoints);
110}
111
112void Thread::SetDeoptimizationShadowFrame(ShadowFrame* sf) {
113  tlsPtr_.deoptimization_shadow_frame = sf;
114}
115
116void Thread::SetDeoptimizationReturnValue(const JValue& ret_val) {
117  tls64_.deoptimization_return_value.SetJ(ret_val.GetJ());
118}
119
120ShadowFrame* Thread::GetAndClearDeoptimizationShadowFrame(JValue* ret_val) {
121  ShadowFrame* sf = tlsPtr_.deoptimization_shadow_frame;
122  tlsPtr_.deoptimization_shadow_frame = nullptr;
123  ret_val->SetJ(tls64_.deoptimization_return_value.GetJ());
124  return sf;
125}
126
127void Thread::SetShadowFrameUnderConstruction(ShadowFrame* sf) {
128  sf->SetLink(tlsPtr_.shadow_frame_under_construction);
129  tlsPtr_.shadow_frame_under_construction = sf;
130}
131
132void Thread::ClearShadowFrameUnderConstruction() {
133  CHECK_NE(static_cast<ShadowFrame*>(nullptr), tlsPtr_.shadow_frame_under_construction);
134  tlsPtr_.shadow_frame_under_construction = tlsPtr_.shadow_frame_under_construction->GetLink();
135}
136
137void Thread::InitTid() {
138  tls32_.tid = ::art::GetTid();
139}
140
141void Thread::InitAfterFork() {
142  // One thread (us) survived the fork, but we have a new tid so we need to
143  // update the value stashed in this Thread*.
144  InitTid();
145}
146
147void* Thread::CreateCallback(void* arg) {
148  Thread* self = reinterpret_cast<Thread*>(arg);
149  Runtime* runtime = Runtime::Current();
150  if (runtime == nullptr) {
151    LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self;
152    return nullptr;
153  }
154  {
155    // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true
156    //       after self->Init().
157    MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
158    // Check that if we got here we cannot be shutting down (as shutdown should never have started
159    // while threads are being born).
160    CHECK(!runtime->IsShuttingDownLocked());
161    CHECK(self->Init(runtime->GetThreadList(), runtime->GetJavaVM()));
162    Runtime::Current()->EndThreadBirth();
163  }
164  {
165    ScopedObjectAccess soa(self);
166
167    // Copy peer into self, deleting global reference when done.
168    CHECK(self->tlsPtr_.jpeer != nullptr);
169    self->tlsPtr_.opeer = soa.Decode<mirror::Object*>(self->tlsPtr_.jpeer);
170    self->GetJniEnv()->DeleteGlobalRef(self->tlsPtr_.jpeer);
171    self->tlsPtr_.jpeer = nullptr;
172    self->SetThreadName(self->GetThreadName(soa)->ToModifiedUtf8().c_str());
173
174    mirror::ArtField* priorityField = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority);
175    self->SetNativePriority(priorityField->GetInt(self->tlsPtr_.opeer));
176    Dbg::PostThreadStart(self);
177
178    // Invoke the 'run' method of our java.lang.Thread.
179    mirror::Object* receiver = self->tlsPtr_.opeer;
180    jmethodID mid = WellKnownClasses::java_lang_Thread_run;
181    InvokeVirtualOrInterfaceWithJValues(soa, receiver, mid, nullptr);
182  }
183  // Detach and delete self.
184  Runtime::Current()->GetThreadList()->Unregister(self);
185
186  return nullptr;
187}
188
189Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
190                                  mirror::Object* thread_peer) {
191  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer);
192  Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetLong(thread_peer)));
193  // Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_
194  // to stop it from going away.
195  if (kIsDebugBuild) {
196    MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
197    if (result != nullptr && !result->IsSuspended()) {
198      Locks::thread_list_lock_->AssertHeld(soa.Self());
199    }
200  }
201  return result;
202}
203
204Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
205                                  jobject java_thread) {
206  return FromManagedThread(soa, soa.Decode<mirror::Object*>(java_thread));
207}
208
209static size_t FixStackSize(size_t stack_size) {
210  // A stack size of zero means "use the default".
211  if (stack_size == 0) {
212    stack_size = Runtime::Current()->GetDefaultStackSize();
213  }
214
215  // Dalvik used the bionic pthread default stack size for native threads,
216  // so include that here to support apps that expect large native stacks.
217  stack_size += 1 * MB;
218
219  // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN.
220  if (stack_size < PTHREAD_STACK_MIN) {
221    stack_size = PTHREAD_STACK_MIN;
222  }
223
224  if (Runtime::Current()->ExplicitStackOverflowChecks()) {
225    // It's likely that callers are trying to ensure they have at least a certain amount of
226    // stack space, so we should add our reserved space on top of what they requested, rather
227    // than implicitly take it away from them.
228    stack_size += GetStackOverflowReservedBytes(kRuntimeISA);
229  } else {
230    // If we are going to use implicit stack checks, allocate space for the protected
231    // region at the bottom of the stack.
232    stack_size += Thread::kStackOverflowImplicitCheckSize +
233        GetStackOverflowReservedBytes(kRuntimeISA);
234  }
235
236  // Some systems require the stack size to be a multiple of the system page size, so round up.
237  stack_size = RoundUp(stack_size, kPageSize);
238
239  return stack_size;
240}
241
242// Global variable to prevent the compiler optimizing away the page reads for the stack.
243uint8_t dont_optimize_this;
244
245// Install a protected region in the stack.  This is used to trigger a SIGSEGV if a stack
246// overflow is detected.  It is located right below the stack_begin_.
247//
248// There is a little complexity here that deserves a special mention.  On some
249// architectures, the stack created using a VM_GROWSDOWN flag
250// to prevent memory being allocated when it's not needed.  This flag makes the
251// kernel only allocate memory for the stack by growing down in memory.  Because we
252// want to put an mprotected region far away from that at the stack top, we need
253// to make sure the pages for the stack are mapped in before we call mprotect.  We do
254// this by reading every page from the stack bottom (highest address) to the stack top.
255// We then madvise this away.
256void Thread::InstallImplicitProtection() {
257  uint8_t* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
258  uint8_t* stack_himem = tlsPtr_.stack_end;
259  uint8_t* stack_top = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(&stack_himem) &
260      ~(kPageSize - 1));    // Page containing current top of stack.
261
262  // First remove the protection on the protected region as will want to read and
263  // write it.  This may fail (on the first attempt when the stack is not mapped)
264  // but we ignore that.
265  UnprotectStack();
266
267  // Map in the stack.  This must be done by reading from the
268  // current stack pointer downwards as the stack may be mapped using VM_GROWSDOWN
269  // in the kernel.  Any access more than a page below the current SP might cause
270  // a segv.
271
272  // Read every page from the high address to the low.
273  for (uint8_t* p = stack_top; p >= pregion; p -= kPageSize) {
274    dont_optimize_this = *p;
275  }
276
277  VLOG(threads) << "installing stack protected region at " << std::hex <<
278      static_cast<void*>(pregion) << " to " <<
279      static_cast<void*>(pregion + kStackOverflowProtectedSize - 1);
280
281  // Protect the bottom of the stack to prevent read/write to it.
282  ProtectStack();
283
284  // Tell the kernel that we won't be needing these pages any more.
285  // NB. madvise will probably write zeroes into the memory (on linux it does).
286  uint32_t unwanted_size = stack_top - pregion - kPageSize;
287  madvise(pregion, unwanted_size, MADV_DONTNEED);
288}
289
290void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
291  CHECK(java_peer != nullptr);
292  Thread* self = static_cast<JNIEnvExt*>(env)->self;
293  Runtime* runtime = Runtime::Current();
294
295  // Atomically start the birth of the thread ensuring the runtime isn't shutting down.
296  bool thread_start_during_shutdown = false;
297  {
298    MutexLock mu(self, *Locks::runtime_shutdown_lock_);
299    if (runtime->IsShuttingDownLocked()) {
300      thread_start_during_shutdown = true;
301    } else {
302      runtime->StartThreadBirth();
303    }
304  }
305  if (thread_start_during_shutdown) {
306    ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError"));
307    env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown");
308    return;
309  }
310
311  Thread* child_thread = new Thread(is_daemon);
312  // Use global JNI ref to hold peer live while child thread starts.
313  child_thread->tlsPtr_.jpeer = env->NewGlobalRef(java_peer);
314  stack_size = FixStackSize(stack_size);
315
316  // Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing to
317  // assign it.
318  env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer,
319                    reinterpret_cast<jlong>(child_thread));
320
321  pthread_t new_pthread;
322  pthread_attr_t attr;
323  CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
324  CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED");
325  CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
326  int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread);
327  CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
328
329  if (pthread_create_result != 0) {
330    // pthread_create(3) failed, so clean up.
331    {
332      MutexLock mu(self, *Locks::runtime_shutdown_lock_);
333      runtime->EndThreadBirth();
334    }
335    // Manually delete the global reference since Thread::Init will not have been run.
336    env->DeleteGlobalRef(child_thread->tlsPtr_.jpeer);
337    child_thread->tlsPtr_.jpeer = nullptr;
338    delete child_thread;
339    child_thread = nullptr;
340    // TODO: remove from thread group?
341    env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0);
342    {
343      std::string msg(StringPrintf("pthread_create (%s stack) failed: %s",
344                                   PrettySize(stack_size).c_str(), strerror(pthread_create_result)));
345      ScopedObjectAccess soa(env);
346      soa.Self()->ThrowOutOfMemoryError(msg.c_str());
347    }
348  }
349}
350
351bool Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) {
352  // This function does all the initialization that must be run by the native thread it applies to.
353  // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so
354  // we can handshake with the corresponding native thread when it's ready.) Check this native
355  // thread hasn't been through here already...
356  CHECK(Thread::Current() == nullptr);
357
358  // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this
359  // avoids pthread_self_ ever being invalid when discovered from Thread::Current().
360  tlsPtr_.pthread_self = pthread_self();
361  CHECK(is_started_);
362
363  SetUpAlternateSignalStack();
364  if (!InitStackHwm()) {
365    return false;
366  }
367  InitCpu();
368  InitTlsEntryPoints();
369  RemoveSuspendTrigger();
370  InitCardTable();
371  InitTid();
372
373  CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self");
374  DCHECK_EQ(Thread::Current(), this);
375
376  tls32_.thin_lock_thread_id = thread_list->AllocThreadId(this);
377
378  tlsPtr_.jni_env = new JNIEnvExt(this, java_vm);
379  thread_list->Register(this);
380  return true;
381}
382
383Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group,
384                       bool create_peer) {
385  Runtime* runtime = Runtime::Current();
386  if (runtime == nullptr) {
387    LOG(ERROR) << "Thread attaching to non-existent runtime: " << thread_name;
388    return nullptr;
389  }
390  Thread* self;
391  {
392    MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
393    if (runtime->IsShuttingDownLocked()) {
394      LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name;
395      return nullptr;
396    } else {
397      Runtime::Current()->StartThreadBirth();
398      self = new Thread(as_daemon);
399      bool init_success = self->Init(runtime->GetThreadList(), runtime->GetJavaVM());
400      Runtime::Current()->EndThreadBirth();
401      if (!init_success) {
402        delete self;
403        return nullptr;
404      }
405    }
406  }
407
408  CHECK_NE(self->GetState(), kRunnable);
409  self->SetState(kNative);
410
411  // If we're the main thread, ClassLinker won't be created until after we're attached,
412  // so that thread needs a two-stage attach. Regular threads don't need this hack.
413  // In the compiler, all threads need this hack, because no-one's going to be getting
414  // a native peer!
415  if (create_peer) {
416    self->CreatePeer(thread_name, as_daemon, thread_group);
417  } else {
418    // These aren't necessary, but they improve diagnostics for unit tests & command-line tools.
419    if (thread_name != nullptr) {
420      self->tlsPtr_.name->assign(thread_name);
421      ::art::SetThreadName(thread_name);
422    } else if (self->GetJniEnv()->check_jni) {
423      LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
424    }
425  }
426
427  {
428    ScopedObjectAccess soa(self);
429    Dbg::PostThreadStart(self);
430  }
431
432  return self;
433}
434
435void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) {
436  Runtime* runtime = Runtime::Current();
437  CHECK(runtime->IsStarted());
438  JNIEnv* env = tlsPtr_.jni_env;
439
440  if (thread_group == nullptr) {
441    thread_group = runtime->GetMainThreadGroup();
442  }
443  ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
444  // Add missing null check in case of OOM b/18297817
445  if (name != nullptr && thread_name.get() == nullptr) {
446    CHECK(IsExceptionPending());
447    return;
448  }
449  jint thread_priority = GetNativePriority();
450  jboolean thread_is_daemon = as_daemon;
451
452  ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
453  if (peer.get() == nullptr) {
454    CHECK(IsExceptionPending());
455    return;
456  }
457  {
458    ScopedObjectAccess soa(this);
459    tlsPtr_.opeer = soa.Decode<mirror::Object*>(peer.get());
460  }
461  env->CallNonvirtualVoidMethod(peer.get(),
462                                WellKnownClasses::java_lang_Thread,
463                                WellKnownClasses::java_lang_Thread_init,
464                                thread_group, thread_name.get(), thread_priority, thread_is_daemon);
465  AssertNoPendingException();
466
467  Thread* self = this;
468  DCHECK_EQ(self, Thread::Current());
469  env->SetLongField(peer.get(), WellKnownClasses::java_lang_Thread_nativePeer,
470                    reinterpret_cast<jlong>(self));
471
472  ScopedObjectAccess soa(self);
473  StackHandleScope<1> hs(self);
474  MutableHandle<mirror::String> peer_thread_name(hs.NewHandle(GetThreadName(soa)));
475  if (peer_thread_name.Get() == nullptr) {
476    // The Thread constructor should have set the Thread.name to a
477    // non-null value. However, because we can run without code
478    // available (in the compiler, in tests), we manually assign the
479    // fields the constructor should have set.
480    if (runtime->IsActiveTransaction()) {
481      InitPeer<true>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority);
482    } else {
483      InitPeer<false>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority);
484    }
485    peer_thread_name.Assign(GetThreadName(soa));
486  }
487  // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null.
488  if (peer_thread_name.Get() != nullptr) {
489    SetThreadName(peer_thread_name->ToModifiedUtf8().c_str());
490  }
491}
492
493template<bool kTransactionActive>
494void Thread::InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group,
495                      jobject thread_name, jint thread_priority) {
496  soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->
497      SetBoolean<kTransactionActive>(tlsPtr_.opeer, thread_is_daemon);
498  soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->
499      SetObject<kTransactionActive>(tlsPtr_.opeer, soa.Decode<mirror::Object*>(thread_group));
500  soa.DecodeField(WellKnownClasses::java_lang_Thread_name)->
501      SetObject<kTransactionActive>(tlsPtr_.opeer, soa.Decode<mirror::Object*>(thread_name));
502  soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->
503      SetInt<kTransactionActive>(tlsPtr_.opeer, thread_priority);
504}
505
506void Thread::SetThreadName(const char* name) {
507  tlsPtr_.name->assign(name);
508  ::art::SetThreadName(name);
509  Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM"));
510}
511
512bool Thread::InitStackHwm() {
513  void* read_stack_base;
514  size_t read_stack_size;
515  size_t read_guard_size;
516  GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size, &read_guard_size);
517
518  // This is included in the SIGQUIT output, but it's useful here for thread debugging.
519  VLOG(threads) << StringPrintf("Native stack is at %p (%s with %s guard)",
520                                read_stack_base,
521                                PrettySize(read_stack_size).c_str(),
522                                PrettySize(read_guard_size).c_str());
523
524  tlsPtr_.stack_begin = reinterpret_cast<uint8_t*>(read_stack_base);
525  tlsPtr_.stack_size = read_stack_size;
526
527  // The minimum stack size we can cope with is the overflow reserved bytes (typically
528  // 8K) + the protected region size (4K) + another page (4K).  Typically this will
529  // be 8+4+4 = 16K.  The thread won't be able to do much with this stack even the GC takes
530  // between 8K and 12K.
531  uint32_t min_stack = GetStackOverflowReservedBytes(kRuntimeISA) + kStackOverflowProtectedSize
532    + 4 * KB;
533  if (read_stack_size <= min_stack) {
534    // Note, as we know the stack is small, avoid operations that could use a lot of stack.
535    LogMessage::LogLineLowStack(__PRETTY_FUNCTION__, __LINE__, ERROR,
536                                "Attempt to attach a thread with a too-small stack");
537    return false;
538  }
539
540  // Set stack_end_ to the bottom of the stack saving space of stack overflows
541
542  Runtime* runtime = Runtime::Current();
543  bool implicit_stack_check = !runtime->ExplicitStackOverflowChecks() && !runtime->IsCompiler();
544  ResetDefaultStackEnd();
545
546  // Install the protected region if we are doing implicit overflow checks.
547  if (implicit_stack_check) {
548    // The thread might have protected region at the bottom.  We need
549    // to install our own region so we need to move the limits
550    // of the stack to make room for it.
551
552    tlsPtr_.stack_begin += read_guard_size + kStackOverflowProtectedSize;
553    tlsPtr_.stack_end += read_guard_size + kStackOverflowProtectedSize;
554    tlsPtr_.stack_size -= read_guard_size;
555
556    InstallImplicitProtection();
557  }
558
559  // Sanity check.
560  int stack_variable;
561  CHECK_GT(&stack_variable, reinterpret_cast<void*>(tlsPtr_.stack_end));
562
563  return true;
564}
565
566void Thread::ShortDump(std::ostream& os) const {
567  os << "Thread[";
568  if (GetThreadId() != 0) {
569    // If we're in kStarting, we won't have a thin lock id or tid yet.
570    os << GetThreadId()
571             << ",tid=" << GetTid() << ',';
572  }
573  os << GetState()
574           << ",Thread*=" << this
575           << ",peer=" << tlsPtr_.opeer
576           << ",\"" << *tlsPtr_.name << "\""
577           << "]";
578}
579
580void Thread::Dump(std::ostream& os) const {
581  DumpState(os);
582  DumpStack(os);
583}
584
585mirror::String* Thread::GetThreadName(const ScopedObjectAccessAlreadyRunnable& soa) const {
586  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
587  return (tlsPtr_.opeer != nullptr) ? reinterpret_cast<mirror::String*>(f->GetObject(tlsPtr_.opeer)) : nullptr;
588}
589
590void Thread::GetThreadName(std::string& name) const {
591  name.assign(*tlsPtr_.name);
592}
593
594uint64_t Thread::GetCpuMicroTime() const {
595#if defined(HAVE_POSIX_CLOCKS)
596  clockid_t cpu_clock_id;
597  pthread_getcpuclockid(tlsPtr_.pthread_self, &cpu_clock_id);
598  timespec now;
599  clock_gettime(cpu_clock_id, &now);
600  return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000);
601#else
602  UNIMPLEMENTED(WARNING);
603  return -1;
604#endif
605}
606
607// Attempt to rectify locks so that we dump thread list with required locks before exiting.
608static void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
609  LOG(ERROR) << *thread << " suspend count already zero.";
610  Locks::thread_suspend_count_lock_->Unlock(self);
611  if (!Locks::mutator_lock_->IsSharedHeld(self)) {
612    Locks::mutator_lock_->SharedTryLock(self);
613    if (!Locks::mutator_lock_->IsSharedHeld(self)) {
614      LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
615    }
616  }
617  if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
618    Locks::thread_list_lock_->TryLock(self);
619    if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
620      LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
621    }
622  }
623  std::ostringstream ss;
624  Runtime::Current()->GetThreadList()->Dump(ss);
625  LOG(FATAL) << ss.str();
626}
627
628void Thread::ModifySuspendCount(Thread* self, int delta, bool for_debugger) {
629  if (kIsDebugBuild) {
630    DCHECK(delta == -1 || delta == +1 || delta == -tls32_.debug_suspend_count)
631          << delta << " " << tls32_.debug_suspend_count << " " << this;
632    DCHECK_GE(tls32_.suspend_count, tls32_.debug_suspend_count) << this;
633    Locks::thread_suspend_count_lock_->AssertHeld(self);
634    if (this != self && !IsSuspended()) {
635      Locks::thread_list_lock_->AssertHeld(self);
636    }
637  }
638  if (UNLIKELY(delta < 0 && tls32_.suspend_count <= 0)) {
639    UnsafeLogFatalForSuspendCount(self, this);
640    return;
641  }
642
643  tls32_.suspend_count += delta;
644  if (for_debugger) {
645    tls32_.debug_suspend_count += delta;
646  }
647
648  if (tls32_.suspend_count == 0) {
649    AtomicClearFlag(kSuspendRequest);
650  } else {
651    AtomicSetFlag(kSuspendRequest);
652    TriggerSuspend();
653  }
654}
655
656void Thread::RunCheckpointFunction() {
657  Closure *checkpoints[kMaxCheckpoints];
658
659  // Grab the suspend_count lock and copy the current set of
660  // checkpoints.  Then clear the list and the flag.  The RequestCheckpoint
661  // function will also grab this lock so we prevent a race between setting
662  // the kCheckpointRequest flag and clearing it.
663  {
664    MutexLock mu(this, *Locks::thread_suspend_count_lock_);
665    for (uint32_t i = 0; i < kMaxCheckpoints; ++i) {
666      checkpoints[i] = tlsPtr_.checkpoint_functions[i];
667      tlsPtr_.checkpoint_functions[i] = nullptr;
668    }
669    AtomicClearFlag(kCheckpointRequest);
670  }
671
672  // Outside the lock, run all the checkpoint functions that
673  // we collected.
674  bool found_checkpoint = false;
675  for (uint32_t i = 0; i < kMaxCheckpoints; ++i) {
676    if (checkpoints[i] != nullptr) {
677      ATRACE_BEGIN("Checkpoint function");
678      checkpoints[i]->Run(this);
679      ATRACE_END();
680      found_checkpoint = true;
681    }
682  }
683  CHECK(found_checkpoint);
684}
685
686bool Thread::RequestCheckpoint(Closure* function) {
687  union StateAndFlags old_state_and_flags;
688  old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
689  if (old_state_and_flags.as_struct.state != kRunnable) {
690    return false;  // Fail, thread is suspended and so can't run a checkpoint.
691  }
692
693  uint32_t available_checkpoint = kMaxCheckpoints;
694  for (uint32_t i = 0 ; i < kMaxCheckpoints; ++i) {
695    if (tlsPtr_.checkpoint_functions[i] == nullptr) {
696      available_checkpoint = i;
697      break;
698    }
699  }
700  if (available_checkpoint == kMaxCheckpoints) {
701    // No checkpoint functions available, we can't run a checkpoint
702    return false;
703  }
704  tlsPtr_.checkpoint_functions[available_checkpoint] = function;
705
706  // Checkpoint function installed now install flag bit.
707  // We must be runnable to request a checkpoint.
708  DCHECK_EQ(old_state_and_flags.as_struct.state, kRunnable);
709  union StateAndFlags new_state_and_flags;
710  new_state_and_flags.as_int = old_state_and_flags.as_int;
711  new_state_and_flags.as_struct.flags |= kCheckpointRequest;
712  bool success =
713      tls32_.state_and_flags.as_atomic_int.CompareExchangeStrongSequentiallyConsistent(old_state_and_flags.as_int,
714                                                                                       new_state_and_flags.as_int);
715  if (UNLIKELY(!success)) {
716    // The thread changed state before the checkpoint was installed.
717    CHECK_EQ(tlsPtr_.checkpoint_functions[available_checkpoint], function);
718    tlsPtr_.checkpoint_functions[available_checkpoint] = nullptr;
719  } else {
720    CHECK_EQ(ReadFlag(kCheckpointRequest), true);
721    TriggerSuspend();
722  }
723  return success;
724}
725
726void Thread::FullSuspendCheck() {
727  VLOG(threads) << this << " self-suspending";
728  ATRACE_BEGIN("Full suspend check");
729  // Make thread appear suspended to other threads, release mutator_lock_.
730  TransitionFromRunnableToSuspended(kSuspended);
731  // Transition back to runnable noting requests to suspend, re-acquire share on mutator_lock_.
732  TransitionFromSuspendedToRunnable();
733  ATRACE_END();
734  VLOG(threads) << this << " self-reviving";
735}
736
737void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) {
738  std::string group_name;
739  int priority;
740  bool is_daemon = false;
741  Thread* self = Thread::Current();
742
743  // Don't do this if we are aborting since the GC may have all the threads suspended. This will
744  // cause ScopedObjectAccessUnchecked to deadlock.
745  if (gAborting == 0 && self != nullptr && thread != nullptr && thread->tlsPtr_.opeer != nullptr) {
746    ScopedObjectAccessUnchecked soa(self);
747    priority = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)
748        ->GetInt(thread->tlsPtr_.opeer);
749    is_daemon = soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)
750        ->GetBoolean(thread->tlsPtr_.opeer);
751
752    mirror::Object* thread_group =
753        soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(thread->tlsPtr_.opeer);
754
755    if (thread_group != nullptr) {
756      mirror::ArtField* group_name_field =
757          soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name);
758      mirror::String* group_name_string =
759          reinterpret_cast<mirror::String*>(group_name_field->GetObject(thread_group));
760      group_name = (group_name_string != nullptr) ? group_name_string->ToModifiedUtf8() : "<null>";
761    }
762  } else {
763    priority = GetNativePriority();
764  }
765
766  std::string scheduler_group_name(GetSchedulerGroupName(tid));
767  if (scheduler_group_name.empty()) {
768    scheduler_group_name = "default";
769  }
770
771  if (thread != nullptr) {
772    os << '"' << *thread->tlsPtr_.name << '"';
773    if (is_daemon) {
774      os << " daemon";
775    }
776    os << " prio=" << priority
777       << " tid=" << thread->GetThreadId()
778       << " " << thread->GetState();
779    if (thread->IsStillStarting()) {
780      os << " (still starting up)";
781    }
782    os << "\n";
783  } else {
784    os << '"' << ::art::GetThreadName(tid) << '"'
785       << " prio=" << priority
786       << " (not attached)\n";
787  }
788
789  if (thread != nullptr) {
790    MutexLock mu(self, *Locks::thread_suspend_count_lock_);
791    os << "  | group=\"" << group_name << "\""
792       << " sCount=" << thread->tls32_.suspend_count
793       << " dsCount=" << thread->tls32_.debug_suspend_count
794       << " obj=" << reinterpret_cast<void*>(thread->tlsPtr_.opeer)
795       << " self=" << reinterpret_cast<const void*>(thread) << "\n";
796  }
797
798  os << "  | sysTid=" << tid
799     << " nice=" << getpriority(PRIO_PROCESS, tid)
800     << " cgrp=" << scheduler_group_name;
801  if (thread != nullptr) {
802    int policy;
803    sched_param sp;
804    CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp),
805                       __FUNCTION__);
806    os << " sched=" << policy << "/" << sp.sched_priority
807       << " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self);
808  }
809  os << "\n";
810
811  // Grab the scheduler stats for this thread.
812  std::string scheduler_stats;
813  if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) {
814    scheduler_stats.resize(scheduler_stats.size() - 1);  // Lose the trailing '\n'.
815  } else {
816    scheduler_stats = "0 0 0";
817  }
818
819  char native_thread_state = '?';
820  int utime = 0;
821  int stime = 0;
822  int task_cpu = 0;
823  GetTaskStats(tid, &native_thread_state, &utime, &stime, &task_cpu);
824
825  os << "  | state=" << native_thread_state
826     << " schedstat=( " << scheduler_stats << " )"
827     << " utm=" << utime
828     << " stm=" << stime
829     << " core=" << task_cpu
830     << " HZ=" << sysconf(_SC_CLK_TCK) << "\n";
831  if (thread != nullptr) {
832    os << "  | stack=" << reinterpret_cast<void*>(thread->tlsPtr_.stack_begin) << "-"
833        << reinterpret_cast<void*>(thread->tlsPtr_.stack_end) << " stackSize="
834        << PrettySize(thread->tlsPtr_.stack_size) << "\n";
835    // Dump the held mutexes.
836    os << "  | held mutexes=";
837    for (size_t i = 0; i < kLockLevelCount; ++i) {
838      if (i != kMonitorLock) {
839        BaseMutex* mutex = thread->GetHeldMutex(static_cast<LockLevel>(i));
840        if (mutex != nullptr) {
841          os << " \"" << mutex->GetName() << "\"";
842          if (mutex->IsReaderWriterMutex()) {
843            ReaderWriterMutex* rw_mutex = down_cast<ReaderWriterMutex*>(mutex);
844            if (rw_mutex->GetExclusiveOwnerTid() == static_cast<uint64_t>(tid)) {
845              os << "(exclusive held)";
846            } else {
847              os << "(shared held)";
848            }
849          }
850        }
851      }
852    }
853    os << "\n";
854  }
855}
856
857void Thread::DumpState(std::ostream& os) const {
858  Thread::DumpState(os, this, GetTid());
859}
860
861struct StackDumpVisitor : public StackVisitor {
862  StackDumpVisitor(std::ostream& os_in, Thread* thread_in, Context* context, bool can_allocate_in)
863      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
864      : StackVisitor(thread_in, context), os(os_in), thread(thread_in),
865        can_allocate(can_allocate_in), last_method(nullptr), last_line_number(0),
866        repetition_count(0), frame_count(0) {
867  }
868
869  virtual ~StackDumpVisitor() {
870    if (frame_count == 0) {
871      os << "  (no managed stack frames)\n";
872    }
873  }
874
875  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
876    mirror::ArtMethod* m = GetMethod();
877    if (m->IsRuntimeMethod()) {
878      return true;
879    }
880    const int kMaxRepetition = 3;
881    mirror::Class* c = m->GetDeclaringClass();
882    mirror::DexCache* dex_cache = c->GetDexCache();
883    int line_number = -1;
884    if (dex_cache != nullptr) {  // be tolerant of bad input
885      const DexFile& dex_file = *dex_cache->GetDexFile();
886      line_number = dex_file.GetLineNumFromPC(m, GetDexPc(false));
887    }
888    if (line_number == last_line_number && last_method == m) {
889      ++repetition_count;
890    } else {
891      if (repetition_count >= kMaxRepetition) {
892        os << "  ... repeated " << (repetition_count - kMaxRepetition) << " times\n";
893      }
894      repetition_count = 0;
895      last_line_number = line_number;
896      last_method = m;
897    }
898    if (repetition_count < kMaxRepetition) {
899      os << "  at " << PrettyMethod(m, false);
900      if (m->IsNative()) {
901        os << "(Native method)";
902      } else {
903        const char* source_file(m->GetDeclaringClassSourceFile());
904        os << "(" << (source_file != nullptr ? source_file : "unavailable")
905           << ":" << line_number << ")";
906      }
907      os << "\n";
908      if (frame_count == 0) {
909        Monitor::DescribeWait(os, thread);
910      }
911      if (can_allocate) {
912        // Visit locks, but do not abort on errors. This would trigger a nested abort.
913        Monitor::VisitLocks(this, DumpLockedObject, &os, false);
914      }
915    }
916
917    ++frame_count;
918    return true;
919  }
920
921  static void DumpLockedObject(mirror::Object* o, void* context)
922      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
923    std::ostream& os = *reinterpret_cast<std::ostream*>(context);
924    os << "  - locked ";
925    if (o == nullptr) {
926      os << "an unknown object";
927    } else {
928      if ((o->GetLockWord(false).GetState() == LockWord::kThinLocked) &&
929          Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
930        // Getting the identity hashcode here would result in lock inflation and suspension of the
931        // current thread, which isn't safe if this is the only runnable thread.
932        os << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", reinterpret_cast<intptr_t>(o),
933                           PrettyTypeOf(o).c_str());
934      } else {
935        os << StringPrintf("<0x%08x> (a %s)", o->IdentityHashCode(), PrettyTypeOf(o).c_str());
936      }
937    }
938    os << "\n";
939  }
940
941  std::ostream& os;
942  const Thread* thread;
943  const bool can_allocate;
944  mirror::ArtMethod* last_method;
945  int last_line_number;
946  int repetition_count;
947  int frame_count;
948};
949
950static bool ShouldShowNativeStack(const Thread* thread)
951    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
952  ThreadState state = thread->GetState();
953
954  // In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting.
955  if (state > kWaiting && state < kStarting) {
956    return true;
957  }
958
959  // In an Object.wait variant or Thread.sleep? That's not interesting.
960  if (state == kTimedWaiting || state == kSleeping || state == kWaiting) {
961    return false;
962  }
963
964  // Threads with no managed stack frames should be shown.
965  const ManagedStack* managed_stack = thread->GetManagedStack();
966  if (managed_stack == NULL || (managed_stack->GetTopQuickFrame() == NULL &&
967      managed_stack->GetTopShadowFrame() == NULL)) {
968    return true;
969  }
970
971  // In some other native method? That's interesting.
972  // We don't just check kNative because native methods will be in state kSuspended if they're
973  // calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the
974  // thread-startup states if it's early enough in their life cycle (http://b/7432159).
975  mirror::ArtMethod* current_method = thread->GetCurrentMethod(nullptr);
976  return current_method != nullptr && current_method->IsNative();
977}
978
979void Thread::DumpJavaStack(std::ostream& os) const {
980  // Dumping the Java stack involves the verifier for locks. The verifier operates under the
981  // assumption that there is no exception pending on entry. Thus, stash any pending exception.
982  // TODO: Find a way to avoid const_cast.
983  StackHandleScope<3> scope(const_cast<Thread*>(this));
984  Handle<mirror::Throwable> exc;
985  Handle<mirror::Object> throw_location_this_object;
986  Handle<mirror::ArtMethod> throw_location_method;
987  uint32_t throw_location_dex_pc;
988  bool have_exception = false;
989  if (IsExceptionPending()) {
990    ThrowLocation exc_location;
991    exc = scope.NewHandle(GetException(&exc_location));
992    throw_location_this_object = scope.NewHandle(exc_location.GetThis());
993    throw_location_method = scope.NewHandle(exc_location.GetMethod());
994    throw_location_dex_pc = exc_location.GetDexPc();
995    const_cast<Thread*>(this)->ClearException();
996    have_exception = true;
997  }
998
999  std::unique_ptr<Context> context(Context::Create());
1000  StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(),
1001                          !tls32_.throwing_OutOfMemoryError);
1002  dumper.WalkStack();
1003
1004  if (have_exception) {
1005    ThrowLocation exc_location(throw_location_this_object.Get(),
1006                               throw_location_method.Get(),
1007                               throw_location_dex_pc);
1008    const_cast<Thread*>(this)->SetException(exc_location, exc.Get());
1009  }
1010}
1011
1012void Thread::DumpStack(std::ostream& os) const {
1013  // TODO: we call this code when dying but may not have suspended the thread ourself. The
1014  //       IsSuspended check is therefore racy with the use for dumping (normally we inhibit
1015  //       the race with the thread_suspend_count_lock_).
1016  bool dump_for_abort = (gAborting > 0);
1017  bool safe_to_dump = (this == Thread::Current() || IsSuspended());
1018  if (!kIsDebugBuild) {
1019    // We always want to dump the stack for an abort, however, there is no point dumping another
1020    // thread's stack in debug builds where we'll hit the not suspended check in the stack walk.
1021    safe_to_dump = (safe_to_dump || dump_for_abort);
1022  }
1023  if (safe_to_dump) {
1024    // If we're currently in native code, dump that stack before dumping the managed stack.
1025    if (dump_for_abort || ShouldShowNativeStack(this)) {
1026      DumpKernelStack(os, GetTid(), "  kernel: ", false);
1027      DumpNativeStack(os, GetTid(), "  native: ", GetCurrentMethod(nullptr, !dump_for_abort));
1028    }
1029    DumpJavaStack(os);
1030  } else {
1031    os << "Not able to dump stack of thread that isn't suspended";
1032  }
1033}
1034
1035void Thread::ThreadExitCallback(void* arg) {
1036  Thread* self = reinterpret_cast<Thread*>(arg);
1037  if (self->tls32_.thread_exit_check_count == 0) {
1038    LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's "
1039        "going to use a pthread_key_create destructor?): " << *self;
1040    CHECK(is_started_);
1041    CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self");
1042    self->tls32_.thread_exit_check_count = 1;
1043  } else {
1044    LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self;
1045  }
1046}
1047
1048void Thread::Startup() {
1049  CHECK(!is_started_);
1050  is_started_ = true;
1051  {
1052    // MutexLock to keep annotalysis happy.
1053    //
1054    // Note we use nullptr for the thread because Thread::Current can
1055    // return garbage since (is_started_ == true) and
1056    // Thread::pthread_key_self_ is not yet initialized.
1057    // This was seen on glibc.
1058    MutexLock mu(nullptr, *Locks::thread_suspend_count_lock_);
1059    resume_cond_ = new ConditionVariable("Thread resumption condition variable",
1060                                         *Locks::thread_suspend_count_lock_);
1061  }
1062
1063  // Allocate a TLS slot.
1064  CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback),
1065                     "self key");
1066
1067  // Double-check the TLS slot allocation.
1068  if (pthread_getspecific(pthread_key_self_) != nullptr) {
1069    LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr";
1070  }
1071}
1072
1073void Thread::FinishStartup() {
1074  Runtime* runtime = Runtime::Current();
1075  CHECK(runtime->IsStarted());
1076
1077  // Finish attaching the main thread.
1078  ScopedObjectAccess soa(Thread::Current());
1079  Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup());
1080
1081  Runtime::Current()->GetClassLinker()->RunRootClinits();
1082}
1083
1084void Thread::Shutdown() {
1085  CHECK(is_started_);
1086  is_started_ = false;
1087  CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key");
1088  MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
1089  if (resume_cond_ != nullptr) {
1090    delete resume_cond_;
1091    resume_cond_ = nullptr;
1092  }
1093}
1094
1095Thread::Thread(bool daemon) : tls32_(daemon), wait_monitor_(nullptr), interrupted_(false) {
1096  wait_mutex_ = new Mutex("a thread wait mutex");
1097  wait_cond_ = new ConditionVariable("a thread wait condition variable", *wait_mutex_);
1098  tlsPtr_.debug_invoke_req = new DebugInvokeReq;
1099  tlsPtr_.single_step_control = new SingleStepControl;
1100  tlsPtr_.instrumentation_stack = new std::deque<instrumentation::InstrumentationStackFrame>;
1101  tlsPtr_.name = new std::string(kThreadNameDuringStartup);
1102  tlsPtr_.nested_signal_state = static_cast<jmp_buf*>(malloc(sizeof(jmp_buf)));
1103
1104  CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread);
1105  tls32_.state_and_flags.as_struct.flags = 0;
1106  tls32_.state_and_flags.as_struct.state = kNative;
1107  memset(&tlsPtr_.held_mutexes[0], 0, sizeof(tlsPtr_.held_mutexes));
1108  std::fill(tlsPtr_.rosalloc_runs,
1109            tlsPtr_.rosalloc_runs + kNumRosAllocThreadLocalSizeBrackets,
1110            gc::allocator::RosAlloc::GetDedicatedFullRun());
1111  for (uint32_t i = 0; i < kMaxCheckpoints; ++i) {
1112    tlsPtr_.checkpoint_functions[i] = nullptr;
1113  }
1114}
1115
1116bool Thread::IsStillStarting() const {
1117  // You might think you can check whether the state is kStarting, but for much of thread startup,
1118  // the thread is in kNative; it might also be in kVmWait.
1119  // You might think you can check whether the peer is nullptr, but the peer is actually created and
1120  // assigned fairly early on, and needs to be.
1121  // It turns out that the last thing to change is the thread name; that's a good proxy for "has
1122  // this thread _ever_ entered kRunnable".
1123  return (tlsPtr_.jpeer == nullptr && tlsPtr_.opeer == nullptr) ||
1124      (*tlsPtr_.name == kThreadNameDuringStartup);
1125}
1126
1127void Thread::AssertPendingException() const {
1128  if (UNLIKELY(!IsExceptionPending())) {
1129    LOG(FATAL) << "Pending exception expected.";
1130  }
1131}
1132
1133void Thread::AssertNoPendingException() const {
1134  if (UNLIKELY(IsExceptionPending())) {
1135    ScopedObjectAccess soa(Thread::Current());
1136    mirror::Throwable* exception = GetException(nullptr);
1137    LOG(FATAL) << "No pending exception expected: " << exception->Dump();
1138  }
1139}
1140
1141void Thread::AssertNoPendingExceptionForNewException(const char* msg) const {
1142  if (UNLIKELY(IsExceptionPending())) {
1143    ScopedObjectAccess soa(Thread::Current());
1144    mirror::Throwable* exception = GetException(nullptr);
1145    LOG(FATAL) << "Throwing new exception '" << msg << "' with unexpected pending exception: "
1146        << exception->Dump();
1147  }
1148}
1149
1150static void MonitorExitVisitor(mirror::Object** object, void* arg, uint32_t /*thread_id*/,
1151                               RootType /*root_type*/)
1152    NO_THREAD_SAFETY_ANALYSIS {
1153  Thread* self = reinterpret_cast<Thread*>(arg);
1154  mirror::Object* entered_monitor = *object;
1155  if (self->HoldsLock(entered_monitor)) {
1156    LOG(WARNING) << "Calling MonitorExit on object "
1157                 << object << " (" << PrettyTypeOf(entered_monitor) << ")"
1158                 << " left locked by native thread "
1159                 << *Thread::Current() << " which is detaching";
1160    entered_monitor->MonitorExit(self);
1161  }
1162}
1163
1164void Thread::Destroy() {
1165  Thread* self = this;
1166  DCHECK_EQ(self, Thread::Current());
1167
1168  if (tlsPtr_.jni_env != nullptr) {
1169    // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
1170    tlsPtr_.jni_env->monitors.VisitRoots(MonitorExitVisitor, self, 0, kRootVMInternal);
1171    // Release locally held global references which releasing may require the mutator lock.
1172    if (tlsPtr_.jpeer != nullptr) {
1173      // If pthread_create fails we don't have a jni env here.
1174      tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.jpeer);
1175      tlsPtr_.jpeer = nullptr;
1176    }
1177    if (tlsPtr_.class_loader_override != nullptr) {
1178      tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.class_loader_override);
1179      tlsPtr_.class_loader_override = nullptr;
1180    }
1181  }
1182
1183  if (tlsPtr_.opeer != nullptr) {
1184    ScopedObjectAccess soa(self);
1185    // We may need to call user-supplied managed code, do this before final clean-up.
1186    HandleUncaughtExceptions(soa);
1187    RemoveFromThreadGroup(soa);
1188
1189    // this.nativePeer = 0;
1190    if (Runtime::Current()->IsActiveTransaction()) {
1191      soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)
1192          ->SetLong<true>(tlsPtr_.opeer, 0);
1193    } else {
1194      soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)
1195          ->SetLong<false>(tlsPtr_.opeer, 0);
1196    }
1197    Dbg::PostThreadDeath(self);
1198
1199    // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone
1200    // who is waiting.
1201    mirror::Object* lock =
1202        soa.DecodeField(WellKnownClasses::java_lang_Thread_lock)->GetObject(tlsPtr_.opeer);
1203    // (This conditional is only needed for tests, where Thread.lock won't have been set.)
1204    if (lock != nullptr) {
1205      StackHandleScope<1> hs(self);
1206      Handle<mirror::Object> h_obj(hs.NewHandle(lock));
1207      ObjectLock<mirror::Object> locker(self, h_obj);
1208      locker.NotifyAll();
1209    }
1210    tlsPtr_.opeer = nullptr;
1211  }
1212
1213  {
1214    ScopedObjectAccess soa(self);
1215    Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this);
1216  }
1217}
1218
1219Thread::~Thread() {
1220  CHECK(tlsPtr_.class_loader_override == nullptr);
1221  CHECK(tlsPtr_.jpeer == nullptr);
1222  CHECK(tlsPtr_.opeer == nullptr);
1223  bool initialized = (tlsPtr_.jni_env != nullptr);  // Did Thread::Init run?
1224  if (initialized) {
1225    delete tlsPtr_.jni_env;
1226    tlsPtr_.jni_env = nullptr;
1227  }
1228  CHECK_NE(GetState(), kRunnable);
1229  CHECK_NE(ReadFlag(kCheckpointRequest), true);
1230  CHECK(tlsPtr_.checkpoint_functions[0] == nullptr);
1231  CHECK(tlsPtr_.checkpoint_functions[1] == nullptr);
1232  CHECK(tlsPtr_.checkpoint_functions[2] == nullptr);
1233
1234  // We may be deleting a still born thread.
1235  SetStateUnsafe(kTerminated);
1236
1237  delete wait_cond_;
1238  delete wait_mutex_;
1239
1240  if (tlsPtr_.long_jump_context != nullptr) {
1241    delete tlsPtr_.long_jump_context;
1242  }
1243
1244  if (initialized) {
1245    CleanupCpu();
1246  }
1247
1248  delete tlsPtr_.debug_invoke_req;
1249  delete tlsPtr_.single_step_control;
1250  delete tlsPtr_.instrumentation_stack;
1251  delete tlsPtr_.name;
1252  delete tlsPtr_.stack_trace_sample;
1253  free(tlsPtr_.nested_signal_state);
1254
1255  Runtime::Current()->GetHeap()->AssertThreadLocalBuffersAreRevoked(this);
1256
1257  TearDownAlternateSignalStack();
1258}
1259
1260void Thread::HandleUncaughtExceptions(ScopedObjectAccess& soa) {
1261  if (!IsExceptionPending()) {
1262    return;
1263  }
1264  ScopedLocalRef<jobject> peer(tlsPtr_.jni_env, soa.AddLocalReference<jobject>(tlsPtr_.opeer));
1265  ScopedThreadStateChange tsc(this, kNative);
1266
1267  // Get and clear the exception.
1268  ScopedLocalRef<jthrowable> exception(tlsPtr_.jni_env, tlsPtr_.jni_env->ExceptionOccurred());
1269  tlsPtr_.jni_env->ExceptionClear();
1270
1271  // If the thread has its own handler, use that.
1272  ScopedLocalRef<jobject> handler(tlsPtr_.jni_env,
1273                                  tlsPtr_.jni_env->GetObjectField(peer.get(),
1274                                      WellKnownClasses::java_lang_Thread_uncaughtHandler));
1275  if (handler.get() == nullptr) {
1276    // Otherwise use the thread group's default handler.
1277    handler.reset(tlsPtr_.jni_env->GetObjectField(peer.get(),
1278                                                  WellKnownClasses::java_lang_Thread_group));
1279  }
1280
1281  // Call the handler.
1282  tlsPtr_.jni_env->CallVoidMethod(handler.get(),
1283      WellKnownClasses::java_lang_Thread__UncaughtExceptionHandler_uncaughtException,
1284      peer.get(), exception.get());
1285
1286  // If the handler threw, clear that exception too.
1287  tlsPtr_.jni_env->ExceptionClear();
1288}
1289
1290void Thread::RemoveFromThreadGroup(ScopedObjectAccess& soa) {
1291  // this.group.removeThread(this);
1292  // group can be null if we're in the compiler or a test.
1293  mirror::Object* ogroup = soa.DecodeField(WellKnownClasses::java_lang_Thread_group)
1294      ->GetObject(tlsPtr_.opeer);
1295  if (ogroup != nullptr) {
1296    ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup));
1297    ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(tlsPtr_.opeer));
1298    ScopedThreadStateChange tsc(soa.Self(), kNative);
1299    tlsPtr_.jni_env->CallVoidMethod(group.get(),
1300                                    WellKnownClasses::java_lang_ThreadGroup_removeThread,
1301                                    peer.get());
1302  }
1303}
1304
1305size_t Thread::NumHandleReferences() {
1306  size_t count = 0;
1307  for (HandleScope* cur = tlsPtr_.top_handle_scope; cur != nullptr; cur = cur->GetLink()) {
1308    count += cur->NumberOfReferences();
1309  }
1310  return count;
1311}
1312
1313bool Thread::HandleScopeContains(jobject obj) const {
1314  StackReference<mirror::Object>* hs_entry =
1315      reinterpret_cast<StackReference<mirror::Object>*>(obj);
1316  for (HandleScope* cur = tlsPtr_.top_handle_scope; cur!= nullptr; cur = cur->GetLink()) {
1317    if (cur->Contains(hs_entry)) {
1318      return true;
1319    }
1320  }
1321  // JNI code invoked from portable code uses shadow frames rather than the handle scope.
1322  return tlsPtr_.managed_stack.ShadowFramesContain(hs_entry);
1323}
1324
1325void Thread::HandleScopeVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id) {
1326  for (HandleScope* cur = tlsPtr_.top_handle_scope; cur; cur = cur->GetLink()) {
1327    size_t num_refs = cur->NumberOfReferences();
1328    for (size_t j = 0; j < num_refs; ++j) {
1329      mirror::Object* object = cur->GetReference(j);
1330      if (object != nullptr) {
1331        mirror::Object* old_obj = object;
1332        visitor(&object, arg, thread_id, kRootNativeStack);
1333        if (old_obj != object) {
1334          cur->SetReference(j, object);
1335        }
1336      }
1337    }
1338  }
1339}
1340
1341mirror::Object* Thread::DecodeJObject(jobject obj) const {
1342  Locks::mutator_lock_->AssertSharedHeld(this);
1343  if (obj == nullptr) {
1344    return nullptr;
1345  }
1346  IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
1347  IndirectRefKind kind = GetIndirectRefKind(ref);
1348  mirror::Object* result;
1349  bool expect_null = false;
1350  // The "kinds" below are sorted by the frequency we expect to encounter them.
1351  if (kind == kLocal) {
1352    IndirectReferenceTable& locals = tlsPtr_.jni_env->locals;
1353    // Local references do not need a read barrier.
1354    result = locals.Get<kWithoutReadBarrier>(ref);
1355  } else if (kind == kHandleScopeOrInvalid) {
1356    // TODO: make stack indirect reference table lookup more efficient.
1357    // Check if this is a local reference in the handle scope.
1358    if (LIKELY(HandleScopeContains(obj))) {
1359      // Read from handle scope.
1360      result = reinterpret_cast<StackReference<mirror::Object>*>(obj)->AsMirrorPtr();
1361      VerifyObject(result);
1362    } else {
1363      tlsPtr_.jni_env->vm->JniAbortF(nullptr, "use of invalid jobject %p", obj);
1364      expect_null = true;
1365      result = nullptr;
1366    }
1367  } else if (kind == kGlobal) {
1368    result = tlsPtr_.jni_env->vm->DecodeGlobal(const_cast<Thread*>(this), ref);
1369  } else {
1370    DCHECK_EQ(kind, kWeakGlobal);
1371    result = tlsPtr_.jni_env->vm->DecodeWeakGlobal(const_cast<Thread*>(this), ref);
1372    if (Runtime::Current()->IsClearedJniWeakGlobal(result)) {
1373      // This is a special case where it's okay to return nullptr.
1374      expect_null = true;
1375      result = nullptr;
1376    }
1377  }
1378
1379  if (UNLIKELY(!expect_null && result == nullptr)) {
1380    tlsPtr_.jni_env->vm->JniAbortF(nullptr, "use of deleted %s %p",
1381                                   ToStr<IndirectRefKind>(kind).c_str(), obj);
1382  }
1383  return result;
1384}
1385
1386// Implements java.lang.Thread.interrupted.
1387bool Thread::Interrupted() {
1388  MutexLock mu(Thread::Current(), *wait_mutex_);
1389  bool interrupted = IsInterruptedLocked();
1390  SetInterruptedLocked(false);
1391  return interrupted;
1392}
1393
1394// Implements java.lang.Thread.isInterrupted.
1395bool Thread::IsInterrupted() {
1396  MutexLock mu(Thread::Current(), *wait_mutex_);
1397  return IsInterruptedLocked();
1398}
1399
1400void Thread::Interrupt(Thread* self) {
1401  MutexLock mu(self, *wait_mutex_);
1402  if (interrupted_) {
1403    return;
1404  }
1405  interrupted_ = true;
1406  NotifyLocked(self);
1407}
1408
1409void Thread::Notify() {
1410  Thread* self = Thread::Current();
1411  MutexLock mu(self, *wait_mutex_);
1412  NotifyLocked(self);
1413}
1414
1415void Thread::NotifyLocked(Thread* self) {
1416  if (wait_monitor_ != nullptr) {
1417    wait_cond_->Signal(self);
1418  }
1419}
1420
1421void Thread::SetClassLoaderOverride(jobject class_loader_override) {
1422  if (tlsPtr_.class_loader_override != nullptr) {
1423    GetJniEnv()->DeleteGlobalRef(tlsPtr_.class_loader_override);
1424  }
1425  tlsPtr_.class_loader_override = GetJniEnv()->NewGlobalRef(class_loader_override);
1426}
1427
1428class CountStackDepthVisitor : public StackVisitor {
1429 public:
1430  explicit CountStackDepthVisitor(Thread* thread)
1431      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1432      : StackVisitor(thread, nullptr),
1433        depth_(0), skip_depth_(0), skipping_(true) {}
1434
1435  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1436    // We want to skip frames up to and including the exception's constructor.
1437    // Note we also skip the frame if it doesn't have a method (namely the callee
1438    // save frame)
1439    mirror::ArtMethod* m = GetMethod();
1440    if (skipping_ && !m->IsRuntimeMethod() &&
1441        !mirror::Throwable::GetJavaLangThrowable()->IsAssignableFrom(m->GetDeclaringClass())) {
1442      skipping_ = false;
1443    }
1444    if (!skipping_) {
1445      if (!m->IsRuntimeMethod()) {  // Ignore runtime frames (in particular callee save).
1446        ++depth_;
1447      }
1448    } else {
1449      ++skip_depth_;
1450    }
1451    return true;
1452  }
1453
1454  int GetDepth() const {
1455    return depth_;
1456  }
1457
1458  int GetSkipDepth() const {
1459    return skip_depth_;
1460  }
1461
1462 private:
1463  uint32_t depth_;
1464  uint32_t skip_depth_;
1465  bool skipping_;
1466};
1467
1468template<bool kTransactionActive>
1469class BuildInternalStackTraceVisitor : public StackVisitor {
1470 public:
1471  explicit BuildInternalStackTraceVisitor(Thread* self, Thread* thread, int skip_depth)
1472      : StackVisitor(thread, nullptr), self_(self),
1473        skip_depth_(skip_depth), count_(0), dex_pc_trace_(nullptr), method_trace_(nullptr) {}
1474
1475  bool Init(int depth)
1476      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1477    // Allocate method trace with an extra slot that will hold the PC trace
1478    StackHandleScope<1> hs(self_);
1479    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1480    Handle<mirror::ObjectArray<mirror::Object>> method_trace(
1481        hs.NewHandle(class_linker->AllocObjectArray<mirror::Object>(self_, depth + 1)));
1482    if (method_trace.Get() == nullptr) {
1483      return false;
1484    }
1485    mirror::IntArray* dex_pc_trace = mirror::IntArray::Alloc(self_, depth);
1486    if (dex_pc_trace == nullptr) {
1487      return false;
1488    }
1489    // Save PC trace in last element of method trace, also places it into the
1490    // object graph.
1491    // We are called from native: use non-transactional mode.
1492    method_trace->Set<kTransactionActive>(depth, dex_pc_trace);
1493    // Set the Object*s and assert that no thread suspension is now possible.
1494    const char* last_no_suspend_cause =
1495        self_->StartAssertNoThreadSuspension("Building internal stack trace");
1496    CHECK(last_no_suspend_cause == nullptr) << last_no_suspend_cause;
1497    method_trace_ = method_trace.Get();
1498    dex_pc_trace_ = dex_pc_trace;
1499    return true;
1500  }
1501
1502  virtual ~BuildInternalStackTraceVisitor() {
1503    if (method_trace_ != nullptr) {
1504      self_->EndAssertNoThreadSuspension(nullptr);
1505    }
1506  }
1507
1508  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1509    if (method_trace_ == nullptr || dex_pc_trace_ == nullptr) {
1510      return true;  // We're probably trying to fillInStackTrace for an OutOfMemoryError.
1511    }
1512    if (skip_depth_ > 0) {
1513      skip_depth_--;
1514      return true;
1515    }
1516    mirror::ArtMethod* m = GetMethod();
1517    if (m->IsRuntimeMethod()) {
1518      return true;  // Ignore runtime frames (in particular callee save).
1519    }
1520    method_trace_->Set<kTransactionActive>(count_, m);
1521    dex_pc_trace_->Set<kTransactionActive>(count_,
1522        m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc());
1523    ++count_;
1524    return true;
1525  }
1526
1527  mirror::ObjectArray<mirror::Object>* GetInternalStackTrace() const {
1528    return method_trace_;
1529  }
1530
1531 private:
1532  Thread* const self_;
1533  // How many more frames to skip.
1534  int32_t skip_depth_;
1535  // Current position down stack trace.
1536  uint32_t count_;
1537  // Array of dex PC values.
1538  mirror::IntArray* dex_pc_trace_;
1539  // An array of the methods on the stack, the last entry is a reference to the PC trace.
1540  mirror::ObjectArray<mirror::Object>* method_trace_;
1541};
1542
1543template<bool kTransactionActive>
1544jobject Thread::CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const {
1545  // Compute depth of stack
1546  CountStackDepthVisitor count_visitor(const_cast<Thread*>(this));
1547  count_visitor.WalkStack();
1548  int32_t depth = count_visitor.GetDepth();
1549  int32_t skip_depth = count_visitor.GetSkipDepth();
1550
1551  // Build internal stack trace.
1552  BuildInternalStackTraceVisitor<kTransactionActive> build_trace_visitor(soa.Self(),
1553                                                                         const_cast<Thread*>(this),
1554                                                                         skip_depth);
1555  if (!build_trace_visitor.Init(depth)) {
1556    return nullptr;  // Allocation failed.
1557  }
1558  build_trace_visitor.WalkStack();
1559  mirror::ObjectArray<mirror::Object>* trace = build_trace_visitor.GetInternalStackTrace();
1560  if (kIsDebugBuild) {
1561    for (int32_t i = 0; i < trace->GetLength(); ++i) {
1562      CHECK(trace->Get(i) != nullptr);
1563    }
1564  }
1565  return soa.AddLocalReference<jobjectArray>(trace);
1566}
1567template jobject Thread::CreateInternalStackTrace<false>(
1568    const ScopedObjectAccessAlreadyRunnable& soa) const;
1569template jobject Thread::CreateInternalStackTrace<true>(
1570    const ScopedObjectAccessAlreadyRunnable& soa) const;
1571
1572jobjectArray Thread::InternalStackTraceToStackTraceElementArray(
1573    const ScopedObjectAccessAlreadyRunnable& soa, jobject internal, jobjectArray output_array,
1574    int* stack_depth) {
1575  // Decode the internal stack trace into the depth, method trace and PC trace
1576  int32_t depth = soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal)->GetLength() - 1;
1577
1578  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1579
1580  jobjectArray result;
1581
1582  if (output_array != nullptr) {
1583    // Reuse the array we were given.
1584    result = output_array;
1585    // ...adjusting the number of frames we'll write to not exceed the array length.
1586    const int32_t traces_length =
1587        soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->GetLength();
1588    depth = std::min(depth, traces_length);
1589  } else {
1590    // Create java_trace array and place in local reference table
1591    mirror::ObjectArray<mirror::StackTraceElement>* java_traces =
1592        class_linker->AllocStackTraceElementArray(soa.Self(), depth);
1593    if (java_traces == nullptr) {
1594      return nullptr;
1595    }
1596    result = soa.AddLocalReference<jobjectArray>(java_traces);
1597  }
1598
1599  if (stack_depth != nullptr) {
1600    *stack_depth = depth;
1601  }
1602
1603  for (int32_t i = 0; i < depth; ++i) {
1604    mirror::ObjectArray<mirror::Object>* method_trace =
1605          soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal);
1606    // Prepare parameters for StackTraceElement(String cls, String method, String file, int line)
1607    mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(method_trace->Get(i));
1608    int32_t line_number;
1609    StackHandleScope<3> hs(soa.Self());
1610    auto class_name_object(hs.NewHandle<mirror::String>(nullptr));
1611    auto source_name_object(hs.NewHandle<mirror::String>(nullptr));
1612    if (method->IsProxyMethod()) {
1613      line_number = -1;
1614      class_name_object.Assign(method->GetDeclaringClass()->GetName());
1615      // source_name_object intentionally left null for proxy methods
1616    } else {
1617      mirror::IntArray* pc_trace = down_cast<mirror::IntArray*>(method_trace->Get(depth));
1618      uint32_t dex_pc = pc_trace->Get(i);
1619      line_number = method->GetLineNumFromDexPC(dex_pc);
1620      // Allocate element, potentially triggering GC
1621      // TODO: reuse class_name_object via Class::name_?
1622      const char* descriptor = method->GetDeclaringClassDescriptor();
1623      CHECK(descriptor != nullptr);
1624      std::string class_name(PrettyDescriptor(descriptor));
1625      class_name_object.Assign(mirror::String::AllocFromModifiedUtf8(soa.Self(), class_name.c_str()));
1626      if (class_name_object.Get() == nullptr) {
1627        return nullptr;
1628      }
1629      const char* source_file = method->GetDeclaringClassSourceFile();
1630      if (source_file != nullptr) {
1631        source_name_object.Assign(mirror::String::AllocFromModifiedUtf8(soa.Self(), source_file));
1632        if (source_name_object.Get() == nullptr) {
1633          return nullptr;
1634        }
1635      }
1636    }
1637    const char* method_name = method->GetName();
1638    CHECK(method_name != nullptr);
1639    Handle<mirror::String> method_name_object(
1640        hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), method_name)));
1641    if (method_name_object.Get() == nullptr) {
1642      return nullptr;
1643    }
1644    mirror::StackTraceElement* obj = mirror::StackTraceElement::Alloc(
1645        soa.Self(), class_name_object, method_name_object, source_name_object, line_number);
1646    if (obj == nullptr) {
1647      return nullptr;
1648    }
1649    // We are called from native: use non-transactional mode.
1650    soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->Set<false>(i, obj);
1651  }
1652  return result;
1653}
1654
1655void Thread::ThrowNewExceptionF(const ThrowLocation& throw_location,
1656                                const char* exception_class_descriptor, const char* fmt, ...) {
1657  va_list args;
1658  va_start(args, fmt);
1659  ThrowNewExceptionV(throw_location, exception_class_descriptor,
1660                     fmt, args);
1661  va_end(args);
1662}
1663
1664void Thread::ThrowNewExceptionV(const ThrowLocation& throw_location,
1665                                const char* exception_class_descriptor,
1666                                const char* fmt, va_list ap) {
1667  std::string msg;
1668  StringAppendV(&msg, fmt, ap);
1669  ThrowNewException(throw_location, exception_class_descriptor, msg.c_str());
1670}
1671
1672void Thread::ThrowNewException(const ThrowLocation& throw_location,
1673                               const char* exception_class_descriptor,
1674                               const char* msg) {
1675  // Callers should either clear or call ThrowNewWrappedException.
1676  AssertNoPendingExceptionForNewException(msg);
1677  ThrowNewWrappedException(throw_location, exception_class_descriptor, msg);
1678}
1679
1680void Thread::ThrowNewWrappedException(const ThrowLocation& throw_location,
1681                                      const char* exception_class_descriptor,
1682                                      const char* msg) {
1683  DCHECK_EQ(this, Thread::Current());
1684  ScopedObjectAccessUnchecked soa(this);
1685  StackHandleScope<5> hs(soa.Self());
1686  // Ensure we don't forget arguments over object allocation.
1687  Handle<mirror::Object> saved_throw_this(hs.NewHandle(throw_location.GetThis()));
1688  Handle<mirror::ArtMethod> saved_throw_method(hs.NewHandle(throw_location.GetMethod()));
1689  // Ignore the cause throw location. TODO: should we report this as a re-throw?
1690  ScopedLocalRef<jobject> cause(GetJniEnv(), soa.AddLocalReference<jobject>(GetException(nullptr)));
1691  bool is_exception_reported = IsExceptionReportedToInstrumentation();
1692  ClearException();
1693  Runtime* runtime = Runtime::Current();
1694
1695  mirror::ClassLoader* cl = nullptr;
1696  if (saved_throw_method.Get() != nullptr) {
1697    cl = saved_throw_method.Get()->GetDeclaringClass()->GetClassLoader();
1698  }
1699  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(cl));
1700  Handle<mirror::Class> exception_class(
1701      hs.NewHandle(runtime->GetClassLinker()->FindClass(this, exception_class_descriptor,
1702                                                        class_loader)));
1703  if (UNLIKELY(exception_class.Get() == nullptr)) {
1704    CHECK(IsExceptionPending());
1705    LOG(ERROR) << "No exception class " << PrettyDescriptor(exception_class_descriptor);
1706    return;
1707  }
1708
1709  if (UNLIKELY(!runtime->GetClassLinker()->EnsureInitialized(soa.Self(), exception_class, true,
1710                                                             true))) {
1711    DCHECK(IsExceptionPending());
1712    return;
1713  }
1714  DCHECK(!runtime->IsStarted() || exception_class->IsThrowableClass());
1715  Handle<mirror::Throwable> exception(
1716      hs.NewHandle(down_cast<mirror::Throwable*>(exception_class->AllocObject(this))));
1717
1718  // If we couldn't allocate the exception, throw the pre-allocated out of memory exception.
1719  if (exception.Get() == nullptr) {
1720    ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(),
1721                                         throw_location.GetDexPc());
1722    SetException(gc_safe_throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError());
1723    SetExceptionReportedToInstrumentation(is_exception_reported);
1724    return;
1725  }
1726
1727  // Choose an appropriate constructor and set up the arguments.
1728  const char* signature;
1729  ScopedLocalRef<jstring> msg_string(GetJniEnv(), nullptr);
1730  if (msg != nullptr) {
1731    // Ensure we remember this and the method over the String allocation.
1732    msg_string.reset(
1733        soa.AddLocalReference<jstring>(mirror::String::AllocFromModifiedUtf8(this, msg)));
1734    if (UNLIKELY(msg_string.get() == nullptr)) {
1735      CHECK(IsExceptionPending());  // OOME.
1736      return;
1737    }
1738    if (cause.get() == nullptr) {
1739      signature = "(Ljava/lang/String;)V";
1740    } else {
1741      signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
1742    }
1743  } else {
1744    if (cause.get() == nullptr) {
1745      signature = "()V";
1746    } else {
1747      signature = "(Ljava/lang/Throwable;)V";
1748    }
1749  }
1750  mirror::ArtMethod* exception_init_method =
1751      exception_class->FindDeclaredDirectMethod("<init>", signature);
1752
1753  CHECK(exception_init_method != nullptr) << "No <init>" << signature << " in "
1754      << PrettyDescriptor(exception_class_descriptor);
1755
1756  if (UNLIKELY(!runtime->IsStarted())) {
1757    // Something is trying to throw an exception without a started runtime, which is the common
1758    // case in the compiler. We won't be able to invoke the constructor of the exception, so set
1759    // the exception fields directly.
1760    if (msg != nullptr) {
1761      exception->SetDetailMessage(down_cast<mirror::String*>(DecodeJObject(msg_string.get())));
1762    }
1763    if (cause.get() != nullptr) {
1764      exception->SetCause(down_cast<mirror::Throwable*>(DecodeJObject(cause.get())));
1765    }
1766    ScopedLocalRef<jobject> trace(GetJniEnv(),
1767                                  Runtime::Current()->IsActiveTransaction()
1768                                      ? CreateInternalStackTrace<true>(soa)
1769                                      : CreateInternalStackTrace<false>(soa));
1770    if (trace.get() != nullptr) {
1771      exception->SetStackState(down_cast<mirror::Throwable*>(DecodeJObject(trace.get())));
1772    }
1773    ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(),
1774                                         throw_location.GetDexPc());
1775    SetException(gc_safe_throw_location, exception.Get());
1776    SetExceptionReportedToInstrumentation(is_exception_reported);
1777  } else {
1778    jvalue jv_args[2];
1779    size_t i = 0;
1780
1781    if (msg != nullptr) {
1782      jv_args[i].l = msg_string.get();
1783      ++i;
1784    }
1785    if (cause.get() != nullptr) {
1786      jv_args[i].l = cause.get();
1787      ++i;
1788    }
1789    InvokeWithJValues(soa, exception.Get(), soa.EncodeMethod(exception_init_method), jv_args);
1790    if (LIKELY(!IsExceptionPending())) {
1791      ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(),
1792                                           throw_location.GetDexPc());
1793      SetException(gc_safe_throw_location, exception.Get());
1794      SetExceptionReportedToInstrumentation(is_exception_reported);
1795    }
1796  }
1797}
1798
1799void Thread::ThrowOutOfMemoryError(const char* msg) {
1800  LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s",
1801      msg, (tls32_.throwing_OutOfMemoryError ? " (recursive case)" : ""));
1802  ThrowLocation throw_location = GetCurrentLocationForThrow();
1803  if (!tls32_.throwing_OutOfMemoryError) {
1804    tls32_.throwing_OutOfMemoryError = true;
1805    ThrowNewException(throw_location, "Ljava/lang/OutOfMemoryError;", msg);
1806    tls32_.throwing_OutOfMemoryError = false;
1807  } else {
1808    Dump(LOG(ERROR));  // The pre-allocated OOME has no stack, so help out and log one.
1809    SetException(throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError());
1810  }
1811}
1812
1813Thread* Thread::CurrentFromGdb() {
1814  return Thread::Current();
1815}
1816
1817void Thread::DumpFromGdb() const {
1818  std::ostringstream ss;
1819  Dump(ss);
1820  std::string str(ss.str());
1821  // log to stderr for debugging command line processes
1822  std::cerr << str;
1823#ifdef HAVE_ANDROID_OS
1824  // log to logcat for debugging frameworks processes
1825  LOG(INFO) << str;
1826#endif
1827}
1828
1829// Explicitly instantiate 32 and 64bit thread offset dumping support.
1830template void Thread::DumpThreadOffset<4>(std::ostream& os, uint32_t offset);
1831template void Thread::DumpThreadOffset<8>(std::ostream& os, uint32_t offset);
1832
1833template<size_t ptr_size>
1834void Thread::DumpThreadOffset(std::ostream& os, uint32_t offset) {
1835#define DO_THREAD_OFFSET(x, y) \
1836    if (offset == x.Uint32Value()) { \
1837      os << y; \
1838      return; \
1839    }
1840  DO_THREAD_OFFSET(ThreadFlagsOffset<ptr_size>(), "state_and_flags")
1841  DO_THREAD_OFFSET(CardTableOffset<ptr_size>(), "card_table")
1842  DO_THREAD_OFFSET(ExceptionOffset<ptr_size>(), "exception")
1843  DO_THREAD_OFFSET(PeerOffset<ptr_size>(), "peer");
1844  DO_THREAD_OFFSET(JniEnvOffset<ptr_size>(), "jni_env")
1845  DO_THREAD_OFFSET(SelfOffset<ptr_size>(), "self")
1846  DO_THREAD_OFFSET(StackEndOffset<ptr_size>(), "stack_end")
1847  DO_THREAD_OFFSET(ThinLockIdOffset<ptr_size>(), "thin_lock_thread_id")
1848  DO_THREAD_OFFSET(TopOfManagedStackOffset<ptr_size>(), "top_quick_frame_method")
1849  DO_THREAD_OFFSET(TopShadowFrameOffset<ptr_size>(), "top_shadow_frame")
1850  DO_THREAD_OFFSET(TopHandleScopeOffset<ptr_size>(), "top_handle_scope")
1851  DO_THREAD_OFFSET(ThreadSuspendTriggerOffset<ptr_size>(), "suspend_trigger")
1852#undef DO_THREAD_OFFSET
1853
1854#define INTERPRETER_ENTRY_POINT_INFO(x) \
1855    if (INTERPRETER_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \
1856      os << #x; \
1857      return; \
1858    }
1859  INTERPRETER_ENTRY_POINT_INFO(pInterpreterToInterpreterBridge)
1860  INTERPRETER_ENTRY_POINT_INFO(pInterpreterToCompiledCodeBridge)
1861#undef INTERPRETER_ENTRY_POINT_INFO
1862
1863#define JNI_ENTRY_POINT_INFO(x) \
1864    if (JNI_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \
1865      os << #x; \
1866      return; \
1867    }
1868  JNI_ENTRY_POINT_INFO(pDlsymLookup)
1869#undef JNI_ENTRY_POINT_INFO
1870
1871#define QUICK_ENTRY_POINT_INFO(x) \
1872    if (QUICK_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \
1873      os << #x; \
1874      return; \
1875    }
1876  QUICK_ENTRY_POINT_INFO(pAllocArray)
1877  QUICK_ENTRY_POINT_INFO(pAllocArrayResolved)
1878  QUICK_ENTRY_POINT_INFO(pAllocArrayWithAccessCheck)
1879  QUICK_ENTRY_POINT_INFO(pAllocObject)
1880  QUICK_ENTRY_POINT_INFO(pAllocObjectResolved)
1881  QUICK_ENTRY_POINT_INFO(pAllocObjectInitialized)
1882  QUICK_ENTRY_POINT_INFO(pAllocObjectWithAccessCheck)
1883  QUICK_ENTRY_POINT_INFO(pCheckAndAllocArray)
1884  QUICK_ENTRY_POINT_INFO(pCheckAndAllocArrayWithAccessCheck)
1885  QUICK_ENTRY_POINT_INFO(pInstanceofNonTrivial)
1886  QUICK_ENTRY_POINT_INFO(pCheckCast)
1887  QUICK_ENTRY_POINT_INFO(pInitializeStaticStorage)
1888  QUICK_ENTRY_POINT_INFO(pInitializeTypeAndVerifyAccess)
1889  QUICK_ENTRY_POINT_INFO(pInitializeType)
1890  QUICK_ENTRY_POINT_INFO(pResolveString)
1891  QUICK_ENTRY_POINT_INFO(pSet8Instance)
1892  QUICK_ENTRY_POINT_INFO(pSet8Static)
1893  QUICK_ENTRY_POINT_INFO(pSet16Instance)
1894  QUICK_ENTRY_POINT_INFO(pSet16Static)
1895  QUICK_ENTRY_POINT_INFO(pSet32Instance)
1896  QUICK_ENTRY_POINT_INFO(pSet32Static)
1897  QUICK_ENTRY_POINT_INFO(pSet64Instance)
1898  QUICK_ENTRY_POINT_INFO(pSet64Static)
1899  QUICK_ENTRY_POINT_INFO(pSetObjInstance)
1900  QUICK_ENTRY_POINT_INFO(pSetObjStatic)
1901  QUICK_ENTRY_POINT_INFO(pGetByteInstance)
1902  QUICK_ENTRY_POINT_INFO(pGetBooleanInstance)
1903  QUICK_ENTRY_POINT_INFO(pGetByteStatic)
1904  QUICK_ENTRY_POINT_INFO(pGetBooleanStatic)
1905  QUICK_ENTRY_POINT_INFO(pGetShortInstance)
1906  QUICK_ENTRY_POINT_INFO(pGetCharInstance)
1907  QUICK_ENTRY_POINT_INFO(pGetShortStatic)
1908  QUICK_ENTRY_POINT_INFO(pGetCharStatic)
1909  QUICK_ENTRY_POINT_INFO(pGet32Instance)
1910  QUICK_ENTRY_POINT_INFO(pGet32Static)
1911  QUICK_ENTRY_POINT_INFO(pGet64Instance)
1912  QUICK_ENTRY_POINT_INFO(pGet64Static)
1913  QUICK_ENTRY_POINT_INFO(pGetObjInstance)
1914  QUICK_ENTRY_POINT_INFO(pGetObjStatic)
1915  QUICK_ENTRY_POINT_INFO(pAputObjectWithNullAndBoundCheck)
1916  QUICK_ENTRY_POINT_INFO(pAputObjectWithBoundCheck)
1917  QUICK_ENTRY_POINT_INFO(pAputObject)
1918  QUICK_ENTRY_POINT_INFO(pHandleFillArrayData)
1919  QUICK_ENTRY_POINT_INFO(pJniMethodStart)
1920  QUICK_ENTRY_POINT_INFO(pJniMethodStartSynchronized)
1921  QUICK_ENTRY_POINT_INFO(pJniMethodEnd)
1922  QUICK_ENTRY_POINT_INFO(pJniMethodEndSynchronized)
1923  QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReference)
1924  QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReferenceSynchronized)
1925  QUICK_ENTRY_POINT_INFO(pQuickGenericJniTrampoline)
1926  QUICK_ENTRY_POINT_INFO(pLockObject)
1927  QUICK_ENTRY_POINT_INFO(pUnlockObject)
1928  QUICK_ENTRY_POINT_INFO(pCmpgDouble)
1929  QUICK_ENTRY_POINT_INFO(pCmpgFloat)
1930  QUICK_ENTRY_POINT_INFO(pCmplDouble)
1931  QUICK_ENTRY_POINT_INFO(pCmplFloat)
1932  QUICK_ENTRY_POINT_INFO(pFmod)
1933  QUICK_ENTRY_POINT_INFO(pL2d)
1934  QUICK_ENTRY_POINT_INFO(pFmodf)
1935  QUICK_ENTRY_POINT_INFO(pL2f)
1936  QUICK_ENTRY_POINT_INFO(pD2iz)
1937  QUICK_ENTRY_POINT_INFO(pF2iz)
1938  QUICK_ENTRY_POINT_INFO(pIdivmod)
1939  QUICK_ENTRY_POINT_INFO(pD2l)
1940  QUICK_ENTRY_POINT_INFO(pF2l)
1941  QUICK_ENTRY_POINT_INFO(pLdiv)
1942  QUICK_ENTRY_POINT_INFO(pLmod)
1943  QUICK_ENTRY_POINT_INFO(pLmul)
1944  QUICK_ENTRY_POINT_INFO(pShlLong)
1945  QUICK_ENTRY_POINT_INFO(pShrLong)
1946  QUICK_ENTRY_POINT_INFO(pUshrLong)
1947  QUICK_ENTRY_POINT_INFO(pIndexOf)
1948  QUICK_ENTRY_POINT_INFO(pStringCompareTo)
1949  QUICK_ENTRY_POINT_INFO(pMemcpy)
1950  QUICK_ENTRY_POINT_INFO(pQuickImtConflictTrampoline)
1951  QUICK_ENTRY_POINT_INFO(pQuickResolutionTrampoline)
1952  QUICK_ENTRY_POINT_INFO(pQuickToInterpreterBridge)
1953  QUICK_ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck)
1954  QUICK_ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck)
1955  QUICK_ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck)
1956  QUICK_ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck)
1957  QUICK_ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck)
1958  QUICK_ENTRY_POINT_INFO(pTestSuspend)
1959  QUICK_ENTRY_POINT_INFO(pDeliverException)
1960  QUICK_ENTRY_POINT_INFO(pThrowArrayBounds)
1961  QUICK_ENTRY_POINT_INFO(pThrowDivZero)
1962  QUICK_ENTRY_POINT_INFO(pThrowNoSuchMethod)
1963  QUICK_ENTRY_POINT_INFO(pThrowNullPointer)
1964  QUICK_ENTRY_POINT_INFO(pThrowStackOverflow)
1965  QUICK_ENTRY_POINT_INFO(pA64Load)
1966  QUICK_ENTRY_POINT_INFO(pA64Store)
1967#undef QUICK_ENTRY_POINT_INFO
1968
1969  os << offset;
1970}
1971
1972void Thread::QuickDeliverException() {
1973  // Get exception from thread.
1974  ThrowLocation throw_location;
1975  mirror::Throwable* exception = GetException(&throw_location);
1976  CHECK(exception != nullptr);
1977  // Don't leave exception visible while we try to find the handler, which may cause class
1978  // resolution.
1979  bool is_exception_reported = IsExceptionReportedToInstrumentation();
1980  ClearException();
1981  bool is_deoptimization = (exception == GetDeoptimizationException());
1982  QuickExceptionHandler exception_handler(this, is_deoptimization);
1983  if (is_deoptimization) {
1984    exception_handler.DeoptimizeStack();
1985  } else {
1986    exception_handler.FindCatch(throw_location, exception, is_exception_reported);
1987  }
1988  exception_handler.UpdateInstrumentationStack();
1989  exception_handler.DoLongJump();
1990  LOG(FATAL) << "UNREACHABLE";
1991  UNREACHABLE();
1992}
1993
1994Context* Thread::GetLongJumpContext() {
1995  Context* result = tlsPtr_.long_jump_context;
1996  if (result == nullptr) {
1997    result = Context::Create();
1998  } else {
1999    tlsPtr_.long_jump_context = nullptr;  // Avoid context being shared.
2000    result->Reset();
2001  }
2002  return result;
2003}
2004
2005// Note: this visitor may return with a method set, but dex_pc_ being DexFile:kDexNoIndex. This is
2006//       so we don't abort in a special situation (thinlocked monitor) when dumping the Java stack.
2007struct CurrentMethodVisitor FINAL : public StackVisitor {
2008  CurrentMethodVisitor(Thread* thread, Context* context, bool abort_on_error)
2009      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
2010      : StackVisitor(thread, context), this_object_(nullptr), method_(nullptr), dex_pc_(0),
2011        abort_on_error_(abort_on_error) {}
2012  bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2013    mirror::ArtMethod* m = GetMethod();
2014    if (m->IsRuntimeMethod()) {
2015      // Continue if this is a runtime method.
2016      return true;
2017    }
2018    if (context_ != nullptr) {
2019      this_object_ = GetThisObject();
2020    }
2021    method_ = m;
2022    dex_pc_ = GetDexPc(abort_on_error_);
2023    return false;
2024  }
2025  mirror::Object* this_object_;
2026  mirror::ArtMethod* method_;
2027  uint32_t dex_pc_;
2028  const bool abort_on_error_;
2029};
2030
2031mirror::ArtMethod* Thread::GetCurrentMethod(uint32_t* dex_pc, bool abort_on_error) const {
2032  CurrentMethodVisitor visitor(const_cast<Thread*>(this), nullptr, abort_on_error);
2033  visitor.WalkStack(false);
2034  if (dex_pc != nullptr) {
2035    *dex_pc = visitor.dex_pc_;
2036  }
2037  return visitor.method_;
2038}
2039
2040ThrowLocation Thread::GetCurrentLocationForThrow() {
2041  Context* context = GetLongJumpContext();
2042  CurrentMethodVisitor visitor(this, context, true);
2043  visitor.WalkStack(false);
2044  ReleaseLongJumpContext(context);
2045  return ThrowLocation(visitor.this_object_, visitor.method_, visitor.dex_pc_);
2046}
2047
2048bool Thread::HoldsLock(mirror::Object* object) const {
2049  if (object == nullptr) {
2050    return false;
2051  }
2052  return object->GetLockOwnerThreadId() == GetThreadId();
2053}
2054
2055// RootVisitor parameters are: (const Object* obj, size_t vreg, const StackVisitor* visitor).
2056template <typename RootVisitor>
2057class ReferenceMapVisitor : public StackVisitor {
2058 public:
2059  ReferenceMapVisitor(Thread* thread, Context* context, const RootVisitor& visitor)
2060      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
2061      : StackVisitor(thread, context), visitor_(visitor) {}
2062
2063  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2064    if (false) {
2065      LOG(INFO) << "Visiting stack roots in " << PrettyMethod(GetMethod())
2066                << StringPrintf("@ PC:%04x", GetDexPc());
2067    }
2068    ShadowFrame* shadow_frame = GetCurrentShadowFrame();
2069    if (shadow_frame != nullptr) {
2070      VisitShadowFrame(shadow_frame);
2071    } else {
2072      VisitQuickFrame();
2073    }
2074    return true;
2075  }
2076
2077  void VisitShadowFrame(ShadowFrame* shadow_frame) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2078    mirror::ArtMethod** method_addr = shadow_frame->GetMethodAddress();
2079    visitor_(reinterpret_cast<mirror::Object**>(method_addr), 0 /*ignored*/, this);
2080    mirror::ArtMethod* m = *method_addr;
2081    DCHECK(m != nullptr);
2082    size_t num_regs = shadow_frame->NumberOfVRegs();
2083    if (m->IsNative() || shadow_frame->HasReferenceArray()) {
2084      // handle scope for JNI or References for interpreter.
2085      for (size_t reg = 0; reg < num_regs; ++reg) {
2086        mirror::Object* ref = shadow_frame->GetVRegReference(reg);
2087        if (ref != nullptr) {
2088          mirror::Object* new_ref = ref;
2089          visitor_(&new_ref, reg, this);
2090          if (new_ref != ref) {
2091            shadow_frame->SetVRegReference(reg, new_ref);
2092          }
2093        }
2094      }
2095    } else {
2096      // Java method.
2097      // Portable path use DexGcMap and store in Method.native_gc_map_.
2098      const uint8_t* gc_map = m->GetNativeGcMap(sizeof(void*));
2099      CHECK(gc_map != nullptr) << PrettyMethod(m);
2100      verifier::DexPcToReferenceMap dex_gc_map(gc_map);
2101      uint32_t dex_pc = shadow_frame->GetDexPC();
2102      const uint8_t* reg_bitmap = dex_gc_map.FindBitMap(dex_pc);
2103      DCHECK(reg_bitmap != nullptr);
2104      num_regs = std::min(dex_gc_map.RegWidth() * 8, num_regs);
2105      for (size_t reg = 0; reg < num_regs; ++reg) {
2106        if (TestBitmap(reg, reg_bitmap)) {
2107          mirror::Object* ref = shadow_frame->GetVRegReference(reg);
2108          if (ref != nullptr) {
2109            mirror::Object* new_ref = ref;
2110            visitor_(&new_ref, reg, this);
2111            if (new_ref != ref) {
2112              shadow_frame->SetVRegReference(reg, new_ref);
2113            }
2114          }
2115        }
2116      }
2117    }
2118  }
2119
2120 private:
2121  void VisitQuickFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2122    StackReference<mirror::ArtMethod>* cur_quick_frame = GetCurrentQuickFrame();
2123    mirror::ArtMethod* m = cur_quick_frame->AsMirrorPtr();
2124    mirror::ArtMethod* old_method = m;
2125    visitor_(reinterpret_cast<mirror::Object**>(&m), 0 /*ignored*/, this);
2126    if (m != old_method) {
2127      cur_quick_frame->Assign(m);
2128    }
2129
2130    // Process register map (which native and runtime methods don't have)
2131    if (!m->IsNative() && !m->IsRuntimeMethod() && !m->IsProxyMethod()) {
2132      if (m->IsOptimized(sizeof(void*))) {
2133        Runtime* runtime = Runtime::Current();
2134        const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
2135        uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
2136        StackMap map = m->GetStackMap(native_pc_offset);
2137        MemoryRegion mask = map.GetStackMask();
2138        for (size_t i = 0; i < mask.size_in_bits(); ++i) {
2139          if (mask.LoadBit(i)) {
2140            StackReference<mirror::Object>* ref_addr =
2141                  reinterpret_cast<StackReference<mirror::Object>*>(cur_quick_frame) + i;
2142            mirror::Object* ref = ref_addr->AsMirrorPtr();
2143            if (ref != nullptr) {
2144              mirror::Object* new_ref = ref;
2145              visitor_(&new_ref, -1, this);
2146              if (ref != new_ref) {
2147                ref_addr->Assign(new_ref);
2148              }
2149            }
2150          }
2151        }
2152      } else {
2153        const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
2154        CHECK(native_gc_map != nullptr) << PrettyMethod(m);
2155        const DexFile::CodeItem* code_item = m->GetCodeItem();
2156        // Can't be nullptr or how would we compile its instructions?
2157        DCHECK(code_item != nullptr) << PrettyMethod(m);
2158        NativePcOffsetToReferenceMap map(native_gc_map);
2159        size_t num_regs = std::min(map.RegWidth() * 8,
2160                                   static_cast<size_t>(code_item->registers_size_));
2161        if (num_regs > 0) {
2162          Runtime* runtime = Runtime::Current();
2163          const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
2164          uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
2165          const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
2166          DCHECK(reg_bitmap != nullptr);
2167          const void* code_pointer = mirror::ArtMethod::EntryPointToCodePointer(entry_point);
2168          const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
2169          QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
2170          // For all dex registers in the bitmap
2171          DCHECK(cur_quick_frame != nullptr);
2172          for (size_t reg = 0; reg < num_regs; ++reg) {
2173            // Does this register hold a reference?
2174            if (TestBitmap(reg, reg_bitmap)) {
2175              uint32_t vmap_offset;
2176              if (vmap_table.IsInContext(reg, kReferenceVReg, &vmap_offset)) {
2177                int vmap_reg = vmap_table.ComputeRegister(frame_info.CoreSpillMask(), vmap_offset,
2178                                                          kReferenceVReg);
2179                // This is sound as spilled GPRs will be word sized (ie 32 or 64bit).
2180                mirror::Object** ref_addr =
2181                    reinterpret_cast<mirror::Object**>(GetGPRAddress(vmap_reg));
2182                if (*ref_addr != nullptr) {
2183                  visitor_(ref_addr, reg, this);
2184                }
2185              } else {
2186                StackReference<mirror::Object>* ref_addr =
2187                    reinterpret_cast<StackReference<mirror::Object>*>(
2188                        GetVRegAddr(cur_quick_frame, code_item, frame_info.CoreSpillMask(),
2189                                    frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), reg));
2190                mirror::Object* ref = ref_addr->AsMirrorPtr();
2191                if (ref != nullptr) {
2192                  mirror::Object* new_ref = ref;
2193                  visitor_(&new_ref, reg, this);
2194                  if (ref != new_ref) {
2195                    ref_addr->Assign(new_ref);
2196                  }
2197                }
2198              }
2199            }
2200          }
2201        }
2202      }
2203    }
2204  }
2205
2206  static bool TestBitmap(size_t reg, const uint8_t* reg_vector) {
2207    return ((reg_vector[reg / kBitsPerByte] >> (reg % kBitsPerByte)) & 0x01) != 0;
2208  }
2209
2210  // Visitor for when we visit a root.
2211  const RootVisitor& visitor_;
2212};
2213
2214class RootCallbackVisitor {
2215 public:
2216  RootCallbackVisitor(RootCallback* callback, void* arg, uint32_t tid)
2217     : callback_(callback), arg_(arg), tid_(tid) {}
2218
2219  void operator()(mirror::Object** obj, size_t, const StackVisitor*) const {
2220    callback_(obj, arg_, tid_, kRootJavaFrame);
2221  }
2222
2223 private:
2224  RootCallback* const callback_;
2225  void* const arg_;
2226  const uint32_t tid_;
2227};
2228
2229void Thread::VisitRoots(RootCallback* visitor, void* arg) {
2230  uint32_t thread_id = GetThreadId();
2231  if (tlsPtr_.opeer != nullptr) {
2232    visitor(&tlsPtr_.opeer, arg, thread_id, kRootThreadObject);
2233  }
2234  if (tlsPtr_.exception != nullptr && tlsPtr_.exception != GetDeoptimizationException()) {
2235    visitor(reinterpret_cast<mirror::Object**>(&tlsPtr_.exception), arg, thread_id, kRootNativeStack);
2236  }
2237  tlsPtr_.throw_location.VisitRoots(visitor, arg);
2238  if (tlsPtr_.monitor_enter_object != nullptr) {
2239    visitor(&tlsPtr_.monitor_enter_object, arg, thread_id, kRootNativeStack);
2240  }
2241  tlsPtr_.jni_env->locals.VisitRoots(visitor, arg, thread_id, kRootJNILocal);
2242  tlsPtr_.jni_env->monitors.VisitRoots(visitor, arg, thread_id, kRootJNIMonitor);
2243  HandleScopeVisitRoots(visitor, arg, thread_id);
2244  if (tlsPtr_.debug_invoke_req != nullptr) {
2245    tlsPtr_.debug_invoke_req->VisitRoots(visitor, arg, thread_id, kRootDebugger);
2246  }
2247  if (tlsPtr_.single_step_control != nullptr) {
2248    tlsPtr_.single_step_control->VisitRoots(visitor, arg, thread_id, kRootDebugger);
2249  }
2250  if (tlsPtr_.deoptimization_shadow_frame != nullptr) {
2251    RootCallbackVisitor visitorToCallback(visitor, arg, thread_id);
2252    ReferenceMapVisitor<RootCallbackVisitor> mapper(this, nullptr, visitorToCallback);
2253    for (ShadowFrame* shadow_frame = tlsPtr_.deoptimization_shadow_frame; shadow_frame != nullptr;
2254        shadow_frame = shadow_frame->GetLink()) {
2255      mapper.VisitShadowFrame(shadow_frame);
2256    }
2257  }
2258  if (tlsPtr_.shadow_frame_under_construction != nullptr) {
2259    RootCallbackVisitor visitorToCallback(visitor, arg, thread_id);
2260    ReferenceMapVisitor<RootCallbackVisitor> mapper(this, nullptr, visitorToCallback);
2261    for (ShadowFrame* shadow_frame = tlsPtr_.shadow_frame_under_construction;
2262        shadow_frame != nullptr;
2263        shadow_frame = shadow_frame->GetLink()) {
2264      mapper.VisitShadowFrame(shadow_frame);
2265    }
2266  }
2267  // Visit roots on this thread's stack
2268  Context* context = GetLongJumpContext();
2269  RootCallbackVisitor visitorToCallback(visitor, arg, thread_id);
2270  ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context, visitorToCallback);
2271  mapper.WalkStack();
2272  ReleaseLongJumpContext(context);
2273  for (instrumentation::InstrumentationStackFrame& frame : *GetInstrumentationStack()) {
2274    if (frame.this_object_ != nullptr) {
2275      visitor(&frame.this_object_, arg, thread_id, kRootJavaFrame);
2276    }
2277    DCHECK(frame.method_ != nullptr);
2278    visitor(reinterpret_cast<mirror::Object**>(&frame.method_), arg, thread_id, kRootJavaFrame);
2279  }
2280}
2281
2282static void VerifyRoot(mirror::Object** root, void* /*arg*/, uint32_t /*thread_id*/,
2283                       RootType /*root_type*/) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2284  VerifyObject(*root);
2285}
2286
2287void Thread::VerifyStackImpl() {
2288  std::unique_ptr<Context> context(Context::Create());
2289  RootCallbackVisitor visitorToCallback(VerifyRoot, Runtime::Current()->GetHeap(), GetThreadId());
2290  ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context.get(), visitorToCallback);
2291  mapper.WalkStack();
2292}
2293
2294// Set the stack end to that to be used during a stack overflow
2295void Thread::SetStackEndForStackOverflow() {
2296  // During stack overflow we allow use of the full stack.
2297  if (tlsPtr_.stack_end == tlsPtr_.stack_begin) {
2298    // However, we seem to have already extended to use the full stack.
2299    LOG(ERROR) << "Need to increase kStackOverflowReservedBytes (currently "
2300               << GetStackOverflowReservedBytes(kRuntimeISA) << ")?";
2301    DumpStack(LOG(ERROR));
2302    LOG(FATAL) << "Recursive stack overflow.";
2303  }
2304
2305  tlsPtr_.stack_end = tlsPtr_.stack_begin;
2306
2307  // Remove the stack overflow protection if is it set up.
2308  bool implicit_stack_check = !Runtime::Current()->ExplicitStackOverflowChecks();
2309  if (implicit_stack_check) {
2310    if (!UnprotectStack()) {
2311      LOG(ERROR) << "Unable to remove stack protection for stack overflow";
2312    }
2313  }
2314}
2315
2316void Thread::SetTlab(uint8_t* start, uint8_t* end) {
2317  DCHECK_LE(start, end);
2318  tlsPtr_.thread_local_start = start;
2319  tlsPtr_.thread_local_pos  = tlsPtr_.thread_local_start;
2320  tlsPtr_.thread_local_end = end;
2321  tlsPtr_.thread_local_objects = 0;
2322}
2323
2324bool Thread::HasTlab() const {
2325  bool has_tlab = tlsPtr_.thread_local_pos != nullptr;
2326  if (has_tlab) {
2327    DCHECK(tlsPtr_.thread_local_start != nullptr && tlsPtr_.thread_local_end != nullptr);
2328  } else {
2329    DCHECK(tlsPtr_.thread_local_start == nullptr && tlsPtr_.thread_local_end == nullptr);
2330  }
2331  return has_tlab;
2332}
2333
2334std::ostream& operator<<(std::ostream& os, const Thread& thread) {
2335  thread.ShortDump(os);
2336  return os;
2337}
2338
2339void Thread::ProtectStack() {
2340  void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
2341  VLOG(threads) << "Protecting stack at " << pregion;
2342  if (mprotect(pregion, kStackOverflowProtectedSize, PROT_NONE) == -1) {
2343    LOG(FATAL) << "Unable to create protected region in stack for implicit overflow check. "
2344        "Reason: "
2345        << strerror(errno) << " size:  " << kStackOverflowProtectedSize;
2346  }
2347}
2348
2349bool Thread::UnprotectStack() {
2350  void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
2351  VLOG(threads) << "Unprotecting stack at " << pregion;
2352  return mprotect(pregion, kStackOverflowProtectedSize, PROT_READ|PROT_WRITE) == 0;
2353}
2354
2355
2356}  // namespace art
2357