Bug 1134885 - ChunkSets should be fallible. r=mmc

This commit is contained in:
Gian-Carlo Pascutto 2015-02-25 08:31:11 +01:00
Родитель f1d154f17c
Коммит 9bf0036576
6 изменённых файлов: 132 добавлений и 43 удалений

Просмотреть файл

@ -41,7 +41,9 @@ ChunkSet::Set(uint32_t aChunk)
{
size_t idx = mChunks.BinaryIndexOf(aChunk);
if (idx == nsTArray<uint32_t>::NoIndex) {
mChunks.InsertElementSorted(aChunk);
if (!mChunks.InsertElementSorted(aChunk)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
@ -87,7 +89,9 @@ ChunkSet::Remove(const ChunkSet& aOther)
}
}
mChunks.SetLength(addIter - mChunks.Elements());
if (!mChunks.SetLength(addIter - mChunks.Elements())) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}

Просмотреть файл

@ -48,7 +48,7 @@ public:
uint32_t *End() { return mChunks.Elements() + mChunks.Length(); }
private:
nsTArray<uint32_t> mChunks;
FallibleTArray<uint32_t> mChunks;
};
}

Просмотреть файл

@ -123,41 +123,48 @@ namespace safebrowsing {
const uint32_t STORE_MAGIC = 0x1231af3b;
const uint32_t CURRENT_VERSION = 3;
void
nsresult
TableUpdate::NewAddPrefix(uint32_t aAddChunk, const Prefix& aHash)
{
AddPrefix *add = mAddPrefixes.AppendElement();
if (!add) return NS_ERROR_OUT_OF_MEMORY;
add->addChunk = aAddChunk;
add->prefix = aHash;
return NS_OK;
}
void
nsresult
TableUpdate::NewSubPrefix(uint32_t aAddChunk, const Prefix& aHash, uint32_t aSubChunk)
{
SubPrefix *sub = mSubPrefixes.AppendElement();
if (!sub) return NS_ERROR_OUT_OF_MEMORY;
sub->addChunk = aAddChunk;
sub->prefix = aHash;
sub->subChunk = aSubChunk;
return NS_OK;
}
void
nsresult
TableUpdate::NewAddComplete(uint32_t aAddChunk, const Completion& aHash)
{
AddComplete *add = mAddCompletes.AppendElement();
if (!add) return NS_ERROR_OUT_OF_MEMORY;
add->addChunk = aAddChunk;
add->complete = aHash;
return NS_OK;
}
void
nsresult
TableUpdate::NewSubComplete(uint32_t aAddChunk, const Completion& aHash, uint32_t aSubChunk)
{
SubComplete *sub = mSubCompletes.AppendElement();
if (!sub) return NS_ERROR_OUT_OF_MEMORY;
sub->addChunk = aAddChunk;
sub->complete = aHash;
sub->subChunk = aSubChunk;
return NS_OK;
}
HashStore::HashStore(const nsACString& aTableName, nsIFile* aStoreDir)
: mTableName(aTableName)
, mStoreDirectory(aStoreDir)

Просмотреть файл

@ -39,18 +39,28 @@ public:
// Throughout, uint32_t aChunk refers only to the chunk number. Chunk data is
// stored in the Prefix structures.
void NewAddChunk(uint32_t aChunk) { mAddChunks.Set(aChunk); }
void NewSubChunk(uint32_t aChunk) { mSubChunks.Set(aChunk); }
void NewAddExpiration(uint32_t aChunk) { mAddExpirations.Set(aChunk); }
void NewSubExpiration(uint32_t aChunk) { mSubExpirations.Set(aChunk); }
void NewAddPrefix(uint32_t aAddChunk, const Prefix& aPrefix);
void NewSubPrefix(uint32_t aAddChunk, const Prefix& aPrefix, uint32_t aSubChunk);
void NewAddComplete(uint32_t aChunk, const Completion& aCompletion);
void NewSubComplete(uint32_t aAddChunk, const Completion& aCompletion,
uint32_t aSubChunk);
nsresult NewAddChunk(uint32_t aChunk) {
return mAddChunks.Set(aChunk);
} NS_WARN_UNUSED_RESULT;
nsresult NewSubChunk(uint32_t aChunk) {
return mSubChunks.Set(aChunk);
} NS_WARN_UNUSED_RESULT;
nsresult NewAddExpiration(uint32_t aChunk) {
return mAddExpirations.Set(aChunk);
} NS_WARN_UNUSED_RESULT;
nsresult NewSubExpiration(uint32_t aChunk) {
return mSubExpirations.Set(aChunk);
} NS_WARN_UNUSED_RESULT;
nsresult NewAddPrefix(uint32_t aAddChunk,
const Prefix& aPrefix) NS_WARN_UNUSED_RESULT;
nsresult NewSubPrefix(uint32_t aAddChunk,
const Prefix& aPrefix,
uint32_t aSubChunk) NS_WARN_UNUSED_RESULT;
nsresult NewAddComplete(uint32_t aChunk,
const Completion& aCompletion) NS_WARN_UNUSED_RESULT;
nsresult NewSubComplete(uint32_t aAddChunk,
const Completion& aCompletion,
uint32_t aSubChunk) NS_WARN_UNUSED_RESULT;
void SetLocalUpdate(void) { mLocalUpdate = true; }
bool IsLocalUpdate(void) { return mLocalUpdate; }

Просмотреть файл

@ -27,6 +27,8 @@ namespace safebrowsing {
// Updates will fail if fed chunks larger than this
const uint32_t MAX_CHUNK_SIZE = (1024 * 1024);
// Updates will fail if the total number of touched chunks is larger than this
const uint32_t MAX_CHUNK_RANGE = 1000000;
const uint32_t DOMAIN_SIZE = 4;
@ -171,11 +173,20 @@ ProtocolParser::ProcessExpirations(const nsCString& aLine)
while (begin != end) {
uint32_t first, last;
if (ParseChunkRange(begin, end, &first, &last)) {
if (last < first) return NS_ERROR_FAILURE;
if (last - first > MAX_CHUNK_RANGE) return NS_ERROR_FAILURE;
for (uint32_t num = first; num <= last; num++) {
if (aLine[0] == 'a')
mTableUpdate->NewAddExpiration(num);
else
mTableUpdate->NewSubExpiration(num);
if (aLine[0] == 'a') {
nsresult rv = mTableUpdate->NewAddExpiration(num);
if (NS_FAILED(rv)) {
return rv;
}
} else {
nsresult rv = mTableUpdate->NewSubExpiration(num);
if (NS_FAILED(rv)) {
return rv;
}
}
}
} else {
return NS_ERROR_FAILURE;
@ -226,18 +237,31 @@ ProtocolParser::ProcessChunkControl(const nsCString& aLine)
LOG(("Processing digest256 data"));
mChunkState.type = (command == 'a') ? CHUNK_ADD_DIGEST : CHUNK_SUB_DIGEST;
}
nsresult rv;
switch (mChunkState.type) {
case CHUNK_ADD:
mTableUpdate->NewAddChunk(mChunkState.num);
rv = mTableUpdate->NewAddChunk(mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
break;
case CHUNK_SUB:
mTableUpdate->NewSubChunk(mChunkState.num);
rv = mTableUpdate->NewSubChunk(mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
break;
case CHUNK_ADD_DIGEST:
mTableUpdate->NewAddChunk(mChunkState.num);
rv = mTableUpdate->NewAddChunk(mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
break;
case CHUNK_SUB_DIGEST:
mTableUpdate->NewSubChunk(mChunkState.num);
rv = mTableUpdate->NewSubChunk(mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
break;
}
@ -323,12 +347,18 @@ ProtocolParser::ProcessPlaintextChunk(const nsACString& aChunk)
if (mChunkState.hashSize == COMPLETE_SIZE) {
Completion hash;
hash.FromPlaintext(line, mCryptoHash);
mTableUpdate->NewAddComplete(mChunkState.num, hash);
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
}
} else {
NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks.");
Prefix hash;
hash.FromPlaintext(line, mCryptoHash);
mTableUpdate->NewAddPrefix(mChunkState.num, hash);
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
}
}
} else {
nsCString::const_iterator begin, iter, end;
@ -346,12 +376,18 @@ ProtocolParser::ProcessPlaintextChunk(const nsACString& aChunk)
if (mChunkState.hashSize == COMPLETE_SIZE) {
Completion hash;
hash.FromPlaintext(Substring(iter, end), mCryptoHash);
mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
} else {
NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks.");
Prefix hash;
hash.FromPlaintext(Substring(iter, end), mCryptoHash);
mTableUpdate->NewSubPrefix(addChunk, hash, mChunkState.num);
nsresult rv = mTableUpdate->NewSubPrefix(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
}
}
}
@ -418,7 +454,10 @@ ProtocolParser::ProcessDigestAdd(const nsACString& aChunk)
Completion hash;
hash.Assign(Substring(aChunk, start, COMPLETE_SIZE));
start += COMPLETE_SIZE;
mTableUpdate->NewAddComplete(mChunkState.num, hash);
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
@ -445,7 +484,10 @@ ProtocolParser::ProcessDigestSub(const nsACString& aChunk)
hash.Assign(Substring(aChunk, start, COMPLETE_SIZE));
start += COMPLETE_SIZE;
mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
@ -458,7 +500,10 @@ ProtocolParser::ProcessHostAdd(const Prefix& aDomain, uint8_t aNumEntries,
"ProcessHostAdd should only be called for prefix hashes.");
if (aNumEntries == 0) {
mTableUpdate->NewAddPrefix(mChunkState.num, aDomain);
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, aDomain);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
@ -470,7 +515,10 @@ ProtocolParser::ProcessHostAdd(const Prefix& aDomain, uint8_t aNumEntries,
for (uint8_t i = 0; i < aNumEntries; i++) {
Prefix hash;
hash.Assign(Substring(aChunk, *aStart, PREFIX_SIZE));
mTableUpdate->NewAddPrefix(mChunkState.num, hash);
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
}
*aStart += PREFIX_SIZE;
}
@ -497,7 +545,10 @@ ProtocolParser::ProcessHostSub(const Prefix& aDomain, uint8_t aNumEntries,
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
addChunk = PR_ntohl(addChunk);
mTableUpdate->NewSubPrefix(addChunk, aDomain, mChunkState.num);
nsresult rv = mTableUpdate->NewSubPrefix(addChunk, aDomain, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
@ -518,7 +569,10 @@ ProtocolParser::ProcessHostSub(const Prefix& aDomain, uint8_t aNumEntries,
prefix.Assign(Substring(aChunk, *aStart, PREFIX_SIZE));
*aStart += PREFIX_SIZE;
mTableUpdate->NewSubPrefix(addChunk, prefix, mChunkState.num);
nsresult rv = mTableUpdate->NewSubPrefix(addChunk, prefix, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
@ -546,7 +600,10 @@ ProtocolParser::ProcessHostAddComplete(uint8_t aNumEntries,
for (uint8_t i = 0; i < aNumEntries; i++) {
Completion hash;
hash.Assign(Substring(aChunk, *aStart, COMPLETE_SIZE));
mTableUpdate->NewAddComplete(mChunkState.num, hash);
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
}
*aStart += COMPLETE_SIZE;
}
@ -583,7 +640,10 @@ ProtocolParser::ProcessHostSubComplete(uint8_t aNumEntries,
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
addChunk = PR_ntohl(addChunk);
mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;

Просмотреть файл

@ -669,9 +669,17 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
TableUpdate * tu = pParse->GetTableUpdate(resultsPtr->ElementAt(i).table);
LOG(("CacheCompletion Addchunk %d hash %X", resultsPtr->ElementAt(i).entry.addChunk,
resultsPtr->ElementAt(i).entry.ToUint32()));
tu->NewAddComplete(resultsPtr->ElementAt(i).entry.addChunk,
resultsPtr->ElementAt(i).entry.complete);
tu->NewAddChunk(resultsPtr->ElementAt(i).entry.addChunk);
rv = tu->NewAddComplete(resultsPtr->ElementAt(i).entry.addChunk,
resultsPtr->ElementAt(i).entry.complete);
if (NS_FAILED(rv)) {
// We can bail without leaking here because ForgetTableUpdates
// hasn't been called yet.
return rv;
}
rv = tu->NewAddChunk(resultsPtr->ElementAt(i).entry.addChunk);
if (NS_FAILED(rv)) {
return rv;
}
tu->SetLocalUpdate();
updates.AppendElement(tu);
pParse->ForgetTableUpdates();