Repository navigation
Expand file tree
/
Copy pathmodel.c
More file actions
180 lines (162 loc) · 6 KB
/
Copy pathmodel.c
File metadata and controls
180 lines (162 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libstate.h"
#include "model.h"
#define MAX_TABLES 64
#define MAX_ROW_BYTES (1 << 20)
struct buf {
unsigned char *bytes;
size_t len, cap;
};
struct table_hash {
unsigned long long name_hash;
unsigned long long content_hash;
};
/* One name and content hash per table. A listing or a row walk cut short by an I/O or allocation failure */
/* makes the snapshot unusable, since a short read is not evidence of lost content. */
struct snapshot {
struct table_hash tables[MAX_TABLES];
int count;
int schema_rc;
int rows_ok;
};
/* Whether the table listing ran to its end. */
static int listing_ok(int rc) { return rc == SQLITE_OK || rc == SQLITE_DONE; }
/* Each snapshot is checked within the same op, so nothing persists across ops. */
static struct snapshot scratch;
static int scratch_valid;
#define MODEL_FILES 3
static int damaged[MODEL_FILES];
void model_reset(void) {
scratch_valid = 0;
memset(damaged, 0, sizeof damaged);
}
void model_note_damage(int file) {
if (file < 0 || file >= MODEL_FILES) return;
damaged[file] = 1;
}
void model_discard(void) { scratch_valid = 0; }
static void put(struct buf *b, const void *bytes, size_t len) {
/* Caps the hashed rows of a huge table, the way script.c's MAX_DUMP caps its read-back. */
fuzz_grow_append(&b->bytes, &b->len, &b->cap, MAX_ROW_BYTES, bytes, len);
}
static void put_row(const struct fuzz_sqlite *api, sqlite3_stmt *stmt, struct buf *b) {
int columns = api->column_count(stmt);
for (int i = 0; i < columns; i++) {
unsigned char type = (unsigned char)api->column_type(stmt, i);
put(b, &type, 1);
if (type == SQLITE_INTEGER) {
sqlite3_int64 value = api->column_int64(stmt, i);
put(b, &value, sizeof value);
} else if (type == SQLITE_FLOAT) {
double value = api->column_double(stmt, i);
put(b, &value, sizeof value);
} else if (type != SQLITE_NULL) {
const void *bytes = api->column_blob(stmt, i);
int len = api->column_bytes(stmt, i);
put(b, &len, sizeof len);
if (bytes) put(b, bytes, (size_t)len);
}
}
}
/* Every table in schema, in name order, rows in the order a plain SELECT returns them. */
static void take_snapshot(const struct fuzz_sqlite *api, sqlite3 *handle, const char *schema, struct snapshot *out) {
char names[MAX_TABLES][128];
int count = 0;
char sql[160];
snprintf(sql, sizeof sql, "SELECT name FROM \"%s\".sqlite_schema WHERE type = 'table' ORDER BY name", schema);
sqlite3_stmt *stmt = NULL;
int rc = api->prepare_v2(handle, sql, -1, &stmt, NULL);
if (rc == SQLITE_OK) {
while ((rc = api->step(stmt)) == SQLITE_ROW) {
const unsigned char *name = api->column_blob(stmt, 0);
int len = api->column_bytes(stmt, 0);
if (count < MAX_TABLES && name && len > 0 && len < (int)sizeof names[0]) {
memcpy(names[count], name, (size_t)len);
names[count++][len] = 0;
}
}
api->finalize(stmt);
}
out->schema_rc = rc;
out->count = count;
out->rows_ok = 1;
for (int i = 0; i < count; i++) {
out->tables[i].name_hash = fuzz_fnv1a((const unsigned char *)names[i], strlen(names[i]));
char select[300];
size_t at = (size_t)snprintf(select, sizeof select, "SELECT * FROM \"%s\".\"", schema);
for (const char *c = names[i]; *c && at + 2 < sizeof select; c++) {
if (*c == '\"') select[at++] = '\"';
select[at++] = *c;
}
if (at + 1 < sizeof select) select[at++] = '\"';
select[at] = 0;
struct buf rows = {0};
stmt = NULL;
int row_rc = api->prepare_v2(handle, select, -1, &stmt, NULL);
if (row_rc == SQLITE_OK) {
while ((row_rc = api->step(stmt)) == SQLITE_ROW) put_row(api, stmt, &rows);
api->finalize(stmt);
}
if (row_rc != SQLITE_DONE) out->rows_ok = 0;
out->tables[i].content_hash = fuzz_fnv1a(rows.bytes, rows.len);
free(rows.bytes);
}
}
static int find_table(const struct snapshot *s, unsigned long long name_hash) {
for (int i = 0; i < s->count; i++) {
if (s->tables[i].name_hash == name_hash) return i;
}
return -1;
}
void model_snapshot(const struct fuzz_sqlite *api, sqlite3 *handle, const char *schema, int file) {
if (!handle || file < 0 || file >= MODEL_FILES || damaged[file]) {
scratch_valid = 0;
return;
}
take_snapshot(api, handle, schema, &scratch);
scratch_valid = listing_ok(scratch.schema_rc) && scratch.rows_ok;
}
void model_check(const struct fuzz_sqlite *api, sqlite3 *handle, const char *schema, int file,
const char *file_name, const char *op) {
if (!scratch_valid || !handle || file < 0 || file >= MODEL_FILES || damaged[file]) {
scratch_valid = 0;
return;
}
struct snapshot after;
take_snapshot(api, handle, schema, &after);
scratch_valid = 0;
if (!listing_ok(after.schema_rc) || !after.rows_ok) return;
int same = after.count == scratch.count;
for (int i = 0; same && i < scratch.count; i++) {
int j = find_table(&after, scratch.tables[i].name_hash);
same = j >= 0 && after.tables[j].content_hash == scratch.tables[i].content_hash;
}
if (!same) {
fprintf(stderr, "oracle=model file=%s reason=%s changed logical content it should have preserved\n", file_name,
op);
abort();
}
}
void model_check_contains(const struct fuzz_sqlite *api, sqlite3 *handle, const char *schema, int file,
const char *file_name, const char *op) {
if (!scratch_valid || !handle || file < 0 || file >= MODEL_FILES || damaged[file]) {
scratch_valid = 0;
return;
}
struct snapshot after;
take_snapshot(api, handle, schema, &after);
scratch_valid = 0;
if (!listing_ok(after.schema_rc) || !after.rows_ok) return;
int same = 1;
for (int i = 0; same && i < scratch.count; i++) {
int j = find_table(&after, scratch.tables[i].name_hash);
same = j >= 0 && after.tables[j].content_hash == scratch.tables[i].content_hash;
}
if (!same) {
fprintf(stderr, "oracle=model file=%s reason=%s changed logical content it should have preserved\n", file_name,
op);
abort();
}
}