Skip to content

[IR] Add overflow check in AllocaInst::getAllocationSize #97170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/IR/Value.h"
#include "llvm/Support/AtomicOrdering.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CheckedArithmetic.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ModRef.h"
Expand Down Expand Up @@ -65,17 +66,25 @@ AllocaInst::getAllocationSize(const DataLayout &DL) const {
if (!C)
return std::nullopt;
assert(!Size.isScalable() && "Array elements cannot have a scalable size");
Size *= C->getZExtValue();
auto CheckedProd =
checkedMulUnsigned(Size.getKnownMinValue(), C->getZExtValue());
if (!CheckedProd)
return std::nullopt;
return TypeSize::getFixed(*CheckedProd);
}
return Size;
}

std::optional<TypeSize>
AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
std::optional<TypeSize> Size = getAllocationSize(DL);
if (Size)
return *Size * 8;
return std::nullopt;
if (!Size)
return std::nullopt;
auto CheckedProd = checkedMulUnsigned(Size->getKnownMinValue(),
static_cast<TypeSize::ScalarTy>(8));
if (!CheckedProd)
return std::nullopt;
return TypeSize::get(*CheckedProd, Size->isScalable());
}

//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions llvm/unittests/IR/InstructionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,7 @@ TEST(InstructionsTest, AllocaInst) {
%F = alloca [2 x half]
%G = alloca [2 x [3 x i128]]
%H = alloca %T
%I = alloca i32, i64 9223372036854775807
ret void
}
)");
Expand All @@ -1766,6 +1767,7 @@ TEST(InstructionsTest, AllocaInst) {
AllocaInst &F = cast<AllocaInst>(*It++);
AllocaInst &G = cast<AllocaInst>(*It++);
AllocaInst &H = cast<AllocaInst>(*It++);
AllocaInst &I = cast<AllocaInst>(*It++);
EXPECT_EQ(A.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
EXPECT_EQ(B.getAllocationSizeInBits(DL), TypeSize::getFixed(128));
EXPECT_FALSE(C.getAllocationSizeInBits(DL));
Expand All @@ -1774,6 +1776,7 @@ TEST(InstructionsTest, AllocaInst) {
EXPECT_EQ(F.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
EXPECT_EQ(G.getAllocationSizeInBits(DL), TypeSize::getFixed(768));
EXPECT_EQ(H.getAllocationSizeInBits(DL), TypeSize::getFixed(160));
EXPECT_FALSE(I.getAllocationSizeInBits(DL));
}

TEST(InstructionsTest, InsertAtBegin) {
Expand Down
Loading