• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ASanStackFrameLayout.h - ComputeASanStackFrameLayout -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines ComputeASanStackFrameLayout and auxiliary data structs.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
14 #define LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 
18 namespace llvm {
19 
20 class AllocaInst;
21 
22 // These magic constants should be the same as in
23 // in asan_internal.h from ASan runtime in compiler-rt.
24 static const int kAsanStackLeftRedzoneMagic = 0xf1;
25 static const int kAsanStackMidRedzoneMagic = 0xf2;
26 static const int kAsanStackRightRedzoneMagic = 0xf3;
27 
28 // Input/output data struct for ComputeASanStackFrameLayout.
29 struct ASanStackVariableDescription {
30   const char *Name;  // Name of the variable that will be displayed by asan
31                      // if a stack-related bug is reported.
32   uint64_t Size;     // Size of the variable in bytes.
33   size_t Alignment;  // Alignment of the variable (power of 2).
34   AllocaInst *AI;    // The actual AllocaInst.
35   size_t Offset;     // Offset from the beginning of the frame;
36                      // set by ComputeASanStackFrameLayout.
37 };
38 
39 // Output data struct for ComputeASanStackFrameLayout.
40 struct ASanStackFrameLayout {
41   // Frame description, see DescribeAddressIfStack in ASan runtime.
42   SmallString<64> DescriptionString;
43   // The contents of the shadow memory for the stack frame that we need
44   // to set at function entry.
45   SmallVector<uint8_t, 64> ShadowBytes;
46   size_t FrameAlignment;  // Alignment for the entire frame.
47   size_t FrameSize;       // Size of the frame in bytes.
48 };
49 
50 void ComputeASanStackFrameLayout(
51     // The array of stack variables. The elements may get reordered and changed.
52     SmallVectorImpl<ASanStackVariableDescription> &Vars,
53     // AddressSanitizer's shadow granularity. Usually 8, may also be 16, 32, 64.
54     size_t Granularity,
55     // The minimal size of the left-most redzone (header).
56     // At least 4 pointer sizes, power of 2, and >= Granularity.
57     // The resulting FrameSize should be multiple of MinHeaderSize.
58     size_t MinHeaderSize,
59     // The result is put here.
60     ASanStackFrameLayout *Layout);
61 
62 } // llvm namespace
63 
64 #endif  // LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
65