2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 19:08:59 +00:00
qpdf/fuzz/qpdf_fuzzer.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
5.4 KiB
C++
Raw Normal View History

2019-06-14 16:34:23 +00:00
#include <qpdf/Buffer.hh>
#include <qpdf/BufferInputSource.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
#include <qpdf/QPDFOutlineDocumentHelper.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <cstdlib>
2019-06-14 16:34:23 +00:00
class DiscardContents: public QPDFObjectHandle::ParserCallbacks
{
public:
~DiscardContents() override = default;
void
handleObject(QPDFObjectHandle) override
2019-06-14 16:34:23 +00:00
{
}
void
handleEOF() override
2019-06-14 16:34:23 +00:00
{
}
};
class FuzzHelper
{
public:
FuzzHelper(unsigned char const* data, size_t size);
void run();
private:
std::shared_ptr<QPDF> getQpdf();
std::shared_ptr<QPDFWriter> getWriter(std::shared_ptr<QPDF>);
void doWrite(std::shared_ptr<QPDFWriter> w);
2019-06-14 16:34:23 +00:00
void testWrite();
void testPages();
void testOutlines();
void doChecks();
Buffer input_buffer;
Pl_Discard discard;
};
FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
// We do not modify data, so it is safe to remove the const for Buffer
input_buffer(const_cast<unsigned char*>(data), size)
{
}
std::shared_ptr<QPDF>
2019-06-14 16:34:23 +00:00
FuzzHelper::getQpdf()
{
auto is =
std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer));
auto qpdf = QPDF::create();
2019-06-14 16:34:23 +00:00
qpdf->processInputSource(is);
return qpdf;
}
std::shared_ptr<QPDFWriter>
FuzzHelper::getWriter(std::shared_ptr<QPDF> qpdf)
2019-06-14 16:34:23 +00:00
{
auto w = std::make_shared<QPDFWriter>(*qpdf);
2019-06-14 16:34:23 +00:00
w->setOutputPipeline(&this->discard);
w->setDecodeLevel(qpdf_dl_all);
return w;
}
void
FuzzHelper::doWrite(std::shared_ptr<QPDFWriter> w)
2019-06-14 16:34:23 +00:00
{
try {
w->write();
} catch (QPDFExc const& e) {
std::cerr << e.what() << std::endl;
} catch (std::runtime_error const& e) {
std::cerr << e.what() << std::endl;
}
2019-06-14 16:34:23 +00:00
}
void
FuzzHelper::testWrite()
{
// Write in various ways to exercise QPDFWriter
std::shared_ptr<QPDF> q;
std::shared_ptr<QPDFWriter> w;
2019-06-14 16:34:23 +00:00
q = getQpdf();
w = getWriter(q);
w->setDeterministicID(true);
w->setQDFMode(true);
2019-06-14 16:34:23 +00:00
doWrite(w);
q = getQpdf();
w = getWriter(q);
w->setStaticID(true);
2019-06-14 16:34:23 +00:00
w->setLinearization(true);
w->setR6EncryptionParameters("u", "o", true, true, true, true, true, true, qpdf_r3p_full, true);
2019-06-14 16:34:23 +00:00
doWrite(w);
q = getQpdf();
w = getWriter(q);
w->setStaticID(true);
2019-06-14 16:34:23 +00:00
w->setObjectStreamMode(qpdf_o_disable);
w->setR3EncryptionParametersInsecure(
"u", "o", true, true, true, true, true, true, qpdf_r3p_full);
2019-06-14 16:34:23 +00:00
doWrite(w);
q = getQpdf();
w = getWriter(q);
w->setDeterministicID(true);
2019-06-14 16:34:23 +00:00
w->setObjectStreamMode(qpdf_o_generate);
w->setLinearization(true);
2019-06-14 16:34:23 +00:00
doWrite(w);
}
void
FuzzHelper::testPages()
{
// Parse all content streams, and exercise some helpers that
// operate on pages.
std::shared_ptr<QPDF> q = getQpdf();
2019-06-14 16:34:23 +00:00
QPDFPageDocumentHelper pdh(*q);
QPDFPageLabelDocumentHelper pldh(*q);
QPDFOutlineDocumentHelper odh(*q);
QPDFAcroFormDocumentHelper afdh(*q);
afdh.generateAppearancesIfNeeded();
pdh.flattenAnnotations();
2019-06-14 16:34:23 +00:00
DiscardContents discard_contents;
int pageno = 0;
for (auto& page: pdh.getAllPages()) {
2019-06-14 16:34:23 +00:00
++pageno;
try {
page.coalesceContentStreams();
page.parseContents(&discard_contents);
page.getImages();
2019-06-14 16:34:23 +00:00
pldh.getLabelForPage(pageno);
QPDFObjectHandle page_obj(page.getObjectHandle());
page_obj.getJSON(JSON::LATEST, true).unparse();
odh.getOutlinesForPage(page_obj.getObjGen());
2019-06-14 16:34:23 +00:00
for (auto& aoh: afdh.getWidgetAnnotationsForPage(page)) {
2019-06-14 16:34:23 +00:00
afdh.getFieldForAnnotation(aoh);
}
} catch (QPDFExc& e) {
std::cerr << "page " << pageno << ": " << e.what() << std::endl;
}
}
}
void
FuzzHelper::testOutlines()
{
std::shared_ptr<QPDF> q = getQpdf();
std::list<std::vector<QPDFOutlineObjectHelper>> queue;
2019-06-14 16:34:23 +00:00
QPDFOutlineDocumentHelper odh(*q);
queue.push_back(odh.getTopLevelOutlines());
while (!queue.empty()) {
for (auto& ol: *(queue.begin())) {
2019-06-14 16:34:23 +00:00
ol.getDestPage();
queue.push_back(ol.getKids());
}
queue.pop_front();
}
}
void
FuzzHelper::doChecks()
{
// Get as much coverage as possible in parts of the library that
// might benefit from fuzzing.
testWrite();
testPages();
testOutlines();
}
void
FuzzHelper::run()
{
// The goal here is that you should be able to throw anything at
// libqpdf and it will respond without any memory errors and never
// do anything worse than throwing a QPDFExc or
// std::runtime_error. Throwing any other kind of exception,
// segfaulting, or having a memory error (when built with
// appropriate sanitizers) will all cause abnormal exit.
try {
doChecks();
} catch (QPDFExc const& e) {
std::cerr << "QPDFExc: " << e.what() << std::endl;
} catch (std::runtime_error const& e) {
std::cerr << "runtime_error: " << e.what() << std::endl;
}
}
extern "C" int
LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
{
#ifndef _WIN32
// Used by jpeg library to work around false positives in memory
// sanitizer.
setenv("JSIMD_FORCENONE", "1", 1);
#endif
2019-06-14 16:34:23 +00:00
FuzzHelper f(data, size);
f.run();
return 0;
}