Blame SOURCES/0001-Fix-several-memory-errors-in-the-SILK-resampler.patch

a612ca
From dc58579c2c7e060084554018e9a2e8c25097a255 Mon Sep 17 00:00:00 2001
a612ca
From: "Timothy B. Terriberry" <tterribe@xiph.org>
a612ca
Date: Wed, 8 May 2013 10:25:52 -0700
a612ca
Subject: [PATCH] Fix several memory errors in the SILK resampler.
a612ca
a612ca
1) The memcpy's were using sizeof(opus_int32), but the type of the
a612ca
    local buffer was opus_int16.
a612ca
2) Because the size was wrong, this potentially allowed the source
a612ca
    and destination regions of the memcpy overlap.
a612ca
   I _believe_ that nSamplesIn is at least fs_in_khZ, which is at
a612ca
    least 8.
a612ca
   Since RESAMPLER_ORDER_FIR_12 is only 8, I don't think that's a
a612ca
    problem once you fix the type size.
a612ca
3) The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the
a612ca
    data stored in it was actually _twice_ the input batch size
a612ca
    (nSamplesIn<<1).
a612ca
a612ca
Because this never blew up in testing, I suspect that in practice
a612ca
 the batch sizes are reasonable enough that none of these things
a612ca
 was ever a problem, but proving that seems non-obvious.
a612ca
a612ca
This patch just converts the whole thing to use CELT's vararrays.
a612ca
This fixes the buffer size problems (since we allocate a buffer
a612ca
 with the actual size we use) and gets these large buffers off the
a612ca
 stack on devices using the pseudo-stack.
a612ca
It also fixes the memcpy problems by changing the sizeof to
a612ca
 opus_int16.
a612ca
It turns out sFIR, which saved state between calls, was being used
a612ca
 elsewhere as opus_int32, so this converts it to a union to make
a612ca
 this sharing explicit.
a612ca
---
a612ca
 silk/resampler_private_IIR_FIR.c  | 14 +++++++++-----
a612ca
 silk/resampler_private_down_FIR.c |  4 ++--
a612ca
 silk/resampler_structs.h          |  5 ++++-
a612ca
 3 files changed, 15 insertions(+), 8 deletions(-)
a612ca
a612ca
diff --git a/silk/resampler_private_IIR_FIR.c b/silk/resampler_private_IIR_FIR.c
a612ca
index d9e42ca..2b9602d 100644
a612ca
--- a/silk/resampler_private_IIR_FIR.c
a612ca
+++ b/silk/resampler_private_IIR_FIR.c
a612ca
@@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
a612ca
 
a612ca
 #include "SigProc_FIX.h"
a612ca
 #include "resampler_private.h"
a612ca
+#include "stack_alloc.h"
a612ca
 
a612ca
 static inline opus_int16 *silk_resampler_private_IIR_FIR_INTERPOL(
a612ca
     opus_int16  *out,
a612ca
@@ -71,10 +72,13 @@ void silk_resampler_private_IIR_FIR(
a612ca
     silk_resampler_state_struct *S = (silk_resampler_state_struct *)SS;
a612ca
     opus_int32 nSamplesIn;
a612ca
     opus_int32 max_index_Q16, index_increment_Q16;
a612ca
-    opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + RESAMPLER_ORDER_FIR_12 ];
a612ca
+    VARDECL( opus_int16, buf );
a612ca
+    SAVE_STACK;
a612ca
+
a612ca
+    ALLOC( buf, 2 * S->batchSize + RESAMPLER_ORDER_FIR_12, opus_int16 );
a612ca
 
a612ca
     /* Copy buffered samples to start of buffer */
a612ca
-    silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
a612ca
+    silk_memcpy( buf, S->sFIR.i16, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
a612ca
 
a612ca
     /* Iterate over blocks of frameSizeIn input samples */
a612ca
     index_increment_Q16 = S->invRatio_Q16;
a612ca
@@ -91,13 +95,13 @@ void silk_resampler_private_IIR_FIR(
a612ca
 
a612ca
         if( inLen > 0 ) {
a612ca
             /* More iterations to do; copy last part of filtered signal to beginning of buffer */
a612ca
-            silk_memcpy( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
a612ca
+            silk_memcpy( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
a612ca
         } else {
a612ca
             break;
a612ca
         }
a612ca
     }
a612ca
 
a612ca
     /* Copy last part of filtered signal to the state for the next call */
a612ca
-    silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
a612ca
+    silk_memcpy( S->sFIR.i16, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
a612ca
+    RESTORE_STACK;
a612ca
 }
a612ca
-
a612ca
diff --git a/silk/resampler_private_down_FIR.c b/silk/resampler_private_down_FIR.c
a612ca
index 5d24564..8bedb0d 100644
a612ca
--- a/silk/resampler_private_down_FIR.c
a612ca
+++ b/silk/resampler_private_down_FIR.c
a612ca
@@ -155,7 +155,7 @@ void silk_resampler_private_down_FIR(
a612ca
     const opus_int16 *FIR_Coefs;
a612ca
 
a612ca
     /* Copy buffered samples to start of buffer */
a612ca
-    silk_memcpy( buf, S->sFIR, S->FIR_Order * sizeof( opus_int32 ) );
a612ca
+    silk_memcpy( buf, S->sFIR.i32, S->FIR_Order * sizeof( opus_int32 ) );
a612ca
 
a612ca
     FIR_Coefs = &S->Coefs[ 2 ];
a612ca
 
a612ca
@@ -185,5 +185,5 @@ void silk_resampler_private_down_FIR(
a612ca
     }
a612ca
 
a612ca
     /* Copy last part of filtered signal to the state for the next call */
a612ca
-    silk_memcpy( S->sFIR, &buf[ nSamplesIn ], S->FIR_Order * sizeof( opus_int32 ) );
a612ca
+    silk_memcpy( S->sFIR.i32, &buf[ nSamplesIn ], S->FIR_Order * sizeof( opus_int32 ) );
a612ca
 }
a612ca
diff --git a/silk/resampler_structs.h b/silk/resampler_structs.h
a612ca
index 4c28bd0..d1a0b95 100644
a612ca
--- a/silk/resampler_structs.h
a612ca
+++ b/silk/resampler_structs.h
a612ca
@@ -37,7 +37,10 @@ extern "C" {
a612ca
 
a612ca
 typedef struct _silk_resampler_state_struct{
a612ca
     opus_int32       sIIR[ SILK_RESAMPLER_MAX_IIR_ORDER ]; /* this must be the first element of this struct */
a612ca
-    opus_int32       sFIR[ SILK_RESAMPLER_MAX_FIR_ORDER ];
a612ca
+    union{
a612ca
+        opus_int32   i32[ SILK_RESAMPLER_MAX_FIR_ORDER ];
a612ca
+        opus_int16   i16[ SILK_RESAMPLER_MAX_FIR_ORDER ];
a612ca
+    }                sFIR;
a612ca
     opus_int16       delayBuf[ 48 ];
a612ca
     opus_int         resampler_function;
a612ca
     opus_int         batchSize;
a612ca
-- 
a612ca
1.8.4.2
a612ca