Skip to content

Commit

Permalink
Apply adjustments for WASM support
Browse files Browse the repository at this point in the history
- Change order of checks for getentropy availability (check symbol __WASM__ first)
- Fix implementation of VFS I/O method xSectorSize (must check for null pointer)
- Use same I/O methods version as underlying implementation
  • Loading branch information
utelle committed Nov 28, 2023
1 parent 85be37e commit 987b1e9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/chacha20poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,16 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
/*
* Platform-specific entropy functions for seeding RNG
*/
#if defined(_WIN32) || defined(__CYGWIN__)
#if defined(__WASM__)

extern size_t getentropy(void* buf, size_t n);

static size_t entropy(void* buf, size_t n)
{
return (getentropy(buf, n) == 0) ? n : 0;
}

#elif defined(_WIN32) || defined(__CYGWIN__)

#if SQLITE3MC_USE_RAND_S

Expand Down Expand Up @@ -389,15 +398,6 @@ static size_t entropy(void* buf, size_t n)
return read_urandom(buf, n);
}

#elif defined(__WASM__)

extern size_t getentropy(void* buf, size_t n);

static size_t entropy(void* buf, size_t n)
{
return (getentropy(buf, n) == 0) ? n : 0;
}

#else
# error "Secure pseudorandom number generator not implemented for this OS"
#endif
Expand Down
77 changes: 72 additions & 5 deletions src/sqlite3mc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,57 @@ static const int walFileHeaderSize = 32;
/*
** Global I/O method structure of SQLite3 Multiple Ciphers VFS
*/
static sqlite3_io_methods mcIoMethodsGlobal =

#define IOMETHODS_VERSION_MIN 1
#define IOMETHODS_VERSION_MAX 3

static sqlite3_io_methods mcIoMethodsGlobal1 =
{
1, /* iVersion */
mcIoClose, /* xClose */
mcIoRead, /* xRead */
mcIoWrite, /* xWrite */
mcIoTruncate, /* xTruncate */
mcIoSync, /* xSync */
mcIoFileSize, /* xFileSize */
mcIoLock, /* xLock */
mcIoUnlock, /* xUnlock */
mcIoCheckReservedLock, /* xCheckReservedLock */
mcIoFileControl, /* xFileControl */
mcIoSectorSize, /* xSectorSize */
mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
0, /* xShmMap */
0, /* xShmLock */
0, /* xShmBarrier */
0, /* xShmUnmap */
0, /* xFetch */
0, /* xUnfetch */
};

static sqlite3_io_methods mcIoMethodsGlobal2 =
{
2, /* iVersion */
mcIoClose, /* xClose */
mcIoRead, /* xRead */
mcIoWrite, /* xWrite */
mcIoTruncate, /* xTruncate */
mcIoSync, /* xSync */
mcIoFileSize, /* xFileSize */
mcIoLock, /* xLock */
mcIoUnlock, /* xUnlock */
mcIoCheckReservedLock, /* xCheckReservedLock */
mcIoFileControl, /* xFileControl */
mcIoSectorSize, /* xSectorSize */
mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
mcIoShmMap, /* xShmMap */
mcIoShmLock, /* xShmLock */
mcIoShmBarrier, /* xShmBarrier */
mcIoShmUnmap, /* xShmUnmap */
0, /* xFetch */
0, /* xUnfetch */
};

static sqlite3_io_methods mcIoMethodsGlobal3 =
{
3, /* iVersion */
mcIoClose, /* xClose */
Expand All @@ -130,6 +180,9 @@ static sqlite3_io_methods mcIoMethodsGlobal =
mcIoUnfetch, /* xUnfetch */
};

static sqlite3_io_methods* mcIoMethodsGlobal[] =
{ 0, &mcIoMethodsGlobal1 , &mcIoMethodsGlobal2 , &mcIoMethodsGlobal3 };

/*
** Internal functions
*/
Expand Down Expand Up @@ -304,7 +357,9 @@ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdrMC* pPg)
{
sqlite3_file* pFile = sqlite3PagerFile(pPg->pPager);
void* aData = 0;
if (pFile->pMethods == &mcIoMethodsGlobal)
if (pFile->pMethods == &mcIoMethodsGlobal1 ||
pFile->pMethods == &mcIoMethodsGlobal2 ||
pFile->pMethods == &mcIoMethodsGlobal3)
{
sqlite3mc_file* mcFile = (sqlite3mc_file*) pFile;
Codec* codec = mcFile->codec;
Expand Down Expand Up @@ -406,9 +461,18 @@ static int mcVfsOpen(sqlite3_vfs* pVfs, const char* zName, sqlite3_file* pFile,
if (rc == SQLITE_OK)
{
/*
** Real open succeeded, initialize methods, register main database files
** Real open succeeded
** Initialize methods (use same version number as underlying implementation
** Register main database files
*/
pFile->pMethods = &mcIoMethodsGlobal;
int ioMethodsVersion = mcFile->pFile->pMethods->iVersion;
if (ioMethodsVersion < IOMETHODS_VERSION_MIN ||
ioMethodsVersion > IOMETHODS_VERSION_MAX)
{
/* If version out of range, use highest known version */
ioMethodsVersion = IOMETHODS_VERSION_MAX;
}
pFile->pMethods = mcIoMethodsGlobal[ioMethodsVersion];
if (flags & SQLITE_OPEN_MAIN_DB)
{
mcMainListAdd(mcFile);
Expand Down Expand Up @@ -1194,7 +1258,10 @@ static int mcIoFileControl(sqlite3_file* pFile, int op, void* pArg)

static int mcIoSectorSize(sqlite3_file* pFile)
{
return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
if (REALFILE(pFile)->pMethods->xSectorSize)
return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
else
return SQLITE_DEFAULT_SECTOR_SIZE;
}

static int mcIoDeviceCharacteristics(sqlite3_file* pFile)
Expand Down

0 comments on commit 987b1e9

Please sign in to comment.