utelle / SQLite3MultipleCiphers

SQLite3 encryption extension with support for multiple ciphers
https://utelle.github.io/SQLite3MultipleCiphers/
MIT License
390 stars 73 forks source link

Can't compile from amalgamation with MUSL. #111

Closed johnfound closed 1 year ago

johnfound commented 1 year ago

I am trying to compile with MUSL, using the following command:

gcc -m32 -Os -shared -fno-stack-protector -mpreferred-stack-boundary=2 -march=x86-64    \
    -fno-omit-frame-pointer                                                             \
    -specs musl/musl-gcc.specs                                                          \
    -DSQLITE_DEFAULT_MEMSTATUS=0                                                        \
    -DSQLITE_LIKE_DOESNT_MATCH_BLOBS                                                    \
    -DSQLITE_MAX_EXPR_DEPTH=0                                                           \
    -DSQLITE_OMIT_DEPRECATED                                                            \
    -DSQLITE_OMIT_PROGRESS_CALLBACK                                                     \
    -DSQLITE_OMIT_SHARED_CACHE                                                          \
    -DSQLITE_ENABLE_FTS5                                                                \
    -DSQLITE_OMIT_COMPLETE                                                              \
    -DSQLITE_OMIT_GET_TABLE                                                             \
    -DSQLITE_OMIT_UTF16                                                                 \
    -DSQLITE_OMIT_AUTHORIZATION                                                         \
    -DSQLITE_THREADSAFE=1                                                               \
    -DSQLITE_ENABLE_STAT4                                                               \
    -DHAVE_USLEEP                                                                       \
./sqlite3.c -o ./libsqlite3.so                                                          \

At first got an error:

./sqlite3.c: In function ‘sqlite3FinishCoding’:
./sqlite3.c:118477:15: error: ‘Parse’ has no member named ‘nTableLock’
118477 |     if( pParse->nTableLock>0 && db->init.busy==0 ){
       |               ^~
Error: Can't compile SQLite.

When I removed SQLITE_OMIT_SHARED_CACHE for test (because Parse depends on it, which is mistake IMHO), got another error:

/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.1/include/wmmintrin.h:87:1: error: inlining failed in call to ‘always_inline’ ‘_mm_aeskeygenassist_si128’: target specific option mismatch
   87 | _mm_aeskeygenassist_si128 (__m128i __X, const int __C)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~
./sqlite3.c:265027:16: note: called from here
265027 |         temp = _mm_extract_epi32(_mm_aeskeygenassist_si128(_mm_setr_epi32(0, temp, 0, 0), 0), 0);
       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error: Can't compile SQLite.

Which is really strange, because I expected to not have external dependencies in the amalgamation source...

Any advises are welcome.

utelle commented 1 year ago

I am trying to compile with MUSL, using the following command:

gcc -m32 -Os -shared -fno-stack-protector -mpreferred-stack-boundary=2 -march=x86-64    \
    -fno-omit-frame-pointer                                                             \
    -specs musl/musl-gcc.specs                                                          \
    -DSQLITE_DEFAULT_MEMSTATUS=0                                                        \
    -DSQLITE_LIKE_DOESNT_MATCH_BLOBS                                                    \
    -DSQLITE_MAX_EXPR_DEPTH=0                                                           \
    -DSQLITE_OMIT_DEPRECATED                                                            \
    -DSQLITE_OMIT_PROGRESS_CALLBACK                                                     \
    -DSQLITE_OMIT_SHARED_CACHE                                                          \
    -DSQLITE_ENABLE_FTS5                                                                \
    -DSQLITE_OMIT_COMPLETE                                                              \
    -DSQLITE_OMIT_GET_TABLE                                                             \
    -DSQLITE_OMIT_UTF16                                                                 \
    -DSQLITE_OMIT_AUTHORIZATION                                                         \
    -DSQLITE_THREADSAFE=1                                                               \
    -DSQLITE_ENABLE_STAT4                                                               \
    -DHAVE_USLEEP                                                                       \
./sqlite3.c -o ./libsqlite3.so                                                          \

First, some general remarks:

  1. Using compile time options SQLITE_OMIT_* may or may not work with the amalgamation (see SQLite's documentation about omitting features)
  2. The source file sqlite3.c is the original SQLite amalgamation while sqlite3mc_amalgamation.c is the amalgamation of this project (but maybe you renamed the file).

At first got an error:

./sqlite3.c: In function ‘sqlite3FinishCoding’:
./sqlite3.c:118477:15: error: ‘Parse’ has no member named ‘nTableLock’
118477 |     if( pParse->nTableLock>0 && db->init.busy==0 ){
       |               ^~
Error: Can't compile SQLite.

This problem is related to the user authentication extension, which is enabled by default, if not explicitly disabled. Just add

-DSQLITE_USER_AUTHENTICATION=0

and the error will go away. This is a known problem with the user authentication extension, but the SQLite core developer team has not addressed this (and other) issues related to this extension. And unfortunately I don't know enough about the inner workings of SQLite to fix it on my own.

When I removed SQLITE_OMIT_SHARED_CACHE for test (because Parse depends on it, which is mistake IMHO),

Yes, that is definitely a bug, but see my comment above.

got another error:

/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.1/include/wmmintrin.h:87:1: error: inlining failed in call to ‘always_inline’ ‘_mm_aeskeygenassist_si128’: target specific option mismatch
   87 | _mm_aeskeygenassist_si128 (__m128i __X, const int __C)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~
./sqlite3.c:265027:16: note: called from here
265027 |         temp = _mm_extract_epi32(_mm_aeskeygenassist_si128(_mm_setr_epi32(0, temp, 0, 0), 0), 0);
       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error: Can't compile SQLite.

Which is really strange, because I expected to not have external dependencies in the amalgamation source...

No, actually this is not strange. SQLite3mc uses native support for AES encryption. To make this native code compile with GCC it is necessary to specify the compiler options -msse4.2 -maes.

johnfound commented 1 year ago

Yes, I have renamed the file, because the build scripts have hard coded name. Thank's for the information. Will test again and report the result.

johnfound commented 1 year ago

It compiled now. Will test further. Thanks for the help.