Linux 4.9.89
[linux/fpc-iii.git] / drivers / misc / lkdtm_bugs.c
blob182ae1894b328861323c42767aadd5f2588318b9
1 /*
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
5 * test source files.
6 */
7 #include "lkdtm.h"
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)
17 #else
18 #define REC_STACK_SIZE (THREAD_SIZE / 8)
19 #endif
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);
32 if (!remaining)
33 return 0;
34 else
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)
41 if (*recur_param < 0)
42 *recur_param = recur_count;
43 else
44 recur_count = *recur_param;
47 void lkdtm_PANIC(void)
49 panic("dumptest");
52 void lkdtm_BUG(void)
54 BUG();
57 void lkdtm_WARNING(void)
59 WARN_ON(1);
62 void lkdtm_EXCEPTION(void)
64 *((int *) 0) = 0;
67 void lkdtm_LOOP(void)
69 for (;;)
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. */
81 char data[8];
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};
89 u32 *p;
90 u32 val = 0x12345678;
92 p = (u32 *)(data + 1);
93 if (*p == 0)
94 val = 0x87654321;
95 *p = val;
98 void lkdtm_SOFTLOCKUP(void)
100 preempt_disable();
101 for (;;)
102 cpu_relax();
105 void lkdtm_HARDLOCKUP(void)
107 local_irq_disable();
108 for (;;)
109 cpu_relax();
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);
123 schedule();
126 void lkdtm_ATOMIC_UNDERFLOW(void)
128 atomic_t under = ATOMIC_INIT(INT_MIN);
130 pr_info("attempting good atomic increment\n");
131 atomic_inc(&under);
132 atomic_dec(&under);
134 pr_info("attempting bad atomic underflow\n");
135 atomic_dec(&under);
138 void lkdtm_ATOMIC_OVERFLOW(void)
140 atomic_t over = ATOMIC_INIT(INT_MAX);
142 pr_info("attempting good atomic decrement\n");
143 atomic_dec(&over);
144 atomic_inc(&over);
146 pr_info("attempting bad atomic overflow\n");
147 atomic_inc(&over);