github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/executor/executor_test.h (about)

     1  // Copyright 2018 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  #include <stdlib.h>
     5  #include <sys/mman.h>
     6  #include <unistd.h>
     7  
     8  static void os_init(int argc, char** argv, void* data, size_t data_size)
     9  {
    10  	void* got = mmap(data, data_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
    11  	if (data != got)
    12  		failmsg("mmap of data segment failed", "want %p, got %p", data, got);
    13  	is_kernel_64_bit = sizeof(unsigned long) == 8;
    14  }
    15  
    16  static intptr_t execute_syscall(const call_t* c, intptr_t a[kMaxArgs])
    17  {
    18  	return c->call(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
    19  }
    20  
    21  static __thread unsigned long* local_cover_start = NULL;
    22  static __thread unsigned long* local_cover_end = NULL;
    23  
    24  #ifdef __clang__
    25  #define notrace
    26  #else
    27  #define notrace __attribute__((no_sanitize_coverage))
    28  #endif
    29  
    30  extern "C" notrace void __sanitizer_cov_trace_pc(void)
    31  {
    32  	unsigned long ip = (unsigned long)__builtin_return_address(0);
    33  	unsigned long* start = local_cover_start;
    34  	unsigned long* end = local_cover_end;
    35  	if (start == NULL || end == NULL)
    36  		return;
    37  	int pos = start[0];
    38  	if (start + pos + 1 < end) {
    39  		start[0] = pos + 1;
    40  		start[pos + 1] = ip;
    41  	}
    42  }
    43  
    44  static void cover_open(cover_t* cov, bool extra)
    45  {
    46  	cov->mmap_alloc_size = kCoverSize * sizeof(unsigned long);
    47  }
    48  
    49  static void cover_enable(cover_t* cov, bool collect_comps, bool extra)
    50  {
    51  	local_cover_start = (unsigned long*)cov->data;
    52  	local_cover_end = (unsigned long*)cov->data_end;
    53  }
    54  
    55  static void cover_reset(cover_t* cov)
    56  {
    57  	*(unsigned long*)(cov->data) = 0;
    58  }
    59  
    60  static void cover_collect(cover_t* cov)
    61  {
    62  	cov->size = *(unsigned long*)(cov->data);
    63  }
    64  
    65  static void cover_protect(cover_t* cov)
    66  {
    67  }
    68  
    69  static void cover_mmap(cover_t* cov)
    70  {
    71  	if (cov->data != NULL)
    72  		fail("cover_mmap invoked on an already mmapped cover_t object");
    73  	if (cov->mmap_alloc_size == 0)
    74  		fail("cover_t structure is corrupted");
    75  	cov->data = (char*)mmap(NULL, cov->mmap_alloc_size,
    76  				PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
    77  	if (cov->data == MAP_FAILED)
    78  		exitf("cover mmap failed");
    79  	cov->data_end = cov->data + cov->mmap_alloc_size;
    80  	cov->data_offset = sizeof(unsigned long);
    81  	// We don't care about the specific PC values for now.
    82  	// Once we do, we might want to consider ASLR here.
    83  	cov->pc_offset = 0;
    84  }
    85  
    86  #if SYZ_EXECUTOR_USES_SHMEM
    87  static void cover_unprotect(cover_t* cov)
    88  {
    89  }
    90  
    91  static bool use_cover_edges(uint64 pc)
    92  {
    93  	return true;
    94  }
    95  #endif