|
6 | 6 |
|
7 | 7 | package runtime
|
8 | 8 |
|
9 |
| -import _ "unsafe" // for go:linkname |
| 9 | +import "unsafe" |
10 | 10 |
|
| 11 | +func libfuzzerCallTraceInit(fn, start, end *byte) |
11 | 12 | func libfuzzerCall(fn *byte, arg0, arg1 uintptr)
|
12 | 13 |
|
13 | 14 | func libfuzzerTraceCmp1(arg0, arg1 uint8) {
|
@@ -42,6 +43,34 @@ func libfuzzerTraceConstCmp8(arg0, arg1 uint64) {
|
42 | 43 | libfuzzerCall(&__sanitizer_cov_trace_const_cmp8, uintptr(arg0), uintptr(arg1))
|
43 | 44 | }
|
44 | 45 |
|
| 46 | +var pcTables []byte |
| 47 | + |
| 48 | +func LibfuzzerInitializeCounters() { |
| 49 | + libfuzzerCallTraceInit(&__sanitizer_cov_8bit_counters_init, &__start___sancov_cntrs, &__stop___sancov_cntrs) |
| 50 | + start := unsafe.Pointer(&__start___sancov_cntrs) |
| 51 | + end := unsafe.Pointer(&__stop___sancov_cntrs) |
| 52 | + |
| 53 | + // PC tables are arrays of ptr-sized integers representing pairs [PC,PCFlags] for every instrumented block. |
| 54 | + // The number of PCs and PCFlags is the same as the number of 8-bit counters. Each PC table entry has |
| 55 | + // the size of two ptr-sized integers. We allocate one more byte than what we actually need so that we can |
| 56 | + // get a pointer representing the end of the PC table array. |
| 57 | + size := (uintptr(end)-uintptr(start))*unsafe.Sizeof(uintptr(0))*2 + 1 |
| 58 | + pcTables = make([]byte, size) |
| 59 | + libfuzzerCallTraceInit(&__sanitizer_cov_pcs_init, &pcTables[0], &pcTables[size-1]) |
| 60 | +} |
| 61 | + |
| 62 | +// libfuzzerIncrementCounter guarantees that the counter never becomes zero |
| 63 | +// again once it has been incremented once. It implements the NeverZero |
| 64 | +// optimization presented by the paper: |
| 65 | +// "AFL++: Combining Incremental Steps of Fuzzing Research" |
| 66 | +func libfuzzerIncrementCounter(counter *uint8) { |
| 67 | + if *counter == 0xff { |
| 68 | + *counter = 1 |
| 69 | + } else { |
| 70 | + *counter++ |
| 71 | + } |
| 72 | +} |
| 73 | + |
45 | 74 | //go:linkname __sanitizer_cov_trace_cmp1 __sanitizer_cov_trace_cmp1
|
46 | 75 | //go:cgo_import_static __sanitizer_cov_trace_cmp1
|
47 | 76 | var __sanitizer_cov_trace_cmp1 byte
|
@@ -73,3 +102,19 @@ var __sanitizer_cov_trace_const_cmp4 byte
|
73 | 102 | //go:linkname __sanitizer_cov_trace_const_cmp8 __sanitizer_cov_trace_const_cmp8
|
74 | 103 | //go:cgo_import_static __sanitizer_cov_trace_const_cmp8
|
75 | 104 | var __sanitizer_cov_trace_const_cmp8 byte
|
| 105 | + |
| 106 | +//go:linkname __sanitizer_cov_8bit_counters_init __sanitizer_cov_8bit_counters_init |
| 107 | +//go:cgo_import_static __sanitizer_cov_8bit_counters_init |
| 108 | +var __sanitizer_cov_8bit_counters_init byte |
| 109 | + |
| 110 | +//go:linkname __start___sancov_cntrs __start___sancov_cntrs |
| 111 | +//go:cgo_import_static __start___sancov_cntrs |
| 112 | +var __start___sancov_cntrs byte |
| 113 | + |
| 114 | +//go:linkname __stop___sancov_cntrs __stop___sancov_cntrs |
| 115 | +//go:cgo_import_static __stop___sancov_cntrs |
| 116 | +var __stop___sancov_cntrs byte |
| 117 | + |
| 118 | +//go:linkname __sanitizer_cov_pcs_init __sanitizer_cov_pcs_init |
| 119 | +//go:cgo_import_static __sanitizer_cov_pcs_init |
| 120 | +var __sanitizer_cov_pcs_init byte |
0 commit comments