Coverage Report

Created: 2025-07-14 06:20

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