1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
11 #include <tools/extendapplicationenvironment.hxx>
13 #include <cppuhelper/bootstrap.hxx>
14 #include <comphelper/processfactory.hxx>
16 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
17 #include <com/sun/star/uno/XComponentContext.hpp>
18 #include <unotools/configmgr.hxx>
19 #include <rtl/bootstrap.hxx>
20 #include <rtl/strbuf.hxx>
21 #include <osl/file.hxx>
22 #include <osl/process.h>
23 #include <vcl/graph.hxx>
24 #include <vcl/print.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/wmf.hxx>
29 #include "headless/svpgdi.hxx"
30 #include "unx/fontmanager.hxx"
31 #include "unx/glyphcache.hxx"
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
39 OUString
getExecutableDir()
42 if (osl_getExecutableFile(&uri
.pData
) != osl_Process_E_None
) {
45 sal_Int32 lastDirSeparatorPos
= uri
.lastIndexOf('/');
46 if (lastDirSeparatorPos
>= 0) {
47 uri
= uri
.copy(0, lastDirSeparatorPos
+ 1);
52 OUString
getExecutableName()
55 if (osl_getExecutableFile(&uri
.pData
) != osl_Process_E_None
) {
58 return uri
.copy(uri
.lastIndexOf('/') + 1);
61 void setFontConfigConf(const OUString
&execdir
)
63 osl::File
aFontConfig("file:///tmp/wmffuzzerfonts.conf");
64 if (aFontConfig
.open(osl_File_OpenFlag_Create
| osl_File_OpenFlag_Write
) == osl::File::E_None
)
67 osl::FileBase::getSystemPathFromFileURL(execdir
, sExecDir
);
69 OStringBuffer
aBuffer("<?xml version=\"1.0\"?>\n<fontconfig><dir>");
70 aBuffer
.append(OUStringToOString(sExecDir
, osl_getThreadTextEncoding()))
71 .append(OUStringToOString(getExecutableName(), osl_getThreadTextEncoding())).append(".fonts");
72 aBuffer
.append("</dir><cachedir>/tmp/cache/fontconfig</cachedir></fontconfig>");
73 OString aConf
= aBuffer
.makeStringAndClear();
74 sal_uInt64 aBytesWritten
;
75 aFontConfig
.write(aConf
.getStr(), aConf
.getLength(), aBytesWritten
);
76 assert(aBytesWritten
== aConf
.getLength());
78 setenv("FONTCONFIG_FILE", "/tmp/wmffuzzerfonts.conf", 0);
84 __attribute__((weak
)) void __lsan_disable();
85 __attribute__((weak
)) void __lsan_enable();
88 void CommonInitialize(int *argc
, char ***argv
)
90 setenv("SAL_USE_VCLPLUGIN", "svp", 1);
91 setenv("JPEGMEM", "768M", 1);
92 setenv("JSIMD_FORCENONE", "1", 1); // https://github.com/libjpeg-turbo/libjpeg-turbo/issues/253
93 setenv("SC_MAX_MATRIX_ELEMENTS", "60000000", 1);
94 setenv("SC_NO_THREADED_CALCULATION", "1", 1);
95 setenv("SAL_DISABLE_PRINTERLIST", "1", 1);
96 setenv("SAL_DISABLE_DEFAULTPRINTER", "1", 1);
97 setenv("SAL_NO_FONT_LOOKUP", "1", 1);
98 setenv("SAX_DISABLE_THREADS", "1", 1);
100 //allow bubbling of max input len to fuzzer targets
102 for (int i
= 0; i
< *argc
; ++i
)
104 if (strncmp((*argv
)[i
], "-max_len=", 9) == 0)
105 nMaxLen
= atoi((*argv
)[i
] + 9);
107 setenv("FUZZ_MAX_INPUT_LEN", "1", nMaxLen
);
109 osl_setCommandArgs(*argc
, *argv
);
111 OUString sExecDir
= getExecutableDir();
112 rtl::Bootstrap::set("BRAND_BASE_DIR", sExecDir
);
113 setFontConfigConf(sExecDir
);
115 tools::extendApplicationEnvironment();
117 Reference
< XComponentContext
> xContext
=
118 defaultBootstrap_InitialComponentContext(sExecDir
+ getExecutableName() + ".unorc");
119 Reference
< XMultiServiceFactory
> xServiceManager( xContext
->getServiceManager(), UNO_QUERY
);
120 if( !xServiceManager
.is() )
121 Application::Abort( "Failed to bootstrap" );
122 comphelper::setProcessServiceFactory( xServiceManager
);
123 utl::ConfigManager::EnableFuzzing();
124 Application::EnableHeadlessMode(false);
127 //we don't have a de-init, so inside this leak disabled region...
129 psp::PrintFontManager::get();
130 //get the printer info
131 Printer::GetPrinterQueues();
133 //https://github.com/google/oss-fuzz/issues/1449
134 //https://github.com/google/oss-fuzz/issues/5441
135 //release the solarmutex so a fork can acquire it which should
136 //allow these fuzzers to work without AFL_DRIVER_DONT_DEFER set
137 //removing the confusion of #5441 and the need for AFL_DRIVER_DONT_DEFER
139 Application::ReleaseSolarMutex();
142 void TypicalFuzzerInitialize(int *argc
, char ***argv
)
147 CommonInitialize(argc
, argv
);
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */