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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
|
@@ -45,6 +45,1581 @@ #include "internal-common.h" #include "internal-target.h" +#include "qemuafl/common.h" +#include "qemuafl/imported/snapshot-inl.h" +#include "qemuafl/qemu-ijon-support.h" + +#include <string.h> +#include <sys/shm.h> +#ifndef AFL_QEMU_STATIC_BUILD + #include <dlfcn.h> +#endif + +/*************************** + * VARIOUS AUXILIARY STUFF * + ***************************/ + +/* This is equivalent to afl-as.h: */ + +static unsigned char + dummy[MAP_SIZE]; /* costs MAP_SIZE but saves a few instructions */ +unsigned char *afl_area_ptr = dummy; /* Exported for afl_gen_trace */ + +/* Exported variables populated by the code patched into elfload.c: */ + +abi_ulong afl_entry_point, /* ELF entry point (_start) */ + afl_exit_point, /* ELF exit point */ + afl_start_code, /* .text start pointer */ + afl_end_code; /* .text end pointer */ + +struct vmrange* afl_instr_code; + +abi_ulong afl_persistent_addr, afl_persistent_ret_addr; +unsigned int afl_persistent_cnt; + +unsigned int block_id = 5; + +u8 afl_compcov_level, block_cov; + +__thread abi_ulong afl_prev_loc; + +struct cmp_map *__afl_cmp_map; + +/* Set in the child process in forkserver mode: */ + +static int forkserver_installed = 0; +static int disable_caching = 0; + +unsigned char afl_fork_child; +unsigned int afl_forksrv_pid; +unsigned char is_persistent; +target_long persistent_stack_offset; +unsigned char persistent_first_pass = 1; +unsigned char persistent_exits; +unsigned char persistent_save_gpr; +unsigned char persistent_memory; +int persisent_retaddr_offset; + +struct api_regs saved_regs; + +u8 * shared_buf; +u32 *shared_buf_len; +u8 sharedmem_fuzzing; + +afl_persistent_hook_fn afl_persistent_hook_ptr; + +/* Instrumentation ratio: */ + +unsigned int afl_inst_rms = MAP_SIZE; /* Exported for afl_gen_trace */ + +/* Function declarations. */ + +static void afl_wait_tsl(CPUState *, int); +static void afl_request_tsl(target_ulong, target_ulong, uint32_t, uint32_t, + TranslationBlock *, int); + +/* Data structures passed around by the translate handlers: */ + +struct afl_tb { + + target_ulong pc; + target_ulong cs_base; + uint32_t flags; + uint32_t cf_mask; + +}; + +struct afl_chain { + + struct afl_tb last_tb; + uint32_t cf_mask; + int tb_exit; + +}; + +struct afl_tsl { + + struct afl_tb tb; + struct afl_chain chain; + char is_chain; + +}; + +/* Some forward decls: */ + +static inline TranslationBlock *tb_find(CPUState *, TranslationBlock *, int, + uint32_t); +static inline void tb_add_jump(TranslationBlock *tb, int n, + TranslationBlock *tb_next); +static void afl_map_shm_fuzz(void); + +/************************* + * ACTUAL IMPLEMENTATION * + *************************/ + +/* Snapshot memory */ + +struct saved_region { + + void* addr; + size_t size; + void* saved; + +}; + +abi_ulong saved_brk; +int lkm_snapshot; +struct saved_region* memory_snapshot; +size_t memory_snapshot_len; + ... +void afl_setup(void) { + + char *id_str = getenv(SHM_ENV_VAR), *inst_r = getenv("AFL_INST_RATIO"); + + int shm_id; + + if (inst_r) { + + unsigned int r; + + r = atoi(inst_r); + + if (r > 100) r = 100; + if (!r) r = 1; + + afl_inst_rms = MAP_SIZE * r / 100; + + } + + if (id_str) { + + shm_id = atoi(id_str); + afl_area_ptr = shmat(shm_id, NULL, 0); + + if (afl_area_ptr == (void *)-1) exit(1); + + /* With AFL_INST_RATIO set to a low value, we want to touch the bitmap + so that the parent doesn't give up on us. */ + + if (inst_r) afl_area_ptr[0] = 1; + + } + + disable_caching = getenv("AFL_QEMU_DISABLE_CACHE") != NULL; + + if (getenv("___AFL_EINS_ZWEI_POLIZEI___")) { // CmpLog forkserver + + id_str = getenv(CMPLOG_SHM_ENV_VAR); + + if (id_str) { + + u32 shm_id = atoi(id_str); + + __afl_cmp_map = shmat(shm_id, NULL, 0); + + if (__afl_cmp_map == (void *)-1) exit(1); + + } + + } + + if (getenv("AFL_INST_LIBS")) { + + afl_start_code = 0; + afl_end_code = (abi_ulong)-1; + + } + + if (getenv("AFL_CODE_START")) + afl_start_code = strtoll(getenv("AFL_CODE_START"), NULL, 16); + if (getenv("AFL_CODE_END")) + afl_end_code = strtoll(getenv("AFL_CODE_END"), NULL, 16); + + int have_names = 0; + if (getenv("AFL_QEMU_INST_RANGES")) { + char *str = getenv("AFL_QEMU_INST_RANGES"); + char *saveptr1, *saveptr2 = NULL, *save_pt1 = NULL; + char *pt1, *pt2, *pt3 = NULL; + + while (1) { + + pt1 = strtok_r(str, ",", &saveptr1); + if (pt1 == NULL) break; + str = NULL; + save_pt1 = strdup(pt1); + + pt2 = strtok_r(pt1, "-", &saveptr2); + pt3 = strtok_r(NULL, "-", &saveptr2); + + struct vmrange* n = calloc(1, sizeof(struct vmrange)); + n->next = afl_instr_code; + + if (pt3 == NULL) { // filename + have_names = 1; + n->start = (target_ulong)-1; + n->end = 0; + n->name = save_pt1; + } else { + n->start = strtoull(pt2, NULL, 16); + n->end = strtoull(pt3, NULL, 16); + if (n->start && n->end) { + n->name = NULL; + free(save_pt1); + } else { + have_names = 1; + n->start = (target_ulong)-1; + n->end = 0; + n->name = save_pt1; + } + } + + afl_instr_code = n; + + } + } + + if (getenv("AFL_QEMU_EXCLUDE_RANGES")) { + char *str = getenv("AFL_QEMU_EXCLUDE_RANGES"); + char *saveptr1, *saveptr2 = NULL, *save_pt1; + char *pt1, *pt2, *pt3 = NULL; + + while (1) { + + pt1 = strtok_r(str, ",", &saveptr1); + if (pt1 == NULL) break; + str = NULL; + save_pt1 = strdup(pt1); + + pt2 = strtok_r(pt1, "-", &saveptr2); + pt3 = strtok_r(NULL, "-", &saveptr2); + + struct vmrange* n = calloc(1, sizeof(struct vmrange)); + n->exclude = true; // These are "exclusion" regions. + n->next = afl_instr_code; + + if (pt3 == NULL) { // filename + have_names = 1; + n->start = (target_ulong)-1; + n->end = 0; + n->name = save_pt1; + } else { + n->start = strtoull(pt2, NULL, 16); + n->end = strtoull(pt3, NULL, 16); + if (n->start && n->end) { + n->name = NULL; + free(save_pt1); + } else { + have_names = 1; + n->start = (target_ulong)-1; + n->end = 0; + n->name = save_pt1; + } + } + + afl_instr_code = n; + + } + } + + if (have_names) { + GSList *map_info = read_self_maps(); + for (GSList *s = map_info; s; s = g_slist_next(s)) { + MapInfo *e = (MapInfo *) s->data; + + if (h2g_valid(e->start)) { + unsigned long min = e->start; + unsigned long max = e->end; + int flags = page_get_flags(h2g(min)); + + max = h2g_valid(max - 1) ? max : (uintptr_t) AFL_G2H(GUEST_ADDR_MAX) + 1; + + if (page_check_range(h2g(min), max - min, flags) == -1) { + continue; + } + + // Now that we have a valid guest address region, compare its + // name against the names we care about: + target_ulong gmin = h2g(min); + target_ulong gmax = h2g(max); + + struct vmrange* n = afl_instr_code; + while (n) { + if (n->name && strstr(e->path, n->name)) { + if (gmin < n->start) n->start = gmin; + if (gmax > n->end) n->end = gmax; + break; + } + n = n->next; + } + } + } + free_self_maps(map_info); + } + + if (getenv("AFL_DEBUG") && afl_instr_code) { + struct vmrange* n = afl_instr_code; + while (n) { + if (n->exclude) { + fprintf(stderr, "Exclude range: 0x%lx-0x%lx (%s)\n", + (unsigned long)n->start, (unsigned long)n->end, + n->name ? n->name : "<noname>"); + } else { + fprintf(stderr, "Instrument range: 0x%lx-0x%lx (%s)\n", + (unsigned long)n->start, (unsigned long)n->end, + n->name ? n->name : "<noname>"); + } + n = n->next; + } + } + + /* Maintain for compatibility */ + if (getenv("AFL_QEMU_COMPCOV")) { afl_compcov_level = 1; } + if (getenv("AFL_COMPCOV_LEVEL")) { + + afl_compcov_level = atoi(getenv("AFL_COMPCOV_LEVEL")); + + } + + /* pthread_atfork() seems somewhat broken in util/rcu.c, and I'm + not entirely sure what is the cause. This disables that + behaviour, and seems to work alright? */ + + rcu_disable_atfork(); + + if (getenv("AFL_QEMU_PERSISTENT_HOOK")) { + +#ifdef AFL_QEMU_STATIC_BUILD + + fprintf(stderr, + "[AFL] ERROR: you cannot use AFL_QEMU_PERSISTENT_HOOK when " + "afl-qemu-trace is static\n"); + exit(1); + +#else + + persistent_save_gpr = 1; + + void *plib = dlopen(getenv("AFL_QEMU_PERSISTENT_HOOK"), RTLD_NOW); + if (!plib) { + + fprintf(stderr, "[AFL] ERROR: invalid AFL_QEMU_PERSISTENT_HOOK=%s - %s\n", + getenv("AFL_QEMU_PERSISTENT_HOOK"), + dlerror()); + exit(1); + + } + + int (*afl_persistent_hook_init_ptr)(void) = + dlsym(plib, "afl_persistent_hook_init"); + if (afl_persistent_hook_init_ptr) + sharedmem_fuzzing = afl_persistent_hook_init_ptr(); + + afl_persistent_hook_ptr = dlsym(plib, "afl_persistent_hook"); + if (!afl_persistent_hook_ptr) { + + fprintf(stderr, + "[AFL] ERROR: failed to find the function " + "\"afl_persistent_hook\" in %s\n", + getenv("AFL_QEMU_PERSISTENT_HOOK")); + exit(1); + + } + +#endif + + } + + if (__afl_cmp_map) return; // no persistent for cmplog + + is_persistent = getenv("AFL_QEMU_PERSISTENT_ADDR") != NULL; + + if (is_persistent) + afl_persistent_addr = strtoll(getenv("AFL_QEMU_PERSISTENT_ADDR"), NULL, 0); + + if (getenv("AFL_QEMU_PERSISTENT_RET")) + afl_persistent_ret_addr = + strtoll(getenv("AFL_QEMU_PERSISTENT_RET"), NULL, 0); + /* If AFL_QEMU_PERSISTENT_RET is not specified patch the return addr */ + + if (getenv("AFL_QEMU_PERSISTENT_GPR")) persistent_save_gpr = 1; + if (getenv("AFL_QEMU_PERSISTENT_MEM")) + persistent_memory = 1; + + if (getenv("AFL_QEMU_PERSISTENT_RETADDR_OFFSET")) + persisent_retaddr_offset = + strtoll(getenv("AFL_QEMU_PERSISTENT_RETADDR_OFFSET"), NULL, 0); + + if (getenv("AFL_QEMU_PERSISTENT_CNT")) + afl_persistent_cnt = strtoll(getenv("AFL_QEMU_PERSISTENT_CNT"), NULL, 0); + else + afl_persistent_cnt = 0; + + if (getenv("AFL_QEMU_PERSISTENT_EXITS")) persistent_exits = 1; + + // TODO persistent exits for other archs not x86 + // TODO persistent mode for other archs not x86 + // TODO cmplog rtn for arm + + if (getenv("AFL_QEMU_SNAPSHOT")) { + + is_persistent = 1; + persistent_save_gpr = 1; + persistent_memory = 1; + persistent_exits = 1; + + if (afl_persistent_addr == 0) + afl_persistent_addr = strtoll(getenv("AFL_QEMU_SNAPSHOT"), NULL, 0); + + } + + if (persistent_memory && afl_snapshot_init() >= 0) + lkm_snapshot = 1; + + if (getenv("AFL_DEBUG")) { + if (is_persistent) + fprintf(stderr, "Persistent: 0x%lx [0x%lx] %s%s%s\n", + (unsigned long)afl_persistent_addr, + (unsigned long)afl_persistent_ret_addr, + (persistent_save_gpr ? "gpr ": ""), + (persistent_memory ? "mem ": ""), + (persistent_exits ? "exits ": "")); + } + + qemu_ijon_init(); + +} + +/* Fork server logic, invoked once we hit _start. */ + +void afl_forkserver(CPUState *cpu) { + + if (forkserver_installed == 1) return; + forkserver_installed = 1; + + if (getenv("AFL_QEMU_DEBUG_MAPS")) open_self_maps(cpu->env_ptr, 1); + + u32 __afl_old_forkserver = 0; + pid_t child_pid; + int t_fd[2]; + u8 child_stopped = 0; + u32 was_killed; + u32 version = 0x41464c00 + FS_NEW_VERSION_MAX; + u32 tmp = version ^ 0xffffffff, status2, status = version; + u8 *msg = (u8 *)&status; + u8 *reply = (u8 *)&status2; + + if (getenv("AFL_DEBUG")) + fprintf(stderr, "Debug: Sending status 0x%08x\n", status); + + if (getenv("AFL_OLD_FORKSERVER")) { + + __afl_old_forkserver = 1; + status = 0; + + fprintf(stderr, "The current version of afl++ qemu mode " + "supports forkserver v1, but afl-fuzz still retains " + "support for the old forkserver (qemu) version\n"); + + } + + /* Tell the parent that we're alive. If the parent doesn't want + to talk, assume that we're not running in forkserver mode. */ + + if (write(FORKSRV_FD + 1, msg, 4) != 4) return; + + afl_forksrv_pid = getpid(); + + int first_run = 1; + + if (!__afl_old_forkserver) { + + if (read(FORKSRV_FD, reply, 4) != 4) { _exit(1); } + if (tmp != status2) { + + fprintf(stderr, "wrong forkserver message from AFL++ tool"); + _exit(1); + + } + + // send the set/requested options to forkserver + status = FS_NEW_OPT_MAPSIZE; // we always send the map size + if (lkm_snapshot) status |= FS_OPT_SNAPSHOT; + if (sharedmem_fuzzing) status |= FS_NEW_OPT_SHDMEM_FUZZ; + + u32 __afl_map_size = MAP_SIZE; + + if (use_ijon) { + + __afl_map_size = (((__afl_map_size + 63) >> 6) << 6); + __afl_map_size += MAP_SIZE_IJON_MAP + MAP_SIZE_IJON_BYTES; + + ijon_map_ptr = afl_area_ptr + MAP_SIZE; + ijon_max_ptr = (uint64_t*)(ijon_map_ptr + MAP_SIZE_IJON_MAP); + + status |= FS_OPT_IJON; + + } + + if (write(FORKSRV_FD + 1, msg, 4) != 4) { + + errno = 0; + _exit(1); + + } + + // Now send the parameters for the set options, increasing by option number + + // FS_NEW_OPT_MAPSIZE - we always send the map size + status = __afl_map_size; + if (write(FORKSRV_FD + 1, msg, 4) != 4) { _exit(1); } + + // send welcome message as final message + status = version; + if (write(FORKSRV_FD + 1, msg, 4) != 4) { _exit(1); } + + } + + // END forkserver handshake + + if (sharedmem_fuzzing) { afl_map_shm_fuzz(); } + + /* All right, let's await orders... */ + + while (1) { + + /* Whoops, parent dead? */ + + if (read(FORKSRV_FD, &was_killed, 4) != 4) exit(2); + + /* If we stopped the child in persistent mode, but there was a race + condition and afl-fuzz already issued SIGKILL, write off the old + process. */ + + if (child_stopped && was_killed) { + + child_stopped = 0; + if (waitpid(child_pid, &status, 0) < 0) exit(8); + + } + + if (!child_stopped) { + + /* Establish a channel with child to grab translation commands. We'll + read from t_fd[0], child will write to TSL_FD. */ + + if (pipe(t_fd) || dup2(t_fd[1], TSL_FD) < 0) exit(3); + close(t_fd[1]); + + child_pid = fork(); + if (child_pid < 0) exit(4); + + if (!child_pid) { + + /* Child process. Close descriptors and run free. */ + + afl_fork_child = 1; + close(FORKSRV_FD); + close(FORKSRV_FD + 1); + close(t_fd[0]); + return; + + } + + /* Parent. */ + + close(TSL_FD); + + } else { + + /* Special handling for persistent mode: if the child is alive but + currently stopped, simply restart it with SIGCONT. */ + + kill(child_pid, SIGCONT); + child_stopped = 0; + + } + + /* Parent. */ + + if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) exit(5); + + /* Collect translation requests until child dies and closes the pipe. */ + + afl_wait_tsl(cpu, t_fd[0]); + + /* Get and relay exit status to parent. */ + + if (waitpid(child_pid, &status, is_persistent ? WUNTRACED : 0) < 0) exit(6); + + /* In persistent mode, the child stops itself with SIGSTOP to indicate + a successful run. In this case, we want to wake it up without forking + again. */ + + if (WIFSTOPPED(status)) + child_stopped = 1; + else if (unlikely(first_run && is_persistent)) { + + fprintf(stderr, "[AFL] ERROR: no persistent iteration executed\n"); + exit(12); // Persistent is wrong + + } + + first_run = 0; + + if (write(FORKSRV_FD + 1, &status, 4) != 4) exit(7); + + } + +} + +/* A simplified persistent mode handler, used as explained in + * llvm_mode/README.md. */ + +static u32 cycle_cnt; + +void afl_persistent_iter(CPUArchState *env) { + + static struct afl_tsl exit_cmd_tsl; + + if (!afl_persistent_cnt || --cycle_cnt) { + + if (persistent_memory) restore_memory_snapshot(); + + if (persistent_save_gpr && !afl_persistent_hook_ptr) { + afl_restore_regs(&saved_regs, env); + } + + if (!disable_caching) { + + memset(&exit_cmd_tsl, 0, sizeof(struct afl_tsl)); + exit_cmd_tsl.tb.pc = (target_ulong)(-1); + + if (write(TSL_FD, &exit_cmd_tsl, sizeof(struct afl_tsl)) != + sizeof(struct afl_tsl)) { + + /* Exit the persistent loop on pipe error */ + afl_area_ptr = dummy; + exit(0); + + } + + } + + // TODO use only pipe + raise(SIGSTOP); + + + // now we have shared_buf updated and ready to use + if (persistent_save_gpr && afl_persistent_hook_ptr) { + + struct api_regs hook_regs = saved_regs; + afl_persistent_hook_ptr(&hook_regs, guest_base, shared_buf, + *shared_buf_len); + afl_restore_regs(&hook_regs, env); + + } + + afl_area_ptr[0] = 1; + afl_prev_loc = 0; + + } else { + + afl_area_ptr = dummy; + exit(0); + + } + +} + +void afl_persistent_loop(CPUArchState *env) { + + if (!afl_fork_child) return; + + if (persistent_first_pass) { + + /* Make sure that every iteration of __AFL_LOOP() starts with a clean slate. + On subsequent calls, the parent will take care of that, but on the first + iteration, it's our job to erase any trace of whatever happened + before the loop. */ + + if (is_persistent) { + + memset(afl_area_ptr, 0, MAP_SIZE); + afl_area_ptr[0] = 1; + afl_prev_loc = 0; + + } + + if (persistent_memory) collect_memory_snapshot(); + + if (persistent_save_gpr) { + + afl_save_regs(&saved_regs, env); + + if (afl_persistent_hook_ptr) { + + struct api_regs hook_regs = saved_regs; + afl_persistent_hook_ptr(&hook_regs, guest_base, shared_buf, + *shared_buf_len); + afl_restore_regs(&hook_regs, env); + + } + + } + + cycle_cnt = afl_persistent_cnt; + persistent_first_pass = 0; + persistent_stack_offset = TARGET_LONG_BITS / 8; + + return; + + } + + if (is_persistent) { + + afl_persistent_iter(env); + + } + +} + +/* This code is invoked whenever QEMU decides that it doesn't have a + translation of a particular block and needs to compute it, or when it + decides to chain two TBs together. When this happens, we tell the parent to + mirror the operation, so that the next fork() has a cached copy. */ + +static void afl_request_tsl(target_ulong pc, target_ulong cb, uint32_t flags, + uint32_t cf_mask, TranslationBlock *last_tb, + int tb_exit) { + + if (disable_caching) return; + + struct afl_tsl t; + + if (!afl_fork_child) return; + + t.tb.pc = pc; + t.tb.cs_base = cb; + t.tb.flags = flags; + t.tb.cf_mask = cf_mask; + t.is_chain = (last_tb != NULL); + + if (t.is_chain) { + + t.chain.last_tb.pc = last_tb->pc; + t.chain.last_tb.cs_base = last_tb->cs_base; + t.chain.last_tb.flags = last_tb->flags; + t.chain.cf_mask = cf_mask; + t.chain.tb_exit = tb_exit; + + } + + if (write(TSL_FD, &t, sizeof(struct afl_tsl)) != sizeof(struct afl_tsl)) + return; + +} + +static inline TranslationBlock * +afl_tb_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, + uint32_t flags, uint32_t cf_mask) +{ + TranslationBlock *tb; + uint32_t hash; + + hash = tb_jmp_cache_hash_func(pc); + tb = qatomic_rcu_read(&cpu->tb_jmp_cache[hash]); + + cf_mask &= ~CF_CLUSTER_MASK; + cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT; + + if (likely(tb && + tb->pc == pc && + tb->cs_base == cs_base && + tb->flags == flags && + tb->trace_vcpu_dstate == *cpu->trace_dstate && + (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == cf_mask)) { + return tb; + } + tb = tb_htable_lookup(cpu, pc, cs_base, flags, cf_mask); + if (tb == NULL) { + return NULL; + } + qatomic_set(&cpu->tb_jmp_cache[hash], tb); + return tb; +} + +/* This is the other side of the same channel. Since timeouts are handled by + afl-fuzz simply killing the child, we can just wait until the pipe breaks. */ + +static void afl_wait_tsl(CPUState *cpu, int fd) { + + struct afl_tsl t; + TranslationBlock *tb, *last_tb; + + if (disable_caching) return; + + while (1) { + + u8 invalid_pc = 0; + + /* Broken pipe means it's time to return to the fork server routine. */ + + if (read(fd, &t, sizeof(struct afl_tsl)) != sizeof(struct afl_tsl)) break; + + /* Exit command for persistent */ + + if (t.tb.pc == (target_ulong)(-1)) return; + + tb = afl_tb_lookup(cpu, t.tb.pc, t.tb.cs_base, t.tb.flags, t.tb.cf_mask); + + if (!tb) { + + /* The child may request to transate a block of memory that is not + mapped in the parent (e.g. jitted code or dlopened code). + This causes a SIGSEV in gen_intermediate_code() and associated + subroutines. We simply avoid caching of such blocks. */ + + if (is_valid_addr(t.tb.pc)) { + + mmap_lock(); + tb = tb_gen_code(cpu, t.tb.pc, t.tb.cs_base, t.tb.flags, t.tb.cf_mask); + mmap_unlock(); + + } else { + + invalid_pc = 1; + + } + + } + + if (t.is_chain && !invalid_pc) { + + last_tb = afl_tb_lookup(cpu, t.chain.last_tb.pc, + t.chain.last_tb.cs_base, + t.chain.last_tb.flags, + t.chain.cf_mask); +#define TB_JMP_RESET_OFFSET_INVALID 0xffff + if (last_tb && (last_tb->jmp_reset_offset[t.chain.tb_exit] != + TB_JMP_RESET_OFFSET_INVALID)) { + + tb_add_jump(last_tb, t.chain.tb_exit, tb); + + } + + } + + } + + close(fd); + +} + /* -icount align implementation. */ typedef struct SyncClocks { @@ -946,6 +2521,7 @@ static int __attribute__((noinline)) cpu_exec_loop(CPUState *cpu, SyncClocks *sc) { int ret; + bool was_translated = false, was_chained = false; /* if an exception is pending, we execute it here */ while (!cpu_handle_exception(cpu, &ret)) { @@ -985,6 +2561,7 @@ cpu_exec_loop(CPUState *cpu, SyncClocks *sc) mmap_lock(); tb = tb_gen_code(cpu, pc, cs_base, flags, cflags); + was_translated = true; mmap_unlock(); /* @@ -1011,6 +2588,11 @@ cpu_exec_loop(CPUState *cpu, SyncClocks *sc) /* See if we can patch the calling TB. */ if (last_tb) { tb_add_jump(last_tb, tb_exit, tb); + was_chained = true; + } + if (was_translated || was_chained) { + afl_request_tsl(s.pc, s.cs_base, s.flags, s.cf_mask, + was_chained ? last_tb : NULL, tb_exit); } cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit);
|