[Forensics-changes] [yara] 200/415: Fix multiple issues with multi-threading implementation in Windows.
Hilko Bengen
bengen at moszumanska.debian.org
Thu Apr 3 05:43:05 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 4d2d5243e003e2bbf111bd73de6644b960b11aea
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Thu Sep 26 15:44:22 2013 +0000
Fix multiple issues with multi-threading implementation in Windows.
---
libyara/libyara.c | 18 ++++++++++++++----
libyara/rules.c | 12 ++++++++++--
libyara/yara.h | 4 ++--
threading.c | 2 +-
threading.h | 10 ++++++++--
windows/yara/yara.vcxproj | 2 ++
yara.c | 23 +++++++----------------
7 files changed, 44 insertions(+), 27 deletions(-)
diff --git a/libyara/libyara.c b/libyara/libyara.c
index fe9f968..8eb4970 100644
--- a/libyara/libyara.c
+++ b/libyara/libyara.c
@@ -27,7 +27,6 @@ limitations under the License.
#ifdef WIN32
#else
#include <pthread.h>
-#define PTHREADS
#endif
char isregexescapable[256];
@@ -35,7 +34,12 @@ char isregexhashable[256];
char isalphanum[256];
char lowercase[256];
+#ifdef WIN32
+DWORD key;
+#else
pthread_key_t key;
+#endif
+
void yr_initialize(void)
{
@@ -80,7 +84,9 @@ void yr_initialize(void)
yr_heap_alloc();
- #ifdef PTHREADS
+ #ifdef WIN32
+ key = TlsAlloc();
+ #else
pthread_key_create(&key, NULL);
#endif
@@ -106,9 +112,9 @@ void yr_finalize(void)
void yr_set_tidx(int tidx)
{
#ifdef WIN32
- //TODO: implement this
+ TlsSetValue(key, (LPVOID) (tidx + 1));
#else
- pthread_setspecific(key, (void*) (size_t) (tidx + 1));
+ pthread_setspecific(key, (void*) (size_t) (tidx + 1));
#endif
}
@@ -125,5 +131,9 @@ void yr_set_tidx(int tidx)
int yr_get_tidx(void)
{
+ #ifdef WIN32
+ return (int) TlsGetValue(key) - 1;
+ #else
return (int) (size_t) pthread_getspecific(key) - 1;
+ #endif
}
diff --git a/libyara/rules.c b/libyara/rules.c
index 5c5e379..dba8750 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -488,14 +488,22 @@ int _yr_scan_verify_match(
void _yr_rules_lock(
YARA_RULES* rules)
{
+ #ifdef WIN32
+ WaitForSingleObject(rules->mutex, INFINITE);
+ #else
pthread_mutex_lock(&rules->mutex);
+ #endif
}
void _yr_rules_unlock(
YARA_RULES* rules)
{
+ #ifdef WIN32
+ ReleaseMutex(rules->mutex);
+ #else
pthread_mutex_unlock(&rules->mutex);
+ #endif
}
@@ -669,7 +677,7 @@ int yr_rules_scan_mem_block(
current_time = time(NULL);
if (difftime(current_time, start_time) > timeout)
- return ERROR_TIMEOUT;
+ return ERROR_SCAN_TIMEOUT;
}
}
@@ -728,7 +736,7 @@ int yr_rules_scan_mem_blocks(
if (tidx < MAX_THREADS)
rules->threads_count++;
else
- result = ERROR_TOO_MANY_THREADS;
+ result = ERROR_TOO_MANY_SCAN_THREADS;
_yr_rules_unlock(rules);
diff --git a/libyara/yara.h b/libyara/yara.h
index 656c413..fcc03d6 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -82,10 +82,10 @@ typedef pthread_mutex_t mutex_t;
#define ERROR_INCLUDE_DEPTH_EXCEEDED 32
#define ERROR_INVALID_OR_CORRUPT_FILE 33
#define ERROR_EXEC_STACK_OVERFLOW 34
-#define ERROR_TIMEOUT 35
+#define ERROR_SCAN_TIMEOUT 35
#define ERROR_LOOP_NESTING_LIMIT_EXCEEDED 36
#define ERROR_DUPLICATE_LOOP_IDENTIFIER 37
-#define ERROR_TOO_MANY_THREADS 38
+#define ERROR_TOO_MANY_SCAN_THREADS 38
#define CALLBACK_MSG_RULE_MATCHING 1
diff --git a/threading.c b/threading.c
index 74da59a..517bd2e 100644
--- a/threading.c
+++ b/threading.c
@@ -17,7 +17,7 @@ limitations under the License.
#include "threading.h"
-int mutex_init(
+void mutex_init(
MUTEX* mutex)
{
#ifdef WIN32
diff --git a/threading.h b/threading.h
index a9a6f5d..8934bfc 100644
--- a/threading.h
+++ b/threading.h
@@ -31,7 +31,7 @@ typedef HANDLE SEMAPHORE;
typedef HANDLE MUTEX;
typedef HANDLE THREAD;
-typedef THREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
+typedef LPTHREAD_START_ROUTINE THREAD_START_ROUTINE;
#else
@@ -42,7 +42,10 @@ typedef void *(*THREAD_START_ROUTINE) (void *);
#endif
-int mutex_init(
+void mutex_init(
+ MUTEX* mutex);
+
+void mutex_destroy(
MUTEX* mutex);
void mutex_lock(
@@ -55,6 +58,9 @@ void semaphore_init(
SEMAPHORE* semaphore,
int value);
+void semaphore_destroy(
+ SEMAPHORE* semaphore);
+
void semaphore_wait(
SEMAPHORE* semaphore);
diff --git a/windows/yara/yara.vcxproj b/windows/yara/yara.vcxproj
index 5d705f3..0a8297e 100644
--- a/windows/yara/yara.vcxproj
+++ b/windows/yara/yara.vcxproj
@@ -19,11 +19,13 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
+ <ClCompile Include="..\..\threading.c" />
<ClCompile Include="..\..\yara.c" />
<ClCompile Include="getopt.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\libyara\yara.h" />
+ <ClInclude Include="..\..\threading.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{E203D7BB-29B9-4152-9208-BB410983CE8C}</ProjectGuid>
diff --git a/yara.c b/yara.c
index 027da62..cdfb961 100644
--- a/yara.c
+++ b/yara.c
@@ -64,6 +64,7 @@ limitations under the License.
#ifdef _MSC_VER
#define snprintf _snprintf
+#define strdup _strdup
#endif
#define MAX_QUEUED_FILES 64
@@ -102,7 +103,6 @@ typedef struct _EXTERNAL
typedef struct _QUEUED_FILE {
char* path;
- ARENA* output;
} QUEUED_FILE;
@@ -179,10 +179,6 @@ void file_queue_put(
mutex_lock(&queue_mutex);
file_queue[queue_tail].path = strdup(file_path);
-
- //TODO: handle errors
- yr_arena_create(&file_queue[queue_tail].output);
-
queue_tail = (queue_tail + 1) % (MAX_QUEUED_FILES + 1);
mutex_unlock(&queue_mutex);
@@ -371,7 +367,7 @@ void print_scanning_error(int error)
case ERROR_INSUFICIENT_MEMORY:
fprintf(stderr, "not enough memory\n");
break;
- case ERROR_TIMEOUT:
+ case ERROR_SCAN_TIMEOUT:
fprintf(stderr, "scanning timed out\n");
break;
case ERROR_COULD_NOT_OPEN_FILE:
@@ -574,10 +570,12 @@ int callback(int message, RULE* rule, void* data)
case CALLBACK_MSG_RULE_NOT_MATCHING:
return handle_message(message, rule, data);
}
+
+ return CALLBACK_ERROR;
}
#ifdef WIN32
-DWORD WINAPI ThreadProc(LPVOID param)
+DWORD WINAPI scanning_thread(LPVOID param)
#else
void* scanning_thread(void* param)
#endif
@@ -609,6 +607,8 @@ void* scanning_thread(void* param)
free(file_path);
file_path = file_queue_get();
}
+
+ return 0;
}
@@ -837,7 +837,6 @@ int main(
int result;
THREAD thread[MAX_THREADS];
- clock_t start, end;
if (!process_cmd_line(argc, argv))
return 0;
@@ -988,8 +987,6 @@ int main(
}
else
{
- start = clock();
-
result = yr_rules_scan_file(
rules,
argv[argc - 1],
@@ -998,17 +995,11 @@ int main(
fast_scan,
timeout);
- end = clock();
-
if (result != ERROR_SUCCESS)
{
fprintf(stderr, "Error scanning %s: ", argv[argc - 1]);
print_scanning_error(result);
}
- else
- {
- printf( "Scanning time: %f s\n", (float)(end - start) / CLOCKS_PER_SEC);
- }
}
yr_rules_destroy(rules);
--
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