Lecture 1: Introduction to Program Analysis
17-355/17-655/17-819: Program Analysis Claire Le Goues January 14, 2020
* Course materials developed with Jonathan Aldrich
1 (c) 2020 C. Le Goues
Lecture 1: Introduction to Program Analysis 17-355/17-655/17-819: - - PowerPoint PPT Presentation
Lecture 1: Introduction to Program Analysis 17-355/17-655/17-819: Program Analysis Claire Le Goues January 14, 2020 * Course materials developed with Jonathan Aldrich (c) 2020 C. Le Goues 1 Learning objectives Provide a high level
17-355/17-655/17-819: Program Analysis Claire Le Goues January 14, 2020
* Course materials developed with Jonathan Aldrich
1 (c) 2020 C. Le Goues
examples of why it is useful.
syllabus.
behind AST walkers for simple bug-finding analyses.
translate between WHILE and While3Addr.
2 (c) 2020 C. Le Goues
determine its properties.
programming language.
3 (c) 2020 C. Le Goues
implications for:
4 (c) 2020 C. Le Goues
5 (c) 2020 C. Le Goues
6 (c) 2020 C. Le Goues
https://github.com/marketplace?category=code-quality
7 (c) 2020 C. Le Goues
8 (c) 2020 C. Le Goues
9 (c) 2020 C. Le Goues
10 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
ERROR: function returns with interrupts disabled!
11 (c) 2020 C. Le Goues
6. | { restore_flags(flags); } ;
10. | enable è { err(“double enable”); } 11.;
13. | disable è { err(“double disable”); } 14.//special pattern that matches when 15.// end of path is reached in this state 16. | $end_of_path$ è 17. { err(“exiting with inter disabled!”); } 18.; 19.}
is_enabled is_disabled disable enable enable è err(double enable) end path è err(exiting with inter disabled)
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
disable è err(double disable)
12 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
Initial state: is_enabled
13 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
Transition to: is_disabled
14 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
Final state: is_disabled
15 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
Transition to: is_enabled
16 (c) 2020 C. Le Goues
4. int b_size) { 5. struct buffer_head *bh; 6. unsigned long flags; 7. save_flags(flags); 8. cli(); // disables interrupts 9. if ((bh = sh->buffer_pool) == NULL) 10. return NULL; 11. sh->buffer_pool = bh -> b_next; 12. bh->b_size = b_size; 13. restore_flags(flags); // re-enables interrupts 14. return bh; 15.}
Example from Engler et al., Checking system rules Using System-Specific, Programmer-Written Compiler Extensions, OSDI ‘000
Final state: is_enabled
17 (c) 2020 C. Le Goues
the program.
18 (c) 2020 C. Le Goues
determine its properties.
programming language.
19 (c) 2020 C. Le Goues
20 (c) 2020 C. Le Goues
Assume that you have a function that can determine if a program p has some nontrivial property (like divides_by_zero): 1. int silly(program p, input i) { 2. p(i); 3. return 5/0; 4. } 5. bool halts(program p, input i) { 6. return divides_by_zero(`silly(p,i)`); 7. }
21 (c) 2020 C. Le Goues
Error exists No error exists Error Reported True positive (correct analysis result) False positive No Error Reported False negative True negative (correct analysis result)
Sound Analysis: reports all defects
typically overapproximated Complete Analysis: every reported defect is an actual defect
typically underapproximated
22 (c) 2020 C. Le Goues
Sound Analysis All Defects Complete Analysis
Unsound and Incomplete Analysis
23 (c) 2020 C. Le Goues
24 (c) 2020 C. Le Goues
https://yanniss.github.io/Soundiness-CACM.pdf
determine its properties.
programming language.
25 (c) 2020 C. Le Goues
determine its properties.
§ Testing: Direct execution of code on test data in a controlled environment. § Analysis: Tools extracting data from test runs.
§ Inspection: Human evaluation of code, design documents (specs and models), modifications. § Analysis: Tools reasoning about the program without executing it.
26 (c) 2020 C. Le Goues
to reason about possible program behavior.
arguments about program behavior.
executions paths simultaneously.
exhaustively about possible program states.
27 (c) 2020 C. Le Goues
underlying language.
28 (c) 2020 C. Le Goues
29 (c) 2020 C. Le Goues
30 (c) 2020 C. Le Goues
Piazza.
matter is sensitive.
point of Piazza.
31 (c) 2020 C. Le Goues
ASAP.
32 (c) 2020 C. Le Goues
taking and whether it is interfacing badly with other courses.
me an email, and go to bed.
33 (c) 2020 C. Le Goues
determine its properties.
programming language.
34 (c) 2020 C. Le Goues
grammar.
as strings of characters.
generators
learn how to parse for real.)
35 (c) 2020 C. Le Goues
statements
arithmetic expressions
variables
number literals
boolean predicates
statement addresses (line numbers)
::= x := a | skip | S1 ; S2
| if P then S1 else S2 | while P do S
::= x | n | a1 opa a2
::= true | false | not P | P1 opb P2 | a1 opr a2
Concrete syntax is similar, but adds things like (parentheses) for disambiguation during parsing
36 (c) 2020 C. Le Goues
37 (c) 2020 C. Le Goues
38 (c) 2020 C. Le Goues
39 (c) 2020 C. Le Goues
patterns.
logic programming, or query languages.
40 (c) 2020 C. Le Goues
For each instruction I in the program if I is a shift instruction if (type of I’s left operand is int && I’s right operand is a constant && value of constant < 0 or > 31) warn(“Shifting by less than 0 or more than 31 is meaningless”)
41 (c) 2020 C. Le Goues
42
https://help.semmle.com/wiki/display/JAVA/Inefficient+empty+string+test
(c) 2020 C. Le Goues
43 (c) 2020 C. Le Goues
44 (c) 2020 C. Le Goues
when string concatenation occurs in a loop
45 (c) 2020 C. Le Goues
statements
arithmetic expressions
variables
number literals
boolean predicates
statement addresses (line numbers)
::= x := a | skip | S1 ; S2
| if P then S1 else S2 | while P do S
::= x | n | a1 opa a2
::= true | false | not P | P1 opb P2 | a1 opr a2
46 (c) 2020 C. Le Goues
instructions
variables
number literals
::= x := n | x := y | x := y op z
47 (c) 2020 C. Le Goues
instructions
variables
number literals
::= x := n | x := y | x := y op z
48 (c) 2020 C. Le Goues
::= x := n | x := y | x := y op z | goto n | if x opr 0 goto n | x := f(y) | return x | x := y.m(z) | x := &p | x := *p | *p := x | x := y.f | x.f := y
49 (c) 2020 C. Le Goues
50 (c) 2020 C. Le Goues