Home | History | Annotate | Download | only in Utils
      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 static const int kAsanStackUseAfterReturnMagic = 0xf5;
     28 static const int kAsanStackUseAfterScopeMagic = 0xf8;
     29 
     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   size_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 
     44 // Output data struct for ComputeASanStackFrameLayout.
     45 struct ASanStackFrameLayout {
     46   size_t Granularity;     // Shadow granularity.
     47   size_t FrameAlignment;  // Alignment for the entire frame.
     48   size_t FrameSize;       // Size of the frame in bytes.
     49 };
     50 
     51 ASanStackFrameLayout ComputeASanStackFrameLayout(
     52     // The array of stack variables. The elements may get reordered and changed.
     53     SmallVectorImpl<ASanStackVariableDescription> &Vars,
     54     // AddressSanitizer's shadow granularity. Usually 8, may also be 16, 32, 64.
     55     size_t Granularity,
     56     // The minimal size of the left-most redzone (header).
     57     // At least 4 pointer sizes, power of 2, and >= Granularity.
     58     // The resulting FrameSize should be multiple of MinHeaderSize.
     59     size_t MinHeaderSize);
     60 
     61 // Compute frame description, see DescribeAddressIfStack in ASan runtime.
     62 SmallString<64> ComputeASanStackFrameDescription(
     63     const SmallVectorImpl<ASanStackVariableDescription> &Vars);
     64 
     65 // Returns shadow bytes with marked red zones. This shadow represents the state
     66 // if the stack frame when all local variables are inside of the own scope.
     67 SmallVector<uint8_t, 64>
     68 GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
     69                const ASanStackFrameLayout &Layout);
     70 
     71 // Returns shadow bytes with marked red zones and after scope. This shadow
     72 // represents the state if the stack frame when all local variables are outside
     73 // of the own scope.
     74 SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
     75     // The array of stack variables. The elements may get reordered and changed.
     76     const SmallVectorImpl<ASanStackVariableDescription> &Vars,
     77     const ASanStackFrameLayout &Layout);
     78 
     79 } // llvm namespace
     80 
     81 #endif  // LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
     82