Skip to content

Commit 4e40142

Browse files
blendinswiftshader-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[subzero] Fix integer overflows during alloca coalescing
Bug: chromium:1427865,chromium:1431761,chromium:1464038,chromium:1464680 Change-Id: Ie09a9ba3709d867544ca045b066b437e2d60da51 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/71928 Kokoro-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@google.com> Presubmit-Ready: Shahbaz Youssefi <syoussefi@google.com> Reviewed-by: Ben Clayton <bclayton@google.com> Tested-by: Shahbaz Youssefi <syoussefi@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@google.com>
1 parent 151fa79 commit 4e40142

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

third_party/subzero/src/IceCfg.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,16 @@ void Cfg::sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas,
837837
uint32_t Alignment = std::max(Alloca->getAlignInBytes(), 1u);
838838
auto *ConstSize =
839839
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+
841850
if (BaseVariableType == BVT_FramePointer) {
842851
// Addressing is relative to the frame pointer. Subtract the offset after
843852
// adding the size of the alloca, because it grows downwards from the
@@ -855,6 +864,14 @@ void Cfg::sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas,
855864
: 0;
856865
Offsets.push_back(CurrentOffset + OutArgsOffsetOrZero);
857866
}
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+
858875
// Update the running offset of the fused alloca region.
859876
CurrentOffset += Size;
860877
}

0 commit comments

Comments
 (0)