]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Make Rose callback types explicitly take scratch
authorJustin Viiret <justin.viiret@intel.com>
Wed, 13 Jan 2016 02:20:38 +0000 (13:20 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Tue, 1 Mar 2016 00:29:04 +0000 (11:29 +1100)
src/rose/eod.c
src/rose/rose_types.h
src/runtime.c

index 2e414ad7eb6b919a85210ac2e3f59b941fdf0a66..7bbf8faf123a8c6b38d7773239145e49e43531c3 100644 (file)
@@ -120,6 +120,29 @@ int roseEodRunIterator(const struct RoseEngine *t, u64a offset,
     return MO_CONTINUE_MATCHING;
 }
 
+/**
+ * \brief Adapts an NfaCallback to the rose callback specified in the
+ * RoseContext.
+ */
+static
+int eodNfaCallback(u64a offset, ReportID report, void *context) {
+    struct hs_scratch *scratch = context;
+    assert(scratch->magic == SCRATCH_MAGIC);
+    return scratch->tctxt.cb(offset, report, scratch);
+}
+
+/**
+ * \brief Adapts a SomNfaCallback to the rose SOM callback specified in the
+ * RoseContext.
+ */
+static
+int eodNfaSomCallback(u64a from_offset, u64a to_offset, ReportID report,
+                      void *context) {
+    struct hs_scratch *scratch = context;
+    assert(scratch->magic == SCRATCH_MAGIC);
+    return scratch->tctxt.cb_som(from_offset, to_offset, report, scratch);
+}
+
 /**
  * \brief Check for (and deliver) reports from active output-exposed (suffix
  * or outfix) NFAs.
@@ -167,8 +190,8 @@ void roseCheckNfaEod(const struct RoseEngine *t, char *state,
             nfaExpandState(nfa, fstate, sstate, offset, key);
         }
 
-        if (nfaCheckFinalState(nfa, fstate, sstate, offset, scratch->tctxt.cb,
-                               scratch->tctxt.cb_som,
+        if (nfaCheckFinalState(nfa, fstate, sstate, offset, eodNfaCallback,
+                               eodNfaSomCallback,
                                scratch) == MO_HALT_MATCHING) {
             DEBUG_PRINTF("user instructed us to stop\n");
             return;
@@ -216,8 +239,8 @@ void roseCheckEodSuffixes(const struct RoseEngine *t, char *state, u64a offset,
          * history buffer. */
         char rv = nfaQueueExecRose(q->nfa, q, MO_INVALID_IDX);
         if (rv) { /* nfa is still alive */
-            if (nfaCheckFinalState(nfa, fstate, sstate, offset,
-                                   scratch->tctxt.cb, scratch->tctxt.cb_som,
+            if (nfaCheckFinalState(nfa, fstate, sstate, offset, eodNfaCallback,
+                                   eodNfaSomCallback,
                                    scratch) == MO_HALT_MATCHING) {
                 DEBUG_PRINTF("user instructed us to stop\n");
                 return;
index 1ba453fe2312f104fa2e5d51dcafc534f8d4ed07..b70328d258543d9302b9f6f5f70d361f4c4bdf90 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
 
 #include "ue2common.h"
 
-struct RoseEngine;
+struct hs_scratch;
 
-// Note: identical signature to NfaCallback, but all Rose callbacks must be
-// passed scratch as their context ptr.
+typedef int (*RoseCallback)(u64a offset, ReportID id,
+                            struct hs_scratch *scratch);
 
-typedef int (*RoseCallback)(u64a offset, ReportID id, void *context);
 typedef int (*RoseCallbackSom)(u64a from_offset, u64a to_offset, ReportID id,
-                               void *context);
+                               struct hs_scratch *scratch);
 
 #endif
index 5276acf6e0a14abdd62d7be15e4842a78edd79c7..d51db18b3d1b1032787cdcacbb590aa538605b33 100644 (file)
@@ -171,11 +171,12 @@ void setBroken(char *state, u8 broken) {
 }
 
 static really_inline
-int roseAdaptor_i(u64a offset, ReportID id, void *context, char is_simple,
-                  char do_som) {
+int roseAdaptor_i(u64a offset, ReportID id, struct hs_scratch *scratch,
+                  char is_simple, char do_som) {
     assert(id != MO_INVALID_IDX); // Should never get an invalid ID.
+    assert(scratch);
+    assert(scratch->magic == SCRATCH_MAGIC);
 
-    struct hs_scratch *scratch = (struct hs_scratch *)context;
     struct core_info *ci = &scratch->core_info;
     const struct RoseEngine *rose = ci->rose;
     DEBUG_PRINTF("internal report %u\n", id);
@@ -326,12 +327,13 @@ exit:
 
 static really_inline
 int roseSomAdaptor_i(u64a from_offset, u64a to_offset, ReportID id,
-                     void *context, char is_simple) {
+                     struct hs_scratch *scratch, char is_simple) {
     assert(id != MO_INVALID_IDX); // Should never get an invalid ID.
+    assert(scratch);
+    assert(scratch->magic == SCRATCH_MAGIC);
 
     u32 flags = 0;
 
-    struct hs_scratch *scratch = (struct hs_scratch *)context;
     struct core_info *ci = &scratch->core_info;
     const struct RoseEngine *rose = ci->rose;
     const struct internal_report *ri = getInternalReport(rose, id);
@@ -488,8 +490,8 @@ hwlmcb_rv_t multiDirectAdaptor(u64a real_end, ReportID direct_id, void *context,
 }
 
 static
-int roseAdaptor(u64a offset, ReportID id, void *context) {
-    return roseAdaptor_i(offset, id, context, 0, 0);
+int roseAdaptor(u64a offset, ReportID id, struct hs_scratch *scratch) {
+    return roseAdaptor_i(offset, id, scratch, 0, 0);
 }
 
 static
@@ -513,8 +515,8 @@ hwlmcb_rv_t hwlmAdaptor(UNUSED size_t start, size_t end, u32 direct_id,
 }
 
 static
-int roseSimpleAdaptor(u64a offset, ReportID id, void *context) {
-    return roseAdaptor_i(offset, id, context, 1, 0);
+int roseSimpleAdaptor(u64a offset, ReportID id, struct hs_scratch *scratch) {
+    return roseAdaptor_i(offset, id, scratch, 1, 0);
 }
 
 static
@@ -539,8 +541,8 @@ hwlmcb_rv_t hwlmSimpleAdaptor(UNUSED size_t start, size_t end, u32 direct_id,
 }
 
 static
-int roseSomAdaptor(u64a offset, ReportID id, void *context) {
-    return roseAdaptor_i(offset, id, context, 0, 1);
+int roseSomAdaptor(u64a offset, ReportID id, struct hs_scratch *scratch) {
+    return roseAdaptor_i(offset, id, scratch, 0, 1);
 }
 
 static
@@ -564,8 +566,8 @@ hwlmcb_rv_t hwlmSomAdaptor(UNUSED size_t start, size_t end, u32 direct_id,
 }
 
 static
-int roseSimpleSomAdaptor(u64a offset, ReportID id, void *context) {
-    return roseAdaptor_i(offset, id, context, 1, 1);
+int roseSimpleSomAdaptor(u64a offset, ReportID id, struct hs_scratch *scratch) {
+    return roseAdaptor_i(offset, id, scratch, 1, 1);
 }
 
 static
@@ -614,14 +616,14 @@ HWLMCallback selectHwlmAdaptor(const struct RoseEngine *rose) {
 
 static
 int roseSomSomAdaptor(u64a from_offset, u64a to_offset, ReportID id,
-                       void *context) {
-    return roseSomAdaptor_i(from_offset, to_offset, id, context, 0);
+                      struct hs_scratch *scratch) {
+    return roseSomAdaptor_i(from_offset, to_offset, id, scratch, 0);
 }
 
 static
 int roseSimpleSomSomAdaptor(u64a from_offset, u64a to_offset, ReportID id,
-                             void *context) {
-    return roseSomAdaptor_i(from_offset, to_offset, id, context, 1);
+                            struct hs_scratch *scratch) {
+    return roseSomAdaptor_i(from_offset, to_offset, id, scratch, 1);
 }
 
 static really_inline
@@ -631,6 +633,56 @@ RoseCallbackSom selectSomAdaptor(const struct RoseEngine *rose) {
     return is_simple ? roseSimpleSomSomAdaptor : roseSomSomAdaptor;
 }
 
+static
+int outfixSimpleSomAdaptor(u64a offset, ReportID id, void *context) {
+    return roseAdaptor_i(offset, id, context, 1, 1);
+}
+
+static
+int outfixSimpleAdaptor(u64a offset, ReportID id, void *context) {
+    return roseAdaptor_i(offset, id, context, 1, 0);
+}
+
+static
+int outfixSomAdaptor(u64a offset, ReportID id, void *context) {
+    return roseAdaptor_i(offset, id, context, 0, 1);
+}
+
+static
+int outfixAdaptor(u64a offset, ReportID id, void *context) {
+    return roseAdaptor_i(offset, id, context, 0, 0);
+}
+
+static really_inline
+NfaCallback selectOutfixAdaptor(const struct RoseEngine *rose) {
+    const char is_simple = rose->simpleCallback;
+    const char do_som = rose->hasSom;
+
+    if (do_som) {
+        return is_simple ? outfixSimpleSomAdaptor : outfixSomAdaptor;
+    } else {
+        return is_simple ? outfixSimpleAdaptor : outfixAdaptor;
+    }
+}
+
+static
+int outfixSimpleSomSomAdaptor(u64a from_offset, u64a to_offset, ReportID id,
+                              void *context) {
+    return roseSomAdaptor_i(from_offset, to_offset, id, context, 1);
+}
+
+static
+int outfixSomSomAdaptor(u64a from_offset, u64a to_offset, ReportID id,
+                        void *context) {
+    return roseSomAdaptor_i(from_offset, to_offset, id, context, 0);
+}
+
+static really_inline
+SomNfaCallback selectOutfixSomAdaptor(const struct RoseEngine *rose) {
+    const char is_simple = rose->simpleCallback;
+    return is_simple ? outfixSimpleSomSomAdaptor : outfixSomSomAdaptor;
+}
+
 static never_inline
 void processReportList(const struct RoseEngine *rose, u32 base_offset,
                        u64a stream_offset, hs_scratch_t *scratch) {
@@ -720,8 +772,8 @@ void initQueue(struct mq *q, u32 qi, const struct RoseEngine *t,
     q->length = scratch->core_info.len;
     q->history = scratch->core_info.hbuf;
     q->hlength = scratch->core_info.hlen;
-    q->cb = selectAdaptor(t);
-    q->som_cb = selectSomAdaptor(t);
+    q->cb = selectOutfixAdaptor(t);
+    q->som_cb = selectOutfixSomAdaptor(t);
     q->context = scratch;
     q->report_current = 0;
 
@@ -792,10 +844,10 @@ void runSmallWriteEngine(const struct SmallWriteEngine *smwr,
     assert(isMcClellanType(nfa->type));
     if (nfa->type == MCCLELLAN_NFA_8) {
         nfaExecMcClellan8_B(nfa, smwr->start_offset, local_buffer,
-                            local_alen, selectAdaptor(rose), scratch);
+                            local_alen, selectOutfixAdaptor(rose), scratch);
     } else {
         nfaExecMcClellan16_B(nfa, smwr->start_offset, local_buffer,
-                             local_alen, selectAdaptor(rose), scratch);
+                             local_alen, selectOutfixAdaptor(rose), scratch);
     }
 }