test_kprobes.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/kernel.h>
  3. #include <linux/kprobes.h>
  4. #include <linux/random.h>
  5. #include <kunit/test.h>
  6. #include "test_kprobes.h"
  7. static struct kprobe kp;
  8. static void setup_kprobe(struct kunit *test, struct kprobe *kp,
  9. const char *symbol, int offset)
  10. {
  11. kp->offset = offset;
  12. kp->addr = NULL;
  13. kp->symbol_name = symbol;
  14. }
  15. static void test_kprobe_offset(struct kunit *test, struct kprobe *kp,
  16. const char *target, int offset)
  17. {
  18. int ret;
  19. setup_kprobe(test, kp, target, 0);
  20. ret = register_kprobe(kp);
  21. if (!ret)
  22. unregister_kprobe(kp);
  23. KUNIT_EXPECT_EQ(test, 0, ret);
  24. setup_kprobe(test, kp, target, offset);
  25. ret = register_kprobe(kp);
  26. KUNIT_EXPECT_EQ(test, -EINVAL, ret);
  27. if (!ret)
  28. unregister_kprobe(kp);
  29. }
  30. static void test_kprobe_odd(struct kunit *test)
  31. {
  32. test_kprobe_offset(test, &kp, "kprobes_target_odd",
  33. kprobes_target_odd_offs);
  34. }
  35. static void test_kprobe_in_insn4(struct kunit *test)
  36. {
  37. test_kprobe_offset(test, &kp, "kprobes_target_in_insn4",
  38. kprobes_target_in_insn4_offs);
  39. }
  40. static void test_kprobe_in_insn6_lo(struct kunit *test)
  41. {
  42. test_kprobe_offset(test, &kp, "kprobes_target_in_insn6_lo",
  43. kprobes_target_in_insn6_lo_offs);
  44. }
  45. static void test_kprobe_in_insn6_hi(struct kunit *test)
  46. {
  47. test_kprobe_offset(test, &kp, "kprobes_target_in_insn6_hi",
  48. kprobes_target_in_insn6_hi_offs);
  49. }
  50. static struct kunit_case kprobes_testcases[] = {
  51. KUNIT_CASE(test_kprobe_odd),
  52. KUNIT_CASE(test_kprobe_in_insn4),
  53. KUNIT_CASE(test_kprobe_in_insn6_lo),
  54. KUNIT_CASE(test_kprobe_in_insn6_hi),
  55. {}
  56. };
  57. static struct kunit_suite kprobes_test_suite = {
  58. .name = "kprobes_test_s390",
  59. .test_cases = kprobes_testcases,
  60. };
  61. kunit_test_suites(&kprobes_test_suite);
  62. MODULE_LICENSE("GPL");