On 2026-07-20 15:18, Richard Earnshaw (foss) wrote:
On 15/07/2026 06:04, Torbjörn SVENSSON wrote:
Hi Richard,

I've updated to your sugestion, but used exit(0) instead of _exit(0) as
'exit' is defined in the stdlib.h header file, while '_exit' is not.
If we use '_exit', we need to have a function prototype too.
Including stdlib.h, or stddef.h, is required for 'NULL' regardless.

Ok for trunk, releases/gcc-16 and releases/gcc-15?


Hmm, actually I think we should use __builtin_exit(), so we don't need to 
declare it.  This saves a lot of
header pre-processing when compiling the test as now we no-longer need 
stddef.h.  Many tests call __builtin_abort() rather than abort() for the same 
reason.

But equally, I suspect we could just return 0 from main and let the normal 
program termination code handle the exit code that way.

Either way avoids the need to include stddef.h.

I ended up with `return 0;`. Note, I also changed `read_mem (NULL);` to 
`read_mem (0);` to get rid of the include.

OK with one or other of those changes.

Pushed as r17-2559-g2e26607f7f14bd, r16-9314-g307954e9c03aba and 
r15-11398-gcf69dd0145e174.

Kind regards,
Torbjörn


R.

Kind regards,
Torbjörn
--

As arm-none-eabi targets might have readable memory at address 0,
g++.dg/torture/pr101373.C test will "work" on some targets, while others
might tigger a fault. To avoid the ambiguity, lest skip the test if
target allows null to be dereferenced.

        PR testsuite/126261

gcc/ChangeLog:

        * doc/sourcebuild.texi (can_deref_null): Document.

gcc/testsuite/ChangeLog:

        * g++.dg/torture/pr101373.C: Use effective-target can_deref_null.
        * lib/target-supports.exp (check_effective_target_can_deref_null):
        New proc.

Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
  gcc/doc/sourcebuild.texi                |  4 ++++
  gcc/testsuite/g++.dg/torture/pr101373.C |  1 +
  gcc/testsuite/lib/target-supports.exp   | 21 +++++++++++++++++++++
  3 files changed, 26 insertions(+)

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index 0e08ef5e645..e2530cf159d 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2739,6 +2739,10 @@ Target supports the execution of @code{amx-movrs} 
instructions.
  @item amx_fp8
  Target supports the execution of @code{amx-fp8} instructions.
+@item can_deref_null
+Target runtime permits dereferencing a null pointer without trapping, for
+example because address zero is mapped and readable.
+
  @item cell_hw
  Test system can execute AltiVec and Cell PPU instructions.
diff --git a/gcc/testsuite/g++.dg/torture/pr101373.C b/gcc/testsuite/g++.dg/torture/pr101373.C
index f8c809739e2..45f1e455eec 100644
--- a/gcc/testsuite/g++.dg/torture/pr101373.C
+++ b/gcc/testsuite/g++.dg/torture/pr101373.C
@@ -1,5 +1,6 @@
  // { dg-do run }
  // { dg-xfail-run-if "PR100409" { *-*-* } }
+// { dg-skip-if "PR126261" { can_deref_null } }
int __attribute__((const,noipa)) foo (int j)
  {
diff --git a/gcc/testsuite/lib/target-supports.exp 
b/gcc/testsuite/lib/target-supports.exp
index edd1290bd9e..9e4966cff0f 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -15134,3 +15134,24 @@ proc check_effective_target_memtag_exec {} {
        }
      return 1;
  }
+
+# Return 1 if the target runtime can deref null, 0 otherwise.
+# Cache the result.
+
+proc check_effective_target_can_deref_null { } {
+    return [check_runtime can_deref_null {
+       #include <stdlib.h>
+       char __attribute__((noipa))
+       read_mem (volatile char *p)
+       {
+         return *p;
+       }
+
+       int
+       main ()
+       {
+         read_mem (NULL);
+         exit (0);
+       }
+      } "-O0 -fno-delete-null-pointer-checks -Wno-null-dereference"]
+}


Reply via email to