0001
0002 #ifndef _LINUX_STACKLEAK_H
0003 #define _LINUX_STACKLEAK_H
0004
0005 #include <linux/sched.h>
0006 #include <linux/sched/task_stack.h>
0007
0008
0009
0010
0011
0012 #define STACKLEAK_POISON -0xBEEF
0013 #define STACKLEAK_SEARCH_DEPTH 128
0014
0015 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
0016 #include <asm/stacktrace.h>
0017
0018
0019
0020
0021 static __always_inline unsigned long
0022 stackleak_task_low_bound(const struct task_struct *tsk)
0023 {
0024
0025
0026
0027
0028 return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
0029 }
0030
0031
0032
0033
0034
0035 static __always_inline unsigned long
0036 stackleak_task_high_bound(const struct task_struct *tsk)
0037 {
0038
0039
0040
0041
0042 return (unsigned long)task_pt_regs(tsk);
0043 }
0044
0045
0046
0047
0048
0049 static __always_inline unsigned long
0050 stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
0051 {
0052 const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
0053 unsigned int poison_count = 0;
0054 unsigned long poison_high = high;
0055 unsigned long sp = high;
0056
0057 while (sp > low && poison_count < depth) {
0058 sp -= sizeof(unsigned long);
0059
0060 if (*(unsigned long *)sp == STACKLEAK_POISON) {
0061 poison_count++;
0062 } else {
0063 poison_count = 0;
0064 poison_high = sp;
0065 }
0066 }
0067
0068 return poison_high;
0069 }
0070
0071 static inline void stackleak_task_init(struct task_struct *t)
0072 {
0073 t->lowest_stack = stackleak_task_low_bound(t);
0074 # ifdef CONFIG_STACKLEAK_METRICS
0075 t->prev_lowest_stack = t->lowest_stack;
0076 # endif
0077 }
0078
0079 #else
0080 static inline void stackleak_task_init(struct task_struct *t) { }
0081 #endif
0082
0083 #endif