Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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