From fc23089098e769bcd5d22fedd2185aeb7898bac0 Mon Sep 17 00:00:00 2001 From: ryne3366 Date: Fri, 16 May 2025 11:20:52 +0800 Subject: [PATCH] sync some memory patches from SQLite orig https://sqlite.org/src/info/cb8b34fa1aa8bbe7 https://sqlite.org/src/info/198a1daae01dd930 https://sqlite.org/src/info/358de1b09f3d5ec0 https://sqlite.org/src/info/f35ce7c122017009 Signed-off-by: ryne3366 --- patch/0007-BugFix-CurrVersion.patch | 147 ++++++++++++++++++++++------ 1 file changed, 116 insertions(+), 31 deletions(-) diff --git a/patch/0007-BugFix-CurrVersion.patch b/patch/0007-BugFix-CurrVersion.patch index c9dd2ba..a32ac26 100644 --- a/patch/0007-BugFix-CurrVersion.patch +++ b/patch/0007-BugFix-CurrVersion.patch @@ -1,17 +1,35 @@ -From 426e0bd27846faf2716ea9149aa296faee8bacf3 Mon Sep 17 00:00:00 2001 +From cb48a51dfbc99ae4b5997f4a5c21c8fa39dd5f73 Mon Sep 17 00:00:00 2001 From: ryne3366 -Date: Thu, 8 May 2025 18:41:23 +0800 -Subject: [PATCH] Bugfix of current version +Date: Fri, 16 May 2025 11:18:39 +0800 +Subject: [PATCH] Sync patches from SQLite orig --- - src/sqlite3.c | 107 +++++++++++++++++++++++++++++++++----------------- - 1 file changed, 72 insertions(+), 35 deletions(-) + src/sqlite3.c | 139 +++++++++++++++++++++++++++++++++++--------------- + 1 file changed, 97 insertions(+), 42 deletions(-) diff --git a/src/sqlite3.c b/src/sqlite3.c -index c82804c..2abe447 100644 +index c82804c..bdf70f6 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c -@@ -43267,6 +43267,9 @@ static int unixOpen( +@@ -42314,11 +42314,16 @@ static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ + + #if SQLITE_MAX_MMAP_SIZE>0 + if( pFd->mmapSizeMax>0 ){ ++ /* Ensure that there is always at least a 256 byte buffer of addressable ++ ** memory following the returned page. If the database is corrupt, ++ ** SQLite may overread the page slightly (in practice only a few bytes, ++ ** but 256 is safe, round, number). */ ++ const int nEofBuffer = 256; + if( pFd->pMapRegion==0 ){ + int rc = unixMapfile(pFd, -1); + if( rc!=SQLITE_OK ) return rc; + } +- if( pFd->mmapSize >= iOff+nAmt ){ ++ if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ + *pp = &((u8 *)pFd->pMapRegion)[iOff]; + pFd->nFetchOut++; + } +@@ -43267,6 +43272,9 @@ static int unixOpen( flags |= SQLITE_OPEN_READONLY; openFlags |= O_RDONLY; isReadonly = 1; @@ -21,7 +39,28 @@ index c82804c..2abe447 100644 fd = robust_open(zName, openFlags, openMode); } } -@@ -57046,6 +57049,9 @@ static int jrnlBufferSize(Pager *pPager){ +@@ -49865,6 +49873,11 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ + + #if SQLITE_MAX_MMAP_SIZE>0 + if( pFd->mmapSizeMax>0 ){ ++ /* Ensure that there is always at least a 256 byte buffer of addressable ++ ** memory following the returned page. If the database is corrupt, ++ ** SQLite may overread the page slightly (in practice only a few bytes, ++ ** but 256 is safe, round, number). */ ++ const int nEofBuffer = 256; + if( pFd->pMapRegion==0 ){ + int rc = winMapfile(pFd, -1); + if( rc!=SQLITE_OK ){ +@@ -49873,7 +49886,7 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ + return rc; + } + } +- if( pFd->mmapSize >= iOff+nAmt ){ ++ if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ + assert( pFd->pMapRegion!=0 ); + *pp = &((u8 *)pFd->pMapRegion)[iOff]; + pFd->nFetchOut++; +@@ -57046,6 +57059,9 @@ static int jrnlBufferSize(Pager *pPager){ ** and debugging only. */ #ifdef SQLITE_CHECK_PAGES @@ -31,7 +70,7 @@ index c82804c..2abe447 100644 #if defined (__arm__) || defined (__aarch64__) #include u32 deep_fast_hash_arm(void *src, int srcLen){ -@@ -57114,6 +57120,9 @@ static u32 pager_pagehash(PgHdr *pPage){ +@@ -57114,6 +57130,9 @@ static u32 pager_pagehash(PgHdr *pPage){ return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData); } static void pager_set_pagehash(PgHdr *pPage){ @@ -41,7 +80,7 @@ index c82804c..2abe447 100644 pPage->pageHash = pager_pagehash(pPage); } -@@ -57125,10 +57134,7 @@ static void pager_set_pagehash(PgHdr *pPage){ +@@ -57125,10 +57144,7 @@ static void pager_set_pagehash(PgHdr *pPage){ #define CHECK_PAGE(x) checkPage(x) static void checkPage(PgHdr *pPg){ Pager *pPager = pPg->pPager; @@ -53,7 +92,7 @@ index c82804c..2abe447 100644 return; } if( pPg->pgno!=1 && pPg->pageHash!=pager_pagehash(pPg) ){ -@@ -58289,9 +58295,11 @@ static int pager_playback_one_page( +@@ -58289,9 +58305,11 @@ static int pager_playback_one_page( ** file. */ #ifdef SQLITE_HAS_CODEC if( !jrnlEnc ){ @@ -68,7 +107,7 @@ index c82804c..2abe447 100644 }else #endif /* SQLITE_HAS_CODEC */ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); -@@ -58302,9 +58310,11 @@ static int pager_playback_one_page( +@@ -58302,9 +58320,11 @@ static int pager_playback_one_page( if( pPager->pBackup ){ #ifdef SQLITE_HAS_CODEC if( jrnlEnc ){ @@ -83,7 +122,7 @@ index c82804c..2abe447 100644 }else #endif /* SQLITE_HAS_CODEC */ sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); -@@ -58359,7 +58369,7 @@ static int pager_playback_one_page( +@@ -58359,7 +58379,7 @@ static int pager_playback_one_page( } #if SQLITE_HAS_CODEC /* Decode the page just read from disk */ @@ -92,7 +131,7 @@ index c82804c..2abe447 100644 #endif /* SQLITE_HAS_CODEC */ sqlite3PcacheRelease(pPg); } -@@ -58937,7 +58947,7 @@ static int readDbPage(PgHdr *pPg){ +@@ -58937,7 +58957,7 @@ static int readDbPage(PgHdr *pPg){ memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); } } @@ -101,7 +140,7 @@ index c82804c..2abe447 100644 PAGER_INCR(sqlite3_pager_readdb_count); PAGER_INCR(pPager->nRead); -@@ -59101,6 +59111,9 @@ static int pagerWalFrames( +@@ -59101,6 +59121,9 @@ static int pagerWalFrames( } #ifdef SQLITE_CHECK_PAGES @@ -111,7 +150,7 @@ index c82804c..2abe447 100644 pList = sqlite3PcacheDirtyList(pPager->pPCache); for(p=pList; p; p=p->pDirty){ pager_set_pagehash(p); -@@ -60344,7 +60357,7 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ +@@ -60344,7 +60367,7 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); if( pList->pgno==1 ) pager_write_changecounter(pList); @@ -120,7 +159,7 @@ index c82804c..2abe447 100644 /* Write out the page data. */ rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); -@@ -60435,7 +60448,7 @@ static int subjournalPage(PgHdr *pPg){ +@@ -60435,7 +60458,7 @@ static int subjournalPage(PgHdr *pPg){ char *pData2; #if SQLITE_HAS_CODEC if( !pPager->subjInMemory ){ @@ -129,7 +168,7 @@ index c82804c..2abe447 100644 }else #endif /* SQLITE_HAS_CODEC */ pData2 = pData; -@@ -61896,7 +61909,7 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ +@@ -61896,7 +61919,7 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) ); assert( pPager->journalHdr<=pPager->journalOff ); @@ -138,7 +177,7 @@ index c82804c..2abe447 100644 cksum = pager_cksum(pPager, (u8*)pData2); /* Even if an IO or diskfull error occurs while journalling the -@@ -62261,7 +62274,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ +@@ -62261,7 +62284,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ if( DIRECT_MODE ){ const void *zBuf; assert( pPager->dbFileSize>0 ); @@ -147,7 +186,53 @@ index c82804c..2abe447 100644 if( rc==SQLITE_OK ){ rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); pPager->aStat[PAGER_STAT_WRITE]++; -@@ -245631,7 +245644,7 @@ CODEC_STATIC int sqlite3CodecDecryptData(CodecContext *ctx, OperateContext which +@@ -100159,8 +100182,7 @@ static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ + /* Set the value of register r[1] in the SQL statement to integer iRow. + ** This is done directly as a performance optimization + */ +- v->aMem[1].flags = MEM_Int; +- v->aMem[1].u.i = iRow; ++ sqlite3VdbeMemSetInt64(&v->aMem[1], iRow); + + /* If the statement has been run before (and is paused at the OP_ResultRow) + ** then back it up to the point where it does the OP_NotExists. This could +@@ -138684,7 +138706,11 @@ static int sqlite3Prepare( + sParse.db = db; + sParse.pReprepare = pReprepare; + assert( ppStmt && *ppStmt==0 ); +- if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory"); ++ if( db->mallocFailed ){ ++ sqlite3ErrorMsg(&sParse, "out of memory"); ++ db->errCode = rc = SQLITE_NOMEM; ++ goto end_prepare; ++ } + assert( sqlite3_mutex_held(db->mutex) ); + + /* For a long-term use prepared statement avoid the use of +@@ -158892,7 +158918,12 @@ static int whereKeyStats( + ** it is extended to two fields. The duplicates that this creates do not + ** cause any problems. + */ +- nField = MIN(pRec->nField, pIdx->nSample); ++ if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ ++ nField = pIdx->nKeyCol; ++ }else{ ++ nField = pIdx->nColumn; ++ } ++ nField = MIN(pRec->nField, nField); + iCol = 0; + iSample = pIdx->nSample * nField; + do{ +@@ -171125,7 +171156,7 @@ static YYACTIONTYPE yy_reduce( + { + if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0].minor.yy561.pUsing==0 ){ + yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131; +- }else if( yymsp[-3].minor.yy131->nSrc==1 ){ ++ }else if( ALWAYS(yymsp[-3].minor.yy131!=0) && yymsp[-3].minor.yy131->nSrc==1 ){ + yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy561); + if( yymsp[-5].minor.yy131 ){ + SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1]; +@@ -245631,7 +245662,7 @@ CODEC_STATIC int sqlite3CodecDecryptData(CodecContext *ctx, OperateContext which if(sqlite3CodecCheckHmac(keyCtx, pgno, inputBuffer.bufferSize + keyCtx->codecConst.initVectorSize, input, input + inputBuffer.bufferSize + keyCtx->codecConst.initVectorSize)){ sqlite3_log(SQLITE_ERROR, "codec: check hmac error at page %d, hmac %d, kdf %d, pageSize %d, iter %d.", pgno, keyCtx->codecConst.hmacAlgo, keyCtx->codecConst.kdfAlgo, keyCtx->codecConst.cipherPageSize, keyCtx->iter); @@ -156,7 +241,7 @@ index c82804c..2abe447 100644 } unsigned char *initVector = input + inputBuffer.bufferSize; void *cipherCtx = opensslGetCtx(keyCtx->codecConst.cipher, CODEC_OPERATION_DECRYPT, keyCtx->key, initVector); -@@ -245654,7 +245667,7 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ +@@ -245654,7 +245685,7 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ int rc = SQLITE_OK; errno_t memcpyRc = EOK; if(ctx == NULL || data == NULL){ @@ -165,7 +250,7 @@ index c82804c..2abe447 100644 } if(pgno == 1){ offset = FILE_HEADER_SIZE; -@@ -245668,12 +245681,13 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ +@@ -245668,12 +245699,13 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ memcpyRc = memcpy_s(pCtx->buffer, cipherPageSize, SQLITE_FILE_HEADER, FILE_HEADER_SIZE); if(memcpyRc != EOK){ sqlite3CodecSetError(pCtx, SQLITE_ERROR); @@ -180,7 +265,7 @@ index c82804c..2abe447 100644 } (void)memcpy_s(pData, cipherPageSize, pCtx->buffer, cipherPageSize); return pData; -@@ -245683,13 +245697,13 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ +@@ -245683,13 +245715,13 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ memcpyRc = memcpy_s(pCtx->buffer, cipherPageSize, pCtx->salt, FILE_HEADER_SIZE); if(memcpyRc != EOK){ sqlite3CodecSetError(pCtx, SQLITE_ERROR); @@ -196,7 +281,7 @@ index c82804c..2abe447 100644 } return pCtx->buffer; break; -@@ -245704,12 +245718,12 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ +@@ -245704,12 +245736,12 @@ void* sqlite3Codec(void *ctx, void *data, Pgno pgno, int mode){ rc = sqlite3CodecEncryptData(pCtx, OPERATE_CONTEXT_READ, pgno, cipherPageSize - offset, (unsigned char *)(pData + offset), pCtx->buffer + offset); if(rc != SQLITE_OK){ sqlite3CodecSetError(pCtx, rc); @@ -211,7 +296,7 @@ index c82804c..2abe447 100644 break; } } -@@ -246320,7 +246334,7 @@ static int PragmaMetaDoubleWrie(sqlite3 *db, int iDb, Parse *parse, const char * +@@ -246320,7 +246352,7 @@ static int PragmaMetaDoubleWrie(sqlite3 *db, int iDb, Parse *parse, const char * sqlite3_mutex_enter(db->mutex); // only support enabled meta double write int rc = MetaDwrOpenAndCheck(pBt); @@ -220,7 +305,7 @@ index c82804c..2abe447 100644 parse->nErr++; parse->rc = rc; } -@@ -246730,7 +246744,7 @@ static int MetaDwrWriteOnePage(Btree *pBt, PgHdr *pPage, MetaDwrHdr *hdr, u8 cur +@@ -246730,7 +246762,7 @@ static int MetaDwrWriteOnePage(Btree *pBt, PgHdr *pPage, MetaDwrHdr *hdr, u8 cur void *pData; #if defined(SQLITE_HAS_CODEC) if ((pData = sqlite3PagerCodec(pPage)) == 0) @@ -229,7 +314,7 @@ index c82804c..2abe447 100644 #else pData = pPage->pData; #endif -@@ -246776,6 +246790,29 @@ static inline const char *GetMetaFilePath(Pager *pPager) +@@ -246776,6 +246808,29 @@ static inline const char *GetMetaFilePath(Pager *pPager) return pPager->metaFd == NULL ? NULL : ((const char *)pPager->metaFd + ROUND8(pPager->pVfs->szOsFile)); } @@ -259,7 +344,7 @@ index c82804c..2abe447 100644 static int MetaDwrOpenFile(Pager *pPager, u8 openCreate) { if (pPager->metaFd || pPager->zFilename == NULL) { return SQLITE_OK; -@@ -246792,15 +246829,10 @@ static int MetaDwrOpenFile(Pager *pPager, u8 openCreate) { +@@ -246792,15 +246847,10 @@ static int MetaDwrOpenFile(Pager *pPager, u8 openCreate) { return SQLITE_NOMEM_BKPT; } sqlite3_snprintf(size, metaPath, "%s-dwr", pPager->zFilename); @@ -276,7 +361,7 @@ index c82804c..2abe447 100644 u32 flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_SUPER_JOURNAL); if (openCreate) { flags |= SQLITE_OPEN_CREATE; -@@ -246888,7 +246920,7 @@ static int MetaDwrLoadAndCheckMetaFile(BtShared *pBt, u8 reportErr) { +@@ -246888,7 +246938,7 @@ static int MetaDwrLoadAndCheckMetaFile(BtShared *pBt, u8 reportErr) { static int MetaDwrReadOnePage(Pager *pPager, MetaDwrHdr *hdr, int idx, u8 *pData) { u64 ofs = CaculateMetaDwrWriteOffset(pPager->pageSize, idx, hdr->zones[idx]); int rc = sqlite3OsRead(pPager->metaFd, pData, pPager->pageSize, ofs); @@ -285,7 +370,7 @@ index c82804c..2abe447 100644 return rc; } -@@ -247513,11 +247545,16 @@ static void walLogCheckpointInfo(Wal *pWal, sqlite3 *db, sqlite3_int64 startTime +@@ -247513,11 +247563,16 @@ static void walLogCheckpointInfo(Wal *pWal, sqlite3 *db, sqlite3_int64 startTime sqlite3_int64 endTime; sqlite3OsCurrentTimeInt64(db->pVfs, &endTime); sqlite3_int64 timeUse = endTime - startTime; @@ -306,5 +391,5 @@ index c82804c..2abe447 100644 #endif // hw export the symbols -- -2.28.0.windows.1 +2.34.1 -- Gitee