From bf08cc4c53de1daacf8a77084e8bff0b0f6158ee Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 30 Apr 2008 11:47:25 -0600 Subject: [PATCH] fix array bounds checks --- src/compile.cpp | 11 ++--- test/Misc.java | 110 +++++++++++++++++++++++++++--------------------- 2 files changed, 68 insertions(+), 53 deletions(-) diff --git a/src/compile.cpp b/src/compile.cpp index 9a870fb15b..b0721925a2 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -1517,7 +1517,8 @@ void NO_RETURN FORCE_ALIGN throwArrayIndexOutOfBounds(MyThread* t, object array, int32_t index) { object message = makeString - (t, "array of length %d indexed at %d", arrayLength(t, array), index); + (t, "array of length %d indexed at %d", + cast(array, BytesPerWord), index); t->exception = makeArrayIndexOutOfBoundsException(t, message); unwind(t); } @@ -1794,7 +1795,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip, } c->cmp(BytesPerWord, index, c->memory(array, ArrayLength, 0, 1)); - c->jge(load); + c->jg(load); if (not c->isConstant(index)) { c->mark(throw_); @@ -1911,7 +1912,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip, } c->cmp(BytesPerWord, index, c->memory(array, ArrayLength, 0, 1)); - c->jge(store); + c->jg(store); if (not c->isConstant(index)) { c->mark(throw_); @@ -3875,11 +3876,11 @@ finish(MyThread* t, Context* context) strcmp (reinterpret_cast (&byteArrayBody(t, className(t, methodClass(t, context->method)), 0)), - "org/eclipse/swt/graphics/ImageData") == 0 and + "Misc") == 0 and strcmp (reinterpret_cast (&byteArrayBody(t, methodName(t, context->method), 0)), - "") == 0) + "main") == 0) { asm("int3"); } diff --git a/test/Misc.java b/test/Misc.java index 1fa2c2d9c5..259a87b5b4 100644 --- a/test/Misc.java +++ b/test/Misc.java @@ -99,30 +99,32 @@ public class Misc { expect(Long.valueOf(231L) == 231L); - long x = 231; - expect((x >> 32) == 0); - expect((x >>> 32) == 0); - expect((x << 32) == 992137445376L); + { long x = 231; + expect((x >> 32) == 0); + expect((x >>> 32) == 0); + expect((x << 32) == 992137445376L); - int shift = 32; - expect((x >> shift) == 0); - expect((x >>> shift) == 0); - expect((x << shift) == 992137445376L); + int shift = 32; + expect((x >> shift) == 0); + expect((x >>> shift) == 0); + expect((x << shift) == 992137445376L); - long y = -231; - expect((y >> 32) == 0xffffffffffffffffL); - expect((y >>> 32) == 0xffffffffL); + long y = -231; + expect((y >> 32) == 0xffffffffffffffffL); + expect((y >>> 32) == 0xffffffffL); + } - byte[] array = new byte[8]; - putLong(231, array, 0); - expect((array[0] & 0xff) == 0); - expect((array[1] & 0xff) == 0); - expect((array[2] & 0xff) == 0); - expect((array[3] & 0xff) == 0); - expect((array[4] & 0xff) == 0); - expect((array[5] & 0xff) == 0); - expect((array[6] & 0xff) == 0); - expect((array[7] & 0xff) == 231); + { byte[] array = new byte[8]; + putLong(231, array, 0); + expect((array[0] & 0xff) == 0); + expect((array[1] & 0xff) == 0); + expect((array[2] & 0xff) == 0); + expect((array[3] & 0xff) == 0); + expect((array[4] & 0xff) == 0); + expect((array[5] & 0xff) == 0); + expect((array[6] & 0xff) == 0); + expect((array[7] & 0xff) == 231); + } java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(8); buffer.putLong(231); @@ -133,37 +135,49 @@ public class Misc { ClassLoader.getSystemClassLoader().toString(); - int a = 2; - int b = 2; - int c = a + b; - - Misc m = new Misc(); - m.toString(); - - String s = "hello"; - m.foo(s); - m.bar(s); - baz(s); - - m.sync(); - syncStatic(false); - try { - syncStatic(true); - } catch (RuntimeException e) { - e.printStackTrace(); + { int a = 2; + int b = 2; + int c = a + b; } - int d = alpha; - beta = 42; - alpha = 43; - int e = beta; - int f = alpha; - m.gamma = 44; + { Misc m = new Misc(); + m.toString(); - expect(beta == 42); - expect(alpha == 43); - expect(m.gamma == 44); + String s = "hello"; + m.foo(s); + m.bar(s); + baz(s); + + m.sync(); + syncStatic(false); + try { + syncStatic(true); + } catch (RuntimeException e) { + e.printStackTrace(); + } + + int d = alpha; + beta = 42; + alpha = 43; + int e = beta; + int f = alpha; + m.gamma = 44; + + expect(beta == 42); + expect(alpha == 43); + expect(m.gamma == 44); + } zip(); + + int[] array = new int[0]; + Exception exception = null; + try { + int x = array[0]; + } catch (ArrayIndexOutOfBoundsException e) { + exception = e; + } + + expect(exception != null); } }