Skip to content

Commit

Permalink
Patch SQLite to silence spurious C warnings and fix data race in sqli…
Browse files Browse the repository at this point in the history
…te3_last_insert_rowid() (#47)
  • Loading branch information
gwynne authored Mar 23, 2023
1 parent babbf08 commit a22db45
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/CSQLite/sqlite3.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma clang diagnostic ignored "-Wambiguous-macro"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version 3.41.2. By combining all the individual C code files into this
Expand Down Expand Up @@ -165427,7 +165429,10 @@ SQLITE_API sqlite_int64 sqlite_nio_sqlite3_last_insert_rowid(sqlite3 *db){
return 0;
}
#endif
return db->lastRowid;
sqlite_nio_sqlite3_mutex_enter(db->mutex);
i64 lastRowId = db->lastRowid;
sqlite_nio_sqlite3_mutex_leave(db->mutex);
return lastRowId;
}
/*
** Set the value returned by the sqlite_nio_sqlite3_last_insert_rowid() API function.
Expand Down
23 changes: 23 additions & 0 deletions scripts/001-warnings-and-data-race.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/Sources/CSQLite/sqlite3.c b/Sources/CSQLite/sqlite3.c
index 536e9b1..01a3eb8 100644
--- a/Sources/CSQLite/sqlite3.c
+++ b/Sources/CSQLite/sqlite3.c
@@ -1,3 +1,5 @@
+#pragma clang diagnostic ignored "-Wambiguous-macro"
+#pragma clang diagnostic ignored "-Wshorten-64-to-32"
/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version 3.41.2. By combining all the individual C code files into this
@@ -165427,7 +165429,10 @@ SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
return 0;
}
#endif
- return db->lastRowid;
+ sqlite3_mutex_enter(db->mutex);
+ i64 lastRowId = db->lastRowid;
+ sqlite3_mutex_leave(db->mutex);
+ return lastRowId;
}
/*
** Set the value returned by the sqlite_nio_sqlite3_last_insert_rowid() API function.

17 changes: 17 additions & 0 deletions scripts/vendor-sqlite3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ let sha3sum: URL!
let swift: URL!
let ar: URL!
let nm: URL!
let patch: URL!
do {
unzip = try await ensureExecutable("unzip")
sqlite3 = try await ensureExecutable("sqlite3")
sha3sum = try await ensureExecutable("sha3sum")
swift = try await ensureExecutable("swift")
ar = try await ensureExecutable("ar")
nm = try await ensureExecutable("nm")
patch = try await ensureExecutable("patch")

try await main()
} catch let error as String {
Expand Down Expand Up @@ -130,6 +132,7 @@ func bumpVersion(_ args: ArraySlice<String>) async throws {
expectedSize: product.sizeInBytes,
expectedSha3Sum: product.sha3Hash
)
try await applySQLitePatches()
try await addVendorPrefixToSQLite()
try product.version.stamp(from: product.downloadURL)
print("Upgraded from \(String(describing: currentVersion)) to \(product.version)")
Expand Down Expand Up @@ -185,6 +188,7 @@ func updateVersion(_ args: ArraySlice<String>) async throws {
let filename = "sqlite-amalgamation-\(version.asDownloadVersion).zip"
let sqliteDownloadURL = URL(string: "\(sqliteURL)/\(year)/\(filename)")!
try await downloadAndUnzipSQLite(from: sqliteDownloadURL, to: filename, expectedSize: nil, expectedSha3Sum: nil)
try await applySQLitePatches()
try await addVendorPrefixToSQLite()
try version.stamp(from: sqliteDownloadURL)
print("Upgraded from \(String(describing: currentVersion)) to \(version)")
Expand Down Expand Up @@ -440,6 +444,19 @@ func getSymbolsToPrefix() async throws -> [String] {
return sortedSymbolsToRewrite.filter { !exclude.contains($0) }
}

func applySQLitePatches() async throws {
try await subprocess(patch, [
"-d", root.path,
"-p1",
"-u",
"-V", "none",
"-i", root
.appendingPathComponent("scripts", isDirectory: true)
.appendingPathComponent("001-warnings-and-data-race.patch", isDirectory: false)
.path,
], captureStdout: false)
}

@discardableResult
func subprocess(_ executable: URL, _ arguments: [String], captureStdout: Bool) async throws -> String? {
let stdout = Pipe()
Expand Down

0 comments on commit a22db45

Please sign in to comment.