test_ubsan.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. typedef void(*test_ubsan_fp)(void);
  6. static void test_ubsan_add_overflow(void)
  7. {
  8. volatile int val = INT_MAX;
  9. val += 2;
  10. }
  11. static void test_ubsan_sub_overflow(void)
  12. {
  13. volatile int val = INT_MIN;
  14. volatile int val2 = 2;
  15. val -= val2;
  16. }
  17. static void test_ubsan_mul_overflow(void)
  18. {
  19. volatile int val = INT_MAX / 2;
  20. val *= 3;
  21. }
  22. static void test_ubsan_negate_overflow(void)
  23. {
  24. volatile int val = INT_MIN;
  25. val = -val;
  26. }
  27. static void test_ubsan_divrem_overflow(void)
  28. {
  29. volatile int val = 16;
  30. volatile int val2 = 0;
  31. val /= val2;
  32. }
  33. static void test_ubsan_vla_bound_not_positive(void)
  34. {
  35. volatile int size = -1;
  36. char buf[size];
  37. (void)buf;
  38. }
  39. static void test_ubsan_shift_out_of_bounds(void)
  40. {
  41. volatile int val = -1;
  42. int val2 = 10;
  43. val2 <<= val;
  44. }
  45. static void test_ubsan_out_of_bounds(void)
  46. {
  47. volatile int i = 4, j = 5;
  48. volatile int arr[i];
  49. arr[j] = i;
  50. }
  51. static void test_ubsan_load_invalid_value(void)
  52. {
  53. volatile char *dst, *src;
  54. bool val, val2, *ptr;
  55. char c = 4;
  56. dst = (char *)&val;
  57. src = &c;
  58. *dst = *src;
  59. ptr = &val2;
  60. val2 = val;
  61. }
  62. static void test_ubsan_null_ptr_deref(void)
  63. {
  64. volatile int *ptr = NULL;
  65. int val;
  66. val = *ptr;
  67. }
  68. static void test_ubsan_misaligned_access(void)
  69. {
  70. volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
  71. volatile int *ptr, val = 6;
  72. ptr = (int *)(arr + 1);
  73. *ptr = val;
  74. }
  75. static void test_ubsan_object_size_mismatch(void)
  76. {
  77. /* "((aligned(8)))" helps this not into be misaligned for ptr-access. */
  78. volatile int val __aligned(8) = 4;
  79. volatile long long *ptr, val2;
  80. ptr = (long long *)&val;
  81. val2 = *ptr;
  82. }
  83. static const test_ubsan_fp test_ubsan_array[] = {
  84. test_ubsan_add_overflow,
  85. test_ubsan_sub_overflow,
  86. test_ubsan_mul_overflow,
  87. test_ubsan_negate_overflow,
  88. test_ubsan_divrem_overflow,
  89. test_ubsan_vla_bound_not_positive,
  90. test_ubsan_shift_out_of_bounds,
  91. test_ubsan_out_of_bounds,
  92. test_ubsan_load_invalid_value,
  93. //test_ubsan_null_ptr_deref, /* exclude it because there is a crash */
  94. test_ubsan_misaligned_access,
  95. test_ubsan_object_size_mismatch,
  96. };
  97. static int __init test_ubsan_init(void)
  98. {
  99. unsigned int i;
  100. for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
  101. test_ubsan_array[i]();
  102. (void)test_ubsan_null_ptr_deref; /* to avoid unsed-function warning */
  103. return 0;
  104. }
  105. module_init(test_ubsan_init);
  106. static void __exit test_ubsan_exit(void)
  107. {
  108. /* do nothing */
  109. }
  110. module_exit(test_ubsan_exit);
  111. MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
  112. MODULE_LICENSE("GPL v2");