learning to find bugs
play

Learning to Find Bugs (Work in progress) Michael Pradel TU - PowerPoint PPT Presentation

Learning to Find Bugs (Work in progress) Michael Pradel TU Darmstadt 1 Joint work with Koushik Sen and Rohan Bavishi Automated Bug Detection Hundreds of bug Thousands of bug detectors patterns One analysis for each Existing bug


  1. Learning to Find Bugs (Work in progress) Michael Pradel TU Darmstadt 1 Joint work with Koushik Sen and Rohan Bavishi

  2. Automated Bug Detection Hundreds of bug Thousands of bug detectors patterns � One analysis for each � Existing bug detectors bug pattern miss most bugs � E.g., Google’s Error Prone framework: 150+ different analyses 2

  3. Automated Bug Detection Hundreds of bug Thousands of bug detectors patterns � One analysis for each � Existing bug detectors bug pattern miss most bugs � E.g., Google’s Error Prone framework: 150+ different analyses Manually creating and tuning bug detectors doesn’t scale 2

  4. Learning to Find Bugs Train a model to identify instances of bug patterns: Buggy code Train machine Classifier learning model Correct code 3

  5. Learning to Find Bugs Train a model to identify instances of bug patterns: New code Buggy code Train machine Classifier learning model Correct code Buggy/Okay 3

  6. Learning to Find Bugs Train a model to identify instances of bug patterns: New code Buggy code Train machine Classifier learning model Correct code Buggy/Okay Problem of writing program analysis Problem of finding training examples 3

  7. Here: Name-based Bug Detection What’s wrong with this code? function setPoint(x, y) { ... } var x_dim = 23; var y_dim = 5; setPoint(y_dim , x_dim ); 4

  8. Here: Name-based Bug Detection What’s wrong with this code? function setPoint(x, y) { ... } var x_dim = 23; var y_dim = 5; setPoint(y_dim , x_dim ); Incorrect order of arguments 4

  9. Prior Work Name-based bug detection � Find unusual and likely incorrect arguments � Exploit similarities of identifier names 2011 First name-based bug detector [ISSTA’11] � Finds incorrectly ordered, equally typed arguments � Compares call sites of same method 5

  10. Prior Work Name-based bug detection � Find unusual and likely incorrect arguments � Exploit similarities of identifier names 2013 Improved analysis [TSE’13] � Improved precision � Effective for multiple languages (Java, C, C++) 5

  11. Prior Work Name-based bug detection � Find unusual and likely incorrect arguments � Exploit similarities of identifier names 2016 Generalized analysis [ICSE’16] � Apply to arbitrary arguments � Heuristic pruning of false positives 5

  12. Prior Work Name-based bug detection � Find unusual and likely incorrect arguments � Exploit similarities of identifier names 2017 Adopted by Google [OOPSLA’17] � Default check in Error Prone framework � Found 2000+ new bugs 5

  13. Problem Solved? Various hand-tuned heuristics � Detect more bugs � Special check for assertEquals calls � Reduce false positives � Hard-coded method names that suggest that swapping is intended, e.g., transpose 6

  14. Problem Solved? Various hand-tuned heuristics � Detect more bugs � Special check for assertEquals calls � Reduce false positives � Hard-coded method names that suggest that swapping is intended, e.g., transpose Goal: Replace hand-tuned analysis with trained machine learning model 6

  15. This Work: Overview Code corpus Create Learn representation training data of identifiers Train model that Bug detector identifies bugs 7

  16. Creating Training Data Program transformation that seeds bugs For swapped arguments: � Visit every function call with ≥ 2 arguments � Positive example: Original order of arguments � Negative example: Swap first two arguments setPoint(x, y); setPoint(y, x); 8

  17. Representing Identifiers How to reason about identifier names? Prior work: Lexical similarity � x similar to x dim Want: Semantic similarity � x similar to width � list similar to seq 9

  18. Background: Word Embeddings Word embeddings in NLP � Continuous vector representation for each word � Similar words have similar vectors Word2Vec: Learn from corpus of text � ”You shall know a word by the company it keeps” � Context: Surrounding words in sentences 10

  19. AST Context What’s the context of an identifier? Our approach: AST-based context � Surrounding nodes: Parent, grandparent, siblings, etc. � Extract node types, node contents, and relative positioning 11

  20. AST Context: Example window.setTimeout(callback , 1000); CallExpr MemberExpr Arguments Identifier Identifier Identifier Literal window setTimeout callBack 1000 12

  21. AST Context: Example window.setTimeout(callback , 1000); CallExpr MemberExpr Arguments Identifier Identifier Identifier Literal window setTimeout callBack 1000 12

  22. Learning Embeddings � Train neural network to predict context from identifier � Use hidden layer as representation for identifier Input layer: Hidden Output layer: Identifier layer Context 13

  23. Learning Embeddings � Train neural network to predict context from identifier � Use hidden layer as representation for identifier Input layer: Hidden Output layer: Identifier layer Context One-hot vectors Embedding vector 13

  24. Training the Bug Detector � Given: Embeddings of callee and two arguments � Train neural network: Predict whether correct or wrong Callee + Probability Arg. 1 that correct + Arg. 2 Two hidden layers 14

  25. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values � Incorrect binary operators � Swapped operands of binary operations 15

  26. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values var callback = function () { .. } � Incorrect binary operators � Swapped operands of binary operations 15

  27. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values "abc" var callback = function () { .. } � Incorrect binary operators � Swapped operands of binary operations 15

  28. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values "abc" var callback = function () { .. } � Incorrect binary operators if (x == undefined) ... � Swapped operands of binary operations 15

  29. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values "abc" var callback = function () { .. } � Incorrect binary operators > if (x == undefined) ... � Swapped operands of binary operations 15

  30. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values "abc" var callback = function () { .. } � Incorrect binary operators > if (x == undefined) ... � Swapped operands of binary operations bytes[i + 1] >> 4 15

  31. Beyond Swapped Arguments Same idea works for other bug patterns � Assignments of incorrect values "abc" var callback = function () { .. } � Incorrect binary operators > if (x == undefined) ... � Swapped operands of binary operations 4 >> bytes[i + 1] bytes[i + 1] >> 4 15

  32. Evaluation: Setup � 100.000 JavaScript files from various projects � 80.000 for training � 20.000 for validation � 68 million lines of code � 37.3 million occurrences of identifiers � 10.1 million occurrences of literals 16

  33. Examples of Bugs // Callback must come before the // number of milliseconds to wait setTimeout (50, dojo.lang.hitch(this , function (){ ... })); // First argument must be smaller than // the second argument array.slice(3, 0); 17

  34. Precision and Recall AST embedding Swapped arguments 1 0.98 Precision 0.96 0.94 0.92 0.9 0 0.2 0.4 0.6 0.8 1 Recall 18

  35. Precision and Recall AST embedding Random embedding Swapped arguments 1 0.98 Precision 0.96 0.94 0.92 0.9 0 0.2 0.4 0.6 0.8 1 Recall 18

  36. Precision and Recall AST embedding Random embedding Wrong operator in binary operations 1 0.9 Precision 0.8 0.7 0.6 0.5 0 0.2 0.4 0.6 0.8 1 Recall 18

  37. Open Challenges Better representation of identifiers � Same name � Same meaning Ensure that seeded bugs are realistic � Learn bug patterns from version histories? Generalize to more bug patterns � Train a model per bug pattern 19

  38. Conclusion Replace manually written program analyses with trained machine learning models Buggy code Train machine Classifier learning model Correct code Precision and recall match or exceed manually written analyses 20

Recommend


More recommend