aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/nfagraph/ng_anchored_dots.cpp
blob: 11d6861d690aef6c47471e17bb02afe38290a9e7 (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * Copyright (c) 2015-2017, Intel Corporation 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/** \file
 * \brief Analysis pass to reform leading dots.
 *
 * We have found that many regexes found in the wild use an anchored dot-repeat
 * to represent an unanchored pattern, particularly if they have been used with
 * a regex engine that assumes that a pattern is anchored. This pass reforms
 * patterns that begin with sequences of dots into a more standard form.
 *
 * In addition, both anchored and unanchored patterns with dot repeats as
 * prefixes will have these prefixes reformed into a canonical form, which some
 * later analyses depend upon.
 */
#include "ng_anchored_dots.h"

#include "grey.h"
#include "ng_holder.h"
#include "ng_util.h"
#include "ue2common.h"
#include "util/container.h"
#include "util/depth.h"
#include "util/graph_range.h"

#include <algorithm>
#include <queue>
#include <set>
#include <vector>

using namespace std;

namespace ue2 {

static
bool findStarts(const NGHolder &g, set<NFAVertex> &anchored,
                set<NFAVertex> &unanchored) {
    // Populate unanchored map
    for (auto v : adjacent_vertices_range(g.startDs, g)) {
        if (is_special(v, g)) {
            continue;
        }
        unanchored.insert(v);
    }

    // Populate anchored map
    for (auto v : adjacent_vertices_range(g.start, g)) {
        if (is_special(v, g)) {
            continue;
        }
        anchored.insert(v);
    }

    if (unanchored == anchored) {
        anchored.clear();
    } else if (!unanchored.empty() && !anchored.empty()) {
        return false;
    }

    return !anchored.empty() || !unanchored.empty();
}

namespace {
class DotInfo {
public:
    DotInfo(NFAVertex v, bool se, u32 idx)
        : vertex(v), hasSelfLoop(se), index(idx) {}

    bool operator<(const DotInfo &other) const {
        if (hasSelfLoop != other.hasSelfLoop)
            return hasSelfLoop < other.hasSelfLoop;
        // tie break with vertex id: lowest ID wins
        return index > other.index;
    }

    NFAVertex vertex;
    bool hasSelfLoop;
    u32 index;
};
}

// Returns nullptr if all vertices in the given set are not dots.
// We can only pick one dot vertex, so we go for a dot-star if it exists,
// otherwise the dot without a self-edge with the lowest ID.
static
NFAVertex findReformable(const NGHolder &g, const set<NFAVertex> &starts,
                         set<NFAVertex> &otherV) {
    priority_queue<DotInfo> dotq;
    for (auto v : starts) {
        if (is_dot(v, g)) {
            u32 idx = g[v].index;
            dotq.push(DotInfo(v, hasSelfLoop(v, g), idx));
        }
    }

    if (dotq.empty()) {
        return NGHolder::null_vertex(); 
    }

    const DotInfo &dot = dotq.top();
    otherV = starts;
    otherV.erase(dot.vertex);
    DEBUG_PRINTF("selected dot vertex %u (%s)\n", dot.index,
                 dot.hasSelfLoop ? "has self-edge" : "no self-edge");
    DEBUG_PRINTF("%zu other vertices\n", otherV.size());
    return dot.vertex;
}

// Returns true if the given vertex is only preceded by start. If start is
// graph.startDs (i.e. unanchored), the given vertex can also be connected to
// graph.start. If selfLoopIsAcceptable is set, self-loops are ignored.
static
bool isStartNode(NFAVertex v, NFAVertex start, const NGHolder &g,
                 bool selfLoopIsAcceptable) {
    for (auto u : inv_adjacent_vertices_range(v, g)) {
        if (selfLoopIsAcceptable && u == v) {
            continue;
        } else if (u == start) {
            continue;
        } else if (start == g.startDs && u == g.start) {
            continue;
        } else {
            return false;
        }
    }
    return true;
}

// Note: this will only remove the anchored first dot in the chain -- any other
// removable nodes will be handled by the unanchored case below.
static
void reformAnchoredRepeatsComponent(NGHolder &g,
                                    set<NFAVertex> &compAnchoredStarts,
                                    set<NFAVertex> &compUnanchoredStarts,
                                    set<NFAVertex> &dead, depth *startBegin,
                                    depth *startEnd) {
    // anchored cases can not have any unanchored starts
    if (!compUnanchoredStarts.empty()) {
        DEBUG_PRINTF("we have unanchored starts, skipping\n");
        return;
    }

    NFAVertex dotV = NGHolder::null_vertex(); 
    set<NFAVertex> otherV;
    dotV = findReformable(g, compAnchoredStarts, otherV);
    if (dotV == NGHolder::null_vertex()) { 
        DEBUG_PRINTF("no candidate reformable dot found.\n");
        return;
    }

    NFAEdge loopEdge;
    bool selfLoop = false;
    bool bustOut = false;

    for (const auto &e : out_edges_range(dotV, g)) {
        NFAVertex t = target(e, g);
        if (t == dotV) {
            selfLoop = true;
            loopEdge = e;
            continue;
        }

        if (is_special(t, g)) {
            bustOut = true;
            break;
        }

        if (!otherV.empty() && otherV.find(t) == otherV.end()) {
            bustOut = true;
            break;
        }
    }

    if (bustOut) {
        DEBUG_PRINTF("busting out\n");
        return;
    }

    if (!isStartNode(dotV, g.start, g, true)) {
        DEBUG_PRINTF("fleeing: vertex %zu has other preds\n", g[dotV].index); 
        return;
    }

    /* get bounds */
    depth min;
    depth max(1); 

    if (selfLoop) {
        // A self-loop indicates that this is a '.+' or '.*'
        max = depth::infinity();
    }

    if (!otherV.empty()) {
        /* We require that the successors of the dot node are are the same
         * as the start vertex. TODO: remember why.
         */
        if (selfLoop) {
            if (otherV.size() != out_degree(dotV, g) - 1) {
                return;
            }
        } else {
            if (otherV.size() != out_degree(dotV, g)) {
                return;
            }
        }

        min = depth(0); 
    } else {
        min = depth(1); 
    }

    *startBegin = min;
    *startEnd = max;

    for (auto t : adjacent_vertices_range(dotV, g)) {
        if (t != dotV) {
            add_edge_if_not_present(g.startDs, t, g);
            add_edge_if_not_present(g.start, t, g);
            compUnanchoredStarts.insert(t);
        }
    }

    for (auto v : otherV) {
        remove_edge(g.start, v, g);
    }

    DEBUG_PRINTF("removing vertex %zu\n", g[dotV].index); 
    clear_vertex(dotV, g);
    dead.insert(dotV);
    compAnchoredStarts.erase(dotV);
}

static
void reformUnanchoredRepeatsComponent(NGHolder &g,
                                      set<NFAVertex> &compAnchoredStarts,
                                      set<NFAVertex> &compUnanchoredStarts,
                                      set<NFAVertex> &dead,
                                      depth *startBegin, depth *startEnd) {
    // unanchored cases can not have any anchored starts
    if (!compAnchoredStarts.empty()) {
        DEBUG_PRINTF("we have anchored starts, skipping\n");
        return;
    }

    while (true) {
        NFAVertex dotV = NGHolder::null_vertex(); 
        set<NFAVertex> otherV;
        dotV = findReformable(g, compUnanchoredStarts, otherV);
        if (dotV == NGHolder::null_vertex()) { 
            DEBUG_PRINTF("no candidate reformable dot found.\n");
            return;
        }

        NFAEdge loopEdge;
        bool selfLoop = false;
        bool bustOut = false;

        for (const auto &e : out_edges_range(dotV, g)) {
            NFAVertex t = target(e, g);

            if (t == dotV) {
                selfLoop = true;
                loopEdge = e;
                continue;
            }

            if (is_special(t, g)) {
                bustOut = true;
                break;
            }

            if (!otherV.empty() && otherV.find(t) == otherV.end()) {
                bustOut = true;
                break;
            }
        }

        if (bustOut) {
            DEBUG_PRINTF("busting out\n");
            if (!selfLoop) {
                return;
            }

            for (auto v : otherV) {
                if (!edge(dotV, v, g).second) {
                    return;
                }
            }

            // A self-loop indicates that this is a '.+' or '.*'
            DEBUG_PRINTF("self-loop detected on %zu\n", g[dotV].index); 
            *startEnd = depth::infinity();
            remove_edge(dotV, dotV, g);
            return;
        }

        if (!isStartNode(dotV, g.startDs, g, true)) {
            DEBUG_PRINTF("fleeing: vertex %zu has other preds\n", 
                         g[dotV].index); 
            return;
        }

        /* get bounds */
        depth min(1); 
        depth max(1); 

        if (selfLoop) {
            // A self-loop indicates that this is a '.+' or '.*'
            DEBUG_PRINTF("self-loop detected\n");
            max = depth::infinity();
        }

        if (!otherV.empty()) {
            if (!selfLoop && otherV.size() != out_degree(dotV, g)) {
                return;
            }

            if (selfLoop && otherV.size() != out_degree(dotV, g) - 1) {
                return;
            }

            if (min > depth(1)) {
                /* this is not a case we can handle */
                DEBUG_PRINTF("min greater than one, skipping\n");
                return;
            }
            min = depth(0); 
        }

        *startBegin += min;
        *startEnd += max;

        for (auto v : otherV) {
            remove_edge(g.start, v, g);
            remove_edge(g.startDs, v, g);
        }

        compUnanchoredStarts.clear();
        for (auto t : adjacent_vertices_range(dotV, g)) {
            if (t != dotV) {
                DEBUG_PRINTF("connecting sds -> %zu\n", g[t].index); 
                add_edge(g.startDs, t, g);
                add_edge(g.start, t, g);
                compUnanchoredStarts.insert(t);
            }
        }

        DEBUG_PRINTF("removing vertex %zu\n", g[dotV].index); 
        dead.insert(dotV);
        clear_vertex(dotV, g);
        compUnanchoredStarts.erase(dotV);
    }
}

// for t to be another optional dot, it must have only in-edges from v and from
// starts
static
bool isOptionalDot(NFAVertex t, NFAVertex v, const NGHolder &g) {
    if (!is_dot(t, g)) {
        return false;
    }

    bool found_v = false, found_start = false;

    for (auto u : inv_adjacent_vertices_range(t, g)) {
        if (u == v) {
            found_v = true;
        } else if (u == g.start || u == g.startDs) {
            found_start = true;
        } else {
            return false;
        }
    }

    return found_v && found_start;
}

static
bool gatherParticipants(const NGHolder &g,
                        NFAVertex start, NFAVertex initialDot,
                        set<NFAVertex> &dots, set<NFAVertex> &succ) {
    // Walk the graph downwards from the initial dot; each dot will have:
    //      1) a single optional dot successor, or
    //      2) N successors (our terminating case)
    dots.insert(initialDot);
    NFAVertex v = initialDot;

    while (out_degree(v, g) == 1) {
        NFAVertex t = *(adjacent_vertices(v, g).first);
        // for t to be another optional dot, it must have only in-edges from v
        // and from starts
        if (isOptionalDot(t, v, g)) {
            // another dot; bail if we've seen it once already
            if (dots.find(t) != dots.end()) {
                DEBUG_PRINTF("cycle detected at vertex %zu\n", g[t].index); 
                return false;
            }
            dots.insert(t);
            v = t;
            continue;
        }
        // otherwise, we found a terminating dot state
        break;
    }

    // Our terminating states are the successors of v.
    // All of these MUST have an edge from start as well.
    for (auto w : adjacent_vertices_range(v, g)) {
        succ.insert(w);
        if (!edge(start, w, g).second) {
            DEBUG_PRINTF("failing, vertex %zu does not have edge from start\n", 
                         g[w].index);
            return false;
        }
    }

    /* All the non chained v connected to start must be in succ as well
     * TODO: remember why (and document). */
    for (auto u : adjacent_vertices_range(start, g)) {
        if (is_special(u, g)) {
            continue;
        }
        if (!contains(dots, u) && !contains(succ, u)) {
            return false;
        }
    }

    return !succ.empty();
}

static
void collapseVariableDotRepeat(NGHolder &g, NFAVertex start,
                               set<NFAVertex> &dead, UNUSED depth *startBegin,
                               depth *startEnd) {
    // Handle optional dot repeat prefixes, e.g.
    //     /^.{0,30}foo/s, /^.{0,5}foo/s, unanchored equivs
    // Note that this code assumes that fixed repeats ('^.{5,20}') have been
    // pruned already, down (in this case) to '^.{0,15}'.

    // The first of our optional dots must be connected to start. The jump edge
    // past it will be verified in gatherParticipants(). If start is
    // graph.start, it should not be connected to startDs.
    NFAVertex initialDot = NGHolder::null_vertex(); 
    for (auto v : adjacent_vertices_range(start, g)) {
        if (is_special(v, g)) {
            continue;
        }
        if (is_dot(v, g) && isStartNode(v, start, g, false)) {
            if (initialDot) {
                return;
            }
            initialDot = v;
            DEBUG_PRINTF("initial dot vertex is %zu\n", g[v].index); 
        }
    }

    if (!initialDot) {
        return;
    }

    // Collect all the other optional dot vertices and the successor vertices
    // by walking down the graph from initialDot
    set<NFAVertex> dots, succ;
    if (!gatherParticipants(g, start, initialDot, dots, succ)) {
        DEBUG_PRINTF("gatherParticipants failed\n");
        return;
    }

    DEBUG_PRINTF("optional dot repeat with %zu participants, "
                 "terminating in %zu non-dot nodes\n",
                 dots.size(), succ.size());

    // Remove all the participants and set the start offset
    dead.insert(dots.begin(), dots.end());

    DEBUG_PRINTF("current offsets: %s-%s\n", startBegin->str().c_str(),
                 startEnd->str().c_str());

    if (start == g.start && startEnd->is_infinite()) {
        *startEnd = depth(dots.size()); 
    } else if (startEnd->is_finite()) {
        *startEnd += dots.size();
    }
    assert(startEnd->is_reachable());

    // Connect our successor vertices to both start and startDs.
    for (auto v : succ) { 
        add_edge_if_not_present(g.start, v, g);
        add_edge_if_not_present(g.startDs, v, g);
    }
}

static
void deleteVertices(set<NFAVertex> &dead, NGHolder &g) {
    if (!dead.empty()) {
        DEBUG_PRINTF("pruning %zu vertices\n", dead.size());
        remove_vertices(dead, g);
    }
    dead.clear();
}

static
void reformAnchoredRepeats(NGHolder &g, depth *startBegin, depth *startEnd) {
    DEBUG_PRINTF("component\n");
    set<NFAVertex> anchored, unanchored, dead;
    if (!findStarts(g, anchored, unanchored)) {
        DEBUG_PRINTF("no starts\n");
        return;
    }

    reformAnchoredRepeatsComponent(g, anchored, unanchored, dead, startBegin,
                                   startEnd);
    deleteVertices(dead, g);

    reformUnanchoredRepeatsComponent(g, anchored, unanchored, dead, startBegin,
                                     startEnd);
    deleteVertices(dead, g);
}

static
void collapseVariableRepeats(NGHolder &g, depth *startBegin, depth *startEnd) {
    DEBUG_PRINTF("collapseVariableRepeats\n");
    set<NFAVertex> dead;

    collapseVariableDotRepeat(g, g.start, dead, startBegin, startEnd);
    deleteVertices(dead, g);

    collapseVariableDotRepeat(g, g.startDs, dead, startBegin, startEnd);
    deleteVertices(dead, g);
}

static
void addDotsBetween(NGHolder &g, NFAVertex lhs, vector<NFAVertex> &rhs,
                    depth min_repeat, depth max_repeat) {
    const bool unbounded = max_repeat.is_infinite();
    if (unbounded) {
        max_repeat = min_repeat;
    }

    assert(max_repeat.is_finite());

    NFAVertex u = lhs;

    if (!min_repeat && unbounded) {
        NFAVertex v = add_vertex(g);
        add_edge(u, v, g);
        g[v].char_reach.setall();

        for (auto w : rhs) {
            add_edge(lhs, w, g);
        }
    }

    for (u32 i = 0; i < min_repeat; i++) {
        NFAVertex v = add_vertex(g);
        add_edge(u, v, g);
        g[v].char_reach.setall();
        u = v;
    }

    NFAVertex split = u;
    /* lhs now split point for optional */
    for (u32 i = min_repeat; i < max_repeat; i++) {
        NFAVertex v = add_vertex(g);
        add_edge(u, v, g);
        if (u != split) {
            add_edge(split, v, g);
        }
        g[v].char_reach.setall();
        u = v;
    }

    if (unbounded) {
        add_edge(u, u, g);
    }

    for (auto w : rhs) {
        add_edge(u, w, g);
        if (split != u) {
            add_edge(split, w, g);
        }
    }
}

static
void restoreLeadingDots(NGHolder &g, const depth &startBegin,
                        const depth &startEnd) {
    if (startBegin == depth(0) && startEnd.is_infinite()) {
        return;
    }
    DEBUG_PRINTF("ungobble (%s, %s)\n", startBegin.str().c_str(),
                 startEnd.str().c_str());

    for (UNUSED auto v : adjacent_vertices_range(g.start, g)) {
        assert(edge(g.startDs, v, g).second);
    }
    clear_out_edges(g.start, g);
    add_edge(g.start, g.startDs, g);

    const bool unbounded = startEnd.is_infinite();

    NFAVertex root = unbounded ? g.startDs : g.start;

    vector<NFAVertex> rhs;
    insert(&rhs, rhs.end(), adjacent_vertices(g.startDs, g));
    rhs.erase(remove(rhs.begin(), rhs.end(), g.startDs), rhs.end());
    for (auto v : rhs) {
        remove_edge(g.startDs, v, g);
    }

    addDotsBetween(g, root, rhs, startBegin, startEnd);
    renumber_vertices(g); 
    renumber_edges(g); 
}

// Entry point.
void reformLeadingDots(NGHolder &g) {
    depth startBegin(0);
    depth startEnd = depth::infinity();

    reformAnchoredRepeats(g, &startBegin, &startEnd);
    collapseVariableRepeats(g, &startBegin, &startEnd);
    restoreLeadingDots(g, startBegin, startEnd);
}

} // namespace ue2