Bug 1800896 - Move padding of the bytecode vector into SaveSRIHash. r=arai

When saving the SRI Hash, we resize the btyecode buffer to the expected size of
the buffer. Previously, after saving the bytecode, the code surrounding
SaveSRIHash introduced the padding necessary for potentially saving bytecode
after.

This patch move the padding into SaveSRIHash, to reduce the overhead of
understanding why the btyecode buffer is being manipulated in what seems to be
out-of-context. Moving it into SaveSRIHash might seems strange but it closer to
other actions to the bytecode buffer which makes it less unexpected.

Differential Revision: https://phabricator.services.mozilla.com/D203131
This commit is contained in:
Nicolas B. Pierron 2024-03-18 14:26:57 +00:00
parent f0e50cc358
commit ad642c1785
2 changed files with 15 additions and 22 deletions

View file

@ -3297,21 +3297,7 @@ nsresult ScriptLoader::OnStreamComplete(
// hash in case we are going to save the bytecode of this script in the
// cache.
if (aRequest->IsSource()) {
uint32_t sriLength = 0;
rv = SaveSRIHash(aRequest, aSRIDataVerifier, &sriLength);
JS::TranscodeBuffer& bytecode = aRequest->SRIAndBytecode();
MOZ_ASSERT_IF(NS_SUCCEEDED(rv), bytecode.length() == sriLength);
// 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;
}
}
rv = SaveSRIHash(aRequest, aSRIDataVerifier);
}
if (NS_SUCCEEDED(rv)) {
@ -3373,14 +3359,13 @@ nsresult ScriptLoader::VerifySRI(ScriptLoadRequest* aRequest,
return rv;
}
nsresult ScriptLoader::SaveSRIHash(ScriptLoadRequest* aRequest,
SRICheckDataVerifier* aSRIDataVerifier,
uint32_t* sriLength) const {
nsresult ScriptLoader::SaveSRIHash(
ScriptLoadRequest* aRequest, SRICheckDataVerifier* aSRIDataVerifier) const {
MOZ_ASSERT(aRequest->IsSource());
JS::TranscodeBuffer& bytecode = aRequest->SRIAndBytecode();
MOZ_ASSERT(bytecode.empty());
uint32_t len;
uint32_t len = 0;
// If the integrity metadata does not correspond to a valid hash function,
// IsComplete would be false.
@ -3418,7 +3403,16 @@ nsresult ScriptLoader::SaveSRIHash(ScriptLoadRequest* aRequest,
SRICheckDataVerifier::DataSummaryLength(len, bytecode.begin(), &srilen)));
MOZ_ASSERT(srilen == len);
*sriLength = len;
MOZ_ASSERT(bytecode.length() == len);
aRequest->SetSRILength(len);
if (aRequest->GetSRILength() != len) {
// 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_OK;
}

View file

@ -566,8 +566,7 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
SRICheckDataVerifier* aSRIDataVerifier) const;
nsresult SaveSRIHash(ScriptLoadRequest* aRequest,
SRICheckDataVerifier* aSRIDataVerifier,
uint32_t* sriLength) const;
SRICheckDataVerifier* aSRIDataVerifier) const;
void ReportErrorToConsole(ScriptLoadRequest* aRequest,
nsresult aResult) const override;