From 168d0ccdd7bdef2480a5e9e3fb3a36810795060c Mon Sep 17 00:00:00 2001 From: Tooru Fujisawa Date: Mon, 8 Jan 2024 14:57:37 +0000 Subject: [PATCH] Bug 1800641 - Part 4: Add ScriptLoadRequest::{Get,Set}SRILength. r=nbp Depends on D197841 Differential Revision: https://phabricator.services.mozilla.com/D197842 --- dom/script/ScriptLoadHandler.cpp | 6 +++--- dom/script/ScriptLoader.cpp | 19 ++++++++++++------- js/loader/ScriptLoadRequest.h | 9 +++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/dom/script/ScriptLoadHandler.cpp b/dom/script/ScriptLoadHandler.cpp index 2840e97d7946..cff8eed5fd9e 100644 --- a/dom/script/ScriptLoadHandler.cpp +++ b/dom/script/ScriptLoadHandler.cpp @@ -187,7 +187,7 @@ ScriptLoadHandler::OnIncrementalData(nsIIncrementalStreamLoader* aLoader, return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest)); } if (sriLength) { - mRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); + mRequest->SetSRILength(sriLength); } } @@ -433,14 +433,14 @@ ScriptLoadHandler::OnStreamComplete(nsIIncrementalStreamLoader* aLoader, return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest)); } - mRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); + mRequest->SetSRILength(sriLength); Vector compressedBytecode; // mRequest has the compressed bytecode, but will be filled with the // uncompressed bytecode compressedBytecode.swap(bytecode); if (!JS::loader::ScriptBytecodeDecompress( - compressedBytecode, mRequest->mBytecodeOffset, bytecode)) { + compressedBytecode, mRequest->GetSRILength(), bytecode)) { return NS_ERROR_UNEXPECTED; } } diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index b851167e10cd..67b3651ad139 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -2685,10 +2685,12 @@ nsresult ScriptLoader::MaybePrepareForBytecodeEncodingAfterExecute( if (aRequest->IsMarkedForBytecodeEncoding()) { TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(), "scriptloader_encode"); + // Check that the TranscodeBuffer which is going to receive the encoded + // bytecode only contains the SRI, and nothing more. + // // NOTE: This assertion will fail once we start encoding more data after the // first encode. - MOZ_ASSERT(aRequest->mBytecodeOffset == - aRequest->SRIAndBytecode().length()); + MOZ_ASSERT(aRequest->GetSRILength() == aRequest->SRIAndBytecode().length()); RegisterForBytecodeEncoding(aRequest); MOZ_ASSERT(IsAlreadyHandledForBytecodeEncodingPreparation(aRequest)); @@ -2981,7 +2983,7 @@ void ScriptLoader::EncodeRequestBytecode(JSContext* aCx, Vector compressedBytecode; // TODO probably need to move this to a helper thread if (!ScriptBytecodeCompress(aRequest->SRIAndBytecode(), - aRequest->mBytecodeOffset, compressedBytecode)) { + aRequest->GetSRILength(), compressedBytecode)) { return; } @@ -3342,10 +3344,13 @@ nsresult ScriptLoader::OnStreamComplete( JS::TranscodeBuffer& bytecode = aRequest->SRIAndBytecode(); MOZ_ASSERT_IF(NS_SUCCEEDED(rv), bytecode.length() == sriLength); - aRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); - if (aRequest->mBytecodeOffset != sriLength) { - // We need extra padding after SRI hash. - if (!bytecode.resize(aRequest->mBytecodeOffset)) { + // TODO: (Bug 1800896) This code should be moved into SaveSRIHash, and the + // SRI out-param can be removed. + aRequest->SetSRILength(sriLength); + if (aRequest->GetSRILength() != sriLength) { + // The bytecode is aligned in the bytecode buffer, and space might be + // reserved for padding after the SRI hash. + if (!bytecode.resize(aRequest->GetSRILength())) { return NS_ERROR_OUT_OF_MEMORY; } } diff --git a/js/loader/ScriptLoadRequest.h b/js/loader/ScriptLoadRequest.h index d6edf5b0bd21..230c8ba5c010 100644 --- a/js/loader/ScriptLoadRequest.h +++ b/js/loader/ScriptLoadRequest.h @@ -259,6 +259,15 @@ class ScriptLoadRequest bytecode.length() - offset); } + size_t GetSRILength() const { + MOZ_ASSERT(IsBytecode() || IsSource()); + return mBytecodeOffset; + } + void SetSRILength(size_t sriLength) { + MOZ_ASSERT(IsBytecode() || IsSource()); + mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); + } + mozilla::CORSMode CORSMode() const { return mFetchOptions->mCORSMode; } void DropBytecodeCacheReferences();