270 likes | 297 Views
Explore the realm of semantic analysis in compilers, focusing on error checking, bug hunting, and prevention of nonsensical code execution. Understand static vs. dynamic analysis and the limits of error checking. Delve into Rice's Theorem and bug detection evaluation.
E N D
Lecture 15 Semantic Analysis KU | Fall 2019 | Drew Davidson
AnnouncementsLecture 15 P3 Code Out Tonight Lab 7 H6 posted a bit late Quiz 2 Review Session • Review Session Sunday • 5:00 – 7:00 • Learned 1131 • Pizza at 6:00 Live Assignments H6 P3
Last TimeLecture 14 – Scope Semantics • Program meaning Scope • Name analysis Semantics
Today’s OutlineLecture 15 – Semantic Analysis Error Checking • What counts as a bad program? • How do we detect bad programs? Limits of Analysis • The halting problem Semantics
Error Checking Semantic Analysis Goal: save programmers from themselves • It’s not enough to compile the programmer’s code • Need to figure out what programmer meant to code
A Compiler: Artist’s RenditionSemantic Analysis A grumpy old man that yells at you for breaking the rules
Quick Audience PollLecture 15 – Semantic Analysis Does this C program compile? Should this C code compile? int a = 0; int main(){ if (false){ b = 6; } return a;}
A Compiler’s Error-Checking ObligationLecture 15 – Semantic Analysis Understandability / Consistency
Bug HuntingSemantic Analysis How do we prevent nonsense code from executing? • We’ll consider two ways of analysis: • Static • Dynamic Putting guardrails on computation
Compiler PerspectiveStatic vs Dynamic Analysis Static • Code analysis without execution Dynamic • Code analysis through execution Checks done at compile time Analysis part of the compiler itself Checks done at run time Analysis embedded into the program
Compiler Focus: Static AnalysisStatic vs Dynamic Analysis Doesn’t slow the program down • Ok to take longer • Ok to apply more heavyweight analysis Has a “holistic” view of the program • Has access to source code • Knowledge of non-executed program paths
Limits of Error CheckingStatic Analysis We’d LOVE to ensure bug-free programs • Observe and report bugs before they are encountered Usually we can’t do this • Limits of static analysis
Limits of Static AnalysisStatic Analysis Theoretical argument Practical argument
The Halting ProblemStatic Analysis Does a computation ever terminate?
Let’s Do Some VocabStatic Analysis Effective method: a procedure that is always yields a correct result on any input
No Effective Method for HaltingStatic Analysis assume white_magic(Function p) returns true if p halts, false if p does not function black_magic(): if white_magic(black_magic): while true { }
Rice’s TheoremStatic Analysis - Limits of Error Checking “All non-trivial semantic properties of programs are undecidable”
Rice’s Theorem - ImplicationsDetour – Limits of Error Checking • We’d like to perfectly capture all bugs • We can’t be right all of the time • We can choose HOW we are wrong
Limits of Static AnalysisStatic Analysis Theoretical argument Practical argument
Evaluating a Bug DetectorEvaluation False True Positive Negative
Soundness and CompletenessDetour – Limits of Error Checking All Programs All Programs Reported bugs Buggy Programs Buggy Programs Reported bugs Complete bug detection Sound bug detection
Partial CorrectnessLimits of Error Checking • Make best-effort procedures that are neither sound nor complete • We can analyze the result of a statement under certain assumptions • Assume that the statement is executed • Assume that the statement actually completes