Skip to content

Commit

Permalink
Add JSONLayout fuzzer
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Korczynski <[email protected]>
  • Loading branch information
AdamKorcz committed Jan 17, 2025
1 parent 9e60eca commit dd3ab93
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/fuzzers/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
#

set(ALL_LOG4CXX_FUZZERS PatternLayoutFuzzer XMLLayoutFuzzer)
set(ALL_LOG4CXX_FUZZERS PatternLayoutFuzzer XMLLayoutFuzzer JSONLayoutFuzzer)
set(LOG4CXX_CHAR "utf-8")

# Get the most recent Git commit ID
Expand Down
86 changes: 86 additions & 0 deletions src/fuzzers/cpp/JSONLayoutFuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdint.h"
#include <log4cxx/logstring.h>
#include <log4cxx/ndc.h>
#include <log4cxx/helpers/stringhelper.h>
#include <fuzzer/FuzzedDataProvider.h>
#include <log4cxx/mdc.h>
#include <log4cxx/jsonlayout.h>

using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Setup JSONLayout
JSONLayout layout;

FuzzedDataProvider fdp(data, size);

// Optional locationinfo
if (fdp.ConsumeBool()) {
layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("locationinfo"));
}
// Optional threadinfo
if (fdp.ConsumeBool()) {
layout.setOption(LOG4CXX_STR("THREADINFO"), LOG4CXX_STR("threadinfo"));
}
// Optional prettyprint
if (fdp.ConsumeBool()) {
layout.setOption(LOG4CXX_STR("PRETTYPRINT"), LOG4CXX_STR("prettyprint"));
}

// Create random strings we need later
std::string key1 = fdp.ConsumeRandomLengthString();
std::string val1 = fdp.ConsumeRandomLengthString();
std::string key2 = fdp.ConsumeRandomLengthString();
std::string val2 = fdp.ConsumeRandomLengthString();
std::string key3 = fdp.ConsumeRandomLengthString();
std::string val3 = fdp.ConsumeRandomLengthString();
std::string key4 = fdp.ConsumeRandomLengthString();
std::string val4 = fdp.ConsumeRandomLengthString();
std::string ndcMessage = fdp.ConsumeRandomLengthString();
std::string loggerStr = fdp.ConsumeRandomLengthString();
std::string content = fdp.ConsumeRemainingBytesAsString();

log4cxx::LogString logger = LOG4CXX_STR(loggerStr);
log4cxx::LevelPtr level = log4cxx::Level::getInfo();
log4cxx::NDC::push(ndcMessage);
log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
new log4cxx::spi::LoggingEvent(
logger, level, LOG4CXX_STR(content), LOG4CXX_LOCATION));

// Set properties
event->setProperty(LOG4CXX_STR(key1), LOG4CXX_STR(val1));
event->setProperty(LOG4CXX_STR(key2), LOG4CXX_STR(val2));

// Set MDC
log4cxx::MDC::put(key3, val3);
log4cxx::MDC::put(key4, val4);

// Call the target API
log4cxx::helpers::Pool p;
log4cxx::LogString result;
layout.format(result, event, p);

// Clean up
log4cxx::NDC::clear();
log4cxx::MDC::clear();
return 0;
}

0 comments on commit dd3ab93

Please sign in to comment.