122 lines
3.8 KiB
C
122 lines
3.8 KiB
C
#define _GNU_SOURCE
|
|
#include <inttypes.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
|
|
#include "Varint.h"
|
|
|
|
int oracle_encode(uint8_t bytes[9], uint64_t value);
|
|
int oracle_decode(const uint8_t bytes[9], uint64_t *value);
|
|
|
|
static size_t cases;
|
|
|
|
static void check(uint8_t *end, const uint8_t bytes[9], size_t available) {
|
|
const uint64_t sentinel = UINT64_C(0xfedcba9876543210);
|
|
uint64_t expected;
|
|
int width = oracle_decode(bytes, &expected);
|
|
if ((size_t)width > available) {
|
|
width = 0;
|
|
expected = sentinel;
|
|
}
|
|
|
|
// Place the last supplied byte immediately before an inaccessible page.
|
|
uint8_t *input = end - available;
|
|
if (available != 0) {
|
|
memcpy(input, bytes, available);
|
|
}
|
|
uint64_t decoded = sentinel;
|
|
int consumed = ziran_sqlite_read_varint(input, available, &decoded);
|
|
if (consumed != width || decoded != expected) {
|
|
fprintf(stderr, "case %zu length %zu: got %d/%016" PRIx64
|
|
", want %d/%016" PRIx64 "\n",
|
|
cases, available, consumed, decoded, width, expected);
|
|
exit(1);
|
|
}
|
|
++cases;
|
|
}
|
|
|
|
static void check_prefixes(uint8_t *end, const uint8_t bytes[9]) {
|
|
for (size_t length = 0; length <= 9; ++length) {
|
|
check(end, bytes, length);
|
|
}
|
|
}
|
|
|
|
static void check_value(uint8_t *end, uint64_t value, int expected_width) {
|
|
uint8_t bytes[9] = {0};
|
|
int width = oracle_encode(bytes, value);
|
|
if (width != expected_width) {
|
|
fprintf(stderr, "unexpected SQLite encoding width for %016" PRIx64 "\n", value);
|
|
exit(1);
|
|
}
|
|
check_prefixes(end, bytes);
|
|
}
|
|
|
|
static uint64_t next_random(uint64_t *state) {
|
|
uint64_t value = *state;
|
|
value ^= value << 13;
|
|
value ^= value >> 7;
|
|
value ^= value << 17;
|
|
*state = value;
|
|
return value;
|
|
}
|
|
|
|
int main(void) {
|
|
long page_size = sysconf(_SC_PAGESIZE);
|
|
if (page_size < 9) {
|
|
fputs("cannot determine system page size\n", stderr);
|
|
return 1;
|
|
}
|
|
size_t page = (size_t)page_size;
|
|
uint8_t *mapping = mmap(NULL, page * 2, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
if (mapping == MAP_FAILED || mprotect(mapping + page, page, PROT_NONE) != 0) {
|
|
perror("guard page");
|
|
return 1;
|
|
}
|
|
uint8_t *end = mapping + page;
|
|
|
|
uint64_t value = 42;
|
|
if (ziran_sqlite_read_varint(NULL, 9, &value) != 0 || value != 42 ||
|
|
ziran_sqlite_read_varint(end, 9, NULL) != 0) {
|
|
fputs("null argument contract failed\n", stderr);
|
|
return 1;
|
|
}
|
|
check_value(end, 0, 1);
|
|
for (int width = 1; width <= 8; ++width) {
|
|
uint64_t boundary = UINT64_C(1) << (7 * width);
|
|
check_value(end, boundary - 1, width);
|
|
check_value(end, boundary, width + 1);
|
|
check_value(end, boundary + 1, width + 1);
|
|
}
|
|
check_value(end, UINT64_C(1) << 63, 9);
|
|
check_value(end, UINT64_MAX, 9);
|
|
|
|
uint8_t bytes[9] = {0};
|
|
memset(bytes, 0x80, sizeof(bytes));
|
|
// Accept SQLite's overlong encodings, including every ninth-byte value.
|
|
for (size_t position = 0; position < 9; ++position) {
|
|
for (int byte = 0; byte <= 255; ++byte) {
|
|
bytes[position] = (uint8_t)byte;
|
|
check_prefixes(end, bytes);
|
|
}
|
|
bytes[position] = 0x80;
|
|
}
|
|
uint64_t state = UINT64_C(0x763f32a14598ed3d);
|
|
for (int sample = 0; sample < 20000; ++sample) {
|
|
for (size_t position = 0; position < sizeof(bytes); ++position) {
|
|
bytes[position] = (uint8_t)next_random(&state);
|
|
}
|
|
check_prefixes(end, bytes);
|
|
}
|
|
if (munmap(mapping, page * 2) != 0) {
|
|
perror("munmap");
|
|
return 1;
|
|
}
|
|
printf("SQLite varint differential: %zu cases passed\n", cases);
|
|
return 0;
|
|
}
|