From 8a2fa6c14c47cf6897f252ab9b04e9ff26ebb3e2 Mon Sep 17 00:00:00 2001 From: Alexander Zuev Date: Wed, 26 Feb 2025 13:38:11 -0800 Subject: [PATCH 1/4] 8350813: Rendering of bulky sound bank from MIDI sequence can cause OutOfMemoryError Check that the calculated audio data size does not exceed available heap memory before committing to the rendering; Add a minimal test case; --- .../media/sound/AudioFileSoundbankReader.java | 9 +++- .../midi/BulkSoundBank/BulkSoundBank.java | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java diff --git a/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java b/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java index 6431c06a90687..a37acbb8a3abb 100644 --- a/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java +++ b/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -90,6 +90,13 @@ public Soundbank getSoundbank(AudioInputStream ais) "Can not allocate enough memory to read audio data."); } + long maximumHeapSize = Runtime.getRuntime().maxMemory() - + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); + if (totalSize > maximumHeapSize) { + throw new InvalidMidiDataException( + "Insufficient heap size to render audio data."); + } + if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)]; diff --git a/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java new file mode 100644 index 0000000000000..07e62df86ae96 --- /dev/null +++ b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.midi.InvalidMidiDataException; +import javax.sound.midi.MidiSystem; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +/** + * @test + * @bug 8350813 + * @summary Rendering of bulky sound bank from MIDI sequence can cause OutOfMemoryError. + * @run main/othervm -Xmx1g BulkSoundBank + */ + +public class BulkSoundBank { + static final byte[] midi = {77, 84, 104, 100, 0, 0, 0, 6, 0, 0, 0, 1, 1, + -32, 77, 84, 114, 107, 0, 0, 0, 50, 0, -1, 88, 4, 4, 2, 24, 8, 0, -1, + 81, 3, 7, -95, 32, 0, -112, 60, 64, -125, 96, -128, 60, 64, -125, -44, + -51, 32, -112, 48, 64, 1, -128, 48, 64, -127, -64, -45, 127, -112, 60, + 64, 1, -128, 60, 64, 0, -1, 47, 0}; + + public static void main(String[] args) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(midi); + try { + MidiSystem.getSoundbank(bis); + } catch (InvalidMidiDataException imda) { + System.out.println("Caught InvalidMidiDataException as expected"); + return; + } + throw new RuntimeException("Test should throw InvalidMidiDataException but it did not."); + } +} + From e48e16a1cf91b5a8bdd219e4650f4ad061e15657 Mon Sep 17 00:00:00 2001 From: Alexander Zuev Date: Mon, 3 Mar 2025 09:59:56 -0800 Subject: [PATCH 2/4] Update test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java Use try with resources clause in the test. Co-authored-by: Harshitha Onkar --- test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java index 07e62df86ae96..f6bce296a1103 100644 --- a/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java +++ b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java @@ -41,14 +41,13 @@ public class BulkSoundBank { 64, 1, -128, 60, 64, 0, -1, 47, 0}; public static void main(String[] args) throws IOException { - ByteArrayInputStream bis = new ByteArrayInputStream(midi); - try { + try (ByteArrayInputStream bis = new ByteArrayInputStream(midi)) { MidiSystem.getSoundbank(bis); + throw new RuntimeException("Test should throw InvalidMidiDataException" + + " but it did not."); } catch (InvalidMidiDataException imda) { System.out.println("Caught InvalidMidiDataException as expected"); - return; } - throw new RuntimeException("Test should throw InvalidMidiDataException but it did not."); } } From e02c586349053f14fafb700dfd20e8dfd3cdff98 Mon Sep 17 00:00:00 2001 From: Alexander Zuev Date: Mon, 3 Mar 2025 10:42:53 -0800 Subject: [PATCH 3/4] Remove extra whitespace --- test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java index f6bce296a1103..259b5b5283c4f 100644 --- a/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java +++ b/test/jdk/javax/sound/midi/BulkSoundBank/BulkSoundBank.java @@ -43,7 +43,7 @@ public class BulkSoundBank { public static void main(String[] args) throws IOException { try (ByteArrayInputStream bis = new ByteArrayInputStream(midi)) { MidiSystem.getSoundbank(bis); - throw new RuntimeException("Test should throw InvalidMidiDataException" + throw new RuntimeException("Test should throw InvalidMidiDataException" + " but it did not."); } catch (InvalidMidiDataException imda) { System.out.println("Caught InvalidMidiDataException as expected"); From a990c8227ef1b4ffe6ed6d8189cc03913b3bc26b Mon Sep 17 00:00:00 2001 From: Alexander Zuev Date: Wed, 5 Mar 2025 13:25:11 -0800 Subject: [PATCH 4/4] Add a treshold of 90% to avoid OOME because of the additional buffers needed to process sound bank. --- .../classes/com/sun/media/sound/AudioFileSoundbankReader.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java b/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java index a37acbb8a3abb..dbc58966973dc 100644 --- a/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java +++ b/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java @@ -90,8 +90,8 @@ public Soundbank getSoundbank(AudioInputStream ais) "Can not allocate enough memory to read audio data."); } - long maximumHeapSize = Runtime.getRuntime().maxMemory() - - (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); + long maximumHeapSize = (long) ((Runtime.getRuntime().maxMemory() - + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())) * 0.9); if (totalSize > maximumHeapSize) { throw new InvalidMidiDataException( "Insufficient heap size to render audio data.");