From 389788a7ee44b1a0dfa8bc479967929c0ccfa684 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 29 Aug 2024 22:52:02 +0200 Subject: [PATCH] vma-prot: Add tests. * tests/test-vma-prot.c: New file. * modules/vma-prot-tests: New file. --- ChangeLog | 4 ++ modules/vma-prot-tests | 13 +++++ tests/test-vma-prot.c | 107 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 modules/vma-prot-tests create mode 100644 tests/test-vma-prot.c diff --git a/ChangeLog b/ChangeLog index 709509e07a..02a11b3629 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-08-29 Bruno Haible + 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 index 0000000000..61d060133d --- /dev/null +++ b/modules/vma-prot-tests @@ -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 index 0000000000..24b5b1873a --- /dev/null +++ b/tests/test-vma-prot.c @@ -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 . */ + +/* Written by Bruno Haible , 2024. */ + +#include + +#include "vma-prot.h" + +#include + +#if HAVE_MAP_ANONYMOUS +# include +# include +#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; +} -- 2.39.5