Skip to content

Commit d6a2624

Browse files
committed
Address review: check aastore bounds before the store type check
JVMS orders aastore checks as NullPointer, then ArrayIndexOutOfBounds, then ArrayStore. The component-type check now runs after an explicit upper-bound check, so an out-of-bounds store of an incompatible value throws ArrayIndexOutOfBoundsException rather than ArrayStoreException.
1 parent 75f695f commit d6a2624

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

jvm_rust/src/interpreter.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ impl Interpreter {
106106
return Err(jvm.exception("java/lang/NullPointerException", "Array is null").await);
107107
}
108108

109+
// JVMS: aastore reports an out-of-bounds index before any ArrayStoreException type check
110+
let length = jvm.array_length(array.as_ref().unwrap()).await?;
111+
if index as usize >= length {
112+
return Err(jvm.exception("java/lang/ArrayIndexOutOfBoundsException", &format!("{}", index)).await);
113+
}
114+
109115
let element_type = jvm.array_element_type(array.as_ref().unwrap()).await?;
110116

111117
// operand stack has only integer, so convert it to the correct type

test_data/ArrayStore.class

214 Bytes
Binary file not shown.

test_data/ArrayStore.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ ase
33
object-array-ok
44
nested-ok
55
ase2
6+
aioobe

test_data/src/ArrayStore.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public static void main(String[] args) {
1515
o[1] = "any";
1616
System.out.println("object-array-ok");
1717

18-
// nested: String[][] holding String[] is fine, holding Integer[] is not
1918
Object[] m = new String[1][];
2019
m[0] = new String[3];
2120
System.out.println("nested-ok");
@@ -25,5 +24,16 @@ public static void main(String[] args) {
2524
} catch (ArrayStoreException e) {
2625
System.out.println("ase2");
2726
}
27+
28+
// out-of-bounds AND incompatible: bounds check must win
29+
Object[] z = new String[0];
30+
try {
31+
z[1] = Integer.valueOf(1);
32+
System.out.println("no-ex");
33+
} catch (ArrayIndexOutOfBoundsException e) {
34+
System.out.println("aioobe");
35+
} catch (ArrayStoreException e) {
36+
System.out.println("ase-wrong");
37+
}
2838
}
2939
}

0 commit comments

Comments
 (0)