tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / vcl / workben / commonfuzzer.hxx
blob72939cb6656d1186097d4bb83e0fbe9c6ab123a8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/main.h>
11 #include <tools/extendapplicationenvironment.hxx>
13 #include <cppuhelper/bootstrap.hxx>
14 #include <comphelper/configuration.hxx>
15 #include <comphelper/processfactory.hxx>
17 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
18 #include <com/sun/star/uno/XComponentContext.hpp>
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>
27 #include <unistd.h>
28 #include <stdlib.h>
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;
35 using namespace cppu;
37 namespace
39 OUString getExecutableDir()
41 OUString uri;
42 if (osl_getExecutableFile(&uri.pData) != osl_Process_E_None) {
43 abort();
45 sal_Int32 lastDirSeparatorPos = uri.lastIndexOf('/');
46 if (lastDirSeparatorPos >= 0) {
47 uri = uri.copy(0, lastDirSeparatorPos + 1);
49 return uri;
52 OUString getExecutableName()
54 OUString uri;
55 if (osl_getExecutableFile(&uri.pData) != osl_Process_E_None) {
56 abort();
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)
66 OUString sExecDir;
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);
82 extern "C" bool GetSpecialCharsForEdit() { return false; }
84 extern "C"
86 __attribute__((weak)) void __lsan_disable();
87 __attribute__((weak)) void __lsan_enable();
90 void CommonInitialize(int *argc, char ***argv)
92 setenv("SAL_USE_VCLPLUGIN", "svp", 1);
93 setenv("JPEGMEM", "768M", 1);
94 setenv("JSIMD_FORCENONE", "1", 1); // https://github.com/libjpeg-turbo/libjpeg-turbo/issues/253
95 setenv("SC_MAX_MATRIX_ELEMENTS", "60000000", 1);
96 setenv("SC_NO_THREADED_CALCULATION", "1", 1);
97 setenv("SAL_DISABLE_PRINTERLIST", "1", 1);
98 setenv("SAL_DISABLE_DEFAULTPRINTER", "1", 1);
99 setenv("SAL_NO_FONT_LOOKUP", "1", 1);
100 setenv("SAX_DISABLE_THREADS", "1", 1);
102 //allow bubbling of max input len to fuzzer targets
103 int nMaxLen = 0;
104 for (int i = 0; i < *argc; ++i)
106 if (strncmp((*argv)[i], "-max_len=", 9) == 0)
107 nMaxLen = atoi((*argv)[i] + 9);
109 setenv("FUZZ_MAX_INPUT_LEN", "1", nMaxLen);
111 osl_setCommandArgs(*argc, *argv);
113 OUString sExecDir = getExecutableDir();
114 rtl::Bootstrap::set("BRAND_BASE_DIR", sExecDir);
115 setFontConfigConf(sExecDir);
117 tools::extendApplicationEnvironment();
119 Reference< XComponentContext > xContext =
120 defaultBootstrap_InitialComponentContext(sExecDir + getExecutableName() + ".unorc");
121 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
122 if( !xServiceManager.is() )
123 Application::Abort( "Failed to bootstrap" );
124 comphelper::setProcessServiceFactory( xServiceManager );
125 comphelper::EnableFuzzing();
126 Application::EnableHeadlessMode(false);
127 InitVCL();
129 //we don't have a de-init, so inside this leak disabled region...
130 //get the font info
131 psp::PrintFontManager::get();
132 //get the printer info
133 Printer::GetPrinterQueues();
135 //https://github.com/google/oss-fuzz/issues/1449
136 //https://github.com/google/oss-fuzz/issues/5441
137 //release the solarmutex so a fork can acquire it which should
138 //allow these fuzzers to work without AFL_DRIVER_DONT_DEFER set
139 //removing the confusion of #5441 and the need for AFL_DRIVER_DONT_DEFER
140 //in .options files
141 Application::ReleaseSolarMutex();
144 void TypicalFuzzerInitialize(int *argc, char ***argv)
146 if (__lsan_disable)
147 __lsan_disable();
149 CommonInitialize(argc, argv);
151 if (__lsan_enable)
152 __lsan_enable();
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */