ng_fuzzy: apply a resource limit to vertex count

This commit is contained in:
Justin Viiret 2017-03-08 11:36:24 +11:00 committed by Matthew Barr
parent ffab97ca8c
commit 533fcf383d
3 changed files with 18 additions and 3 deletions

View File

@ -156,7 +156,8 @@ Grey::Grey(void) :
limitEngineSize(1073741824), // 1 GB
limitDFASize(1073741824), // 1 GB
limitNFASize(1048576), // 1 MB
limitLBRSize(1048576) // 1 MB
limitLBRSize(1048576), // 1 MB
limitApproxMatchingVertices(5000)
{
assert(maxAnchoredRegion < 64); /* a[lm]_log_sum have limited capacity */
}
@ -317,6 +318,7 @@ void applyGreyOverrides(Grey *g, const string &s) {
G_UPDATE(limitDFASize);
G_UPDATE(limitNFASize);
G_UPDATE(limitLBRSize);
G_UPDATE(limitApproxMatchingVertices);
#undef G_UPDATE
if (key == "simple_som") {

View File

@ -201,6 +201,9 @@ struct Grey {
u32 limitDFASize; //!< max size of a DFA (in bytes)
u32 limitNFASize; //!< max size of an NFA (in bytes)
u32 limitLBRSize; //!< max size of an LBR engine (in bytes)
// Approximate matching limits.
u32 limitApproxMatchingVertices; //!< max number of vertices per graph
};
#ifndef RELEASE_BUILD

View File

@ -665,13 +665,23 @@ void validate_fuzzy_compile(const NGHolder &g, u32 edit_distance, bool utf8,
}
}
void make_fuzzy(NGHolder &g, u32 edit_distance, UNUSED const Grey &grey) {
void make_fuzzy(NGHolder &g, u32 edit_distance, const Grey &grey) {
if (edit_distance == 0) {
return;
}
assert(grey.allowApproximateMatching);
assert(grey.maxEditDistance >= edit_distance);
ShadowGraph sg(g, edit_distance);
sg.fuzz_graph();
// For safety, enforce limit on actual vertex count.
if (num_vertices(g) > grey.limitApproxMatchingVertices) {
DEBUG_PRINTF("built %zu vertices > limit of %u\n", num_vertices(g),
grey.limitApproxMatchingVertices);
throw ResourceLimitError();
}
}
}
} // namespace ue2