aboutsummaryrefslogtreecommitdiffstats
path: root/executor/common_kvm.h
diff options
context:
space:
mode:
authorAlexander Potapenko <glider@google.com>2025-09-30 11:02:37 +0200
committerAlexander Potapenko <glider@google.com>2025-10-17 06:51:20 +0000
commit6ca4530067ac25a78291b176b6d3dbe6ba592d15 (patch)
tree731490cefa5b43b5ccf48f83f77979a829da72c0 /executor/common_kvm.h
parent554d3ef1c24ae1bc3c8c439e8c8f3a9006434244 (diff)
executor: introduce DEFINE_GUEST_FN_TO_GPA_FN()
DEFINE_GUEST_FN_TO_GPA_FN() allows to define helper functions to calculate guest addresses in the host/guest code.
Diffstat (limited to 'executor/common_kvm.h')
-rw-r--r--executor/common_kvm.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/executor/common_kvm.h b/executor/common_kvm.h
index fecd00922..b11e3e06b 100644
--- a/executor/common_kvm.h
+++ b/executor/common_kvm.h
@@ -5,6 +5,24 @@
// Common KVM-related definitions.
+extern char *__start_guest, *__stop_guest;
+
+// Define a function that calculates the guest physical address for a guest function.
+// Execute failure_action if the function does not belong to the guest section.
+// This function is using volatile accesses, otherwise the compiler may attempt
+// to store e.g. &__start_guest + offset as a constant in .rodata.
+#define DEFINE_GUEST_FN_TO_GPA_FN(fn_name, offset, failure_action) \
+ static inline uintptr_t fn_name(uintptr_t f) \
+ { \
+ volatile uintptr_t start = (uintptr_t)&__start_guest; \
+ volatile uintptr_t stop = (uintptr_t)&__stop_guest; \
+ if (f >= start && f < stop) { \
+ return f - start + (offset); \
+ } \
+ (failure_action); \
+ return 0; \
+ }
+
#if SYZ_EXECUTOR || __NR_syz_kvm_assert_syzos_kvm_exit
static long syz_kvm_assert_syzos_kvm_exit(volatile long a0, volatile long a1)
{