Why YARA-X scanned ~1.6x faster than libyara (perf + source audit, 2026-11-14): not the language — both spend ~5 s in the atom search loop — but verification (libyara 2.0 s vs 0.3 s). YARA-X derives filesize bounds and header constraints per rule at compile time and disables those patterns before the search, so their atom hits are dropped with one bit test. In the yara-forge core pack 1787 rules require an MZ header and 2498 bound filesize, so for big Linux ELFs most of libyara's verification is wasted. Rule partitioning gets the same win with libyara, and drops the rules' atoms from the automaton entirely: - src/rule_partition.cpp (pure): lexer-aware splitter (comments, text, regex after = or matches, hex), facts from TOP-LEVEL `and` terms only (MZ / pe.is_pe, ELF, Mach-O magic; filesize < / <=), exclusion per (MZ / non-MZ) x size bucket (<1, 1-4, 4-16, >=16 MiB). Global rules and rules referenced by kept rules always stay. - YaraScanner builds subsets in a background thread after each load, in priority order (big non-PE first) under rule_partition_budget_pct (default 100 ~ one extra full set: 3 subsets, +59 MiB; all 16 class x size subsets would cost +428 MiB). Cached under rules_cache_dir/partitions/. File, buffer and fd scans pick their subset; process-memory scans use the full set. - Fails closed to the full rules: include directives, a split count that differs from libyara's, or a subset compile / count error. - Status rules.partitions (state, reason, subsets, kept_rules/scans per partition, full_scans); config rule_partitions (default true) and rule_partition_budget_pct. Measured on the 5132-rule pack: big ASan ELFs 1.64x, 1200 system binaries 1.59x, 0 match-set differences, including 348 positive files across MZ/ELF/raw x small/5 MiB. Tests: splitter traps, facts, exclusion soundness property, real pack = 5132 rules; differential test (24 file shapes x memory / ranges / fd scans, every subset built) and fail-closed; bench --partitions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
279 lines
13 KiB
C++
279 lines
13 KiB
C++
// strawberry-scan-bench: dev tool (build-only, not installed). Runs the REAL scan path
|
|
// (DetectionPipeline::scan_on_demand: budgeted read -> sha256 -> IOC -> YARA) over a list of files
|
|
// with a real rule set and the balanced resource governor, so a Tracy capture of it (build with
|
|
// -Denable_profiling=true, run under scripts/profile_capture.sh) breaks a scan into stages without
|
|
// needing root or the daemon. Prints one line per file plus a total.
|
|
//
|
|
// strawberry-scan-bench --rules DIR [--profile balanced] [--repeat N] FILE...
|
|
// (a FILE argument of "@list.txt" reads one path per line)
|
|
// strawberry-scan-bench --rules DIR --engine FILE... # engine A/B: every file scanned with each
|
|
// variant (full, fast, loaded, loaded+fast) straight through YaraScanner; prints per-variant
|
|
// time and every match difference against "full" (the parity check for a variant).
|
|
#include "strawberry/config.hpp"
|
|
#include "strawberry/detection_pipeline.hpp"
|
|
#include "strawberry/resource_governor.hpp"
|
|
#include "strawberry/profiling.hpp"
|
|
#include "strawberry/yara_scanner.hpp"
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
#include <chrono>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
int usage() {
|
|
std::cerr << "usage: strawberry-scan-bench --rules DIR [--profile NAME] [--repeat N] FILE... (@list.txt ok)\n";
|
|
return 2;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
std::string rules;
|
|
std::string profile = "balanced";
|
|
int repeat = 1;
|
|
bool engine_ab = false;
|
|
bool partitions_ab = false;
|
|
std::vector<std::string> files;
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
if (a == "--rules" && i + 1 < argc) {
|
|
rules = argv[++i];
|
|
} else if (a == "--partitions") {
|
|
partitions_ab = true;
|
|
} else if (a == "--engine") {
|
|
engine_ab = true;
|
|
} else if (a == "--profile" && i + 1 < argc) {
|
|
profile = argv[++i];
|
|
} else if (a == "--repeat" && i + 1 < argc) {
|
|
char* end = nullptr;
|
|
repeat = static_cast<int>(std::strtol(argv[++i], &end, 10));
|
|
if (*end != '\0' || repeat < 1) return usage();
|
|
} else if (!a.empty() && a[0] == '@') {
|
|
std::ifstream list(a.substr(1));
|
|
for (std::string line; std::getline(list, line);) {
|
|
if (!line.empty()) files.push_back(line);
|
|
}
|
|
} else if (!a.empty() && a[0] != '-') {
|
|
files.push_back(a);
|
|
} else {
|
|
return usage();
|
|
}
|
|
}
|
|
if (rules.empty() || files.empty()) return usage();
|
|
const auto named = strawberry::scan_profile_named(profile);
|
|
if (!named) {
|
|
std::cerr << "unknown profile " << profile << "\n";
|
|
return 2;
|
|
}
|
|
|
|
if (partitions_ab) {
|
|
// Partition A/B on the real pack: full rule set vs partitioned, same files, alternating order;
|
|
// every match-set difference is printed (it must be zero) and per-mode time is reported.
|
|
strawberry::YaraScanner off, on;
|
|
off.set_rule_partitions(false);
|
|
on.set_rule_partitions(true);
|
|
const std::vector<strawberry::YaraScanner::RuleSource> src{{rules, ""}};
|
|
const auto rss_now = [] {
|
|
std::ifstream statm("/proc/self/statm");
|
|
long pages = 0, rss = 0;
|
|
statm >> pages >> rss;
|
|
return rss * (::sysconf(_SC_PAGESIZE) / 1024) / 1024;
|
|
};
|
|
const long rss0 = rss_now();
|
|
if (!off.load_rules(src)) return 1;
|
|
const long rss_full = rss_now();
|
|
if (!on.load_rules(src)) return 1;
|
|
for (int i = 0; i < 1200 && on.partition_status().state == "building"; ++i)
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
const auto st = on.partition_status();
|
|
std::cout << "partitions: " << st.state << " " << st.reason << ", " << st.subsets << " subsets\n";
|
|
if (std::getenv("SB_BENCH_RSS")) {
|
|
// RSS with both scanners loaded (full + full-with-subsets): the subsets' cost is the delta
|
|
// against twice a single full set, measured separately with SB_BENCH_RSS=single.
|
|
std::ifstream statm("/proc/self/statm");
|
|
long pages = 0, rss = 0;
|
|
statm >> pages >> rss;
|
|
const long rss_both = rss * (::sysconf(_SC_PAGESIZE) / 1024) / 1024;
|
|
std::cout << "rss_mib base " << rss0 << " | +full set " << (rss_full - rss0) << " | +partitioned scanner (full + subsets) "
|
|
<< (rss_both - rss_full) << " => subsets cost " << (rss_both - rss_full) - (rss_full - rss0) << "\n";
|
|
}
|
|
if (st.state != "ready") return 1;
|
|
if (std::getenv("SB_BENCH_KEPT")) {
|
|
static const char* cls[] = {"nonpe", "pe"};
|
|
static const char* bk[] = {"<1M", "1-4M", "4-16M", ">=16M"};
|
|
for (std::size_t i = 0; i < 8; ++i)
|
|
std::cout << "kept " << cls[i / 4] << " " << bk[i % 4] << ": " << st.kept[i] << " / " << st.total_rules << "\n";
|
|
}
|
|
double t_off = 0, t_on = 0;
|
|
uint64_t bytes = 0;
|
|
int nfiles = 0, diffs = 0, matched = 0;
|
|
for (const auto& path : files) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
std::vector<unsigned char> data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
if (data.empty()) continue;
|
|
++nfiles;
|
|
bytes += data.size();
|
|
std::vector<strawberry::YaraMatch> a, b;
|
|
const auto timed = [&](strawberry::YaraScanner& sc, std::vector<strawberry::YaraMatch>& m) {
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
(void) sc.scan_memory(data.data(), data.size(), m, 120);
|
|
return std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
|
|
};
|
|
if (nfiles % 2) {
|
|
t_off += timed(off, a);
|
|
t_on += timed(on, b);
|
|
} else {
|
|
t_on += timed(on, b);
|
|
t_off += timed(off, a);
|
|
}
|
|
auto names = [](const std::vector<strawberry::YaraMatch>& m) {
|
|
std::vector<std::string> n;
|
|
for (const auto& x : m) n.push_back(x.rule_name);
|
|
std::sort(n.begin(), n.end());
|
|
return n;
|
|
};
|
|
if (!a.empty()) ++matched;
|
|
if (names(a) != names(b)) {
|
|
++diffs;
|
|
std::cout << "DIFF " << path << ": full=" << a.size() << " partitioned=" << b.size() << "\n";
|
|
}
|
|
}
|
|
const double mib = bytes / 1048576.0;
|
|
std::cout << nfiles << " files, " << mib << " MiB, " << matched << " with matches, " << diffs
|
|
<< " match-set differences\n";
|
|
std::cout << "full: " << t_off << " s (" << (t_off > 0 ? mib / t_off : 0) << " MiB/s)\n";
|
|
std::cout << "partitioned: " << t_on << " s (" << (t_on > 0 ? mib / t_on : 0) << " MiB/s), speedup "
|
|
<< (t_on > 0 ? t_off / t_on : 0) << "x\n";
|
|
return diffs == 0 ? 0 : 3;
|
|
}
|
|
|
|
if (engine_ab) {
|
|
strawberry::YaraScanner scanner;
|
|
if (!scanner.load_rules(std::vector<strawberry::YaraScanner::RuleSource>{{rules, ""}})) {
|
|
std::cerr << "cannot load rules " << rules << "\n";
|
|
return 1;
|
|
}
|
|
struct Variant {
|
|
const char* name;
|
|
bool loaded;
|
|
bool fast;
|
|
double secs = 0;
|
|
uint64_t bytes = 0;
|
|
int diffs = 0, matched = 0;
|
|
};
|
|
std::vector<Variant> variants{{"full", false, false}, {"fast", false, true}, {"loaded", true, false},
|
|
{"loaded+fast", true, true}};
|
|
int nfiles = 0;
|
|
for (const auto& path : files) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
std::vector<unsigned char> data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
if (data.empty()) continue;
|
|
++nfiles;
|
|
const auto ranges = strawberry::elf_load_ranges(data.data(), data.size());
|
|
std::vector<std::string> baseline;
|
|
for (auto& v : variants) {
|
|
const std::vector<strawberry::ScanRange> use = v.loaded ? ranges : std::vector<strawberry::ScanRange>{};
|
|
uint64_t scanned = 0;
|
|
for (const auto& r : use) scanned += r.size;
|
|
if (use.empty()) scanned = data.size();
|
|
strawberry::YaraScanner::EngineOptions eo;
|
|
eo.fast_mode = v.fast;
|
|
std::vector<strawberry::YaraMatch> m;
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
{
|
|
STRAWBERRY_ZONE("bench.engine_scan");
|
|
(void) scanner.scan_memory_ranges(data.data(), data.size(), use, eo, m, 60);
|
|
}
|
|
v.secs += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
|
|
v.bytes += scanned;
|
|
std::vector<std::string> names;
|
|
for (const auto& x : m) names.push_back(x.namespace_name + ":" + x.rule_name);
|
|
std::sort(names.begin(), names.end());
|
|
if (!names.empty()) ++v.matched;
|
|
if (&v == &variants[0]) {
|
|
baseline = names;
|
|
} else if (names != baseline) {
|
|
++v.diffs;
|
|
std::cout << "DIFF " << v.name << " " << path << ": full=" << baseline.size()
|
|
<< " " << v.name << "=" << names.size() << "\n";
|
|
}
|
|
}
|
|
}
|
|
std::cout << nfiles << " files\n";
|
|
for (const auto& v : variants) {
|
|
std::cout << v.name << ": " << v.secs << " s, " << v.bytes / 1048576.0 << " MiB scanned, "
|
|
<< (v.secs > 0 ? v.bytes / 1048576.0 / v.secs : 0) << " MiB/s, speedup "
|
|
<< (v.secs > 0 ? variants[0].secs / v.secs : 0) << "x, " << v.matched << " files matched, "
|
|
<< v.diffs << " match-set differences vs full\n";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const auto work = std::filesystem::temp_directory_path() / ("sb-scan-bench-" + std::to_string(::getpid()));
|
|
std::filesystem::create_directories(work);
|
|
strawberry::GovernorOptions options;
|
|
options.profile = *named;
|
|
strawberry::ResourceGovernor governor(options);
|
|
governor.tick();
|
|
strawberry::Config config;
|
|
config.db_path = (work / "bench.db").string();
|
|
config.yara_rules_path = rules;
|
|
config.rules_cache_dir = (work / "rules-cache").string();
|
|
strawberry::DetectionPipeline pipeline;
|
|
pipeline.set_governor(&governor);
|
|
const auto t_init = std::chrono::steady_clock::now();
|
|
if (!pipeline.initialize(config)) {
|
|
std::cerr << "pipeline initialize failed (rules: " << rules << ")\n";
|
|
return 1;
|
|
}
|
|
std::cout << "rules loaded in "
|
|
<< std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - t_init).count()
|
|
<< " ms\n";
|
|
|
|
uint64_t total_bytes = 0;
|
|
int64_t total_us = 0;
|
|
int scanned = 0, failed = 0, matched = 0;
|
|
for (int r = 0; r < repeat; ++r) {
|
|
for (const auto& path : files) {
|
|
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOCTTY);
|
|
if (fd < 0) {
|
|
++failed;
|
|
continue;
|
|
}
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
const auto res = pipeline.scan_on_demand(fd, config.process_scan_max_bytes);
|
|
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t0).count();
|
|
::close(fd);
|
|
if (!res.ok && !res.error.empty()) {
|
|
++failed;
|
|
std::cout << "FAIL " << path << ": " << res.error << "\n";
|
|
continue;
|
|
}
|
|
++scanned;
|
|
total_bytes += res.size;
|
|
total_us += us;
|
|
if (!res.matches.empty()) ++matched;
|
|
std::cout << us / 1000 << " ms " << res.size / 1048576.0 << " MiB " << res.matches.size()
|
|
<< " match(es) " << path << "\n";
|
|
}
|
|
}
|
|
pipeline.shutdown();
|
|
std::error_code ec;
|
|
std::filesystem::remove_all(work, ec);
|
|
const double secs = static_cast<double>(total_us) / 1e6;
|
|
std::cout << "total: " << scanned << " scanned, " << failed << " failed, " << matched << " with matches, "
|
|
<< total_bytes / 1048576.0 << " MiB in " << secs << " s ("
|
|
<< (secs > 0 ? total_bytes / 1048576.0 / secs : 0) << " MiB/s)\n";
|
|
return failed > 0 && scanned == 0 ? 1 : 0;
|
|
}
|