A bug with automatic variables and finally

Tim Wilkinson tim at tjwassoc.co.uk
Wed Apr 30 14:58:47 PDT 1997


Gary Howland wrote:
> 
> This appears to be a bug:
> 
>     public class Finally
>     {
>         public Finally() {}
> 
>         public void
>         x()
>         {
>             int i = 3;
> 
>             try {
>                 ((Object)null).toString();
>             }
>             finally {
>                 System.out.println("i = "+i);
>             }
>         }
> 
>         public static void
>         main(String[] args)
>         {
>             new Finally().x();
>         }
>     }
> 
> When run, 'i' no longer has the value 3 it appears to be rubbish.
> The Sun JDK seems to work OK.

Gary,

This is a JIT related bug.  Essentially the value of 'i' is in a
register when the exception occurs (SEGV in this case) and is lost by
the time the relevant handler is found.  The solution to this problem is
unclear.  The simplest thing to do is to make sure registers are always
written back to memory before executing instructions which may cause
runtime exceptions.  You can then get cleverer by only writing back
values which might be needed in the relevant handlers (rather than
blindly writing then all) for the exception block you're currently in. 
Even so, this is going to hurt performance for no obvious gain.

I had a bit of a wade though my JVM manual and it is remarkably
unhelpful on how precise exceptions must be.  I'd like to believe they
allow for a 'relaxed' approach to runtime exceptions ... but I should be
so lucky.  Unfortunately, the flow analyses to do this correctly *and*
efficiently is non-trivial (sigh).

Tim
--
  Tim Wilkinson                         Tel/Fax: +44 181 440 0658
  T. J. Wilkinson & Associates,         Mobile:  +44 370 621006
  London, UK.                           Email:   tim at tjwassoc.co.uk



More information about the kaffe mailing list