File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -837,7 +837,16 @@ void Cfg::sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas,
837
837
uint32_t Alignment = std::max (Alloca->getAlignInBytes (), 1u );
838
838
auto *ConstSize =
839
839
llvm::dyn_cast<ConstantInteger32>(Alloca->getSizeInBytes ());
840
- uint32_t Size = Utils::applyAlignment (ConstSize->getValue (), Alignment);
840
+ const uint32_t Size =
841
+ Utils::applyAlignment (ConstSize->getValue (), Alignment);
842
+
843
+ // Ensure that the Size does not exceed StackSizeLimit which can lead to
844
+ // undefined behavior below.
845
+ if (Size > StackSizeLimit) {
846
+ llvm::report_fatal_error (" Local variable exceeds stack size limit" );
847
+ return ; // NOTREACHED
848
+ }
849
+
841
850
if (BaseVariableType == BVT_FramePointer) {
842
851
// Addressing is relative to the frame pointer. Subtract the offset after
843
852
// adding the size of the alloca, because it grows downwards from the
@@ -855,6 +864,14 @@ void Cfg::sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas,
855
864
: 0 ;
856
865
Offsets.push_back (CurrentOffset + OutArgsOffsetOrZero);
857
866
}
867
+
868
+ // Ensure that the addition below does not overflow or exceed
869
+ // StackSizeLimit as this leads to undefined behavior.
870
+ if (CurrentOffset + Size > StackSizeLimit) {
871
+ llvm::report_fatal_error (" Local variable exceeds stack size limit" );
872
+ return ; // NOTREACHED
873
+ }
874
+
858
875
// Update the running offset of the fused alloca region.
859
876
CurrentOffset += Size;
860
877
}
You can’t perform that action at this time.
0 commit comments