From c3f291e9fef8dff4c5c1b92d03627fb07937ce29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 26 Jul 2024 11:31:46 +0200 Subject: [PATCH] Fix for possible overflow in the resampler (can prevent some audio clicks) Comment fixes --- Core/HW/StereoResampler.cpp | 8 +++++++- Core/MIPS/MIPS.h | 3 ++- SDL/README.TXT | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Core/HW/StereoResampler.cpp b/Core/HW/StereoResampler.cpp index b7fa658e4b..56c5bdc52d 100644 --- a/Core/HW/StereoResampler.cpp +++ b/Core/HW/StereoResampler.cpp @@ -165,7 +165,13 @@ void StereoResampler::Clear() { } inline int16_t MixSingleSample(int16_t s1, int16_t s2, uint16_t frac) { - return s1 + (((s2 - s1) * frac) >> 16); + int32_t value = s1 + (((s2 - s1) * frac) >> 16); + if (value < -32767) + return -32767; + else if (value > 32767) + return 32767; + else + return (int16_t)value; } // Executed from sound stream thread, pulling sound out of the buffer. diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index b2949465f4..4b53fe2e58 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -194,7 +194,8 @@ public: // Register-allocated JIT Temps don't get flushed so we don't reserve space for them. // However, the IR interpreter needs some temps that can stick around between ops. - // Can be indexed through r[] using indices 192+. + // Can be indexed through r[] using indices 192+, thanks to predictable struct layout. + // Unfortunately, UBSAN isn't too happy about these. u32 t[16]; //192 // If vfpuCtrl (prefixes) get mysterious values, check the VFPU regcache code. diff --git a/SDL/README.TXT b/SDL/README.TXT index 0d5e9451ae..d4115dca4a 100644 --- a/SDL/README.TXT +++ b/SDL/README.TXT @@ -25,3 +25,17 @@ Running with valgrind Here's an example where we both use suppressions, and generate new ones (that you can then take from suppressions.log and simplify and copy to valgrind-wsl2.supp): > valgrind --suppressions=SDL/valgrind-wsl2.supp --gen-suppressions=all --log-file=suppressions.log build/PPSSPPSDL + +Running with ASAN +================= + +./b.sh --sanitize + +build/PPSSPPSDL.app/Contents/MacOS/PPSSPPSDL + +Running with UBSAN +================== + +./b.sh --sanitize --sanitizeub + +UBSAN_OPTIONS=print_stacktrack=true build/PPSSPPSDL.app/Contents/MacOS/PPSSPPSDL