]> Savannah Git Hosting - gnulib.git/commitdiff
vma-prot: Add tests.
authorBruno Haible <bruno@clisp.org>
Thu, 29 Aug 2024 20:52:02 +0000 (22:52 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 29 Aug 2024 21:54:34 +0000 (23:54 +0200)
* tests/test-vma-prot.c: New file.
* modules/vma-prot-tests: New file.

ChangeLog
modules/vma-prot-tests [new file with mode: 0644]
tests/test-vma-prot.c [new file with mode: 0644]

index 709509e07a05cbe47a6371cc142dbcd9e96a9e9f..02a11b3629660e9e8ebd4926c48c51c255c3f700 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-08-29  Bruno Haible  <bruno@clisp.org>
 
+       vma-prot: Add tests.
+       * tests/test-vma-prot.c: New file.
+       * modules/vma-prot-tests: New file.
+
        vma-prot: New module.
        * lib/vma-prot.h: New file.
        * lib/vma-prot.c: New file.
diff --git a/modules/vma-prot-tests b/modules/vma-prot-tests
new file mode 100644 (file)
index 0000000..61d0601
--- /dev/null
@@ -0,0 +1,13 @@
+Files:
+tests/test-vma-prot.c
+tests/macros.h
+m4/mmap-anon.m4
+
+Depends-on:
+
+configure.ac:
+gl_FUNC_MMAP_ANON
+
+Makefile.am:
+TESTS += test-vma-prot
+check_PROGRAMS += test-vma-prot
diff --git a/tests/test-vma-prot.c b/tests/test-vma-prot.c
new file mode 100644 (file)
index 0000000..24b5b18
--- /dev/null
@@ -0,0 +1,107 @@
+/* Test of getting protection of a virtual memory area.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2024.  */
+
+#include <config.h>
+
+#include "vma-prot.h"
+
+#include <stdlib.h>
+
+#if HAVE_MAP_ANONYMOUS
+# include <sys/mman.h>
+# include <unistd.h>
+#endif
+
+#include "macros.h"
+
+int dummy;
+
+int
+f (int x)
+{
+  return x;
+}
+
+int
+main (void)
+{
+  int prot;
+
+  /* Test on memory allocated through malloc().  */
+  {
+    char *mem = malloc (10);
+    prot = get_vma_prot (mem + 2, 5);
+    ASSERT (prot != -1);
+    ASSERT (((VMA_PROT_READ | VMA_PROT_WRITE) & ~prot) == 0);
+  }
+  {
+    char *mem = malloc (1000000);
+    prot = get_vma_prot (mem, 1000000);
+    ASSERT (prot != -1);
+    ASSERT (((VMA_PROT_READ | VMA_PROT_WRITE) & ~prot) == 0);
+  }
+
+#if HAVE_MAP_ANONYMOUS
+  /* Test on memory allocated through mmap().  */
+  {
+    char *mem = mmap (NULL, 1024*1024, PROT_READ | PROT_WRITE,
+                      MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+    if (mem != (char *)(-1))
+      {
+        prot = get_vma_prot (mem, 1024*1024);
+        ASSERT (prot != -1);
+        ASSERT (prot == VMA_PROT_READ | VMA_PROT_WRITE);
+
+        size_t pagesize = sysconf (_SC_PAGESIZE);
+        if (pagesize <= 512*1024
+            && munmap (mem + pagesize, pagesize) >= 0)
+          {
+            prot = get_vma_prot (mem, 1024*1024);
+            ASSERT (prot != -1);
+            ASSERT (prot == 0);
+          }
+      }
+  }
+#endif
+
+  /* Test on a global variable.  */
+  prot = get_vma_prot (&dummy, sizeof (dummy));
+  ASSERT (prot != -1);
+  ASSERT (((VMA_PROT_READ | VMA_PROT_WRITE) & ~prot) == 0);
+
+  /* Test on a function.  */
+  prot = get_vma_prot (&f, 1);
+  ASSERT (prot != -1);
+#if (defined __hppa || defined __hppa64__ \
+     || defined __ia64__ \
+     || defined _AIX \
+     || (defined __powerpc64__ && defined __linux__ && _CALL_ELF != 2))
+  /* On these platforms, a function pointer is actually a pointer to
+     a struct of pointers.  */
+#else
+  /* On these platforms, a function pointer is a pointer to or into some
+     machine instruction.  */
+  ASSERT ((VMA_PROT_EXECUTE & ~prot) == 0);
+#endif
+#if !defined __OpenBSD__
+  /* Except on OpenBSD/arm64, the machine instructions are also readable.  */
+  ASSERT ((VMA_PROT_READ & ~prot) == 0);
+#endif
+
+  return test_exit_status;
+}