[PATCH] support: add check_mem_access

Yury Khrustalev yury.khrustalev@arm.com
Tue Sep 9 14:23:46 GMT 2025


Add check_mem_access(addr) function to check if memory at addr can
be written or read returning false if memory is not accessible.
---
base-commit: 5c522d7a58
---
 nptl/tst-guard1.c            |  7 ++++---
 support/Makefile             |  1 +
 support/check_mem_access.h   | 12 ++++++++++++
 support/support_mem_access.c | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 support/check_mem_access.h
 create mode 100644 support/support_mem_access.c

diff --git a/nptl/tst-guard1.c b/nptl/tst-guard1.c
index e3e06df0fc..3a3e9c5c75 100644
--- a/nptl/tst-guard1.c
+++ b/nptl/tst-guard1.c
@@ -26,6 +26,7 @@
 #include <support/xsignal.h>
 #include <support/xthread.h>
 #include <support/xunistd.h>
+#include <support/check_mem_access.h>
 #include <sys/mman.h>
 #include <stdlib.h>
 
@@ -185,9 +186,9 @@ tf (void *closure)
     }
 
   /* Ensure we can access the stack area.  */
-  TEST_COMPARE (try_read_buf (s.stack), true);
-  TEST_COMPARE (try_read_buf (&s.stack[s.stacksize / 2]), true);
-  TEST_COMPARE (try_read_buf (&s.stack[s.stacksize - 1]), true);
+  TEST_COMPARE (check_mem_access (s.stack, false), true);
+  TEST_COMPARE (check_mem_access (&s.stack[s.stacksize / 2], false), true);
+  TEST_COMPARE (check_mem_access (&s.stack[s.stacksize - 1], false), true);
 
   /* Check if accessing the guard area results in SIGSEGV.  */
   if (s.guardsize > 0)
diff --git a/support/Makefile b/support/Makefile
index f67f38130a..f0a1e1ca44 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -66,6 +66,7 @@ libsupport-routines = \
   support_format_netent \
   support_fuse \
   support_isolate_in_subprocess \
+  support_mem_access \
   support_mutex_pi_monotonic \
   support_need_proc \
   support_open_and_compare_file_bytes \
diff --git a/support/check_mem_access.h b/support/check_mem_access.h
new file mode 100644
index 0000000000..cd5ea07c9e
--- /dev/null
+++ b/support/check_mem_access.h
@@ -0,0 +1,12 @@
+#ifndef SUPPORT_CHECK_MEM_ACCESS_H
+#define SUPPORT_CHECK_MEM_ACCESS_H
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+bool check_mem_access (const void *addr, bool write);
+
+__END_DECLS
+
+#endif // SUPPORT_CHECK_MEM_ACCESS_H
diff --git a/support/support_mem_access.c b/support/support_mem_access.c
new file mode 100644
index 0000000000..5a11a476a2
--- /dev/null
+++ b/support/support_mem_access.c
@@ -0,0 +1,33 @@
+
+#include <setjmp.h>
+#include <signal.h>
+
+static sigjmp_buf sigsegv_jmp_buf;
+
+static void
+__sigsegv_handler (int signum)
+{
+  siglongjmp (sigsegv_jmp_buf, signum);
+}
+
+bool check_mem_access (const void *addr, bool write)
+{
+  static bool handler_set_up;
+  if (!handler_set_up)
+    {
+      signal (SIGSEGV, __sigsegv_handler);
+      handler_set_up = true;
+    }
+  int r = sigsetjmp (sigsegv_jmp_buf, 0);
+  if (r == 0)
+    {
+      if (write)
+        *(volatile char *)addr = 'x';
+      else
+        *(volatile char *)addr;
+      return false;
+    }
+  if (r == SIGSEGV)
+    return true;
+  return false;
+}
-- 
2.47.3



More information about the Libc-alpha mailing list