11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_EXECUTION_STACK_GUARD_H_ 61cb0ef41Sopenharmony_ci#define V8_EXECUTION_STACK_GUARD_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "include/v8-internal.h" 91cb0ef41Sopenharmony_ci#include "src/base/atomicops.h" 101cb0ef41Sopenharmony_ci#include "src/common/globals.h" 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cinamespace v8 { 131cb0ef41Sopenharmony_cinamespace internal { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciclass ExecutionAccess; 161cb0ef41Sopenharmony_ciclass InterruptsScope; 171cb0ef41Sopenharmony_ciclass Isolate; 181cb0ef41Sopenharmony_ciclass Object; 191cb0ef41Sopenharmony_ciclass RootVisitor; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// StackGuard contains the handling of the limits that are used to limit the 221cb0ef41Sopenharmony_ci// number of nested invocations of JavaScript and the stack size used in each 231cb0ef41Sopenharmony_ci// invocation. 241cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE V8_NODISCARD StackGuard final { 251cb0ef41Sopenharmony_ci public: 261cb0ef41Sopenharmony_ci StackGuard(const StackGuard&) = delete; 271cb0ef41Sopenharmony_ci StackGuard& operator=(const StackGuard&) = delete; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci explicit StackGuard(Isolate* isolate) : isolate_(isolate) {} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci // Pass the address beyond which the stack should not grow. The stack 321cb0ef41Sopenharmony_ci // is assumed to grow downwards. 331cb0ef41Sopenharmony_ci void SetStackLimit(uintptr_t limit); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // The simulator uses a separate JS stack. Limits on the JS stack might have 361cb0ef41Sopenharmony_ci // to be adjusted in order to reflect overflows of the C stack, because we 371cb0ef41Sopenharmony_ci // cannot rely on the interleaving of frames on the simulator. 381cb0ef41Sopenharmony_ci void AdjustStackLimitForSimulator(); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci // Threading support. 411cb0ef41Sopenharmony_ci char* ArchiveStackGuard(char* to); 421cb0ef41Sopenharmony_ci char* RestoreStackGuard(char* from); 431cb0ef41Sopenharmony_ci static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } 441cb0ef41Sopenharmony_ci void FreeThreadResources(); 451cb0ef41Sopenharmony_ci // Sets up the default stack guard for this thread. 461cb0ef41Sopenharmony_ci void InitThread(const ExecutionAccess& lock); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci#define INTERRUPT_LIST(V) \ 491cb0ef41Sopenharmony_ci V(TERMINATE_EXECUTION, TerminateExecution, 0) \ 501cb0ef41Sopenharmony_ci V(GC_REQUEST, GC, 1) \ 511cb0ef41Sopenharmony_ci V(INSTALL_CODE, InstallCode, 2) \ 521cb0ef41Sopenharmony_ci V(INSTALL_BASELINE_CODE, InstallBaselineCode, 3) \ 531cb0ef41Sopenharmony_ci V(API_INTERRUPT, ApiInterrupt, 4) \ 541cb0ef41Sopenharmony_ci V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 5) \ 551cb0ef41Sopenharmony_ci V(GROW_SHARED_MEMORY, GrowSharedMemory, 6) \ 561cb0ef41Sopenharmony_ci V(LOG_WASM_CODE, LogWasmCode, 7) \ 571cb0ef41Sopenharmony_ci V(WASM_CODE_GC, WasmCodeGC, 8) \ 581cb0ef41Sopenharmony_ci V(INSTALL_MAGLEV_CODE, InstallMaglevCode, 9) 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci#define V(NAME, Name, id) \ 611cb0ef41Sopenharmony_ci inline bool Check##Name() { return CheckInterrupt(NAME); } \ 621cb0ef41Sopenharmony_ci inline void Request##Name() { RequestInterrupt(NAME); } \ 631cb0ef41Sopenharmony_ci inline void Clear##Name() { ClearInterrupt(NAME); } 641cb0ef41Sopenharmony_ci INTERRUPT_LIST(V) 651cb0ef41Sopenharmony_ci#undef V 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci // Flag used to set the interrupt causes. 681cb0ef41Sopenharmony_ci enum InterruptFlag { 691cb0ef41Sopenharmony_ci#define V(NAME, Name, id) NAME = (1 << id), 701cb0ef41Sopenharmony_ci INTERRUPT_LIST(V) 711cb0ef41Sopenharmony_ci#undef V 721cb0ef41Sopenharmony_ci#define V(NAME, Name, id) NAME | 731cb0ef41Sopenharmony_ci ALL_INTERRUPTS = INTERRUPT_LIST(V) 0 741cb0ef41Sopenharmony_ci#undef V 751cb0ef41Sopenharmony_ci }; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci uintptr_t climit() { return thread_local_.climit(); } 781cb0ef41Sopenharmony_ci uintptr_t jslimit() { return thread_local_.jslimit(); } 791cb0ef41Sopenharmony_ci // This provides an asynchronous read of the stack limits for the current 801cb0ef41Sopenharmony_ci // thread. There are no locks protecting this, but it is assumed that you 811cb0ef41Sopenharmony_ci // have the global V8 lock if you are using multiple V8 threads. 821cb0ef41Sopenharmony_ci uintptr_t real_climit() { return thread_local_.real_climit_; } 831cb0ef41Sopenharmony_ci uintptr_t real_jslimit() { return thread_local_.real_jslimit_; } 841cb0ef41Sopenharmony_ci Address address_of_jslimit() { 851cb0ef41Sopenharmony_ci return reinterpret_cast<Address>(&thread_local_.jslimit_); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci Address address_of_real_jslimit() { 881cb0ef41Sopenharmony_ci return reinterpret_cast<Address>(&thread_local_.real_jslimit_); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci // If the stack guard is triggered, but it is not an actual 921cb0ef41Sopenharmony_ci // stack overflow, then handle the interruption accordingly. 931cb0ef41Sopenharmony_ci Object HandleInterrupts(); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci // Special case of {HandleInterrupts}: checks for termination requests only. 961cb0ef41Sopenharmony_ci // This is guaranteed to never cause GC, so can be used to interrupt 971cb0ef41Sopenharmony_ci // long-running computations that are not GC-safe. 981cb0ef41Sopenharmony_ci bool HasTerminationRequest(); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci static constexpr int kSizeInBytes = 7 * kSystemPointerSize; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci static char* Iterate(RootVisitor* v, char* thread_storage) { 1031cb0ef41Sopenharmony_ci return thread_storage + ArchiveSpacePerThread(); 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci private: 1071cb0ef41Sopenharmony_ci bool CheckInterrupt(InterruptFlag flag); 1081cb0ef41Sopenharmony_ci void RequestInterrupt(InterruptFlag flag); 1091cb0ef41Sopenharmony_ci void ClearInterrupt(InterruptFlag flag); 1101cb0ef41Sopenharmony_ci int FetchAndClearInterrupts(); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci // You should hold the ExecutionAccess lock when calling this method. 1131cb0ef41Sopenharmony_ci bool has_pending_interrupts(const ExecutionAccess& lock) { 1141cb0ef41Sopenharmony_ci return thread_local_.interrupt_flags_ != 0; 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci // You should hold the ExecutionAccess lock when calling this method. 1181cb0ef41Sopenharmony_ci inline void set_interrupt_limits(const ExecutionAccess& lock); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci // Reset limits to actual values. For example after handling interrupt. 1211cb0ef41Sopenharmony_ci // You should hold the ExecutionAccess lock when calling this method. 1221cb0ef41Sopenharmony_ci inline void reset_limits(const ExecutionAccess& lock); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci // Enable or disable interrupts. 1251cb0ef41Sopenharmony_ci void EnableInterrupts(); 1261cb0ef41Sopenharmony_ci void DisableInterrupts(); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci#if V8_TARGET_ARCH_64_BIT 1291cb0ef41Sopenharmony_ci static const uintptr_t kInterruptLimit = uintptr_t{0xfffffffffffffffe}; 1301cb0ef41Sopenharmony_ci static const uintptr_t kIllegalLimit = uintptr_t{0xfffffffffffffff8}; 1311cb0ef41Sopenharmony_ci#else 1321cb0ef41Sopenharmony_ci static const uintptr_t kInterruptLimit = 0xfffffffe; 1331cb0ef41Sopenharmony_ci static const uintptr_t kIllegalLimit = 0xfffffff8; 1341cb0ef41Sopenharmony_ci#endif 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci void PushInterruptsScope(InterruptsScope* scope); 1371cb0ef41Sopenharmony_ci void PopInterruptsScope(); 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci class ThreadLocal final { 1401cb0ef41Sopenharmony_ci public: 1411cb0ef41Sopenharmony_ci ThreadLocal() {} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci void Initialize(Isolate* isolate, const ExecutionAccess& lock); 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci // The stack limit is split into a JavaScript and a C++ stack limit. These 1461cb0ef41Sopenharmony_ci // two are the same except when running on a simulator where the C++ and 1471cb0ef41Sopenharmony_ci // JavaScript stacks are separate. Each of the two stack limits have two 1481cb0ef41Sopenharmony_ci // values. The one with the real_ prefix is the actual stack limit 1491cb0ef41Sopenharmony_ci // set for the VM. The one without the real_ prefix has the same value as 1501cb0ef41Sopenharmony_ci // the actual stack limit except when there is an interruption (e.g. debug 1511cb0ef41Sopenharmony_ci // break or preemption) in which case it is lowered to make stack checks 1521cb0ef41Sopenharmony_ci // fail. Both the generated code and the runtime system check against the 1531cb0ef41Sopenharmony_ci // one without the real_ prefix. 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci // Actual JavaScript stack limit set for the VM. 1561cb0ef41Sopenharmony_ci uintptr_t real_jslimit_ = kIllegalLimit; 1571cb0ef41Sopenharmony_ci // Actual C++ stack limit set for the VM. 1581cb0ef41Sopenharmony_ci uintptr_t real_climit_ = kIllegalLimit; 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci // jslimit_ and climit_ can be read without any lock. 1611cb0ef41Sopenharmony_ci // Writing requires the ExecutionAccess lock. 1621cb0ef41Sopenharmony_ci base::AtomicWord jslimit_ = kIllegalLimit; 1631cb0ef41Sopenharmony_ci base::AtomicWord climit_ = kIllegalLimit; 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci uintptr_t jslimit() { 1661cb0ef41Sopenharmony_ci return bit_cast<uintptr_t>(base::Relaxed_Load(&jslimit_)); 1671cb0ef41Sopenharmony_ci } 1681cb0ef41Sopenharmony_ci void set_jslimit(uintptr_t limit) { 1691cb0ef41Sopenharmony_ci return base::Relaxed_Store(&jslimit_, 1701cb0ef41Sopenharmony_ci static_cast<base::AtomicWord>(limit)); 1711cb0ef41Sopenharmony_ci } 1721cb0ef41Sopenharmony_ci uintptr_t climit() { 1731cb0ef41Sopenharmony_ci return bit_cast<uintptr_t>(base::Relaxed_Load(&climit_)); 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci void set_climit(uintptr_t limit) { 1761cb0ef41Sopenharmony_ci return base::Relaxed_Store(&climit_, 1771cb0ef41Sopenharmony_ci static_cast<base::AtomicWord>(limit)); 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci InterruptsScope* interrupt_scopes_ = nullptr; 1811cb0ef41Sopenharmony_ci intptr_t interrupt_flags_ = 0; 1821cb0ef41Sopenharmony_ci }; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci // TODO(isolates): Technically this could be calculated directly from a 1851cb0ef41Sopenharmony_ci // pointer to StackGuard. 1861cb0ef41Sopenharmony_ci Isolate* isolate_; 1871cb0ef41Sopenharmony_ci ThreadLocal thread_local_; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci friend class Isolate; 1901cb0ef41Sopenharmony_ci friend class StackLimitCheck; 1911cb0ef41Sopenharmony_ci friend class InterruptsScope; 1921cb0ef41Sopenharmony_ci}; 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ciSTATIC_ASSERT(StackGuard::kSizeInBytes == sizeof(StackGuard)); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci} // namespace internal 1971cb0ef41Sopenharmony_ci} // namespace v8 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci#endif // V8_EXECUTION_STACK_GUARD_H_ 200