Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/ASanStackFrameLayout.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
10
//
11
//===----------------------------------------------------------------------===//
12
#include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
13
#include "llvm/ADT/SmallString.h"
14
#include "llvm/IR/DebugInfo.h"
15
#include "llvm/Support/MathExtras.h"
16
#include "llvm/Support/ScopedPrinter.h"
17
#include "llvm/Support/raw_ostream.h"
18
#include <algorithm>
19
20
namespace llvm {
21
22
// We sort the stack variables by alignment (largest first) to minimize
23
// unnecessary large gaps due to alignment.
24
// It is tempting to also sort variables by size so that larger variables
25
// have larger redzones at both ends. But reordering will make report analysis
26
// harder, especially when temporary unnamed variables are present.
27
// So, until we can provide more information (type, line number, etc)
28
// for the stack variables we avoid reordering them too much.
29
static inline bool CompareVars(const ASanStackVariableDescription &a,
30
73
                               const ASanStackVariableDescription &b) {
31
73
  return a.Alignment > b.Alignment;
32
73
}
33
34
// We also force minimal alignment for all vars to kMinAlignment so that vars
35
// with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
36
static const size_t kMinAlignment = 16;
37
38
// We want to add a full redzone after every variable.
39
// The larger the variable Size the larger is the redzone.
40
// The resulting frame size is a multiple of Alignment.
41
static size_t VarAndRedzoneSize(size_t Size, size_t Granularity,
42
186
                                size_t Alignment) {
43
186
  size_t Res = 0;
44
186
  if (Size <= 4)  
Res = 1681
;
45
105
  else if (Size <= 16) 
Res = 3242
;
46
63
  else if (Size <= 128) 
Res = Size + 3252
;
47
11
  else if (Size <= 512) 
Res = Size + 643
;
48
8
  else if (Size <= 4096) Res = Size + 128;
49
0
  else                   Res = Size + 256;
50
186
  return alignTo(std::max(Res, 2 * Granularity), Alignment);
51
186
}
52
53
ASanStackFrameLayout
54
ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
55
115
                            size_t Granularity, size_t MinHeaderSize) {
56
115
  assert(Granularity >= 8 && Granularity <= 64 &&
57
115
         (Granularity & (Granularity - 1)) == 0);
58
115
  assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
59
115
         MinHeaderSize >= Granularity);
60
115
  const size_t NumVars = Vars.size();
61
115
  assert(NumVars > 0);
62
301
  for (size_t i = 0; i < NumVars; 
i++186
)
63
186
    Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
64
115
65
115
  llvm::stable_sort(Vars, CompareVars);
66
115
67
115
  ASanStackFrameLayout Layout;
68
115
  Layout.Granularity = Granularity;
69
115
  Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
70
115
  size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
71
115
     Vars[0].Alignment);
72
115
  assert((Offset % Granularity) == 0);
73
301
  for (size_t i = 0; i < NumVars; 
i++186
) {
74
186
    bool IsLast = i == NumVars - 1;
75
186
    size_t Alignment = std::max(Granularity, Vars[i].Alignment);
76
186
    (void)Alignment;  // Used only in asserts.
77
186
    size_t Size = Vars[i].Size;
78
186
    assert((Alignment & (Alignment - 1)) == 0);
79
186
    assert(Layout.FrameAlignment >= Alignment);
80
186
    assert((Offset % Alignment) == 0);
81
186
    assert(Size > 0);
82
186
    size_t NextAlignment = IsLast ? 
Granularity115
83
186
                   : 
std::max(Granularity, Vars[i + 1].Alignment)71
;
84
186
    size_t SizeWithRedzone = VarAndRedzoneSize(Size, Granularity,
85
186
                                               NextAlignment);
86
186
    Vars[i].Offset = Offset;
87
186
    Offset += SizeWithRedzone;
88
186
  }
89
115
  if (Offset % MinHeaderSize) {
90
45
    Offset += MinHeaderSize - (Offset % MinHeaderSize);
91
45
  }
92
115
  Layout.FrameSize = Offset;
93
115
  assert((Layout.FrameSize % MinHeaderSize) == 0);
94
115
  return Layout;
95
115
}
96
97
SmallString<64> ComputeASanStackFrameDescription(
98
115
    const SmallVectorImpl<ASanStackVariableDescription> &Vars) {
99
115
  SmallString<2048> StackDescriptionStorage;
100
115
  raw_svector_ostream StackDescription(StackDescriptionStorage);
101
115
  StackDescription << Vars.size();
102
115
103
186
  for (const auto &Var : Vars) {
104
186
    std::string Name = Var.Name;
105
186
    if (Var.Line) {
106
12
      Name += ":";
107
12
      Name += to_string(Var.Line);
108
12
    }
109
186
    StackDescription << " " << Var.Offset << " " << Var.Size << " "
110
186
                     << Name.size() << " " << Name;
111
186
  }
112
115
  return StackDescription.str();
113
115
}
114
115
SmallVector<uint8_t, 64>
116
GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
117
159
               const ASanStackFrameLayout &Layout) {
118
159
  assert(Vars.size() > 0);
119
159
  SmallVector<uint8_t, 64> SB;
120
159
  SB.clear();
121
159
  const size_t Granularity = Layout.Granularity;
122
159
  SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
123
247
  for (const auto &Var : Vars) {
124
247
    SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
125
247
126
247
    SB.resize(SB.size() + Var.Size / Granularity, 0);
127
247
    if (Var.Size % Granularity)
128
189
      SB.push_back(Var.Size % Granularity);
129
247
  }
130
159
  SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
131
159
  return SB;
132
159
}
133
134
SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
135
    const SmallVectorImpl<ASanStackVariableDescription> &Vars,
136
115
    const ASanStackFrameLayout &Layout) {
137
115
  SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
138
115
  const size_t Granularity = Layout.Granularity;
139
115
140
186
  for (const auto &Var : Vars) {
141
186
    assert(Var.LifetimeSize <= Var.Size);
142
186
    const size_t LifetimeShadowSize =
143
186
        (Var.LifetimeSize + Granularity - 1) / Granularity;
144
186
    const size_t Offset = Var.Offset / Granularity;
145
186
    std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
146
186
              kAsanStackUseAfterScopeMagic);
147
186
  }
148
115
149
115
  return SB;
150
115
}
151
152
} // llvm namespace