[Forensics-changes] [yara] 392/415: Improve multi-threading support

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:27 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch debian
in repository yara.

commit 8dc2485785bc8e7a8b6afd67ec3e0a25038d0bad
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Thu Feb 6 11:17:25 2014 +0100

    Improve multi-threading support
    
    In previous versions YR_RULES objects can't be used by more than MAX_THREADS during the whole lifetime of the object. Now this limit applies to the number of threads *simultaneosly* using the object.
---
 libyara/compiler.c |  2 +-
 libyara/re.c       |  6 ++++++
 libyara/rules.c    | 42 +++++++++++++++++++++++++-----------------
 libyara/yara.h     | 13 +++++++++++--
 4 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index 260c42a..4661f2c 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -504,7 +504,7 @@ int yr_compiler_get_rules(
     yara_rules->externals_list_head = rules_file_header->externals_list_head;
     yara_rules->automaton = rules_file_header->automaton;
     yara_rules->code_start = rules_file_header->code_start;
-    yara_rules->threads_count = 0;
+    yara_rules->tidx_mask = 0;
 
     #if WIN32
     yara_rules->mutex = CreateMutex(NULL, FALSE, NULL);
diff --git a/libyara/re.c b/libyara/re.c
index cb2e452..ae87c42 100644
--- a/libyara/re.c
+++ b/libyara/re.c
@@ -166,6 +166,12 @@ int yr_re_finalize_thread()
     yr_free(storage);
   }
 
+  #ifdef WIN32
+  TlsSetValue(thread_storage_key, NULL);
+  #else
+  pthread_setspecific(thread_storage_key, NULL);
+  #endif
+
   return ERROR_SUCCESS;
 }
 
diff --git a/libyara/rules.c b/libyara/rules.c
index a9ed9f0..5b0a3ec 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -1099,35 +1099,37 @@ int yr_rules_scan_mem_blocks(
   YR_ARENA* matches_arena = NULL;
 
   time_t start_time;
+  tidx_mask_t bit;
 
   int message;
-  int tidx;
+  int tidx = 0;
   int result = ERROR_SUCCESS;
 
   context.file_size = block->size;
   context.mem_block = block;
   context.entry_point = UNDEFINED;
 
-  tidx = yr_get_tidx();
+  _yr_rules_lock(rules);
 
-  if (tidx == -1)
-  {
-    _yr_rules_lock(rules);
+  bit = 1;
 
-    tidx = rules->threads_count;
+  while (rules->tidx_mask & bit)
+  {
+    tidx++;
+    bit <<= 1;
+  }
 
-    if (tidx < MAX_THREADS)
-      rules->threads_count++;
-    else
-      result = ERROR_TOO_MANY_SCAN_THREADS;
+  if (tidx < MAX_THREADS)
+    rules->tidx_mask |= bit;
+  else
+    result = ERROR_TOO_MANY_SCAN_THREADS;
 
-    _yr_rules_unlock(rules);
+  _yr_rules_unlock(rules);
 
-    if (result != ERROR_SUCCESS)
-      return result;
+  if (result != ERROR_SUCCESS)
+    return result;
 
-    yr_set_tidx(tidx);
-  }
+  yr_set_tidx(tidx);
 
   result = yr_arena_create(1024, 0, &matches_arena);
 
@@ -1226,6 +1228,12 @@ _exit:
   if (matches_arena != NULL)
     yr_arena_destroy(matches_arena);
 
+  _yr_rules_lock(rules);
+  rules->tidx_mask &= ~(1 << tidx);
+  _yr_rules_unlock(rules);
+
+  yr_set_tidx(-1);
+
   return result;
 }
 
@@ -1334,7 +1342,7 @@ int yr_rules_save(
     YR_RULES* rules,
     const char* filename)
 {
-  assert(rules->threads_count == 0);
+  assert(rules->tidx_mask == 0);
   return yr_arena_save(rules->arena, filename);
 }
 
@@ -1367,7 +1375,7 @@ int yr_rules_load(
   new_rules->code_start = header->code_start;
   new_rules->externals_list_head = header->externals_list_head;
   new_rules->rules_list_head = header->rules_list_head;
-  new_rules->threads_count = 0;
+  new_rules->tidx_mask = 0;
 
   #if WIN32
   new_rules->mutex = CreateMutex(NULL, FALSE, NULL);
diff --git a/libyara/yara.h b/libyara/yara.h
index 4bea20a..1f7cc2e 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -29,6 +29,8 @@ typedef HANDLE mutex_t;
 typedef pthread_mutex_t mutex_t;
 #endif
 
+typedef int32_t tidx_mask_t;
+
 #ifdef _MSC_VER
 #define snprintf _snprintf
 #endif
@@ -99,10 +101,16 @@ typedef pthread_mutex_t mutex_t;
 #define LOOP_LOCAL_VARS 4
 #define MAX_LOOP_NESTING 4
 #define MAX_INCLUDE_DEPTH 16
-#define MAX_THREADS 32
+
 #define STRING_CHAINING_THRESHOLD 200
 #define LEX_BUF_SIZE  1024
 
+// MAX_THREADS is the number of threads that can use a YR_RULES
+// object simultaneosly. This value is limited by the number of
+// bits in tidx_mask.
+
+#define MAX_THREADS sizeof(tidx_mask_t) * 8
+
 
 #ifndef MAX_PATH
 #define MAX_PATH 1024
@@ -580,8 +588,9 @@ typedef struct _YR_MEMORY_BLOCK
 
 typedef struct _YR_RULES {
 
-  int threads_count;
+  tidx_mask_t tidx_mask;
   uint8_t* code_start;
+
   mutex_t mutex;
 
   YR_ARENA* arena;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list