Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233/* * lock_pause.c: hold a child process at a step of its locking protocol. * * A test preloads this into a child (DYLD_INSERT_LIBRARIES on macOS, * LD_PRELOAD elsewhere) with one of two variables set, and the child writes * 'L' to its standard output once loaded, so a test whose child runs without * it fails rather than passes. At each pause the child writes a byte naming * it and reads one byte from its standard input before the call returns, and * it stands there with whatever else it holds, for as long as the test needs. * * SQLITE_TEST_PAUSE_AT_CLAIM (test_writer_lock.ml, db_writer.exe): the first * write lock the child asks for over the 512 bytes from PENDING_BYTE (the * claim, an EXCLUSIVE lock in one request) and is refused writes 'F'. After * that, the first write lock it takes on the PENDING byte alone writes 'P', * once it holds it. * * SQLITE_TEST_PAUSE_AT_SHM_HEADER (test_wal_index.ml, db_session.exe): every * pwrite the child makes over the two copies of the WAL-index header, the * first 96 bytes of a file whose name ends in "-shm", writes 'H' once it has * returned, so the test reads the index as a reader of another process finds * it between two of the writer's writes. */
#ifndef __APPLE__#define _GNU_SOURCE/* pwrite and pwrite64 are defined below as the two symbols they are. */#undef _FILE_OFFSET_BITS#endif
#include <errno.h>#include <fcntl.h>#include <limits.h>#include <stdarg.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <unistd.h>
#ifndef __APPLE__#include <dlfcn.h>#endif
#define PENDING_BYTE 0x40000000LL#define CLAIM_SPAN 512
/* The two copies of the WAL-index header (walformat.html, section 2.1). */#define SHM_HEADERS 96
static int paused_at_claim = 0;static int paused_at_pending = 0;
static int armed(void) { return getenv("SQLITE_TEST_PAUSE_AT_CLAIM") != NULL; }
static int shm_armed(void){ return getenv("SQLITE_TEST_PAUSE_AT_SHM_HEADER") != NULL;}
static void say(char c){ while (write(1, &c, 1) < 0 && errno == EINTR) ;}
static void await_go(void){ char c; while (read(0, &c, 1) < 0 && errno == EINTR) ;}
__attribute__((constructor)) static void loaded(void){ if (armed() || shm_armed()) say('L');}
/* Whether [fd] is open on a file whose name ends in "-shm". */static int is_shm(int fd){ char path[PATH_MAX]; size_t len;#ifdef __APPLE__ if (fcntl(fd, F_GETPATH, path) == -1) return 0;#else char link[64]; ssize_t n;
snprintf(link, sizeof link, "/proc/self/fd/%d", fd); n = readlink(link, path, sizeof path - 1); if (n < 0) return 0; path[n] = '\0';#endif len = strlen(path); return len >= 4 && strcmp(path + len - 4, "-shm") == 0;}
static ssize_t after_pwrite(int fd, off_t off, ssize_t ret){ int saved = errno;
if (ret > 0 && off < SHM_HEADERS && shm_armed() && is_shm(fd)) { say('H'); await_go(); } errno = saved; return ret;}
/* Whether [l], asked of [fd] with [cmd], is a write lock over [len] bytes from PENDING_BYTE. OCaml's Unix.lockf asks from the descriptor's offset (SEEK_CUR). */static int is_write_lock(int fd, int cmd, const struct flock *l, off_t len){ off_t base;
if (cmd != F_SETLK || l == NULL || l->l_type != F_WRLCK || l->l_len != len) return 0; if (l->l_whence == SEEK_SET) base = 0; else if (l->l_whence == SEEK_CUR) base = lseek(fd, 0, SEEK_CUR); else return 0; return base >= 0 && base + l->l_start == PENDING_BYTE;}
static int after(int fd, int cmd, intptr_t arg, int ret){ int saved = errno; const struct flock *l = (const struct flock *)arg;
if (ret == -1 && (saved == EAGAIN || saved == EACCES) && !paused_at_claim && armed() && is_write_lock(fd, cmd, l, CLAIM_SPAN)) { paused_at_claim = 1; say('F'); await_go(); } else if (ret == 0 && paused_at_claim && !paused_at_pending && is_write_lock(fd, cmd, l, 1)) { paused_at_pending = 1; say('P'); await_go(); } errno = saved; return ret;}
#ifdef __APPLE__
/* dyld's interposition: calls into libSystem's fcntl from every other image reach [paused_fcntl]; its own call reaches the real one. */static int paused_fcntl(int fd, int cmd, ...){ va_list ap; intptr_t arg;
va_start(ap, cmd); arg = va_arg(ap, intptr_t); va_end(ap); return after(fd, cmd, arg, fcntl(fd, cmd, arg));}
static ssize_t paused_pwrite(int fd, const void *buf, size_t n, off_t off){ return after_pwrite(fd, off, pwrite(fd, buf, n, off));}
__attribute__((used)) static const struct { const void *replacement; const void *replacee;} interposers[] __attribute__((section("__DATA,__interpose"))) = { {(const void *)paused_fcntl, (const void *)fcntl}, {(const void *)paused_pwrite, (const void *)pwrite},};
#else
static int forward(const char *name, int fd, int cmd, intptr_t arg){ int (*next)(int, int, ...) = (int (*)(int, int, ...))dlsym(RTLD_NEXT, name);
if (next == NULL) next = (int (*)(int, int, ...))dlsym(RTLD_NEXT, "fcntl"); return after(fd, cmd, arg, next(fd, cmd, arg));}
int fcntl(int fd, int cmd, ...){ va_list ap; intptr_t arg;
va_start(ap, cmd); arg = va_arg(ap, intptr_t); va_end(ap); return forward("fcntl", fd, cmd, arg);}
int fcntl64(int fd, int cmd, ...){ va_list ap; intptr_t arg;
va_start(ap, cmd); arg = va_arg(ap, intptr_t); va_end(ap); return forward("fcntl64", fd, cmd, arg);}
ssize_t pwrite(int fd, const void *buf, size_t n, off_t off){ ssize_t (*next)(int, const void *, size_t, off_t) = (ssize_t (*)(int, const void *, size_t, off_t))dlsym(RTLD_NEXT, "pwrite");
return after_pwrite(fd, off, next(fd, buf, n, off));}
ssize_t pwrite64(int fd, const void *buf, size_t n, off64_t off){ ssize_t (*next)(int, const void *, size_t, off64_t) = (ssize_t (*)(int, const void *, size_t, off64_t))dlsym(RTLD_NEXT, "pwrite64");
return after_pwrite(fd, (off_t)off, next(fd, buf, n, off));}
#endif