| 1 | /* Check that the entire libc.so program image is readable if contiguous. |
| 2 | Copyright (C) 2025 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <gnu/lib-names.h> |
| 20 | #include <link.h> |
| 21 | #include <support/check.h> |
| 22 | #include <support/xdlfcn.h> |
| 23 | #include <support/xunistd.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | static int |
| 28 | do_test (void) |
| 29 | { |
| 30 | struct link_map *l = xdlopen (LIBC_SO, RTLD_NOW); |
| 31 | |
| 32 | /* The dynamic loader fills holes with PROT_NONE mappings. */ |
| 33 | if (!l->l_contiguous) |
| 34 | FAIL_EXIT1 ("libc.so link map is not contiguous" ); |
| 35 | |
| 36 | /* Direct probing does not work because not everything is readable |
| 37 | due to PROT_NONE mappings. */ |
| 38 | int pagesize = getpagesize (); |
| 39 | ElfW(Addr) addr = l->l_map_start; |
| 40 | TEST_COMPARE (addr % pagesize, 0); |
| 41 | while (addr < l->l_map_end) |
| 42 | { |
| 43 | void *expected = (void *) addr; |
| 44 | void *ptr = xmmap (addr: expected, length: 1, PROT_READ | PROT_WRITE, |
| 45 | MAP_PRIVATE | MAP_ANONYMOUS, fd: -1); |
| 46 | if (ptr == expected) |
| 47 | FAIL ("hole in libc.so memory image after %lu bytes" , |
| 48 | (unsigned long int) (addr - l->l_map_start)); |
| 49 | xmunmap (addr: ptr, length: 1); |
| 50 | addr += pagesize; |
| 51 | } |
| 52 | |
| 53 | xdlclose (handle: l); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | #include <support/test-driver.c> |
| 58 | |