From 89aa2d0df744e9d219f8e4040281abd1a0033032 Mon Sep 17 00:00:00 2001 From: Yuanbo Feng Date: Thu, 15 Aug 2024 15:56:34 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0fdsan,=20checkHeader?= =?UTF-8?q?=E5=92=8Clog=20dump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: boris-sun --- BUILD.gn | 4 + src/sqlite3.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 199 insertions(+), 5 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index aba0de3..18d9f04 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -63,6 +63,8 @@ ohos_shared_library("sqlite") { "SQLITE_CODEC_ATTACH_CHANGED", "SQLITE_ENABLE_DROPTABLE_CALLBACK", "OPENSSL_SUPPRESS_DEPRECATED", + "LOG_DUMP", + "FDSAN_ENABLE", ] cflags_c = [ "-fvisibility=hidden", @@ -123,6 +125,8 @@ ohos_executable("sqlite3") { "SQLITE_DIRECT_OVERFLOW_READ", "SQLITE_SHARED_BLOCK_OPTIMIZATION", "OPENSSL_SUPPRESS_DEPRECATED", + "LOG_DUMP", + "FDSAN_ENABLE", ] cflags = [ diff --git a/src/sqlite3.c b/src/sqlite3.c index 9988125..a610bf7 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -21179,6 +21179,10 @@ SQLITE_API extern int sqlite3_open_file_count; #define OpenCounter(X) #endif /* defined(SQLITE_TEST) */ +#ifdef FDSAN_ENABLE +#define SQLITE_FDSAN_TAG 5351 +#endif + #endif /* !defined(_OS_COMMON_H_) */ /************** End of os_common.h *******************************************/ @@ -37451,11 +37455,20 @@ static int robust_open(const char *z, int f, mode_t m){ if( errno==EINTR ) continue; break; } +#ifdef FDSAN_ENABLE + if( fdsan_get_owner_tag(fd)==0 ){ + fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); + } +#endif if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; if( (f & (O_EXCL|O_CREAT))==(O_EXCL|O_CREAT) ){ (void)osUnlink(z); } +#ifdef FDSAN_ENABLE + fdsan_close_with_tag(fd, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); +#else osClose(fd); +#endif sqlite3_log(SQLITE_WARNING, "attempt to open \"%s\" as file descriptor %d", z, fd); fd = -1; @@ -38054,7 +38067,13 @@ static int unixLogErrorAtLine( ** and move on. */ static void robust_close(unixFile *pFile, int h, int lineno){ - if( osClose(h) ){ + int rc; +#ifdef FDSAN_ENABLE + rc = fdsan_close_with_tag(h, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); +#else + rc = osClose(h); +#endif + if( rc ){ unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", pFile ? pFile->zPath : 0, lineno); } @@ -38148,6 +38167,9 @@ static int findInodeInfo( storeLastErrno(pFile, errno); #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; +#endif +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR, "findInodeInfo-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc); #endif return SQLITE_IOERR; } @@ -38167,11 +38189,17 @@ static int findInodeInfo( do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); if( rc!=1 ){ storeLastErrno(pFile, errno); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR, "findInodeInfo-osWrite, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc); +#endif return SQLITE_IOERR; } rc = osFstat(fd, &statbuf); if( rc!=0 ){ storeLastErrno(pFile, errno); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR, "findInodeInfo-msdos-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc); +#endif return SQLITE_IOERR; } } @@ -39333,6 +39361,9 @@ static int flockUnlock(sqlite3_file *id, int eFileLock) { #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS return SQLITE_OK; #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_UNLOCK, "IOERR_UNLOCK fd[%d], eFileLock[%d]", pFile->h, eFileLock); +#endif return SQLITE_IOERR_UNLOCK; }else{ pFile->eFileLock = NO_LOCK; @@ -40159,14 +40190,23 @@ static int unixRead( #endif #ifdef EDEVERR case EDEVERR: +#endif +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_CORRUPTFS, "unixRead-EDEVERR, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); #endif return SQLITE_IOERR_CORRUPTFS; } +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_READ, "unixRead-got<0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); +#endif return SQLITE_IOERR_READ; }else{ storeLastErrno(pFile, 0); /* not a system error */ /* Unread parts of the buffer must be zero-filled */ memset(&((char*)pBuf)[got], 0, amt-got); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_SHORT_READ, "unixRead-got>=0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); +#endif return SQLITE_IOERR_SHORT_READ; } } @@ -40303,9 +40343,27 @@ static int unixWrite( if( amt>wrote ){ if( wrote<0 && pFile->lastErrno!=ENOSPC ){ /* lastErrno set by seekAndWrite */ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_WRITE, + "unixWrite, lastErrno set by seekAndWrite, fd[%d], offset[%lld], wrote[%d], amt[%d], lastErrno[%d]", + pFile->h, + offset, + wrote, + amt, + pFile->lastErrno); +#endif return SQLITE_IOERR_WRITE; }else{ storeLastErrno(pFile, 0); /* not a system error */ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_WRITE, + "unixWrite, not a system error, fd[%d], offset[%lld], wrote[%d], amt[%d], lastErrno[%d]", + pFile->h, + offset, + wrote, + amt, + pFile->lastErrno); +#endif return SQLITE_FULL; } } @@ -40537,6 +40595,12 @@ static int unixSync(sqlite3_file *id, int flags){ HAVE_FULLFSYNC, isFullsync)); rc = osOpenDirectory(pFile->zPath, &dirfd); if( rc==SQLITE_OK ){ +#ifdef FDSAN_ENABLE + // FDSAN_OWNER_TYPE_FILE is used because files and directories are not distinguished. + if( fdsan_get_owner_tag(dirfd)==0 ){ + fdsan_exchange_owner_tag(dirfd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); + } +#endif full_fsync(dirfd, 0, 0); robust_close(pFile, dirfd, __LINE__); }else{ @@ -40659,7 +40723,17 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ do{ err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); }while( err==EINTR ); - if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; + if( err && err!=EINVAL ){ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_WRITE, + "fcntlSizeHint-fallocate, fd[%d], bufSize[%lld], nSize[%lld] err[%d]", + pFile->h, + buf.st_size, + nSize, + err); +#endif + return SQLITE_IOERR_WRITE; + } #else /* If the OS does not have posix_fallocate(), fake it. Write a ** single byte to the last byte in each block that falls entirely @@ -40678,7 +40752,18 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ for(/*no-op*/; iWrite=nSize ) iWrite = nSize - 1; nWrite = seekAndWrite(pFile, iWrite, "", 1); - if( nWrite!=1 ) return SQLITE_IOERR_WRITE; + if( nWrite!=1 ){ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_WRITE, + "fcntlSizeHint-seekAndWrite, fd[%d], nWrite[%d], nSize[%d], nBlk[%d], iWrite[%lld]", + pFile->h, + nWrite, + nSize, + nBlk, + iWrite); +#endif + return SQLITE_IOERR_WRITE; + } } #endif } @@ -40733,14 +40818,29 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); +#ifdef LOG_DUMP + if( rc ){ + sqlite3_log(SQLITE_IOERR_BEGIN_ATOMIC, "unixFileControl-begin, fd[%d], op[%d]", pFile->h, op); + } +#endif return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; } case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); +#ifdef LOG_DUMP + if( rc ){ + sqlite3_log(SQLITE_IOERR_COMMIT_ATOMIC, "unixFileControl-commit, fd[%d], op[%d]", pFile->h, op); + } +#endif return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; } case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); +#ifdef LOG_DUMP + if( rc ){ + sqlite3_log(SQLITE_IOERR_ROLLBACK_ATOMIC, "unixFileControl-rollback, fd[%d], op[%d]", pFile->h, op); + } +#endif return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; } #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ @@ -41686,9 +41786,19 @@ static int unixShmLock( int *aLock; p = pDbFd->pShm; - if( p==0 ) return SQLITE_IOERR_SHMLOCK; + if( p==0 ){ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShm, fd[%d], ofst[%d], n[%d], flags[%d]", ofst, n, flags); +#endif + return SQLITE_IOERR_SHMLOCK; + } pShmNode = p->pShmNode; - if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; + if( NEVER(pShmNode==0) ){ +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShmNode, fd[%d], ofst[%d], n[%d], flags[%d]", ofst, n, flags); +#endif + return SQLITE_IOERR_SHMLOCK; + } aLock = pShmNode->aLock; assert( pShmNode==pDbFd->pInode->pShmNode ); @@ -43060,6 +43170,9 @@ static int unixOpen( if( fstatfs(fd, &fsInfo) == -1 ){ storeLastErrno(p, errno); robust_close(p, fd, __LINE__); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_ACCESS, "unixOpen, fd[%d], flags[%d], errno[%d]", fd, errno, flags); +#endif return SQLITE_IOERR_ACCESS; } if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { @@ -43153,6 +43266,12 @@ static int unixDelete( int fd; rc = osOpenDirectory(zPath, &fd); if( rc==SQLITE_OK ){ +#ifdef FDSAN_ENABLE + // FDSAN_OWNER_TYPE_FILE is used because files and directories are not distinguished. + if( fdsan_get_owner_tag(fd)==0 ){ + fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); + } +#endif if( full_fsync(fd,0,0) ){ rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); } @@ -43782,6 +43901,9 @@ static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", lPath, errno, osGetpid(0))); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_LOCK, "proxyGetLockPath len[%d], dbLen[%d], i[%d]", len, dbLen, i); +#endif return SQLITE_IOERR_LOCK; } len = strlcat(lPath, "sqliteplocks", maxLen); @@ -43900,6 +44022,7 @@ static int proxyCreateUnixFile( case EACCES: return SQLITE_PERM; case EIO: + sqlite3_log(SQLITE_IOERR_LOCK, "proxyCreateUnixFile-EIO, fd[%d]", fd); return SQLITE_IOERR_LOCK; /* even though it is the conch */ default: return SQLITE_CANTOPEN_BKPT; @@ -44068,6 +44191,9 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ struct stat buf; if( osFstat(conchFile->h, &buf) ){ storeLastErrno(pFile, errno); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_LOCK, "proxyConchLock pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno); +#endif return SQLITE_IOERR_LOCK; } @@ -44088,6 +44214,9 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); if( len<0 ){ storeLastErrno(pFile, errno); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_LOCK, "proxyConchLock tries 2, pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno); +#endif return SQLITE_IOERR_LOCK; } if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ @@ -44162,6 +44291,9 @@ static int proxyTakeConch(unixFile *pFile){ if( readLen<0 ){ /* I/O error: lastErrno set by seekAndRead */ storeLastErrno(pFile, conchFile->lastErrno); +#ifdef LOG_DUMP + sqlite3_log(SQLITE_IOERR_READ, "proxyTakeConch fd:[%d], pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, conchFile->lastErrno); +#endif rc = SQLITE_IOERR_READ; goto end_takeconch; }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || @@ -65214,7 +65346,9 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ for(i=2; iaReadMark[i] = READMARK_NOT_USED; assert( pInfo->aReadMark[0]==0 ); } +static int checkHeaderValid(Pager *pager, u8 *zBuf, const char *logStr); +static int checkDbHeaderValid(sqlite3 *db, int iDbpage, u8 *zBuf); /* ** Copy as much content as we can from the WAL back into the database file ** in response to an sqlite3_wal_checkpoint() request or the equivalent. @@ -65355,6 +65489,8 @@ static int walCheckpoint( if( rc!=SQLITE_OK ) break; iOffset = (iDbpage-1)*(i64)szPage; testcase( IS_BIG_INT(iOffset) ); + rc = checkDbHeaderValid(db, iDbpage, zBuf); + if( rc!=SQLITE_OK ) break; rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset); if( rc!=SQLITE_OK ) break; } @@ -66749,6 +66885,10 @@ static int walWriteOneFrame( int rc; /* Result code from subfunctions */ void *pData; /* Data actually written */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ + if( pPage->pgno==1 ){ + rc = checkHeaderValid(pPage->pPager, pPage->pData, "walWrite"); + if( rc!=SQLITE_OK ) return rc; + } #if defined(SQLITE_HAS_CODEC) if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; #else @@ -67174,6 +67314,15 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint( walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); pWal->ckptLock = 0; } +#ifdef LOG_DUMP + sqlite3_log(SQLITE_NOTICE, + "checkpoint res[%d], szPage[%u], minFrame[%u], readOnly[%u], exclusiveMode[%d]", + rc, + pWal->szPage, + pWal->minFrame, + pWal->readOnly, + pWal->exclusiveMode); +#endif WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); #ifdef SQLITE_ENABLE_SETLK_TIMEOUT if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; @@ -68550,6 +68699,30 @@ SQLITE_PRIVATE sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){ } #endif +static int checkHeaderValid(Pager *pager, u8 *zBuf, const char *logStr){ +#ifdef SQLITE_HAS_CODEC + if( pager==NULL || pager->pCodec ){ + return SQLITE_OK; + } +#endif + if( zBuf && strncmp((const char *)zBuf, zMagicHeader, 16)!=0 ){ + sqlite3_log(SQLITE_NOTADB, "[%s]wrong header format, memory might be overwritten!", logStr); + return SQLITE_NOTADB; + } + return SQLITE_OK; +} + +static int checkDbHeaderValid(sqlite3 *db, int iDbpage, u8 *zBuf){ + if( iDbpage==1 && db->aDb ){ + Btree *p = db->aDb[0].pBt; + if( p && p->pBt ){ + Pager *pager = sqlite3BtreePager(p); + return checkHeaderValid(pager, zBuf, "ckpt"); + } + } + return SQLITE_OK; +} + /* ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single ** (MemPage*) as an argument. The (MemPage*) must not be NULL. @@ -70380,6 +70553,23 @@ static int decodeFlags(MemPage *pPage, int flagByte){ pPage->intKeyLeaf = 0; pPage->xCellSize = cellSizePtr; pPage->xParseCell = btreeParseCellPtrIndex; +#ifdef LOG_DUMP + sqlite3_log(SQLITE_CORRUPT, + "database corruption at page[%u], flagByte[%x], isInit[%u], intKey[%u], intKeyLeaf[%u], leaf[%u], " + "childPtrSize[%u], cellOffset[%u], nCell[%u], hdrOffset[%u], minLocal, maxLocal[%u]", + pPage->pgno, + flagByte, + pPage->isInit, + pPage->intKey, + pPage->intKeyLeaf, + pPage->leaf, + pPage->childPtrSize, + pPage->cellOffset, + pPage->nCell, + pPage->hdrOffset, + pPage->minLocal, + pPage->maxLocal); +#endif return SQLITE_CORRUPT_PAGE(pPage); } pPage->max1bytePayload = pBt->max1bytePayload; -- Gitee From 56fd0c30cf9d8c84881deb9ef00e158b7684e4a1 Mon Sep 17 00:00:00 2001 From: sunboyu Date: Mon, 19 Aug 2024 21:22:47 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9log=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9fdsan=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=86=97=E4=BD=99=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: = --- src/sqlite3.c | 87 ++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 50 deletions(-) diff --git a/src/sqlite3.c b/src/sqlite3.c index a610bf7..35ee1bd 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -37138,7 +37138,24 @@ static pid_t randomnessPid = 0; ** which always has the same well-defined interface. */ static int posixOpen(const char *zFile, int flags, int mode){ - return open(zFile, flags, mode); + int fd = open(zFile, flags, mode); +#ifdef FDSAN_ENABLE + if ( fd > 0 && fdsan_get_owner_tag(fd) == 0 ) { + fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); + } +#endif + return fd; +} + +/* +** Change close to posixClose, use fdsan_close_with_tag when fdsan enable. +*/ +static int posixClose(int fd) { +#ifdef FDSAN_ENABLE + return fdsan_close_with_tag(fd, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); +#else + return close(fd); +#endif } /* Forward reference */ @@ -37159,7 +37176,7 @@ static struct unix_syscall { { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) - { "close", (sqlite3_syscall_ptr)close, 0 }, + { "close", (sqlite3_syscall_ptr)posixClose, 0 }, #define osClose ((int(*)(int))aSyscall[1].pCurrent) { "access", (sqlite3_syscall_ptr)access, 0 }, @@ -37455,20 +37472,11 @@ static int robust_open(const char *z, int f, mode_t m){ if( errno==EINTR ) continue; break; } -#ifdef FDSAN_ENABLE - if( fdsan_get_owner_tag(fd)==0 ){ - fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); - } -#endif if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; if( (f & (O_EXCL|O_CREAT))==(O_EXCL|O_CREAT) ){ (void)osUnlink(z); } -#ifdef FDSAN_ENABLE - fdsan_close_with_tag(fd, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); -#else osClose(fd); -#endif sqlite3_log(SQLITE_WARNING, "attempt to open \"%s\" as file descriptor %d", z, fd); fd = -1; @@ -38067,13 +38075,7 @@ static int unixLogErrorAtLine( ** and move on. */ static void robust_close(unixFile *pFile, int h, int lineno){ - int rc; -#ifdef FDSAN_ENABLE - rc = fdsan_close_with_tag(h, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); -#else - rc = osClose(h); -#endif - if( rc ){ + if( osClose(h) ){ unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", pFile ? pFile->zPath : 0, lineno); } @@ -40192,21 +40194,18 @@ static int unixRead( case EDEVERR: #endif #ifdef LOG_DUMP - sqlite3_log(SQLITE_IOERR_CORRUPTFS, "unixRead-EDEVERR, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); + sqlite3_log(SQLITE_IOERR_CORRUPTFS, "unixRead-EDEVERR, fd:[%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); #endif return SQLITE_IOERR_CORRUPTFS; } #ifdef LOG_DUMP - sqlite3_log(SQLITE_IOERR_READ, "unixRead-got<0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); + sqlite3_log(SQLITE_IOERR_READ, "unixRead-got<0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); #endif return SQLITE_IOERR_READ; }else{ storeLastErrno(pFile, 0); /* not a system error */ /* Unread parts of the buffer must be zero-filled */ memset(&((char*)pBuf)[got], 0, amt-got); -#ifdef LOG_DUMP - sqlite3_log(SQLITE_IOERR_SHORT_READ, "unixRead-got>=0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->h, amt, got, offset); -#endif return SQLITE_IOERR_SHORT_READ; } } @@ -40595,12 +40594,6 @@ static int unixSync(sqlite3_file *id, int flags){ HAVE_FULLFSYNC, isFullsync)); rc = osOpenDirectory(pFile->zPath, &dirfd); if( rc==SQLITE_OK ){ -#ifdef FDSAN_ENABLE - // FDSAN_OWNER_TYPE_FILE is used because files and directories are not distinguished. - if( fdsan_get_owner_tag(dirfd)==0 ){ - fdsan_exchange_owner_tag(dirfd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); - } -#endif full_fsync(dirfd, 0, 0); robust_close(pFile, dirfd, __LINE__); }else{ @@ -40726,14 +40719,14 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ if( err && err!=EINVAL ){ #ifdef LOG_DUMP sqlite3_log(SQLITE_IOERR_WRITE, - "fcntlSizeHint-fallocate, fd[%d], bufSize[%lld], nSize[%lld] err[%d]", - pFile->h, - buf.st_size, - nSize, - err); + "fcntlSizeHint-fallocate, fd[%d], bufSize[%lld], nSize[%lld] err[%d]", + pFile->h, + buf.st_size, + nSize, + err); #endif - return SQLITE_IOERR_WRITE; - } + return SQLITE_IOERR_WRITE; + } #else /* If the OS does not have posix_fallocate(), fake it. Write a ** single byte to the last byte in each block that falls entirely @@ -43266,12 +43259,6 @@ static int unixDelete( int fd; rc = osOpenDirectory(zPath, &fd); if( rc==SQLITE_OK ){ -#ifdef FDSAN_ENABLE - // FDSAN_OWNER_TYPE_FILE is used because files and directories are not distinguished. - if( fdsan_get_owner_tag(fd)==0 ){ - fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); - } -#endif if( full_fsync(fd,0,0) ){ rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); } @@ -44215,7 +44202,7 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ if( len<0 ){ storeLastErrno(pFile, errno); #ifdef LOG_DUMP - sqlite3_log(SQLITE_IOERR_LOCK, "proxyConchLock tries 2, pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno); + sqlite3_log(SQLITE_IOERR_LOCK, "proxyConchLock tries 2, pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno); #endif return SQLITE_IOERR_LOCK; } @@ -44292,7 +44279,7 @@ static int proxyTakeConch(unixFile *pFile){ /* I/O error: lastErrno set by seekAndRead */ storeLastErrno(pFile, conchFile->lastErrno); #ifdef LOG_DUMP - sqlite3_log(SQLITE_IOERR_READ, "proxyTakeConch fd:[%d], pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, conchFile->lastErrno); + sqlite3_log(SQLITE_IOERR_READ, "proxyTakeConch pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, conchFile->lastErrno); #endif rc = SQLITE_IOERR_READ; goto end_takeconch; @@ -67316,12 +67303,12 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint( } #ifdef LOG_DUMP sqlite3_log(SQLITE_NOTICE, - "checkpoint res[%d], szPage[%u], minFrame[%u], readOnly[%u], exclusiveMode[%d]", - rc, - pWal->szPage, - pWal->minFrame, - pWal->readOnly, - pWal->exclusiveMode); + "checkpoint res[%d], szPage[%u], minFrame[%u], readOnly[%u], exclusiveMode[%d]", + rc, + pWal->szPage, + pWal->minFrame, + pWal->readOnly, + pWal->exclusiveMode); #endif WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); #ifdef SQLITE_ENABLE_SETLK_TIMEOUT -- Gitee From b2926aa6d754bd7a4932d0d118941aed44196394 Mon Sep 17 00:00:00 2001 From: boris-sun Date: Tue, 27 Aug 2024 12:09:29 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9B=91=E6=8E=A7=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?wal=20checkpoint=E6=88=90=E5=8A=9F=E6=97=B6=E6=97=A5=E5=BF=97?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=B8=BA=E8=AE=B0=E5=BD=95=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwtmichael Signed-off-by: boris-sun --- BUILD.gn | 2 ++ src/sqlite3.c | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 18d9f04..ff13f50 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -65,6 +65,7 @@ ohos_shared_library("sqlite") { "OPENSSL_SUPPRESS_DEPRECATED", "LOG_DUMP", "FDSAN_ENABLE", + "HARMONY_OS", ] cflags_c = [ "-fvisibility=hidden", @@ -127,6 +128,7 @@ ohos_executable("sqlite3") { "OPENSSL_SUPPRESS_DEPRECATED", "LOG_DUMP", "FDSAN_ENABLE", + "HARMONY_OS", ] cflags = [ diff --git a/src/sqlite3.c b/src/sqlite3.c index 35ee1bd..21c0bea 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -37128,6 +37128,30 @@ static pid_t randomnessPid = 0; #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 #endif /* __linux__ */ +#ifdef HARMONY_OS +#define HMFS_MONITOR_FL 0x00000002 +#define HMFS_IOCTL_HW_GET_FLAGS _IOR(0xf5, 70, unsigned int) +#define HMFS_IOCTL_HW_SET_FLAGS _IOR(0xf5, 71, unsigned int) + +static void enableDbFileDelMonitor(int32_t fd) +{ + unsigned int flags = 0; + int ret = ioctl(fd, HMFS_IOCTL_HW_GET_FLAGS, &flags); + if (ret < 0) { + sqlite3_log(SQLITE_WARNING, "Not support fd %d enable del monitor errno = %d", fd, errno); + return; + } + if (flags & HMFS_MONITOR_FL) { + return; + } + flags |= HMFS_MONITOR_FL; + ret = ioctl(fd, HMFS_IOCTL_HW_SET_FLAGS, &flags); + if (ret < 0) { + sqlite3_log(SQLITE_WARNING, "Fd %d enable del monitor go wrong, errno = %d", fd, errno); + } +} + +#endif /* __HARMONY_OS__ */ /* ** Different Unix systems declare open() in different ways. Same use @@ -37143,6 +37167,9 @@ static int posixOpen(const char *zFile, int flags, int mode){ if ( fd > 0 && fdsan_get_owner_tag(fd) == 0 ) { fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); } +#endif +#ifdef HARMONY_OS + enableDbFileDelMonitor(fd); #endif return fd; } @@ -67178,6 +67205,8 @@ SQLITE_PRIVATE int sqlite3WalFrames( return rc; } +static sqlite3_int64 g_lastCkptTime = 0; + /* ** This routine is called to implement sqlite3_wal_checkpoint() and ** related interfaces. @@ -67302,13 +67331,10 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint( pWal->ckptLock = 0; } #ifdef LOG_DUMP - sqlite3_log(SQLITE_NOTICE, - "checkpoint res[%d], szPage[%u], minFrame[%u], readOnly[%u], exclusiveMode[%d]", - rc, - pWal->szPage, - pWal->minFrame, - pWal->readOnly, - pWal->exclusiveMode); + if( rc ){ + sqlite3_log(SQLITE_NOTICE, "ckpt rc[%d]", rc); + } + sqlite3OsCurrentTimeInt64(db->pVfs, &g_lastCkptTime); #endif WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); #ifdef SQLITE_ENABLE_SETLK_TIMEOUT @@ -70543,7 +70569,7 @@ static int decodeFlags(MemPage *pPage, int flagByte){ #ifdef LOG_DUMP sqlite3_log(SQLITE_CORRUPT, "database corruption at page[%u], flagByte[%x], isInit[%u], intKey[%u], intKeyLeaf[%u], leaf[%u], " - "childPtrSize[%u], cellOffset[%u], nCell[%u], hdrOffset[%u], minLocal, maxLocal[%u]", + "childPtrSize[%u], cellOffset[%u], nCell[%u], hdrOffset[%u], minLocal[%u], maxLocal[%u], last ckpt time[%lld]", pPage->pgno, flagByte, pPage->isInit, @@ -70555,7 +70581,8 @@ static int decodeFlags(MemPage *pPage, int flagByte){ pPage->nCell, pPage->hdrOffset, pPage->minLocal, - pPage->maxLocal); + pPage->maxLocal, + g_lastCkptTime); #endif return SQLITE_CORRUPT_PAGE(pPage); } -- Gitee From 1c20d4bdd5b8d4bb6c0b4659c3ac9de62b65c44e Mon Sep 17 00:00:00 2001 From: boris-sun Date: Thu, 29 Aug 2024 16:29:15 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=BB=84=E5=8C=BA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: boris-sun --- src/sqlite3.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/sqlite3.c b/src/sqlite3.c index 21c0bea..a6c4f41 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -37138,7 +37138,6 @@ static void enableDbFileDelMonitor(int32_t fd) unsigned int flags = 0; int ret = ioctl(fd, HMFS_IOCTL_HW_GET_FLAGS, &flags); if (ret < 0) { - sqlite3_log(SQLITE_WARNING, "Not support fd %d enable del monitor errno = %d", fd, errno); return; } if (flags & HMFS_MONITOR_FL) { @@ -37164,12 +37163,14 @@ static void enableDbFileDelMonitor(int32_t fd) static int posixOpen(const char *zFile, int flags, int mode){ int fd = open(zFile, flags, mode); #ifdef FDSAN_ENABLE - if ( fd > 0 && fdsan_get_owner_tag(fd) == 0 ) { - fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); + if( fd >= 0 ){ + fdsan_exchange_owner_tag(fd, 0, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, SQLITE_FDSAN_TAG)); } #endif #ifdef HARMONY_OS - enableDbFileDelMonitor(fd); + if( fd >= 0 ){ + enableDbFileDelMonitor(fd); + } #endif return fd; } -- Gitee From b9eced6139e9f8c2dd5f5e1b728907295ea82e31 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Sat, 31 Aug 2024 17:19:44 +0800 Subject: [PATCH 5/6] fix a stack overflow that could be caused by a recursively defined Signed-off-by: zwtmichael --- src/sqlite3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sqlite3.c b/src/sqlite3.c index 21c0bea..91862df 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -104825,7 +104825,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sqlite3WalkExprList(pWalker, pList); if( is_agg ){ #ifndef SQLITE_OMIT_WINDOWFUNC - if( pWin ){ + if( pWin && pParse->nErr==0 ){ Select *pSel = pNC->pWinSelect; assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) ); if( IN_RENAME_OBJECT==0 ){ -- Gitee From b0317531f2afe0ac4c733b92a16b17f553ec8321 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Tue, 10 Sep 2024 20:20:25 +0800 Subject: [PATCH 6/6] add pac ret Signed-off-by: zwtmichael --- BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/BUILD.gn b/BUILD.gn index ff13f50..570cebb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -32,6 +32,7 @@ group("libsqlite") { } ohos_shared_library("sqlite") { + branch_protector_ret = "pac_ret" sources = [ "src/sqlite3.c" ] defines = [ -- Gitee