[Soot-list] exception handler lookup

John Jorgensen jorgnsn at lcd.uregina.ca
Wed Mar 15 00:00:40 EST 2006


>>>>> "gdennis" == Greg Dennis <gdennis at mit.edu> writes:

    gdennis> Specifically, given a unit 'u' in a method 'm'
    gdennis> and the runtime type of an exception 'e', I would
    gdennis> like to know the handler unit in m that would that
    gdennis> would be the successor of u if u throws e. If there
    gdennis> is no such handler in m -- if m does not catch e at
    gdennis> u -- I do *not* need to know the potential handlers
    gdennis> in m's callers. Just a special unknown value in the
    gdennis> latter case would do.

There is support for this in the code that generates CFGs, though
I don't remember the code well enough to give detailed
step-by-step instructions (i.e. there are probably mistakes
below, but the gist is right).

Essentially, given a Body b for your method m:

   ExceptionalUnitGraph g = new ExceptionalUnitGraph(b);
   for (Iterator destIt = g.getExceptionDests(u); destIt.hasNext(); ) {
     ExceptionalGraph.ExceptionDest dest = 
       (ExceptionalGraph.ExceptionDest) destIt.next();
     Unit handlerStart = (Unit) dest.getHandlerNode(); 
          // first unit in handler, null if exception escapes method
     ThrowableSet throwables = dest.getThrowables();
          // representation of the set of Throwable types caught
          // by this handler
     if (throwables.catchableAs(e)) {
       // handlerStart catches e
     }
   }

This is documented in the javadoc for
soot.toolkits.graph.ExceptionalUnitGraph, with grimy details
about how exceptional control flow is analyzed in

  http://www.sable.mcgill.ca/publications/techreports/#report2003-3

I looked at the source to src/soot/util/cfgcmd/CFGToDotGraph.java
to remind myself how this works. CFGToDotGraph is part of the
code which produces visualizations of CFGs using graphviz, and it
probably provides the clearest example of the use of
ExceptionDest (the other places within Soot which use
ExceptionDest are the classes which actually generate CFGs,
src/soot/toolkits/graph/{ExceptionalBlockGraph.java,ExceptionalUnitGraph.java}
and src/soot/toolkits/exceptions/TraphTightener.java).

If you have graphviz installed, it may be worthwhile generating a
few visualizations using

  java soot.tools.CFGViewer --soot-classpath  <soot-class-path> \
     -f none --graph=ExceptionalUnitGraph --show-exception-dests \
     --throw-analysis unit <analyzed-class-name>

to generate dot files that provide visualizations of the
ExceptionDests and the corresponding CFG edges (exception
destinations and CFG edges are different: if u throws an
exception that is caught by h, there will be CFG edges from the
_predecessors_ of u to h; this is explained in the javadoc, in
the technical report, and in a couple threads in the soot-list
archives).



More information about the Soot-list mailing list