Add missing code to properly grow buffer

Related feedback:
- https://github.com/orgs/uBlockOrigin/teams/ublock-issues-volunteers/discussions/293

Related commit:
- 725e6931f5

Through all the changes, forgot to pay attention to scenarios
where the `filterData` needs to grow -- the buffer's defautl
size is set to accomodate default filter lists, and subscribing
to more lists would cause the static network filtering engine
to fail because the buffer was not resized when needed.
This commit is contained in:
Raymond Hill 2021-12-04 17:06:09 -05:00
parent b98836ab8e
commit b78b277907
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 24 additions and 20 deletions

View File

@ -361,22 +361,27 @@ const FILTER_DATA_PAGE_SIZE = 65536;
let filterData = new Int32Array(FILTER_DATA_PAGE_SIZE * 5); let filterData = new Int32Array(FILTER_DATA_PAGE_SIZE * 5);
let filterDataWritePtr = 2; let filterDataWritePtr = 2;
function filterDataAlloc(...args) { function filterDataGrow(len) {
const idata = filterDataWritePtr; if ( len <= filterData.length ) { return; }
const len = args.length; const newLen = (len + FILTER_DATA_PAGE_SIZE-1) & ~(FILTER_DATA_PAGE_SIZE-1);
filterDataWritePtr += len; const newBuf = new Int32Array(newLen);
if ( filterDataWritePtr > filterData.length ) { newBuf.set(filterData);
const newLength = (filterDataWritePtr + FILTER_DATA_PAGE_SIZE-1) & ~(FILTER_DATA_PAGE_SIZE-1); filterData = newBuf;
filterData = new Int32Array(newLength);
} }
for ( let j = 0; j < len; j++ ) { function filterDataAlloc(...args) {
filterData[idata+j] = args[j]; const len = args.length;
const idata = filterDataAllocLen(len);
for ( let i = 0; i < len; i++ ) {
filterData[idata+i] = args[i];
} }
return idata; return idata;
} }
function filterDataAllocLen(len) { function filterDataAllocLen(len) {
const idata = filterDataWritePtr; const idata = filterDataWritePtr;
filterDataWritePtr += len; filterDataWritePtr += len;
if ( filterDataWritePtr > filterData.length ) {
filterDataGrow(filterDataWritePtr);
}
return idata; return idata;
} }
const filterSequenceAdd = (a, b) => { const filterSequenceAdd = (a, b) => {
@ -395,10 +400,8 @@ function filterDataToSelfie() {
function filterDataFromSelfie(selfie) { function filterDataFromSelfie(selfie) {
if ( typeof selfie !== 'string' || selfie === '' ) { return false; } if ( typeof selfie !== 'string' || selfie === '' ) { return false; }
const data = JSON.parse(selfie); const data = JSON.parse(selfie);
const newLen = (data.length + FILTER_DATA_PAGE_SIZE-1) & ~(FILTER_DATA_PAGE_SIZE-1); if ( Array.isArray(data) === false ) { return false; }
if ( newLen > filterData.length ) { filterDataGrow(data.length);
filterData = new Int32Array(newLen);
}
filterDataWritePtr = data.length; filterDataWritePtr = data.length;
filterData.set(data); filterData.set(data);
return true; return true;
@ -446,6 +449,7 @@ function filterRefsToSelfie() {
function filterRefsFromSelfie(selfie) { function filterRefsFromSelfie(selfie) {
if ( typeof selfie !== 'string' || selfie === '' ) { return false; } if ( typeof selfie !== 'string' || selfie === '' ) { return false; }
const refs = JSON.parse(selfie); const refs = JSON.parse(selfie);
if ( Array.isArray(refs) === false ) { return false; }
for ( let i = 0; i < refs.length; i++ ) { for ( let i = 0; i < refs.length; i++ ) {
const v = refs[i]; const v = refs[i];
switch ( v.t ) { switch ( v.t ) {
@ -1836,10 +1840,10 @@ const FilterCollection = class {
filterData[idata+1] = i; filterData[idata+1] = i;
} }
static create() { static create(fid = -1) {
return filterDataAlloc( return filterDataAlloc(
this.fid, // fid fid !== -1 ? fid : FilterCollection.fid,
0 // i 0
); );
} }
@ -2280,7 +2284,7 @@ const FilterBucket = class extends FilterCollection {
static create() { static create() {
const idata = filterDataAllocLen(4); const idata = filterDataAllocLen(4);
filterData[idata+0] = FilterBucket.fid; // fid filterData[idata+0] = FilterBucket.fid; // fid
filterData[idata+1] = super.create(); // icollection filterData[idata+1] = FilterCollection.create(); // icollection
filterData[idata+2] = 0; // n filterData[idata+2] = 0; // n
filterData[idata+3] = 0; // $matchedUnit filterData[idata+3] = 0; // $matchedUnit
return idata; return idata;
@ -3681,7 +3685,7 @@ FilterContainer.prototype.freeze = function() {
if ( this.optimized !== true && this.optimizeTaskId === undefined ) { if ( this.optimized !== true && this.optimizeTaskId === undefined ) {
this.optimizeTaskId = queueTask(( ) => { this.optimizeTaskId = queueTask(( ) => {
this.optimizeTaskId = undefined; this.optimizeTaskId = undefined;
this.optimize(10); this.optimize();
}, 2000); }, 2000);
} }
}; };