blob: 243bb8a9117ae5998384b1146c21e8a0ec4f923e [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 Murdoch61f157c2016-09-16 13:49:30 +0100109 bool has_exception = value->IsException(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 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());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100143 auto value =
144 Builtins::InvokeApiFunction(isolate, function, receiver, argc, argv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 bool has_exception = value.is_null();
146 DCHECK(has_exception == isolate->has_pending_exception());
147 if (has_exception) {
148 isolate->ReportPendingMessages();
149 return MaybeHandle<Object>();
150 } else {
151 isolate->clear_pending_message();
152 }
153 return value;
Ben Murdoch589d6972011-11-30 16:04:58 +0000154 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 return Invoke(isolate, false, callable, receiver, argc, argv,
156 isolate->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000157}
158
159
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160// static
161MaybeHandle<Object> Execution::New(Handle<JSFunction> constructor, int argc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 Handle<Object> argv[]) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 return New(constructor->GetIsolate(), constructor, constructor, argc, argv);
Steve Blocka7e24c12009-10-30 11:49:00 +0000164}
165
166
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167// static
168MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
169 Handle<Object> new_target, int argc,
170 Handle<Object> argv[]) {
171 return Invoke(isolate, true, constructor,
172 isolate->factory()->undefined_value(), argc, argv, new_target);
173}
174
175
176MaybeHandle<Object> Execution::TryCall(Isolate* isolate,
177 Handle<Object> callable,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 Handle<Object> receiver, int argc,
179 Handle<Object> args[],
180 MaybeHandle<Object>* exception_out) {
181 bool is_termination = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 MaybeHandle<Object> maybe_result;
183 if (exception_out != NULL) *exception_out = MaybeHandle<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 // Enter a try-block while executing the JavaScript code. To avoid
185 // duplicate error printing it must be non-verbose. Also, to avoid
186 // creating message objects during stack overflow we shouldn't
187 // capture messages.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 catcher.SetVerbose(false);
191 catcher.SetCaptureMessage(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000192
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 maybe_result = Call(isolate, callable, receiver, argc, args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 if (maybe_result.is_null()) {
196 DCHECK(catcher.HasCaught());
197 DCHECK(isolate->has_pending_exception());
198 DCHECK(isolate->external_caught_exception());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 if (isolate->pending_exception() ==
200 isolate->heap()->termination_exception()) {
201 is_termination = true;
202 } else {
203 if (exception_out != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 *exception_out = v8::Utils::OpenHandle(*catcher.Exception());
205 }
206 }
207 isolate->OptionalRescheduleException(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000209
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 DCHECK(!isolate->has_pending_exception());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212
213 // Re-request terminate execution interrupt to trigger later.
214 if (is_termination) isolate->stack_guard()->RequestTerminateExecution();
215
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000217}
218
219
Steve Blocka7e24c12009-10-30 11:49:00 +0000220void StackGuard::SetStackLimit(uintptr_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +0100221 ExecutionAccess access(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100222 // If the current limits are special (e.g. due to a pending interrupt) then
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 // leave them alone.
Ben Murdoch257744e2011-11-30 15:57:28 +0000224 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000225 if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
226 thread_local_.set_jslimit(jslimit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228 if (thread_local_.climit() == thread_local_.real_climit_) {
229 thread_local_.set_climit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 }
Steve Blockd0582a62009-12-15 09:54:21 +0000231 thread_local_.real_climit_ = limit;
232 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000233}
234
235
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000236void StackGuard::AdjustStackLimitForSimulator() {
237 ExecutionAccess access(isolate_);
238 uintptr_t climit = thread_local_.real_climit_;
239 // If the current limits are special (e.g. due to a pending interrupt) then
240 // leave them alone.
241 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, climit);
242 if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
243 thread_local_.set_jslimit(jslimit);
244 isolate_->heap()->SetStackLimits();
245 }
246}
247
248
249void StackGuard::EnableInterrupts() {
250 ExecutionAccess access(isolate_);
251 if (has_pending_interrupts(access)) {
252 set_interrupt_limits(access);
253 }
254}
255
256
Steve Blocka7e24c12009-10-30 11:49:00 +0000257void StackGuard::DisableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100258 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 reset_limits(access);
260}
261
262
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263void StackGuard::PushPostponeInterruptsScope(PostponeInterruptsScope* scope) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100264 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 // Intercept already requested interrupts.
266 int intercepted = thread_local_.interrupt_flags_ & scope->intercept_mask_;
267 scope->intercepted_flags_ = intercepted;
268 thread_local_.interrupt_flags_ &= ~intercepted;
269 if (!has_pending_interrupts(access)) reset_limits(access);
270 // Add scope to the chain.
271 scope->prev_ = thread_local_.postpone_interrupts_;
272 thread_local_.postpone_interrupts_ = scope;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100273}
274
275
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276void StackGuard::PopPostponeInterruptsScope() {
Steve Block44f0eee2011-05-26 01:26:41 +0100277 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 PostponeInterruptsScope* top = thread_local_.postpone_interrupts_;
279 // Make intercepted interrupts active.
280 DCHECK((thread_local_.interrupt_flags_ & top->intercept_mask_) == 0);
281 thread_local_.interrupt_flags_ |= top->intercepted_flags_;
282 if (has_pending_interrupts(access)) set_interrupt_limits(access);
283 // Remove scope from chain.
284 thread_local_.postpone_interrupts_ = top->prev_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000285}
286
287
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288bool StackGuard::CheckInterrupt(InterruptFlag flag) {
Steve Block44f0eee2011-05-26 01:26:41 +0100289 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 return thread_local_.interrupt_flags_ & flag;
291}
292
293
294void StackGuard::RequestInterrupt(InterruptFlag flag) {
295 ExecutionAccess access(isolate_);
296 // Check the chain of PostponeInterruptsScopes for interception.
297 if (thread_local_.postpone_interrupts_ &&
298 thread_local_.postpone_interrupts_->Intercept(flag)) {
299 return;
300 }
301
302 // Not intercepted. Set as active interrupt flag.
303 thread_local_.interrupt_flags_ |= flag;
Steve Block6ded16b2010-05-10 14:33:55 +0100304 set_interrupt_limits(access);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305
306 // If this isolate is waiting in a futex, notify it to wake up.
307 isolate_->futex_wait_list_node()->NotifyWake();
Steve Blocka7e24c12009-10-30 11:49:00 +0000308}
309
310
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311void StackGuard::ClearInterrupt(InterruptFlag flag) {
Steve Block44f0eee2011-05-26 01:26:41 +0100312 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 // Clear the interrupt flag from the chain of PostponeInterruptsScopes.
314 for (PostponeInterruptsScope* current = thread_local_.postpone_interrupts_;
315 current != NULL;
316 current = current->prev_) {
317 current->intercepted_flags_ &= ~flag;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100318 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319
320 // Clear the interrupt flag from the active interrupt flags.
321 thread_local_.interrupt_flags_ &= ~flag;
322 if (!has_pending_interrupts(access)) reset_limits(access);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100323}
324
325
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100327 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 bool result = (thread_local_.interrupt_flags_ & flag);
329 thread_local_.interrupt_flags_ &= ~flag;
330 if (!has_pending_interrupts(access)) reset_limits(access);
331 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000332}
333
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335char* StackGuard::ArchiveStackGuard(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +0100336 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 ThreadLocal blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100339
340 // Set the stack limits using the old thread_local_.
341 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
342 // (as the ctor called SetStackLimits, which looked at the
343 // current thread_local_ from StackGuard)-- but is this
344 // really what was intended?
345 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 thread_local_ = blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100347
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 return to + sizeof(ThreadLocal);
349}
350
351
352char* StackGuard::RestoreStackGuard(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +0100353 ExecutionAccess access(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354 MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Block44f0eee2011-05-26 01:26:41 +0100355 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 return from + sizeof(ThreadLocal);
357}
358
359
Steve Blocka7e24c12009-10-30 11:49:00 +0000360void StackGuard::FreeThreadResources() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000361 Isolate::PerIsolateThreadData* per_thread =
362 isolate_->FindOrAllocatePerThreadDataForThisThread();
363 per_thread->set_stack_limit(thread_local_.real_climit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364}
365
366
367void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000368 real_jslimit_ = kIllegalLimit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 set_jslimit(kIllegalLimit);
Steve Blockd0582a62009-12-15 09:54:21 +0000370 real_climit_ = kIllegalLimit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000371 set_climit(kIllegalLimit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 postpone_interrupts_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 interrupt_flags_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000374}
375
376
Ben Murdoch257744e2011-11-30 15:57:28 +0000377bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100378 bool should_set_stack_limits = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000379 if (real_climit_ == kIllegalLimit) {
Steve Block1e0659c2011-05-24 12:43:12 +0100380 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 DCHECK(GetCurrentStackPosition() > kLimitSize);
382 uintptr_t limit = GetCurrentStackPosition() - kLimitSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000383 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
Steve Blockd0582a62009-12-15 09:54:21 +0000385 real_climit_ = limit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 set_climit(limit);
Steve Block44f0eee2011-05-26 01:26:41 +0100387 should_set_stack_limits = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 postpone_interrupts_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 interrupt_flags_ = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100391 return should_set_stack_limits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000392}
393
394
395void StackGuard::ClearThread(const ExecutionAccess& lock) {
396 thread_local_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +0100397 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000398}
399
400
401void StackGuard::InitThread(const ExecutionAccess& lock) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000402 if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
403 Isolate::PerIsolateThreadData* per_thread =
404 isolate_->FindOrAllocatePerThreadDataForThisThread();
405 uintptr_t stored_limit = per_thread->stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 // You should hold the ExecutionAccess lock when you call this.
Steve Block44f0eee2011-05-26 01:26:41 +0100407 if (stored_limit != 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000408 SetStackLimit(stored_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 }
410}
411
412
413// --- C a l l s t o n a t i v e s ---
414
Steve Blocka7e24c12009-10-30 11:49:00 +0000415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
417 Handle<JSFunction> fun,
418 Handle<Object> pos,
419 Handle<Object> is_global) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100420 Isolate* isolate = fun->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100421 Handle<Object> args[] = { recv, fun, pos, is_global };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 MaybeHandle<Object> maybe_result =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 TryCall(isolate, isolate->get_stack_trace_line_fun(),
424 isolate->factory()->undefined_value(), arraysize(args), args);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 Handle<Object> result;
426 if (!maybe_result.ToHandle(&result) || !result->IsString()) {
427 return isolate->factory()->empty_string();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100428 }
429
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 return Handle<String>::cast(result);
431}
432
433
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434void StackGuard::HandleGCInterrupt() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 if (CheckAndClearInterrupt(GC_REQUEST)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436 isolate_->heap()->HandleGCRequest();
437 }
438}
439
440
441Object* StackGuard::HandleInterrupts() {
442 if (FLAG_verify_predictable) {
443 // Advance synthetic time by making a time request.
444 isolate_->heap()->MonotonicallyIncreasingTimeInMs();
445 }
446
447 if (CheckAndClearInterrupt(GC_REQUEST)) {
448 isolate_->heap()->HandleGCRequest();
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 if (CheckDebugBreak() || CheckDebugCommand()) {
452 isolate_->debug()->HandleDebugBreak();
453 }
454
455 if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) {
456 return isolate_->TerminateExecution();
457 }
458
459 if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) {
460 isolate_->heap()->DeoptMarkedAllocationSites();
461 }
462
463 if (CheckAndClearInterrupt(INSTALL_CODE)) {
464 DCHECK(isolate_->concurrent_recompilation_enabled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 }
467
468 if (CheckAndClearInterrupt(API_INTERRUPT)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400469 // Callbacks must be invoked outside of ExecusionAccess lock.
470 isolate_->InvokeApiInterruptCallbacks();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 }
472
473 isolate_->counters()->stack_interrupts()->Increment();
474 isolate_->counters()->runtime_profiler_ticks()->Increment();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100475 isolate_->runtime_profiler()->MarkCandidatesForOptimization();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476
477 return isolate_->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480} // namespace internal
481} // namespace v8