-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into anilm3/v2
- Loading branch information
Showing
3 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
file(GLOB_RECURSE LIBDDWAF_BENCHMARK_SOURCE *.cpp) | ||
|
||
add_executable(benchmark ${LIBDDWAF_BENCHMARK_SOURCE}) | ||
target_compile_options(benchmark PRIVATE $<$<BOOL:${LIBDDWAF_ENABLE_LTO}>:-flto>) | ||
target_link_libraries(benchmark PRIVATE libddwaf_objects lib_yamlcpp lib_rapidjson m) | ||
target_include_directories(benchmark PRIVATE ${libddwaf_SOURCE_DIR}/src) | ||
add_executable(waf_benchmark ${LIBDDWAF_BENCHMARK_SOURCE}) | ||
target_compile_options(waf_benchmark PRIVATE $<$<BOOL:${LIBDDWAF_ENABLE_LTO}>:-flto>) | ||
target_link_libraries(waf_benchmark PRIVATE libddwaf_objects lib_yamlcpp lib_rapidjson m) | ||
target_include_directories(waf_benchmark PRIVATE ${libddwaf_SOURCE_DIR}/src) | ||
|
||
set_target_properties(benchmark PROPERTIES | ||
set_target_properties(waf_benchmark PROPERTIES | ||
CXX_STANDARD 20 | ||
CXX_STANDARD_REQUIRED YES | ||
CXX_EXTENSIONS NO) | ||
|
||
add_custom_target(run_benchmark | ||
COMMAND $<TARGET_FILE:benchmark> | ||
COMMAND $<TARGET_FILE:waf_benchmark> | ||
--scenarios=${CMAKE_CURRENT_SOURCE_DIR}/scenarios | ||
--iterations=1000 | ||
--format=json | ||
--output=benchmark_results.json | ||
--fixtures="random" | ||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} | ||
) | ||
add_dependencies(run_benchmark benchmark) | ||
add_dependencies(run_benchmark waf_benchmark) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Unless explicitly stated otherwise all files in this repository are | ||
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License. | ||
// | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2021 Datadog, Inc. | ||
|
||
#include <cstdlib> | ||
#include <exception> | ||
#include <iostream> | ||
#include <optional> | ||
#include <rapidjson/document.h> | ||
#include <rapidjson/stringbuffer.h> | ||
#include <rapidjson/prettywriter.h> | ||
#include <re2/re2.h> | ||
#include <re2/regexp.h> | ||
#include <string> | ||
#include <yaml-cpp/node/parse.h> | ||
|
||
#include "common/utils.hpp" | ||
|
||
std::optional<std::string> simplify_regex(const std::string &str) { | ||
re2::RE2 regex(str); | ||
|
||
auto *regexp = regex.Regexp(); | ||
auto simplified = regexp->ToString(); | ||
if (simplified != str) { | ||
return simplified; | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
void simplify_condition(auto &condition, auto &allocator) | ||
{ | ||
std::string_view op = condition["operator"].GetString(); | ||
if (op != "match_regex") { | ||
return; | ||
} | ||
|
||
auto ¶meters = condition["parameters"]; | ||
auto ®ex = parameters["regex"]; | ||
std::string regex_str = regex.GetString(); | ||
|
||
auto simplified_regex = simplify_regex(regex_str); | ||
if (simplified_regex.has_value()) { | ||
regex.SetString(simplified_regex.value(), allocator); | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int retval = EXIT_SUCCESS; | ||
|
||
if (argc < 2) { | ||
std::cout << "Usage: " << argv[0] << " <json/yaml file>\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
rapidjson::Document doc; | ||
doc.Parse(read_file(argv[1])); | ||
|
||
auto &alloc = doc.GetAllocator(); | ||
|
||
if (doc.HasMember("rules")) { | ||
auto &rules = doc["rules"]; | ||
for (auto &rule : rules.GetArray()) { | ||
auto &conditions = rule["conditions"]; | ||
for (auto &condition : conditions.GetArray()) { | ||
simplify_condition(condition, alloc); | ||
} | ||
} | ||
} | ||
|
||
if (doc.HasMember("scanners")) { | ||
auto &scanners = doc["scanners"]; | ||
for (auto &scanner : scanners.GetArray()) { | ||
auto value_it = scanner.FindMember("value"); | ||
if (value_it != scanner.MemberEnd()) { | ||
auto &value = value_it->value; | ||
simplify_condition(value, alloc); | ||
} | ||
|
||
auto key_it = scanner.FindMember("key"); | ||
if (key_it != scanner.MemberEnd()) { | ||
auto &key = key_it->value; | ||
simplify_condition(key, alloc); | ||
} | ||
} | ||
} | ||
|
||
rapidjson::StringBuffer buffer; | ||
buffer.Clear(); | ||
|
||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); | ||
writer.SetIndent(' ', 2); | ||
doc.Accept(writer); | ||
|
||
std::cout << buffer.GetString(); | ||
|
||
return retval; | ||
} |