Handling Exceptions in Slicing Java Programs
Learn how to extend System Dependence Graph based slicing to support exceptions in Java programs and handle errors effectively.
Handling Exceptions in Slicing Java Programs
E N D
Presentation Transcript
Slicing Java Programs thatThrow and Catch Exceptions Matthew Allen Susan Horwitz University of Wisconsin-Madison PEPM 2003 San Diego, CA June 7, 2003
Motivation • Exceptions: • Important error-handling technique • BUT: • Current program-slicing algorithms don’t handle exceptions • Goal: • Extend System Dependence Graph (SDG) based slicing to support exceptions Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Outline • Motivation • Program Slicing • Slicing with the SDG • Extending SDG-Based Slicing to Handle Exceptions • Related Work • Conclusions Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Program Slicing • A slice from a component S is the set of components that might affect: • Whether or how often S executes • The value of a variable used at S Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Slicing Example • int x, y; • void foo() { • fact(); • print(x); • print(y); • } • void fact() { • x = 1; • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Slicing Example • int x, y; • void foo() { • fact(); • print(x); • print(y); slice from here • } • void fact() { • x = 1; • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Slicing Example • int x, y; • void foo() { • fact(); • print(x); • print(y); slice from here • } • void fact() { • x = 1; • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Slicing Programs with Exceptions • Exceptions can affect: • Whether or how often a statement executes • Value of a variable used at a statement Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example: when/how often stmt executes • void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example: when/how often stmt executes • void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); Slice from here • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example: when/how often stmt executes • void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); Slice from here • } • } • void fact() throws NegEx{ • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); • } • catch (NegEx e) { • print(x); • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); • } • catch (NegEx e) { • print(x); Slice from here • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); • } • catch (NegEx e) { • print(x); Slice from here • } • } • void fact() throws NegEx{ • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); • } • catch (NegEx e) { • print(x); Slice from here • } • } • void fact() throws NegEx{ • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); Slice from here • } • catch (NegEx e) { • print(x); • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Exception affects value of variable • void foo() { • try { • fact(); • print(x); Slice from here • } • catch (NegEx e) { • print(x); • } • } • void fact() throws NegEx{ • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Outline • Motivation • Program Slicing • Slicing with the SDG (no exceptions) • Extending SDG-Based Slicing to Handle Exceptions • Related Work • Conclusions Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } print(x) print(y) exit foo while (y > 0) x = x * y enter foo CFGPDGSDG F T call fact enter fact F T x = 1 F T y-- exit
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } print(x) print(y) exit foo while (y > 0) x = x * y enter foo y = y_in CFGPDGSDG F T y_in = y call fact x = x_out y = y_out enter fact y = y_in F T x = 1 F T y-- x_out = x y_out = y exit
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } call fact T T T T enter foo T T T print(x) y_in = y x_out=x y_out=y x = x_out y = y_out print(y) enter fact x = x * y T T T T T y = y_in y=y_in T T y-- x = 1 while (y > 0) CFGPDGSDG Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } call fact enter foo print(x) y_in = y y_out=y x_out=x x = x_out y = y_out print(y) enter fact x = x * y y = y_in y=y_in y-- x = 1 while (y > 0) CFGPDGSDG Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } call fact enter foo print(x) y_in = y y_out=y x_out=x x = x_out enter fact x = x * y y = y_in y=y_in y-- x = 1 while (y > 0) CFGPDGSDG print(y) y = y_out Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } enter foo call fact call fact y = y_in enter foo print(x) y = y_out y_in = y y_in = y y_out=y x_out=x x = x_out enter fact x = x * y y = y_in y=y_in y-- x = 1 while (y > 0) CFGPDGSDG print(y) y = y_out Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void foo() { fact(); print(x); print(y); } static void fact() throws NegEx { x = 1; while (y > 0) { x = x * y; y--; } } enter foo call fact call fact y = y_in enter foo print(x) y = y_out y_in = y y_in = y y_out=y x_out=x x = x_out enter fact enter fact x = x * y y = y_in y=y_in y_out=y y=y_in while (y > 0) y-- x = 1 while (y > 0) y-- CFGPDGSDG print(y) y = y_out Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Outline • Motivation • Program Slicing • Slicing with the SDG • Extending SDG-Based Slicing to Handle Exceptions • Related Work • Conclusions Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example Revisited • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); Slice from here • } • } • static void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo y=y_in try call fact normal return catch (NegEx e) y_in=y x=x_out y=y_out print (“no error”) print (“error”) enter fact y=y_in x=1 if(y<0) x_out=x y_out=y throw new NegEx() while (y>0) normal exit NegEx exit x=x*y y-- Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo catch (NegEx e) if(y<0) throw new NegEx() NegEx exit y=y_in try call fact normal return catch (NegEx e) y_in=y x=x_out y=y_out print (“no error”) print (“error”) enter fact y=y_in x=1 if(y<0) x_out=x y_out=y throw new NegEx() while (y>0) normal exit NegEx exit x=x*y y-- Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
static void fact() • throws NegEx • { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter fact y = y_in throw new NegEx() x=1 if(y<0) while (y>0) x=x*y y-- F T • static void fact() • throws NegEx • { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } F T F T x_out = x y_out = y exit Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter fact y = y_in throw new NegEx() x=1 if(y<0) while (y>0) x=x*y y-- F T • static void fact() { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } F T NegEx exit F T x_out = x y_out = y exit Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter fact y = y_in throw new NegEx() NegEx exit x=1 if(y<0) while (y>0) x=x*y y-- F T • static void fact() { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } F T F T normal exit x_out = x y_out = y exit Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter fact y = y_in throw new NegEx() NegEx exit x=1 if(y<0) while (y>0) x=x*y y-- normal exit F T • static void fact() { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } F T F T F T x_out = x y_out = y exit Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter fact y = y_in throw new NegEx() NegEx exit x=1 if(y<0) while (y>0) x=x*y y-- normal exit F T • static void fact() { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } F T F T F T x_out = x y_out = y exit Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try y = y_in call fact catch (NegEx e) print (“no error”) print (“error”) x_out = x y_out = y exit • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try y = y_in call fact print (“no error”) print (“error”) x_out = x y_out = y exit • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } normal NegEx normal return catch (NegEx e) Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try y = y_in call fact normal return catch (NegEx e) print (“no error”) print (“error”) x_out = x y_out = y exit • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } normal NegEx F F T T Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try y = y_in call fact normal return catch (NegEx e) print (“no error”) print (“error”) x_out = x y_out = y exit • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } F T normal NegEx F F T T Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try y = y_in call fact normal return catch (NegEx e) print (“no error”) print (“error”) x_out = x y_out = y exit • static void foo() { • try { • fact(); • print(“no error”); • } • catch (NegEx e) { • print(“error”); • } • } F T normal NegEx F F T T Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example revisited • static void foo() { • try { • fact(); • print(x); • } • catch (NegEx e) { • print(x); Slice from here • } • } • static void fact() { • x = 1; • if (y < 0) • throw new NegEx (); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try x=x_out call fact normal return catch (NegEx e) enter fact x=1 if(y<0) throw new NegEx() while (y>0) normal exit x_out=x x=1 NegEx exit x=x*y y-- x=x*y y=y_in y_in=y y=y_out x=x_out print(x) print(x) y=y_in x_out=x y_out=y Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
enter foo try call fact normal return catch (NegEx e) print(x) print(x) enter fact x=1 if(y<0) throw new NegEx() while (y>0) normal exit NegEx exit x=x*y y-- y=y_in y_in=y x=x_out y=y_out x=x_out y=y_out y=y_in y_out=y x_out=x y_out=y x_out=x
enter foo try call fact normal return catch (NegEx e) x=x_out y=y_out y=y_out x=x_out print(x) enter fact x=1 x=1 if(y<0) x_out=x x_out=x y_out=y y_out=y throw new NegEx() while (y>0) normal exit NegEx exit x=x*y y-- x_out=x y=y_in y_in=y x=x_out print(x) y=y_in
enter foo normal return y=y_out y=y_out x=x_out print(x) x=1 x_out=x y_out=y y_out=y while (y>0) normal exit x=x*y y-- x_out=x y=y_in try call fact catch (NegEx e) y_in=y x=x_out print(x) enter fact y=y_in if(y<0) throw new NegEx() NegEx exit
Other Issues • finally clauses • Unchecked exceptions • See paper! Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Outline • Motivation • Program Slicing • Slicing with the SDG • Extending SDG-Based Slicing to Handle Exceptions • Related Work • Conclusions Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Related Work • [Sinha / Harrold / Rothermel 1999] • Addresses slicing programs with exceptions. • Some similar aspects . • Problems: • Does not correctly represent interprocedural control dependences when length of call chain from try to throw is greater than 1. • Does not address data dependences. • [Sinha / Harrold 1998, 2000] • Addresses handling finally clauses. • See paper. Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Conclusions • Slicing is an important operation. • Slicing Java programs is an area of current interest. • Contribution: • Extend SDG-based Slicing: • To correctly handle exceptions. • Changes only to CFG. • Same slicing algorithm! Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz
Example: when/how often stmt executes • void foo() { • try { • fact(); • print(“no error”); Slice from here • } • catch (NegEx e) { • print(“error”); • } • } • void fact() throws NegEx { • x = 1; • if (y < 0) • throw new NegEx(); • while (y > 0) { • x = x * y; • y--; • } • } Slicing Java Programs that Throw and Catch Exceptions – Allen / Horwitz