• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "thread.h"
18 
19 #include <limits.h>  // for INT_MAX
20 #include <pthread.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <sys/resource.h>
24 #include <sys/time.h>
25 
26 #if __has_feature(hwaddress_sanitizer)
27 #include <sanitizer/hwasan_interface.h>
28 #else
29 #define __hwasan_tag_pointer(p, t) (p)
30 #endif
31 
32 #include <algorithm>
33 #include <atomic>
34 #include <bitset>
35 #include <cerrno>
36 #include <iostream>
37 #include <list>
38 #include <sstream>
39 
40 #include "android-base/file.h"
41 #include "android-base/stringprintf.h"
42 #include "android-base/strings.h"
43 
44 #include "arch/context-inl.h"
45 #include "arch/context.h"
46 #include "art_field-inl.h"
47 #include "art_method-inl.h"
48 #include "base/atomic.h"
49 #include "base/bit_utils.h"
50 #include "base/casts.h"
51 #include "base/file_utils.h"
52 #include "base/memory_tool.h"
53 #include "base/mutex.h"
54 #include "base/stl_util.h"
55 #include "base/systrace.h"
56 #include "base/time_utils.h"
57 #include "base/timing_logger.h"
58 #include "base/to_str.h"
59 #include "base/utils.h"
60 #include "class_linker-inl.h"
61 #include "class_root-inl.h"
62 #include "debugger.h"
63 #include "dex/descriptors_names.h"
64 #include "dex/dex_file-inl.h"
65 #include "dex/dex_file_annotations.h"
66 #include "dex/dex_file_types.h"
67 #include "entrypoints/entrypoint_utils.h"
68 #include "entrypoints/quick/quick_alloc_entrypoints.h"
69 #include "gc/accounting/card_table-inl.h"
70 #include "gc/accounting/heap_bitmap-inl.h"
71 #include "gc/allocator/rosalloc.h"
72 #include "gc/heap.h"
73 #include "gc/space/space-inl.h"
74 #include "gc_root.h"
75 #include "handle_scope-inl.h"
76 #include "indirect_reference_table-inl.h"
77 #include "instrumentation.h"
78 #include "interpreter/interpreter.h"
79 #include "interpreter/shadow_frame-inl.h"
80 #include "java_frame_root_info.h"
81 #include "jni/java_vm_ext.h"
82 #include "jni/jni_internal.h"
83 #include "mirror/class-alloc-inl.h"
84 #include "mirror/class_loader.h"
85 #include "mirror/object_array-alloc-inl.h"
86 #include "mirror/object_array-inl.h"
87 #include "mirror/stack_trace_element.h"
88 #include "monitor.h"
89 #include "monitor_objects_stack_visitor.h"
90 #include "native_stack_dump.h"
91 #include "nativehelper/scoped_local_ref.h"
92 #include "nativehelper/scoped_utf_chars.h"
93 #include "nterp_helpers.h"
94 #include "nth_caller_visitor.h"
95 #include "oat_quick_method_header.h"
96 #include "obj_ptr-inl.h"
97 #include "object_lock.h"
98 #include "palette/palette.h"
99 #include "quick/quick_method_frame_info.h"
100 #include "quick_exception_handler.h"
101 #include "read_barrier-inl.h"
102 #include "reflection.h"
103 #include "reflective_handle_scope-inl.h"
104 #include "runtime-inl.h"
105 #include "runtime.h"
106 #include "runtime_callbacks.h"
107 #include "scoped_thread_state_change-inl.h"
108 #include "scoped_disable_public_sdk_checker.h"
109 #include "stack.h"
110 #include "stack_map.h"
111 #include "thread-inl.h"
112 #include "thread_list.h"
113 #include "verifier/method_verifier.h"
114 #include "verify_object.h"
115 #include "well_known_classes.h"
116 
117 #if ART_USE_FUTEXES
118 #include "linux/futex.h"
119 #include "sys/syscall.h"
120 #ifndef SYS_futex
121 #define SYS_futex __NR_futex
122 #endif
123 #endif  // ART_USE_FUTEXES
124 
125 #pragma clang diagnostic push
126 #pragma clang diagnostic error "-Wconversion"
127 
128 namespace art {
129 
130 using android::base::StringAppendV;
131 using android::base::StringPrintf;
132 
133 extern "C" NO_RETURN void artDeoptimize(Thread* self);
134 
135 bool Thread::is_started_ = false;
136 pthread_key_t Thread::pthread_key_self_;
137 ConditionVariable* Thread::resume_cond_ = nullptr;
138 const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
139 bool (*Thread::is_sensitive_thread_hook_)() = nullptr;
140 Thread* Thread::jit_sensitive_thread_ = nullptr;
141 #ifndef __BIONIC__
142 thread_local Thread* Thread::self_tls_ = nullptr;
143 #endif
144 
145 static constexpr bool kVerifyImageObjectsMarked = kIsDebugBuild;
146 
147 // For implicit overflow checks we reserve an extra piece of memory at the bottom
148 // of the stack (lowest memory).  The higher portion of the memory
149 // is protected against reads and the lower is available for use while
150 // throwing the StackOverflow exception.
151 constexpr size_t kStackOverflowProtectedSize = 4 * kMemoryToolStackGuardSizeScale * KB;
152 
153 static const char* kThreadNameDuringStartup = "<native thread without managed peer>";
154 
InitCardTable()155 void Thread::InitCardTable() {
156   tlsPtr_.card_table = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin();
157 }
158 
UnimplementedEntryPoint()159 static void UnimplementedEntryPoint() {
160   UNIMPLEMENTED(FATAL);
161 }
162 
163 void InitEntryPoints(JniEntryPoints* jpoints,
164                      QuickEntryPoints* qpoints,
165                      bool monitor_jni_entry_exit);
166 void UpdateReadBarrierEntrypoints(QuickEntryPoints* qpoints, bool is_active);
167 
SetIsGcMarkingAndUpdateEntrypoints(bool is_marking)168 void Thread::SetIsGcMarkingAndUpdateEntrypoints(bool is_marking) {
169   CHECK(kUseReadBarrier);
170   tls32_.is_gc_marking = is_marking;
171   UpdateReadBarrierEntrypoints(&tlsPtr_.quick_entrypoints, /* is_active= */ is_marking);
172 }
173 
InitTlsEntryPoints()174 void Thread::InitTlsEntryPoints() {
175   ScopedTrace trace("InitTlsEntryPoints");
176   // Insert a placeholder so we can easily tell if we call an unimplemented entry point.
177   uintptr_t* begin = reinterpret_cast<uintptr_t*>(&tlsPtr_.jni_entrypoints);
178   uintptr_t* end = reinterpret_cast<uintptr_t*>(
179       reinterpret_cast<uint8_t*>(&tlsPtr_.quick_entrypoints) + sizeof(tlsPtr_.quick_entrypoints));
180   for (uintptr_t* it = begin; it != end; ++it) {
181     *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint);
182   }
183   bool monitor_jni_entry_exit = false;
184   PaletteShouldReportJniInvocations(&monitor_jni_entry_exit);
185   if (monitor_jni_entry_exit) {
186     AtomicSetFlag(ThreadFlag::kMonitorJniEntryExit);
187   }
188   InitEntryPoints(&tlsPtr_.jni_entrypoints, &tlsPtr_.quick_entrypoints, monitor_jni_entry_exit);
189 }
190 
ResetQuickAllocEntryPointsForThread()191 void Thread::ResetQuickAllocEntryPointsForThread() {
192   ResetQuickAllocEntryPoints(&tlsPtr_.quick_entrypoints);
193 }
194 
195 class DeoptimizationContextRecord {
196  public:
DeoptimizationContextRecord(const JValue & ret_val,bool is_reference,bool from_code,ObjPtr<mirror::Throwable> pending_exception,DeoptimizationMethodType method_type,DeoptimizationContextRecord * link)197   DeoptimizationContextRecord(const JValue& ret_val,
198                               bool is_reference,
199                               bool from_code,
200                               ObjPtr<mirror::Throwable> pending_exception,
201                               DeoptimizationMethodType method_type,
202                               DeoptimizationContextRecord* link)
203       : ret_val_(ret_val),
204         is_reference_(is_reference),
205         from_code_(from_code),
206         pending_exception_(pending_exception.Ptr()),
207         deopt_method_type_(method_type),
208         link_(link) {}
209 
GetReturnValue() const210   JValue GetReturnValue() const { return ret_val_; }
IsReference() const211   bool IsReference() const { return is_reference_; }
GetFromCode() const212   bool GetFromCode() const { return from_code_; }
GetPendingException() const213   ObjPtr<mirror::Throwable> GetPendingException() const { return pending_exception_; }
GetLink() const214   DeoptimizationContextRecord* GetLink() const { return link_; }
GetReturnValueAsGCRoot()215   mirror::Object** GetReturnValueAsGCRoot() {
216     DCHECK(is_reference_);
217     return ret_val_.GetGCRoot();
218   }
GetPendingExceptionAsGCRoot()219   mirror::Object** GetPendingExceptionAsGCRoot() {
220     return reinterpret_cast<mirror::Object**>(&pending_exception_);
221   }
GetDeoptimizationMethodType() const222   DeoptimizationMethodType GetDeoptimizationMethodType() const {
223     return deopt_method_type_;
224   }
225 
226  private:
227   // The value returned by the method at the top of the stack before deoptimization.
228   JValue ret_val_;
229 
230   // Indicates whether the returned value is a reference. If so, the GC will visit it.
231   const bool is_reference_;
232 
233   // Whether the context was created from an explicit deoptimization in the code.
234   const bool from_code_;
235 
236   // The exception that was pending before deoptimization (or null if there was no pending
237   // exception).
238   mirror::Throwable* pending_exception_;
239 
240   // Whether the context was created for an (idempotent) runtime method.
241   const DeoptimizationMethodType deopt_method_type_;
242 
243   // A link to the previous DeoptimizationContextRecord.
244   DeoptimizationContextRecord* const link_;
245 
246   DISALLOW_COPY_AND_ASSIGN(DeoptimizationContextRecord);
247 };
248 
249 class StackedShadowFrameRecord {
250  public:
StackedShadowFrameRecord(ShadowFrame * shadow_frame,StackedShadowFrameType type,StackedShadowFrameRecord * link)251   StackedShadowFrameRecord(ShadowFrame* shadow_frame,
252                            StackedShadowFrameType type,
253                            StackedShadowFrameRecord* link)
254       : shadow_frame_(shadow_frame),
255         type_(type),
256         link_(link) {}
257 
GetShadowFrame() const258   ShadowFrame* GetShadowFrame() const { return shadow_frame_; }
GetType() const259   StackedShadowFrameType GetType() const { return type_; }
GetLink() const260   StackedShadowFrameRecord* GetLink() const { return link_; }
261 
262  private:
263   ShadowFrame* const shadow_frame_;
264   const StackedShadowFrameType type_;
265   StackedShadowFrameRecord* const link_;
266 
267   DISALLOW_COPY_AND_ASSIGN(StackedShadowFrameRecord);
268 };
269 
PushDeoptimizationContext(const JValue & return_value,bool is_reference,ObjPtr<mirror::Throwable> exception,bool from_code,DeoptimizationMethodType method_type)270 void Thread::PushDeoptimizationContext(const JValue& return_value,
271                                        bool is_reference,
272                                        ObjPtr<mirror::Throwable> exception,
273                                        bool from_code,
274                                        DeoptimizationMethodType method_type) {
275   DeoptimizationContextRecord* record = new DeoptimizationContextRecord(
276       return_value,
277       is_reference,
278       from_code,
279       exception,
280       method_type,
281       tlsPtr_.deoptimization_context_stack);
282   tlsPtr_.deoptimization_context_stack = record;
283 }
284 
PopDeoptimizationContext(JValue * result,ObjPtr<mirror::Throwable> * exception,bool * from_code,DeoptimizationMethodType * method_type)285 void Thread::PopDeoptimizationContext(JValue* result,
286                                       ObjPtr<mirror::Throwable>* exception,
287                                       bool* from_code,
288                                       DeoptimizationMethodType* method_type) {
289   AssertHasDeoptimizationContext();
290   DeoptimizationContextRecord* record = tlsPtr_.deoptimization_context_stack;
291   tlsPtr_.deoptimization_context_stack = record->GetLink();
292   result->SetJ(record->GetReturnValue().GetJ());
293   *exception = record->GetPendingException();
294   *from_code = record->GetFromCode();
295   *method_type = record->GetDeoptimizationMethodType();
296   delete record;
297 }
298 
AssertHasDeoptimizationContext()299 void Thread::AssertHasDeoptimizationContext() {
300   CHECK(tlsPtr_.deoptimization_context_stack != nullptr)
301       << "No deoptimization context for thread " << *this;
302 }
303 
304 enum {
305   kPermitAvailable = 0,  // Incrementing consumes the permit
306   kNoPermit = 1,  // Incrementing marks as waiter waiting
307   kNoPermitWaiterWaiting = 2
308 };
309 
Park(bool is_absolute,int64_t time)310 void Thread::Park(bool is_absolute, int64_t time) {
311   DCHECK(this == Thread::Current());
312 #if ART_USE_FUTEXES
313   // Consume the permit, or mark as waiting. This cannot cause park_state to go
314   // outside of its valid range (0, 1, 2), because in all cases where 2 is
315   // assigned it is set back to 1 before returning, and this method cannot run
316   // concurrently with itself since it operates on the current thread.
317   int old_state = tls32_.park_state_.fetch_add(1, std::memory_order_relaxed);
318   if (old_state == kNoPermit) {
319     // no permit was available. block thread until later.
320     Runtime::Current()->GetRuntimeCallbacks()->ThreadParkStart(is_absolute, time);
321     bool timed_out = false;
322     if (!is_absolute && time == 0) {
323       // Thread.getState() is documented to return waiting for untimed parks.
324       ScopedThreadSuspension sts(this, ThreadState::kWaiting);
325       DCHECK_EQ(NumberOfHeldMutexes(), 0u);
326       int result = futex(tls32_.park_state_.Address(),
327                      FUTEX_WAIT_PRIVATE,
328                      /* sleep if val = */ kNoPermitWaiterWaiting,
329                      /* timeout */ nullptr,
330                      nullptr,
331                      0);
332       // This errno check must happen before the scope is closed, to ensure that
333       // no destructors (such as ScopedThreadSuspension) overwrite errno.
334       if (result == -1) {
335         switch (errno) {
336           case EAGAIN:
337             FALLTHROUGH_INTENDED;
338           case EINTR: break;  // park() is allowed to spuriously return
339           default: PLOG(FATAL) << "Failed to park";
340         }
341       }
342     } else if (time > 0) {
343       // Only actually suspend and futex_wait if we're going to wait for some
344       // positive amount of time - the kernel will reject negative times with
345       // EINVAL, and a zero time will just noop.
346 
347       // Thread.getState() is documented to return timed wait for timed parks.
348       ScopedThreadSuspension sts(this, ThreadState::kTimedWaiting);
349       DCHECK_EQ(NumberOfHeldMutexes(), 0u);
350       timespec timespec;
351       int result = 0;
352       if (is_absolute) {
353         // Time is millis when scheduled for an absolute time
354         timespec.tv_nsec = (time % 1000) * 1000000;
355         timespec.tv_sec = SaturatedTimeT(time / 1000);
356         // This odd looking pattern is recommended by futex documentation to
357         // wait until an absolute deadline, with otherwise identical behavior to
358         // FUTEX_WAIT_PRIVATE. This also allows parkUntil() to return at the
359         // correct time when the system clock changes.
360         result = futex(tls32_.park_state_.Address(),
361                        FUTEX_WAIT_BITSET_PRIVATE | FUTEX_CLOCK_REALTIME,
362                        /* sleep if val = */ kNoPermitWaiterWaiting,
363                        &timespec,
364                        nullptr,
365                        static_cast<int>(FUTEX_BITSET_MATCH_ANY));
366       } else {
367         // Time is nanos when scheduled for a relative time
368         timespec.tv_sec = SaturatedTimeT(time / 1000000000);
369         timespec.tv_nsec = time % 1000000000;
370         result = futex(tls32_.park_state_.Address(),
371                        FUTEX_WAIT_PRIVATE,
372                        /* sleep if val = */ kNoPermitWaiterWaiting,
373                        &timespec,
374                        nullptr,
375                        0);
376       }
377       // This errno check must happen before the scope is closed, to ensure that
378       // no destructors (such as ScopedThreadSuspension) overwrite errno.
379       if (result == -1) {
380         switch (errno) {
381           case ETIMEDOUT:
382             timed_out = true;
383             FALLTHROUGH_INTENDED;
384           case EAGAIN:
385           case EINTR: break;  // park() is allowed to spuriously return
386           default: PLOG(FATAL) << "Failed to park";
387         }
388       }
389     }
390     // Mark as no longer waiting, and consume permit if there is one.
391     tls32_.park_state_.store(kNoPermit, std::memory_order_relaxed);
392     // TODO: Call to signal jvmti here
393     Runtime::Current()->GetRuntimeCallbacks()->ThreadParkFinished(timed_out);
394   } else {
395     // the fetch_add has consumed the permit. immediately return.
396     DCHECK_EQ(old_state, kPermitAvailable);
397   }
398 #else
399   #pragma clang diagnostic push
400   #pragma clang diagnostic warning "-W#warnings"
401   #warning "LockSupport.park/unpark implemented as noops without FUTEX support."
402   #pragma clang diagnostic pop
403   UNUSED(is_absolute, time);
404   UNIMPLEMENTED(WARNING);
405   sched_yield();
406 #endif
407 }
408 
Unpark()409 void Thread::Unpark() {
410 #if ART_USE_FUTEXES
411   // Set permit available; will be consumed either by fetch_add (when the thread
412   // tries to park) or store (when the parked thread is woken up)
413   if (tls32_.park_state_.exchange(kPermitAvailable, std::memory_order_relaxed)
414       == kNoPermitWaiterWaiting) {
415     int result = futex(tls32_.park_state_.Address(),
416                        FUTEX_WAKE_PRIVATE,
417                        /* number of waiters = */ 1,
418                        nullptr,
419                        nullptr,
420                        0);
421     if (result == -1) {
422       PLOG(FATAL) << "Failed to unpark";
423     }
424   }
425 #else
426   UNIMPLEMENTED(WARNING);
427 #endif
428 }
429 
PushStackedShadowFrame(ShadowFrame * sf,StackedShadowFrameType type)430 void Thread::PushStackedShadowFrame(ShadowFrame* sf, StackedShadowFrameType type) {
431   StackedShadowFrameRecord* record = new StackedShadowFrameRecord(
432       sf, type, tlsPtr_.stacked_shadow_frame_record);
433   tlsPtr_.stacked_shadow_frame_record = record;
434 }
435 
PopStackedShadowFrame(StackedShadowFrameType type,bool must_be_present)436 ShadowFrame* Thread::PopStackedShadowFrame(StackedShadowFrameType type, bool must_be_present) {
437   StackedShadowFrameRecord* record = tlsPtr_.stacked_shadow_frame_record;
438   if (must_be_present) {
439     DCHECK(record != nullptr);
440   } else {
441     if (record == nullptr || record->GetType() != type) {
442       return nullptr;
443     }
444   }
445   tlsPtr_.stacked_shadow_frame_record = record->GetLink();
446   ShadowFrame* shadow_frame = record->GetShadowFrame();
447   delete record;
448   return shadow_frame;
449 }
450 
451 class FrameIdToShadowFrame {
452  public:
Create(size_t frame_id,ShadowFrame * shadow_frame,FrameIdToShadowFrame * next,size_t num_vregs)453   static FrameIdToShadowFrame* Create(size_t frame_id,
454                                       ShadowFrame* shadow_frame,
455                                       FrameIdToShadowFrame* next,
456                                       size_t num_vregs) {
457     // Append a bool array at the end to keep track of what vregs are updated by the debugger.
458     uint8_t* memory = new uint8_t[sizeof(FrameIdToShadowFrame) + sizeof(bool) * num_vregs];
459     return new (memory) FrameIdToShadowFrame(frame_id, shadow_frame, next);
460   }
461 
Delete(FrameIdToShadowFrame * f)462   static void Delete(FrameIdToShadowFrame* f) {
463     uint8_t* memory = reinterpret_cast<uint8_t*>(f);
464     delete[] memory;
465   }
466 
GetFrameId() const467   size_t GetFrameId() const { return frame_id_; }
GetShadowFrame() const468   ShadowFrame* GetShadowFrame() const { return shadow_frame_; }
GetNext() const469   FrameIdToShadowFrame* GetNext() const { return next_; }
SetNext(FrameIdToShadowFrame * next)470   void SetNext(FrameIdToShadowFrame* next) { next_ = next; }
GetUpdatedVRegFlags()471   bool* GetUpdatedVRegFlags() {
472     return updated_vreg_flags_;
473   }
474 
475  private:
FrameIdToShadowFrame(size_t frame_id,ShadowFrame * shadow_frame,FrameIdToShadowFrame * next)476   FrameIdToShadowFrame(size_t frame_id,
477                        ShadowFrame* shadow_frame,
478                        FrameIdToShadowFrame* next)
479       : frame_id_(frame_id),
480         shadow_frame_(shadow_frame),
481         next_(next) {}
482 
483   const size_t frame_id_;
484   ShadowFrame* const shadow_frame_;
485   FrameIdToShadowFrame* next_;
486   bool updated_vreg_flags_[0];
487 
488   DISALLOW_COPY_AND_ASSIGN(FrameIdToShadowFrame);
489 };
490 
FindFrameIdToShadowFrame(FrameIdToShadowFrame * head,size_t frame_id)491 static FrameIdToShadowFrame* FindFrameIdToShadowFrame(FrameIdToShadowFrame* head,
492                                                       size_t frame_id) {
493   FrameIdToShadowFrame* found = nullptr;
494   for (FrameIdToShadowFrame* record = head; record != nullptr; record = record->GetNext()) {
495     if (record->GetFrameId() == frame_id) {
496       if (kIsDebugBuild) {
497         // Check we have at most one record for this frame.
498         CHECK(found == nullptr) << "Multiple records for the frame " << frame_id;
499         found = record;
500       } else {
501         return record;
502       }
503     }
504   }
505   return found;
506 }
507 
FindDebuggerShadowFrame(size_t frame_id)508 ShadowFrame* Thread::FindDebuggerShadowFrame(size_t frame_id) {
509   FrameIdToShadowFrame* record = FindFrameIdToShadowFrame(
510       tlsPtr_.frame_id_to_shadow_frame, frame_id);
511   if (record != nullptr) {
512     return record->GetShadowFrame();
513   }
514   return nullptr;
515 }
516 
517 // Must only be called when FindDebuggerShadowFrame(frame_id) returns non-nullptr.
GetUpdatedVRegFlags(size_t frame_id)518 bool* Thread::GetUpdatedVRegFlags(size_t frame_id) {
519   FrameIdToShadowFrame* record = FindFrameIdToShadowFrame(
520       tlsPtr_.frame_id_to_shadow_frame, frame_id);
521   CHECK(record != nullptr);
522   return record->GetUpdatedVRegFlags();
523 }
524 
FindOrCreateDebuggerShadowFrame(size_t frame_id,uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc)525 ShadowFrame* Thread::FindOrCreateDebuggerShadowFrame(size_t frame_id,
526                                                      uint32_t num_vregs,
527                                                      ArtMethod* method,
528                                                      uint32_t dex_pc) {
529   ShadowFrame* shadow_frame = FindDebuggerShadowFrame(frame_id);
530   if (shadow_frame != nullptr) {
531     return shadow_frame;
532   }
533   VLOG(deopt) << "Create pre-deopted ShadowFrame for " << ArtMethod::PrettyMethod(method);
534   shadow_frame = ShadowFrame::CreateDeoptimizedFrame(num_vregs, nullptr, method, dex_pc);
535   FrameIdToShadowFrame* record = FrameIdToShadowFrame::Create(frame_id,
536                                                               shadow_frame,
537                                                               tlsPtr_.frame_id_to_shadow_frame,
538                                                               num_vregs);
539   for (uint32_t i = 0; i < num_vregs; i++) {
540     // Do this to clear all references for root visitors.
541     shadow_frame->SetVRegReference(i, nullptr);
542     // This flag will be changed to true if the debugger modifies the value.
543     record->GetUpdatedVRegFlags()[i] = false;
544   }
545   tlsPtr_.frame_id_to_shadow_frame = record;
546   return shadow_frame;
547 }
548 
GetCustomTLS(const char * key)549 TLSData* Thread::GetCustomTLS(const char* key) {
550   MutexLock mu(Thread::Current(), *Locks::custom_tls_lock_);
551   auto it = custom_tls_.find(key);
552   return (it != custom_tls_.end()) ? it->second.get() : nullptr;
553 }
554 
SetCustomTLS(const char * key,TLSData * data)555 void Thread::SetCustomTLS(const char* key, TLSData* data) {
556   // We will swap the old data (which might be nullptr) with this and then delete it outside of the
557   // custom_tls_lock_.
558   std::unique_ptr<TLSData> old_data(data);
559   {
560     MutexLock mu(Thread::Current(), *Locks::custom_tls_lock_);
561     custom_tls_.GetOrCreate(key, []() { return std::unique_ptr<TLSData>(); }).swap(old_data);
562   }
563 }
564 
RemoveDebuggerShadowFrameMapping(size_t frame_id)565 void Thread::RemoveDebuggerShadowFrameMapping(size_t frame_id) {
566   FrameIdToShadowFrame* head = tlsPtr_.frame_id_to_shadow_frame;
567   if (head->GetFrameId() == frame_id) {
568     tlsPtr_.frame_id_to_shadow_frame = head->GetNext();
569     FrameIdToShadowFrame::Delete(head);
570     return;
571   }
572   FrameIdToShadowFrame* prev = head;
573   for (FrameIdToShadowFrame* record = head->GetNext();
574        record != nullptr;
575        prev = record, record = record->GetNext()) {
576     if (record->GetFrameId() == frame_id) {
577       prev->SetNext(record->GetNext());
578       FrameIdToShadowFrame::Delete(record);
579       return;
580     }
581   }
582   LOG(FATAL) << "No shadow frame for frame " << frame_id;
583   UNREACHABLE();
584 }
585 
InitTid()586 void Thread::InitTid() {
587   tls32_.tid = ::art::GetTid();
588 }
589 
InitAfterFork()590 void Thread::InitAfterFork() {
591   // One thread (us) survived the fork, but we have a new tid so we need to
592   // update the value stashed in this Thread*.
593   InitTid();
594 }
595 
DeleteJPeer(JNIEnv * env)596 void Thread::DeleteJPeer(JNIEnv* env) {
597   // Make sure nothing can observe both opeer and jpeer set at the same time.
598   jobject old_jpeer = tlsPtr_.jpeer;
599   CHECK(old_jpeer != nullptr);
600   tlsPtr_.jpeer = nullptr;
601   env->DeleteGlobalRef(old_jpeer);
602 }
603 
CreateCallback(void * arg)604 void* Thread::CreateCallback(void* arg) {
605   Thread* self = reinterpret_cast<Thread*>(arg);
606   Runtime* runtime = Runtime::Current();
607   if (runtime == nullptr) {
608     LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self;
609     return nullptr;
610   }
611   {
612     // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true
613     //       after self->Init().
614     MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
615     // Check that if we got here we cannot be shutting down (as shutdown should never have started
616     // while threads are being born).
617     CHECK(!runtime->IsShuttingDownLocked());
618     // Note: given that the JNIEnv is created in the parent thread, the only failure point here is
619     //       a mess in InitStackHwm. We do not have a reasonable way to recover from that, so abort
620     //       the runtime in such a case. In case this ever changes, we need to make sure here to
621     //       delete the tmp_jni_env, as we own it at this point.
622     CHECK(self->Init(runtime->GetThreadList(), runtime->GetJavaVM(), self->tlsPtr_.tmp_jni_env));
623     self->tlsPtr_.tmp_jni_env = nullptr;
624     Runtime::Current()->EndThreadBirth();
625   }
626   {
627     ScopedObjectAccess soa(self);
628     self->InitStringEntryPoints();
629 
630     // Copy peer into self, deleting global reference when done.
631     CHECK(self->tlsPtr_.jpeer != nullptr);
632     self->tlsPtr_.opeer = soa.Decode<mirror::Object>(self->tlsPtr_.jpeer).Ptr();
633     // Make sure nothing can observe both opeer and jpeer set at the same time.
634     self->DeleteJPeer(self->GetJniEnv());
635     self->SetThreadName(self->GetThreadName()->ToModifiedUtf8().c_str());
636 
637     ArtField* priorityField = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority);
638     self->SetNativePriority(priorityField->GetInt(self->tlsPtr_.opeer));
639 
640     runtime->GetRuntimeCallbacks()->ThreadStart(self);
641 
642     // Unpark ourselves if the java peer was unparked before it started (see
643     // b/28845097#comment49 for more information)
644 
645     ArtField* unparkedField = jni::DecodeArtField(
646         WellKnownClasses::java_lang_Thread_unparkedBeforeStart);
647     bool should_unpark = false;
648     {
649       // Hold the lock here, so that if another thread calls unpark before the thread starts
650       // we don't observe the unparkedBeforeStart field before the unparker writes to it,
651       // which could cause a lost unpark.
652       art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
653       should_unpark = unparkedField->GetBoolean(self->tlsPtr_.opeer) == JNI_TRUE;
654     }
655     if (should_unpark) {
656       self->Unpark();
657     }
658     // Invoke the 'run' method of our java.lang.Thread.
659     ObjPtr<mirror::Object> receiver = self->tlsPtr_.opeer;
660     jmethodID mid = WellKnownClasses::java_lang_Thread_run;
661     ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(receiver));
662     InvokeVirtualOrInterfaceWithJValues(soa, ref.get(), mid, nullptr);
663   }
664   // Detach and delete self.
665   Runtime::Current()->GetThreadList()->Unregister(self);
666 
667   return nullptr;
668 }
669 
FromManagedThread(const ScopedObjectAccessAlreadyRunnable & soa,ObjPtr<mirror::Object> thread_peer)670 Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
671                                   ObjPtr<mirror::Object> thread_peer) {
672   ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer);
673   Thread* result = reinterpret_cast64<Thread*>(f->GetLong(thread_peer));
674   // Check that if we have a result it is either suspended or we hold the thread_list_lock_
675   // to stop it from going away.
676   if (kIsDebugBuild) {
677     MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
678     if (result != nullptr && !result->IsSuspended()) {
679       Locks::thread_list_lock_->AssertHeld(soa.Self());
680     }
681   }
682   return result;
683 }
684 
FromManagedThread(const ScopedObjectAccessAlreadyRunnable & soa,jobject java_thread)685 Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
686                                   jobject java_thread) {
687   return FromManagedThread(soa, soa.Decode<mirror::Object>(java_thread));
688 }
689 
FixStackSize(size_t stack_size)690 static size_t FixStackSize(size_t stack_size) {
691   // A stack size of zero means "use the default".
692   if (stack_size == 0) {
693     stack_size = Runtime::Current()->GetDefaultStackSize();
694   }
695 
696   // Dalvik used the bionic pthread default stack size for native threads,
697   // so include that here to support apps that expect large native stacks.
698   stack_size += 1 * MB;
699 
700   // Under sanitization, frames of the interpreter may become bigger, both for C code as
701   // well as the ShadowFrame. Ensure a larger minimum size. Otherwise initialization
702   // of all core classes cannot be done in all test circumstances.
703   if (kMemoryToolIsAvailable) {
704     stack_size = std::max(2 * MB, stack_size);
705   }
706 
707   // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN.
708   if (stack_size < PTHREAD_STACK_MIN) {
709     stack_size = PTHREAD_STACK_MIN;
710   }
711 
712   if (Runtime::Current()->GetImplicitStackOverflowChecks()) {
713     // If we are going to use implicit stack checks, allocate space for the protected
714     // region at the bottom of the stack.
715     stack_size += Thread::kStackOverflowImplicitCheckSize +
716         GetStackOverflowReservedBytes(kRuntimeISA);
717   } else {
718     // It's likely that callers are trying to ensure they have at least a certain amount of
719     // stack space, so we should add our reserved space on top of what they requested, rather
720     // than implicitly take it away from them.
721     stack_size += GetStackOverflowReservedBytes(kRuntimeISA);
722   }
723 
724   // Some systems require the stack size to be a multiple of the system page size, so round up.
725   stack_size = RoundUp(stack_size, kPageSize);
726 
727   return stack_size;
728 }
729 
730 // Return the nearest page-aligned address below the current stack top.
731 NO_INLINE
FindStackTop()732 static uint8_t* FindStackTop() {
733   return reinterpret_cast<uint8_t*>(
734       AlignDown(__builtin_frame_address(0), kPageSize));
735 }
736 
737 // Install a protected region in the stack.  This is used to trigger a SIGSEGV if a stack
738 // overflow is detected.  It is located right below the stack_begin_.
739 ATTRIBUTE_NO_SANITIZE_ADDRESS
InstallImplicitProtection()740 void Thread::InstallImplicitProtection() {
741   uint8_t* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
742   // Page containing current top of stack.
743   uint8_t* stack_top = FindStackTop();
744 
745   // Try to directly protect the stack.
746   VLOG(threads) << "installing stack protected region at " << std::hex <<
747         static_cast<void*>(pregion) << " to " <<
748         static_cast<void*>(pregion + kStackOverflowProtectedSize - 1);
749   if (ProtectStack(/* fatal_on_error= */ false)) {
750     // Tell the kernel that we won't be needing these pages any more.
751     // NB. madvise will probably write zeroes into the memory (on linux it does).
752     size_t unwanted_size =
753         reinterpret_cast<uintptr_t>(stack_top) - reinterpret_cast<uintptr_t>(pregion) - kPageSize;
754     madvise(pregion, unwanted_size, MADV_DONTNEED);
755     return;
756   }
757 
758   // There is a little complexity here that deserves a special mention.  On some
759   // architectures, the stack is created using a VM_GROWSDOWN flag
760   // to prevent memory being allocated when it's not needed.  This flag makes the
761   // kernel only allocate memory for the stack by growing down in memory.  Because we
762   // want to put an mprotected region far away from that at the stack top, we need
763   // to make sure the pages for the stack are mapped in before we call mprotect.
764   //
765   // The failed mprotect in UnprotectStack is an indication of a thread with VM_GROWSDOWN
766   // with a non-mapped stack (usually only the main thread).
767   //
768   // We map in the stack by reading every page from the stack bottom (highest address)
769   // to the stack top. (We then madvise this away.) This must be done by reading from the
770   // current stack pointer downwards.
771   //
772   // Accesses too far below the current machine register corresponding to the stack pointer (e.g.,
773   // ESP on x86[-32], SP on ARM) might cause a SIGSEGV (at least on x86 with newer kernels). We
774   // thus have to move the stack pointer. We do this portably by using a recursive function with a
775   // large stack frame size.
776 
777   // (Defensively) first remove the protection on the protected region as we'll want to read
778   // and write it. Ignore errors.
779   UnprotectStack();
780 
781   VLOG(threads) << "Need to map in stack for thread at " << std::hex <<
782       static_cast<void*>(pregion);
783 
784   struct RecurseDownStack {
785     // This function has an intentionally large stack size.
786 #pragma GCC diagnostic push
787 #pragma GCC diagnostic ignored "-Wframe-larger-than="
788     NO_INLINE
789     static void Touch(uintptr_t target) {
790       volatile size_t zero = 0;
791       // Use a large local volatile array to ensure a large frame size. Do not use anything close
792       // to a full page for ASAN. It would be nice to ensure the frame size is at most a page, but
793       // there is no pragma support for this.
794       // Note: for ASAN we need to shrink the array a bit, as there's other overhead.
795       constexpr size_t kAsanMultiplier =
796 #ifdef ADDRESS_SANITIZER
797           2u;
798 #else
799           1u;
800 #endif
801       // Keep space uninitialized as it can overflow the stack otherwise (should Clang actually
802       // auto-initialize this local variable).
803       volatile char space[kPageSize - (kAsanMultiplier * 256)] __attribute__((uninitialized));
804       char sink ATTRIBUTE_UNUSED = space[zero];  // NOLINT
805       // Remove tag from the pointer. Nop in non-hwasan builds.
806       uintptr_t addr = reinterpret_cast<uintptr_t>(__hwasan_tag_pointer(space, 0));
807       if (addr >= target + kPageSize) {
808         Touch(target);
809       }
810       zero *= 2;  // Try to avoid tail recursion.
811     }
812 #pragma GCC diagnostic pop
813   };
814   RecurseDownStack::Touch(reinterpret_cast<uintptr_t>(pregion));
815 
816   VLOG(threads) << "(again) installing stack protected region at " << std::hex <<
817       static_cast<void*>(pregion) << " to " <<
818       static_cast<void*>(pregion + kStackOverflowProtectedSize - 1);
819 
820   // Protect the bottom of the stack to prevent read/write to it.
821   ProtectStack(/* fatal_on_error= */ true);
822 
823   // Tell the kernel that we won't be needing these pages any more.
824   // NB. madvise will probably write zeroes into the memory (on linux it does).
825   size_t unwanted_size =
826       reinterpret_cast<uintptr_t>(stack_top) - reinterpret_cast<uintptr_t>(pregion) - kPageSize;
827   madvise(pregion, unwanted_size, MADV_DONTNEED);
828 }
829 
CreateNativeThread(JNIEnv * env,jobject java_peer,size_t stack_size,bool is_daemon)830 void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
831   CHECK(java_peer != nullptr);
832   Thread* self = static_cast<JNIEnvExt*>(env)->GetSelf();
833 
834   if (VLOG_IS_ON(threads)) {
835     ScopedObjectAccess soa(env);
836 
837     ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name);
838     ObjPtr<mirror::String> java_name =
839         f->GetObject(soa.Decode<mirror::Object>(java_peer))->AsString();
840     std::string thread_name;
841     if (java_name != nullptr) {
842       thread_name = java_name->ToModifiedUtf8();
843     } else {
844       thread_name = "(Unnamed)";
845     }
846 
847     VLOG(threads) << "Creating native thread for " << thread_name;
848     self->Dump(LOG_STREAM(INFO));
849   }
850 
851   Runtime* runtime = Runtime::Current();
852 
853   // Atomically start the birth of the thread ensuring the runtime isn't shutting down.
854   bool thread_start_during_shutdown = false;
855   {
856     MutexLock mu(self, *Locks::runtime_shutdown_lock_);
857     if (runtime->IsShuttingDownLocked()) {
858       thread_start_during_shutdown = true;
859     } else {
860       runtime->StartThreadBirth();
861     }
862   }
863   if (thread_start_during_shutdown) {
864     ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError"));
865     env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown");
866     return;
867   }
868 
869   Thread* child_thread = new Thread(is_daemon);
870   // Use global JNI ref to hold peer live while child thread starts.
871   child_thread->tlsPtr_.jpeer = env->NewGlobalRef(java_peer);
872   stack_size = FixStackSize(stack_size);
873 
874   // Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing
875   // to assign it.
876   env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer,
877                     reinterpret_cast<jlong>(child_thread));
878 
879   // Try to allocate a JNIEnvExt for the thread. We do this here as we might be out of memory and
880   // do not have a good way to report this on the child's side.
881   std::string error_msg;
882   std::unique_ptr<JNIEnvExt> child_jni_env_ext(
883       JNIEnvExt::Create(child_thread, Runtime::Current()->GetJavaVM(), &error_msg));
884 
885   int pthread_create_result = 0;
886   if (child_jni_env_ext.get() != nullptr) {
887     pthread_t new_pthread;
888     pthread_attr_t attr;
889     child_thread->tlsPtr_.tmp_jni_env = child_jni_env_ext.get();
890     CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
891     CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED),
892                        "PTHREAD_CREATE_DETACHED");
893     CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
894     pthread_create_result = pthread_create(&new_pthread,
895                                            &attr,
896                                            Thread::CreateCallback,
897                                            child_thread);
898     CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
899 
900     if (pthread_create_result == 0) {
901       // pthread_create started the new thread. The child is now responsible for managing the
902       // JNIEnvExt we created.
903       // Note: we can't check for tmp_jni_env == nullptr, as that would require synchronization
904       //       between the threads.
905       child_jni_env_ext.release();  // NOLINT pthreads API.
906       return;
907     }
908   }
909 
910   // Either JNIEnvExt::Create or pthread_create(3) failed, so clean up.
911   {
912     MutexLock mu(self, *Locks::runtime_shutdown_lock_);
913     runtime->EndThreadBirth();
914   }
915   // Manually delete the global reference since Thread::Init will not have been run. Make sure
916   // nothing can observe both opeer and jpeer set at the same time.
917   child_thread->DeleteJPeer(env);
918   delete child_thread;
919   child_thread = nullptr;
920   // TODO: remove from thread group?
921   env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0);
922   {
923     std::string msg(child_jni_env_ext.get() == nullptr ?
924         StringPrintf("Could not allocate JNI Env: %s", error_msg.c_str()) :
925         StringPrintf("pthread_create (%s stack) failed: %s",
926                                  PrettySize(stack_size).c_str(), strerror(pthread_create_result)));
927     ScopedObjectAccess soa(env);
928     soa.Self()->ThrowOutOfMemoryError(msg.c_str());
929   }
930 }
931 
Init(ThreadList * thread_list,JavaVMExt * java_vm,JNIEnvExt * jni_env_ext)932 bool Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm, JNIEnvExt* jni_env_ext) {
933   // This function does all the initialization that must be run by the native thread it applies to.
934   // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so
935   // we can handshake with the corresponding native thread when it's ready.) Check this native
936   // thread hasn't been through here already...
937   CHECK(Thread::Current() == nullptr);
938 
939   // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this
940   // avoids pthread_self_ ever being invalid when discovered from Thread::Current().
941   tlsPtr_.pthread_self = pthread_self();
942   CHECK(is_started_);
943 
944   ScopedTrace trace("Thread::Init");
945 
946   SetUpAlternateSignalStack();
947   if (!InitStackHwm()) {
948     return false;
949   }
950   InitCpu();
951   InitTlsEntryPoints();
952   RemoveSuspendTrigger();
953   InitCardTable();
954   InitTid();
955 
956 #ifdef __BIONIC__
957   __get_tls()[TLS_SLOT_ART_THREAD_SELF] = this;
958 #else
959   CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self");
960   Thread::self_tls_ = this;
961 #endif
962   DCHECK_EQ(Thread::Current(), this);
963 
964   tls32_.thin_lock_thread_id = thread_list->AllocThreadId(this);
965 
966   if (jni_env_ext != nullptr) {
967     DCHECK_EQ(jni_env_ext->GetVm(), java_vm);
968     DCHECK_EQ(jni_env_ext->GetSelf(), this);
969     tlsPtr_.jni_env = jni_env_ext;
970   } else {
971     std::string error_msg;
972     tlsPtr_.jni_env = JNIEnvExt::Create(this, java_vm, &error_msg);
973     if (tlsPtr_.jni_env == nullptr) {
974       LOG(ERROR) << "Failed to create JNIEnvExt: " << error_msg;
975       return false;
976     }
977   }
978 
979   ScopedTrace trace3("ThreadList::Register");
980   thread_list->Register(this);
981   return true;
982 }
983 
984 template <typename PeerAction>
Attach(const char * thread_name,bool as_daemon,PeerAction peer_action)985 Thread* Thread::Attach(const char* thread_name, bool as_daemon, PeerAction peer_action) {
986   Runtime* runtime = Runtime::Current();
987   ScopedTrace trace("Thread::Attach");
988   if (runtime == nullptr) {
989     LOG(ERROR) << "Thread attaching to non-existent runtime: " <<
990         ((thread_name != nullptr) ? thread_name : "(Unnamed)");
991     return nullptr;
992   }
993   Thread* self;
994   {
995     ScopedTrace trace2("Thread birth");
996     MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
997     if (runtime->IsShuttingDownLocked()) {
998       LOG(WARNING) << "Thread attaching while runtime is shutting down: " <<
999           ((thread_name != nullptr) ? thread_name : "(Unnamed)");
1000       return nullptr;
1001     } else {
1002       Runtime::Current()->StartThreadBirth();
1003       self = new Thread(as_daemon);
1004       bool init_success = self->Init(runtime->GetThreadList(), runtime->GetJavaVM());
1005       Runtime::Current()->EndThreadBirth();
1006       if (!init_success) {
1007         delete self;
1008         return nullptr;
1009       }
1010     }
1011   }
1012 
1013   self->InitStringEntryPoints();
1014 
1015   CHECK_NE(self->GetState(), ThreadState::kRunnable);
1016   self->SetState(ThreadState::kNative);
1017 
1018   // Run the action that is acting on the peer.
1019   if (!peer_action(self)) {
1020     runtime->GetThreadList()->Unregister(self);
1021     // Unregister deletes self, no need to do this here.
1022     return nullptr;
1023   }
1024 
1025   if (VLOG_IS_ON(threads)) {
1026     if (thread_name != nullptr) {
1027       VLOG(threads) << "Attaching thread " << thread_name;
1028     } else {
1029       VLOG(threads) << "Attaching unnamed thread.";
1030     }
1031     ScopedObjectAccess soa(self);
1032     self->Dump(LOG_STREAM(INFO));
1033   }
1034 
1035   {
1036     ScopedObjectAccess soa(self);
1037     runtime->GetRuntimeCallbacks()->ThreadStart(self);
1038   }
1039 
1040   return self;
1041 }
1042 
Attach(const char * thread_name,bool as_daemon,jobject thread_group,bool create_peer)1043 Thread* Thread::Attach(const char* thread_name,
1044                        bool as_daemon,
1045                        jobject thread_group,
1046                        bool create_peer) {
1047   auto create_peer_action = [&](Thread* self) {
1048     // If we're the main thread, ClassLinker won't be created until after we're attached,
1049     // so that thread needs a two-stage attach. Regular threads don't need this hack.
1050     // In the compiler, all threads need this hack, because no-one's going to be getting
1051     // a native peer!
1052     if (create_peer) {
1053       self->CreatePeer(thread_name, as_daemon, thread_group);
1054       if (self->IsExceptionPending()) {
1055         // We cannot keep the exception around, as we're deleting self. Try to be helpful and log
1056         // the failure but do not dump the exception details. If we fail to allocate the peer, we
1057         // usually also fail to allocate an exception object and throw a pre-allocated OOME without
1058         // any useful information. If we do manage to allocate the exception object, the memory
1059         // information in the message could have been collected too late and therefore misleading.
1060         {
1061           ScopedObjectAccess soa(self);
1062           LOG(ERROR) << "Exception creating thread peer: "
1063                      << ((thread_name != nullptr) ? thread_name : "<null>");
1064           self->ClearException();
1065         }
1066         return false;
1067       }
1068     } else {
1069       // These aren't necessary, but they improve diagnostics for unit tests & command-line tools.
1070       if (thread_name != nullptr) {
1071         self->SetCachedThreadName(thread_name);
1072         ::art::SetThreadName(thread_name);
1073       } else if (self->GetJniEnv()->IsCheckJniEnabled()) {
1074         LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
1075       }
1076     }
1077     return true;
1078   };
1079   return Attach(thread_name, as_daemon, create_peer_action);
1080 }
1081 
Attach(const char * thread_name,bool as_daemon,jobject thread_peer)1082 Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_peer) {
1083   auto set_peer_action = [&](Thread* self) {
1084     // Install the given peer.
1085     {
1086       DCHECK(self == Thread::Current());
1087       ScopedObjectAccess soa(self);
1088       self->tlsPtr_.opeer = soa.Decode<mirror::Object>(thread_peer).Ptr();
1089     }
1090     self->GetJniEnv()->SetLongField(thread_peer,
1091                                     WellKnownClasses::java_lang_Thread_nativePeer,
1092                                     reinterpret_cast64<jlong>(self));
1093     return true;
1094   };
1095   return Attach(thread_name, as_daemon, set_peer_action);
1096 }
1097 
CreatePeer(const char * name,bool as_daemon,jobject thread_group)1098 void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) {
1099   Runtime* runtime = Runtime::Current();
1100   CHECK(runtime->IsStarted());
1101   JNIEnv* env = tlsPtr_.jni_env;
1102 
1103   if (thread_group == nullptr) {
1104     thread_group = runtime->GetMainThreadGroup();
1105   }
1106   ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
1107   // Add missing null check in case of OOM b/18297817
1108   if (name != nullptr && thread_name.get() == nullptr) {
1109     CHECK(IsExceptionPending());
1110     return;
1111   }
1112   jint thread_priority = GetNativePriority();
1113   jboolean thread_is_daemon = as_daemon;
1114 
1115   ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
1116   if (peer.get() == nullptr) {
1117     CHECK(IsExceptionPending());
1118     return;
1119   }
1120   {
1121     ScopedObjectAccess soa(this);
1122     tlsPtr_.opeer = soa.Decode<mirror::Object>(peer.get()).Ptr();
1123   }
1124   env->CallNonvirtualVoidMethod(peer.get(),
1125                                 WellKnownClasses::java_lang_Thread,
1126                                 WellKnownClasses::java_lang_Thread_init,
1127                                 thread_group, thread_name.get(), thread_priority, thread_is_daemon);
1128   if (IsExceptionPending()) {
1129     return;
1130   }
1131 
1132   Thread* self = this;
1133   DCHECK_EQ(self, Thread::Current());
1134   env->SetLongField(peer.get(),
1135                     WellKnownClasses::java_lang_Thread_nativePeer,
1136                     reinterpret_cast64<jlong>(self));
1137 
1138   ScopedObjectAccess soa(self);
1139   StackHandleScope<1> hs(self);
1140   MutableHandle<mirror::String> peer_thread_name(hs.NewHandle(GetThreadName()));
1141   if (peer_thread_name == nullptr) {
1142     // The Thread constructor should have set the Thread.name to a
1143     // non-null value. However, because we can run without code
1144     // available (in the compiler, in tests), we manually assign the
1145     // fields the constructor should have set.
1146     if (runtime->IsActiveTransaction()) {
1147       InitPeer<true>(soa,
1148                      tlsPtr_.opeer,
1149                      thread_is_daemon,
1150                      thread_group,
1151                      thread_name.get(),
1152                      thread_priority);
1153     } else {
1154       InitPeer<false>(soa,
1155                       tlsPtr_.opeer,
1156                       thread_is_daemon,
1157                       thread_group,
1158                       thread_name.get(),
1159                       thread_priority);
1160     }
1161     peer_thread_name.Assign(GetThreadName());
1162   }
1163   // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null.
1164   if (peer_thread_name != nullptr) {
1165     SetThreadName(peer_thread_name->ToModifiedUtf8().c_str());
1166   }
1167 }
1168 
CreateCompileTimePeer(JNIEnv * env,const char * name,bool as_daemon,jobject thread_group)1169 jobject Thread::CreateCompileTimePeer(JNIEnv* env,
1170                                       const char* name,
1171                                       bool as_daemon,
1172                                       jobject thread_group) {
1173   Runtime* runtime = Runtime::Current();
1174   CHECK(!runtime->IsStarted());
1175 
1176   if (thread_group == nullptr) {
1177     thread_group = runtime->GetMainThreadGroup();
1178   }
1179   ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
1180   // Add missing null check in case of OOM b/18297817
1181   if (name != nullptr && thread_name.get() == nullptr) {
1182     CHECK(Thread::Current()->IsExceptionPending());
1183     return nullptr;
1184   }
1185   jint thread_priority = kNormThreadPriority;  // Always normalize to NORM priority.
1186   jboolean thread_is_daemon = as_daemon;
1187 
1188   ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
1189   if (peer.get() == nullptr) {
1190     CHECK(Thread::Current()->IsExceptionPending());
1191     return nullptr;
1192   }
1193 
1194   // We cannot call Thread.init, as it will recursively ask for currentThread.
1195 
1196   // The Thread constructor should have set the Thread.name to a
1197   // non-null value. However, because we can run without code
1198   // available (in the compiler, in tests), we manually assign the
1199   // fields the constructor should have set.
1200   ScopedObjectAccessUnchecked soa(Thread::Current());
1201   if (runtime->IsActiveTransaction()) {
1202     InitPeer<true>(soa,
1203                    soa.Decode<mirror::Object>(peer.get()),
1204                    thread_is_daemon,
1205                    thread_group,
1206                    thread_name.get(),
1207                    thread_priority);
1208   } else {
1209     InitPeer<false>(soa,
1210                     soa.Decode<mirror::Object>(peer.get()),
1211                     thread_is_daemon,
1212                     thread_group,
1213                     thread_name.get(),
1214                     thread_priority);
1215   }
1216 
1217   return peer.release();
1218 }
1219 
1220 template<bool kTransactionActive>
InitPeer(ScopedObjectAccessAlreadyRunnable & soa,ObjPtr<mirror::Object> peer,jboolean thread_is_daemon,jobject thread_group,jobject thread_name,jint thread_priority)1221 void Thread::InitPeer(ScopedObjectAccessAlreadyRunnable& soa,
1222                       ObjPtr<mirror::Object> peer,
1223                       jboolean thread_is_daemon,
1224                       jobject thread_group,
1225                       jobject thread_name,
1226                       jint thread_priority) {
1227   jni::DecodeArtField(WellKnownClasses::java_lang_Thread_daemon)->
1228       SetBoolean<kTransactionActive>(peer, thread_is_daemon);
1229   jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)->
1230       SetObject<kTransactionActive>(peer, soa.Decode<mirror::Object>(thread_group));
1231   jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name)->
1232       SetObject<kTransactionActive>(peer, soa.Decode<mirror::Object>(thread_name));
1233   jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority)->
1234       SetInt<kTransactionActive>(peer, thread_priority);
1235 }
1236 
SetCachedThreadName(const char * name)1237 void Thread::SetCachedThreadName(const char* name) {
1238   DCHECK(name != kThreadNameDuringStartup);
1239   const char* old_name = tlsPtr_.name.exchange(name == nullptr ? nullptr : strdup(name));
1240   if (old_name != nullptr && old_name !=  kThreadNameDuringStartup) {
1241     // Deallocate it, carefully. Note that the load has to be ordered wrt the store of the xchg.
1242     for (uint32_t i = 0; UNLIKELY(tls32_.num_name_readers.load(std::memory_order_seq_cst) != 0);
1243          ++i) {
1244       static constexpr uint32_t kNumSpins = 1000;
1245       // Ugly, but keeps us from having to do anything on the reader side.
1246       if (i > kNumSpins) {
1247         usleep(500);
1248       }
1249     }
1250     // We saw the reader count drop to zero since we replaced the name; old one is now safe to
1251     // deallocate.
1252     free(const_cast<char *>(old_name));
1253   }
1254 }
1255 
SetThreadName(const char * name)1256 void Thread::SetThreadName(const char* name) {
1257   SetCachedThreadName(name);
1258   ::art::SetThreadName(name);
1259   Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM"));
1260 }
1261 
GetThreadStack(pthread_t thread,void ** stack_base,size_t * stack_size,size_t * guard_size)1262 static void GetThreadStack(pthread_t thread,
1263                            void** stack_base,
1264                            size_t* stack_size,
1265                            size_t* guard_size) {
1266 #if defined(__APPLE__)
1267   *stack_size = pthread_get_stacksize_np(thread);
1268   void* stack_addr = pthread_get_stackaddr_np(thread);
1269 
1270   // Check whether stack_addr is the base or end of the stack.
1271   // (On Mac OS 10.7, it's the end.)
1272   int stack_variable;
1273   if (stack_addr > &stack_variable) {
1274     *stack_base = reinterpret_cast<uint8_t*>(stack_addr) - *stack_size;
1275   } else {
1276     *stack_base = stack_addr;
1277   }
1278 
1279   // This is wrong, but there doesn't seem to be a way to get the actual value on the Mac.
1280   pthread_attr_t attributes;
1281   CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__);
1282   CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
1283   CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
1284 #else
1285   pthread_attr_t attributes;
1286   CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__);
1287   CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__);
1288   CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
1289   CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
1290 
1291 #if defined(__GLIBC__)
1292   // If we're the main thread, check whether we were run with an unlimited stack. In that case,
1293   // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection
1294   // will be broken because we'll die long before we get close to 2GB.
1295   bool is_main_thread = (::art::GetTid() == static_cast<uint32_t>(getpid()));
1296   if (is_main_thread) {
1297     rlimit stack_limit;
1298     if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
1299       PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed";
1300     }
1301     if (stack_limit.rlim_cur == RLIM_INFINITY) {
1302       size_t old_stack_size = *stack_size;
1303 
1304       // Use the kernel default limit as our size, and adjust the base to match.
1305       *stack_size = 8 * MB;
1306       *stack_base = reinterpret_cast<uint8_t*>(*stack_base) + (old_stack_size - *stack_size);
1307 
1308       VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")"
1309                     << " to " << PrettySize(*stack_size)
1310                     << " with base " << *stack_base;
1311     }
1312   }
1313 #endif
1314 
1315 #endif
1316 }
1317 
InitStackHwm()1318 bool Thread::InitStackHwm() {
1319   ScopedTrace trace("InitStackHwm");
1320   void* read_stack_base;
1321   size_t read_stack_size;
1322   size_t read_guard_size;
1323   GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size, &read_guard_size);
1324 
1325   tlsPtr_.stack_begin = reinterpret_cast<uint8_t*>(read_stack_base);
1326   tlsPtr_.stack_size = read_stack_size;
1327 
1328   // The minimum stack size we can cope with is the overflow reserved bytes (typically
1329   // 8K) + the protected region size (4K) + another page (4K).  Typically this will
1330   // be 8+4+4 = 16K.  The thread won't be able to do much with this stack even the GC takes
1331   // between 8K and 12K.
1332   uint32_t min_stack = GetStackOverflowReservedBytes(kRuntimeISA) + kStackOverflowProtectedSize
1333     + 4 * KB;
1334   if (read_stack_size <= min_stack) {
1335     // Note, as we know the stack is small, avoid operations that could use a lot of stack.
1336     LogHelper::LogLineLowStack(__PRETTY_FUNCTION__,
1337                                __LINE__,
1338                                ::android::base::ERROR,
1339                                "Attempt to attach a thread with a too-small stack");
1340     return false;
1341   }
1342 
1343   // This is included in the SIGQUIT output, but it's useful here for thread debugging.
1344   VLOG(threads) << StringPrintf("Native stack is at %p (%s with %s guard)",
1345                                 read_stack_base,
1346                                 PrettySize(read_stack_size).c_str(),
1347                                 PrettySize(read_guard_size).c_str());
1348 
1349   // Set stack_end_ to the bottom of the stack saving space of stack overflows
1350 
1351   Runtime* runtime = Runtime::Current();
1352   bool implicit_stack_check =
1353       runtime->GetImplicitStackOverflowChecks() && !runtime->IsAotCompiler();
1354 
1355   ResetDefaultStackEnd();
1356 
1357   // Install the protected region if we are doing implicit overflow checks.
1358   if (implicit_stack_check) {
1359     // The thread might have protected region at the bottom.  We need
1360     // to install our own region so we need to move the limits
1361     // of the stack to make room for it.
1362 
1363     tlsPtr_.stack_begin += read_guard_size + kStackOverflowProtectedSize;
1364     tlsPtr_.stack_end += read_guard_size + kStackOverflowProtectedSize;
1365     tlsPtr_.stack_size -= read_guard_size + kStackOverflowProtectedSize;
1366 
1367     InstallImplicitProtection();
1368   }
1369 
1370   // Consistency check.
1371   CHECK_GT(FindStackTop(), reinterpret_cast<void*>(tlsPtr_.stack_end));
1372 
1373   return true;
1374 }
1375 
ShortDump(std::ostream & os) const1376 void Thread::ShortDump(std::ostream& os) const {
1377   os << "Thread[";
1378   if (GetThreadId() != 0) {
1379     // If we're in kStarting, we won't have a thin lock id or tid yet.
1380     os << GetThreadId()
1381        << ",tid=" << GetTid() << ',';
1382   }
1383   tls32_.num_name_readers.fetch_add(1, std::memory_order_seq_cst);
1384   const char* name = tlsPtr_.name.load();
1385   os << GetState()
1386      << ",Thread*=" << this
1387      << ",peer=" << tlsPtr_.opeer
1388      << ",\"" << (name == nullptr ? "null" : name) << "\""
1389      << "]";
1390   tls32_.num_name_readers.fetch_sub(1 /* at least memory_order_release */);
1391 }
1392 
Dump(std::ostream & os,bool dump_native_stack,BacktraceMap * backtrace_map,bool force_dump_stack) const1393 void Thread::Dump(std::ostream& os, bool dump_native_stack, BacktraceMap* backtrace_map,
1394                   bool force_dump_stack) const {
1395   DumpState(os);
1396   DumpStack(os, dump_native_stack, backtrace_map, force_dump_stack);
1397 }
1398 
GetThreadName() const1399 ObjPtr<mirror::String> Thread::GetThreadName() const {
1400   ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name);
1401   if (tlsPtr_.opeer == nullptr) {
1402     return nullptr;
1403   }
1404   ObjPtr<mirror::Object> name = f->GetObject(tlsPtr_.opeer);
1405   return name == nullptr ? nullptr : name->AsString();
1406 }
1407 
GetThreadName(std::string & name) const1408 void Thread::GetThreadName(std::string& name) const {
1409   tls32_.num_name_readers.fetch_add(1, std::memory_order_seq_cst);
1410   // The store part of the increment has to be ordered with respect to the following load.
1411   name.assign(tlsPtr_.name.load(std::memory_order_seq_cst));
1412   tls32_.num_name_readers.fetch_sub(1 /* at least memory_order_release */);
1413 }
1414 
GetCpuMicroTime() const1415 uint64_t Thread::GetCpuMicroTime() const {
1416 #if defined(__linux__)
1417   clockid_t cpu_clock_id;
1418   pthread_getcpuclockid(tlsPtr_.pthread_self, &cpu_clock_id);
1419   timespec now;
1420   clock_gettime(cpu_clock_id, &now);
1421   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) +
1422          static_cast<uint64_t>(now.tv_nsec) / UINT64_C(1000);
1423 #else  // __APPLE__
1424   UNIMPLEMENTED(WARNING);
1425   return -1;
1426 #endif
1427 }
1428 
1429 // Attempt to rectify locks so that we dump thread list with required locks before exiting.
UnsafeLogFatalForSuspendCount(Thread * self,Thread * thread)1430 static void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
1431   LOG(ERROR) << *thread << " suspend count already zero.";
1432   Locks::thread_suspend_count_lock_->Unlock(self);
1433   if (!Locks::mutator_lock_->IsSharedHeld(self)) {
1434     Locks::mutator_lock_->SharedTryLock(self);
1435     if (!Locks::mutator_lock_->IsSharedHeld(self)) {
1436       LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
1437     }
1438   }
1439   if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
1440     Locks::thread_list_lock_->TryLock(self);
1441     if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
1442       LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
1443     }
1444   }
1445   std::ostringstream ss;
1446   Runtime::Current()->GetThreadList()->Dump(ss);
1447   LOG(FATAL) << ss.str();
1448 }
1449 
ModifySuspendCountInternal(Thread * self,int delta,AtomicInteger * suspend_barrier,SuspendReason reason)1450 bool Thread::ModifySuspendCountInternal(Thread* self,
1451                                         int delta,
1452                                         AtomicInteger* suspend_barrier,
1453                                         SuspendReason reason) {
1454   if (kIsDebugBuild) {
1455     DCHECK(delta == -1 || delta == +1)
1456           << reason << " " << delta << " " << this;
1457     Locks::thread_suspend_count_lock_->AssertHeld(self);
1458     if (this != self && !IsSuspended()) {
1459       Locks::thread_list_lock_->AssertHeld(self);
1460     }
1461   }
1462   // User code suspensions need to be checked more closely since they originate from code outside of
1463   // the runtime's control.
1464   if (UNLIKELY(reason == SuspendReason::kForUserCode)) {
1465     Locks::user_code_suspension_lock_->AssertHeld(self);
1466     if (UNLIKELY(delta + tls32_.user_code_suspend_count < 0)) {
1467       LOG(ERROR) << "attempting to modify suspend count in an illegal way.";
1468       return false;
1469     }
1470   }
1471   if (UNLIKELY(delta < 0 && tls32_.suspend_count <= 0)) {
1472     UnsafeLogFatalForSuspendCount(self, this);
1473     return false;
1474   }
1475 
1476   if (kUseReadBarrier && delta > 0 && this != self && tlsPtr_.flip_function != nullptr) {
1477     // Force retry of a suspend request if it's in the middle of a thread flip to avoid a
1478     // deadlock. b/31683379.
1479     return false;
1480   }
1481 
1482   uint32_t flags = enum_cast<uint32_t>(ThreadFlag::kSuspendRequest);
1483   if (delta > 0 && suspend_barrier != nullptr) {
1484     uint32_t available_barrier = kMaxSuspendBarriers;
1485     for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
1486       if (tlsPtr_.active_suspend_barriers[i] == nullptr) {
1487         available_barrier = i;
1488         break;
1489       }
1490     }
1491     if (available_barrier == kMaxSuspendBarriers) {
1492       // No barrier spaces available, we can't add another.
1493       return false;
1494     }
1495     tlsPtr_.active_suspend_barriers[available_barrier] = suspend_barrier;
1496     flags |= enum_cast<uint32_t>(ThreadFlag::kActiveSuspendBarrier);
1497   }
1498 
1499   tls32_.suspend_count += delta;
1500   switch (reason) {
1501     case SuspendReason::kForUserCode:
1502       tls32_.user_code_suspend_count += delta;
1503       break;
1504     case SuspendReason::kInternal:
1505       break;
1506   }
1507 
1508   if (tls32_.suspend_count == 0) {
1509     AtomicClearFlag(ThreadFlag::kSuspendRequest);
1510   } else {
1511     // Two bits might be set simultaneously.
1512     tls32_.state_and_flags.fetch_or(flags, std::memory_order_seq_cst);
1513     TriggerSuspend();
1514   }
1515   return true;
1516 }
1517 
PassActiveSuspendBarriers(Thread * self)1518 bool Thread::PassActiveSuspendBarriers(Thread* self) {
1519   // Grab the suspend_count lock and copy the current set of
1520   // barriers. Then clear the list and the flag. The ModifySuspendCount
1521   // function requires the lock so we prevent a race between setting
1522   // the kActiveSuspendBarrier flag and clearing it.
1523   AtomicInteger* pass_barriers[kMaxSuspendBarriers];
1524   {
1525     MutexLock mu(self, *Locks::thread_suspend_count_lock_);
1526     if (!ReadFlag(ThreadFlag::kActiveSuspendBarrier)) {
1527       // quick exit test: the barriers have already been claimed - this is
1528       // possible as there may be a race to claim and it doesn't matter
1529       // who wins.
1530       // All of the callers of this function (except the SuspendAllInternal)
1531       // will first test the kActiveSuspendBarrier flag without lock. Here
1532       // double-check whether the barrier has been passed with the
1533       // suspend_count lock.
1534       return false;
1535     }
1536 
1537     for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
1538       pass_barriers[i] = tlsPtr_.active_suspend_barriers[i];
1539       tlsPtr_.active_suspend_barriers[i] = nullptr;
1540     }
1541     AtomicClearFlag(ThreadFlag::kActiveSuspendBarrier);
1542   }
1543 
1544   uint32_t barrier_count = 0;
1545   for (uint32_t i = 0; i < kMaxSuspendBarriers; i++) {
1546     AtomicInteger* pending_threads = pass_barriers[i];
1547     if (pending_threads != nullptr) {
1548       bool done = false;
1549       do {
1550         int32_t cur_val = pending_threads->load(std::memory_order_relaxed);
1551         CHECK_GT(cur_val, 0) << "Unexpected value for PassActiveSuspendBarriers(): " << cur_val;
1552         // Reduce value by 1.
1553         done = pending_threads->CompareAndSetWeakRelaxed(cur_val, cur_val - 1);
1554 #if ART_USE_FUTEXES
1555         if (done && (cur_val - 1) == 0) {  // Weak CAS may fail spuriously.
1556           futex(pending_threads->Address(), FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0);
1557         }
1558 #endif
1559       } while (!done);
1560       ++barrier_count;
1561     }
1562   }
1563   CHECK_GT(barrier_count, 0U);
1564   return true;
1565 }
1566 
ClearSuspendBarrier(AtomicInteger * target)1567 void Thread::ClearSuspendBarrier(AtomicInteger* target) {
1568   CHECK(ReadFlag(ThreadFlag::kActiveSuspendBarrier));
1569   bool clear_flag = true;
1570   for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
1571     AtomicInteger* ptr = tlsPtr_.active_suspend_barriers[i];
1572     if (ptr == target) {
1573       tlsPtr_.active_suspend_barriers[i] = nullptr;
1574     } else if (ptr != nullptr) {
1575       clear_flag = false;
1576     }
1577   }
1578   if (LIKELY(clear_flag)) {
1579     AtomicClearFlag(ThreadFlag::kActiveSuspendBarrier);
1580   }
1581 }
1582 
RunCheckpointFunction()1583 void Thread::RunCheckpointFunction() {
1584   // If this thread is suspended and another thread is running the checkpoint on its behalf,
1585   // we may have a pending flip function that we need to run for the sake of those checkpoints
1586   // that need to walk the stack. We should not see the flip function flags when the thread
1587   // is running the checkpoint on its own.
1588   StateAndFlags state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
1589   if (UNLIKELY(state_and_flags.IsAnyOfFlagsSet(FlipFunctionFlags()))) {
1590     DCHECK(IsSuspended());
1591     Thread* self = Thread::Current();
1592     DCHECK(self != this);
1593     if (state_and_flags.IsFlagSet(ThreadFlag::kPendingFlipFunction)) {
1594       EnsureFlipFunctionStarted(self);
1595       state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
1596       DCHECK(!state_and_flags.IsFlagSet(ThreadFlag::kPendingFlipFunction));
1597     }
1598     if (state_and_flags.IsFlagSet(ThreadFlag::kRunningFlipFunction)) {
1599       WaitForFlipFunction(self);
1600     }
1601   }
1602 
1603   // Grab the suspend_count lock, get the next checkpoint and update all the checkpoint fields. If
1604   // there are no more checkpoints we will also clear the kCheckpointRequest flag.
1605   Closure* checkpoint;
1606   {
1607     MutexLock mu(this, *Locks::thread_suspend_count_lock_);
1608     checkpoint = tlsPtr_.checkpoint_function;
1609     if (!checkpoint_overflow_.empty()) {
1610       // Overflow list not empty, copy the first one out and continue.
1611       tlsPtr_.checkpoint_function = checkpoint_overflow_.front();
1612       checkpoint_overflow_.pop_front();
1613     } else {
1614       // No overflow checkpoints. Clear the kCheckpointRequest flag
1615       tlsPtr_.checkpoint_function = nullptr;
1616       AtomicClearFlag(ThreadFlag::kCheckpointRequest);
1617     }
1618   }
1619   // Outside the lock, run the checkpoint function.
1620   ScopedTrace trace("Run checkpoint function");
1621   CHECK(checkpoint != nullptr) << "Checkpoint flag set without pending checkpoint";
1622   checkpoint->Run(this);
1623 }
1624 
RunEmptyCheckpoint()1625 void Thread::RunEmptyCheckpoint() {
1626   // Note: Empty checkpoint does not access the thread's stack,
1627   // so we do not need to check for the flip function.
1628   DCHECK_EQ(Thread::Current(), this);
1629   AtomicClearFlag(ThreadFlag::kEmptyCheckpointRequest);
1630   Runtime::Current()->GetThreadList()->EmptyCheckpointBarrier()->Pass(this);
1631 }
1632 
RequestCheckpoint(Closure * function)1633 bool Thread::RequestCheckpoint(Closure* function) {
1634   StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
1635   if (old_state_and_flags.GetState() != ThreadState::kRunnable) {
1636     return false;  // Fail, thread is suspended and so can't run a checkpoint.
1637   }
1638 
1639   // We must be runnable to request a checkpoint.
1640   DCHECK_EQ(old_state_and_flags.GetState(), ThreadState::kRunnable);
1641   StateAndFlags new_state_and_flags = old_state_and_flags;
1642   new_state_and_flags.SetFlag(ThreadFlag::kCheckpointRequest);
1643   bool success = tls32_.state_and_flags.CompareAndSetStrongSequentiallyConsistent(
1644       old_state_and_flags.GetValue(), new_state_and_flags.GetValue());
1645   if (success) {
1646     // Succeeded setting checkpoint flag, now insert the actual checkpoint.
1647     if (tlsPtr_.checkpoint_function == nullptr) {
1648       tlsPtr_.checkpoint_function = function;
1649     } else {
1650       checkpoint_overflow_.push_back(function);
1651     }
1652     CHECK(ReadFlag(ThreadFlag::kCheckpointRequest));
1653     TriggerSuspend();
1654   }
1655   return success;
1656 }
1657 
RequestEmptyCheckpoint()1658 bool Thread::RequestEmptyCheckpoint() {
1659   StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
1660   if (old_state_and_flags.GetState() != ThreadState::kRunnable) {
1661     // If it's not runnable, we don't need to do anything because it won't be in the middle of a
1662     // heap access (eg. the read barrier).
1663     return false;
1664   }
1665 
1666   // We must be runnable to request a checkpoint.
1667   DCHECK_EQ(old_state_and_flags.GetState(), ThreadState::kRunnable);
1668   StateAndFlags new_state_and_flags = old_state_and_flags;
1669   new_state_and_flags.SetFlag(ThreadFlag::kEmptyCheckpointRequest);
1670   bool success = tls32_.state_and_flags.CompareAndSetStrongSequentiallyConsistent(
1671       old_state_and_flags.GetValue(), new_state_and_flags.GetValue());
1672   if (success) {
1673     TriggerSuspend();
1674   }
1675   return success;
1676 }
1677 
1678 class BarrierClosure : public Closure {
1679  public:
BarrierClosure(Closure * wrapped)1680   explicit BarrierClosure(Closure* wrapped) : wrapped_(wrapped), barrier_(0) {}
1681 
Run(Thread * self)1682   void Run(Thread* self) override {
1683     wrapped_->Run(self);
1684     barrier_.Pass(self);
1685   }
1686 
Wait(Thread * self,ThreadState suspend_state)1687   void Wait(Thread* self, ThreadState suspend_state) {
1688     if (suspend_state != ThreadState::kRunnable) {
1689       barrier_.Increment<Barrier::kDisallowHoldingLocks>(self, 1);
1690     } else {
1691       barrier_.Increment<Barrier::kAllowHoldingLocks>(self, 1);
1692     }
1693   }
1694 
1695  private:
1696   Closure* wrapped_;
1697   Barrier barrier_;
1698 };
1699 
1700 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
RequestSynchronousCheckpoint(Closure * function,ThreadState suspend_state)1701 bool Thread::RequestSynchronousCheckpoint(Closure* function, ThreadState suspend_state) {
1702   Thread* self = Thread::Current();
1703   if (this == Thread::Current()) {
1704     Locks::thread_list_lock_->AssertExclusiveHeld(self);
1705     // Unlock the tll before running so that the state is the same regardless of thread.
1706     Locks::thread_list_lock_->ExclusiveUnlock(self);
1707     // Asked to run on this thread. Just run.
1708     function->Run(this);
1709     return true;
1710   }
1711 
1712   // The current thread is not this thread.
1713 
1714   if (GetState() == ThreadState::kTerminated) {
1715     Locks::thread_list_lock_->ExclusiveUnlock(self);
1716     return false;
1717   }
1718 
1719   struct ScopedThreadListLockUnlock {
1720     explicit ScopedThreadListLockUnlock(Thread* self_in) RELEASE(*Locks::thread_list_lock_)
1721         : self_thread(self_in) {
1722       Locks::thread_list_lock_->AssertHeld(self_thread);
1723       Locks::thread_list_lock_->Unlock(self_thread);
1724     }
1725 
1726     ~ScopedThreadListLockUnlock() ACQUIRE(*Locks::thread_list_lock_) {
1727       Locks::thread_list_lock_->AssertNotHeld(self_thread);
1728       Locks::thread_list_lock_->Lock(self_thread);
1729     }
1730 
1731     Thread* self_thread;
1732   };
1733 
1734   for (;;) {
1735     Locks::thread_list_lock_->AssertExclusiveHeld(self);
1736     // If this thread is runnable, try to schedule a checkpoint. Do some gymnastics to not hold the
1737     // suspend-count lock for too long.
1738     if (GetState() == ThreadState::kRunnable) {
1739       BarrierClosure barrier_closure(function);
1740       bool installed = false;
1741       {
1742         MutexLock mu(self, *Locks::thread_suspend_count_lock_);
1743         installed = RequestCheckpoint(&barrier_closure);
1744       }
1745       if (installed) {
1746         // Relinquish the thread-list lock. We should not wait holding any locks. We cannot
1747         // reacquire it since we don't know if 'this' hasn't been deleted yet.
1748         Locks::thread_list_lock_->ExclusiveUnlock(self);
1749         ScopedThreadStateChange sts(self, suspend_state);
1750         barrier_closure.Wait(self, suspend_state);
1751         return true;
1752       }
1753       // Fall-through.
1754     }
1755 
1756     // This thread is not runnable, make sure we stay suspended, then run the checkpoint.
1757     // Note: ModifySuspendCountInternal also expects the thread_list_lock to be held in
1758     //       certain situations.
1759     {
1760       MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
1761 
1762       if (!ModifySuspendCount(self, +1, nullptr, SuspendReason::kInternal)) {
1763         // Just retry the loop.
1764         sched_yield();
1765         continue;
1766       }
1767     }
1768 
1769     {
1770       // Release for the wait. The suspension will keep us from being deleted. Reacquire after so
1771       // that we can call ModifySuspendCount without racing against ThreadList::Unregister.
1772       ScopedThreadListLockUnlock stllu(self);
1773       {
1774         ScopedThreadStateChange sts(self, suspend_state);
1775         while (GetState() == ThreadState::kRunnable) {
1776           // We became runnable again. Wait till the suspend triggered in ModifySuspendCount
1777           // moves us to suspended.
1778           sched_yield();
1779         }
1780       }
1781 
1782       function->Run(this);
1783     }
1784 
1785     {
1786       MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
1787 
1788       DCHECK_NE(GetState(), ThreadState::kRunnable);
1789       bool updated = ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
1790       DCHECK(updated);
1791     }
1792 
1793     {
1794       // Imitate ResumeAll, the thread may be waiting on Thread::resume_cond_ since we raised its
1795       // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
1796       MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
1797       Thread::resume_cond_->Broadcast(self);
1798     }
1799 
1800     // Release the thread_list_lock_ to be consistent with the barrier-closure path.
1801     Locks::thread_list_lock_->ExclusiveUnlock(self);
1802 
1803     return true;  // We're done, break out of the loop.
1804   }
1805 }
1806 
SetFlipFunction(Closure * function)1807 void Thread::SetFlipFunction(Closure* function) {
1808   // This is called with all threads suspended, except for the calling thread.
1809   DCHECK(IsSuspended() || Thread::Current() == this);
1810   DCHECK(function != nullptr);
1811   DCHECK(tlsPtr_.flip_function == nullptr);
1812   tlsPtr_.flip_function = function;
1813   DCHECK(!GetStateAndFlags(std::memory_order_relaxed).IsAnyOfFlagsSet(FlipFunctionFlags()));
1814   AtomicSetFlag(ThreadFlag::kPendingFlipFunction, std::memory_order_release);
1815 }
1816 
EnsureFlipFunctionStarted(Thread * self)1817 void Thread::EnsureFlipFunctionStarted(Thread* self) {
1818   while (true) {
1819     StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
1820     if (!old_state_and_flags.IsFlagSet(ThreadFlag::kPendingFlipFunction)) {
1821       return;
1822     }
1823     DCHECK(!old_state_and_flags.IsFlagSet(ThreadFlag::kRunningFlipFunction));
1824     StateAndFlags new_state_and_flags =
1825         old_state_and_flags.WithFlag(ThreadFlag::kRunningFlipFunction)
1826                            .WithoutFlag(ThreadFlag::kPendingFlipFunction);
1827     if (tls32_.state_and_flags.CompareAndSetWeakAcquire(old_state_and_flags.GetValue(),
1828                                                         new_state_and_flags.GetValue())) {
1829       RunFlipFunction(self, /*notify=*/ true);
1830       DCHECK(!GetStateAndFlags(std::memory_order_relaxed).IsAnyOfFlagsSet(FlipFunctionFlags()));
1831       return;
1832     }
1833   }
1834 }
1835 
RunFlipFunction(Thread * self,bool notify)1836 void Thread::RunFlipFunction(Thread* self, bool notify) {
1837   // This function is called for suspended threads and by the thread running
1838   // `ThreadList::FlipThreadRoots()` after we've successfully set the flag
1839   // `ThreadFlag::kRunningFlipFunction`. This flag is not set if the thread is
1840   // running the flip function right after transitioning to Runnable as
1841   // no other thread may run checkpoints on a thread that's actually Runnable.
1842   DCHECK_EQ(notify, ReadFlag(ThreadFlag::kRunningFlipFunction));
1843 
1844   Closure* flip_function = tlsPtr_.flip_function;
1845   tlsPtr_.flip_function = nullptr;
1846   DCHECK(flip_function != nullptr);
1847   flip_function->Run(this);
1848 
1849   if (notify) {
1850     // Clear the `ThreadFlag::kRunningFlipFunction` and `ThreadFlag::kWaitingForFlipFunction`.
1851     // Check if the latter was actually set, indicating that there is at least one waiting thread.
1852     constexpr uint32_t kFlagsToClear = enum_cast<uint32_t>(ThreadFlag::kRunningFlipFunction) |
1853                                        enum_cast<uint32_t>(ThreadFlag::kWaitingForFlipFunction);
1854     StateAndFlags old_state_and_flags(
1855         tls32_.state_and_flags.fetch_and(~kFlagsToClear, std::memory_order_release));
1856     if (old_state_and_flags.IsFlagSet(ThreadFlag::kWaitingForFlipFunction)) {
1857       // Notify all threads that are waiting for completion (at least one).
1858       // TODO: Should we create a separate mutex and condition variable instead
1859       // of piggy-backing on the `thread_suspend_count_lock_` and `resume_cond_`?
1860       MutexLock mu(self, *Locks::thread_suspend_count_lock_);
1861       resume_cond_->Broadcast(self);
1862     }
1863   }
1864 }
1865 
WaitForFlipFunction(Thread * self)1866 void Thread::WaitForFlipFunction(Thread* self) {
1867   // Another thread is running the flip function. Wait for it to complete.
1868   // Check the flag while holding the mutex so that we do not miss the broadcast.
1869   // Repeat the check after waiting to guard against spurious wakeups (and because
1870   // we share the `thread_suspend_count_lock_` and `resume_cond_` with other code).
1871   MutexLock mu(self, *Locks::thread_suspend_count_lock_);
1872   while (true) {
1873     StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_acquire);
1874     DCHECK(!old_state_and_flags.IsFlagSet(ThreadFlag::kPendingFlipFunction));
1875     if (!old_state_and_flags.IsFlagSet(ThreadFlag::kRunningFlipFunction)) {
1876       DCHECK(!old_state_and_flags.IsAnyOfFlagsSet(FlipFunctionFlags()));
1877       break;
1878     }
1879     if (!old_state_and_flags.IsFlagSet(ThreadFlag::kWaitingForFlipFunction)) {
1880       // Mark that there is a waiting thread.
1881       StateAndFlags new_state_and_flags =
1882           old_state_and_flags.WithFlag(ThreadFlag::kWaitingForFlipFunction);
1883       if (!tls32_.state_and_flags.CompareAndSetWeakRelaxed(old_state_and_flags.GetValue(),
1884                                                            new_state_and_flags.GetValue())) {
1885         continue;  // Retry.
1886       }
1887     }
1888     resume_cond_->Wait(self);
1889   }
1890 }
1891 
FullSuspendCheck(bool implicit)1892 void Thread::FullSuspendCheck(bool implicit) {
1893   ScopedTrace trace(__FUNCTION__);
1894   VLOG(threads) << this << " self-suspending";
1895   // Make thread appear suspended to other threads, release mutator_lock_.
1896   // Transition to suspended and back to runnable, re-acquire share on mutator_lock_.
1897   ScopedThreadSuspension(this, ThreadState::kSuspended);  // NOLINT
1898   if (implicit) {
1899     // For implicit suspend check we want to `madvise()` away
1900     // the alternate signal stack to avoid wasting memory.
1901     MadviseAwayAlternateSignalStack();
1902   }
1903   VLOG(threads) << this << " self-reviving";
1904 }
1905 
GetSchedulerGroupName(pid_t tid)1906 static std::string GetSchedulerGroupName(pid_t tid) {
1907   // /proc/<pid>/cgroup looks like this:
1908   // 2:devices:/
1909   // 1:cpuacct,cpu:/
1910   // We want the third field from the line whose second field contains the "cpu" token.
1911   std::string cgroup_file;
1912   if (!android::base::ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid),
1913                                        &cgroup_file)) {
1914     return "";
1915   }
1916   std::vector<std::string> cgroup_lines;
1917   Split(cgroup_file, '\n', &cgroup_lines);
1918   for (size_t i = 0; i < cgroup_lines.size(); ++i) {
1919     std::vector<std::string> cgroup_fields;
1920     Split(cgroup_lines[i], ':', &cgroup_fields);
1921     std::vector<std::string> cgroups;
1922     Split(cgroup_fields[1], ',', &cgroups);
1923     for (size_t j = 0; j < cgroups.size(); ++j) {
1924       if (cgroups[j] == "cpu") {
1925         return cgroup_fields[2].substr(1);  // Skip the leading slash.
1926       }
1927     }
1928   }
1929   return "";
1930 }
1931 
DumpState(std::ostream & os,const Thread * thread,pid_t tid)1932 void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) {
1933   std::string group_name;
1934   int priority;
1935   bool is_daemon = false;
1936   Thread* self = Thread::Current();
1937 
1938   // Don't do this if we are aborting since the GC may have all the threads suspended. This will
1939   // cause ScopedObjectAccessUnchecked to deadlock.
1940   if (gAborting == 0 && self != nullptr && thread != nullptr && thread->tlsPtr_.opeer != nullptr) {
1941     ScopedObjectAccessUnchecked soa(self);
1942     priority = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority)
1943         ->GetInt(thread->tlsPtr_.opeer);
1944     is_daemon = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_daemon)
1945         ->GetBoolean(thread->tlsPtr_.opeer);
1946 
1947     ObjPtr<mirror::Object> thread_group =
1948         jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)
1949             ->GetObject(thread->tlsPtr_.opeer);
1950 
1951     if (thread_group != nullptr) {
1952       ArtField* group_name_field =
1953           jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_name);
1954       ObjPtr<mirror::String> group_name_string =
1955           group_name_field->GetObject(thread_group)->AsString();
1956       group_name = (group_name_string != nullptr) ? group_name_string->ToModifiedUtf8() : "<null>";
1957     }
1958   } else if (thread != nullptr) {
1959     priority = thread->GetNativePriority();
1960   } else {
1961     palette_status_t status = PaletteSchedGetPriority(tid, &priority);
1962     CHECK(status == PALETTE_STATUS_OK || status == PALETTE_STATUS_CHECK_ERRNO);
1963   }
1964 
1965   std::string scheduler_group_name(GetSchedulerGroupName(tid));
1966   if (scheduler_group_name.empty()) {
1967     scheduler_group_name = "default";
1968   }
1969 
1970   if (thread != nullptr) {
1971     thread->tls32_.num_name_readers.fetch_add(1, std::memory_order_seq_cst);
1972     os << '"' << thread->tlsPtr_.name.load() << '"';
1973     thread->tls32_.num_name_readers.fetch_sub(1 /* at least memory_order_release */);
1974     if (is_daemon) {
1975       os << " daemon";
1976     }
1977     os << " prio=" << priority
1978        << " tid=" << thread->GetThreadId()
1979        << " " << thread->GetState();
1980     if (thread->IsStillStarting()) {
1981       os << " (still starting up)";
1982     }
1983     os << "\n";
1984   } else {
1985     os << '"' << ::art::GetThreadName(tid) << '"'
1986        << " prio=" << priority
1987        << " (not attached)\n";
1988   }
1989 
1990   if (thread != nullptr) {
1991     auto suspend_log_fn = [&]() REQUIRES(Locks::thread_suspend_count_lock_) {
1992       StateAndFlags state_and_flags = thread->GetStateAndFlags(std::memory_order_relaxed);
1993       static_assert(
1994           static_cast<std::underlying_type_t<ThreadState>>(ThreadState::kRunnable) == 0u);
1995       state_and_flags.SetState(ThreadState::kRunnable);  // Clear state bits.
1996       os << "  | group=\"" << group_name << "\""
1997          << " sCount=" << thread->tls32_.suspend_count
1998          << " ucsCount=" << thread->tls32_.user_code_suspend_count
1999          << " flags=" << state_and_flags.GetValue()
2000          << " obj=" << reinterpret_cast<void*>(thread->tlsPtr_.opeer)
2001          << " self=" << reinterpret_cast<const void*>(thread) << "\n";
2002     };
2003     if (Locks::thread_suspend_count_lock_->IsExclusiveHeld(self)) {
2004       Locks::thread_suspend_count_lock_->AssertExclusiveHeld(self);  // For annotalysis.
2005       suspend_log_fn();
2006     } else {
2007       MutexLock mu(self, *Locks::thread_suspend_count_lock_);
2008       suspend_log_fn();
2009     }
2010   }
2011 
2012   os << "  | sysTid=" << tid
2013      << " nice=" << getpriority(PRIO_PROCESS, static_cast<id_t>(tid))
2014      << " cgrp=" << scheduler_group_name;
2015   if (thread != nullptr) {
2016     int policy;
2017     sched_param sp;
2018 #if !defined(__APPLE__)
2019     // b/36445592 Don't use pthread_getschedparam since pthread may have exited.
2020     policy = sched_getscheduler(tid);
2021     if (policy == -1) {
2022       PLOG(WARNING) << "sched_getscheduler(" << tid << ")";
2023     }
2024     int sched_getparam_result = sched_getparam(tid, &sp);
2025     if (sched_getparam_result == -1) {
2026       PLOG(WARNING) << "sched_getparam(" << tid << ", &sp)";
2027       sp.sched_priority = -1;
2028     }
2029 #else
2030     CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp),
2031                        __FUNCTION__);
2032 #endif
2033     os << " sched=" << policy << "/" << sp.sched_priority
2034        << " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self);
2035   }
2036   os << "\n";
2037 
2038   // Grab the scheduler stats for this thread.
2039   std::string scheduler_stats;
2040   if (android::base::ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid),
2041                                       &scheduler_stats)
2042       && !scheduler_stats.empty()) {
2043     scheduler_stats = android::base::Trim(scheduler_stats);  // Lose the trailing '\n'.
2044   } else {
2045     scheduler_stats = "0 0 0";
2046   }
2047 
2048   char native_thread_state = '?';
2049   int utime = 0;
2050   int stime = 0;
2051   int task_cpu = 0;
2052   GetTaskStats(tid, &native_thread_state, &utime, &stime, &task_cpu);
2053 
2054   os << "  | state=" << native_thread_state
2055      << " schedstat=( " << scheduler_stats << " )"
2056      << " utm=" << utime
2057      << " stm=" << stime
2058      << " core=" << task_cpu
2059      << " HZ=" << sysconf(_SC_CLK_TCK) << "\n";
2060   if (thread != nullptr) {
2061     os << "  | stack=" << reinterpret_cast<void*>(thread->tlsPtr_.stack_begin) << "-"
2062         << reinterpret_cast<void*>(thread->tlsPtr_.stack_end) << " stackSize="
2063         << PrettySize(thread->tlsPtr_.stack_size) << "\n";
2064     // Dump the held mutexes.
2065     os << "  | held mutexes=";
2066     for (size_t i = 0; i < kLockLevelCount; ++i) {
2067       if (i != kMonitorLock) {
2068         BaseMutex* mutex = thread->GetHeldMutex(static_cast<LockLevel>(i));
2069         if (mutex != nullptr) {
2070           os << " \"" << mutex->GetName() << "\"";
2071           if (mutex->IsReaderWriterMutex()) {
2072             ReaderWriterMutex* rw_mutex = down_cast<ReaderWriterMutex*>(mutex);
2073             if (rw_mutex->GetExclusiveOwnerTid() == tid) {
2074               os << "(exclusive held)";
2075             } else {
2076               os << "(shared held)";
2077             }
2078           }
2079         }
2080       }
2081     }
2082     os << "\n";
2083   }
2084 }
2085 
DumpState(std::ostream & os) const2086 void Thread::DumpState(std::ostream& os) const {
2087   Thread::DumpState(os, this, GetTid());
2088 }
2089 
2090 struct StackDumpVisitor : public MonitorObjectsStackVisitor {
StackDumpVisitorart::StackDumpVisitor2091   StackDumpVisitor(std::ostream& os_in,
2092                    Thread* thread_in,
2093                    Context* context,
2094                    bool can_allocate,
2095                    bool check_suspended = true,
2096                    bool dump_locks = true)
2097       REQUIRES_SHARED(Locks::mutator_lock_)
2098       : MonitorObjectsStackVisitor(thread_in,
2099                                    context,
2100                                    check_suspended,
2101                                    can_allocate && dump_locks),
2102         os(os_in),
2103         last_method(nullptr),
2104         last_line_number(0),
2105         repetition_count(0) {}
2106 
~StackDumpVisitorart::StackDumpVisitor2107   virtual ~StackDumpVisitor() {
2108     if (frame_count == 0) {
2109       os << "  (no managed stack frames)\n";
2110     }
2111   }
2112 
2113   static constexpr size_t kMaxRepetition = 3u;
2114 
StartMethodart::StackDumpVisitor2115   VisitMethodResult StartMethod(ArtMethod* m, size_t frame_nr ATTRIBUTE_UNUSED)
2116       override
2117       REQUIRES_SHARED(Locks::mutator_lock_) {
2118     m = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
2119     ObjPtr<mirror::DexCache> dex_cache = m->GetDexCache();
2120     int line_number = -1;
2121     uint32_t dex_pc = GetDexPc(false);
2122     if (dex_cache != nullptr) {  // be tolerant of bad input
2123       const DexFile* dex_file = dex_cache->GetDexFile();
2124       line_number = annotations::GetLineNumFromPC(dex_file, m, dex_pc);
2125     }
2126     if (line_number == last_line_number && last_method == m) {
2127       ++repetition_count;
2128     } else {
2129       if (repetition_count >= kMaxRepetition) {
2130         os << "  ... repeated " << (repetition_count - kMaxRepetition) << " times\n";
2131       }
2132       repetition_count = 0;
2133       last_line_number = line_number;
2134       last_method = m;
2135     }
2136 
2137     if (repetition_count >= kMaxRepetition) {
2138       // Skip visiting=printing anything.
2139       return VisitMethodResult::kSkipMethod;
2140     }
2141 
2142     os << "  at " << m->PrettyMethod(false);
2143     if (m->IsNative()) {
2144       os << "(Native method)";
2145     } else {
2146       const char* source_file(m->GetDeclaringClassSourceFile());
2147       if (line_number == -1) {
2148         // If we failed to map to a line number, use
2149         // the dex pc as the line number and leave source file null
2150         source_file = nullptr;
2151         line_number = static_cast<int32_t>(dex_pc);
2152       }
2153       os << "(" << (source_file != nullptr ? source_file : "unavailable")
2154                        << ":" << line_number << ")";
2155     }
2156     os << "\n";
2157     // Go and visit locks.
2158     return VisitMethodResult::kContinueMethod;
2159   }
2160 
EndMethodart::StackDumpVisitor2161   VisitMethodResult EndMethod(ArtMethod* m ATTRIBUTE_UNUSED) override {
2162     return VisitMethodResult::kContinueMethod;
2163   }
2164 
VisitWaitingObjectart::StackDumpVisitor2165   void VisitWaitingObject(ObjPtr<mirror::Object> obj, ThreadState state ATTRIBUTE_UNUSED)
2166       override
2167       REQUIRES_SHARED(Locks::mutator_lock_) {
2168     PrintObject(obj, "  - waiting on ", ThreadList::kInvalidThreadId);
2169   }
VisitSleepingObjectart::StackDumpVisitor2170   void VisitSleepingObject(ObjPtr<mirror::Object> obj)
2171       override
2172       REQUIRES_SHARED(Locks::mutator_lock_) {
2173     PrintObject(obj, "  - sleeping on ", ThreadList::kInvalidThreadId);
2174   }
VisitBlockedOnObjectart::StackDumpVisitor2175   void VisitBlockedOnObject(ObjPtr<mirror::Object> obj,
2176                             ThreadState state,
2177                             uint32_t owner_tid)
2178       override
2179       REQUIRES_SHARED(Locks::mutator_lock_) {
2180     const char* msg;
2181     switch (state) {
2182       case ThreadState::kBlocked:
2183         msg = "  - waiting to lock ";
2184         break;
2185 
2186       case ThreadState::kWaitingForLockInflation:
2187         msg = "  - waiting for lock inflation of ";
2188         break;
2189 
2190       default:
2191         LOG(FATAL) << "Unreachable";
2192         UNREACHABLE();
2193     }
2194     PrintObject(obj, msg, owner_tid);
2195   }
VisitLockedObjectart::StackDumpVisitor2196   void VisitLockedObject(ObjPtr<mirror::Object> obj)
2197       override
2198       REQUIRES_SHARED(Locks::mutator_lock_) {
2199     PrintObject(obj, "  - locked ", ThreadList::kInvalidThreadId);
2200   }
2201 
PrintObjectart::StackDumpVisitor2202   void PrintObject(ObjPtr<mirror::Object> obj,
2203                    const char* msg,
2204                    uint32_t owner_tid) REQUIRES_SHARED(Locks::mutator_lock_) {
2205     if (obj == nullptr) {
2206       os << msg << "an unknown object";
2207     } else {
2208       if ((obj->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
2209           Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
2210         // Getting the identity hashcode here would result in lock inflation and suspension of the
2211         // current thread, which isn't safe if this is the only runnable thread.
2212         os << msg << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
2213                                   reinterpret_cast<intptr_t>(obj.Ptr()),
2214                                   obj->PrettyTypeOf().c_str());
2215       } else {
2216         // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
2217         // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
2218         // suspension and move pretty_object.
2219         const std::string pretty_type(obj->PrettyTypeOf());
2220         os << msg << StringPrintf("<0x%08x> (a %s)", obj->IdentityHashCode(), pretty_type.c_str());
2221       }
2222     }
2223     if (owner_tid != ThreadList::kInvalidThreadId) {
2224       os << " held by thread " << owner_tid;
2225     }
2226     os << "\n";
2227   }
2228 
2229   std::ostream& os;
2230   ArtMethod* last_method;
2231   int last_line_number;
2232   size_t repetition_count;
2233 };
2234 
ShouldShowNativeStack(const Thread * thread)2235 static bool ShouldShowNativeStack(const Thread* thread)
2236     REQUIRES_SHARED(Locks::mutator_lock_) {
2237   ThreadState state = thread->GetState();
2238 
2239   // In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting.
2240   if (state > ThreadState::kWaiting && state < ThreadState::kStarting) {
2241     return true;
2242   }
2243 
2244   // In an Object.wait variant or Thread.sleep? That's not interesting.
2245   if (state == ThreadState::kTimedWaiting ||
2246       state == ThreadState::kSleeping ||
2247       state == ThreadState::kWaiting) {
2248     return false;
2249   }
2250 
2251   // Threads with no managed stack frames should be shown.
2252   if (!thread->HasManagedStack()) {
2253     return true;
2254   }
2255 
2256   // In some other native method? That's interesting.
2257   // We don't just check kNative because native methods will be in state kSuspended if they're
2258   // calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the
2259   // thread-startup states if it's early enough in their life cycle (http://b/7432159).
2260   ArtMethod* current_method = thread->GetCurrentMethod(nullptr);
2261   return current_method != nullptr && current_method->IsNative();
2262 }
2263 
DumpJavaStack(std::ostream & os,bool check_suspended,bool dump_locks) const2264 void Thread::DumpJavaStack(std::ostream& os, bool check_suspended, bool dump_locks) const {
2265   // Dumping the Java stack involves the verifier for locks. The verifier operates under the
2266   // assumption that there is no exception pending on entry. Thus, stash any pending exception.
2267   // Thread::Current() instead of this in case a thread is dumping the stack of another suspended
2268   // thread.
2269   ScopedExceptionStorage ses(Thread::Current());
2270 
2271   std::unique_ptr<Context> context(Context::Create());
2272   StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(),
2273                           !tls32_.throwing_OutOfMemoryError, check_suspended, dump_locks);
2274   dumper.WalkStack();
2275 }
2276 
DumpStack(std::ostream & os,bool dump_native_stack,BacktraceMap * backtrace_map,bool force_dump_stack) const2277 void Thread::DumpStack(std::ostream& os,
2278                        bool dump_native_stack,
2279                        BacktraceMap* backtrace_map,
2280                        bool force_dump_stack) const {
2281   // TODO: we call this code when dying but may not have suspended the thread ourself. The
2282   //       IsSuspended check is therefore racy with the use for dumping (normally we inhibit
2283   //       the race with the thread_suspend_count_lock_).
2284   bool dump_for_abort = (gAborting > 0);
2285   bool safe_to_dump = (this == Thread::Current() || IsSuspended());
2286   if (!kIsDebugBuild) {
2287     // We always want to dump the stack for an abort, however, there is no point dumping another
2288     // thread's stack in debug builds where we'll hit the not suspended check in the stack walk.
2289     safe_to_dump = (safe_to_dump || dump_for_abort);
2290   }
2291   if (safe_to_dump || force_dump_stack) {
2292     // If we're currently in native code, dump that stack before dumping the managed stack.
2293     if (dump_native_stack && (dump_for_abort || force_dump_stack || ShouldShowNativeStack(this))) {
2294       ArtMethod* method =
2295           GetCurrentMethod(nullptr,
2296                            /*check_suspended=*/ !force_dump_stack,
2297                            /*abort_on_error=*/ !(dump_for_abort || force_dump_stack));
2298       DumpNativeStack(os, GetTid(), backtrace_map, "  native: ", method);
2299     }
2300     DumpJavaStack(os,
2301                   /*check_suspended=*/ !force_dump_stack,
2302                   /*dump_locks=*/ !force_dump_stack);
2303   } else {
2304     os << "Not able to dump stack of thread that isn't suspended";
2305   }
2306 }
2307 
ThreadExitCallback(void * arg)2308 void Thread::ThreadExitCallback(void* arg) {
2309   Thread* self = reinterpret_cast<Thread*>(arg);
2310   if (self->tls32_.thread_exit_check_count == 0) {
2311     LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's "
2312         "going to use a pthread_key_create destructor?): " << *self;
2313     CHECK(is_started_);
2314 #ifdef __BIONIC__
2315     __get_tls()[TLS_SLOT_ART_THREAD_SELF] = self;
2316 #else
2317     CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self");
2318     Thread::self_tls_ = self;
2319 #endif
2320     self->tls32_.thread_exit_check_count = 1;
2321   } else {
2322     LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self;
2323   }
2324 }
2325 
Startup()2326 void Thread::Startup() {
2327   CHECK(!is_started_);
2328   is_started_ = true;
2329   {
2330     // MutexLock to keep annotalysis happy.
2331     //
2332     // Note we use null for the thread because Thread::Current can
2333     // return garbage since (is_started_ == true) and
2334     // Thread::pthread_key_self_ is not yet initialized.
2335     // This was seen on glibc.
2336     MutexLock mu(nullptr, *Locks::thread_suspend_count_lock_);
2337     resume_cond_ = new ConditionVariable("Thread resumption condition variable",
2338                                          *Locks::thread_suspend_count_lock_);
2339   }
2340 
2341   // Allocate a TLS slot.
2342   CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback),
2343                      "self key");
2344 
2345   // Double-check the TLS slot allocation.
2346   if (pthread_getspecific(pthread_key_self_) != nullptr) {
2347     LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr";
2348   }
2349 #ifndef __BIONIC__
2350   CHECK(Thread::self_tls_ == nullptr);
2351 #endif
2352 }
2353 
FinishStartup()2354 void Thread::FinishStartup() {
2355   Runtime* runtime = Runtime::Current();
2356   CHECK(runtime->IsStarted());
2357 
2358   // Finish attaching the main thread.
2359   ScopedObjectAccess soa(Thread::Current());
2360   soa.Self()->CreatePeer("main", false, runtime->GetMainThreadGroup());
2361   soa.Self()->AssertNoPendingException();
2362 
2363   runtime->RunRootClinits(soa.Self());
2364 
2365   // The thread counts as started from now on. We need to add it to the ThreadGroup. For regular
2366   // threads, this is done in Thread.start() on the Java side.
2367   soa.Self()->NotifyThreadGroup(soa, runtime->GetMainThreadGroup());
2368   soa.Self()->AssertNoPendingException();
2369 }
2370 
Shutdown()2371 void Thread::Shutdown() {
2372   CHECK(is_started_);
2373   is_started_ = false;
2374   CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key");
2375   MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
2376   if (resume_cond_ != nullptr) {
2377     delete resume_cond_;
2378     resume_cond_ = nullptr;
2379   }
2380 }
2381 
NotifyThreadGroup(ScopedObjectAccessAlreadyRunnable & soa,jobject thread_group)2382 void Thread::NotifyThreadGroup(ScopedObjectAccessAlreadyRunnable& soa, jobject thread_group) {
2383   ScopedLocalRef<jobject> thread_jobject(
2384       soa.Env(), soa.Env()->AddLocalReference<jobject>(Thread::Current()->GetPeer()));
2385   ScopedLocalRef<jobject> thread_group_jobject_scoped(
2386       soa.Env(), nullptr);
2387   jobject thread_group_jobject = thread_group;
2388   if (thread_group == nullptr || kIsDebugBuild) {
2389     // There is always a group set. Retrieve it.
2390     thread_group_jobject_scoped.reset(
2391         soa.Env()->GetObjectField(thread_jobject.get(),
2392                                   WellKnownClasses::java_lang_Thread_group));
2393     thread_group_jobject = thread_group_jobject_scoped.get();
2394     if (kIsDebugBuild && thread_group != nullptr) {
2395       CHECK(soa.Env()->IsSameObject(thread_group, thread_group_jobject));
2396     }
2397   }
2398   soa.Env()->CallNonvirtualVoidMethod(thread_group_jobject,
2399                                       WellKnownClasses::java_lang_ThreadGroup,
2400                                       WellKnownClasses::java_lang_ThreadGroup_add,
2401                                       thread_jobject.get());
2402 }
2403 
Thread(bool daemon)2404 Thread::Thread(bool daemon)
2405     : tls32_(daemon),
2406       wait_monitor_(nullptr),
2407       is_runtime_thread_(false) {
2408   wait_mutex_ = new Mutex("a thread wait mutex", LockLevel::kThreadWaitLock);
2409   wait_cond_ = new ConditionVariable("a thread wait condition variable", *wait_mutex_);
2410   tlsPtr_.mutator_lock = Locks::mutator_lock_;
2411   DCHECK(tlsPtr_.mutator_lock != nullptr);
2412   tlsPtr_.instrumentation_stack =
2413       new std::map<uintptr_t, instrumentation::InstrumentationStackFrame>;
2414   tlsPtr_.name.store(kThreadNameDuringStartup, std::memory_order_relaxed);
2415 
2416   static_assert((sizeof(Thread) % 4) == 0U,
2417                 "art::Thread has a size which is not a multiple of 4.");
2418   DCHECK_EQ(GetStateAndFlags(std::memory_order_relaxed).GetValue(), 0u);
2419   StateAndFlags state_and_flags = StateAndFlags(0u).WithState(ThreadState::kNative);
2420   tls32_.state_and_flags.store(state_and_flags.GetValue(), std::memory_order_relaxed);
2421   tls32_.interrupted.store(false, std::memory_order_relaxed);
2422   // Initialize with no permit; if the java Thread was unparked before being
2423   // started, it will unpark itself before calling into java code.
2424   tls32_.park_state_.store(kNoPermit, std::memory_order_relaxed);
2425   memset(&tlsPtr_.held_mutexes[0], 0, sizeof(tlsPtr_.held_mutexes));
2426   std::fill(tlsPtr_.rosalloc_runs,
2427             tlsPtr_.rosalloc_runs + kNumRosAllocThreadLocalSizeBracketsInThread,
2428             gc::allocator::RosAlloc::GetDedicatedFullRun());
2429   tlsPtr_.checkpoint_function = nullptr;
2430   for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
2431     tlsPtr_.active_suspend_barriers[i] = nullptr;
2432   }
2433   tlsPtr_.flip_function = nullptr;
2434   tlsPtr_.thread_local_mark_stack = nullptr;
2435   tls32_.is_transitioning_to_runnable = false;
2436   ResetTlab();
2437 }
2438 
CanLoadClasses() const2439 bool Thread::CanLoadClasses() const {
2440   return !IsRuntimeThread() || !Runtime::Current()->IsJavaDebuggable();
2441 }
2442 
IsStillStarting() const2443 bool Thread::IsStillStarting() const {
2444   // You might think you can check whether the state is kStarting, but for much of thread startup,
2445   // the thread is in kNative; it might also be in kVmWait.
2446   // You might think you can check whether the peer is null, but the peer is actually created and
2447   // assigned fairly early on, and needs to be.
2448   // It turns out that the last thing to change is the thread name; that's a good proxy for "has
2449   // this thread _ever_ entered kRunnable".
2450   return (tlsPtr_.jpeer == nullptr && tlsPtr_.opeer == nullptr) ||
2451       (tlsPtr_.name.load() == kThreadNameDuringStartup);
2452 }
2453 
AssertPendingException() const2454 void Thread::AssertPendingException() const {
2455   CHECK(IsExceptionPending()) << "Pending exception expected.";
2456 }
2457 
AssertPendingOOMException() const2458 void Thread::AssertPendingOOMException() const {
2459   AssertPendingException();
2460   auto* e = GetException();
2461   CHECK_EQ(e->GetClass(), DecodeJObject(WellKnownClasses::java_lang_OutOfMemoryError)->AsClass())
2462       << e->Dump();
2463 }
2464 
AssertNoPendingException() const2465 void Thread::AssertNoPendingException() const {
2466   if (UNLIKELY(IsExceptionPending())) {
2467     ScopedObjectAccess soa(Thread::Current());
2468     LOG(FATAL) << "No pending exception expected: " << GetException()->Dump();
2469   }
2470 }
2471 
AssertNoPendingExceptionForNewException(const char * msg) const2472 void Thread::AssertNoPendingExceptionForNewException(const char* msg) const {
2473   if (UNLIKELY(IsExceptionPending())) {
2474     ScopedObjectAccess soa(Thread::Current());
2475     LOG(FATAL) << "Throwing new exception '" << msg << "' with unexpected pending exception: "
2476         << GetException()->Dump();
2477   }
2478 }
2479 
2480 class MonitorExitVisitor : public SingleRootVisitor {
2481  public:
MonitorExitVisitor(Thread * self)2482   explicit MonitorExitVisitor(Thread* self) : self_(self) { }
2483 
2484   // NO_THREAD_SAFETY_ANALYSIS due to MonitorExit.
VisitRoot(mirror::Object * entered_monitor,const RootInfo & info ATTRIBUTE_UNUSED)2485   void VisitRoot(mirror::Object* entered_monitor, const RootInfo& info ATTRIBUTE_UNUSED)
2486       override NO_THREAD_SAFETY_ANALYSIS {
2487     if (self_->HoldsLock(entered_monitor)) {
2488       LOG(WARNING) << "Calling MonitorExit on object "
2489                    << entered_monitor << " (" << entered_monitor->PrettyTypeOf() << ")"
2490                    << " left locked by native thread "
2491                    << *Thread::Current() << " which is detaching";
2492       entered_monitor->MonitorExit(self_);
2493     }
2494   }
2495 
2496  private:
2497   Thread* const self_;
2498 };
2499 
Destroy()2500 void Thread::Destroy() {
2501   Thread* self = this;
2502   DCHECK_EQ(self, Thread::Current());
2503 
2504   if (tlsPtr_.jni_env != nullptr) {
2505     {
2506       ScopedObjectAccess soa(self);
2507       MonitorExitVisitor visitor(self);
2508       // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
2509       tlsPtr_.jni_env->monitors_.VisitRoots(&visitor, RootInfo(kRootVMInternal));
2510     }
2511     // Release locally held global references which releasing may require the mutator lock.
2512     if (tlsPtr_.jpeer != nullptr) {
2513       // If pthread_create fails we don't have a jni env here.
2514       tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.jpeer);
2515       tlsPtr_.jpeer = nullptr;
2516     }
2517     if (tlsPtr_.class_loader_override != nullptr) {
2518       tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.class_loader_override);
2519       tlsPtr_.class_loader_override = nullptr;
2520     }
2521   }
2522 
2523   if (tlsPtr_.opeer != nullptr) {
2524     ScopedObjectAccess soa(self);
2525     // We may need to call user-supplied managed code, do this before final clean-up.
2526     HandleUncaughtExceptions(soa);
2527     RemoveFromThreadGroup(soa);
2528     Runtime* runtime = Runtime::Current();
2529     if (runtime != nullptr) {
2530       runtime->GetRuntimeCallbacks()->ThreadDeath(self);
2531     }
2532 
2533     // this.nativePeer = 0;
2534     if (Runtime::Current()->IsActiveTransaction()) {
2535       jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer)
2536           ->SetLong<true>(tlsPtr_.opeer, 0);
2537     } else {
2538       jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer)
2539           ->SetLong<false>(tlsPtr_.opeer, 0);
2540     }
2541 
2542     // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone
2543     // who is waiting.
2544     ObjPtr<mirror::Object> lock =
2545         jni::DecodeArtField(WellKnownClasses::java_lang_Thread_lock)->GetObject(tlsPtr_.opeer);
2546     // (This conditional is only needed for tests, where Thread.lock won't have been set.)
2547     if (lock != nullptr) {
2548       StackHandleScope<1> hs(self);
2549       Handle<mirror::Object> h_obj(hs.NewHandle(lock));
2550       ObjectLock<mirror::Object> locker(self, h_obj);
2551       locker.NotifyAll();
2552     }
2553     tlsPtr_.opeer = nullptr;
2554   }
2555 
2556   {
2557     ScopedObjectAccess soa(self);
2558     Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this);
2559   }
2560   // Mark-stack revocation must be performed at the very end. No
2561   // checkpoint/flip-function or read-barrier should be called after this.
2562   if (kUseReadBarrier) {
2563     Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->RevokeThreadLocalMarkStack(this);
2564   }
2565 }
2566 
~Thread()2567 Thread::~Thread() {
2568   CHECK(tlsPtr_.class_loader_override == nullptr);
2569   CHECK(tlsPtr_.jpeer == nullptr);
2570   CHECK(tlsPtr_.opeer == nullptr);
2571   bool initialized = (tlsPtr_.jni_env != nullptr);  // Did Thread::Init run?
2572   if (initialized) {
2573     delete tlsPtr_.jni_env;
2574     tlsPtr_.jni_env = nullptr;
2575   }
2576   CHECK_NE(GetState(), ThreadState::kRunnable);
2577   CHECK(!ReadFlag(ThreadFlag::kCheckpointRequest));
2578   CHECK(!ReadFlag(ThreadFlag::kEmptyCheckpointRequest));
2579   CHECK(tlsPtr_.checkpoint_function == nullptr);
2580   CHECK_EQ(checkpoint_overflow_.size(), 0u);
2581   CHECK(tlsPtr_.flip_function == nullptr);
2582   CHECK_EQ(tls32_.is_transitioning_to_runnable, false);
2583 
2584   // Make sure we processed all deoptimization requests.
2585   CHECK(tlsPtr_.deoptimization_context_stack == nullptr) << "Missed deoptimization";
2586   CHECK(tlsPtr_.frame_id_to_shadow_frame == nullptr) <<
2587       "Not all deoptimized frames have been consumed by the debugger.";
2588 
2589   // We may be deleting a still born thread.
2590   SetStateUnsafe(ThreadState::kTerminated);
2591 
2592   delete wait_cond_;
2593   delete wait_mutex_;
2594 
2595   if (tlsPtr_.long_jump_context != nullptr) {
2596     delete tlsPtr_.long_jump_context;
2597   }
2598 
2599   if (initialized) {
2600     CleanupCpu();
2601   }
2602 
2603   delete tlsPtr_.instrumentation_stack;
2604   SetCachedThreadName(nullptr);  // Deallocate name.
2605   delete tlsPtr_.deps_or_stack_trace_sample.stack_trace_sample;
2606 
2607   Runtime::Current()->GetHeap()->AssertThreadLocalBuffersAreRevoked(this);
2608 
2609   TearDownAlternateSignalStack();
2610 }
2611 
HandleUncaughtExceptions(ScopedObjectAccessAlreadyRunnable & soa)2612 void Thread::HandleUncaughtExceptions(ScopedObjectAccessAlreadyRunnable& soa) {
2613   if (!IsExceptionPending()) {
2614     return;
2615   }
2616   ScopedLocalRef<jobject> peer(tlsPtr_.jni_env, soa.AddLocalReference<jobject>(tlsPtr_.opeer));
2617   ScopedThreadStateChange tsc(this, ThreadState::kNative);
2618 
2619   // Get and clear the exception.
2620   ScopedLocalRef<jthrowable> exception(tlsPtr_.jni_env, tlsPtr_.jni_env->ExceptionOccurred());
2621   tlsPtr_.jni_env->ExceptionClear();
2622 
2623   // Call the Thread instance's dispatchUncaughtException(Throwable)
2624   tlsPtr_.jni_env->CallVoidMethod(peer.get(),
2625       WellKnownClasses::java_lang_Thread_dispatchUncaughtException,
2626       exception.get());
2627 
2628   // If the dispatchUncaughtException threw, clear that exception too.
2629   tlsPtr_.jni_env->ExceptionClear();
2630 }
2631 
RemoveFromThreadGroup(ScopedObjectAccessAlreadyRunnable & soa)2632 void Thread::RemoveFromThreadGroup(ScopedObjectAccessAlreadyRunnable& soa) {
2633   // this.group.removeThread(this);
2634   // group can be null if we're in the compiler or a test.
2635   ObjPtr<mirror::Object> ogroup = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)
2636       ->GetObject(tlsPtr_.opeer);
2637   if (ogroup != nullptr) {
2638     ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup));
2639     ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(tlsPtr_.opeer));
2640     ScopedThreadStateChange tsc(soa.Self(), ThreadState::kNative);
2641     tlsPtr_.jni_env->CallVoidMethod(group.get(),
2642                                     WellKnownClasses::java_lang_ThreadGroup_removeThread,
2643                                     peer.get());
2644   }
2645 }
2646 
2647 template <bool kPointsToStack>
2648 class JniTransitionReferenceVisitor : public StackVisitor {
2649  public:
JniTransitionReferenceVisitor(Thread * thread,void * obj)2650   JniTransitionReferenceVisitor(Thread* thread, void* obj) REQUIRES_SHARED(Locks::mutator_lock_)
2651       : StackVisitor(thread, /*context=*/ nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
2652         obj_(obj),
2653         found_(false) {}
2654 
VisitFrame()2655   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
2656     ArtMethod* m = GetMethod();
2657     if (!m->IsNative() || m->IsCriticalNative()) {
2658       return true;
2659     }
2660     if (kPointsToStack) {
2661       uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
2662       size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
2663       uint32_t* current_vreg = reinterpret_cast<uint32_t*>(sp + frame_size + sizeof(ArtMethod*));
2664       if (!m->IsStatic()) {
2665         if (current_vreg == obj_) {
2666           found_ = true;
2667           return false;
2668         }
2669         current_vreg += 1u;
2670       }
2671       uint32_t shorty_length;
2672       const char* shorty = m->GetShorty(&shorty_length);
2673       for (size_t i = 1; i != shorty_length; ++i) {
2674         switch (shorty[i]) {
2675           case 'D':
2676           case 'J':
2677             current_vreg += 2u;
2678             break;
2679           case 'L':
2680             if (current_vreg == obj_) {
2681               found_ = true;
2682               return false;
2683             }
2684             FALLTHROUGH_INTENDED;
2685           default:
2686             current_vreg += 1u;
2687             break;
2688         }
2689       }
2690       // Continue only if the object is somewhere higher on the stack.
2691       return obj_ >= current_vreg;
2692     } else {  // if (kPointsToStack)
2693       if (m->IsStatic() && obj_ == m->GetDeclaringClassAddressWithoutBarrier()) {
2694         found_ = true;
2695         return false;
2696       }
2697       return true;
2698     }
2699   }
2700 
Found() const2701   bool Found() const {
2702     return found_;
2703   }
2704 
2705  private:
2706   void* obj_;
2707   bool found_;
2708 };
2709 
IsJniTransitionReference(jobject obj) const2710 bool Thread::IsJniTransitionReference(jobject obj) const {
2711   DCHECK(obj != nullptr);
2712   // We need a non-const pointer for stack walk even if we're not modifying the thread state.
2713   Thread* thread = const_cast<Thread*>(this);
2714   uint8_t* raw_obj = reinterpret_cast<uint8_t*>(obj);
2715   if (static_cast<size_t>(raw_obj - tlsPtr_.stack_begin) < tlsPtr_.stack_size) {
2716     JniTransitionReferenceVisitor</*kPointsToStack=*/ true> visitor(thread, raw_obj);
2717     visitor.WalkStack();
2718     return visitor.Found();
2719   } else {
2720     JniTransitionReferenceVisitor</*kPointsToStack=*/ false> visitor(thread, raw_obj);
2721     visitor.WalkStack();
2722     return visitor.Found();
2723   }
2724 }
2725 
HandleScopeVisitRoots(RootVisitor * visitor,uint32_t thread_id)2726 void Thread::HandleScopeVisitRoots(RootVisitor* visitor, uint32_t thread_id) {
2727   BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
2728       visitor, RootInfo(kRootNativeStack, thread_id));
2729   for (BaseHandleScope* cur = tlsPtr_.top_handle_scope; cur; cur = cur->GetLink()) {
2730     cur->VisitRoots(buffered_visitor);
2731   }
2732 }
2733 
DecodeJObject(jobject obj) const2734 ObjPtr<mirror::Object> Thread::DecodeJObject(jobject obj) const {
2735   if (obj == nullptr) {
2736     return nullptr;
2737   }
2738   IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
2739   IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
2740   ObjPtr<mirror::Object> result;
2741   bool expect_null = false;
2742   // The "kinds" below are sorted by the frequency we expect to encounter them.
2743   if (kind == kLocal) {
2744     IndirectReferenceTable& locals = tlsPtr_.jni_env->locals_;
2745     // Local references do not need a read barrier.
2746     result = locals.Get<kWithoutReadBarrier>(ref);
2747   } else if (kind == kJniTransitionOrInvalid) {
2748     // The `jclass` for a static method points to the CompressedReference<> in the
2749     // `ArtMethod::declaring_class_`. Other `jobject` arguments point to spilled stack
2750     // references but a StackReference<> is just a subclass of CompressedReference<>.
2751     DCHECK(IsJniTransitionReference(obj));
2752     result = reinterpret_cast<mirror::CompressedReference<mirror::Object>*>(obj)->AsMirrorPtr();
2753     VerifyObject(result);
2754   } else if (kind == kGlobal) {
2755     result = tlsPtr_.jni_env->vm_->DecodeGlobal(ref);
2756   } else {
2757     DCHECK_EQ(kind, kWeakGlobal);
2758     result = tlsPtr_.jni_env->vm_->DecodeWeakGlobal(const_cast<Thread*>(this), ref);
2759     if (Runtime::Current()->IsClearedJniWeakGlobal(result)) {
2760       // This is a special case where it's okay to return null.
2761       expect_null = true;
2762       result = nullptr;
2763     }
2764   }
2765 
2766   DCHECK(expect_null || result != nullptr)
2767       << "use of deleted " << ToStr<IndirectRefKind>(kind).c_str()
2768       << " " << static_cast<const void*>(obj);
2769   return result;
2770 }
2771 
IsJWeakCleared(jweak obj) const2772 bool Thread::IsJWeakCleared(jweak obj) const {
2773   CHECK(obj != nullptr);
2774   IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
2775   IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
2776   CHECK_EQ(kind, kWeakGlobal);
2777   return tlsPtr_.jni_env->vm_->IsWeakGlobalCleared(const_cast<Thread*>(this), ref);
2778 }
2779 
2780 // Implements java.lang.Thread.interrupted.
Interrupted()2781 bool Thread::Interrupted() {
2782   DCHECK_EQ(Thread::Current(), this);
2783   // No other thread can concurrently reset the interrupted flag.
2784   bool interrupted = tls32_.interrupted.load(std::memory_order_seq_cst);
2785   if (interrupted) {
2786     tls32_.interrupted.store(false, std::memory_order_seq_cst);
2787   }
2788   return interrupted;
2789 }
2790 
2791 // Implements java.lang.Thread.isInterrupted.
IsInterrupted()2792 bool Thread::IsInterrupted() {
2793   return tls32_.interrupted.load(std::memory_order_seq_cst);
2794 }
2795 
Interrupt(Thread * self)2796 void Thread::Interrupt(Thread* self) {
2797   {
2798     MutexLock mu(self, *wait_mutex_);
2799     if (tls32_.interrupted.load(std::memory_order_seq_cst)) {
2800       return;
2801     }
2802     tls32_.interrupted.store(true, std::memory_order_seq_cst);
2803     NotifyLocked(self);
2804   }
2805   Unpark();
2806 }
2807 
Notify()2808 void Thread::Notify() {
2809   Thread* self = Thread::Current();
2810   MutexLock mu(self, *wait_mutex_);
2811   NotifyLocked(self);
2812 }
2813 
NotifyLocked(Thread * self)2814 void Thread::NotifyLocked(Thread* self) {
2815   if (wait_monitor_ != nullptr) {
2816     wait_cond_->Signal(self);
2817   }
2818 }
2819 
SetClassLoaderOverride(jobject class_loader_override)2820 void Thread::SetClassLoaderOverride(jobject class_loader_override) {
2821   if (tlsPtr_.class_loader_override != nullptr) {
2822     GetJniEnv()->DeleteGlobalRef(tlsPtr_.class_loader_override);
2823   }
2824   tlsPtr_.class_loader_override = GetJniEnv()->NewGlobalRef(class_loader_override);
2825 }
2826 
2827 using ArtMethodDexPcPair = std::pair<ArtMethod*, uint32_t>;
2828 
2829 // Counts the stack trace depth and also fetches the first max_saved_frames frames.
2830 class FetchStackTraceVisitor : public StackVisitor {
2831  public:
FetchStackTraceVisitor(Thread * thread,ArtMethodDexPcPair * saved_frames=nullptr,size_t max_saved_frames=0)2832   explicit FetchStackTraceVisitor(Thread* thread,
2833                                   ArtMethodDexPcPair* saved_frames = nullptr,
2834                                   size_t max_saved_frames = 0)
2835       REQUIRES_SHARED(Locks::mutator_lock_)
2836       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
2837         saved_frames_(saved_frames),
2838         max_saved_frames_(max_saved_frames) {}
2839 
VisitFrame()2840   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
2841     // We want to skip frames up to and including the exception's constructor.
2842     // Note we also skip the frame if it doesn't have a method (namely the callee
2843     // save frame)
2844     ArtMethod* m = GetMethod();
2845     if (skipping_ && !m->IsRuntimeMethod() &&
2846         !GetClassRoot<mirror::Throwable>()->IsAssignableFrom(m->GetDeclaringClass())) {
2847       skipping_ = false;
2848     }
2849     if (!skipping_) {
2850       if (!m->IsRuntimeMethod()) {  // Ignore runtime frames (in particular callee save).
2851         if (depth_ < max_saved_frames_) {
2852           saved_frames_[depth_].first = m;
2853           saved_frames_[depth_].second = m->IsProxyMethod() ? dex::kDexNoIndex : GetDexPc();
2854         }
2855         ++depth_;
2856       }
2857     } else {
2858       ++skip_depth_;
2859     }
2860     return true;
2861   }
2862 
GetDepth() const2863   uint32_t GetDepth() const {
2864     return depth_;
2865   }
2866 
GetSkipDepth() const2867   uint32_t GetSkipDepth() const {
2868     return skip_depth_;
2869   }
2870 
2871  private:
2872   uint32_t depth_ = 0;
2873   uint32_t skip_depth_ = 0;
2874   bool skipping_ = true;
2875   ArtMethodDexPcPair* saved_frames_;
2876   const size_t max_saved_frames_;
2877 
2878   DISALLOW_COPY_AND_ASSIGN(FetchStackTraceVisitor);
2879 };
2880 
2881 class BuildInternalStackTraceVisitor : public StackVisitor {
2882  public:
BuildInternalStackTraceVisitor(Thread * self,Thread * thread,uint32_t skip_depth)2883   BuildInternalStackTraceVisitor(Thread* self, Thread* thread, uint32_t skip_depth)
2884       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
2885         self_(self),
2886         skip_depth_(skip_depth),
2887         pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()) {}
2888 
Init(uint32_t depth)2889   bool Init(uint32_t depth) REQUIRES_SHARED(Locks::mutator_lock_) ACQUIRE(Roles::uninterruptible_) {
2890     // Allocate method trace as an object array where the first element is a pointer array that
2891     // contains the ArtMethod pointers and dex PCs. The rest of the elements are the declaring
2892     // class of the ArtMethod pointers.
2893     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2894     StackHandleScope<1> hs(self_);
2895     ObjPtr<mirror::Class> array_class =
2896         GetClassRoot<mirror::ObjectArray<mirror::Object>>(class_linker);
2897     // The first element is the methods and dex pc array, the other elements are declaring classes
2898     // for the methods to ensure classes in the stack trace don't get unloaded.
2899     Handle<mirror::ObjectArray<mirror::Object>> trace(
2900         hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc(
2901             hs.Self(), array_class, static_cast<int32_t>(depth) + 1)));
2902     if (trace == nullptr) {
2903       // Acquire uninterruptible_ in all paths.
2904       self_->StartAssertNoThreadSuspension("Building internal stack trace");
2905       self_->AssertPendingOOMException();
2906       return false;
2907     }
2908     ObjPtr<mirror::PointerArray> methods_and_pcs =
2909         class_linker->AllocPointerArray(self_, depth * 2);
2910     const char* last_no_suspend_cause =
2911         self_->StartAssertNoThreadSuspension("Building internal stack trace");
2912     if (methods_and_pcs == nullptr) {
2913       self_->AssertPendingOOMException();
2914       return false;
2915     }
2916     trace->Set</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(0, methods_and_pcs);
2917     trace_ = trace.Get();
2918     // If We are called from native, use non-transactional mode.
2919     CHECK(last_no_suspend_cause == nullptr) << last_no_suspend_cause;
2920     return true;
2921   }
2922 
RELEASE(Roles::uninterruptible_)2923   virtual ~BuildInternalStackTraceVisitor() RELEASE(Roles::uninterruptible_) {
2924     self_->EndAssertNoThreadSuspension(nullptr);
2925   }
2926 
VisitFrame()2927   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
2928     if (trace_ == nullptr) {
2929       return true;  // We're probably trying to fillInStackTrace for an OutOfMemoryError.
2930     }
2931     if (skip_depth_ > 0) {
2932       skip_depth_--;
2933       return true;
2934     }
2935     ArtMethod* m = GetMethod();
2936     if (m->IsRuntimeMethod()) {
2937       return true;  // Ignore runtime frames (in particular callee save).
2938     }
2939     AddFrame(m, m->IsProxyMethod() ? dex::kDexNoIndex : GetDexPc());
2940     return true;
2941   }
2942 
AddFrame(ArtMethod * method,uint32_t dex_pc)2943   void AddFrame(ArtMethod* method, uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) {
2944     ObjPtr<mirror::PointerArray> methods_and_pcs = GetTraceMethodsAndPCs();
2945     methods_and_pcs->SetElementPtrSize</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
2946         count_, method, pointer_size_);
2947     methods_and_pcs->SetElementPtrSize</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
2948         static_cast<uint32_t>(methods_and_pcs->GetLength()) / 2 + count_, dex_pc, pointer_size_);
2949     // Save the declaring class of the method to ensure that the declaring classes of the methods
2950     // do not get unloaded while the stack trace is live.
2951     trace_->Set</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
2952         static_cast<int32_t>(count_) + 1, method->GetDeclaringClass());
2953     ++count_;
2954   }
2955 
GetTraceMethodsAndPCs() const2956   ObjPtr<mirror::PointerArray> GetTraceMethodsAndPCs() const REQUIRES_SHARED(Locks::mutator_lock_) {
2957     return ObjPtr<mirror::PointerArray>::DownCast(trace_->Get(0));
2958   }
2959 
GetInternalStackTrace() const2960   mirror::ObjectArray<mirror::Object>* GetInternalStackTrace() const {
2961     return trace_;
2962   }
2963 
2964  private:
2965   Thread* const self_;
2966   // How many more frames to skip.
2967   uint32_t skip_depth_;
2968   // Current position down stack trace.
2969   uint32_t count_ = 0;
2970   // An object array where the first element is a pointer array that contains the ArtMethod
2971   // pointers on the stack and dex PCs. The rest of the elements are the declaring class of
2972   // the ArtMethod pointers. trace_[i+1] contains the declaring class of the ArtMethod of the
2973   // i'th frame. We're initializing a newly allocated trace, so we do not need to record that
2974   // under a transaction. If the transaction is aborted, the whole trace shall be unreachable.
2975   mirror::ObjectArray<mirror::Object>* trace_ = nullptr;
2976   // For cross compilation.
2977   const PointerSize pointer_size_;
2978 
2979   DISALLOW_COPY_AND_ASSIGN(BuildInternalStackTraceVisitor);
2980 };
2981 
CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable & soa) const2982 jobject Thread::CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const {
2983   // Compute depth of stack, save frames if possible to avoid needing to recompute many.
2984   constexpr size_t kMaxSavedFrames = 256;
2985   std::unique_ptr<ArtMethodDexPcPair[]> saved_frames(new ArtMethodDexPcPair[kMaxSavedFrames]);
2986   FetchStackTraceVisitor count_visitor(const_cast<Thread*>(this),
2987                                        &saved_frames[0],
2988                                        kMaxSavedFrames);
2989   count_visitor.WalkStack();
2990   const uint32_t depth = count_visitor.GetDepth();
2991   const uint32_t skip_depth = count_visitor.GetSkipDepth();
2992 
2993   // Build internal stack trace.
2994   BuildInternalStackTraceVisitor build_trace_visitor(
2995       soa.Self(), const_cast<Thread*>(this), skip_depth);
2996   if (!build_trace_visitor.Init(depth)) {
2997     return nullptr;  // Allocation failed.
2998   }
2999   // If we saved all of the frames we don't even need to do the actual stack walk. This is faster
3000   // than doing the stack walk twice.
3001   if (depth < kMaxSavedFrames) {
3002     for (size_t i = 0; i < depth; ++i) {
3003       build_trace_visitor.AddFrame(saved_frames[i].first, saved_frames[i].second);
3004     }
3005   } else {
3006     build_trace_visitor.WalkStack();
3007   }
3008 
3009   mirror::ObjectArray<mirror::Object>* trace = build_trace_visitor.GetInternalStackTrace();
3010   if (kIsDebugBuild) {
3011     ObjPtr<mirror::PointerArray> trace_methods = build_trace_visitor.GetTraceMethodsAndPCs();
3012     // Second half of trace_methods is dex PCs.
3013     for (uint32_t i = 0; i < static_cast<uint32_t>(trace_methods->GetLength() / 2); ++i) {
3014       auto* method = trace_methods->GetElementPtrSize<ArtMethod*>(
3015           i, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
3016       CHECK(method != nullptr);
3017     }
3018   }
3019   return soa.AddLocalReference<jobject>(trace);
3020 }
3021 
IsExceptionThrownByCurrentMethod(ObjPtr<mirror::Throwable> exception) const3022 bool Thread::IsExceptionThrownByCurrentMethod(ObjPtr<mirror::Throwable> exception) const {
3023   // Only count the depth since we do not pass a stack frame array as an argument.
3024   FetchStackTraceVisitor count_visitor(const_cast<Thread*>(this));
3025   count_visitor.WalkStack();
3026   return count_visitor.GetDepth() == static_cast<uint32_t>(exception->GetStackDepth());
3027 }
3028 
CreateStackTraceElement(const ScopedObjectAccessAlreadyRunnable & soa,ArtMethod * method,uint32_t dex_pc)3029 static ObjPtr<mirror::StackTraceElement> CreateStackTraceElement(
3030     const ScopedObjectAccessAlreadyRunnable& soa,
3031     ArtMethod* method,
3032     uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) {
3033   int32_t line_number;
3034   StackHandleScope<3> hs(soa.Self());
3035   auto class_name_object(hs.NewHandle<mirror::String>(nullptr));
3036   auto source_name_object(hs.NewHandle<mirror::String>(nullptr));
3037   if (method->IsProxyMethod()) {
3038     line_number = -1;
3039     class_name_object.Assign(method->GetDeclaringClass()->GetName());
3040     // source_name_object intentionally left null for proxy methods
3041   } else {
3042     line_number = method->GetLineNumFromDexPC(dex_pc);
3043     // Allocate element, potentially triggering GC
3044     // TODO: reuse class_name_object via Class::name_?
3045     const char* descriptor = method->GetDeclaringClassDescriptor();
3046     CHECK(descriptor != nullptr);
3047     std::string class_name(PrettyDescriptor(descriptor));
3048     class_name_object.Assign(
3049         mirror::String::AllocFromModifiedUtf8(soa.Self(), class_name.c_str()));
3050     if (class_name_object == nullptr) {
3051       soa.Self()->AssertPendingOOMException();
3052       return nullptr;
3053     }
3054     const char* source_file = method->GetDeclaringClassSourceFile();
3055     if (line_number == -1) {
3056       // Make the line_number field of StackTraceElement hold the dex pc.
3057       // source_name_object is intentionally left null if we failed to map the dex pc to
3058       // a line number (most probably because there is no debug info). See b/30183883.
3059       line_number = static_cast<int32_t>(dex_pc);
3060     } else {
3061       if (source_file != nullptr) {
3062         source_name_object.Assign(mirror::String::AllocFromModifiedUtf8(soa.Self(), source_file));
3063         if (source_name_object == nullptr) {
3064           soa.Self()->AssertPendingOOMException();
3065           return nullptr;
3066         }
3067       }
3068     }
3069   }
3070   const char* method_name = method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName();
3071   CHECK(method_name != nullptr);
3072   Handle<mirror::String> method_name_object(
3073       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), method_name)));
3074   if (method_name_object == nullptr) {
3075     return nullptr;
3076   }
3077   return mirror::StackTraceElement::Alloc(soa.Self(),
3078                                           class_name_object,
3079                                           method_name_object,
3080                                           source_name_object,
3081                                           line_number);
3082 }
3083 
InternalStackTraceToStackTraceElementArray(const ScopedObjectAccessAlreadyRunnable & soa,jobject internal,jobjectArray output_array,int * stack_depth)3084 jobjectArray Thread::InternalStackTraceToStackTraceElementArray(
3085     const ScopedObjectAccessAlreadyRunnable& soa,
3086     jobject internal,
3087     jobjectArray output_array,
3088     int* stack_depth) {
3089   // Decode the internal stack trace into the depth, method trace and PC trace.
3090   // Subtract one for the methods and PC trace.
3091   int32_t depth = soa.Decode<mirror::Array>(internal)->GetLength() - 1;
3092   DCHECK_GE(depth, 0);
3093 
3094   ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
3095 
3096   jobjectArray result;
3097 
3098   if (output_array != nullptr) {
3099     // Reuse the array we were given.
3100     result = output_array;
3101     // ...adjusting the number of frames we'll write to not exceed the array length.
3102     const int32_t traces_length =
3103         soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>>(result)->GetLength();
3104     depth = std::min(depth, traces_length);
3105   } else {
3106     // Create java_trace array and place in local reference table
3107     ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> java_traces =
3108         class_linker->AllocStackTraceElementArray(soa.Self(), static_cast<size_t>(depth));
3109     if (java_traces == nullptr) {
3110       return nullptr;
3111     }
3112     result = soa.AddLocalReference<jobjectArray>(java_traces);
3113   }
3114 
3115   if (stack_depth != nullptr) {
3116     *stack_depth = depth;
3117   }
3118 
3119   for (uint32_t i = 0; i < static_cast<uint32_t>(depth); ++i) {
3120     ObjPtr<mirror::ObjectArray<mirror::Object>> decoded_traces =
3121         soa.Decode<mirror::Object>(internal)->AsObjectArray<mirror::Object>();
3122     // Methods and dex PC trace is element 0.
3123     DCHECK(decoded_traces->Get(0)->IsIntArray() || decoded_traces->Get(0)->IsLongArray());
3124     const ObjPtr<mirror::PointerArray> method_trace =
3125         ObjPtr<mirror::PointerArray>::DownCast(decoded_traces->Get(0));
3126     // Prepare parameters for StackTraceElement(String cls, String method, String file, int line)
3127     ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, kRuntimePointerSize);
3128     uint32_t dex_pc = method_trace->GetElementPtrSize<uint32_t>(
3129         i + static_cast<uint32_t>(method_trace->GetLength()) / 2, kRuntimePointerSize);
3130     const ObjPtr<mirror::StackTraceElement> obj = CreateStackTraceElement(soa, method, dex_pc);
3131     if (obj == nullptr) {
3132       return nullptr;
3133     }
3134     // We are called from native: use non-transactional mode.
3135     soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>>(result)->Set<false>(
3136         static_cast<int32_t>(i), obj);
3137   }
3138   return result;
3139 }
3140 
CreateAnnotatedStackTrace(const ScopedObjectAccessAlreadyRunnable & soa) const3141 jobjectArray Thread::CreateAnnotatedStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const {
3142   // This code allocates. Do not allow it to operate with a pending exception.
3143   if (IsExceptionPending()) {
3144     return nullptr;
3145   }
3146 
3147   class CollectFramesAndLocksStackVisitor : public MonitorObjectsStackVisitor {
3148    public:
3149     CollectFramesAndLocksStackVisitor(const ScopedObjectAccessAlreadyRunnable& soaa_in,
3150                                       Thread* self,
3151                                       Context* context)
3152         : MonitorObjectsStackVisitor(self, context),
3153           wait_jobject_(soaa_in.Env(), nullptr),
3154           block_jobject_(soaa_in.Env(), nullptr),
3155           soaa_(soaa_in) {}
3156 
3157    protected:
3158     VisitMethodResult StartMethod(ArtMethod* m, size_t frame_nr ATTRIBUTE_UNUSED)
3159         override
3160         REQUIRES_SHARED(Locks::mutator_lock_) {
3161       ObjPtr<mirror::StackTraceElement> obj = CreateStackTraceElement(
3162           soaa_, m, GetDexPc(/* abort on error */ false));
3163       if (obj == nullptr) {
3164         return VisitMethodResult::kEndStackWalk;
3165       }
3166       stack_trace_elements_.emplace_back(soaa_.Env(), soaa_.AddLocalReference<jobject>(obj.Ptr()));
3167       return VisitMethodResult::kContinueMethod;
3168     }
3169 
3170     VisitMethodResult EndMethod(ArtMethod* m ATTRIBUTE_UNUSED) override {
3171       lock_objects_.push_back({});
3172       lock_objects_[lock_objects_.size() - 1].swap(frame_lock_objects_);
3173 
3174       DCHECK_EQ(lock_objects_.size(), stack_trace_elements_.size());
3175 
3176       return VisitMethodResult::kContinueMethod;
3177     }
3178 
3179     void VisitWaitingObject(ObjPtr<mirror::Object> obj, ThreadState state ATTRIBUTE_UNUSED)
3180         override
3181         REQUIRES_SHARED(Locks::mutator_lock_) {
3182       wait_jobject_.reset(soaa_.AddLocalReference<jobject>(obj));
3183     }
3184     void VisitSleepingObject(ObjPtr<mirror::Object> obj)
3185         override
3186         REQUIRES_SHARED(Locks::mutator_lock_) {
3187       wait_jobject_.reset(soaa_.AddLocalReference<jobject>(obj));
3188     }
3189     void VisitBlockedOnObject(ObjPtr<mirror::Object> obj,
3190                               ThreadState state ATTRIBUTE_UNUSED,
3191                               uint32_t owner_tid ATTRIBUTE_UNUSED)
3192         override
3193         REQUIRES_SHARED(Locks::mutator_lock_) {
3194       block_jobject_.reset(soaa_.AddLocalReference<jobject>(obj));
3195     }
3196     void VisitLockedObject(ObjPtr<mirror::Object> obj)
3197         override
3198         REQUIRES_SHARED(Locks::mutator_lock_) {
3199       frame_lock_objects_.emplace_back(soaa_.Env(), soaa_.AddLocalReference<jobject>(obj));
3200     }
3201 
3202    public:
3203     std::vector<ScopedLocalRef<jobject>> stack_trace_elements_;
3204     ScopedLocalRef<jobject> wait_jobject_;
3205     ScopedLocalRef<jobject> block_jobject_;
3206     std::vector<std::vector<ScopedLocalRef<jobject>>> lock_objects_;
3207 
3208    private:
3209     const ScopedObjectAccessAlreadyRunnable& soaa_;
3210 
3211     std::vector<ScopedLocalRef<jobject>> frame_lock_objects_;
3212   };
3213 
3214   std::unique_ptr<Context> context(Context::Create());
3215   CollectFramesAndLocksStackVisitor dumper(soa, const_cast<Thread*>(this), context.get());
3216   dumper.WalkStack();
3217 
3218   // There should not be a pending exception. Otherwise, return with it pending.
3219   if (IsExceptionPending()) {
3220     return nullptr;
3221   }
3222 
3223   // Now go and create Java arrays.
3224 
3225   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3226 
3227   StackHandleScope<6> hs(soa.Self());
3228   Handle<mirror::Class> h_aste_array_class = hs.NewHandle(class_linker->FindSystemClass(
3229       soa.Self(),
3230       "[Ldalvik/system/AnnotatedStackTraceElement;"));
3231   if (h_aste_array_class == nullptr) {
3232     return nullptr;
3233   }
3234   Handle<mirror::Class> h_aste_class = hs.NewHandle(h_aste_array_class->GetComponentType());
3235 
3236   Handle<mirror::Class> h_o_array_class =
3237       hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Object>>(class_linker));
3238   DCHECK(h_o_array_class != nullptr);  // Class roots must be already initialized.
3239 
3240 
3241   // Make sure the AnnotatedStackTraceElement.class is initialized, b/76208924 .
3242   class_linker->EnsureInitialized(soa.Self(),
3243                                   h_aste_class,
3244                                   /* can_init_fields= */ true,
3245                                   /* can_init_parents= */ true);
3246   if (soa.Self()->IsExceptionPending()) {
3247     // This should not fail in a healthy runtime.
3248     return nullptr;
3249   }
3250 
3251   ArtField* stack_trace_element_field =
3252       h_aste_class->FindDeclaredInstanceField("stackTraceElement", "Ljava/lang/StackTraceElement;");
3253   DCHECK(stack_trace_element_field != nullptr);
3254   ArtField* held_locks_field =
3255       h_aste_class->FindDeclaredInstanceField("heldLocks", "[Ljava/lang/Object;");
3256   DCHECK(held_locks_field != nullptr);
3257   ArtField* blocked_on_field =
3258       h_aste_class->FindDeclaredInstanceField("blockedOn", "Ljava/lang/Object;");
3259   DCHECK(blocked_on_field != nullptr);
3260 
3261   int32_t length = static_cast<int32_t>(dumper.stack_trace_elements_.size());
3262   ObjPtr<mirror::ObjectArray<mirror::Object>> array =
3263       mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), h_aste_array_class.Get(), length);
3264   if (array == nullptr) {
3265     soa.Self()->AssertPendingOOMException();
3266     return nullptr;
3267   }
3268 
3269   ScopedLocalRef<jobjectArray> result(soa.Env(), soa.Env()->AddLocalReference<jobjectArray>(array));
3270 
3271   MutableHandle<mirror::Object> handle(hs.NewHandle<mirror::Object>(nullptr));
3272   MutableHandle<mirror::ObjectArray<mirror::Object>> handle2(
3273       hs.NewHandle<mirror::ObjectArray<mirror::Object>>(nullptr));
3274   for (size_t i = 0; i != static_cast<size_t>(length); ++i) {
3275     handle.Assign(h_aste_class->AllocObject(soa.Self()));
3276     if (handle == nullptr) {
3277       soa.Self()->AssertPendingOOMException();
3278       return nullptr;
3279     }
3280 
3281     // Set stack trace element.
3282     stack_trace_element_field->SetObject<false>(
3283         handle.Get(), soa.Decode<mirror::Object>(dumper.stack_trace_elements_[i].get()));
3284 
3285     // Create locked-on array.
3286     if (!dumper.lock_objects_[i].empty()) {
3287       handle2.Assign(mirror::ObjectArray<mirror::Object>::Alloc(
3288           soa.Self(), h_o_array_class.Get(), static_cast<int32_t>(dumper.lock_objects_[i].size())));
3289       if (handle2 == nullptr) {
3290         soa.Self()->AssertPendingOOMException();
3291         return nullptr;
3292       }
3293       int32_t j = 0;
3294       for (auto& scoped_local : dumper.lock_objects_[i]) {
3295         if (scoped_local == nullptr) {
3296           continue;
3297         }
3298         handle2->Set(j, soa.Decode<mirror::Object>(scoped_local.get()));
3299         DCHECK(!soa.Self()->IsExceptionPending());
3300         j++;
3301       }
3302       held_locks_field->SetObject<false>(handle.Get(), handle2.Get());
3303     }
3304 
3305     // Set blocked-on object.
3306     if (i == 0) {
3307       if (dumper.block_jobject_ != nullptr) {
3308         blocked_on_field->SetObject<false>(
3309             handle.Get(), soa.Decode<mirror::Object>(dumper.block_jobject_.get()));
3310       }
3311     }
3312 
3313     ScopedLocalRef<jobject> elem(soa.Env(), soa.AddLocalReference<jobject>(handle.Get()));
3314     soa.Env()->SetObjectArrayElement(result.get(), static_cast<jsize>(i), elem.get());
3315     DCHECK(!soa.Self()->IsExceptionPending());
3316   }
3317 
3318   return result.release();
3319 }
3320 
ThrowNewExceptionF(const char * exception_class_descriptor,const char * fmt,...)3321 void Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) {
3322   va_list args;
3323   va_start(args, fmt);
3324   ThrowNewExceptionV(exception_class_descriptor, fmt, args);
3325   va_end(args);
3326 }
3327 
ThrowNewExceptionV(const char * exception_class_descriptor,const char * fmt,va_list ap)3328 void Thread::ThrowNewExceptionV(const char* exception_class_descriptor,
3329                                 const char* fmt, va_list ap) {
3330   std::string msg;
3331   StringAppendV(&msg, fmt, ap);
3332   ThrowNewException(exception_class_descriptor, msg.c_str());
3333 }
3334 
ThrowNewException(const char * exception_class_descriptor,const char * msg)3335 void Thread::ThrowNewException(const char* exception_class_descriptor,
3336                                const char* msg) {
3337   // Callers should either clear or call ThrowNewWrappedException.
3338   AssertNoPendingExceptionForNewException(msg);
3339   ThrowNewWrappedException(exception_class_descriptor, msg);
3340 }
3341 
GetCurrentClassLoader(Thread * self)3342 static ObjPtr<mirror::ClassLoader> GetCurrentClassLoader(Thread* self)
3343     REQUIRES_SHARED(Locks::mutator_lock_) {
3344   ArtMethod* method = self->GetCurrentMethod(nullptr);
3345   return method != nullptr
3346       ? method->GetDeclaringClass()->GetClassLoader()
3347       : nullptr;
3348 }
3349 
ThrowNewWrappedException(const char * exception_class_descriptor,const char * msg)3350 void Thread::ThrowNewWrappedException(const char* exception_class_descriptor,
3351                                       const char* msg) {
3352   DCHECK_EQ(this, Thread::Current());
3353   ScopedObjectAccessUnchecked soa(this);
3354   StackHandleScope<3> hs(soa.Self());
3355 
3356   // Disable public sdk checks if we need to throw exceptions.
3357   // The checks are only used in AOT compilation and may block (exception) class
3358   // initialization if it needs access to private fields (e.g. serialVersionUID).
3359   //
3360   // Since throwing an exception will EnsureInitialization and the public sdk may
3361   // block that, disable the checks. It's ok to do so, because the thrown exceptions
3362   // are not part of the application code that needs to verified.
3363   ScopedDisablePublicSdkChecker sdpsc;
3364 
3365   Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetCurrentClassLoader(soa.Self())));
3366   ScopedLocalRef<jobject> cause(GetJniEnv(), soa.AddLocalReference<jobject>(GetException()));
3367   ClearException();
3368   Runtime* runtime = Runtime::Current();
3369   auto* cl = runtime->GetClassLinker();
3370   Handle<mirror::Class> exception_class(
3371       hs.NewHandle(cl->FindClass(this, exception_class_descriptor, class_loader)));
3372   if (UNLIKELY(exception_class == nullptr)) {
3373     CHECK(IsExceptionPending());
3374     LOG(ERROR) << "No exception class " << PrettyDescriptor(exception_class_descriptor);
3375     return;
3376   }
3377 
3378   if (UNLIKELY(!runtime->GetClassLinker()->EnsureInitialized(soa.Self(), exception_class, true,
3379                                                              true))) {
3380     DCHECK(IsExceptionPending());
3381     return;
3382   }
3383   DCHECK_IMPLIES(runtime->IsStarted(), exception_class->IsThrowableClass());
3384   Handle<mirror::Throwable> exception(
3385       hs.NewHandle(ObjPtr<mirror::Throwable>::DownCast(exception_class->AllocObject(this))));
3386 
3387   // If we couldn't allocate the exception, throw the pre-allocated out of memory exception.
3388   if (exception == nullptr) {
3389     Dump(LOG_STREAM(WARNING));  // The pre-allocated OOME has no stack, so help out and log one.
3390     SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryErrorWhenThrowingException());
3391     return;
3392   }
3393 
3394   // Choose an appropriate constructor and set up the arguments.
3395   const char* signature;
3396   ScopedLocalRef<jstring> msg_string(GetJniEnv(), nullptr);
3397   if (msg != nullptr) {
3398     // Ensure we remember this and the method over the String allocation.
3399     msg_string.reset(
3400         soa.AddLocalReference<jstring>(mirror::String::AllocFromModifiedUtf8(this, msg)));
3401     if (UNLIKELY(msg_string.get() == nullptr)) {
3402       CHECK(IsExceptionPending());  // OOME.
3403       return;
3404     }
3405     if (cause.get() == nullptr) {
3406       signature = "(Ljava/lang/String;)V";
3407     } else {
3408       signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
3409     }
3410   } else {
3411     if (cause.get() == nullptr) {
3412       signature = "()V";
3413     } else {
3414       signature = "(Ljava/lang/Throwable;)V";
3415     }
3416   }
3417   ArtMethod* exception_init_method =
3418       exception_class->FindConstructor(signature, cl->GetImagePointerSize());
3419 
3420   CHECK(exception_init_method != nullptr) << "No <init>" << signature << " in "
3421       << PrettyDescriptor(exception_class_descriptor);
3422 
3423   if (UNLIKELY(!runtime->IsStarted())) {
3424     // Something is trying to throw an exception without a started runtime, which is the common
3425     // case in the compiler. We won't be able to invoke the constructor of the exception, so set
3426     // the exception fields directly.
3427     if (msg != nullptr) {
3428       exception->SetDetailMessage(DecodeJObject(msg_string.get())->AsString());
3429     }
3430     if (cause.get() != nullptr) {
3431       exception->SetCause(DecodeJObject(cause.get())->AsThrowable());
3432     }
3433     ScopedLocalRef<jobject> trace(GetJniEnv(), CreateInternalStackTrace(soa));
3434     if (trace.get() != nullptr) {
3435       exception->SetStackState(DecodeJObject(trace.get()).Ptr());
3436     }
3437     SetException(exception.Get());
3438   } else {
3439     jvalue jv_args[2];
3440     size_t i = 0;
3441 
3442     if (msg != nullptr) {
3443       jv_args[i].l = msg_string.get();
3444       ++i;
3445     }
3446     if (cause.get() != nullptr) {
3447       jv_args[i].l = cause.get();
3448       ++i;
3449     }
3450     ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(exception.Get()));
3451     InvokeWithJValues(soa, ref.get(), exception_init_method, jv_args);
3452     if (LIKELY(!IsExceptionPending())) {
3453       SetException(exception.Get());
3454     }
3455   }
3456 }
3457 
ThrowOutOfMemoryError(const char * msg)3458 void Thread::ThrowOutOfMemoryError(const char* msg) {
3459   LOG(WARNING) << "Throwing OutOfMemoryError "
3460                << '"' << msg << '"'
3461                << " (VmSize " << GetProcessStatus("VmSize")
3462                << (tls32_.throwing_OutOfMemoryError ? ", recursive case)" : ")");
3463   ScopedTrace trace("OutOfMemoryError");
3464   if (!tls32_.throwing_OutOfMemoryError) {
3465     tls32_.throwing_OutOfMemoryError = true;
3466     ThrowNewException("Ljava/lang/OutOfMemoryError;", msg);
3467     tls32_.throwing_OutOfMemoryError = false;
3468   } else {
3469     Dump(LOG_STREAM(WARNING));  // The pre-allocated OOME has no stack, so help out and log one.
3470     SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryErrorWhenThrowingOOME());
3471   }
3472 }
3473 
CurrentFromGdb()3474 Thread* Thread::CurrentFromGdb() {
3475   return Thread::Current();
3476 }
3477 
DumpFromGdb() const3478 void Thread::DumpFromGdb() const {
3479   std::ostringstream ss;
3480   Dump(ss);
3481   std::string str(ss.str());
3482   // log to stderr for debugging command line processes
3483   std::cerr << str;
3484 #ifdef ART_TARGET_ANDROID
3485   // log to logcat for debugging frameworks processes
3486   LOG(INFO) << str;
3487 #endif
3488 }
3489 
3490 // Explicitly instantiate 32 and 64bit thread offset dumping support.
3491 template
3492 void Thread::DumpThreadOffset<PointerSize::k32>(std::ostream& os, uint32_t offset);
3493 template
3494 void Thread::DumpThreadOffset<PointerSize::k64>(std::ostream& os, uint32_t offset);
3495 
3496 template<PointerSize ptr_size>
DumpThreadOffset(std::ostream & os,uint32_t offset)3497 void Thread::DumpThreadOffset(std::ostream& os, uint32_t offset) {
3498 #define DO_THREAD_OFFSET(x, y) \
3499     if (offset == (x).Uint32Value()) { \
3500       os << (y); \
3501       return; \
3502     }
3503   DO_THREAD_OFFSET(ThreadFlagsOffset<ptr_size>(), "state_and_flags")
3504   DO_THREAD_OFFSET(CardTableOffset<ptr_size>(), "card_table")
3505   DO_THREAD_OFFSET(ExceptionOffset<ptr_size>(), "exception")
3506   DO_THREAD_OFFSET(PeerOffset<ptr_size>(), "peer");
3507   DO_THREAD_OFFSET(JniEnvOffset<ptr_size>(), "jni_env")
3508   DO_THREAD_OFFSET(SelfOffset<ptr_size>(), "self")
3509   DO_THREAD_OFFSET(StackEndOffset<ptr_size>(), "stack_end")
3510   DO_THREAD_OFFSET(ThinLockIdOffset<ptr_size>(), "thin_lock_thread_id")
3511   DO_THREAD_OFFSET(IsGcMarkingOffset<ptr_size>(), "is_gc_marking")
3512   DO_THREAD_OFFSET(TopOfManagedStackOffset<ptr_size>(), "top_quick_frame_method")
3513   DO_THREAD_OFFSET(TopShadowFrameOffset<ptr_size>(), "top_shadow_frame")
3514   DO_THREAD_OFFSET(TopHandleScopeOffset<ptr_size>(), "top_handle_scope")
3515   DO_THREAD_OFFSET(ThreadSuspendTriggerOffset<ptr_size>(), "suspend_trigger")
3516 #undef DO_THREAD_OFFSET
3517 
3518 #define JNI_ENTRY_POINT_INFO(x) \
3519     if (JNI_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \
3520       os << #x; \
3521       return; \
3522     }
3523   JNI_ENTRY_POINT_INFO(pDlsymLookup)
3524   JNI_ENTRY_POINT_INFO(pDlsymLookupCritical)
3525 #undef JNI_ENTRY_POINT_INFO
3526 
3527 #define QUICK_ENTRY_POINT_INFO(x) \
3528     if (QUICK_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \
3529       os << #x; \
3530       return; \
3531     }
3532   QUICK_ENTRY_POINT_INFO(pAllocArrayResolved)
3533   QUICK_ENTRY_POINT_INFO(pAllocArrayResolved8)
3534   QUICK_ENTRY_POINT_INFO(pAllocArrayResolved16)
3535   QUICK_ENTRY_POINT_INFO(pAllocArrayResolved32)
3536   QUICK_ENTRY_POINT_INFO(pAllocArrayResolved64)
3537   QUICK_ENTRY_POINT_INFO(pAllocObjectResolved)
3538   QUICK_ENTRY_POINT_INFO(pAllocObjectInitialized)
3539   QUICK_ENTRY_POINT_INFO(pAllocObjectWithChecks)
3540   QUICK_ENTRY_POINT_INFO(pAllocStringObject)
3541   QUICK_ENTRY_POINT_INFO(pAllocStringFromBytes)
3542   QUICK_ENTRY_POINT_INFO(pAllocStringFromChars)
3543   QUICK_ENTRY_POINT_INFO(pAllocStringFromString)
3544   QUICK_ENTRY_POINT_INFO(pInstanceofNonTrivial)
3545   QUICK_ENTRY_POINT_INFO(pCheckInstanceOf)
3546   QUICK_ENTRY_POINT_INFO(pInitializeStaticStorage)
3547   QUICK_ENTRY_POINT_INFO(pResolveTypeAndVerifyAccess)
3548   QUICK_ENTRY_POINT_INFO(pResolveType)
3549   QUICK_ENTRY_POINT_INFO(pResolveString)
3550   QUICK_ENTRY_POINT_INFO(pSet8Instance)
3551   QUICK_ENTRY_POINT_INFO(pSet8Static)
3552   QUICK_ENTRY_POINT_INFO(pSet16Instance)
3553   QUICK_ENTRY_POINT_INFO(pSet16Static)
3554   QUICK_ENTRY_POINT_INFO(pSet32Instance)
3555   QUICK_ENTRY_POINT_INFO(pSet32Static)
3556   QUICK_ENTRY_POINT_INFO(pSet64Instance)
3557   QUICK_ENTRY_POINT_INFO(pSet64Static)
3558   QUICK_ENTRY_POINT_INFO(pSetObjInstance)
3559   QUICK_ENTRY_POINT_INFO(pSetObjStatic)
3560   QUICK_ENTRY_POINT_INFO(pGetByteInstance)
3561   QUICK_ENTRY_POINT_INFO(pGetBooleanInstance)
3562   QUICK_ENTRY_POINT_INFO(pGetByteStatic)
3563   QUICK_ENTRY_POINT_INFO(pGetBooleanStatic)
3564   QUICK_ENTRY_POINT_INFO(pGetShortInstance)
3565   QUICK_ENTRY_POINT_INFO(pGetCharInstance)
3566   QUICK_ENTRY_POINT_INFO(pGetShortStatic)
3567   QUICK_ENTRY_POINT_INFO(pGetCharStatic)
3568   QUICK_ENTRY_POINT_INFO(pGet32Instance)
3569   QUICK_ENTRY_POINT_INFO(pGet32Static)
3570   QUICK_ENTRY_POINT_INFO(pGet64Instance)
3571   QUICK_ENTRY_POINT_INFO(pGet64Static)
3572   QUICK_ENTRY_POINT_INFO(pGetObjInstance)
3573   QUICK_ENTRY_POINT_INFO(pGetObjStatic)
3574   QUICK_ENTRY_POINT_INFO(pAputObject)
3575   QUICK_ENTRY_POINT_INFO(pJniMethodStart)
3576   QUICK_ENTRY_POINT_INFO(pJniMethodEnd)
3577   QUICK_ENTRY_POINT_INFO(pJniDecodeReferenceResult)
3578   QUICK_ENTRY_POINT_INFO(pJniLockObject)
3579   QUICK_ENTRY_POINT_INFO(pJniUnlockObject)
3580   QUICK_ENTRY_POINT_INFO(pQuickGenericJniTrampoline)
3581   QUICK_ENTRY_POINT_INFO(pLockObject)
3582   QUICK_ENTRY_POINT_INFO(pUnlockObject)
3583   QUICK_ENTRY_POINT_INFO(pCmpgDouble)
3584   QUICK_ENTRY_POINT_INFO(pCmpgFloat)
3585   QUICK_ENTRY_POINT_INFO(pCmplDouble)
3586   QUICK_ENTRY_POINT_INFO(pCmplFloat)
3587   QUICK_ENTRY_POINT_INFO(pCos)
3588   QUICK_ENTRY_POINT_INFO(pSin)
3589   QUICK_ENTRY_POINT_INFO(pAcos)
3590   QUICK_ENTRY_POINT_INFO(pAsin)
3591   QUICK_ENTRY_POINT_INFO(pAtan)
3592   QUICK_ENTRY_POINT_INFO(pAtan2)
3593   QUICK_ENTRY_POINT_INFO(pCbrt)
3594   QUICK_ENTRY_POINT_INFO(pCosh)
3595   QUICK_ENTRY_POINT_INFO(pExp)
3596   QUICK_ENTRY_POINT_INFO(pExpm1)
3597   QUICK_ENTRY_POINT_INFO(pHypot)
3598   QUICK_ENTRY_POINT_INFO(pLog)
3599   QUICK_ENTRY_POINT_INFO(pLog10)
3600   QUICK_ENTRY_POINT_INFO(pNextAfter)
3601   QUICK_ENTRY_POINT_INFO(pSinh)
3602   QUICK_ENTRY_POINT_INFO(pTan)
3603   QUICK_ENTRY_POINT_INFO(pTanh)
3604   QUICK_ENTRY_POINT_INFO(pFmod)
3605   QUICK_ENTRY_POINT_INFO(pL2d)
3606   QUICK_ENTRY_POINT_INFO(pFmodf)
3607   QUICK_ENTRY_POINT_INFO(pL2f)
3608   QUICK_ENTRY_POINT_INFO(pD2iz)
3609   QUICK_ENTRY_POINT_INFO(pF2iz)
3610   QUICK_ENTRY_POINT_INFO(pIdivmod)
3611   QUICK_ENTRY_POINT_INFO(pD2l)
3612   QUICK_ENTRY_POINT_INFO(pF2l)
3613   QUICK_ENTRY_POINT_INFO(pLdiv)
3614   QUICK_ENTRY_POINT_INFO(pLmod)
3615   QUICK_ENTRY_POINT_INFO(pLmul)
3616   QUICK_ENTRY_POINT_INFO(pShlLong)
3617   QUICK_ENTRY_POINT_INFO(pShrLong)
3618   QUICK_ENTRY_POINT_INFO(pUshrLong)
3619   QUICK_ENTRY_POINT_INFO(pIndexOf)
3620   QUICK_ENTRY_POINT_INFO(pStringCompareTo)
3621   QUICK_ENTRY_POINT_INFO(pMemcpy)
3622   QUICK_ENTRY_POINT_INFO(pQuickImtConflictTrampoline)
3623   QUICK_ENTRY_POINT_INFO(pQuickResolutionTrampoline)
3624   QUICK_ENTRY_POINT_INFO(pQuickToInterpreterBridge)
3625   QUICK_ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck)
3626   QUICK_ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck)
3627   QUICK_ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck)
3628   QUICK_ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck)
3629   QUICK_ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck)
3630   QUICK_ENTRY_POINT_INFO(pInvokePolymorphic)
3631   QUICK_ENTRY_POINT_INFO(pTestSuspend)
3632   QUICK_ENTRY_POINT_INFO(pDeliverException)
3633   QUICK_ENTRY_POINT_INFO(pThrowArrayBounds)
3634   QUICK_ENTRY_POINT_INFO(pThrowDivZero)
3635   QUICK_ENTRY_POINT_INFO(pThrowNullPointer)
3636   QUICK_ENTRY_POINT_INFO(pThrowStackOverflow)
3637   QUICK_ENTRY_POINT_INFO(pDeoptimize)
3638   QUICK_ENTRY_POINT_INFO(pA64Load)
3639   QUICK_ENTRY_POINT_INFO(pA64Store)
3640   QUICK_ENTRY_POINT_INFO(pNewEmptyString)
3641   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_B)
3642   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BI)
3643   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BII)
3644   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BIII)
3645   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BIIString)
3646   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BString)
3647   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BIICharset)
3648   QUICK_ENTRY_POINT_INFO(pNewStringFromBytes_BCharset)
3649   QUICK_ENTRY_POINT_INFO(pNewStringFromChars_C)
3650   QUICK_ENTRY_POINT_INFO(pNewStringFromChars_CII)
3651   QUICK_ENTRY_POINT_INFO(pNewStringFromChars_IIC)
3652   QUICK_ENTRY_POINT_INFO(pNewStringFromCodePoints)
3653   QUICK_ENTRY_POINT_INFO(pNewStringFromString)
3654   QUICK_ENTRY_POINT_INFO(pNewStringFromStringBuffer)
3655   QUICK_ENTRY_POINT_INFO(pNewStringFromStringBuilder)
3656   QUICK_ENTRY_POINT_INFO(pJniReadBarrier)
3657   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg00)
3658   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg01)
3659   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg02)
3660   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg03)
3661   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg04)
3662   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg05)
3663   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg06)
3664   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg07)
3665   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg08)
3666   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg09)
3667   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg10)
3668   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg11)
3669   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg12)
3670   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg13)
3671   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg14)
3672   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg15)
3673   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg16)
3674   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg17)
3675   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg18)
3676   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg19)
3677   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg20)
3678   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg21)
3679   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg22)
3680   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg23)
3681   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg24)
3682   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg25)
3683   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg26)
3684   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg27)
3685   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg28)
3686   QUICK_ENTRY_POINT_INFO(pReadBarrierMarkReg29)
3687   QUICK_ENTRY_POINT_INFO(pReadBarrierSlow)
3688   QUICK_ENTRY_POINT_INFO(pReadBarrierForRootSlow)
3689 #undef QUICK_ENTRY_POINT_INFO
3690 
3691   os << offset;
3692 }
3693 
QuickDeliverException()3694 void Thread::QuickDeliverException() {
3695   // Get exception from thread.
3696   ObjPtr<mirror::Throwable> exception = GetException();
3697   CHECK(exception != nullptr);
3698   if (exception == GetDeoptimizationException()) {
3699     artDeoptimize(this);
3700     UNREACHABLE();
3701   }
3702 
3703   ReadBarrier::MaybeAssertToSpaceInvariant(exception.Ptr());
3704 
3705   // This is a real exception: let the instrumentation know about it. Exception throw listener
3706   // could set a breakpoint or install listeners that might require a deoptimization. Hence the
3707   // deoptimization check needs to happen after calling the listener.
3708   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
3709   if (instrumentation->HasExceptionThrownListeners() &&
3710       IsExceptionThrownByCurrentMethod(exception)) {
3711     // Instrumentation may cause GC so keep the exception object safe.
3712     StackHandleScope<1> hs(this);
3713     HandleWrapperObjPtr<mirror::Throwable> h_exception(hs.NewHandleWrapper(&exception));
3714     instrumentation->ExceptionThrownEvent(this, exception);
3715   }
3716   // Does instrumentation need to deoptimize the stack or otherwise go to interpreter for something?
3717   // Note: we do this *after* reporting the exception to instrumentation in case it now requires
3718   // deoptimization. It may happen if a debugger is attached and requests new events (single-step,
3719   // breakpoint, ...) when the exception is reported.
3720   //
3721   // Note we need to check for both force_frame_pop and force_retry_instruction. The first is
3722   // expected to happen fairly regularly but the second can only happen if we are using
3723   // instrumentation trampolines (for example with DDMS tracing). That forces us to do deopt later
3724   // and see every frame being popped. We don't need to handle it any differently.
3725   ShadowFrame* cf;
3726   bool force_deopt = false;
3727   if (Runtime::Current()->AreNonStandardExitsEnabled() || kIsDebugBuild) {
3728     NthCallerVisitor visitor(this, 0, false);
3729     visitor.WalkStack();
3730     cf = visitor.GetCurrentShadowFrame();
3731     if (cf == nullptr) {
3732       cf = FindDebuggerShadowFrame(visitor.GetFrameId());
3733     }
3734     bool force_frame_pop = cf != nullptr && cf->GetForcePopFrame();
3735     bool force_retry_instr = cf != nullptr && cf->GetForceRetryInstruction();
3736     if (kIsDebugBuild && force_frame_pop) {
3737       DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
3738       NthCallerVisitor penultimate_visitor(this, 1, false);
3739       penultimate_visitor.WalkStack();
3740       ShadowFrame* penultimate_frame = penultimate_visitor.GetCurrentShadowFrame();
3741       if (penultimate_frame == nullptr) {
3742         penultimate_frame = FindDebuggerShadowFrame(penultimate_visitor.GetFrameId());
3743       }
3744     }
3745     if (force_retry_instr) {
3746       DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
3747     }
3748     force_deopt = force_frame_pop || force_retry_instr;
3749   }
3750   if (Dbg::IsForcedInterpreterNeededForException(this) || force_deopt || IsForceInterpreter()) {
3751     NthCallerVisitor visitor(this, 0, false);
3752     visitor.WalkStack();
3753     if (Runtime::Current()->IsAsyncDeoptimizeable(visitor.caller_pc)) {
3754       // method_type shouldn't matter due to exception handling.
3755       const DeoptimizationMethodType method_type = DeoptimizationMethodType::kDefault;
3756       // Save the exception into the deoptimization context so it can be restored
3757       // before entering the interpreter.
3758       if (force_deopt) {
3759         VLOG(deopt) << "Deopting " << cf->GetMethod()->PrettyMethod() << " for frame-pop";
3760         DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
3761         // Get rid of the exception since we are doing a framepop instead.
3762         LOG(WARNING) << "Suppressing pending exception for retry-instruction/frame-pop: "
3763                      << exception->Dump();
3764         ClearException();
3765       }
3766       PushDeoptimizationContext(
3767           JValue(),
3768           /* is_reference= */ false,
3769           (force_deopt ? nullptr : exception),
3770           /* from_code= */ false,
3771           method_type);
3772       artDeoptimize(this);
3773       UNREACHABLE();
3774     } else if (visitor.caller != nullptr) {
3775       LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
3776                    << visitor.caller->PrettyMethod();
3777     }
3778   }
3779 
3780   // Don't leave exception visible while we try to find the handler, which may cause class
3781   // resolution.
3782   ClearException();
3783   QuickExceptionHandler exception_handler(this, false);
3784   exception_handler.FindCatch(exception);
3785   if (exception_handler.GetClearException()) {
3786     // Exception was cleared as part of delivery.
3787     DCHECK(!IsExceptionPending());
3788   } else {
3789     // Exception was put back with a throw location.
3790     DCHECK(IsExceptionPending());
3791     // Check the to-space invariant on the re-installed exception (if applicable).
3792     ReadBarrier::MaybeAssertToSpaceInvariant(GetException());
3793   }
3794   exception_handler.DoLongJump();
3795 }
3796 
GetLongJumpContext()3797 Context* Thread::GetLongJumpContext() {
3798   Context* result = tlsPtr_.long_jump_context;
3799   if (result == nullptr) {
3800     result = Context::Create();
3801   } else {
3802     tlsPtr_.long_jump_context = nullptr;  // Avoid context being shared.
3803     result->Reset();
3804   }
3805   return result;
3806 }
3807 
GetCurrentMethod(uint32_t * dex_pc_out,bool check_suspended,bool abort_on_error) const3808 ArtMethod* Thread::GetCurrentMethod(uint32_t* dex_pc_out,
3809                                     bool check_suspended,
3810                                     bool abort_on_error) const {
3811   // Note: this visitor may return with a method set, but dex_pc_ being DexFile:kDexNoIndex. This is
3812   //       so we don't abort in a special situation (thinlocked monitor) when dumping the Java
3813   //       stack.
3814   ArtMethod* method = nullptr;
3815   uint32_t dex_pc = dex::kDexNoIndex;
3816   StackVisitor::WalkStack(
3817       [&](const StackVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
3818         ArtMethod* m = visitor->GetMethod();
3819         if (m->IsRuntimeMethod()) {
3820           // Continue if this is a runtime method.
3821           return true;
3822         }
3823         method = m;
3824         dex_pc = visitor->GetDexPc(abort_on_error);
3825         return false;
3826       },
3827       const_cast<Thread*>(this),
3828       /* context= */ nullptr,
3829       StackVisitor::StackWalkKind::kIncludeInlinedFrames,
3830       check_suspended);
3831 
3832   if (dex_pc_out != nullptr) {
3833     *dex_pc_out = dex_pc;
3834   }
3835   return method;
3836 }
3837 
HoldsLock(ObjPtr<mirror::Object> object) const3838 bool Thread::HoldsLock(ObjPtr<mirror::Object> object) const {
3839   return object != nullptr && object->GetLockOwnerThreadId() == GetThreadId();
3840 }
3841 
3842 extern std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
3843     REQUIRES_SHARED(Locks::mutator_lock_);
3844 
3845 // RootVisitor parameters are: (const Object* obj, size_t vreg, const StackVisitor* visitor).
3846 template <typename RootVisitor, bool kPrecise = false>
3847 class ReferenceMapVisitor : public StackVisitor {
3848  public:
ReferenceMapVisitor(Thread * thread,Context * context,RootVisitor & visitor)3849   ReferenceMapVisitor(Thread* thread, Context* context, RootVisitor& visitor)
3850       REQUIRES_SHARED(Locks::mutator_lock_)
3851         // We are visiting the references in compiled frames, so we do not need
3852         // to know the inlined frames.
3853       : StackVisitor(thread, context, StackVisitor::StackWalkKind::kSkipInlinedFrames),
3854         visitor_(visitor) {}
3855 
VisitFrame()3856   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
3857     if (false) {
3858       LOG(INFO) << "Visiting stack roots in " << ArtMethod::PrettyMethod(GetMethod())
3859                 << StringPrintf("@ PC:%04x", GetDexPc());
3860     }
3861     ShadowFrame* shadow_frame = GetCurrentShadowFrame();
3862     if (shadow_frame != nullptr) {
3863       VisitShadowFrame(shadow_frame);
3864     } else if (GetCurrentOatQuickMethodHeader()->IsNterpMethodHeader()) {
3865       VisitNterpFrame();
3866     } else {
3867       VisitQuickFrame();
3868     }
3869     return true;
3870   }
3871 
VisitShadowFrame(ShadowFrame * shadow_frame)3872   void VisitShadowFrame(ShadowFrame* shadow_frame) REQUIRES_SHARED(Locks::mutator_lock_) {
3873     ArtMethod* m = shadow_frame->GetMethod();
3874     VisitDeclaringClass(m);
3875     DCHECK(m != nullptr);
3876     size_t num_regs = shadow_frame->NumberOfVRegs();
3877     // handle scope for JNI or References for interpreter.
3878     for (size_t reg = 0; reg < num_regs; ++reg) {
3879       mirror::Object* ref = shadow_frame->GetVRegReference(reg);
3880       if (ref != nullptr) {
3881         mirror::Object* new_ref = ref;
3882         visitor_(&new_ref, reg, this);
3883         if (new_ref != ref) {
3884           shadow_frame->SetVRegReference(reg, new_ref);
3885         }
3886       }
3887     }
3888     // Mark lock count map required for structured locking checks.
3889     shadow_frame->GetLockCountData().VisitMonitors(visitor_, /* vreg= */ -1, this);
3890   }
3891 
3892  private:
3893   // Visiting the declaring class is necessary so that we don't unload the class of a method that
3894   // is executing. We need to ensure that the code stays mapped. NO_THREAD_SAFETY_ANALYSIS since
3895   // the threads do not all hold the heap bitmap lock for parallel GC.
VisitDeclaringClass(ArtMethod * method)3896   void VisitDeclaringClass(ArtMethod* method)
3897       REQUIRES_SHARED(Locks::mutator_lock_)
3898       NO_THREAD_SAFETY_ANALYSIS {
3899     ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
3900     // klass can be null for runtime methods.
3901     if (klass != nullptr) {
3902       if (kVerifyImageObjectsMarked) {
3903         gc::Heap* const heap = Runtime::Current()->GetHeap();
3904         gc::space::ContinuousSpace* space = heap->FindContinuousSpaceFromObject(klass,
3905                                                                                 /*fail_ok=*/true);
3906         if (space != nullptr && space->IsImageSpace()) {
3907           bool failed = false;
3908           if (!space->GetLiveBitmap()->Test(klass.Ptr())) {
3909             failed = true;
3910             LOG(FATAL_WITHOUT_ABORT) << "Unmarked object in image " << *space;
3911           } else if (!heap->GetLiveBitmap()->Test(klass.Ptr())) {
3912             failed = true;
3913             LOG(FATAL_WITHOUT_ABORT) << "Unmarked object in image through live bitmap " << *space;
3914           }
3915           if (failed) {
3916             GetThread()->Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
3917             space->AsImageSpace()->DumpSections(LOG_STREAM(FATAL_WITHOUT_ABORT));
3918             LOG(FATAL_WITHOUT_ABORT) << "Method@" << method->GetDexMethodIndex() << ":" << method
3919                                      << " klass@" << klass.Ptr();
3920             // Pretty info last in case it crashes.
3921             LOG(FATAL) << "Method " << method->PrettyMethod() << " klass "
3922                        << klass->PrettyClass();
3923           }
3924         }
3925       }
3926       mirror::Object* new_ref = klass.Ptr();
3927       visitor_(&new_ref, /* vreg= */ JavaFrameRootInfo::kMethodDeclaringClass, this);
3928       if (new_ref != klass) {
3929         method->CASDeclaringClass(klass.Ptr(), new_ref->AsClass());
3930       }
3931     }
3932   }
3933 
VisitNterpFrame()3934   void VisitNterpFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
3935     ArtMethod** cur_quick_frame = GetCurrentQuickFrame();
3936     StackReference<mirror::Object>* vreg_ref_base =
3937         reinterpret_cast<StackReference<mirror::Object>*>(NterpGetReferenceArray(cur_quick_frame));
3938     StackReference<mirror::Object>* vreg_int_base =
3939         reinterpret_cast<StackReference<mirror::Object>*>(NterpGetRegistersArray(cur_quick_frame));
3940     CodeItemDataAccessor accessor((*cur_quick_frame)->DexInstructionData());
3941     const uint16_t num_regs = accessor.RegistersSize();
3942     // An nterp frame has two arrays: a dex register array and a reference array
3943     // that shadows the dex register array but only containing references
3944     // (non-reference dex registers have nulls). See nterp_helpers.cc.
3945     for (size_t reg = 0; reg < num_regs; ++reg) {
3946       StackReference<mirror::Object>* ref_addr = vreg_ref_base + reg;
3947       mirror::Object* ref = ref_addr->AsMirrorPtr();
3948       if (ref != nullptr) {
3949         mirror::Object* new_ref = ref;
3950         visitor_(&new_ref, reg, this);
3951         if (new_ref != ref) {
3952           ref_addr->Assign(new_ref);
3953           StackReference<mirror::Object>* int_addr = vreg_int_base + reg;
3954           int_addr->Assign(new_ref);
3955         }
3956       }
3957     }
3958   }
3959 
3960   template <typename T>
3961   ALWAYS_INLINE
VisitQuickFrameWithVregCallback()3962   inline void VisitQuickFrameWithVregCallback() REQUIRES_SHARED(Locks::mutator_lock_) {
3963     ArtMethod** cur_quick_frame = GetCurrentQuickFrame();
3964     DCHECK(cur_quick_frame != nullptr);
3965     ArtMethod* m = *cur_quick_frame;
3966     VisitDeclaringClass(m);
3967 
3968     if (m->IsNative()) {
3969       // TODO: Spill the `this` reference in the AOT-compiled String.charAt()
3970       // slow-path for throwing SIOOBE, so that we can remove this carve-out.
3971       if (UNLIKELY(m->IsIntrinsic()) &&
3972           m->GetIntrinsic() == enum_cast<uint32_t>(Intrinsics::kStringCharAt)) {
3973         // The String.charAt() method is AOT-compiled with an intrinsic implementation
3974         // instead of a JNI stub. It has a slow path that constructs a runtime frame
3975         // for throwing SIOOBE and in that path we do not get the `this` pointer
3976         // spilled on the stack, so there is nothing to visit. We can distinguish
3977         // this from the GenericJni path by checking that the PC is in the boot image
3978         // (PC shall be known thanks to the runtime frame for throwing SIOOBE).
3979         // Note that JIT does not emit that intrinic implementation.
3980         const void* pc = reinterpret_cast<const void*>(GetCurrentQuickFramePc());
3981         if (pc != 0u && Runtime::Current()->GetHeap()->IsInBootImageOatFile(pc)) {
3982           return;
3983         }
3984       }
3985       // Native methods spill their arguments to the reserved vregs in the caller's frame
3986       // and use pointers to these stack references as jobject, jclass, jarray, etc.
3987       // Note: We can come here for a @CriticalNative method when it needs to resolve the
3988       // target native function but there would be no references to visit below.
3989       const size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
3990       const size_t method_pointer_size = static_cast<size_t>(kRuntimePointerSize);
3991       uint32_t* current_vreg = reinterpret_cast<uint32_t*>(
3992           reinterpret_cast<uint8_t*>(cur_quick_frame) + frame_size + method_pointer_size);
3993       auto visit = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
3994         auto* ref_addr = reinterpret_cast<StackReference<mirror::Object>*>(current_vreg);
3995         mirror::Object* ref = ref_addr->AsMirrorPtr();
3996         if (ref != nullptr) {
3997           mirror::Object* new_ref = ref;
3998           visitor_(&new_ref, /* vreg= */ JavaFrameRootInfo::kNativeReferenceArgument, this);
3999           if (ref != new_ref) {
4000             ref_addr->Assign(new_ref);
4001           }
4002         }
4003       };
4004       const char* shorty = m->GetShorty();
4005       if (!m->IsStatic()) {
4006         visit();
4007         current_vreg += 1u;
4008       }
4009       for (shorty += 1u; *shorty != 0; ++shorty) {
4010         switch (*shorty) {
4011           case 'D':
4012           case 'J':
4013             current_vreg += 2u;
4014             break;
4015           case 'L':
4016             visit();
4017             FALLTHROUGH_INTENDED;
4018           default:
4019             current_vreg += 1u;
4020             break;
4021         }
4022       }
4023     } else if (!m->IsRuntimeMethod() && (!m->IsProxyMethod() || m->IsConstructor())) {
4024       // Process register map (which native, runtime and proxy methods don't have)
4025       const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
4026       DCHECK(method_header->IsOptimized());
4027       StackReference<mirror::Object>* vreg_base =
4028           reinterpret_cast<StackReference<mirror::Object>*>(cur_quick_frame);
4029       uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
4030       CodeInfo code_info = kPrecise
4031           ? CodeInfo(method_header)  // We will need dex register maps.
4032           : CodeInfo::DecodeGcMasksOnly(method_header);
4033       StackMap map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
4034       DCHECK(map.IsValid());
4035 
4036       T vreg_info(m, code_info, map, visitor_);
4037 
4038       // Visit stack entries that hold pointers.
4039       BitMemoryRegion stack_mask = code_info.GetStackMaskOf(map);
4040       for (size_t i = 0; i < stack_mask.size_in_bits(); ++i) {
4041         if (stack_mask.LoadBit(i)) {
4042           StackReference<mirror::Object>* ref_addr = vreg_base + i;
4043           mirror::Object* ref = ref_addr->AsMirrorPtr();
4044           if (ref != nullptr) {
4045             mirror::Object* new_ref = ref;
4046             vreg_info.VisitStack(&new_ref, i, this);
4047             if (ref != new_ref) {
4048               ref_addr->Assign(new_ref);
4049             }
4050           }
4051         }
4052       }
4053       // Visit callee-save registers that hold pointers.
4054       uint32_t register_mask = code_info.GetRegisterMaskOf(map);
4055       for (uint32_t i = 0; i < BitSizeOf<uint32_t>(); ++i) {
4056         if (register_mask & (1 << i)) {
4057           mirror::Object** ref_addr = reinterpret_cast<mirror::Object**>(GetGPRAddress(i));
4058           if (kIsDebugBuild && ref_addr == nullptr) {
4059             std::string thread_name;
4060             GetThread()->GetThreadName(thread_name);
4061             LOG(FATAL_WITHOUT_ABORT) << "On thread " << thread_name;
4062             DescribeStack(GetThread());
4063             LOG(FATAL) << "Found an unsaved callee-save register " << i << " (null GPRAddress) "
4064                        << "set in register_mask=" << register_mask << " at " << DescribeLocation();
4065           }
4066           if (*ref_addr != nullptr) {
4067             vreg_info.VisitRegister(ref_addr, i, this);
4068           }
4069         }
4070       }
4071     } else if (!m->IsRuntimeMethod() && m->IsProxyMethod()) {
4072       // If this is a proxy method, visit its reference arguments.
4073       DCHECK(!m->IsStatic());
4074       DCHECK(!m->IsNative());
4075       std::vector<StackReference<mirror::Object>*> ref_addrs =
4076           GetProxyReferenceArguments(cur_quick_frame);
4077       for (StackReference<mirror::Object>* ref_addr : ref_addrs) {
4078         mirror::Object* ref = ref_addr->AsMirrorPtr();
4079         if (ref != nullptr) {
4080           mirror::Object* new_ref = ref;
4081           visitor_(&new_ref, /* vreg= */ JavaFrameRootInfo::kProxyReferenceArgument, this);
4082           if (ref != new_ref) {
4083             ref_addr->Assign(new_ref);
4084           }
4085         }
4086       }
4087     }
4088   }
4089 
VisitQuickFrame()4090   void VisitQuickFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
4091     if (kPrecise) {
4092       VisitQuickFramePrecise();
4093     } else {
4094       VisitQuickFrameNonPrecise();
4095     }
4096   }
4097 
VisitQuickFrameNonPrecise()4098   void VisitQuickFrameNonPrecise() REQUIRES_SHARED(Locks::mutator_lock_) {
4099     struct UndefinedVRegInfo {
4100       UndefinedVRegInfo(ArtMethod* method ATTRIBUTE_UNUSED,
4101                         const CodeInfo& code_info ATTRIBUTE_UNUSED,
4102                         const StackMap& map ATTRIBUTE_UNUSED,
4103                         RootVisitor& _visitor)
4104           : visitor(_visitor) {
4105       }
4106 
4107       ALWAYS_INLINE
4108       void VisitStack(mirror::Object** ref,
4109                       size_t stack_index ATTRIBUTE_UNUSED,
4110                       const StackVisitor* stack_visitor)
4111           REQUIRES_SHARED(Locks::mutator_lock_) {
4112         visitor(ref, JavaFrameRootInfo::kImpreciseVreg, stack_visitor);
4113       }
4114 
4115       ALWAYS_INLINE
4116       void VisitRegister(mirror::Object** ref,
4117                          size_t register_index ATTRIBUTE_UNUSED,
4118                          const StackVisitor* stack_visitor)
4119           REQUIRES_SHARED(Locks::mutator_lock_) {
4120         visitor(ref, JavaFrameRootInfo::kImpreciseVreg, stack_visitor);
4121       }
4122 
4123       RootVisitor& visitor;
4124     };
4125     VisitQuickFrameWithVregCallback<UndefinedVRegInfo>();
4126   }
4127 
VisitQuickFramePrecise()4128   void VisitQuickFramePrecise() REQUIRES_SHARED(Locks::mutator_lock_) {
4129     struct StackMapVRegInfo {
4130       StackMapVRegInfo(ArtMethod* method,
4131                        const CodeInfo& _code_info,
4132                        const StackMap& map,
4133                        RootVisitor& _visitor)
4134           : number_of_dex_registers(method->DexInstructionData().RegistersSize()),
4135             code_info(_code_info),
4136             dex_register_map(code_info.GetDexRegisterMapOf(map)),
4137             visitor(_visitor) {
4138         DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
4139       }
4140 
4141       // TODO: If necessary, we should consider caching a reverse map instead of the linear
4142       //       lookups for each location.
4143       void FindWithType(const size_t index,
4144                         const DexRegisterLocation::Kind kind,
4145                         mirror::Object** ref,
4146                         const StackVisitor* stack_visitor)
4147           REQUIRES_SHARED(Locks::mutator_lock_) {
4148         bool found = false;
4149         for (size_t dex_reg = 0; dex_reg != number_of_dex_registers; ++dex_reg) {
4150           DexRegisterLocation location = dex_register_map[dex_reg];
4151           if (location.GetKind() == kind && static_cast<size_t>(location.GetValue()) == index) {
4152             visitor(ref, dex_reg, stack_visitor);
4153             found = true;
4154           }
4155         }
4156 
4157         if (!found) {
4158           // If nothing found, report with unknown.
4159           visitor(ref, JavaFrameRootInfo::kUnknownVreg, stack_visitor);
4160         }
4161       }
4162 
4163       void VisitStack(mirror::Object** ref, size_t stack_index, const StackVisitor* stack_visitor)
4164           REQUIRES_SHARED(Locks::mutator_lock_) {
4165         const size_t stack_offset = stack_index * kFrameSlotSize;
4166         FindWithType(stack_offset,
4167                      DexRegisterLocation::Kind::kInStack,
4168                      ref,
4169                      stack_visitor);
4170       }
4171 
4172       void VisitRegister(mirror::Object** ref,
4173                          size_t register_index,
4174                          const StackVisitor* stack_visitor)
4175           REQUIRES_SHARED(Locks::mutator_lock_) {
4176         FindWithType(register_index,
4177                      DexRegisterLocation::Kind::kInRegister,
4178                      ref,
4179                      stack_visitor);
4180       }
4181 
4182       size_t number_of_dex_registers;
4183       const CodeInfo& code_info;
4184       DexRegisterMap dex_register_map;
4185       RootVisitor& visitor;
4186     };
4187     VisitQuickFrameWithVregCallback<StackMapVRegInfo>();
4188   }
4189 
4190   // Visitor for when we visit a root.
4191   RootVisitor& visitor_;
4192 };
4193 
4194 class RootCallbackVisitor {
4195  public:
RootCallbackVisitor(RootVisitor * visitor,uint32_t tid)4196   RootCallbackVisitor(RootVisitor* visitor, uint32_t tid) : visitor_(visitor), tid_(tid) {}
4197 
operator ()(mirror::Object ** obj,size_t vreg,const StackVisitor * stack_visitor) const4198   void operator()(mirror::Object** obj, size_t vreg, const StackVisitor* stack_visitor) const
4199       REQUIRES_SHARED(Locks::mutator_lock_) {
4200     visitor_->VisitRoot(obj, JavaFrameRootInfo(tid_, stack_visitor, vreg));
4201   }
4202 
4203  private:
4204   RootVisitor* const visitor_;
4205   const uint32_t tid_;
4206 };
4207 
VisitReflectiveTargets(ReflectiveValueVisitor * visitor)4208 void Thread::VisitReflectiveTargets(ReflectiveValueVisitor* visitor) {
4209   for (BaseReflectiveHandleScope* brhs = GetTopReflectiveHandleScope();
4210        brhs != nullptr;
4211        brhs = brhs->GetLink()) {
4212     brhs->VisitTargets(visitor);
4213   }
4214 }
4215 
4216 // FIXME: clang-r433403 reports the below function exceeds frame size limit.
4217 // http://b/197647048
4218 #pragma GCC diagnostic push
4219 #pragma GCC diagnostic ignored "-Wframe-larger-than="
4220 template <bool kPrecise>
VisitRoots(RootVisitor * visitor)4221 void Thread::VisitRoots(RootVisitor* visitor) {
4222   const uint32_t thread_id = GetThreadId();
4223   visitor->VisitRootIfNonNull(&tlsPtr_.opeer, RootInfo(kRootThreadObject, thread_id));
4224   if (tlsPtr_.exception != nullptr && tlsPtr_.exception != GetDeoptimizationException()) {
4225     visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&tlsPtr_.exception),
4226                        RootInfo(kRootNativeStack, thread_id));
4227   }
4228   if (tlsPtr_.async_exception != nullptr) {
4229     visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&tlsPtr_.async_exception),
4230                        RootInfo(kRootNativeStack, thread_id));
4231   }
4232   visitor->VisitRootIfNonNull(&tlsPtr_.monitor_enter_object, RootInfo(kRootNativeStack, thread_id));
4233   tlsPtr_.jni_env->VisitJniLocalRoots(visitor, RootInfo(kRootJNILocal, thread_id));
4234   tlsPtr_.jni_env->VisitMonitorRoots(visitor, RootInfo(kRootJNIMonitor, thread_id));
4235   HandleScopeVisitRoots(visitor, thread_id);
4236   // Visit roots for deoptimization.
4237   if (tlsPtr_.stacked_shadow_frame_record != nullptr) {
4238     RootCallbackVisitor visitor_to_callback(visitor, thread_id);
4239     ReferenceMapVisitor<RootCallbackVisitor, kPrecise> mapper(this, nullptr, visitor_to_callback);
4240     for (StackedShadowFrameRecord* record = tlsPtr_.stacked_shadow_frame_record;
4241          record != nullptr;
4242          record = record->GetLink()) {
4243       for (ShadowFrame* shadow_frame = record->GetShadowFrame();
4244            shadow_frame != nullptr;
4245            shadow_frame = shadow_frame->GetLink()) {
4246         mapper.VisitShadowFrame(shadow_frame);
4247       }
4248     }
4249   }
4250   for (DeoptimizationContextRecord* record = tlsPtr_.deoptimization_context_stack;
4251        record != nullptr;
4252        record = record->GetLink()) {
4253     if (record->IsReference()) {
4254       visitor->VisitRootIfNonNull(record->GetReturnValueAsGCRoot(),
4255                                   RootInfo(kRootThreadObject, thread_id));
4256     }
4257     visitor->VisitRootIfNonNull(record->GetPendingExceptionAsGCRoot(),
4258                                 RootInfo(kRootThreadObject, thread_id));
4259   }
4260   if (tlsPtr_.frame_id_to_shadow_frame != nullptr) {
4261     RootCallbackVisitor visitor_to_callback(visitor, thread_id);
4262     ReferenceMapVisitor<RootCallbackVisitor, kPrecise> mapper(this, nullptr, visitor_to_callback);
4263     for (FrameIdToShadowFrame* record = tlsPtr_.frame_id_to_shadow_frame;
4264          record != nullptr;
4265          record = record->GetNext()) {
4266       mapper.VisitShadowFrame(record->GetShadowFrame());
4267     }
4268   }
4269   for (auto* verifier = tlsPtr_.method_verifier; verifier != nullptr; verifier = verifier->link_) {
4270     verifier->VisitRoots(visitor, RootInfo(kRootNativeStack, thread_id));
4271   }
4272   // Visit roots on this thread's stack
4273   RuntimeContextType context;
4274   RootCallbackVisitor visitor_to_callback(visitor, thread_id);
4275   ReferenceMapVisitor<RootCallbackVisitor, kPrecise> mapper(this, &context, visitor_to_callback);
4276   mapper.template WalkStack<StackVisitor::CountTransitions::kNo>(false);
4277   for (auto& entry : *GetInstrumentationStack()) {
4278     visitor->VisitRootIfNonNull(&entry.second.this_object_, RootInfo(kRootVMInternal, thread_id));
4279   }
4280 }
4281 #pragma GCC diagnostic pop
4282 
SweepCacheEntry(IsMarkedVisitor * visitor,const Instruction * inst,size_t * value)4283 static void SweepCacheEntry(IsMarkedVisitor* visitor, const Instruction* inst, size_t* value)
4284     REQUIRES_SHARED(Locks::mutator_lock_) {
4285   if (inst == nullptr) {
4286     return;
4287   }
4288   using Opcode = Instruction::Code;
4289   Opcode opcode = inst->Opcode();
4290   switch (opcode) {
4291     case Opcode::NEW_INSTANCE:
4292     case Opcode::CHECK_CAST:
4293     case Opcode::INSTANCE_OF:
4294     case Opcode::NEW_ARRAY:
4295     case Opcode::CONST_CLASS: {
4296       mirror::Class* cls = reinterpret_cast<mirror::Class*>(*value);
4297       if (cls == nullptr || cls == Runtime::GetWeakClassSentinel()) {
4298         // Entry got deleted in a previous sweep.
4299         return;
4300       }
4301       Runtime::ProcessWeakClass(
4302           reinterpret_cast<GcRoot<mirror::Class>*>(value),
4303           visitor,
4304           Runtime::GetWeakClassSentinel());
4305       return;
4306     }
4307     case Opcode::CONST_STRING:
4308     case Opcode::CONST_STRING_JUMBO: {
4309       mirror::Object* object = reinterpret_cast<mirror::Object*>(*value);
4310       mirror::Object* new_object = visitor->IsMarked(object);
4311       // We know the string is marked because it's a strongly-interned string that
4312       // is always alive (see b/117621117 for trying to make those strings weak).
4313       // The IsMarked implementation of the CMS collector returns
4314       // null for newly allocated objects, but we know those haven't moved. Therefore,
4315       // only update the entry if we get a different non-null string.
4316       if (new_object != nullptr && new_object != object) {
4317         *value = reinterpret_cast<size_t>(new_object);
4318       }
4319       return;
4320     }
4321     default:
4322       // The following opcode ranges store non-reference values.
4323       if ((Opcode::IGET <= opcode && opcode <= Opcode::SPUT_SHORT) ||
4324           (Opcode::INVOKE_VIRTUAL <= opcode && opcode <= Opcode::INVOKE_INTERFACE_RANGE)) {
4325         return;  // Nothing to do for the GC.
4326       }
4327       // New opcode is using the cache. We need to explicitly handle it in this method.
4328       DCHECK(false) << "Unhandled opcode " << inst->Opcode();
4329   }
4330 };
4331 
SweepInterpreterCaches(IsMarkedVisitor * visitor)4332 void Thread::SweepInterpreterCaches(IsMarkedVisitor* visitor) {
4333   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
4334   Runtime::Current()->GetThreadList()->ForEach([visitor](Thread* thread) {
4335     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
4336     for (InterpreterCache::Entry& entry : thread->GetInterpreterCache()->GetArray()) {
4337       SweepCacheEntry(visitor, reinterpret_cast<const Instruction*>(entry.first), &entry.second);
4338     }
4339   });
4340 }
4341 
4342 // FIXME: clang-r433403 reports the below function exceeds frame size limit.
4343 // http://b/197647048
4344 #pragma GCC diagnostic push
4345 #pragma GCC diagnostic ignored "-Wframe-larger-than="
VisitRoots(RootVisitor * visitor,VisitRootFlags flags)4346 void Thread::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
4347   if ((flags & VisitRootFlags::kVisitRootFlagPrecise) != 0) {
4348     VisitRoots</* kPrecise= */ true>(visitor);
4349   } else {
4350     VisitRoots</* kPrecise= */ false>(visitor);
4351   }
4352 }
4353 #pragma GCC diagnostic pop
4354 
4355 class VerifyRootVisitor : public SingleRootVisitor {
4356  public:
VisitRoot(mirror::Object * root,const RootInfo & info ATTRIBUTE_UNUSED)4357   void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED)
4358       override REQUIRES_SHARED(Locks::mutator_lock_) {
4359     VerifyObject(root);
4360   }
4361 };
4362 
VerifyStackImpl()4363 void Thread::VerifyStackImpl() {
4364   if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
4365     VerifyRootVisitor visitor;
4366     std::unique_ptr<Context> context(Context::Create());
4367     RootCallbackVisitor visitor_to_callback(&visitor, GetThreadId());
4368     ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context.get(), visitor_to_callback);
4369     mapper.WalkStack();
4370   }
4371 }
4372 
4373 // Set the stack end to that to be used during a stack overflow
SetStackEndForStackOverflow()4374 void Thread::SetStackEndForStackOverflow() {
4375   // During stack overflow we allow use of the full stack.
4376   if (tlsPtr_.stack_end == tlsPtr_.stack_begin) {
4377     // However, we seem to have already extended to use the full stack.
4378     LOG(ERROR) << "Need to increase kStackOverflowReservedBytes (currently "
4379                << GetStackOverflowReservedBytes(kRuntimeISA) << ")?";
4380     DumpStack(LOG_STREAM(ERROR));
4381     LOG(FATAL) << "Recursive stack overflow.";
4382   }
4383 
4384   tlsPtr_.stack_end = tlsPtr_.stack_begin;
4385 
4386   // Remove the stack overflow protection if is it set up.
4387   bool implicit_stack_check = Runtime::Current()->GetImplicitStackOverflowChecks();
4388   if (implicit_stack_check) {
4389     if (!UnprotectStack()) {
4390       LOG(ERROR) << "Unable to remove stack protection for stack overflow";
4391     }
4392   }
4393 }
4394 
SetTlab(uint8_t * start,uint8_t * end,uint8_t * limit)4395 void Thread::SetTlab(uint8_t* start, uint8_t* end, uint8_t* limit) {
4396   DCHECK_LE(start, end);
4397   DCHECK_LE(end, limit);
4398   tlsPtr_.thread_local_start = start;
4399   tlsPtr_.thread_local_pos  = tlsPtr_.thread_local_start;
4400   tlsPtr_.thread_local_end = end;
4401   tlsPtr_.thread_local_limit = limit;
4402   tlsPtr_.thread_local_objects = 0;
4403 }
4404 
ResetTlab()4405 void Thread::ResetTlab() {
4406   gc::Heap* const heap = Runtime::Current()->GetHeap();
4407   if (heap->GetHeapSampler().IsEnabled()) {
4408     // Note: We always ResetTlab before SetTlab, therefore we can do the sample
4409     // offset adjustment here.
4410     heap->AdjustSampleOffset(GetTlabPosOffset());
4411     VLOG(heap) << "JHP: ResetTlab, Tid: " << GetTid()
4412                << " adjustment = "
4413                << (tlsPtr_.thread_local_pos - tlsPtr_.thread_local_start);
4414   }
4415   SetTlab(nullptr, nullptr, nullptr);
4416 }
4417 
HasTlab() const4418 bool Thread::HasTlab() const {
4419   const bool has_tlab = tlsPtr_.thread_local_pos != nullptr;
4420   if (has_tlab) {
4421     DCHECK(tlsPtr_.thread_local_start != nullptr && tlsPtr_.thread_local_end != nullptr);
4422   } else {
4423     DCHECK(tlsPtr_.thread_local_start == nullptr && tlsPtr_.thread_local_end == nullptr);
4424   }
4425   return has_tlab;
4426 }
4427 
operator <<(std::ostream & os,const Thread & thread)4428 std::ostream& operator<<(std::ostream& os, const Thread& thread) {
4429   thread.ShortDump(os);
4430   return os;
4431 }
4432 
ProtectStack(bool fatal_on_error)4433 bool Thread::ProtectStack(bool fatal_on_error) {
4434   void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
4435   VLOG(threads) << "Protecting stack at " << pregion;
4436   if (mprotect(pregion, kStackOverflowProtectedSize, PROT_NONE) == -1) {
4437     if (fatal_on_error) {
4438       LOG(FATAL) << "Unable to create protected region in stack for implicit overflow check. "
4439           "Reason: "
4440           << strerror(errno) << " size:  " << kStackOverflowProtectedSize;
4441     }
4442     return false;
4443   }
4444   return true;
4445 }
4446 
UnprotectStack()4447 bool Thread::UnprotectStack() {
4448   void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
4449   VLOG(threads) << "Unprotecting stack at " << pregion;
4450   return mprotect(pregion, kStackOverflowProtectedSize, PROT_READ|PROT_WRITE) == 0;
4451 }
4452 
PushVerifier(verifier::MethodVerifier * verifier)4453 void Thread::PushVerifier(verifier::MethodVerifier* verifier) {
4454   verifier->link_ = tlsPtr_.method_verifier;
4455   tlsPtr_.method_verifier = verifier;
4456 }
4457 
PopVerifier(verifier::MethodVerifier * verifier)4458 void Thread::PopVerifier(verifier::MethodVerifier* verifier) {
4459   CHECK_EQ(tlsPtr_.method_verifier, verifier);
4460   tlsPtr_.method_verifier = verifier->link_;
4461 }
4462 
NumberOfHeldMutexes() const4463 size_t Thread::NumberOfHeldMutexes() const {
4464   size_t count = 0;
4465   for (BaseMutex* mu : tlsPtr_.held_mutexes) {
4466     count += mu != nullptr ? 1 : 0;
4467   }
4468   return count;
4469 }
4470 
DeoptimizeWithDeoptimizationException(JValue * result)4471 void Thread::DeoptimizeWithDeoptimizationException(JValue* result) {
4472   DCHECK_EQ(GetException(), Thread::GetDeoptimizationException());
4473   ClearException();
4474   ShadowFrame* shadow_frame =
4475       PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame);
4476   ObjPtr<mirror::Throwable> pending_exception;
4477   bool from_code = false;
4478   DeoptimizationMethodType method_type;
4479   PopDeoptimizationContext(result, &pending_exception, &from_code, &method_type);
4480   SetTopOfStack(nullptr);
4481   SetTopOfShadowStack(shadow_frame);
4482 
4483   // Restore the exception that was pending before deoptimization then interpret the
4484   // deoptimized frames.
4485   if (pending_exception != nullptr) {
4486     SetException(pending_exception);
4487   }
4488   interpreter::EnterInterpreterFromDeoptimize(this,
4489                                               shadow_frame,
4490                                               result,
4491                                               from_code,
4492                                               method_type);
4493 }
4494 
SetAsyncException(ObjPtr<mirror::Throwable> new_exception)4495 void Thread::SetAsyncException(ObjPtr<mirror::Throwable> new_exception) {
4496   CHECK(new_exception != nullptr);
4497   Runtime::Current()->SetAsyncExceptionsThrown();
4498   if (kIsDebugBuild) {
4499     // Make sure we are in a checkpoint.
4500     MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
4501     CHECK(this == Thread::Current() || GetSuspendCount() >= 1)
4502         << "It doesn't look like this was called in a checkpoint! this: "
4503         << this << " count: " << GetSuspendCount();
4504   }
4505   tlsPtr_.async_exception = new_exception.Ptr();
4506 }
4507 
ObserveAsyncException()4508 bool Thread::ObserveAsyncException() {
4509   DCHECK(this == Thread::Current());
4510   if (tlsPtr_.async_exception != nullptr) {
4511     if (tlsPtr_.exception != nullptr) {
4512       LOG(WARNING) << "Overwriting pending exception with async exception. Pending exception is: "
4513                    << tlsPtr_.exception->Dump();
4514       LOG(WARNING) << "Async exception is " << tlsPtr_.async_exception->Dump();
4515     }
4516     tlsPtr_.exception = tlsPtr_.async_exception;
4517     tlsPtr_.async_exception = nullptr;
4518     return true;
4519   } else {
4520     return IsExceptionPending();
4521   }
4522 }
4523 
SetException(ObjPtr<mirror::Throwable> new_exception)4524 void Thread::SetException(ObjPtr<mirror::Throwable> new_exception) {
4525   CHECK(new_exception != nullptr);
4526   // TODO: DCHECK(!IsExceptionPending());
4527   tlsPtr_.exception = new_exception.Ptr();
4528 }
4529 
IsAotCompiler()4530 bool Thread::IsAotCompiler() {
4531   return Runtime::Current()->IsAotCompiler();
4532 }
4533 
GetPeerFromOtherThread() const4534 mirror::Object* Thread::GetPeerFromOtherThread() const {
4535   DCHECK(tlsPtr_.jpeer == nullptr);
4536   mirror::Object* peer = tlsPtr_.opeer;
4537   if (kUseReadBarrier && Current()->GetIsGcMarking()) {
4538     // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
4539     // may have not been flipped yet and peer may be a from-space (stale) ref. So explicitly
4540     // mark/forward it here.
4541     peer = art::ReadBarrier::Mark(peer);
4542   }
4543   return peer;
4544 }
4545 
SetReadBarrierEntrypoints()4546 void Thread::SetReadBarrierEntrypoints() {
4547   // Make sure entrypoints aren't null.
4548   UpdateReadBarrierEntrypoints(&tlsPtr_.quick_entrypoints, /* is_active=*/ true);
4549 }
4550 
ClearAllInterpreterCaches()4551 void Thread::ClearAllInterpreterCaches() {
4552   static struct ClearInterpreterCacheClosure : Closure {
4553     void Run(Thread* thread) override {
4554       thread->GetInterpreterCache()->Clear(thread);
4555     }
4556   } closure;
4557   Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
4558 }
4559 
4560 
ReleaseLongJumpContextInternal()4561 void Thread::ReleaseLongJumpContextInternal() {
4562   // Each QuickExceptionHandler gets a long jump context and uses
4563   // it for doing the long jump, after finding catch blocks/doing deoptimization.
4564   // Both finding catch blocks and deoptimization can trigger another
4565   // exception such as a result of class loading. So there can be nested
4566   // cases of exception handling and multiple contexts being used.
4567   // ReleaseLongJumpContext tries to save the context in tlsPtr_.long_jump_context
4568   // for reuse so there is no need to always allocate a new one each time when
4569   // getting a context. Since we only keep one context for reuse, delete the
4570   // existing one since the passed in context is yet to be used for longjump.
4571   delete tlsPtr_.long_jump_context;
4572 }
4573 
SetNativePriority(int new_priority)4574 void Thread::SetNativePriority(int new_priority) {
4575   palette_status_t status = PaletteSchedSetPriority(GetTid(), new_priority);
4576   CHECK(status == PALETTE_STATUS_OK || status == PALETTE_STATUS_CHECK_ERRNO);
4577 }
4578 
GetNativePriority() const4579 int Thread::GetNativePriority() const {
4580   int priority = 0;
4581   palette_status_t status = PaletteSchedGetPriority(GetTid(), &priority);
4582   CHECK(status == PALETTE_STATUS_OK || status == PALETTE_STATUS_CHECK_ERRNO);
4583   return priority;
4584 }
4585 
IsSystemDaemon() const4586 bool Thread::IsSystemDaemon() const {
4587   if (GetPeer() == nullptr) {
4588     return false;
4589   }
4590   return jni::DecodeArtField(
4591       WellKnownClasses::java_lang_Thread_systemDaemon)->GetBoolean(GetPeer());
4592 }
4593 
StateAndFlagsAsHexString() const4594 std::string Thread::StateAndFlagsAsHexString() const {
4595   std::stringstream result_stream;
4596   result_stream << std::hex << GetStateAndFlags(std::memory_order_relaxed).GetValue();
4597   return result_stream.str();
4598 }
4599 
ScopedExceptionStorage(art::Thread * self)4600 ScopedExceptionStorage::ScopedExceptionStorage(art::Thread* self)
4601     : self_(self), hs_(self_), excp_(hs_.NewHandle<art::mirror::Throwable>(self_->GetException())) {
4602   self_->ClearException();
4603 }
4604 
SuppressOldException(const char * message)4605 void ScopedExceptionStorage::SuppressOldException(const char* message) {
4606   CHECK(self_->IsExceptionPending()) << *self_;
4607   ObjPtr<mirror::Throwable> old_suppressed(excp_.Get());
4608   excp_.Assign(self_->GetException());
4609   LOG(WARNING) << message << "Suppressing old exception: " << old_suppressed->Dump();
4610   self_->ClearException();
4611 }
4612 
~ScopedExceptionStorage()4613 ScopedExceptionStorage::~ScopedExceptionStorage() {
4614   CHECK(!self_->IsExceptionPending()) << *self_;
4615   if (!excp_.IsNull()) {
4616     self_->SetException(excp_.Get());
4617   }
4618 }
4619 
4620 }  // namespace art
4621 
4622 #pragma clang diagnostic pop  // -Wconversion
4623