shithub: libsamplerate

Download patch

ref: 8c325e35494ba9d2a5578d4a6702691884b85f0f
parent: 9fb7f432edca0386a624f41b89e4c0115e6c36df
author: Erik de Castro Lopo <erikd@mega-nerd.com>
date: Sat Aug 24 13:15:00 EDT 2019

Extend varispeed test to trigger out-of-bounds buffer read

Dramatic decreases in the SRC ratio can cause the SINC converters to read
from a location before the start of the input data buffer. Debugging this
was made easier by use of the ASAN sanitizer.

Inspired by a PR from Alexander Grund <alexander.grund@tu-dresden.de>

--- a/tests/util.h
+++ b/tests/util.h
@@ -29,6 +29,12 @@
 
 const char * get_cpu_name (void) ;
 
+#define ASSERT(condition) \
+	if (!(condition)) \
+	{	printf ("Condition failed on Line %d : %s\n\n", __LINE__, #condition) ; \
+		exit (1) ; \
+	    } ;
+
 #if OS_IS_WIN32
 /*
 **	Extra Win32 hacks.
--- a/tests/varispeed_test.c
+++ b/tests/varispeed_test.c
@@ -23,23 +23,47 @@
 #define fftw_cleanup()
 #endif
 
-#define	BUFFER_LEN		(1 << 16)
+#define	BUFFER_LEN		(1 << 15)
 
 static void varispeed_test (int converter, double target_snr) ;
+static void varispeed_bounds_test (int converter) ;
+static void set_ratio_test (int converter, int channels, double initial_ratio, double second_ratio) ;
 
 int
 main (void)
 {
-	puts ("") ;
-	printf ("    Zero Order Hold interpolator    : ") ;
+	puts ("\n    Varispeed SNR test") ;
+	printf ("        Zero Order Hold interpolator    : ") ;
+	fflush (stdout) ;
 	varispeed_test (SRC_ZERO_ORDER_HOLD, 10.0) ;
+	puts ("ok") ;
 
-	printf ("    Linear interpolator             : ") ;
+	printf ("        Linear interpolator             : ") ;
+	fflush (stdout) ;
 	varispeed_test (SRC_LINEAR, 10.0) ;
+	puts ("ok") ;
 
-	printf ("    Sinc interpolator               : ") ;
+	printf ("        Sinc interpolator               : ") ;
+	fflush (stdout) ;
 	varispeed_test (SRC_SINC_FASTEST, 115.0) ;
+	puts ("ok") ;
 
+	puts ("\n    Varispeed bounds test") ;
+	printf ("        Zero Order Hold interpolator    : ") ;
+	fflush (stdout) ;
+	varispeed_bounds_test (SRC_ZERO_ORDER_HOLD) ;
+	puts ("ok") ;
+
+	printf ("        Linear interpolator             : ") ;
+	fflush (stdout) ;
+	varispeed_bounds_test (SRC_LINEAR) ;
+	puts ("ok") ;
+
+	printf ("        Sinc interpolator               : ") ;
+	fflush (stdout) ;
+	varispeed_bounds_test (SRC_SINC_FASTEST) ;
+	puts ("ok") ;
+
 	fftw_cleanup () ;
 	puts ("") ;
 
@@ -65,7 +89,7 @@
 
 	/* Perform sample rate conversion. */
 	if ((src_state = src_new (converter, 1, &error)) == NULL)
-	{	printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
+	{	printf ("\n\nLine %d : src_new () failed : %s\n\n", __LINE__, src_strerror (error)) ;
 		exit (1) ;
 		} ;
 
@@ -144,8 +168,104 @@
 		exit (1) ;
 		} ;
 
-	puts ("ok") ;
-
 	return ;
 } /* varispeed_test */
 
+static void
+varispeed_bounds_test (int converter)
+{	double ratios [] = { 0.1, 0.01, 20 } ;
+	int chan, r1, r2 ;
+
+	for (chan = 1 ; chan <= 9 ; chan ++)
+		for (r1 = 0 ; r1 < ARRAY_LEN (ratios) ; r1++)
+			for (r2 = 0 ; r2 < ARRAY_LEN (ratios) ; r2 ++)
+				if (r1 != r2)
+					set_ratio_test (converter, chan, ratios [r1], ratios [r2]) ;
+} /* varispeed_bounds_test */
+
+static void
+set_ratio_test (int converter, int channels, double initial_ratio, double second_ratio)
+{	const int total_input_frames = BUFFER_LEN ;
+	/* Maximum upsample ratio is 20, use a value beigger. */
+	const int total_output_frames = 25 * BUFFER_LEN ;
+
+	/* Interested in array boundary conditions, so all zero data here is fine. */
+	float *input = calloc (total_input_frames * channels, sizeof (float)) ;
+	float *output = calloc (total_output_frames * channels, sizeof (float)) ;
+
+	char details [128] ;
+
+	const int max_loop_count = 100000 ;
+	const int chunk_size = 128 ;
+
+	SRC_STATE *src_state ;
+	SRC_DATA src_data ;
+
+	int error, k, total_frames_used, total_frames_gen ;
+
+	snprintf (details, sizeof (details), "%d channels, ratio %g -> %g", channels, initial_ratio, second_ratio) ;
+
+	if ((src_state = src_new (converter, channels, &error)) == NULL)
+	{	printf ("\n\nLine %d : src_new () failed : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	total_frames_used = 0 ;
+	total_frames_gen = 0 ;
+
+	memset (&src_data, 0, sizeof (src_data)) ;
+	src_data.end_of_input = 0 ;
+	src_data.src_ratio = initial_ratio ;
+	src_data.data_in = input ;
+	src_data.data_out = output ;
+	src_data.input_frames = chunk_size ;
+	src_data.output_frames = total_output_frames ;
+
+	/* Process one chunk at initial_ratio. */
+	if ((error = src_process (src_state, &src_data)))
+	{	printf ("\n\nLine %d : %s : %s\n\n", __LINE__, details, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	/* Now hard switch to second_ratio ... */
+	src_data.src_ratio = second_ratio ;
+	if ((error = src_process (src_state, &src_data)))
+	{	printf ("\n\nLine %d : %s : %s\n\n", __LINE__, details, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	/* ... and process the remaining. */
+	for (k = 0 ; k < max_loop_count ; k ++)
+	{	if ((error = src_process (src_state, &src_data)) != 0)
+		{	printf ("\n\nLine %d : %s : %s\n\n", __LINE__, details, src_strerror (error)) ;
+			exit (1) ;
+			} ;
+
+		if (src_data.end_of_input && src_data.output_frames_gen == 0)
+			break ;
+
+		total_frames_used	+= src_data.input_frames_used ;
+		total_frames_gen 	+= src_data.output_frames_gen ;
+
+		src_data.data_in	+= src_data.input_frames_used * channels ;
+		src_data.data_out	+= src_data.output_frames_gen * channels ;
+
+		src_data.input_frames	= total_input_frames - total_frames_used ;
+		src_data.output_frames	= total_output_frames - total_frames_gen ;
+
+		src_data.end_of_input = total_frames_used >= total_input_frames ? 1 : 0 ;
+		} ;
+
+	ASSERT (k < max_loop_count) ;
+	ASSERT (total_frames_gen > 0) ;
+
+	for (k = 0 ; k < total_frames_gen * channels ; k ++)
+		ASSERT (! isnan (output [k])) ;
+
+	src_state = src_delete (src_state) ;
+
+	free (input) ;
+	free (output) ;
+
+	return ;
+} /* set_ratio_test */