2222#include <linux/kcov.h>
2323#include <asm/setup.h>
2424
25+ /* Number of 64-bit words written per one comparison: */
26+ #define KCOV_WORDS_PER_CMP 4
27+
2528/*
2629 * kcov descriptor (one per opened debugfs file).
2730 * State transitions of the descriptor:
2831 * - initial state after open()
2932 * - then there must be a single ioctl(KCOV_INIT_TRACE) call
3033 * - then, mmap() call (several calls are allowed but not useful)
31- * - then, repeated enable/disable for a task (only one task a time allowed)
34+ * - then, ioctl(KCOV_ENABLE, arg), where arg is
35+ * KCOV_TRACE_PC - to trace only the PCs
36+ * or
37+ * KCOV_TRACE_CMP - to trace only the comparison operands
38+ * - then, ioctl(KCOV_DISABLE) to disable the task.
39+ * Enabling/disabling ioctls can be repeated (only one task a time allowed).
3240 */
3341struct kcov {
3442 /*
@@ -48,51 +56,176 @@ struct kcov {
4856 struct task_struct * t ;
4957};
5058
51- /*
52- * Entry point from instrumented code.
53- * This is called once per basic-block/edge.
54- */
55- void notrace __sanitizer_cov_trace_pc (void )
59+ static bool check_kcov_mode (enum kcov_mode needed_mode , struct task_struct * t )
5660{
57- struct task_struct * t ;
5861 enum kcov_mode mode ;
5962
60- t = current ;
6163 /*
6264 * We are interested in code coverage as a function of a syscall inputs,
6365 * so we ignore code executed in interrupts.
6466 */
6567 if (!in_task ())
66- return ;
68+ return false ;
6769 mode = READ_ONCE (t -> kcov_mode );
68- if (mode == KCOV_MODE_TRACE ) {
69- unsigned long * area ;
70- unsigned long pos ;
71- unsigned long ip = _RET_IP_ ;
70+ /*
71+ * There is some code that runs in interrupts but for which
72+ * in_interrupt() returns false (e.g. preempt_schedule_irq()).
73+ * READ_ONCE()/barrier() effectively provides load-acquire wrt
74+ * interrupts, there are paired barrier()/WRITE_ONCE() in
75+ * kcov_ioctl_locked().
76+ */
77+ barrier ();
78+ return mode == needed_mode ;
79+ }
7280
81+ static unsigned long canonicalize_ip (unsigned long ip )
82+ {
7383#ifdef CONFIG_RANDOMIZE_BASE
74- ip -= kaslr_offset ();
84+ ip -= kaslr_offset ();
7585#endif
86+ return ip ;
87+ }
7688
77- /*
78- * There is some code that runs in interrupts but for which
79- * in_interrupt() returns false (e.g. preempt_schedule_irq()).
80- * READ_ONCE()/barrier() effectively provides load-acquire wrt
81- * interrupts, there are paired barrier()/WRITE_ONCE() in
82- * kcov_ioctl_locked().
83- */
84- barrier ();
85- area = t -> kcov_area ;
86- /* The first word is number of subsequent PCs. */
87- pos = READ_ONCE (area [0 ]) + 1 ;
88- if (likely (pos < t -> kcov_size )) {
89- area [pos ] = ip ;
90- WRITE_ONCE (area [0 ], pos );
91- }
89+ /*
90+ * Entry point from instrumented code.
91+ * This is called once per basic-block/edge.
92+ */
93+ void notrace __sanitizer_cov_trace_pc (void )
94+ {
95+ struct task_struct * t ;
96+ unsigned long * area ;
97+ unsigned long ip = canonicalize_ip (_RET_IP_ );
98+ unsigned long pos ;
99+
100+ t = current ;
101+ if (!check_kcov_mode (KCOV_MODE_TRACE_PC , t ))
102+ return ;
103+
104+ area = t -> kcov_area ;
105+ /* The first 64-bit word is the number of subsequent PCs. */
106+ pos = READ_ONCE (area [0 ]) + 1 ;
107+ if (likely (pos < t -> kcov_size )) {
108+ area [pos ] = ip ;
109+ WRITE_ONCE (area [0 ], pos );
92110 }
93111}
94112EXPORT_SYMBOL (__sanitizer_cov_trace_pc );
95113
114+ #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
115+ static void write_comp_data (u64 type , u64 arg1 , u64 arg2 , u64 ip )
116+ {
117+ struct task_struct * t ;
118+ u64 * area ;
119+ u64 count , start_index , end_pos , max_pos ;
120+
121+ t = current ;
122+ if (!check_kcov_mode (KCOV_MODE_TRACE_CMP , t ))
123+ return ;
124+
125+ ip = canonicalize_ip (ip );
126+
127+ /*
128+ * We write all comparison arguments and types as u64.
129+ * The buffer was allocated for t->kcov_size unsigned longs.
130+ */
131+ area = (u64 * )t -> kcov_area ;
132+ max_pos = t -> kcov_size * sizeof (unsigned long );
133+
134+ count = READ_ONCE (area [0 ]);
135+
136+ /* Every record is KCOV_WORDS_PER_CMP 64-bit words. */
137+ start_index = 1 + count * KCOV_WORDS_PER_CMP ;
138+ end_pos = (start_index + KCOV_WORDS_PER_CMP ) * sizeof (u64 );
139+ if (likely (end_pos <= max_pos )) {
140+ area [start_index ] = type ;
141+ area [start_index + 1 ] = arg1 ;
142+ area [start_index + 2 ] = arg2 ;
143+ area [start_index + 3 ] = ip ;
144+ WRITE_ONCE (area [0 ], count + 1 );
145+ }
146+ }
147+
148+ void notrace __sanitizer_cov_trace_cmp1 (u8 arg1 , u8 arg2 )
149+ {
150+ write_comp_data (KCOV_CMP_SIZE (0 ), arg1 , arg2 , _RET_IP_ );
151+ }
152+ EXPORT_SYMBOL (__sanitizer_cov_trace_cmp1 );
153+
154+ void notrace __sanitizer_cov_trace_cmp2 (u16 arg1 , u16 arg2 )
155+ {
156+ write_comp_data (KCOV_CMP_SIZE (1 ), arg1 , arg2 , _RET_IP_ );
157+ }
158+ EXPORT_SYMBOL (__sanitizer_cov_trace_cmp2 );
159+
160+ void notrace __sanitizer_cov_trace_cmp4 (u16 arg1 , u16 arg2 )
161+ {
162+ write_comp_data (KCOV_CMP_SIZE (2 ), arg1 , arg2 , _RET_IP_ );
163+ }
164+ EXPORT_SYMBOL (__sanitizer_cov_trace_cmp4 );
165+
166+ void notrace __sanitizer_cov_trace_cmp8 (u64 arg1 , u64 arg2 )
167+ {
168+ write_comp_data (KCOV_CMP_SIZE (3 ), arg1 , arg2 , _RET_IP_ );
169+ }
170+ EXPORT_SYMBOL (__sanitizer_cov_trace_cmp8 );
171+
172+ void notrace __sanitizer_cov_trace_const_cmp1 (u8 arg1 , u8 arg2 )
173+ {
174+ write_comp_data (KCOV_CMP_SIZE (0 ) | KCOV_CMP_CONST , arg1 , arg2 ,
175+ _RET_IP_ );
176+ }
177+ EXPORT_SYMBOL (__sanitizer_cov_trace_const_cmp1 );
178+
179+ void notrace __sanitizer_cov_trace_const_cmp2 (u16 arg1 , u16 arg2 )
180+ {
181+ write_comp_data (KCOV_CMP_SIZE (1 ) | KCOV_CMP_CONST , arg1 , arg2 ,
182+ _RET_IP_ );
183+ }
184+ EXPORT_SYMBOL (__sanitizer_cov_trace_const_cmp2 );
185+
186+ void notrace __sanitizer_cov_trace_const_cmp4 (u16 arg1 , u16 arg2 )
187+ {
188+ write_comp_data (KCOV_CMP_SIZE (2 ) | KCOV_CMP_CONST , arg1 , arg2 ,
189+ _RET_IP_ );
190+ }
191+ EXPORT_SYMBOL (__sanitizer_cov_trace_const_cmp4 );
192+
193+ void notrace __sanitizer_cov_trace_const_cmp8 (u64 arg1 , u64 arg2 )
194+ {
195+ write_comp_data (KCOV_CMP_SIZE (3 ) | KCOV_CMP_CONST , arg1 , arg2 ,
196+ _RET_IP_ );
197+ }
198+ EXPORT_SYMBOL (__sanitizer_cov_trace_const_cmp8 );
199+
200+ void notrace __sanitizer_cov_trace_switch (u64 val , u64 * cases )
201+ {
202+ u64 i ;
203+ u64 count = cases [0 ];
204+ u64 size = cases [1 ];
205+ u64 type = KCOV_CMP_CONST ;
206+
207+ switch (size ) {
208+ case 8 :
209+ type |= KCOV_CMP_SIZE (0 );
210+ break ;
211+ case 16 :
212+ type |= KCOV_CMP_SIZE (1 );
213+ break ;
214+ case 32 :
215+ type |= KCOV_CMP_SIZE (2 );
216+ break ;
217+ case 64 :
218+ type |= KCOV_CMP_SIZE (3 );
219+ break ;
220+ default :
221+ return ;
222+ }
223+ for (i = 0 ; i < count ; i ++ )
224+ write_comp_data (type , cases [i + 2 ], val , _RET_IP_ );
225+ }
226+ EXPORT_SYMBOL (__sanitizer_cov_trace_switch );
227+ #endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */
228+
96229static void kcov_get (struct kcov * kcov )
97230{
98231 atomic_inc (& kcov -> refcount );
@@ -129,6 +262,7 @@ void kcov_task_exit(struct task_struct *t)
129262 /* Just to not leave dangling references behind. */
130263 kcov_task_init (t );
131264 kcov -> t = NULL ;
265+ kcov -> mode = KCOV_MODE_INIT ;
132266 spin_unlock (& kcov -> lock );
133267 kcov_put (kcov );
134268}
@@ -147,7 +281,7 @@ static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
147281
148282 spin_lock (& kcov -> lock );
149283 size = kcov -> size * sizeof (unsigned long );
150- if (kcov -> mode == KCOV_MODE_DISABLED || vma -> vm_pgoff != 0 ||
284+ if (kcov -> mode != KCOV_MODE_INIT || vma -> vm_pgoff != 0 ||
151285 vma -> vm_end - vma -> vm_start != size ) {
152286 res = - EINVAL ;
153287 goto exit ;
@@ -176,6 +310,7 @@ static int kcov_open(struct inode *inode, struct file *filep)
176310 kcov = kzalloc (sizeof (* kcov ), GFP_KERNEL );
177311 if (!kcov )
178312 return - ENOMEM ;
313+ kcov -> mode = KCOV_MODE_DISABLED ;
179314 atomic_set (& kcov -> refcount , 1 );
180315 spin_lock_init (& kcov -> lock );
181316 filep -> private_data = kcov ;
@@ -211,7 +346,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
211346 if (size < 2 || size > INT_MAX / sizeof (unsigned long ))
212347 return - EINVAL ;
213348 kcov -> size = size ;
214- kcov -> mode = KCOV_MODE_TRACE ;
349+ kcov -> mode = KCOV_MODE_INIT ;
215350 return 0 ;
216351 case KCOV_ENABLE :
217352 /*
@@ -221,17 +356,25 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
221356 * at task exit or voluntary by KCOV_DISABLE. After that it can
222357 * be enabled for another task.
223358 */
224- unused = arg ;
225- if (unused != 0 || kcov -> mode == KCOV_MODE_DISABLED ||
226- kcov -> area == NULL )
359+ if (kcov -> mode != KCOV_MODE_INIT || !kcov -> area )
227360 return - EINVAL ;
228361 if (kcov -> t != NULL )
229362 return - EBUSY ;
363+ if (arg == KCOV_TRACE_PC )
364+ kcov -> mode = KCOV_MODE_TRACE_PC ;
365+ else if (arg == KCOV_TRACE_CMP )
366+ #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
367+ kcov -> mode = KCOV_MODE_TRACE_CMP ;
368+ #else
369+ return - ENOTSUPP ;
370+ #endif
371+ else
372+ return - EINVAL ;
230373 t = current ;
231374 /* Cache in task struct for performance. */
232375 t -> kcov_size = kcov -> size ;
233376 t -> kcov_area = kcov -> area ;
234- /* See comment in __sanitizer_cov_trace_pc (). */
377+ /* See comment in check_kcov_mode (). */
235378 barrier ();
236379 WRITE_ONCE (t -> kcov_mode , kcov -> mode );
237380 t -> kcov = kcov ;
@@ -249,6 +392,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
249392 return - EINVAL ;
250393 kcov_task_init (t );
251394 kcov -> t = NULL ;
395+ kcov -> mode = KCOV_MODE_INIT ;
252396 kcov_put (kcov );
253397 return 0 ;
254398 default :
0 commit comments