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