Bug 1800641 - Part 4: Add ScriptLoadRequest::{Get,Set}SRILength. r=nbp

Depends on D197841

Differential Revision: https://phabricator.services.mozilla.com/D197842
This commit is contained in:
Tooru Fujisawa
2024-01-08 14:57:37 +00:00
parent 929368a3f4
commit 168d0ccdd7
3 changed files with 24 additions and 10 deletions

View File

@@ -187,7 +187,7 @@ ScriptLoadHandler::OnIncrementalData(nsIIncrementalStreamLoader* aLoader,
return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest)); return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest));
} }
if (sriLength) { if (sriLength) {
mRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); mRequest->SetSRILength(sriLength);
} }
} }
@@ -433,14 +433,14 @@ ScriptLoadHandler::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest)); return channelRequest->Cancel(mScriptLoader->RestartLoad(mRequest));
} }
mRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); mRequest->SetSRILength(sriLength);
Vector<uint8_t> compressedBytecode; Vector<uint8_t> compressedBytecode;
// mRequest has the compressed bytecode, but will be filled with the // mRequest has the compressed bytecode, but will be filled with the
// uncompressed bytecode // uncompressed bytecode
compressedBytecode.swap(bytecode); compressedBytecode.swap(bytecode);
if (!JS::loader::ScriptBytecodeDecompress( if (!JS::loader::ScriptBytecodeDecompress(
compressedBytecode, mRequest->mBytecodeOffset, bytecode)) { compressedBytecode, mRequest->GetSRILength(), bytecode)) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
} }

View File

@@ -2685,10 +2685,12 @@ nsresult ScriptLoader::MaybePrepareForBytecodeEncodingAfterExecute(
if (aRequest->IsMarkedForBytecodeEncoding()) { if (aRequest->IsMarkedForBytecodeEncoding()) {
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(), TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"scriptloader_encode"); "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 // NOTE: This assertion will fail once we start encoding more data after the
// first encode. // first encode.
MOZ_ASSERT(aRequest->mBytecodeOffset == MOZ_ASSERT(aRequest->GetSRILength() == aRequest->SRIAndBytecode().length());
aRequest->SRIAndBytecode().length());
RegisterForBytecodeEncoding(aRequest); RegisterForBytecodeEncoding(aRequest);
MOZ_ASSERT(IsAlreadyHandledForBytecodeEncodingPreparation(aRequest)); MOZ_ASSERT(IsAlreadyHandledForBytecodeEncodingPreparation(aRequest));
@@ -2981,7 +2983,7 @@ void ScriptLoader::EncodeRequestBytecode(JSContext* aCx,
Vector<uint8_t> compressedBytecode; Vector<uint8_t> compressedBytecode;
// TODO probably need to move this to a helper thread // TODO probably need to move this to a helper thread
if (!ScriptBytecodeCompress(aRequest->SRIAndBytecode(), if (!ScriptBytecodeCompress(aRequest->SRIAndBytecode(),
aRequest->mBytecodeOffset, compressedBytecode)) { aRequest->GetSRILength(), compressedBytecode)) {
return; return;
} }
@@ -3342,10 +3344,13 @@ nsresult ScriptLoader::OnStreamComplete(
JS::TranscodeBuffer& bytecode = aRequest->SRIAndBytecode(); JS::TranscodeBuffer& bytecode = aRequest->SRIAndBytecode();
MOZ_ASSERT_IF(NS_SUCCEEDED(rv), bytecode.length() == sriLength); MOZ_ASSERT_IF(NS_SUCCEEDED(rv), bytecode.length() == sriLength);
aRequest->mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength); // TODO: (Bug 1800896) This code should be moved into SaveSRIHash, and the
if (aRequest->mBytecodeOffset != sriLength) { // SRI out-param can be removed.
// We need extra padding after SRI hash. aRequest->SetSRILength(sriLength);
if (!bytecode.resize(aRequest->mBytecodeOffset)) { 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; return NS_ERROR_OUT_OF_MEMORY;
} }
} }

View File

@@ -259,6 +259,15 @@ class ScriptLoadRequest
bytecode.length() - offset); 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; } mozilla::CORSMode CORSMode() const { return mFetchOptions->mCORSMode; }
void DropBytecodeCacheReferences(); void DropBytecodeCacheReferences();