| 1 |
/* Fuzz-testing of libgccjit API. |
| 2 |
Currently this triggers internal compiler errors, typically due to type |
| 3 |
mismatches. */ |
| 4 |
#include <limits.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <assert.h> |
| 8 |
|
| 9 |
#include "libgccjit.h" |
| 10 |
|
| 11 |
#define TEST_PROVIDES_MAIN |
| 12 |
#include "harness.h" |
| 13 |
|
| 14 |
typedef struct fuzzer |
| 15 |
{ |
| 16 |
gcc_jit_context *ctxt; |
| 17 |
|
| 18 |
unsigned int seed; |
| 19 |
|
| 20 |
int num_types; |
| 21 |
gcc_jit_type **types; |
| 22 |
|
| 23 |
int num_globals; |
| 24 |
gcc_jit_lvalue **globals; |
| 25 |
|
| 26 |
int num_funcs; |
| 27 |
gcc_jit_function **funcs; |
| 28 |
|
| 29 |
} fuzzer; |
| 30 |
|
| 31 |
static void |
| 32 |
fuzzer_init (fuzzer *f, gcc_jit_context *ctxt, unsigned int seed); |
| 33 |
|
| 34 |
static int |
| 35 |
fuzzer_randrange (fuzzer *f, int min, int max); |
| 36 |
|
| 37 |
static gcc_jit_location * |
| 38 |
get_random_location (fuzzer *f); |
| 39 |
|
| 40 |
static gcc_jit_type * |
| 41 |
get_random_type (fuzzer *f); |
| 42 |
|
| 43 |
static gcc_jit_type * |
| 44 |
make_random_type (fuzzer *f); |
| 45 |
|
| 46 |
static gcc_jit_lvalue * |
| 47 |
make_random_global (fuzzer *f); |
| 48 |
|
| 49 |
static gcc_jit_function * |
| 50 |
make_random_function (fuzzer *f); |
| 51 |
|
| 52 |
typedef struct function_fuzzer |
| 53 |
{ |
| 54 |
fuzzer *f; |
| 55 |
|
| 56 |
int num_params; |
| 57 |
gcc_jit_param **params; |
| 58 |
|
| 59 |
gcc_jit_function *fn; |
| 60 |
|
| 61 |
int num_locals; |
| 62 |
gcc_jit_lvalue **locals; |
| 63 |
|
| 64 |
gcc_jit_block *block; |
| 65 |
|
| 66 |
} function_fuzzer; |
| 67 |
|
| 68 |
static void |
| 69 |
function_fuzzer_add_stmt (function_fuzzer *ff); |
| 70 |
|
| 71 |
static gcc_jit_lvalue * |
| 72 |
get_random_lvalue (function_fuzzer *ff, int max_depth); |
| 73 |
|
| 74 |
static gcc_jit_rvalue * |
| 75 |
get_random_rvalue (function_fuzzer *ff, int max_depth); |
| 76 |
|
| 77 |
/* fuzzer defns. */ |
| 78 |
|
| 79 |
static void |
| 80 |
fuzzer_init (fuzzer *f, gcc_jit_context *ctxt, unsigned int seed) |
| 81 |
{ |
| 82 |
int i; |
| 83 |
memset (f, 0, sizeof (*f)); |
| 84 |
f->ctxt = ctxt; |
| 85 |
f->seed = seed; |
| 86 |
|
| 87 |
int num_types = fuzzer_randrange (f, 5, 10); |
| 88 |
f->types = malloc (num_types * sizeof (gcc_jit_type *)); |
| 89 |
|
| 90 |
int num_funcs = fuzzer_randrange (f, 3, 5); |
| 91 |
f->funcs = malloc (num_funcs * sizeof (gcc_jit_function *)); |
| 92 |
|
| 93 |
int num_globals = fuzzer_randrange (f, 5, 10); |
| 94 |
f->globals = malloc (num_globals * sizeof (gcc_jit_lvalue *)); |
| 95 |
|
| 96 |
for (i = 0; i < num_types; i++) |
| 97 |
{ |
| 98 |
gcc_jit_type *type = make_random_type (f); |
| 99 |
f->types[f->num_types++] = type; |
| 100 |
} |
| 101 |
|
| 102 |
for (i = 0; i < num_globals; i++) |
| 103 |
f->globals[f->num_globals++] = make_random_global (f); |
| 104 |
|
| 105 |
for (i = 0; i < num_funcs; i++) |
| 106 |
f->funcs[f->num_funcs++] = make_random_function (f); |
| 107 |
|
| 108 |
/* Now clean out f. */ |
| 109 |
free (f->types); |
| 110 |
free (f->funcs); |
| 111 |
free (f->globals); |
| 112 |
} |
| 113 |
|
| 114 |
/* Get random int in inclusive range [min, max]. */ |
| 115 |
|
| 116 |
static int fuzzer_randrange (fuzzer *f, int min, int max) |
| 117 |
{ |
| 118 |
assert (min <= max); |
| 119 |
int i = rand_r (&f->seed); |
| 120 |
int result = (i % (max + 1 - min)) + min; |
| 121 |
assert (result >= min); |
| 122 |
assert (result <= max); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
static gcc_jit_location * |
| 127 |
get_random_location (fuzzer *f) |
| 128 |
{ |
| 129 |
const char *filename = NULL; |
| 130 |
|
| 131 |
if (fuzzer_randrange (f, 0, 1)) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
switch (fuzzer_randrange (f, 1, 2)) |
| 135 |
{ |
| 136 |
case 1: |
| 137 |
filename = "foo.c"; |
| 138 |
break; |
| 139 |
case 2: |
| 140 |
filename = "bar.c"; |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
return gcc_jit_context_new_location (f->ctxt, |
| 145 |
filename, |
| 146 |
fuzzer_randrange (f, 1, 1000), |
| 147 |
fuzzer_randrange (f, 1, 1000)); |
| 148 |
} |
| 149 |
|
| 150 |
const enum gcc_jit_types types[] = { |
| 151 |
GCC_JIT_TYPE_VOID, |
| 152 |
|
| 153 |
GCC_JIT_TYPE_VOID_PTR, |
| 154 |
|
| 155 |
GCC_JIT_TYPE_CHAR, |
| 156 |
GCC_JIT_TYPE_SIGNED_CHAR, |
| 157 |
GCC_JIT_TYPE_UNSIGNED_CHAR, |
| 158 |
|
| 159 |
GCC_JIT_TYPE_SHORT, |
| 160 |
GCC_JIT_TYPE_UNSIGNED_SHORT, |
| 161 |
|
| 162 |
GCC_JIT_TYPE_INT, |
| 163 |
GCC_JIT_TYPE_UNSIGNED_INT, |
| 164 |
|
| 165 |
GCC_JIT_TYPE_LONG, |
| 166 |
GCC_JIT_TYPE_UNSIGNED_LONG, |
| 167 |
|
| 168 |
GCC_JIT_TYPE_LONG_LONG, |
| 169 |
GCC_JIT_TYPE_UNSIGNED_LONG_LONG, |
| 170 |
|
| 171 |
GCC_JIT_TYPE_FLOAT, |
| 172 |
GCC_JIT_TYPE_DOUBLE, |
| 173 |
GCC_JIT_TYPE_LONG_DOUBLE, |
| 174 |
|
| 175 |
GCC_JIT_TYPE_CONST_CHAR_PTR, |
| 176 |
|
| 177 |
GCC_JIT_TYPE_SIZE_T, |
| 178 |
|
| 179 |
GCC_JIT_TYPE_FILE_PTR |
| 180 |
}; |
| 181 |
#define NUM_TYPES (sizeof(types)/sizeof(types[0])) |
| 182 |
|
| 183 |
static gcc_jit_type * |
| 184 |
get_random_type (fuzzer *f) |
| 185 |
{ |
| 186 |
int i = fuzzer_randrange (f, 0, (NUM_TYPES - 1) + f->num_types); |
| 187 |
if (i < NUM_TYPES) |
| 188 |
return gcc_jit_context_get_type (f->ctxt, types[i]); |
| 189 |
assert ((i - NUM_TYPES) < f->num_types); |
| 190 |
return f->types[i - NUM_TYPES]; |
| 191 |
} |
| 192 |
|
| 193 |
static gcc_jit_type * |
| 194 |
make_random_type (fuzzer *f) |
| 195 |
{ |
| 196 |
switch (fuzzer_randrange (f, 0, 8)) |
| 197 |
{ |
| 198 |
case 0: |
| 199 |
return gcc_jit_type_get_pointer (get_random_type (f)); |
| 200 |
case 1: |
| 201 |
return gcc_jit_type_get_const (get_random_type (f)); |
| 202 |
case 2: |
| 203 |
return gcc_jit_type_get_vector (get_random_type (f), 4); |
| 204 |
case 3: |
| 205 |
return gcc_jit_type_get_volatile (get_random_type (f)); |
| 206 |
case 4: |
| 207 |
return gcc_jit_type_get_aligned (get_random_type (f), 4); |
| 208 |
default: |
| 209 |
{ |
| 210 |
/* Create a struct. */ |
| 211 |
int num_fields = fuzzer_randrange (f, 0, 10); |
| 212 |
gcc_jit_field **fields = \ |
| 213 |
malloc (num_fields * sizeof (gcc_jit_field *)); |
| 214 |
int i; |
| 215 |
for (i = 0; i < num_fields ; i++) |
| 216 |
{ |
| 217 |
char field_name[256]; |
| 218 |
sprintf (field_name, "field%i", i); |
| 219 |
fields[i] = gcc_jit_context_new_field (f->ctxt, |
| 220 |
get_random_location (f), |
| 221 |
get_random_type (f), |
| 222 |
field_name); |
| 223 |
} |
| 224 |
char struct_name[256]; |
| 225 |
sprintf (struct_name, "s%i", f->num_types); |
| 226 |
gcc_jit_struct *struct_ = \ |
| 227 |
gcc_jit_context_new_struct_type (f->ctxt, |
| 228 |
get_random_location (f), |
| 229 |
struct_name, |
| 230 |
num_fields, |
| 231 |
fields); |
| 232 |
free (fields); |
| 233 |
return gcc_jit_struct_as_type (struct_); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
static gcc_jit_lvalue * |
| 239 |
make_random_global (fuzzer *f) |
| 240 |
{ |
| 241 |
char global_name[256]; |
| 242 |
sprintf (global_name, "g%i", f->num_globals); |
| 243 |
return gcc_jit_context_new_global (f->ctxt, |
| 244 |
get_random_location (f), |
| 245 |
GCC_JIT_GLOBAL_EXPORTED, |
| 246 |
get_random_type (f), |
| 247 |
global_name); |
| 248 |
} |
| 249 |
|
| 250 |
static gcc_jit_function * |
| 251 |
make_random_function (fuzzer *f) |
| 252 |
{ |
| 253 |
char func_name[256]; |
| 254 |
sprintf (func_name, "fn%i", f->num_funcs); |
| 255 |
|
| 256 |
function_fuzzer *ff = malloc (sizeof (function_fuzzer)); |
| 257 |
memset (ff, 0, sizeof (*ff)); |
| 258 |
|
| 259 |
ff->f = f; |
| 260 |
|
| 261 |
ff->num_params = fuzzer_randrange (f, 0, 10); |
| 262 |
ff->params = malloc (ff->num_params * sizeof (gcc_jit_param *)); |
| 263 |
int i; |
| 264 |
for (i = 0; i < ff->num_params; i++) |
| 265 |
{ |
| 266 |
char param_name[256]; |
| 267 |
sprintf (param_name, "param%i", i); |
| 268 |
ff->params[i] = \ |
| 269 |
gcc_jit_context_new_param (f->ctxt, |
| 270 |
get_random_location (f), |
| 271 |
get_random_type (f), |
| 272 |
param_name); |
| 273 |
} |
| 274 |
|
| 275 |
enum gcc_jit_function_kind kind = |
| 276 |
((enum gcc_jit_function_kind) |
| 277 |
fuzzer_randrange (f, 0, GCC_JIT_FUNCTION_IMPORTED)); |
| 278 |
|
| 279 |
ff->fn = \ |
| 280 |
gcc_jit_context_new_function ( |
| 281 |
f->ctxt, |
| 282 |
get_random_location (f), |
| 283 |
kind, |
| 284 |
get_random_type (f), |
| 285 |
func_name, |
| 286 |
ff->num_params, |
| 287 |
ff->params, |
| 288 |
fuzzer_randrange (f, 0, 1)); |
| 289 |
ff->block = gcc_jit_function_new_block (ff->fn, NULL); |
| 290 |
|
| 291 |
/* Create locals. */ |
| 292 |
if (kind != GCC_JIT_FUNCTION_IMPORTED) |
| 293 |
{ |
| 294 |
ff->num_locals = fuzzer_randrange (f, 0, 10); |
| 295 |
ff->locals = malloc (ff->num_locals * sizeof (gcc_jit_lvalue *)); |
| 296 |
for (i = 0; i < ff->num_locals; i++) |
| 297 |
{ |
| 298 |
char local_name[256]; |
| 299 |
sprintf (local_name, "local%i", i); |
| 300 |
ff->locals[i] = |
| 301 |
gcc_jit_function_new_local (ff->fn, |
| 302 |
get_random_location (f), |
| 303 |
get_random_type (f), |
| 304 |
local_name); |
| 305 |
} |
| 306 |
} |
| 307 |
/* TODO: use locals. */ |
| 308 |
|
| 309 |
if (kind != GCC_JIT_FUNCTION_IMPORTED) |
| 310 |
{ |
| 311 |
/* TODO: create body */ |
| 312 |
int num_stmts = fuzzer_randrange (f, 0, 10); |
| 313 |
for (i = 0; i < num_stmts; i++) |
| 314 |
function_fuzzer_add_stmt (ff); |
| 315 |
} |
| 316 |
|
| 317 |
gcc_jit_block_end_with_return (ff->block, NULL, get_random_rvalue (ff, 3)); |
| 318 |
|
| 319 |
|
| 320 |
gcc_jit_function *result = ff->fn; |
| 321 |
|
| 322 |
free (ff->locals); |
| 323 |
free (ff->params); |
| 324 |
free (ff); |
| 325 |
|
| 326 |
return result; |
| 327 |
} |
| 328 |
|
| 329 |
/* function_fuzzer defns. */ |
| 330 |
|
| 331 |
static void function_fuzzer_add_stmt (function_fuzzer *ff) |
| 332 |
{ |
| 333 |
gcc_jit_block_add_eval (ff->block, |
| 334 |
get_random_location (ff->f), |
| 335 |
get_random_rvalue (ff, 4)); |
| 336 |
gcc_jit_block_add_assignment (ff->block, |
| 337 |
get_random_location (ff->f), |
| 338 |
get_random_lvalue (ff, 4), |
| 339 |
get_random_rvalue (ff, 4)); |
| 340 |
/* TODO: place more kinds of statement */ |
| 341 |
/* TODO: labels */ |
| 342 |
} |
| 343 |
|
| 344 |
static gcc_jit_lvalue *get_random_lvalue (function_fuzzer *ff, int max_depth) |
| 345 |
{ |
| 346 |
int choice = fuzzer_randrange (ff->f, 0, |
| 347 |
ff->num_params |
| 348 |
+ ff->num_locals |
| 349 |
+ ff->f->num_globals - 1); |
| 350 |
if (choice < ff->num_params) |
| 351 |
return gcc_jit_param_as_lvalue (ff->params[choice]); |
| 352 |
choice -= ff->num_params; |
| 353 |
|
| 354 |
if (choice < ff->num_locals) |
| 355 |
return ff->locals[choice]; |
| 356 |
choice -= ff->num_locals; |
| 357 |
|
| 358 |
assert (choice < ff->f->num_globals); |
| 359 |
return ff->f->globals[choice]; |
| 360 |
} |
| 361 |
|
| 362 |
static gcc_jit_rvalue *get_random_rvalue (function_fuzzer *ff, int max_depth) |
| 363 |
{ |
| 364 |
int use_lvalue = fuzzer_randrange (ff->f, 0, 1); |
| 365 |
if (use_lvalue) |
| 366 |
return gcc_jit_lvalue_as_rvalue (get_random_lvalue (ff, max_depth)); |
| 367 |
|
| 368 |
int choice = fuzzer_randrange (ff->f, 0, 1); |
| 369 |
|
| 370 |
/* Compound op: */ |
| 371 |
switch (choice) |
| 372 |
{ |
| 373 |
case 0: |
| 374 |
return gcc_jit_context_new_string_literal (ff->f->ctxt, "hello"); |
| 375 |
case 1: |
| 376 |
return gcc_jit_context_new_rvalue_from_int ( |
| 377 |
ff->f->ctxt, |
| 378 |
get_random_type (ff->f), |
| 379 |
fuzzer_randrange (ff->f, 0, INT_MAX)); |
| 380 |
case 2: |
| 381 |
return gcc_jit_context_new_rvalue_from_double ( |
| 382 |
ff->f->ctxt, |
| 383 |
get_random_type (ff->f), |
| 384 |
((double)fuzzer_randrange (ff->f, 0, INT_MAX)) |
| 385 |
/ (double)fuzzer_randrange (ff->f, 0, INT_MAX)); |
| 386 |
case 3: |
| 387 |
return gcc_jit_context_new_unary_op ( |
| 388 |
ff->f->ctxt, |
| 389 |
get_random_location (ff->f), |
| 390 |
((enum gcc_jit_unary_op) |
| 391 |
fuzzer_randrange (ff->f, 0, GCC_JIT_UNARY_OP_LOGICAL_NEGATE)), |
| 392 |
get_random_type (ff->f), |
| 393 |
get_random_rvalue (ff, max_depth - 1)); |
| 394 |
case 4: |
| 395 |
return gcc_jit_context_new_binary_op ( |
| 396 |
ff->f->ctxt, |
| 397 |
get_random_location (ff->f), |
| 398 |
((enum gcc_jit_binary_op) |
| 399 |
fuzzer_randrange (ff->f, 0, GCC_JIT_BINARY_OP_LOGICAL_OR)), |
| 400 |
get_random_type (ff->f), |
| 401 |
get_random_rvalue (ff, max_depth - 1), |
| 402 |
get_random_rvalue (ff, max_depth - 1)); |
| 403 |
case 5: |
| 404 |
return gcc_jit_lvalue_get_address ( |
| 405 |
get_random_lvalue (ff, max_depth - 1), |
| 406 |
get_random_location (ff->f)); |
| 407 |
|
| 408 |
/* TODO: |
| 409 |
- comparisons |
| 410 |
- calls |
| 411 |
- array lookup |
| 412 |
- fields |
| 413 |
- dereferencing */ |
| 414 |
} |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
/* Top-level defns for use by harness. */ |
| 420 |
void |
| 421 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 422 |
{ |
| 423 |
fuzzer f; |
| 424 |
int seed = *(int*)user_data; |
| 425 |
|
| 426 |
fuzzer_init (&f, ctxt, seed); |
| 427 |
} |
| 428 |
|
| 429 |
static int num_completed_compilations = 0; |
| 430 |
|
| 431 |
void |
| 432 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 433 |
{ |
| 434 |
/* We can make no guarantees about whether we built something |
| 435 |
valid or not, and the result might have an infinite loop, |
| 436 |
so we can't execute it. |
| 437 |
|
| 438 |
If we survive to reach here, note the fact for DejaGnu. */ |
| 439 |
pass ("%s: survived compilation", test); |
| 440 |
if (result) |
| 441 |
num_completed_compilations++; |
| 442 |
} |
| 443 |
|
| 444 |
static void |
| 445 |
test_fuzzer (const char *argv0, int seed) |
| 446 |
{ |
| 447 |
test_jit (argv0, &seed); |
| 448 |
} |
| 449 |
|
| 450 |
int |
| 451 |
main (int argc, char **argv) |
| 452 |
{ |
| 453 |
int i, seed; |
| 454 |
const int NUM_ITERATIONS = 2; |
| 455 |
const int NUM_SEEDS = 100; |
| 456 |
for (i = 1; i <= NUM_ITERATIONS; i++) |
| 457 |
{ |
| 458 |
for (seed = 0; seed < NUM_SEEDS ; seed++) |
| 459 |
{ |
| 460 |
snprintf (test, sizeof (test), |
| 461 |
"%s iteration %d of %d; seed %d of %d", |
| 462 |
extract_progname (argv[0]), |
| 463 |
i, NUM_ITERATIONS, seed, NUM_SEEDS); |
| 464 |
test_fuzzer (argv[0], seed); |
| 465 |
} |
| 466 |
} |
| 467 |
pass ("%s: survived running all tests", extract_progname (argv[0])); |
| 468 |
note ("%s: num completed compilations: %d", extract_progname (argv[0]), |
| 469 |
num_completed_compilations); |
| 470 |
totals (); |
| 471 |
|
| 472 |
return 0; |
| 473 |
} |