aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/rose/rose_build_castle.cpp
blob: 59bab3b1f98c4585571d3b55d3730e1950161f53 (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
/*
 * 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.
 */

#include "rose_build_castle.h"

#include "rose_build_impl.h"
#include "ue2common.h"
#include "nfa/castlecompile.h"
#include "nfagraph/ng_holder.h"
#include "nfagraph/ng_puff.h"
#include "util/charreach.h"
#include "util/compile_context.h"
#include "util/container.h"
#include "util/dump_charclass.h"
#include "util/graph_range.h"
#include "util/ue2string.h"

#include <map>
#include <set>
#include <string>
#include <vector>

#include <boost/range/adaptor/map.hpp>

using namespace std;
using boost::adaptors::map_values;

namespace ue2 {

static
void makeCastle(LeftEngInfo &left,
            unordered_map<const NGHolder *, shared_ptr<CastleProto>> &cache) {
    if (left.dfa || left.haig || left.castle) {
        return;
    }
    if (!left.graph) {
        return;
    }

    const NGHolder &h = *left.graph;
    DEBUG_PRINTF("prefix %p\n", &h);

    if (contains(cache, &h)) {
        DEBUG_PRINTF("using cached CastleProto\n");
        left.castle = cache[&h];
        left.graph.reset();
        return;
    }

    PureRepeat pr;
    if (isPureRepeat(h, pr) && pr.reports.size() == 1) {
        DEBUG_PRINTF("vertex preceded by infix repeat %s\n",
                     pr.bounds.str().c_str());
        left.castle = make_shared<CastleProto>(h.kind, pr);
        cache[&h] = left.castle;
        left.graph.reset();
    }
}

static
void makeCastleSuffix(RoseBuildImpl &tbi, RoseVertex v,
            unordered_map<const NGHolder *, shared_ptr<CastleProto>> &cache) {
    RoseSuffixInfo &suffix = tbi.g[v].suffix;
    if (!suffix.graph) {
        return;
    }
    const NGHolder &h = *suffix.graph;
    DEBUG_PRINTF("suffix %p\n", &h);

    if (contains(cache, &h)) {
        DEBUG_PRINTF("using cached CastleProto\n");
        suffix.castle = cache[&h];
        suffix.graph.reset();
        return;
    }

    // The MPV will probably do a better job on the cases it's designed
    // for.
    const bool fixed_depth = tbi.g[v].min_offset == tbi.g[v].max_offset;
    if (isPuffable(h, fixed_depth, tbi.rm, tbi.cc.grey)) {
        DEBUG_PRINTF("leaving suffix for puff\n");
        return;
    }

    PureRepeat pr;
    if (isPureRepeat(h, pr) && pr.reports.size() == 1) {
        DEBUG_PRINTF("suffix repeat %s\n", pr.bounds.str().c_str());

        // Right now, the Castle uses much more stream state to represent a
        // {m,1} repeat than just leaving it to an NFA.
        if (pr.bounds.max <= depth(1)) {
            DEBUG_PRINTF("leaving for other engines\n");
            return;
        }

        suffix.castle = make_shared<CastleProto>(h.kind, pr);
        cache[&h] = suffix.castle;
        suffix.graph.reset();
    }
}

static
vector<rose_literal_id> literals_for_vertex(const RoseBuildImpl &tbi,
                                            RoseVertex v) {
    vector<rose_literal_id> rv;

    for (const u32 id : tbi.g[v].literals) {
        rv.push_back(tbi.literals.at(id));
    }

    return rv;
}

static
void renovateCastle(RoseBuildImpl &tbi, CastleProto *castle,
              const vector<RoseVertex> &verts) {
    DEBUG_PRINTF("looking to renovate\n");

    if (castle->repeats.size() != 1) {
        assert(0);  /* should not have merged castles yet */
        return;
    }

    PureRepeat &pr = castle->repeats.begin()->second;
    if (pr.bounds.max.is_finite()) {
        /* repeat cannot be turned into pseudo .* */
        return;
    }

    RoseGraph &g = tbi.g;
    const CharReach &cr = castle->reach();

    DEBUG_PRINTF("cr || %zu\n", cr.count());

    u32 allowed_to_remove = ~0;
    size_t min_succ_lit_len = 0;

    for (RoseVertex v : verts) {
        assert(g[v].left.castle.get() == castle);
        DEBUG_PRINTF("%zu checks at lag %u\n", g[v].index, g[v].left.lag);
        vector<rose_literal_id> lits = literals_for_vertex(tbi, v);
        for (const auto &e : lits) {
            DEBUG_PRINTF("%s +%u\n", dumpString(e.s).c_str(), e.delay);
            if (e.delay) {
                return; /* bail - TODO: be less lazy */
            }

            vector<CharReach> rem_local_cr;
            u32 ok_count = 0;
            for (auto it = e.s.end() - g[v].left.lag; it != e.s.end(); ++it) {
                if (!isSubsetOf(*it, cr)) {
                    break;
                }

                ok_count++;
            }
            LIMIT_TO_AT_MOST(&allowed_to_remove, ok_count);
            ENSURE_AT_LEAST(&min_succ_lit_len, e.elength());
        }
    }

    DEBUG_PRINTF("possible to decrease lag by %u\n", allowed_to_remove);


    for (RoseVertex v : verts) {
        assert(g[v].left.lag >= allowed_to_remove);
        g[v].left.lag -= allowed_to_remove;
    }

    assert(castle->repeats.size() == 1); /* should not have merged castles yet */

    pr.bounds.max += allowed_to_remove;

    /* Although it is always safe to increase the min bound as well, we would
     * rather not as a >0 min bound means that we have to store state as well.
     *
     * As it was legal to run with the original lag, we know that it is not
     * possible to have an overlapping match which finishes within the trigger
     * literal past the original lag point. However, if there is already a min
     * bound constraint this would be broken if we did not also increase the
     * min bound. */

    if (pr.bounds.min > 0ULL || allowed_to_remove > min_succ_lit_len) {
        pr.bounds.min += allowed_to_remove;
    }
}

void makeCastles(RoseBuildImpl &tbi) {
    if (!tbi.cc.grey.allowCastle && !tbi.cc.grey.allowLbr) {
        return;
    }

    RoseGraph &g = tbi.g;

    // Caches so that we can reuse analysis on graphs we've seen already.
    unordered_map<const NGHolder *, shared_ptr<CastleProto> > left_cache;
    unordered_map<const NGHolder *, shared_ptr<CastleProto> > suffix_cache;

    unordered_map<CastleProto *, vector<RoseVertex>> rev;

    for (RoseVertex v : vertices_range(g)) {
        if (g[v].left && !tbi.isRootSuccessor(v)) {
            makeCastle(g[v].left, left_cache);
            if (g[v].left.castle) {
                rev[g[v].left.castle.get()].push_back(v);
            }
        }

        if (g[v].suffix) {
            makeCastleSuffix(tbi, v, suffix_cache);
        }
    }

    for (const auto &e : rev) {
        renovateCastle(tbi, e.first, e.second);
    }
}

bool unmakeCastles(RoseBuildImpl &tbi) {
    RoseGraph &g = tbi.g;

    const size_t MAX_UNMAKE_VERTICES = 64;

    map<left_id, vector<RoseVertex> > left_castles;
    map<suffix_id, vector<RoseVertex> > suffix_castles;
    bool changed = false;

    for (auto v : vertices_range(g)) {
        const LeftEngInfo &left = g[v].left;
        if (left.castle && left.castle->repeats.size() > 1) {
            left_castles[left].push_back(v);
        }
        const RoseSuffixInfo &suffix = g[v].suffix;
        if (suffix.castle && suffix.castle->repeats.size() > 1) {
            suffix_castles[suffix].push_back(v);
        }
    }

    for (const auto &e : left_castles) {
        assert(e.first.castle());
        shared_ptr<NGHolder> h = makeHolder(*e.first.castle(), tbi.cc);
        if (!h || num_vertices(*h) > MAX_UNMAKE_VERTICES) {
            continue;
        }
        DEBUG_PRINTF("replace rose with holder (%zu vertices)\n",
                     num_vertices(*h));
        for (auto v : e.second) {
            assert(g[v].left.castle.get() == e.first.castle());
            g[v].left.graph = h;
            g[v].left.castle.reset();
            changed = true;
        }
    }

    for (const auto &e : suffix_castles) {
        assert(e.first.castle());
        shared_ptr<NGHolder> h = makeHolder(*e.first.castle(), tbi.cc);
        if (!h || num_vertices(*h) > MAX_UNMAKE_VERTICES) {
            continue;
        }
        DEBUG_PRINTF("replace suffix with holder (%zu vertices)\n",
                     num_vertices(*h));
        for (auto v : e.second) {
            assert(g[v].suffix.castle.get() == e.first.castle());
            g[v].suffix.graph = h;
            g[v].suffix.castle.reset();
            changed = true;
        }
    }

    return changed;
}

void remapCastleTops(RoseBuildImpl &tbi) {
    unordered_map<CastleProto *, vector<RoseVertex>> rose_castles;
    unordered_map<CastleProto *, vector<RoseVertex>> suffix_castles;

    RoseGraph &g = tbi.g;
    for (auto v : vertices_range(g)) {
        if (g[v].left.castle) {
            rose_castles[g[v].left.castle.get()].push_back(v);
        }
        if (g[v].suffix.castle) {
            suffix_castles[g[v].suffix.castle.get()].push_back(v);
        }
    }

    DEBUG_PRINTF("%zu rose castles, %zu suffix castles\n", rose_castles.size(),
                  suffix_castles.size());

    map<u32, u32> top_map;

    // Remap Rose Castles.
    for (const auto &rc : rose_castles) {
        CastleProto *c = rc.first;
        const vector<RoseVertex> &verts = rc.second;

        DEBUG_PRINTF("rose castle %p (%zu repeats) has %zu verts\n", c,
                      c->repeats.size(), verts.size());

        top_map.clear();
        remapCastleTops(*c, top_map);

        // Update the tops on the edges leading into vertices in v.
        for (auto v : verts) {
            for (const auto &e : in_edges_range(v, g)) {
                g[e].rose_top = top_map.at(g[e].rose_top);
            }
        }
    }

    // Remap Suffix Castles.
    for (const auto &e : suffix_castles) {
        CastleProto *c = e.first;
        const vector<RoseVertex> &verts = e.second;

        DEBUG_PRINTF("suffix castle %p (%zu repeats) has %zu verts\n", c,
                      c->repeats.size(), verts.size());

        top_map.clear();
        remapCastleTops(*c, top_map);

        // Update the tops on the suffixes.
        for (auto v : verts) {
            assert(g[v].suffix);
            g[v].suffix.top = top_map.at(g[v].suffix.top);
        }
    }
}

bool triggerKillsRoseCastle(const RoseBuildImpl &tbi, const left_id &left,
                            const set<ue2_literal> &all_lits,
                            const RoseEdge &e) {
    assert(left.castle());
    const CastleProto &c = *left.castle();

    const depth max_width = findMaxWidth(c);
    DEBUG_PRINTF("castle max width is %s\n", max_width.str().c_str());

    /* check each pred literal to see if they all kill previous castle
     * state */
    for (u32 lit_id : tbi.g[source(e, tbi.g)].literals) {
        const rose_literal_id &pred_lit = tbi.literals.at(lit_id);
        const ue2_literal s = findNonOverlappingTail(all_lits, pred_lit.s);
        const CharReach &cr = c.reach();

        DEBUG_PRINTF("s=%s, castle reach=%s\n", dumpString(s).c_str(),
                      describeClass(cr).c_str());

        for (const auto &s_cr : s) {
            if (!overlaps(cr, s_cr)) {
                DEBUG_PRINTF("reach %s kills castle\n",
                             describeClass(s_cr).c_str());
                goto next_pred;
            }
        }

        if (max_width < depth(s.length())) {
            DEBUG_PRINTF("literal width >= castle max width\n");
            goto next_pred;
        }

        return false;

    next_pred:;
    }

    return true;
}

} // namespace ue2