From 4d3ff205be797c61cfdef49330d255b1822a3288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 16 Jan 2009 11:44:04 +0000 Subject: [PATCH] gst-libs/gst/fft/: Use correct struct alignment everywhere to prevent unaligned memory accesses, resulting in SIGBUS ... Original commit message from CVS: * gst-libs/gst/fft/_kiss_fft_guts_f32.h: * gst-libs/gst/fft/_kiss_fft_guts_f64.h: * gst-libs/gst/fft/_kiss_fft_guts_s16.h: * gst-libs/gst/fft/_kiss_fft_guts_s32.h: * gst-libs/gst/fft/kiss_fftr_f32.c: (kiss_fftr_f32_alloc): * gst-libs/gst/fft/kiss_fftr_f64.c: (kiss_fftr_f64_alloc): * gst-libs/gst/fft/kiss_fftr_s16.c: (kiss_fftr_s16_alloc): * gst-libs/gst/fft/kiss_fftr_s32.c: (kiss_fftr_s32_alloc): Use correct struct alignment everywhere to prevent unaligned memory accesses, resulting in SIGBUS on sparc and probably others. Fixes bug #500833. --- ChangeLog | 14 ++++++++++++++ gst-libs/gst/fft/_kiss_fft_guts_f32.h | 14 ++++++++++++++ gst-libs/gst/fft/_kiss_fft_guts_f64.h | 14 ++++++++++++++ gst-libs/gst/fft/_kiss_fft_guts_s16.h | 14 ++++++++++++++ gst-libs/gst/fft/_kiss_fft_guts_s32.h | 14 ++++++++++++++ gst-libs/gst/fft/kiss_fftr_f32.c | 10 +++++----- gst-libs/gst/fft/kiss_fftr_f64.c | 11 ++++++----- gst-libs/gst/fft/kiss_fftr_s16.c | 11 ++++++----- gst-libs/gst/fft/kiss_fftr_s32.c | 11 ++++++----- 9 files changed, 93 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 073c061522..b6b58ab6b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2009-01-16 Sebastian Dröge + + * gst-libs/gst/fft/_kiss_fft_guts_f32.h: + * gst-libs/gst/fft/_kiss_fft_guts_f64.h: + * gst-libs/gst/fft/_kiss_fft_guts_s16.h: + * gst-libs/gst/fft/_kiss_fft_guts_s32.h: + * gst-libs/gst/fft/kiss_fftr_f32.c: (kiss_fftr_f32_alloc): + * gst-libs/gst/fft/kiss_fftr_f64.c: (kiss_fftr_f64_alloc): + * gst-libs/gst/fft/kiss_fftr_s16.c: (kiss_fftr_s16_alloc): + * gst-libs/gst/fft/kiss_fftr_s32.c: (kiss_fftr_s32_alloc): + Use correct struct alignment everywhere to prevent unaligned + memory accesses, resulting in SIGBUS on sparc and probably others. + Fixes bug #500833. + 2009-01-16 Sebastian Dröge * gst-libs/gst/tag/gsttagdemux.c: (gst_tag_demux_srcpad_event): diff --git a/gst-libs/gst/fft/_kiss_fft_guts_f32.h b/gst-libs/gst/fft/_kiss_fft_guts_f32.h index 15dc06b414..78d2e2e81c 100644 --- a/gst-libs/gst/fft/_kiss_fft_guts_f32.h +++ b/gst-libs/gst/fft/_kiss_fft_guts_f32.h @@ -19,6 +19,20 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "kiss_fft_f32.h" #include +/* The 2*sizeof(size_t) alignment here is borrowed from + * GNU libc, so it should be good most everywhere. + * It is more conservative than is needed on some 64-bit + * platforms, but ia64 does require a 16-byte alignment. + * The SIMD extensions for x86 and ppc32 would want a + * larger alignment than this, but we don't need to + * do better than malloc. + * + * Borrowed from GLib's gobject/gtype.c + */ +#define STRUCT_ALIGNMENT (2 * sizeof (size_t)) +#define ALIGN_STRUCT(offset) \ + ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT) + #define MAXFACTORS 32 /* e.g. an fft of length 128 has 4 factors as far as kissfft is concerned diff --git a/gst-libs/gst/fft/_kiss_fft_guts_f64.h b/gst-libs/gst/fft/_kiss_fft_guts_f64.h index 276f65eab4..4202716d8c 100644 --- a/gst-libs/gst/fft/_kiss_fft_guts_f64.h +++ b/gst-libs/gst/fft/_kiss_fft_guts_f64.h @@ -19,6 +19,20 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "kiss_fft_f64.h" #include +/* The 2*sizeof(size_t) alignment here is borrowed from + * GNU libc, so it should be good most everywhere. + * It is more conservative than is needed on some 64-bit + * platforms, but ia64 does require a 16-byte alignment. + * The SIMD extensions for x86 and ppc32 would want a + * larger alignment than this, but we don't need to + * do better than malloc. + * + * Borrowed from GLib's gobject/gtype.c + */ +#define STRUCT_ALIGNMENT (2 * sizeof (size_t)) +#define ALIGN_STRUCT(offset) \ + ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT) + #define MAXFACTORS 32 /* e.g. an fft of length 128 has 4 factors as far as kissfft is concerned diff --git a/gst-libs/gst/fft/_kiss_fft_guts_s16.h b/gst-libs/gst/fft/_kiss_fft_guts_s16.h index 110ed7b4d7..2d8256e097 100644 --- a/gst-libs/gst/fft/_kiss_fft_guts_s16.h +++ b/gst-libs/gst/fft/_kiss_fft_guts_s16.h @@ -19,6 +19,20 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "kiss_fft_s16.h" #include +/* The 2*sizeof(size_t) alignment here is borrowed from + * GNU libc, so it should be good most everywhere. + * It is more conservative than is needed on some 64-bit + * platforms, but ia64 does require a 16-byte alignment. + * The SIMD extensions for x86 and ppc32 would want a + * larger alignment than this, but we don't need to + * do better than malloc. + * + * Borrowed from GLib's gobject/gtype.c + */ +#define STRUCT_ALIGNMENT (2 * sizeof (size_t)) +#define ALIGN_STRUCT(offset) \ + ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT) + #define MAXFACTORS 32 /* e.g. an fft of length 128 has 4 factors as far as kissfft is concerned diff --git a/gst-libs/gst/fft/_kiss_fft_guts_s32.h b/gst-libs/gst/fft/_kiss_fft_guts_s32.h index 5da7ec3963..f0ec9c0f44 100644 --- a/gst-libs/gst/fft/_kiss_fft_guts_s32.h +++ b/gst-libs/gst/fft/_kiss_fft_guts_s32.h @@ -19,6 +19,20 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "kiss_fft_s32.h" #include +/* The 2*sizeof(size_t) alignment here is borrowed from + * GNU libc, so it should be good most everywhere. + * It is more conservative than is needed on some 64-bit + * platforms, but ia64 does require a 16-byte alignment. + * The SIMD extensions for x86 and ppc32 would want a + * larger alignment than this, but we don't need to + * do better than malloc. + * + * Borrowed from GLib's gobject/gtype.c + */ +#define STRUCT_ALIGNMENT (2 * sizeof (size_t)) +#define ALIGN_STRUCT(offset) \ + ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT) + #define MAXFACTORS 32 /* e.g. an fft of length 128 has 4 factors as far as kissfft is concerned diff --git a/gst-libs/gst/fft/kiss_fftr_f32.c b/gst-libs/gst/fft/kiss_fftr_f32.c index 1f9fdb21a5..ea45206eb0 100644 --- a/gst-libs/gst/fft/kiss_fftr_f32.c +++ b/gst-libs/gst/fft/kiss_fftr_f32.c @@ -39,9 +39,8 @@ kiss_fftr_f32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) nfft >>= 1; kiss_fft_f32_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = - sizeof (struct kiss_fftr_f32_state) + subsize + - sizeof (kiss_fft_f32_cpx) * (nfft * 2); + memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_f32_state)) + + ALIGN_STRUCT (subsize) + sizeof (kiss_fft_f32_cpx) * (nfft * 2); if (lenmem == NULL) { st = (kiss_fftr_f32_cfg) KISS_FFT_F32_MALLOC (memneeded); @@ -53,8 +52,9 @@ kiss_fftr_f32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) if (!st) return NULL; - st->substate = (kiss_fft_f32_cfg) (st + 1); /*just beyond kiss_fftr_f32_state struct */ - st->tmpbuf = (kiss_fft_f32_cpx *) (((char *) st->substate) + subsize); + st->substate = (kiss_fft_f32_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_f32_state))); /*just beyond kiss_fftr_f32_state struct */ + st->tmpbuf = + (kiss_fft_f32_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize)); st->super_twiddles = st->tmpbuf + nfft; kiss_fft_f32_alloc (nfft, inverse_fft, st->substate, &subsize); diff --git a/gst-libs/gst/fft/kiss_fftr_f64.c b/gst-libs/gst/fft/kiss_fftr_f64.c index d566336d37..59587147ba 100644 --- a/gst-libs/gst/fft/kiss_fftr_f64.c +++ b/gst-libs/gst/fft/kiss_fftr_f64.c @@ -39,9 +39,9 @@ kiss_fftr_f64_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) nfft >>= 1; kiss_fft_f64_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = - sizeof (struct kiss_fftr_f64_state) + subsize + - sizeof (kiss_fft_f64_cpx) * (nfft * 2); + memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_f64_state)) + + ALIGN_STRUCT (subsize) + + sizeof (kiss_fft_f64_cpx) * (nfft * 2); if (lenmem == NULL) { st = (kiss_fftr_f64_cfg) KISS_FFT_F64_MALLOC (memneeded); @@ -53,8 +53,9 @@ kiss_fftr_f64_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) if (!st) return NULL; - st->substate = (kiss_fft_f64_cfg) (st + 1); /*just beyond kiss_fftr_f64_state struct */ - st->tmpbuf = (kiss_fft_f64_cpx *) (((char *) st->substate) + subsize); + st->substate = (kiss_fft_f64_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_f64_state))); /*just beyond kiss_fftr_f64_state struct */ + st->tmpbuf = + (kiss_fft_f64_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize)); st->super_twiddles = st->tmpbuf + nfft; kiss_fft_f64_alloc (nfft, inverse_fft, st->substate, &subsize); diff --git a/gst-libs/gst/fft/kiss_fftr_s16.c b/gst-libs/gst/fft/kiss_fftr_s16.c index 2df51557a2..803fa63380 100644 --- a/gst-libs/gst/fft/kiss_fftr_s16.c +++ b/gst-libs/gst/fft/kiss_fftr_s16.c @@ -39,9 +39,9 @@ kiss_fftr_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) nfft >>= 1; kiss_fft_s16_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = - sizeof (struct kiss_fftr_s16_state) + subsize + - sizeof (kiss_fft_s16_cpx) * (nfft * 2); + memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_s16_state)) + + ALIGN_STRUCT (subsize) + + sizeof (kiss_fft_s16_cpx) * (nfft * 2); if (lenmem == NULL) { st = (kiss_fftr_s16_cfg) KISS_FFT_S16_MALLOC (memneeded); @@ -53,8 +53,9 @@ kiss_fftr_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) if (!st) return NULL; - st->substate = (kiss_fft_s16_cfg) (st + 1); /*just beyond kiss_fftr_s16_state struct */ - st->tmpbuf = (kiss_fft_s16_cpx *) (((char *) st->substate) + subsize); + st->substate = (kiss_fft_s16_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_s16_state))); /*just beyond kiss_fftr_s16_state struct */ + st->tmpbuf = + (kiss_fft_s16_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize)); st->super_twiddles = st->tmpbuf + nfft; kiss_fft_s16_alloc (nfft, inverse_fft, st->substate, &subsize); diff --git a/gst-libs/gst/fft/kiss_fftr_s32.c b/gst-libs/gst/fft/kiss_fftr_s32.c index 47dc187036..e2a8541a5a 100644 --- a/gst-libs/gst/fft/kiss_fftr_s32.c +++ b/gst-libs/gst/fft/kiss_fftr_s32.c @@ -39,9 +39,9 @@ kiss_fftr_s32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) nfft >>= 1; kiss_fft_s32_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = - sizeof (struct kiss_fftr_s32_state) + subsize + - sizeof (kiss_fft_s32_cpx) * (nfft * 2); + memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_s32_state)) + + ALIGN_STRUCT (subsize) + + sizeof (kiss_fft_s32_cpx) * (nfft * 2); if (lenmem == NULL) { st = (kiss_fftr_s32_cfg) KISS_FFT_S32_MALLOC (memneeded); @@ -53,8 +53,9 @@ kiss_fftr_s32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) if (!st) return NULL; - st->substate = (kiss_fft_s32_cfg) (st + 1); /*just beyond kiss_fftr_s32_state struct */ - st->tmpbuf = (kiss_fft_s32_cpx *) (((char *) st->substate) + subsize); + st->substate = (kiss_fft_s32_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_s32_state))); /*just beyond kiss_fftr_s32_state struct */ + st->tmpbuf = + (kiss_fft_s32_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize)); st->super_twiddles = st->tmpbuf + nfft; kiss_fft_s32_alloc (nfft, inverse_fft, st->substate, &subsize);