From 8ba764448d79d4c54e2042de076ead555153db2a Mon Sep 17 00:00:00 2001 From: ryne3366 Date: Fri, 8 Aug 2025 16:30:53 +0800 Subject: [PATCH] Fix meta dwr overwrite Signed-off-by: ryne3366 --- patch/0001-History-features-on-OH.patch | 4 +- patch/0006-Support-Binlog.patch | 2 +- patch/0011-Support-compress-db.patch | 2 +- patch/0012-Bugfix-on-current-version.patch | 38 ++++--- unittest/BUILD.gn | 1 + unittest/sqlite_metadwr_test.cpp | 109 +++++++++++++++++++++ 6 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 unittest/sqlite_metadwr_test.cpp diff --git a/patch/0001-History-features-on-OH.patch b/patch/0001-History-features-on-OH.patch index 9b09c15..84d4bc3 100644 --- a/patch/0001-History-features-on-OH.patch +++ b/patch/0001-History-features-on-OH.patch @@ -3011,7 +3011,7 @@ index 730b247..b132937 100644 +}; + +typedef struct sqlite3_api_routines_extra sqlite3_api_routines_extra; -+static const sqlite3_api_routines_extra sqlite3HwApis = { ++static const sqlite3_api_routines_extra sqlite3ExtraApis = { + sqlite3_initialize, + sqlite3_config, +#ifdef SQLITE_HAS_CODEC @@ -3028,7 +3028,7 @@ index 730b247..b132937 100644 +}; + +EXPORT_SYMBOLS const sqlite3_api_routines *sqlite3_export_symbols = &sqlite3Apis; -+EXPORT_SYMBOLS const sqlite3_api_routines_extra *sqlite3_export_extra_symbols = &sqlite3HwApis; ++EXPORT_SYMBOLS const sqlite3_api_routines_extra *sqlite3_export_extra_symbols = &sqlite3ExtraApis; +/************** End export the symbols *****************************************/ +#endif /* SQLITE_EXPORT_SYMBOLS */ -- diff --git a/patch/0006-Support-Binlog.patch b/patch/0006-Support-Binlog.patch index e7ffebe..99a55fa 100644 --- a/patch/0006-Support-Binlog.patch +++ b/patch/0006-Support-Binlog.patch @@ -1765,7 +1765,7 @@ index e7e8b3c..3f1195a 100644 }; typedef struct sqlite3_api_routines_extra sqlite3_api_routines_extra; -@@ -256952,13 +258433,22 @@ static const sqlite3_api_routines_extra sqlite3HwApis = { +@@ -256952,13 +258433,22 @@ static const sqlite3_api_routines_extra sqlite3ExtraApis = { sqlite3_key, sqlite3_key_v2, sqlite3_rekey, diff --git a/patch/0011-Support-compress-db.patch b/patch/0011-Support-compress-db.patch index c16336e..9ff6880 100644 --- a/patch/0011-Support-compress-db.patch +++ b/patch/0011-Support-compress-db.patch @@ -2083,7 +2083,7 @@ index c84348b..1abddb3 100644 }; typedef struct sqlite3_api_routines_extra sqlite3_api_routines_extra; -@@ -258641,6 +259073,11 @@ static const sqlite3_api_routines_extra sqlite3HwApis = { +@@ -258641,6 +259073,11 @@ static const sqlite3_api_routines_extra sqlite3ExtraApis = { 0, 0, #endif/* SQLITE_ENABLE_BINLOG */ diff --git a/patch/0012-Bugfix-on-current-version.patch b/patch/0012-Bugfix-on-current-version.patch index 380ce4e..244412c 100644 --- a/patch/0012-Bugfix-on-current-version.patch +++ b/patch/0012-Bugfix-on-current-version.patch @@ -1,4 +1,4 @@ -From cb6159523c20f6c9d0545d0a45b605743ef59c21 Mon Sep 17 00:00:00 2001 +From 2c64c2a700786ce5bf2cf0ad5a9f46ac725ff846 Mon Sep 17 00:00:00 2001 From: MartinChoo <214582617@qq.com> Date: Tue, 5 Aug 2025 22:40:40 +0800 Subject: [PATCH] Bugfix on current version @@ -7,8 +7,8 @@ Signed-off-by: MartinChoo <214582617@qq.com> --- ext/misc/cksumvfs.c | 11 +- src/compressvfs.c | 5 +- - src/sqlite3.c | 297 +++++++++++++++++++++++++++++++++++--------- - 3 files changed, 249 insertions(+), 64 deletions(-) + src/sqlite3.c | 302 +++++++++++++++++++++++++++++++++++--------- + 3 files changed, 253 insertions(+), 65 deletions(-) diff --git a/ext/misc/cksumvfs.c b/ext/misc/cksumvfs.c index 27b1028..e89edcd 100644 @@ -83,7 +83,7 @@ index f2fe169..b6f1681 100644 sqlite3_busy_timeout(db, 2000); // Set time out:2s rc = sqlite3_exec(db, pre_pragma, NULL, NULL, NULL); diff --git a/src/sqlite3.c b/src/sqlite3.c -index d5d6627..2bd0b71 100644 +index d5d6627..c491821 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -5363,7 +5363,7 @@ SQLITE_API int sqlite3_set_droptable_handle(sqlite3*, void (*xFunc)(sqlite3*,con @@ -520,7 +520,19 @@ index d5d6627..2bd0b71 100644 static void MetaDwrUpdateHeaderDbInfo(BtShared *pBt) { MetaDwrHdr *hdr = pBt->pPager->metaHdr; // 28 offset: dbSize, freelist pageNo, freelist pages count, schema cookie -@@ -257211,11 +257391,12 @@ static int MetaDwrOpenFile(Pager *pPager, u8 openCreate) { +@@ -257146,7 +257326,10 @@ static int MetaDwrRestoreAllPages(Btree *pBt, const ScanPages *metaPages, MetaDw + return rc; + } + } +- hdr->pageCnt = metaPages->pageCnt; ++ hdr->pageCnt = i; ++ if (metaPages->pageCnt > META_DWR_MAX_PAGES) { ++ sqlite3_log(SQLITE_WARNING_DUMP, "Meta dwr restore pages %u out of range", metaPages->pageCnt); ++ } + MetaDwrUpdateHeaderDbInfo(pBt->pBt); + return rc; + } +@@ -257211,11 +257394,12 @@ static int MetaDwrOpenFile(Pager *pPager, u8 openCreate) { if (pPager->metaMapPage == NULL) { sqlite3_int64 sz = META_DWR_HEADER_PAGE_SIZE; sqlite3OsFileControlHint(metaFd, SQLITE_FCNTL_CHUNK_SIZE, &sz); @@ -538,7 +550,7 @@ index d5d6627..2bd0b71 100644 } } #endif /* SQLITE_OS_UNIX */ -@@ -257589,7 +257770,7 @@ CHK_RESTORE_OUT: +@@ -257589,7 +257773,7 @@ CHK_RESTORE_OUT: return rc; } @@ -547,7 +559,7 @@ index d5d6627..2bd0b71 100644 { #if SQLITE_OS_UNIX u8 checkFileId = Sqlite3GetCheckFileId(pPager->pVfs); -@@ -257597,10 +257778,18 @@ static inline u8 IsConnectionValidForCheck(Pager *pPager) +@@ -257597,10 +257781,18 @@ static inline u8 IsConnectionValidForCheck(Pager *pPager) return 0; } unixFile *fd = Sqlite3GetUnixFile(pPager->fd, checkFileId); @@ -567,7 +579,7 @@ index d5d6627..2bd0b71 100644 return 1; #else return 0; -@@ -257610,7 +257799,7 @@ static inline u8 IsConnectionValidForCheck(Pager *pPager) +@@ -257610,7 +257802,7 @@ static inline u8 IsConnectionValidForCheck(Pager *pPager) static int MetaDwrOpenAndCheck(Btree *pBt) { Pager *pPager = pBt->pBt->pPager; @@ -576,7 +588,7 @@ index d5d6627..2bd0b71 100644 return SQLITE_OK; } #ifdef SQLITE_HAS_CODEC -@@ -257655,7 +257844,7 @@ DWR_OPEN_OUT: +@@ -257655,7 +257847,7 @@ DWR_OPEN_OUT: static void MetaDwrDisable(Btree *pBt) { Pager *pPager = pBt->pBt->pPager; @@ -585,7 +597,7 @@ index d5d6627..2bd0b71 100644 return; } #ifdef SQLITE_HAS_CODEC -@@ -257681,19 +257870,6 @@ static void MetaDwrDisable(Btree *pBt) +@@ -257681,19 +257873,6 @@ static void MetaDwrDisable(Btree *pBt) #endif /* SQLITE_META_DWR */ #if SQLITE_OS_UNIX @@ -605,7 +617,7 @@ index d5d6627..2bd0b71 100644 static void ResetLockStatus(void) { (void)memset(&g_lockStatus, 0, sizeof(g_lockStatus)); -@@ -257797,8 +257973,13 @@ static inline const char *FlockToName(int l_type) +@@ -257797,8 +257976,13 @@ static inline const char *FlockToName(int l_type) static int DumpProcessLocks(int fd, struct flock *lock, const char *lockName, char *dumpBuf, int bufLen) { @@ -620,7 +632,7 @@ index d5d6627..2bd0b71 100644 sqlite3_log(SQLITE_ERROR, "[SQLite]Get wal file lock ofs %u failed, errno: %d", lock->l_start, errno); return 0; } -@@ -259039,9 +259220,13 @@ struct sqlite3_api_routines_extra { +@@ -259039,9 +259223,13 @@ struct sqlite3_api_routines_extra { int (*key_v2)(sqlite3*,const char*,const void*,int); int (*rekey)(sqlite3*,const void*,int); int (*rekey_v2)(sqlite3*,const char*,const void*,int); @@ -636,5 +648,5 @@ index d5d6627..2bd0b71 100644 int (*compressdb_backup)(sqlite3*, const char*); #else -- -2.47.0.windows.2 +2.28.0.windows.1 diff --git a/unittest/BUILD.gn b/unittest/BUILD.gn index a9250f3..93864a1 100644 --- a/unittest/BUILD.gn +++ b/unittest/BUILD.gn @@ -37,6 +37,7 @@ ohos_unittest("libsqlittest") { "./common.cpp", "./sqlite_cksum_test.cpp", "./sqlite_compress_test.cpp", + "./sqlite_metadwr_test.cpp", "./sqlite_test.cpp", ] diff --git a/unittest/sqlite_metadwr_test.cpp b/unittest/sqlite_metadwr_test.cpp new file mode 100644 index 0000000..6f6a828 --- /dev/null +++ b/unittest/sqlite_metadwr_test.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "sqlite3sym.h" + +using namespace testing::ext; +using namespace std; + +#define TEST_DIR "./sqlitetest" +#define TEST_DB (TEST_DIR "/test.db") +#define TEST_DWR (TEST_DIR "/test.db-dwr") + +static void UtSqliteLogPrint(const void *data, int err, const char *msg) +{ + std::cout << "LibSQLiteMetaDwrTest SQLite xLog err:" << err << ", msg:" << msg << std::endl; +} + +class LibSQLiteMetaDwrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LibSQLiteMetaDwrTest::SetUpTestCase(void) +{ + // permission 0770 + mkdir(TEST_DIR, 0770); +} + +void LibSQLiteMetaDwrTest::TearDownTestCase(void) +{ +} + +void LibSQLiteMetaDwrTest::SetUp(void) +{ + unlink(TEST_DB); + unlink(TEST_DWR); + sqlite3_config(SQLITE_CONFIG_LOG, &UtSqliteLogPrint, NULL); +} + +void LibSQLiteMetaDwrTest::TearDown(void) +{ + sqlite3_config(SQLITE_CONFIG_LOG, NULL, NULL); +} + +/** + * @tc.name: Lib_SQLite_Meta_Dwr_Test_001 + * @tc.desc: Test write meta dwr pages out of range. + * @tc.type: FUNC + */ +HWTEST_F(LibSQLiteMetaDwrTest, Lib_SQLite_Meta_Dwr_Test_001, TestSize.Level2) +{ + /** + * @tc.steps: step1. Open database + * @tc.expected: step1. Return SQLITE_OK + */ + sqlite3 *db = NULL; + EXPECT_EQ(sqlite3_open(TEST_DB, &db), SQLITE_OK); + /** + * @tc.steps: step2. Enable meta dwr + * @tc.expected: step2. Execute successfully + */ + EXPECT_EQ(sqlite3_exec(db, "pragma meta_double_write=enabled", NULL, NULL, NULL), SQLITE_OK); + /** + * @tc.steps: step3. Create tables to reach max meta dwr pages + * @tc.expected: step3. Execute successfully + */ + std::string tableNamePrefix("TestTableInfoForMetaDwr"); + // table tail 4000 + tableNamePrefix += std::string(4000, 'a'); + // meta dwr pages correspond to 240 table info + for (int i = 0; i < 240; i++) { + std::string cteateTable = std::string("CREATE TABLE ") + tableNamePrefix + to_string(i) + + "(id INTEGER PRIMARY KEY, name TEXT);"; + EXPECT_EQ(sqlite3_exec(db, cteateTable.c_str(), NULL, NULL, NULL), SQLITE_OK); + } + sqlite3_close(db); + /** + * @tc.steps: step4. Expect meta dwr file limit size + * @tc.expected: step4. Execute successfully + */ + struct stat statbuf; + EXPECT_EQ(stat(TEST_DWR, &statbuf), 0); + // max 1001 pages and page size 4096 + EXPECT_EQ(statbuf.st_size, 1001 * 4096); +} -- Gitee