From d81a346d7436fc7c12e4ee5a7f9b077c4cd4cf53 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 17 Jul 2024 12:26:23 +0300 Subject: [PATCH 1/5] sqlite3: Implement sqlite3_{initialize,shutdown} They're no-ops for now. --- sqlite3/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlite3/src/lib.rs b/sqlite3/src/lib.rs index 25f31cff0..3f6475fad 100644 --- a/sqlite3/src/lib.rs +++ b/sqlite3/src/lib.rs @@ -38,12 +38,12 @@ impl<'a> sqlite3_stmt<'a> { #[no_mangle] pub unsafe extern "C" fn sqlite3_initialize() -> ffi::c_int { - todo!(); + SQLITE_OK } #[no_mangle] pub unsafe extern "C" fn sqlite3_shutdown() -> ffi::c_int { - todo!(); + SQLITE_OK } #[no_mangle] From 8fb50cc9bc29ef7d7e7e33b7b038a5b6ca936459 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 17 Jul 2024 12:27:41 +0300 Subject: [PATCH 2/5] sqlite3: Auto initialize in sqlite3_open() --- sqlite3/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sqlite3/src/lib.rs b/sqlite3/src/lib.rs index 3f6475fad..e060bc8b4 100644 --- a/sqlite3/src/lib.rs +++ b/sqlite3/src/lib.rs @@ -51,6 +51,10 @@ pub unsafe extern "C" fn sqlite3_open( filename: *const ffi::c_char, db_out: *mut *mut sqlite3, ) -> ffi::c_int { + let rc = sqlite3_initialize(); + if rc != SQLITE_OK { + return rc; + } if filename.is_null() { return SQLITE_MISUSE; } From 4efa6e5efa7f8776799d817374cdd67a8bad3221 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 17 Jul 2024 12:33:55 +0300 Subject: [PATCH 3/5] sqlite3: API function tracing --- Cargo.lock | 27 ++++++++++++++++++++++++++- sqlite3/Cargo.toml | 2 ++ sqlite3/src/lib.rs | 10 ++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 558a8da50..9d178321f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -526,6 +526,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" version = "0.10.2" @@ -539,6 +549,19 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -952,7 +975,7 @@ dependencies = [ "clap 4.5.8", "cli-table", "dirs", - "env_logger", + "env_logger 0.10.2", "limbo_core", "rustyline", ] @@ -1006,7 +1029,9 @@ name = "limbo_sqlite3" version = "0.0.0" dependencies = [ "cbindgen", + "env_logger 0.11.3", "limbo_core", + "log", ] [[package]] diff --git a/sqlite3/Cargo.toml b/sqlite3/Cargo.toml index 1a4f0ee13..1ea36f305 100644 --- a/sqlite3/Cargo.toml +++ b/sqlite3/Cargo.toml @@ -16,4 +16,6 @@ doc = false cbindgen = "0.24.0" [dependencies] +env_logger = "0.11.3" limbo_core = { path = "../core" } +log = "0.4.22" diff --git a/sqlite3/src/lib.rs b/sqlite3/src/lib.rs index e060bc8b4..f724e6ab8 100644 --- a/sqlite3/src/lib.rs +++ b/sqlite3/src/lib.rs @@ -1,6 +1,7 @@ #![allow(clippy::missing_safety_doc)] #![allow(non_camel_case_types)] +use log::trace; use std::cell::RefCell; use std::ffi; use std::rc::Rc; @@ -36,8 +37,13 @@ impl<'a> sqlite3_stmt<'a> { } } +static INIT_DONE: std::sync::Once = std::sync::Once::new(); + #[no_mangle] pub unsafe extern "C" fn sqlite3_initialize() -> ffi::c_int { + INIT_DONE.call_once(|| { + env_logger::init(); + }); SQLITE_OK } @@ -51,6 +57,7 @@ pub unsafe extern "C" fn sqlite3_open( filename: *const ffi::c_char, db_out: *mut *mut sqlite3, ) -> ffi::c_int { + trace!("sqlite3_open"); let rc = sqlite3_initialize(); if rc != SQLITE_OK { return rc; @@ -87,11 +94,13 @@ pub unsafe extern "C" fn sqlite3_open_v2( _flags: ffi::c_int, _z_vfs: *const ffi::c_char, ) -> ffi::c_int { + trace!("sqlite3_open_v2"); sqlite3_open(filename, db_out) } #[no_mangle] pub unsafe extern "C" fn sqlite3_close(db: *mut sqlite3) -> ffi::c_int { + trace!("sqlite3_close"); if db.is_null() { return SQLITE_OK; } @@ -101,6 +110,7 @@ pub unsafe extern "C" fn sqlite3_close(db: *mut sqlite3) -> ffi::c_int { #[no_mangle] pub unsafe extern "C" fn sqlite3_close_v2(db: *mut sqlite3) -> ffi::c_int { + trace!("sqlite3_close_v2"); sqlite3_close(db) } From a08051b1063a26dc90e8092c114c68e899bb7dec Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 17 Jul 2024 12:44:04 +0300 Subject: [PATCH 4/5] sqlite3: Disable tests that SIGSEGV with sqlite3 on x86 --- sqlite3/tests/test-open.c | 5 +++-- sqlite3/tests/test-prepare.c | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sqlite3/tests/test-open.c b/sqlite3/tests/test-open.c index 78508a6d3..fe8d7ddc2 100644 --- a/sqlite3/tests/test-open.c +++ b/sqlite3/tests/test-open.c @@ -7,9 +7,10 @@ void test_open_misuse(void) { - CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open(NULL, NULL)); + // TODO: SIGSEGV with sqlite3 +// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open(NULL, NULL)); - CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open("local.db", NULL)); +// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open("local.db", NULL)); } void test_open_not_found(void) diff --git a/sqlite3/tests/test-prepare.c b/sqlite3/tests/test-prepare.c index d9f358273..7fa978da3 100644 --- a/sqlite3/tests/test-prepare.c +++ b/sqlite3/tests/test-prepare.c @@ -12,13 +12,16 @@ void test_prepare_misuse(void) CHECK_EQUAL(SQLITE_OK, sqlite3_open("../../testing/testing.db", &db)); // Database handle is NULL. - CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(NULL, "SELECT 1", -1, NULL, NULL)); + // TODO: SIGSEGV with sqlite3 +// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(NULL, "SELECT 1", -1, NULL, NULL)); // Output statement is NULL. - CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", -1, NULL, NULL)); + // TODO: SIGSEGV with sqlite3 +// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", -1, NULL, NULL)); // SQL string length is too short, truncating the statement. - CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", 7, NULL, NULL)); + // TODO: SIGSEGV with sqlite3 +// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", 7, NULL, NULL)); CHECK_EQUAL(SQLITE_OK, sqlite3_close(db)); } From af7b8b6768e7436d11b49d3b02a061640b6c2dba Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 17 Jul 2024 12:53:55 +0300 Subject: [PATCH 5/5] sqlite3: Implement some auxiliary functions This adds `sqlite3_libversion()`, `sqlite3_libversion_number()`, and `libsql3_threadsafe()`. --- sqlite3/src/lib.rs | 8 +++++--- sqlite3/tests/Makefile | 1 + sqlite3/tests/main.c | 4 ++++ sqlite3/tests/test-aux.c | 11 +++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 sqlite3/tests/test-aux.c diff --git a/sqlite3/src/lib.rs b/sqlite3/src/lib.rs index f724e6ab8..aad33b792 100644 --- a/sqlite3/src/lib.rs +++ b/sqlite3/src/lib.rs @@ -773,15 +773,17 @@ pub unsafe extern "C" fn sqlite3_complete(_sql: *const std::ffi::c_char) -> ffi: #[no_mangle] pub unsafe extern "C" fn sqlite3_threadsafe() -> ffi::c_int { - todo!(); + 1 } #[no_mangle] pub unsafe extern "C" fn sqlite3_libversion() -> *const std::ffi::c_char { - todo!(); + ffi::CStr::from_bytes_with_nul(b"3.42.0\0") + .unwrap() + .as_ptr() } #[no_mangle] pub unsafe extern "C" fn sqlite3_libversion_number() -> ffi::c_int { - todo!(); + 3042000 } diff --git a/sqlite3/tests/Makefile b/sqlite3/tests/Makefile index 84beb2307..1450fb499 100644 --- a/sqlite3/tests/Makefile +++ b/sqlite3/tests/Makefile @@ -15,6 +15,7 @@ CFLAGS = -g -Wall -std=c17 -MMD -MP LIBS ?= -lsqlite3 OBJS += main.o +OBJS += test-aux.o OBJS += test-close.o OBJS += test-open.o OBJS += test-prepare.o diff --git a/sqlite3/tests/main.c b/sqlite3/tests/main.c index 58590ea84..0166aa860 100644 --- a/sqlite3/tests/main.c +++ b/sqlite3/tests/main.c @@ -1,3 +1,5 @@ +extern void test_libversion(); +extern void test_libversion_number(); extern void test_open_misuse(); extern void test_open_not_found(); extern void test_open_existing(); @@ -6,6 +8,8 @@ extern void test_prepare_misuse(); int main(int argc, char *argv[]) { + test_libversion(); + test_libversion_number(); test_open_misuse(); test_open_not_found(); test_open_existing(); diff --git a/sqlite3/tests/test-aux.c b/sqlite3/tests/test-aux.c new file mode 100644 index 000000000..77c1c3b7a --- /dev/null +++ b/sqlite3/tests/test-aux.c @@ -0,0 +1,11 @@ +#include "check.h" + +#include + +void test_libversion(void) { + sqlite3_libversion(); +} + +void test_libversion_number(void) { + sqlite3_libversion_number(); +}