Skip to content

Commit

Permalink
fuzz: cleanup per-test environment after each fuzz case. (envoyproxy#…
Browse files Browse the repository at this point in the history
…4253)

This should solve our resource exhaustion problem in ClusterFuzz.

Risk level: Low
Testing: Bazel test and oss-fuzz Docker image.

Signed-off-by: Harvey Tuch <[email protected]>
  • Loading branch information
htuch authored Aug 27, 2018
1 parent 52beb06 commit 9857cfe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/fuzz/fuzz_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ PerTestEnvironment::PerTestEnvironment()
TestEnvironment::createPath(test_tmpdir_);
}

PerTestEnvironment::~PerTestEnvironment() { TestEnvironment::removePath(test_tmpdir_); }

void Runner::setupEnvironment(int argc, char** argv, spdlog::level::level_enum default_log_level) {
Event::Libevent::Global::initialize();

Expand Down
1 change: 1 addition & 0 deletions test/fuzz/fuzz_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Fuzz {
class PerTestEnvironment {
public:
PerTestEnvironment();
~PerTestEnvironment();

std::string temporaryPath(const std::string& path) const { return test_tmpdir_ + "/" + path; }

Expand Down
13 changes: 13 additions & 0 deletions test/test_common/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ void TestEnvironment::createParentPath(const std::string& path) {
RELEASE_ASSERT(::system(("mkdir -p $(dirname " + path + ")").c_str()) == 0, "");
#endif
}

void TestEnvironment::removePath(const std::string& path) {
RELEASE_ASSERT(StringUtil::startsWith(path.c_str(), TestEnvironment::temporaryDirectory()), "");
#ifdef __cpp_lib_experimental_filesystem
// We don't want to rely on rm etc. if we can avoid it, since it might not
// exist in some environments such as ClusterFuzz.
std::experimental::filesystem::remove_all(std::experimental::filesystem::path(path));
#else
// No support on this system for std::experimental::filesystem.
RELEASE_ASSERT(::system(("rm -rf " + path).c_str()) == 0, "");
#endif
}

absl::optional<std::string> TestEnvironment::getOptionalEnvVar(const std::string& var) {
const char* path = ::getenv(var.c_str());
if (path == nullptr) {
Expand Down
6 changes: 6 additions & 0 deletions test/test_common/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,11 @@ class TestEnvironment {
* @param path.
*/
static void createParentPath(const std::string& path);

/**
* Remove a path on the filesystem (rm -rf ... equivalent).
* @param path.
*/
static void removePath(const std::string& path);
};
} // namespace Envoy

0 comments on commit 9857cfe

Please sign in to comment.