Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: disable logging when running under fuzz engine. #4161

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions test/fuzz/fuzz_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
namespace Envoy {
namespace Fuzz {

void Runner::setupEnvironment(int argc, char** argv) {
spdlog::level::level_enum Runner::log_level_;

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

TestEnvironment::initializeOptions(argc, argv);

static auto* lock = new Thread::MutexBasicLockable();
const auto environment_log_level = TestEnvironment::getOptions().logLevel();
Logger::Registry::initialize(std::min(environment_log_level, spdlog::level::info),
TestEnvironment::getOptions().logFormat(), *lock);
// We only override the default log level if it looks like we're debugging;
// otherwise the default environment log level might override the default and
// spew too much when running under a fuzz engine.
log_level_ =
environment_log_level <= spdlog::level::debug ? environment_log_level : default_log_level;
Logger::Registry::initialize(log_level_, TestEnvironment::getOptions().logFormat(), *lock);
}

} // namespace Fuzz
} // namespace Envoy

extern "C" int LLVMFuzzerInitialize(int* /*argc*/, char*** argv) {
Envoy::Fuzz::Runner::setupEnvironment(1, *argv);
Envoy::Fuzz::Runner::setupEnvironment(1, *argv, spdlog::level::off);
return 0;
}
12 changes: 11 additions & 1 deletion test/fuzz/fuzz_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Bring in DEFINE_PROTO_FUZZER definition as per
// https://github.com/google/libprotobuf-mutator#integrating-with-libfuzzer.
#include "libprotobuf_mutator/src/libfuzzer/libfuzzer_macro.h"
#include "spdlog/spdlog.h"

namespace Envoy {
namespace Fuzz {
Expand All @@ -17,8 +18,17 @@ class Runner {
* invoked in this environment.
* @param argc number of command-line args.
* @param argv array of command-line args.
* @param default_loglevel default log level (overridable with -l).
*/
static void setupEnvironment(int argc, char** argv);
static void setupEnvironment(int argc, char** argv, spdlog::level::level_enum default_log_level);

/**
* @return spdlog::level::level_enum the log level for the fuzzer.
*/
static spdlog::level::level_enum logLevel() { return log_level_; }

private:
static spdlog::level::level_enum log_level_;
};

} // namespace Fuzz
Expand Down
2 changes: 1 addition & 1 deletion test/fuzz/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ int main(int argc, char** argv) {
RELEASE_ASSERT(Envoy::Filesystem::directoryExists(corpus_path), "");
Envoy::test_corpus_ = Envoy::TestUtility::listFiles(corpus_path, true);
testing::InitGoogleTest(&argc, argv);
Envoy::Fuzz::Runner::setupEnvironment(argc, argv);
Envoy::Fuzz::Runner::setupEnvironment(argc, argv, spdlog::level::info);
return RUN_ALL_TESTS();
}
1 change: 1 addition & 0 deletions test/mocks/server/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MockOptions::MockOptions(const std::string& config_path) : config_path_(config_p
ON_CALL(*this, serviceClusterName()).WillByDefault(ReturnRef(service_cluster_name_));
ON_CALL(*this, serviceNodeName()).WillByDefault(ReturnRef(service_node_name_));
ON_CALL(*this, serviceZone()).WillByDefault(ReturnRef(service_zone_name_));
ON_CALL(*this, logLevel()).WillByDefault(Return(log_level_));
ON_CALL(*this, logPath()).WillByDefault(ReturnRef(log_path_));
ON_CALL(*this, maxStats()).WillByDefault(Return(1000));
ON_CALL(*this, statsOptions()).WillByDefault(ReturnRef(stats_options_));
Expand Down
1 change: 1 addition & 0 deletions test/mocks/server/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class MockOptions : public Options {
std::string service_cluster_name_;
std::string service_node_name_;
std::string service_zone_name_;
spdlog::level::level_enum log_level_{spdlog::level::trace};
std::string log_path_;
Stats::StatsOptionsImpl stats_options_;
bool hot_restart_disabled_{};
Expand Down
1 change: 1 addition & 0 deletions test/server/config_validation/config_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ DEFINE_PROTO_FUZZER(const envoy::config::bootstrap::v2::Bootstrap& input) {
bootstrap_file << input.DebugString();
options.config_path_ = bootstrap_path;
options.v2_config_only_ = true;
options.log_level_ = Fuzz::Runner::logLevel();

try {
validateConfig(options, Network::Address::InstanceConstSharedPtr(), component_factory);
Expand Down
1 change: 1 addition & 0 deletions test/server/server_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ DEFINE_PROTO_FUZZER(const envoy::config::bootstrap::v2::Bootstrap& input) {
bootstrap_file << input.DebugString();
options.config_path_ = bootstrap_path;
options.v2_config_only_ = true;
options.log_level_ = Fuzz::Runner::logLevel();
}

try {
Expand Down