From 86daff8ae8dcef3029b243ae63d3ed69b82552cc Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Thu, 25 Nov 2021 13:19:11 +0100 Subject: [PATCH] [WASM] Fix invalid exception type for IOOB array access --- .../stdlib/wasm/builtins/kotlin/Array.kt | 7 ++- .../stdlib/wasm/builtins/kotlin/Arrays.kt | 56 +++++++++++++------ 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/libraries/stdlib/wasm/builtins/kotlin/Array.kt b/libraries/stdlib/wasm/builtins/kotlin/Array.kt index 98a40a67815..1872d4191e1 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Array.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Array.kt @@ -40,8 +40,10 @@ public class Array constructor(size: Int) { * where the behavior is unspecified. */ @Suppress("UNCHECKED_CAST") - public operator fun get(index: Int): T = - storage.get(index) as T + public operator fun get(index: Int): T { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) as T + } /** * Sets the array element at the specified [index] to the specified [value]. This method can @@ -54,6 +56,7 @@ public class Array constructor(size: Int) { * where the behavior is unspecified. */ public operator fun set(index: Int, value: T) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } diff --git a/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt b/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt index 6126576f0ff..52bedde2bc5 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt @@ -14,10 +14,13 @@ public class ByteArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Byte = - storage.get(index) + public operator fun get(index: Int): Byte { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Byte) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -41,10 +44,13 @@ public class CharArray(size: Int) { storage.fill(size) { init(it) } } - public operator fun get(index: Int): Char = - storage.get(index) + public operator fun get(index: Int): Char { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Char) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -69,10 +75,13 @@ public class ShortArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Short = - storage.get(index) + public operator fun get(index: Int): Short { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Short) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -97,10 +106,13 @@ public class IntArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Int = - storage.get(index) + public operator fun get(index: Int): Int { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Int) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -125,10 +137,13 @@ public class LongArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Long = - storage.get(index) + public operator fun get(index: Int): Long { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Long) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -152,10 +167,13 @@ public class FloatArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Float = - storage.get(index) + public operator fun get(index: Int): Float { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Float) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -179,10 +197,13 @@ public class DoubleArray(size: Int) { storage.fill(size, init) } - public operator fun get(index: Int): Double = - storage.get(index) + public operator fun get(index: Int): Double { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index) + } public operator fun set(index: Int, value: Double) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value) } @@ -206,10 +227,13 @@ public class BooleanArray(size: Int) { storage.fill(size) { init(it).toInt().reinterpretAsByte() } } - public operator fun get(index: Int): Boolean = - storage.get(index).reinterpretAsInt().reinterpretAsBoolean() + public operator fun get(index: Int): Boolean { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + return storage.get(index).reinterpretAsInt().reinterpretAsBoolean() + } public operator fun set(index: Int, value: Boolean) { + if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() storage.set(index, value.toInt().reinterpretAsByte()) }