aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2022-04-07 12:31:13 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2022-04-11 07:15:33 +0000
commit967b5e73a5c157a0b650dd389722d4bc2cec03e5 (patch)
treef182040e8d891a82aa756b8b7fd23d41bf87456c
parentf0800b40df8ee152d522a1cd40b7ad1a8cf03f4b (diff)
Add module for providing AddressSanitizer support
Change-Id: Id34e57a59ad21961ee32da1bf93f6a55596d95aa Reviewed-by: Ivan Komissarov <ABBAPOH@gmail.com>
-rw-r--r--doc/reference/modules/asan-module.qdoc77
-rw-r--r--share/qbs/module-providers/Qt/templates/core.qbs6
-rw-r--r--share/qbs/modules/Sanitizers/address/asan.qbs75
-rw-r--r--tests/auto/blackbox/testdata/sanitizer/sanitizer.qbs10
-rw-r--r--tests/auto/blackbox/tst_blackbox.cpp4
5 files changed, 165 insertions, 7 deletions
diff --git a/doc/reference/modules/asan-module.qdoc b/doc/reference/modules/asan-module.qdoc
new file mode 100644
index 000000000..6f6ee66c0
--- /dev/null
+++ b/doc/reference/modules/asan-module.qdoc
@@ -0,0 +1,77 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qbs.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \qmltype Sanitizers.address
+ \inqmlmodule QbsModules
+ \brief Provides AddressSanitizer support.
+
+ Provides support for the \l{https://clang.llvm.org/docs/AddressSanitizer.html}{AddressSanitizer}
+ tool, which uses compiler instrumentation to detect memory corruption bugs at runtime.
+*/
+
+
+/*!
+ \qmlproperty string Sanitizers.address::detectUseAfterReturn
+
+ Controls whether support for detecting problems with stack use after returning
+ from a function should be built into the application.
+
+ The possible values are:
+ \list
+ \li \c "always": Check for this type of problem unconditionally.
+ \li \c "runtime": Build with this capability, but only do the respective checks if they
+ are explicitly requested at runtime.
+ \li \c "never": Do not build with support for this check.
+ \endlist
+
+ \note Only the clang compiler (starting at 13.0) supports the full set of values.
+ With GCC and MSVC, "runtime" maps to "always".
+
+ \defaultvalue \c "always"
+*/
+
+/*!
+ \qmlproperty bool Sanitizers.address::detectUseAfterScope
+
+ Controls whether to detect bugs relating the use of variables after they've gone out of scope.
+
+ \note This property has no effect when building with with GCC and MSVC.
+
+ \defaultvalue \c true
+*/
+
+/*!
+ \qmlproperty bool Sanitizers.address::enabled
+
+ Controls whether to enable AddressSanitizer.
+
+ \note You do not need to check if the current toolchain supports AddressSanitizer:
+ If \QBS detects that it doesn't, this property has no effect.
+
+ \defaultvalue \c true
+*/
diff --git a/share/qbs/module-providers/Qt/templates/core.qbs b/share/qbs/module-providers/Qt/templates/core.qbs
index fd81930ba..50d68d3a1 100644
--- a/share/qbs/module-providers/Qt/templates/core.qbs
+++ b/share/qbs/module-providers/Qt/templates/core.qbs
@@ -18,6 +18,7 @@ Module {
&& qbs.targetPlatform === targetPlatform + "-simulator"
Depends { name: "cpp" }
+ Depends { name: "Sanitizers.address" }
Depends { name: "Qt.android_support"; condition: qbs.targetOS.contains("android") }
Properties {
@@ -118,6 +119,9 @@ Module {
property bool lreleaseMultiplexMode: false
property stringList moduleConfig: @moduleConfig@
+
+ Sanitizers.address.enabled: config.contains("sanitize_address")
+
Properties {
condition: moduleConfig.contains("use_gold_linker")
cpp.linkerVariant: "gold"
@@ -151,8 +155,6 @@ Module {
cpp.driverFlags: {
var flags = [];
if (qbs.toolchain.contains("gcc")) {
- if (config.contains("sanitize_address"))
- flags.push("-fsanitize=address");
if (config.contains("sanitize_undefined"))
flags.push("-fsanitize=undefined");
if (config.contains("sanitize_thread"))
diff --git a/share/qbs/modules/Sanitizers/address/asan.qbs b/share/qbs/modules/Sanitizers/address/asan.qbs
new file mode 100644
index 000000000..39605ef4e
--- /dev/null
+++ b/share/qbs/modules/Sanitizers/address/asan.qbs
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing
+**
+** This file is part of Qbs.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms and
+** conditions see http://www.qt.io/terms-conditions. For further information
+** use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+import qbs.Utilities
+
+Module {
+ Depends { name: "cpp" }
+
+ property bool enabled: true
+ readonly property bool _supported: qbs.toolchain.contains("gcc")
+ || qbs.toolchain.contains("clang-cl")
+ || (qbs.toolchain.contains("msvc")
+ && Utilities.versionCompare(cpp.compilerVersion, "19.28.29500.0") >= 0)
+ readonly property bool _enabled: enabled && _supported
+
+ property string detectUseAfterReturn: "always"
+ PropertyOptions {
+ name: "detectUseAfterReturn"
+ description: "Whether to detect problems with stack use after return from a function"
+ allowedValues: ["never", "runtime", "always"]
+ }
+
+ property bool detectUseAfterScope: true
+
+ cpp.driverFlags: {
+ var flags = [];
+ if (!_enabled)
+ return flags;
+ if (qbs.toolchain.contains("msvc") && !qbs.toolchain.contains("clang-cl")) {
+ flags.push("/fsanitize=address");
+ if (detectUseAfterReturn !== "never")
+ flags.push("/fsanitize-address-use-after-return");
+ return flags;
+ }
+ flags.push("-fsanitize=address", "-fno-omit-frame-pointer");
+ if (detectUseAfterScope)
+ flags.push("-fsanitize-address-use-after-scope");
+ if (detectUseAfterReturn) {
+ if (qbs.toolchain.contains("llvm")) {
+ if (Utilities.versionCompare(cpp.compilerVersion, "13") >= 0)
+ flags.push("-fsanitize-address-use-after-return=" + detectUseAfterReturn);
+ } else if (detectUseAfterReturn === "never") {
+ flags.push("--param", "asan-use-after-return=0");
+ }
+ }
+ return flags;
+ }
+}
diff --git a/tests/auto/blackbox/testdata/sanitizer/sanitizer.qbs b/tests/auto/blackbox/testdata/sanitizer/sanitizer.qbs
index 0aa7f3e87..9126a7913 100644
--- a/tests/auto/blackbox/testdata/sanitizer/sanitizer.qbs
+++ b/tests/auto/blackbox/testdata/sanitizer/sanitizer.qbs
@@ -1,7 +1,13 @@
CppApplication {
+ Depends { name: "Sanitizers.address" }
+ Sanitizers.address.enabled: sanitizer === "address"
property string sanitizer
property bool supportsSanitizer: {
+ if (qbs.toolchain.contains("mingw"))
+ return false;
+ if (sanitizer === "address")
+ return Sanitizers.address._supported;
if (qbs.toolchain.contains("clang-cl")) {
if (cpp.toolchainInstallPath.contains("Microsoft Visual Studio")
&& qbs.architecture === "x86_64") {
@@ -13,8 +19,6 @@ CppApplication {
}
if (!qbs.toolchain.contains("gcc"))
return false;
- if (qbs.toolchain.contains("mingw"))
- return false;
if (qbs.targetOS.contains("ios")) {
// thread sanitizer is not supported
return sanitizer !== "thread";
@@ -34,7 +38,7 @@ CppApplication {
cpp.minimumMacosVersion: "10.8"
consoleApplication: true
cpp.runtimeLibrary: "static"
- cpp.driverFlags: sanitizer ? ["-fsanitize=" + sanitizer] : []
+ cpp.driverFlags: sanitizer && sanitizer !== "address" ? ["-fsanitize=" + sanitizer] : []
cpp.debugInformation: true
files: "sanitizer.cpp"
}
diff --git a/tests/auto/blackbox/tst_blackbox.cpp b/tests/auto/blackbox/tst_blackbox.cpp
index 45953a88c..bfec09344 100644
--- a/tests/auto/blackbox/tst_blackbox.cpp
+++ b/tests/auto/blackbox/tst_blackbox.cpp
@@ -2670,10 +2670,10 @@ void TestBlackbox::sanitizer()
if (m_qbsStdout.contains(QByteArrayLiteral("Compiler does not support sanitizer")))
QSKIP("Compiler does not support the specified sanitizer");
if (!sanitizer.isEmpty()) {
- QVERIFY2(m_qbsStdout.contains(QByteArrayLiteral("-fsanitize=") + sanitizer.toLatin1()),
+ QVERIFY2(m_qbsStdout.contains(QByteArrayLiteral("fsanitize=") + sanitizer.toLatin1()),
qPrintable(m_qbsStdout));
} else {
- QVERIFY2(!m_qbsStdout.contains(QByteArrayLiteral("-fsanitize=")), qPrintable(m_qbsStdout));
+ QVERIFY2(!m_qbsStdout.contains(QByteArrayLiteral("fsanitize=")), qPrintable(m_qbsStdout));
}
}