2 * This is for all the tests related to logic bugs (e.g. bad dereferences,
3 * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
4 * lockups) along with other things that don't fit well into existing LKDTM
7 #define pr_fmt(fmt) "lkdtm: " fmt
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
15 * Make sure our attempts to over run the kernel stack doesn't trigger
16 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
17 * recurse past the end of THREAD_SIZE by default.
19 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
20 #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
22 #define REC_STACK_SIZE (THREAD_SIZE / 8)
24 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
26 static int recur_count = REC_NUM_DEFAULT;
28 static DEFINE_SPINLOCK(lock_me_up);
30 static int recursive_loop(int remaining)
32 char buf[REC_STACK_SIZE];
34 /* Make sure compiler does not optimize this away. */
35 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
39 return recursive_loop(remaining - 1);
42 /* If the depth is negative, use the default, otherwise keep parameter. */
43 void __init lkdtm_bugs_init(int *recur_param)
46 *recur_param = recur_count;
48 recur_count = *recur_param;
51 void lkdtm_PANIC(void)
61 void lkdtm_WARNING(void)
66 void lkdtm_EXCEPTION(void)
77 void lkdtm_OVERFLOW(void)
79 (void) recursive_loop(recur_count);
82 noinline void lkdtm_CORRUPT_STACK(void)
84 /* Use default char array length that triggers stack protection. */
87 memset((void *)data, 0, 64);
90 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
92 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
96 p = (u32 *)(data + 1);
102 void lkdtm_SOFTLOCKUP(void)
109 void lkdtm_HARDLOCKUP(void)
116 void lkdtm_SPINLOCKUP(void)
118 /* Must be called twice to trigger. */
119 spin_lock(&lock_me_up);
120 /* Let sparse know we intended to exit holding the lock. */
121 __release(&lock_me_up);
124 void lkdtm_HUNG_TASK(void)
126 set_current_state(TASK_UNINTERRUPTIBLE);
130 void lkdtm_ATOMIC_UNDERFLOW(void)
132 atomic_t under = ATOMIC_INIT(INT_MIN);
134 pr_info("attempting good atomic increment\n");
138 pr_info("attempting bad atomic underflow\n");
142 void lkdtm_ATOMIC_OVERFLOW(void)
144 atomic_t over = ATOMIC_INIT(INT_MAX);
146 pr_info("attempting good atomic decrement\n");
150 pr_info("attempting bad atomic overflow\n");