0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-03-07 07:15:38 +00:00

58 Commits

Author SHA1 Message Date
be1fca578f feat(vector): add basic functions (vector/vector_extract etc) ()
* add basic functions for working with f32/f64 vectors

* add header file

* fix comment style

* add generic code for working with vectors

* register vector functions

* fix edge cases in vector extract impl

* add static flag for vector (will be used later)

* add basic TCL-based tests

* guard vector functions with SQLITE_OMIT_VECTOR

* adjust build scripts

* delete libsql_vector_idx from this branch

* add one more test

* update bundles

* fix asserts

* fix constants (#define-s are universal but const int doesn't recognized by all compilers)

* update bundles

* review fixes

* build bundles

* add comment about linking math library
2024-07-08 08:27:33 +00:00
2402249b5d apply alter column command only to the table ()
* add alter column test

* apply alter column only to the table and ignore indices/triggers/views etc

* fix tests

* update bundle

* add test in rust_suite
2024-07-04 19:48:33 +00:00
b17b6ee0d4 adjust ALTER COLUMN parsing ()
* add basic TCL tests for ALTER COLUMN libsql feature

* propagate length of the new column definition directly from the parser

- this will allow libsql to automatically handle comment and space characters appended to the column definition

* small formatting fixes

* cargo xtask build-bundled

* add test against sqlite3 source in rust_suite

* fix test in rust_suite
2024-07-02 22:02:14 +00:00
355ec0aa16 fix overflow in row counters ()
* replace row read/written with saturating counter

* regenerate bundled sqlite

* fix invalid cast
2024-07-02 09:38:14 +00:00
95216464a1 add CI job to validate that bundle is up to date ()
* add CI job to validate that bundle is up to date

* cargo xtask build-bundled
2024-06-27 15:28:21 +00:00
47fef88f19 ffi: blocklist wasm fns from bindgen () 2024-03-07 20:13:39 +00:00
c8902c3bf6 libsql-sys: Disable SQLCipher support ()
The SQLCipher cipher does not work with frame injection:

https://github.com/tursodatabase/libsql/issues/1122

Therefore, disable the cipher for now and switch default to "aes256cbc".
2024-03-06 08:55:19 +00:00
49db7d57b7 Make sure the C code doesn't segfault ()
* make sure pager is valid

We have had a segfault issue recently because the pager passed down
to the c code was null. That happened because libsql-server had
encryption disabled, while libsql had encryption enabled.

To prevent issues like that in the future, the C code should be checking
if the arguments it needs are valid.

Because this function returned a pointer, we now change it so that it
can return an integer, with the pointer as a return argument.

* update generated code

We have generated code in-tree. We should either remove it, or keep it
in sync. For now, just keeping it in sync.

This patch is just the result of running tests, and seeing what changes.
2024-02-28 20:05:50 +00:00
52d8b8f1c4 Multi-scopes auth & Attach fix ()
* test snapshots

* allow multi-scope auth

* hors-sujet: fix C compile warning

* update jwt and add hashbrown feature

* replace Auth with RequestContext

* extract RequestContext

* fix auth check for multi-scopes

* pass NamespaceName in StmtKind::Attach

* fallback to global auth if key is not set in ns config

* add allow_attach to namespace creation

* return meta store from namespace store

* test attach

* fmt

* review edits
2024-02-26 13:56:20 +00:00
112c72bb8e bugfix: RANDOM ROWID should be respected in VACUUM and bulk insert ops ()
* Add regression tests for random rowid bugs

Tests added for:

1. VACUUM does not respect RANDOM ROWID - https://github.com/tursodatabase/libsql/issues/929
2. Bulk insert into a fresh table does not respect RANDOM ROWID - https://github.com/tursodatabase/libsql/issues/1046

In both cases, the table should end up random rowids, but they seem to have sequential ones.

* bugfix: pass `regNextRowid` to `OP_NewRowid` call in `xferOptimization`

The register `regNextRowid` contains the `LIBSQL_RANDOM_ROWID_MARKER` value
which tells the VDBE to use a random value for row ids. The method `xferOptimization`
is used in `VACUUM` and bulk insert routines where data from one table is
inserted in another. Since the data is inserted in a loop, it checks for
max rowid once and then keeps inserting it serially. Hence we pass the marker
in each call, so that random id is generated.
2024-02-19 16:58:50 +00:00
c4438e0897 Make encryption cipher configurable and switch default to SQLCipher ()
* libsql: Make encryption cipher configurable

Introduce a `EncryptionConfig` struct to configure both encrytion cipher
and key. Needed to support multiple ciphers.

Fixes 

* libsql-ffi: Switch to SQLCipher as the default cipher

Fixes 
2024-02-19 15:37:42 +00:00
8b8b1d4ed3 libsql-ffi: Fix bundled sqlite3mc ()
The commit d178de8f07 fixed ABI breakage, but we need to regenerate the
bundled sqlite3mc code to make sure the bug fix is included when
encryption is enabled.
2024-02-16 09:11:27 +00:00
d178de8f07 bugfix: reset modifications of sqlite3_vtab ()
* Remove `libsql_module` field from `sqlite3_vtab`

* remove `libsql_module` struct

Also removed the associated functions `libsql_create_module_v2`, `libsql_create_module` functions'.

The `libsql_module` had a function `xPreparedSql` which is now moved to `sqlite_module`. The `sqlite_module` might get changed in the upstream, so added some padding space for our custom functions

* generate ffi bindings

* Add a github workflow to test crsqlite

* fix crsqlite: remove `pLibsqlModule` references

* Add tests for sqlite-vss extension
2024-02-15 18:18:30 +00:00
86172b548e Fix sqlite3mc build ()
* libsql-ffi: Enable PIC for sqlite3mc build

I am seeing the following build error when building the libsql-ffi crate
on Linux/x86-64:

```
/usr/bin/ld:
/tmp/rustcYVhHYo/liblibsql_ffi-20adf4ca11d8e922.rlib(sqlite3mc.c.o):
relocation R_X86_64_32S against `.rodata' can not be used when making a
shared object; recompile with -fPIC
```

Let's turn on PIC for sqlite3mc to fix the problem.

* libsql-ffi: Fix sqlite3mc build under `cross`

We need to use the C compiler configured by `cross` with sqlite3mc;
otherwise we end up building with the host compiler, resulting in errors
like this:

```
  = note: /usr/lib/gcc-cross/aarch64-linux-gnu/5/../../../../aarch64-linux-gnu/bin/ld: /tmp/rustcJ70WRo/liblibsql_ffi-5d7e6e8037dbda37.rlib(sqlite3mc.c.o): Relocations in generic ELF (EM: 62)
```

* libsql-ffi: Rewrite build_libsqlite3mc.sh in Rust

The bash shell script is turning out to be painful to run on Windows so
let's just rewrite the logic in Rust and place it in `build.rs`.
2024-02-14 15:26:16 +00:00
cd82068edf libsql_server,bottomless: add encryption support ()
* namespace,replication: add LogFile encryption

Anything that uses our LogFile format can now be encrypted
on-disk.
Tested locally by seeing that `wallog` file contains garbage
and no sensible plaintext strings can be extracted from it.

* test fixups

* libsql-ffi: add libsql_generate_initial_vector and...

... libsql_generate_aes256_key to make them reachable from Rust.

* connection: expose additional encryption symbols

* libsql-server: derive aes256 from user passphrase properly

And by properly, I mean calling back to SQLite3MultipleCiphers' code.

* replication: rename Encryptor to FrameEncryptor

Encryptor sounds a little too generic for this specific use case.

* replication: add snapshot encryption

It uses the same mechanism as wallog encryption, now abstracted
away to libsql-replication crate to be reused.

* replication: add an encryption feature for compilation

* cargo fmt pass

* fix remaining SnapshotFile::open calls in tests

* logger: add an encryption test

* replication: use a single buffer for encryption

Ideally we could even encrypt in place, but WalPage is also
used in snapshots and it's buffered, and that makes it exceptionally
annoying to explain to the borrow checker.

* bottomless: restore with libsql_replication::injector

... instead of the transaction page cache. That gives us free
encryption, since the injector is encryption-aware.

This patch doesn't hook encryption_key parameter yet, it will
come in the next patch.

* bottomless: pass the encryption key in options

For WAL restoration, but also to be able to encrypt data that gets
sent to S3.

* bottomless: inherit encryption key from db config if not specified

* libsql-sys: add db_change_counter()

The helper function calls the underlying C API to extract
4 bytes from offset 24 of the database header and return it.
It's the database change counter, which we can use to compare
two databases and decide which one is newer than the other.

* bottomless: use sqlite API to read database metadata

With encryption enabled, we can no longer just go ahead and read data
from given offsets, we must go through the VFS layer instead.
Fortunately, we can just open a database connection and ask for all
the metadata we need.

* libsql-sys: make db change counter actually read from the db file

* bottomless: treat change counter == 1 as a new database

... which it is, after setting the journal mode. Otherwise we decide
too eagerly that the local database is the source of truth.

* libsql-server: fix a local embedded replica test

rebase conflict with encryption

* bottomless-cli: allow passing the encryption key

* replication: rebase new test to the new api

* snapshots: do not try to decrypt headers

They are not encrypted, so we shouldn't attempt to decrypt the data.

* logger: restore encrypted frames during recovery

Instead of decrypting and encrypting back, we just copy encrypted
frames as is during the recovery process, saves IO.

* compaction: clear unused encryption_key parameter

It wasn't used since for compaction we only need headers,
which are unencrypted.

* replication: switch to FrameBorrowed::new_zeroed

Following MarinPostma's suggestion.

Co-authored-by: Marin Postma <postma.marin@protonmail.com>

* replication: rebase chores, fixing parameters

* libsql-replication: use page_mut() to decrypt data in-place

* rustfmt

* bottomless: use 0 for disabling autocheckpoint

... instead of u32::MAX. Effectively it's similar, but 0 is the correct
choice.

* rustfmt

* libsql-server: make cbc, aes optional for encryption only

* post-rebase fixes

* libsql-replication: suppress warnings when no encryption

* libsql: add encryption support for local databases

* libsql: add bytes dependency for encryption

* libsql-ffi: build libsqlite3mc without debug symbols

Technically it should just depend on cargo build mode,
but that's left for a follow-up.

* bindings: an attempt to compile bindings with releasemode

... partially to save space, but also to make them faster.

---------

Co-authored-by: Marin Postma <postma.marin@protonmail.com>
2024-02-09 14:27:39 +00:00
a72c066a8e Wal related changes ()
wal extensions
2024-02-06 17:44:23 +00:00
e37397b40c SQLite3MultipleCiphers: enable default foreign keys support ()
It was always enabled in libsql-server by default, so let's follow suit.
2024-02-02 09:33:26 +00:00
9d8de96a5b ci: fix aarch64 build typo () 2024-02-01 17:58:56 +00:00
ed691b0475 chore: fix cmake arm builds () 2024-01-31 19:52:15 +00:00
fab6df37bd SQLite3MultipleCiphers: enable dbstat and other vtabs ()
The CMake definitions look like they only apply to builds which enable
shell, which doesn't make much sense. We rely on dbstat, so let's just
enable it.
2024-01-30 10:53:18 +00:00
2e04f8febf wal.c: fix the function pointer type ()
* wal.c: fix the function pointer type

A 100% benign error, because the function has the correct signature,
but gets rid of a compiler warning.

* regenerate sqlite3.c amalgamation

* libsql-ffi: regenerate SQLite3MultipleCiphers' sqlite3.c

... not needed later, because an existing patch series switches it
to use the sqlite3.c amalgamation file from bundled/src/,
but let's update it for now.
2024-01-29 11:46:51 +00:00
f05f349c06 chore: fix ffi build with all features ()
* chore: fix ffi build with all features

* set flags only for clang

* Update libsql-ffi/build.rs
2024-01-25 21:03:11 +00:00
e1cc9ff829 return number of committed frames on insert_frames () 2024-01-25 16:39:29 +00:00
dd0e23c81c fix docker build 2024-01-24 11:03:03 +01:00
62f61dbf14 review edits 2024-01-19 12:16:25 +01:00
ac9e2e3a0f update bundled 2024-01-19 09:33:21 +01:00
bca3f12761 update rust code for checkpoint callback
- add the checkpoint callback to Wal::checkpoint
- use dynamic dispatch for callbacks (correctness issue)
- pass `frames_in_wal` and `backfilled` as ref to `Wal::checkpoint`
  because sqlite can set them despite returning `SQLITE_BUSY`
2024-01-19 09:33:19 +01:00
ad5ff8d171 libsql-ffi: deduplicate sqlite3.c amalgamations
From now on, SQLite3MultipleCiphers will use the amalgamation
file kept in libsql-ffi/bundled/src.
2024-01-19 08:22:57 +01:00
d149ecebd8 libsql-ffi: compile-in Wasm conditionally with encryption enabled 2024-01-19 08:22:57 +01:00
c8ebf82ede libsql-ffi: enable wasm runtime in -F encryption mode 2024-01-19 08:22:57 +01:00
0b422eb040 rusqlite: amend the sqlite3_profile test to account...
... for SQLite3MultipleCiphers internal statements.
2024-01-19 08:22:57 +01:00
d0e7ef1e3f libsql-ffi: explicitly turn on column metadata 2024-01-19 08:22:57 +01:00
4824cb213f libsql-ffi: add a few flags for SQLite3MultipleCiphers 2024-01-19 08:22:57 +01:00
e66af58251 libsql-ffi: refresh sqlite3.c 2024-01-19 08:22:56 +01:00
b4b63ff35c SQLite3MultipleCiphers: switch to just aes256
so that we're opinionated and compile-in less code.
2024-01-12 10:30:57 +01:00
dedbdd03b3 Merge pull request from tursodatabase/sqlite3mc_part0
Encryption at rest preparations: integrate with SQLite3MultipleCiphers
2024-01-11 18:23:01 +00:00
f0929037d1 chore: fix ffi build script 2024-01-10 09:53:19 -05:00
65f18c73de un-submodule SQLite3MultipleCiphers: 1.8.1 2024-01-09 18:12:03 +01:00
c504f8311b libsql-ffi: move multiple-ciphers support to a feature
Enabled with `cargo build -F multiple-ciphers`
2024-01-09 18:12:02 +01:00
a58248d2b7 update SQLite3MultipleCiphers submodule 2024-01-09 18:12:02 +01:00
4413562eb6 use actually fixed libsql 2024-01-09 18:12:02 +01:00
c74fb49f5c still hacky, but compiles lib instead of shipping precompiled 2024-01-09 18:12:02 +01:00
253a295865 encryption demo: based on precompiled libsqlite3mc.so
** DEEP, DEEP DRAFT **

The prebuilt library is from my fork:
https://github.com/psarna/SQLite3MultipleCiphers

The key is hardcoded to "heyhey".

After you run sqld with this patch, all data is encoded on disk
with a "heyhey" key. You can't read it directly from the file,
unless you use sqlite3mc's shell and start with
 > PRAGMA KEY=heyhey;

, and then it gets properly decrypted.

** TODO **
1. We need to adopt SQLite3MultipleCiphers source code and integrate
   with our build system, if we want to use it.
2. Pretty sure the hardcoded "heyhey" passphrase won't pass SOC2,
   but I need to consult that with a lawyer.
2024-01-09 18:12:02 +01:00
1ed8b99ec6 update bundled sqlite3.c 2024-01-08 16:00:51 +01:00
d9ebd45dbf regenerate bundled c 2023-12-26 11:13:45 +01:00
802a961d00 libsql-ffi: regenerate sqlite3.c 2023-12-20 12:23:08 +01:00
e58c294e70 fix test leaving stray directory 2023-12-01 11:09:40 +01:00
a750925350 update bindings 2023-11-30 12:07:31 +01:00
3d7c3c0a5f static initialization of sqlite3wal rc 2023-11-30 11:26:49 +01:00
05f6d47643 make pager's wal a pointer 2023-11-30 11:26:44 +01:00