posix_memalign.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = 0;
  77. size < 3 * alignment && size < (1U << 31);
  78. size += ((size == 0) ? 1 :
  79. (alignment >> (LG_SIZEOF_PTR-1)) - 1)) {
  80. for (i = 0; i < NITER; i++) {
  81. err = posix_memalign(&ps[i],
  82. alignment, size);
  83. if (err) {
  84. char buf[BUFERROR_BUF];
  85. buferror(get_errno(), buf, sizeof(buf));
  86. test_fail(
  87. "Error for alignment=%zu, "
  88. "size=%zu (%#zx): %s",
  89. alignment, size, size, buf);
  90. }
  91. total += malloc_usable_size(ps[i]);
  92. if (total >= (MAXALIGN << 1)) {
  93. break;
  94. }
  95. }
  96. for (i = 0; i < NITER; i++) {
  97. if (ps[i] != NULL) {
  98. free(ps[i]);
  99. ps[i] = NULL;
  100. }
  101. }
  102. }
  103. purge();
  104. }
  105. #undef NITER
  106. }
  107. TEST_END
  108. int
  109. main(void) {
  110. return test(
  111. test_alignment_errors,
  112. test_oom_errors,
  113. test_alignment_and_size);
  114. }