forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_sql_test.c
More file actions
495 lines (413 loc) · 14 KB
/
Copy pathcompression_sql_test.c
File metadata and controls
495 lines (413 loc) · 14 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* This file and its contents are licensed under the Timescale License.
* Please see the included NOTICE for copyright information and
* LICENSE-TIMESCALE for a copy of the license.
*/
#include <math.h>
#include <postgres.h>
#include <dirent.h>
#include "guc.h"
#include "compression/arrow_c_data_interface.h"
#include "compression_sql_test.h"
#include <funcapi.h>
#include <libpq/pqformat.h>
#include <utils/builtins.h>
#include <utils/uuid.h>
#if !defined(NDEBUG) || defined(TS_COMPRESSION_FUZZING)
static int
get_compression_algorithm(char *name)
{
if (pg_strcasecmp(name, "deltadelta") == 0)
{
return COMPRESSION_ALGORITHM_DELTADELTA;
}
else if (pg_strcasecmp(name, "gorilla") == 0)
{
return COMPRESSION_ALGORITHM_GORILLA;
}
else if (pg_strcasecmp(name, "array") == 0)
{
return COMPRESSION_ALGORITHM_ARRAY;
}
else if (pg_strcasecmp(name, "dictionary") == 0)
{
return COMPRESSION_ALGORITHM_DICTIONARY;
}
else if (pg_strcasecmp(name, "bool") == 0)
{
return COMPRESSION_ALGORITHM_BOOL;
}
else if (pg_strcasecmp(name, "uuid") == 0)
{
return COMPRESSION_ALGORITHM_UUID;
}
ereport(ERROR, (errmsg("unknown compression algorithm %s", name)));
return _INVALID_COMPRESSION_ALGORITHM;
}
/*
* Specializations of test functions for arithmetic types.
*/
#define ALGO GORILLA
#define CTYPE float8
#define PG_TYPE_PREFIX FLOAT8
#define DATUM_TO_CTYPE DatumGetFloat8
#define IS_FINITE(X) isfinite((double) (X))
#include "decompress_arithmetic_test_impl.c"
#define ALGO DELTADELTA
#define CTYPE int64
#define PG_TYPE_PREFIX INT8
#define DATUM_TO_CTYPE DatumGetInt64
#include "decompress_arithmetic_test_impl.c"
#define ALGO BOOL
#define CTYPE bool
#define PG_TYPE_PREFIX BOOL
#define DATUM_TO_CTYPE DatumGetBool
#define ARROW_GET_VALUE(A, I) (arrow_row_is_valid(((const uint64 *) ((A)->buffers[1])), I))
#include "decompress_arithmetic_test_impl.c"
#define ALGO DICTIONARY
#define CTYPE pg_uuid_t
#define PG_TYPE_PREFIX UUID
#define DATUM_TO_CTYPE *DatumGetUUIDP
#define IS_NOT_EQUAL(X, Y) (memcmp(&(X), &(Y), sizeof(pg_uuid_t)) != 0)
#include "decompress_arithmetic_test_impl.c"
#define ALGO UUID
#define CTYPE pg_uuid_t
#define PG_TYPE_PREFIX UUID
#define DATUM_TO_CTYPE *DatumGetUUIDP
#define IS_NOT_EQUAL(X, Y) (memcmp(&(X), &(Y), sizeof(pg_uuid_t)) != 0)
#include "decompress_arithmetic_test_impl.c"
/*
* The table of the supported testing configurations. We use it to generate
* dispatch tables and specializations of test functions.
*/
#define APPLY_FOR_TYPES(X) \
X(GORILLA, FLOAT8, true) \
X(GORILLA, FLOAT8, false) \
X(DELTADELTA, INT8, true) \
X(DELTADELTA, INT8, false) \
X(ARRAY, TEXT, false) \
X(ARRAY, TEXT, true) \
X(DICTIONARY, TEXT, false) \
X(DICTIONARY, TEXT, true) \
X(DICTIONARY, UUID, false) \
X(DICTIONARY, UUID, true) \
X(BOOL, BOOL, false) \
X(BOOL, BOOL, true) \
X(UUID, UUID, false) \
X(UUID, UUID, true)
static int (*get_decompress_fn(int algo, Oid type))(const uint8 *Data, size_t Size, bool bulk)
{
#define DISPATCH(ALGO, PGTYPE, BULK) \
if (algo == COMPRESSION_ALGORITHM_##ALGO && type == PGTYPE##OID) \
{ \
return decompress_##ALGO##_##PGTYPE; \
}
APPLY_FOR_TYPES(DISPATCH)
elog(ERROR,
"no decompression function for compression algorithm %d with element type %d",
algo,
type);
pg_unreachable();
#undef DISPATCH
}
/*
* Read and decompress compressed data from file. Useful for debugging the
* results of fuzzing.
* The out parameter bytes is volatile because we want to fill it even
* if we error out later.
*/
static void
read_compressed_data_file_impl(int algo, Oid type, const char *path, bool bulk, volatile int *bytes,
int *rows)
{
FILE *f = fopen(path, "r");
if (!f)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FILE), errmsg("could not open the file '%s'", path)));
}
fseek(f, 0, SEEK_END);
const size_t fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
*rows = 0;
*bytes = fsize;
if (fsize == 0)
{
/*
* Skip empty data, because we'll just get "no data left in message"
* right away.
*/
fclose(f);
return;
}
char *string = palloc(fsize + 1);
size_t elements_read = fread(string, fsize, 1, f);
if (elements_read != 1)
{
fclose(f);
ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FILE), errmsg("failed to read file '%s'", path)));
}
fclose(f);
string[fsize] = 0;
*rows = get_decompress_fn(algo, type)((const uint8 *) string, fsize, bulk);
}
TS_FUNCTION_INFO_V1(ts_read_compressed_data_file);
/* Read and decompress compressed data from file -- SQL-callable wrapper. */
Datum
ts_read_compressed_data_file(PG_FUNCTION_ARGS)
{
int rows;
int bytes;
read_compressed_data_file_impl(get_compression_algorithm(PG_GETARG_CSTRING(0)),
PG_GETARG_OID(1),
PG_GETARG_CSTRING(2),
PG_GETARG_BOOL(3),
&bytes,
&rows);
PG_RETURN_INT32(rows);
}
TS_FUNCTION_INFO_V1(ts_read_compressed_data_directory);
/*
* Read and decompress all compressed data files from directory. Useful for
* checking the fuzzing corpuses in the regression tests.
*/
Datum
ts_read_compressed_data_directory(PG_FUNCTION_ARGS)
{
/* Output columns of this function. */
enum
{
out_path = 0,
out_bytes,
out_rows,
out_sqlstate,
out_location,
_out_columns
};
/* Cross-call context for this set-returning function. */
struct user_context
{
DIR *dp;
struct dirent *ep;
};
char *name = PG_GETARG_CSTRING(2);
const int algo = get_compression_algorithm(PG_GETARG_CSTRING(0));
FuncCallContext *funcctx;
struct user_context *c;
MemoryContext call_memory_context = CurrentMemoryContext;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL())
{
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/* switch to memory context appropriate for multiple function calls */
MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &funcctx->tuple_desc) != TYPEFUNC_COMPOSITE)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
}
/*
* generate attribute metadata needed later to produce tuples from raw
* C strings
*/
funcctx->attinmeta = TupleDescGetAttInMetadata(funcctx->tuple_desc);
funcctx->user_fctx = palloc(sizeof(struct user_context));
c = funcctx->user_fctx;
c->dp = opendir(name);
if (!c->dp)
{
elog(ERROR, "could not open directory '%s'", name);
}
MemoryContextSwitchTo(call_memory_context);
}
funcctx = SRF_PERCALL_SETUP();
c = (struct user_context *) funcctx->user_fctx;
Datum values[_out_columns] = { 0 };
bool nulls[_out_columns] = { 0 };
for (int i = 0; i < _out_columns; i++)
{
nulls[i] = true;
}
while ((c->ep = readdir(c->dp)))
{
if (c->ep->d_name[0] == '.')
{
continue;
}
char *path = psprintf("%s/%s", name, c->ep->d_name);
/* The return values are: path, ret, sqlstate, status, location. */
values[out_path] = PointerGetDatum(cstring_to_text(path));
nulls[out_path] = false;
int rows;
volatile int bytes = 0;
PG_TRY();
{
read_compressed_data_file_impl(algo,
PG_GETARG_OID(1),
path,
PG_GETARG_BOOL(3),
&bytes,
&rows);
values[out_rows] = Int32GetDatum(rows);
nulls[out_rows] = false;
}
PG_CATCH();
{
MemoryContextSwitchTo(call_memory_context);
ErrorData *error = CopyErrorData();
FlushErrorState();
values[out_sqlstate] =
PointerGetDatum(cstring_to_text(unpack_sql_state(error->sqlerrcode)));
nulls[out_sqlstate] = false;
if (error->filename)
{
values[out_location] = PointerGetDatum(
cstring_to_text(psprintf("%s:%d", error->filename, error->lineno)));
nulls[out_location] = false;
}
FreeErrorData(error);
}
PG_END_TRY();
values[out_bytes] = Int32GetDatum(bytes);
nulls[out_bytes] = false;
SRF_RETURN_NEXT(funcctx,
HeapTupleGetDatum(heap_form_tuple(funcctx->tuple_desc, values, nulls)));
}
(void) closedir(c->dp);
SRF_RETURN_DONE(funcctx);
}
#endif
#ifdef TS_COMPRESSION_FUZZING
/*
* Fuzzing target for all supported types.
*/
static int
target_generic(const uint8 *Data, size_t Size, CompressionAlgorithm requested_algo, Oid pg_type,
bool bulk)
{
StringInfoData si = { .data = (char *) Data, .len = Size };
const CompressionAlgorithm data_algo = pq_getmsgbyte(&si);
CheckCompressedData(data_algo > 0 && data_algo < _END_COMPRESSION_ALGORITHMS);
if (data_algo != requested_algo)
{
/*
* It's convenient to fuzz only one algorithm at a time. We specialize
* the fuzz target for one algorithm, so that the fuzzer doesn't waste
* time discovering others from scratch.
*/
return -1;
}
const CompressionAlgorithmDefinition *def = algorithm_definition(data_algo);
Datum compressed_data = def->compressed_data_recv(&si);
if (bulk)
{
DecompressAllFunction decompress_all = tsl_get_decompress_all_function(data_algo, pg_type);
decompress_all(compressed_data, pg_type, CurrentMemoryContext);
return 0;
}
DecompressionIterator *iter = def->iterator_init_forward(compressed_data, pg_type);
for (DecompressResult r = iter->try_next(iter); !r.is_done; r = iter->try_next(iter))
;
return 0;
}
/*
* This is a wrapper for fuzzing target. It will called by the libfuzzer driver.
* It has to catch the postgres exceptions normally produced for corrupt data.
*/
static int
target_wrapper(const uint8_t *Data, size_t Size, CompressionAlgorithm requested_algo, Oid pg_type,
bool bulk)
{
MemoryContextReset(CurrentMemoryContext);
int res = 0;
PG_TRY();
{
CHECK_FOR_INTERRUPTS();
res = target_generic(Data, Size, requested_algo, pg_type, bulk);
}
PG_CATCH();
{
/* EmitErrorReport(); */
FlushErrorState();
}
PG_END_TRY();
/*
* -1 means "don't include it into corpus", return it if the test function
* says so, otherwise return 0. Some test functions also returns the number
* of rows for the correct data, the fuzzer doesn't understand these values.
*/
return res == -1 ? -1 : 0;
}
/*
* Specializations of fuzzing targets for supported types that will be directly
* called by the fuzzing driver.
*/
#define DECLARE_TARGET(ALGO, PGTYPE, BULK) \
static int target_##ALGO##_##PGTYPE##_##BULK(const uint8_t *D, size_t S) \
{ \
return target_wrapper(D, S, COMPRESSION_ALGORITHM_##ALGO, PGTYPE##OID, BULK); \
}
APPLY_FOR_TYPES(DECLARE_TARGET)
#undef DECLARE_TARGET
/*
* libfuzzer fuzzing driver that we import from LLVM libraries. It will run our
* test functions with random inputs.
*/
extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
int (*UserCb)(const uint8_t *Data, size_t Size));
/*
* The SQL function to perform fuzzing.
*/
TS_FUNCTION_INFO_V1(ts_fuzz_compression);
Datum
ts_fuzz_compression(PG_FUNCTION_ARGS)
{
/*
* We use the memory context size larger than default here, so that all data
* allocated by fuzzing fit into the first chunk. The first chunk is not
* deallocated when the memory context is reset, so this reduces overhead
* caused by repeated reallocations.
* The particular value of 8MB is somewhat arbitrary and large. In practice,
* we have inputs of 1k rows max here, which decompress to 8 kB max.
*/
MemoryContext fuzzing_context =
AllocSetContextCreate(CurrentMemoryContext, "fuzzing", 0, 8 * 1024 * 1024, 8 * 1024 * 1024);
MemoryContext old_context = MemoryContextSwitchTo(fuzzing_context);
char *argvdata[] = { "PostgresFuzzer",
"-timeout=1",
"-report_slow_units=1",
// "-use_value_profile=1",
"-reload=1",
//"-print_coverage=1",
//"-print_full_coverage=1",
//"-print_final_stats=1",
//"-help=1",
psprintf("-runs=%d", PG_GETARG_INT32(3)),
"corpus" /* in the database directory */,
NULL };
char **argv = argvdata;
int argc = sizeof(argvdata) / sizeof(*argvdata) - 1;
int algo = get_compression_algorithm(PG_GETARG_CSTRING(0));
Oid type = PG_GETARG_OID(1);
bool bulk = PG_GETARG_BOOL(2);
int (*target)(const uint8_t *, size_t) = NULL;
#define DISPATCH(ALGO, PGTYPE, BULK) \
if (algo == COMPRESSION_ALGORITHM_##ALGO && type == PGTYPE##OID && bulk == BULK) \
{ \
target = target_##ALGO##_##PGTYPE##_##BULK; \
}
APPLY_FOR_TYPES(DISPATCH)
#undef DISPATCH
if (target == NULL)
{
elog(ERROR, "no llvm fuzz target for compression algorithm %d and type %d", algo, type);
}
int res = LLVMFuzzerRunDriver(&argc, &argv, target);
MemoryContextSwitchTo(old_context);
PG_RETURN_INT32(res);
}
#endif