[Forensics-changes] [yara] 22/192: Allow MAX_THREADS to be set arbitrarily (#513)

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:31:42 UTC 2017


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

bengen pushed a commit to annotated tag v3.6.0
in repository yara.

commit 1dc7ad569049c197a5e8d1ffea025dd0521bc31a
Author: Emerson R. Wiley <emerson.wiley at gmail.com>
Date:   Thu Sep 8 10:05:54 2016 -0700

    Allow MAX_THREADS to be set arbitrarily (#513)
---
 libyara/compiler.c            |  2 +-
 libyara/include/yara/limits.h |  4 ++--
 libyara/include/yara/types.h  |  4 +---
 libyara/include/yara/utils.h  | 19 +++++++++++++++++++
 libyara/rules.c               | 19 ++++++++++---------
 5 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index 4c0fdeb..40a761f 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -590,7 +590,7 @@ YR_API int yr_compiler_get_rules(
   yara_rules->match_table = rules_file_header->match_table;
   yara_rules->transition_table = rules_file_header->transition_table;
   yara_rules->code_start = rules_file_header->code_start;
-  yara_rules->tidx_mask = 0;
+  memset(yara_rules->tidx_mask, 0, sizeof(yara_rules->tidx_mask));
 
   FAIL_ON_ERROR_WITH_CLEANUP(
       yr_mutex_create(&yara_rules->mutex),
diff --git a/libyara/include/yara/limits.h b/libyara/include/yara/limits.h
index 72b3f10..27072f4 100644
--- a/libyara/include/yara/limits.h
+++ b/libyara/include/yara/limits.h
@@ -34,10 +34,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <windows.h>
 #endif
 
+#include "utils.h"
 
 // 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.
+// object simultaneosly.
 
 #define MAX_THREADS 32
 
diff --git a/libyara/include/yara/types.h b/libyara/include/yara/types.h
index bdacaa9..a7c50cc 100644
--- a/libyara/include/yara/types.h
+++ b/libyara/include/yara/types.h
@@ -44,8 +44,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <time.h>
 #endif
 
-typedef int32_t tidx_mask_t;
-
 
 #define DECLARE_REFERENCE(type, name) \
     union { type name; int64_t name##_; } YR_ALIGN(8)
@@ -369,7 +367,7 @@ typedef struct _YR_AC_AUTOMATON
 
 typedef struct _YR_RULES {
 
-  tidx_mask_t tidx_mask;
+  unsigned char tidx_mask[YR_BITARRAY_NCHARS(MAX_THREADS)];
   uint8_t* code_start;
 
   YR_MUTEX mutex;
diff --git a/libyara/include/yara/utils.h b/libyara/include/yara/utils.h
index 104d9ee..2323fdc 100644
--- a/libyara/include/yara/utils.h
+++ b/libyara/include/yara/utils.h
@@ -78,6 +78,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #else
 
 #include <stdlib.h>
+#include <limits.h>
 
 #define assertf(expr, msg, ...) \
     if(!(expr)) { \
@@ -87,4 +88,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #endif
 
+// Set, unset, and test bits in an array of unsigned characters by integer
+// index. The underlying array must be of type char or unsigned char to
+// ensure compatibility with the CHAR_BIT constant used in these definitions.
+
+#define YR_BITARRAY_SET(uchar_array_base, bitnum) \
+          (((uchar_array_base)[(bitnum)/CHAR_BIT]) = \
+            ((uchar_array_base)[(bitnum)/CHAR_BIT] | (1 << ((bitnum) % CHAR_BIT))))
+
+#define YR_BITARRAY_UNSET(uchar_array_base, bitnum) \
+          (((uchar_array_base)[(bitnum)/CHAR_BIT]) = \
+            ((uchar_array_base)[(bitnum)/CHAR_BIT] & (~(1 << ((bitnum) % CHAR_BIT)))))
+
+#define YR_BITARRAY_TEST(uchar_array_base, bitnum) \
+          (((uchar_array_base)[(bitnum)/CHAR_BIT] & (1 << ((bitnum) % CHAR_BIT))) != 0)
+
+#define YR_BITARRAY_NCHARS(bitnum) \
+          (((bitnum)+(CHAR_BIT-1))/CHAR_BIT)
+
 #endif
diff --git a/libyara/rules.c b/libyara/rules.c
index 490c472..e708c70 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -342,7 +342,6 @@ YR_API int yr_rules_scan_mem_blocks(
   YR_MEMORY_BLOCK* block;
 
   time_t start_time;
-  tidx_mask_t bit = 1;
 
   int tidx = 0;
   int result = ERROR_SUCCESS;
@@ -354,16 +353,16 @@ YR_API int yr_rules_scan_mem_blocks(
 
   yr_mutex_lock(&rules->mutex);
 
-  while (rules->tidx_mask & bit)
+  while (YR_BITARRAY_TEST(rules->tidx_mask, tidx) && tidx < MAX_THREADS)
   {
     tidx++;
-    bit <<= 1;
   }
 
-  if (tidx < MAX_THREADS)
-    rules->tidx_mask |= bit;
-  else
+  if (tidx < MAX_THREADS) {
+    YR_BITARRAY_SET(rules->tidx_mask, tidx);
+  } else {
     result = ERROR_TOO_MANY_SCAN_THREADS;
+  }
 
   yr_mutex_unlock(&rules->mutex);
 
@@ -527,7 +526,7 @@ _exit:
         (YR_HASH_TABLE_FREE_VALUE_FUNC) yr_object_destroy);
 
   yr_mutex_lock(&rules->mutex);
-  rules->tidx_mask &= ~(1 << tidx);
+  YR_BITARRAY_UNSET(rules->tidx_mask, tidx);
   yr_mutex_unlock(&rules->mutex);
 
   yr_set_tidx(-1);
@@ -703,7 +702,7 @@ YR_API int yr_rules_load_stream(
   new_rules->rules_list_head = header->rules_list_head;
   new_rules->match_table = header->match_table;
   new_rules->transition_table = header->transition_table;
-  new_rules->tidx_mask = 0;
+  memset(new_rules->tidx_mask, 0, sizeof(new_rules->tidx_mask));
 
   FAIL_ON_ERROR_WITH_CLEANUP(
       yr_mutex_create(&new_rules->mutex),
@@ -742,7 +741,9 @@ YR_API int yr_rules_save_stream(
     YR_RULES* rules,
     YR_STREAM* stream)
 {
-  assert(rules->tidx_mask == 0);
+  for (int i = 0; i < YR_BITARRAY_NCHARS(MAX_THREADS); ++i) {
+    assert(rules->tidx_mask[i] == 0);
+  }
   return yr_arena_save_stream(rules->arena, stream);
 }
 

-- 
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