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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package lore
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/google/syzkaller/dashboard/dashapi"
"github.com/google/syzkaller/pkg/email"
)
// Thread is a generic representation of a single discussion in the mailing list.
type Thread struct {
Subject string
MessageID string
Type dashapi.DiscussionType
BugIDs []string
Messages []*email.Email
}
// Series represents a single patch series sent over email.
type Series struct {
Subject string
MessageID string
Version int
Corrupted string // If non-empty, contains a reason why the series better be ignored.
Tags []string
Patches []Patch
}
type Patch struct {
Seq int
*email.Email
}
// Threads extracts individual threads from a list of emails.
func Threads(emails []*email.Email) []*Thread {
return listThreads(emails, 0)
}
func listThreads(emails []*email.Email, maxDepth int) []*Thread {
ctx := &parseCtx{
maxDepth: maxDepth,
messages: map[string]*email.Email{},
next: map[*email.Email][]*email.Email{},
}
for _, email := range emails {
ctx.record(email)
}
ctx.process()
return ctx.threads
}
// PatchSeries is similar to Threads, but returns only the patch series submitted to the mailing lists.
func PatchSeries(emails []*email.Email) []*Series {
var ret []*Series
// Normally, all following series patches are sent in response to the first email sent.
// So there's no sense to look at deeper replies.
for _, thread := range listThreads(emails, 1) {
if thread.Type != dashapi.DiscussionPatch {
continue
}
patch, ok := parsePatchSubject(thread.Subject)
if !ok {
// It must never be happening.
panic("DiscussionPatch is set, but we fail to parse the thread subject")
}
total := patch.Total.ValueOr(1)
series := &Series{
Subject: patch.Title,
MessageID: thread.MessageID,
Version: patch.Version.ValueOr(1),
Tags: patch.Tags,
}
ret = append(ret, series)
if patch.Seq.IsSet() && patch.Seq.Value() > 1 {
series.Corrupted = "the first patch has seq>1"
continue
}
hasSeq := map[int]bool{}
for _, email := range thread.Messages {
patch, ok := parsePatchSubject(email.Subject)
if !ok {
continue
}
seq := patch.Seq.ValueOr(1)
if seq == 0 {
// The cover email is not of interest.
continue
}
if hasSeq[seq] {
// It's weird if that really happens, but let's skip for now.
continue
}
hasSeq[seq] = true
series.Patches = append(series.Patches, Patch{
Seq: seq,
Email: email,
})
}
if len(hasSeq) != total {
series.Corrupted = fmt.Sprintf("the subject mentions %d patches, %d are found",
total, len(hasSeq))
continue
}
if len(series.Patches) == 0 {
series.Corrupted = "0 patches"
continue
}
sort.Slice(series.Patches, func(i, j int) bool {
return series.Patches[i].Seq < series.Patches[j].Seq
})
}
return ret
}
// DiscussionType extracts the specific discussion type from an email.
func DiscussionType(msg *email.Email) dashapi.DiscussionType {
discType := dashapi.DiscussionMention
if msg.OwnEmail {
discType = dashapi.DiscussionReport
}
// This is very crude, but should work for now.
if _, ok := parsePatchSubject(msg.Subject); ok {
discType = dashapi.DiscussionPatch
} else if strings.Contains(msg.Subject, "Monthly") {
discType = dashapi.DiscussionReminder
}
return discType
}
type PatchSubject struct {
Title string
Tags []string // Sometimes there's e.g. "net" or "next-next" in the subject.
Version Optional[int]
Seq Optional[int] // The "Seq/Total" part.
Total Optional[int]
}
// nolint: lll
var patchSubjectRe = regexp.MustCompile(`(?mi)^\[(?:([\w\s-]+)\s)?PATCH(?:\s([\w\s-]+))??(?:\s0*(\d+)\/(\d+))?\]\s*(.+)`)
func parsePatchSubject(subject string) (PatchSubject, bool) {
var ret PatchSubject
groups := patchSubjectRe.FindStringSubmatch(subject)
if len(groups) == 0 {
return ret, false
}
tags := strings.Fields(groups[1])
for _, tag := range append(tags, strings.Fields(groups[2])...) {
if strings.HasPrefix(tag, "v") {
val, err := strconv.Atoi(strings.TrimPrefix(tag, "v"))
if err == nil {
ret.Version.Set(val)
continue
}
}
ret.Tags = append(ret.Tags, tag)
}
sort.Strings(ret.Tags)
if groups[3] != "" {
if val, err := strconv.Atoi(groups[3]); err == nil {
ret.Seq.Set(val)
}
}
if groups[4] != "" {
if val, err := strconv.Atoi(groups[4]); err == nil {
ret.Total.Set(val)
}
}
ret.Title = groups[5]
return ret, true
}
type parseCtx struct {
maxDepth int
threads []*Thread
messages map[string]*email.Email
next map[*email.Email][]*email.Email
}
func (c *parseCtx) record(msg *email.Email) {
c.messages[msg.MessageID] = msg
}
func (c *parseCtx) process() {
// List messages for which we dont't have ancestors.
nodes := []*email.Email{}
for _, msg := range c.messages {
if msg.InReplyTo == "" || c.messages[msg.InReplyTo] == nil {
nodes = append(nodes, msg)
} else {
parent := c.messages[msg.InReplyTo]
c.next[parent] = append(c.next[parent], msg)
}
}
// Iterate starting from these tree nodes.
for _, node := range nodes {
c.visit(node, nil, 0)
}
// Collect BugIDs.
for _, thread := range c.threads {
unique := map[string]struct{}{}
for _, msg := range thread.Messages {
for _, id := range msg.BugIDs {
unique[id] = struct{}{}
}
}
var ids []string
for id := range unique {
ids = append(ids, id)
}
sort.Strings(ids)
thread.BugIDs = ids
}
}
func (c *parseCtx) visit(msg *email.Email, thread *Thread, depth int) {
var oldInfo *email.OldThreadInfo
if thread != nil {
oldInfo = &email.OldThreadInfo{
ThreadType: thread.Type,
}
}
msgType := DiscussionType(msg)
switch email.NewMessageAction(msg, msgType, oldInfo) {
case email.ActionIgnore:
thread = nil
case email.ActionAppend:
thread.Messages = append(thread.Messages, msg)
case email.ActionNewThread:
thread = &Thread{
MessageID: msg.MessageID,
Subject: msg.Subject,
Type: msgType,
Messages: []*email.Email{msg},
}
c.threads = append(c.threads, thread)
}
if c.maxDepth == 0 || depth < c.maxDepth {
for _, nextMsg := range c.next[msg] {
c.visit(nextMsg, thread, depth+1)
}
}
}
type Optional[T any] struct {
val T
set bool
}
func value[T any](val T) Optional[T] {
return Optional[T]{val: val, set: true}
}
func (o Optional[T]) IsSet() bool {
return o.set
}
func (o Optional[T]) Value() T {
return o.val
}
func (o Optional[T]) ValueOr(def T) T {
if o.set {
return o.val
}
return def
}
func (o *Optional[T]) Set(val T) {
o.val = val
o.set = true
}
|