2019-09-20 Kamil Rytarowski <n54@gmx.com>
diff mbox series

Message ID 20190920204541.610-1-n54@gmx.com
State New
Headers show
Series
  • 2019-09-20 Kamil Rytarowski <n54@gmx.com>
Related show

Commit Message

Kamil Rytarowski Sept. 20, 2019, 8:45 p.m. UTC
GCC version of https://reviews.llvm.org/D67719

From 422827582d84e078df2a8e303d807c830a155ab5 Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54@gmx.com>
Date: Fri, 20 Sep 2019 22:02:09 +0200
Subject: [PATCH] 2019-09-20  Kamil Rytarowski  <n54@gmx.com>

	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
	builtin __SANITIZE_LEAK__ macros for fsanitize=leak switch.
	* doc/cpp.texi: Document new macros.

	* c-c++-common/lsan/sanitize-leak-macro.c: New test.
---
 gcc/ChangeLog                                        |  6 ++++++
 gcc/cppbuiltin.c                                     |  3 +++
 gcc/doc/cpp.texi                                     |  3 +++
 gcc/testsuite/ChangeLog                              |  4 ++++
 .../c-c++-common/lsan/sanitize-lsan-macro.c          | 12 ++++++++++++
 5 files changed, 28 insertions(+)
 create mode 100644 gcc/testsuite/c-c++-common/lsan/sanitize-lsan-macro.c

--
2.23.0

Comments

Jakub Jelinek Sept. 21, 2019, 2:53 p.m. UTC | #1
On Fri, Sep 20, 2019 at 10:45:42PM +0200, Kamil Rytarowski wrote:
> GCC version of https://reviews.llvm.org/D67719
> 
> From 422827582d84e078df2a8e303d807c830a155ab5 Mon Sep 17 00:00:00 2001
> From: Kamil Rytarowski <n54@gmx.com>
> Date: Fri, 20 Sep 2019 22:02:09 +0200
> Subject: [PATCH] 2019-09-20  Kamil Rytarowski  <n54@gmx.com>
> 
> 	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
> 	builtin __SANITIZE_LEAK__ macros for fsanitize=leak switch.
> 	* doc/cpp.texi: Document new macros.
> 
> 	* c-c++-common/lsan/sanitize-leak-macro.c: New test.

And this looks even more pointless.  -fsanitize=leak generally isn't a
compiler option, just a linker option, but a patch like this would make it a
compiler option too.  There are no code changes with -fsanitize=leak, just
linking arranges a malloc etc. replacement that traces the leaks.

	Jakub
Kamil Rytarowski Sept. 21, 2019, 3:50 p.m. UTC | #2
On 21.09.2019 16:53, Jakub Jelinek wrote:
> On Fri, Sep 20, 2019 at 10:45:42PM +0200, Kamil Rytarowski wrote:
>> GCC version of https://reviews.llvm.org/D67719
>>
>> From 422827582d84e078df2a8e303d807c830a155ab5 Mon Sep 17 00:00:00 2001
>> From: Kamil Rytarowski <n54@gmx.com>
>> Date: Fri, 20 Sep 2019 22:02:09 +0200
>> Subject: [PATCH] 2019-09-20  Kamil Rytarowski  <n54@gmx.com>
>>
>> 	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
>> 	builtin __SANITIZE_LEAK__ macros for fsanitize=leak switch.
>> 	* doc/cpp.texi: Document new macros.
>>
>> 	* c-c++-common/lsan/sanitize-leak-macro.c: New test.
> 
> And this looks even more pointless.  -fsanitize=leak generally isn't a
> compiler option, just a linker option,

This is wrong. -fsanitize=leak IS a compiler option.

> but a patch like this would make it a
> compiler option too.  There are no code changes with -fsanitize=leak, just
> linking arranges a malloc etc. replacement that traces the leaks.
> 

I am aware about its implementation as I ported LSan to NetBSD.

> 	Jakub
> 

The same argument applies here as in the UBSan patch.

Here we have got a special use-case to use standalone LSan on NetBSD we
we run whole userland sanitization and ASan is much more fragile
(largely due to its incompleteness in interceptors).

This ifdef is also scheduled to be included in public system headers.

Patch
diff mbox series

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 76269e17eb0..1bddb4835a0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@ 
+2019-09-20  Kamil Rytarowski  <n54@gmx.com>
+
+	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
+	builtin __SANITIZE_LEAK__ macros for fsanitize=leak switch.
+	* doc/cpp.texi: Document new macros.
+
 2019-09-20  Richard Biener  <rguenther@suse.de>
 	Uros Bizjak  <ubizjak@gmail.com>

diff --git a/gcc/cppbuiltin.c b/gcc/cppbuiltin.c
index 60e5bedc366..5a1b6555c53 100644
--- a/gcc/cppbuiltin.c
+++ b/gcc/cppbuiltin.c
@@ -93,6 +93,9 @@  define_builtin_macros_for_compilation_flags (cpp_reader *pfile)
   if (flag_sanitize & SANITIZE_ADDRESS)
     cpp_define (pfile, "__SANITIZE_ADDRESS__");

+  if (flag_sanitize & SANITIZE_LEAK)
+    cpp_define (pfile, "__SANITIZE_LEAK__");
+
   if (flag_sanitize & SANITIZE_THREAD)
     cpp_define (pfile, "__SANITIZE_THREAD__");

diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi
index f2de39a270c..74861eaf9c5 100644
--- a/gcc/doc/cpp.texi
+++ b/gcc/doc/cpp.texi
@@ -2359,6 +2359,9 @@  in use.
 This macro is defined, with value 1, when @option{-fsanitize=address}
 or @option{-fsanitize=kernel-address} are in use.

+@item __SANITIZE_LEAK__
+This macro is defined, with value 1, when @option{-fsanitize=leak} is in use.
+
 @item __SANITIZE_THREAD__
 This macro is defined, with value 1, when @option{-fsanitize=thread} is in use.

diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 05c25ee28ce..4b37e1f3643 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@ 
+2019-09-20  Kamil Rytarowski  <n54@gmx.com>
+
+	* c-c++-common/lsan/sanitize-leak-macro.c: New test.
+
 2019-09-20  Iain Sandoe  <iain@sandoe.co.uk>

 	* gcc.target/i386/naked-1.c: Alter options to use non-
diff --git a/gcc/testsuite/c-c++-common/lsan/sanitize-lsan-macro.c b/gcc/testsuite/c-c++-common/lsan/sanitize-lsan-macro.c
new file mode 100644
index 00000000000..c588aa32e86
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/lsan/sanitize-lsan-macro.c
@@ -0,0 +1,12 @@ 
+/* Check that -fsanitize=leak options defines __SANITIZE_LEAK__ macros.  */
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O0" } } */
+
+int
+main ()
+{
+#ifndef __SANITIZE_LEAK__
+  bad construction
+#endif
+  return 0;
+}