Bug 1266047 - Fix crash in mozilla::AudioBufferAddWithScale_SSE r=padenot draft
authorDan Minor <dminor@mozilla.com>
Wed, 20 Apr 2016 11:54:50 -0400
changeset 354324 0dff8258c4cc0d468c18267680f053ff1a240ad5
parent 354226 f05a1242fb29023bd7ebc492897ed3d6907733c7
child 518970 8a2cc1fcafd49c7d9a83bfa43187eae153f03b7e
push id16042
push userdminor@mozilla.com
push dateWed, 20 Apr 2016 17:00:06 +0000
reviewerspadenot
bugs1266047, 1266112
milestone48.0a1
Bug 1266047 - Fix crash in mozilla::AudioBufferAddWithScale_SSE r=padenot This adds alignment checks to fallback to scalar operations if the received buffers are not properly aligned. Bug 1266112 is the follow on to either fix the alignment problem or add a vector path for the aligned portion of the buffers. MozReview-Commit-ID: 5HCXzipXlqD
dom/media/webaudio/AudioNodeEngine.cpp
dom/media/webaudio/AudioNodeEngineSSE2.cpp
--- a/dom/media/webaudio/AudioNodeEngine.cpp
+++ b/dom/media/webaudio/AudioNodeEngine.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AudioNodeEngine.h"
 #ifdef BUILD_ARM_NEON
 #include "mozilla/arm.h"
 #include "AudioNodeEngineNEON.h"
 #endif
 #ifdef USE_SSE2
+#include "AlignmentUtils.h"
 #include "AudioNodeEngineSSE2.h"
 #endif
 
 namespace mozilla {
 
 already_AddRefed<ThreadSharedFloatArrayBufferList>
 ThreadSharedFloatArrayBufferList::Create(uint32_t aChannelCount,
                                          size_t aLength,
@@ -71,17 +72,20 @@ void AudioBufferAddWithScale(const float
 #ifdef BUILD_ARM_NEON
   if (mozilla::supports_neon()) {
     AudioBufferAddWithScale_NEON(aInput, aScale, aOutput, aSize);
     return;
   }
 #endif
 
 #ifdef USE_SSE2
-  if (mozilla::supports_sse2()) {
+  // TODO: See Bug 1266112, we should either fix the source of the unaligned
+  //       buffers or do as much as possible with vector instructions and
+  //       fallback to scalar instructions where necessary.
+  if (mozilla::supports_sse2() && IS_ALIGNED16(aInput) && IS_ALIGNED16(aOutput)) {
     AudioBufferAddWithScale_SSE(aInput, aScale, aOutput, aSize);
     return;
   }
 #endif
 
   if (aScale == 1.0f) {
     for (uint32_t i = 0; i < aSize; ++i) {
       aOutput[i] += aInput[i];
@@ -112,20 +116,23 @@ AudioBlockCopyChannelWithScale(const flo
 #ifdef BUILD_ARM_NEON
     if (mozilla::supports_neon()) {
       AudioBlockCopyChannelWithScale_NEON(aInput, aScale, aOutput);
       return;
     }
 #endif
 
 #ifdef USE_SSE2
-  if (mozilla::supports_sse2()) {
-    AudioBlockCopyChannelWithScale_SSE(aInput, aScale, aOutput);
-    return;
-  }
+    // TODO: See Bug 1266112, we should either fix the source of the unaligned
+    //       buffers or do as much as possible with vector instructions and
+    //       fallback to scalar instructions where necessary.
+    if (mozilla::supports_sse2() && IS_ALIGNED16(aInput) && IS_ALIGNED16(aOutput)) {
+      AudioBlockCopyChannelWithScale_SSE(aInput, aScale, aOutput);
+      return;
+    }
 #endif
 
     for (uint32_t i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
       aOutput[i] = aInput[i]*aScale;
     }
   }
 }
 
--- a/dom/media/webaudio/AudioNodeEngineSSE2.cpp
+++ b/dom/media/webaudio/AudioNodeEngineSSE2.cpp
@@ -1,15 +1,15 @@
 /* -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* this source code form is subject to the terms of the mozilla public
  * license, v. 2.0. if a copy of the mpl was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "AudioNodeEngineSSE2.h"
 #include "AlignmentUtils.h"
-#include "AudioNodeEngineSSE2.h"
 #include <emmintrin.h>
 
 
 namespace mozilla {
 void
 AudioBufferAddWithScale_SSE(const float* aInput,
                             float aScale,
                             float* aOutput,