Bug Summary

File:lib/CodeGen/SafeStackLayout.cpp
Warning:line 106, column 5
Value stored to 'LastRegionEnd' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SafeStackLayout.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/lib/CodeGen -I /build/llvm-toolchain-snapshot-7~svn338205/lib/CodeGen -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn338205/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/lib/CodeGen -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-29-043837-17923-1 -x c++ /build/llvm-toolchain-snapshot-7~svn338205/lib/CodeGen/SafeStackLayout.cpp -faddrsig
1//===- SafeStackLayout.cpp - SafeStack frame layout -----------------------===//
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#include "SafeStackLayout.h"
11#include "SafeStackColoring.h"
12#include "llvm/IR/Value.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/Compiler.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/MathExtras.h"
17#include "llvm/Support/raw_ostream.h"
18#include <algorithm>
19#include <cassert>
20
21using namespace llvm;
22using namespace llvm::safestack;
23
24#define DEBUG_TYPE"safestacklayout" "safestacklayout"
25
26static cl::opt<bool> ClLayout("safe-stack-layout",
27 cl::desc("enable safe stack layout"), cl::Hidden,
28 cl::init(true));
29
30LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void StackLayout::print(raw_ostream &OS) {
31 OS << "Stack regions:\n";
32 for (unsigned i = 0; i < Regions.size(); ++i) {
33 OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
34 << "), range " << Regions[i].Range << "\n";
35 }
36 OS << "Stack objects:\n";
37 for (auto &IT : ObjectOffsets) {
38 OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
39 }
40}
41
42void StackLayout::addObject(const Value *V, unsigned Size, unsigned Alignment,
43 const StackColoring::LiveRange &Range) {
44 StackObjects.push_back({V, Size, Alignment, Range});
45 ObjectAlignments[V] = Alignment;
46 MaxAlignment = std::max(MaxAlignment, Alignment);
47}
48
49static unsigned AdjustStackOffset(unsigned Offset, unsigned Size,
50 unsigned Alignment) {
51 return alignTo(Offset + Size, Alignment) - Size;
52}
53
54void StackLayout::layoutObject(StackObject &Obj) {
55 if (!ClLayout) {
56 // If layout is disabled, just grab the next aligned address.
57 // This effectively disables stack coloring as well.
58 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
59 unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment);
60 unsigned End = Start + Obj.Size;
61 Regions.emplace_back(Start, End, Obj.Range);
62 ObjectOffsets[Obj.Handle] = End;
63 return;
64 }
65
66 LLVM_DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << "Layout: size " <<
Obj.Size << ", align " << Obj.Alignment <<
", range " << Obj.Range << "\n"; } } while (false
)
67 << Obj.Alignment << ", range " << Obj.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << "Layout: size " <<
Obj.Size << ", align " << Obj.Alignment <<
", range " << Obj.Range << "\n"; } } while (false
)
;
68 assert(Obj.Alignment <= MaxAlignment)(static_cast <bool> (Obj.Alignment <= MaxAlignment) ?
void (0) : __assert_fail ("Obj.Alignment <= MaxAlignment"
, "/build/llvm-toolchain-snapshot-7~svn338205/lib/CodeGen/SafeStackLayout.cpp"
, 68, __extension__ __PRETTY_FUNCTION__))
;
69 unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment);
70 unsigned End = Start + Obj.Size;
71 LLVM_DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " First candidate: " <<
Start << " .. " << End << "\n"; } } while (
false)
;
72 for (const StackRegion &R : Regions) {
73 LLVM_DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.Enddo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Examining region: "
<< R.Start << " .. " << R.End << ", range "
<< R.Range << "\n"; } } while (false)
74 << ", range " << R.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Examining region: "
<< R.Start << " .. " << R.End << ", range "
<< R.Range << "\n"; } } while (false)
;
75 assert(End >= R.Start)(static_cast <bool> (End >= R.Start) ? void (0) : __assert_fail
("End >= R.Start", "/build/llvm-toolchain-snapshot-7~svn338205/lib/CodeGen/SafeStackLayout.cpp"
, 75, __extension__ __PRETTY_FUNCTION__))
;
76 if (Start >= R.End) {
77 LLVM_DEBUG(dbgs() << " Does not intersect, skip.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Does not intersect, skip.\n"
; } } while (false)
;
78 continue;
79 }
80 if (Obj.Range.Overlaps(R.Range)) {
81 // Find the next appropriate location.
82 Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment);
83 End = Start + Obj.Size;
84 LLVM_DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Overlaps. Next candidate: "
<< Start << " .. " << End << "\n"; }
} while (false)
85 << End << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Overlaps. Next candidate: "
<< Start << " .. " << End << "\n"; }
} while (false)
;
86 continue;
87 }
88 if (End <= R.End) {
89 LLVM_DEBUG(dbgs() << " Reusing region(s).\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Reusing region(s).\n"
; } } while (false)
;
90 break;
91 }
92 }
93
94 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
95 if (End > LastRegionEnd) {
96 // Insert a new region at the end. Maybe two.
97 if (Start > LastRegionEnd) {
98 LLVM_DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating gap region: "
<< LastRegionEnd << " .. " << Start <<
"\n"; } } while (false)
99 << Start << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating gap region: "
<< LastRegionEnd << " .. " << Start <<
"\n"; } } while (false)
;
100 Regions.emplace_back(LastRegionEnd, Start, StackColoring::LiveRange());
101 LastRegionEnd = Start;
102 }
103 LLVM_DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating new region: "
<< LastRegionEnd << " .. " << End <<
", range " << Obj.Range << "\n"; } } while (false
)
104 << End << ", range " << Obj.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating new region: "
<< LastRegionEnd << " .. " << End <<
", range " << Obj.Range << "\n"; } } while (false
)
;
105 Regions.emplace_back(LastRegionEnd, End, Obj.Range);
106 LastRegionEnd = End;
Value stored to 'LastRegionEnd' is never read
107 }
108
109 // Split starting and ending regions if necessary.
110 for (unsigned i = 0; i < Regions.size(); ++i) {
111 StackRegion &R = Regions[i];
112 if (Start > R.Start && Start < R.End) {
113 StackRegion R0 = R;
114 R.Start = R0.End = Start;
115 Regions.insert(&R, R0);
116 continue;
117 }
118 if (End > R.Start && End < R.End) {
119 StackRegion R0 = R;
120 R0.End = R.Start = End;
121 Regions.insert(&R, R0);
122 break;
123 }
124 }
125
126 // Update live ranges for all affected regions.
127 for (StackRegion &R : Regions) {
128 if (Start < R.End && End > R.Start)
129 R.Range.Join(Obj.Range);
130 if (End <= R.End)
131 break;
132 }
133
134 ObjectOffsets[Obj.Handle] = End;
135}
136
137void StackLayout::computeLayout() {
138 // Simple greedy algorithm.
139 // If this is replaced with something smarter, it must preserve the property
140 // that the first object is always at the offset 0 in the stack frame (for
141 // StackProtectorSlot), or handle stack protector in some other way.
142
143 // Sort objects by size (largest first) to reduce fragmentation.
144 if (StackObjects.size() > 2)
145 std::stable_sort(StackObjects.begin() + 1, StackObjects.end(),
146 [](const StackObject &a, const StackObject &b) {
147 return a.Size > b.Size;
148 });
149
150 for (auto &Obj : StackObjects)
151 layoutObject(Obj);
152
153 LLVM_DEBUG(print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { print(dbgs()); } } while (false)
;
154}