Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/workben/fodt2pdffuzzer.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <tools/stream.hxx>
11
#include <vcl/FilterConfigItem.hxx>
12
#include <com/sun/star/awt/XToolkit.hpp>
13
#include <com/sun/star/ucb/XContentProvider.hpp>
14
#include <com/sun/star/ucb/XUniversalContentBroker.hpp>
15
#include <libxml/parser.h>
16
#include "commonfuzzer.hxx"
17
18
extern "C" bool TestPDFExportFODT(SvStream& rStream);
19
20
0
static void silent_error_func(void*, const char* /*format*/, ...) {}
21
22
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
23
13
{
24
13
    if (__lsan_disable)
25
0
        __lsan_disable();
26
27
13
    CommonInitialize(argc, argv);
28
29
    // initialise unconfigured UCB:
30
13
    css::uno::Reference<css::ucb::XUniversalContentBroker> xUcb(
31
13
        comphelper::getProcessServiceFactory()->createInstance(
32
13
            "com.sun.star.ucb.UniversalContentBroker"),
33
13
        css::uno::UNO_QUERY_THROW);
34
13
    css::uno::Sequence<css::uno::Any> aArgs{ css::uno::Any(OUString("NoConfig")) };
35
13
    css::uno::Reference<css::ucb::XContentProvider> xFileProvider(
36
13
        comphelper::getProcessServiceFactory()->createInstanceWithArguments(
37
13
            "com.sun.star.ucb.FileContentProvider", aArgs),
38
13
        css::uno::UNO_QUERY_THROW);
39
13
    xUcb->registerContentProvider(xFileProvider, "file", true);
40
41
    // create and hold a reference to XToolkit here to avoid the lsan warning about its leak
42
    // due to getting created in the unusual case of no vcl main loop
43
13
    static css::uno::Reference<css::awt::XToolkit> xTk(
44
13
        comphelper::getProcessServiceFactory()->createInstance("com.sun.star.awt.Toolkit"),
45
13
        css::uno::UNO_QUERY_THROW);
46
47
13
    if (__lsan_enable)
48
0
        __lsan_enable();
49
50
13
    xmlInitParser();
51
13
    xmlSetGenericErrorFunc(nullptr, silent_error_func);
52
53
13
    return 0;
54
13
}
55
56
extern "C" size_t LLVMFuzzerMutate(uint8_t* Data, size_t Size, size_t MaxSize);
57
58
extern "C" {
59
6.25k
__attribute__((weak)) void __msan_unpoison(const volatile void*, size_t) {}
60
}
61
62
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* Data, size_t Size, size_t MaxSize,
63
                                          unsigned int /*Seed*/)
64
0
{
65
0
    size_t Ret = LLVMFuzzerMutate(Data, Size, MaxSize);
66
67
0
    __msan_unpoison(Data, Ret);
68
69
    // an effort to only generate valid xml, in this fuzzer we only really care
70
    // about the deeper levels of turning valid input into writer layout and
71
    // pdf export
72
73
0
    xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
74
0
    xmlDocPtr Doc = xmlCtxtReadMemory(ctxt, reinterpret_cast<const char*>(Data), Ret, nullptr,
75
0
                                      nullptr, XML_PARSE_NONET);
76
0
    if (Doc == nullptr)
77
0
        Ret = 0;
78
0
    else
79
0
        xmlFreeDoc(Doc);
80
0
    xmlFreeParserCtxt(ctxt);
81
0
    xmlResetLastError();
82
0
    return Ret;
83
0
}
84
85
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86
11.1k
{
87
11.1k
    SvMemoryStream aStream(const_cast<uint8_t*>(data), size, StreamMode::READ);
88
11.1k
    bool bFODTLoaded = TestPDFExportFODT(aStream);
89
    // if the fodt didn't load then reject so that input will not be added to the corpus
90
    // we're not interested in input that doesn't go on to exercise the pdf export
91
11.1k
    return bFODTLoaded ? 0 : -1;
92
11.1k
}
93
94
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */