ASanStackFrameLayout.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASanStackFrameLayout.h - ComputeASanStackFrameLayout -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This header defines ComputeASanStackFrameLayout and auxiliary data structs.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
  18. #define LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. namespace llvm {
  22. class AllocaInst;
  23. // These magic constants should be the same as in
  24. // in asan_internal.h from ASan runtime in compiler-rt.
  25. static const int kAsanStackLeftRedzoneMagic = 0xf1;
  26. static const int kAsanStackMidRedzoneMagic = 0xf2;
  27. static const int kAsanStackRightRedzoneMagic = 0xf3;
  28. static const int kAsanStackUseAfterReturnMagic = 0xf5;
  29. static const int kAsanStackUseAfterScopeMagic = 0xf8;
  30. // Input/output data struct for ComputeASanStackFrameLayout.
  31. struct ASanStackVariableDescription {
  32. const char *Name; // Name of the variable that will be displayed by asan
  33. // if a stack-related bug is reported.
  34. uint64_t Size; // Size of the variable in bytes.
  35. size_t LifetimeSize; // Size in bytes to use for lifetime analysis check.
  36. // Will be rounded up to Granularity.
  37. uint64_t Alignment; // Alignment of the variable (power of 2).
  38. AllocaInst *AI; // The actual AllocaInst.
  39. size_t Offset; // Offset from the beginning of the frame;
  40. // set by ComputeASanStackFrameLayout.
  41. unsigned Line; // Line number.
  42. };
  43. // Output data struct for ComputeASanStackFrameLayout.
  44. struct ASanStackFrameLayout {
  45. uint64_t Granularity; // Shadow granularity.
  46. uint64_t FrameAlignment; // Alignment for the entire frame.
  47. uint64_t FrameSize; // Size of the frame in bytes.
  48. };
  49. ASanStackFrameLayout ComputeASanStackFrameLayout(
  50. // The array of stack variables. The elements may get reordered and changed.
  51. SmallVectorImpl<ASanStackVariableDescription> &Vars,
  52. // AddressSanitizer's shadow granularity. Usually 8, may also be 16, 32, 64.
  53. uint64_t Granularity,
  54. // The minimal size of the left-most redzone (header).
  55. // At least 4 pointer sizes, power of 2, and >= Granularity.
  56. // The resulting FrameSize should be multiple of MinHeaderSize.
  57. uint64_t MinHeaderSize);
  58. // Compute frame description, see DescribeAddressIfStack in ASan runtime.
  59. SmallString<64> ComputeASanStackFrameDescription(
  60. const SmallVectorImpl<ASanStackVariableDescription> &Vars);
  61. // Returns shadow bytes with marked red zones. This shadow represents the state
  62. // if the stack frame when all local variables are inside of the own scope.
  63. SmallVector<uint8_t, 64>
  64. GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
  65. const ASanStackFrameLayout &Layout);
  66. // Returns shadow bytes with marked red zones and after scope. This shadow
  67. // represents the state if the stack frame when all local variables are outside
  68. // of the own scope.
  69. SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
  70. // The array of stack variables. The elements may get reordered and changed.
  71. const SmallVectorImpl<ASanStackVariableDescription> &Vars,
  72. const ASanStackFrameLayout &Layout);
  73. } // llvm namespace
  74. #endif // LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
  75. #ifdef __GNUC__
  76. #pragma GCC diagnostic pop
  77. #endif