Skip to content

Commit

Permalink
test-only: Placing a mutex while evaluating the pm operator
Browse files Browse the repository at this point in the history
Performing an earlier optimization of the tree (before threads creation)
  • Loading branch information
Felipe Zimmerle committed Sep 8, 2017
1 parent 09743c9 commit 48b5ca7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
Expand Up @@ -18,7 +18,7 @@

#include <unistd.h>

#define NUM_THREADS 100
#define NUM_THREADS 20


char request_header[] = "" \
Expand Down Expand Up @@ -78,7 +78,7 @@ static void *process_request(void *data) {
modsecurity::Rules *rules = a->rules;
int z = 0;

for (z = 0; z < 10000; z++) {
for (z = 0; z < 1000; z++) {
modsecurity::Transaction *modsecTransaction = \
new modsecurity::Transaction(modsec, rules, NULL);
modsecTransaction->processConnection(ip, 12345, "127.0.0.1", 80);
Expand Down Expand Up @@ -157,14 +157,12 @@ class ReadingLogsViaRuleMessage {

for (i = 0; i < NUM_THREADS; i++) {
pthread_create(&threads[i], NULL, process_request, (void *)&dms);
//process_request((void *)&dms);
}

usleep(10000);

for (i=0; i < NUM_THREADS; i++) {
pthread_join(threads[i], &status);
std::cout << "Main: completed thread id :" << i << std::endl;
}
delete rules;
delete modsec;
Expand Down
5 changes: 3 additions & 2 deletions examples/reading_logs_via_rule_message/simple_request.cc
Expand Up @@ -18,14 +18,15 @@

#include <modsecurity/modsecurity.h>
#include <modsecurity/rules.h>
#include "examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h"
#include "reading_logs_via_rule_message.h"


char request_uri2[] = "/index.html?d=1";

std::unordered_multimap<std::string, std::string> requestHeaders;

int main(int argc, char **argv) {
char a = '\0';
*argv++;
if (*argv == NULL) {
*argv--;
Expand All @@ -41,7 +42,7 @@ int main(int argc, char **argv) {

std::string rules(*argv);
ReadingLogsViaRuleMessage rlvrm(requestHeaders, request_uri, request_body,
"", response_body, ip, rules);
&a, response_body, ip, rules);
rlvrm.process();


Expand Down
10 changes: 8 additions & 2 deletions src/operators/pm.cc
Expand Up @@ -40,6 +40,7 @@ Pm::~Pm() {

free(m_p);
m_p = NULL;
pthread_mutex_destroy(&m_lock);
}


Expand Down Expand Up @@ -86,8 +87,9 @@ bool Pm::evaluate(Transaction *transaction, Rule *rule,
pt.parser = m_p;
pt.ptr = NULL;
const char *match = NULL;

pthread_mutex_lock(&m_lock);
rc = acmp_process_quick(&pt, &match, input.c_str(), input.length());
pthread_mutex_unlock(&m_lock);
bool capture = rule && rule->getActionsByName("capture").size() > 0;

if (rc > 0 && transaction) {
Expand All @@ -114,6 +116,8 @@ bool Pm::init(const std::string &file, std::string *error) {
std::istringstream *iss;
const char *err = NULL;

pthread_mutex_init(&m_lock, NULL);

char *content = parse_pm_content(m_param.c_str(), m_param.length(), &err);
if (content == NULL) {
iss = new std::istringstream(m_param);
Expand All @@ -129,7 +133,9 @@ bool Pm::init(const std::string &file, std::string *error) {
acmp_add_pattern(m_p, a.c_str(), NULL, NULL, a.length());
}

acmp_prepare(m_p);
while (m_p->is_failtree_done == 0) {
acmp_prepare(m_p);
}

if (content) {
free(content);
Expand Down
3 changes: 3 additions & 0 deletions src/operators/pm.h
Expand Up @@ -55,6 +55,9 @@ class Pm : public Operator {

protected:
ACMP *m_p;

private:
pthread_mutex_t m_lock;
};


Expand Down
20 changes: 18 additions & 2 deletions src/utils/acmp.cc
Expand Up @@ -297,6 +297,10 @@ static void acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
if ((pos - lb) > 1) {
left = lb + (pos - lb) / 2;
node->left = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
node->left->node = NULL;
node->left->right = NULL;
node->left->left = NULL;
node->left->letter = 0;
/* ENH: Check alloc succeded */
node->left->node = nodes[left];
node->left->letter = nodes[left]->letter;
Expand All @@ -307,6 +311,10 @@ static void acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
if ((rb - pos) > 1) {
right = pos + (rb - pos) / 2;
node->right = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
node->right->node = NULL;
node->right->right = NULL;
node->right->left = NULL;
node->right->letter = 0;
/* ENH: Check alloc succeded */
node->right->node = nodes[right];
node->right->letter = nodes[right]->letter;
Expand Down Expand Up @@ -355,8 +363,12 @@ static void acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
nodes[i] = nodes[j];
nodes[j] = tmp;
}
if (node->btree) { free (node->btree); node->btree = NULL; }
if (node->btree != NULL) {
free(node->btree);
node->btree = NULL;
}
node->btree = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));

/* ENH: Check alloc succeded */
pos = count / 2;
node->btree->node = nodes[pos];
Expand All @@ -365,7 +377,9 @@ static void acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
for (i = 0; i < count; i++) {
if (nodes[i]->child != NULL) acmp_build_binary_tree(parser, nodes[i]);
}
free(nodes);
if (nodes != NULL) {
free(nodes);
}
}

/**
Expand Down Expand Up @@ -532,9 +546,11 @@ int acmp_process_quick(ACMPT *acmpt, const char **match, const char *data, size_
const char *end;
int offset = 0;

/*
if (acmpt->parser->is_failtree_done == 0) {
acmp_prepare(acmpt->parser);
};
*/

parser = acmpt->parser;
if (acmpt->ptr == NULL) acmpt->ptr = parser->root_node;
Expand Down

0 comments on commit 48b5ca7

Please sign in to comment.