-
Notifications
You must be signed in to change notification settings - Fork 124
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
Add HTMLLayout fuzzer #454
Open
AdamKorcz
wants to merge
2
commits into
apache:master
Choose a base branch
from
AdamKorcz:fuzz2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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/htmllayout.h> | ||
|
||
using namespace log4cxx; | ||
using namespace log4cxx::helpers; | ||
using namespace log4cxx::spi; | ||
|
||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||
// Setup HTMLLayout | ||
HTMLLayout layout; | ||
log4cxx::helpers::Pool p; | ||
|
||
FuzzedDataProvider fdp(data, size); | ||
|
||
// Optional locationinfo | ||
if (fdp.ConsumeBool()) { | ||
layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("true")); | ||
} | ||
// Optional threadinfo | ||
if (fdp.ConsumeBool()) { | ||
LOG4CXX_DECODE_CHAR(title, fdp.ConsumeRandomLengthString()); | ||
layout.setOption(LOG4CXX_STR("TITLE"), title); | ||
} | ||
|
||
// Header | ||
if (fdp.ConsumeBool()) { | ||
std::string headerStr = fdp.ConsumeRandomLengthString(); | ||
layout.appendHeader(LOG4CXX_STR(headerStr), p); | ||
} | ||
|
||
// 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::LogString result; | ||
layout.format(result, event, p); | ||
|
||
// Clean up | ||
log4cxx::NDC::clear(); | ||
log4cxx::MDC::clear(); | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you need to provide a maximum length to ConsumeRandomLengthString or all subsequent calls will return an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not the case. The fuzzer determines the length of the string in a deterministic manner. It can consist of the entire test case, but those odd cases are fine. Ref the doc:
"ConsumeRandomLengthString method returns a std::string as well, but its length is derived from the fuzz input and typically is hard to predict, though always deterministic. The caller can provide the max length argument."
https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#methods-for-extracting-sequences-of-bytes
I also tested it locally and found that the non-first invocations of
fdp.ConsumeRandomLengthString()
do indeed generate strings.The one that should always be last is
ConsumeRemainingBytesAsString()
. If it is not, then the remaining attempts to generate strings will yield empty strings like you say.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the documentation (and the
ConsumeRandomLengthString
function name) is missleading. When I build XMLLayoutFuzzer with an extra linelog4cxx::helpers::LogLog::debug(result);
I get this result (for example):Note also there is bug in XMLLayoutFuzzer. I believe
was supposed to be: