#include "src/execution/execution.h"
#include "src/api/api-inl.h"
#include "src/debug/debug.h"
#include "src/execution/frames.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/vm-state-inl.h"
#include "src/logging/runtime-call-stats-scope.h"
#if V8_ENABLE_WEBASSEMBLY
#include "src/compiler/wasm-compiler.h"
#include "src/wasm/wasm-engine.h"
#endif
namespace v8 {
namespace internal {
namespace {
Handle<Object> NormalizeReceiver(Isolate* isolate, Handle<Object> receiver) {
if (receiver->IsJSGlobalObject()) {
return handle(Handle<JSGlobalObject>::cast(receiver)->global_proxy(),
isolate);
}
return receiver;
}
struct InvokeParams {
static InvokeParams SetUpForNew(Isolate* isolate, Handle<Object> constructor,
Handle<Object> new_target, int argc,
Handle<Object>* argv);
static InvokeParams SetUpForCall(Isolate* isolate, Handle<Object> callable,
Handle<Object> receiver, int argc,
Handle<Object>* argv);
static InvokeParams SetUpForTryCall(
Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
int argc, Handle<Object>* argv,
Execution::MessageHandling message_handling,
MaybeHandle<Object>* exception_out, bool reschedule_terminate);
static InvokeParams SetUpForRunMicrotasks(Isolate* isolate,
MicrotaskQueue* microtask_queue,
MaybeHandle<Object>* exception_out);
bool IsScript() const {
if (!target->IsJSFunction()) return false;
Handle<JSFunction> function = Handle<JSFunction>::cast(target);
return function->shared().is_script();
}
Handle<FixedArray> GetAndResetHostDefinedOptions() {
DCHECK(IsScript());
DCHECK_EQ(argc, 1);
auto options = Handle<FixedArray>::cast(argv[0]);
argv = nullptr;
argc = 0;
return options;
}
Handle<Object> target;
Handle<Object> receiver;
int argc;
Handle<Object>* argv;
Handle<Object> new_target;
MicrotaskQueue* microtask_queue;
Execution::MessageHandling message_handling;
MaybeHandle<Object>* exception_out;
bool is_construct;
Execution::Target execution_target;
bool reschedule_terminate;
};
InvokeParams InvokeParams::SetUpForNew(Isolate* isolate,
Handle<Object> constructor,
Handle<Object> new_target, int argc,
Handle<Object>* argv) {
InvokeParams params;
params.target = constructor;
params.receiver = isolate->factory()->undefined_value();
DCHECK(!params.IsScript());
params.argc = argc;
params.argv = argv;
params.new_target = new_target;
params.microtask_queue = nullptr;
params.message_handling = Execution::MessageHandling::kReport;
params.exception_out = nullptr;
params.is_construct = true;
params.execution_target = Execution::Target::kCallable;
params.reschedule_terminate = true;
return params;
}
InvokeParams InvokeParams::SetUpForCall(Isolate* isolate,
Handle<Object> callable,
Handle<Object> receiver, int argc,
Handle<Object>* argv) {
InvokeParams params;
params.target = callable;
params.receiver = NormalizeReceiver(isolate, receiver);
DCHECK_IMPLIES(params.IsScript(), argc == 1);
DCHECK_IMPLIES(params.IsScript(), argv[0]->IsFixedArray());
params.argc = argc;
params.argv = argv;
params.new_target = isolate->factory()->undefined_value();
params.microtask_queue = nullptr;
params.message_handling = Execution::MessageHandling::kReport;
params.exception_out = nullptr;
params.is_construct = false;
params.execution_target = Execution::Target::kCallable;
params.reschedule_terminate = true;
return params;
}
InvokeParams InvokeParams::SetUpForTryCall(
Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
int argc, Handle<Object>* argv, Execution::MessageHandling message_handling,
MaybeHandle<Object>* exception_out, bool reschedule_terminate) {
InvokeParams params;
params.target = callable;
params.receiver = NormalizeReceiver(isolate, receiver);
DCHECK_IMPLIES(params.IsScript(), argc == 1);
DCHECK_IMPLIES(params.IsScript(), argv[0]->IsFixedArray());
params.argc = argc;
params.argv = argv;
params.new_target = isolate->factory()->undefined_value();
params.microtask_queue = nullptr;
params.message_handling = message_handling;
params.exception_out = exception_out;
params.is_construct = false;
params.execution_target = Execution::Target::kCallable;
params.reschedule_terminate = reschedule_terminate;
return params;
}
InvokeParams InvokeParams::SetUpForRunMicrotasks(
Isolate* isolate, MicrotaskQueue* microtask_queue,
MaybeHandle<Object>* exception_out) {
auto undefined = isolate->factory()->undefined_value();
InvokeParams params;
params.target = undefined;
params.receiver = undefined;
params.argc = 0;
params.argv = nullptr;
params.new_target = undefined;
params.microtask_queue = microtask_queue;
params.message_handling = Execution::MessageHandling::kReport;
params.exception_out = exception_out;
params.is_construct = false;
params.execution_target = Execution::Target::kRunMicrotasks;
params.reschedule_terminate = true;
return params;
}
Handle<CodeT> JSEntry(Isolate* isolate, Execution::Target execution_target,
bool is_construct) {
if (is_construct) {
DCHECK_EQ(Execution::Target::kCallable, execution_target);
return BUILTIN_CODE(isolate, JSConstructEntry);
} else if (execution_target == Execution::Target::kCallable) {
DCHECK(!is_construct);
return BUILTIN_CODE(isolate, JSEntry);
} else if (execution_target == Execution::Target::kRunMicrotasks) {
DCHECK(!is_construct);
return BUILTIN_CODE(isolate, JSRunMicrotasksEntry);
}
UNREACHABLE();
}
MaybeHandle<Context> NewScriptContext(Isolate* isolate,
Handle<JSFunction> function,
Handle<FixedArray> host_defined_options) {
if (isolate->debug_execution_mode() == DebugInfo::kSideEffects) {
isolate->Throw(*isolate->factory()->NewEvalError(
MessageTemplate::kNoSideEffectDebugEvaluate));
return MaybeHandle<Context>();
}
SaveAndSwitchContext save(isolate, function->context());
SharedFunctionInfo sfi = function->shared();
Handle<Script> script(Script::cast(sfi.script()), isolate);
Handle<ScopeInfo> scope_info(sfi.scope_info(), isolate);
Handle<NativeContext> native_context(NativeContext::cast(function->context()),
isolate);
Handle<JSGlobalObject> global_object(native_context->global_object(),
isolate);
Handle<ScriptContextTable> script_context(
native_context->script_context_table(), isolate);
for (auto it : ScopeInfo::IterateLocalNames(scope_info)) {
Handle<String> name(it->name(), isolate);
VariableMode mode = scope_info->ContextLocalMode(it->index());
VariableLookupResult lookup;
if (ScriptContextTable::Lookup(isolate, *script_context, *name, &lookup)) {
if (IsLexicalVariableMode(mode) || IsLexicalVariableMode(lookup.mode)) {
Handle<Context> context = ScriptContextTable::GetContext(
isolate, script_context, lookup.context_index);
if (!(((mode == VariableMode::kLet &&
lookup.mode == VariableMode::kLet) ||
(mode == VariableMode::kConst &&
lookup.mode == VariableMode::kConst)) &&
scope_info->IsReplModeScope() &&
context->scope_info().IsReplModeScope())) {
MessageLocation location(script, 0, 1);
return isolate->ThrowAt<Context>(
isolate->factory()->NewSyntaxError(
MessageTemplate::kVarRedeclaration, name),
&location);
}
}
}
if (IsLexicalVariableMode(mode)) {
LookupIterator it(isolate, global_object, name, global_object,
LookupIterator::OWN_SKIP_INTERCEPTOR);
Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
CHECK(!maybe.IsNothing());
if ((maybe.FromJust() & DONT_DELETE) != 0) {
MessageLocation location(script, 0, 1);
return isolate->ThrowAt<Context>(
isolate->factory()->NewSyntaxError(
MessageTemplate::kVarRedeclaration, name),
&location);
}
JSGlobalObject::InvalidatePropertyCell(global_object, name);
}
}
Handle<Context> result =
isolate->factory()->NewScriptContext(native_context, scope_info);
result->Initialize(isolate);
Handle<ScriptContextTable> new_script_context_table =
ScriptContextTable::Extend(script_context, result);
native_context->synchronized_set_script_context_table(
*new_script_context_table);
return result;
}
V8_WARN_UNUSED_RESULT MaybeHandle<Object> Invoke(Isolate* isolate,
const InvokeParams& params) {
RCS_SCOPE(isolate, RuntimeCallCounterId::kInvoke);
DCHECK(!params.receiver->IsJSGlobalObject());
DCHECK_LE(params.argc, FixedArray::kMaxLength);
#if V8_ENABLE_WEBASSEMBLY
DCHECK_IMPLIES(wasm::GetWasmCodeManager()->HasMemoryProtectionKeySupport(),
!wasm::GetWasmCodeManager()->MemoryProtectionKeyWritable());
#endif
#ifdef USE_SIMULATOR
StackLimitCheck check(isolate);
if (check.HasOverflowed()) {
isolate->StackOverflow();
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->ReportPendingMessages();
}
return MaybeHandle<Object>();
}
#endif
if (params.target->IsJSFunction()) {
Handle<JSFunction> function = Handle<JSFunction>::cast(params.target);
if ((!params.is_construct || function->IsConstructor()) &&
function->shared().IsApiFunction() &&
!function->shared().BreakAtEntry()) {
SaveAndSwitchContext save(isolate, function->context());
DCHECK(function->context().global_object().IsJSGlobalObject());
Handle<Object> receiver = params.is_construct
? isolate->factory()->the_hole_value()
: params.receiver;
auto value = Builtins::InvokeApiFunction(
isolate, params.is_construct, function, receiver, params.argc,
params.argv, Handle<HeapObject>::cast(params.new_target));
bool has_exception = value.is_null();
DCHECK(has_exception == isolate->has_pending_exception());
if (has_exception) {
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->ReportPendingMessages();
}
return MaybeHandle<Object>();
} else {
isolate->clear_pending_message();
}
return value;
}
#ifdef DEBUG
if (function->shared().is_script()) {
DCHECK(params.IsScript());
DCHECK(params.receiver->IsJSGlobalProxy());
DCHECK_EQ(params.argc, 1);
DCHECK(params.argv[0]->IsFixedArray());
} else {
DCHECK(!params.IsScript());
}
#endif
if (function->shared().needs_script_context()) {
Handle<Context> context;
Handle<FixedArray> host_defined_options =
const_cast<InvokeParams&>(params).GetAndResetHostDefinedOptions();
if (!NewScriptContext(isolate, function, host_defined_options)
.ToHandle(&context)) {
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->ReportPendingMessages();
}
return MaybeHandle<Object>();
}
function->set_context(*context);
}
}
VMState<JS> state(isolate);
CHECK(AllowJavascriptExecution::IsAllowed(isolate));
if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
isolate->ThrowIllegalOperation();
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->ReportPendingMessages();
}
return MaybeHandle<Object>();
}
if (!DumpOnJavascriptExecution::IsAllowed(isolate)) {
V8::GetCurrentPlatform()->DumpWithoutCrashing();
return isolate->factory()->undefined_value();
}
if (params.execution_target == Execution::Target::kCallable) {
Handle<Context> context = isolate->native_context();
if (!context->script_execution_callback().IsUndefined(isolate)) {
v8::Context::AbortScriptExecutionCallback callback =
v8::ToCData<v8::Context::AbortScriptExecutionCallback>(
context->script_execution_callback());
v8::Isolate* api_isolate = reinterpret_cast<v8::Isolate*>(isolate);
v8::Local<v8::Context> api_context = v8::Utils::ToLocal(context);
callback(api_isolate, api_context);
DCHECK(!isolate->has_scheduled_exception());
isolate->ThrowIllegalOperation();
return MaybeHandle<Object>();
}
}
Object value;
Handle<CodeT> code =
JSEntry(isolate, params.execution_target, params.is_construct);
{
SaveContext save(isolate);
SealHandleScope shs(isolate);
if (FLAG_clear_exceptions_on_js_entry) isolate->clear_pending_exception();
if (params.execution_target == Execution::Target::kCallable) {
using JSEntryFunction = GeneratedCode<Address(
Address root_register_value, Address new_target, Address target,
Address receiver, intptr_t argc, Address** argv)>;
JSEntryFunction stub_entry =
JSEntryFunction::FromAddress(isolate, code->InstructionStart());
Address orig_func = params.new_target->ptr();
Address func = params.target->ptr();
Address recv = params.receiver->ptr();
Address** argv = reinterpret_cast<Address**>(params.argv);
RCS_SCOPE(isolate, RuntimeCallCounterId::kJS_Execution);
value = Object(stub_entry.Call(isolate->isolate_data()->isolate_root(),
orig_func, func, recv,
JSParameterCount(params.argc), argv));
} else {
DCHECK_EQ(Execution::Target::kRunMicrotasks, params.execution_target);
using JSEntryFunction = GeneratedCode<Address(
Address root_register_value, MicrotaskQueue* microtask_queue)>;
JSEntryFunction stub_entry =
JSEntryFunction::FromAddress(isolate, code->InstructionStart());
RCS_SCOPE(isolate, RuntimeCallCounterId::kJS_Execution);
value = Object(stub_entry.Call(isolate->isolate_data()->isolate_root(),
params.microtask_queue));
}
}
#ifdef VERIFY_HEAP
if (FLAG_verify_heap) {
value.ObjectVerify(isolate);
}
#endif
bool has_exception = value.IsException(isolate);
DCHECK(has_exception == isolate->has_pending_exception());
if (has_exception) {
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->ReportPendingMessages();
}
return MaybeHandle<Object>();
} else {
isolate->clear_pending_message();
}
return Handle<Object>(value, isolate);
}
MaybeHandle<Object> InvokeWithTryCatch(Isolate* isolate,
const InvokeParams& params) {
bool is_termination = false;
MaybeHandle<Object> maybe_result;
if (params.exception_out != nullptr) {
*params.exception_out = MaybeHandle<Object>();
}
DCHECK_IMPLIES(
params.message_handling == Execution::MessageHandling::kKeepPending,
params.exception_out == nullptr);
{
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
catcher.SetVerbose(false);
catcher.SetCaptureMessage(false);
maybe_result = Invoke(isolate, params);
if (maybe_result.is_null()) {
DCHECK(isolate->has_pending_exception());
if (isolate->pending_exception() ==
ReadOnlyRoots(isolate).termination_exception()) {
is_termination = true;
} else {
if (params.exception_out != nullptr) {
DCHECK(catcher.HasCaught());
DCHECK(isolate->external_caught_exception());
*params.exception_out = v8::Utils::OpenHandle(*catcher.Exception());
}
if (params.message_handling == Execution::MessageHandling::kReport) {
isolate->OptionalRescheduleException(true);
}
}
}
}
if (is_termination && params.reschedule_terminate) {
isolate->OptionalRescheduleException(false);
}
return maybe_result;
}
}
MaybeHandle<Object> Execution::Call(Isolate* isolate, Handle<Object> callable,
Handle<Object> receiver, int argc,
Handle<Object> argv[]) {
DCHECK_IMPLIES(callable->IsJSFunction(),
!JSFunction::cast(*callable).shared().is_script());
return Invoke(isolate, InvokeParams::SetUpForCall(isolate, callable, receiver,
argc, argv));
}
MaybeHandle<Object> Execution::CallScript(Isolate* isolate,
Handle<JSFunction> script_function,
Handle<Object> receiver,
Handle<Object> host_defined_options) {
DCHECK(script_function->shared().is_script());
DCHECK(receiver->IsJSGlobalProxy() || receiver->IsJSGlobalObject());
return Invoke(
isolate, InvokeParams::SetUpForCall(isolate, script_function, receiver, 1,
&host_defined_options));
}
MaybeHandle<Object> Execution::CallBuiltin(Isolate* isolate,
Handle<JSFunction> builtin,
Handle<Object> receiver, int argc,
Handle<Object> argv[]) {
DCHECK(builtin->code().is_builtin());
DisableBreak no_break(isolate->debug());
return Invoke(isolate, InvokeParams::SetUpForCall(isolate, builtin, receiver,
argc, argv));
}
MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
int argc, Handle<Object> argv[]) {
return New(isolate, constructor, constructor, argc, argv);
}
MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
Handle<Object> new_target, int argc,
Handle<Object> argv[]) {
return Invoke(isolate, InvokeParams::SetUpForNew(isolate, constructor,
new_target, argc, argv));
}
MaybeHandle<Object> Execution::TryCallScript(
Isolate* isolate, Handle<JSFunction> script_function,
Handle<Object> receiver, Handle<FixedArray> host_defined_options,
MessageHandling message_handling, MaybeHandle<Object>* exception_out,
bool reschedule_terminate) {
DCHECK(script_function->shared().is_script());
DCHECK(receiver->IsJSGlobalProxy() || receiver->IsJSGlobalObject());
Handle<Object> argument = host_defined_options;
return InvokeWithTryCatch(
isolate, InvokeParams::SetUpForTryCall(
isolate, script_function, receiver, 1, &argument,
message_handling, exception_out, reschedule_terminate));
}
MaybeHandle<Object> Execution::TryCall(
Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
int argc, Handle<Object> argv[], MessageHandling message_handling,
MaybeHandle<Object>* exception_out, bool reschedule_terminate) {
DCHECK_IMPLIES(callable->IsJSFunction(),
!JSFunction::cast(*callable).shared().is_script());
return InvokeWithTryCatch(
isolate, InvokeParams::SetUpForTryCall(
isolate, callable, receiver, argc, argv, message_handling,
exception_out, reschedule_terminate));
}
MaybeHandle<Object> Execution::TryRunMicrotasks(
Isolate* isolate, MicrotaskQueue* microtask_queue,
MaybeHandle<Object>* exception_out) {
return InvokeWithTryCatch(
isolate, InvokeParams::SetUpForRunMicrotasks(isolate, microtask_queue,
exception_out));
}
struct StackHandlerMarker {
Address next;
Address padding;
};
STATIC_ASSERT(offsetof(StackHandlerMarker, next) ==
StackHandlerConstants::kNextOffset);
STATIC_ASSERT(offsetof(StackHandlerMarker, padding) ==
StackHandlerConstants::kPaddingOffset);
STATIC_ASSERT(sizeof(StackHandlerMarker) == StackHandlerConstants::kSize);
#if V8_ENABLE_WEBASSEMBLY
void Execution::CallWasm(Isolate* isolate, Handle<CodeT> wrapper_code,
Address wasm_call_target, Handle<Object> object_ref,
Address packed_args) {
using WasmEntryStub = GeneratedCode<Address(
Address target, Address object_ref, Address argv, Address c_entry_fp)>;
WasmEntryStub stub_entry =
WasmEntryStub::FromAddress(isolate, wrapper_code->InstructionStart());
SaveContext save(isolate);
SealHandleScope shs(isolate);
Address saved_c_entry_fp = *isolate->c_entry_fp_address();
Address saved_js_entry_sp = *isolate->js_entry_sp_address();
if (saved_js_entry_sp == kNullAddress) {
*isolate->js_entry_sp_address() = GetCurrentStackPosition();
}
StackHandlerMarker stack_handler;
stack_handler.next = isolate->thread_local_top()->handler_;
#ifdef V8_USE_ADDRESS_SANITIZER
stack_handler.padding = GetCurrentStackPosition();
#else
stack_handler.padding = 0;
#endif
isolate->thread_local_top()->handler_ =
reinterpret_cast<Address>(&stack_handler);
trap_handler::SetThreadInWasm();
{
RCS_SCOPE(isolate, RuntimeCallCounterId::kJS_Execution);
STATIC_ASSERT(compiler::CWasmEntryParameters::kCodeEntry == 0);
STATIC_ASSERT(compiler::CWasmEntryParameters::kObjectRef == 1);
STATIC_ASSERT(compiler::CWasmEntryParameters::kArgumentsBuffer == 2);
STATIC_ASSERT(compiler::CWasmEntryParameters::kCEntryFp == 3);
Address result = stub_entry.Call(wasm_call_target, object_ref->ptr(),
packed_args, saved_c_entry_fp);
if (result != kNullAddress) {
isolate->set_pending_exception(Object(result));
}
}
if (trap_handler::IsThreadInWasm()) {
trap_handler::ClearThreadInWasm();
}
isolate->thread_local_top()->handler_ = stack_handler.next;
if (saved_js_entry_sp == kNullAddress) {
*isolate->js_entry_sp_address() = saved_js_entry_sp;
}
*isolate->c_entry_fp_address() = saved_c_entry_fp;
}
#endif
}
}