Jagger | 561b979 | 2016-09-12 15:14:37 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include "../common.h" |
| 4 | |
| 5 | #include <ctype.h> |
| 6 | #include <errno.h> |
| 7 | #include <inttypes.h> |
| 8 | #include <stdbool.h> |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include "../util.h" |
| 19 | |
| 20 | static feedback_t bbMapFb; |
| 21 | static feedback_t *feedback = &bbMapFb; |
| 22 | static uint32_t my_thread_no = 0; |
| 23 | |
| 24 | __attribute__ ((constructor)) |
| 25 | static void mapBB(void) |
| 26 | { |
| 27 | char *my_thread_no_str = getenv(_HF_THREAD_NO_ENV); |
| 28 | if (my_thread_no_str == NULL) { |
| 29 | return; |
| 30 | } |
| 31 | my_thread_no = atoi(my_thread_no_str); |
| 32 | |
| 33 | if (my_thread_no >= _HF_THREAD_MAX) { |
| 34 | fprintf(stderr, "my_thread_no > _HF_THREAD_MAX (%" PRIu32 " > %d)\n", my_thread_no, |
| 35 | _HF_THREAD_MAX); |
| 36 | _exit(1); |
| 37 | } |
| 38 | struct stat st; |
| 39 | if (fstat(_HF_BITMAP_FD, &st) == -1) { |
| 40 | return; |
| 41 | } |
| 42 | if (st.st_size != sizeof(feedback_t)) { |
| 43 | fprintf(stderr, "st.size != sizeof(feedback_t) (%zu != %zu)\n", (size_t) st.st_size, |
| 44 | sizeof(feedback_t)); |
| 45 | _exit(1); |
| 46 | } |
| 47 | if ((feedback = |
| 48 | mmap(NULL, sizeof(feedback_t), PROT_READ | PROT_WRITE, MAP_SHARED, _HF_BITMAP_FD, |
| 49 | 0)) == MAP_FAILED) { |
| 50 | fprintf(stderr, "mmap: %s\n", strerror(errno)); |
| 51 | _exit(1); |
| 52 | } |
| 53 | feedback->pidFeedbackPc[my_thread_no] = 0U; |
| 54 | feedback->pidFeedbackCmp[my_thread_no] = 0U; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * -finstrument-functions |
| 59 | */ |
| 60 | void __cyg_profile_func_enter(void *func, void *caller) |
| 61 | { |
| 62 | register size_t pos = |
| 63 | (((uintptr_t) func << 12) | ((uintptr_t) caller & 0xFFF)) & _HF_PERF_BITMAP_BITSZ_MASK; |
| 64 | register uint8_t prev = ATOMIC_BTS(feedback->bbMapPc, pos); |
| 65 | if (!prev) { |
| 66 | ATOMIC_PRE_INC_RELAXED(feedback->pidFeedbackPc[my_thread_no]); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void __cyg_profile_func_exit(void *func UNUSED, void *caller UNUSED) |
| 71 | { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | /* |
Jagger | 561b979 | 2016-09-12 15:14:37 +0200 | [diff] [blame] | 76 | * -fsanitize-coverage=trace-pc,indirect-calls,trace-cmp |
| 77 | */ |
| 78 | void __sanitizer_cov_trace_pc(void) |
| 79 | { |
| 80 | register uintptr_t ret = (uintptr_t) __builtin_return_address(0) & _HF_PERF_BITMAP_BITSZ_MASK; |
| 81 | register uint8_t prev = ATOMIC_BTS(feedback->bbMapPc, ret); |
| 82 | if (!prev) { |
| 83 | ATOMIC_PRE_INC_RELAXED(feedback->pidFeedbackPc[my_thread_no]); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void __sanitizer_cov_trace_pc_indir(void *callee) |
| 88 | { |
| 89 | register size_t pos = |
| 90 | (((uintptr_t) callee << 12) | ((uintptr_t) __builtin_return_address(0) & 0xFFF)) & |
| 91 | _HF_PERF_BITMAP_BITSZ_MASK; |
| 92 | register uint8_t prev = ATOMIC_BTS(feedback->bbMapPc, pos); |
| 93 | if (!prev) { |
| 94 | ATOMIC_PRE_INC_RELAXED(feedback->pidFeedbackPc[my_thread_no]); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) |
| 99 | { |
| 100 | uintptr_t pos = (uintptr_t) __builtin_return_address(0) % _HF_PERF_BITMAP_SIZE_16M; |
| 101 | register uint8_t v = ((sizeof(Arg1) * 8) - __builtin_popcount(Arg1 ^ Arg2)); |
| 102 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 103 | if (prev < v) { |
| 104 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 105 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) |
| 110 | { |
| 111 | uintptr_t pos = (uintptr_t) __builtin_return_address(0) % _HF_PERF_BITMAP_SIZE_16M; |
| 112 | register uint8_t v = ((sizeof(Arg1) * 8) - __builtin_popcount(Arg1 ^ Arg2)); |
| 113 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 114 | if (prev < v) { |
| 115 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 116 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) |
| 121 | { |
| 122 | uintptr_t pos = (uintptr_t) __builtin_return_address(0) % _HF_PERF_BITMAP_SIZE_16M; |
| 123 | register uint8_t v = ((sizeof(Arg1) * 8) - __builtin_popcount(Arg1 ^ Arg2)); |
| 124 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 125 | if (prev < v) { |
| 126 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 127 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) |
| 132 | { |
| 133 | uintptr_t pos = (uintptr_t) __builtin_return_address(0) % _HF_PERF_BITMAP_SIZE_16M; |
| 134 | register uint8_t v = ((sizeof(Arg1) * 8) - __builtin_popcountll(Arg1 ^ Arg2)); |
| 135 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 136 | if (prev < v) { |
| 137 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 138 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Cases[0] is number of comparison entries |
| 144 | * Cases[1] is length of Val in bytes |
| 145 | */ |
| 146 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t * Cases) |
| 147 | { |
| 148 | for (uint64_t i = 0; i < Cases[0]; i++) { |
| 149 | uintptr_t pos = ((uintptr_t) __builtin_return_address(0) + i) % _HF_PERF_BITMAP_SIZE_16M; |
Jagger | e9606ce | 2016-09-14 02:50:22 +0200 | [diff] [blame] | 150 | uint8_t v = ((8 * Cases[1]) - __builtin_popcountll(Val ^ Cases[i + 2])); |
Jagger | 561b979 | 2016-09-12 15:14:37 +0200 | [diff] [blame] | 151 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 152 | if (prev < v) { |
| 153 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 154 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Robert Swiecki | ab28815 | 2016-10-03 03:42:20 +0200 | [diff] [blame] | 159 | /* |
| 160 | * -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp |
| 161 | */ |
Robert Swiecki | 574b039 | 2016-10-03 03:32:19 +0200 | [diff] [blame] | 162 | void __sanitizer_cov_trace_pc_guard_init(uint32_t * start, uint32_t * stop) |
| 163 | { |
Robert Swiecki | 3eee4b5 | 2016-10-03 03:44:54 +0200 | [diff] [blame^] | 164 | static bool inited = false; |
| 165 | if (inited == true) { |
| 166 | return; |
| 167 | } |
| 168 | inited = true; |
Robert Swiecki | 574b039 | 2016-10-03 03:32:19 +0200 | [diff] [blame] | 169 | for (uint32_t * x = start; x < stop; x++) { |
| 170 | *x = 1U; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void __sanitizer_cov_trace_pc_guard(uint32_t * guard UNUSED) |
| 175 | { |
| 176 | *guard = 0U; |
| 177 | ATOMIC_PRE_INC_RELAXED(feedback->pidFeedbackPc[my_thread_no]); |
| 178 | } |
Robert Swiecki | ab28815 | 2016-10-03 03:42:20 +0200 | [diff] [blame] | 179 | |
| 180 | void libhfuzz_instrumentUpdateCmpMap(void *addr, unsigned int new) |
| 181 | { |
| 182 | uintptr_t pos = (uintptr_t) addr % _HF_PERF_BITMAP_SIZE_16M; |
| 183 | uint8_t v = new > 254 ? 254 : new; |
| 184 | uint8_t prev = ATOMIC_GET(feedback->bbMapCmp[pos]); |
| 185 | if (prev < v) { |
| 186 | ATOMIC_SET(feedback->bbMapCmp[pos], v); |
| 187 | ATOMIC_POST_ADD(feedback->pidFeedbackCmp[my_thread_no], v - prev); |
| 188 | } |
| 189 | } |