1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_THREAD_INL_H_
18 #define ART_RUNTIME_THREAD_INL_H_
19
20 #include "thread.h"
21
22 #include "arch/instruction_set.h"
23 #include "base/aborting.h"
24 #include "base/casts.h"
25 #include "base/mutex-inl.h"
26 #include "base/time_utils.h"
27 #include "jni/jni_env_ext.h"
28 #include "managed_stack-inl.h"
29 #include "obj_ptr.h"
30 #include "suspend_reason.h"
31 #include "thread-current-inl.h"
32 #include "thread_pool.h"
33
34 namespace art {
35
36 // Quickly access the current thread from a JNIEnv.
ThreadForEnv(JNIEnv * env)37 static inline Thread* ThreadForEnv(JNIEnv* env) {
38 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
39 return full_env->GetSelf();
40 }
41
AllowThreadSuspension()42 inline void Thread::AllowThreadSuspension() {
43 CheckSuspend();
44 // Invalidate the current thread's object pointers (ObjPtr) to catch possible moving GC bugs due
45 // to missing handles.
46 PoisonObjectPointers();
47 }
48
CheckSuspend(bool implicit)49 inline void Thread::CheckSuspend(bool implicit) {
50 DCHECK_EQ(Thread::Current(), this);
51 while (true) {
52 StateAndFlags state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
53 if (LIKELY(!state_and_flags.IsAnyOfFlagsSet(SuspendOrCheckpointRequestFlags()))) {
54 break;
55 } else if (state_and_flags.IsFlagSet(ThreadFlag::kCheckpointRequest)) {
56 RunCheckpointFunction();
57 } else if (state_and_flags.IsFlagSet(ThreadFlag::kSuspendRequest)) {
58 FullSuspendCheck(implicit);
59 implicit = false; // We do not need to `MadviseAwayAlternateSignalStack()` anymore.
60 } else {
61 DCHECK(state_and_flags.IsFlagSet(ThreadFlag::kEmptyCheckpointRequest));
62 RunEmptyCheckpoint();
63 }
64 }
65 if (implicit) {
66 // For implicit suspend check we want to `madvise()` away
67 // the alternate signal stack to avoid wasting memory.
68 MadviseAwayAlternateSignalStack();
69 }
70 }
71
CheckEmptyCheckpointFromWeakRefAccess(BaseMutex * cond_var_mutex)72 inline void Thread::CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex) {
73 Thread* self = Thread::Current();
74 DCHECK_EQ(self, this);
75 for (;;) {
76 if (ReadFlag(ThreadFlag::kEmptyCheckpointRequest)) {
77 RunEmptyCheckpoint();
78 // Check we hold only an expected mutex when accessing weak ref.
79 if (kIsDebugBuild) {
80 for (int i = kLockLevelCount - 1; i >= 0; --i) {
81 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
82 if (held_mutex != nullptr &&
83 held_mutex != GetMutatorLock() &&
84 held_mutex != cond_var_mutex) {
85 CHECK(Locks::IsExpectedOnWeakRefAccess(held_mutex))
86 << "Holding unexpected mutex " << held_mutex->GetName()
87 << " when accessing weak ref";
88 }
89 }
90 }
91 } else {
92 break;
93 }
94 }
95 }
96
CheckEmptyCheckpointFromMutex()97 inline void Thread::CheckEmptyCheckpointFromMutex() {
98 DCHECK_EQ(Thread::Current(), this);
99 for (;;) {
100 if (ReadFlag(ThreadFlag::kEmptyCheckpointRequest)) {
101 RunEmptyCheckpoint();
102 } else {
103 break;
104 }
105 }
106 }
107
SetState(ThreadState new_state)108 inline ThreadState Thread::SetState(ThreadState new_state) {
109 // Should only be used to change between suspended states.
110 // Cannot use this code to change into or from Runnable as changing to Runnable should
111 // fail if the `ThreadFlag::kSuspendRequest` is set and changing from Runnable might
112 // miss passing an active suspend barrier.
113 DCHECK_NE(new_state, ThreadState::kRunnable);
114 if (kIsDebugBuild && this != Thread::Current()) {
115 std::string name;
116 GetThreadName(name);
117 LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()="
118 << Thread::Current() << ") changing state to " << new_state;
119 }
120
121 while (true) {
122 StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
123 CHECK_NE(old_state_and_flags.GetState(), ThreadState::kRunnable)
124 << new_state << " " << *this << " " << *Thread::Current();
125 StateAndFlags new_state_and_flags = old_state_and_flags.WithState(new_state);
126 bool done =
127 tls32_.state_and_flags.CompareAndSetWeakRelaxed(old_state_and_flags.GetValue(),
128 new_state_and_flags.GetValue());
129 if (done) {
130 return static_cast<ThreadState>(old_state_and_flags.GetState());
131 }
132 }
133 }
134
IsThreadSuspensionAllowable()135 inline bool Thread::IsThreadSuspensionAllowable() const {
136 if (tls32_.no_thread_suspension != 0) {
137 return false;
138 }
139 for (int i = kLockLevelCount - 1; i >= 0; --i) {
140 if (i != kMutatorLock &&
141 i != kUserCodeSuspensionLock &&
142 GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
143 return false;
144 }
145 }
146 // Thread autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
147 // have the mutex meaning we need to do this hack.
148 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
149 return tls32_.user_code_suspend_count != 0;
150 };
151 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
152 return false;
153 }
154 return true;
155 }
156
AssertThreadSuspensionIsAllowable(bool check_locks)157 inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
158 if (kIsDebugBuild) {
159 if (gAborting == 0) {
160 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
161 }
162 if (check_locks) {
163 bool bad_mutexes_held = false;
164 for (int i = kLockLevelCount - 1; i >= 0; --i) {
165 // We expect no locks except the mutator lock. User code suspension lock is OK as long as
166 // we aren't going to be held suspended due to SuspendReason::kForUserCode.
167 if (i != kMutatorLock && i != kUserCodeSuspensionLock) {
168 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
169 if (held_mutex != nullptr) {
170 LOG(ERROR) << "holding \"" << held_mutex->GetName()
171 << "\" at point where thread suspension is expected";
172 bad_mutexes_held = true;
173 }
174 }
175 }
176 // Make sure that if we hold the user_code_suspension_lock_ we aren't suspending due to
177 // user_code_suspend_count which would prevent the thread from ever waking up. Thread
178 // autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
179 // have the mutex meaning we need to do this hack.
180 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
181 return tls32_.user_code_suspend_count != 0;
182 };
183 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
184 LOG(ERROR) << "suspending due to user-code while holding \""
185 << Locks::user_code_suspension_lock_->GetName() << "\"! Thread would never "
186 << "wake up.";
187 bad_mutexes_held = true;
188 }
189 if (gAborting == 0) {
190 CHECK(!bad_mutexes_held);
191 }
192 }
193 }
194 }
195
TransitionToSuspendedAndRunCheckpoints(ThreadState new_state)196 inline void Thread::TransitionToSuspendedAndRunCheckpoints(ThreadState new_state) {
197 DCHECK_NE(new_state, ThreadState::kRunnable);
198 while (true) {
199 StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
200 DCHECK_EQ(old_state_and_flags.GetState(), ThreadState::kRunnable);
201 if (UNLIKELY(old_state_and_flags.IsFlagSet(ThreadFlag::kCheckpointRequest))) {
202 RunCheckpointFunction();
203 continue;
204 }
205 if (UNLIKELY(old_state_and_flags.IsFlagSet(ThreadFlag::kEmptyCheckpointRequest))) {
206 RunEmptyCheckpoint();
207 continue;
208 }
209 // Change the state but keep the current flags (kCheckpointRequest is clear).
210 DCHECK(!old_state_and_flags.IsFlagSet(ThreadFlag::kCheckpointRequest));
211 DCHECK(!old_state_and_flags.IsFlagSet(ThreadFlag::kEmptyCheckpointRequest));
212 StateAndFlags new_state_and_flags = old_state_and_flags.WithState(new_state);
213
214 // CAS the value, ensuring that prior memory operations are visible to any thread
215 // that observes that we are suspended.
216 bool done =
217 tls32_.state_and_flags.CompareAndSetWeakRelease(old_state_and_flags.GetValue(),
218 new_state_and_flags.GetValue());
219 if (LIKELY(done)) {
220 break;
221 }
222 }
223 }
224
PassActiveSuspendBarriers()225 inline void Thread::PassActiveSuspendBarriers() {
226 while (true) {
227 StateAndFlags state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
228 if (LIKELY(!state_and_flags.IsFlagSet(ThreadFlag::kCheckpointRequest) &&
229 !state_and_flags.IsFlagSet(ThreadFlag::kEmptyCheckpointRequest) &&
230 !state_and_flags.IsFlagSet(ThreadFlag::kActiveSuspendBarrier))) {
231 break;
232 } else if (state_and_flags.IsFlagSet(ThreadFlag::kActiveSuspendBarrier)) {
233 PassActiveSuspendBarriers(this);
234 } else {
235 // Impossible
236 LOG(FATAL) << "Fatal, thread transitioned into suspended without running the checkpoint";
237 }
238 }
239 }
240
TransitionFromRunnableToSuspended(ThreadState new_state)241 inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
242 // Note: JNI stubs inline a fast path of this method that transitions to suspended if
243 // there are no flags set and then clears the `held_mutexes[kMutatorLock]` (this comes
244 // from a specialized `BaseMutex::RegisterAsLockedImpl(., kMutatorLock)` inlined from
245 // the `GetMutatorLock()->TransitionFromRunnableToSuspended(this)` below).
246 // Therefore any code added here (other than debug build assertions) should be gated
247 // on some flag being set, so that the JNI stub can take the slow path to get here.
248 AssertThreadSuspensionIsAllowable();
249 PoisonObjectPointersIfDebug();
250 DCHECK_EQ(this, Thread::Current());
251 // Change to non-runnable state, thereby appearing suspended to the system.
252 TransitionToSuspendedAndRunCheckpoints(new_state);
253 // Mark the release of the share of the mutator lock.
254 GetMutatorLock()->TransitionFromRunnableToSuspended(this);
255 // Once suspended - check the active suspend barrier flag
256 PassActiveSuspendBarriers();
257 }
258
TransitionFromSuspendedToRunnable()259 inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
260 // Note: JNI stubs inline a fast path of this method that transitions to Runnable if
261 // there are no flags set and then stores the mutator lock to `held_mutexes[kMutatorLock]`
262 // (this comes from a specialized `BaseMutex::RegisterAsUnlockedImpl(., kMutatorLock)`
263 // inlined from the `GetMutatorLock()->TransitionFromSuspendedToRunnable(this)` below).
264 // Therefore any code added here (other than debug build assertions) should be gated
265 // on some flag being set, so that the JNI stub can take the slow path to get here.
266 StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
267 ThreadState old_state = old_state_and_flags.GetState();
268 DCHECK_NE(old_state, ThreadState::kRunnable);
269 while (true) {
270 GetMutatorLock()->AssertNotHeld(this); // Otherwise we starve GC.
271 // Optimize for the return from native code case - this is the fast path.
272 // Atomically change from suspended to runnable if no suspend request pending.
273 constexpr uint32_t kCheckedFlags =
274 SuspendOrCheckpointRequestFlags() |
275 enum_cast<uint32_t>(ThreadFlag::kActiveSuspendBarrier) |
276 FlipFunctionFlags();
277 if (LIKELY(!old_state_and_flags.IsAnyOfFlagsSet(kCheckedFlags))) {
278 // CAS the value with a memory barrier.
279 StateAndFlags new_state_and_flags = old_state_and_flags.WithState(ThreadState::kRunnable);
280 if (LIKELY(tls32_.state_and_flags.CompareAndSetWeakAcquire(old_state_and_flags.GetValue(),
281 new_state_and_flags.GetValue()))) {
282 // Mark the acquisition of a share of the mutator lock.
283 GetMutatorLock()->TransitionFromSuspendedToRunnable(this);
284 break;
285 }
286 } else if (old_state_and_flags.IsFlagSet(ThreadFlag::kActiveSuspendBarrier)) {
287 PassActiveSuspendBarriers(this);
288 } else if (UNLIKELY(old_state_and_flags.IsFlagSet(ThreadFlag::kCheckpointRequest) ||
289 old_state_and_flags.IsFlagSet(ThreadFlag::kEmptyCheckpointRequest))) {
290 // Checkpoint flags should not be set while in suspended state.
291 static_assert(static_cast<std::underlying_type_t<ThreadState>>(ThreadState::kRunnable) == 0u);
292 LOG(FATAL) << "Transitioning to Runnable with checkpoint flag,"
293 // Note: Keeping unused flags. If they are set, it points to memory corruption.
294 << " flags=" << old_state_and_flags.WithState(ThreadState::kRunnable).GetValue()
295 << " state=" << old_state_and_flags.GetState();
296 } else if (old_state_and_flags.IsFlagSet(ThreadFlag::kSuspendRequest)) {
297 // Wait while our suspend count is non-zero.
298
299 // We pass null to the MutexLock as we may be in a situation where the
300 // runtime is shutting down. Guarding ourselves from that situation
301 // requires to take the shutdown lock, which is undesirable here.
302 Thread* thread_to_pass = nullptr;
303 if (kIsDebugBuild && !IsDaemon()) {
304 // We know we can make our debug locking checks on non-daemon threads,
305 // so re-enable them on debug builds.
306 thread_to_pass = this;
307 }
308 MutexLock mu(thread_to_pass, *Locks::thread_suspend_count_lock_);
309 ScopedTransitioningToRunnable scoped_transitioning_to_runnable(this);
310 // Reload state and flags after locking the mutex.
311 old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
312 DCHECK_EQ(old_state, old_state_and_flags.GetState());
313 while (old_state_and_flags.IsFlagSet(ThreadFlag::kSuspendRequest)) {
314 // Re-check when Thread::resume_cond_ is notified.
315 Thread::resume_cond_->Wait(thread_to_pass);
316 // Reload state and flags after waiting.
317 old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
318 DCHECK_EQ(old_state, old_state_and_flags.GetState());
319 }
320 DCHECK_EQ(GetSuspendCount(), 0);
321 } else if (UNLIKELY(old_state_and_flags.IsFlagSet(ThreadFlag::kRunningFlipFunction)) ||
322 UNLIKELY(old_state_and_flags.IsFlagSet(ThreadFlag::kWaitingForFlipFunction))) {
323 // The thread should be suspended while another thread is running the flip function.
324 static_assert(static_cast<std::underlying_type_t<ThreadState>>(ThreadState::kRunnable) == 0u);
325 LOG(FATAL) << "Transitioning to Runnable while another thread is running the flip function,"
326 // Note: Keeping unused flags. If they are set, it points to memory corruption.
327 << " flags=" << old_state_and_flags.WithState(ThreadState::kRunnable).GetValue()
328 << " state=" << old_state_and_flags.GetState();
329 } else {
330 DCHECK(old_state_and_flags.IsFlagSet(ThreadFlag::kPendingFlipFunction));
331 // CAS the value with a memory barrier.
332 // Do not set `ThreadFlag::kRunningFlipFunction` as no other thread can run
333 // the flip function for a thread that is not suspended.
334 StateAndFlags new_state_and_flags = old_state_and_flags.WithState(ThreadState::kRunnable)
335 .WithoutFlag(ThreadFlag::kPendingFlipFunction);
336 if (LIKELY(tls32_.state_and_flags.CompareAndSetWeakAcquire(old_state_and_flags.GetValue(),
337 new_state_and_flags.GetValue()))) {
338 // Mark the acquisition of a share of the mutator lock.
339 GetMutatorLock()->TransitionFromSuspendedToRunnable(this);
340 // Run the flip function.
341 RunFlipFunction(this, /*notify=*/ false);
342 break;
343 }
344 }
345 // Reload state and flags.
346 old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
347 DCHECK_EQ(old_state, old_state_and_flags.GetState());
348 }
349 return static_cast<ThreadState>(old_state);
350 }
351
AllocTlab(size_t bytes)352 inline mirror::Object* Thread::AllocTlab(size_t bytes) {
353 DCHECK_GE(TlabSize(), bytes);
354 ++tlsPtr_.thread_local_objects;
355 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
356 tlsPtr_.thread_local_pos += bytes;
357 return ret;
358 }
359
PushOnThreadLocalAllocationStack(mirror::Object * obj)360 inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
361 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
362 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
363 // There's room.
364 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
365 sizeof(StackReference<mirror::Object>),
366 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
367 DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr);
368 tlsPtr_.thread_local_alloc_stack_top->Assign(obj);
369 ++tlsPtr_.thread_local_alloc_stack_top;
370 return true;
371 }
372 return false;
373 }
374
GetWeakRefAccessEnabled()375 inline bool Thread::GetWeakRefAccessEnabled() const {
376 CHECK(kUseReadBarrier);
377 DCHECK(this == Thread::Current());
378 WeakRefAccessState s = tls32_.weak_ref_access_enabled.load(std::memory_order_relaxed);
379 if (LIKELY(s == WeakRefAccessState::kVisiblyEnabled)) {
380 return true;
381 }
382 s = tls32_.weak_ref_access_enabled.load(std::memory_order_acquire);
383 if (s == WeakRefAccessState::kVisiblyEnabled) {
384 return true;
385 } else if (s == WeakRefAccessState::kDisabled) {
386 return false;
387 }
388 DCHECK(s == WeakRefAccessState::kEnabled)
389 << "state = " << static_cast<std::underlying_type_t<WeakRefAccessState>>(s);
390 // The state is only changed back to DISABLED during a checkpoint. Thus no other thread can
391 // change the value concurrently here. No other thread reads the value we store here, so there
392 // is no need for a release store.
393 tls32_.weak_ref_access_enabled.store(WeakRefAccessState::kVisiblyEnabled,
394 std::memory_order_relaxed);
395 return true;
396 }
397
SetThreadLocalAllocationStack(StackReference<mirror::Object> * start,StackReference<mirror::Object> * end)398 inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
399 StackReference<mirror::Object>* end) {
400 DCHECK(Thread::Current() == this) << "Should be called by self";
401 DCHECK(start != nullptr);
402 DCHECK(end != nullptr);
403 DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>));
404 DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>));
405 DCHECK_LT(start, end);
406 tlsPtr_.thread_local_alloc_stack_end = end;
407 tlsPtr_.thread_local_alloc_stack_top = start;
408 }
409
RevokeThreadLocalAllocationStack()410 inline void Thread::RevokeThreadLocalAllocationStack() {
411 if (kIsDebugBuild) {
412 // Note: self is not necessarily equal to this thread since thread may be suspended.
413 Thread* self = Thread::Current();
414 DCHECK(this == self || IsSuspended() || GetState() == ThreadState::kWaitingPerformingGc)
415 << GetState() << " thread " << this << " self " << self;
416 }
417 tlsPtr_.thread_local_alloc_stack_end = nullptr;
418 tlsPtr_.thread_local_alloc_stack_top = nullptr;
419 }
420
PoisonObjectPointersIfDebug()421 inline void Thread::PoisonObjectPointersIfDebug() {
422 if (kObjPtrPoisoning) {
423 Thread::Current()->PoisonObjectPointers();
424 }
425 }
426
ModifySuspendCount(Thread * self,int delta,AtomicInteger * suspend_barrier,SuspendReason reason)427 inline bool Thread::ModifySuspendCount(Thread* self,
428 int delta,
429 AtomicInteger* suspend_barrier,
430 SuspendReason reason) {
431 if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) {
432 // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if
433 // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop.
434 while (true) {
435 if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, reason))) {
436 return true;
437 } else {
438 // Failure means the list of active_suspend_barriers is full or we are in the middle of a
439 // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and
440 // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the
441 // flip function. Note that we could not simply wait for the thread to change to a suspended
442 // state, because it might need to run checkpoint function before the state change or
443 // resumes from the resume_cond_, which also needs thread_suspend_count_lock_.
444 //
445 // The list of active_suspend_barriers is very unlikely to be full since more than
446 // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and
447 // target thread stays in kRunnable in the mean time.
448 Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
449 NanoSleep(100000);
450 Locks::thread_suspend_count_lock_->ExclusiveLock(self);
451 }
452 }
453 } else {
454 return ModifySuspendCountInternal(self, delta, suspend_barrier, reason);
455 }
456 }
457
PushShadowFrame(ShadowFrame * new_top_frame)458 inline ShadowFrame* Thread::PushShadowFrame(ShadowFrame* new_top_frame) {
459 new_top_frame->CheckConsistentVRegs();
460 return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame);
461 }
462
PopShadowFrame()463 inline ShadowFrame* Thread::PopShadowFrame() {
464 return tlsPtr_.managed_stack.PopShadowFrame();
465 }
466
GetStackEndForInterpreter(bool implicit_overflow_check)467 inline uint8_t* Thread::GetStackEndForInterpreter(bool implicit_overflow_check) const {
468 uint8_t* end = tlsPtr_.stack_end + (implicit_overflow_check
469 ? GetStackOverflowReservedBytes(kRuntimeISA)
470 : 0);
471 if (kIsDebugBuild) {
472 // In a debuggable build, but especially under ASAN, the access-checks interpreter has a
473 // potentially humongous stack size. We don't want to take too much of the stack regularly,
474 // so do not increase the regular reserved size (for compiled code etc) and only report the
475 // virtually smaller stack to the interpreter here.
476 end += GetStackOverflowReservedBytes(kRuntimeISA);
477 }
478 return end;
479 }
480
ResetDefaultStackEnd()481 inline void Thread::ResetDefaultStackEnd() {
482 // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
483 // to throw a StackOverflowError.
484 tlsPtr_.stack_end = tlsPtr_.stack_begin + GetStackOverflowReservedBytes(kRuntimeISA);
485 }
486
487 } // namespace art
488
489 #endif // ART_RUNTIME_THREAD_INL_H_
490