1// Copyright 2014 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_EXECUTION_H_
6#define V8_EXECUTION_H_
7
8#include "src/base/atomicops.h"
9#include "src/globals.h"
10
11namespace v8 {
12namespace internal {
13
14class MicrotaskQueue;
15
16template <typename T>
17class Handle;
18
19class Execution final : public AllStatic {
20 public:
21 // Whether to report pending messages, or keep them pending on the isolate.
22 enum class MessageHandling { kReport, kKeepPending };
23 enum class Target { kCallable, kRunMicrotasks };
24
25 // Call a function, the caller supplies a receiver and an array
26 // of arguments.
27 //
28 // When the function called is not in strict mode, receiver is
29 // converted to an object.
30 //
31 V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Call(
32 Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
33 int argc, Handle<Object> argv[]);
34
35 // Construct object from function, the caller supplies an array of
36 // arguments.
37 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New(
38 Isolate* isolate, Handle<Object> constructor, int argc,
39 Handle<Object> argv[]);
40 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New(
41 Isolate* isolate, Handle<Object> constructor, Handle<Object> new_target,
42 int argc, Handle<Object> argv[]);
43
44 // Call a function, just like Call(), but handle don't report exceptions
45 // externally.
46 // The return value is either the result of calling the function (if no
47 // exception occurred), or an empty handle.
48 // If message_handling is MessageHandling::kReport, exceptions (except for
49 // termination exceptions) will be stored in exception_out (if not a
50 // nullptr).
51 V8_EXPORT_PRIVATE static MaybeHandle<Object> TryCall(
52 Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
53 int argc, Handle<Object> argv[], MessageHandling message_handling,
54 MaybeHandle<Object>* exception_out);
55 // Convenience method for performing RunMicrotasks
56 static MaybeHandle<Object> TryRunMicrotasks(
57 Isolate* isolate, MicrotaskQueue* microtask_queue,
58 MaybeHandle<Object>* exception_out);
59};
60
61
62class ExecutionAccess;
63class InterruptsScope;
64
65// StackGuard contains the handling of the limits that are used to limit the
66// number of nested invocations of JavaScript and the stack size used in each
67// invocation.
68class V8_EXPORT_PRIVATE StackGuard final {
69 public:
70 explicit StackGuard(Isolate* isolate) : isolate_(isolate) {}
71
72 // Pass the address beyond which the stack should not grow. The stack
73 // is assumed to grow downwards.
74 void SetStackLimit(uintptr_t limit);
75
76 // The simulator uses a separate JS stack. Limits on the JS stack might have
77 // to be adjusted in order to reflect overflows of the C stack, because we
78 // cannot rely on the interleaving of frames on the simulator.
79 void AdjustStackLimitForSimulator();
80
81 // Threading support.
82 char* ArchiveStackGuard(char* to);
83 char* RestoreStackGuard(char* from);
84 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
85 void FreeThreadResources();
86 // Sets up the default stack guard for this thread if it has not
87 // already been set up.
88 void InitThread(const ExecutionAccess& lock);
89 // Clears the stack guard for this thread so it does not look as if
90 // it has been set up.
91 void ClearThread(const ExecutionAccess& lock);
92
93#define INTERRUPT_LIST(V) \
94 V(TERMINATE_EXECUTION, TerminateExecution, 0) \
95 V(GC_REQUEST, GC, 1) \
96 V(INSTALL_CODE, InstallCode, 2) \
97 V(API_INTERRUPT, ApiInterrupt, 3) \
98 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 4) \
99 V(GROW_SHARED_MEMORY, GrowSharedMemory, 5)
100
101#define V(NAME, Name, id) \
102 inline bool Check##Name() { return CheckInterrupt(NAME); } \
103 inline bool CheckAndClear##Name() { return CheckAndClearInterrupt(NAME); } \
104 inline void Request##Name() { RequestInterrupt(NAME); } \
105 inline void Clear##Name() { ClearInterrupt(NAME); }
106 INTERRUPT_LIST(V)
107#undef V
108
109 // Flag used to set the interrupt causes.
110 enum InterruptFlag {
111 #define V(NAME, Name, id) NAME = (1 << id),
112 INTERRUPT_LIST(V)
113 #undef V
114 #define V(NAME, Name, id) NAME |
115 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
116 #undef V
117 };
118
119 uintptr_t climit() { return thread_local_.climit(); }
120 uintptr_t jslimit() { return thread_local_.jslimit(); }
121 // This provides an asynchronous read of the stack limits for the current
122 // thread. There are no locks protecting this, but it is assumed that you
123 // have the global V8 lock if you are using multiple V8 threads.
124 uintptr_t real_climit() {
125 return thread_local_.real_climit_;
126 }
127 uintptr_t real_jslimit() {
128 return thread_local_.real_jslimit_;
129 }
130 Address address_of_jslimit() {
131 return reinterpret_cast<Address>(&thread_local_.jslimit_);
132 }
133 Address address_of_real_jslimit() {
134 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
135 }
136
137 // If the stack guard is triggered, but it is not an actual
138 // stack overflow, then handle the interruption accordingly.
139 Object HandleInterrupts();
140
141 private:
142 bool CheckInterrupt(InterruptFlag flag);
143 void RequestInterrupt(InterruptFlag flag);
144 void ClearInterrupt(InterruptFlag flag);
145 bool CheckAndClearInterrupt(InterruptFlag flag);
146
147 // You should hold the ExecutionAccess lock when calling this method.
148 bool has_pending_interrupts(const ExecutionAccess& lock) {
149 return thread_local_.interrupt_flags_ != 0;
150 }
151
152 // You should hold the ExecutionAccess lock when calling this method.
153 inline void set_interrupt_limits(const ExecutionAccess& lock);
154
155 // Reset limits to actual values. For example after handling interrupt.
156 // You should hold the ExecutionAccess lock when calling this method.
157 inline void reset_limits(const ExecutionAccess& lock);
158
159 // Enable or disable interrupts.
160 void EnableInterrupts();
161 void DisableInterrupts();
162
163#if V8_TARGET_ARCH_64_BIT
164 static const uintptr_t kInterruptLimit = uintptr_t{0xfffffffffffffffe};
165 static const uintptr_t kIllegalLimit = uintptr_t{0xfffffffffffffff8};
166#else
167 static const uintptr_t kInterruptLimit = 0xfffffffe;
168 static const uintptr_t kIllegalLimit = 0xfffffff8;
169#endif
170
171 void PushInterruptsScope(InterruptsScope* scope);
172 void PopInterruptsScope();
173
174 class ThreadLocal final {
175 public:
176 ThreadLocal() { Clear(); }
177 // You should hold the ExecutionAccess lock when you call Initialize or
178 // Clear.
179 void Clear();
180
181 // Returns true if the heap's stack limits should be set, false if not.
182 bool Initialize(Isolate* isolate);
183
184 // The stack limit is split into a JavaScript and a C++ stack limit. These
185 // two are the same except when running on a simulator where the C++ and
186 // JavaScript stacks are separate. Each of the two stack limits have two
187 // values. The one eith the real_ prefix is the actual stack limit
188 // set for the VM. The one without the real_ prefix has the same value as
189 // the actual stack limit except when there is an interruption (e.g. debug
190 // break or preemption) in which case it is lowered to make stack checks
191 // fail. Both the generated code and the runtime system check against the
192 // one without the real_ prefix.
193 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
194 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
195
196 // jslimit_ and climit_ can be read without any lock.
197 // Writing requires the ExecutionAccess lock.
198 base::AtomicWord jslimit_;
199 base::AtomicWord climit_;
200
201 uintptr_t jslimit() {
202 return bit_cast<uintptr_t>(base::Relaxed_Load(&jslimit_));
203 }
204 void set_jslimit(uintptr_t limit) {
205 return base::Relaxed_Store(&jslimit_,
206 static_cast<base::AtomicWord>(limit));
207 }
208 uintptr_t climit() {
209 return bit_cast<uintptr_t>(base::Relaxed_Load(&climit_));
210 }
211 void set_climit(uintptr_t limit) {
212 return base::Relaxed_Store(&climit_,
213 static_cast<base::AtomicWord>(limit));
214 }
215
216 InterruptsScope* interrupt_scopes_;
217 int interrupt_flags_;
218 };
219
220 // TODO(isolates): Technically this could be calculated directly from a
221 // pointer to StackGuard.
222 Isolate* isolate_;
223 ThreadLocal thread_local_;
224
225 friend class Isolate;
226 friend class StackLimitCheck;
227 friend class InterruptsScope;
228
229 DISALLOW_COPY_AND_ASSIGN(StackGuard);
230};
231
232} // namespace internal
233} // namespace v8
234
235#endif // V8_EXECUTION_H_
236