#ifndef V8_EXECUTION_STACK_GUARD_H_
#define V8_EXECUTION_STACK_GUARD_H_
#include "include/v8-internal.h"
#include "src/base/atomicops.h"
#include "src/common/globals.h"
namespace v8 {
namespace internal {
class ExecutionAccess;
class InterruptsScope;
class Isolate;
class Object;
class RootVisitor;
class V8_EXPORT_PRIVATE V8_NODISCARD StackGuard final {
public:
StackGuard(const StackGuard&) = delete;
StackGuard& operator=(const StackGuard&) = delete;
explicit StackGuard(Isolate* isolate) : isolate_(isolate) {}
void SetStackLimit(uintptr_t limit);
void SetStackLimitForStackSwitching(uintptr_t limit);
#ifdef USE_SIMULATOR
void AdjustStackLimitForSimulator();
void ResetStackLimitForSimulator();
#endif
char* ArchiveStackGuard(char* to);
char* RestoreStackGuard(char* from);
static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
void FreeThreadResources();
void InitThread(const ExecutionAccess& lock);
enum class InterruptLevel { kNoGC, kNoHeapWrites, kAnyEffect };
static constexpr int kNumberOfInterruptLevels = 3;
#define INTERRUPT_LIST(V) \
V(TERMINATE_EXECUTION, TerminateExecution, 0, InterruptLevel::kNoGC) \
V(GC_REQUEST, GC, 1, InterruptLevel::kNoHeapWrites) \
V(INSTALL_CODE, InstallCode, 2, InterruptLevel::kAnyEffect) \
V(INSTALL_BASELINE_CODE, InstallBaselineCode, 3, InterruptLevel::kAnyEffect) \
V(API_INTERRUPT, ApiInterrupt, 4, InterruptLevel::kNoHeapWrites) \
V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 5, \
InterruptLevel::kNoHeapWrites) \
V(GROW_SHARED_MEMORY, GrowSharedMemory, 6, InterruptLevel::kNoGC) \
V(LOG_WASM_CODE, LogWasmCode, 7, InterruptLevel::kAnyEffect) \
V(WASM_CODE_GC, WasmCodeGC, 8, InterruptLevel::kNoHeapWrites) \
V(INSTALL_MAGLEV_CODE, InstallMaglevCode, 9, InterruptLevel::kAnyEffect) \
V(GLOBAL_SAFEPOINT, GlobalSafepoint, 10, InterruptLevel::kNoHeapWrites) \
V(START_INCREMENTAL_MARKING, StartIncrementalMarking, 11, \
InterruptLevel::kNoHeapWrites)
#define V(NAME, Name, id, interrupt_level) \
inline bool Check##Name() { return CheckInterrupt(NAME); } \
inline void Request##Name() { RequestInterrupt(NAME); } \
inline void Clear##Name() { ClearInterrupt(NAME); }
INTERRUPT_LIST(V)
#undef V
enum InterruptFlag : uint32_t {
#define V(NAME, Name, id, interrupt_level) NAME = (1 << id),
INTERRUPT_LIST(V)
#undef V
#define V(NAME, Name, id, interrupt_level) NAME |
ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
#undef V
};
static_assert(InterruptFlag::ALL_INTERRUPTS <
std::numeric_limits<uint32_t>::max());
static constexpr InterruptFlag InterruptLevelMask(InterruptLevel level) {
#define V(NAME, Name, id, interrupt_level) \
| (interrupt_level <= level ? NAME : 0)
return static_cast<InterruptFlag>(0 INTERRUPT_LIST(V));
#undef V
}
uintptr_t climit() {
#ifdef USE_SIMULATOR
return thread_local_.climit();
#else
return thread_local_.jslimit();
#endif
}
uintptr_t jslimit() { return thread_local_.jslimit(); }
uintptr_t real_climit() {
#ifdef USE_SIMULATOR
return thread_local_.real_climit_;
#else
return thread_local_.real_jslimit_;
#endif
}
uintptr_t real_jslimit() { return thread_local_.real_jslimit_; }
Address address_of_jslimit() {
return reinterpret_cast<Address>(&thread_local_.jslimit_);
}
Address address_of_real_jslimit() {
return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
}
Address address_of_interrupt_request(InterruptLevel level) {
return reinterpret_cast<Address>(
&thread_local_.interrupt_requested_[static_cast<int>(level)]);
}
static constexpr int jslimit_offset() {
return offsetof(StackGuard, thread_local_) +
offsetof(ThreadLocal, jslimit_);
}
static constexpr int real_jslimit_offset() {
return offsetof(StackGuard, thread_local_) +
offsetof(ThreadLocal, real_jslimit_);
}
static constexpr int no_heap_write_interrupt_request_offset() {
return offsetof(StackGuard, thread_local_) +
offsetof(ThreadLocal, interrupt_requested_) +
static_cast<size_t>(InterruptLevel::kNoHeapWrites);
}
Tagged<Object> HandleInterrupts(
InterruptLevel level = InterruptLevel::kAnyEffect);
bool HasTerminationRequest();
static constexpr int kSizeInBytes = 8 * kSystemPointerSize;
static char* Iterate(RootVisitor* v, char* thread_storage) {
return thread_storage + ArchiveSpacePerThread();
}
private:
bool CheckInterrupt(InterruptFlag flag);
void RequestInterrupt(InterruptFlag flag);
void ClearInterrupt(InterruptFlag flag);
int FetchAndClearInterrupts(InterruptLevel level);
void SetStackLimitInternal(const ExecutionAccess& lock, uintptr_t limit,
uintptr_t jslimit);
bool has_pending_interrupts(const ExecutionAccess& lock) {
return thread_local_.interrupt_flags_ != 0;
}
inline void update_interrupt_requests_and_stack_limits(
const ExecutionAccess& lock);
#if V8_TARGET_ARCH_64_BIT
static const uintptr_t kInterruptLimit = uintptr_t{0xfffffffffffffffe};
static const uintptr_t kIllegalLimit = uintptr_t{0xfffffffffffffff8};
#else
static const uintptr_t kInterruptLimit = 0xfffffffe;
static const uintptr_t kIllegalLimit = 0xfffffff8;
#endif
void PushInterruptsScope(InterruptsScope* scope);
void PopInterruptsScope();
class ThreadLocal final {
public:
ThreadLocal() {}
void Initialize(Isolate* isolate, const ExecutionAccess& lock);
uintptr_t real_jslimit_ = kIllegalLimit;
#ifdef USE_SIMULATOR
uintptr_t real_climit_ = kIllegalLimit;
#else
uintptr_t padding1_;
#endif
base::AtomicWord jslimit_ = kIllegalLimit;
#ifdef USE_SIMULATOR
base::AtomicWord climit_ = kIllegalLimit;
#else
uintptr_t padding2_;
#endif
uintptr_t jslimit() {
return base::bit_cast<uintptr_t>(base::Relaxed_Load(&jslimit_));
}
void set_jslimit(uintptr_t limit) {
return base::Relaxed_Store(&jslimit_,
static_cast<base::AtomicWord>(limit));
}
#ifdef USE_SIMULATOR
uintptr_t climit() {
return base::bit_cast<uintptr_t>(base::Relaxed_Load(&climit_));
}
void set_climit(uintptr_t limit) {
return base::Relaxed_Store(&climit_,
static_cast<base::AtomicWord>(limit));
}
#endif
base::Atomic8 interrupt_requested_[kNumberOfInterruptLevels] = {
false, false, false};
void set_interrupt_requested(InterruptLevel level, bool requested) {
base::Relaxed_Store(&interrupt_requested_[static_cast<int>(level)],
requested);
}
bool has_interrupt_requested(InterruptLevel level) {
return base::Relaxed_Load(&interrupt_requested_[static_cast<int>(level)]);
}
InterruptsScope* interrupt_scopes_ = nullptr;
uint32_t interrupt_flags_ = 0;
};
Isolate* isolate_;
ThreadLocal thread_local_;
friend class Isolate;
friend class StackLimitCheck;
friend class InterruptsScope;
static_assert(std::is_standard_layout_v<ThreadLocal>);
};
static_assert(StackGuard::kSizeInBytes == sizeof(StackGuard));
}
}
#endif