]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
fdr: add grey box control for flood detection
authorJustin Viiret <justin.viiret@intel.com>
Fri, 10 Feb 2017 02:04:22 +0000 (13:04 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 04:59:22 +0000 (14:59 +1000)
src/fdr/fdr_compile.cpp
src/fdr/fdr_compile_internal.h
src/fdr/flood_compile.cpp
src/fdr/teddy_compile.cpp
src/fdr/teddy_compile.h
src/grey.cpp
src/grey.h

index f99fcb65ae0beaa35a04efb4ccf9fbc3fe204c46..015fa51e2f147ff03ca0cf1e594477ef388f5a37 100644 (file)
@@ -74,6 +74,7 @@ namespace {
 class FDRCompiler : boost::noncopyable {
 private:
     const FDREngineDescription &eng;
+    const Grey &grey;
     vector<u8> tab;
     vector<hwlmLiteral> lits;
     map<BucketIndex, std::vector<LiteralIndex> > bucketToLits;
@@ -90,9 +91,9 @@ private:
 
 public:
     FDRCompiler(vector<hwlmLiteral> lits_in, const FDREngineDescription &eng_in,
-                bool make_small_in)
-        : eng(eng_in), tab(eng_in.getTabSizeBytes()), lits(move(lits_in)),
-          make_small(make_small_in) {}
+                bool make_small_in, const Grey &grey_in)
+        : eng(eng_in), grey(grey_in), tab(eng_in.getTabSizeBytes()),
+          lits(move(lits_in)), make_small(make_small_in) {}
 
     aligned_unique_ptr<FDR> build();
 };
@@ -146,7 +147,7 @@ void FDRCompiler::createInitialState(FDR *fdr) {
 aligned_unique_ptr<FDR> FDRCompiler::setupFDR() {
     size_t tabSize = eng.getTabSizeBytes();
 
-    auto floodControlTmp = setupFDRFloodControl(lits, eng);
+    auto floodControlTmp = setupFDRFloodControl(lits, eng, grey);
     auto confirmTmp = setupFullConfs(lits, eng, bucketToLits, make_small);
 
     assert(ISALIGNED_16(tabSize));
@@ -543,7 +544,7 @@ aligned_unique_ptr<FDR> fdrBuildTableInternal(const vector<hwlmLiteral> &lits,
     DEBUG_PRINTF("cpu has %s\n", target.has_avx2() ? "avx2" : "no-avx2");
 
     if (grey.fdrAllowTeddy) {
-        auto fdr = teddyBuildTableHinted(lits, make_small, hint, target);
+        auto fdr = teddyBuildTableHinted(lits, make_small, hint, target, grey);
         if (fdr) {
             DEBUG_PRINTF("build with teddy succeeded\n");
             return fdr;
@@ -566,7 +567,7 @@ aligned_unique_ptr<FDR> fdrBuildTableInternal(const vector<hwlmLiteral> &lits,
         des->stride = 1;
     }
 
-    FDRCompiler fc(lits, *des, make_small);
+    FDRCompiler fc(lits, *des, make_small, grey);
     return fc.build();
 }
 
index 0fd599025dc18f019938e64ef9e4922bbfc6e1fe..73de4d4268a2d597039268b794644ec4b05907c5 100644 (file)
@@ -55,6 +55,7 @@ typedef u32 PositionInBucket;  // zero is 'we are matching right now!",
 class EngineDescription;
 class FDREngineDescription;
 struct hwlmStreamingControl;
+struct Grey;
 
 std::pair<aligned_unique_ptr<u8>, size_t> setupFullConfs(
     const std::vector<hwlmLiteral> &lits, const EngineDescription &eng,
@@ -67,7 +68,7 @@ std::pair<aligned_unique_ptr<u8>, size_t> setupFullConfs(
 // right state yet to allow blindly advancing
 std::pair<aligned_unique_ptr<u8>, size_t>
 setupFDRFloodControl(const std::vector<hwlmLiteral> &lits,
-                     const EngineDescription &eng);
+                     const EngineDescription &eng, const Grey &grey);
 
 std::pair<aligned_unique_ptr<u8>, size_t>
 fdrBuildTableStreaming(const std::vector<hwlmLiteral> &lits,
index 62693c300cb6fc2c5b4dda84a383d10c246072d0..b6d23c9d20f74942f8bf3b22543cbe0aaf5e095b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016, Intel Corporation
+ * 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:
@@ -30,6 +30,7 @@
 #include "fdr_confirm.h"
 #include "fdr_compile_internal.h"
 #include "fdr_engine_description.h"
+#include "grey.h"
 #include "ue2common.h"
 #include "util/alloc.h"
 #include "util/bitutils.h"
@@ -92,7 +93,7 @@ void addFlood(vector<FDRFlood> &tmpFlood, u8 c, const hwlmLiteral &lit,
 
 pair<aligned_unique_ptr<u8>, size_t>
 setupFDRFloodControl(const vector<hwlmLiteral> &lits,
-                     const EngineDescription &eng) {
+                     const EngineDescription &eng, const Grey &grey) {
     vector<FDRFlood> tmpFlood(N_CHARS);
     u32 default_suffix = eng.getDefaultFloodSuffixLength();
 
@@ -187,6 +188,14 @@ setupFDRFloodControl(const vector<hwlmLiteral> &lits,
     }
 #endif
 
+    // If flood detection has been switched off in the grey box, we comply by
+    // setting idCount too high for all floods.
+    if (!grey.fdrAllowFlood) {
+        for (auto &fl : tmpFlood) {
+            fl.idCount = FDR_FLOOD_MAX_IDS;
+        }
+    }
+
     map<FDRFlood, CharReach, FloodComparator> flood2chars;
     for (u32 i = 0; i < N_CHARS; i++) {
         FDRFlood fl = tmpFlood[i];
index 66466e6cb4205dac3934b061615c4dd6c959f635..0915528029ececf35400c4a583334a3789400b7b 100644 (file)
@@ -31,6 +31,7 @@
 #include "fdr_compile_internal.h"
 #include "fdr_confirm.h"
 #include "fdr_engine_description.h"
+#include "grey.h"
 #include "ue2common.h"
 #include "util/alloc.h"
 #include "util/compare.h"
@@ -66,13 +67,16 @@ namespace {
 
 class TeddyCompiler : boost::noncopyable {
     const TeddyEngineDescription &eng;
+    const Grey &grey;
     const vector<hwlmLiteral> &lits;
     bool make_small;
 
 public:
     TeddyCompiler(const vector<hwlmLiteral> &lits_in,
-                  const TeddyEngineDescription &eng_in, bool make_small_in)
-        : eng(eng_in), lits(lits_in), make_small(make_small_in) {}
+                  const TeddyEngineDescription &eng_in, bool make_small_in,
+                  const Grey &grey_in)
+        : eng(eng_in), grey(grey_in), lits(lits_in), make_small(make_small_in) {
+    }
 
     aligned_unique_ptr<FDR> build();
     bool pack(map<BucketIndex, std::vector<LiteralIndex> > &bucketToLits);
@@ -307,7 +311,7 @@ aligned_unique_ptr<FDR> TeddyCompiler::build() {
 
     size_t maskLen = eng.numMasks * 16 * 2 * maskWidth;
 
-    auto floodControlTmp = setupFDRFloodControl(lits, eng);
+    auto floodControlTmp = setupFDRFloodControl(lits, eng, grey);
     auto confirmTmp = setupFullConfs(lits, eng, bucketToLits, make_small);
 
     size_t size = ROUNDUP_N(sizeof(Teddy) +
@@ -417,7 +421,8 @@ aligned_unique_ptr<FDR> TeddyCompiler::build() {
 
 aligned_unique_ptr<FDR> teddyBuildTableHinted(const vector<hwlmLiteral> &lits,
                                               bool make_small, u32 hint,
-                                              const target_t &target) {
+                                              const target_t &target,
+                                              const Grey &grey) {
     unique_ptr<TeddyEngineDescription> des;
     if (hint == HINT_INVALID) {
         des = chooseTeddyEngine(target, lits);
@@ -427,7 +432,7 @@ aligned_unique_ptr<FDR> teddyBuildTableHinted(const vector<hwlmLiteral> &lits,
     if (!des) {
         return nullptr;
     }
-    TeddyCompiler tc(lits, *des, make_small);
+    TeddyCompiler tc(lits, *des, make_small, grey);
     return tc.build();
 }
 
index bdd1586575317355580caeab3bfcc1bc7f347c48..07eb18f64061d0e817e2e76e110dd46076515a79 100644 (file)
@@ -43,11 +43,12 @@ struct target_t;
 
 namespace ue2 {
 
+struct Grey;
 struct hwlmLiteral;
 
 ue2::aligned_unique_ptr<FDR>
 teddyBuildTableHinted(const std::vector<hwlmLiteral> &lits, bool make_small,
-                      u32 hint, const target_t &target);
+                      u32 hint, const target_t &target, const Grey &grey);
 
 } // namespace ue2
 
index 8881666e5e61d84b7c9a0ad16f286ccaf71d78ff..cd19e863d7d3ce12dcecad97d88007d4e9613419 100644 (file)
@@ -63,6 +63,7 @@ Grey::Grey(void) :
                    allowDecoratedLiteral(true),
                    allowNoodle(true),
                    fdrAllowTeddy(true),
+                   fdrAllowFlood(true),
                    violetAvoidSuffixes(true),
                    violetAvoidWeakInfixes(true),
                    violetDoubleCut(true),
@@ -226,6 +227,7 @@ void applyGreyOverrides(Grey *g, const string &s) {
         G_UPDATE(allowDecoratedLiteral);
         G_UPDATE(allowNoodle);
         G_UPDATE(fdrAllowTeddy);
+        G_UPDATE(fdrAllowFlood);
         G_UPDATE(violetAvoidSuffixes);
         G_UPDATE(violetAvoidWeakInfixes);
         G_UPDATE(violetDoubleCut);
index 17d8252773eedddc04caaab532d341e5d31d0592..dcbc2e7d6e67dfb8782415ee77db1f745d6174ab 100644 (file)
@@ -64,6 +64,7 @@ struct Grey {
 
     bool allowNoodle;
     bool fdrAllowTeddy;
+    bool fdrAllowFlood;
 
     u32  violetAvoidSuffixes; /* 0=never, 1=sometimes, 2=always */
     bool violetAvoidWeakInfixes;