blob: 9080418187662f2479e75a4113402ae37212ff22 [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "thread.h"
#include <pthread.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/time.h>
#if __has_feature(hwaddress_sanitizer)
#include <sanitizer/hwasan_interface.h>
#else
#define __hwasan_tag_pointer(p, t) (p)
#endif
#include <algorithm>
#include <bitset>
#include <cerrno>
#include <iostream>
#include <list>
#include <sstream>
#include "android-base/stringprintf.h"
#include "android-base/strings.h"
#include "arch/context-inl.h"
#include "arch/context.h"
#include "art_field-inl.h"
#include "art_method-inl.h"
#include "base/atomic.h"
#include "base/bit_utils.h"
#include "base/casts.h"
#include "arch/context.h"
#include "base/file_utils.h"
#include "base/memory_tool.h"
#include "base/mutex.h"
#include "base/stl_util.h"
#include "base/systrace.h"
#include "base/timing_logger.h"
#include "base/to_str.h"
#include "base/utils.h"
#include "class_linker-inl.h"
#include "class_root.h"
#include "debugger.h"
#include "dex/descriptors_names.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file_annotations.h"
#include "dex/dex_file_types.h"
#include "entrypoints/entrypoint_utils.h"
#include "entrypoints/quick/quick_alloc_entrypoints.h"
#include "gc/accounting/card_table-inl.h"
#include "gc/accounting/heap_bitmap-inl.h"
#include "gc/allocator/rosalloc.h"
#include "gc/heap.h"
#include "gc/space/space-inl.h"
#include "gc_root.h"
#include "handle_scope-inl.h"
#include "indirect_reference_table-inl.h"
#include "instrumentation.h"
#include "interpreter/interpreter.h"
#include "interpreter/mterp/mterp.h"
#include "interpreter/shadow_frame-inl.h"
#include "java_frame_root_info.h"
#include "jni/java_vm_ext.h"
#include "jni/jni_internal.h"
#include "mirror/class-alloc-inl.h"
#include "mirror/class_loader.h"
#include "mirror/object_array-alloc-inl.h"
#include "mirror/object_array-inl.h"
#include "mirror/stack_trace_element.h"
#include "monitor.h"
#include "monitor_objects_stack_visitor.h"
#include "native_stack_dump.h"
#include "nativehelper/scoped_local_ref.h"
#include "nativehelper/scoped_utf_chars.h"
#include "nth_caller_visitor.h"
#include "oat_quick_method_header.h"
#include "obj_ptr-inl.h"
#include "object_lock.h"
#include "palette/palette.h"
#include "quick/quick_method_frame_info.h"
#include "quick_exception_handler.h"
#include "read_barrier-inl.h"
#include "reflection.h"
#include "runtime-inl.h"
#include "runtime.h"
#include "runtime_callbacks.h"
#include "scoped_thread_state_change-inl.h"
#include "stack.h"
#include "stack_map.h"
#include "thread-inl.h"
#include "thread_list.h"
#include "verifier/method_verifier.h"
#include "verify_object.h"
#include "well_known_classes.h"
#if ART_USE_FUTEXES
#include "linux/futex.h"
#include "sys/syscall.h"
#ifndef SYS_futex
#define SYS_futex __NR_futex
#endif
#endif // ART_USE_FUTEXES
namespace art {
using android::base::StringAppendV;
using android::base::StringPrintf;
extern "C" NO_RETURN void artDeoptimize(Thread* self);
bool Thread::is_started_ = false;
pthread_key_t Thread::pthread_key_self_;
ConditionVariable* Thread::resume_cond_ = nullptr;
const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
bool (*Thread::is_sensitive_thread_hook_)() = nullptr;
Thread* Thread::jit_sensitive_thread_ = nullptr;
static constexpr bool kVerifyImageObjectsMarked = kIsDebugBuild;
// For implicit overflow checks we reserve an extra piece of memory at the bottom
// of the stack (lowest memory). The higher portion of the memory
// is protected against reads and the lower is available for use while
// throwing the StackOverflow exception.
constexpr size_t kStackOverflowProtectedSize = 4 * kMemoryToolStackGuardSizeScale * KB;
static const char* kThreadNameDuringStartup = "<native thread without managed peer>";
void Thread::InitCardTable() {
tlsPtr_.card_table = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin();
}
static void UnimplementedEntryPoint() {
UNIMPLEMENTED(FATAL);
}
void InitEntryPoints(JniEntryPoints* jpoints, QuickEntryPoints* qpoints);
void UpdateReadBarrierEntrypoints(QuickEntryPoints* qpoints, bool is_active);
void Thread::SetIsGcMarkingAndUpdateEntrypoints(bool is_marking) {
CHECK(kUseReadBarrier);
tls32_.is_gc_marking = is_marking;
UpdateReadBarrierEntrypoints(&tlsPtr_.quick_entrypoints, /* is_active= */ is_marking);
ResetQuickAllocEntryPointsForThread(is_marking);
}
void Thread::InitTlsEntryPoints() {
ScopedTrace trace("InitTlsEntryPoints");
// Insert a placeholder so we can easily tell if we call an unimplemented entry point.
uintptr_t* begin = reinterpret_cast<uintptr_t*>(&tlsPtr_.jni_entrypoints);
uintptr_t* end = reinterpret_cast<uintptr_t*>(
reinterpret_cast<uint8_t*>(&tlsPtr_.quick_entrypoints) + sizeof(tlsPtr_.quick_entrypoints));
for (uintptr_t* it = begin; it != end; ++it) {
*it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint);
}
InitEntryPoints(&tlsPtr_.jni_entrypoints, &tlsPtr_.quick_entrypoints);
}
void Thread::ResetQuickAllocEntryPointsForThread(bool is_marking) {
if (kUseReadBarrier && kRuntimeISA != InstructionSet::kX86_64) {
// Allocation entrypoint switching is currently only implemented for X86_64.
is_marking = true;
}
ResetQuickAllocEntryPoints(&tlsPtr_.quick_entrypoints, is_marking);
}
class DeoptimizationContextRecord {
public:
DeoptimizationContextRecord(const JValue& ret_val,
bool is_reference,
bool from_code,
ObjPtr<mirror::Throwable> pending_exception,
DeoptimizationMethodType method_type,
DeoptimizationContextRecord* link)
: ret_val_(ret_val),
is_reference_(is_reference),
from_code_(from_code),
pending_exception_(pending_exception.Ptr()),
deopt_method_type_(method_type),
link_(link) {}
JValue GetReturnValue() const { return ret_val_; }
bool IsReference() const { return is_reference_; }
bool GetFromCode() const { return from_code_; }
ObjPtr<mirror::Throwable> GetPendingException() const { return pending_exception_; }
DeoptimizationContextRecord* GetLink() const { return link_; }
mirror::Object** GetReturnValueAsGCRoot() {
DCHECK(is_reference_);
return ret_val_.GetGCRoot();
}
mirror::Object** GetPendingExceptionAsGCRoot() {
return reinterpret_cast<mirror::Object**>(&pending_exception_);
}
DeoptimizationMethodType GetDeoptimizationMethodType() const {
return deopt_method_type_;
}
private:
// The value returned by the method at the top of the stack before deoptimization.
JValue ret_val_;
// Indicates whether the returned value is a reference. If so, the GC will visit it.
const bool is_reference_;
// Whether the context was created from an explicit deoptimization in the code.
const bool from_code_;
// The exception that was pending before deoptimization (or null if there was no pending
// exception).
mirror::Throwable* pending_exception_;
// Whether the context was created for an (idempotent) runtime method.
const DeoptimizationMethodType deopt_method_type_;
// A link to the previous DeoptimizationContextRecord.
DeoptimizationContextRecord* const link_;
DISALLOW_COPY_AND_ASSIGN(DeoptimizationContextRecord);
};
class StackedShadowFrameRecord {
public:
StackedShadowFrameRecord(ShadowFrame* shadow_frame,
StackedShadowFrameType type,
StackedShadowFrameRecord* link)
: shadow_frame_(shadow_frame),
type_(type),
link_(link) {}
ShadowFrame* GetShadowFrame() const { return shadow_frame_; }
StackedShadowFrameType GetType() const { return type_; }
StackedShadowFrameRecord* GetLink() const { return link_; }
private:
ShadowFrame* const shadow_frame_;
const StackedShadowFrameType type_;
StackedShadowFrameRecord* const link_;
DISALLOW_COPY_AND_ASSIGN(StackedShadowFrameRecord);
};
void Thread::PushDeoptimizationContext(const JValue& return_value,
bool is_reference,
ObjPtr<mirror::Throwable> exception,
bool from_code,
DeoptimizationMethodType method_type) {
DeoptimizationContextRecord* record = new DeoptimizationContextRecord(
return_value,
is_reference,
from_code,
exception,
method_type,
tlsPtr_.deoptimization_context_stack);
tlsPtr_.deoptimization_context_stack = record;
}
void Thread::PopDeoptimizationContext(JValue* result,
ObjPtr<mirror::Throwable>* exception,
bool* from_code,
DeoptimizationMethodType* method_type) {
AssertHasDeoptimizationContext();
DeoptimizationContextRecord* record = tlsPtr_.deoptimization_context_stack;
tlsPtr_.deoptimization_context_stack = record->GetLink();
result->SetJ(record->GetReturnValue().GetJ());
*exception = record->GetPendingException();
*from_code = record->GetFromCode();
*method_type = record->GetDeoptimizationMethodType();
delete record;
}
void Thread::AssertHasDeoptimizationContext() {
CHECK(tlsPtr_.deoptimization_context_stack != nullptr)
<< "No deoptimization context for thread " << *this;
}
enum {
kPermitAvailable = 0, // Incrementing consumes the permit
kNoPermit = 1, // Incrementing marks as waiter waiting
kNoPermitWaiterWaiting = 2
};
void Thread::Park(bool is_absolute, int64_t time) {
DCHECK(this == Thread::Current());
#if ART_USE_FUTEXES
// Consume the permit, or mark as waiting. This cannot cause park_state to go
// outside of its valid range (0, 1, 2), because in all cases where 2 is
// assigned it is set back to 1 before returning, and this method cannot run
// concurrently with itself since it operates on the current thread.
int old_state = tls32_.park_state_.fetch_add(1, std::memory_order_relaxed);
if (old_state == kNoPermit) {
// no permit was available. block thread until later.
Runtime::Current()->GetRuntimeCallbacks()->ThreadParkStart(is_absolute, time);
bool timed_out = false;
if (!is_absolute && time == 0) {
// Thread.getState() is documented to return waiting for untimed parks.
ScopedThreadSuspension sts(this, ThreadState::kWaiting);
DCHECK_EQ(NumberOfHeldMutexes(), 0u);
int result = futex(tls32_.park_state_.Address(),
FUTEX_WAIT_PRIVATE,
/* sleep if val = */ kNoPermitWaiterWaiting,
/* timeout */ nullptr,
nullptr,
0);
// This errno check must happen before the scope is closed, to ensure that
// no destructors (such as ScopedThreadSuspension) overwrite errno.
if (result == -1) {
switch (errno) {
case EAGAIN:
FALLTHROUGH_INTENDED;
case EINTR: break; // park() is allowed to spuriously return
default: PLOG(FATAL) << "Failed to park";
}
}
} else if (time > 0) {
// Only actually suspend and futex_wait if we're going to wait for some
// positive amount of time - the kernel will reject negative times with
// EINVAL, and a zero time will just noop.
// Thread.getState() is documented to return timed wait for timed parks.
ScopedThreadSuspension sts(this, ThreadState::kTimedWaiting);
DCHECK_EQ(NumberOfHeldMutexes(), 0u);
timespec timespec;
int result = 0;
if (is_absolute) {
// Time is millis when scheduled for an absolute time
timespec.tv_nsec = (time % 1000) * 1000000;
timespec.tv_sec = time / 1000;
// This odd looking pattern is recommended by futex documentation to
// wait until an absolute deadline, with otherwise identical behavior to
// FUTEX_WAIT_PRIVATE. This also allows parkUntil() to return at the
// correct time when the system clock changes.
result = futex(tls32_.park_state_.Address(),
FUTEX_WAIT_BITSET_PRIVATE | FUTEX_CLOCK_REALTIME,
/* sleep if val = */ kNoPermitWaiterWaiting,
&timespec,
nullptr,
FUTEX_BITSET_MATCH_ANY);
} else {
// Time is nanos when scheduled for a relative time
timespec.tv_sec = time / 1000000000;
timespec.tv_nsec = time % 1000000000;
result = futex(tls32_.park_state_.Address(),
FUTEX_WAIT_PRIVATE,
/* sleep if val = */ kNoPermitWaiterWaiting,
&timespec,
nullptr,
0);
}
// This errno check must happen before the scope is closed, to ensure that
// no destructors (such as ScopedThreadSuspension) overwrite errno.
if (result == -1) {
switch (errno) {
case ETIMEDOUT:
timed_out = true;
FALLTHROUGH_INTENDED;
case EAGAIN:
case EINTR: break; // park() is allowed to spuriously return
default: PLOG(FATAL) << "Failed to park";
}
}
}
// Mark as no longer waiting, and consume permit if there is one.
tls32_.park_state_.store(kNoPermit, std::memory_order_relaxed);
// TODO: Call to signal jvmti here
Runtime::Current()->GetRuntimeCallbacks()->ThreadParkFinished(timed_out);
} else {
// the fetch_add has consumed the permit. immediately return.
DCHECK_EQ(old_state, kPermitAvailable);
}
#else
#pragma clang diagnostic push
#pragma clang diagnostic warning "-W#warnings"
#warning "LockSupport.park/unpark implemented as noops without FUTEX support."
#pragma clang diagnostic pop
UNUSED(is_absolute, time);
UNIMPLEMENTED(WARNING);
sched_yield();
#endif
}
void Thread::Unpark() {
#if ART_USE_FUTEXES
// Set permit available; will be consumed either by fetch_add (when the thread
// tries to park) or store (when the parked thread is woken up)
if (tls32_.park_state_.exchange(kPermitAvailable, std::memory_order_relaxed)
== kNoPermitWaiterWaiting) {
int result = futex(tls32_.park_state_.Address(),
FUTEX_WAKE_PRIVATE,
/* number of waiters = */ 1,
nullptr,
nullptr,
0);
if (result == -1) {
PLOG(FATAL) << "Failed to unpark";
}
}
#else
UNIMPLEMENTED(WARNING);
#endif
}
void Thread::PushStackedShadowFrame(ShadowFrame* sf, StackedShadowFrameType type) {
StackedShadowFrameRecord* record = new StackedShadowFrameRecord(
sf, type, tlsPtr_.stacked_shadow_frame_record);
tlsPtr_.stacked_shadow_frame_record = record;
}
ShadowFrame* Thread::PopStackedShadowFrame(StackedShadowFrameType type, bool must_be_present) {
StackedShadowFrameRecord* record = tlsPtr_.stacked_shadow_frame_record;
if (must_be_present) {
DCHECK(record != nullptr);
} else {
if (record == nullptr || record->GetType() != type) {
return nullptr;
}
}
tlsPtr_.stacked_shadow_frame_record = record->GetLink();
ShadowFrame* shadow_frame = record->GetShadowFrame();
delete record;
return shadow_frame;
}
class FrameIdToShadowFrame {
public:
static FrameIdToShadowFrame* Create(size_t frame_id,
ShadowFrame* shadow_frame,
FrameIdToShadowFrame* next,
size_t num_vregs) {
// Append a bool array at the end to keep track of what vregs are updated by the debugger.
uint8_t* memory = new uint8_t[sizeof(FrameIdToShadowFrame) + sizeof(bool) * num_vregs];
return new (memory) FrameIdToShadowFrame(frame_id, shadow_frame, next);
}
static void Delete(FrameIdToShadowFrame* f) {
uint8_t* memory = reinterpret_cast<uint8_t*>(f);
delete[] memory;
}
size_t GetFrameId() const { return frame_id_; }
ShadowFrame* GetShadowFrame() const { return shadow_frame_; }
FrameIdToShadowFrame* GetNext() const { return next_; }
void SetNext(FrameIdToShadowFrame* next) { next_ = next; }
bool* GetUpdatedVRegFlags() {
return updated_vreg_flags_;
}
private:
FrameIdToShadowFrame(size_t frame_id,
ShadowFrame* shadow_frame,
FrameIdToShadowFrame* next)
: frame_id_(frame_id),
shadow_frame_(shadow_frame),
next_(next) {}
const size_t frame_id_;
ShadowFrame* const shadow_frame_;
FrameIdToShadowFrame* next_;
bool updated_vreg_flags_[0];
DISALLOW_COPY_AND_ASSIGN(FrameIdToShadowFrame);
};
static FrameIdToShadowFrame* FindFrameIdToShadowFrame(FrameIdToShadowFrame* head,
size_t frame_id) {
FrameIdToShadowFrame* found = nullptr;
for (FrameIdToShadowFrame* record = head; record != nullptr; record = record->GetNext()) {
if (record->GetFrameId() == frame_id) {
if (kIsDebugBuild) {
// Sanity check we have at most one record for this frame.
CHECK(found == nullptr) << "Multiple records for the frame " << frame_id;
found = record;
} else {
return record;
}
}
}
return found;
}
ShadowFrame* Thread::FindDebuggerShadowFrame(size_t frame_id) {
FrameIdToShadowFrame* record = FindFrameIdToShadowFrame(
tlsPtr_.frame_id_to_shadow_frame, frame_id);
if (record != nullptr) {
return record->GetShadowFrame();
}
return nullptr;
}
// Must only be called when FindDebuggerShadowFrame(frame_id) returns non-nullptr.
bool* Thread::GetUpdatedVRegFlags(size_t frame_id) {
FrameIdToShadowFrame* record = FindFrameIdToShadowFrame(
tlsPtr_.frame_id_to_shadow_frame, frame_id);
CHECK(record != nullptr);
return record->GetUpdatedVRegFlags();
}
ShadowFrame* Thread::FindOrCreateDebuggerShadowFrame(size_t frame_id,
uint32_t num_vregs,
ArtMethod* method,
uint32_t dex_pc) {
ShadowFrame* shadow_frame = FindDebuggerShadowFrame(frame_id);
if (shadow_frame != nullptr) {
return shadow_frame;
}
VLOG(deopt) << "Create pre-deopted ShadowFrame for " << ArtMethod::PrettyMethod(method);
shadow_frame = ShadowFrame::CreateDeoptimizedFrame(num_vregs, nullptr, method, dex_pc);
FrameIdToShadowFrame* record = FrameIdToShadowFrame::Create(frame_id,
shadow_frame,
tlsPtr_.frame_id_to_shadow_frame,
num_vregs);
for (uint32_t i = 0; i < num_vregs; i++) {
// Do this to clear all references for root visitors.
shadow_frame->SetVRegReference(i, nullptr);
// This flag will be changed to true if the debugger modifies the value.
record->GetUpdatedVRegFlags()[i] = false;
}
tlsPtr_.frame_id_to_shadow_frame = record;
return shadow_frame;
}
TLSData* Thread::GetCustomTLS(const char* key) {
MutexLock mu(Thread::Current(), *Locks::custom_tls_lock_);
auto it = custom_tls_.find(key);
return (it != custom_tls_.end()) ? it->second.get() : nullptr;
}
void Thread::SetCustomTLS(const char* key, TLSData* data) {
// We will swap the old data (which might be nullptr) with this and then delete it outside of the
// custom_tls_lock_.
std::unique_ptr<TLSData> old_data(data);
{
MutexLock mu(Thread::Current(), *Locks::custom_tls_lock_);
custom_tls_.GetOrCreate(key, []() { return std::unique_ptr<TLSData>(); }).swap(old_data);
}
}
void Thread::RemoveDebuggerShadowFrameMapping(size_t frame_id) {
FrameIdToShadowFrame* head = tlsPtr_.frame_id_to_shadow_frame;
if (head->GetFrameId() == frame_id) {
tlsPtr_.frame_id_to_shadow_frame = head->GetNext();
FrameIdToShadowFrame::Delete(head);
return;
}
FrameIdToShadowFrame* prev = head;
for (FrameIdToShadowFrame* record = head->GetNext();
record != nullptr;
prev = record, record = record->GetNext()) {
if (record->GetFrameId() == frame_id) {
prev->SetNext(record->GetNext());
FrameIdToShadowFrame::Delete(record);
return;
}
}
LOG(FATAL) << "No shadow frame for frame " << frame_id;
UNREACHABLE();
}
void Thread::InitTid() {
tls32_.tid = ::art::GetTid();
}
void Thread::InitAfterFork() {
// One thread (us) survived the fork, but we have a new tid so we need to
// update the value stashed in this Thread*.
InitTid();
}
void* Thread::CreateCallback(void* arg) {
Thread* self = reinterpret_cast<Thread*>(arg);
Runtime* runtime = Runtime::Current();
if (runtime == nullptr) {
LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self;
return nullptr;
}
{
// TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true
// after self->Init().
MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
// Check that if we got here we cannot be shutting down (as shutdown should never have started
// while threads are being born).
CHECK(!runtime->IsShuttingDownLocked());
// Note: given that the JNIEnv is created in the parent thread, the only failure point here is
// a mess in InitStackHwm. We do not have a reasonable way to recover from that, so abort
// the runtime in such a case. In case this ever changes, we need to make sure here to
// delete the tmp_jni_env, as we own it at this point.
CHECK(self->Init(runtime->GetThreadList(), runtime->GetJavaVM(), self->tlsPtr_.tmp_jni_env));
self->tlsPtr_.tmp_jni_env = nullptr;
Runtime::Current()->EndThreadBirth();
}
{
ScopedObjectAccess soa(self);
self->InitStringEntryPoints();
// Copy peer into self, deleting global reference when done.
CHECK(self->tlsPtr_.jpeer != nullptr);
self->tlsPtr_.opeer = soa.Decode<mirror::Object>(self->tlsPtr_.jpeer).Ptr();
self->GetJniEnv()->DeleteGlobalRef(self->tlsPtr_.jpeer);
self->tlsPtr_.jpeer = nullptr;
self->SetThreadName(self->GetThreadName()->ToModifiedUtf8().c_str());
ArtField* priorityField = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority);
self->SetNativePriority(priorityField->GetInt(self->tlsPtr_.opeer));
runtime->GetRuntimeCallbacks()->ThreadStart(self);
// Unpark ourselves if the java peer was unparked before it started (see
// b/28845097#comment49 for more information)
ArtField* unparkedField = jni::DecodeArtField(
WellKnownClasses::java_lang_Thread_unparkedBeforeStart);
bool should_unpark = false;
{
// Hold the lock here, so that if another thread calls unpark before the thread starts
// we don't observe the unparkedBeforeStart field before the unparker writes to it,
// which could cause a lost unpark.
art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
should_unpark = unparkedField->GetBoolean(self->tlsPtr_.opeer) == JNI_TRUE;
}
if (should_unpark) {
self->Unpark();
}
// Invoke the 'run' method of our java.lang.Thread.
ObjPtr<mirror::Object> receiver = self->tlsPtr_.opeer;
jmethodID mid = WellKnownClasses::java_lang_Thread_run;
ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(receiver));
InvokeVirtualOrInterfaceWithJValues(soa, ref.get(), mid, nullptr);
}
// Detach and delete self.
Runtime::Current()->GetThreadList()->Unregister(self);
return nullptr;
}
Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
ObjPtr<mirror::Object> thread_peer) {
ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer);
Thread* result = reinterpret_cast64<Thread*>(f->GetLong(thread_peer));
// Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_
// to stop it from going away.
if (kIsDebugBuild) {
MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
if (result != nullptr && !result->IsSuspended()) {
Locks::thread_list_lock_->AssertHeld(soa.Self());
}
}
return result;
}
Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
jobject java_thread) {
return FromManagedThread(soa, soa.Decode<mirror::Object>(java_thread));
}
static size_t FixStackSize(size_t stack_size) {
// A stack size of zero means "use the default".
if (stack_size == 0) {
stack_size = Runtime::Current()->GetDefaultStackSize();
}
// Dalvik used the bionic pthread default stack size for native threads,
// so include that here to support apps that expect large native stacks.
stack_size += 1 * MB;
// Under sanitization, frames of the interpreter may become bigger, both for C code as
// well as the ShadowFrame. Ensure a larger minimum size. Otherwise initialization
// of all core classes cannot be done in all test circumstances.
if (kMemoryToolIsAvailable) {
stack_size = std::max(2 * MB, stack_size);
}
// It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN.
if (stack_size < PTHREAD_STACK_MIN) {
stack_size = PTHREAD_STACK_MIN;
}
if (Runtime::Current()->ExplicitStackOverflowChecks()) {
// It's likely that callers are trying to ensure they have at least a certain amount of
// stack space, so we should add our reserved space on top of what they requested, rather
// than implicitly take it away from them.
stack_size += GetStackOverflowReservedBytes(kRuntimeISA);
} else {
// If we are going to use implicit stack checks, allocate space for the protected
// region at the bottom of the stack.
stack_size += Thread::kStackOverflowImplicitCheckSize +
GetStackOverflowReservedBytes(kRuntimeISA);
}
// Some systems require the stack size to be a multiple of the system page size, so round up.
stack_size = RoundUp(stack_size, kPageSize);
return stack_size;
}
// Return the nearest page-aligned address below the current stack top.
NO_INLINE
static uint8_t* FindStackTop() {
return reinterpret_cast<uint8_t*>(
AlignDown(__builtin_frame_address(0), kPageSize));
}
// Install a protected region in the stack. This is used to trigger a SIGSEGV if a stack
// overflow is detected. It is located right below the stack_begin_.
ATTRIBUTE_NO_SANITIZE_ADDRESS
void Thread::InstallImplicitProtection() {
uint8_t* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize;
// Page containing current top of stack.
uint8_t* stack_top = FindStackTop();
// Try to directly protect the stack.
VLOG(threads) << "installing stack protected region at " << std::hex <<
static_cast<void*>(pregion) << " to " <<
static_cast<void*>(pregion + kStackOverflowProtectedSize - 1);
if (ProtectStack(/* fatal_on_error= */ false)) {
// Tell the kernel that we won't be needing these pages any more.
// NB. madvise will probably write zeroes into the memory (on linux it does).
uint32_t unwanted_size = stack_top - pregion - kPageSize;
madvise(pregion, unwanted_size, MADV_DONTNEED);
return;
}
// There is a little complexity here that deserves a special mention. On some
// architectures, the stack is created using a VM_GROWSDOWN flag
// to prevent memory being allocated when it's not needed. This flag makes the
// kernel only allocate memory for the stack by growing down in memory. Because we
// want to put an mprotected region far away from that at the stack top, we need
// to make sure the pages for the stack are mapped in before we call mprotect.
//
// The failed mprotect in UnprotectStack is an indication of a thread with VM_GROWSDOWN
// with a non-mapped stack (usually only the main thread).
//
// We map in the stack by reading every page from the stack bottom (highest address)
// to the stack top. (We then madvise this away.) This must be done by reading from the
// current stack pointer downwards.
//
// Accesses too far below the current machine register corresponding to the stack pointer (e.g.,
// ESP on x86[-32], SP on ARM) might cause a SIGSEGV (at least on x86 with newer kernels). We
// thus have to move the stack pointer. We do this portably by using a recursive function with a
// large stack frame size.
// (Defensively) first remove the protection on the protected region as we'll want to read
// and write it. Ignore errors.
UnprotectStack();
VLOG(threads) << "Need to map in stack for thread at " << std::hex <<
static_cast<void*>(pregion);
struct RecurseDownStack {
// This function has an intentionally large stack size.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wframe-larger-than="
NO_INLINE
static void Touch(uintptr_t target) {
volatile size_t zero = 0;
// Use a large local volatile array to ensure a large frame size. Do not use anything close
// to a full page for ASAN. It would be nice to ensure the frame size is at most a page, but
// there is no pragma support for this.
// Note: for ASAN we need to shrink the array a bit, as there's other overhead.
constexpr size_t kAsanMultiplier =
#ifdef ADDRESS_SANITIZER
2u;
#else
1u;
#endif
volatile char space[kPageSize - (kAsanMultiplier * 256)];
char sink ATTRIBUTE_UNUSED = space[zero]; // NOLINT
// Remove tag from the pointer. Nop in non-hwasan builds.
uintptr_t addr = reinterpret_cast<uintptr_t>(__hwasan_tag_pointer(space, 0));
if (addr >= target + kPageSize) {
Touch(target);
}
zero *= 2; // Try to avoid tail recursion.
}
#pragma GCC diagnostic pop
};
RecurseDownStack::Touch(reinterpret_cast<uintptr_t>(pregion));
VLOG(threads) << "(again) installing stack protected region at " << std::hex <<
static_cast<void*>(pregion) << " to " <<
static_cast<void*>(pregion + kStackOverflowProtectedSize - 1);
// Protect the bottom of the stack to prevent read/write to it.
ProtectStack(/* fatal_on_error= */ true);
// Tell the kernel that we won't be needing these pages any more.
// NB. madvise will probably write zeroes into the memory (on linux it does).
uint32_t unwanted_size = stack_top - pregion - kPageSize;
madvise(pregion, unwanted_size, MADV_DONTNEED);
}
void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
CHECK(java_peer != nullptr);
Thread* self = static_cast<JNIEnvExt*>(env)->GetSelf();
if (VLOG_IS_ON(threads)) {
ScopedObjectAccess soa(env);
ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name);
ObjPtr<mirror::String> java_name =
f->GetObject(soa.Decode<mirror::Object>(java_peer))->AsString();
std::string thread_name;
if (java_name != nullptr) {
thread_name = java_name->ToModifiedUtf8();
} else {
thread_name = "(Unnamed)";
}
VLOG(threads) << "Creating native thread for " << thread_name;
self->Dump(LOG_STREAM(INFO));
}
Runtime* runtime = Runtime::Current();
// Atomically start the birth of the thread ensuring the runtime isn't shutting down.
bool thread_start_during_shutdown = false;
{
MutexLock mu(self, *Locks::runtime_shutdown_lock_);
if (runtime->IsShuttingDownLocked()) {
thread_start_during_shutdown = true;
} else {
runtime->StartThreadBirth();
}
}
if (thread_start_during_shutdown) {
ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError"));
env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown");
return;
}
Thread* child_thread = new Thread(is_daemon);
// Use global JNI ref to hold peer live while child thread starts.
child_thread->tlsPtr_.jpeer = env->NewGlobalRef(java_peer);
stack_size = FixStackSize(stack_size);
// Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing
// to assign it.
env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer,
reinterpret_cast<jlong>(child_thread));
// Try to allocate a JNIEnvExt for the thread. We do this here as we might be out of memory and
// do not have a good way to report this on the child's side.
std::string error_msg;
std::unique_ptr<JNIEnvExt> child_jni_env_ext(
JNIEnvExt::Create(child_thread, Runtime::Current()->GetJavaVM(), &error_msg));
int pthread_create_result = 0;
if (child_jni_env_ext.get() != nullptr) {
pthread_t new_pthread;
pthread_attr_t attr;
child_thread->tlsPtr_.tmp_jni_env = child_jni_env_ext.get();
CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED),
"PTHREAD_CREATE_DETACHED");
CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
pthread_create_result = pthread_create(&new_pthread,
&attr,
Thread::CreateCallback,
child_thread);
CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
if (pthread_create_result == 0) {
// pthread_create started the new thread. The child is now responsible for managing the
// JNIEnvExt we created.
// Note: we can't check for tmp_jni_env == nullptr, as that would require synchronization
// between the threads.
child_jni_env_ext.release(); // NOLINT pthreads API.
return;
}
}
// Either JNIEnvExt::Create or pthread_create(3) failed, so clean up.
{
MutexLock mu(self, *Locks::runtime_shutdown_lock_);
runtime->EndThreadBirth();
}
// Manually delete the global reference since Thread::Init will not have been run.
env->DeleteGlobalRef(child_thread->tlsPtr_.jpeer);
child_thread->tlsPtr_.jpeer = nullptr;
delete child_thread;
child_thread = nullptr;
// TODO: remove from thread group?
env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0);
{
std::string msg(child_jni_env_ext.get() == nullptr ?
StringPrintf("Could not allocate JNI Env: %s", error_msg.c_str()) :
StringPrintf("pthread_create (%s stack) failed: %s",
PrettySize(stack_size).c_str(), strerror(pthread_create_result)));
ScopedObjectAccess soa(env);
soa.Self()->ThrowOutOfMemoryError(msg.c_str());
}
}
bool Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm, JNIEnvExt* jni_env_ext) {
// This function does all the initialization that must be run by the native thread it applies to.
// (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so
// we can handshake with the corresponding native thread when it's ready.) Check this native
// thread hasn't been through here already...
CHECK(Thread::Current() == nullptr);
// Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this
// avoids pthread_self_ ever being invalid when discovered from Thread::Current().
tlsPtr_.pthread_self = pthread_self();
CHECK(is_started_);
ScopedTrace trace("Thread::Init");
SetUpAlternateSignalStack();
if (!InitStackHwm()) {
return false;
}
InitCpu();
InitTlsEntryPoints();
RemoveSuspendTrigger();
InitCardTable();
InitTid();
{
ScopedTrace trace2("InitInterpreterTls");
interpreter::InitInterpreterTls(this);
}
#ifdef ART_TARGET_ANDROID
__get_tls()[TLS_SLOT_ART_THREAD_SELF] = this;
#else
CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self");
#endif
DCHECK_EQ(Thread::Current(), this);
tls32_.thin_lock_thread_id = thread_list->AllocThreadId(this);
if (jni_env_ext != nullptr) {
DCHECK_EQ(jni_env_ext->GetVm(), java_vm);
DCHECK_EQ(jni_env_ext->GetSelf(), this);
tlsPtr_.jni_env = jni_env_ext;
} else {
std::string error_msg;
tlsPtr_.jni_env = JNIEnvExt::Create(this, java_vm, &error_msg);
if (tlsPtr_.jni_env == nullptr) {
LOG(ERROR) << "Failed to create JNIEnvExt: " << error_msg;
return false;
}
}
ScopedTrace trace3("ThreadList::Register");
thread_list->Register(this);
return true;
}
template <typename PeerAction>
Thread* Thread::Attach(const char* thread_name, bool as_daemon, PeerAction peer_action) {
Runtime* runtime = Runtime::Current();
ScopedTrace trace("Thread::Attach");
if (runtime == nullptr) {
LOG(ERROR) << "Thread attaching to non-existent runtime: " <<
((thread_name != nullptr) ? thread_name : "(Unnamed)");
return nullptr;
}
Thread* self;
{
ScopedTrace trace2("Thread birth");
MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
if (runtime->IsShuttingDownLocked()) {
LOG(WARNING) << "Thread attaching while runtime is shutting down: " <<
((thread_name != nullptr) ? thread_name : "(Unnamed)");
return nullptr;
} else {
Runtime::Current()->StartThreadBirth();
self = new Thread(as_daemon);
bool init_success = self->Init(runtime->GetThreadList(), runtime->GetJavaVM());
Runtime::Current()->EndThreadBirth();
if (!init_success) {
delete self;
return nullptr;
}
}
}
self->InitStringEntryPoints();
CHECK_NE(self->GetState(), kRunnable);
self->SetState(kNative);
// Run the action that is acting on the peer.
if (!peer_action(self)) {
runtime->GetThreadList()->Unregister(self);
// Unregister deletes self, no need to do this here.
return nullptr;
}
if (VLOG_IS_ON(threads)) {
if (thread_name != nullptr) {
VLOG(threads) << "Attaching thread " << thread_name;
} else {
VLOG(threads) << "Attaching unnamed thread.";
}
ScopedObjectAccess soa(self);
self->Dump(LOG_STREAM(INFO));
}
{
ScopedObjectAccess soa(self);
runtime->GetRuntimeCallbacks()->ThreadStart(self);
}
return self;
}
Thread* Thread::Attach(const char* thread_name,
bool as_daemon,
jobject thread_group,
bool create_peer) {
auto create_peer_action = [&](Thread* self) {
// If we're the main thread, ClassLinker won't be created until after we're attached,
// so that thread needs a two-stage attach. Regular threads don't need this hack.
// In the compiler, all threads need this hack, because no-one's going to be getting
// a native peer!
if (create_peer) {
self->CreatePeer(thread_name, as_daemon, thread_group);
if (self->IsExceptionPending()) {
// We cannot keep the exception around, as we're deleting self. Try to be helpful and log
// it.
{
ScopedObjectAccess soa(self);
LOG(ERROR) << "Exception creating thread peer:";
LOG(ERROR) << self->GetException()->Dump();
self->ClearException();
}
return false;
}
} else {
// These aren't necessary, but they improve diagnostics for unit tests & command-line tools.
if (thread_name != nullptr) {
self->tlsPtr_.name->assign(thread_name);
::art::SetThreadName(thread_name);
} else if (self->GetJniEnv()->IsCheckJniEnabled()) {
LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
}
}
return true;
};
return Attach(thread_name, as_daemon, create_peer_action);
}
Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_peer) {
auto set_peer_action = [&](Thread* self) {
// Install the given peer.
{
DCHECK(self == Thread::Current());
ScopedObjectAccess soa(self);
self->tlsPtr_.opeer = soa.Decode<mirror::Object>(thread_peer).Ptr();
}
self->GetJniEnv()->SetLongField(thread_peer,
WellKnownClasses::java_lang_Thread_nativePeer,
reinterpret_cast64<jlong>(self));
return true;
};
return Attach(thread_name, as_daemon, set_peer_action);
}
void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) {
Runtime* runtime = Runtime::Current();
CHECK(runtime->IsStarted());
JNIEnv* env = tlsPtr_.jni_env;
if (thread_group == nullptr) {
thread_group = runtime->GetMainThreadGroup();
}
ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
// Add missing null check in case of OOM b/18297817
if (name != nullptr && thread_name.get() == nullptr) {
CHECK(IsExceptionPending());
return;
}
jint thread_priority = GetNativePriority();
jboolean thread_is_daemon = as_daemon;
ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
if (peer.get() == nullptr) {
CHECK(IsExceptionPending());
return;
}
{
ScopedObjectAccess soa(this);
tlsPtr_.opeer = soa.Decode<mirror::Object>(peer.get()).Ptr();
}
env->CallNonvirtualVoidMethod(peer.get(),
WellKnownClasses::java_lang_Thread,
WellKnownClasses::java_lang_Thread_init,
thread_group, thread_name.get(), thread_priority, thread_is_daemon);
if (IsExceptionPending()) {
return;
}
Thread* self = this;
DCHECK_EQ(self, Thread::Current());
env->SetLongField(peer.get(),
WellKnownClasses::java_lang_Thread_nativePeer,
reinterpret_cast64<jlong>(self));
ScopedObjectAccess soa(self);
StackHandleScope<1> hs(self);
MutableHandle<mirror::String> peer_thread_name(hs.NewHandle(GetThreadName()));
if (peer_thread_name == nullptr) {
// The Thread constructor should have set the Thread.name to a
// non-null value. However, because we can run without code
// available (in the compiler, in tests), we manually assign the
// fields the constructor should have set.
if (runtime->IsActiveTransaction()) {
InitPeer<true>(soa,
tlsPtr_.opeer,
thread_is_daemon,
thread_group,
thread_name.get(),
thread_priority);
} else {
InitPeer<false>(soa,
tlsPtr_.opeer,
thread_is_daemon,
thread_group,
thread_name.get(),
thread_priority);
}
peer_thread_name.Assign(GetThreadName());
}
// 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null.
if (peer_thread_name != nullptr) {
SetThreadName(peer_thread_name->ToModifiedUtf8().c_str());
}
}
jobject Thread::CreateCompileTimePeer(JNIEnv* env,
const char* name,
bool as_daemon,
jobject thread_group) {
Runtime* runtime = Runtime::Current();
CHECK(!runtime->IsStarted());
if (thread_group == nullptr) {
thread_group = runtime->GetMainThreadGroup();
}
ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
// Add missing null check in case of OOM b/18297817
if (name != nullptr && thread_name.get() == nullptr) {
CHECK(Thread::Current()->IsExceptionPending());
return nullptr;
}
jint thread_priority = kNormThreadPriority; // Always normalize to NORM priority.
jboolean thread_is_daemon = as_daemon;
ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
if (peer.get() == nullptr) {
CHECK(Thread::Current()->IsExceptionPending());
return nullptr;
}
// We cannot call Thread.init, as it will recursively ask for currentThread.
// The Thread constructor should have set the Thread.name to a
// non-null value. However, because we can run without code
// available (in the compiler, in tests), we manually assign the
// fields the constructor should have set.
ScopedObjectAccessUnchecked soa(Thread::Current());
if (runtime->IsActiveTransaction()) {
InitPeer<true>(soa,
soa.Decode<mirror::Object>(peer.get()),
thread_is_daemon,
thread_group,
thread_name.get(),
thread_priority);
} else {
InitPeer<false>(soa,
soa.Decode<mirror::Object>(peer.get()),
thread_is_daemon,
thread_group,
thread_name.get(),
thread_priority);
}
return peer.release();
}
template<bool kTransactionActive>
void Thread::InitPeer(ScopedObjectAccessAlreadyRunnable& soa,
ObjPtr<mirror::Object> peer,
jboolean thread_is_daemon,
jobject thread_group,
jobject thread_name,
jint thread_priority) {
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_daemon)->
SetBoolean<kTransactionActive>(peer, thread_is_daemon);
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)->
SetObject<kTransactionActive>(peer, soa.Decode<mirror::Object>(thread_group));
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name)->
SetObject<kTransactionActive>(peer, soa.Decode<mirror::Object>(thread_name));
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority)->
SetInt<kTransactionActive>(peer, thread_priority);
}
void Thread::SetThreadName(const char* name) {
tlsPtr_.name->assign(name);
::art::SetThreadName(name);
Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM"));
}
static void GetThreadStack(pthread_t thread,
void** stack_base,
size_t* stack_size,
size_t* guard_size) {
#if defined(__APPLE__)
*stack_size = pthread_get_stacksize_np(thread);
void* stack_addr = pthread_get_stackaddr_np(thread);
// Check whether stack_addr is the base or end of the stack.
// (On Mac OS 10.7, it's the end.)
int stack_variable;
if (stack_addr > &stack_variable) {
*stack_base = reinterpret_cast<uint8_t*>(stack_addr) - *stack_size;
} else {
*stack_base = stack_addr;
}
// This is wrong, but there doesn't seem to be a way to get the actual value on the Mac.
pthread_attr_t attributes;
CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__);
CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
#else
pthread_attr_t attributes;
CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__);
CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__);
CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
#if defined(__GLIBC__)
// If we're the main thread, check whether we were run with an unlimited stack. In that case,
// glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection
// will be broken because we'll die long before we get close to 2GB.
bool is_main_thread = (::art::GetTid() == getpid());
if (is_main_thread) {
rlimit stack_limit;
if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed";
}
if (stack_limit.rlim_cur == RLIM_INFINITY) {
size_t old_stack_size = *stack_size;
// Use the kernel default limit as our size, and adjust the base to match.
*stack_size = 8 * MB;
*stack_base = reinterpret_cast<uint8_t*>(*stack_base) + (old_stack_size - *stack_size);
VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")"
<< " to " << PrettySize(*stack_size)
<< " with base " << *stack_base;
}
}
#endif
#endif
}
bool Thread::InitStackHwm() {
ScopedTrace trace("InitStackHwm");
void* read_stack_base;
size_t read_stack_size;
size_t read_guard_size;
GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size, &read_guard_size);
tlsPtr_.stack_begin = reinterpret_cast<uint8_t*>(read_stack_base);
tlsPtr_.stack_size = read_stack_size;
// The minimum stack size we can cope with is the overflow reserved bytes (typically
// 8K) + the protected region size (4K) + another page (4K). Typically this will
// be 8+4+4 = 16K. The thread won't be able to do much with this stack even the GC takes
// between 8K and 12K.
uint32_t min_stack = GetStackOverflowReservedBytes(kRuntimeISA) + kStackOverflowProtectedSize
+ 4 * KB;
if (read_stack_size <= min_stack) {
// Note, as we know the stack is small, avoid operations that could use a lot of stack.
LogHelper::LogLineLowStack(__PRETTY_FUNCTION__,
__LINE__,
::android::base::ERROR,
"Attempt to attach a thread with a too-small stack");
return false;
}
// This is included in the SIGQUIT output, but it's useful here for thread debugging.
VLOG(threads) << StringPrintf("Native stack is at %p (%s with %s guard)",
read_stack_base,
PrettySize(read_stack_size).c_str(),
PrettySize(read_guard_size).c_str());
// Set stack_end_ to the bottom of the stack saving space of stack overflows
Runtime* runtime = Runtime::Current();
bool implicit_stack_check = !runtime->ExplicitStackOverflowChecks() && !runtime->IsAotCompiler();
ResetDefaultStackEnd();
// Install the protected region if we are doing implicit overflow checks.
if (implicit_stack_check) {
// The thread might have protected region at the bottom. We need
// to install our own region so we need to move the limits
// of the stack to make room for it.
tlsPtr_.stack_begin += read_guard_size + kStackOverflowProtectedSize;
tlsPtr_.stack_end += read_guard_size + kStackOverflowProtectedSize;
tlsPtr_.stack_size -= read_guard_size;
InstallImplicitProtection();
}
// Sanity check.
CHECK_GT(FindStackTop(), reinterpret_cast<void*>(tlsPtr_.stack_end));
return true;
}
void Thread::ShortDump(std::ostream& os) const {
os << "Thread[";
if (GetThreadId() != 0) {
// If we're in kStarting, we won't have a thin lock id or tid yet.
os << GetThreadId()
<< ",tid=" << GetTid() << ',';
}
os << GetState()
<< ",Thread*=" << this
<< ",peer=" << tlsPtr_.opeer
<< ",\"" << (tlsPtr_.name != nullptr ? *tlsPtr_.name : "null") << "\""
<< "]";
}
void Thread::Dump(std::ostream& os, bool dump_native_stack, BacktraceMap* backtrace_map,
bool force_dump_stack) const {
DumpState(os);
DumpStack(os, dump_native_stack, backtrace_map, force_dump_stack);
}
mirror::String* Thread::GetThreadName() const {
ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name);
if (tlsPtr_.opeer == nullptr) {
return nullptr;
}
ObjPtr<mirror::Object> name = f->GetObject(tlsPtr_.opeer);
return name == nullptr ? nullptr : name->AsString();
}
void Thread::GetThreadName(std::string& name) const {
name.assign(*tlsPtr_.name);
}
uint64_t Thread::GetCpuMicroTime() const {
#if defined(__linux__)
clockid_t cpu_clock_id;
pthread_getcpuclockid(tlsPtr_.pthread_self, &cpu_clock_id);
timespec now;
clock_gettime(cpu_clock_id, &now);
return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000);
#else // __APPLE__
UNIMPLEMENTED(WARNING);
return -1;
#endif
}
// Attempt to rectify locks so that we dump thread list with required locks before exiting.
static void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
LOG(ERROR) << *thread << " suspend count already zero.";
Locks::thread_suspend_count_lock_->Unlock(self);
if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Locks::mutator_lock_->SharedTryLock(self);
if (!Locks::mutator_lock_->IsSharedHeld(self)) {
LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
}
}
if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
Locks::thread_list_lock_->TryLock(self);
if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
}
}
std::ostringstream ss;
Runtime::Current()->GetThreadList()->Dump(ss);
LOG(FATAL) << ss.str();
}
bool Thread::ModifySuspendCountInternal(Thread* self,
int delta,
AtomicInteger* suspend_barrier,
SuspendReason reason) {
if (kIsDebugBuild) {
DCHECK(delta == -1 || delta == +1 || delta == -tls32_.debug_suspend_count)
<< reason << " " << delta << " " << tls32_.debug_suspend_count << " " << this;
DCHECK_GE(tls32_.suspend_count, tls32_.debug_suspend_count) << this;
Locks::thread_suspend_count_lock_->AssertHeld(self);
if (this != self && !IsSuspended()) {
Locks::thread_list_lock_->AssertHeld(self);
}
}
// User code suspensions need to be checked more closely since they originate from code outside of
// the runtime's control.
if (UNLIKELY(reason == SuspendReason::kForUserCode)) {
Locks::user_code_suspension_lock_->AssertHeld(self);
if (UNLIKELY(delta + tls32_.user_code_suspend_count < 0)) {
LOG(ERROR) << "attempting to modify suspend count in an illegal way.";
return false;
}
}
if (UNLIKELY(delta < 0 && tls32_.suspend_count <= 0)) {
UnsafeLogFatalForSuspendCount(self, this);
return false;
}
if (kUseReadBarrier && delta > 0 && this != self && tlsPtr_.flip_function != nullptr) {
// Force retry of a suspend request if it's in the middle of a thread flip to avoid a
// deadlock. b/31683379.
return false;
}
uint16_t flags = kSuspendRequest;
if (delta > 0 && suspend_barrier != nullptr) {
uint32_t available_barrier = kMaxSuspendBarriers;
for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
if (tlsPtr_.active_suspend_barriers[i] == nullptr) {
available_barrier = i;
break;
}
}
if (available_barrier == kMaxSuspendBarriers) {
// No barrier spaces available, we can't add another.
return false;
}
tlsPtr_.active_suspend_barriers[available_barrier] = suspend_barrier;
flags |= kActiveSuspendBarrier;
}
tls32_.suspend_count += delta;
switch (reason) {
case SuspendReason::kForDebugger:
tls32_.debug_suspend_count += delta;
break;
case SuspendReason::kForUserCode:
tls32_.user_code_suspend_count += delta;
break;
case SuspendReason::kInternal:
break;
}
if (tls32_.suspend_count == 0) {
AtomicClearFlag(kSuspendRequest);
} else {
// Two bits might be set simultaneously.
tls32_.state_and_flags.as_atomic_int.fetch_or(flags, std::memory_order_seq_cst);
TriggerSuspend();
}
return true;
}
bool Thread::PassActiveSuspendBarriers(Thread* self) {
// Grab the suspend_count lock and copy the current set of
// barriers. Then clear the list and the flag. The ModifySuspendCount
// function requires the lock so we prevent a race between setting
// the kActiveSuspendBarrier flag and clearing it.
AtomicInteger* pass_barriers[kMaxSuspendBarriers];
{
MutexLock mu(self, *Locks::thread_suspend_count_lock_);
if (!ReadFlag(kActiveSuspendBarrier)) {
// quick exit test: the barriers have already been claimed - this is
// possible as there may be a race to claim and it doesn't matter
// who wins.
// All of the callers of this function (except the SuspendAllInternal)
// will first test the kActiveSuspendBarrier flag without lock. Here
// double-check whether the barrier has been passed with the
// suspend_count lock.
return false;
}
for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
pass_barriers[i] = tlsPtr_.active_suspend_barriers[i];
tlsPtr_.active_suspend_barriers[i] = nullptr;
}
AtomicClearFlag(kActiveSuspendBarrier);
}
uint32_t barrier_count = 0;
for (uint32_t i = 0; i < kMaxSuspendBarriers; i++) {
AtomicInteger* pending_threads = pass_barriers[i];
if (pending_threads != nullptr) {
bool done = false;
do {
int32_t cur_val = pending_threads->load(std::memory_order_relaxed);
CHECK_GT(cur_val, 0) << "Unexpected value for PassActiveSuspendBarriers(): " << cur_val;
// Reduce value by 1.
done = pending_threads->CompareAndSetWeakRelaxed(cur_val, cur_val - 1);
#if ART_USE_FUTEXES
if (done && (cur_val - 1) == 0) { // Weak CAS may fail spuriously.
futex(pending_threads->Address(), FUTEX_WAKE_PRIVATE, -1, nullptr, nullptr, 0);
}
#endif
} while (!done);
++barrier_count;
}
}
CHECK_GT(barrier_count, 0U);
return true;
}
void Thread::ClearSuspendBarrier(AtomicInteger* target) {
CHECK(ReadFlag(kActiveSuspendBarrier));
bool clear_flag = true;
for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
AtomicInteger* ptr = tlsPtr_.active_suspend_barriers[i];
if (ptr == target) {
tlsPtr_.active_suspend_barriers[i] = nullptr;
} else if (ptr != nullptr) {
clear_flag = false;
}
}
if (LIKELY(clear_flag)) {
AtomicClearFlag(kActiveSuspendBarrier);
}
}
void Thread::RunCheckpointFunction() {
// Grab the suspend_count lock, get the next checkpoint and update all the checkpoint fields. If
// there are no more checkpoints we will also clear the kCheckpointRequest flag.
Closure* checkpoint;
{
MutexLock mu(this, *Locks::thread_suspend_count_lock_);
checkpoint = tlsPtr_.checkpoint_function;
if (!checkpoint_overflow_.empty()) {
// Overflow list not empty, copy the first one out and continue.
tlsPtr_.checkpoint_function = checkpoint_overflow_.front();
checkpoint_overflow_.pop_front();
} else {
// No overflow checkpoints. Clear the kCheckpointRequest flag
tlsPtr_.checkpoint_function = nullptr;
AtomicClearFlag(kCheckpointRequest);
}
}
// Outside the lock, run the checkpoint function.
ScopedTrace trace("Run checkpoint function");
CHECK(checkpoint != nullptr) << "Checkpoint flag set without pending checkpoint";
checkpoint->Run(this);
}
void Thread::RunEmptyCheckpoint() {
DCHECK_EQ(Thread::Current(), this);
AtomicClearFlag(kEmptyCheckpointRequest);
Runtime::Current()->GetThreadList()->EmptyCheckpointBarrier()->Pass(this);
}
bool Thread::RequestCheckpoint(Closure* function) {
union StateAndFlags old_state_and_flags;
old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
if (old_state_and_flags.as_struct.state != kRunnable) {
return false; // Fail, thread is suspended and so can't run a checkpoint.
}
// We must be runnable to request a checkpoint.
DCHECK_EQ(old_state_and_flags.as_struct.state, kRunnable);
union StateAndFlags new_state_and_flags;
new_state_and_flags.as_int = old_state_and_flags.as_int;
new_state_and_flags.as_struct.flags |= kCheckpointRequest;
bool success = tls32_.state_and_flags.as_atomic_int.CompareAndSetStrongSequentiallyConsistent(
old_state_and_flags.as_int, new_state_and_flags.as_int);
if (success) {
// Succeeded setting checkpoint flag, now insert the actual checkpoint.
if (tlsPtr_.checkpoint_function == nullptr) {
tlsPtr_.checkpoint_function = function;
} else {
checkpoint_overflow_.push_back(function);
}
CHECK_EQ(ReadFlag(kCheckpointRequest), true);
TriggerSuspend();
}
return success;
}
bool Thread::RequestEmptyCheckpoint() {
union StateAndFlags old_state_and_flags;
old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
if (old_state_and_flags.as_struct.state != kRunnable) {
// If it's not runnable, we don't need to do anything because it won't be in the middle of a
// heap access (eg. the read barrier).
return false;
}
// We must be runnable to request a checkpoint.
DCHECK_EQ(old_state_and_flags.as_struct.state, kRunnable);
union StateAndFlags new_state_and_flags;
new_state_and_flags.as_int = old_state_and_flags.as_int;
new_state_and_flags.as_struct.flags |= kEmptyCheckpointRequest;
bool success = tls32_.state_and_flags.as_atomic_int.CompareAndSetStrongSequentiallyConsistent(
old_state_and_flags.as_int, new_state_and_flags.as_int);
if (success) {
TriggerSuspend();
}
return success;
}
class BarrierClosure : public Closure {
public:
explicit BarrierClosure(Closure* wrapped) : wrapped_(wrapped), barrier_(0) {}
void Run(Thread* self) override {
wrapped_->Run(self);
barrier_.Pass(self);
}
void Wait(Thread* self, ThreadState suspend_state) {
if (suspend_state != ThreadState::kRunnable) {
barrier_.Increment<Barrier::kDisallowHoldingLocks>(self, 1);
} else {
barrier_.Increment<Barrier::kAllowHoldingLocks>(self, 1);
}
}
private:
Closure* wrapped_;
Barrier barrier_;
};
// RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
bool Thread::RequestSynchronousCheckpoint(Closure* function, ThreadState suspend_state) {
Thread* self = Thread::Current();
if (this == Thread::Current()) {
Locks::thread_list_lock_->AssertExclusiveHeld(self);
// Unlock the tll before running so that the state is the same regardless of thread.
Locks::thread_list_lock_->ExclusiveUnlock(self);
// Asked to run on this thread. Just run.
function->Run(this);
return true;
}
// The current thread is not this thread.
if (GetState() == ThreadState::kTerminated) {
Locks::thread_list_lock_->ExclusiveUnlock(self);
return false;
}
struct ScopedThreadListLockUnlock {
explicit ScopedThreadListLockUnlock(Thread* self_in) RELEASE(*Locks::thread_list_lock_)
: self_thread(self_in) {
Locks::thread_list_lock_->AssertHeld(self_thread);
Locks::thread_list_lock_->Unlock(self_thread);
}
~ScopedThreadListLockUnlock() ACQUIRE(*Locks::thread_list_lock_) {
Locks::thread_list_lock_->AssertNotHeld(self_thread);
Locks::thread_list_lock_->Lock(self_thread);
}
Thread* self_thread;
};
for (;;) {
Locks::thread_list_lock_->AssertExclusiveHeld(self);
// If this thread is runnable, try to schedule a checkpoint. Do some gymnastics to not hold the
// suspend-count lock for too long.
if (GetState() == ThreadState::kRunnable) {
BarrierClosure barrier_closure(function);
bool installed = false;
{
MutexLock mu(self, *Locks::thread_suspend_count_lock_);
installed = RequestCheckpoint(&barrier_closure);
}
if (installed) {
// Relinquish the thread-list lock. We should not wait holding any locks. We cannot
// reacquire it since we don't know if 'this' hasn't been deleted yet.
Locks::thread_list_lock_->ExclusiveUnlock(self);
ScopedThreadStateChange sts(self, suspend_state);
barrier_closure.Wait(self, suspend_state);
return true;
}
// Fall-through.
}
// This thread is not runnable, make sure we stay suspended, then run the checkpoint.
// Note: ModifySuspendCountInternal also expects the thread_list_lock to be held in
// certain situations.
{
MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
if (!ModifySuspendCount(self, +1, nullptr, SuspendReason::kInternal)) {
// Just retry the loop.
sched_yield();
continue;
}
}
{
// Release for the wait. The suspension will keep us from being deleted. Reacquire after so
// that we can call ModifySuspendCount without racing against ThreadList::Unregister.
ScopedThreadListLockUnlock stllu(self);
{
ScopedThreadStateChange sts(self, suspend_state);
while (GetState() == ThreadState::kRunnable) {
// We became runnable again. Wait till the suspend triggered in ModifySuspendCount
// moves us to suspended.
sched_yield();
}
}
function->Run(this);
}
{
MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
DCHECK_NE(GetState(), ThreadState::kRunnable);
bool updated = ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
DCHECK(updated);
}
{
// Imitate ResumeAll, the thread may be waiting on Thread::resume_cond_ since we raised its
// suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Thread::resume_cond_->Broadcast(self);
}
// Release the thread_list_lock_ to be consistent with the barrier-closure path.
Locks::thread_list_lock_->ExclusiveUnlock(self);
return true; // We're done, break out of the loop.
}
}
Closure* Thread::GetFlipFunction() {
Atomic<Closure*>* atomic_func = reinterpret_cast<Atomic<Closure*>*>(&tlsPtr_.flip_function);
Closure* func;
do {
func = atomic_func->load(std::memory_order_relaxed);
if (func == nullptr) {
return nullptr;
}
} while (!atomic_func->CompareAndSetWeakSequentiallyConsistent(func, nullptr));
DCHECK(func != nullptr);
return func;
}
void Thread::SetFlipFunction(Closure* function) {
CHECK(function != nullptr);
Atomic<Closure*>* atomic_func = reinterpret_cast<Atomic<Closure*>*>(&tlsPtr_.flip_function);
atomic_func->store(function, std::memory_order_seq_cst);
}
void Thread::FullSuspendCheck() {
ScopedTrace trace(__FUNCTION__);
VLOG(threads) << this << " self-suspending";
// Make thread appear suspended to other threads, release mutator_lock_.
// Transition to suspended and back to runnable, re-acquire share on mutator_lock_.
ScopedThreadSuspension(this, kSuspended); // NOLINT
VLOG(threads) << this << " self-reviving";
}
static std::string GetSchedulerGroupName(pid_t tid) {
// /proc/<pid>/cgroup looks like this:
// 2:devices:/
// 1:cpuacct,cpu:/
// We want the third field from the line whose second field contains the "cpu" token.
std::string cgroup_file;
if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) {
return "";
}
std::vector<std::string> cgroup_lines;
Split(cgroup_file, '\n', &cgroup_lines);
for (size_t i = 0; i < cgroup_lines.size(); ++i) {
std::vector<std::string> cgroup_fields;
Split(cgroup_lines[i], ':', &cgroup_fields);
std::vector<std::string> cgroups;
Split(cgroup_fields[1], ',', &cgroups);
for (size_t j = 0; j < cgroups.size(); ++j) {
if (cgroups[j] == "cpu") {
return cgroup_fields[2].substr(1); // Skip the leading slash.
}
}
}
return "";
}
void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) {
std::string group_name;
int priority;
bool is_daemon = false;
Thread* self = Thread::Current();
// If flip_function is not null, it means we have run a checkpoint
// before the thread wakes up to execute the flip function and the
// thread roots haven't been forwarded. So the following access to
// the roots (opeer or methods in the frames) would be bad. Run it
// here. TODO: clean up.
if (thread != nullptr) {
ScopedObjectAccessUnchecked soa(self);
Thread* this_thread = const_cast<Thread*>(thread);
Closure* flip_func = this_thread->GetFlipFunction();
if (flip_func != nullptr) {
flip_func->Run(this_thread);
}
}
// Don't do this if we are aborting since the GC may have all the threads suspended. This will
// cause ScopedObjectAccessUnchecked to deadlock.
if (gAborting == 0 && self != nullptr && thread != nullptr && thread->tlsPtr_.opeer != nullptr) {
ScopedObjectAccessUnchecked soa(self);
priority = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_priority)
->GetInt(thread->tlsPtr_.opeer);
is_daemon = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_daemon)
->GetBoolean(thread->tlsPtr_.opeer);
ObjPtr<mirror::Object> thread_group =
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)
->GetObject(thread->tlsPtr_.opeer);
if (thread_group != nullptr) {
ArtField* group_name_field =
jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_name);
ObjPtr<mirror::String> group_name_string =
group_name_field->GetObject(thread_group)->AsString();
group_name = (group_name_string != nullptr) ? group_name_string->ToModifiedUtf8() : "<null>";
}
} else {
priority = GetNativePriority();
}
std::string scheduler_group_name(GetSchedulerGroupName(tid));
if (scheduler_group_name.empty()) {
scheduler_group_name = "default";
}
if (thread != nullptr) {
os << '"' << *thread->tlsPtr_.name << '"';
if (is_daemon) {
os << " daemon";
}
os << " prio=" << priority
<< " tid=" << thread->GetThreadId()
<< " " << thread->GetState();
if (thread->IsStillStarting()) {
os << " (still starting up)";
}
os << "\n";
} else {
os << '"' << ::art::GetThreadName(tid) << '"'
<< " prio=" << priority
<< " (not attached)\n";
}
if (thread != nullptr) {
MutexLock mu(self, *Locks::thread_suspend_count_lock_);
os << " | group=\"" << group_name << "\""
<< " sCount=" << thread->tls32_.suspend_count
<< " dsCount=" << thread->tls32_.debug_suspend_count
<< " flags=" << thread->tls32_.state_and_flags.as_struct.flags
<< " obj=" << reinterpret_cast<void*>(thread->tlsPtr_.opeer)
<< " self=" << reinterpret_cast<const void*>(thread) << "\n";
}
os << " | sysTid=" << tid
<< " nice=" << getpriority(PRIO_PROCESS, tid)
<< " cgrp=" << scheduler_group_name;
if (thread != nullptr) {
int policy;
sched_param sp;
#if !defined(__APPLE__)
// b/36445592 Don't use pthread_getschedparam since pthread may have exited.
policy = sched_getscheduler(tid);
if (policy == -1) {
PLOG(WARNING) << "sched_getscheduler(" << tid << ")";
}
int sched_getparam_result = sched_getparam(tid, &sp);
if (sched_getparam_result == -1) {
PLOG(WARNING) << "sched_getparam(" << tid << ", &sp)";
sp.sched_priority = -1;
}
#else
CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp),
__FUNCTION__);
#endif
os << " sched=" << policy << "/" << sp.sched_priority
<< " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self);
}
os << "\n";
// Grab the scheduler stats for this thread.
std::string scheduler_stats;
if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)
&& !scheduler_stats.empty()) {
scheduler_stats = android::base::Trim(scheduler_stats); // Lose the trailing '\n'.
} else {
scheduler_stats = "0 0 0";
}
char native_thread_state = '?';
int utime = 0;
int stime = 0;
int task_cpu = 0;
GetTaskStats(tid, &native_thread_state, &utime, &stime, &task_cpu);
os << " | state=" << native_thread_state
<< " schedstat=( " << scheduler_stats << " )"
<< " utm=" << utime
<< " stm=" << stime
<< " core=" << task_cpu
<< " HZ=" << sysconf(_SC_CLK_TCK) << "\n";
if (thread != nullptr) {
os << " | stack=" << reinterpret_cast<void*>(thread->tlsPtr_.stack_begin) << "-"
<< reinterpret_cast<void*>(thread->tlsPtr_.stack_end) << " stackSize="
<< PrettySize(thread->tlsPtr_.stack_size) << "\n";
// Dump the held mutexes.
os << " | held mutexes=";
for (size_t i = 0; i < kLockLevelCount; ++i) {
if (i != kMonitorLock) {
BaseMutex* mutex = thread->GetHeldMutex(static_cast<LockLevel>(i));
if (mutex != nullptr) {
os << " \"" << mutex->GetName() << "\"";
if (mutex->IsReaderWriterMutex()) {
ReaderWriterMutex* rw_mutex = down_cast<ReaderWriterMutex*>(mutex);
if (rw_mutex->GetExclusiveOwnerTid() == tid) {
os << "(exclusive held)";
} else {
os << "(shared held)";
}
}
}
}
}
os << "\n";
}
}
void Thread::DumpState(std::ostream& os) const {
Thread::DumpState(os, this, GetTid());
}
struct StackDumpVisitor : public MonitorObjectsStackVisitor {
StackDumpVisitor(std::ostream& os_in,
Thread* thread_in,
Context* context,
bool can_allocate,
bool check_suspended = true,
bool dump_locks = true)
REQUIRES_SHARED(Locks::mutator_lock_)
: MonitorObjectsStackVisitor(thread_in,
context,
check_suspended,
can_allocate && dump_locks),
os(os_in),
last_method(nullptr),
last_line_number(0),
repetition_count(0) {}
virtual ~StackDumpVisitor() {
if (frame_count == 0) {
os << " (no managed stack frames)\n";
}
}
static constexpr size_t kMaxRepetition = 3u;
VisitMethodResult StartMethod(ArtMethod* m, size_t frame_nr ATTRIBUTE_UNUSED)
override
REQUIRES_SHARED(Locks::mutator_lock_) {
m = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
ObjPtr<mirror::DexCache> dex_cache = m->GetDexCache();
int line_number = -1;
if (dex_cache != nullptr) { // be tolerant of bad input
const DexFile* dex_file = dex_cache->GetDexFile();
line_number = annotations::GetLineNumFromPC(dex_file, m, GetDexPc(false));
}
if (line_number == last_line_number && last_method == m) {
++repetition_count;
} else {
if (repetition_count >= kMaxRepetition) {
os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n";
}
repetition_count = 0;
last_line_number = line_number;
last_method = m;
}
if (repetition_count >= kMaxRepetition) {
// Skip visiting=printing anything.
return VisitMethodResult::kSkipMethod;
}
os << " at " << m->PrettyMethod(false);
if (m->IsNative()) {
os << "(Native method)";
} else {
const char* source_file(m->GetDeclaringClassSourceFile());
os << "(" << (source_file != nullptr ? source_file : "unavailable")
<< ":" << line_number << ")";
}
os << "\n";
// Go and visit locks.
return VisitMethodResult::kContinueMethod;
}
VisitMethodResult EndMethod(ArtMethod* m ATTRIBUTE_UNUSED) override {
return VisitMethodResult::kContinueMethod;
}
void VisitWaitingObject(mirror::Object* obj, ThreadState state ATTRIBUTE_UNUSED)
override
REQUIRES_SHARED(Locks::mutator_lock_) {
PrintObject(obj, " - waiting on ", ThreadList::kInvalidThreadId);
}
void VisitSleepingObject(mirror::Object* obj)
override
REQUIRES_SHARED(Locks::mutator_lock_) {
PrintObject(obj, " - sleeping on ", ThreadList::kInvalidThreadId);
}
void VisitBlockedOnObject(mirror::Object* obj,
ThreadState state,
uint32_t owner_tid)
override
REQUIRES_SHARED(Locks::mutator_lock_) {
const char* msg;
switch (state) {
case kBlocked:
msg = " - waiting to lock ";
break;
case kWaitingForLockInflation:
msg = " - waiting for lock inflation of ";
break;
default:
LOG(FATAL) << "Unreachable";
UNREACHABLE();
}
PrintObject(obj, msg, owner_tid);
}
void VisitLockedObject(mirror::Object* obj)
override
REQUIRES_SHARED(Locks::mutator_lock_) {
PrintObject(obj, " - locked ", ThreadList::kInvalidThreadId);
}
void PrintObject(mirror::Object* obj,
const char* msg,
uint32_t owner_tid) REQUIRES_SHARED(Locks::mutator_lock_) {
if (obj == nullptr) {
os << msg << "an unknown object";
} else {
if ((obj->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
// Getting the identity hashcode here would result in lock inflation and suspension of the
// current thread, which isn't safe if this is the only runnable thread.
os << msg << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
reinterpret_cast<intptr_t>(obj),
obj->PrettyTypeOf().c_str());
} else {
// - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
// Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
// suspension and move pretty_object.
const std::string pretty_type(obj->PrettyTypeOf());
os << msg << StringPrintf("<0x%08x> (a %s)", obj->IdentityHashCode(), pretty_type.c_str());
}
}
if (owner_tid != ThreadList::kInvalidThreadId) {
os << " held by thread " << owner_tid;
}
os << "\n";
}
std::ostream& os;
ArtMethod* last_method;
int last_line_number;
size_t repetition_count;
};
static bool ShouldShowNativeStack(const Thread* thread)
REQUIRES_SHARED(Locks::mutator_lock_) {
ThreadState state = thread->GetState();
// In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting.
if (state > kWaiting && state < kStarting) {
return true;
}
// In an Object.wait variant or Thread.sleep? That's not interesting.
if (state == kTimedWaiting || state == kSleeping || state == kWaiting) {
return false;
}
// Threads with no managed stack frames should be shown.
if (!thread->HasManagedStack()) {
return true;
}
// In some other native method? That's interesting.
// We don't just check kNative because native methods will be in state kSuspended if they're
// calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the
// thread-startup states if it's early enough in their life cycle (http://b/7432159).
ArtMethod* current_method = thread->GetCurrentMethod(nullptr);
return current_method != nullptr && current_method->IsNative();
}
void Thread::DumpJavaStack(std::ostream& os, bool check_suspended, bool dump_locks) const {
// If flip_function is not null, it means we have run a checkpoint
// before the thread wakes up to execute the flip function and the
// thread roots haven't been forwarded. So the following access to
// the roots (locks or methods in the frames) would be bad. Run it
// here. TODO: clean up.
{
Thread* this_thread = const_cast<Thread*>(this);
Closure* flip_func = this_thread->GetFlipFunction();
if (flip_func != nullptr) {
flip_func->Run(this_thread);
}
}
// Dumping the Java stack involves the verifier for locks. The verifier operates under the
// assumption that there is no exception pending on entry. Thus, stash any pending exception.
// Thread::Current() instead of this in case a thread is dumping the stack of another suspended
// thread.
StackHandleScope<1> scope(Thread::Current());
Handle<mirror::Throwable> exc;
bool have_exception = false;
if (IsExceptionPending()) {
exc = scope.NewHandle(GetException());
const_cast<Thread*>(this)->ClearException();
have_exception = true;
}
std::unique_ptr<Context> context(Context::Create());
StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(),
!tls32_.throwing_OutOfMemoryError, check_suspended, dump_locks);
dumper.WalkStack();
if (have_exception) {
const_cast<Thread*>(this)->SetException(exc.Get());
}
}
void Thread::DumpStack(std::ostream& os,
bool dump_native_stack,
BacktraceMap* backtrace_map,
bool force_dump_stack) const {
// TODO: we call this code when dying but may not have suspended the thread ourself. The
// IsSuspended check is therefore racy with the use for dumping (normally we inhibit
// the race with the thread_suspend_count_lock_).
bool dump_for_abort = (gAborting > 0);
bool safe_to_dump = (this == Thread::Current() || IsSuspended());
if (!kIsDebugBuild) {
// We always want to dump the stack for an abort, however, there is no point dumping another
// thread's stack in debug builds where we'll hit the not suspended check in the stack walk.
safe_to_dump = (safe_to_dump || dump_for_abort);
}
if (safe_to_dump || force_dump_stack) {
// If we're currently in native code, dump that stack before dumping the managed stack.
if (dump_native_stack && (dump_for_abort || force_dump_stack || ShouldShowNativeStack(this))) {
DumpKernelStack(os, GetTid(), " kernel: ", false);
ArtMethod* method =
GetCurrentMethod(nullptr,
/*check_suspended=*/ !force_dump_stack,
/*abort_on_error=*/ !(dump_for_abort || force_dump_stack));
DumpNativeStack(os, GetTid(), backtrace_map, " native: ", method);
}
DumpJavaStack(os,
/*check_suspended=*/ !force_dump_stack,
/*dump_locks=*/ !force_dump_stack);
} else {
os << "Not able to dump stack of thread that isn't suspended";
}
}
void Thread::ThreadExitCallback(void* arg) {
Thread* self = reinterpret_cast<Thread*>(arg);
if (self->tls32_.thread_exit_check_count == 0) {
LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's "
"going to use a pthread_key_create destructor?): " << *self;
CHECK(is_started_);
#ifdef ART_TARGET_ANDROID
__get_tls()[TLS_SLOT_ART_THREAD_SELF] = self;
#else
CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self");
#endif
self->tls32_.thread_exit_check_count = 1;
} else {
LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self;
}
}
void Thread::Startup() {
CHECK(!is_started_);
is_started_ = true;
{
// MutexLock to keep annotalysis happy.
//
// Note we use null for the thread because Thread::Current can
// return garbage since (is_started_ == true) and
// Thread::pthread_key_self_ is not yet initialized.
// This was seen on glibc.
MutexLock mu(nullptr, *Locks::thread_suspend_count_lock_);
resume_cond_ = new ConditionVariable("Thread resumption condition variable",
*Locks::thread_suspend_count_lock_);
}
// Allocate a TLS slot.
CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback),
"self key");
// Double-check the TLS slot allocation.
if (pthread_getspecific(pthread_key_self_) != nullptr) {
LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr";
}
}
void Thread::FinishStartup() {
Runtime* runtime = Runtime::Current();
CHECK(runtime->IsStarted());
// Finish attaching the main thread.
ScopedObjectAccess soa(Thread::Current());
soa.Self()->CreatePeer("main", false, runtime->GetMainThreadGroup());
soa.Self()->AssertNoPendingException();
runtime->RunRootClinits(soa.Self());
// The thread counts as started from now on. We need to add it to the ThreadGroup. For regular
// threads, this is done in Thread.start() on the Java side.
soa.Self()->NotifyThreadGroup(soa, runtime->GetMainThreadGroup());
soa.Self()->AssertNoPendingException();
}
void Thread::Shutdown() {
CHECK(is_started_);
is_started_ = false;
CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key");
MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
if (resume_cond_ != nullptr) {
delete resume_cond_;
resume_cond_ = nullptr;
}
}
void Thread::NotifyThreadGroup(ScopedObjectAccessAlreadyRunnable& soa, jobject thread_group) {
ScopedLocalRef<jobject> thread_jobject(
soa.Env(), soa.Env()->AddLocalReference<jobject>(Thread::Current()->GetPeer()));
ScopedLocalRef<jobject> thread_group_jobject_scoped(
soa.Env(), nullptr);
jobject thread_group_jobject = thread_group;
if (thread_group == nullptr || kIsDebugBuild) {
// There is always a group set. Retrieve it.
thread_group_jobject_scoped.reset(
soa.Env()->GetObjectField(thread_jobject.get(),
WellKnownClasses::java_lang_Thread_group));
thread_group_jobject = thread_group_jobject_scoped.get();
if (kIsDebugBuild && thread_group != nullptr) {
CHECK(soa.Env()->IsSameObject(thread_group, thread_group_jobject));
}
}
soa.Env()->CallNonvirtualVoidMethod(thread_group_jobject,
WellKnownClasses::java_lang_ThreadGroup,
WellKnownClasses::java_lang_ThreadGroup_add,
thread_jobject.get());
}
Thread::Thread(bool daemon)
: tls32_(daemon),
wait_monitor_(nullptr),
is_runtime_thread_(false) {
wait_mutex_ = new Mutex("a thread wait mutex", LockLevel::kThreadWaitLock);
wait_cond_ = new ConditionVariable("a thread wait condition variable", *wait_mutex_);
tlsPtr_.instrumentation_stack = new std::deque<instrumentation::InstrumentationStackFrame>;
tlsPtr_.name = new std::string(kThreadNameDuringStartup);
static_assert((sizeof(Thread) % 4) == 0U,
"art::Thread has a size which is not a multiple of 4.");
tls32_.state_and_flags.as_struct.flags = 0;
tls32_.state_and_flags.as_struct.state = kNative;
tls32_.interrupted.store(false, std::memory_order_relaxed);
// Initialize with no permit; if the java Thread was unparked before being
// started, it will unpark itself before calling into java code.
tls32_.park_state_.store(kNoPermit, std::memory_order_relaxed);
memset(&tlsPtr_.held_mutexes[0], 0, sizeof(tlsPtr_.held_mutexes));
std::fill(tlsPtr_.rosalloc_runs,
tlsPtr_.rosalloc_runs + kNumRosAllocThreadLocalSizeBracketsInThread,
gc::allocator::RosAlloc::GetDedicatedFullRun());
tlsPtr_.checkpoint_function = nullptr;
for (uint32_t i = 0; i < kMaxSuspendBarriers; ++i) {
tlsPtr_.active_suspend_barriers[i] = nullptr;
}
tlsPtr_.flip_function = nullptr;
tlsPtr_.thread_local_mark_stack = nullptr;
tls32_.is_transitioning_to_runnable = false;
tls32_.use_mterp = false;
}
void Thread::NotifyInTheadList() {
tls32_.use_mterp = interpreter::CanUseMterp();
}
bool Thread::CanLoadClasses() const {
return !IsRuntimeThread() || !Runtime::Current()->IsJavaDebuggable();
}
bool Thread::IsStillStarting() const {
// You might think you can check whether the state is kStarting, but for much of thread startup,
// the thread is in kNative; it might also be in kVmWait.
// You might think you can check whether the peer is null, but the peer is actually created and
// assigned fairly early on, and needs to be.
// It turns out that the last thing to change is the thread name; that's a good proxy for "has
// this thread _ever_ entered kRunnable".
return (tlsPtr_.jpeer == nullptr && tlsPtr_.opeer == nullptr) ||
(*tlsPtr_.name == kThreadNameDuringStartup);
}
void Thread::AssertPendingException() const {
CHECK(IsExceptionPending()) << "Pending exception expected.";
}
void Thread::AssertPendingOOMException() const {
AssertPendingException();
auto* e = GetException();
CHECK_EQ(e->GetClass(), DecodeJObject(WellKnownClasses::java_lang_OutOfMemoryError)->AsClass())
<< e->Dump();
}
void Thread::AssertNoPendingException() const {
if (UNLIKELY(IsExceptionPending())) {
ScopedObjectAccess soa(Thread::Current());
LOG(FATAL) << "No pending exception expected: " << GetException()->Dump();
}
}
void Thread::AssertNoPendingExceptionForNewException(const char* msg) const {
if (UNLIKELY(IsExceptionPending())) {
ScopedObjectAccess soa(Thread::Current());
LOG(FATAL) << "Throwing new exception '" << msg << "' with unexpected pending exception: "
<< GetException()->Dump();
}
}
class MonitorExitVisitor : public SingleRootVisitor {
public:
explicit MonitorExitVisitor(Thread* self) : self_(self) { }
// NO_THREAD_SAFETY_ANALYSIS due to MonitorExit.
void VisitRoot(mirror::Object* entered_monitor, const RootInfo& info ATTRIBUTE_UNUSED)
override NO_THREAD_SAFETY_ANALYSIS {
if (self_->HoldsLock(entered_monitor)) {
LOG(WARNING) << "Calling MonitorExit on object "
<< entered_monitor << " (" << entered_monitor->PrettyTypeOf() << ")"
<< " left locked by native thread "
<< *Thread::Current() << " which is detaching";
entered_monitor->MonitorExit(self_);
}
}
private:
Thread* const self_;
};
void Thread::Destroy() {
Thread* self = this;
DCHECK_EQ(self, Thread::Current());
if (tlsPtr_.jni_env != nullptr) {
{
ScopedObjectAccess soa(self);
MonitorExitVisitor visitor(self);
// On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
tlsPtr_.jni_env->monitors_.VisitRoots(&visitor, RootInfo(kRootVMInternal));
}
// Release locally held global references which releasing may require the mutator lock.
if (tlsPtr_.jpeer != nullptr) {
// If pthread_create fails we don't have a jni env here.
tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.jpeer);
tlsPtr_.jpeer = nullptr;
}
if (tlsPtr_.class_loader_override != nullptr) {
tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.class_loader_override);
tlsPtr_.class_loader_override = nullptr;
}
}
if (tlsPtr_.opeer != nullptr) {
ScopedObjectAccess soa(self);
// We may need to call user-supplied managed code, do this before final clean-up.
HandleUncaughtExceptions(soa);
RemoveFromThreadGroup(soa);
Runtime* runtime = Runtime::Current();
if (runtime != nullptr) {
runtime->GetRuntimeCallbacks()->ThreadDeath(self);
}
// this.nativePeer = 0;
if (Runtime::Current()->IsActiveTransaction()) {
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer)
->SetLong<true>(tlsPtr_.opeer, 0);
} else {
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_nativePeer)
->SetLong<false>(tlsPtr_.opeer, 0);
}
// Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone
// who is waiting.
ObjPtr<mirror::Object> lock =
jni::DecodeArtField(WellKnownClasses::java_lang_Thread_lock)->GetObject(tlsPtr_.opeer);
// (This conditional is only needed for tests, where Thread.lock won't have been set.)
if (lock != nullptr) {
StackHandleScope<1> hs(self);
Handle<mirror::Object> h_obj(hs.NewHandle(lock));
ObjectLock<mirror::Object> locker(self, h_obj);
locker.NotifyAll();
}
tlsPtr_.opeer = nullptr;
}
{
ScopedObjectAccess soa(self);
Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this);
if (kUseReadBarrier) {
Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->RevokeThreadLocalMarkStack(this);
}
}
}
Thread::~Thread() {
CHECK(tlsPtr_.class_loader_override == nullptr);
CHECK(tlsPtr_.jpeer == nullptr);
CHECK(tlsPtr_.opeer == nullptr);
bool initialized = (tlsPtr_.jni_env != nullptr); // Did Thread::Init run?
if (initialized) {
delete tlsPtr_.jni_env;
tlsPtr_.jni_env = nullptr;
}
CHECK_NE(GetState(), kRunnable);
CHECK(!ReadFlag(kCheckpointRequest));
CHECK(!ReadFlag(kEmptyCheckpointRequest));
CHECK(tlsPtr_.checkpoint_function == nullptr);
CHECK_EQ(checkpoint_overflow_.size(), 0u);
CHECK(tlsPtr_.flip_function == nullptr);
CHECK_EQ(tls32_.is_transitioning_to_runnable, false);
// Make sure we processed all deoptimization requests.
CHECK(tlsPtr_.deoptimization_context_stack == nullptr) << "Missed deoptimization";
CHECK(tlsPtr_.frame_id_to_shadow_frame == nullptr) <<
"Not all deoptimized frames have been consumed by the debugger.";
// We may be deleting a still born thread.
SetStateUnsafe(kTerminated);
delete wait_cond_;
delete wait_mutex_;
if (tlsPtr_.long_jump_context != nullptr) {
delete tlsPtr_.long_jump_context;
}
if (initialized) {
CleanupCpu();
}
if (tlsPtr_.single_step_control != nullptr) {
delete tlsPtr_.single_step_control;
}
delete tlsPtr_.instrumentation_stack;
delete tlsPtr_.name;
delete tlsPtr_.deps_or_stack_trace_sample.stack_trace_sample;
Runtime::Current()->GetHeap()->AssertThreadLocalBuffersAreRevoked(this);
TearDownAlternateSignalStack();
}
void Thread::HandleUncaughtExceptions(ScopedObjectAccessAlreadyRunnable& soa) {
if (!IsExceptionPending()) {
return;
}
ScopedLocalRef<jobject> peer(tlsPtr_.jni_env, soa.AddLocalReference<jobject>(tlsPtr_.opeer));
ScopedThreadStateChange tsc(this, kNative);
// Get and clear the exception.
ScopedLocalRef<jthrowable> exception(tlsPtr_.jni_env, tlsPtr_.jni_env->ExceptionOccurred());
tlsPtr_.jni_env->ExceptionClear();
// Call the Thread instance's dispatchUncaughtException(Throwable)
tlsPtr_.jni_env->CallVoidMethod(peer.get(),
WellKnownClasses::java_lang_Thread_dispatchUncaughtException,
exception.get());
// If the dispatchUncaughtException threw, clear that exception too.
tlsPtr_.jni_env->ExceptionClear();
}
void Thread::RemoveFromThreadGroup(ScopedObjectAccessAlreadyRunnable& soa) {
// this.group.removeThread(this);
// group can be null if we're in the compiler or a test.
ObjPtr<mirror::Object> ogroup = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group)
->GetObject(tlsPtr_.opeer);
if (ogroup != nullptr) {
ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup));
ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(tlsPtr_.opeer));
ScopedThreadStateChange tsc(soa.Self(), kNative);
tlsPtr_.jni_env->CallVoidMethod(group.get(),
WellKnownClasses::java_lang_ThreadGroup_removeThread,
peer.get());
}
}
bool Thread::HandleScopeContains(jobject obj) const {
StackReference<mirror::Object>* hs_entry =
reinterpret_cast<StackReference<mirror::Object>*>(obj);
for (BaseHandleScope* cur = tlsPtr_.top_handle_scope; cur!= nullptr; cur = cur->GetLink()) {
if (cur->Contains(hs_entry)) {
return true;
}
}
// JNI code invoked from portable code uses shadow frames rather than the handle scope.
return tlsPtr_.managed_stack.ShadowFramesContain(hs_entry);
}
void Thread::HandleScopeVisitRoots(RootVisitor* visitor, pid_t thread_id) {
BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
visitor, RootInfo(kRootNativeStack, thread_id));
for (BaseHandleScope* cur = tlsPtr_.top_handle_scope; cur; cur = cur->GetLink()) {
cur->VisitRoots(buffered_visitor);
}
}
ObjPtr<mirror::Object> Thread::DecodeJObject(jobject obj) const {
if (obj == nullptr) {
return nullptr;
}
IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);