tst-posix_memalign.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Test for posix_memalign.
  2. Copyright (C) 2013-2019 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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. static int errors = 0;
  21. static void
  22. merror (const char *msg)
  23. {
  24. ++errors;
  25. printf ("Error: %s\n", msg);
  26. }
  27. static int
  28. do_test (void)
  29. {
  30. void *p;
  31. int ret;
  32. unsigned long pagesize = getpagesize ();
  33. unsigned long ptrval;
  34. p = NULL;
  35. /* An attempt to allocate a huge value should return ENOMEM and
  36. p should remain NULL. */
  37. ret = posix_memalign (&p, sizeof (void *), -1);
  38. if (ret != ENOMEM)
  39. merror ("posix_memalign (&p, sizeof (void *), -1) succeeded.");
  40. if (ret == ENOMEM && p != NULL)
  41. merror ("returned an error but pointer was modified");
  42. free (p);
  43. p = NULL;
  44. /* Test to expose integer overflow in malloc internals from BZ #15857. */
  45. ret = posix_memalign (&p, pagesize, -pagesize);
  46. if (ret != ENOMEM)
  47. merror ("posix_memalign (&p, pagesize, -pagesize) succeeded.");
  48. free (p);
  49. p = NULL;
  50. /* Test to expose integer overflow in malloc internals from BZ #16038. */
  51. ret = posix_memalign (&p, -1, pagesize);
  52. if (ret != EINVAL)
  53. merror ("posix_memalign (&p, -1, pagesize) succeeded.");
  54. free (p);
  55. p = NULL;
  56. /* A zero-sized allocation should succeed with glibc, returning zero
  57. and setting p to a non-NULL value. */
  58. ret = posix_memalign (&p, sizeof (void *), 0);
  59. if (ret != 0 || p == NULL)
  60. merror ("posix_memalign (&p, sizeof (void *), 0) failed.");
  61. free (p);
  62. ret = posix_memalign (&p, 0x300, 10);
  63. if (ret != EINVAL)
  64. merror ("posix_memalign (&p, 0x300, 10) succeeded.");
  65. ret = posix_memalign (&p, 0, 10);
  66. if (ret != EINVAL)
  67. merror ("posix_memalign (&p, 0, 10) succeeded.");
  68. p = NULL;
  69. ret = posix_memalign (&p, 0x100, 10);
  70. if (ret != 0)
  71. merror ("posix_memalign (&p, 0x100, 10) failed.");
  72. if (ret == 0 && p == NULL)
  73. merror ("returned success but pointer is NULL");
  74. ptrval = (unsigned long) p;
  75. if (ret == 0 && (ptrval & 0xff) != 0)
  76. merror ("pointer is not aligned to 0x100");
  77. free (p);
  78. return errors != 0;
  79. }
  80. #define TEST_FUNCTION do_test ()
  81. #include "../test-skeleton.c"