Debian Codegraph @

js/src/irregexp/imported/regexp-stack.h

raw
1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_REGEXP_REGEXP_STACK_H_
6 #define V8_REGEXP_REGEXP_STACK_H_
7
8 #include "irregexp/RegExpShim.h"
9
10 namespace v8 {
11 namespace internal {
12
13 class RegExpStack;
14
15 // Maintains a per-v8thread stack area that can be used by irregexp
16 // implementation for its backtracking stack.
17 class V8_NODISCARD RegExpStackScope final {
18 public:
19 // Create and delete an instance to control the life-time of a growing stack.
20
21 // Initializes the stack memory area if necessary.
22 explicit RegExpStackScope(Isolate* isolate);
23 ~RegExpStackScope(); // Releases the stack if it has grown.
24 RegExpStackScope(const RegExpStackScope&) = delete;
25 RegExpStackScope& operator=(const RegExpStackScope&) = delete;
26
27 RegExpStack* stack() const { return regexp_stack_; }
28
29 private:
30 RegExpStack* const regexp_stack_;
31 const ptrdiff_t old_sp_top_delta_;
32 };
33
34 class RegExpStack final {
35 public:
36 RegExpStack();
37 ~RegExpStack();
38 RegExpStack(const RegExpStack&) = delete;
39 RegExpStack& operator=(const RegExpStack&) = delete;
40
41 #if defined(V8_TARGET_ARCH_PPC64) || defined(V8_TARGET_ARCH_S390X)
42 static constexpr int kSlotSize = kSystemPointerSize;
43 #else
44 static constexpr int kSlotSize = kInt32Size;
45 #endif
46 // Number of allocated locations on the stack below the limit. No sequence of
47 // pushes must be longer than this without doing a stack-limit check.
48 static constexpr int kStackLimitSlackSlotCount = 32;
49 static constexpr int kStackLimitSlackSize =
50 kStackLimitSlackSlotCount * kSlotSize;
51
52 Address begin() const {
53 return reinterpret_cast<Address>(thread_local_.memory_);
54 }
55 Address end() const {
56 DCHECK_NE(0, thread_local_.memory_size_);
57 DCHECK_EQ(thread_local_.memory_top_,
58 thread_local_.memory_ + thread_local_.memory_size_);
59 return reinterpret_cast<Address>(thread_local_.memory_top_);
60 }
61 Address memory_top() const { return end(); }
62
63 Address stack_pointer() const {
64 return reinterpret_cast<Address>(thread_local_.stack_pointer_);
65 }
66
67 size_t memory_size() const { return thread_local_.memory_size_; }
68
69 // If the stack pointer gets below the limit, we should react and
70 // either grow the stack or report an out-of-stack exception.
71 // There is only a limited number of locations below the stack limit,
72 // so users of the stack should check the stack limit during any
73 // sequence of pushes longer that this.
74 Address* limit_address_address() { return &thread_local_.limit_; }
75
76 // Ensures that there is a memory area with at least the specified size.
77 // If passing zero, the default/minimum size buffer is allocated.
78 Address EnsureCapacity(size_t size);
79
80 // Thread local archiving.
81 static constexpr int ArchiveSpacePerThread() {
82 return static_cast<int>(kThreadLocalSize);
83 }
84 char* ArchiveStack(char* to);
85 char* RestoreStack(char* from);
86 void FreeThreadResources() { thread_local_.ResetToStaticStack(this); }
87
88 // Maximal size of allocated stack area.
89 static constexpr size_t kMaximumStackSize = 64 * MB;
90
91 private:
92 // Artificial limit used when the thread-local state has been destroyed.
93 static const Address kMemoryTop =
94 static_cast<Address>(static_cast<uintptr_t>(-1));
95
96 // In addition to dynamically-allocated, variable-sized stacks, we also have
97 // a statically allocated and sized area that is used whenever no dynamic
98 // stack is allocated. This guarantees that a stack is always available and
99 // we can skip availability-checks later on.
100 static constexpr size_t kStaticStackSize = 1 * KB;
101 // It's at least double the slack size to ensure that we have a bit of
102 // breathing room before NativeRegExpMacroAssembler::GrowStack must be
103 // called.
104 static_assert(kStaticStackSize >= 2 * kStackLimitSlackSize);
105 static_assert(kStaticStackSize <= kMaximumStackSize);
106 uint8_t static_stack_[kStaticStackSize] = {0};
107
108 // Minimal size of dynamically-allocated stack area.
109 static constexpr size_t kMinimumDynamicStackSize = 2 * KB;
110 static_assert(kMinimumDynamicStackSize == 2 * kStaticStackSize);
111
112 // Structure holding the allocated memory, size and limit. Thread switching
113 // archives and restores this struct.
114 struct ThreadLocal {
115 explicit ThreadLocal(RegExpStack* regexp_stack) {
116 ResetToStaticStack(regexp_stack);
117 }
118
119 // If memory_size_ > 0 then
120 // - memory_, memory_top_, stack_pointer_ must be non-nullptr
121 // - memory_top_ = memory_ + memory_size_
122 // - memory_ <= stack_pointer_ <= memory_top_
123 uint8_t* memory_ = nullptr;
124 uint8_t* memory_top_ = nullptr;
125 size_t memory_size_ = 0;
126 uint8_t* stack_pointer_ = nullptr;
127 Address limit_ = kNullAddress;
128 bool owns_memory_ = false; // Whether memory_ is owned and must be freed.
129
130 void ResetToStaticStack(RegExpStack* regexp_stack);
131 void ResetToStaticStackIfEmpty(RegExpStack* regexp_stack) {
132 if (stack_pointer_ == memory_top_) ResetToStaticStack(regexp_stack);
133 }
134 void FreeAndInvalidate();
135 };
136 static constexpr size_t kThreadLocalSize = sizeof(ThreadLocal);
137
138 Address memory_top_address_address() {
139 return reinterpret_cast<Address>(&thread_local_.memory_top_);
140 }
141
142 Address stack_pointer_address() {
143 return reinterpret_cast<Address>(&thread_local_.stack_pointer_);
144 }
145
146 // A position-independent representation of the stack pointer.
147 ptrdiff_t sp_top_delta() const {
148 ptrdiff_t result =
149 reinterpret_cast<intptr_t>(thread_local_.stack_pointer_) -
150 reinterpret_cast<intptr_t>(thread_local_.memory_top_);
151 DCHECK_LE(result, 0);
152 return result;
153 }
154
155 // Resets the buffer if it has grown beyond the default/minimum size and is
156 // empty.
157 void ResetIfEmpty() { thread_local_.ResetToStaticStackIfEmpty(this); }
158
159 // Whether the ThreadLocal storage has been invalidated.
160 bool IsValid() const { return thread_local_.memory_ != nullptr; }
161
162 ThreadLocal thread_local_;
163
164 friend class ExternalReference;
165 friend class RegExpStackScope;
166 };
167
168 } // namespace internal
169 } // namespace v8
170
171 #endif // V8_REGEXP_REGEXP_STACK_H_