bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#include "test-lib.h"
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#if SIZEOF_VOID_P == 8
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainentypedef char uint32max_array_t[4294967295];
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#else
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainentypedef char uint32max_array_t[65535];
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#endif
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenextern struct pool test_pool;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void test_mempool_overflow(void)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen{
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_begin("mempool overflow");
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#if SIZEOF_VOID_P == 8
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen uint32max_array_t *m1 = p_new(&test_pool, uint32max_array_t, 4294967297ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m1 == POINTER_CAST(18446744073709551615ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen char *m2 = p_new(&test_pool, char, 18446744073709551615ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m2 == POINTER_CAST(18446744073709551615ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen uint32_t *m3 = p_new(&test_pool, uint32_t, 4611686018427387903ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m3 == POINTER_CAST(18446744073709551612ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* grow */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m1, uint32max_array_t, 4294967296ULL, 4294967297ULL) == POINTER_CAST(18446744073709551615ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m2, char, 18446744073709551614ULL, 18446744073709551615ULL) == POINTER_CAST(18446744073709551615ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m3, uint32_t, 4611686018427387902ULL, 4611686018427387903ULL) == POINTER_CAST(18446744073709551612ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* shrink */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m1, uint32max_array_t, 4294967297ULL, 4294967296ULL) == POINTER_CAST(18446744069414584320ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m2, char, 18446744073709551615ULL, 18446744073709551614ULL) == POINTER_CAST(18446744073709551614ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m3, uint32_t, 4611686018427387903ULL, 4611686018427387902ULL) == POINTER_CAST(18446744073709551608ULL));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#elif SIZEOF_VOID_P == 4
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen uint32max_array_t *m1 = p_new(&test_pool, uint32max_array_t, 65537);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m1 == POINTER_CAST(4294967295U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen char *m2 = p_new(&test_pool, char, 4294967295U);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m2 == POINTER_CAST(4294967295U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen uint32_t *m3 = p_new(&test_pool, uint32_t, 1073741823U);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(m3 == POINTER_CAST(4294967292U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* grow */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m1, uint32max_array_t, 65536, 65537) == POINTER_CAST(4294967295U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m2, char, 4294967294U, 4294967295U) == POINTER_CAST(4294967295U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m3, uint32_t, 1073741822U, 1073741823U) == POINTER_CAST(4294967292U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* shrink */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m1, uint32max_array_t, 65537, 65536) == POINTER_CAST(4294901760U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m2, char, 4294967295U, 4294967294U) == POINTER_CAST(4294967294U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_assert(p_realloc_type(&test_pool, m3, uint32_t, 1073741823U, 1073741822U) == POINTER_CAST(4294967288U));
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#else
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen# error unsupported pointer size
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#endif
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_end();
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen}
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenenum fatal_test_state fatal_mempool(unsigned int stage)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen{
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen static uint32max_array_t *m1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen static uint32_t *m2;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen test_expect_fatal_string("memory allocation overflow");
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#if SIZEOF_VOID_P == 8
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen switch(stage) {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 0:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_begin("fatal mempool overflow");
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_new(&test_pool, uint32max_array_t, 4294967298ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 1:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_new(&test_pool, uint32_t, 4611686018427387904ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 2: /* grow */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_realloc_type(&test_pool, m1, uint32max_array_t, 4294967297ULL, 4294967298ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 3:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_realloc_type(&test_pool, m2, uint32_t, 4611686018427387903ULL, 4611686018427387904ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 4: /* shrink */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_realloc_type(&test_pool, m1, uint32max_array_t, 4294967298ULL, 4294967297ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 5:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_realloc_type(&test_pool, m2, uint32_t, 4611686018427387904ULL, 4611686018427387903ULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#elif SIZEOF_VOID_P == 4
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen switch(stage) {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 0:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_begin("fatal mempool overflow");
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_new(&test_pool, uint32max_array_t, 65538);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 1:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_new(&test_pool, uint32_t, 1073741824U);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 2: /* grow */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_realloc_type(&test_pool, m1, uint32max_array_t, 65537, 65538);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 3:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_realloc_type(&test_pool, m2, uint32_t, 1073741823U, 1073741824U);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 4: /* shrink */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m1 = p_realloc_type(&test_pool, m1, uint32max_array_t, 65538, 65537);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen case 5:
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen m2 = p_realloc_type(&test_pool, m2, uint32_t, 1073741824U, 1073741823U);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FAILURE;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#else
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen# error unsupported pointer size
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#endif
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen test_expect_fatal_string(NULL);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_end();
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return FATAL_TEST_FINISHED;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen}
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic const char *pool_test_get_name(pool_t pool ATTR_UNUSED) { return "test"; }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void pool_test_ref(pool_t pool ATTR_UNUSED) { }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void pool_test_unref(pool_t *pool) { *pool = NULL; }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void *pool_test_malloc(pool_t pool ATTR_UNUSED, size_t size) { return POINTER_CAST(size); }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void pool_test_free(pool_t pool ATTR_UNUSED, void *mem ATTR_UNUSED) { }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void *pool_test_realloc(pool_t pool ATTR_UNUSED, void *mem ATTR_UNUSED,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen size_t old_size ATTR_UNUSED, size_t new_size) {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen return POINTER_CAST(new_size);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen}
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic void pool_test_clear(pool_t pool ATTR_UNUSED) { }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic size_t pool_test_get_max_easy_alloc_size(pool_t pool ATTR_UNUSED) { return 12345; }
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstatic const struct pool_vfuncs test_pool_vfuncs = {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_get_name,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_ref,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_unref,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_malloc,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_free,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_realloc,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_clear,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_test_get_max_easy_alloc_size
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct pool test_pool = {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen .v = &test_pool_vfuncs,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen .alloconly_pool = TRUE,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen .datastack_pool = FALSE,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenvoid test_mempool(void)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen{
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen test_mempool_overflow();
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen}