Skip to content

Commit 91a9559

Browse files
Add time limit to all-clusters-app-fuzzing
Fuzzing binary now searches for environment variable `FUZZ_CAMPAIGN_MINUTES` to automatically limit, halt execution, and dump gcov data once X minutes have elapsed. This was required to extract gcov data from a fuzzing binary as under normal circumstances manually aborting the execution did not produce any gcov data. google/fuzzing#41
1 parent 65eb13d commit 91a9559

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

examples/all-clusters-app/linux/fuzzing-main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
#include <CommissionableInit.h>
2222

23+
#include <chrono>
24+
#include <iostream>
25+
26+
extern "C" void __gcov_dump();
27+
2328
using namespace chip;
2429
using namespace chip::DeviceLayer;
2530

@@ -40,6 +45,28 @@ void CleanShutdown()
4045

4146
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * aData, size_t aSize)
4247
{
48+
static auto fuzzCampaignStart = std::chrono::steady_clock::now();
49+
static auto fuzzCampaignMinutes = [](){
50+
char *envString = getenv("FUZZ_CAMPAIGN_MINUTES");
51+
52+
int minutes = (envString == NULL) ? 0 : atoi(envString);
53+
if (minutes > 0) std::cerr << "FUZZ_CAMPAIGN_MINUTES: " << minutes << std::endl;
54+
55+
return minutes;
56+
} ();
57+
58+
// Check elapsed time
59+
if (fuzzCampaignMinutes > 0) {
60+
auto current = std::chrono::steady_clock::now();
61+
auto elapsedMinutes = std::chrono::duration_cast<std::chrono::minutes>(current - fuzzCampaignStart).count();
62+
if (elapsedMinutes >= fuzzCampaignMinutes) {
63+
// Passed scheduled end
64+
std::cerr << "Stopping fuzzing after " << elapsedMinutes << " minutes" << std::endl;
65+
__gcov_dump();
66+
exit(0);
67+
}
68+
}
69+
4370
static bool matterStackInitialized = false;
4471
if (!matterStackInitialized)
4572
{

0 commit comments

Comments
 (0)