blob: 37e41d50d30a554b665fbbbd0e5693b534cb0c69 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/execution.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/bootstrapper.h"
8#include "src/codegen.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/isolate-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/messages.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000012
13namespace v8 {
14namespace internal {
15
Steve Block44f0eee2011-05-26 01:26:41 +010016StackGuard::StackGuard()
17 : isolate_(NULL) {
18}
19
20
21void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 DCHECK(isolate_ != NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 thread_local_.set_jslimit(kInterruptLimit);
24 thread_local_.set_climit(kInterruptLimit);
Steve Block44f0eee2011-05-26 01:26:41 +010025 isolate_->heap()->SetStackLimits();
26}
27
28
29void StackGuard::reset_limits(const ExecutionAccess& lock) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 DCHECK(isolate_ != NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 thread_local_.set_jslimit(thread_local_.real_jslimit_);
32 thread_local_.set_climit(thread_local_.real_climit_);
Steve Block44f0eee2011-05-26 01:26:41 +010033 isolate_->heap()->SetStackLimits();
34}
35
36
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037static void PrintDeserializedCodeInfo(Handle<JSFunction> function) {
38 if (function->code() == function->shared()->code() &&
39 function->shared()->deserialized()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 PrintF("[Running deserialized script");
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041 Object* script = function->shared()->script();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 if (script->IsScript()) {
43 Object* name = Script::cast(script)->name();
44 if (name->IsString()) {
45 PrintF(": %s", String::cast(name)->ToCString().get());
46 }
47 }
48 PrintF("]\n");
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 }
50}
51
52
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053namespace {
54
55MUST_USE_RESULT MaybeHandle<Object> Invoke(Isolate* isolate, bool is_construct,
56 Handle<Object> target,
57 Handle<Object> receiver, int argc,
58 Handle<Object> args[],
59 Handle<Object> new_target) {
60 DCHECK(!receiver->IsJSGlobalObject());
Steve Block44f0eee2011-05-26 01:26:41 +010061
Steve Blocka7e24c12009-10-30 11:49:00 +000062 // Entering JavaScript.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 VMState<JS> state(isolate);
64 CHECK(AllowJavascriptExecution::IsAllowed(isolate));
65 if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
66 isolate->ThrowIllegalOperation();
67 isolate->ReportPendingMessages();
68 return MaybeHandle<Object>();
69 }
Steve Blocka7e24c12009-10-30 11:49:00 +000070
71 // Placeholder for return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 Object* value = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000073
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 typedef Object* (*JSEntryFunction)(Object* new_target, Object* target,
75 Object* receiver, int argc,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010076 Object*** args);
Steve Blocka7e24c12009-10-30 11:49:00 +000077
Ben Murdoch3ef787d2012-04-12 10:51:47 +010078 Handle<Code> code = is_construct
79 ? isolate->factory()->js_construct_entry_code()
80 : isolate->factory()->js_entry_code();
Steve Blocka7e24c12009-10-30 11:49:00 +000081
Steve Blocka7e24c12009-10-30 11:49:00 +000082 {
83 // Save and restore context around invocation and block the
84 // allocation of handles without explicit handle scopes.
Steve Block44f0eee2011-05-26 01:26:41 +010085 SaveContext save(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 SealHandleScope shs(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087 JSEntryFunction stub_entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
Steve Blocka7e24c12009-10-30 11:49:00 +000088
89 // Call the function through the right JS entry stub.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 Object* orig_func = *new_target;
91 Object* func = *target;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010092 Object* recv = *receiver;
93 Object*** argv = reinterpret_cast<Object***>(args);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 if (FLAG_profile_deserialization && target->IsJSFunction()) {
95 PrintDeserializedCodeInfo(Handle<JSFunction>::cast(target));
96 }
Ben Murdochc5610432016-08-08 18:44:38 +010097 RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::JS_Execution);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098 value = CALL_GENERATED_CODE(isolate, stub_entry, orig_func, func, recv,
99 argc, argv);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 }
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102#ifdef VERIFY_HEAP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103 if (FLAG_verify_heap) {
104 value->ObjectVerify();
105 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000106#endif
107
108 // Update the pending exception flag and return the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 bool has_exception = value->IsException();
110 DCHECK(has_exception == isolate->has_pending_exception());
111 if (has_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +0100112 isolate->ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 return MaybeHandle<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100115 isolate->clear_pending_message();
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 }
117
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 return Handle<Object>(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000119}
120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121} // namespace
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123
124// static
125MaybeHandle<Object> Execution::Call(Isolate* isolate, Handle<Object> callable,
126 Handle<Object> receiver, int argc,
127 Handle<Object> argv[]) {
128 // Convert calls on global objects to be calls on the global
129 // receiver instead to avoid having a 'this' pointer which refers
130 // directly to a global object.
131 if (receiver->IsJSGlobalObject()) {
132 receiver =
133 handle(Handle<JSGlobalObject>::cast(receiver)->global_proxy(), isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +0000134 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 // api callbacks can be called directly.
137 if (callable->IsJSFunction() &&
138 Handle<JSFunction>::cast(callable)->shared()->IsApiFunction()) {
139 Handle<JSFunction> function = Handle<JSFunction>::cast(callable);
140 SaveContext save(isolate);
141 isolate->set_context(function->context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 DCHECK(function->context()->global_object()->IsJSGlobalObject());
143 auto value = Builtins::InvokeApiFunction(function, receiver, argc, argv);
144 bool has_exception = value.is_null();
145 DCHECK(has_exception == isolate->has_pending_exception());
146 if (has_exception) {
147 isolate->ReportPendingMessages();
148 return MaybeHandle<Object>();
149 } else {
150 isolate->clear_pending_message();
151 }
152 return value;
Ben Murdoch589d6972011-11-30 16:04:58 +0000153 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 return Invoke(isolate, false, callable, receiver, argc, argv,
155 isolate->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000156}
157
158
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159// static
160MaybeHandle<Object> Execution::New(Handle<JSFunction> constructor, int argc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 Handle<Object> argv[]) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162 return New(constructor->GetIsolate(), constructor, constructor, argc, argv);
Steve Blocka7e24c12009-10-30 11:49:00 +0000163}
164
165
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166// static
167MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
168 Handle<Object> new_target, int argc,
169 Handle<Object> argv[]) {
170 return Invoke(isolate, true, constructor,
171 isolate->factory()->undefined_value(), argc, argv, new_target);
172}
173
174
175MaybeHandle<Object> Execution::TryCall(Isolate* isolate,
176 Handle<Object> callable,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 Handle<Object> receiver, int argc,
178 Handle<Object> args[],
179 MaybeHandle<Object>* exception_out) {
180 bool is_termination = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 MaybeHandle<Object> maybe_result;
182 if (exception_out != NULL) *exception_out = MaybeHandle<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 // Enter a try-block while executing the JavaScript code. To avoid
184 // duplicate error printing it must be non-verbose. Also, to avoid
185 // creating message objects during stack overflow we shouldn't
186 // capture messages.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 catcher.SetVerbose(false);
190 catcher.SetCaptureMessage(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000191
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 maybe_result = Call(isolate, callable, receiver, argc, args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 if (maybe_result.is_null()) {
195 DCHECK(catcher.HasCaught());
196 DCHECK(isolate->has_pending_exception());
197 DCHECK(isolate->external_caught_exception());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198 if (isolate->pending_exception() ==
199 isolate->heap()->termination_exception()) {
200 is_termination = true;
201 } else {
202 if (exception_out != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 *exception_out = v8::Utils::OpenHandle(*catcher.Exception());
204 }
205 }
206 isolate->OptionalRescheduleException(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000207 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 DCHECK(!isolate->has_pending_exception());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211
212 // Re-request terminate execution interrupt to trigger later.
213 if (is_termination) isolate->stack_guard()->RequestTerminateExecution();
214
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000216}
217
218
Steve Blocka7e24c12009-10-30 11:49:00 +0000219void StackGuard::SetStackLimit(uintptr_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +0100220 ExecutionAccess access(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100221 // If the current limits are special (e.g. due to a pending interrupt) then
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 // leave them alone.
Ben Murdoch257744e2011-11-30 15:57:28 +0000223 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224 if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
225 thread_local_.set_jslimit(jslimit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000227 if (thread_local_.climit() == thread_local_.real_climit_) {
228 thread_local_.set_climit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 }
Steve Blockd0582a62009-12-15 09:54:21 +0000230 thread_local_.real_climit_ = limit;
231 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000232}
233
234
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235void StackGuard::AdjustStackLimitForSimulator() {
236 ExecutionAccess access(isolate_);
237 uintptr_t climit = thread_local_.real_climit_;
238 // If the current limits are special (e.g. due to a pending interrupt) then
239 // leave them alone.
240 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, climit);
241 if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
242 thread_local_.set_jslimit(jslimit);
243 isolate_->heap()->SetStackLimits();
244 }
245}
246
247
248void StackGuard::EnableInterrupts() {
249 ExecutionAccess access(isolate_);
250 if (has_pending_interrupts(access)) {
251 set_interrupt_limits(access);
252 }
253}
254
255
Steve Blocka7e24c12009-10-30 11:49:00 +0000256void StackGuard::DisableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 reset_limits(access);
259}
260
261
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262void StackGuard::PushPostponeInterruptsScope(PostponeInterruptsScope* scope) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100263 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 // Intercept already requested interrupts.
265 int intercepted = thread_local_.interrupt_flags_ & scope->intercept_mask_;
266 scope->intercepted_flags_ = intercepted;
267 thread_local_.interrupt_flags_ &= ~intercepted;
268 if (!has_pending_interrupts(access)) reset_limits(access);
269 // Add scope to the chain.
270 scope->prev_ = thread_local_.postpone_interrupts_;
271 thread_local_.postpone_interrupts_ = scope;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100272}
273
274
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275void StackGuard::PopPostponeInterruptsScope() {
Steve Block44f0eee2011-05-26 01:26:41 +0100276 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 PostponeInterruptsScope* top = thread_local_.postpone_interrupts_;
278 // Make intercepted interrupts active.
279 DCHECK((thread_local_.interrupt_flags_ & top->intercept_mask_) == 0);
280 thread_local_.interrupt_flags_ |= top->intercepted_flags_;
281 if (has_pending_interrupts(access)) set_interrupt_limits(access);
282 // Remove scope from chain.
283 thread_local_.postpone_interrupts_ = top->prev_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284}
285
286
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287bool StackGuard::CheckInterrupt(InterruptFlag flag) {
Steve Block44f0eee2011-05-26 01:26:41 +0100288 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 return thread_local_.interrupt_flags_ & flag;
290}
291
292
293void StackGuard::RequestInterrupt(InterruptFlag flag) {
294 ExecutionAccess access(isolate_);
295 // Check the chain of PostponeInterruptsScopes for interception.
296 if (thread_local_.postpone_interrupts_ &&
297 thread_local_.postpone_interrupts_->Intercept(flag)) {
298 return;
299 }
300
301 // Not intercepted. Set as active interrupt flag.
302 thread_local_.interrupt_flags_ |= flag;
Steve Block6ded16b2010-05-10 14:33:55 +0100303 set_interrupt_limits(access);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000304
305 // If this isolate is waiting in a futex, notify it to wake up.
306 isolate_->futex_wait_list_node()->NotifyWake();
Steve Blocka7e24c12009-10-30 11:49:00 +0000307}
308
309
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310void StackGuard::ClearInterrupt(InterruptFlag flag) {
Steve Block44f0eee2011-05-26 01:26:41 +0100311 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 // Clear the interrupt flag from the chain of PostponeInterruptsScopes.
313 for (PostponeInterruptsScope* current = thread_local_.postpone_interrupts_;
314 current != NULL;
315 current = current->prev_) {
316 current->intercepted_flags_ &= ~flag;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100317 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318
319 // Clear the interrupt flag from the active interrupt flags.
320 thread_local_.interrupt_flags_ &= ~flag;
321 if (!has_pending_interrupts(access)) reset_limits(access);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100322}
323
324
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100326 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327 bool result = (thread_local_.interrupt_flags_ & flag);
328 thread_local_.interrupt_flags_ &= ~flag;
329 if (!has_pending_interrupts(access)) reset_limits(access);
330 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000331}
332
333
Steve Blocka7e24c12009-10-30 11:49:00 +0000334char* StackGuard::ArchiveStackGuard(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +0100335 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 ThreadLocal blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100338
339 // Set the stack limits using the old thread_local_.
340 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
341 // (as the ctor called SetStackLimits, which looked at the
342 // current thread_local_ from StackGuard)-- but is this
343 // really what was intended?
344 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 thread_local_ = blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 return to + sizeof(ThreadLocal);
348}
349
350
351char* StackGuard::RestoreStackGuard(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +0100352 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Block44f0eee2011-05-26 01:26:41 +0100354 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 return from + sizeof(ThreadLocal);
356}
357
358
Steve Blocka7e24c12009-10-30 11:49:00 +0000359void StackGuard::FreeThreadResources() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000360 Isolate::PerIsolateThreadData* per_thread =
361 isolate_->FindOrAllocatePerThreadDataForThisThread();
362 per_thread->set_stack_limit(thread_local_.real_climit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363}
364
365
366void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000367 real_jslimit_ = kIllegalLimit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000368 set_jslimit(kIllegalLimit);
Steve Blockd0582a62009-12-15 09:54:21 +0000369 real_climit_ = kIllegalLimit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000370 set_climit(kIllegalLimit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 postpone_interrupts_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 interrupt_flags_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000373}
374
375
Ben Murdoch257744e2011-11-30 15:57:28 +0000376bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100377 bool should_set_stack_limits = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000378 if (real_climit_ == kIllegalLimit) {
Steve Block1e0659c2011-05-24 12:43:12 +0100379 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 DCHECK(GetCurrentStackPosition() > kLimitSize);
381 uintptr_t limit = GetCurrentStackPosition() - kLimitSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000382 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
Steve Blockd0582a62009-12-15 09:54:21 +0000384 real_climit_ = limit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 set_climit(limit);
Steve Block44f0eee2011-05-26 01:26:41 +0100386 should_set_stack_limits = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 postpone_interrupts_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 interrupt_flags_ = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100390 return should_set_stack_limits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000391}
392
393
394void StackGuard::ClearThread(const ExecutionAccess& lock) {
395 thread_local_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +0100396 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000397}
398
399
400void StackGuard::InitThread(const ExecutionAccess& lock) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000401 if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
402 Isolate::PerIsolateThreadData* per_thread =
403 isolate_->FindOrAllocatePerThreadDataForThisThread();
404 uintptr_t stored_limit = per_thread->stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 // You should hold the ExecutionAccess lock when you call this.
Steve Block44f0eee2011-05-26 01:26:41 +0100406 if (stored_limit != 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000407 SetStackLimit(stored_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 }
409}
410
411
412// --- C a l l s t o n a t i v e s ---
413
Steve Blocka7e24c12009-10-30 11:49:00 +0000414
Steve Blocka7e24c12009-10-30 11:49:00 +0000415Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
416 Handle<JSFunction> fun,
417 Handle<Object> pos,
418 Handle<Object> is_global) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100419 Isolate* isolate = fun->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100420 Handle<Object> args[] = { recv, fun, pos, is_global };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 MaybeHandle<Object> maybe_result =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000422 TryCall(isolate, isolate->get_stack_trace_line_fun(),
423 isolate->factory()->undefined_value(), arraysize(args), args);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424 Handle<Object> result;
425 if (!maybe_result.ToHandle(&result) || !result->IsString()) {
426 return isolate->factory()->empty_string();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100427 }
428
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 return Handle<String>::cast(result);
430}
431
432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433void StackGuard::HandleGCInterrupt() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 if (CheckAndClearInterrupt(GC_REQUEST)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 isolate_->heap()->HandleGCRequest();
436 }
437}
438
439
440Object* StackGuard::HandleInterrupts() {
441 if (FLAG_verify_predictable) {
442 // Advance synthetic time by making a time request.
443 isolate_->heap()->MonotonicallyIncreasingTimeInMs();
444 }
445
446 if (CheckAndClearInterrupt(GC_REQUEST)) {
447 isolate_->heap()->HandleGCRequest();
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000449
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 if (CheckDebugBreak() || CheckDebugCommand()) {
451 isolate_->debug()->HandleDebugBreak();
452 }
453
454 if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) {
455 return isolate_->TerminateExecution();
456 }
457
458 if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) {
459 isolate_->heap()->DeoptMarkedAllocationSites();
460 }
461
462 if (CheckAndClearInterrupt(INSTALL_CODE)) {
463 DCHECK(isolate_->concurrent_recompilation_enabled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000464 isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 }
466
467 if (CheckAndClearInterrupt(API_INTERRUPT)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400468 // Callbacks must be invoked outside of ExecusionAccess lock.
469 isolate_->InvokeApiInterruptCallbacks();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 }
471
472 isolate_->counters()->stack_interrupts()->Increment();
473 isolate_->counters()->runtime_profiler_ticks()->Increment();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100474 isolate_->runtime_profiler()->MarkCandidatesForOptimization();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475
476 return isolate_->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000477}
478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479} // namespace internal
480} // namespace v8