On 14/07/2026 22:03, Torbjörn SVENSSON wrote:
Is there any better way to test if the target will trap or not on the deref?

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

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   | 22 ++++++++++++++++++++++
  3 files changed, 27 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..79f25e3cee3 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -15134,3 +15134,25 @@ 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 <stddef.h>
+       __attribute__((noipa)) static volatile int *
+       get_null (void)
+       {
+         return (volatile int *) NULL;
+       }
+
+       int main ()
+       {
+         volatile int *p = get_null ();
+         int x = *p;
+         (void) x;
+         return 0;
+       }
+      } "-O0"]
+}

That's pretty close to what I would have written, which would have been something like

char __attribute__((noipa))
read_mem (volatile char *p)
{
  return *p;
}

int main ()
{
  read_mem (NULL);
  _exit (0);
}

and then, to be on the safe side, add -fno-delete-null-pointer-checks and -Wno-null-dereference to the flags.

R.

Reply via email to