github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/executor/common_kvm.h (about)

     1  // Copyright 2025 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  #ifndef EXECUTOR_COMMON_KVM_H
     5  #define EXECUTOR_COMMON_KVM_H
     6  
     7  // This file is shared between executor and csource package.
     8  
     9  // Common KVM-related definitions.
    10  
    11  #include "common_kvm_syzos.h"
    12  #include "kvm.h"
    13  
    14  #if SYZ_EXECUTOR || __NR_syz_kvm_add_vcpu || __NR_syz_kvm_setup_cpu || __NR_syz_kvm_setup_syzos_vm
    15  extern char* __start_guest;
    16  
    17  // executor_fn_guest_addr() is compiled into both the host and the guest code.
    18  static inline uintptr_t executor_fn_guest_addr(void* fn)
    19  {
    20  	// Prevent the compiler from creating a .rodata constant for
    21  	// &__start_guest + SYZOS_ADDR_EXECUTOR_CODE.
    22  	volatile uintptr_t start = (uintptr_t)&__start_guest;
    23  	volatile uintptr_t offset = SYZOS_ADDR_EXECUTOR_CODE;
    24  	return (uintptr_t)fn - start + offset;
    25  }
    26  
    27  #if SYZ_EXECUTOR
    28  // In Clang-based C++ builds, use template magic to ensure that only guest functions can be passed
    29  // to executor_fn_guest_addr().
    30  template <typename R, typename... A>
    31  uintptr_t static inline executor_fn_guest_addr(__addrspace_guest R (*fn)(A...))
    32  {
    33  	return executor_fn_guest_addr((void*)fn);
    34  }
    35  
    36  #endif
    37  
    38  #endif
    39  
    40  #if SYZ_EXECUTOR || __NR_syz_kvm_assert_syzos_kvm_exit
    41  static long syz_kvm_assert_syzos_kvm_exit(volatile long a0, volatile long a1)
    42  {
    43  	struct kvm_run* run = (struct kvm_run*)a0;
    44  	uint64 expect = a1;
    45  
    46  	if (!run) {
    47  		errno = EINVAL;
    48  		return -1;
    49  	}
    50  
    51  	if (run->exit_reason != expect) {
    52  		errno = EDOM;
    53  		return -1;
    54  	}
    55  	return 0;
    56  }
    57  #endif
    58  
    59  #endif // EXECUTOR_COMMON_KVM_H