This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++ PATCH] Fix -fsanitize={null,alignment} of references (PR c++/79572)


On Fri, Nov 24, 2017 at 03:26:11PM +0100, Jakub Jelinek wrote:
> On Fri, Nov 24, 2017 at 05:16:27PM +0300, Maxim Kuvyrkov wrote:
> > Using __builtin_printf causes this test to fail sporadically when
> > cross-testing.  Stdout and stderr output can get mixed in
> > cross-testing, so dejagnu might see
> > ==
> > g++.dg/ubsan/null-8.C:18:7: runtime error: reference binding to null
> > pointer of type iref is NULL
> > 'const int'
> > ==
> > instead of
> > ==
> > g++.dg/ubsan/null-8.C:18:7: runtime error: reference binding to null
> > pointer of type 'const int'
> > iref is NULL
> > ==
> > 
> > Is it essential for this testcase to use __builtin_printf or simple
> > "fprintf (stderr, ...)" would do just fine?
> 
> That would mean bringing in stdio.h, which is very much undesirable.
> 
> If you want, just revert the patch, verify the testcase FAILs,
> and then tweak it to say:
> __attribute__((noinline, noclone))
> void
> bar (const *x, int y)
> {
>   asm volatile ("" : : "g" (x), "g" (y) : "memory");
> }
> 
> and change __builtin_printf ("iref %d\n", iref);
> to bar ("iref %d\n", iref);
> and __builtin_printf ("iref is NULL\n");
> to bar ("iref is NULL\n", 0);
> If the test still FAILs and is fixed after you reapply the patch,
> the change is preapproved.

Verified myself:
./cc1plus.246620 -O0 -quiet -fsanitize=null -std=c++14 null-8.C; g++ -fsanitize=undefined -o null-8{,.s}; ./null-8
./cc1plus.246621 -O0 -quiet -fsanitize=null -std=c++14 null-8.C; g++ -fsanitize=undefined -o null-8{,.s}; ./null-8
null-8.C:25:7: runtime error: reference binding to null pointer of type 'const int'
./cc1plus.246620 -O2 -quiet -fsanitize=null -std=c++14 null-8.C; g++ -fsanitize=undefined -o null-8{,.s}; ./null-8
./cc1plus.246621 -O2 -quiet -fsanitize=null -std=c++14 null-8.C; g++ -fsanitize=undefined -o null-8{,.s}; ./null-8
null-8.C:25:7: runtime error: reference binding to null pointer of type 'const int'

Committed to trunk:

2017-11-27  Jakub Jelinek  <jakub@redhat.com>

	* g++.dg/ubsan/null-8.C (bar): New function.
	(foo): Use bar instead of __builtin_printf.

--- gcc/testsuite/g++.dg/ubsan/null-8.C.jj	2017-03-31 20:38:44.000000000 +0200
+++ gcc/testsuite/g++.dg/ubsan/null-8.C	2017-11-27 11:27:17.311529667 +0100
@@ -3,13 +3,20 @@
 // { dg-options "-fsanitize=null -std=c++14" }
 // { dg-output "reference binding to null pointer of type 'const int'" }
 
+__attribute__((noinline, noclone))
+void
+bar (int x)
+{
+  asm volatile ("" : : "r" (x) : "memory");
+}
+
 void
 foo (const int &iref)
 {
   if (&iref)
-    __builtin_printf ("iref %d\n", iref);
+    bar (iref);
   else
-    __builtin_printf ("iref is NULL\n");
+    bar (1);
 }
 
 int


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]