--- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/testcases/t_aligned_alloc.c | 156 ++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 mingw-w64-crt/testcases/t_aligned_alloc.c
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 5f7805c94739..1f0f1c6df658 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -4311,6 +4311,7 @@ testcase_progs = \ testcases/tstmainc \ testcases/tstmaincpp \ testcases/tstmain_sys_xxx \ + testcases/t_aligned_alloc \ testcases/t_ansi_io \ testcases/t_findfirst \ testcases/t_float \ diff --git a/mingw-w64-crt/testcases/t_aligned_alloc.c b/mingw-w64-crt/testcases/t_aligned_alloc.c new file mode 100644 index 000000000000..1e4cbdea2db0 --- /dev/null +++ b/mingw-w64-crt/testcases/t_aligned_alloc.c @@ -0,0 +1,156 @@ +#include <stdio.h> +#include <stdint.h> +#include <assert.h> +#include <string.h> +#include <malloc.h> + +#define TEST(a, b, msg, ...) do { \ + if ((a) != (b)) { \ + fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ + fprintf(stderr, msg, ##__VA_ARGS__); \ + fputc('\n', stderr); \ + return 1; \ + } \ +} while (0) + +#define TEST_MEM(ptr, byte, size, msg, ...) do { \ + size_t i; \ + for (i = 0; i < size; i++) { \ + if (((unsigned char *)ptr)[i] != (unsigned char)byte) { \ + fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ + fprintf(stderr, msg, ##__VA_ARGS__); \ + fputc('\n', stderr); \ + return 1; \ + } \ + } \ +} while (0) + +int main() { + void *ptr; + size_t size; + + + /* + * Surprisingly this malloc/_msize test is failing on msvcrt10.dll and msvcrt20.dll + * because these two CRT libraries are buggy and return _msize rounded to even number. + * So it means that also mingw-w64 _aligned_msize function (wrapper around _msize) + * returns wrong value when using those two CRT libraries. + */ + ptr = malloc(231); + assert(ptr != NULL); + size = _msize(ptr); + TEST(size, 231, "_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + free(ptr); + + + ptr = malloc(1000); + assert(ptr != NULL); + + size = _msize(ptr); + TEST(size, 1000, "_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + memset(ptr, 0x01, 1000); + + ptr = _recalloc(ptr, 100, 1); + assert(ptr != NULL); + + size = _msize(ptr); + TEST(size, 100, "_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + ptr = _recalloc(ptr, 200, 1); + assert(ptr != NULL); + + size = _msize(ptr); + TEST(size, 200, "_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + TEST_MEM(ptr, 0x01, 100, "_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i], (unsigned long)i); + TEST_MEM(ptr+100, 0x00, 100, "_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i+100], (unsigned long)i+100); + + free(ptr); + + + ptr = _aligned_malloc(100, 128); + assert(ptr != NULL); + TEST((uintptr_t)ptr % 128, 0, "_aligned_malloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 0); + TEST(size, 100, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + memset(ptr, 0x01, 100); + + ptr = _aligned_realloc(ptr, 2000, 128); + assert(ptr != NULL); + TEST((uintptr_t)ptr % 128, 0, "_aligned_realloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 0); + TEST(size, 2000, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + TEST_MEM(ptr, 0x01, 100, "_aligned_realloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i], (unsigned long)i); + + memset(ptr, 0x02, 2000); + + ptr = _aligned_realloc(ptr, 10, 128); + assert(ptr != NULL); + TEST((uintptr_t)ptr % 128, 0, "_aligned_realloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 0); + TEST(size, 10, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + ptr = _aligned_recalloc(ptr, 20, 1, 128); + assert(ptr != NULL); + TEST((uintptr_t)ptr % 128, 0, "_aligned_recalloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 0); + TEST(size, 20, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + TEST_MEM(ptr, 0x02, 10, "_aligned_realloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i], (unsigned long)i); + TEST_MEM(ptr+10, 0x00, 10, "_aligned_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i+10], (unsigned long)i+10); + + ptr = _aligned_recalloc(ptr, 3, 2, 128); + assert(ptr != NULL); + TEST((uintptr_t)ptr % 128, 0, "_aligned_recalloc: ptr 0x%p is not aligned", ptr); + + TEST_MEM(ptr, 0x02, 6, "_aligned_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i], (unsigned long)i); + + _aligned_free(ptr); + + + ptr = _aligned_offset_malloc(300, 128, 7); + TEST((uintptr_t)(ptr+7) % 128, 0, "_aligned_offset_malloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 7); + TEST(size, 300, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + ptr = _aligned_offset_realloc(ptr, 3000, 128, 7); + assert(ptr != NULL); + TEST((uintptr_t)(ptr+7) % 128, 0, "_aligned_offset_realloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 7); + TEST(size, 3000, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + memset(ptr, 0x01, 3000); + + ptr = _aligned_offset_recalloc(ptr, 100, 1, 128, 7); + assert(ptr != NULL); + TEST((uintptr_t)(ptr+7) % 128, 0, "_aligned_offset_recalloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 7); + TEST(size, 100, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + memset(ptr, 0x02, 100); + + ptr = _aligned_offset_recalloc(ptr, 110, 1, 128, 7); + assert(ptr != NULL); + TEST((uintptr_t)(ptr+7) % 128, 0, "_aligned_offset_recalloc: ptr 0x%p is not aligned", ptr); + + size = _aligned_msize(ptr, 128, 7); + TEST(size, 110, "_aligned_msize: ptr 0x%p has incorrect size %lu", ptr, (unsigned long)size); + + TEST_MEM(ptr, 0x02, 100, "_aligned_offset_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i], (unsigned long)i); + TEST_MEM(ptr+100, 0x00, 10, "_aligned_offset_recalloc: 0x%p has incorrect byte 0x%02x at %lu", ptr, ((unsigned char *)ptr)[i+100], (unsigned long)i+100); + + _aligned_free(ptr); + + + return 0; +} -- 2.20.1 _______________________________________________ Mingw-w64-public mailing list Mingw-w64-public@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mingw-w64-public