]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Use hyperscan check function
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 21 Apr 2017 10:41:49 +0000 (11:41 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 21 Apr 2017 10:41:49 +0000 (11:41 +0100)
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/libserver/url.c
src/libutil/multipattern.c
src/libutil/multipattern.h

index 1e13f6a567fd0d412574cf252fd3d33bbef95bb6..70cb362407e80d09dfce7189848cf05f5af1517c 100644 (file)
@@ -280,8 +280,7 @@ rspamd_rcl_options_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                        section, cfg->cfg_pool, obj,
                        cfg, err)) {
                /* We need to init this early */
-               rspamd_multipattern_library_init (cfg->hs_cache_dir,
-                               cfg->libs_ctx->crypto_ctx);
+               rspamd_multipattern_library_init (cfg->hs_cache_dir);
 
                return TRUE;
        }
index 23d38bc0cadd32c173a624cd948b6f1009fc5aca..c97db53fb522dc2f9fd78e8a51c2186aa417c199 100644 (file)
@@ -669,8 +669,7 @@ rspamd_config_post_load (struct rspamd_config *cfg,
        }
 
        rspamd_regexp_library_init ();
-       rspamd_multipattern_library_init (cfg->hs_cache_dir,
-                       cfg->libs_ctx->crypto_ctx);
+       rspamd_multipattern_library_init (cfg->hs_cache_dir);
 
 #ifdef WITH_HYPERSCAN
        if (!cfg->disable_hyperscan) {
index 1efae6019df185beb9b0182f25b33dc2da95a118..9f377edb98151884834907ff5ac84458c1fca16c 100644 (file)
@@ -460,7 +460,8 @@ rspamd_url_add_static_matchers (struct url_match_scanner *sc)
                if (static_matchers[i].flags & URL_FLAG_REGEXP) {
                        rspamd_multipattern_add_pattern (url_scanner->search_trie,
                                        static_matchers[i].pattern,
-                                       RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8|RSPAMD_MULTIPATTERN_RE);
+                                       RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8|
+                                                       RSPAMD_MULTIPATTERN_RE);
                }
                else {
                        rspamd_multipattern_add_pattern (url_scanner->search_trie,
index 13a167cd17a3729626d31d6fb5c1bbdf1cdb7422..35ef51a7088f9daa94ad5f8d521fc9467746b6c7 100644 (file)
 
 #define MAX_SCRATCH 4
 
+enum rspamd_hs_check_state {
+       RSPAMD_HS_UNCHECKED = 0,
+       RSPAMD_HS_SUPPORTED,
+       RSPAMD_HS_UNSUPPORTED
+};
+
 static const char *hs_cache_dir = NULL;
-#ifdef WITH_HYPERSCAN
-static gboolean hs_suitable_cpu = FALSE;
-#endif
+static enum rspamd_hs_check_state hs_suitable_cpu = RSPAMD_HS_UNCHECKED;
+
 
 struct rspamd_multipattern {
 #ifdef WITH_HYPERSCAN
@@ -57,15 +62,29 @@ rspamd_multipattern_quark (void)
        return g_quark_from_static_string ("multipattern");
 }
 
+static inline gboolean
+rspamd_hs_check (void)
+{
+#ifdef WITH_HYPERSCAN
+       if (G_UNLIKELY (hs_suitable_cpu == RSPAMD_HS_UNCHECKED)) {
+               if (hs_valid_platform () == HS_SUCCESS) {
+                       hs_suitable_cpu = RSPAMD_HS_SUPPORTED;
+               }
+               else {
+                       hs_suitable_cpu = RSPAMD_HS_UNSUPPORTED;
+               }
+       }
+#endif
+
+       return hs_suitable_cpu == RSPAMD_HS_SUPPORTED;
+}
+
 void
-rspamd_multipattern_library_init (const gchar *cache_dir,
-               struct rspamd_cryptobox_library_ctx *crypto_ctx)
+rspamd_multipattern_library_init (const gchar *cache_dir)
 {
        hs_cache_dir = cache_dir;
 #ifdef WITH_HYPERSCAN
-       if (crypto_ctx->cpu_config & CPUID_SSSE3) {
-               hs_suitable_cpu = TRUE;
-       }
+       rspamd_hs_check ();
 #endif
 }
 
@@ -284,7 +303,7 @@ rspamd_multipattern_pattern_filter (const gchar *pattern, gsize len,
 {
        gchar *ret = NULL;
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                if (flags & RSPAMD_MULTIPATTERN_TLD) {
                        ret = rspamd_multipattern_escape_tld_hyperscan (pattern, len, dst_len);
                }
@@ -323,7 +342,7 @@ rspamd_multipattern_create (enum rspamd_multipattern_flags flags)
        mp->flags = flags;
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                mp->hs_pats = g_array_new (FALSE, TRUE, sizeof (gchar *));
                mp->hs_flags = g_array_new (FALSE, TRUE, sizeof (gint));
                mp->hs_ids = g_array_new (FALSE, TRUE, sizeof (gint));
@@ -348,7 +367,7 @@ rspamd_multipattern_create_sized (guint npatterns,
        mp->flags = flags;
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                mp->hs_pats = g_array_sized_new (FALSE, TRUE, sizeof (gchar *), npatterns);
                mp->hs_flags = g_array_sized_new (FALSE, TRUE, sizeof (gint), npatterns);
                mp->hs_ids = g_array_sized_new (FALSE, TRUE, sizeof (gint), npatterns);
@@ -383,7 +402,7 @@ rspamd_multipattern_add_pattern_len (struct rspamd_multipattern *mp,
        g_assert (!mp->compiled);
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                gchar *np;
                gint fl = HS_FLAG_SOM_LEFTMOST;
 
@@ -522,7 +541,7 @@ rspamd_multipattern_compile (struct rspamd_multipattern *mp, GError **err)
        g_assert (!mp->compiled);
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                guint i;
                hs_platform_info_t plt;
                hs_compile_error_t *hs_errors;
@@ -652,7 +671,7 @@ rspamd_multipattern_lookup (struct rspamd_multipattern *mp,
        cbd.ret = 0;
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                hs_scratch_t *scr = NULL;
                guint i;
 
@@ -706,7 +725,7 @@ rspamd_multipattern_destroy (struct rspamd_multipattern *mp)
 
        if (mp) {
 #ifdef WITH_HYPERSCAN
-               if (hs_suitable_cpu) {
+               if (rspamd_hs_check ()) {
                        gchar *p;
 
                        if (mp->compiled && mp->cnt > 0) {
@@ -755,7 +774,7 @@ rspamd_multipattern_get_pattern (struct rspamd_multipattern *mp,
        g_assert (index < mp->cnt);
 
 #ifdef WITH_HYPERSCAN
-       if (hs_suitable_cpu) {
+       if (rspamd_hs_check ()) {
                return g_array_index (mp->hs_pats, gchar *, index);
        }
 #endif
@@ -774,3 +793,9 @@ rspamd_multipattern_get_npatterns (struct rspamd_multipattern *mp)
 
        return mp->cnt;
 }
+
+gboolean
+rspamd_multipattern_has_hyperscan (void)
+{
+       return rspamd_hs_check ();
+}
\ No newline at end of file
index c2f30f32171e47eecc0af8a26ca7e07590779e6b..6a7dd4a8885fa27a5397afaef6be1fc0a07ee4ba 100644 (file)
@@ -61,8 +61,7 @@ typedef gint (*rspamd_multipattern_cb_t) (struct rspamd_multipattern *mp,
  * Init multipart library and set the appropriate cache dir
  * @param cache_dir
  */
-void rspamd_multipattern_library_init (const gchar *cache_dir,
-               struct rspamd_cryptobox_library_ctx *crypto_ctx);
+void rspamd_multipattern_library_init (const gchar *cache_dir);
 
 /**
  * Creates empty multipattern structure
@@ -78,8 +77,8 @@ struct rspamd_multipattern *rspamd_multipattern_create (
  * @param reserved
  * @return
  */
-struct rspamd_multipattern *rspamd_multipattern_create_sized (
-               enum rspamd_multipattern_flags flags, guint reserved);
+struct rspamd_multipattern *rspamd_multipattern_create_sized (guint reserved,
+               enum rspamd_multipattern_flags flags);
 
 /**
  * Creates new multipattern structure
@@ -153,4 +152,10 @@ guint rspamd_multipattern_get_npatterns (struct rspamd_multipattern *mp);
  */
 void rspamd_multipattern_destroy (struct rspamd_multipattern *mp);
 
+/**
+ * Returns TRUE if hyperscan is supported
+ * @return
+ */
+gboolean rspamd_multipattern_has_hyperscan (void);
+
 #endif /* SRC_LIBUTIL_MULTIPATTERN_H_ */