posix_memalign.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "test/jemalloc_test.h"
  2. #define MAXALIGN (((size_t)1) << 23)
  3. /*
  4. * On systems which can't merge extents, tests that call this function generate
  5. * a lot of dirty memory very quickly. Purging between cycles mitigates
  6. * potential OOM on e.g. 32-bit Windows.
  7. */
  8. static void
  9. purge(void) {
  10. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  11. "Unexpected mallctl error");
  12. }
  13. TEST_BEGIN(test_alignment_errors) {
  14. size_t alignment;
  15. void *p;
  16. for (alignment = 0; alignment < sizeof(void *); alignment++) {
  17. assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
  18. "Expected error for invalid alignment %zu",
  19. alignment);
  20. }
  21. for (alignment = sizeof(size_t); alignment < MAXALIGN;
  22. alignment <<= 1) {
  23. assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
  24. "Expected error for invalid alignment %zu",
  25. alignment + 1);
  26. }
  27. }
  28. TEST_END
  29. TEST_BEGIN(test_oom_errors) {
  30. size_t alignment, size;
  31. void *p;
  32. #if LG_SIZEOF_PTR == 3
  33. alignment = UINT64_C(0x8000000000000000);
  34. size = UINT64_C(0x8000000000000000);
  35. #else
  36. alignment = 0x80000000LU;
  37. size = 0x80000000LU;
  38. #endif
  39. assert_d_ne(posix_memalign(&p, alignment, size), 0,
  40. "Expected error for posix_memalign(&p, %zu, %zu)",
  41. alignment, size);
  42. #if LG_SIZEOF_PTR == 3
  43. alignment = UINT64_C(0x4000000000000000);
  44. size = UINT64_C(0xc000000000000001);
  45. #else
  46. alignment = 0x40000000LU;
  47. size = 0xc0000001LU;
  48. #endif
  49. assert_d_ne(posix_memalign(&p, alignment, size), 0,
  50. "Expected error for posix_memalign(&p, %zu, %zu)",
  51. alignment, size);
  52. alignment = 0x10LU;
  53. #if LG_SIZEOF_PTR == 3
  54. size = UINT64_C(0xfffffffffffffff0);
  55. #else
  56. size = 0xfffffff0LU;
  57. #endif
  58. assert_d_ne(posix_memalign(&p, alignment, size), 0,
  59. "Expected error for posix_memalign(&p, %zu, %zu)",
  60. alignment, size);
  61. }
  62. TEST_END
  63. TEST_BEGIN(test_alignment_and_size) {
  64. #define NITER 4
  65. size_t alignment, size, total;
  66. unsigned i;
  67. int err;
  68. void *ps[NITER];
  69. for (i = 0; i < NITER; i++) {
  70. ps[i] = NULL;
  71. }
  72. for (alignment = 8;
  73. alignment <= MAXALIGN;
  74. alignment <<= 1) {
  75. total = 0;
  76. for (size = 1;
  77. size < 3 * alignment && size < (1U << 31);
  78. size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  79. for (i = 0; i < NITER; i++) {
  80. err = posix_memalign(&ps[i],
  81. alignment, size);
  82. if (err) {
  83. char buf[BUFERROR_BUF];
  84. buferror(get_errno(), buf, sizeof(buf));
  85. test_fail(
  86. "Error for alignment=%zu, "
  87. "size=%zu (%#zx): %s",
  88. alignment, size, size, buf);
  89. }
  90. total += malloc_usable_size(ps[i]);
  91. if (total >= (MAXALIGN << 1)) {
  92. break;
  93. }
  94. }
  95. for (i = 0; i < NITER; i++) {
  96. if (ps[i] != NULL) {
  97. free(ps[i]);
  98. ps[i] = NULL;
  99. }
  100. }
  101. }
  102. purge();
  103. }
  104. #undef NITER
  105. }
  106. TEST_END
  107. int
  108. main(void) {
  109. return test(
  110. test_alignment_errors,
  111. test_oom_errors,
  112. test_alignment_and_size);
  113. }