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
8 #include <linux/sched.h>
11 * Make sure our attempts to over run the kernel stack doesn't trigger
12 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
13 * recurse past the end of THREAD_SIZE by default.
15 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
16 #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
18 #define REC_STACK_SIZE (THREAD_SIZE / 8)
20 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
22 static int recur_count = REC_NUM_DEFAULT;
24 static DEFINE_SPINLOCK(lock_me_up);
26 static int recursive_loop(int remaining)
28 char buf[REC_STACK_SIZE];
30 /* Make sure compiler does not optimize this away. */
31 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
35 return recursive_loop(remaining - 1);
38 /* If the depth is negative, use the default, otherwise keep parameter. */
39 void __init lkdtm_bugs_init(int *recur_param)
42 *recur_param = recur_count;
44 recur_count = *recur_param;
47 void lkdtm_PANIC(void)
57 void lkdtm_WARNING(void)
62 void lkdtm_EXCEPTION(void)
73 void lkdtm_OVERFLOW(void)
75 (void) recursive_loop(recur_count);
78 noinline void lkdtm_CORRUPT_STACK(void)
80 /* Use default char array length that triggers stack protection. */
83 memset((void *)data, 0, 64);
86 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
88 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
92 p = (u32 *)(data + 1);
98 void lkdtm_SOFTLOCKUP(void)
105 void lkdtm_HARDLOCKUP(void)
112 void lkdtm_SPINLOCKUP(void)
114 /* Must be called twice to trigger. */
115 spin_lock(&lock_me_up);
116 /* Let sparse know we intended to exit holding the lock. */
117 __release(&lock_me_up);
120 void lkdtm_HUNG_TASK(void)
122 set_current_state(TASK_UNINTERRUPTIBLE);
126 void lkdtm_ATOMIC_UNDERFLOW(void)
128 atomic_t under = ATOMIC_INIT(INT_MIN);
130 pr_info("attempting good atomic increment\n");
134 pr_info("attempting bad atomic underflow\n");
138 void lkdtm_ATOMIC_OVERFLOW(void)
140 atomic_t over = ATOMIC_INIT(INT_MAX);
142 pr_info("attempting good atomic decrement\n");
146 pr_info("attempting bad atomic overflow\n");