diff --git a/_kiss_fft_guts.h b/_kiss_fft_guts.h index f4b6832..94c33ae 100644 --- a/_kiss_fft_guts.h +++ b/_kiss_fft_guts.h @@ -31,6 +31,13 @@ struct kiss_fft_state{ kiss_fft_cpx twiddles[1]; }; +/* Rounds up the size of each object that kiss_fftnd_alloc / kiss_fftndr_alloc + * pack into one block, so that the next object starts suitably aligned for + * pointers, ints and kiss_fft_cpx (including SIMD vectors). */ +union kiss_fft_pack_align { void *p; int i; kiss_fft_cpx c; }; +#define KISS_FFT_PACK_SIZE_UP(size) \ + (((size) + sizeof(union kiss_fft_pack_align) - 1) / sizeof(union kiss_fft_pack_align) * sizeof(union kiss_fft_pack_align)) + /* Explanation of macros dealing with complex math: diff --git a/kfc.c b/kfc.c index 270944a..bddb55a 100644 --- a/kfc.c +++ b/kfc.c @@ -68,7 +68,7 @@ void kfc_cleanup(void) kfc_cfg next=NULL; while (cur){ next = cur->next; - free(cur); + KISS_FFT_FREE(cur); cur=next; } ncached=0; diff --git a/kiss_fft.h b/kiss_fft.h index a568076..1400392 100644 --- a/kiss_fft.h +++ b/kiss_fft.h @@ -51,10 +51,10 @@ extern "C" { #ifdef HAVE_LASX # include # define kiss_fft_scalar __m256 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 31UL) & ~0x1FUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) aligned_alloc(32, KISS_FFT_ALIGN_SIZE_UP(nbytes)) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 31UL) & ~0x1FUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE free @@ -62,10 +62,10 @@ extern "C" { #elif defined(HAVE_LSX) # include # define kiss_fft_scalar __m128 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) aligned_alloc(16, KISS_FFT_ALIGN_SIZE_UP(nbytes)) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE free @@ -73,10 +73,10 @@ extern "C" { #else # include # define kiss_fft_scalar __m128 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE _mm_free diff --git a/kiss_fftnd.c b/kiss_fftnd.c index 550d231..575e8b3 100644 --- a/kiss_fftnd.c +++ b/kiss_fftnd.c @@ -24,7 +24,7 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m kiss_fftnd_cfg st = NULL; int i; int dimprod=1; - size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state)); + size_t memneeded = KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftnd_state)); char * ptr = NULL; for (i=0;istates[i] */ + memneeded += KISS_FFT_PACK_SIZE_UP(sublen); /* st->states[i] */ dimprod *= dims[i]; } - memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims);/* st->dims */ - memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims);/* st->states */ - memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */ + memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(int) * ndims);/* st->dims */ + memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(void*) * ndims);/* st->states */ + memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */ if (lenmem == NULL) {/* allocate for the caller*/ - ptr = (char *) malloc (memneeded); + ptr = (char *) KISS_FFT_MALLOC (memneeded); } else { /* initialize supplied buffer if big enough */ if (*lenmem >= memneeded) ptr = (char *) mem; @@ -57,23 +57,23 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m st = (kiss_fftnd_cfg) ptr; st->dimprod = dimprod; st->ndims = ndims; - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state)); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftnd_state)); st->states = (kiss_fft_cfg *)ptr; - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(void*) * ndims); st->dims = (int*)ptr; - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(int) * ndims); st->tmpbuf = (kiss_fft_cpx*)ptr; - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); for (i=0;idims[i] = dims[i]; kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len); st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len); - ptr += len; + ptr += KISS_FFT_PACK_SIZE_UP(len); } /* Hi there! @@ -166,6 +166,12 @@ void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) const kiss_fft_cpx * bufin=fin; kiss_fft_cpx * bufout; + if (st->ndims == 0) { /* a zero-dimensional transform is the identity */ + if (fin != fout) + *fout = *fin; + return; + } + /*arrange it so the last bufout == fout*/ if ( st->ndims & 1 ) { bufout = fout; diff --git a/kiss_fftndr.c b/kiss_fftndr.c index c1cd27b..bdc5857 100644 --- a/kiss_fftndr.c +++ b/kiss_fftndr.c @@ -22,8 +22,11 @@ struct kiss_fftndr_state static size_t prod(const int *dims, int ndims) { size_t x=1; - while (ndims--) + while (ndims--) { + if (*dims > 0 && x > (size_t)-1 / (size_t)*dims) + return (size_t)-1; /* overflow: saturate so the size check below fails */ x *= *dims++; + } return x; } @@ -37,29 +40,27 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void size_t dimOther = prod(dims,ndims-1); size_t memneeded; char * ptr = NULL; - int k,check; (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr); + if (nr == 0) + return NULL; /* dimReal not supported by kiss_fftr (e.g. odd) */ + + /* check for overflow: the buffers below need about 2*dimOther*(dimReal+2) scalars; keep a 2x margin */ + /* kiss_fftnd counts the other dimensions in an int */ + if (dimOther > INT_MAX || dimOther > (size_t)-1 / 4 / sizeof(kiss_fft_scalar) / ((size_t)dimReal + 2)) { + if (lenmem!=NULL) + *lenmem = (size_t)(-1); + return NULL; + } (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd); ntmp = MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass + dimOther*(size_t)(dimReal+2) * sizeof(kiss_fft_scalar); // large enough to hold entire input in case of in-place - memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_ALIGN_SIZE_UP(nr) + KISS_FFT_ALIGN_SIZE_UP(nd) + KISS_FFT_ALIGN_SIZE_UP(ntmp); - - /* check for overflow */ - check = memneeded; - for (k=0;k= memneeded) ptr = (char *)mem; @@ -70,14 +71,14 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void st = (kiss_fftndr_cfg) ptr; memset( st , 0 , memneeded); - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftndr_state)); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftndr_state)); st->dimReal = dimReal; st->dimOther = dimOther; st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,ptr,&nr); - ptr += KISS_FFT_ALIGN_SIZE_UP(nr); + ptr += KISS_FFT_PACK_SIZE_UP(nr); st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ptr,&nd); - ptr += KISS_FFT_ALIGN_SIZE_UP(nd); + ptr += KISS_FFT_PACK_SIZE_UP(nd); st->tmpbuf = ptr; return st; diff --git a/kiss_fftndr.h b/kiss_fftndr.h index 0d56a1f..5c082a5 100644 --- a/kiss_fftndr.h +++ b/kiss_fftndr.h @@ -46,7 +46,7 @@ void KISS_FFT_API kiss_fftndri( */ -#define kiss_fftndr_free free +#define kiss_fftndr_free KISS_FFT_FREE #ifdef __cplusplus } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ab16cf4..6ca6a42 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -49,6 +49,11 @@ target_compile_definitions(tkfc PRIVATE KFC_TEST) add_kissfft_test_executable(ffr twotonetest.c) add_kissfft_test_executable(tr test_real.c) add_kissfft_test_executable(tsizeval test_size_validation.c) +add_kissfft_test_executable(tndralloc test_fftndr_alloc.c) +# includes ../kiss_fftr.c, ../kiss_fftnd.c and ../kiss_fftndr.c to reach their private structs +add_kissfft_test_executable(tndalign test_nd_alignment.c) +# unity build: compiles the library sources itself with KISS_FFT_MALLOC/FREE overridden +add_kissfft_test_executable(tcustalloc test_custom_alloc.c) add_kissfft_test_executable(next_fast_size next_fast_size.c) diff --git a/test/Makefile b/test/Makefile index 17dcf92..7d0ead0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -44,6 +44,9 @@ ifneq ($(KISSFFT_OPENMP),1) SELFTEST = st-$(KISSFFT_DATATYPE) TESTREAL = tr-$(KISSFFT_DATATYPE) TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE) + TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE) + TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE) + TESTCUSTOMALLOC = tcustalloc-$(KISSFFT_DATATYPE) TESTKFC = tkfc-$(KISSFFT_DATATYPE) TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) TESTCPP = testcpp-$(KISSFFT_DATATYPE) @@ -54,6 +57,9 @@ else SELFTEST = st-$(KISSFFT_DATATYPE)-openmp TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp + TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)-openmp + TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE)-openmp + TESTCUSTOMALLOC = tcustalloc-$(KISSFFT_DATATYPE)-openmp TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp @@ -105,7 +111,7 @@ endif # Target: "make all" # -all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT) +all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTNDALIGN) $(TESTCUSTOMALLOC) $(TESTKFC) $(TESTFASTFILT) # # Individual test make rules @@ -123,6 +129,17 @@ $(TESTREAL): test_real.c $(TESTSIZEVAL): test_size_validation.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm +$(TESTNDRALLOC): test_fftndr_alloc.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm + +# includes ../kiss_fftr.c, ../kiss_fftnd.c and ../kiss_fftndr.c to reach their private structs +$(TESTNDALIGN): test_nd_alignment.c ../kiss_fftr.c ../kiss_fftnd.c ../kiss_fftndr.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm + +# unity build: compiles the library sources itself with KISS_FFT_MALLOC/FREE overridden, so it must not link the library +$(TESTCUSTOMALLOC): test_custom_alloc.c ../kiss_fft.c ../kiss_fftr.c ../kiss_fftnd.c ../kiss_fftndr.c ../kfc.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -lm + $(BENCHKISS): benchkiss.c pstats.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm @@ -182,6 +199,12 @@ endif @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL) $(warning ======allocation size validation regression test (type= $(KISSFFT_DATATYPE) )) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL) + $(warning ======kiss_fftndr allocation regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDRALLOC) + $(warning ======multi-dimensional FFT state alignment regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDALIGN) + $(warning ======custom KISS_FFT_MALLOC/KISS_FFT_FREE regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTCUSTOMALLOC) $(warning ======timing test (type=$(KISSFFT_DATATYPE))) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true @@ -193,4 +216,4 @@ endif # clean: - rm -f *~ bm-* st-* tr-* tsizeval-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c + rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* tndalign-* tcustalloc-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c diff --git a/test/test_custom_alloc.c b/test/test_custom_alloc.c new file mode 100644 index 0000000..dc903c8 --- /dev/null +++ b/test/test_custom_alloc.c @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression test for user-overridden KISS_FFT_MALLOC / KISS_FFT_FREE. + * + * The library sources are compiled INTO this test (unity build) with + * KISS_FFT_MALLOC/KISS_FFT_FREE replaced by tracking wrappers, so the test + * does not link against the prebuilt library. In addition, plain malloc() + * and free() are redirected while the library sources are compiled, to catch + * code that bypasses the overridable allocator: + * 1. every allocation made by kiss_fft_alloc, kiss_fftr_alloc, + * kiss_fftnd_alloc, kiss_fftndr_alloc and kfc_fft goes through + * KISS_FFT_MALLOC (no raw malloc) + * 2. kiss_fft_free, kiss_fftr_free, kiss_fftndr_free and kfc_cleanup only + * release blocks that KISS_FFT_MALLOC returned, and do so through + * KISS_FFT_FREE (no raw free) + * 3. nothing is left allocated afterwards + * 4. with USE_SIMD, overriding the allocator must still compile (the + * KISS_FFT_ALIGN_CHECK / KISS_FFT_ALIGN_SIZE_UP macros must stay defined) + */ +#include +#include +#include +#include +#include +#include + +/* the tracking allocator, defined below; must be visible before kiss_fft.h */ +static void *test_malloc(size_t n); +static void test_free(void *p); +#define KISS_FFT_MALLOC(nbytes) test_malloc(nbytes) +#define KISS_FFT_FREE(ptr) test_free(ptr) + +#include "kiss_fft.h" /* pull in every system header the library needs while */ +#include "_kiss_fft_guts.h" /* malloc/free are still the real functions */ + +/* ---- tracking allocator -------------------------------------------------- */ + +#define ALLOC_ALIGN 32 /* enough for SSE (16) and AVX/LASX (32) kiss_fft_scalar */ +#define MAX_BLOCKS 256 + +struct block { + void *user; /* pointer handed to the library */ + void *raw; /* pointer obtained from the C library */ + int owned; /* 1: returned by KISS_FFT_MALLOC, 0: library called raw malloc */ +}; + +static struct block blocks[MAX_BLOCKS]; +static int nblocks = 0; + +/* counters since the last begin_scenario() */ +static int n_wrapper_alloc = 0; /* KISS_FFT_MALLOC calls */ +static int n_raw_malloc = 0; /* library called malloc() directly */ +static int n_raw_free_owned = 0; /* library called free() on a KISS_FFT_MALLOC block */ +static int n_foreign_free = 0; /* KISS_FFT_FREE on a block KISS_FFT_MALLOC did not return */ +static int n_unknown_free = 0; /* free of a pointer nobody allocated */ + +static int failures = 0; + +static void *real_malloc(size_t n) +{ + return malloc(n); +} + +static void real_free(void *p) +{ + free(p); +} + +static int find_block(const void *p) +{ + int i; + for (i = 0; i < nblocks; ++i) + if (blocks[i].user == p) + return i; + return -1; +} + +static void add_block(void *user, void *raw, int owned) +{ + if (nblocks == MAX_BLOCKS) { + printf("FAIL: test allocation table full\n"); + exit(1); + } + blocks[nblocks].user = user; + blocks[nblocks].raw = raw; + blocks[nblocks].owned = owned; + ++nblocks; +} + +static void drop_block(int i) +{ + real_free(blocks[i].raw); + blocks[i] = blocks[--nblocks]; +} + +static void *test_malloc(size_t n) +{ + char *raw = (char *)real_malloc(n + ALLOC_ALIGN); + void *user; + if (raw == NULL) + return NULL; + user = (void *)(((uintptr_t)raw + ALLOC_ALIGN - 1) & ~(uintptr_t)(ALLOC_ALIGN - 1)); + add_block(user, raw, 1); + ++n_wrapper_alloc; + return user; +} + +static void test_free(void *p) +{ + int i; + if (p == NULL) + return; + i = find_block(p); + if (i < 0) { + ++n_unknown_free; + return; /* not ours: do not hand it to the C library */ + } + if (!blocks[i].owned) + ++n_foreign_free; + drop_block(i); +} + +/* what plain malloc()/free() inside the library sources turn into */ +static void *raw_malloc(size_t n) +{ + void *p = real_malloc(n); + if (p != NULL) { + add_block(p, p, 0); + ++n_raw_malloc; + } + return p; +} + +static void raw_free(void *p) +{ + int i; + if (p == NULL) + return; + i = find_block(p); + if (i < 0) { + ++n_unknown_free; + return; + } + if (blocks[i].owned) + ++n_raw_free_owned; + drop_block(i); +} + +#define malloc(nbytes) raw_malloc(nbytes) +#define free(ptr) raw_free(ptr) + +/* ---- library under test, compiled in with the overrides above ----------- */ + +/* the library itself is not built with -W; keep its sign-compare / maybe-uninitialized noise out of this test */ +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsign-compare" +# if !defined(__clang__) +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +# endif +#endif +#include "../kiss_fft.c" +#include "../kiss_fftr.c" +#include "../kiss_fftnd.c" +#include "../kiss_fftndr.c" +#include "../kfc.c" +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic pop +#endif + +/* ---- test ---------------------------------------------------------------- */ + +static void begin_scenario(void) +{ + n_wrapper_alloc = n_raw_malloc = n_raw_free_owned = 0; + n_foreign_free = n_unknown_free = 0; +} + +/* call right after the *_alloc call under test */ +static void expect_wrapper_alloc(const char *what) +{ + if (n_wrapper_alloc == 0) { + printf("FAIL: %s did not allocate through KISS_FFT_MALLOC\n", what); + ++failures; + } +} + +/* call after the object was released with the library's own free function */ +static void end_scenario(const char *what) +{ + int i, live_owned = 0, live_raw = 0; + + for (i = 0; i < nblocks; ++i) { + if (blocks[i].owned) + ++live_owned; + else + ++live_raw; + } + if (n_raw_malloc) { + printf("FAIL: %s called malloc() %d time(s) instead of KISS_FFT_MALLOC\n", what, n_raw_malloc); + ++failures; + } + if (n_raw_free_owned) { + printf("FAIL: %s called free() %d time(s) on KISS_FFT_MALLOC memory instead of KISS_FFT_FREE\n", + what, n_raw_free_owned); + ++failures; + } + if (n_foreign_free) { + printf("FAIL: %s passed %d block(s) not allocated by KISS_FFT_MALLOC to KISS_FFT_FREE\n", + what, n_foreign_free); + ++failures; + } + if (n_unknown_free) { + printf("FAIL: %s freed %d pointer(s) nobody allocated\n", what, n_unknown_free); + ++failures; + } + if (live_owned || live_raw) { + printf("FAIL: %s left %d KISS_FFT_MALLOC block(s) and %d malloc block(s) allocated\n", + what, live_owned, live_raw); + ++failures; + } + while (nblocks > 0) /* do not let one failing scenario leak into the next */ + drop_block(nblocks - 1); +} + +/* the redirection itself must work, otherwise a PASS below means nothing */ +static void test_harness(void) +{ + void *p; + + begin_scenario(); + p = malloc(8); + if (n_raw_malloc != 1 || nblocks != 1) { + printf("FAIL: test harness did not intercept malloc()\n"); + ++failures; + } + free(p); + p = KISS_FFT_MALLOC(8); + free(p); /* wrong way to release a KISS_FFT_MALLOC block */ + if (n_raw_free_owned != 1 || n_wrapper_alloc != 1 || nblocks != 0) { + printf("FAIL: test harness did not intercept free()\n"); + ++failures; + } +} + +static void test_kiss_fft(void) +{ + kiss_fft_cpx in[7], out[7]; + kiss_fft_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fft_alloc(7, 0, NULL, NULL); /* prime size: generic butterfly uses a temp buffer */ + expect_wrapper_alloc("kiss_fft_alloc"); + if (cfg != NULL) { + kiss_fft(cfg, in, out); + kiss_fft(cfg, in, in); /* "in-place" also uses a temp buffer */ + } + kiss_fft_free(cfg); + end_scenario("kiss_fft"); +} + +static void test_kiss_fftr(void) +{ + kiss_fft_scalar in[16]; + kiss_fft_cpx out[9]; + kiss_fftr_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftr_alloc(16, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftr_alloc"); + if (cfg != NULL) + kiss_fftr(cfg, in, out); + kiss_fftr_free(cfg); + end_scenario("kiss_fftr"); +} + +static void test_kiss_fftnd(void) +{ + static const int dims[2] = {4, 6}; + kiss_fft_cpx in[24], out[24]; + kiss_fftnd_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftnd_alloc(dims, 2, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftnd_alloc"); + if (cfg != NULL) + kiss_fftnd(cfg, in, out); + kiss_fft_free(cfg); + end_scenario("kiss_fftnd"); +} + +static void test_kiss_fftndr(void) +{ + static const int dims[2] = {4, 6}; + kiss_fft_scalar in[24]; + kiss_fft_cpx out[4 * 4]; + kiss_fftndr_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftndr_alloc(dims, 2, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftndr_alloc"); + if (cfg != NULL) + kiss_fftndr(cfg, in, out); + kiss_fftndr_free(cfg); + end_scenario("kiss_fftndr"); +} + +static void test_kfc(void) +{ + kiss_fft_cpx in[8], out[8]; + + memset(in, 0, sizeof(in)); + begin_scenario(); + kfc_fft(8, in, out); + kfc_ifft(8, in, out); + expect_wrapper_alloc("kfc_fft"); + kfc_cleanup(); + end_scenario("kfc"); +} + +int main(void) +{ + test_harness(); + test_kiss_fft(); + test_kiss_fftr(); + test_kiss_fftnd(); + test_kiss_fftndr(); + test_kfc(); + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +} diff --git a/test/test_fftndr_alloc.c b/test/test_fftndr_alloc.c new file mode 100644 index 0000000..a4e2676 --- /dev/null +++ b/test/test_fftndr_alloc.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression tests for kiss_fftndr_alloc / kiss_fftndr: + * 1. valid even NxN sizes must not be rejected by the allocation size check + * 2. an odd last dimension (unsupported by kiss_fftr) must make alloc fail + * 3. ndims == 1 must produce the same spectrum as kiss_fftr + */ +#include +#include +#include +#include "kiss_fftndr.h" +#include "kiss_fftr.h" + +static int failures = 0; + +static void fail(const char *msg, int a, int b) +{ + printf("FAIL: %s (%d,%d)\n", msg, a, b); + ++failures; +} + +/* allocate through the caller-supplied-buffer path; returns NULL on failure */ +static kiss_fftndr_cfg alloc_ndr(const int *dims, int ndims, void **buf) +{ + size_t len = 0; + *buf = NULL; + kiss_fftndr_alloc(dims, ndims, 0, NULL, &len); + if (len == 0 || len == (size_t)-1) + return NULL; + *buf = malloc(len); + if (*buf == NULL) + return NULL; + return kiss_fftndr_alloc(dims, ndims, 0, *buf, &len); +} + +static void test_valid_sizes_accepted(void) +{ + int n, first = 0, rejected = 0; + for (n = 2; n <= 512; n += 2) { + int dims[2]; + void *buf; + dims[0] = n; + dims[1] = n; + if (alloc_ndr(dims, 2, &buf) == NULL) { + if (!first) + first = n; + ++rejected; + } + free(buf); + } + if (rejected) { + printf("(%d valid even NxN sizes in 2..512 rejected)\n", rejected); + fail("kiss_fftndr_alloc rejected a valid square size, first", first, first); + } +} + +static void test_odd_last_dim_rejected(void) +{ + int dims[2] = {4, 5}; + void *buf; + if (alloc_ndr(dims, 2, &buf) != NULL) + fail("kiss_fftndr_alloc accepted an odd last dimension", dims[0], dims[1]); + free(buf); +} + +/* the inner kiss_fftnd counts dims[0..ndims-2] in an int: a product above INT_MAX + * must be reported as too large by the size query (no allocation is attempted) */ +static void test_int_overflow_rejected(void) +{ + int dims[3] = {46341, 46341, 2}; /* 46341^2 > INT_MAX */ + size_t len = 0; + kiss_fftndr_alloc(dims, 3, 0, NULL, &len); + if (len != (size_t)-1) + fail("kiss_fftndr_alloc size query accepted dims whose product overflows int", dims[0], dims[1]); +} + +#ifndef USE_SIMD +static void test_one_dimension_matches_fftr(void) +{ + enum { N = 8 }; + int dims[1] = {N}; + kiss_fft_scalar in[N]; + kiss_fft_cpx got[N / 2 + 1], want[N / 2 + 1]; + void *buf; + kiss_fftndr_cfg ndr; + kiss_fftr_cfg r; + int k; + + for (k = 0; k < N; ++k) + in[k] = (kiss_fft_scalar)(100 * (k + 1)); + + ndr = alloc_ndr(dims, 1, &buf); + r = kiss_fftr_alloc(N, 0, NULL, NULL); + if (ndr == NULL || r == NULL) { + fail("allocation failed for ndims == 1", N, 0); + } else { + kiss_fftndr(ndr, in, got); + kiss_fftr(r, in, want); + for (k = 0; k <= N / 2; ++k) + if (got[k].r != want[k].r || got[k].i != want[k].i) + fail("ndims == 1 output differs from kiss_fftr at bin", k, 0); + } + free(buf); + kiss_fftr_free(r); +} +#endif + +int main(void) +{ + test_valid_sizes_accepted(); + test_odd_last_dim_rejected(); + test_int_overflow_rejected(); +#ifndef USE_SIMD + test_one_dimension_matches_fftr(); +#endif + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +} diff --git a/test/test_nd_alignment.c b/test/test_nd_alignment.c new file mode 100644 index 0000000..4aaea62 --- /dev/null +++ b/test/test_nd_alignment.c @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression test for the memory layout of kiss_fftnd_alloc / kiss_fftndr_alloc. + * + * Both pack several objects into one block. Every object must start at an + * address suitably aligned for its type, otherwise the transforms perform + * misaligned accesses (undefined behavior; a SIGBUS/UBSan error on strict + * platforms, invisible on x86 without sanitizers): + * 1. kiss_fftnd: with an odd number of dimensions the int dims[] array shifts + * tmpbuf and the per-dimension kiss_fft_state objects by 4 bytes, which + * breaks 8-byte aligned types (double) + * 2. kiss_fftndr: the nd state (which contains pointers) follows the real-FFT + * state, whose size need not be a multiple of the pointer size (int16_t) + * + * The private structs are visible because the library sources are compiled + * into this test; the states are built in a caller-supplied, over-aligned + * buffer, so the result depends only on the library's own layout code. + * No transform is run. + */ +#include +#include +#include +#include +#include +#include "kiss_fft.h" +#include "_kiss_fft_guts.h" + +/* the library itself is not built with -W; keep its sign-compare noise out of this test */ +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsign-compare" +#endif +#include "../kiss_fftr.c" +#include "../kiss_fftnd.c" +#include "../kiss_fftndr.c" +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic pop +#endif + +/* C89-portable alignment requirement of a type: padding after a char member */ +#define ALIGN_PROBE(tag, type) struct tag { char c; type x; } +#define ALIGNOF(tag) offsetof(struct tag, x) + +ALIGN_PROBE(probe_cpx, kiss_fft_cpx); +ALIGN_PROBE(probe_fft_state, struct kiss_fft_state); +ALIGN_PROBE(probe_fftr_state, struct kiss_fftr_state); +ALIGN_PROBE(probe_fftnd_state, struct kiss_fftnd_state); +ALIGN_PROBE(probe_ptr, void *); +ALIGN_PROBE(probe_int, int); + +#define BUF_ALIGN 64 /* more than any type here needs, so only the layout can misalign */ + +static int failures = 0; + +static void fmt_dims(char *out, const int *dims, int ndims) +{ + int i; + char *p = out; + *p++ = '{'; + for (i = 0; i < ndims; ++i) + p += sprintf(p, i ? ",%d" : "%d", dims[i]); + *p++ = '}'; + *p = '\0'; +} + +static void check_aligned(const char *owner, const char *dimstr, const char *what, + int index, const void *p, size_t align) +{ + size_t off = (size_t)((uintptr_t)p % align); + if (off != 0) { + if (index >= 0) + printf("FAIL: %s dims %s: %s[%d] at offset %lu is misaligned (needs %lu-byte alignment)\n", + owner, dimstr, what, index, (unsigned long)off, (unsigned long)align); + else + printf("FAIL: %s dims %s: %s at offset %lu is misaligned (needs %lu-byte alignment)\n", + owner, dimstr, what, (unsigned long)off, (unsigned long)align); + ++failures; + } +} + +static void check_nd_state(const char *owner, const char *dimstr, kiss_fftnd_cfg nd) +{ + int k; + check_aligned(owner, dimstr, "states array", -1, nd->states, ALIGNOF(probe_ptr)); + check_aligned(owner, dimstr, "dims array", -1, nd->dims, ALIGNOF(probe_int)); + check_aligned(owner, dimstr, "tmpbuf", -1, nd->tmpbuf, ALIGNOF(probe_cpx)); + for (k = 0; k < nd->ndims; ++k) + check_aligned(owner, dimstr, "kiss_fft_state", k, nd->states[k], ALIGNOF(probe_fft_state)); +} + +/* allocate through the caller-supplied-buffer path, in a BUF_ALIGN-aligned block; + * returns NULL (and *raw == NULL or to be freed) if the size is not accepted */ +static void *alloc_in_aligned_buffer(size_t len, void **raw) +{ + *raw = malloc(len + BUF_ALIGN); + if (*raw == NULL) + return NULL; + return (void *)(((uintptr_t)*raw + BUF_ALIGN - 1) & ~(uintptr_t)(BUF_ALIGN - 1)); +} + +static void test_fftnd(const int *dims, int ndims) +{ + char dimstr[64]; + size_t len = 0; + void *raw, *buf; + kiss_fftnd_cfg st; + + fmt_dims(dimstr, dims, ndims); + (void)kiss_fftnd_alloc(dims, ndims, 0, NULL, &len); + buf = alloc_in_aligned_buffer(len, &raw); + if (buf == NULL) { + printf("FAIL: out of memory for kiss_fftnd dims %s\n", dimstr); + ++failures; + return; + } + st = kiss_fftnd_alloc(dims, ndims, 0, buf, &len); + if (st == NULL) { + printf("FAIL: kiss_fftnd_alloc rejected dims %s\n", dimstr); + ++failures; + } else { + check_nd_state("kiss_fftnd", dimstr, st); + } + free(raw); +} + +static void test_fftndr(const int *dims, int ndims) +{ + char dimstr[64]; + size_t len = 0; + void *raw, *buf; + kiss_fftndr_cfg st; + + fmt_dims(dimstr, dims, ndims); + (void)kiss_fftndr_alloc(dims, ndims, 0, NULL, &len); + if (len == 0 || len == (size_t)-1) { + printf("(note: kiss_fftndr_alloc size query rejected dims %s, skipped)\n", dimstr); + return; + } + buf = alloc_in_aligned_buffer(len, &raw); + if (buf == NULL) { + printf("FAIL: out of memory for kiss_fftndr dims %s\n", dimstr); + ++failures; + return; + } + st = kiss_fftndr_alloc(dims, ndims, 0, buf, &len); + if (st == NULL) { + printf("(note: kiss_fftndr_alloc rejected dims %s, skipped)\n", dimstr); + } else { + check_aligned("kiss_fftndr", dimstr, "real-FFT state", -1, st->cfg_r, ALIGNOF(probe_fftr_state)); + check_aligned("kiss_fftndr", dimstr, "nd state", -1, st->cfg_nd, ALIGNOF(probe_fftnd_state)); + check_aligned("kiss_fftndr", dimstr, "tmpbuf", -1, st->tmpbuf, ALIGNOF(probe_cpx)); + if (ndims > 1) + check_nd_state("kiss_fftndr's nd state", dimstr, st->cfg_nd); + } + free(raw); +} + +int main(void) +{ + static const int d7[] = {7}; + static const int d345[] = {3, 4, 5}; + static const int d46[] = {4, 6}; + static const int d23456[] = {2, 3, 4, 5, 6}; + static const int d6[] = {6}; + static const int d346[] = {3, 4, 6}; + static const int d58[] = {5, 8}; + + test_fftnd(d7, 1); + test_fftnd(d345, 3); + test_fftnd(d46, 2); + test_fftnd(d23456, 5); + + test_fftndr(d46, 2); + test_fftndr(d6, 1); + test_fftndr(d346, 3); + test_fftndr(d58, 2); + test_fftndr(d23456, 5); + + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +}