File indexing completed on 2024-05-12 04:39:14

0001 /*
0002     SPDX-FileCopyrightText: 2022 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "sanitizer_test_init.h"
0008 
0009 #include <QString>
0010 #include <QtGlobal>
0011 
0012 #if defined(Q_OS_UNIX)
0013 #include <unistd.h>
0014 #endif
0015 
0016 namespace KDevelop
0017 {
0018 namespace
0019 {
0020 #if defined(Q_OS_UNIX)
0021     /**
0022      * LLVM and ASAN don't play well together, see [1].
0023      * We have to disable sigaltstack or we crash.
0024      * The code below detects whether we need to disable this option.
0025      * If yes, we change the environment and re-execute ourselves with the new
0026      * environment. Sadly, it doesn't seem to work to use __asan_default_options
0027      * for this purpose here...
0028      *
0029      * [1]: https://github.com/google/sanitizers/issues/849
0030      */
0031     [[maybe_unused]] void setupAsan(char** argv)
0032     {
0033         auto asanOptions = qEnvironmentVariable("ASAN_OPTIONS");
0034         if (asanOptions.contains(QLatin1String("use_sigaltstack"))) {
0035             return; // nothing to do
0036         }
0037 
0038         asanOptions += QLatin1String(",use_sigaltstack=0");
0039         qputenv("ASAN_OPTIONS", asanOptions.toUtf8());
0040 
0041         execv(argv[0], argv);
0042     }
0043 #endif
0044 }
0045 
0046 void sanitizerTestInit([[maybe_unused]] char** argv)
0047 {
0048 #if defined(Q_OS_UNIX)
0049 
0050 // gcc
0051 #if defined(__SANITIZE_ADDRESS__)
0052     setupAsan(argv);
0053     return;
0054 #endif
0055 
0056 // clang
0057 #if defined(__has_feature)
0058 #if __has_feature(address_sanitizer)
0059     setupAsan(argv);
0060     return;
0061 #endif
0062 #endif
0063 
0064 #endif
0065 }
0066 }