From 47ff6460be95ccb8ff7cde8794a7fbe658579cd8 Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Wed, 8 Aug 2018 10:40:03 +0100 Subject: [PATCH] Bug 1480966 - Make ScriptLoader::GetScriptSource faillible on OOM r=baku --- dom/script/ScriptLoader.cpp | 57 +++++++++++++++++++++---------------- dom/script/ScriptLoader.h | 5 ++-- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 09dca944eabb..a4c6ac681402 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -491,8 +491,12 @@ ScriptLoader::CreateModuleScript(ModuleLoadRequest* aRequest) rv = FillCompileOptionsForRequest(aes, aRequest, global, &options); if (NS_SUCCEEDED(rv)) { - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); - rv = nsJSUtils::CompileModule(cx, srcBuf, global, options, &module); + auto srcBuf = GetScriptSource(cx, aRequest); + if (srcBuf) { + rv = nsJSUtils::CompileModule(cx, *srcBuf, global, options, &module); + } else { + rv = NS_ERROR_OUT_OF_MEMORY; + } } } @@ -1824,11 +1828,11 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, if (aRequest->IsModuleRequest()) { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); - if (!JS::CompileOffThreadModule(cx, options, - srcBuf, - OffThreadScriptLoaderCallback, - static_cast(runnable))) { + auto srcBuf = GetScriptSource(cx, aRequest); + if (!srcBuf || !JS::CompileOffThreadModule(cx, options, + *srcBuf, + OffThreadScriptLoaderCallback, + static_cast(runnable))) { return NS_ERROR_OUT_OF_MEMORY; } } else if (aRequest->IsBytecode()) { @@ -1852,11 +1856,11 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, #endif } else { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); - if (!JS::CompileOffThread(cx, options, - srcBuf, - OffThreadScriptLoaderCallback, - static_cast(runnable))) { + auto srcBuf = GetScriptSource(cx, aRequest); + if (!srcBuf || !JS::CompileOffThread(cx, options, + *srcBuf, + OffThreadScriptLoaderCallback, + static_cast(runnable))) { return NS_ERROR_OUT_OF_MEMORY; } } @@ -1896,7 +1900,7 @@ ScriptLoader::CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest) return ProcessRequest(aRequest); } -SourceBufferHolder +mozilla::Maybe ScriptLoader::GetScriptSource(JSContext* aCx, ScriptLoadRequest* aRequest) { // Return a SourceBufferHolder object holding the script's source text. @@ -1909,15 +1913,17 @@ ScriptLoader::GetScriptSource(JSContext* aCx, ScriptLoadRequest* aRequest) size_t nbytes = inlineData.Length() * sizeof(char16_t); JS::UniqueTwoByteChars chars(static_cast(JS_malloc(aCx, nbytes))); - MOZ_RELEASE_ASSERT(chars); + if (!chars) { + return Nothing(); + } + memcpy(chars.get(), inlineData.get(), nbytes); - return SourceBufferHolder(std::move(chars), inlineData.Length()); + return Some(SourceBufferHolder(std::move(chars), inlineData.Length())); } size_t length = aRequest->ScriptText().length(); - return SourceBufferHolder(aRequest->ScriptText().extractOrCopyRawBuffer(), - length, - SourceBufferHolder::GiveOwnership); + JS::UniqueTwoByteChars chars(aRequest->ScriptText().extractOrCopyRawBuffer()); + return Some(SourceBufferHolder(std::move(chars), length)); } nsresult @@ -2369,14 +2375,17 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) &script); } else { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); + auto srcBuf = GetScriptSource(cx, aRequest); - if (recordreplay::IsRecordingOrReplaying()) { - recordreplay::NoteContentParse(this, options.filename(), "application/javascript", - srcBuf.get(), srcBuf.length()); + if (srcBuf) { + if (recordreplay::IsRecordingOrReplaying()) { + recordreplay::NoteContentParse(this, options.filename(), "application/javascript", + srcBuf->get(), srcBuf->length()); + } + rv = exec.CompileAndExec(options, *srcBuf, &script); + } else { + rv = NS_ERROR_OUT_OF_MEMORY; } - - rv = exec.CompileAndExec(options, srcBuf, &script); } } } diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index 92e3cc08f149..50b37468893e 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -24,6 +24,7 @@ #include "mozilla/dom/ScriptLoadRequest.h" #include "mozilla/dom/SRIMetadata.h" #include "mozilla/dom/SRICheck.h" +#include "mozilla/Maybe.h" #include "mozilla/MozPromise.h" #include "mozilla/net/ReferrerPolicy.h" #include "mozilla/Vector.h" @@ -506,8 +507,8 @@ private: void MaybeMoveToLoadedList(ScriptLoadRequest* aRequest); - JS::SourceBufferHolder GetScriptSource(JSContext* aCx, - ScriptLoadRequest* aRequest); + mozilla::Maybe GetScriptSource(JSContext* aCx, + ScriptLoadRequest* aRequest); void SetModuleFetchStarted(ModuleLoadRequest *aRequest); void SetModuleFetchFinishedAndResumeWaitingRequests(ModuleLoadRequest* aRequest,