StackSafetyAnalysis.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ==================================
  2. Stack Safety Analysis
  3. ==================================
  4. Introduction
  5. ============
  6. The Stack Safety Analysis determines if stack allocated variables can be
  7. considered 'safe' from memory access bugs.
  8. The primary purpose of the analysis is to be used by sanitizers to avoid
  9. unnecessary instrumentation of 'safe' variables. SafeStack is going to be the
  10. first user.
  11. 'safe' variables can be defined as variables that can not be used out-of-scope
  12. (e.g. use-after-return) or accessed out of bounds. In the future it can be
  13. extended to track other variable properties. E.g. we plan to extend
  14. implementation with a check to make sure that variable is always initialized
  15. before every read to optimize use-of-uninitialized-memory checks.
  16. How it works
  17. ============
  18. The analysis is implemented in two stages:
  19. The intra-procedural, or 'local', stage performs a depth-first search inside
  20. functions to collect all uses of each alloca, including loads/stores and uses as
  21. arguments functions. After this stage we know which parts of the alloca are used
  22. by functions itself but we don't know what happens after it is passed as
  23. an argument to another function.
  24. The inter-procedural, or 'global', stage, resolves what happens to allocas after
  25. they are passed as function arguments. This stage performs a depth-first search
  26. on function calls inside a single module and propagates allocas usage through
  27. functions calls.
  28. When used with ThinLTO, the global stage performs a whole program analysis over
  29. the Module Summary Index.
  30. Testing
  31. =======
  32. The analysis is covered with lit tests.
  33. We expect that users can tolerate false classification of variables as
  34. 'unsafe' when in-fact it's 'safe'. This may lead to inefficient code. However, we
  35. can't accept false 'safe' classification which may cause sanitizers to miss actual
  36. bugs in instrumented code. To avoid that we want additional validation tool.
  37. AddressSanitizer may help with this validation. We can instrument all variables
  38. as usual but additionally store stack-safe information in the
  39. ``ASanStackVariableDescription``. Then if AddressSanitizer detects a bug on
  40. a 'safe' variable we can produce an additional report to let the user know that
  41. probably Stack Safety Analysis failed and we should check for a bug in the
  42. compiler.