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
|
/*
* Copyright (c) 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_impl.h"
#include "nfa/castlecompile.h"
#include "nfagraph/ng_repeat.h"
#include "smallwrite/smallwrite_build.h"
#include "util/compile_context.h"
#include "util/boundary_reports.h"
#include "util/make_unique.h"
#include "util/report_manager.h"
using namespace std;
namespace ue2 {
static
bool requiresDedupe(const NGHolder &h, const flat_set<ReportID> &reports,
const Grey &grey) {
/* TODO: tighten */
NFAVertex seen_vert = NGHolder::null_vertex();
for (auto v : inv_adjacent_vertices_range(h.accept, h)) {
if (has_intersection(h[v].reports, reports)) {
if (seen_vert != NGHolder::null_vertex()) {
return true;
}
seen_vert = v;
}
}
for (auto v : inv_adjacent_vertices_range(h.acceptEod, h)) {
if (has_intersection(h[v].reports, reports)) {
if (seen_vert != NGHolder::null_vertex()) {
return true;
}
seen_vert = v;
}
}
if (seen_vert) {
/* if the reporting vertex is part of of a terminal repeat, the
* construction process may reform the graph splitting it into two
* vertices (pos, cyclic) and hence require dedupe */
vector<GraphRepeatInfo> repeats;
findRepeats(h, grey.minExtBoundedRepeatSize, &repeats);
for (const auto &repeat : repeats) {
if (find(repeat.vertices.begin(), repeat.vertices.end(),
seen_vert) != repeat.vertices.end()) {
return true;
}
}
}
return false;
}
class RoseDedupeAuxImpl : public RoseDedupeAux {
public:
explicit RoseDedupeAuxImpl(const RoseBuildImpl &build_in);
bool requiresDedupeSupport(
const flat_set<ReportID> &reports) const override;
private:
bool hasSafeMultiReports(const flat_set<ReportID> &reports) const;
const RoseBuildImpl &build;
map<ReportID, set<RoseVertex>> vert_map; //!< ordinary literals
map<ReportID, set<RoseVertex>> sb_vert_map; //!< small block literals
map<ReportID, set<suffix_id>> suffix_map;
map<ReportID, set<const OutfixInfo *>> outfix_map;
map<ReportID, set<const raw_puff *>> puff_map;
unordered_set<ReportID> live_reports; //!< all live internal reports.
};
unique_ptr<RoseDedupeAux> RoseBuildImpl::generateDedupeAux() const {
return ue2::make_unique<RoseDedupeAuxImpl>(*this);
}
RoseDedupeAux::~RoseDedupeAux() = default;
RoseDedupeAuxImpl::RoseDedupeAuxImpl(const RoseBuildImpl &build_in)
: build(build_in) {
const RoseGraph &g = build.g;
set<suffix_id> suffixes;
for (auto v : vertices_range(g)) {
insert(&live_reports, g[v].reports);
// Literals in the small block table are "shadow" copies of literals in
// the other tables that do not run in the same runtime invocation.
// Dedupe key assignment will be taken care of by the real literals.
if (build.hasLiteralInTable(v, ROSE_ANCHORED_SMALL_BLOCK)) {
for (const auto &report_id : g[v].reports) {
sb_vert_map[report_id].insert(v);
}
} else {
for (const auto &report_id : g[v].reports) {
vert_map[report_id].insert(v);
}
}
// Several vertices may share a suffix, so we collect the set of
// suffixes first to avoid repeating work.
if (g[v].suffix) {
suffixes.insert(g[v].suffix);
}
}
for (const auto &suffix : suffixes) {
for (const auto &report_id : all_reports(suffix)) {
suffix_map[report_id].insert(suffix);
live_reports.insert(report_id);
}
}
for (const auto &outfix : build.outfixes) {
for (const auto &report_id : all_reports(outfix)) {
outfix_map[report_id].insert(&outfix);
live_reports.insert(report_id);
}
}
if (build.mpv_outfix) {
auto *mpv = build.mpv_outfix->mpv();
for (const auto &puff : mpv->puffettes) {
puff_map[puff.report].insert(&puff);
live_reports.insert(puff.report);
}
for (const auto &puff : mpv->triggered_puffettes) {
puff_map[puff.report].insert(&puff);
live_reports.insert(puff.report);
}
}
for (const auto &report_id : build.smwr.all_reports()) {
live_reports.insert(report_id);
}
// Collect live reports from boundary reports.
insert(&live_reports, build.boundary.report_at_0);
insert(&live_reports, build.boundary.report_at_0_eod);
insert(&live_reports, build.boundary.report_at_eod);
DEBUG_PRINTF("%zu of %zu reports are live\n", live_reports.size(),
build.rm.numReports());
}
static
vector<CharReach> makePath(const rose_literal_id &lit) {
vector<CharReach> path(begin(lit.s), end(lit.s));
for (u32 i = 0; i < lit.delay; i++) {
path.push_back(CharReach::dot());
}
return path;
}
/**
* \brief True if one of the given literals overlaps with the suffix of
* another, meaning that they could arrive at the same offset.
*/
static
bool literalsCouldRace(const rose_literal_id &lit1,
const rose_literal_id &lit2) {
DEBUG_PRINTF("compare %s (delay %u) and %s (delay %u)\n",
dumpString(lit1.s).c_str(), lit1.delay,
dumpString(lit2.s).c_str(), lit2.delay);
// Add dots on the end of each literal for delay.
const auto v1 = makePath(lit1);
const auto v2 = makePath(lit2);
// See if the smaller path is a suffix of the larger path.
const auto *smaller = v1.size() < v2.size() ? &v1 : &v2;
const auto *bigger = v1.size() < v2.size() ? &v2 : &v1;
auto r = mismatch(smaller->rbegin(), smaller->rend(), bigger->rbegin(),
overlaps);
return r.first == smaller->rend();
}
bool RoseDedupeAuxImpl::hasSafeMultiReports(
const flat_set<ReportID> &reports) const {
if (reports.size() <= 1) {
return true;
}
/* We have more than one ReportID corresponding to the external ID that is
* presented to the user. These may differ in offset adjustment, bounds
* checks, etc. */
/* TODO: work out if these differences will actually cause problems */
/* One common case where we know we don't have a problem is if there are
* precisely two reports, one for the main Rose path and one for the
* "small block matcher" path. */
if (reports.size() == 2) {
ReportID id1 = *reports.begin();
ReportID id2 = *reports.rbegin();
bool has_verts_1 = contains(vert_map, id1);
bool has_verts_2 = contains(vert_map, id2);
bool has_sb_verts_1 = contains(sb_vert_map, id1);
bool has_sb_verts_2 = contains(sb_vert_map, id2);
if (has_verts_1 != has_verts_2 && has_sb_verts_1 != has_sb_verts_2) {
DEBUG_PRINTF("two reports, one full and one small block: ok\n");
return true;
}
}
DEBUG_PRINTF("more than one report\n");
return false;
}
bool RoseDedupeAuxImpl::requiresDedupeSupport(
const flat_set<ReportID> &reports_in) const {
/* TODO: this could be expanded to check for offset or character
constraints */
// We don't want to consider dead reports (tracked by ReportManager but no
// longer used) for the purposes of assigning dupe keys.
flat_set<ReportID> reports;
for (auto id : reports_in) {
if (contains(live_reports, id)) {
reports.insert(id);
}
}
DEBUG_PRINTF("live reports: %s\n", as_string_list(reports).c_str());
const RoseGraph &g = build.g;
bool has_suffix = false;
bool has_outfix = false;
if (!hasSafeMultiReports(reports)) {
DEBUG_PRINTF("multiple reports not safe\n");
return true;
}
set<RoseVertex> roles;
set<suffix_id> suffixes;
set<const OutfixInfo *> outfixes;
set<const raw_puff *> puffettes;
for (ReportID r : reports) {
if (contains(vert_map, r)) {
insert(&roles, vert_map.at(r));
}
if (contains(suffix_map, r)) {
insert(&suffixes, suffix_map.at(r));
}
if (contains(outfix_map, r)) {
insert(&outfixes, outfix_map.at(r));
}
if (contains(puff_map, r)) {
insert(&puffettes, puff_map.at(r));
}
}
/* roles */
map<u32, u32> lits; // Literal ID -> count of occurrences.
const bool has_role = !roles.empty();
for (auto v : roles) {
for (const auto &lit : g[v].literals) {
lits[lit]++;
}
if (g[v].eod_accept) {
// Literals plugged into this EOD accept must be taken into account
// as well.
for (auto u : inv_adjacent_vertices_range(v, g)) {
for (const auto &lit : g[u].literals) {
lits[lit]++;
}
}
}
}
/* literals */
for (const auto &m : lits) {
if (m.second > 1) {
DEBUG_PRINTF("lit %u used by >1 reporting roles\n", m.first);
return true;
}
}
for (auto it = begin(lits); it != end(lits); ++it) {
const auto &lit1 = build.literals.at(it->first);
for (auto jt = next(it); jt != end(lits); ++jt) {
const auto &lit2 = build.literals.at(jt->first);
if (literalsCouldRace(lit1, lit2)) {
DEBUG_PRINTF("literals could race\n");
return true;
}
}
}
/* suffixes */
for (const auto &suffix : suffixes) {
if (has_suffix || has_role) {
return true; /* scope for badness */
}
has_suffix = true;
/* some lesser suffix engines (nfas, haig, castle) can raise multiple
* matches for a report id at the same offset if there are multiple
* report states live. */
if (suffix.haig()) {
return true;
}
if (suffix.graph() &&
requiresDedupe(*suffix.graph(), reports, build.cc.grey)) {
return true;
}
if (suffix.castle() && requiresDedupe(*suffix.castle(), reports)) {
return true;
}
}
/* outfixes */
for (const auto &outfix_ptr : outfixes) {
assert(outfix_ptr);
const OutfixInfo &out = *outfix_ptr;
if (has_outfix || has_role || has_suffix) {
return true;
}
has_outfix = true;
if (out.haig()) {
return true; /* haig may report matches with different SOM at the
same offset */
}
if (out.holder() &&
requiresDedupe(*out.holder(), reports, build.cc.grey)) {
return true;
}
}
/* mpv */
for (UNUSED const auto &puff : puffettes) {
if (has_outfix || has_role || has_suffix) {
return true;
}
has_outfix = true;
}
/* boundary */
if (has_intersection(build.boundary.report_at_eod, reports)) {
if (has_outfix || has_role || has_suffix) {
return true;
}
}
return false;
}
} // namespace ue2
|