guest_print_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A test for GUEST_PRINTF
  4. *
  5. * Copyright 2022, Google, Inc. and/or its affiliates.
  6. */
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/ioctl.h>
  12. #include "test_util.h"
  13. #include "kvm_util.h"
  14. #include "processor.h"
  15. #include "ucall_common.h"
  16. struct guest_vals {
  17. uint64_t a;
  18. uint64_t b;
  19. uint64_t type;
  20. };
  21. static struct guest_vals vals;
  22. /* GUEST_PRINTF()/GUEST_ASSERT_FMT() does not support float or double. */
  23. #define TYPE_LIST \
  24. TYPE(test_type_i64, I64, "%ld", int64_t) \
  25. TYPE(test_type_u64, U64u, "%lu", uint64_t) \
  26. TYPE(test_type_x64, U64x, "0x%lx", uint64_t) \
  27. TYPE(test_type_X64, U64X, "0x%lX", uint64_t) \
  28. TYPE(test_type_u32, U32u, "%u", uint32_t) \
  29. TYPE(test_type_x32, U32x, "0x%x", uint32_t) \
  30. TYPE(test_type_X32, U32X, "0x%X", uint32_t) \
  31. TYPE(test_type_int, INT, "%d", int) \
  32. TYPE(test_type_char, CHAR, "%c", char) \
  33. TYPE(test_type_str, STR, "'%s'", const char *) \
  34. TYPE(test_type_ptr, PTR, "%p", uintptr_t)
  35. enum args_type {
  36. #define TYPE(fn, ext, fmt_t, T) TYPE_##ext,
  37. TYPE_LIST
  38. #undef TYPE
  39. };
  40. static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
  41. const char *expected_assert);
  42. #define BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T) \
  43. const char *PRINTF_FMT_##ext = "Got params a = " fmt_t " and b = " fmt_t; \
  44. const char *ASSERT_FMT_##ext = "Expected " fmt_t ", got " fmt_t " instead"; \
  45. static void fn(struct kvm_vcpu *vcpu, T a, T b) \
  46. { \
  47. char expected_printf[UCALL_BUFFER_LEN]; \
  48. char expected_assert[UCALL_BUFFER_LEN]; \
  49. \
  50. snprintf(expected_printf, UCALL_BUFFER_LEN, PRINTF_FMT_##ext, a, b); \
  51. snprintf(expected_assert, UCALL_BUFFER_LEN, ASSERT_FMT_##ext, a, b); \
  52. vals = (struct guest_vals){ (uint64_t)a, (uint64_t)b, TYPE_##ext }; \
  53. sync_global_to_guest(vcpu->vm, vals); \
  54. run_test(vcpu, expected_printf, expected_assert); \
  55. }
  56. #define TYPE(fn, ext, fmt_t, T) \
  57. BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T)
  58. TYPE_LIST
  59. #undef TYPE
  60. static void guest_code(void)
  61. {
  62. while (1) {
  63. switch (vals.type) {
  64. #define TYPE(fn, ext, fmt_t, T) \
  65. case TYPE_##ext: \
  66. GUEST_PRINTF(PRINTF_FMT_##ext, vals.a, vals.b); \
  67. __GUEST_ASSERT(vals.a == vals.b, \
  68. ASSERT_FMT_##ext, vals.a, vals.b); \
  69. break;
  70. TYPE_LIST
  71. #undef TYPE
  72. default:
  73. GUEST_SYNC(vals.type);
  74. }
  75. GUEST_DONE();
  76. }
  77. }
  78. /*
  79. * Unfortunately this gets a little messy because 'assert_msg' doesn't
  80. * just contains the matching string, it also contains additional assert
  81. * info. Fortunately the part that matches should be at the very end of
  82. * 'assert_msg'.
  83. */
  84. static void ucall_abort(const char *assert_msg, const char *expected_assert_msg)
  85. {
  86. int len_str = strlen(assert_msg);
  87. int len_substr = strlen(expected_assert_msg);
  88. int offset = len_str - len_substr;
  89. TEST_ASSERT(len_substr <= len_str,
  90. "Expected '%s' to be a substring of '%s'",
  91. assert_msg, expected_assert_msg);
  92. TEST_ASSERT(strcmp(&assert_msg[offset], expected_assert_msg) == 0,
  93. "Unexpected mismatch. Expected: '%s', got: '%s'",
  94. expected_assert_msg, &assert_msg[offset]);
  95. }
  96. /*
  97. * Open code vcpu_run(), sans the UCALL_ABORT handling, so that intentional
  98. * guest asserts guest can be verified instead of being reported as failures.
  99. */
  100. static void do_vcpu_run(struct kvm_vcpu *vcpu)
  101. {
  102. int r;
  103. do {
  104. r = __vcpu_run(vcpu);
  105. } while (r == -1 && errno == EINTR);
  106. TEST_ASSERT(!r, KVM_IOCTL_ERROR(KVM_RUN, r));
  107. }
  108. static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
  109. const char *expected_assert)
  110. {
  111. struct kvm_run *run = vcpu->run;
  112. struct ucall uc;
  113. while (1) {
  114. do_vcpu_run(vcpu);
  115. TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
  116. "Unexpected exit reason: %u (%s),",
  117. run->exit_reason, exit_reason_str(run->exit_reason));
  118. switch (get_ucall(vcpu, &uc)) {
  119. case UCALL_SYNC:
  120. TEST_FAIL("Unknown 'args_type' = %lu", uc.args[1]);
  121. break;
  122. case UCALL_PRINTF:
  123. TEST_ASSERT(strcmp(uc.buffer, expected_printf) == 0,
  124. "Unexpected mismatch. Expected: '%s', got: '%s'",
  125. expected_printf, uc.buffer);
  126. break;
  127. case UCALL_ABORT:
  128. ucall_abort(uc.buffer, expected_assert);
  129. break;
  130. case UCALL_DONE:
  131. return;
  132. default:
  133. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  134. }
  135. }
  136. }
  137. static void guest_code_limits(void)
  138. {
  139. char test_str[UCALL_BUFFER_LEN + 10];
  140. memset(test_str, 'a', sizeof(test_str));
  141. test_str[sizeof(test_str) - 1] = 0;
  142. GUEST_PRINTF("%s", test_str);
  143. }
  144. static void test_limits(void)
  145. {
  146. struct kvm_vcpu *vcpu;
  147. struct kvm_run *run;
  148. struct kvm_vm *vm;
  149. struct ucall uc;
  150. vm = vm_create_with_one_vcpu(&vcpu, guest_code_limits);
  151. run = vcpu->run;
  152. do_vcpu_run(vcpu);
  153. TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
  154. "Unexpected exit reason: %u (%s),",
  155. run->exit_reason, exit_reason_str(run->exit_reason));
  156. TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_ABORT,
  157. "Unexpected ucall command: %lu, Expected: %u (UCALL_ABORT)",
  158. uc.cmd, UCALL_ABORT);
  159. kvm_vm_free(vm);
  160. }
  161. int main(int argc, char *argv[])
  162. {
  163. struct kvm_vcpu *vcpu;
  164. struct kvm_vm *vm;
  165. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  166. test_type_i64(vcpu, -1, -1);
  167. test_type_i64(vcpu, -1, 1);
  168. test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
  169. test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
  170. test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
  171. test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
  172. test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
  173. test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
  174. test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
  175. test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
  176. test_type_u32(vcpu, 0x90abcdef, 0x90abcdef);
  177. test_type_u32(vcpu, 0x90abcdef, 0x90abcdee);
  178. test_type_x32(vcpu, 0x90abcdef, 0x90abcdef);
  179. test_type_x32(vcpu, 0x90abcdef, 0x90abcdee);
  180. test_type_X32(vcpu, 0x90abcdef, 0x90abcdef);
  181. test_type_X32(vcpu, 0x90abcdef, 0x90abcdee);
  182. test_type_int(vcpu, -1, -1);
  183. test_type_int(vcpu, -1, 1);
  184. test_type_int(vcpu, 1, 1);
  185. test_type_char(vcpu, 'a', 'a');
  186. test_type_char(vcpu, 'a', 'A');
  187. test_type_char(vcpu, 'a', 'b');
  188. test_type_str(vcpu, "foo", "foo");
  189. test_type_str(vcpu, "foo", "bar");
  190. test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
  191. test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
  192. kvm_vm_free(vm);
  193. test_limits();
  194. return 0;
  195. }