From c5eb9573e74471b6aa3bd0df049a6113073f6a60 Mon Sep 17 00:00:00 2001 From: horo Date: Mon, 18 May 2015 20:11:33 -0700 Subject: [PATCH] [V8] Added Script::is_opaque flag for embedders When the page is controlled by a ServiceWorker, the ServiceWorker can return an opaque (non-CORS cross origin) resource response. We need to treat the messages from such script resource as opaque. Committed: https://crrev.com/7a599c5e1242d3c5ab7515ee149623da90ae69ec Cr-Commit-Position: refs/heads/master@{#28445} Review URL: https://codereview.chromium.org/1140673002 Cr-Commit-Position: refs/heads/master@{#28459} --- include/v8.h | 83 +++++++++++++++++++++++++----------- src/accessors.cc | 5 ++- src/api.cc | 46 +++++++++----------- src/bootstrapper.cc | 11 ++--- src/compilation-cache.cc | 29 +++++-------- src/compilation-cache.h | 10 ++--- src/compiler.cc | 14 +++--- src/compiler.h | 2 +- src/debug.cc | 5 ++- src/objects-inl.h | 12 ++++-- src/objects.h | 22 ++++------ test/cctest/compiler/test-linkage.cc | 4 +- test/cctest/test-api.cc | 31 ++++++++------ test/cctest/test-compiler.cc | 4 +- test/cctest/test-heap.cc | 33 +++++++++----- test/cctest/test-serialize.cc | 4 +- 16 files changed, 176 insertions(+), 139 deletions(-) diff --git a/include/v8.h b/include/v8.h index 4bef472..d2d0da0 100644 --- a/include/v8.h +++ b/include/v8.h @@ -973,6 +973,38 @@ class V8_EXPORT Data { /** + * The optional attributes of ScriptOrigin. + */ +class ScriptOriginOptions { + public: + V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false, + bool is_shared_cross_origin = false, + bool is_opaque = false) + : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) | + (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) | + (is_opaque ? kIsOpaque : 0)) {} + V8_INLINE ScriptOriginOptions(int flags) + : flags_(flags & + (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {} + bool IsEmbedderDebugScript() const { + return (flags_ & kIsEmbedderDebugScript) != 0; + } + bool IsSharedCrossOrigin() const { + return (flags_ & kIsSharedCrossOrigin) != 0; + } + bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; } + int Flags() const { return flags_; } + + private: + enum { + kIsEmbedderDebugScript = 1, + kIsSharedCrossOrigin = 1 << 1, + kIsOpaque = 1 << 2 + }; + const int flags_; +}; + +/** * The origin, within a file, of a script. */ class ScriptOrigin { @@ -984,31 +1016,23 @@ class ScriptOrigin { Handle resource_is_shared_cross_origin = Handle(), Handle script_id = Handle(), Handle resource_is_embedder_debug_script = Handle(), - Handle source_map_url = Handle()) - : resource_name_(resource_name), - resource_line_offset_(resource_line_offset), - resource_column_offset_(resource_column_offset), - resource_is_embedder_debug_script_(resource_is_embedder_debug_script), - resource_is_shared_cross_origin_(resource_is_shared_cross_origin), - script_id_(script_id), - source_map_url_(source_map_url) {} + Handle source_map_url = Handle(), + Handle resource_is_opaque = Handle()); V8_INLINE Handle ResourceName() const; V8_INLINE Handle ResourceLineOffset() const; V8_INLINE Handle ResourceColumnOffset() const; /** * Returns true for embedder's debugger scripts */ - V8_INLINE Handle ResourceIsEmbedderDebugScript() const; - V8_INLINE Handle ResourceIsSharedCrossOrigin() const; V8_INLINE Handle ScriptID() const; V8_INLINE Handle SourceMapUrl() const; + V8_INLINE ScriptOriginOptions Options() const { return options_; } private: Handle resource_name_; Handle resource_line_offset_; Handle resource_column_offset_; - Handle resource_is_embedder_debug_script_; - Handle resource_is_shared_cross_origin_; + ScriptOriginOptions options_; Handle script_id_; Handle source_map_url_; }; @@ -1160,8 +1184,7 @@ class V8_EXPORT ScriptCompiler { Handle resource_name; Handle resource_line_offset; Handle resource_column_offset; - Handle resource_is_embedder_debug_script; - Handle resource_is_shared_cross_origin; + ScriptOriginOptions resource_options; Handle source_map_url; // Cached data from previous compilation (if a kConsume*Cache flag is @@ -1450,6 +1473,7 @@ class V8_EXPORT Message { * this Message was generated to V8. */ bool IsSharedCrossOrigin() const; + bool IsOpaque() const; // TODO(1245381): Print to a string instead of on a FILE. static void PrintCurrentStackTrace(Isolate* isolate, FILE* out); @@ -7226,6 +7250,24 @@ int FunctionCallbackInfo::Length() const { return length_; } +ScriptOrigin::ScriptOrigin(Handle resource_name, + Handle resource_line_offset, + Handle resource_column_offset, + Handle resource_is_shared_cross_origin, + Handle script_id, + Handle resource_is_embedder_debug_script, + Handle source_map_url, + Handle resource_is_opaque) + : resource_name_(resource_name), + resource_line_offset_(resource_line_offset), + resource_column_offset_(resource_column_offset), + options_(!resource_is_embedder_debug_script.IsEmpty() && + resource_is_embedder_debug_script->IsTrue(), + !resource_is_shared_cross_origin.IsEmpty() && + resource_is_shared_cross_origin->IsTrue(), + !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()), + script_id_(script_id), + source_map_url_(source_map_url) {} Handle ScriptOrigin::ResourceName() const { return resource_name_; @@ -7242,16 +7284,6 @@ Handle ScriptOrigin::ResourceColumnOffset() const { } -Handle ScriptOrigin::ResourceIsEmbedderDebugScript() const { - return resource_is_embedder_debug_script_; -} - - -Handle ScriptOrigin::ResourceIsSharedCrossOrigin() const { - return resource_is_shared_cross_origin_; -} - - Handle ScriptOrigin::ScriptID() const { return script_id_; } @@ -7266,8 +7298,7 @@ ScriptCompiler::Source::Source(Local string, const ScriptOrigin& origin, resource_name(origin.ResourceName()), resource_line_offset(origin.ResourceLineOffset()), resource_column_offset(origin.ResourceColumnOffset()), - resource_is_embedder_debug_script(origin.ResourceIsEmbedderDebugScript()), - resource_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin()), + resource_options(origin.Options()), source_map_url(origin.SourceMapUrl()), cached_data(data) {} diff --git a/src/accessors.cc b/src/accessors.cc index 50df0dd..e192007 100644 --- a/src/accessors.cc +++ b/src/accessors.cc @@ -677,8 +677,9 @@ void Accessors::ScriptIsEmbedderDebugScriptGetter( DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.This()); - bool is_embedder_debug_script = - Script::cast(JSValue::cast(object)->value())->is_embedder_debug_script(); + bool is_embedder_debug_script = Script::cast(JSValue::cast(object)->value()) + ->origin_options() + .IsEmbedderDebugScript(); Object* res = *isolate->factory()->ToBoolean(is_embedder_debug_script); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } diff --git a/src/api.cc b/src/api.cc index 4ad4042..ea29d55 100644 --- a/src/api.cc +++ b/src/api.cc @@ -188,14 +188,16 @@ static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate, i::Handle source_map_url(script->source_mapping_url(), isolate); v8::Isolate* v8_isolate = reinterpret_cast(script->GetIsolate()); + ScriptOriginOptions options(script->origin_options()); v8::ScriptOrigin origin( Utils::ToLocal(scriptName), v8::Integer::New(v8_isolate, script->line_offset()->value()), v8::Integer::New(v8_isolate, script->column_offset()->value()), - v8::Boolean::New(v8_isolate, script->is_shared_cross_origin()), + v8::Boolean::New(v8_isolate, options.IsSharedCrossOrigin()), v8::Integer::New(v8_isolate, script->id()->value()), - v8::Boolean::New(v8_isolate, script->is_embedder_debug_script()), - Utils::ToLocal(source_map_url)); + v8::Boolean::New(v8_isolate, options.IsEmbedderDebugScript()), + Utils::ToLocal(source_map_url), + v8::Boolean::New(v8_isolate, options.IsOpaque())); return origin; } @@ -1716,8 +1718,6 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( i::Handle source_map_url; int line_offset = 0; int column_offset = 0; - bool is_embedder_debug_script = false; - bool is_shared_cross_origin = false; if (!source->resource_name.IsEmpty()) { name_obj = Utils::OpenHandle(*(source->resource_name)); } @@ -1728,21 +1728,13 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( column_offset = static_cast(source->resource_column_offset->Value()); } - if (!source->resource_is_shared_cross_origin.IsEmpty()) { - is_shared_cross_origin = - source->resource_is_shared_cross_origin->IsTrue(); - } - if (!source->resource_is_embedder_debug_script.IsEmpty()) { - is_embedder_debug_script = - source->resource_is_embedder_debug_script->IsTrue(); - } if (!source->source_map_url.IsEmpty()) { source_map_url = Utils::OpenHandle(*(source->source_map_url)); } result = i::Compiler::CompileScript( - str, name_obj, line_offset, column_offset, is_embedder_debug_script, - is_shared_cross_origin, source_map_url, isolate->native_context(), NULL, - &script_data, options, i::NOT_NATIVES_CODE, is_module); + str, name_obj, line_offset, column_offset, source->resource_options, + source_map_url, isolate->native_context(), NULL, &script_data, options, + i::NOT_NATIVES_CODE, is_module); has_pending_exception = result.is_null(); if (has_pending_exception && script_data != NULL) { // This case won't happen during normal operation; we have compiled @@ -1976,14 +1968,7 @@ MaybeLocal