tst-pvalloc-fortify.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Test fortify-source allocation size handling in pvalloc (bug 25401).
  2. Copyright (C) 2020-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <https://www.gnu.org/licenses/>. */
  15. #undef _FORTIFY_SOURCE
  16. #define _FORTIFY_SOURCE 2
  17. #include <malloc.h>
  18. #include <string.h>
  19. #include <support/check.h>
  20. #include <support/xunistd.h>
  21. #include <unistd.h>
  22. static int
  23. do_test (void)
  24. {
  25. /* The test below assumes that pvalloc rounds up the allocation size
  26. to at least 8. */
  27. TEST_VERIFY (xsysconf (_SC_PAGESIZE) >= 8);
  28. void *p = pvalloc (5);
  29. TEST_VERIFY_EXIT (p != NULL);
  30. /* This is valid assuming the page size is at least 8 because
  31. pvalloc rounds up the allocation size to a multiple of the page
  32. size. Due to bug 25041, this used to trigger a compiler
  33. warning. */
  34. strcpy (p, "abcdefg");
  35. asm ("" : : "g" (p) : "memory"); /* Optimization barrier. */
  36. TEST_VERIFY (malloc_usable_size (p) >= xsysconf (_SC_PAGESIZE));
  37. return 0;
  38. }
  39. #include <support/test-driver.c>