Add new builtin __SANITIZE_UNDEFINED__ macros for fsanitize=undefined switch
diff mbox series

Message ID 20190921113110.14188-1-n54@gmx.com
State New
Headers show
Series
  • Add new builtin __SANITIZE_UNDEFINED__ macros for fsanitize=undefined switch
Related show

Commit Message

Kamil Rytarowski Sept. 21, 2019, 11:31 a.m. UTC
GCC version of https://reviews.llvm.org/D52386

2019-09-21  Kamil Rytarowski  <n54@gmx.com>

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

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

--
2.23.0

Comments

Jakub Jelinek Sept. 21, 2019, 2:51 p.m. UTC | #1
On Sat, Sep 21, 2019 at 01:31:10PM +0200, Kamil Rytarowski wrote:
> GCC version of https://reviews.llvm.org/D52386
> 
> 2019-09-21  Kamil Rytarowski  <n54@gmx.com>
> 
> 	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
> 	builtin __SANITIZE_UNDEFINED__ macros for fsanitize=undefined switch.
> 	* doc/cpp.texi: Document new macros.
> 
> 	* c-c++-common/lsan/sanitize-undefined-macro.c: New test.

I fail to see a good use case for this, it will just lead people to add
workarounds for ubsan diagnostics without actually fixing it for real in
their production code.  Also, -fsanitize=undefined is a collection of many
different sanitizers.  Those that want such macros to say provide libubsan
definitions on their own can do that in their build system, next to
-fsanitize=undefined provide their own -Dwhatever that will make that
happen.

	Jakub
Kamil Rytarowski Sept. 21, 2019, 3:30 p.m. UTC | #2
On 21.09.2019 16:51, Jakub Jelinek wrote:
> On Sat, Sep 21, 2019 at 01:31:10PM +0200, Kamil Rytarowski wrote:
>> GCC version of https://reviews.llvm.org/D52386
>>
>> 2019-09-21  Kamil Rytarowski  <n54@gmx.com>
>>
>> 	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
>> 	builtin __SANITIZE_UNDEFINED__ macros for fsanitize=undefined switch.
>> 	* doc/cpp.texi: Document new macros.
>>
>> 	* c-c++-common/lsan/sanitize-undefined-macro.c: New test.
> 
> I fail to see a good use case for this, it will just lead people to add
> workarounds for ubsan diagnostics without actually fixing it for real in
> their production code.

It's needed in real production code in the NetBSD kernel/userland as
there are legitimate code paths that are not UBSan clean.

There is also a UBSan specific header that can be picked with this ifdef.

>  Also, -fsanitize=undefined is a collection of many
> different sanitizers.

I wrote a full UBSan kerne&userland&in-libc runtime so I am aware about
this, however I don't have a use case on fine-grained processor checks.

>  Those that want such macros to say provide libubsan
> definitions on their own can do that in their build system, next to
> -fsanitize=undefined provide their own -Dwhatever that will make that
> happen.

Do you see point in e.g. __SANITIZER_ADDRESS__ ?

This macro is intended to be used in public NetBSD system headers (as we
need it there) and you volunteer to patch scons, cmake, bmake, gmake,
gn, autotools etc for us?

> 
> 	Jakub
>

Patch
diff mbox series

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 7b773167433..4f99190ca0c 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@ 
+2019-09-21  Kamil Rytarowski  <n54@gmx.com>
+
+	* cppbuiltin.c (define_builtin_macros_for_compilation_flags): Add new
+	builtin __SANITIZE_UNDEFINED__ macros for fsanitize=undefined switch.
+	* doc/cpp.texi: Document new macros.
+
 2019-09-20  Jonas Pfeil  <jonas.pfeil@uli-ulm.de>

 	* config/microblaze/microblaze.h (ASM_OUTPUT_SKIP): Use
diff --git a/gcc/cppbuiltin.c b/gcc/cppbuiltin.c
index 60e5bedc366..221fb68b575 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_UNDEFINED)
+    cpp_define (pfile, "__SANITIZE_UNDEFINED__");
+
   if (flag_sanitize & SANITIZE_THREAD)
     cpp_define (pfile, "__SANITIZE_THREAD__");

diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi
index f2de39a270c..3145d3ebab4 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_UNDEFINED__
+This macro is defined, with value 1, when @option{-fsanitize=undefined} 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..e35880b1b2f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@ 
+2019-09-21  Kamil Rytarowski  <n54@gmx.com>
+
+	* c-c++-common/usan/sanitize-undefined-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/ubsan/sanitize-ubsan-macro.c b/gcc/testsuite/c-c++-common/ubsan/sanitize-ubsan-macro.c
new file mode 100644
index 00000000000..ecca41da32e
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/sanitize-ubsan-macro.c
@@ -0,0 +1,12 @@ 
+/* Check that -fsanitize=undefined options defines __SANITIZE_UNDEFINED__ macros.  */
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O0" } } */
+
+int
+main ()
+{
+#ifndef __SANITIZE_UNDEFINED__
+  bad construction
+#endif
+  return 0;
+}