forked from seclab-fudan/SyzDirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkcov.diff
164 lines (155 loc) · 5.11 KB
/
kcov.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
diff --git a/include/linux/kcov.h b/include/linux/kcov.h
index 55dc338f6bcd..36cd678425be 100644
--- a/include/linux/kcov.h
+++ b/include/linux/kcov.h
@@ -41,6 +41,7 @@ do { \
/* See Documentation/dev-tools/kcov.rst for usage details. */
void kcov_remote_start(u64 handle);
void kcov_remote_stop(void);
+void notrace kcov_mark_block(u32 i);
u64 kcov_common_handle(void);
static inline void kcov_remote_start_common(u64 id)
@@ -88,6 +89,6 @@ static inline void kcov_remote_start_common(u64 id) {}
static inline void kcov_remote_start_usb(u64 id) {}
static inline void kcov_remote_start_usb_softirq(u64 id) {}
static inline void kcov_remote_stop_softirq(void) {}
-
+static inline void kcov_mark_block(u32 i) {}
#endif /* CONFIG_KCOV */
#endif /* _LINUX_KCOV_H */
diff --git a/kernel/kcov.c b/kernel/kcov.c
index 36ca640c4f8e..71596e861104 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -30,6 +30,8 @@
/* Number of 64-bit words written per one comparison: */
#define KCOV_WORDS_PER_CMP 4
+#define DISTBLOCKSIZE 300
+
/*
* kcov descriptor (one per opened debugfs file).
* State transitions of the descriptor:
@@ -189,27 +191,45 @@ static notrace unsigned long canonicalize_ip(unsigned long ip)
* Entry point from instrumented code.
* This is called once per basic-block/edge.
*/
-void notrace __sanitizer_cov_trace_pc(void)
+void notrace __sanitizer_cov_trace_pc(u32 dt)
{
struct task_struct *t;
unsigned long *area;
unsigned long ip = canonicalize_ip(_RET_IP_);
unsigned long pos;
+ u32* dt_area;
t = current;
if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
return;
-
- area = t->kcov_area;
+
+ dt_area = t->kcov_area;
+ area = (unsigned long*)((u32*)t->kcov_area + DISTBLOCKSIZE);
/* The first 64-bit word is the number of subsequent PCs. */
pos = READ_ONCE(area[0]) + 1;
- if (likely(pos < t->kcov_size)) {
+ if (likely(pos < t->kcov_size - DISTBLOCKSIZE / 2)) {
area[pos] = ip;
WRITE_ONCE(area[0], pos);
- }
+ }
+ if (dt < READ_ONCE(dt_area[0]))
+ WRITE_ONCE(dt_area[0], dt);
}
EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
+void notrace kcov_mark_block(u32 i)
+{
+ struct task_struct *t;
+ u32 *dt_area;
+
+ t = current;
+ if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
+ return;
+
+ dt_area = (u32*)t->kcov_area + 1;
+ WRITE_ONCE(dt_area[i], READ_ONCE(dt_area[i]) + 1);
+}
+EXPORT_SYMBOL(kcov_mark_block);
+
#ifdef CONFIG_KCOV_ENABLE_COMPARISONS
static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip)
{
@@ -584,7 +604,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
* that must not overflow.
*/
size = arg;
- if (size < 2 || size > INT_MAX / sizeof(unsigned long))
+ if (size < DISTBLOCKSIZE / 2 + 2 || size > INT_MAX / sizeof(unsigned long))
return -EINVAL;
kcov->size = size;
kcov->mode = KCOV_MODE_INIT;
@@ -634,7 +654,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
mode = kcov_get_mode(remote_arg->trace_mode);
if (mode < 0)
return mode;
- if (remote_arg->area_size > LONG_MAX / sizeof(unsigned long))
+ if (remote_arg->area_size > LONG_MAX / sizeof(unsigned long) || remote_arg->area_size < DISTBLOCKSIZE / 2)
return -EINVAL;
kcov->mode = mode;
t->kcov = kcov;
@@ -821,6 +841,7 @@ void kcov_remote_start(u64 handle)
unsigned int size;
int sequence;
unsigned long flags;
+ u32* dt_area;
if (WARN_ON(!kcov_check_handle(handle, true, true, true)))
return;
@@ -887,7 +908,16 @@ void kcov_remote_start(u64 handle)
}
/* Reset coverage size. */
- *(u64 *)area = 0;
+ if (mode == KCOV_MODE_TRACE_PC) {
+ dt_area = area;
+ dt_area[0] = 0xffffffff;
+ // +2 is for buf distance
+ for (int i = 1; i < DISTBLOCKSIZE + 2; i++) {
+ dt_area[i] = 0;
+ }
+ } else {
+ *(u64 *)area = 0;
+ }
if (in_serving_softirq()) {
kcov_remote_softirq_start(t);
@@ -907,6 +937,7 @@ static void kcov_move_area(enum kcov_mode mode, void *dst_area,
u64 count_size, entry_size_log;
u64 dst_len, src_len;
void *dst_entries, *src_entries;
+ u32 *dst_dt_entries, *src_dt_entries;
u64 dst_occupied, dst_free, bytes_to_move, entries_moved;
kcov_debug("%px %u <= %px %lu\n",
@@ -914,6 +945,12 @@ static void kcov_move_area(enum kcov_mode mode, void *dst_area,
switch (mode) {
case KCOV_MODE_TRACE_PC:
+ dst_dt_entries = dst_area;
+ src_dt_entries = src_area;
+ dst_area_size -= DISTBLOCKSIZE / 2;
+ dst_area = dst_dt_entries + DISTBLOCKSIZE;
+ src_area = src_dt_entries + DISTBLOCKSIZE;
+
dst_len = READ_ONCE(*(unsigned long *)dst_area);
src_len = *(unsigned long *)src_area;
count_size = sizeof(unsigned long);
@@ -935,6 +972,14 @@ static void kcov_move_area(enum kcov_mode mode, void *dst_area,
if (dst_len > ((dst_area_size * word_size - count_size) >>
entry_size_log))
return;
+
+ if (mode == KCOV_MODE_TRACE_PC) {
+ dst_dt_entries[0] = min(dst_dt_entries[0], src_dt_entries[0]);
+ for (int i = 1; i < DISTBLOCKSIZE; i++) {
+ WRITE_ONCE(dst_dt_entries[i], READ_ONCE(dst_dt_entries[i]) + READ_ONCE(src_dt_entries[i]));
+ }
+ }
+
dst_occupied = count_size + (dst_len << entry_size_log);
dst_free = dst_area_size * word_size - dst_occupied;
bytes_to_move = min(dst_free, src_len << entry_size_log);