Coverage Report

Created: 2025-08-26 07:08

/src/qpdf/fuzz/qpdf_fuzzer.cc
Line
Count
Source
1
#include <qpdf/Buffer.hh>
2
#include <qpdf/BufferInputSource.hh>
3
#include <qpdf/Pl_DCT.hh>
4
#include <qpdf/Pl_Discard.hh>
5
#include <qpdf/Pl_Flate.hh>
6
#include <qpdf/Pl_PNGFilter.hh>
7
#include <qpdf/Pl_RunLength.hh>
8
#include <qpdf/Pl_TIFFPredictor.hh>
9
#include <qpdf/QPDF.hh>
10
#include <qpdf/QPDFPageObjectHelper.hh>
11
#include <qpdf/QPDFWriter.hh>
12
#include <qpdf/QUtil.hh>
13
#include <cstdlib>
14
15
class FuzzHelper
16
{
17
  public:
18
    FuzzHelper(unsigned char const* data, size_t size);
19
    void run();
20
21
  private:
22
    std::shared_ptr<QPDF> getQpdf();
23
    std::shared_ptr<QPDFWriter> getWriter(std::shared_ptr<QPDF>);
24
    void doWrite(std::shared_ptr<QPDFWriter> w);
25
    void testWrite();
26
    void doChecks();
27
28
    Buffer input_buffer;
29
    Pl_Discard discard;
30
};
31
32
FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
33
    // We do not modify data, so it is safe to remove the const for Buffer
34
24.2k
    input_buffer(const_cast<unsigned char*>(data), size)
35
24.2k
{
36
24.2k
}
37
38
std::shared_ptr<QPDF>
39
FuzzHelper::getQpdf()
40
24.2k
{
41
24.2k
    auto is =
42
24.2k
        std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer));
43
24.2k
    auto qpdf = QPDF::create();
44
24.2k
    qpdf->setMaxWarnings(200);
45
24.2k
    qpdf->processInputSource(is);
46
24.2k
    return qpdf;
47
24.2k
}
48
49
std::shared_ptr<QPDFWriter>
50
FuzzHelper::getWriter(std::shared_ptr<QPDF> qpdf)
51
9.44k
{
52
9.44k
    auto w = std::make_shared<QPDFWriter>(*qpdf);
53
9.44k
    w->setOutputPipeline(&this->discard);
54
9.44k
    w->setDecodeLevel(qpdf_dl_all);
55
9.44k
    return w;
56
9.44k
}
57
58
void
59
FuzzHelper::doWrite(std::shared_ptr<QPDFWriter> w)
60
9.25k
{
61
9.25k
    try {
62
9.25k
        w->write();
63
9.25k
    } catch (QPDFExc const& e) {
64
345
        std::cerr << e.what() << '\n';
65
345
    } catch (std::runtime_error const& e) {
66
156
        std::cerr << e.what() << '\n';
67
156
    }
68
9.25k
}
69
70
void
71
FuzzHelper::testWrite()
72
24.2k
{
73
    // Write in various ways to exercise QPDFWriter
74
75
24.2k
    std::shared_ptr<QPDF> q;
76
24.2k
    std::shared_ptr<QPDFWriter> w;
77
78
24.2k
    q = getQpdf();
79
24.2k
    w = getWriter(q);
80
24.2k
    w->setDeterministicID(true);
81
24.2k
    w->setQDFMode(true);
82
24.2k
    doWrite(w);
83
24.2k
}
84
85
void
86
FuzzHelper::doChecks()
87
24.2k
{
88
    // Limit the memory used to decompress JPEG files during fuzzing. Excessive memory use during
89
    // fuzzing is due to corrupt JPEG data which sometimes cannot be detected before
90
    // jpeg_start_decompress is called. During normal use of qpdf very large JPEGs can occasionally
91
    // occur legitimately and therefore must be allowed during normal operations.
92
24.2k
    Pl_DCT::setMemoryLimit(100'000'000);
93
24.2k
    Pl_DCT::setScanLimit(50);
94
95
24.2k
    Pl_PNGFilter::setMemoryLimit(1'000'000);
96
24.2k
    Pl_RunLength::setMemoryLimit(1'000'000);
97
24.2k
    Pl_TIFFPredictor::setMemoryLimit(1'000'000);
98
24.2k
    Pl_Flate::memory_limit(200'000);
99
100
    // Do not decompress corrupt data. This may cause extended runtime within jpeglib without
101
    // exercising additional code paths in qpdf, and potentially causing counterproductive timeouts.
102
24.2k
    Pl_DCT::setThrowOnCorruptData(true);
103
104
    // Get as much coverage as possible in parts of the library that
105
    // might benefit from fuzzing.
106
24.2k
    std::cerr << "\ninfo: starting testWrite\n";
107
24.2k
    testWrite();
108
24.2k
}
109
110
void
111
FuzzHelper::run()
112
24.2k
{
113
    // The goal here is that you should be able to throw anything at
114
    // libqpdf and it will respond without any memory errors and never
115
    // do anything worse than throwing a QPDFExc or
116
    // std::runtime_error. Throwing any other kind of exception,
117
    // segfaulting, or having a memory error (when built with
118
    // appropriate sanitizers) will all cause abnormal exit.
119
24.2k
    try {
120
24.2k
        doChecks();
121
24.2k
    } catch (QPDFExc const& e) {
122
14.8k
        std::cerr << "QPDFExc: " << e.what() << '\n';
123
14.8k
    } catch (std::runtime_error const& e) {
124
163
        std::cerr << "runtime_error: " << e.what() << '\n';
125
163
    }
126
24.2k
}
127
128
extern "C" int
129
LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
130
24.2k
{
131
24.2k
#ifndef _WIN32
132
    // Used by jpeg library to work around false positives in memory
133
    // sanitizer.
134
24.2k
    setenv("JSIMD_FORCENONE", "1", 1);
135
24.2k
#endif
136
24.2k
    FuzzHelper f(data, size);
137
24.2k
    f.run();
138
24.2k
    return 0;
139
24.2k
}